ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
util.c
Go to the documentation of this file.
1 
10 /*
11  *
12  * BlueZ - Bluetooth protocol stack for Linux
13  *
14  * Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
15  *
16  *
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30  *
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <stdarg.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <dirent.h>
44 #include <limits.h>
45 #include <string.h>
46 
47 #include "util.h"
48 
56 void util_debug(util_debug_func_t function, void *user_data,
57  const char *format, ...)
58 {
59  char str[78];
60  va_list ap;
61 
62  if (!function || !format)
63  return;
64 
65  va_start(ap, format);
66  vsnprintf(str, sizeof(str), format, ap);
67  va_end(ap);
68 
69  function(str, user_data);
70 }
71 
81 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
82  util_debug_func_t function, void *user_data)
83 {
84  static const char hexdigits[] = "0123456789abcdef";
85  char str[68];
86  size_t i;
87 
88  if (!function || !len)
89  return;
90 
91  str[0] = dir;
92 
93  for (i = 0; i < len; i++) {
94  str[((i % 16) * 3) + 1] = ' ';
95  str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
96  str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
97  str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
98 
99  if ((i + 1) % 16 == 0) {
100  str[49] = ' ';
101  str[50] = ' ';
102  str[67] = '\0';
103  function(str, user_data);
104  str[0] = ' ';
105  }
106  }
107 
108  if (i % 16 > 0) {
109  size_t j;
110  for (j = (i % 16); j < 16; j++) {
111  str[(j * 3) + 1] = ' ';
112  str[(j * 3) + 2] = ' ';
113  str[(j * 3) + 3] = ' ';
114  str[j + 51] = ' ';
115  }
116  str[49] = ' ';
117  str[50] = ' ';
118  str[67] = '\0';
119  function(str, user_data);
120  }
121 }
122 
130 unsigned char util_get_dt(const char *parent, const char *name)
131 {
132  char filename[PATH_MAX];
133  struct stat st;
134 
135  snprintf(filename, sizeof(filename), "%s/%s", parent, name);
136  if (lstat(filename, &st) == 0 && S_ISDIR(st.st_mode))
137  return DT_DIR;
138 
139  return DT_UNKNOWN;
140 }
141 
152 uint8_t util_get_uid(unsigned int *bitmap, uint8_t max)
153 {
154  uint8_t id;
155 
156  id = ffs(~*bitmap);
157 
158  if (!id || id > max)
159  return 0;
160 
161  *bitmap |= 1 << (id - 1);
162 
163  return id;
164 }
165 
172 void util_clear_uid(unsigned int *bitmap, uint8_t id)
173 {
174  if (!id)
175  return;
176 
177  *bitmap &= ~(1 << (id - 1));
178 }
void(* util_debug_func_t)(const char *str, void *user_data)
Definition: util.h:86
uint8_t util_get_uid(unsigned int *bitmap, uint8_t max)
Definition: util.c:152
void util_hexdump(const char dir, const unsigned char *buf, size_t len, util_debug_func_t function, void *user_data)
Definition: util.c:81
void util_clear_uid(unsigned int *bitmap, uint8_t id)
Definition: util.c:172
void * user_data
Definition: mainloop.c:83
void util_debug(util_debug_func_t function, void *user_data, const char *format,...)
Definition: util.c:56
unsigned char util_get_dt(const char *parent, const char *name)
Definition: util.c:130