Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / JSBluetoothClass.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. 
15  */
16
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"
27
28
29 using namespace std;
30 using namespace DPL;
31 using namespace WrtDeviceApis;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace TizenApis::Commons;
34
35 namespace TizenApis {
36 namespace Tizen1_0 {
37
38 JSClassDefinition JSBluetoothClass::m_classInfo =
39 {
40         0,
41         kJSClassAttributeNone,
42         "BluetoothClass",
43         NULL,
44         m_properties,
45         m_function,
46         initialize,
47         finalize,
48         NULL, 
49         NULL, 
50         NULL, 
51         NULL, 
52         NULL, 
53         NULL,
54         NULL,
55         hasInstance,
56         NULL
57 };
58
59         
60 JSStaticFunction JSBluetoothClass::m_function[] =
61 {
62         { "hasService", JSBluetoothClass::hasService, kJSPropertyAttributeNone },
63
64         { 0, 0, 0 }
65 };
66
67
68 JSStaticValue JSBluetoothClass::m_properties[] = 
69 {
70         {"major", JSBluetoothClass::getProperty, NULL, kJSPropertyAttributeReadOnly },
71         {"minor", JSBluetoothClass::getProperty, NULL, kJSPropertyAttributeReadOnly },  
72         {"services", JSBluetoothClass::getProperty, NULL, kJSPropertyAttributeReadOnly },       
73         {0, 0, 0, 0}
74 };
75
76
77 const JSClassRef JSBluetoothClass::getClassRef() 
78 {
79         if (!m_jsClassRef) {
80                 m_jsClassRef = JSClassCreate(&m_classInfo);
81         }
82         return m_jsClassRef;
83 }
84
85 const JSClassDefinition* JSBluetoothClass::getClassInfo() 
86 {
87         return &m_classInfo;
88 }
89
90 JSClassRef JSBluetoothClass::m_jsClassRef = JSClassCreate(JSBluetoothClass::getClassInfo());
91
92 void JSBluetoothClass::initialize(JSContextRef context, JSObjectRef object) 
93 {
94         LogDebug("JSBluetoothClass::initialize ");
95         JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(object));
96
97         if (priv == NULL)
98         {
99                 BluetoothClassPtr btClass(new BluetoothClass());
100                 priv = new JSBluetoothClassPriv( context, btClass);
101                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
102                 {
103                         LogError("Object can't store private data.");
104                         delete priv;
105                 }
106         }
107         else
108         {
109                 LogDebug("JSBluetoothClass::already exist ");           
110         }
111 }
112
113
114 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context)
115 {
116         BluetoothClassPtr btClass(new BluetoothClass());
117         JSBluetoothClassPriv* priv = new JSBluetoothClassPriv( context, btClass);
118         btClass->setReadOnly();
119
120         return JSObjectMake(context, getClassRef(), priv);
121 }
122
123 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, int device)
124 {
125         BluetoothClassPtr btClass(new BluetoothClass());
126         JSBluetoothClassPriv* priv = new JSBluetoothClassPriv( context, btClass);
127
128         btClass->fromInt(device);
129         btClass->setReadOnly();
130         
131         return JSObjectMake(context, getClassRef(), priv);
132
133 }
134
135 JSObjectRef JSBluetoothClass::createJSObject(JSContextRef context, BluetoothDeviceDataClass devClass)
136 {
137         BluetoothClassPtr btClass(new BluetoothClass());
138         JSBluetoothClassPriv* priv = new JSBluetoothClassPriv( context, btClass);
139
140         btClass->setMajor(devClass.major);
141         btClass->setMinor(devClass.minor);
142         btClass->setServices(devClass.majorServiceMask);
143         btClass->setReadOnly();
144         
145         LogDebug("Major:" << std::hex << devClass.major << "Minor:" << std::hex << devClass.minor << "ServiceClass:" << devClass.majorServiceMask);
146         
147         return JSObjectMake(context, getClassRef(), priv);
148 }
149
150
151 void JSBluetoothClass::finalize(JSObjectRef object) 
152 {
153         JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(object));
154
155         LogDebug("JSBluetoothClass::Finalrize");
156
157         if (priv != NULL) 
158         {
159                 JSObjectSetPrivate(object, NULL);
160                 delete priv;
161         }
162 }
163
164 bool JSBluetoothClass::hasInstance(JSContextRef context, JSObjectRef constructor,
165                 JSValueRef possibleInstance, JSValueRef* exception) 
166 {
167         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
168 }
169
170 JSValueRef JSBluetoothClass::getProperty(JSContextRef context,
171                                                                                 JSObjectRef object,
172                                                                                 JSStringRef propertyName,
173                                                                                 JSValueRef* exception)
174 {
175         LogDebug("OK"); 
176
177         Converter converter(context);
178         JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(object));    
179         
180         try 
181         {
182                 if (priv == NULL)
183                 {
184                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
185                 }
186
187                 BluetoothClassPtr btClass(priv->getObject());
188                 std::string key = converter.toString(propertyName);
189                 int value = 0;
190                 
191                 if (key == "major") 
192                 {
193                         value = btClass->getMajor();
194                         return converter.toJSValueRef(value);
195                 }
196                 else if (key == "minor")
197                 {
198                         value = btClass->getMinor();
199                         return converter.toJSValueRef(value);
200                 }
201                 else if (key == "services")
202                 {
203                         std::vector<int> service = btClass->getServices();
204                         JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
205
206                         if (!result) 
207                         {
208                                 ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Could not create array object.");
209                         }
210
211                         for (std::size_t i = 0; i < service.size(); ++i) 
212                         {
213                                 JSValueRef value = JSValueMakeNumber(context, service[i]);
214
215                                 if (!JSSetArrayElement(context, result, i, value)) 
216                                 {
217                                         ThrowMsg(WrtDeviceApis::Commons::ConversionException, "Could not fill array.");
218                                 }
219                         }
220
221                         return result;
222                 }
223                 else
224                 {
225                         btClass->find(key, value);
226                         return converter.toJSValueRef(value);
227                 }
228         }
229         Catch (WrtDeviceApis::Commons::Exception) 
230         {
231                 LogError("Unkwon Exception");
232         }       
233         return JSValueMakeNull(context);
234 }
235
236 JSValueRef JSBluetoothClass::hasService(JSContextRef context, JSObjectRef object,
237                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
238                 JSValueRef* exception) 
239
240 {
241         JSBluetoothClassPriv* priv = static_cast<JSBluetoothClassPriv*>(JSObjectGetPrivate(thisObject));        
242         
243         try 
244         {
245                 if (priv == NULL)
246                 {
247                         LogError("priv null");
248
249                         Throw(WrtDeviceApis::Commons::UnknownException);
250                 }
251
252                 if (argumentCount != 1 || JSValueIsNull(context, arguments[0]) == true 
253                         || JSValueIsNumber(context, arguments[0]) == false)
254                 {
255                         LogError("argument fail");
256                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
257                 }
258
259                 
260                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
261                                 priv->getContext(),
262                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_BLUETOOTHCLASS_HAS_SERVICE]);
263
264                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
265
266                 BluetoothClassPtr btClass(priv->getObject());
267                 std::vector<int> service = btClass->getServices();
268                 Converter converter(context);
269                 int queryService = converter.toInt(arguments[0]);
270                 int index = 0;
271
272                 for (index = 0; index < (int)service.size(); index++)
273                 {
274                         if (service[index] == queryService)
275                         {
276                                 return converter.toJSValueRef(true);
277                         }
278                 }
279                 return converter.toJSValueRef(false);
280                 
281         }
282         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
283         {
284                 LogError("InvalidArgumentException");
285                 return JSTizenExceptionFactory::postException(context, exception, 
286                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");     
287         }       
288         Catch (WrtDeviceApis::Commons::SecurityException) 
289         {
290                 LogError("permission denied error");
291                 return JSTizenExceptionFactory::postException(context, exception, 
292                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
293
294         }
295         Catch (WrtDeviceApis::Commons::Exception) 
296         {
297                 LogError("UnkownException");
298                 return JSTizenExceptionFactory::postException(context, exception, 
299                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
300         }
301         return JSValueMakeNull(context);
302 }
303 }
304 }
305