summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2013-02-14 16:24:41 +0100
committerMarcel Holtmann <marcel@holtmann.org>2013-02-14 16:24:41 +0100
commit983b20db4a63db6b3bc1b8e8d4153788c29b253c (patch)
treed598f33f26346452e5dc2fa8d5e040fd02868830 /src/shared
parent974770da9fb0693bb4bb57e5adc0b067f62b2ec4 (diff)
downloadconnman-983b20db4a63db6b3bc1b8e8d4153788c29b253c.tar.gz
connman-983b20db4a63db6b3bc1b8e8d4153788c29b253c.tar.bz2
connman-983b20db4a63db6b3bc1b8e8d4153788c29b253c.zip
shared: Add generic debug and hexdump helpers
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/util.c90
-rw-r--r--src/shared/util.h31
2 files changed, 121 insertions, 0 deletions
diff --git a/src/shared/util.c b/src/shared/util.c
new file mode 100644
index 00000000..df045c5b
--- /dev/null
+++ b/src/shared/util.c
@@ -0,0 +1,90 @@
+/*
+ *
+ * Connection Manager
+ *
+ * Copyright (C) 2012 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; 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 <stdio.h>
+#include <ctype.h>
+#include <stdarg.h>
+
+#include "src/shared/util.h"
+
+void util_debug(util_debug_func_t function, void *user_data,
+ const char *format, ...)
+{
+ char str[78];
+ va_list ap;
+
+ if (!function || !format)
+ return;
+
+ va_start(ap, format);
+ vsnprintf(str, sizeof(str), format, ap);
+ va_end(ap);
+
+ function(str, user_data);
+}
+
+void util_hexdump(const char dir, const unsigned char *buf, size_t len,
+ util_debug_func_t function, void *user_data)
+{
+ static const char hexdigits[] = "0123456789abcdef";
+ char str[68];
+ size_t i;
+
+ if (!function || !len)
+ return;
+
+ str[0] = dir;
+
+ for (i = 0; i < len; i++) {
+ str[((i % 16) * 3) + 1] = ' ';
+ str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
+ str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
+ str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
+
+ if ((i + 1) % 16 == 0) {
+ str[49] = ' ';
+ str[50] = ' ';
+ str[67] = '\0';
+ function(str, user_data);
+ str[0] = ' ';
+ }
+ }
+
+ if (i % 16 > 0) {
+ size_t j;
+ for (j = (i % 16); j < 16; j++) {
+ str[(j * 3) + 1] = ' ';
+ str[(j * 3) + 2] = ' ';
+ str[(j * 3) + 3] = ' ';
+ str[j + 51] = ' ';
+ }
+ str[49] = ' ';
+ str[50] = ' ';
+ str[67] = '\0';
+ function(str, user_data);
+ }
+}
diff --git a/src/shared/util.h b/src/shared/util.h
new file mode 100644
index 00000000..cb81fc57
--- /dev/null
+++ b/src/shared/util.h
@@ -0,0 +1,31 @@
+/*
+ *
+ * Connection Manager
+ *
+ * Copyright (C) 2012 Intel Corporation. All rights reserved.
+ *
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+typedef void (*util_debug_func_t)(const char *str, void *user_data);
+
+void util_debug(util_debug_func_t function, void *user_data,
+ const char *format, ...)
+ __attribute__((format(printf, 3, 4)));
+
+void util_hexdump(const char dir, const unsigned char *buf, size_t len,
+ util_debug_func_t function, void *user_data);