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)
164 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(object));
166 throw TypeMismatchException("Private object is NULL");
169 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONMESSAGE)) {
170 JSObjectRef object = NULL;
171 if(!JSValueIsNull(context, value)) {
172 if(!JSValueIsObject(context, value)) {
173 throw TypeMismatchException("Value is not Object");
177 object = JSValueToObject(context, value, &ex);
179 throw TypeMismatchException("Can't convert to Object");
182 if(!JSObjectIsFunction(context, object)) {
183 throw TypeMismatchException("Not function");
187 LoggerD("onmessage() is NULL");
190 return priv->setOnMessage(context, object);
192 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONCLOSE)) {
193 JSObjectRef object = NULL;
194 if(!JSValueIsNull(context, value)) {
195 if(!JSValueIsObject(context, value)) {
196 throw TypeMismatchException("Value is not Object");
200 object = JSValueToObject(context, value, &ex);
202 throw TypeMismatchException("Can't convert to Object");
205 if(!JSObjectIsFunction(context, object)) {
206 throw TypeMismatchException("Not function");
210 LoggerD("onclose() is NULL");
213 return priv->setOnClose(context, object);
215 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_SOCKET_ONERROR)) {
216 JSObjectRef object = NULL;
217 if(!JSValueIsNull(context, value)) {
218 if(!JSValueIsObject(context, value)) {
219 throw TypeMismatchException("Value is not Object");
223 object = JSValueToObject(context, value, &ex);
225 throw TypeMismatchException("Can't convert to Object");
228 if(!JSObjectIsFunction(context, object)) {
229 throw TypeMismatchException("Not function");
233 LoggerD("onerror() is NULL");
236 return priv->setOnError(context, object);
238 } catch (const BasePlatformException &err) {
239 JSWebAPIErrorFactory::postException(context, exception, err);
245 JSValueRef JSBluetoothSocket::writeData(JSContextRef context,
247 JSObjectRef thisObject,
248 size_t argumentCount,
249 const JSValueRef arguments[],
250 JSValueRef* exception)
252 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
255 //AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_SOCKET_API_WRITE_DATA);
256 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
259 // Check whether Bluetooth is supported or not
260 if(!BluetoothAdapter::isBluetoothSupported()) {
261 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
265 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject));
267 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
270 ArgumentValidator validator(context, argumentCount, arguments);
272 JSObjectRef dataArrayObj = validator.toArrayObject(0); // data
273 size_t size = JSGetArrayLength(context, dataArrayObj);
274 char *buffer = new char[size];
275 for(size_t i = 0; i < size; ++i) {
276 JSValueRef element = JSGetArrayElement(context, dataArrayObj, i);
277 buffer[i] = static_cast<char>(JSUtil::JSValueToByte(context, element));
279 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
281 return JSUtil::toJSValueRef(context, priv->writeData(buffer, size));
282 } catch (const BasePlatformException &err) {
283 return JSWebAPIErrorFactory::postException(context, exception, err);
285 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.writeData().");
286 return JSWebAPIErrorFactory::postException(context, exception, err);
290 JSValueRef JSBluetoothSocket::readData(JSContextRef context,
292 JSObjectRef thisObject,
293 size_t argumentCount,
294 const JSValueRef arguments[],
295 JSValueRef* exception)
297 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
300 //AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_SOCKET_API_READ_DATA);
301 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
304 // Check whether Bluetooth is supported or not
305 if(!BluetoothAdapter::isBluetoothSupported()) {
306 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
310 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject));
312 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
315 std::vector<signed char> data = priv->readData();
316 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
318 return JSUtil::toJSValueRef_(context, data);
319 } catch (const BasePlatformException &err) {
320 return JSWebAPIErrorFactory::postException(context, exception, err);
322 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.readData().");
323 return JSWebAPIErrorFactory::postException(context, exception, err);
327 JSValueRef JSBluetoothSocket::close(JSContextRef context,
329 JSObjectRef thisObject,
330 size_t argumentCount,
331 const JSValueRef arguments[],
332 JSValueRef* exception)
334 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
337 //AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_SOCKET_API_CLOSE);
338 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
341 // Check whether Bluetooth is supported or not
342 if(!BluetoothAdapter::isBluetoothSupported()) {
343 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
347 BluetoothSocketPtr priv = static_cast<BluetoothSocketPtr>(JSObjectGetPrivate(thisObject));
349 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
353 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
355 return JSValueMakeUndefined(context);
356 } catch (const BasePlatformException &err) {
357 return JSWebAPIErrorFactory::postException(context, exception, err);
359 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothSocket.close().");
360 return JSWebAPIErrorFactory::postException(context, exception, err);