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