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