edf77ca3092881a1dc0708f57956518ce318b189
[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         BT_DBG("num_properties: %d", num_properties);
53
54         for (i = 0; i < num_properties; i++) {
55                 BT_DBG("===>Prop type: %d, Len: %d<===", properties[i].type, properties[i].len);
56
57                 switch (properties[i].type) {
58                 case BT_PROPERTY_BDADDR: {
59                         addr = (bt_bdaddr_t *)properties[i].val;
60                                 memcpy(dev_info->address.addr, addr->address, 6);
61                         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
62                                         dev_info->address.addr[0], dev_info->address.addr[1],
63                                         dev_info->address.addr[2], dev_info->address.addr[3],
64                                         dev_info->address.addr[4], dev_info->address.addr[5]);
65                         break;
66                 }
67                 case BT_PROPERTY_CLASS_OF_DEVICE: {
68                         dev_info->cod = *((int *)properties[i].val);
69                         BT_DBG("CLASS: 0x%06x", dev_info->cod);
70                         break;
71                 }
72                 case BT_PROPERTY_BDNAME: {
73                         name = properties[i].val;
74
75                         g_strlcpy(dev_info->name, (const gchar *)name->name, BT_DEVICE_NAME_LENGTH_MAX);
76                         BT_DBG("NAME: %s", dev_info->name);
77                         break;
78                 }
79                 case  BT_PROPERTY_REMOTE_FRIENDLY_NAME: {
80                         bt_bdname_t *alias = properties[i].val;
81                         if (NULL != alias && (0 != properties[i].len))
82                                 g_strlcpy(dev_info->alias, (const gchar *)alias->name, BT_DEVICE_NAME_LENGTH_MAX);
83                         BT_DBG("FRIENDLY NAME: [%s]", dev_info->alias);
84                         break;
85                 }
86                 case BT_PROPERTY_REMOTE_PAIRED: {
87                         dev_info->is_bonded = *((unsigned char*)properties[i].val);
88                         BT_DBG("BONDED [%d]", dev_info->is_bonded);
89                         break;
90                 }
91                 case BT_PROPERTY_REMOTE_CONNECTED: {
92                         dev_info->is_connected = *((int*)properties[i].val);
93                         BT_DBG("CONNECTED [%d]", dev_info->is_connected);
94                         break;
95                 }
96                 case BT_PROPERTY_REMOTE_TRUST: {
97                         dev_info->is_trusted = *((unsigned char*)properties[i].val);
98                         BT_DBG("TRUSTED [%d]", dev_info->is_trusted);
99                         break;
100                 }
101                 case BT_PROPERTY_REMOTE_RSSI: {
102                         dev_info->rssi = *((int *)properties[i].val);
103                         BT_DBG("RSSI: %d", dev_info->rssi);
104                         break;
105                 }
106                 case BT_PROPERTY_UUIDS: {
107                         uuids  = (service_uuid_t *)properties[i].val;
108                         BT_DBG("Length of properties from HAL [%d]", properties[i].len);
109                         uuid_count = properties[i].len/sizeof(bt_uuid_t);
110                         table_len += uuid_count;
111                         for (; tmp_uuid_cnt < table_len; tmp_uuid_cnt++) {
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.BT_PROPERTY_UUIDS:%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_BLE_ADV_DATA: {
135                         if (adv_info) {
136                                 adv_info->adv_data = properties[i].val;
137                                 adv_info->len = properties[i].len;
138                         }
139                         BT_DBG("----Advertising Data Length: %d", properties[i].len);
140                         break;
141                 }
142                 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: {
143                         BT_INFO("BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: Not Handled!!");
144                         break;
145                 }
146                 case BT_PROPERTY_SERVICE_RECORD: {
147                         BT_INFO("BT_PROPERTY_SERVICE_RECORD: Not Handled!!");
148                         break;
149                 }
150                 case BT_PROPERTY_REMOTE_IS_ALIAS_SET: {
151                         dev_info->is_alias_set = *((unsigned char*)properties[i].val);
152                         BT_DBG("IS_ALIAS_SET [%d]", dev_info->is_alias_set);
153                         break;
154                 }
155                 default:
156                         BT_WARN("Property not handled");
157                         break;
158                 }
159         }
160 }
161
162 oal_status_t convert_to_oal_status(bt_status_t status)
163 {
164         oal_status_t ret = OAL_STATUS_INTERNAL_ERROR;
165
166         switch (status) {
167         case BT_STATUS_SUCCESS:
168         case BT_STATUS_DONE:
169                 ret = OAL_STATUS_SUCCESS;
170                 break;
171         case BT_STATUS_NOT_READY:
172                 ret = OAL_STATUS_NOT_READY;
173                 break;
174         case BT_STATUS_BUSY:
175                 ret = OAL_STATUS_BUSY;
176                 break;
177         case BT_STATUS_PARM_INVALID:
178                 ret = OAL_STATUS_INVALID_PARAM;
179                 break;
180         case BT_STATUS_RMT_DEV_DOWN:
181                 ret = OAL_STATUS_RMT_DEVICE_DOWN;
182                 break;
183         case BT_STATUS_AUTH_FAILURE:
184                 ret = OAL_STATUS_AUTH_FAILED;
185                 break;
186         case BT_STATUS_UNSUPPORTED:
187                 ret = OAL_STATUS_NOT_SUPPORT;
188                 break;
189         case BT_STATUS_UNHANDLED:
190         case BT_STATUS_FAIL:
191         case BT_STATUS_NOMEM:
192         default:
193                 ret = OAL_STATUS_INTERNAL_ERROR;
194                 break;
195         }
196         return ret;
197 }
198
199 static const char * status_str[] = {
200         "BT_STATUS_SUCCESS",
201         "BT_STATUS_FAIL",
202         "BT_STATUS_NOT_READY",
203         "BT_STATUS_NOMEM",
204         "BT_STATUS_BUSY",
205         "BT_STATUS_DONE",
206         "BT_STATUS_UNSUPPORTED",
207         "BT_STATUS_PARM_INVALID",
208         "BT_STATUS_UNHANDLED",
209         "BT_STATUS_AUTH_FAILURE",
210         "BT_STATUS_RMT_DEV_DOWN"
211 };
212
213 int check_duplicate_uuid(oal_uuid_t *table, oal_uuid_t toMatch, int table_len)
214 {
215         int i;
216         int ret = 1;
217
218         for (i = 0; i < table_len; i++) {
219                 ret = memcmp(table[i].uuid, toMatch.uuid, 16);
220                 if (ret == 0)
221                         break;
222         }
223         return ret;
224 }
225
226 const char* status2string(bt_status_t status)
227 {
228         if (status <= BT_STATUS_RMT_DEV_DOWN)
229                 return status_str[status];
230         else {
231                 BT_ERR("Invalid BT status from stack");
232                 return "BT_STATUS_UNKNOWN";
233         }
234 }