2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/prctl.h>
29 #include <arpa/inet.h>
31 #include <bluetooth.h>
32 #include "oal-internal.h"
33 #include "oal-common.h"
35 #define BT_UUID_STRING_SIZE 37
36 #define BT_UUID_LENGTH_MAX 16
38 void parse_device_properties(int num_properties, bt_property_t *properties,
39 remote_device_t *dev_info, ble_adv_data_t * adv_info)
42 int uuid_count = 0, table_len = 0;
45 char lcl_uuid[BT_UUID_STRING_MAX];
47 bt_bdaddr_t * addr = {0};
48 bt_bdname_t *name = {0};
49 service_uuid_t *uuids;
50 bt_device_type_t dev_type;
52 BT_DBG("num_properties: %d", num_properties);
54 for (i = 0; i < num_properties; i++) {
55 BT_DBG("===>Prop type: %d, Len: %d<===", properties[i].type, properties[i].len);
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]);
67 case BT_PROPERTY_CLASS_OF_DEVICE: {
68 dev_info->cod = *((int *)properties[i].val);
69 BT_DBG("CLASS: 0x%06x", dev_info->cod);
72 case BT_PROPERTY_BDNAME: {
73 name = properties[i].val;
75 g_strlcpy(dev_info->name, (const gchar *)name->name, BT_DEVICE_NAME_LENGTH_MAX);
76 BT_DBG("NAME: %s", dev_info->name);
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);
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);
91 case BT_PROPERTY_REMOTE_CONNECTED: {
92 dev_info->is_connected = *((int*)properties[i].val);
93 BT_DBG("CONNECTED [%d]", dev_info->is_connected);
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);
101 case BT_PROPERTY_REMOTE_RSSI: {
102 dev_info->rssi = *((int *)properties[i].val);
103 BT_DBG("RSSI: %d", dev_info->rssi);
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);
116 memcpy(&dev_info->uuid[dev_info->uuid_count++].uuid,
117 &uuids[tmp_uuid_cnt].uuid, 16);
119 BT_DBG("Duplicate UUID found:%s\n", lcl_uuid);
121 BT_DBG("%d.BT_PROPERTY_UUIDS:%s", dev_info->uuid_count, lcl_uuid);
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
134 case BT_PROPERTY_REMOTE_BLE_ADV_DATA: {
136 adv_info->adv_data = properties[i].val;
137 adv_info->len = properties[i].len;
139 BT_DBG("----Advertising Data Length: %d", properties[i].len);
142 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: {
143 BT_INFO("BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: Not Handled!!");
146 case BT_PROPERTY_SERVICE_RECORD: {
147 BT_INFO("BT_PROPERTY_SERVICE_RECORD: Not Handled!!");
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);
156 BT_WARN("Property not handled");
162 oal_status_t convert_to_oal_status(bt_status_t status)
164 oal_status_t ret = OAL_STATUS_INTERNAL_ERROR;
167 case BT_STATUS_SUCCESS:
169 ret = OAL_STATUS_SUCCESS;
171 case BT_STATUS_NOT_READY:
172 ret = OAL_STATUS_NOT_READY;
175 ret = OAL_STATUS_BUSY;
177 case BT_STATUS_PARM_INVALID:
178 ret = OAL_STATUS_INVALID_PARAM;
180 case BT_STATUS_RMT_DEV_DOWN:
181 ret = OAL_STATUS_RMT_DEVICE_DOWN;
183 case BT_STATUS_AUTH_FAILURE:
184 ret = OAL_STATUS_AUTH_FAILED;
186 case BT_STATUS_UNSUPPORTED:
187 ret = OAL_STATUS_NOT_SUPPORT;
189 case BT_STATUS_CONN_TOUT:
190 ret = OAL_STATUS_LINK_LOSS;
193 case BT_STATUS_CONN_TERM_LOCAL_HOST:
194 ret = OAL_STATUS_CONN_TERM_LOCAL_HOST;
196 case BT_STATUS_CONN_TERM_RMT_HOST:
197 ret = OAL_STATUS_CONN_TERM_RMT_HOST;
200 case BT_STATUS_UNHANDLED:
202 case BT_STATUS_NOMEM:
204 ret = OAL_STATUS_INTERNAL_ERROR;
210 static const char * status_str[] = {
213 "BT_STATUS_NOT_READY",
217 "BT_STATUS_UNSUPPORTED",
218 "BT_STATUS_PARM_INVALID",
219 "BT_STATUS_UNHANDLED",
220 "BT_STATUS_AUTH_FAILURE",
221 "BT_STATUS_RMT_DEV_DOWN"
224 int check_duplicate_uuid(oal_uuid_t *table, oal_uuid_t toMatch, int table_len)
229 for (i = 0; i < table_len; i++) {
230 ret = memcmp(table[i].uuid, toMatch.uuid, 16);
237 const char* status2string(bt_status_t status)
239 if (status <= BT_STATUS_RMT_DEV_DOWN)
240 return status_str[status];
242 BT_ERR("Invalid BT status from stack");
243 return "BT_STATUS_UNKNOWN";