merge with master
[platform/framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothCBManager.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
19 #include <bluetooth.h>
20 #include "BluetoothCBManager.h"
21 #include "BluetoothAdapterManager.h"
22 #include "BluetoothDeviceManager.h"
23 #include "BluetoothSocketManager.h"
24 #include "BluetoothServiceHandlerManager.h"
25 #include <dpl/singleton_impl.h>
26 #include <dpl/scoped_array.h>
27
28 #include <cassert>
29 #include <Commons/Exception.h>
30
31 using namespace DPL;
32 IMPLEMENT_SINGLETON(DeviceAPI::Bluetooth::BluetoothCBManager);
33
34 namespace DeviceAPI {
35 namespace Bluetooth {
36
37 namespace {
38
39 static void capi_callback_bt_state_changed(int result, bt_adapter_state_e adapter_state, void* user_data)
40 {
41         if( result == BT_ERROR_NONE && user_data != NULL)
42         {
43                 LogDebug("State Changed" << adapter_state);
44                 ((BluetoothAdapterManager*)user_data)->setPoweredManualAnswer();
45
46         }
47         else
48         {
49                 LogDebug("Error from platform");
50         }
51 }
52
53 static void capi_callback_bt_name_changed(char *device_name, void *user_data)
54 {
55         ((BluetoothAdapterManager*)user_data)->setAdapterNameManualAnswer(device_name);
56 }
57
58 static void capi_callback_bt_discovery_state_changed(int result, 
59         bt_adapter_device_discovery_state_e discovery_state, bt_adapter_device_discovery_info_s *discovery_info, void *user_data)
60 {
61         LogDebug("Discovery state" << discovery_state);
62         
63         if (discovery_state ==  BT_ADAPTER_DEVICE_DISCOVERY_FOUND && discovery_info != NULL)
64         {
65                 LogDebug("found device" << discovery_info->remote_name << " " << discovery_info->remote_address);
66         }
67         
68         LogDebug("Discovery state" << discovery_state);
69
70         if (user_data != NULL)
71         {
72                 ((BluetoothAdapterManager*)user_data)->discoveryDevicesCallbackEmit(result, discovery_state, discovery_info);
73         }
74         else
75         {
76                 LogDebug("Error from platform");
77         }
78
79 }
80
81 static void capi_callback_bt_device_bond_created(int result, bt_device_info_s *device_info, void *user_data)
82 {
83         if (user_data != NULL)
84         {
85                 ((BluetoothAdapterManager*)user_data)->createBondingManualAnswer(result, device_info);
86         }
87         else
88         {
89                 LogDebug("Error from platform");
90         }
91
92 }
93
94 static void capi_callback_bt_device_bond_destroyed(int result, char* remote_address, void *user_data)
95 {
96         if (user_data != NULL)
97         {
98                 ((BluetoothAdapterManager*)user_data)->destroyBondingManualAnswer(result);
99         }
100         else
101         {
102                 LogDebug("Error from platform");
103         }
104 }
105
106 static void capi_bt_device_service_searched(int result, bt_device_sdp_info_s* sdp_info, void* user_data)
107 {
108         if (user_data != NULL && sdp_info != NULL && sdp_info->remote_address != NULL)
109         {
110                 ((BluetoothCBManager*)user_data)->serviceSearchRouter(result, sdp_info, user_data);
111         }
112         else
113         {
114                 LogDebug("Error from platform");
115         }
116
117 }
118
119 static void capi_callback_bt_socket_connection_state_changed(int result, bt_socket_connection_state_e connection_state, 
120         bt_socket_connection_s *connection, void *user_data)
121 {
122         if (user_data != NULL && connection != NULL && connection->remote_address != NULL)
123         {
124                 ((BluetoothCBManager*)user_data)->connectionStateRouter(result, connection_state, connection);
125         }
126         else
127         {
128                 LogDebug("Error from platform");
129         }
130
131 }
132
133 static void capi_callback_bt_data_received(bt_socket_received_data_s *data, void *user_data)
134 {
135         if (user_data != NULL && data != NULL) 
136         {
137                 ((BluetoothCBManager*)(user_data))->socketDataReceivedRouter(data);
138         }
139         else 
140         {
141                 LogDebug("Error from platform");
142         }
143 }
144
145
146
147 }
148
149
150
151
152 BluetoothCBManager::BluetoothCBManager()
153 {
154         LogDebug("BluetoothCBManager singleton");
155
156 }
157
158 BluetoothCBManager::~BluetoothCBManager()
159 {
160         LogDebug("BluetoothCBManager singleton removed");
161         disconnectAllSockets();
162
163
164 }
165
166
167 void BluetoothCBManager::addAdapterCallback(adapterCallback cb, void *passData, std::string address)
168 {
169         std::vector<adapterCallback>::iterator it;
170
171         for (it = m_adapterCallback.begin(); it < m_adapterCallback.end(); ++it) 
172         {
173                 if (*it == cb)
174                         break;
175         }
176         
177         if (cb == ADAPTER_DESTROYBOND)
178         {
179                 LogDebug("ADAPTER_DESTROYBOND");
180         }
181
182         if (it == m_adapterCallback.end()) 
183         {
184
185                 switch (cb) 
186                 {
187                 case ADAPTER_SETPOWERED:
188                         if( bt_adapter_set_state_changed_cb(capi_callback_bt_state_changed, passData) != BT_ERROR_NONE )
189                         {
190                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "call back set error");
191                         }
192                         break;
193                 case ADAPTER_SETNAME:
194                         if (bt_adapter_set_name_changed_cb(capi_callback_bt_name_changed, passData) != BT_ERROR_NONE)
195                         {
196                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
197                         }
198                         break;
199                 case ADAPTER_DISCOVERY:
200                         if (bt_adapter_set_device_discovery_state_changed_cb(capi_callback_bt_discovery_state_changed, 
201                                 passData)!= BT_ERROR_NONE)
202                         {
203                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,  "callback set error");             
204                         }
205                         break;
206                 case ADAPTER_SERVICESEARCH:
207                         addServiceSearchCallback(address, passData, FROM_ADAPTER);
208                         break;
209                 case ADAPTER_CREATEBOND:
210                         if (bt_device_set_bond_created_cb(capi_callback_bt_device_bond_created, passData) != BT_ERROR_NONE )
211                         {
212                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
213                         }
214                         break;
215                 case ADAPTER_DESTROYBOND:
216                         if (bt_device_set_bond_destroyed_cb (capi_callback_bt_device_bond_destroyed, passData) != BT_ERROR_NONE )
217                         {
218                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "callback set error");
219                         }
220                         break;
221                 case ADAPTER_REGISTER:
222                         LogDebug("do nothing to set callback");
223                         break;                  
224                 }
225                 
226                 m_adapterCallback.push_back(cb);
227                 LogDebug("operation call back is set successfully");
228         }
229         else 
230         {
231                 LogDebug("same operation now, throws error");
232                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same operation now");
233         }
234
235         
236 }
237
238 void BluetoothCBManager::removeAdapterCallback(adapterCallback cb, std::string address)
239 {
240         std::vector<adapterCallback>::iterator it;
241
242         for (it = m_adapterCallback.begin(); it != m_adapterCallback.end(); ++it) 
243         {
244                 if (*it == cb) 
245                 {
246                         break;
247                 }
248         }       
249
250         if (it == m_adapterCallback.end())
251         {
252                 LogDebug("there is no such callback");
253         }
254         else 
255         {
256                 m_adapterCallback.erase(it);
257                 LogDebug("operation call back is unset successfully");
258
259                 switch (cb) 
260                 {
261                 case ADAPTER_SETPOWERED:
262                         bt_adapter_unset_state_changed_cb();
263                         break;
264                 case ADAPTER_SETNAME:
265                         bt_adapter_unset_name_changed_cb();
266                         break;
267                 case ADAPTER_DISCOVERY:
268                         bt_adapter_unset_device_discovery_state_changed_cb();
269                         break;
270                 case ADAPTER_SERVICESEARCH:
271                         removeServiceSearchCallback(address);
272                         break;
273                 case ADAPTER_CREATEBOND:
274                         bt_device_unset_bond_created_cb();
275                         break;
276                 case ADAPTER_DESTROYBOND:                       
277                         bt_device_unset_bond_destroyed_cb();
278                         break;
279                 case ADAPTER_REGISTER:
280                         LogDebug("do nothing to set callback");
281                         break;                  
282                         
283                 }
284                 
285         }
286 }
287
288 void BluetoothCBManager::registerSocket(std::string uuid, int socket)
289 {
290         std::map<std::string, int>::iterator it = m_serverSocketUuidMap.find(uuid);
291
292         if (it != m_serverSocketUuidMap.end()) 
293         {
294                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same uuid exist");
295         }
296   
297         m_serverSocketUuidMap[uuid] = socket;
298 }
299
300 void BluetoothCBManager::checkUUIDAvailable(std::string uuid)
301 {
302         std::map<std::string, int>::iterator it = m_serverSocketUuidMap.find(uuid);
303
304         if (it != m_serverSocketUuidMap.end()) 
305         {
306                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same uuid exist");
307         }
308 }
309
310 void BluetoothCBManager::unregisterSocket(std::string uuid)
311 {
312         std::map<std::string, int>::iterator it = m_serverSocketUuidMap.find(uuid);
313
314         if (it != m_serverSocketUuidMap.end()) 
315         {
316                 LogDebug("no such uuid");
317         }
318
319         m_serverSocketUuidMap.erase (uuid);
320         
321 }
322
323 void BluetoothCBManager::serviceSearchRouter(int result, bt_device_sdp_info_s* sdp_info, void* user_data)
324 {
325         std::map<std::string, void*>::iterator it = 
326                 m_serviceSearchMap.find(sdp_info->remote_address);
327         std::map<std::string, managerType>::iterator managerIt = 
328                 m_serviceSearchManagerMap.find(sdp_info->remote_address);
329
330         LogDebug(sdp_info->remote_address);
331         
332         if (it != m_serviceSearchMap.end() && managerIt !=  m_serviceSearchManagerMap.end())
333         {
334                 if (managerIt->second == FROM_DEVICE)
335                 {
336                         ((BluetoothDeviceManager*)(it->second))->serviceSearchManualAnswer(result);
337                 }
338                 else if(managerIt->second == FROM_ADAPTER)
339                 {
340                         ((BluetoothAdapterManager*)(it->second))->serviceSearchManualAnswer(result);
341                 }
342         }
343         else 
344         {
345                 LogDebug("manager is not registered");
346         }
347 }
348
349 void BluetoothCBManager::connectionStateRouter(int result, bt_socket_connection_state_e connection_state, 
350         bt_socket_connection_s *connection)
351 {
352         std::map<std::string, void*>::iterator it;
353         std::map<std::string, managerType>::iterator managerIt;
354
355         if (connection->service_uuid == NULL) 
356         {
357                 LogDebug("Error service uuid NULL");
358                 return;
359         }
360
361         LogDebug(connection->service_uuid);
362         
363         std::string uuid = connection->service_uuid;
364
365         if (connection->local_role == BT_SOCKET_SERVER)
366         {
367                 LogDebug("Route servicehandler");
368                 
369                 it = m_devicePrivateObjectMap.find(uuid);       
370                 managerIt = m_devicePrivateObjectManagerMap.find(uuid);
371
372                 if (it != m_devicePrivateObjectMap.end() && managerIt !=  m_devicePrivateObjectManagerMap.end())
373                 {
374                         if(managerIt->second == FROM_SERVICE)
375                         {
376                                 ((BluetoothServiceHandlerManager*)(it->second))->connectionStateChangedEmit(result, connection_state, connection);
377                         }               
378                 }
379                         
380         }
381
382         it = m_devicePrivateObjectMap.find(connection->remote_address);
383         managerIt = m_devicePrivateObjectManagerMap.find(connection->remote_address);
384                         
385         if (it != m_devicePrivateObjectMap.end() && managerIt !=  m_devicePrivateObjectManagerMap.end())
386         {
387                 if (managerIt->second == FROM_DEVICE)
388                 {
389                         LogDebug("Route device");
390                         ((BluetoothDeviceManager*)(it->second))->connectToServiceByUUIDManualAnswer(result, connection_state, connection);
391                 }
392                 else if(managerIt->second == FROM_SOCKET)
393                 {
394                         LogDebug("Route socket");
395                         ((BluetoothSocketManager*)(it->second))->connectionStateChangedEmit(result, connection_state, connection);
396                 }
397         }
398
399 }
400
401 void BluetoothCBManager::socketDataReceivedRouter(bt_socket_received_data_s *data)
402 {
403         std::map<int, void*>::iterator it = m_socketPrivateObjectMap.find(data->socket_fd);
404
405         if (it != m_socketPrivateObjectMap.end()) 
406         {
407                 ((BluetoothSocketManager*)(it->second))->setDataReceivedEmit(data);
408         }
409         else 
410         {
411                 LogDebug("manager is not registered");
412         }
413
414 }
415
416 void BluetoothCBManager::addServiceSearchCallback(std::string deviceAddress, void *data, managerType type)
417 {
418         if (m_serviceSearchMap.size() != 0)
419         {
420                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "same operation now");
421         }
422
423         std::map<std::string, void*>::iterator it = 
424                 m_serviceSearchMap.find(deviceAddress);
425
426         if (it != m_serviceSearchMap.end())
427         {
428                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "searvice search same device address");
429         }
430
431         if (bt_device_set_service_searched_cb(capi_bt_device_service_searched, this) != BT_ERROR_NONE)
432         {
433                 ThrowMsg(WrtDeviceApis::Commons::UnknownException,      "callback set error");          
434         }
435
436         LogDebug(deviceAddress);
437         
438         m_serviceSearchMap[deviceAddress] = data;
439         m_serviceSearchManagerMap[deviceAddress] = type;
440
441 }
442
443 void BluetoothCBManager::removeServiceSearchCallback(std::string deviceAddress)
444 {
445         std::map<std::string, void*>::iterator it = 
446                 m_serviceSearchMap.find(deviceAddress);
447
448         if (it == m_serviceSearchMap.end())
449         {
450                 LogDebug("there is no such address");
451                 return;
452         }
453
454
455         m_serviceSearchMap.erase(deviceAddress);
456         m_serviceSearchManagerMap.erase(deviceAddress);
457         
458         if (m_serviceSearchMap.size() == 0) 
459         {
460                 bt_device_unset_service_searched_cb();
461         }
462 }
463
464 void BluetoothCBManager::addConnectionStateCallback(std::string deviceAddress, void *data, managerType type)
465 {
466         std::map<std::string, void*>::iterator it = 
467                 m_devicePrivateObjectMap.find(deviceAddress);
468
469         if (it != m_devicePrivateObjectMap.end())
470         {
471                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "connecting to same device address");
472         }
473
474         if (m_devicePrivateObjectMap.size() == 0)
475         {
476                 if (bt_socket_set_connection_state_changed_cb(capi_callback_bt_socket_connection_state_changed, this) != BT_ERROR_NONE)
477                 {
478                         LogDebug("callback set error");
479                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "callback set error");
480                 }
481         }
482         LogDebug("connection callback for " << deviceAddress);
483         m_devicePrivateObjectMap[deviceAddress] = data;
484         m_devicePrivateObjectManagerMap[deviceAddress] = type;
485 }
486
487 void BluetoothCBManager::removeConnectionStateCallback(std::string deviceAddress, managerType type)
488 {
489         std::map<std::string, void*>::iterator it = 
490                 m_devicePrivateObjectMap.find(deviceAddress);
491
492         if (it == m_devicePrivateObjectMap.end())
493         {
494                 LogDebug("there is no such address");
495                 return;
496         }
497
498         if (type == m_devicePrivateObjectManagerMap[deviceAddress])
499         {
500                 m_devicePrivateObjectMap.erase(deviceAddress);
501                 m_devicePrivateObjectManagerMap.erase(deviceAddress);
502                 LogDebug("remove connection" << deviceAddress);
503         }
504         else 
505         {
506                 LogDebug("type different");
507         }
508
509         if (m_devicePrivateObjectMap.size() == 0) 
510         {
511                 LogDebug("remove connection state call back");
512                 bt_socket_unset_connection_state_changed_cb();          
513         }
514 }
515
516 void BluetoothCBManager::addSocketDataReceivedCallback(int socket, void *data)
517 {
518         std::map<int, void*>::iterator it = m_socketPrivateObjectMap.find(socket);
519
520         if (it != m_socketPrivateObjectMap.end())
521         {
522                 ThrowMsg(WrtDeviceApis::Commons::AlreadyInUseException, "there is same socket");
523         }
524
525         if (m_socketPrivateObjectMap.size() == 0)
526         {
527                 LogDebug("set data receive call back");
528                 if (bt_socket_set_data_received_cb(capi_callback_bt_data_received, this) != BT_ERROR_NONE)
529                 {
530                         LogDebug("callback set error");
531                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "callback set error");
532                 }
533         }
534         
535         m_socketPrivateObjectMap[socket] = data;
536         LogDebug(socket << "add socket (" << data << ")");
537
538 }
539
540 void BluetoothCBManager::removeSocketDataReceivedCallback(int socket)
541 {
542         std::map<int, void*>::iterator it = m_socketPrivateObjectMap.find(socket);
543         
544         if (it == m_socketPrivateObjectMap.end())
545         {
546                 LogDebug("there is no such socket");
547                 return;
548         }
549
550         LogDebug(socket << "remove socket " <<  m_socketPrivateObjectMap[socket] );
551         m_socketPrivateObjectMap.erase(socket);
552
553         if (m_socketPrivateObjectMap.size() == 0)
554         {
555                 LogDebug("remove data receive call back");
556                 bt_socket_unset_data_received_cb(); 
557         }
558 }
559
560 void BluetoothCBManager::clearAll()
561 {
562         m_adapterCallback.clear();
563         m_serverSocketUuidMap.clear();
564         m_devicePrivateObjectMap.clear();
565         m_devicePrivateObjectManagerMap.clear();
566         m_serviceSearchMap.clear();
567         m_serviceSearchManagerMap.clear();
568         m_socketPrivateObjectMap.clear();
569 }
570 void BluetoothCBManager::disconnectAllSockets()
571 {
572         // close connection socket
573         for (std::map<int, void*>::iterator it = m_socketPrivateObjectMap.begin(); it != m_socketPrivateObjectMap.end(); ++it)
574         {
575                 bt_socket_disconnect_rfcomm((*it).first);
576         }       
577
578         // close server socket
579         for (std::map<std::string, int>::iterator it = m_serverSocketUuidMap.begin(); it != m_serverSocketUuidMap.end(); ++it)
580         {
581                 bt_socket_destroy_rfcomm((*it).second);
582         }
583
584 }
585
586 }
587 }