2 // Tizen Web Device API
\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
\r
5 // Licensed under the Apache License, Version 2.0 (the License);
\r
6 // you may not use this file except in compliance with the License.
\r
7 // You may obtain a copy of the License at
\r
9 // http://www.apache.org/licenses/LICENSE-2.0
\r
11 // Unless required by applicable law or agreed to in writing, software
\r
12 // distributed under the License is distributed on an "AS IS" BASIS,
\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 // See the License for the specific language governing permissions and
\r
15 // limitations under the License.
\r
20 #include <SecurityExceptions.h>
\r
22 #include <ArgumentValidator.h>
\r
23 #include <GlobalContextManager.h>
\r
24 #include <PlatformException.h>
\r
25 #include <MultiCallbackUserData.h>
\r
27 #include "plugin_config.h"
\r
28 #include "JSBluetoothHealthChannel.h"
\r
29 #include "BluetoothHealthProfileHandler.h"
\r
31 #include <TimeTracer.h>
\r
34 using namespace WrtDeviceApis::Commons;
\r
35 using namespace DeviceAPI::Common;
\r
37 namespace DeviceAPI {
\r
38 namespace Bluetooth {
\r
40 JSClassDefinition JSBluetoothHealthChannel::m_classInfo = {
\r
42 kJSClassAttributeNone,
\r
43 "BluetoothHealthChannel",
\r
45 m_property, //StaticValues
\r
46 m_function, //StaticFunctions
\r
47 initialize, //Initialize
\r
48 finalize, //Finalize
\r
49 NULL, //HasProperty,
\r
50 NULL, //GetProperty,
\r
51 NULL, //SetProperty,
\r
52 NULL, //DeleteProperty,
\r
53 NULL, //GetPropertyNames,
\r
54 NULL, //CallAsFunction,
\r
55 NULL, //CallAsConstructor,
\r
56 NULL, //HasInstance,
\r
57 NULL //ConvertToType
\r
60 JSStaticValue JSBluetoothHealthChannel::m_property[] = {
\r
61 { BLUETOOTH_HEALTH_CHANNEL_PEER, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
62 { BLUETOOTH_HEALTH_CHANNEL_TYPE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
63 { BLUETOOTH_HEALTH_CHANNEL_APP, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
\r
64 { BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeDontDelete },
\r
68 JSStaticFunction JSBluetoothHealthChannel::m_function[] = {
\r
69 { BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA, sendData, kJSPropertyAttributeNone },
\r
70 { BLUETOOTH_HEALTH_CHANNEL_API_CLOSE, close, kJSPropertyAttributeNone },
\r
71 { BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER, setListener, kJSPropertyAttributeNone },
\r
72 { BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER, unsetListener, kJSPropertyAttributeNone },
\r
76 JSClassRef JSBluetoothHealthChannel::m_jsClassRef = JSClassCreate(JSBluetoothHealthChannel::getClassInfo());
\r
78 const JSClassRef JSBluetoothHealthChannel::getClassRef()
\r
80 if (!m_jsClassRef) {
\r
81 m_jsClassRef = JSClassCreate(&m_classInfo);
\r
83 return m_jsClassRef;
\r
86 const JSClassDefinition* JSBluetoothHealthChannel::getClassInfo()
\r
88 return &m_classInfo;
\r
91 JSObjectRef JSBluetoothHealthChannel::createJSObject(JSContextRef context, BluetoothHealthChannelPtr channel)
\r
93 return JSObjectMake(context, getClassRef(), static_cast<void*>(channel));
\r
96 void JSBluetoothHealthChannel::initialize(JSContextRef context, JSObjectRef object)
\r
101 void JSBluetoothHealthChannel::finalize(JSObjectRef object)
\r
103 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object));
\r
105 JSObjectSetPrivate(object, NULL);
\r
110 JSValueRef JSBluetoothHealthChannel::getProperty(JSContextRef context,
\r
111 JSObjectRef object,
\r
112 JSStringRef propertyName,
\r
113 JSValueRef* exception)
\r
116 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object));
\r
118 throw TypeMismatchException("Private object is NULL");
\r
121 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_PEER)) {
\r
122 return priv->getPeer(context);
\r
124 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_TYPE)) {
\r
125 return JSUtil::toJSValueRef(context, priv->getChannelTypeStr());
\r
127 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_APP)) {
\r
128 return priv->getApp(context);
\r
130 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED)) {
\r
131 return JSUtil::toJSValueRef(context, priv->getConnectionState());
\r
133 } catch (const BasePlatformException &err) {
\r
134 LoggerW("Getting property is failed: " << err.getMessage().c_str());
\r
140 JSValueRef JSBluetoothHealthChannel::sendData(JSContextRef context,
\r
141 JSObjectRef object,
\r
142 JSObjectRef thisObject,
\r
143 size_t argumentCount,
\r
144 const JSValueRef arguments[],
\r
145 JSValueRef* exception)
\r
149 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
\r
153 TIME_TRACER_ITEM_BEGIN("sendData::ACE", 1);;
\r
154 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_HEALTH_CHANNEL_API_SEND_DATA);
\r
155 TIME_TRACER_ITEM_END("sendData::ACE", 1);;
\r
156 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
\r
161 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
\r
163 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
\r
166 ArgumentValidator validator(context, argumentCount, arguments);
\r
168 JSObjectRef dataArrayObj = validator.toArrayObject(0); // data
\r
169 size_t size = JSGetArrayLength(context, dataArrayObj);
\r
170 char *buffer = new char[size];
\r
171 for(size_t i = 0; i < size; ++i) {
\r
172 JSValueRef element = JSGetArrayElement(context, dataArrayObj, i);
\r
173 buffer[i] = static_cast<char>(JSUtil::JSValueToByte(context, element));
\r
176 JSValueRef result = JSUtil::toJSValueRef(context, priv->sendData(buffer, size));
\r
178 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
\r
181 } catch (const BasePlatformException &err) {
\r
182 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
184 DeviceAPI::Common::UnknownException err("Unknown Error");
\r
185 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
189 JSValueRef JSBluetoothHealthChannel::close(JSContextRef context,
\r
190 JSObjectRef object,
\r
191 JSObjectRef thisObject,
\r
192 size_t argumentCount,
\r
193 const JSValueRef arguments[],
\r
194 JSValueRef* exception)
\r
198 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
\r
202 TIME_TRACER_ITEM_BEGIN("close::ACE", 1);;
\r
203 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_HEALTH_CHANNEL_API_CLOSE);
\r
204 TIME_TRACER_ITEM_END("close::ACE", 1);;
\r
205 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
\r
210 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
\r
212 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
\r
216 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
\r
218 return JSValueMakeUndefined(context);
\r
219 } catch (const BasePlatformException &err) {
\r
220 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
222 DeviceAPI::Common::UnknownException err("Unknown Error");
\r
223 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
227 JSValueRef JSBluetoothHealthChannel::setListener(JSContextRef context,
\r
228 JSObjectRef object,
\r
229 JSObjectRef thisObject,
\r
230 size_t argumentCount,
\r
231 const JSValueRef arguments[],
\r
232 JSValueRef* exception)
\r
236 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
\r
240 TIME_TRACER_ITEM_BEGIN("setListener::ACE", 1);;
\r
241 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_HEALTH_CHANNEL_API_SET_LISTENER);
\r
242 TIME_TRACER_ITEM_END("setListener::ACE", 1);;
\r
243 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
\r
248 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
\r
250 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
\r
253 // Validate arguments
\r
254 ArgumentValidator validator(context, argumentCount, arguments);
\r
257 JSObjectRef successCallback = validator.toCallbackObject(0, false, "onmessage", "onclose", NULL);
\r
259 MultiCallbackUserDataPtr callback(
\r
260 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
\r
262 LoggerW("Can't create MultiMultiCallbackUserData");
\r
266 JSValueRef onmessageValue = JSUtil::getProperty(context , successCallback, "onmessage");
\r
267 if(!JSValueIsUndefined(context, onmessageValue)) {
\r
268 LoggerD("There is a onmessage()");
\r
269 callback->setCallback("onmessage", JSUtil::JSValueToObject(context, onmessageValue));
\r
273 JSValueRef oncloseValue = JSUtil::getProperty(context , successCallback, "onclose");
\r
274 if(!JSValueIsUndefined(context, oncloseValue)) {
\r
275 LoggerD("There is a onclose()");
\r
276 callback->setCallback("onclose", JSUtil::JSValueToObject(context, oncloseValue));
\r
280 priv->setListener(callback);
\r
281 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
\r
283 return JSValueMakeUndefined(context);
\r
284 } catch (const BasePlatformException &err) {
\r
285 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
287 DeviceAPI::Common::UnknownException err("Unknown Error");
\r
288 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
292 JSValueRef JSBluetoothHealthChannel::unsetListener(JSContextRef context,
\r
293 JSObjectRef object,
\r
294 JSObjectRef thisObject,
\r
295 size_t argumentCount,
\r
296 const JSValueRef arguments[],
\r
297 JSValueRef* exception)
\r
301 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;
\r
305 TIME_TRACER_ITEM_BEGIN("unsetListener::ACE", 1);;
\r
306 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_HEALTH_CHANNEL_API_UNSET_LISTENER);
\r
307 TIME_TRACER_ITEM_END("unsetListener::ACE", 1);;
\r
308 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
\r
313 BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));
\r
315 throw DeviceAPI::Common::UnknownException("Private object is NULL.");
\r
318 priv->unsetListener();
\r
319 TIME_TRACER_ITEM_END(__FUNCTION__, 1);;
\r
321 return JSValueMakeUndefined(context);
\r
322 } catch (const BasePlatformException &err) {
\r
323 return JSWebAPIErrorFactory::postException(context, exception, err);
\r
325 DeviceAPI::Common::UnknownException err("Unknown Error");
\r
326 return JSWebAPIErrorFactory::postException(context, exception, err);
\r