1 /* log.c - logging helpers */
2
3 /*
4 * Copyright (c) 2017 Nordic Semiconductor ASA
5 * Copyright (c) 2016 Intel Corporation
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 /* Helper for printk parameters to convert from binary to hex.
11 * We declare multiple buffers so the helper can be used multiple times
12 * in a single printk call.
13 */
14
15 #include <stddef.h>
16 #include <ble_types/types.h>
17 #include <ble_os.h>
18 #include <misc/util.h>
19 #include <bluetooth/bluetooth.h>
20 #include <bluetooth/uuid.h>
21 #include <bluetooth/hci.h>
22
bt_hex_real(const void * buf,size_t len)23 const char *bt_hex_real(const void *buf, size_t len)
24 {
25 static const char hex[] = "0123456789abcdef";
26 static char str[129];
27 const u8_t *b = buf;
28 size_t i;
29
30 len = MIN(len, (sizeof(str) - 1) / 2);
31
32 for (i = 0; i < len; i++) {
33 str[i * 2] = hex[b[i] >> 4];
34 str[i * 2 + 1] = hex[b[i] & 0xf];
35 }
36
37 str[i * 2] = '\0';
38
39 return str;
40 }
41
bt_addr_str_real(const bt_addr_t * addr)42 const char *bt_addr_str_real(const bt_addr_t *addr)
43 {
44 static char str[BT_ADDR_STR_LEN];
45
46 bt_addr_to_str(addr, str, sizeof(str));
47
48 return str;
49 }
50
bt_addr_le_str_real(const bt_addr_le_t * addr)51 const char *bt_addr_le_str_real(const bt_addr_le_t *addr)
52 {
53 static char str[BT_ADDR_LE_STR_LEN];
54
55 bt_addr_le_to_str(addr, str, sizeof(str));
56
57 return str;
58 }
59
bt_uuid_str_real(const struct bt_uuid * uuid)60 const char *bt_uuid_str_real(const struct bt_uuid *uuid)
61 {
62 static char str[BT_UUID_STR_LEN];
63
64 bt_uuid_to_str(uuid, str, sizeof(str));
65
66 return str;
67 }
68
hextostring(const uint8_t * source,char * dest,int len)69 void hextostring(const uint8_t *source, char *dest, int len)
70 {
71 int i;
72 char tmp[3];
73
74 for (i = 0; i < len; i++) {
75 sprintf(tmp, "%02x", (unsigned char)source[i]);
76 memcpy(&dest[i * 2], tmp, 2);
77 }
78 }
79
stringtohex(char * str,u8_t * out,u8_t count)80 u8_t stringtohex(char *str, u8_t *out, u8_t count)
81 {
82 u8_t i = 0, j = 0;
83 u8_t n = 0;
84
85 memset(out, 0, count);
86 if (strlen(str) != count << 1) {
87 return 0;
88 }
89
90 while (i < count) {
91 while (j < 2) {
92 n = i * 2 + j;
93 if (str[n] >= 'a' && str[n] <= 'f') {
94 out[i] = out[i] << 4 | (str[n] - 'a' + 10);
95 } else if (str[n] >= 'A' && str[n] <= 'F') {
96 out[i] = out[i] << 4 | (str[n] - 'A' + 10);
97 } else if (str[n] >= '0' && str[n] <= '9') {
98 out[i] = out[i] << 4 | (str[n] - '0');
99 } else {
100 return 0;
101 }
102 j++;
103 }
104 j = 0;
105 i++;
106 }
107
108 return count;
109 }
110
111