ac9135b75f79d7b0b2828b9e0b937a50ed56235e
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / oal-device-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 <dlog.h>
22 #include <string.h>
23
24 #include <bluetooth.h>
25
26 #include "oal-event.h"
27 #include "oal-internal.h"
28 #include "oal-common.h"
29 #include "oal-manager.h"
30 #include "oal-utils.h"
31
32 static const bt_interface_t * blued_api;
33
34 void device_mgr_init(const bt_interface_t * stack_if)
35 {
36         blued_api = stack_if;
37 }
38
39 void device_mgr_cleanup(void)
40 {
41         BT_DBG();
42         blued_api = NULL;
43 }
44
45 oal_status_t device_query_attributes(bt_address_t *addr)
46 {
47         int res;
48         bdstr_t bdstr;
49
50         CHECK_OAL_INITIALIZED();
51
52         OAL_CHECK_PARAMETER(addr, return);
53
54         API_TRACE("[%s]", bdt_bd2str(addr, &bdstr));
55
56         res = blued_api->get_remote_device_properties((bt_bdaddr_t *)addr);
57         if (res != BT_STATUS_SUCCESS) {
58                 BT_ERR("get_remote_device_properties error: [%s]", status2string(res));
59                 return convert_to_oal_status(res);
60         }
61
62         return OAL_STATUS_SUCCESS;
63 }
64
65 oal_status_t device_set_alias(bt_address_t * addr, char * alias)
66 {
67         int res;
68         bt_property_t prop;
69         bdstr_t bdstr;
70
71         CHECK_OAL_INITIALIZED();
72         OAL_CHECK_PARAMETER(addr, return);
73         OAL_CHECK_PARAMETER(alias, return);
74
75         API_TRACE("%s ->Alias: %s", bdt_bd2str(addr, &bdstr), alias);
76
77         prop.type = BT_PROPERTY_REMOTE_FRIENDLY_NAME;
78         prop.len = strlen(alias);
79         prop.val = alias;
80         res = blued_api->set_remote_device_property((bt_bdaddr_t*)addr, &prop);
81         if (res != BT_STATUS_SUCCESS) {
82                 BT_ERR("set_remote_device_property error: [%s]", status2string(res));
83                 BT_ERR("Alias: %s", alias);
84                 return convert_to_oal_status(res);
85         }
86
87         return OAL_STATUS_SUCCESS;
88 }
89
90 void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
91                 int num_properties, bt_property_t *properties)
92 {
93         oal_event_t event;
94         gpointer event_data = NULL;
95         remote_device_t *dev_info;
96         ble_adv_data_t adv_info;
97         gsize size = 0;
98         bdstr_t bdstr;
99
100         if(BT_STATUS_SUCCESS != status) {
101                 BT_ERR("[%s]status: %d", bdt_bd2str((bt_address_t*)bd_addr, &bdstr), status);
102                 return;
103         }
104
105         BT_DBG("[%s]", bdt_bd2str((bt_address_t*)bd_addr, &bdstr));
106         dev_info = g_new0(remote_device_t, 1);
107         memcpy(dev_info->address.addr, bd_addr->address, 6);
108         parse_device_properties(num_properties, properties, dev_info, &adv_info);
109
110         if(num_properties == 1) {
111                 /* For one particular property a dedicated event to be sent */
112                 switch(properties[0].type) {
113                 case BT_PROPERTY_BDNAME:
114                         event = OAL_EVENT_DEVICE_NAME;
115                         event_data = dev_info;
116                         send_event_trace(event, event_data, sizeof(remote_device_t),
117                                 (bt_address_t*)bd_addr, "Name: %s", dev_info->name);
118                         return;
119                 case BT_PROPERTY_UUIDS: {
120                         event_dev_services_t *services_info;
121                         bt_uuid_t *uuids = (bt_uuid_t *) properties[0].val;
122
123                         services_info = g_malloc(sizeof(event_dev_services_t) + properties[0].len);
124                         services_info->address = dev_info->address;
125                         memcpy(services_info->service_list, uuids, properties[0].len);
126                         services_info->num = properties[0].len/sizeof(bt_uuid_t);
127                         event = OAL_EVENT_DEVICE_SERVICES;
128                         event_data = services_info;
129                         size = sizeof(event_dev_services_t) + properties[0].len;
130                         g_free(dev_info);
131                         break;
132                 }
133                 default:
134                         BT_ERR("Single Property [%d] not handled", properties[0].type);
135                         g_free(dev_info);
136                         return;
137                 }
138         } else {
139                 event_dev_properties_t *dev_props_event = g_new0(event_dev_properties_t, 1);
140                 if (dev_info->type != DEV_TYPE_BREDR) {
141                         int i;
142
143                         BT_INFO("BLE Device");
144                         /* BLE Single or DUAL mode found, so it should have Adv data */
145                         dev_props_event->adv_len = adv_info.len;
146                         if(dev_props_event->adv_len > 0)
147                                 memcpy(dev_props_event->adv_data,
148                                         adv_info.adv_data, adv_info.len);
149
150                         for (i = 0; i < dev_props_event->adv_len; i++)
151                                 BT_INFO("Adv Data[%d] = [0x%x]",
152                                         i, dev_props_event->adv_data[i]);
153                         memcpy(&dev_props_event->device_info,
154                                 dev_info, sizeof(remote_device_t));
155                 } else {
156                         BT_INFO("BREDR type Device");
157                         memcpy(&dev_props_event->device_info,
158                                 dev_info, sizeof(remote_device_t));
159                 }
160
161                 event_data = dev_props_event;
162                 event = OAL_EVENT_DEVICE_PROPERTIES;
163                 size = sizeof(event_dev_properties_t);
164         }
165
166         send_event_bda_trace(event, event_data, size, (bt_address_t*)bd_addr);
167 }