2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <SecurityExceptions.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
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"
31 #include <TimeTracer.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSBluetoothHealthProfileHandler::m_classInfo = {
42 kJSClassAttributeNone,
43 "BluetoothHealthProfileHandler",
44 JSBluetoothProfileHandler::getClassRef(), //ParentClass
46 m_function, //StaticFunctions
47 initialize, //Initialize
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
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 },
66 JSClassRef JSBluetoothHealthProfileHandler::m_jsClassRef = JSClassCreate(JSBluetoothHealthProfileHandler::getClassInfo());
68 const JSClassRef JSBluetoothHealthProfileHandler::getClassRef()
71 m_jsClassRef = JSClassCreate(&m_classInfo);
76 const JSClassDefinition* JSBluetoothHealthProfileHandler::getClassInfo()
81 JSObjectRef JSBluetoothHealthProfileHandler::createJSObject(JSContextRef context)
83 return JSObjectMake(context, getClassRef(), NULL);
86 void JSBluetoothHealthProfileHandler::initialize(JSContextRef context, JSObjectRef object)
92 void JSBluetoothHealthProfileHandler::finalize(JSObjectRef object)
97 JSValueRef JSBluetoothHealthProfileHandler::registerSinkApplication(JSContextRef context,
99 JSObjectRef thisObject,
100 size_t argumentCount,
101 const JSValueRef arguments[],
102 JSValueRef* exception)
106 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
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);
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
121 MultiCallbackUserDataPtr callback(
122 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
124 LoggerW("Can't create MultiCallbackUserData");
127 callback->setCallback("success", successCallback);
128 callback->setCallback("error", errorCallback);
131 BluetoothHealthProfileHandler::getInstance()->registerSinkApp(dataType, name, callback);
132 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
134 return JSValueMakeUndefined(context);
135 } catch (const BasePlatformException &err) {
136 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
137 return JSWebAPIErrorFactory::postException(context, exception, err);
139 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
140 return JSWebAPIErrorFactory::postException(context, exception, err);
144 JSValueRef JSBluetoothHealthProfileHandler::connectToSource(JSContextRef context,
146 JSObjectRef thisObject,
147 size_t argumentCount,
148 const JSValueRef arguments[],
149 JSValueRef* exception)
153 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
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);
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");
168 JSObjectRef appObj = validator.toObject(1); // application
169 if(!JSValueIsObjectOfClass(context, appObj, JSBluetoothHealthApplication::getClassRef())) {
170 throw TypeMismatchException("application is not a BluetoothHealthApplication object");
173 JSObjectRef successCallback = validator.toFunction(2); // successCallback
174 JSObjectRef errorCallback = validator.toFunction(3, true); // errorCallback
177 MultiCallbackUserDataPtr callback(
178 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
180 LoggerW("Can't create MultiCallbackUserData");
183 callback->setCallback("success", successCallback);
184 callback->setCallback("error", errorCallback);
187 BluetoothHealthProfileHandler::getInstance()->connectToSource(remoteObj, appObj, callback);
188 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
190 return JSValueMakeUndefined(context);
191 } catch (const BasePlatformException &err) {
192 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
193 return JSWebAPIErrorFactory::postException(context, exception, err);
195 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
196 return JSWebAPIErrorFactory::postException(context, exception, err);