Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / JSBluetoothClassDeviceService.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
18 #include <CommonsJavaScript/Converter.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include "JSBluetoothClassDeviceService.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
36 namespace TizenApis {
37 namespace Tizen1_0 {
38
39 JSClassDefinition JSBluetoothClassDeviceService::m_classInfo =
40 {
41         0,
42         kJSClassAttributeNone,
43         "BluetoothClassDeviceService",
44         NULL,
45         m_properties,
46         NULL,
47         initialize,
48         finalize,
49         NULL, 
50         NULL, 
51         NULL, 
52         NULL, 
53         NULL, 
54         NULL,
55         NULL,
56         hasInstance,
57         NULL
58 };
59
60         
61
62 JSStaticValue JSBluetoothClassDeviceService::m_properties[] = 
63 {
64         {"LIMITED_DISCOVERABILITY", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
65         {"POSITIONING", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
66         {"NETWORKING", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
67         {"RENDERING", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
68         {"CAPTURING", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
69         {"OBJECT_TRANSFER", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
70         {"AUDIO", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
71         {"TELEPHONY", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
72         {"INFORMATION", JSBluetoothClassDeviceService::getProperty, NULL, kJSPropertyAttributeReadOnly },
73         {0, 0, 0, 0}
74 };
75
76
77 const JSClassRef JSBluetoothClassDeviceService::getClassRef() 
78 {
79         if (!m_jsClassRef) 
80         {
81                 m_jsClassRef = JSClassCreate(&m_classInfo);
82         }
83         return m_jsClassRef;
84 }
85
86 const JSClassDefinition* JSBluetoothClassDeviceService::getClassInfo() 
87 {
88         return &m_classInfo;
89 }
90
91 JSClassRef JSBluetoothClassDeviceService::m_jsClassRef = JSClassCreate(JSBluetoothClassDeviceService::getClassInfo());
92
93 void JSBluetoothClassDeviceService::initialize(JSContextRef context, JSObjectRef object) 
94 {
95         LogDebug("JSBluetoothClassDeviceService::initialize ");
96         JSBluetoothClassDeviceServicePriv* priv = static_cast<JSBluetoothClassDeviceServicePriv*>(JSObjectGetPrivate(object));
97
98         if (priv == NULL)
99         {
100                 BluetoothClassPtr btClass(new BluetoothClass());
101                 priv = new JSBluetoothClassDeviceServicePriv( context, btClass);
102                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
103                 {
104                         LogError("Object can't store private data.");
105                         delete priv;
106                 }
107         }
108         else
109         {
110                 LogDebug("JSBluetoothClassDeviceService::already exist ");              
111         }
112 }
113
114
115 JSObjectRef JSBluetoothClassDeviceService::createJSObject(JSContextRef context)
116 {
117         BluetoothClassPtr btClass(new BluetoothClass());
118         JSBluetoothClassDeviceServicePriv* priv = new JSBluetoothClassDeviceServicePriv( context, btClass);
119         btClass->setReadOnly();
120
121         return JSObjectMake(context, getClassRef(), priv);
122 }
123
124 void JSBluetoothClassDeviceService::finalize(JSObjectRef object) 
125 {
126         JSBluetoothClassDeviceServicePriv* priv = static_cast<JSBluetoothClassDeviceServicePriv*>(JSObjectGetPrivate(object));
127
128         LogDebug("JSBluetoothClassDeviceService::Finalrize");
129
130         if (priv != NULL) 
131         {
132                 JSObjectSetPrivate(object, NULL);
133                 delete priv;
134         }
135 }
136
137 bool JSBluetoothClassDeviceService::hasInstance(JSContextRef context, JSObjectRef constructor,
138                 JSValueRef possibleInstance, JSValueRef* exception) 
139 {
140         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
141 }
142
143 JSValueRef JSBluetoothClassDeviceService::getProperty(JSContextRef context,
144                                                                                 JSObjectRef object,
145                                                                                 JSStringRef propertyName,
146                                                                                 JSValueRef* exception)
147 {
148         LogDebug("OK"); 
149
150         Converter converter(context);
151         JSBluetoothClassDeviceServicePriv* priv = static_cast<JSBluetoothClassDeviceServicePriv*>(JSObjectGetPrivate(object));  
152         
153         try 
154         {
155                 if (priv == NULL)
156                 {
157                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
158                 }
159
160                 BluetoothClassPtr btClass(priv->getObject());
161                 std::string key = converter.toString(propertyName);
162                 int value = 0;
163                 
164                 if (btClass->find(key, value) == false)
165                 {
166                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "can not find property" << key);
167                 }
168                 return converter.toJSValueRef(value);
169         }
170         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
171         {
172                 LogError("InvalidArgumentException");
173                 return JSTizenExceptionFactory::postException(context, exception, 
174                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");     
175         }       
176         Catch (WrtDeviceApis::Commons::SecurityException) 
177         {
178                 LogError("permission denied error");
179                 return JSTizenExceptionFactory::postException(context, exception, 
180                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
181
182         }
183         Catch (WrtDeviceApis::Commons::Exception) 
184         {
185                 LogError("UnkownException");
186                 return JSTizenExceptionFactory::postException(context, exception, 
187                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
188         }
189
190         return JSValueMakeNull(context);
191 }
192 }
193 }
194
195