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 <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24 #include <PropertyBag.h>
25 #include <bluetooth.h>
27 #include "plugin_config_impl.h"
28 #include "JSBluetoothManager.h"
29 #include "JSBluetoothClassDeviceMajor.h"
30 #include "JSBluetoothClassDeviceMinor.h"
31 #include "JSBluetoothClassDeviceService.h"
32 #include "JSBluetoothAdapter.h"
33 #include "BluetoothAdapter.h"
34 #include "JSBluetoothLowEnergyAdapter.h"
35 #include "BluetoothLowEnergyAdapter.h"
37 #include <TimeTracer.h>
40 using namespace WrtDeviceApis::Commons;
41 using namespace DeviceAPI::Common;
46 JSClassDefinition JSBluetoothManager::m_classInfo = {
48 kJSClassAttributeNone,
51 m_property, //StaticValues
52 m_function, //StaticFunctions
53 initialize, //Initialize
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
66 JSStaticValue JSBluetoothManager::m_property[] = {
68 BLUETOOTH_MANAGER_DEVICE_MAJOR,
71 kJSPropertyAttributeNone |
72 kJSPropertyAttributeReadOnly |
73 kJSPropertyAttributeDontDelete
76 BLUETOOTH_MANAGER_DEVICE_MINOR,
79 kJSPropertyAttributeNone |
80 kJSPropertyAttributeReadOnly |
81 kJSPropertyAttributeDontDelete
84 BLUETOOTH_MANAGER_DEVICE_SERVICE,
87 kJSPropertyAttributeNone |
88 kJSPropertyAttributeReadOnly |
89 kJSPropertyAttributeDontDelete
94 JSStaticFunction JSBluetoothManager::m_function[] = {
96 BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER,
98 kJSPropertyAttributeNone
101 BLUETOOTH_MANAGER_API_GET_LOW_ENERGY_ADAPTER,
103 kJSPropertyAttributeNone
108 JSClassRef JSBluetoothManager::m_jsClassRef =
109 JSClassCreate(JSBluetoothManager::getClassInfo());
111 const JSClassRef JSBluetoothManager::getClassRef()
114 m_jsClassRef = JSClassCreate(&m_classInfo);
119 const JSClassDefinition* JSBluetoothManager::getClassInfo()
124 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object)
128 if (!JSObjectGetPrivate(object)) {
129 JSBluetoothManagerPriv* priv = new JSBluetoothManagerPriv();
133 priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MAJOR,
134 JSBluetoothClassDeviceMajor::createJSObject(context));
137 priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MINOR,
138 JSBluetoothClassDeviceMinor::createJSObject(context));
141 priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_SERVICE,
142 JSBluetoothClassDeviceService::createJSObject(context));
144 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
145 LOGW("Failed to set private data");
148 LOGD("Private creation ok");
151 LOGW("Failed to create private data");
155 LOGW("already has private data");
159 void JSBluetoothManager::finalize(JSObjectRef object)
161 JSBluetoothManagerPriv *priv =
162 static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
164 JSObjectSetPrivate(object, NULL);
169 JSValueRef JSBluetoothManager::getReadOnlyProperty(JSContextRef context,
171 JSStringRef propertyName,
172 JSValueRef* exception)
174 JSBluetoothManagerPriv *priv =
175 static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
177 LOGW("There is no private data");
181 std::string name = JSUtil::JSStringToString(context, propertyName);
182 return priv->getProperty(context, propertyName);
185 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context,
187 JSObjectRef thisObject,
188 size_t argumentCount,
189 const JSValueRef arguments[],
190 JSValueRef* exception)
192 JSBluetoothManagerPriv *priv =
193 static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
197 TypeMismatchException err("Private Object is null");
198 return JSWebAPIErrorFactory::postException(context, exception, err);
201 TIME_TRACER_ITEM_BEGIN("getDefaultAdapter", 1);
202 TIZEN_CHECK_ACCESS(context, exception, priv,
203 BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER);
204 TIME_TRACER_ITEM_END("getDefaultAdapter::ACE", 1);
207 BluetoothAdapter::getInstance()->copyAceCheckAccessFunction(priv);
208 JSObjectRef adapter = JSBluetoothAdapter::createJSObject(context);
210 TIME_TRACER_ITEM_END("getDefaultAdapter", 1);
213 } catch (const BasePlatformException &err) {
214 return JSWebAPIErrorFactory::postException(context, exception, err);
216 Common::UnknownException err(
217 "Unknown Error in BluetoothManager.getDefaultAdapter().");
218 return JSWebAPIErrorFactory::postException(context, exception, err);
222 JSValueRef JSBluetoothManager::getLEAdapter(JSContextRef context,
224 JSObjectRef thisObject,
225 size_t argumentCount,
226 const JSValueRef arguments[],
227 JSValueRef* exception)
229 JSBluetoothManagerPriv *priv =
230 static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
234 TypeMismatchException err("Private Object is null");
235 return JSWebAPIErrorFactory::postException(context, exception, err);
238 TIZEN_CHECK_ACCESS(context, exception, priv,
239 BLUETOOTH_MANAGER_API_GET_LOW_ENERGY_ADAPTER);
242 //make sure that BluetoothAdapter is already initialized
243 BluetoothAdapter::getInstance();
245 BluetoothLowEnergyAdapter::getInstance()->copyAceCheckAccessFunction(priv);
246 JSObjectRef adapter = JSBluetoothLowEnergyAdapter::createJSObject(context);
248 } catch (const BasePlatformException &err) {
249 return JSWebAPIErrorFactory::postException(context, exception, err);
251 Common::UnknownException err(
252 "Unknown Error in BluetoothManager.getDefaultAdapter().");
253 return JSWebAPIErrorFactory::postException(context, exception, err);