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