Update change log and spec for wrt-plugins-tizen_0.4.70
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothHealthProfileHandler.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
24 #include "plugin_config.h"
25 #include "JSBluetoothHealthProfileHandler.h"
26 #include "JSBluetoothProfileHandler.h"
27 #include "JSBluetoothHealthApplication.h"
28 #include "JSBluetoothDevice.h"
29 #include "BluetoothHealthProfileHandler.h"
30
31 #include <TimeTracer.h>
32 #include <Logger.h>
33
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
36
37 namespace DeviceAPI {
38 namespace Bluetooth {
39
40 JSClassDefinition JSBluetoothHealthProfileHandler::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     "BluetoothHealthProfileHandler",
44     JSBluetoothProfileHandler::getClassRef(), //ParentClass
45     NULL, //StaticValues
46     m_function, //StaticFunctions
47     initialize, //Initialize
48     finalize, //Finalize
49     NULL, //HasProperty,
50     NULL, //GetProperty,
51     NULL, //SetProperty,
52     NULL, //DeleteProperty,
53     NULL, //GetPropertyNames,
54     NULL, //CallAsFunction,
55     NULL, //CallAsConstructor,
56     NULL, //HasInstance,
57     NULL //ConvertToType
58 };
59
60 JSStaticFunction JSBluetoothHealthProfileHandler::m_function[] = {
61     { BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION, registerSinkApplication, kJSPropertyAttributeNone },
62     { BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE, connectToSource, kJSPropertyAttributeNone },
63     { 0, 0, 0 }
64 };
65
66 JSClassRef JSBluetoothHealthProfileHandler::m_jsClassRef = JSClassCreate(JSBluetoothHealthProfileHandler::getClassInfo());
67
68 const JSClassRef JSBluetoothHealthProfileHandler::getClassRef()
69 {
70     if (!m_jsClassRef) {
71         m_jsClassRef = JSClassCreate(&m_classInfo);
72     }
73     return m_jsClassRef;
74 }
75
76 const JSClassDefinition* JSBluetoothHealthProfileHandler::getClassInfo()
77 {
78     return &m_classInfo;
79 }
80
81 JSObjectRef JSBluetoothHealthProfileHandler::createJSObject(JSContextRef context)
82 {
83     return JSObjectMake(context, getClassRef(), NULL);
84 }
85
86 void JSBluetoothHealthProfileHandler::initialize(JSContextRef context, JSObjectRef object)
87 {
88     // do nothing
89 }
90
91
92 void JSBluetoothHealthProfileHandler::finalize(JSObjectRef object)
93 {
94     // do nothing
95 }
96
97 JSValueRef JSBluetoothHealthProfileHandler::registerSinkApplication(JSContextRef context,
98     JSObjectRef object,
99     JSObjectRef thisObject,
100     size_t argumentCount,
101     const JSValueRef arguments[],
102     JSValueRef* exception)
103 {
104     LoggerD("Enter");
105     
106     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
107                 // Access Check
108         TIME_TRACER_ITEM_BEGIN("registerSinkApplication::ACE", 1);
109         TIZEN_CHECK_ACCESS(context, exception, BluetoothHealthProfileHandler::getInstance(), BLUETOOTH_HEALTH_PROFILE_HANDLER_API_REGISTER_SINK_APPLICATION);   
110         TIME_TRACER_ITEM_END("registerSinkApplication::ACE", 1);
111
112
113     try {
114         ArgumentValidator validator(context, argumentCount, arguments);        
115         unsigned short dataType = static_cast<unsigned short>(validator.toULong(0));  // dataType
116         std::string name = validator.toString(1);  // name
117         JSObjectRef successCallback = validator.toFunction(2);  // successCallback  
118         JSObjectRef errorCallback = validator.toFunction(3, true);  // errorCallback
119
120         // perform
121         MultiCallbackUserDataPtr callback(
122                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
123         if(!callback){
124             LoggerW("Can't create MultiCallbackUserData");
125         }
126         else {
127             callback->setCallback("success", successCallback);
128             callback->setCallback("error", errorCallback);
129         }      
130         
131         BluetoothHealthProfileHandler::getInstance()->registerSinkApp(dataType, name, callback);
132         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
133         
134         return JSValueMakeUndefined(context);
135     } catch (const BasePlatformException &err) {
136         TIME_TRACER_ITEM_END(__FUNCTION__, 1);    
137         return JSWebAPIErrorFactory::postException(context, exception, err);
138     } catch (...) {
139         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
140         return JSWebAPIErrorFactory::postException(context, exception, err);
141     }    
142 }
143
144 JSValueRef JSBluetoothHealthProfileHandler::connectToSource(JSContextRef context,
145     JSObjectRef object,
146     JSObjectRef thisObject,
147     size_t argumentCount,
148     const JSValueRef arguments[],
149     JSValueRef* exception)
150 {
151     LoggerD("Enter");
152     
153     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
154     
155     // Access Check
156     TIME_TRACER_ITEM_BEGIN("connectToSource::ACE", 1);
157         TIZEN_CHECK_ACCESS(context, exception, BluetoothHealthProfileHandler::getInstance(), BLUETOOTH_HEALTH_PROFILE_HANDLER_API_CONNECT_TO_SOURCE);   
158     TIME_TRACER_ITEM_END("connectToSource::ACE", 1);
159
160
161     try {
162         ArgumentValidator validator(context, argumentCount, arguments);
163         JSObjectRef remoteObj = validator.toObject(0);  // remoteDevice        
164         if(!JSValueIsObjectOfClass(context, remoteObj, JSBluetoothDevice::getClassRef())) {
165             throw TypeMismatchException("remoteDevice is not a BluetoothDevice object");
166         }
167
168         JSObjectRef appObj = validator.toObject(1);  // application
169         if(!JSValueIsObjectOfClass(context, appObj, JSBluetoothHealthApplication::getClassRef())) {
170             throw TypeMismatchException("application is not a BluetoothHealthApplication object");
171         }
172
173         JSObjectRef successCallback = validator.toFunction(2);  // successCallback  
174         JSObjectRef errorCallback = validator.toFunction(3, true);  // errorCallback
175
176         // perform
177         MultiCallbackUserDataPtr callback(
178                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
179         if(!callback){
180             LoggerW("Can't create MultiCallbackUserData");
181         }
182         else {
183             callback->setCallback("success", successCallback);
184             callback->setCallback("error", errorCallback);
185         }      
186
187         BluetoothHealthProfileHandler::getInstance()->connectToSource(remoteObj, appObj, callback);
188         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
189         
190         return JSValueMakeUndefined(context);    
191     } catch (const BasePlatformException &err) {
192         TIME_TRACER_ITEM_END(__FUNCTION__, 1);    
193         return JSWebAPIErrorFactory::postException(context, exception, err);
194     } catch (...) {
195         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
196         return JSWebAPIErrorFactory::postException(context, exception, err);
197     }    
198 }
199
200
201 } // Bluetooth
202 } // DeviceAPI