All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] Add gatutil.c to share common APIs with GAtServer
@ 2010-01-11 10:53 Zhenhua Zhang
  2010-01-11 10:53 ` [PATCH 2/4] Add GAtServer basic parsing support Zhenhua Zhang
  2010-01-14 15:49 ` [PATCH 1/4] Add gatutil.c to share common APIs with GAtServer Denis Kenzior
  0 siblings, 2 replies; 10+ messages in thread
From: Zhenhua Zhang @ 2010-01-11 10:53 UTC (permalink / raw)
  To: ofono

[-- Attachment #1: Type: text/plain, Size: 10937 bytes --]

Add gatutil.c/gatutil.h and move shared typedef and APIs into it.
So that they can be shared by GAtServer.
---
 Makefile.am       |    3 +-
 gatchat/gatchat.c |   98 +++-------------------------------------
 gatchat/gatchat.h |    8 +---
 gatchat/gatutil.c |  131 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 gatchat/gatutil.h |   46 +++++++++++++++++++
 5 files changed, 186 insertions(+), 100 deletions(-)
 create mode 100644 gatchat/gatutil.c
 create mode 100644 gatchat/gatutil.h

diff --git a/Makefile.am b/Makefile.am
index 276b478..67882b9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -51,7 +51,8 @@ gatchat_sources = gatchat/gatchat.h gatchat/gatchat.c \
 				gatchat/ringbuffer.h gatchat/ringbuffer.c \
 				gatchat/gatmux.h gatchat/gatmux.c \
 				gatchat/gsm0710.h gatchat/gsm0710.c \
-				gatchat/gattty.h gatchat/gattty.c
+				gatchat/gattty.h gatchat/gattty.c \
+				gatchat/gatutil.h gatchat/gatutil.c
 
 udev_files = plugins/ofono.rules
 
diff --git a/gatchat/gatchat.c b/gatchat/gatchat.c
index 8045c6b..e5cf235 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)
 {
@@ -786,7 +710,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;
 
@@ -932,8 +857,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)
@@ -961,7 +887,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;
@@ -992,20 +917,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_set_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,
diff --git a/gatchat/gatchat.h b/gatchat/gatchat.h
index 999b92d..3c3fa5d 100644
--- a/gatchat/gatchat.h
+++ b/gatchat/gatchat.h
@@ -26,19 +26,13 @@
 extern "C" {
 #endif
 
-#include "gatresult.h"
 #include "gatsyntax.h"
+#include "gatutil.h"
 
 struct _GAtChat;
 
 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);
 
 GIOChannel *g_at_chat_get_channel(GAtChat *chat);
diff --git a/gatchat/gatutil.c b/gatchat/gatutil.c
new file mode 100644
index 0000000..585a590
--- /dev/null
+++ b/gatchat/gatutil.c
@@ -0,0 +1,131 @@
+/*
+ *
+ *  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_set_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;
+	io_flags |= G_IO_FLAG_IS_READABLE;
+	io_flags |= G_IO_FLAG_IS_WRITEABLE;
+
+	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
index 0000000..63bbfff
--- /dev/null
+++ b/gatchat/gatutil.h
@@ -0,0 +1,46 @@
+/*
+ *
+ *  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 "gatresult.h"
+
+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);
+
+void g_at_util_debug_chat(GAtDebugFunc debugf, gboolean in, const char *str,
+						gsize len, gpointer user_data);
+
+gboolean g_at_util_set_io(GIOChannel *io);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __GATUTIL_H */
-- 
1.6.3.3


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-01-20 20:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-01-11 10:53 [PATCH 1/4] Add gatutil.c to share common APIs with GAtServer Zhenhua Zhang
2010-01-11 10:53 ` [PATCH 2/4] Add GAtServer basic parsing support Zhenhua Zhang
2010-01-11 10:53   ` [PATCH 3/4] Add AT command parsing support and interface Zhenhua Zhang
2010-01-11 10:53     ` [PATCH 4/4] Add test-server test application for GAtServer Zhenhua Zhang
2010-01-14 17:24   ` [PATCH 2/4] Add GAtServer basic parsing support Denis Kenzior
2010-01-14 21:53     ` Marcel Holtmann
2010-01-14 20:57       ` Denis Kenzior
2010-01-14 15:49 ` [PATCH 1/4] Add gatutil.c to share common APIs with GAtServer Denis Kenzior
2010-01-15  1:24   ` Zhang, Zhenhua
2010-01-20 20:23     ` Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.