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.
18 #include <SecurityExceptions.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
26 #include "plugin_config.h"
28 #include "JSBluetoothClass.h"
29 #include "BluetoothAdapter.h"
31 #include <TimeTracer.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSBluetoothClass::m_classInfo = {
42 kJSClassAttributeNone,
45 m_property, //StaticValues
46 m_function, //StaticFunctions
47 initialize, //Initialize
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
60 JSStaticValue JSBluetoothClass::m_property[] = {
61 { BLUETOOTH_CLASS_MAJOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62 { BLUETOOTH_CLASS_MINOR, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63 { BLUETOOTH_CLASS_SERVICES, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
67 JSStaticFunction JSBluetoothClass::m_function[] = {
68 { BLUETOOTH_CLASS_API_HAS_SERVICE, hasService, kJSPropertyAttributeNone },
72 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
74 const JSClassRef JSBluetoothClass::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 const JSClassDefinition* JSBluetoothClass::getClassInfo()
87 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothClassSharedPtr bluetoothClass)
89 BluetoothClassHolderPtr holder = new BluetoothClassHolder(bluetoothClass);
90 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
93 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object)
99 void JSBluetoothClass::finalize(JSObjectRef object)
101 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
103 JSObjectSetPrivate(object, NULL);
108 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
110 JSStringRef propertyName,
111 JSValueRef* exception)
114 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(object));
116 throw TypeMismatchException("Private object is NULL");
119 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MAJOR)) {
120 return JSUtil::toJSValueRef(context, priv->mClass->getMajor());
122 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_MINOR)) {
123 return JSUtil::toJSValueRef(context, priv->mClass->getMinor());
125 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_CLASS_SERVICES)) {
126 return priv->mClass->getServices(context);
128 } catch (const BasePlatformException &err) {
129 LoggerW("Getting property is failed: " << err.getMessage().c_str());
135 JSValueRef JSBluetoothClass::hasService(JSContextRef context,
137 JSObjectRef thisObject,
138 size_t argumentCount,
139 const JSValueRef arguments[],
140 JSValueRef* exception)
143 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
146 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_CLASS_API_HAS_SERVICE);
147 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
150 // Check whether Bluetooth is supported or not
151 if(!BluetoothAdapter::isBluetoothSupported()) {
152 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
156 BluetoothClassHolderPtr priv = static_cast<BluetoothClassHolderPtr>(JSObjectGetPrivate(thisObject));
158 throw TypeMismatchException("Private object is NULL.");
161 ArgumentValidator validator(context, argumentCount, arguments);
162 unsigned long service = validator.toULong(0); // uuid
163 if(service > 65535) {
164 throw InvalidValuesException("Invalid value");
166 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
168 return JSUtil::toJSValueRef(context, priv->mClass->hasService(service));
169 } catch (const BasePlatformException &err) {
170 return JSWebAPIException::throwException(context, exception, err);
172 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothClass.hasService().");
173 return JSWebAPIException::throwException(context, exception, err);