ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
uuid.c
Go to the documentation of this file.
1 
10 /*
11  *
12  * BlueZ - Bluetooth protocol stack for Linux
13  *
14  * Copyright (C) 2011 Nokia Corporation
15  * Copyright (C) 2011 Marcel Holtmann <marcel@holtmann.org>
16  *
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include <string.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 
42 #include "bluetooth.h"
43 #include "uuid.h"
44 
46  .data = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
47  0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }
48 };
49 
50 #define BASE_UUID16_OFFSET 2
51 #define BASE_UUID32_OFFSET 0
52 
53 static void bt_uuid16_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
54 {
55  uint16_t be16;
56 
58  dst->type = BT_UUID128;
59 
60  /*
61  * No matter the system: 128-bit UUIDs should be stored
62  * as big-endian. 16-bit UUIDs are stored on host order.
63  */
64 
65  be16 = htons(src->value.u16);
66  memcpy(&dst->value.u128.data[BASE_UUID16_OFFSET], &be16, sizeof(be16));
67 }
68 
69 static void bt_uuid32_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
70 {
71  uint32_t be32;
72 
74  dst->type = BT_UUID128;
75 
76  /*
77  * No matter the system: 128-bit UUIDs should be stored
78  * as big-endian. 32-bit UUIDs are stored on host order.
79  */
80 
81  be32 = htonl(src->value.u32);
82  memcpy(&dst->value.u128.data[BASE_UUID32_OFFSET], &be32, sizeof(be32));
83 }
84 
85 void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
86 {
87  switch (src->type) {
88  case BT_UUID128:
89  *dst = *src;
90  break;
91  case BT_UUID32:
92  bt_uuid32_to_uuid128(src, dst);
93  break;
94  case BT_UUID16:
95  bt_uuid16_to_uuid128(src, dst);
96  break;
97  case BT_UUID_UNSPEC:
98  default:
99  break;
100  }
101 }
102 
103 static int bt_uuid128_cmp(const bt_uuid_t *u1, const bt_uuid_t *u2)
104 {
105  return memcmp(&u1->value.u128, &u2->value.u128, sizeof(uint128_t));
106 }
107 
108 int bt_uuid16_create(bt_uuid_t *btuuid, uint16_t value)
109 {
110  memset(btuuid, 0, sizeof(bt_uuid_t));
111  btuuid->type = BT_UUID16;
112  btuuid->value.u16 = value;
113 
114  return 0;
115 }
116 
117 int bt_uuid32_create(bt_uuid_t *btuuid, uint32_t value)
118 {
119  memset(btuuid, 0, sizeof(bt_uuid_t));
120  btuuid->type = BT_UUID32;
121  btuuid->value.u32 = value;
122 
123  return 0;
124 }
125 
127 {
128  memset(btuuid, 0, sizeof(bt_uuid_t));
129  btuuid->type = BT_UUID128;
130  btuuid->value.u128 = value;
131 
132  return 0;
133 }
134 
135 int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)
136 {
137  bt_uuid_t u1, u2;
138 
139  bt_uuid_to_uuid128(uuid1, &u1);
140  bt_uuid_to_uuid128(uuid2, &u2);
141 
142  return bt_uuid128_cmp(&u1, &u2);
143 }
144 
145 /*
146  * convert the UUID to string, copying a maximum of n characters.
147  */
148 int bt_uuid_to_string(const bt_uuid_t *uuid, char *str, size_t n)
149 {
150  if (!uuid) {
151  snprintf(str, n, "NULL");
152  return -EINVAL;
153  }
154 
155  switch (uuid->type) {
156  case BT_UUID16:
157  snprintf(str, n, "%.4x", uuid->value.u16);
158  break;
159  case BT_UUID32:
160  snprintf(str, n, "%.8x", uuid->value.u32);
161  break;
162  case BT_UUID128: {
163  unsigned int data0;
164  unsigned short data1;
165  unsigned short data2;
166  unsigned short data3;
167  unsigned int data4;
168  unsigned short data5;
169 
170  const uint8_t *data = (uint8_t *) &uuid->value.u128;
171 
172  memcpy(&data0, &data[0], 4);
173  memcpy(&data1, &data[4], 2);
174  memcpy(&data2, &data[6], 2);
175  memcpy(&data3, &data[8], 2);
176  memcpy(&data4, &data[10], 4);
177  memcpy(&data5, &data[14], 2);
178 
179  snprintf(str, n, "%.8x-%.4x-%.4x-%.4x-%.8x%.4x",
180  ntohl(data0), ntohs(data1),
181  ntohs(data2), ntohs(data3),
182  ntohl(data4), ntohs(data5));
183  }
184  break;
185  case BT_UUID_UNSPEC:
186  default:
187  snprintf(str, n, "Type of UUID (%x) unknown.", uuid->type);
188  return -EINVAL; /* Enum type of UUID not set */
189  }
190 
191  return 0;
192 }
193 
194 static inline int is_uuid128(const char *string)
195 {
196  return (strlen(string) == 36 &&
197  string[8] == '-' &&
198  string[13] == '-' &&
199  string[18] == '-' &&
200  string[23] == '-');
201 }
202 
203 static inline int is_base_uuid128(const char *string)
204 {
205  uint16_t uuid;
206  char dummy;
207 
208  if (!is_uuid128(string))
209  return 0;
210 
211  return sscanf(string,
212  "0000%04hx-0000-1000-8000-00805%1[fF]9%1[bB]34%1[fF]%1[bB]",
213  &uuid, &dummy, &dummy, &dummy, &dummy) == 5;
214 }
215 
216 static inline int is_uuid32(const char *string)
217 {
218  return (strlen(string) == 8 || strlen(string) == 10);
219 }
220 
221 static inline int is_uuid16(const char *string)
222 {
223  return (strlen(string) == 4 || strlen(string) == 6);
224 }
225 
226 static int bt_string_to_uuid16(bt_uuid_t *uuid, const char *string)
227 {
228  uint16_t u16;
229  char *endptr = NULL;
230 
231  u16 = strtol(string, &endptr, 16);
232  if (endptr && (*endptr == '\0' || *endptr == '-')) {
233  bt_uuid16_create(uuid, u16);
234  return 0;
235  }
236 
237  return -EINVAL;
238 }
239 
240 static int bt_string_to_uuid32(bt_uuid_t *uuid, const char *string)
241 {
242  uint32_t u32;
243  char *endptr = NULL;
244 
245  u32 = strtol(string, &endptr, 16);
246  if (endptr && *endptr == '\0') {
247  bt_uuid32_create(uuid, u32);
248  return 0;
249  }
250 
251  return -EINVAL;
252 }
253 
254 static int bt_string_to_uuid128(bt_uuid_t *uuid, const char *string)
255 {
256  uint32_t data0, data4;
257  uint16_t data1, data2, data3, data5;
258  uint128_t u128;
259  uint8_t *val = (uint8_t *) &u128;
260 
261  if (sscanf(string, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
262  &data0, &data1, &data2,
263  &data3, &data4, &data5) != 6)
264  return -EINVAL;
265 
266  data0 = htonl(data0);
267  data1 = htons(data1);
268  data2 = htons(data2);
269  data3 = htons(data3);
270  data4 = htonl(data4);
271  data5 = htons(data5);
272 
273  memcpy(&val[0], &data0, 4);
274  memcpy(&val[4], &data1, 2);
275  memcpy(&val[6], &data2, 2);
276  memcpy(&val[8], &data3, 2);
277  memcpy(&val[10], &data4, 4);
278  memcpy(&val[14], &data5, 2);
279 
280  bt_uuid128_create(uuid, u128);
281 
282  return 0;
283 }
284 
285 int bt_string_to_uuid(bt_uuid_t *uuid, const char *string)
286 {
287  if (is_base_uuid128(string))
288  return bt_string_to_uuid16(uuid, string + 4);
289  else if (is_uuid128(string))
290  return bt_string_to_uuid128(uuid, string);
291  else if (is_uuid32(string))
292  return bt_string_to_uuid32(uuid, string);
293  else if (is_uuid16(string))
294  return bt_string_to_uuid16(uuid, string);
295 
296  return -EINVAL;
297 }
298 
299 int bt_uuid_strcmp(const void *a, const void *b)
300 {
301  return strcasecmp(a, b);
302 }
303 
304 int bt_uuid_to_le(const bt_uuid_t *src, void *dst)
305 {
306  bt_uuid_t uuid;
307 
308  switch (src->type) {
309  case BT_UUID16:
310  bt_put_le16(src->value.u16, dst);
311  return 0;
312  case BT_UUID32:
313  bt_uuid32_to_uuid128(src, &uuid);
314  src = &uuid;
315  /* Fallthrough */
316  case BT_UUID128:
317  /* Convert from 128-bit BE to LE */
318  bswap_128(&src->value.u128, dst);
319  return 0;
320  case BT_UUID_UNSPEC:
321  default:
322  return -EINVAL;
323  }
324 }
uint128_t u128
Definition: uuid.h:152
int bt_uuid_strcmp(const void *a, const void *b)
Definition: uuid.c:299
int bt_uuid32_create(bt_uuid_t *btuuid, uint32_t value)
Definition: uuid.c:117
int bt_string_to_uuid(bt_uuid_t *uuid, const char *string)
Definition: uuid.c:285
uint8_t data[16]
Definition: bluetooth.h:343
uint16_t u16
Definition: uuid.h:150
void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
Definition: uuid.c:85
union bt_uuid_t::@8 value
static int is_uuid128(const char *string)
Definition: uuid.c:194
int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)
Definition: uuid.c:135
static int bt_string_to_uuid16(bt_uuid_t *uuid, const char *string)
Definition: uuid.c:226
enum bt_uuid_t::@7 type
static int bt_string_to_uuid128(bt_uuid_t *uuid, const char *string)
Definition: uuid.c:254
static int is_uuid16(const char *string)
Definition: uuid.c:221
static int is_uuid32(const char *string)
Definition: uuid.c:216
static void bswap_128(const void *src, void *dst)
Definition: bluetooth.h:346
int bt_uuid16_create(bt_uuid_t *btuuid, uint16_t value)
Definition: uuid.c:108
static int is_base_uuid128(const char *string)
Definition: uuid.c:203
static uint128_t bluetooth_base_uuid
Definition: uuid.c:45
#define BASE_UUID16_OFFSET
Definition: uuid.c:50
uint32_t u32
Definition: uuid.h:151
static int bt_uuid128_cmp(const bt_uuid_t *u1, const bt_uuid_t *u2)
Definition: uuid.c:103
static int bt_string_to_uuid32(bt_uuid_t *uuid, const char *string)
Definition: uuid.c:240
Definition: crypto.c:447
int bt_uuid_to_le(const bt_uuid_t *src, void *dst)
Definition: uuid.c:304
static void bt_uuid16_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
Definition: uuid.c:53
static void bt_uuid32_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst)
Definition: uuid.c:69
static void bt_put_le16(uint16_t val, const void *ptr)
Definition: bluetooth.h:225
#define BASE_UUID32_OFFSET
Definition: uuid.c:51
int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value)
Definition: uuid.c:126
int bt_uuid_to_string(const bt_uuid_t *uuid, char *str, size_t n)
Definition: uuid.c:148