Get local IRK value
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / common / oal-common.c
1 /*
2 * Open Adaptation Layer (OAL)
3 *
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
5 *
6 * Contact: Anupam Roy <anupam.r@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *                          http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <dlog.h>
25 #include <string.h>
26 #include <vconf.h>
27 #include <sys/prctl.h>
28 #include <unistd.h>
29 #include <arpa/inet.h>
30
31 #include <bluetooth.h>
32 #include "oal-internal.h"
33 #include "oal-common.h"
34
35 #define BT_UUID_STRING_SIZE 37
36 #define BT_UUID_LENGTH_MAX 16
37
38 void parse_device_properties(int num_properties, bt_property_t *properties,
39                 remote_device_t *dev_info, ble_adv_data_t * adv_info)
40 {
41         int i = 0;
42         int uuid_count = 0, table_len = 0;
43         int tmp_uuid_cnt = 0;
44         int chk = 0;
45         char lcl_uuid[BT_UUID_STRING_MAX];
46
47         bt_bdaddr_t * addr = {0};
48         bt_bdname_t *name = {0};
49         service_uuid_t *uuids;
50         bt_device_type_t dev_type;
51
52         for (i = 0; i < num_properties; i++) {
53                 switch (properties[i].type) {
54                 case BT_PROPERTY_BDADDR: {
55                         addr = (bt_bdaddr_t *)properties[i].val;
56                                 memcpy(dev_info->address.addr, addr->address, 6);
57                         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
58                                         dev_info->address.addr[0], dev_info->address.addr[1],
59                                         dev_info->address.addr[2], dev_info->address.addr[3],
60                                         dev_info->address.addr[4], dev_info->address.addr[5]);
61                         break;
62                 }
63                 case BT_PROPERTY_CLASS_OF_DEVICE: {
64                         dev_info->cod = *((int *)properties[i].val);
65                         BT_DBG("CLASS: 0x%06x", dev_info->cod);
66                         break;
67                 }
68                 case BT_PROPERTY_BDNAME: {
69                         name = properties[i].val;
70
71                         g_strlcpy(dev_info->name, (const gchar *)name->name, BT_DEVICE_NAME_LENGTH_MAX);
72                         BT_DBG("NAME: %s", dev_info->name);
73                         break;
74                 }
75                 case  BT_PROPERTY_REMOTE_FRIENDLY_NAME: {
76                         bt_bdname_t *alias = properties[i].val;
77                         if (NULL != alias && (0 != properties[i].len))
78                                 g_strlcpy(dev_info->alias, (const gchar *)alias->name, BT_DEVICE_NAME_LENGTH_MAX);
79                         BT_DBG("FRIENDLY NAME: [%s]", dev_info->alias);
80                         break;
81                 }
82                 case BT_PROPERTY_REMOTE_PAIRED: {
83                         dev_info->is_bonded = *((unsigned char*)properties[i].val);
84                         BT_DBG("BONDED [%d]", dev_info->is_bonded);
85                         break;
86                 }
87                 case BT_PROPERTY_REMOTE_CONNECTED: {
88                         dev_info->is_connected = *((int*)properties[i].val);
89                         BT_DBG("CONNECTED [%d]", dev_info->is_connected);
90                         break;
91                 }
92                 case BT_PROPERTY_REMOTE_TRUST: {
93                         dev_info->is_trusted = *((unsigned char*)properties[i].val);
94                         BT_DBG("TRUSTED [%d]", dev_info->is_trusted);
95                         break;
96                 }
97                 case BT_PROPERTY_REMOTE_RSSI: {
98                         dev_info->rssi = *((int *)properties[i].val);
99                         BT_DBG("RSSI: %d", dev_info->rssi);
100                         break;
101                 }
102                 case BT_PROPERTY_UUIDS: {
103                         uuids  = (service_uuid_t *)properties[i].val;
104                         BT_DBG("Length of properties from HAL [%d]", properties[i].len);
105                         uuid_count = properties[i].len/sizeof(bt_uuid_t);
106                         table_len += uuid_count;
107                         for (; tmp_uuid_cnt < table_len; tmp_uuid_cnt++) {
108                                 if(dev_info->uuid_count >= BT_MAX_SERVICES_FOR_DEVICE) {
109                                         BT_INFO("The max length of UUID exceeded");
110                                         break;
111                                 }
112                                 uuid_to_string(&uuids[tmp_uuid_cnt], lcl_uuid);
113                                 chk = check_duplicate_uuid(dev_info->uuid,
114                                         uuids[tmp_uuid_cnt], dev_info->uuid_count);
115                                 if (chk != 0) {
116                                         memcpy(&dev_info->uuid[dev_info->uuid_count++].uuid,
117                                                         &uuids[tmp_uuid_cnt].uuid, 16);
118                                 } else {
119                                         BT_DBG("Duplicate UUID found:%s\n", lcl_uuid);
120                                 }
121                                 BT_DBG("[%d] %s", dev_info->uuid_count, lcl_uuid);
122                         }
123                         break;
124                 }
125                 case BT_PROPERTY_TYPE_OF_DEVICE: {
126                         dev_type = *((bt_device_type_t *)properties[i].val);
127                         if (dev_type == BT_DEVICE_DEVTYPE_BLE)
128                                 BT_DBG("Single mode BLE Device");
129                         else if (dev_type == BT_DEVICE_DEVTYPE_DUAL)
130                                 BT_DBG("Dual mode BLE Device");
131                         dev_info->type = dev_type - 1;//OAL enum starts with 0 and Bluedroid with 1
132                         break;
133                 }
134                 case BT_PROPERTY_REMOTE_DEVICE_MANUFACTURER_DATA: {
135                         uint8_t *data = (uint8_t *)properties[i].val;
136                         int len;
137                         for (len = 0; len < properties[i].len; len++)
138                                 dev_info->manufacturer_data[len] = data[len];
139                         dev_info->manufacturer_data_len = len;
140                         break;
141                 }
142                 case BT_PROPERTY_REMOTE_BLE_ADV_DATA: {
143                         if (adv_info) {
144                                 adv_info->adv_data = properties[i].val;
145                                 adv_info->len = properties[i].len;
146                         }
147                         BT_DBG("----Advertising Data Length: %d", properties[i].len);
148                         break;
149                 }
150                 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: {
151                         BT_INFO("BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: Not Handled!!");
152                         break;
153                 }
154                 case BT_PROPERTY_SERVICE_RECORD: {
155                         BT_INFO("BT_PROPERTY_SERVICE_RECORD: Not Handled!!");
156                         break;
157                 }
158                 case BT_PROPERTY_REMOTE_IS_ALIAS_SET: {
159                         dev_info->is_alias_set = *((unsigned char*)properties[i].val);
160                         BT_DBG("IS_ALIAS_SET [%d]", dev_info->is_alias_set);
161                         break;
162                 }
163                 default:
164                         BT_WARN("Property not handled");
165                         break;
166                 }
167         }
168 }
169
170 oal_status_t convert_to_oal_status(bt_status_t status)
171 {
172         oal_status_t ret = OAL_STATUS_INTERNAL_ERROR;
173
174         switch (status) {
175         case BT_STATUS_SUCCESS:
176         case BT_STATUS_DONE:
177                 ret = OAL_STATUS_SUCCESS;
178                 break;
179         case BT_STATUS_NOT_READY:
180                 ret = OAL_STATUS_NOT_READY;
181                 break;
182         case BT_STATUS_BUSY:
183                 ret = OAL_STATUS_BUSY;
184                 break;
185         case BT_STATUS_PARM_INVALID:
186                 ret = OAL_STATUS_INVALID_PARAM;
187                 break;
188         case BT_STATUS_RMT_DEV_DOWN:
189                 ret = OAL_STATUS_RMT_DEVICE_DOWN;
190                 break;
191         case BT_STATUS_AUTH_FAILURE:
192                 ret = OAL_STATUS_AUTH_FAILED;
193                 break;
194         case BT_STATUS_UNSUPPORTED:
195                 ret = OAL_STATUS_NOT_SUPPORT;
196                 break;
197         case BT_STATUS_NOT_PAIRED:
198                 ret = OAL_STATUS_NOT_PAIRED;
199                 break;
200         case BT_STATUS_CONN_TOUT:
201                 ret = OAL_STATUS_LINK_LOSS;
202                 break;
203         case BT_STATUS_ALREADY_CONNECT:
204                 ret = OAL_STATUS_ALREADY_CONNECT;
205                 break;
206 #ifdef TIZEN_BT_HAL
207         case BT_STATUS_CONN_TERM_LOCAL_HOST:
208                 ret = OAL_STATUS_CONN_TERM_LOCAL_HOST;
209                 break;
210         case BT_STATUS_CONN_TERM_RMT_HOST:
211                 ret = OAL_STATUS_CONN_TERM_RMT_HOST;
212                 break;
213 #endif
214         case BT_STATUS_UNHANDLED:
215         case BT_STATUS_FAIL:
216         case BT_STATUS_NOMEM:
217         default:
218                 ret = OAL_STATUS_INTERNAL_ERROR;
219                 break;
220         }
221         return ret;
222 }
223
224 static const char * status_str[] = {
225         "BT_STATUS_SUCCESS",
226         "BT_STATUS_FAIL",
227         "BT_STATUS_NOT_READY",
228         "BT_STATUS_NOMEM",
229         "BT_STATUS_BUSY",
230         "BT_STATUS_DONE",
231         "BT_STATUS_UNSUPPORTED",
232         "BT_STATUS_PARM_INVALID",
233         "BT_STATUS_UNHANDLED",
234         "BT_STATUS_AUTH_FAILURE",
235         "BT_STATUS_RMT_DEV_DOWN"
236 };
237
238 int check_duplicate_uuid(oal_uuid_t *table, oal_uuid_t toMatch, int table_len)
239 {
240         int i;
241         int ret = 1;
242
243         for (i = 0; i < table_len; i++) {
244                 ret = memcmp(table[i].uuid, toMatch.uuid, 16);
245                 if (ret == 0)
246                         break;
247         }
248         return ret;
249 }
250
251 const char* status2string(bt_status_t status)
252 {
253         if (status <= BT_STATUS_RMT_DEV_DOWN)
254                 return status_str[status];
255         else {
256                 BT_ERR("Invalid BT status from stack");
257                 return "BT_STATUS_UNKNOWN";
258         }
259 }