2 * Copyright (C) 2013 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <hardware/bluetooth.h>
22 #define MAX_UUID_STR_LEN 37
23 #define HAL_UUID_LEN 16
24 #define MAX_ADDR_STR_LEN 18
26 static const char BT_BASE_UUID[] = {
27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
28 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb
31 const char *bt_uuid_t2str(const uint8_t *uuid, char *buf);
32 const char *btuuid2str(const uint8_t *uuid);
33 const char *bt_bdaddr_t2str(const bt_bdaddr_t *bd_addr, char *buf);
34 void str2bt_bdaddr_t(const char *str, bt_bdaddr_t *bd_addr);
35 void str2bt_uuid_t(const char *str, bt_uuid_t *uuid);
36 const char *btproperty2str(const bt_property_t *property);
37 const char *bdaddr2str(const bt_bdaddr_t *bd_addr);
39 int get_config(const char *config_key, char *value, const char *fallback);
42 * Begin mapping section
44 * There are some mappings between integer values (enums) and strings
45 * to be presented to user. To make it easier to convert between those two
46 * set of macros is given. It is specially useful when we want to have
47 * strings that match constants from header files like:
48 * BT_STATUS_SUCCESS (0) and corresponding "BT_STATUS_SUCCESS"
51 * INTMAP(int, -1, "invalid")
52 * DELEMENT(BT_STATUS_SUCCESS)
53 * DELEMENT(BT_STATUS_FAIL)
54 * MELEMENT(123, "Some strange value")
57 * Just by doing this we have mapping table plus two functions:
58 * int str2int(const char *str);
59 * const char *int2str(int v);
61 * second argument to INTMAP specifies value to be returned from
62 * str2int function when there is not mapping for such number
63 * third argument specifies default value to be returned from int2str
65 * If same mapping is to be used in several source files put
66 * INTMAP in c file and DECINTMAP in h file.
68 * For mappings that are to be used in single file only
69 * use SINTMAP which will create the same but everything will be marked
74 int val; /* int value */
75 const char *str; /* corresponding string */
78 int int2str_findint(int v, const struct int2str m[]);
79 int int2str_findstr(const char *str, const struct int2str m[]);
80 const char *enum_defines(void *v, int i);
81 const char *enum_strings(void *v, int i);
82 const char *enum_one_string(void *v, int i);
84 #define TYPE_ENUM(type) ((void *) &__##type##2str[0])
85 #define DECINTMAP(type) \
86 extern struct int2str __##type##2str[]; \
87 const char *type##2##str(type v); \
88 type str##2##type(const char *str); \
90 #define INTMAP(type, deft, defs) \
91 const char *type##2##str(type v) \
93 int i = int2str_findint((int) v, __##type##2str); \
94 return (i < 0) ? defs : __##type##2str[i].str; \
96 type str##2##type(const char *str) \
98 int i = int2str_findstr(str, __##type##2str); \
99 return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
101 struct int2str __##type##2str[] = {
103 #define SINTMAP(type, deft, defs) \
104 static struct int2str __##type##2str[]; \
105 static inline const char *type##2##str(type v) \
107 int i = int2str_findint((int) v, __##type##2str); \
108 return (i < 0) ? defs : __##type##2str[i].str; \
110 static inline type str##2##type(const char *str) \
112 int i = int2str_findstr(str, __##type##2str); \
113 return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
115 static struct int2str __##type##2str[] = {
117 #define ENDMAP {0, NULL} };
119 /* use this to generate string from header file constant */
120 #define MELEMENT(v, s) {v, s}
121 /* use this to have arbitrary mapping from int to string */
122 #define DELEMENT(s) {s, #s}
123 /* End of mapping section */
125 DECINTMAP(bt_status_t);
126 DECINTMAP(bt_state_t);
127 DECINTMAP(bt_device_type_t);
128 DECINTMAP(bt_scan_mode_t);
129 DECINTMAP(bt_discovery_state_t);
130 DECINTMAP(bt_acl_state_t);
131 DECINTMAP(bt_bond_state_t);
132 DECINTMAP(bt_ssp_variant_t);
133 DECINTMAP(bt_property_type_t);
134 DECINTMAP(bt_cb_thread_evt);
136 static inline uint16_t get_le16(const void *src)
138 const struct __attribute__((packed)) {
142 return le16toh(p->le16);
145 static inline void put_le16(uint16_t val, void *dst)
147 struct __attribute__((packed)) {
151 p->le16 = htole16(val);