a367159fcf07288004c8b65bdf19d2d4244e7aed
[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                                 uuid_to_string(&uuids[tmp_uuid_cnt], lcl_uuid);
109                                 chk = check_duplicate_uuid(dev_info->uuid,
110                                         uuids[tmp_uuid_cnt], dev_info->uuid_count);
111                                 if (chk != 0) {
112                                         memcpy(&dev_info->uuid[dev_info->uuid_count++].uuid,
113                                                         &uuids[tmp_uuid_cnt].uuid, 16);
114                                 } else {
115                                         BT_DBG("Duplicate UUID found:%s\n", lcl_uuid);
116                                 }
117                                 BT_DBG("[%d] %s", dev_info->uuid_count, lcl_uuid);
118                         }
119                         break;
120                 }
121                 case BT_PROPERTY_TYPE_OF_DEVICE: {
122                         dev_type = *((bt_device_type_t *)properties[i].val);
123                         if (dev_type == BT_DEVICE_DEVTYPE_BLE)
124                                 BT_DBG("Single mode BLE Device");
125                         else if (dev_type == BT_DEVICE_DEVTYPE_DUAL)
126                                 BT_DBG("Dual mode BLE Device");
127                         dev_info->type = dev_type - 1;//OAL enum starts with 0 and Bluedroid with 1
128                         break;
129                 }
130                 case BT_PROPERTY_REMOTE_BLE_ADV_DATA: {
131                         if (adv_info) {
132                                 adv_info->adv_data = properties[i].val;
133                                 adv_info->len = properties[i].len;
134                         }
135                         BT_DBG("----Advertising Data Length: %d", properties[i].len);
136                         break;
137                 }
138                 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: {
139                         BT_INFO("BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: Not Handled!!");
140                         break;
141                 }
142                 case BT_PROPERTY_SERVICE_RECORD: {
143                         BT_INFO("BT_PROPERTY_SERVICE_RECORD: Not Handled!!");
144                         break;
145                 }
146                 case BT_PROPERTY_REMOTE_IS_ALIAS_SET: {
147                         dev_info->is_alias_set = *((unsigned char*)properties[i].val);
148                         BT_DBG("IS_ALIAS_SET [%d]", dev_info->is_alias_set);
149                         break;
150                 }
151                 default:
152                         BT_WARN("Property not handled");
153                         break;
154                 }
155         }
156 }
157
158 oal_status_t convert_to_oal_status(bt_status_t status)
159 {
160         oal_status_t ret = OAL_STATUS_INTERNAL_ERROR;
161
162         switch (status) {
163         case BT_STATUS_SUCCESS:
164         case BT_STATUS_DONE:
165                 ret = OAL_STATUS_SUCCESS;
166                 break;
167         case BT_STATUS_NOT_READY:
168                 ret = OAL_STATUS_NOT_READY;
169                 break;
170         case BT_STATUS_BUSY:
171                 ret = OAL_STATUS_BUSY;
172                 break;
173         case BT_STATUS_PARM_INVALID:
174                 ret = OAL_STATUS_INVALID_PARAM;
175                 break;
176         case BT_STATUS_RMT_DEV_DOWN:
177                 ret = OAL_STATUS_RMT_DEVICE_DOWN;
178                 break;
179         case BT_STATUS_AUTH_FAILURE:
180                 ret = OAL_STATUS_AUTH_FAILED;
181                 break;
182         case BT_STATUS_UNSUPPORTED:
183                 ret = OAL_STATUS_NOT_SUPPORT;
184                 break;
185         case BT_STATUS_NOT_PAIRED:
186                 ret = OAL_STATUS_NOT_PAIRED;
187                 break;
188         case BT_STATUS_CONN_TOUT:
189                 ret = OAL_STATUS_LINK_LOSS;
190                 break;
191 #ifdef TIZEN_BT_HAL
192         case BT_STATUS_CONN_TERM_LOCAL_HOST:
193                 ret = OAL_STATUS_CONN_TERM_LOCAL_HOST;
194                 break;
195         case BT_STATUS_CONN_TERM_RMT_HOST:
196                 ret = OAL_STATUS_CONN_TERM_RMT_HOST;
197                 break;
198 #endif
199         case BT_STATUS_UNHANDLED:
200         case BT_STATUS_FAIL:
201         case BT_STATUS_NOMEM:
202         default:
203                 ret = OAL_STATUS_INTERNAL_ERROR;
204                 break;
205         }
206         return ret;
207 }
208
209 static const char * status_str[] = {
210         "BT_STATUS_SUCCESS",
211         "BT_STATUS_FAIL",
212         "BT_STATUS_NOT_READY",
213         "BT_STATUS_NOMEM",
214         "BT_STATUS_BUSY",
215         "BT_STATUS_DONE",
216         "BT_STATUS_UNSUPPORTED",
217         "BT_STATUS_PARM_INVALID",
218         "BT_STATUS_UNHANDLED",
219         "BT_STATUS_AUTH_FAILURE",
220         "BT_STATUS_RMT_DEV_DOWN"
221 };
222
223 int check_duplicate_uuid(oal_uuid_t *table, oal_uuid_t toMatch, int table_len)
224 {
225         int i;
226         int ret = 1;
227
228         for (i = 0; i < table_len; i++) {
229                 ret = memcmp(table[i].uuid, toMatch.uuid, 16);
230                 if (ret == 0)
231                         break;
232         }
233         return ret;
234 }
235
236 const char* status2string(bt_status_t status)
237 {
238         if (status <= BT_STATUS_RMT_DEV_DOWN)
239                 return status_str[status];
240         else {
241                 BT_ERR("Invalid BT status from stack");
242                 return "BT_STATUS_UNKNOWN";
243         }
244 }