2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
36 BluetoothConverter::BluetoothConverter(JSContextRef context) :
41 BluetoothConverter::~BluetoothConverter()
47 JSValueRef BluetoothConverter::toFunctionOrNull(const JSValueRef& arg)
49 if (JSValueIsNull(m_context, arg))
55 else if (JSValueIsUndefined(m_context, arg) || JSValueIsObject(m_context, arg) == false)
57 LogDebug("undefined, not object");
58 ThrowMsg(ConversionException, "Not a function nor JS null.");
60 else if (Validator(m_context).isCallback(arg))
65 ThrowMsg(InvalidArgumentException, "unexpected argument");
69 JSValueRef BluetoothConverter::toFunction(const JSValueRef& arg)
71 if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)
72 || !JSValueIsObject(m_context, arg))
74 ThrowMsg(ConversionException, "JS null passed as function.");
76 else if (Validator(m_context).isCallback(arg))
81 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
86 EventBTOnDiscoveryDevicesPrivateDataPtr
87 BluetoothConverter::toEventBTOnDiscoveryDevicesPrivateData(JSObjectRef thisObject, JSValueRef successParam, JSValueRef errorParam)
89 JSValueRef errorCalback;
90 if (JSValueIsNull(m_context, successParam) || JSValueIsUndefined(m_context, successParam)
91 || !JSValueIsObject(m_context, successParam))
93 LogError("not a object");
94 Throw(ConversionException);
97 JSObjectRef objectCallbacks = toJSObjectRef(successParam);
98 Validator validator(m_context);
99 BluetoothDiscoveryDevicesSuccessCallback result;
101 errorCalback = toFunctionOrNull(errorParam);
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");
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)))
111 LogError("java script call back set error");
112 Throw(ConversionException);
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);
119 onSuccessCbm->setOnError(errorCalback);
120 onFoundCbm->setOnError(errorCalback);
121 onFinishedCbm->setOnError(errorCalback);
123 return EventBTOnDiscoveryDevicesPrivateDataPtr(
124 new EventBTOnDiscoveryDevicesPrivateData(onSuccessCbm, onFoundCbm, onFinishedCbm));
127 BluetoothSocketNotifierPrivateDataPtr
128 BluetoothConverter::getInitalBluetoothSocketNotifierPrivateData()
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);
134 return BluetoothSocketNotifierPrivateDataPtr(
135 new BluetoothSocketNotifierPrivateData(onMessageCbm, onErrorCbm, onCloseCbm));
139 void BluetoothConverter::setBluetoothSocketNotifierCallback(JSStringRef propertyName, JSValueRef value,
140 JSObjectRef object, BluetoothSocketNotifierPrivateDataPtr& privData)
142 JSValueRef successCallback = toFunction(value);
144 if(JSStringIsEqualToUTF8CString(propertyName, "onClose"))
146 privData->getOnClose()->setOnSuccess(successCallback);
148 else if(JSStringIsEqualToUTF8CString(propertyName, "onError"))
150 privData->getOnError()->setOnSuccess(successCallback);
152 else if(JSStringIsEqualToUTF8CString(propertyName, "onMessage"))
154 privData->getOnMessage()->setOnSuccess(successCallback);
155 privData->setObject(object);
159 ThrowMsg(ConversionException, "unknown property");
164 JSObjectRef BluetoothConverter::toBluetoothDevices(const std::vector<TizenApis::Api::Bluetooth::BluetoothDeviceData>& devices)
166 int count = devices.size();
168 JSObjectRef adapterObject[count];
170 for (index = 0; index < count; index++)
172 adapterObject[index] = JSBluetoothDevice::createJSObject(m_context, devices[index]);
174 JSObjectRef result = JSObjectMakeArray(m_context, count, adapterObject, NULL);
178 JSObjectRef BluetoothConverter::toBluetoothByteArray(std::vector<char>& data)
180 JSObjectRef result = JSCreateArrayObject(m_context, 0, NULL);
181 int length = data.size();
185 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "Could not create array object.");
188 LogDebug("size" << length);
189 for (int i = 0; i < length; ++i)
191 JSValueRef value = JSValueMakeNumber(m_context, data[i]);
193 if (!JSSetArrayElement(m_context, result, i, value))
195 ThrowMsg(WrtDeviceApis::Commons::UnknownException, "Could not fill array.");
200 std::string BluetoothConverter::toBluetoothDeviceAddress(JSValueRef deviceAddress)
202 if (!JSValueIsString(m_context, deviceAddress) || JSValueIsNull(m_context, deviceAddress))
204 ThrowMsg(ConversionException, "Not a string");
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";
211 if (!re.FullMatch(address))
213 ThrowMsg(ConversionException, "not a bluetooth device address");
216 if (address.size() != compareAddress.size())
218 ThrowMsg(InvalidArgumentException, "too long or too short parameter");
224 std::string BluetoothConverter::toBluetoothUUIDString(JSValueRef uuidString)
226 if (!JSValueIsString(m_context, uuidString) || JSValueIsNull(m_context, uuidString))
228 ThrowMsg(ConversionException, "Not a string");
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";
235 if (!re.FullMatch(uuid))
237 LogDebug("not a bluetooth service uuid");
238 ThrowMsg(ConversionException, "not a bluetooth service uuid");
241 if (uuid.size() != compareUUID.size())
243 LogDebug("too long or too short parameter");
244 ThrowMsg(InvalidArgumentException, "too long or too short parameter");
254 unsigned short BluetoothConverter::toBluetoothSecurityLevelInt(JSValueRef level)
256 unsigned short securityLevel= SECURITY_LEVEL_HIGH_VALUE_INT;
258 if (!JSValueIsNull(m_context, level))
260 if (!JSValueIsString(m_context, level))
262 ThrowMsg(ConversionException, "Not a string");
265 std::string securityLevelStr = toString(level);
267 if(strcmp(securityLevelStr.c_str(), SECURITY_LEVEL_HIGH_VALUE) == 0)
269 securityLevel = SECURITY_LEVEL_HIGH_VALUE_INT;
273 if (strcmp(securityLevelStr.c_str(), SECURITY_LEVEL_MEDIUM_VALUE) == 0 ||
274 strcmp(securityLevelStr.c_str(), SECURITY_LEVEL_LOW_VALUE) == 0)
276 Throw(UnsupportedException);
280 Throw(ConversionException);
284 return securityLevel;
289 JSValueRef BluetoothConverter::toBluetoothSecurityLevelJSValueRef(unsigned short level)
295 case SECURITY_LEVEL_HIGH_VALUE_INT:
296 result = toJSValueRef(SECURITY_LEVEL_HIGH_VALUE);
298 case SECURITY_LEVEL_MEDIUM_VALUE_INT:
299 result = toJSValueRef(SECURITY_LEVEL_MEDIUM_VALUE);
301 case SECURITY_LEVEL_LOW_VALUE_INT:
302 result = toJSValueRef(SECURITY_LEVEL_LOW_VALUE);
305 result = toJSValueRef(SECURITY_LEVEL_HIGH_VALUE);
310 JSValueRef BluetoothConverter::toBluetoothProtocolJSValue(unsigned short int protocol)
312 if (protocol == PROTOCOL_TYPE_RFCOMM_VALUE_INT)
314 return toJSValueRef(PROTOCOL_TYPE_RFCOMM_VALUE);
317 else if (protocol == PROTOCOL_TYPE_L2CAP_VALUE_INT)
319 return toJSValueRef(PROTOCOL_TYPE_L2CAP_VALUE);
323 ThrowMsg(ConversionException, "Not a protocol value");
327 JSValueRef BluetoothConverter::toBluetoothStateJSValue(unsigned short int state)
329 if (state == SOCKET_STATE_CLOSED_VALUE_INT)
331 return toJSValueRef(SOCKET_STATE_CLOSED_VALUE);
334 else if (state == SOCKET_STATE_OPEN_VALUE_INT)
336 return toJSValueRef(SOCKET_STATE_OPEN_VALUE);
340 ThrowMsg(ConversionException, "Not a state value");