tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothManager.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
18 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24 #include <PropertyBag.h>
25 #include <bluetooth.h>
26
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"
36
37 #include <TimeTracer.h>
38 #include <Logger.h>
39
40 using namespace WrtDeviceApis::Commons;
41 using namespace DeviceAPI::Common;
42
43 namespace DeviceAPI {
44 namespace Bluetooth {
45
46 JSClassDefinition JSBluetoothManager::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     "BluetoothManager",
50     NULL, //ParentClass
51     m_property, //StaticValues
52     m_function, //StaticFunctions
53     initialize, //Initialize
54     finalize, //Finalize
55     NULL, //HasProperty,
56     NULL, //GetProperty,
57     NULL, //SetProperty,
58     NULL, //DeleteProperty,
59     NULL, //GetPropertyNames,
60     NULL, //CallAsFunction,
61     NULL, //CallAsConstructor,
62     NULL, //HasInstance,
63     NULL //ConvertToType
64 };
65
66 JSStaticValue JSBluetoothManager::m_property[] = {
67     {
68         BLUETOOTH_MANAGER_DEVICE_MAJOR,
69         getReadOnlyProperty,
70         NULL,
71         kJSPropertyAttributeNone |
72         kJSPropertyAttributeReadOnly |
73         kJSPropertyAttributeDontDelete
74     },
75     {
76         BLUETOOTH_MANAGER_DEVICE_MINOR,
77         getReadOnlyProperty,
78         NULL,
79         kJSPropertyAttributeNone |
80         kJSPropertyAttributeReadOnly |
81         kJSPropertyAttributeDontDelete
82     },
83     {
84         BLUETOOTH_MANAGER_DEVICE_SERVICE,
85         getReadOnlyProperty,
86         NULL,
87         kJSPropertyAttributeNone |
88         kJSPropertyAttributeReadOnly |
89         kJSPropertyAttributeDontDelete
90     },
91     { 0, 0, 0, 0 }
92 };
93
94 JSStaticFunction JSBluetoothManager::m_function[] = {
95     {
96         BLUETOOTH_MANAGER_API_GET_DEFAULT_ADAPTER,
97         getDefaultAdapter,
98         kJSPropertyAttributeNone
99     },
100     {
101         BLUETOOTH_MANAGER_API_GET_LOW_ENERGY_ADAPTER,
102         getLEAdapter,
103         kJSPropertyAttributeNone
104     },
105     { 0, 0, 0 }
106 };
107
108 JSClassRef JSBluetoothManager::m_jsClassRef =
109     JSClassCreate(JSBluetoothManager::getClassInfo());
110
111 const JSClassRef JSBluetoothManager::getClassRef()
112 {
113     if (!m_jsClassRef) {
114         m_jsClassRef = JSClassCreate(&m_classInfo);
115     }
116     return m_jsClassRef;
117 }
118
119 const JSClassDefinition* JSBluetoothManager::getClassInfo()
120 {
121     return &m_classInfo;
122 }
123
124 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object)
125 {
126     LOGD("initialize");
127
128     if (!JSObjectGetPrivate(object)) {
129         JSBluetoothManagerPriv* priv = new JSBluetoothManagerPriv();
130
131         if(priv) {
132         // deviceMajor
133             priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MAJOR,
134             JSBluetoothClassDeviceMajor::createJSObject(context));
135
136             // deviceMinor
137             priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_MINOR,
138             JSBluetoothClassDeviceMinor::createJSObject(context));
139
140             // deviceService
141             priv->setProperty(context, BLUETOOTH_MANAGER_DEVICE_SERVICE,
142             JSBluetoothClassDeviceService::createJSObject(context));
143
144             if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
145                 LOGW("Failed to set private data");
146                 delete priv;
147             }
148             LOGD("Private creation ok");
149         }
150         else {
151             LOGW("Failed to create private data");
152         }
153     }
154     else {
155         LOGW("already has private data");
156     }
157 }
158
159 void JSBluetoothManager::finalize(JSObjectRef object)
160 {
161     JSBluetoothManagerPriv *priv =
162         static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
163     if (priv) {
164         JSObjectSetPrivate(object, NULL);
165         delete priv;
166     }
167 }
168
169 JSValueRef JSBluetoothManager::getReadOnlyProperty(JSContextRef context,
170         JSObjectRef object,
171         JSStringRef propertyName,
172         JSValueRef* exception)
173 {
174     JSBluetoothManagerPriv *priv =
175         static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
176     if(!priv) {
177         LOGW("There is no private data");
178         return NULL;
179     }
180
181     std::string name = JSUtil::JSStringToString(context, propertyName);
182     return priv->getProperty(context, propertyName);
183 }
184
185 JSValueRef JSBluetoothManager::getDefaultAdapter(JSContextRef context,
186         JSObjectRef object,
187         JSObjectRef thisObject,
188         size_t argumentCount,
189         const JSValueRef arguments[],
190         JSValueRef* exception)
191 {
192     JSBluetoothManagerPriv *priv =
193         static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
194
195     if (!priv)
196     {
197         TypeMismatchException err("Private Object is null");
198         return JSWebAPIErrorFactory::postException(context, exception, err);
199     }
200
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);
205
206     try {
207         BluetoothAdapter::getInstance()->copyAceCheckAccessFunction(priv);
208         JSObjectRef adapter = JSBluetoothAdapter::createJSObject(context);
209
210         TIME_TRACER_ITEM_END("getDefaultAdapter", 1);
211         return adapter;
212
213     } catch (const BasePlatformException &err) {
214         return JSWebAPIErrorFactory::postException(context, exception, err);
215     } catch (...) {
216         Common::UnknownException err(
217             "Unknown Error in BluetoothManager.getDefaultAdapter().");
218         return JSWebAPIErrorFactory::postException(context, exception, err);
219     }
220 }
221
222 JSValueRef JSBluetoothManager::getLEAdapter(JSContextRef context,
223         JSObjectRef object,
224         JSObjectRef thisObject,
225         size_t argumentCount,
226         const JSValueRef arguments[],
227         JSValueRef* exception)
228 {
229     JSBluetoothManagerPriv *priv =
230         static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(thisObject));
231
232     if (!priv)
233     {
234         TypeMismatchException err("Private Object is null");
235         return JSWebAPIErrorFactory::postException(context, exception, err);
236     }
237
238     TIZEN_CHECK_ACCESS(context, exception, priv,
239         BLUETOOTH_MANAGER_API_GET_LOW_ENERGY_ADAPTER);
240
241     try {
242         //make sure that BluetoothAdapter is already initialized
243         BluetoothAdapter::getInstance();
244
245         BluetoothLowEnergyAdapter::getInstance()->copyAceCheckAccessFunction(priv);
246         JSObjectRef adapter = JSBluetoothLowEnergyAdapter::createJSObject(context);
247         return adapter;
248     } catch (const BasePlatformException &err) {
249         return JSWebAPIErrorFactory::postException(context, exception, err);
250     } catch (...) {
251         Common::UnknownException err(
252             "Unknown Error in BluetoothManager.getDefaultAdapter().");
253         return JSWebAPIErrorFactory::postException(context, exception, err);
254     }
255 }
256
257
258 } // Bluetooth
259 } // DeviceAPI