tizen 2.3.1 release
[framework/web/mobile/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothLEDevice.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "BluetoothLEDevice.h"
19 #include "BluetoothUtil.h"
20 #include <Logger.h>
21 #include <glib.h>
22 #include "plugin_config_impl.h"
23 #include <JSUtil.h>
24
25 namespace DeviceAPI {
26 namespace Bluetooth {
27
28 BluetoothLEDevice::BluetoothLEDevice(
29     bt_adapter_le_device_scan_result_info_s* deviceInfo)
30 {
31     LOGD("Enter");
32
33     m_address = deviceInfo->remote_address;
34
35     char *device_name = NULL;
36     int ret = bt_adapter_le_get_scan_result_device_name(deviceInfo,
37         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &device_name);
38     if (ret != BT_ERROR_NONE) {
39         LOGE("Failed to get device name from scan response: %d", ret);
40         int inRet = bt_adapter_le_get_scan_result_device_name(deviceInfo,
41             BT_ADAPTER_LE_PACKET_ADVERTISING , &device_name);
42             if (inRet != BT_ERROR_NONE) {
43                 LOGE("Failed to get device name from advertise data: %d", ret);
44             }
45             else {
46                     m_name = device_name;
47                     g_free(device_name);
48             }
49     }
50     else {
51         m_name = device_name;
52         g_free(device_name);
53     }
54
55     int powerLevel = 0;
56     ret = bt_adapter_le_get_scan_result_tx_power_level(deviceInfo,
57         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &powerLevel);
58     if (ret != BT_ERROR_NONE) {
59         LOGE("Failed to get txpower strength from scan response: %d", ret);
60         int inRet = bt_adapter_le_get_scan_result_tx_power_level(deviceInfo,
61             BT_ADAPTER_LE_PACKET_ADVERTISING , &powerLevel);
62             if (inRet != BT_ERROR_NONE) {
63                 LOGE("Failed to get txpower strength from advertise data: %d", ret);
64             }
65             else {
66                 m_txPower = powerLevel;
67             }
68     }
69     else {
70         m_txPower = powerLevel;
71     }
72
73     int appearance = 0;
74     ret = bt_adapter_le_get_scan_result_appearance(deviceInfo,
75         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &appearance);
76     if (ret != BT_ERROR_NONE) {
77         LOGE("Failed to get appearance value from scan response: %d", ret);
78         int inRet = bt_adapter_le_get_scan_result_appearance(deviceInfo,
79             BT_ADAPTER_LE_PACKET_ADVERTISING , &appearance);
80             if (inRet != BT_ERROR_NONE) {
81                 LOGE("Failed to get appearance value from advertise data: %d", ret);
82             }
83             else {
84                 m_appearance = appearance;
85             }
86     }
87     else {
88         m_appearance = appearance;
89     }
90
91     bt_adapter_le_service_data_s *serviceDataList = NULL;
92     int serviceDataListCount = 0;
93     ret = bt_adapter_le_get_scan_result_service_data_list(deviceInfo,
94         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &serviceDataList, &serviceDataListCount);
95     if (ret != BT_ERROR_NONE) {
96         LOGE("Failed to get service data list from scan response: %d", ret);
97         int inRet = bt_adapter_le_get_scan_result_service_data_list(deviceInfo,
98             BT_ADAPTER_LE_PACKET_ADVERTISING , &serviceDataList, &serviceDataListCount);
99             if (inRet != BT_ERROR_NONE) {
100                 LOGE("Failed to get service data list from advertise data: %d", ret);
101             }
102             else {
103                 assignServiceDataList(serviceDataList, serviceDataListCount);
104             }
105     }
106     else {
107         assignServiceDataList(serviceDataList, serviceDataListCount);
108     }
109
110     int manufacturer_id = 0;
111     char* manufacturer_data = NULL;
112     int manufacturerDataCount = 0;
113     BluetoothLEManufacturerData* manufacturer = new BluetoothLEManufacturerData();
114
115     ret = bt_adapter_le_get_scan_result_manufacturer_data(deviceInfo,
116         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &manufacturer_id, &manufacturer_data, &manufacturerDataCount);
117     if (ret != BT_ERROR_NONE) {
118         LOGE("Failed to get manufacturer data list from scan response: %d", ret);
119         int inRet = bt_adapter_le_get_scan_result_manufacturer_data(deviceInfo,
120             BT_ADAPTER_LE_PACKET_ADVERTISING , &manufacturer_id, &manufacturer_data, &manufacturerDataCount);
121             if (inRet != BT_ERROR_NONE) {
122                 LOGE("Failed to get manufacturer data list from advertise data: %d", ret);
123                 manufacturer->setId("");
124                 manufacturer->setData("");
125                 m_manufacturerData = manufacturer;
126                 g_free(manufacturer_data);
127             }
128             else {
129                 LOGD("manufacturer : id %d, data %s", manufacturer_id, manufacturer_data);
130                 manufacturer->setId(std::to_string(manufacturer_id));
131                 manufacturer->setData(manufacturer_data);
132                 m_manufacturerData = manufacturer;
133                 g_free(manufacturer_data);
134             }
135     }
136     else {
137         manufacturer->setId(std::to_string(manufacturer_id));
138         manufacturer->setData(manufacturer_data);
139         m_manufacturerData = manufacturer;
140         g_free(manufacturer_data);
141     }
142
143     char** service_solicitation_uuids = NULL;
144     int service_solicitation_uuids_count = 0;
145     ret = bt_adapter_le_get_scan_result_service_solicitation_uuids(deviceInfo,
146         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &service_solicitation_uuids, &service_solicitation_uuids_count);
147     if (ret != BT_ERROR_NONE) {
148         LOGE("Failed to get the service solicitation UUID list from scan response: %d", ret);
149         int inRet = bt_adapter_le_get_scan_result_service_solicitation_uuids(deviceInfo,
150             BT_ADAPTER_LE_PACKET_ADVERTISING , &service_solicitation_uuids, &service_solicitation_uuids_count);
151             if (inRet != BT_ERROR_NONE) {
152                 LOGE("Failed to get the service solicitation UUID list from advertise data: %d", ret);
153             }
154             else {
155                 assignServicesolicitationUUIDS(service_solicitation_uuids, service_solicitation_uuids_count);
156             }
157     }
158     else {
159         assignServicesolicitationUUIDS(service_solicitation_uuids, service_solicitation_uuids_count);
160     }
161
162     char **uuids = NULL;
163     int count = 0;
164     ret = bt_adapter_le_get_scan_result_service_uuids(deviceInfo,
165         BT_ADAPTER_LE_PACKET_SCAN_RESPONSE , &uuids, &count);
166     if (ret != BT_ERROR_NONE) {
167         LOGE("Failed to get the service service_uuids list from scan response: %d", ret);
168         int inRet = bt_adapter_le_get_scan_result_service_uuids(deviceInfo,
169             BT_ADAPTER_LE_PACKET_ADVERTISING , &uuids, &count);
170             if (inRet != BT_ERROR_NONE) {
171                 LOGE("Failed to get the service service_uuids list from advertise data: %d", ret);
172             }
173             else {
174                 m_uuids = std::vector<std::string>();
175                 for (int i = 0; i < count; ++i) {
176                     m_uuids->push_back(uuids[i]);
177                     g_free(uuids[i]);
178                 }
179                 g_free(uuids);
180             }
181     }
182     else {
183         m_uuids = std::vector<std::string>();
184         for (int i = 0; i < count; ++i) {
185             m_uuids->push_back(uuids[i]);
186             g_free(uuids[i]);
187         }
188         g_free(uuids);
189     }
190 }
191
192 void BluetoothLEDevice::assignServicesolicitationUUIDS(char** service_solicitation_uuids,
193         int service_solicitation_uuids_count) {
194
195     m_service_solicitation_uuids = std::vector<std::string>();
196     for (int i = 0; i < service_solicitation_uuids_count; ++i) {
197         m_service_solicitation_uuids->push_back(
198             std::string(service_solicitation_uuids[i]));
199         g_free(service_solicitation_uuids[i]);
200     }
201     g_free(service_solicitation_uuids);
202 }
203
204 void BluetoothLEDevice::assignServiceDataList(bt_adapter_le_service_data_s *serviceDataList,
205 int serviceDataListCount) {
206
207     m_servicesList = std::vector< std::pair<std::string, std::string>>();
208     for( int i = 0; i < serviceDataListCount; ++i){
209         m_servicesList->push_back(std::make_pair(std::string(serviceDataList[i].service_uuid, 4),
210             std::string(serviceDataList[i].service_data, serviceDataList[i].service_data_len)));
211     }
212
213     if( serviceDataListCount > 0 ) {
214         int ret = bt_adapter_le_free_service_data_list(serviceDataList, serviceDataListCount);
215         if( ret !=  BT_ERROR_NONE ) {
216             LOGW("Failed to free service data list: %d", ret);
217         }
218     }
219     g_free(serviceDataList);
220 }
221
222 std::string BluetoothLEDevice::getAddress() const
223 {
224     return m_address;
225 }
226
227 boost::optional<std::string> BluetoothLEDevice::getName() const
228 {
229     if(!m_name) {
230         return boost::none;
231     }
232
233     return m_name;
234 }
235
236 boost::optional<std::vector<std::string>> BluetoothLEDevice::getUUIDs() const
237 {
238     if(!m_uuids) {
239         return boost::none;
240     }
241
242     return m_uuids;
243 }
244
245 boost::optional<long> BluetoothLEDevice::getTXPower() const {
246
247     if(!m_txPower) {
248         return boost::none;
249     }
250
251     return m_txPower;
252 }
253
254 boost::optional<long> BluetoothLEDevice::getAppearance() const {
255
256     if(!m_appearance) {
257         return boost::none;
258     }
259
260     return m_appearance;
261 }
262
263 boost::optional<std::vector<std::string>> BluetoothLEDevice::getSolicitationUUIDs() const {
264
265     if(!m_service_solicitation_uuids) {
266         return boost::none;
267     }
268
269     return m_service_solicitation_uuids;
270 }
271
272 JSValueRef BluetoothLEDevice::getManufacturer(JSContextRef ctx) const {
273
274     if(!m_manufacturerData) {
275         return JSValueMakeNull(ctx);
276     }
277
278     JSObjectRef jsobject = JSObjectMake(ctx, NULL, NULL );
279     Common::JSUtil::setProperty(ctx , jsobject, BLUETOOTH_LE_DEVICE_MANUFACTURER_DATA_ID,
280         Common::JSUtil::toJSValueRef(ctx, m_manufacturerData->getId()), kJSPropertyAttributeNone, NULL);
281     Common::JSUtil::setProperty(ctx , jsobject, BLUETOOTH_LE_DEVICE_MANUFACTURER_DATA_DATA,
282         Common::JSUtil::toJSValueRef(ctx, m_manufacturerData->getData()), kJSPropertyAttributeNone, NULL);
283     return jsobject;
284 }
285
286 JSValueRef BluetoothLEDevice::getServiceData(JSContextRef ctx) const {
287     LOGD("Entered");
288
289     if(!m_servicesList) {
290         return JSValueMakeNull(ctx);
291     }
292
293     std::vector<JSObjectRef> servicesDatas;
294     for( auto it = m_servicesList->begin(); it != m_servicesList->end(); ++it )
295     {
296         JSObjectRef jsservice = JSObjectMake(ctx, NULL, NULL );
297         Common::JSUtil::setProperty(ctx , jsservice, BLUETOOTH_LE_DEVICE_SERVICE_DATA_UUIDS,
298             Common::JSUtil::toJSValueRef(ctx, it->first), kJSPropertyAttributeNone, NULL);
299         Common::JSUtil::setProperty(ctx , jsservice, BLUETOOTH_LE_DEVICE_SERVICE_DATA_DATA,
300             Common::JSUtil::toJSValueRef(ctx, it->second), kJSPropertyAttributeNone, NULL);
301         servicesDatas.push_back(jsservice);
302     }
303     return Common::JSUtil::toJSValueRef_<JSObjectRef>(ctx, servicesDatas);
304 }
305
306 } // Bluetooth
307 } // DeviceAPI