diff options
author | Nathan Williams <njw@chromium.org> | 2012-03-23 14:05:40 -0400 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-05-02 21:05:06 +0200 |
commit | 00cb8a26a829bd26ea7f1d953ebdbe657e85f83c (patch) | |
tree | 9e5237e78a096f8cd1f8d555a585509a12daf23a | |
parent | 67698b43c4c088fbd319de6f14914e958a0888d8 (diff) |
Add a utility routine to do ITU V.250 quoting of strings for AT commands.
BUG=chromium-os:27096,chromium-os:27063
TEST=None
Change-Id: Ic1d24a9e4b7421db7f8d16c52535bd6d2780423e
-rw-r--r-- | src/mm-at-serial-port.c | 26 | ||||
-rw-r--r-- | src/mm-at-serial-port.h | 6 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/mm-at-serial-port.c b/src/mm-at-serial-port.c index 4a5afa4b..c16e8786 100644 --- a/src/mm-at-serial-port.c +++ b/src/mm-at-serial-port.c @@ -49,6 +49,32 @@ typedef struct { /*****************************************************************************/ +gchar * +mm_at_serial_port_quote_string (const char *string) +{ + int len, i; + gchar *quoted, *pos; + + if (string == NULL) + len = 0; + else + len = strlen (string); + quoted = g_malloc (3 + 3 * len); /* worst case */ + + pos = quoted; + *pos++ = '"'; + for (i = 0 ; i < len; i++) { + if (string[i] < 0x20 || string[i] == '"' || string[i] == '\\') + pos += sprintf (pos, "\\%02X", string[i]); + else + *pos++ = string[i]; + } + *pos++ = '"'; + *pos++ = '\0'; + + return quoted; +} + void mm_at_serial_port_set_response_parser (MMAtSerialPort *self, MMAtSerialResponseParserFn fn, diff --git a/src/mm-at-serial-port.h b/src/mm-at-serial-port.h index 02435123..8ed8db9d 100644 --- a/src/mm-at-serial-port.h +++ b/src/mm-at-serial-port.h @@ -104,6 +104,12 @@ void mm_at_serial_port_queue_command_cached (MMAtSerialPort *self, MMAtSerialResponseFn callback, gpointer user_data); +/* + * Convert a string into a quoted and escaped string. Returns a new + * allocated string. Follows ITU V.250 5.4.2.2 "String constants". + */ +gchar *mm_at_serial_port_quote_string (const char *string); + /* Just for unit tests */ void mm_at_serial_port_remove_echo (GByteArray *response); |