3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/types.h>
37 #include "src/shared/util.h"
39 void util_debug(util_debug_func_t function, void *user_data,
40 const char *format, ...)
45 if (!function || !format)
49 vsnprintf(str, sizeof(str), format, ap);
52 function(str, user_data);
55 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
56 util_debug_func_t function, void *user_data)
58 static const char hexdigits[] = "0123456789abcdef";
62 if (!function || !len)
67 for (i = 0; i < len; i++) {
68 str[((i % 16) * 3) + 1] = ' ';
69 str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
70 str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
71 str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
73 if ((i + 1) % 16 == 0) {
77 function(str, user_data);
84 for (j = (i % 16); j < 16; j++) {
85 str[(j * 3) + 1] = ' ';
86 str[(j * 3) + 2] = ' ';
87 str[(j * 3) + 3] = ' ';
93 function(str, user_data);
97 /* Helper for getting the dirent type in case readdir returns DT_UNKNOWN */
98 unsigned char util_get_dt(const char *parent, const char *name)
100 char filename[PATH_MAX];
103 snprintf(filename, sizeof(filename), "%s/%s", parent, name);
104 if (lstat(filename, &st) == 0 && S_ISDIR(st.st_mode))