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