Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / JSBluetoothManager.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 <API/Bluetooth/BluetoothProperty.h>
26 #include "JSBluetoothManager.h"
27 #include "JSBluetoothClassDeviceMajor.h"
28 #include "JSBluetoothClassDeviceMinor.h"
29 #include "JSBluetoothClassDeviceService.h"
30
31
32 using namespace TizenApis::Api::Bluetooth;
33 using namespace WrtDeviceApis::Commons;
34 using namespace TizenApis::Commons;
35
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39
40 JSClassRef JSBluetoothManager::m_jsClassRef = NULL;
41
42 JSClassDefinition JSBluetoothManager::m_classInfo =
43 {
44         0,
45         kJSClassAttributeNone,
46         "BluetoothManager",
47         NULL,
48         m_properties,
49         NULL,
50         initialize,
51         finalize,
52         NULL, 
53         NULL, 
54         NULL, 
55         NULL, 
56         NULL, 
57         NULL,
58         NULL,
59         NULL,
60         NULL
61 };
62
63
64
65
66 JSStaticValue JSBluetoothManager::m_properties[] = 
67 {
68 /*      {PROTOCOL_TYPE_RFCOMM, getProperty, NULL, kJSPropertyAttributeReadOnly},
69         {PROTOCOL_TYPE_L2CAP, getProperty, NULL, kJSPropertyAttributeReadOnly},
70         {SECURITY_LEVEL_LOW, getProperty, NULL, kJSPropertyAttributeReadOnly},
71         {SECURITY_LEVEL_MEDIUM, getProperty, NULL, kJSPropertyAttributeReadOnly},
72         {SECURITY_LEVEL_HIGH, getProperty, NULL, kJSPropertyAttributeReadOnly},*/
73         {"DeviceMajor", getProperty, NULL, kJSPropertyAttributeReadOnly},
74         {"DeviceMinor", getProperty, NULL, kJSPropertyAttributeReadOnly},
75         {"DeviceService", getProperty, NULL, kJSPropertyAttributeReadOnly},     
76         {0, 0, 0, 0}
77 };
78
79
80
81 const JSClassRef JSBluetoothManager::getClassRef() 
82 {
83         if (!m_jsClassRef) 
84         {
85                 m_jsClassRef = JSClassCreate(&m_classInfo);
86         }
87         return m_jsClassRef;
88 }
89
90
91 void JSBluetoothManager::getPropertyNames(JSContextRef context,
92                                         JSObjectRef object,
93                                         JSPropertyNameAccumulatorRef propertyNames)
94 {
95 }
96
97
98 JSValueRef JSBluetoothManager::getProperty(JSContextRef context,
99                                         JSObjectRef object,
100                                         JSStringRef propertyName,
101                                         JSValueRef* exception)
102 {
103         LogDebug("OK"); 
104         
105         try 
106         {
107         /*
108                 if(JSStringIsEqualToUTF8CString(propertyName, PROTOCOL_TYPE_RFCOMM)) 
109                 {
110                         return converter.toJSValueRef(PROTOCOL_TYPE_RFCOMM_VALUE);
111                 }
112                 else if(JSStringIsEqualToUTF8CString(propertyName, PROTOCOL_TYPE_L2CAP)) 
113                 {
114                         return converter.toJSValueRef(PROTOCOL_TYPE_L2CAP_VALUE);
115                 }
116                 else if(JSStringIsEqualToUTF8CString(propertyName, SECURITY_LEVEL_LOW)) 
117                 {
118                         return converter.toJSValueRef(SECURITY_LEVEL_LOW_VALUE);
119                 }
120                 else if(JSStringIsEqualToUTF8CString(propertyName, SECURITY_LEVEL_MEDIUM)) 
121                 {
122                         return converter.toJSValueRef(SECURITY_LEVEL_MEDIUM_VALUE);
123                 }
124                 else if(JSStringIsEqualToUTF8CString(propertyName, SECURITY_LEVEL_HIGH)) 
125                 {
126                         return converter.toJSValueRef(SECURITY_LEVEL_HIGH_VALUE);
127                 }
128                 else*/ if(JSStringIsEqualToUTF8CString(propertyName, "DeviceMajor")) 
129                 {
130                         return JSBluetoothClassDeviceMajor::createJSObject(context);
131                 }
132                 else if(JSStringIsEqualToUTF8CString(propertyName, "DeviceMinor")) 
133                 {
134                         return JSBluetoothClassDeviceMinor::createJSObject(context);
135                 }
136                 else if(JSStringIsEqualToUTF8CString(propertyName, "DeviceService")) 
137                 {
138                         return JSBluetoothClassDeviceService::createJSObject(context);
139                 }
140
141         }
142         Catch (WrtDeviceApis::Commons::ConversionException)
143         {
144                 LogError("ConversionException");
145                 return JSTizenExceptionFactory::postException(context, exception,
146                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
147         }
148         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
149         {
150                 LogError("InvalidArgumentException");
151                 return JSTizenExceptionFactory::postException(context, exception,
152                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
153         }
154         Catch(WrtDeviceApis::Commons::Exception) 
155         {
156                 LogError("UnkownException");
157                 return JSTizenExceptionFactory::postException(context, exception,
158                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
159         }
160         return JSValueMakeUndefined(context);
161 }
162
163
164
165 const JSClassDefinition* JSBluetoothManager::getClassInfo() 
166 {
167         return &m_classInfo;
168 }
169
170
171 void JSBluetoothManager::initialize(JSContextRef context, JSObjectRef object) 
172 {
173         JSBluetoothManagerPriv *priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
174         LogDebug("JSBluetoothManager::initialize");
175
176         if (priv != NULL)
177         {
178                 LogError("already exist");
179         }
180         else
181         {
182                 priv = new JSBluetoothManagerPriv(context);
183
184                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
185                 {
186                         LogError("Object can't store private data.");
187                         delete priv;
188                 }
189         }
190 }
191
192 void JSBluetoothManager::finalize(JSObjectRef object) 
193 {
194         JSBluetoothManagerPriv* priv = static_cast<JSBluetoothManagerPriv*>(JSObjectGetPrivate(object));
195         LogDebug("JSBluetoothManager::Finalrize");
196
197         if (priv != NULL)
198         {
199                 JSObjectSetPrivate(object, NULL);
200                 LogDebug("Deleting BluetoothManager");
201                 delete priv;
202         }
203 }
204
205 bool JSBluetoothManager::hasInstance(JSContextRef context, JSObjectRef constructor,
206                 JSValueRef possibleInstance, JSValueRef* exception) 
207 {
208         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
209 }
210 }
211 }
212