summaryrefslogtreecommitdiffstats
path: root/gatchat/gatchat.c
diff options
context:
space:
mode:
authorZhenhua Zhang <zhenhua.zhang@intel.com>2010-01-15 09:15:38 +0800
committerDenis Kenzior <denkenz@gmail.com>2010-01-20 14:13:57 -0600
commit7145edd3a416b133da1a208292b919256c822baf (patch)
treeb82a7390f207785b950889869782b1cdbcf1a60a /gatchat/gatchat.c
parente82972722418407737c68631a78bb3c96e6f7d55 (diff)
downloadofono-7145edd3a416b133da1a208292b919256c822baf.tar.bz2
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/gatchat.c')
-rw-r--r--gatchat/gatchat.c98
1 files changed, 6 insertions, 92 deletions
diff --git a/gatchat/gatchat.c b/gatchat/gatchat.c
index 7dfc4cb8..9d8d6406 100644
--- a/gatchat/gatchat.c
+++ b/gatchat/gatchat.c
@@ -40,7 +40,6 @@
static const char *none_prefix[] = { NULL };
static void g_at_chat_wakeup_writer(GAtChat *chat);
-static void debug_chat(GAtChat *chat, gboolean in, const char *str, gsize len);
struct at_command {
char *cmd;
@@ -686,81 +685,6 @@ static void new_bytes(GAtChat *p)
g_at_chat_unref(p);
}
-static void debug_chat(GAtChat *chat, gboolean in, const char *str, gsize len)
-{
- 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 (!chat->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);
- }
- }
- }
-
- chat->debugf(escaped_str, chat->debug_data);
- g_free(escaped_str);
-}
-
static gboolean received_data(GIOChannel *channel, GIOCondition cond,
gpointer data)
{
@@ -785,7 +709,8 @@ static gboolean received_data(GIOChannel *channel, GIOCondition cond,
buf = ring_buffer_write_ptr(chat->buf);
err = g_io_channel_read(channel, (char *) buf, toread, &rbytes);
- debug_chat(chat, TRUE, (char *)buf, rbytes);
+ g_at_util_debug_chat(chat->debugf, TRUE, (char *)buf, rbytes,
+ chat->debug_data);
total_read += rbytes;
@@ -931,8 +856,9 @@ static gboolean can_write_data(GIOChannel *channel, GIOCondition cond,
return FALSE;
}
- debug_chat(chat, FALSE, cmd->cmd + chat->cmd_bytes_written,
- bytes_written);
+ g_at_util_debug_chat(chat->debugf, FALSE,
+ cmd->cmd + chat->cmd_bytes_written,
+ bytes_written, chat->debug_data);
chat->cmd_bytes_written += bytes_written;
if (bytes_written < towrite)
@@ -960,7 +886,6 @@ static void g_at_chat_wakeup_writer(GAtChat *chat)
GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax)
{
GAtChat *chat;
- GIOFlags io_flags;
if (!channel)
return NULL;
@@ -991,20 +916,9 @@ GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax)
chat->notify_list = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, (GDestroyNotify)at_notify_destroy);
- if (g_io_channel_set_encoding(channel, NULL, NULL) !=
- G_IO_STATUS_NORMAL)
- goto error;
-
- io_flags = g_io_channel_get_flags(channel);
-
- io_flags |= G_IO_FLAG_NONBLOCK;
-
- if (g_io_channel_set_flags(channel, io_flags, NULL) !=
- G_IO_STATUS_NORMAL)
+ if (!g_at_util_setup_io(channel))
goto error;
- g_io_channel_set_close_on_unref(channel, TRUE);
-
chat->channel = channel;
chat->read_watch = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,