Sync GAtChat library with oFono
authorMarcel Holtmann <marcel@holtmann.org>
Sat, 23 Jan 2010 10:20:00 +0000 (11:20 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Sat, 23 Jan 2010 10:20:00 +0000 (11:20 +0100)
Makefile.plugins
gatchat/gat.h [new file with mode: 0644]
gatchat/gatchat.c
gatchat/gatchat.h
gatchat/gatutil.c [new file with mode: 0644]
gatchat/gatutil.h [new file with mode: 0644]

index 5d9733204df9cae2c86fe52c6f70c01efbafbf84..2406327491b1cc996b96df3ea8b4516c535ccefa 100644 (file)
@@ -5,7 +5,9 @@ plugins_libgatchat_la_SOURCES = gatchat/gatchat.h gatchat/gatchat.c \
                                gatchat/gatresult.h gatchat/gatresult.c \
                                gatchat/gatsyntax.h gatchat/gatsyntax.c \
                                gatchat/ringbuffer.h gatchat/ringbuffer.c \
-                               gatchat/gattty.h gatchat/gattty.c
+                               gatchat/gattty.h gatchat/gattty.c \
+                               gatchat/gatutil.h gatchat/gatutil.c \
+                               gatchat/gat.h
 
 
 plugin_cflags = -fvisibility=hidden -I$(srcdir)/gdbus \
diff --git a/gatchat/gat.h b/gatchat/gat.h
new file mode 100644 (file)
index 0000000..6696e7d
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ *
+ *  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
+ *
+ */
+
+#ifndef __GAT_H
+#define __GAT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef void (*GAtDisconnectFunc)(gpointer user_data);
+typedef void (*GAtDebugFunc)(const char *str, gpointer user_data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __GAT_H */
index 7dfc4cb8b3aab4071094d1787c7d44329d2eaed6..7026408c56df4c4c6a9cae5fe85a990834d23801 100644 (file)
@@ -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(TRUE, (char *)buf, rbytes,
+                                       chat->debugf, chat->debug_data);
 
                total_read += rbytes;
 
@@ -931,8 +856,8 @@ 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(FALSE, cmd->cmd + chat->cmd_bytes_written,
+                               bytes_written, chat->debugf, chat->debug_data);
        chat->cmd_bytes_written += bytes_written;
 
        if (bytes_written < towrite)
@@ -960,7 +885,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 +915,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,
index 999b92dd6e3c8f9cf7a86a878199e2e24df456d2..f76fc4218ae8f3b815ff23ab94a682641671fa7a 100644 (file)
@@ -28,6 +28,7 @@ extern "C" {
 
 #include "gatresult.h"
 #include "gatsyntax.h"
+#include "gatutil.h"
 
 struct _GAtChat;
 
@@ -36,8 +37,6 @@ typedef struct _GAtChat GAtChat;
 typedef void (*GAtResultFunc)(gboolean success, GAtResult *result,
                                gpointer user_data);
 typedef void (*GAtNotifyFunc)(GAtResult *result, gpointer user_data);
-typedef void (*GAtDisconnectFunc)(gpointer user_data);
-typedef void (*GAtDebugFunc)(const char *str, gpointer user_data);
 
 GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax);
 
diff --git a/gatchat/gatutil.c b/gatchat/gatutil.c
new file mode 100644 (file)
index 0000000..dcff4c7
--- /dev/null
@@ -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(gboolean in, const char *str, gsize len,
+                               GAtDebugFunc debugf, 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;
+}
+
diff --git a/gatchat/gatutil.h b/gatchat/gatutil.h
new file mode 100644 (file)
index 0000000..3aa8265
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *
+ *  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
+ *
+ */
+
+#ifndef __GATUTIL_H
+#define __GATUTIL_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "gat.h"
+
+void g_at_util_debug_chat(gboolean in, const char *str, gsize len,
+                               GAtDebugFunc debugf, gpointer user_data);
+
+gboolean g_at_util_setup_io(GIOChannel *io);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __GATUTIL_H */