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 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]);
63 case BT_PROPERTY_CLASS_OF_DEVICE: {
64 dev_info->cod = *((int *)properties[i].val);
65 BT_DBG("CLASS: 0x%06x", dev_info->cod);
68 case BT_PROPERTY_BDNAME: {
69 name = properties[i].val;
71 g_strlcpy(dev_info->name, (const gchar *)name->name, BT_DEVICE_NAME_LENGTH_MAX);
72 BT_DBG("NAME: %s", dev_info->name);
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);
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);
87 case BT_PROPERTY_REMOTE_CONNECTED: {
88 dev_info->is_connected = *((int*)properties[i].val);
89 BT_DBG("CONNECTED [%d]", dev_info->is_connected);
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);
97 case BT_PROPERTY_REMOTE_RSSI: {
98 dev_info->rssi = *((int *)properties[i].val);
99 BT_DBG("RSSI: %d", dev_info->rssi);
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);
112 memcpy(&dev_info->uuid[dev_info->uuid_count++].uuid,
113 &uuids[tmp_uuid_cnt].uuid, 16);
115 BT_DBG("Duplicate UUID found:%s\n", lcl_uuid);
117 BT_DBG("[%d] %s", dev_info->uuid_count, lcl_uuid);
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
130 case BT_PROPERTY_REMOTE_BLE_ADV_DATA: {
132 adv_info->adv_data = properties[i].val;
133 adv_info->len = properties[i].len;
135 BT_DBG("----Advertising Data Length: %d", properties[i].len);
138 case BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: {
139 BT_INFO("BT_PROPERTY_REMOTE_DEVICE_TIMESTAMP: Not Handled!!");
142 case BT_PROPERTY_SERVICE_RECORD: {
143 BT_INFO("BT_PROPERTY_SERVICE_RECORD: Not Handled!!");
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);
152 BT_WARN("Property not handled");
158 oal_status_t convert_to_oal_status(bt_status_t status)
160 oal_status_t ret = OAL_STATUS_INTERNAL_ERROR;
163 case BT_STATUS_SUCCESS:
165 ret = OAL_STATUS_SUCCESS;
167 case BT_STATUS_NOT_READY:
168 ret = OAL_STATUS_NOT_READY;
171 ret = OAL_STATUS_BUSY;
173 case BT_STATUS_PARM_INVALID:
174 ret = OAL_STATUS_INVALID_PARAM;
176 case BT_STATUS_RMT_DEV_DOWN:
177 ret = OAL_STATUS_RMT_DEVICE_DOWN;
179 case BT_STATUS_AUTH_FAILURE:
180 ret = OAL_STATUS_AUTH_FAILED;
182 case BT_STATUS_UNSUPPORTED:
183 ret = OAL_STATUS_NOT_SUPPORT;
185 case BT_STATUS_NOT_PAIRED:
186 ret = OAL_STATUS_NOT_PAIRED;
188 case BT_STATUS_CONN_TOUT:
189 ret = OAL_STATUS_LINK_LOSS;
190 case BT_STATUS_ALREADY_CONNECT:
191 ret = OAL_STATUS_ALREADY_CONNECT;
194 case BT_STATUS_CONN_TERM_LOCAL_HOST:
195 ret = OAL_STATUS_CONN_TERM_LOCAL_HOST;
197 case BT_STATUS_CONN_TERM_RMT_HOST:
198 ret = OAL_STATUS_CONN_TERM_RMT_HOST;
201 case BT_STATUS_UNHANDLED:
203 case BT_STATUS_NOMEM:
205 ret = OAL_STATUS_INTERNAL_ERROR;
211 static const char * status_str[] = {
214 "BT_STATUS_NOT_READY",
218 "BT_STATUS_UNSUPPORTED",
219 "BT_STATUS_PARM_INVALID",
220 "BT_STATUS_UNHANDLED",
221 "BT_STATUS_AUTH_FAILURE",
222 "BT_STATUS_RMT_DEV_DOWN"
225 int check_duplicate_uuid(oal_uuid_t *table, oal_uuid_t toMatch, int table_len)
230 for (i = 0; i < table_len; i++) {
231 ret = memcmp(table[i].uuid, toMatch.uuid, 16);
238 const char* status2string(bt_status_t status)
240 if (status <= BT_STATUS_RMT_DEV_DOWN)
241 return status_str[status];
243 BT_ERR("Invalid BT status from stack");
244 return "BT_STATUS_UNKNOWN";