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