3 * AT chat library with GLib integration
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 void g_at_util_debug_chat(gboolean in, const char *str, gsize len,
35 GAtDebugFunc debugf, gpointer user_data)
37 char type = in ? '<' : '>';
38 gsize escaped = 2; /* Enough for '<', ' ' */
40 const char *esc = "<ESC>";
41 gsize esc_size = strlen(esc);
42 const char *ctrlz = "<CtrlZ>";
43 gsize ctrlz_size = strlen(ctrlz);
46 if (debugf == NULL || !len)
49 for (i = 0; i < len; i++) {
52 if (g_ascii_isprint(c))
54 else if (c == '\r' || c == '\t' || c == '\n')
57 escaped += ctrlz_size;
64 escaped_str = g_try_malloc(escaped + 1);
65 if (escaped_str == NULL)
68 escaped_str[0] = type;
70 escaped_str[2] = '\0';
71 escaped_str[escaped] = '\0';
73 for (escaped = 2, i = 0; i < len; i++) {
74 unsigned char c = str[i];
78 escaped_str[escaped++] = '\\';
79 escaped_str[escaped++] = 'r';
82 escaped_str[escaped++] = '\\';
83 escaped_str[escaped++] = 't';
86 escaped_str[escaped++] = '\\';
87 escaped_str[escaped++] = 'n';
90 strncpy(&escaped_str[escaped], ctrlz, ctrlz_size);
91 escaped += ctrlz_size;
94 strncpy(&escaped_str[escaped], esc, esc_size);
98 if (g_ascii_isprint(c))
99 escaped_str[escaped++] = c;
101 escaped_str[escaped++] = '\\';
102 escaped_str[escaped++] = '0' + ((c >> 6) & 07);
103 escaped_str[escaped++] = '0' + ((c >> 3) & 07);
104 escaped_str[escaped++] = '0' + (c & 07);
109 debugf(escaped_str, user_data);
113 void g_at_util_debug_dump(gboolean in, const unsigned char *buf, gsize len,
114 GAtDebugFunc debugf, gpointer user_data)
116 char type = in ? '<' : '>';
120 if (debugf == NULL || !len)
123 str = g_string_sized_new(1 + (len * 2));
127 g_string_append_c(str, type);
129 for (i = 0; i < len; i++)
130 g_string_append_printf(str, " %02x", buf[i]);
132 debugf(str->str, user_data);
133 g_string_free(str, TRUE);
136 void g_at_util_debug_hexdump(gboolean in, const unsigned char *buf, gsize len,
137 GAtDebugFunc debugf, gpointer user_data)
139 static const char hexdigits[] = "0123456789abcdef";
143 if (debugf == NULL || !len)
146 str[0] = in ? '<' : '>';
148 for (i = 0; i < len; i++) {
149 str[((i % 16) * 3) + 1] = ' ';
150 str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
151 str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
152 str[(i % 16) + 51] = g_ascii_isprint(buf[i]) ? buf[i] : '.';
154 if ((i + 1) % 16 == 0) {
158 debugf(str, user_data);
165 for (j = (i % 16); j < 16; j++) {
166 str[(j * 3) + 1] = ' ';
167 str[(j * 3) + 2] = ' ';
168 str[(j * 3) + 3] = ' ';
174 debugf(str, user_data);
178 gboolean g_at_util_setup_io(GIOChannel *io, GIOFlags flags)
182 if (g_io_channel_set_encoding(io, NULL, NULL) != G_IO_STATUS_NORMAL)
185 g_io_channel_set_buffered(io, FALSE);
187 if (flags & G_IO_FLAG_SET_MASK) {
188 io_flags = g_io_channel_get_flags(io);
190 io_flags |= (flags & G_IO_FLAG_SET_MASK);
192 if (g_io_channel_set_flags(io, io_flags, NULL) !=
197 g_io_channel_set_close_on_unref(io, TRUE);