2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <CommonsJavaScript/Converter.h>
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <Tizen/Common/SecurityExceptions.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include "JSBluetoothClass.h"
26 #include "plugin_config.h"
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace TizenApis::Commons;
38 JSClassDefinition JSBluetoothClass::m_classInfo =
41 kJSClassAttributeNone,
60 JSStaticFunction JSBluetoothClass::m_function[] =
62 { "hasService", JSBluetoothClass::hasService, kJSPropertyAttributeNone },
68 JSStaticValue JSBluetoothClass::m_properties[] =
70 {"major", JSBluetoothClass::getProperty, NULL, kJSPropertyAttributeReadOnly },
71 {"minor", JSBluetoothClass::getProperty, NULL, kJSPropertyAttributeReadOnly },
72 {"services", JSBluetoothClass::getProperty, NULL, kJSPropertyAttributeReadOnly },
77 const JSClassRef JSBluetoothClass::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_classInfo);
85 const JSClassDefinition* JSBluetoothClass::getClassInfo()
90 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
92 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object)
94 LogDebug("JSBluetoothClass::initialize ");
95 JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(object));
99 BluetoothClassPtr btClass(new BluetoothClass());
100 priv = new JSBluetoothClassPriv( context, btClass);
101 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
103 LogError("Object can't store private data.");
109 LogDebug("JSBluetoothClass::already exist ");
114 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context)
116 BluetoothClassPtr btClass(new BluetoothClass());
117 JSBluetoothClassPriv* priv = new JSBluetoothClassPriv( context, btClass);
118 btClass->setReadOnly();
120 return JSObjectMake(context, getClassRef(), priv);
123 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, int device)
125 BluetoothClassPtr btClass(new BluetoothClass());
126 JSBluetoothClassPriv* priv = new JSBluetoothClassPriv( context, btClass);
128 btClass->fromInt(device);
129 btClass->setReadOnly();
131 return JSObjectMake(context, getClassRef(), priv);
135 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothDeviceDataClass devClass)
137 BluetoothClassPtr btClass(new BluetoothClass());
138 JSBluetoothClassPriv* priv = new JSBluetoothClassPriv( context, btClass);
140 btClass->setMajor(devClass.major);
141 btClass->setMinor(devClass.minor);
142 btClass->setServices(devClass.majorServiceMask);
143 btClass->setReadOnly();
145 LogDebug("Major:" << std::hex << devClass.major << "Minor:" << std::hex << devClass.minor << "ServiceClass:" << devClass.majorServiceMask);
147 return JSObjectMake(context, getClassRef(), priv);
151 void JSBluetoothClass::finalize(JSObjectRef object)
153 JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(object));
155 LogDebug("JSBluetoothClass::Finalrize");
159 JSObjectSetPrivate(object, NULL);
164 bool JSBluetoothClass::hasInstance(JSContextRef context, JSObjectRef constructor,
165 JSValueRef possibleInstance, JSValueRef* exception)
167 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
170 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
172 JSStringRef propertyName,
173 JSValueRef* exception)
177 Converter converter(context);
178 JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(object));
184 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
187 BluetoothClassPtr btClass(priv->getObject());
188 std::string key = converter.toString(propertyName);
193 value = btClass->getMajor();
194 return converter.toJSValueRef(value);
196 else if (key == "minor")
198 value = btClass->getMinor();
199 return converter.toJSValueRef(value);
201 else if (key == "services")
203 std::vector<int> service = btClass->getServices();
204 JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
208 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Could not create array object.");
211 for (std::size_t i = 0; i < service.size(); ++i)
213 JSValueRef value = JSValueMakeNumber(context, service[i]);
215 if (!JSSetArrayElement(context, result, i, value))
217 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Could not fill array.");
225 btClass->find(key, value);
226 return converter.toJSValueRef(value);
229 Catch (WrtDeviceApis::Commons::Exception)
231 LogError("Unkwon Exception");
233 return JSValueMakeNull(context);
236 JSValueRef JSBluetoothClass::hasService(JSContextRef context, JSObjectRef object,
237 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
238 JSValueRef* exception)
241 JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(thisObject));
247 LogError("priv null");
249 Throw(WrtDeviceApis::Commons::UnknownException);
252 if (argumentCount != 1 || JSValueIsNull(context, arguments[0]) == true
253 || JSValueIsNumber(context, arguments[0]) == false)
255 LogError("argument fail");
256 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
260 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
262 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_BLUETOOTHCLASS_HAS_SERVICE]);
264 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
266 BluetoothClassPtr btClass(priv->getObject());
267 std::vector<int> service = btClass->getServices();
268 Converter converter(context);
269 int queryService = converter.toInt(arguments[0]);
272 for (index = 0; index < (int)service.size(); index++)
274 if (service[index] == queryService)
276 return converter.toJSValueRef(true);
279 return converter.toJSValueRef(false);
282 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
284 LogError("InvalidArgumentException");
285 return JSTizenExceptionFactory::postException(context, exception,
286 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
288 Catch (WrtDeviceApis::Commons::SecurityException)
290 LogError("permission denied error");
291 return JSTizenExceptionFactory::postException(context, exception,
292 JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");
295 Catch (WrtDeviceApis::Commons::Exception)
297 LogError("UnkownException");
298 return JSTizenExceptionFactory::postException(context, exception,
299 JSTizenException::UNKNOWN_ERROR, "Unkown error");
301 return JSValueMakeNull(context);