Update change log and spec for wrt-plugins-tizen_0.4.20
[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
28 using namespace DeviceAPI::Common;
29
30 namespace DeviceAPI {
31 namespace Bluetooth {
32
33 BluetoothSocket::BluetoothSocket(bt_socket_connection_s *connection)
34 {
35     LogDebug("Enter");
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     LogDebug("Enter");
51     if(mIsConnected) {
52         if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
53             LogWarning("Already disconnected");
54         }
55         
56         if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
57             LogWarning("Already done");
58         }
59     }
60 }
61
62 bool BluetoothSocket::setOnMessage(JSContextRef context, JSObjectRef onMessage)
63 {
64     LogDebug("Enter");
65     MultiCallbackUserDataPtr callback(
66             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
67     if(!callback){
68         LogWarning("Can't create MultiCallbackUserData");
69         return false;
70     }    
71     callback->setCallback("onmessage", onMessage);
72     mOnMessage = callback;
73
74     return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONMESSAGE, onMessage);    
75 }
76
77 bool BluetoothSocket::setOnClose(JSContextRef context, JSObjectRef onClose)
78 {
79     LogDebug("Enter");
80     MultiCallbackUserDataPtr callback(
81             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
82     if(!callback){
83         LogWarning("Can't create MultiCallbackUserData");
84         return false;
85     }    
86     callback->setCallback("onclose", onClose);
87     mOnClose = callback;
88
89     return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONCLOSE, onClose);    
90 }
91
92 bool BluetoothSocket::setOnError(JSContextRef context, JSObjectRef onError)
93 {
94     LogDebug("Enter");
95     MultiCallbackUserDataPtr callback(
96             new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
97     if(!callback){
98         LogWarning("Can't create MultiCallbackUserData");
99         return false;
100     }    
101     callback->setCallback("onerror", onError);
102     mOnError = callback;
103
104     return mLocalProperty.setProperty(context, BLUETOOTH_SOCKET_ONERROR, onError);    
105 }
106
107 std::string BluetoothSocket::getUUID() const
108 {
109     LogDebug("Enter");
110     return mUUID;
111 }
112
113 void BluetoothSocket::setConnectionState(bool isConnected)
114 {
115     LogDebug("Enter");
116     mIsConnected = isConnected;
117 }
118
119 bool BluetoothSocket::getConnectionState()
120 {
121     LogDebug("Enter");
122     return mIsConnected;
123 }
124
125 JSValueRef BluetoothSocket::getPeer(JSContextRef context)
126 {
127     LogDebug("Enter");
128     /*
129     JSValueRef peer = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_PEER);
130     if(peer == NULL && mPeer != NULL) {
131         peer = JSBluetoothDevice::createJSObject(context, mPeer);
132         mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, peer);
133     }
134     
135     return peer;
136     */
137     return JSBluetoothDevice::createJSObject(context, mPeer);
138 }
139
140 MultiCallbackUserDataPtr BluetoothSocket::getOnMessage() const
141 {
142     return mOnMessage;
143 }
144
145 JSValueRef BluetoothSocket::getOnMessage(JSContextRef context)
146 {
147     LogDebug("Enter");
148     JSValueRef onMessage = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONMESSAGE);
149     if(onMessage == NULL) {
150         LogDebug("onmessage is null");
151         return JSValueMakeNull(context);
152     }
153     
154     return onMessage;
155 }
156
157 MultiCallbackUserDataPtr BluetoothSocket::getOnClose() const
158 {
159     return mOnClose;
160 }
161
162 JSValueRef BluetoothSocket::getOnClose(JSContextRef context)
163 {
164     LogDebug("Enter");
165     
166     JSValueRef onClose = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONCLOSE);
167     if(onClose == NULL) {
168         LogDebug("onclose is null");
169         return JSValueMakeNull(context);
170     }
171     
172     return onClose;
173 }
174
175 MultiCallbackUserDataPtr BluetoothSocket::getOnError() const
176 {
177     return mOnError;
178 }
179
180 JSValueRef BluetoothSocket::getOnError(JSContextRef context)
181 {
182     LogDebug("Enter");
183
184     JSValueRef onError = mLocalProperty.getProperty(context, BLUETOOTH_SOCKET_ONERROR);
185     if(onError == NULL) {
186         LogDebug("onerror is null");
187         return JSValueMakeNull(context);
188     }
189     
190     return onError;
191 }
192
193 unsigned long BluetoothSocket::writeData(char* data, unsigned long size)
194 {
195     LogDebug("Enter");
196     
197     unsigned long ret = 0;
198     if(bt_socket_send_data(mConnectedSocket, data, static_cast<int>(size)) == BT_ERROR_NONE) {
199         LogDebug("bt_socket_send_data() succeeded");
200         ret = size;
201     }
202     else {
203         throw UnknownException("Unknown error");
204     }
205
206     delete data;
207     return ret;
208 }
209
210 void BluetoothSocket::storeRecivedData(char *data, unsigned long size)
211 {
212     LogDebug("Enter");
213
214     for(unsigned long i = 0; i < size; i++) {
215         mReceivedData.push_back(static_cast<signed char>(data[i]));
216     }
217 }
218
219 std::vector<signed char> BluetoothSocket::readData()
220 {
221     LogDebug("Enter");
222
223     std::vector<signed char> result (mReceivedData);
224     mReceivedData.clear();
225
226     return result;
227 }
228
229 void BluetoothSocket::close()
230 {
231     LogDebug("Enter");
232
233     if(!mIsConnected) {
234         LogDebug("Already disconnected");
235         return;
236     }
237     
238     if(bt_socket_disconnect_rfcomm(mConnectedSocket) != BT_ERROR_NONE) {
239         LogError("bt_socket_disconnect_rfcomm() failed");
240         return;
241     }    
242
243     mIsConnected = false;
244     if(!BluetoothAdapter::getInstance()->closeConnectedSocket(mConnectedSocket)) {
245         throw UnknownException("Unknown error");
246     }
247 }
248
249 } // Bluetooth
250 } // DeviceAPI