Changes for successful build after porting of Tizen 2.4 code
[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 <endian.h>
19
20 #include <hardware/bluetooth.h>
21
22 #define MAX_UUID_STR_LEN        37
23 #define HAL_UUID_LEN            16
24 #define MAX_ADDR_STR_LEN        18
25
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
29 };
30
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);
38
39 int get_config(const char *config_key, char *value, const char *fallback);
40
41 /*
42  * Begin mapping section
43  *
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"
49  * Example of usage:
50  *
51  * INTMAP(int, -1, "invalid")
52  *   DELEMENT(BT_STATUS_SUCCESS)
53  *   DELEMENT(BT_STATUS_FAIL)
54  *   MELEMENT(123, "Some strange value")
55  * ENDMAP
56  *
57  * Just by doing this we have mapping table plus two functions:
58  *  int str2int(const char *str);
59  *  const char *int2str(int v);
60  *
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
64  *
65  * If same mapping is to be used in several source files put
66  * INTMAP in c file and DECINTMAP in h file.
67  *
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
70  * as static.
71  */
72
73 struct int2str {
74         int val;                /* int value */
75         const char *str;        /* corresponding string */
76 };
77
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);
83
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); \
89
90 #define INTMAP(type, deft, defs) \
91 const char *type##2##str(type v) \
92 { \
93         int i = int2str_findint((int) v, __##type##2str); \
94         return (i < 0) ? defs : __##type##2str[i].str; \
95 } \
96 type str##2##type(const char *str) \
97 { \
98         int i = int2str_findstr(str, __##type##2str); \
99         return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
100 } \
101 struct int2str __##type##2str[] = {
102
103 #define SINTMAP(type, deft, defs) \
104 static struct int2str __##type##2str[]; \
105 static inline const char *type##2##str(type v) \
106 { \
107         int i = int2str_findint((int) v, __##type##2str); \
108         return (i < 0) ? defs : __##type##2str[i].str; \
109 } \
110 static inline type str##2##type(const char *str) \
111 { \
112         int i = int2str_findstr(str, __##type##2str); \
113         return (i < 0) ? (type) deft : (type) (__##type##2str[i].val); \
114 } \
115 static struct int2str __##type##2str[] = {
116
117 #define ENDMAP {0, NULL} };
118
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 */
124
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);
135
136 static inline uint16_t get_le16(const void *src)
137 {
138         const struct __attribute__((packed)) {
139                 uint16_t le16;
140         } *p = src;
141
142         return le16toh(p->le16);
143 }
144
145 static inline void put_le16(uint16_t val, void *dst)
146 {
147         struct __attribute__((packed)) {
148                 uint16_t le16;
149         } *p = dst;
150
151         p->le16 = htole16(val);
152 }