Remove hardcoded path for multiuser support
[platform/framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothSocket.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 "BluetoothSocket.h"
22 #include "BluetoothAdapter.h"
23 #include "plugin_config.h"
24 #include "JSBluetoothDevice.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 BluetoothSocket::BluetoothSocket(bt_socket_connection_s *connection)
35 {
36     mConnectedSocket = connection->socket_fd;
37     mUUID = std::string(connection->service_uuid);
38     mIsConnected = true;
39
40     bt_device_info_s *deviceInfo = NULL;
41     if(bt_adapter_get_bonded_device_info(connection->remote_address, &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
42         BluetoothDeviceSharedPtr device(new BluetoothDevice(deviceInfo));
43         mPeer = device;
44         bt_adapter_free_device_info(deviceInfo);
45     }    
46 }
47
48 BluetoothSocket::~BluetoothSocket()
49 {
50     if(mIsConnected) {
51         if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
52             LoggerW("Already disconnected");
53         }
54         
55         if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
56             LoggerW("Already done");
57         }
58     }
59 }
60
61 bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage)
62 {
63     MultiCallbackUserDataPtr callback(
64             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
65     if(!callback){
66         LoggerW("Can't create MultiCallbackUserData");
67         return false;
68     }    
69     callback->setCallback("onmessage", onMessage);
70     mOnMessage = callback;
71
72     return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage);    
73 }
74
75 bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose)
76 {
77     MultiCallbackUserDataPtr callback(
78             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
79     if(!callback){
80         LoggerW("Can't create MultiCallbackUserData");
81         return false;
82     }    
83     callback->setCallback("onclose", onClose);
84     mOnClose = callback;
85
86     return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose);    
87 }
88
89 bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError)
90 {
91     MultiCallbackUserDataPtr callback(
92             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
93     if(!callback){
94         LoggerW("Can't create MultiCallbackUserData");
95         return false;
96     }    
97     callback->setCallback("onerror", onError);
98     mOnError = callback;
99
100     return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError);    
101 }
102
103 std::string BluetoothSocket::getUUID() const
104 {
105     return mUUID;
106 }
107
108 void BluetoothSocket::setConnectionState(bool isConnected)
109 {
110     mIsConnected = isConnected;
111 }
112
113 bool BluetoothSocket::getConnectionState()
114 {
115     return mIsConnected;
116 }
117
118 JSValueRef BluetoothSocket::getPeer(JSContextRef context)
119 {
120     /*
121     JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER);
122     if(peer == NULL && mPeer != NULL) {
123         peer = JSBluetoothDevice::createJSObject(context, mPeer);
124         mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer);
125     }
126     
127     return peer;
128     */
129     return JSBluetoothDevice::createJSObject(context, mPeer);
130 }
131
132 MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const
133 {
134     return mOnMessage;
135 }
136
137 JSValueRef BluetoothSocket::getOnMessage(JSContextRef context)
138 {
139     JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE);
140     if(onMessage == NULL) {
141         LoggerD("onmessage is null");
142         return JSValueMakeNull(context);
143     }
144     
145     return onMessage;
146 }
147
148 MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const
149 {
150     return mOnClose;
151 }
152
153 JSValueRef BluetoothSocket::getOnClose(JSContextRef context)
154 {
155     JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE);
156     if(onClose == NULL) {
157         LoggerD("onclose is null");
158         return JSValueMakeNull(context);
159     }
160     
161     return onClose;
162 }
163
164 MultiCallbackUserDataPtr BluetoothSocket::getOnError() const
165 {
166     return mOnError;
167 }
168
169 JSValueRef BluetoothSocket::getOnError(JSContextRef context)
170 {
171     JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR);
172     if(onError == NULL) {
173         LoggerD("onerror is null");
174         return JSValueMakeNull(context);
175     }
176     
177     return onError;
178 }
179
180 unsigned long BluetoothSocket::writeData(char* data, unsigned long size)
181 {
182     unsigned long ret = 0;
183     TIME_TRACER_ITEM_BEGIN("writeData::bt_socket_send_data", 1);
184     if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) {
185         TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1);
186         LoggerD("bt_socket_send_data() succeeded");
187         ret = size;
188     }
189     else {
190         TIME_TRACER_ITEM_END("writeData::bt_socket_send_data", 1);
191         throw UnknownException("Unknown error");
192     }
193
194     //delete data;
195     return ret;
196 }
197
198 void BluetoothSocket::storeRecivedData(char *data, unsigned long size)
199 {
200     for(unsigned long i = 0; i < size; i++) {
201         mReceivedData.push_back(static_cast<signed char>(data[i]));
202     }
203 }
204
205 std::vector<signed char> BluetoothSocket::readData()
206 {
207     std::vector<signed char> result (mReceivedData);
208     mReceivedData.clear();
209
210     return result;
211 }
212
213 void BluetoothSocket::close()
214 {
215     if(!mIsConnected) {
216         LoggerD("Already disconnected");
217         return;
218     }
219
220     TIME_TRACER_ITEM_BEGIN("close::bt_socket_disconnect_rfcomm", 1);
221     if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
222         TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1);
223         LoggerE("bt_socket_disconnect_rfcomm() failed");
224         throw UnknownException("Unknown error");
225     }
226     TIME_TRACER_ITEM_END("close::bt_socket_disconnect_rfcomm", 1);
227
228     mIsConnected = false;
229 }
230
231 } // Bluetooth
232 } // DeviceAPI