Update change log and spec for wrt-plugins-tizen_0.4.44
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothServiceHandler.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 <GlobalContextManager.h>
19 #include <PlatformException.h>
20
21 #include "BluetoothServiceHandler.h"
22 #include "BluetoothAdapter.h"
23 #include "BluetoothCallbackUtil.h"
24 #include "plugin_config.h"
25
26 #include <Logger.h>
27 #include <TimeTracer.h>
28
29 using namespace DeviceAPI::Common;
30
31 namespace DeviceAPI {
32 namespace Bluetooth {
33
34 BluetoothServiceHandler::BluetoothServiceHandler(std::string uuid, std::string name, int registeredSocket)
35 {
36     mUUID = uuid;
37     mName = name;
38     mRegisteredSocket = registeredSocket;
39     mIsRegistered = true;
40     mIsConnected = false;
41 }
42
43 BluetoothServiceHandler::~BluetoothServiceHandler()
44 {
45     if(mIsRegistered) {        
46         BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
47         if(bt_socket_destroy_rfcomm(mRegisteredSocket) != BT_ERROR_NONE) {
48             LoggerW("Already destroyed");
49         }
50     }
51 }
52
53 bool BluetoothServiceHandler::setOnConnect(JSContextRef context, JSObjectRef onConnect)
54 {
55     MultiCallbackUserDataPtr callback(
56             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
57     if(!callback){
58         LoggerW("Can't create MultiCallbackUserData");
59         return false;
60     }    
61     callback->setCallback("onconnect", onConnect);
62     mOnConnect = callback;
63
64     return mLocalProperty.setProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT, onConnect);
65 }
66
67 std::string BluetoothServiceHandler::getUUID() const
68 {
69     return mUUID;
70 }
71
72 std::string BluetoothServiceHandler::getName() const
73 {
74     return mName;
75 }
76
77 int BluetoothServiceHandler::getRegisteredSocket() const
78 {
79     return mRegisteredSocket;
80 }
81
82 MultiCallbackUserDataPtr BluetoothServiceHandler::getOnConnect() const
83 {
84     return mOnConnect;
85 }
86
87 JSValueRef BluetoothServiceHandler::getOnConnect(JSContextRef context)
88 {
89     JSValueRef onConnect = mLocalProperty.getProperty(context, BLUETOOTH_SERVICE_HANDLER_ONCONNECT);
90     if(onConnect == NULL) {
91         LoggerD("onconnect is null");
92         return JSValueMakeNull(context);
93     }
94     
95     return onConnect;
96 }
97
98 void BluetoothServiceHandler::setConnectionState(bool isConnected)
99 {
100     mIsConnected = isConnected;
101 }
102
103 bool BluetoothServiceHandler::getConnectionState() const
104 {
105     return mIsConnected;
106 }
107
108 void BluetoothServiceHandler::unregister(MultiCallbackUserDataPtr userData)
109 {
110     if(BluetoothAdapter::getInstance()->getPowered() == true) {
111         TIME_TRACER_ITEM_BEGIN("unregister::bt_socket_destroy_rfcomm", 1);
112         if(bt_socket_destroy_rfcomm(mRegisteredSocket) == BT_ERROR_NONE) {
113             TIME_TRACER_ITEM_END("unregister::bt_socket_destroy_rfcomm", 1);
114             mIsRegistered = false;
115             BluetoothAdapter::getInstance()->unregisterUUID(mUUID);
116             BluetoothCallbackUtil::syncToAsyncSuccessCallback(userData);
117         }
118         else {
119             UnknownException *error = new UnknownException("Unknown exception");
120             BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);
121         }
122     } else {   // Not enabled
123         ServiceNotAvailableException *error = new ServiceNotAvailableException("Bluetooth device is turned off");
124         BluetoothCallbackUtil::syncToAsyncErrorCallback(userData, error);
125     }    
126 }
127
128 } // Bluetooth
129 } // DeviceAPI