3 * AT chat library with GLib integration
5 * Copyright (C) 2008-2010 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
33 void g_at_util_debug_chat(gboolean in, const char *str, gsize len,
34 GAtDebugFunc debugf, gpointer user_data)
36 char type = in ? '<' : '>';
37 gsize escaped = 2; /* Enough for '<', ' ' */
39 const char *esc = "<ESC>";
40 gsize esc_size = strlen(esc);
41 const char *ctrlz = "<CtrlZ>";
42 gsize ctrlz_size = strlen(ctrlz);
48 for (i = 0; i < len; i++) {
53 else if (c == '\r' || c == '\t' || c == '\n')
56 escaped += ctrlz_size;
63 escaped_str = g_malloc(escaped + 1);
64 escaped_str[0] = type;
66 escaped_str[2] = '\0';
67 escaped_str[escaped] = '\0';
69 for (escaped = 2, i = 0; i < len; i++) {
74 escaped_str[escaped++] = '\\';
75 escaped_str[escaped++] = 'r';
78 escaped_str[escaped++] = '\\';
79 escaped_str[escaped++] = 't';
82 escaped_str[escaped++] = '\\';
83 escaped_str[escaped++] = 'n';
86 strncpy(&escaped_str[escaped], ctrlz, ctrlz_size);
87 escaped += ctrlz_size;
90 strncpy(&escaped_str[escaped], esc, esc_size);
95 escaped_str[escaped++] = c;
97 escaped_str[escaped++] = '\\';
98 escaped_str[escaped++] = '0' + ((c >> 6) & 07);
99 escaped_str[escaped++] = '0' + ((c >> 3) & 07);
100 escaped_str[escaped++] = '0' + (c & 07);
105 debugf(escaped_str, user_data);
109 gboolean g_at_util_setup_io(GIOChannel *io, GIOFlags flags)
113 if (g_io_channel_set_encoding(io, NULL, NULL) !=
117 io_flags = g_io_channel_get_flags(io);
119 io_flags |= (flags & G_IO_FLAG_SET_MASK);
121 if (g_io_channel_set_flags(io, io_flags, NULL) !=
125 g_io_channel_set_close_on_unref(io, TRUE);