[Adapt] Implement is_service_used API
[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
34 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
35
36 static const bt_interface_t * blued_api;
37
38 static bt_address_t local_address;
39 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
40 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
41 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
42 static int discoverable_timeout = 0;
43
44 /* Forward declarations */
45 const char * status2string(bt_status_t status);
46 oal_status_t convert_to_oal_status(bt_status_t status);
47 void parse_device_properties(int num_properties, bt_property_t *properties,
48                 remote_device_t *dev_info, ble_adv_data_t * adv_info);
49 static gboolean retry_enable_adapter(gpointer data);
50 oal_status_t oal_mgr_init_internal(void);
51
52
53 /* Callback registered with Stack */
54 static void cb_adapter_state_change(bt_state_t status);
55 static void cb_adapter_properties (bt_status_t status,
56                 int num_properties, bt_property_t *properties);
57
58 static bt_callbacks_t callbacks = {
59         sizeof(callbacks),
60         cb_adapter_state_change,
61         cb_adapter_properties,
62         NULL, /* remote_device_properties_callback */
63         NULL, /* device_found_callback */
64         NULL, /* discovery_state_changed_callback */
65         NULL, /* pin_request_callback */
66         NULL, /* ssp_request_callback */
67         NULL, /* bond_state_changed_callback */
68         NULL, /* acl_state_changed_callback */
69         NULL, /* callback_thread_event */
70         NULL, /* dut_mode_recv_callback */
71         NULL, /* le_test_mode_callback*/
72         NULL, /* energy_info_callback */
73 };
74
75 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
76 {
77         int ret;
78         blued_api = stack_if;
79
80         ret = blued_api->init(&callbacks);
81
82         if (ret != BT_STATUS_SUCCESS) {
83                 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
84                 blued_api->cleanup();
85                 return convert_to_oal_status(ret);
86         }
87
88         return OAL_STATUS_SUCCESS;
89 }
90
91 const bt_interface_t* adapter_get_stack_interface(void)
92 {
93         return blued_api;
94 }
95
96 void adapter_mgr_cleanup(void)
97 {
98         /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
99         BT_DBG();
100 }
101
102 oal_status_t adapter_enable(void)
103 {
104         int ret = BT_STATUS_SUCCESS;
105
106         API_TRACE();
107         CHECK_OAL_INITIALIZED();
108         if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
109                 g_timeout_add(200, retry_enable_adapter, NULL);
110                 return OAL_STATUS_PENDING;
111         }
112
113         ret = blued_api->enable();
114
115         if (ret != BT_STATUS_SUCCESS) {
116                 BT_ERR("Enable failed: [%s]", status2string(ret));
117                 return convert_to_oal_status(ret);
118         }
119
120         return OAL_STATUS_SUCCESS;
121 }
122
123 oal_status_t adapter_disable(void)
124 {
125         int ret;
126
127         API_TRACE();
128
129         CHECK_OAL_INITIALIZED();
130
131         ret = blued_api->disable();
132
133         if (ret != BT_STATUS_SUCCESS) {
134                 BT_ERR("Disable failed: [%s]", status2string(ret));
135                 return convert_to_oal_status(ret);
136         }
137         return OAL_STATUS_SUCCESS;
138 }
139
140 /* Callbacks from Stack */
141 static void cb_adapter_state_change(bt_state_t status)
142 {
143         BT_DBG("+");
144         oal_event_t event;
145
146         event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
147
148         send_event(event, NULL, 0);
149 }
150
151 static gboolean retry_enable_adapter(gpointer data)
152 {
153         adapter_enable();
154         return FALSE;
155 }
156
157 oal_status_t adapter_get_address(void)
158 {
159         int ret;
160
161         CHECK_OAL_INITIALIZED();
162
163         API_TRACE();
164
165         ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
166         if (ret != BT_STATUS_SUCCESS) {
167                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
168                 return convert_to_oal_status(ret);
169         }
170
171         return OAL_STATUS_SUCCESS;
172 }
173
174 oal_status_t adapter_get_version(void)
175 {
176         int ret;
177
178         CHECK_OAL_INITIALIZED();
179
180         API_TRACE();
181
182         ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
183         if (ret != BT_STATUS_SUCCESS) {
184                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
185                 return convert_to_oal_status(ret);
186         }
187
188         return OAL_STATUS_SUCCESS;
189 }
190
191 oal_status_t adapter_get_name(void)
192 {
193         int ret;
194
195         CHECK_OAL_INITIALIZED();
196
197         API_TRACE();
198
199         ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
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_is_discoverable(int *p_discoverable)
209 {
210         OAL_CHECK_PARAMETER(p_discoverable, return);
211
212         *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
213
214         API_TRACE("%d", *p_discoverable);
215
216         return OAL_STATUS_SUCCESS;
217 }
218
219 oal_status_t adapter_is_connectable(int *p_connectable)
220 {
221         OAL_CHECK_PARAMETER(p_connectable, return);
222
223         *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
224                 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
225
226         API_TRACE("%d", *p_connectable);
227
228         return OAL_STATUS_SUCCESS;
229 }
230
231 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
232 {
233         API_TRACE("%d", discoverable_timeout);
234
235         *p_timeout = discoverable_timeout;
236
237         return OAL_STATUS_SUCCESS;
238 }
239
240 oal_status_t adapter_get_service_uuids(void)
241 {
242         int ret;
243
244         CHECK_OAL_INITIALIZED();
245
246         API_TRACE();
247
248         ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
249         if (ret != BT_STATUS_SUCCESS) {
250                 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
251                 return convert_to_oal_status(ret);
252         }
253
254         return OAL_STATUS_SUCCESS;
255 }
256
257 static void cb_adapter_properties (bt_status_t status,
258                                                int num_properties,
259                                                bt_property_t *properties)
260 {
261         int i;
262
263         BT_DBG("status: %d, count: %d", status, num_properties);
264
265         if (status != BT_STATUS_SUCCESS) {
266                 if (num_properties == 1) {
267                         BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
268                                 status2string(status), num_properties, properties[num_properties-1].type);
269                 } else {
270                         BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
271                 }
272                 return;
273         }
274
275         for (i = 0; i < num_properties; i++) {
276                 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
277                 switch (properties[i].type) {
278                 case BT_PROPERTY_VERSION: {
279                         g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
280                         local_version[properties[i].len] = '\0';
281
282                         BT_DBG("Version: %s", local_version);
283                         /* Send event to application */
284                         if (num_properties == 1) {
285                                 char *adapter_ver = g_strdup(local_version);
286
287                                 /* Application has requested this property SET/GET hence send EVENT */
288                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
289                         }
290                         break;
291                 }
292                 case BT_PROPERTY_BDNAME: {
293                         g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
294                         local_name[properties[i].len] = '\0';
295
296                         BT_DBG("Name: %s", local_name);
297                         /* Send event to application */
298                         if (num_properties == 1) {
299                                 char * adap_name = g_strdup(local_name);
300
301                                 /* Application has requested this property SET/GET hence send EVENT */
302                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
303                         }
304                         break;
305                 }
306                 case BT_PROPERTY_BDADDR: {
307                         bt_bdaddr_t * addr;
308
309                         addr =  properties[i].val;
310                         memcpy(local_address.addr, addr->address, 6);
311                         if (num_properties == 1) {
312                                 /* Application has requested this property SET/GET hence send EVENT */
313                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
314                                                 g_memdup(&local_address, sizeof(local_address)),
315                                                 sizeof(local_address));
316                         }
317                         break;
318                 }
319                 case BT_PROPERTY_UUIDS: {
320                         int num_uuid;
321
322                         num_uuid = properties[i].len/sizeof(bt_uuid_t);
323
324                         BT_DBG("num_uuid: %d", num_uuid);
325
326                         /* Send event to application */
327                         if (num_properties == 1) {
328                                 event_adapter_services_t *uuids_event;
329
330                                 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
331                                 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
332                                 uuids_event->num = num_uuid;
333
334                                 /* Application has requested this property SET/GET hence send EVENT */
335                                 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
336                                                 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
337                         }
338                         break;
339                 }
340                 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
341                         bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
342
343                         BT_INFO("Scan mode (%d)", cur_mode);
344
345                         scan_mode = cur_mode;
346
347                         /* Send event to application */
348                         if (num_properties == 1) {
349                                 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
350
351                                 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
352                                         event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
353                                 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
354                                         event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
355
356                                 /* Application has requested this property SET/GET hence send EVENT */
357                                 send_event(event, NULL, 0);
358                         }
359                         break;
360                 }
361                 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
362                         int timeout;
363
364                         timeout = *((uint32_t*)properties[i].val);
365
366                         BT_INFO("Discoverability timeout: %d", timeout);
367                         discoverable_timeout = timeout;
368
369                         send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
370                                         g_memdup(properties[i].val, sizeof(uint32_t)),
371                                         sizeof(uint32_t));
372                         break;
373                 }
374                 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
375                         int j;
376                         int num_bonded;
377                         bt_bdaddr_t *bonded_addr_list;
378                         event_device_list_t *event_data;
379
380                         num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
381                         BT_DBG("num_bonded %d", num_bonded);
382
383                         if (num_properties > 1) /* No explicit req for this prop, ignore */
384                                 break;
385
386                         bonded_addr_list = properties[i].val;
387                         event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
388                         event_data->num = num_bonded;
389
390                         for (j = 0; j < num_bonded; j++)
391                                 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
392
393                         send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
394                                         event_data, (num_bonded * sizeof(bt_bdaddr_t)));
395                         break;
396                 }
397                 default:
398                         BT_WARN("Unhandled property: %d", properties[i].type);
399                         break;
400                 }
401         }
402 }