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 "JSBluetoothHealthChannel.h"
29 #include "BluetoothHealthProfileHandler.h"
31 #include <TimeTracer.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSBluetoothHealthChannel::m_classInfo = {
42 kJSClassAttributeNone,
43 "BluetoothHealthChannel",
45 m_property, //StaticValues
46 m_function, //StaticFunctions
47 initialize, //Initialize
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
60 JSStaticValue JSBluetoothHealthChannel::m_property[] = {
61 { BLUETOOTH_HEALTH_CHANNEL_PEER, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_HEALTH_CHANNEL_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_HEALTH_CHANNEL_APP, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
64 { BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
68 JSStaticFunction JSBluetoothHealthChannel::m_function[] = {
69 { BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA, sendData, kJSPropertyAttributeNone },
70 { "close", close, kJSPropertyAttributeNone },
71 { BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER, setListener, kJSPropertyAttributeNone },
72 { BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER, unsetListener, kJSPropertyAttributeNone },
76 JSClassRef JSBluetoothHealthChannel::m_jsClassRef = JSClassCreate(JSBluetoothHealthChannel::getClassInfo());
78 const JSClassRef JSBluetoothHealthChannel::getClassRef()
81 m_jsClassRef = JSClassCreate(&m_classInfo);
86 const JSClassDefinition* JSBluetoothHealthChannel::getClassInfo()
91 JSObjectRef JSBluetoothHealthChannel::createJSObject(JSContextRef context, BluetoothHealthChannelPtr channel)
93 return JSObjectMake(context, getClassRef(), static_cast<void*>(channel));
96 void JSBluetoothHealthChannel::initialize(JSContextRef context, JSObjectRef object)
101 void JSBluetoothHealthChannel::finalize(JSObjectRef object)
103 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object));
105 JSObjectSetPrivate(object, NULL);
110 JSValueRef JSBluetoothHealthChannel::getProperty(JSContextRef context,
112 JSStringRef propertyName,
113 JSValueRef* exception)
116 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object));
118 throw TypeMismatchException("Private object is NULL");
121 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_PEER)) {
122 return priv->getPeer(context);
124 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_TYPE)) {
125 return JSUtil::toJSValueRef(context, priv->getChannelTypeStr());
127 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_APP)) {
128 return priv->getApp(context);
130 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED)) {
131 return JSUtil::toJSValueRef(context, priv->getConnectionState());
133 } catch (const BasePlatformException &err) {
134 LoggerW("Getting property is failed: " << err.getMessage().c_str());
140 JSValueRef JSBluetoothHealthChannel::sendData(JSContextRef context,
142 JSObjectRef thisObject,
143 size_t argumentCount,
144 const JSValueRef arguments[],
145 JSValueRef* exception)
149 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
153 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
155 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
158 TIME_TRACER_ITEM_BEGIN("sendData::ACE", 1);;
159 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA);
160 TIME_TRACER_ITEM_END("sendData::ACE", 1);;
162 ArgumentValidator validator(context, argumentCount, arguments);
164 JSObjectRef dataArrayObj = validator.toArrayObject(0); // data
165 size_t size = JSGetArrayLength(context, dataArrayObj);
166 char *buffer = new char[size];
167 for(size_t i = 0; i < size; ++i) {
168 JSValueRef element = JSGetArrayElement(context, dataArrayObj, i);
169 buffer[i] = static_cast<char>(JSUtil::JSValueToByte(context, element));
172 JSValueRef result = JSUtil::toJSValueRef(context, priv->sendData(buffer, size));
174 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
177 } catch (const BasePlatformException &err) {
178 return JSWebAPIErrorFactory::postException(context, exception, err);
180 DeviceAPI::Common::UnknownException err("Unknown Error");
181 return JSWebAPIErrorFactory::postException(context, exception, err);
185 JSValueRef JSBluetoothHealthChannel::close(JSContextRef context,
187 JSObjectRef thisObject,
188 size_t argumentCount,
189 const JSValueRef arguments[],
190 JSValueRef* exception)
194 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
200 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
202 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
205 TIME_TRACER_ITEM_BEGIN("close::ACE", 1);;
206 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_CLOSE);
207 TIME_TRACER_ITEM_END("close::ACE", 1);;
210 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
212 return JSValueMakeUndefined(context);
213 } catch (const BasePlatformException &err) {
214 return JSWebAPIErrorFactory::postException(context, exception, err);
216 DeviceAPI::Common::UnknownException err("Unknown Error");
217 return JSWebAPIErrorFactory::postException(context, exception, err);
221 JSValueRef JSBluetoothHealthChannel::setListener(JSContextRef context,
223 JSObjectRef thisObject,
224 size_t argumentCount,
225 const JSValueRef arguments[],
226 JSValueRef* exception)
230 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
236 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
238 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
242 TIME_TRACER_ITEM_BEGIN("setListener::ACE", 1);;
243 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER);
244 TIME_TRACER_ITEM_END("setListener::ACE", 1);;
247 // Validate arguments
248 ArgumentValidator validator(context, argumentCount, arguments);
251 JSObjectRef successCallback = validator.toCallbackObject(0, false, "onmessage", "onclose", NULL);
253 MultiCallbackUserDataPtr callback(
254 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
256 LoggerW("Can't create MultiMultiCallbackUserData");
260 JSValueRef onmessageValue = JSUtil::getProperty(context , successCallback, "onmessage");
261 if(!JSValueIsUndefined(context, onmessageValue)) {
262 LoggerD("There is a onmessage()");
263 callback->setCallback("onmessage", JSUtil::JSValueToObject(context, onmessageValue));
267 JSValueRef oncloseValue = JSUtil::getProperty(context , successCallback, "onclose");
268 if(!JSValueIsUndefined(context, oncloseValue)) {
269 LoggerD("There is a onclose()");
270 callback->setCallback("onclose", JSUtil::JSValueToObject(context, oncloseValue));
274 priv->setListener(callback);
275 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
277 return JSValueMakeUndefined(context);
278 } catch (const BasePlatformException &err) {
279 return JSWebAPIErrorFactory::postException(context, exception, err);
281 DeviceAPI::Common::UnknownException err("Unknown Error");
282 return JSWebAPIErrorFactory::postException(context, exception, err);
286 JSValueRef JSBluetoothHealthChannel::unsetListener(JSContextRef context,
288 JSObjectRef thisObject,
289 size_t argumentCount,
290 const JSValueRef arguments[],
291 JSValueRef* exception)
295 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
300 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
302 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
305 TIME_TRACER_ITEM_BEGIN("unsetListener::ACE", 1);
306 TIZEN_CHECK_ACCESS(context, exception, priv, BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER);
307 TIME_TRACER_ITEM_END("unsetListener::ACE", 1);
310 priv->unsetListener();
311 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
313 return JSValueMakeUndefined(context);
314 } catch (const BasePlatformException &err) {
315 return JSWebAPIErrorFactory::postException(context, exception, err);
317 DeviceAPI::Common::UnknownException err("Unknown Error");
318 return JSWebAPIErrorFactory::postException(context, exception, err);