2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <SecurityExceptions.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
25 #include <MultiCallbackUserData.h>
27 #include "plugin_config.h"
28 #include "JSBluetoothSocket.h"
29 #include "BluetoothSocket.h"
30 #include "BluetoothAdapter.h"
32 #include <TimeTracer.h>
35 using namespace WrtDeviceApis::Commons;
36 using namespace DeviceAPI::Common;
41 JSClassDefinition JSBluetoothSocket::m_classInfo = {
43 kJSClassAttributeNone,
46 m_property, //StaticValues
47 m_function, //StaticFunctions
48 initialize, //Initialize
53 NULL, //DeleteProperty,
54 NULL, //GetPropertyNames,
55 NULL, //CallAsFunction,
56 NULL, //CallAsConstructor,
61 JSStaticValue JSBluetoothSocket::m_property[] = {
62 { BLUETOOTH_SOCKET_UUID, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_SOCKET_STATE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
64 { BLUETOOTH_SOCKET_PEER, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
65 { BLUETOOTH_SOCKET_ONMESSAGE, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
66 { BLUETOOTH_SOCKET_ONCLOSE, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
67 { BLUETOOTH_SOCKET_ONERROR, getProperty, setProperty, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
71 JSStaticFunction JSBluetoothSocket::m_function[] = {
72 { BLUETOOTH_SOCKET_API_WRITE_DATA, writeData, kJSPropertyAttributeNone },
73 { BLUETOOTH_SOCKET_API_READ_DATA, readData, kJSPropertyAttributeNone },
74 { BLUETOOTH_SOCKET_API_CLOSE, close, kJSPropertyAttributeNone },
78 JSClassRef JSBluetoothSocket::m_jsClassRef = JSClassCreate(JSBluetoothSocket::getClassInfo());
80 const JSClassRef JSBluetoothSocket::getClassRef()
83 m_jsClassRef = JSClassCreate(&m_classInfo);
88 const JSClassDefinition* JSBluetoothSocket::getClassInfo()
93 JSObjectRef JSBluetoothSocket::createJSObject(JSContextRef context, BluetoothSocketPtr socket)
95 return JSObjectMake(context, getClassRef(), static_cast<void*>(socket));
98 void JSBluetoothSocket::initialize(JSContextRef context, JSObjectRef object)
103 void JSBluetoothSocket::finalize(JSObjectRef object)
105 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object));
107 JSObjectSetPrivate(object, NULL);
112 JSValueRef JSBluetoothSocket::getProperty(JSContextRef context,
114 JSStringRef propertyName,
115 JSValueRef* exception)
118 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object));
120 throw TypeMismatchException("Private object is NULL");
123 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_UUID)) {
124 return JSUtil::toJSValueRef(context, priv->getUUID());
126 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_STATE)) {
127 LoggerD("get state");
129 if(priv->getConnectionState()) {
135 LoggerD("state: " << state);
136 return JSUtil::toJSValueRef(context, state);
138 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_PEER)) {
139 return priv->getPeer(context);
141 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONMESSAGE)) {
142 return priv->getOnMessage(context);
144 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONCLOSE)) {
145 return priv->getOnClose(context);
147 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONERROR)) {
148 return priv->getOnError(context);
150 } catch (const BasePlatformException &err) {
151 LoggerW("Getting property is failed: " << err.getMessage().c_str());
157 bool JSBluetoothSocket::setProperty(JSContextRef context,
159 JSStringRef propertyName,
161 JSValueRef* exception)
166 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object));
168 throw TypeMismatchException("Private object is NULL");
171 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONMESSAGE)) {
172 JSObjectRef object = NULL;
173 if(!JSValueIsNull(context, value)) {
174 if(!JSValueIsObject(context, value)) {
175 throw TypeMismatchException("Value is not Object");
178 object = JSUtil::JSValueToObject(context, value);
180 if(!JSObjectIsFunction(context, object)) {
181 throw TypeMismatchException("Not function");
185 LoggerD("onmessage() is NULL");
188 return priv->setOnMessage(context, object);
190 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONCLOSE)) {
191 JSObjectRef object = NULL;
192 if(!JSValueIsNull(context, value)) {
193 if(!JSValueIsObject(context, value)) {
194 throw TypeMismatchException("Value is not Object");
197 object = JSUtil::JSValueToObject(context, value);
199 if(!JSObjectIsFunction(context, object)) {
200 throw TypeMismatchException("Not function");
204 LoggerD("onclose() is NULL");
207 return priv->setOnClose(context, object);
209 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONERROR)) {
210 JSObjectRef object = NULL;
211 if(!JSValueIsNull(context, value)) {
212 if(!JSValueIsObject(context, value)) {
213 throw TypeMismatchException("Value is not Object");
216 object = JSUtil::JSValueToObject(context, value);
218 if(!JSObjectIsFunction(context, object)) {
219 throw TypeMismatchException("Not function");
223 LoggerD("onerror() is NULL");
226 return priv->setOnError(context, object);
228 } catch (const BasePlatformException &err) {
229 JSWebAPIErrorFactory::postException(context, exception, err);
235 JSValueRef JSBluetoothSocket::writeData(JSContextRef context,
237 JSObjectRef thisObject,
238 size_t argumentCount,
239 const JSValueRef arguments[],
240 JSValueRef* exception)
242 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
246 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject));
248 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
252 TIME_TRACER_ITEM_BEGIN("writeData::ACE", 1);
253 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SOCKET_API_WRITE_DATA);
254 TIME_TRACER_ITEM_END("writeData::ACE", 1);
256 ArgumentValidator validator(context, argumentCount, arguments);
258 JSObjectRef dataArrayObj = validator.toArrayObject(0); // data
259 size_t size = JSGetArrayLength(context, dataArrayObj);
260 char *buffer = new char[size];
261 for(size_t i = 0; i < size; ++i) {
262 JSValueRef element = JSGetArrayElement(context, dataArrayObj, i);
263 buffer[i] = static_cast<char>(JSUtil::JSValueToByte(context, element));
266 JSValueRef result = JSUtil::toJSValueRef(context, priv->writeData(buffer, size));
268 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
271 } catch (const BasePlatformException &err) {
272 return JSWebAPIErrorFactory::postException(context, exception, err);
274 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.writeData().");
275 return JSWebAPIErrorFactory::postException(context, exception, err);
279 JSValueRef JSBluetoothSocket::readData(JSContextRef context,
281 JSObjectRef thisObject,
282 size_t argumentCount,
283 const JSValueRef arguments[],
284 JSValueRef* exception)
286 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
291 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject));
293 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
297 TIME_TRACER_ITEM_BEGIN("readData::ACE", 1);
298 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SOCKET_API_READ_DATA);
299 TIME_TRACER_ITEM_END("readData::ACE", 1);
302 std::vector<signed char> data = priv->readData();
303 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
305 return JSUtil::toJSValueRef_(context, data);
306 } catch (const BasePlatformException &err) {
307 return JSWebAPIErrorFactory::postException(context, exception, err);
309 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.readData().");
310 return JSWebAPIErrorFactory::postException(context, exception, err);
314 JSValueRef JSBluetoothSocket::close(JSContextRef context,
316 JSObjectRef thisObject,
317 size_t argumentCount,
318 const JSValueRef arguments[],
319 JSValueRef* exception)
321 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
325 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject));
327 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
331 TIME_TRACER_ITEM_BEGIN("close::ACE", 1);
332 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_SOCKET_API_CLOSE);
333 TIME_TRACER_ITEM_END("close::ACE", 1);
337 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
339 return JSValueMakeUndefined(context);
340 } catch (const BasePlatformException &err) {
341 return JSWebAPIErrorFactory::postException(context, exception, err);
343 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.close().");
344 return JSWebAPIErrorFactory::postException(context, exception, err);