Update change log and spec for wrt-plugins-tizen_0.4.44
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothHealthChannel.cpp
1 //\r
2 // Tizen Web Device API\r
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.\r
4 //\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
8 //\r
9 // http://www.apache.org/licenses/LICENSE-2.0\r
10 //\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
16 //\r
17 \r
18 #include <vector>\r
19 \r
20 #include <SecurityExceptions.h>\r
21 #include <JSUtil.h>\r
22 #include <ArgumentValidator.h>\r
23 #include <GlobalContextManager.h>\r
24 #include <PlatformException.h>\r
25 #include <MultiCallbackUserData.h>\r
26 \r
27 #include "plugin_config.h"\r
28 #include "JSBluetoothHealthChannel.h"\r
29 #include "BluetoothHealthProfileHandler.h"\r
30 \r
31 #include <TimeTracer.h>\r
32 #include <Logger.h>\r
33 \r
34 using namespace WrtDeviceApis::Commons;\r
35 using namespace DeviceAPI::Common;\r
36 \r
37 namespace DeviceAPI {\r
38 namespace Bluetooth {\r
39 \r
40 JSClassDefinition JSBluetoothHealthChannel::m_classInfo = {\r
41     0,\r
42     kJSClassAttributeNone,\r
43     "BluetoothHealthChannel",\r
44     NULL, //ParentClass\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
58 };\r
59 \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
65     { 0, 0, 0, 0 }\r
66 };\r
67 \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
73     { 0, 0, 0 }\r
74 };\r
75 \r
76 JSClassRef JSBluetoothHealthChannel::m_jsClassRef = JSClassCreate(JSBluetoothHealthChannel::getClassInfo());\r
77 \r
78 const JSClassRef JSBluetoothHealthChannel::getClassRef()\r
79 {\r
80     if (!m_jsClassRef) {\r
81         m_jsClassRef = JSClassCreate(&m_classInfo);\r
82     }\r
83     return m_jsClassRef;\r
84 }\r
85 \r
86 const JSClassDefinition* JSBluetoothHealthChannel::getClassInfo()\r
87 {\r
88     return &m_classInfo;\r
89 }\r
90 \r
91 JSObjectRef JSBluetoothHealthChannel::createJSObject(JSContextRef context, BluetoothHealthChannelPtr channel)\r
92 {\r
93     return JSObjectMake(context, getClassRef(), static_cast<void*>(channel));\r
94 }\r
95 \r
96 void JSBluetoothHealthChannel::initialize(JSContextRef context, JSObjectRef object)\r
97 {\r
98     // do nothing\r
99 }\r
100 \r
101 void JSBluetoothHealthChannel::finalize(JSObjectRef object)\r
102 {\r
103     BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object));\r
104     if (priv) {\r
105         JSObjectSetPrivate(object, NULL);\r
106         delete priv;\r
107     }\r
108 }\r
109 \r
110 JSValueRef JSBluetoothHealthChannel::getProperty(JSContextRef context,\r
111         JSObjectRef object,\r
112         JSStringRef propertyName,\r
113         JSValueRef* exception)\r
114 {\r
115     try {\r
116         BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(object));\r
117         if (!priv) {\r
118             throw TypeMismatchException("Private object is NULL");\r
119         }\r
120 \r
121         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_PEER)) {\r
122             return priv->getPeer(context);\r
123         }\r
124         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_TYPE)) {\r
125             return JSUtil::toJSValueRef(context, priv->getChannelTypeStr());\r
126         }\r
127         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_APP)) {\r
128             return priv->getApp(context);\r
129         }\r
130         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_HEALTH_CHANNEL_IS_CONNECTED)) {\r
131             return JSUtil::toJSValueRef(context, priv->getConnectionState());\r
132         }\r
133     } catch (const BasePlatformException &err) {\r
134         LoggerW("Getting property is failed: " << err.getMessage().c_str());\r
135     }\r
136 \r
137     return NULL;\r
138 }\r
139 \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
146 {\r
147     LoggerD("Enter");\r
148 \r
149     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;\r
150 \r
151     // Access Check\r
152     /*\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
157     */\r
158 \r
159     try {\r
160         // Private Object\r
161         BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));\r
162         if (!priv) {\r
163             throw DeviceAPI::Common::UnknownException("Private object is NULL.");\r
164         }\r
165     \r
166         ArgumentValidator validator(context, argumentCount, arguments);\r
167 \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
174         }\r
175 \r
176         JSValueRef result = JSUtil::toJSValueRef(context, priv->sendData(buffer, size));\r
177         delete buffer;\r
178         TIME_TRACER_ITEM_END(__FUNCTION__, 1);;\r
179 \r
180         return result;\r
181     } catch (const BasePlatformException &err) {\r
182         return JSWebAPIErrorFactory::postException(context, exception, err);\r
183     } catch (...) {\r
184         DeviceAPI::Common::UnknownException err("Unknown Error");\r
185         return JSWebAPIErrorFactory::postException(context, exception, err);\r
186     }\r
187 }\r
188 \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
195 {\r
196     LoggerD("Enter");\r
197 \r
198     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;\r
199     \r
200     // Access Check\r
201     /*\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
206     */\r
207 \r
208     try {\r
209         // Private Object\r
210         BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));\r
211         if (!priv) {\r
212             throw DeviceAPI::Common::UnknownException("Private object is NULL.");\r
213         }\r
214 \r
215         priv->close();\r
216         TIME_TRACER_ITEM_END(__FUNCTION__, 1);;\r
217         \r
218         return JSValueMakeUndefined(context);\r
219     } catch (const BasePlatformException &err) {\r
220         return JSWebAPIErrorFactory::postException(context, exception, err);\r
221     } catch (...) {\r
222         DeviceAPI::Common::UnknownException err("Unknown Error");\r
223         return JSWebAPIErrorFactory::postException(context, exception, err);\r
224     }\r
225 }\r
226 \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
233 {\r
234     LoggerD("Enter");\r
235 \r
236     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;\r
237     \r
238     // Access Check\r
239     /*\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
244     */\r
245 \r
246     try {\r
247         // Private Object\r
248         BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));\r
249         if (!priv) {\r
250             throw DeviceAPI::Common::UnknownException("Private object is NULL.");\r
251         }\r
252 \r
253         // Validate arguments\r
254         ArgumentValidator validator(context, argumentCount, arguments);\r
255 \r
256         // successCallback\r
257         JSObjectRef successCallback = validator.toCallbackObject(0, false, "onmessage", "onclose", NULL);\r
258 \r
259         MultiCallbackUserDataPtr callback(\r
260                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));\r
261         if(!callback){\r
262             LoggerW("Can't create MultiMultiCallbackUserData");\r
263         }\r
264         else {\r
265             // onmessage\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
270             }\r
271             \r
272             // onclose\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
277             }\r
278         }\r
279 \r
280         priv->setListener(callback);\r
281         TIME_TRACER_ITEM_END(__FUNCTION__, 1);;\r
282         \r
283         return JSValueMakeUndefined(context);\r
284     } catch (const BasePlatformException &err) {\r
285         return JSWebAPIErrorFactory::postException(context, exception, err);\r
286     } catch (...) {\r
287         DeviceAPI::Common::UnknownException err("Unknown Error");\r
288         return JSWebAPIErrorFactory::postException(context, exception, err);\r
289     }\r
290 }\r
291 \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
298 {\r
299     LoggerD("Enter");\r
300 \r
301     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);;\r
302     \r
303     // Access Check\r
304     /*\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
309     */\r
310 \r
311     try {\r
312         // Private Object\r
313         BluetoothHealthChannelPtr priv = static_cast<BluetoothHealthChannelPtr>(JSObjectGetPrivate(thisObject));\r
314         if (!priv) {\r
315             throw DeviceAPI::Common::UnknownException("Private object is NULL.");\r
316         }\r
317 \r
318         priv->unsetListener();\r
319         TIME_TRACER_ITEM_END(__FUNCTION__, 1);;\r
320         \r
321         return JSValueMakeUndefined(context);\r
322     } catch (const BasePlatformException &err) {\r
323         return JSWebAPIErrorFactory::postException(context, exception, err);\r
324     } catch (...) {\r
325         DeviceAPI::Common::UnknownException err("Unknown Error");\r
326         return JSWebAPIErrorFactory::postException(context, exception, err);\r
327     }\r
328 }\r
329 \r
330 \r
331 \r
332 } // Bluetooth\r
333 } // DeviceAPI\r
334 \r