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