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