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