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.
17 #include "JSBluetoothGATTCharacteristic.h"
18 #include "JSBluetoothGATTBase.h"
20 #include <PlatformException.h>
22 #include "plugin_config_impl.h"
23 #include <ArgumentValidator.h>
24 #include <GlobalContextManager.h>
25 #include <JSWebAPIErrorFactory.h>
26 #include <MultiCallbackUserData.h>
31 JSClassDefinition JSBluetoothGATTCharacteristic::m_classInfo = {
32 kJSClassAttributeNone,
34 "BluetoothGATTCharacteristic",
35 JSBluetoothGATTBase::getClassRef(), //ParentClass
36 m_property, //StaticValues
37 m_function, //StaticFunctions
43 NULL, //DeleteProperty,
44 NULL, //GetPropertyNames,
45 NULL, //CallAsFunction,
46 NULL, //CallAsConstructor,
51 JSStaticFunction JSBluetoothGATTCharacteristic::m_function[] = {
53 BLUETOOTH_LOW_ENERGY_ADD_VALUE_LISTENER,
54 addValueChangeListener,
55 kJSPropertyAttributeNone
58 BLUETOOTH_LOW_ENERGY_REMOVE_VALUE_LISTENER,
59 removeValueChangeListener,
60 kJSPropertyAttributeNone
65 JSStaticValue JSBluetoothGATTCharacteristic::m_property[] = {
67 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISBROADCAST,
70 kJSPropertyAttributeDontDelete |
71 kJSPropertyAttributeReadOnly
74 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_HASEXTENDED,
77 kJSPropertyAttributeDontDelete |
78 kJSPropertyAttributeReadOnly
81 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISNOTIFY,
84 kJSPropertyAttributeDontDelete |
85 kJSPropertyAttributeReadOnly
88 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISINDIE,
91 kJSPropertyAttributeDontDelete |
92 kJSPropertyAttributeReadOnly
95 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISREAD,
98 kJSPropertyAttributeDontDelete |
99 kJSPropertyAttributeReadOnly
102 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISSIGNEDWRITE,
105 kJSPropertyAttributeDontDelete |
106 kJSPropertyAttributeReadOnly
109 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITE,
112 kJSPropertyAttributeDontDelete |
113 kJSPropertyAttributeReadOnly
116 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITENORESPONSE,
119 kJSPropertyAttributeDontDelete |
120 kJSPropertyAttributeReadOnly
123 BLUETOOTH_GATT_LOW_ENERGY_CHARACTERISTIC_DESCRIPTORS,
126 kJSPropertyAttributeNone |
127 kJSPropertyAttributeReadOnly |
128 kJSPropertyAttributeDontDelete
133 JSValueRef JSBluetoothGATTCharacteristic::getProperty(JSContextRef context,
135 JSStringRef propertyName,
136 JSValueRef* exception)
141 auto priv = getPrivateObject(context, object);
143 if(JSStringIsEqualToUTF8CString(propertyName,
144 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISBROADCAST)) {
145 LOGD("get isBroadCast");
146 return priv->getIsBroadCast(context);
148 else if(JSStringIsEqualToUTF8CString(propertyName,
149 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_HASEXTENDED)) {
150 LOGD("get hasExtended");
151 return priv->getIsHasExtended(context);
153 else if(JSStringIsEqualToUTF8CString(propertyName,
154 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISNOTIFY)) {
155 LOGD("get isNotify");
156 return priv->getIsNotify(context);
158 else if(JSStringIsEqualToUTF8CString(propertyName,
159 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISINDIE)) {
161 return priv->getIsIndie(context);
163 else if(JSStringIsEqualToUTF8CString(propertyName,
164 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISREAD)) {
166 return priv->getIsRead(context);
168 else if(JSStringIsEqualToUTF8CString(propertyName,
169 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISSIGNEDWRITE)) {
170 LOGD("get isSignedWrite");
171 return priv->getIsSignedWrite(context);
173 else if(JSStringIsEqualToUTF8CString(propertyName,
174 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITE)) {
176 return priv->getIsWrite(context);
178 else if(JSStringIsEqualToUTF8CString(propertyName,
179 BLUETOOTH_LOW_ENERGY_GATT_PROPERTY_ISWRITENORESPONSE)) {
180 LOGD("get isWriteNoResponse");
181 return priv->getIsWriteNoResponse(context);
183 else if(JSStringIsEqualToUTF8CString(propertyName,
184 BLUETOOTH_GATT_LOW_ENERGY_CHARACTERISTIC_DESCRIPTORS)) {
185 LOGD("get descriptors");
186 return priv->getDescriptors(context);
189 } catch (const Common::BasePlatformException &err) {
190 LOGW("Getting property is failed: %s", err.getMessage().c_str());
196 JSClassRef JSBluetoothGATTCharacteristic::m_jsClassRef =
197 JSClassCreate(JSBluetoothGATTCharacteristic::getClassInfo());
199 const JSClassRef JSBluetoothGATTCharacteristic::getClassRef()
202 m_jsClassRef = JSClassCreate(&m_classInfo);
207 const JSClassDefinition* JSBluetoothGATTCharacteristic::getClassInfo()
212 JSObjectRef JSBluetoothGATTCharacteristic::createJSObject(JSContextRef context,
213 BluetoothGATTCharacteristicPtr _pCharacteristic)
215 BluetoothGATTBaseHolder* priv = new BluetoothGATTBaseHolder(_pCharacteristic);
216 return JSObjectMake(context, getClassRef(), priv);
219 BluetoothGATTCharacteristicPtr JSBluetoothGATTCharacteristic::getPrivateObject(JSContextRef context,
220 JSValueRef value_ref)
223 if (!JSValueIsObjectOfClass(context, value_ref, getClassRef())) {
224 LOGE("TypeMismatch");
225 throw Common::TypeMismatchException("TypeMismatch");
228 JSObjectRef object = Common::JSUtil::JSValueToObject(context, value_ref);
229 BluetoothGATTBaseHolder* priv = static_cast<BluetoothGATTBaseHolder*>(JSObjectGetPrivate(object));
231 LOGE("Priv is null");
232 throw Common::UnknownException("Priv is null");
236 LOGE("Native is null");
237 throw Common::UnknownException("Native is null");
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");
246 return characteristicPtr;
249 JSValueRef JSBluetoothGATTCharacteristic::addValueChangeListener(JSContextRef context,
251 JSObjectRef thisObject,
252 size_t argumentCount,
253 const JSValueRef arguments[],
254 JSValueRef* exception) {
257 std::shared_ptr<Common::MultiCallbackUserData> callback = NULL;
260 BluetoothGATTCharacteristicPtr priv = getPrivateObject(context, thisObject);
261 Common::ArgumentValidator validator(context, argumentCount, arguments);
263 JSObjectRef successCallback = validator.toFunction(0, false );
265 callback.reset( new Common::MultiCallbackUserData(
266 Common::GlobalContextManager::getInstance()->getGlobalContext(context)) );
268 LOGW("Can't create ReadValueSuccessCallback");
269 throw Common::UnknownException("Failed to add value listener");
272 callback->setCallback("onread", successCallback);
274 long id = priv->addValueChangeListener(callback);
276 return Common::JSUtil::toJSValueRef(context, id);
277 } catch (const Common::BasePlatformException &err) {
278 return Common::JSWebAPIErrorFactory::postException(context, exception, err);
280 Common::UnknownException err(
281 "Unknown Error in JSBluetoothGATTCharacteristic addValueChangeListener");
282 return Common::JSWebAPIErrorFactory::postException(context, exception, err);
286 JSValueRef JSBluetoothGATTCharacteristic::removeValueChangeListener(JSContextRef context,
288 JSObjectRef thisObject,
289 size_t argumentCount,
290 const JSValueRef arguments[],
291 JSValueRef* exception) {
296 BluetoothGATTCharacteristicPtr priv = getPrivateObject(context, thisObject);
297 Common::ArgumentValidator validator(context, argumentCount, arguments);
299 long id = validator.toLong(0, false);
301 priv->removeValueChangeListener(id);
303 return JSValueMakeUndefined(context);
304 } catch (const Common::BasePlatformException &err) {
305 return Common::JSWebAPIErrorFactory::postException(context, exception, err);
307 Common::UnknownException err(
308 "Unknown Error in JSBluetoothGATTCharacteristic removeValueChangeListener");
309 return Common::JSWebAPIErrorFactory::postException(context, exception, err);