tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothGATTCharacteristic.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 #include "JSBluetoothGATTCharacteristic.h"
18 #include "JSBluetoothGATTBase.h"
19 #include <Logger.h>
20 #include <PlatformException.h>
21 #include <JSUtil.h>
22 #include "plugin_config_impl.h"
23 #include <ArgumentValidator.h>
24 #include <GlobalContextManager.h>
25 #include <JSWebAPIErrorFactory.h>
26 #include <MultiCallbackUserData.h>
27
28 namespace DeviceAPI {
29 namespace Bluetooth {
30
31 JSClassDefinition JSBluetoothGATTCharacteristic::m_classInfo = {
32     kJSClassAttributeNone,
33     0,
34     "BluetoothGATTCharacteristic",
35     JSBluetoothGATTBase::getClassRef(), //ParentClass
36     m_property, //StaticValues
37     m_function, //StaticFunctions
38     NULL, //Initialize
39     NULL, //Finalize
40     NULL, //HasProperty,
41     NULL, //GetProperty,
42     NULL, //SetProperty,
43     NULL, //DeleteProperty,
44     NULL, //GetPropertyNames,
45     NULL, //CallAsFunction,
46     NULL, //CallAsConstructor,
47     NULL, //HasInstance,
48     NULL //ConvertToType
49 };
50
51 JSStaticFunction JSBluetoothGATTCharacteristic::m_function[] = {
52     {
53         BLUETOOTH_LOW_ENERGY_ADD_VALUE_LISTENER,
54         addValueChangeListener,
55         kJSPropertyAttributeNone
56     },
57     {
58         BLUETOOTH_LOW_ENERGY_REMOVE_VALUE_LISTENER,
59         removeValueChangeListener,
60         kJSPropertyAttributeNone
61     },
62     { 0, 0, 0 }
63 };
64
65 JSStaticValue JSBluetoothGATTCharacteristic::m_property[] = {
66     {
67         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISBROADCAST,
68         getProperty,
69         NULL,
70         kJSPropertyAttributeDontDelete |
71         kJSPropertyAttributeReadOnly
72     },
73     {
74         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_HASEXTENDED,
75         getProperty,
76         NULL,
77         kJSPropertyAttributeDontDelete |
78         kJSPropertyAttributeReadOnly
79     },
80     {
81         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISNOTIFY,
82         getProperty,
83         NULL,
84         kJSPropertyAttributeDontDelete |
85         kJSPropertyAttributeReadOnly
86     },
87     {
88         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISINDIE,
89         getProperty,
90         NULL,
91         kJSPropertyAttributeDontDelete |
92         kJSPropertyAttributeReadOnly
93     },
94     {
95         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISREAD,
96         getProperty,
97         NULL,
98         kJSPropertyAttributeDontDelete |
99         kJSPropertyAttributeReadOnly
100     },
101     {
102         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISSIGNEDWRITE,
103         getProperty,
104         NULL,
105         kJSPropertyAttributeDontDelete |
106         kJSPropertyAttributeReadOnly
107     },
108     {
109         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITE,
110         getProperty,
111         NULL,
112         kJSPropertyAttributeDontDelete |
113         kJSPropertyAttributeReadOnly
114     },
115     {
116         BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITENORESPONSE,
117         getProperty,
118         NULL,
119         kJSPropertyAttributeDontDelete |
120         kJSPropertyAttributeReadOnly
121     },
122     {
123         BLUETOOTH_GATT_LOW_ENERGY_CHARACTERISTIC_DESCRIPTORS,
124         getProperty,
125         NULL,
126         kJSPropertyAttributeNone |
127         kJSPropertyAttributeReadOnly |
128         kJSPropertyAttributeDontDelete
129     },
130     {0,0,0,0}
131 };
132
133 JSValueRef JSBluetoothGATTCharacteristic::getProperty(JSContextRef context,
134         JSObjectRef object,
135         JSStringRef propertyName,
136         JSValueRef* exception)
137 {
138     LOGD("Enter");
139
140     try {
141         auto priv = getPrivateObject(context, object);
142
143         if(JSStringIsEqualToUTF8CString(propertyName,
144             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISBROADCAST)) {
145             LOGD("get isBroadCast");
146             return priv->getIsBroadCast(context);
147         }
148         else if(JSStringIsEqualToUTF8CString(propertyName,
149             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_HASEXTENDED)) {
150             LOGD("get hasExtended");
151             return priv->getIsHasExtended(context);
152         }
153         else if(JSStringIsEqualToUTF8CString(propertyName,
154             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISNOTIFY)) {
155             LOGD("get isNotify");
156             return priv->getIsNotify(context);
157         }
158         else if(JSStringIsEqualToUTF8CString(propertyName,
159             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISINDIE)) {
160             LOGD("get isIndie");
161             return priv->getIsIndie(context);
162         }
163         else if(JSStringIsEqualToUTF8CString(propertyName,
164             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISREAD)) {
165             LOGD("get isRead");
166             return priv->getIsRead(context);
167         }
168         else if(JSStringIsEqualToUTF8CString(propertyName,
169             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISSIGNEDWRITE)) {
170             LOGD("get isSignedWrite");
171             return priv->getIsSignedWrite(context);
172         }
173         else if(JSStringIsEqualToUTF8CString(propertyName,
174             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITE)) {
175             LOGD("get isWrite");
176             return priv->getIsWrite(context);
177         }
178         else if(JSStringIsEqualToUTF8CString(propertyName,
179             BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITENORESPONSE)) {
180             LOGD("get isWriteNoResponse");
181             return priv->getIsWriteNoResponse(context);
182         }
183         else if(JSStringIsEqualToUTF8CString(propertyName,
184             BLUETOOTH_GATT_LOW_ENERGY_CHARACTERISTIC_DESCRIPTORS)) {
185             LOGD("get descriptors");
186             return priv->getDescriptors(context);
187         }
188
189     } catch (const Common::BasePlatformException &err) {
190         LOGW("Getting property is failed: %s", err.getMessage().c_str());
191     }
192
193     return NULL;
194 }
195
196 JSClassRef JSBluetoothGATTCharacteristic::m_jsClassRef =
197     JSClassCreate(JSBluetoothGATTCharacteristic::getClassInfo());
198
199 const JSClassRef JSBluetoothGATTCharacteristic::getClassRef()
200 {
201     if (!m_jsClassRef) {
202         m_jsClassRef = JSClassCreate(&m_classInfo);
203     }
204     return m_jsClassRef;
205 }
206
207 const JSClassDefinition* JSBluetoothGATTCharacteristic::getClassInfo()
208 {
209     return &m_classInfo;
210 }
211
212 JSObjectRef JSBluetoothGATTCharacteristic::createJSObject(JSContextRef context,
213     BluetoothGATTCharacteristicPtr _pCharacteristic)
214 {
215     BluetoothGATTBaseHolder* priv = new BluetoothGATTBaseHolder(_pCharacteristic);
216     return JSObjectMake(context, getClassRef(), priv);
217 }
218
219 BluetoothGATTCharacteristicPtr JSBluetoothGATTCharacteristic::getPrivateObject(JSContextRef context,
220         JSValueRef value_ref)
221 {
222     LOGD("Entered");
223     if (!JSValueIsObjectOfClass(context, value_ref, getClassRef())) {
224         LOGE("TypeMismatch");
225         throw Common::TypeMismatchException("TypeMismatch");
226     }
227
228     JSObjectRef object = Common::JSUtil::JSValueToObject(context, value_ref);
229     BluetoothGATTBaseHolder* priv = static_cast<BluetoothGATTBaseHolder*>(JSObjectGetPrivate(object));
230     if (!priv) {
231         LOGE("Priv is null");
232         throw Common::UnknownException("Priv is null");
233     }
234
235     if (!(priv->ptr)) {
236         LOGE("Native is null");
237         throw Common::UnknownException("Native is null");
238     }
239
240     BluetoothGATTCharacteristicPtr characteristicPtr = std::dynamic_pointer_cast<BluetoothGATTCharacteristic>(priv->ptr);
241     if(!characteristicPtr) {
242         LOGE("Invalid cast to BluetoothGATTCharacteristicPtr");
243         throw Common::UnknownException("Invalid cast to BluetoothGATTCharacteristicPtr");
244     }
245
246     return characteristicPtr;
247 }
248
249 JSValueRef JSBluetoothGATTCharacteristic::addValueChangeListener(JSContextRef context,
250     JSObjectRef object,
251     JSObjectRef thisObject,
252     size_t argumentCount,
253     const JSValueRef arguments[],
254     JSValueRef* exception) {
255
256     LOGD("Entered");
257     std::shared_ptr<Common::MultiCallbackUserData> callback = NULL;
258     try {
259
260         BluetoothGATTCharacteristicPtr priv = getPrivateObject(context, thisObject);
261         Common::ArgumentValidator validator(context, argumentCount, arguments);
262
263         JSObjectRef successCallback = validator.toFunction(0, false );
264
265         callback.reset( new Common::MultiCallbackUserData(
266                 Common::GlobalContextManager::getInstance()->getGlobalContext(context)) );
267         if (!callback){
268             LOGW("Can't create ReadValueSuccessCallback");
269             throw Common::UnknownException("Failed to add value listener");
270         }
271
272         callback->setCallback("onread", successCallback);
273
274         long id = priv->addValueChangeListener(callback);
275
276         return Common::JSUtil::toJSValueRef(context, id);
277     } catch (const Common::BasePlatformException &err) {
278         return Common::JSWebAPIErrorFactory::postException(context, exception, err);
279     } catch (...) {
280         Common::UnknownException err(
281             "Unknown Error in JSBluetoothGATTCharacteristic addValueChangeListener");
282         return Common::JSWebAPIErrorFactory::postException(context, exception, err);
283     }
284 }
285
286 JSValueRef JSBluetoothGATTCharacteristic::removeValueChangeListener(JSContextRef context,
287     JSObjectRef object,
288     JSObjectRef thisObject,
289     size_t argumentCount,
290     const JSValueRef arguments[],
291     JSValueRef* exception) {
292
293     LOGD("Entered");
294     try {
295
296         BluetoothGATTCharacteristicPtr priv = getPrivateObject(context, thisObject);
297         Common::ArgumentValidator validator(context, argumentCount, arguments);
298
299         long id = validator.toLong(0, false);
300
301         priv->removeValueChangeListener(id);
302
303         return JSValueMakeUndefined(context);
304     } catch (const Common::BasePlatformException &err) {
305         return Common::JSWebAPIErrorFactory::postException(context, exception, err);
306     } catch (...) {
307         Common::UnknownException err(
308             "Unknown Error in JSBluetoothGATTCharacteristic removeValueChangeListener");
309         return Common::JSWebAPIErrorFactory::postException(context, exception, err);
310     }
311 }
312
313 } // Bluetooth
314 } // DeviceAPI
315
316