Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / BluetoothConverter.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17 #include <dpl/assert.h>
18 #include <Commons/Exception.h>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <API/Bluetooth/BluetoothProperty.h>
23 #include "BluetoothConverter.h"
24 #include "JSBluetoothDevice.h"
25
26 #include <pcrecpp.h>
27
28 using namespace std;
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31
32
33 namespace TizenApis {
34 namespace Tizen1_0 {
35
36 BluetoothConverter::BluetoothConverter(JSContextRef context) :
37     Converter(context)
38 {
39 }
40
41 BluetoothConverter::~BluetoothConverter()
42 {
43 }
44
45
46
47 JSValueRef BluetoothConverter::toFunctionOrNull(const JSValueRef& arg)
48 {
49         if (JSValueIsNull(m_context, arg))
50         {
51                 LogDebug("null.");
52
53                 return NULL;
54         } 
55         else if (JSValueIsUndefined(m_context, arg) || JSValueIsObject(m_context, arg) == false)
56         {
57                 LogDebug("undefined, not object");
58                 ThrowMsg(ConversionException, "Not a function nor JS null.");
59         }
60         else if (Validator(m_context).isCallback(arg)) 
61         {
62                 return arg;
63         } 
64
65         ThrowMsg(InvalidArgumentException, "unexpected argument");
66         return NULL;
67 }
68
69 JSValueRef BluetoothConverter::toFunction(const JSValueRef& arg)
70 {
71         if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) 
72                 || !JSValueIsObject(m_context, arg))
73         {
74                 ThrowMsg(ConversionException, "JS null passed as function.");
75         }       
76         else if (Validator(m_context).isCallback(arg)) 
77         {
78                 return arg;
79         } 
80
81         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
82         return NULL;
83 }
84
85
86 EventBTOnDiscoveryDevicesPrivateDataPtr 
87         BluetoothConverter::toEventBTOnDiscoveryDevicesPrivateData(JSObjectRef thisObject, JSValueRef successParam, JSValueRef errorParam)
88 {
89         JSValueRef errorCalback;
90         if (JSValueIsNull(m_context, successParam) || JSValueIsUndefined(m_context, successParam) 
91                         || !JSValueIsObject(m_context, successParam))
92         {
93                 LogError("not a object");
94                 Throw(ConversionException);
95         }
96
97         JSObjectRef objectCallbacks = toJSObjectRef(successParam);
98         Validator validator(m_context);
99         BluetoothDiscoveryDevicesSuccessCallback result;
100
101         errorCalback = toFunctionOrNull(errorParam);
102
103         result.onSuccess = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onSuccess");
104         result.onFound = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onFound");
105         result.onFinished = JSUtils::getJSPropertyOrUndefined(m_context, objectCallbacks, "onFinish");
106                         
107         if ((!validator.isNullOrUndefined(result.onSuccess) && !validator.isCallback(result.onSuccess)) ||
108                 (!validator.isNullOrUndefined(result.onFound) && !validator.isCallback(result.onFound)) ||
109                 (!validator.isNullOrUndefined(result.onFinished) && !validator.isCallback(result.onFinished)))
110         {
111                 LogError("java script call back set error");
112                 Throw(ConversionException);
113         }
114
115         JSCallbackManagerPtr onSuccessCbm = JSCallbackManager::createObject(m_context, result.onSuccess, NULL);
116         JSCallbackManagerPtr onFoundCbm = JSCallbackManager::createObject(m_context, result.onFound, NULL);
117         JSCallbackManagerPtr onFinishedCbm = JSCallbackManager::createObject(m_context, result.onFinished, NULL);
118
119         onSuccessCbm->setOnError(errorCalback);
120         onFoundCbm->setOnError(errorCalback);
121         onFinishedCbm->setOnError(errorCalback);
122
123         return EventBTOnDiscoveryDevicesPrivateDataPtr(
124                 new EventBTOnDiscoveryDevicesPrivateData(onSuccessCbm, onFoundCbm, onFinishedCbm));
125 }
126
127 BluetoothSocketNotifierPrivateDataPtr 
128         BluetoothConverter::getInitalBluetoothSocketNotifierPrivateData()
129 {
130         JSCallbackManagerPtr onMessageCbm = JSCallbackManager::createObject(m_context, NULL, NULL);
131         JSCallbackManagerPtr onErrorCbm = JSCallbackManager::createObject(m_context, NULL, NULL);
132         JSCallbackManagerPtr onCloseCbm = JSCallbackManager::createObject(m_context, NULL, NULL);
133
134         return BluetoothSocketNotifierPrivateDataPtr(
135                 new BluetoothSocketNotifierPrivateData(onMessageCbm, onErrorCbm, onCloseCbm));
136         
137 }
138
139 void BluetoothConverter::setBluetoothSocketNotifierCallback(JSStringRef propertyName, JSValueRef value, 
140         JSObjectRef object, BluetoothSocketNotifierPrivateDataPtr& privData)
141 {
142         JSValueRef successCallback = toFunction(value);
143
144         if(JSStringIsEqualToUTF8CString(propertyName, "onClose")) 
145         {
146                 privData->getOnClose()->setOnSuccess(successCallback);
147         }
148         else if(JSStringIsEqualToUTF8CString(propertyName, "onError")) 
149         {
150                 privData->getOnError()->setOnSuccess(successCallback);
151         }
152         else if(JSStringIsEqualToUTF8CString(propertyName, "onMessage")) 
153         {
154                 privData->getOnMessage()->setOnSuccess(successCallback);
155                 privData->setObject(object);
156         }
157         else
158         {
159                 ThrowMsg(ConversionException, "unknown property");
160         }
161                         
162 }
163
164 JSObjectRef BluetoothConverter::toBluetoothDevices(const std::vector<TizenApis::Api::Bluetooth::BluetoothDeviceData>& devices)
165 {
166         int count = devices.size();
167         int index = 0;
168         JSObjectRef adapterObject[count];
169         
170         for (index = 0; index < count; index++)
171         {
172                 adapterObject[index] = JSBluetoothDevice::createJSObject(m_context, devices[index]);
173         }
174         JSObjectRef result = JSObjectMakeArray(m_context, count, adapterObject, NULL);
175         return result;  
176 }
177
178 JSObjectRef BluetoothConverter::toBluetoothByteArray(std::vector<char>& data)
179 {
180         JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
181         int length = data.size();
182
183         if (!result) 
184         {
185                 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "Could not create array object.");
186         }
187
188         LogDebug("size" << length);
189         for (int i = 0; i < length; ++i) 
190         {
191                 JSValueRef value = JSValueMakeNumber(m_context, data[i]);
192
193                 if (!JSSetArrayElement(m_context, result, i, value)) 
194                 {
195                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "Could not fill array.");
196                 }
197         }
198         return result;
199 }
200 std::string BluetoothConverter::toBluetoothDeviceAddress(JSValueRef deviceAddress)
201 {
202         if (!JSValueIsString(m_context, deviceAddress) || JSValueIsNull(m_context, deviceAddress))
203         {
204                 ThrowMsg(ConversionException, "Not a string");
205         }
206
207         std::string address = toString(deviceAddress);
208         pcrecpp::RE re("(([0-9a-zA-Z]+):)+([0-9a-zA-Z]+)");
209         std::string compareAddress = "00:12:47:08:9A:A6";
210
211         if (!re.FullMatch(address))
212         {
213                 ThrowMsg(ConversionException, "not a bluetooth device address");
214         }
215
216         if (address.size() != compareAddress.size())
217         {
218                 ThrowMsg(InvalidArgumentException, "too long or too short parameter");
219         }
220         
221         return address;
222 }
223
224 std::string BluetoothConverter::toBluetoothUUIDString(JSValueRef uuidString)
225 {
226         if (!JSValueIsString(m_context, uuidString) || JSValueIsNull(m_context, uuidString))
227         {
228                 ThrowMsg(ConversionException, "Not a string");
229         }
230
231         std::string uuid = toString(uuidString);
232         pcrecpp::RE re("(([0-9a-zA-Z]+)-)+([0-9a-zA-Z]+)");
233         std::string compareUUID = "00001101-0000-1000-8000-00805F9B34FB";
234
235         if (!re.FullMatch(uuid))
236         {
237                 LogDebug("not a bluetooth service uuid");
238                 ThrowMsg(ConversionException, "not a bluetooth service uuid");
239         }
240
241         if (uuid.size() != compareUUID.size())
242         {
243                 LogDebug("too long or too short parameter");
244                 ThrowMsg(InvalidArgumentException, "too long or too short parameter");
245         }
246
247         return uuid;
248 }
249
250
251
252
253
254 unsigned short BluetoothConverter::toBluetoothSecurityLevelInt(JSValueRef level)
255 {
256         unsigned short securityLevel= SECURITY_LEVEL_HIGH_VALUE_INT;
257
258         if (!JSValueIsNull(m_context, level))
259         {
260                 if (!JSValueIsString(m_context, level))
261                 {
262                         ThrowMsg(ConversionException, "Not a string");
263                 }
264         
265                 std::string securityLevelStr = toString(level);
266                 
267                 if(strcmp(securityLevelStr.c_str(), SECURITY_LEVEL_HIGH_VALUE) == 0) 
268                 {
269                         securityLevel = SECURITY_LEVEL_HIGH_VALUE_INT;
270                 }
271                 else
272                 {
273                         if (strcmp(securityLevelStr.c_str(), SECURITY_LEVEL_MEDIUM_VALUE) == 0 ||
274                                 strcmp(securityLevelStr.c_str(), SECURITY_LEVEL_LOW_VALUE) == 0)
275                         {
276                                 Throw(UnsupportedException);
277                         }
278                         else
279                         {
280                                 Throw(ConversionException);
281                         }
282                 }
283         }
284         return securityLevel;
285 }
286
287
288
289 JSValueRef BluetoothConverter::toBluetoothSecurityLevelJSValueRef(unsigned short level)
290 {
291         JSValueRef result;
292
293         switch (level)
294         {
295         case SECURITY_LEVEL_HIGH_VALUE_INT:
296                 result = toJSValueRef(SECURITY_LEVEL_HIGH_VALUE);
297                 break;
298         case SECURITY_LEVEL_MEDIUM_VALUE_INT:
299                 result = toJSValueRef(SECURITY_LEVEL_MEDIUM_VALUE);
300                 break;
301         case SECURITY_LEVEL_LOW_VALUE_INT:
302                 result = toJSValueRef(SECURITY_LEVEL_LOW_VALUE);
303                 break;
304         default:
305                 result = toJSValueRef(SECURITY_LEVEL_HIGH_VALUE);               
306         }
307         return result;
308 }
309
310 JSValueRef BluetoothConverter::toBluetoothProtocolJSValue(unsigned short int protocol)
311 {
312         if (protocol == PROTOCOL_TYPE_RFCOMM_VALUE_INT)
313         {
314                 return toJSValueRef(PROTOCOL_TYPE_RFCOMM_VALUE);
315
316         }
317         else if (protocol == PROTOCOL_TYPE_L2CAP_VALUE_INT)
318         {
319                 return toJSValueRef(PROTOCOL_TYPE_L2CAP_VALUE);
320         }
321         else
322         {
323                 ThrowMsg(ConversionException, "Not a protocol value");
324         }
325 }
326
327 JSValueRef BluetoothConverter::toBluetoothStateJSValue(unsigned short int state)
328 {
329         if (state == SOCKET_STATE_CLOSED_VALUE_INT)
330         {
331                 return toJSValueRef(SOCKET_STATE_CLOSED_VALUE);
332         
333         }
334         else if (state == SOCKET_STATE_OPEN_VALUE_INT)
335         {
336                 return toJSValueRef(SOCKET_STATE_OPEN_VALUE);
337         }
338         else
339         {
340                 ThrowMsg(ConversionException, "Not a state value");
341
342         }
343 }
344
345 }
346 }
347