diff options
author | Zhenhua Zhang <zhenhua.zhang@intel.com> | 2010-01-15 09:15:38 +0800 |
---|---|---|
committer | Denis Kenzior <denkenz@gmail.com> | 2010-01-20 14:13:57 -0600 |
commit | 7145edd3a416b133da1a208292b919256c822baf (patch) | |
tree | b82a7390f207785b950889869782b1cdbcf1a60a /gatchat/gatutil.c | |
parent | e82972722418407737c68631a78bb3c96e6f7d55 (diff) | |
download | ofono-7145edd3a416b133da1a208292b919256c822baf.tar.gz |
Add gatutil.c to share common APIs with GAtServer
Add gatutil.c/h gat.h and move shared typedef and APIs into it.
So that they can be shared by GAtServer and GAtChat.
Diffstat (limited to 'gatchat/gatutil.c')
-rw-r--r-- | gatchat/gatutil.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/gatchat/gatutil.c b/gatchat/gatutil.c new file mode 100644 index 00000000..6bc5dcbb --- /dev/null +++ b/gatchat/gatutil.c @@ -0,0 +1,129 @@ +/* + * + * AT chat library with GLib integration + * + * Copyright (C) 2008-2010 Intel Corporation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <string.h> +#include <ctype.h> + +#include <glib.h> + +#include "gatutil.h" + +void g_at_util_debug_chat(GAtDebugFunc debugf, gboolean in, const char *str, + gsize len, gpointer user_data) +{ + char type = in ? '<' : '>'; + gsize escaped = 2; /* Enough for '<', ' ' */ + char *escaped_str; + const char *esc = "<ESC>"; + gsize esc_size = strlen(esc); + const char *ctrlz = "<CtrlZ>"; + gsize ctrlz_size = strlen(ctrlz); + gsize i; + + if (!debugf || !len) + return; + + for (i = 0; i < len; i++) { + char c = str[i]; + + if (isprint(c)) + escaped += 1; + else if (c == '\r' || c == '\t' || c == '\n') + escaped += 2; + else if (c == 26) + escaped += ctrlz_size; + else if (c == 25) + escaped += esc_size; + else + escaped += 4; + } + + escaped_str = g_malloc(escaped + 1); + escaped_str[0] = type; + escaped_str[1] = ' '; + escaped_str[2] = '\0'; + escaped_str[escaped] = '\0'; + + for (escaped = 2, i = 0; i < len; i++) { + char c = str[i]; + + switch (c) { + case '\r': + escaped_str[escaped++] = '\\'; + escaped_str[escaped++] = 'r'; + break; + case '\t': + escaped_str[escaped++] = '\\'; + escaped_str[escaped++] = 't'; + break; + case '\n': + escaped_str[escaped++] = '\\'; + escaped_str[escaped++] = 'n'; + break; + case 26: + strncpy(&escaped_str[escaped], ctrlz, ctrlz_size); + escaped += ctrlz_size; + break; + case 25: + strncpy(&escaped_str[escaped], esc, esc_size); + escaped += esc_size; + break; + default: + if (isprint(c)) + escaped_str[escaped++] = c; + else { + escaped_str[escaped++] = '\\'; + escaped_str[escaped++] = '0' + ((c >> 6) & 07); + escaped_str[escaped++] = '0' + ((c >> 3) & 07); + escaped_str[escaped++] = '0' + (c & 07); + } + } + } + + debugf(escaped_str, user_data); + g_free(escaped_str); +} + +gboolean g_at_util_setup_io(GIOChannel *io) +{ + GIOFlags io_flags; + + if (g_io_channel_set_encoding(io, NULL, NULL) != + G_IO_STATUS_NORMAL) + return FALSE; + + io_flags = g_io_channel_get_flags(io); + + io_flags |= G_IO_FLAG_NONBLOCK; + + if (g_io_channel_set_flags(io, io_flags, NULL) != + G_IO_STATUS_NORMAL) + return FALSE; + + g_io_channel_set_close_on_unref(io, TRUE); + + return TRUE; +} + |