[Adaprt: OAL] Implement create and remove bond
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-device-mgr.c
1 /*
2  * Open Adaptation Layer (OAL)
3  *
4  * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdio.h>
21 #include <dlog.h>
22 #include <string.h>
23
24 #include <bluetooth.h>
25
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"
32
33 static const bt_interface_t * blued_api;
34
35 void device_mgr_init(const bt_interface_t * stack_if)
36 {
37         blued_api = stack_if;
38 }
39
40 void device_mgr_cleanup(void)
41 {
42         BT_DBG();
43         blued_api = NULL;
44 }
45
46 oal_status_t device_query_attributes(bt_address_t *addr)
47 {
48         int res;
49         bdstr_t bdstr;
50
51         CHECK_OAL_INITIALIZED();
52
53         OAL_CHECK_PARAMETER(addr, return);
54
55         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
56
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);
61         }
62
63         return OAL_STATUS_SUCCESS;
64 }
65
66 oal_status_t device_set_alias(bt_address_t * addr, char * alias)
67 {
68         int res;
69         bt_property_t prop;
70         bdstr_t bdstr;
71
72         CHECK_OAL_INITIALIZED();
73         OAL_CHECK_PARAMETER(addr, return);
74         OAL_CHECK_PARAMETER(alias, return);
75
76         API_TRACE("%s ->Alias: %s", bdt_bd2str(addr, &bdstr), alias);
77
78         prop.type = BT_PROPERTY_REMOTE_FRIENDLY_NAME;
79         prop.len = strlen(alias);
80         prop.val = 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);
86         }
87
88         return OAL_STATUS_SUCCESS;
89 }
90
91 oal_status_t device_create_bond(bt_address_t *addr, connection_type_e transport)
92 {
93         int res;
94         bdstr_t bdstr;
95
96         CHECK_OAL_INITIALIZED();
97
98         OAL_CHECK_PARAMETER(addr, return);
99
100         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
101
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);
106         }
107
108         return OAL_STATUS_SUCCESS;
109 }
110
111 oal_status_t device_destroy_bond(bt_address_t * addr)
112 {
113         int res;
114         bdstr_t bdstr;
115
116         CHECK_OAL_INITIALIZED();
117
118         OAL_CHECK_PARAMETER(addr, return);
119
120         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
121
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);
126         }
127
128         return OAL_STATUS_SUCCESS;
129 }
130
131 void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
132                 int num_properties, bt_property_t *properties)
133 {
134         oal_event_t event;
135         gpointer event_data = NULL;
136         remote_device_t *dev_info;
137         ble_adv_data_t adv_info;
138         gsize size = 0;
139         bdstr_t bdstr;
140
141         if(BT_STATUS_SUCCESS != status) {
142                 BT_ERR("[%s]status: %d", bdt_bd2str((bt_address_t*)bd_addr, &bdstr), status);
143                 return;
144         }
145
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);
150
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);
159                         return;
160                 case BT_PROPERTY_UUIDS: {
161                         event_dev_services_t *services_info;
162                         bt_uuid_t *uuids = (bt_uuid_t *) properties[0].val;
163
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;
171                         g_free(dev_info);
172                         break;
173                 }
174                 default:
175                         BT_ERR("Single Property [%d] not handled", properties[0].type);
176                         g_free(dev_info);
177                         return;
178                 }
179         } else {
180                 event_dev_properties_t *dev_props_event = g_new0(event_dev_properties_t, 1);
181                 if (dev_info->type != DEV_TYPE_BREDR) {
182                         int i;
183
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);
190
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));
196                 } else {
197                         BT_INFO("BREDR type Device");
198                         memcpy(&dev_props_event->device_info,
199                                 dev_info, sizeof(remote_device_t));
200                 }
201
202                 event_data = dev_props_event;
203                 event = OAL_EVENT_DEVICE_PROPERTIES;
204                 size = sizeof(event_dev_properties_t);
205         }
206
207         send_event_bda_trace(event, event_data, size, (bt_address_t*)bd_addr);
208 }
209
210 void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
211                                         bt_bond_state_t state)
212 {
213         bt_address_t * address = g_new0(bt_address_t, 1);
214         oal_event_t event;
215         gsize size = 0;
216
217         BT_DBG("status: %d, state: %d", status, state);
218
219         memcpy(address->addr, bd_addr->address, 6);
220
221         switch(state) {
222                 case BT_BOND_STATE_BONDED:
223                         event = OAL_EVENT_DEVICE_BONDING_SUCCESS;
224                         break;
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);
233                                 g_free(address);
234                                 return;
235                         } else
236                                 event = OAL_EVENT_DEVICE_BONDING_REMOVED;
237                         break;
238                 case BT_BOND_STATE_BONDING:
239                         g_free(address);
240                         return;
241                 default:
242                         BT_ERR("Unexpected Bond state %d", state);
243                         g_free(address);
244                         return;
245         }
246         send_event_bda_trace(event, address, size, (bt_address_t*)bd_addr);
247 }