Fix Crash issue
[platform/upstream/bluez.git] / android / hal-utils.h
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /*
3  * Copyright (C) 2013 Intel Corporation
4  *
5  */
6
7 #include <endian.h>
8
9 #include <hardware/bluetooth.h>
10
11 #define MAX_UUID_STR_LEN        37
12 #define HAL_UUID_LEN            16
13 #define MAX_ADDR_STR_LEN        18
14
15 static const char BT_BASE_UUID[] = {
16         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
17         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb
18 };
19
20 const char *bt_uuid_t2str(const uint8_t *uuid, char *buf);
21 const char *btuuid2str(const uint8_t *uuid);
22 const char *bt_bdaddr_t2str(const bt_bdaddr_t *bd_addr, char *buf);
23 void str2bt_bdaddr_t(const char *str, bt_bdaddr_t *bd_addr);
24 void str2bt_uuid_t(const char *str, bt_uuid_t *uuid);
25 const char *btproperty2str(const bt_property_t *property);
26 const char *bdaddr2str(const bt_bdaddr_t *bd_addr);
27
28 int get_config(const char *config_key, char *value, const char *fallback);
29
30 /*
31  * Begin mapping section
32  *
33  * There are some mappings between integer values (enums) and strings
34  * to be presented to user. To make it easier to convert between those two
35  * set of macros is given. It is specially useful when we want to have
36  * strings that match constants from header files like:
37  *  BT_STATUS_SUCCESS (0) and corresponding "BT_STATUS_SUCCESS"
38  * Example of usage:
39  *
40  * INTMAP(int, -1, "invalid")
41  *   DELEMENT(BT_STATUS_SUCCESS)
42  *   DELEMENT(BT_STATUS_FAIL)
43  *   MELEMENT(123, "Some strange value")
44  * ENDMAP
45  *
46  * Just by doing this we have mapping table plus two functions:
47  *  int str2int(const char *str);
48  *  const char *int2str(int v);
49  *
50  * second argument to INTMAP specifies value to be returned from
51  * str2int function when there is not mapping for such number
52  * third argument specifies default value to be returned from int2str
53  *
54  * If same mapping is to be used in several source files put
55  * INTMAP in c file and DECINTMAP in h file.
56  *
57  * For mappings that are to be used in single file only
58  * use SINTMAP which will create the same but everything will be marked
59  * as static.
60  */
61
62 struct int2str {
63         int val;                /* int value */
64         const char *str;        /* corresponding string */
65 };
66
67 int int2str_findint(int v, const struct int2str m[]);
68 int int2str_findstr(const char *str, const struct int2str m[]);
69 const char *enum_defines(void *v, int i);
70 const char *enum_strings(void *v, int i);
71 const char *enum_one_string(void *v, int i);
72
73 #define TYPE_ENUM(type) ((void *) &__##type##2str[0])
74 #define DECINTMAP(type) \
75 extern struct int2str __##type##2str[]; \
76 const char *type##2##str(type v); \
77 type str##2##type(const char *str); \
78
79 #define INTMAP(type, deft, defs) \
80 const char *type##2##str(type v) \
81 { \
82         int i = int2str_findint((int) v, __##type##2str); \
83         return (i < 0) ? defs : __##type##2str[i].str; \
84 } \
85 type str##2##type(const char *str) \
86 { \
87         int i = int2str_findstr(str, __##type##2str); \
88         return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
89 } \
90 struct int2str __##type##2str[] = {
91
92 #define SINTMAP(type, deft, defs) \
93 static struct int2str __##type##2str[]; \
94 static inline const char *type##2##str(type v) \
95 { \
96         int i = int2str_findint((int) v, __##type##2str); \
97         return (i < 0) ? defs : __##type##2str[i].str; \
98 } \
99 static inline type str##2##type(const char *str) \
100 { \
101         int i = int2str_findstr(str, __##type##2str); \
102         return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
103 } \
104 static struct int2str __##type##2str[] = {
105
106 #define ENDMAP {0, NULL} };
107
108 /* use this to generate string from header file constant */
109 #define MELEMENT(v, s) {v, s}
110 /* use this to have arbitrary mapping from int to string */
111 #define DELEMENT(s) {s, #s}
112 /* End of mapping section */
113
114 DECINTMAP(bt_status_t);
115 DECINTMAP(bt_state_t);
116 DECINTMAP(bt_device_type_t);
117 DECINTMAP(bt_scan_mode_t);
118 DECINTMAP(bt_discovery_state_t);
119 DECINTMAP(bt_acl_state_t);
120 DECINTMAP(bt_bond_state_t);
121 DECINTMAP(bt_ssp_variant_t);
122 DECINTMAP(bt_property_type_t);
123 DECINTMAP(bt_cb_thread_evt);
124
125 static inline uint16_t get_le16(const void *src)
126 {
127         const struct __attribute__((packed)) {
128                 uint16_t le16;
129         } *p = src;
130
131         return le16toh(p->le16);
132 }
133
134 static inline void put_le16(uint16_t val, void *dst)
135 {
136         struct __attribute__((packed)) {
137                 uint16_t le16;
138         } *p = dst;
139
140         p->le16 = htole16(val);
141 }