2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <bluetooth.h>
26 #include "oal-event.h"
27 #include "oal-internal.h"
28 #include "oal-common.h"
29 #include "oal-manager.h"
30 #include "oal-utils.h"
31 #include "oal-device-mgr.h"
33 static const bt_interface_t * blued_api;
35 void device_mgr_init(const bt_interface_t * stack_if)
40 void device_mgr_cleanup(void)
46 oal_status_t device_query_attributes(bt_address_t *addr)
51 CHECK_OAL_INITIALIZED();
53 OAL_CHECK_PARAMETER(addr, return);
55 API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
57 res = blued_api->get_remote_device_properties((bt_bdaddr_t *)addr);
58 if (res != BT_STATUS_SUCCESS) {
59 BT_ERR("get_remote_device_properties error: [%s]", status2string(res));
60 return convert_to_oal_status(res);
63 return OAL_STATUS_SUCCESS;
66 oal_status_t device_set_alias(bt_address_t * addr, char * alias)
72 CHECK_OAL_INITIALIZED();
73 OAL_CHECK_PARAMETER(addr, return);
74 OAL_CHECK_PARAMETER(alias, return);
76 API_TRACE("%s ->Alias: %s", bdt_bd2str(addr, &bdstr), alias);
78 prop.type = BT_PROPERTY_REMOTE_FRIENDLY_NAME;
79 prop.len = strlen(alias);
81 res = blued_api->set_remote_device_property((bt_bdaddr_t*)addr, &prop);
82 if (res != BT_STATUS_SUCCESS) {
83 BT_ERR("set_remote_device_property error: [%s]", status2string(res));
84 BT_ERR("Alias: %s", alias);
85 return convert_to_oal_status(res);
88 return OAL_STATUS_SUCCESS;
91 oal_status_t device_create_bond(bt_address_t *addr, connection_type_e transport)
96 CHECK_OAL_INITIALIZED();
98 OAL_CHECK_PARAMETER(addr, return);
100 API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
102 res = blued_api->create_bond((bt_bdaddr_t *)addr, (unsigned char)transport);
103 if (res != BT_STATUS_SUCCESS) {
104 BT_ERR("create_bond error: [%s]", status2string(res));
105 return convert_to_oal_status(res);
108 return OAL_STATUS_SUCCESS;
111 oal_status_t device_destroy_bond(bt_address_t * addr)
116 CHECK_OAL_INITIALIZED();
118 OAL_CHECK_PARAMETER(addr, return);
120 API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
122 res = blued_api->remove_bond((bt_bdaddr_t *)addr);
123 if (res != BT_STATUS_SUCCESS) {
124 BT_ERR("remove_bond error: [%s]", status2string(res));
125 return convert_to_oal_status(res);
128 return OAL_STATUS_SUCCESS;
131 void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
132 int num_properties, bt_property_t *properties)
135 gpointer event_data = NULL;
136 remote_device_t *dev_info;
137 ble_adv_data_t adv_info;
141 if(BT_STATUS_SUCCESS != status) {
142 BT_ERR("[%s]status: %d", bdt_bd2str((bt_address_t*)bd_addr, &bdstr), status);
146 BT_DBG("[%s]", bdt_bd2str((bt_address_t*)bd_addr, &bdstr));
147 dev_info = g_new0(remote_device_t, 1);
148 memcpy(dev_info->address.addr, bd_addr->address, 6);
149 parse_device_properties(num_properties, properties, dev_info, &adv_info);
151 if(num_properties == 1) {
152 /* For one particular property a dedicated event to be sent */
153 switch(properties[0].type) {
154 case BT_PROPERTY_BDNAME:
155 event = OAL_EVENT_DEVICE_NAME;
156 event_data = dev_info;
157 send_event_trace(event, event_data, sizeof(remote_device_t),
158 (bt_address_t*)bd_addr, "Name: %s", dev_info->name);
160 case BT_PROPERTY_UUIDS: {
161 event_dev_services_t *services_info;
162 bt_uuid_t *uuids = (bt_uuid_t *) properties[0].val;
164 services_info = g_malloc(sizeof(event_dev_services_t) + properties[0].len);
165 services_info->address = dev_info->address;
166 memcpy(services_info->service_list, uuids, properties[0].len);
167 services_info->num = properties[0].len/sizeof(bt_uuid_t);
168 event = OAL_EVENT_DEVICE_SERVICES;
169 event_data = services_info;
170 size = sizeof(event_dev_services_t) + properties[0].len;
175 BT_ERR("Single Property [%d] not handled", properties[0].type);
180 event_dev_properties_t *dev_props_event = g_new0(event_dev_properties_t, 1);
181 if (dev_info->type != DEV_TYPE_BREDR) {
184 BT_INFO("BLE Device");
185 /* BLE Single or DUAL mode found, so it should have Adv data */
186 dev_props_event->adv_len = adv_info.len;
187 if(dev_props_event->adv_len > 0)
188 memcpy(dev_props_event->adv_data,
189 adv_info.adv_data, adv_info.len);
191 for (i = 0; i < dev_props_event->adv_len; i++)
192 BT_INFO("Adv Data[%d] = [0x%x]",
193 i, dev_props_event->adv_data[i]);
194 memcpy(&dev_props_event->device_info,
195 dev_info, sizeof(remote_device_t));
197 BT_INFO("BREDR type Device");
198 memcpy(&dev_props_event->device_info,
199 dev_info, sizeof(remote_device_t));
202 event_data = dev_props_event;
203 event = OAL_EVENT_DEVICE_PROPERTIES;
204 size = sizeof(event_dev_properties_t);
207 send_event_bda_trace(event, event_data, size, (bt_address_t*)bd_addr);
210 void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
211 bt_bond_state_t state)
213 bt_address_t * address = g_new0(bt_address_t, 1);
217 BT_DBG("status: %d, state: %d", status, state);
219 memcpy(address->addr, bd_addr->address, 6);
222 case BT_BOND_STATE_BONDED:
223 event = OAL_EVENT_DEVICE_BONDING_SUCCESS;
225 case BT_BOND_STATE_NONE:
226 /* Reaches both when bonding removed or bonding cancelled */
227 if (BT_STATUS_SUCCESS != status) {
228 event_dev_bond_failed_t * bond_fail_info = g_new0(event_dev_bond_failed_t, 1);
229 bond_fail_info->status = convert_to_oal_status(status);
230 bond_fail_info->address = *address;
231 size = sizeof(event_dev_bond_failed_t);
232 send_event_bda_trace(OAL_EVENT_DEVICE_BONDING_FAILED, bond_fail_info, size, (bt_address_t*)bd_addr);
236 event = OAL_EVENT_DEVICE_BONDING_REMOVED;
238 case BT_BOND_STATE_BONDING:
242 BT_ERR("Unexpected Bond state %d", state);
246 send_event_bda_trace(event, address, size, (bt_address_t*)bd_addr);