[Adapt: OAL] Implement ACL connection state changed events
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-adapter-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 <stdlib.h>
22 #include <dlog.h>
23 #include <string.h>
24 #include <vconf.h>
25 #include <sys/wait.h>
26
27 #include <bluetooth.h>
28
29 #include "oal-event.h"
30 #include "oal-internal.h"
31 #include "oal-manager.h"
32 #include "oal-hardware.h"
33 #include "oal-common.h"
34 #include "oal-utils.h"
35
36 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
37
38 static const bt_interface_t * blued_api;
39
40 static bt_address_t local_address;
41 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
42 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
43 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
44 static int discoverable_timeout = 0;
45
46 /* Forward declarations */
47 oal_status_t convert_to_oal_status(bt_status_t status);
48 static gboolean retry_enable_adapter(gpointer data);
49 oal_status_t oal_mgr_init_internal(void);
50
51
52 /* Callback registered with Stack */
53 static void cb_adapter_state_change(bt_state_t status);
54 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
55 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
56 static void cb_adapter_properties (bt_status_t status,
57                 int num_properties, bt_property_t *properties);
58 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
59                 int num_properties, bt_property_t *properties);
60 extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
61                                         bt_bond_state_t state);
62 extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
63                                             bt_acl_state_t state);
64
65 static bt_callbacks_t callbacks = {
66         sizeof(callbacks),
67         cb_adapter_state_change,
68         cb_adapter_properties,
69         cb_device_properties,
70         cb_adapter_device_found,
71         cb_adapter_discovery_state_changed,
72         NULL, /* pin_request_callback */
73         NULL, /* ssp_request_callback */
74         cb_device_bond_state_changed,
75         cb_device_acl_state_changed,
76         NULL, /* callback_thread_event */
77         NULL, /* dut_mode_recv_callback */
78         NULL, /* le_test_mode_callback*/
79         NULL, /* energy_info_callback */
80 };
81
82 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
83 {
84         int ret;
85         blued_api = stack_if;
86
87         ret = blued_api->init(&callbacks);
88
89         if (ret != BT_STATUS_SUCCESS) {
90                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
91                 blued_api->cleanup();
92                 return convert_to_oal_status(ret);
93         }
94
95         return OAL_STATUS_SUCCESS;
96 }
97
98 const bt_interface_t* adapter_get_stack_interface(void)
99 {
100         return blued_api;
101 }
102
103 void adapter_mgr_cleanup(void)
104 {
105         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
106         BT_DBG();
107 }
108
109 oal_status_t adapter_enable(void)
110 {
111         int ret = BT_STATUS_SUCCESS;
112
113         API_TRACE();
114         CHECK_OAL_INITIALIZED();
115         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
116                 g_timeout_add(200, retry_enable_adapter, NULL);
117                 return OAL_STATUS_PENDING;
118         }
119
120         ret = blued_api->enable();
121
122         if (ret != BT_STATUS_SUCCESS) {
123                 BT_ERR("Enable failed: [%s]", status2string(ret));
124                 return convert_to_oal_status(ret);
125         }
126
127         return OAL_STATUS_SUCCESS;
128 }
129
130 oal_status_t adapter_disable(void)
131 {
132         int ret;
133
134         API_TRACE();
135
136         CHECK_OAL_INITIALIZED();
137
138         ret = blued_api->disable();
139
140         if (ret != BT_STATUS_SUCCESS) {
141                 BT_ERR("Disable failed: [%s]", status2string(ret));
142                 return convert_to_oal_status(ret);
143         }
144         return OAL_STATUS_SUCCESS;
145 }
146
147 oal_status_t adapter_start_inquiry(void)
148 {
149         int ret;
150
151         API_TRACE();
152
153         CHECK_OAL_INITIALIZED();
154
155         ret = blued_api->start_discovery();
156         if (ret != BT_STATUS_SUCCESS) {
157                 BT_ERR("start_discovery failed: [%s]", status2string(ret));
158                 return convert_to_oal_status(ret);
159         }
160
161         return OAL_STATUS_SUCCESS;
162 }
163
164 oal_status_t adapter_stop_inquiry(void)
165 {
166         int ret;
167
168         API_TRACE();
169
170         CHECK_OAL_INITIALIZED();
171
172         ret = blued_api->cancel_discovery();
173         if (ret != BT_STATUS_SUCCESS) {
174                 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
175                 return convert_to_oal_status(ret);
176         }
177
178         return OAL_STATUS_SUCCESS;
179 }
180
181 /* Callbacks from Stack */
182 static void cb_adapter_state_change(bt_state_t status)
183 {
184         BT_DBG("+");
185         oal_event_t event;
186
187         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
188
189         send_event(event, NULL, 0);
190 }
191
192 static gboolean retry_enable_adapter(gpointer data)
193 {
194         adapter_enable();
195         return FALSE;
196 }
197
198 oal_status_t adapter_get_address(void)
199 {
200         int ret;
201
202         API_TRACE();
203         CHECK_OAL_INITIALIZED();
204
205         ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
206         if (ret != BT_STATUS_SUCCESS) {
207                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
208                 return convert_to_oal_status(ret);
209         }
210
211         return OAL_STATUS_SUCCESS;
212 }
213
214 oal_status_t adapter_get_version(void)
215 {
216         int ret;
217
218         API_TRACE();
219         CHECK_OAL_INITIALIZED();
220
221         ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
222         if (ret != BT_STATUS_SUCCESS) {
223                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
224                 return convert_to_oal_status(ret);
225         }
226
227         return OAL_STATUS_SUCCESS;
228 }
229
230 oal_status_t adapter_get_name(void)
231 {
232         int ret;
233
234         CHECK_OAL_INITIALIZED();
235
236         API_TRACE();
237
238         ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
239         if (ret != BT_STATUS_SUCCESS) {
240                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
241                 return convert_to_oal_status(ret);
242         }
243
244         return OAL_STATUS_SUCCESS;
245 }
246
247 oal_status_t adapter_set_name(char * name)
248 {
249         int ret;
250         bt_property_t prop;
251
252         CHECK_OAL_INITIALIZED();
253
254         OAL_CHECK_PARAMETER(name, return);
255         API_TRACE("Name: %s", name);
256
257         prop.type = BT_PROPERTY_BDNAME;
258         prop.len = strlen(name);
259         prop.val = name;
260
261         ret = blued_api->set_adapter_property(&prop);
262         if (ret != BT_STATUS_SUCCESS) {
263                 BT_ERR("set_adapter_property: [%s]", status2string(ret));
264                 ret = OAL_STATUS_INTERNAL_ERROR;
265         } else
266                 ret = OAL_STATUS_SUCCESS;
267
268         return ret;
269 }
270
271 oal_status_t adapter_is_discoverable(int *p_discoverable)
272 {
273         OAL_CHECK_PARAMETER(p_discoverable, return);
274
275         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
276
277         API_TRACE("%d", *p_discoverable);
278
279         return OAL_STATUS_SUCCESS;
280 }
281
282 oal_status_t adapter_is_connectable(int *p_connectable)
283 {
284         OAL_CHECK_PARAMETER(p_connectable, return);
285
286         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
287                 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
288
289         API_TRACE("%d", *p_connectable);
290
291         return OAL_STATUS_SUCCESS;
292 }
293
294 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
295 {
296         API_TRACE("%d", discoverable_timeout);
297
298         *p_timeout = discoverable_timeout;
299
300         return OAL_STATUS_SUCCESS;
301 }
302
303 oal_status_t adapter_get_service_uuids(void)
304 {
305         int ret;
306
307         CHECK_OAL_INITIALIZED();
308
309         API_TRACE();
310
311         ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
312         if (ret != BT_STATUS_SUCCESS) {
313                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
314                 return convert_to_oal_status(ret);
315         }
316
317         return OAL_STATUS_SUCCESS;
318 }
319
320 oal_status_t adapter_get_bonded_devices(void)
321 {
322         int ret;
323
324         CHECK_OAL_INITIALIZED();
325
326         API_TRACE();
327
328         ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
329         if (ret != BT_STATUS_SUCCESS) {
330                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
331                 return convert_to_oal_status(ret);
332         }
333
334         return OAL_STATUS_SUCCESS;
335 }
336
337 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
338 {
339         bt_property_t prop;
340         int res;
341
342         BT_DBG("+");
343
344         CHECK_OAL_INITIALIZED();
345
346         prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
347         prop.len = sizeof(bt_scan_mode_t);
348         prop.val = &mode;
349         res = blued_api->set_adapter_property(&prop);
350         if (res != BT_STATUS_SUCCESS) {
351                 BT_ERR("set scan mode failed [%s]", status2string(res));
352                 return convert_to_oal_status(res);
353         }
354
355         BT_DBG("-");
356         return OAL_STATUS_SUCCESS;
357 }
358
359 oal_status_t adapter_set_connectable(int connectable)
360 {
361         bt_scan_mode_t mode;
362
363         API_TRACE("%d", connectable);
364
365         CHECK_OAL_INITIALIZED();
366
367         mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
368
369         return set_scan_mode(mode);
370 }
371
372 oal_status_t adapter_set_discoverable(void)
373 {
374         CHECK_OAL_INITIALIZED();
375         API_TRACE();
376
377         return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
378 }
379
380 oal_status_t adapter_set_discoverable_timeout(int timeout)
381 {
382         bt_property_t prop;
383         int res;
384         uint32_t prop_val = timeout;
385
386         CHECK_OAL_INITIALIZED();
387         API_TRACE("%d", timeout);
388
389         prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
390         prop.len = sizeof(prop_val);
391         prop.val = &prop_val;
392         res = blued_api->set_adapter_property(&prop);
393         if (res != BT_STATUS_SUCCESS) {
394                 BT_ERR("set_adapter_property failed [%s]", status2string(res));
395                 return convert_to_oal_status(res);
396         }
397         return OAL_STATUS_SUCCESS;
398 }
399
400 static void cb_adapter_properties(bt_status_t status,
401                                                int num_properties,
402                                                bt_property_t *properties)
403 {
404         int i;
405
406         BT_DBG("status: %d, count: %d", status, num_properties);
407
408         if (status != BT_STATUS_SUCCESS) {
409                 if (num_properties == 1) {
410                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
411                                 status2string(status), num_properties, properties[num_properties-1].type);
412                 } else {
413                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
414                 }
415                 return;
416         }
417
418         for (i = 0; i < num_properties; i++) {
419                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
420                 switch (properties[i].type) {
421                 case BT_PROPERTY_VERSION: {
422                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
423                         local_version[properties[i].len] = '\0';
424
425                         BT_DBG("Version: %s", local_version);
426                         /* Send event to application */
427                         if (num_properties == 1) {
428                                 char *adapter_ver = g_strdup(local_version);
429
430                                 /* Application has requested this property SET/GET hence send EVENT */
431                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
432                         }
433                         break;
434                 }
435                 case BT_PROPERTY_BDNAME: {
436                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
437                         local_name[properties[i].len] = '\0';
438
439                         BT_DBG("Name: %s", local_name);
440                         /* Send event to application */
441                         if (num_properties == 1) {
442                                 char * adap_name = g_strdup(local_name);
443
444                                 /* Application has requested this property SET/GET hence send EVENT */
445                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
446                         }
447                         break;
448                 }
449                 case BT_PROPERTY_BDADDR: {
450                         bt_bdaddr_t * addr;
451
452                         addr =  properties[i].val;
453                         memcpy(local_address.addr, addr->address, 6);
454                         if (num_properties == 1) {
455                                 /* Application has requested this property SET/GET hence send EVENT */
456                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
457                                                 g_memdup(&local_address, sizeof(local_address)),
458                                                 sizeof(local_address));
459                         }
460                         break;
461                 }
462                 case BT_PROPERTY_UUIDS: {
463                         int num_uuid;
464
465                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
466
467                         BT_DBG("num_uuid: %d", num_uuid);
468
469                         /* Send event to application */
470                         if (num_properties == 1) {
471                                 event_adapter_services_t *uuids_event;
472
473                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
474                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
475                                 uuids_event->num = num_uuid;
476
477                                 /* Application has requested this property SET/GET hence send EVENT */
478                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
479                                                 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
480                         }
481                         break;
482                 }
483                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
484                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
485
486                         BT_INFO("Scan mode (%d)", cur_mode);
487
488                         scan_mode = cur_mode;
489
490                         /* Send event to application */
491                         if (num_properties == 1) {
492                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
493
494                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
495                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
496                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
497                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
498
499                                 /* Application has requested this property SET/GET hence send EVENT */
500                                 send_event(event, NULL, 0);
501                         }
502                         break;
503                 }
504                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
505                         int timeout;
506
507                         timeout = *((uint32_t*)properties[i].val);
508
509                         BT_INFO("Discoverability timeout: %d", timeout);
510                         discoverable_timeout = timeout;
511
512                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
513                                         g_memdup(properties[i].val, sizeof(uint32_t)),
514                                         sizeof(uint32_t));
515                         break;
516                 }
517                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
518                         int j;
519                         int num_bonded;
520                         bt_bdaddr_t *bonded_addr_list;
521                         event_device_list_t *event_data;
522
523                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
524                         BT_DBG("num_bonded %d", num_bonded);
525
526                         if (num_properties > 1) /* No explicit req for this prop, ignore */
527                                 break;
528
529                         bonded_addr_list = properties[i].val;
530                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
531                         event_data->num = num_bonded;
532
533                         for (j = 0; j < num_bonded; j++)
534                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
535
536                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
537                                         event_data, (num_bonded * sizeof(bt_bdaddr_t)));
538                         break;
539                 }
540                 default:
541                          BT_WARN("Unhandled property: %d", properties[i].type);
542                          break;
543                 }
544         }
545 }
546
547 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
548 {
549         oal_event_t event;
550
551         event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
552
553         BT_DBG("%d", state);
554         send_event(event, NULL, 0);
555 }
556
557 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
558 {
559         remote_device_t dev_info;
560         ble_adv_data_t adv_info;
561         oal_event_t event;
562         gpointer event_data;
563         gsize size = 0;
564         BT_DBG("+");
565
566         if (num_properties == 0) {
567                 BT_ERR("Unexpected, properties count is zero!!");
568                 return;
569         }
570
571         memset(&dev_info, 0x00, sizeof(remote_device_t));
572         memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
573
574         print_bt_properties(num_properties, properties);
575         parse_device_properties(num_properties, properties, &dev_info, &adv_info);
576
577         BT_INFO("number of properties= [%d] ", num_properties, size);
578
579         if (dev_info.type != DEV_TYPE_BREDR) {
580                 /* BLE Single or DUAL mode found, so it should have Adv data */
581                 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
582
583                 ble_dev_event->adv_len = adv_info.len;
584
585                 if (adv_info.len > 0 && adv_info.adv_data) {
586                         memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
587                         ble_dev_event->adv_len = adv_info.len;
588                 } else
589                         ble_dev_event->adv_len = 0;
590
591                 ble_dev_event->device_info = dev_info;
592
593                 event_data = ble_dev_event;
594                 size = sizeof(event_ble_dev_found_t);
595                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
596         } else {
597                 /* BREDR device, so No Adv data */
598                 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
599
600                 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
601                 event_data = dev_event;
602                 size = sizeof(remote_device_t);
603                 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
604         }
605
606         send_event(event, event_data, size);
607
608         BT_DBG("-");
609 }