update the header for Doxygen
[platform/framework/native/bluetooth.git] / src / FNetBt_BluetoothManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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 // @file        FNetBt_BluetoothManagerImpl.cpp
18 // @brief       This is the implementation file for the _BluetoothManagerImpl class.
19 //
20
21 #include <FBaseByteBuffer.h>
22 #include <FNetBtBluetoothTypes.h>
23 #include <FNetBtBluetoothDevice.h>
24 #include <FNetBtBluetoothManager.h>
25 #include <FNetBtIBluetoothManagerEventListener.h>
26 #include <FNetBtIBluetoothDeviceEventListener.h>
27 #include <FBaseSysLog.h>
28 #include "FNetBt_BluetoothGapSystemAdapter.h"
29 #include "FNetBt_BluetoothDeviceImpl.h"
30 #include "FNetBt_BluetoothManagerImpl.h"
31 #include "FNetBt_BluetoothIpcProxy.h"
32 #include "FNetBt_BluetoothManagerEvent.h"
33 #include "FNetBt_BluetoothManagerEventArg.h"
34 #include "FNetBt_BluetoothDeviceEvent.h"
35 #include "FNetBt_BluetoothDeviceEventArg.h"
36 #include "FNetBt_BluetoothConnectionEvent.h"
37 #include "FNetBt_BluetoothConnectionEventArg.h"
38
39 using namespace std;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Runtime;
42 using namespace Tizen::Base::Collection;
43
44 namespace Tizen { namespace Net { namespace Bluetooth
45 {
46
47 _BluetoothManagerImpl::_BluetoothManagerImpl(void)
48         : __pIpcProxy(null)
49         , __pGapAdapter(null)
50         , __pMgrEvent(null)
51         , __pDevEvent(null)
52         , __pConEvent(null)
53         , __pDevEvtListener(null)
54         , __pConEvtListener(null)
55         , __pLocalDevice(null)
56         , __pairedDeviceList()
57         , __pairedDeviceMap()
58         , __stateMutex()
59         , __pairedDevMapMutex()
60         , __currentState(_BT_MGR_STATE_DEACTIVATED)
61         , __pairingState(_BT_PAIRING_STATE_NONE)
62         , __pairingTargetAddress()
63         , __discoveredDeviceList()
64 {
65 }
66
67 _BluetoothManagerImpl::~_BluetoothManagerImpl(void)
68 {
69         if (__pGapAdapter != null)
70         {
71                 if ((__currentState == _BT_MGR_STATE_DISCOVERY_REQUESTED)
72                         || (__currentState == _BT_MGR_STATE_ON_DISCOVERY))
73                 {
74                         SysLog(NID_NET_BT, "Cancel the device discovery for resource clean.");
75                         (void) CancelDiscovery();
76                 }
77
78                 __pGapAdapter->UnregisterManagerEventListener(*this);
79                 __pGapAdapter->UnregisterDeviceEventListener(*this);
80         }
81 }
82
83 result
84 _BluetoothManagerImpl::Construct(IBluetoothManagerEventListener& listener)
85 {
86         result r = E_SUCCESS;
87         unique_ptr<_BluetoothManagerEvent> pMgrEvent;
88         unique_ptr<_BluetoothDeviceEvent> pDevEvent;
89         unique_ptr<_BluetoothConnectionEvent> pConEvent;
90
91         r = __stateMutex.Create();
92         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to create the state mutex.");
93
94         r = __pairedDevMapMutex.Create();
95         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to create the map mutex.");
96
97         r = __pairingTargetAddress.Construct(_BT_ADDRESS_LENGTH);
98         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to construct the address for pairing.");
99
100         __pIpcProxy = _BluetoothIpcProxy::GetInstance();
101         SysTryReturnResult(NID_NET_BT, __pIpcProxy != null, E_SYSTEM, "Failed to get an instance of _BluetoothIpcProxy.");
102
103         __pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
104         SysTryReturnResult(NID_NET_BT, __pGapAdapter != null, E_SYSTEM, "Failed to get an instance of _BluetoothGapSystemAdapter.");
105
106         pMgrEvent.reset(new (std::nothrow) _BluetoothManagerEvent());
107         SysTryReturnResult(NID_NET_BT, pMgrEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
108         r = pMgrEvent->Construct();
109         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to construct the event.");
110
111         pDevEvent.reset(new (std::nothrow) _BluetoothDeviceEvent());
112         SysTryReturnResult(NID_NET_BT, pDevEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
113         r = pDevEvent->Construct();
114         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to construct the event.");
115
116         pConEvent.reset(new (std::nothrow) _BluetoothConnectionEvent());
117         SysTryReturnResult(NID_NET_BT, pConEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
118         r = pConEvent->Construct();
119         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to construct the event.");
120
121         // add the IBluetoothManagerEventListener instance to a new created _BluetoothManagerEvent.
122         r = pMgrEvent->AddListener(listener, true);
123         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM,
124                         "Failed to add the application listener for BluetoothManager event");
125
126         // initialize the paired device list which is used for old version APIs
127         __pairedDeviceList.Construct();
128         r = RefreshPairedDeviceList();
129         SysTryReturn(NID_NET_BT, r == E_SUCCESS, r, r, "[%s] Failed to make the paired device list.", GetErrorMessage(r));
130
131         // initialize the paired device map which is used since 2.0
132         __pairedDeviceMap.Construct();
133         r = RefreshPairedDeviceMap();
134         SysTryReturn(NID_NET_BT, r == E_SUCCESS, r, r, "[%s] Failed to make the paired device map.", GetErrorMessage(r));
135
136         // registers this listener to the manager event of the system adapter for activation/deactivation
137         r = __pGapAdapter->RegisterManagerEventListener(*this, false);
138         SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, 
139                            "Failed to register the manager event listener to _BluetoothSystemAdapter.");
140
141         // registers this listener to the device event of the system adapter for device/service discovery
142         r = __pGapAdapter->RegisterDeviceEventListener(*this);
143         if (r != E_SUCCESS)
144         {
145                 __pGapAdapter->UnregisterManagerEventListener(*this);
146                 SysLogException(NID_NET_BT, E_SYSTEM,
147                                 "[E_SYSTEM] Failed to register the device event listener to _BluetoothSystemAdapter.");
148                 return E_SYSTEM;
149         }
150
151         // checks the initial state of local Bluetooth
152         __stateMutex.Acquire();
153
154         if (__pGapAdapter->IsActivated() == true)
155         {
156                 if (__pGapAdapter->IsDiscoveryInProgress() == true)
157                 {
158                         __currentState = _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM;
159                 }
160                 else
161                 {
162                         __currentState = _BT_MGR_STATE_ACTIVATED;
163                 }
164         }
165
166         __stateMutex.Release();
167
168         __pMgrEvent = move(pMgrEvent);
169         __pDevEvent = move(pDevEvent);
170         __pConEvent = move(pConEvent);
171
172         return E_SUCCESS;
173 }
174
175 result
176 _BluetoothManagerImpl::Activate(void)
177 {
178         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
179
180         result r = E_SUCCESS;
181
182         __stateMutex.Acquire();
183
184         switch (__currentState)
185         {
186         case _BT_MGR_STATE_DEACTIVATED:
187                 r = __pGapAdapter->Activate();
188                 if (r == E_SUCCESS)
189                 {
190                         __currentState = _BT_MGR_STATE_ACTIVATING;
191                 }
192                 break;
193
194         case _BT_MGR_STATE_ACTIVATING:
195                 r = E_IN_PROGRESS;
196                 break;
197
198         default:
199                 r = E_INVALID_OPERATION;
200                 break;
201         }
202         __stateMutex.Release();
203
204         if (r != E_SUCCESS)
205         {
206                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on activating Bluetooth.", GetErrorMessage(r));
207         }
208
209         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
210
211         return r;
212 }
213
214 result
215 _BluetoothManagerImpl::Deactivate(void)
216 {
217         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
218
219         result r = E_SUCCESS;
220
221         __stateMutex.Acquire();
222
223         switch (__currentState)
224         {
225         case _BT_MGR_STATE_DEACTIVATED:
226         case _BT_MGR_STATE_ACTIVATING:
227                 r = E_INVALID_OPERATION;
228                 break;
229
230         case _BT_MGR_STATE_DEACTIVATING:
231                 r = E_IN_PROGRESS;
232                 break;
233
234         default:
235                 r = __pGapAdapter->Deactivate();
236                 if (r == E_SUCCESS)
237                 {
238                         __currentState = _BT_MGR_STATE_DEACTIVATING;
239                 }
240                 break;
241         }
242         __stateMutex.Release();
243
244         if (r != E_SUCCESS)
245         {
246                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on deactivating Bluetooth.", GetErrorMessage(r));
247         }
248
249         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
250
251         return r;
252 }
253
254 bool
255 _BluetoothManagerImpl::IsAvailable(BluetoothConnectionType type) const
256 {
257         return __pGapAdapter->IsAvailable(type);
258 }
259
260 const BluetoothDevice*
261 _BluetoothManagerImpl::GetLocalDevice(void)
262 {
263         ByteBuffer localAddr;
264         String localAddrStr;
265         String localName;
266         result r = E_SUCCESS;
267         _BluetoothDeviceImpl* pImpl = null;
268
269         // Gets the Bluetooth address.
270         localAddrStr = __pGapAdapter->GetLocalDeviceAddress();
271         localAddr.Construct(_BT_ADDRESS_LENGTH);
272         r = _BluetoothDeviceImpl::GetAddressByteBuffer(localAddrStr, L":", localAddr);
273         SysTryReturn(NID_NET_BT, r == E_SUCCESS, null, E_SYSTEM, "[E_SYSTEM] Getting the Bluetooth address has failed.");
274
275         // Gets the Bluetooth local name.
276         localName = GetLocalDeviceName();
277
278         if (__pLocalDevice == null)
279         {
280                 __pLocalDevice = new (std::nothrow) BluetoothDevice();
281                 if (__pLocalDevice == null)
282                 {
283                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient on creation of BluetoothDevice.");
284                         SetLastResult(E_OUT_OF_MEMORY);
285                         return null;
286                 }
287
288                 pImpl = _BluetoothDeviceImpl::GetInstance(*__pLocalDevice);
289                 pImpl->SetAddress(localAddr);
290                 pImpl->SetName(localName);
291                 pImpl->SetMajorDeviceClassType(BT_COD_MAJ_DEV_CLS_PHONE);
292                 pImpl->SetMinorDeviceClassType(BT_COD_MIN_DEV_CLS_CELLULAR);
293                 pImpl->SetServiceClassList(BT_COD_SVC_UNKNOWN);
294                 pImpl->SetServiceList(BT_SVC_NONE);
295         }
296         else
297         {
298                 r = _BluetoothDeviceImpl::GetInstance(*__pLocalDevice)->SetAddress(localAddr);
299                 if (r == E_SUCCESS)
300                 {
301                         _BluetoothDeviceImpl::GetInstance(*__pLocalDevice)->SetName(localName);
302                 }
303                 else
304                 {
305                         SysLogException(NID_NET_BT, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
306                         SetLastResult(E_SYSTEM);
307                         return null;
308                 }
309         }
310
311         SetLastResult(E_SUCCESS);
312
313         return __pLocalDevice;
314 }
315
316 Tizen::Base::String
317 _BluetoothManagerImpl::GetLocalDeviceAddress(void) const
318 {
319         return __pGapAdapter->GetLocalDeviceAddress();
320 }
321
322 Tizen::Base::String
323 _BluetoothManagerImpl::GetLocalDeviceName(void) const
324 {
325         result r = E_SUCCESS;
326         String localName;
327
328         // Get the local name from osp-connectivity-service.
329         r = __pIpcProxy->GetLocalDeviceName(localName);
330         SysTryLog(NID_NET_BT, r == E_SUCCESS, "Getting the local name through osp-connectivity-service has failed.");
331         // Ignores other cases
332
333         return localName;
334 }
335
336 BluetoothDeviceStateType
337 _BluetoothManagerImpl::GetLocalDeviceState() const
338 {
339         if (__pGapAdapter->IsActivated() == true)
340         {
341                 if (__pGapAdapter->GetDiscoverableMode() == BT_DISC_MODE_DISCOVERABLE)
342                 {
343                         return BT_DEVICE_STATE_DISCOVERABLE;
344                 }
345
346                 return BT_DEVICE_STATE_NOT_DISCOVERABLE;
347         }
348
349         return BT_DEVICE_STATE_OFF;
350 }
351
352 int
353 _BluetoothManagerImpl::GetRemainingTimeAsDiscoverable(void) const
354 {
355         return __pGapAdapter->GetRemainingTimeAsDiscoverable();
356 }
357
358 bool
359 _BluetoothManagerImpl::IsActivated(void) const
360 {
361         return __pGapAdapter->IsActivated();
362 }
363
364 BluetoothDiscoverableMode
365 _BluetoothManagerImpl::GetDiscoverableMode(void) const
366 {
367         return __pGapAdapter->GetDiscoverableMode();
368 }
369
370 bool
371 _BluetoothManagerImpl::IsDiscoveryInProgress(void) const
372 {
373         return __pGapAdapter->IsDiscoveryInProgress();
374 }
375
376 result
377 _BluetoothManagerImpl::SetLocalDeviceName(const Tizen::Base::String& deviceName)
378 {
379         result r = E_SUCCESS;
380
381         SysTryReturn(NID_NET_BT, deviceName.GetLength() > 0, E_INVALID_ARG, E_INVALID_ARG,
382                                 "[E_INVALID_ARG] The specified input name is an empty string.");
383
384         // Sets the local name through osp-connectivity-service.
385         r = __pIpcProxy->SetLocalDeviceName(deviceName);
386         SysTryReturn(NID_NET_BT, r == E_SUCCESS, r, r,
387                                 "[%s] Setting the local device name through osp-connectivity-service has failed.", GetErrorMessage(r));
388
389         return E_SUCCESS;
390 }
391
392 result
393 _BluetoothManagerImpl::SetDiscoverableMode(BluetoothDiscoverableMode mode, int seconds)
394 {
395         result r = E_SUCCESS;
396
397         // Sets the discoverable mode through osp-connectivity-service.
398         r = __pIpcProxy->SetDiscoverableMode((int)mode, seconds);
399         SysTryReturnResult(NID_NET_BT, r != E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED,
400                                 "The application does not have the privilege to call this method.");
401         SysTryReturn(NID_NET_BT, r == E_SUCCESS, r, r,
402                                 "[%s] Setting the the discoverable mode through osp-connectivity-service has failed.", GetErrorMessage(r));
403
404         return E_SUCCESS;
405 }
406
407 result
408 _BluetoothManagerImpl::RefreshPairedDeviceList(void)
409 {
410         result r = E_SUCCESS;
411         IList* pList = null;
412         BluetoothDevice* pPairedDevice = null;
413         int numOfElements = 0;
414
415         pList = __pGapAdapter->GetAllPairedDeviceListN();
416
417         if (pList != null)
418         {
419                 __pairedDeviceList.RemoveAll(true);
420                 numOfElements = pList->GetCount();
421
422                 for (int i = 0; i < numOfElements; i++)
423                 {
424                         pPairedDevice = (BluetoothDevice*) pList->GetAt(i);
425
426                         if (pPairedDevice != null)
427                         {
428                                 r = __pairedDeviceList.Add(*pPairedDevice);
429
430                                 // propagates all kinds of exception as E_SYSTEM
431                                 // except E_OUT_OF_MEMORY
432                                 r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
433                         }
434                         else
435                         {
436                                 r = E_SYSTEM;
437                         }
438
439                         if (r != E_SUCCESS)
440                         {
441                                 pList->RemoveAll(true);
442                                 __pairedDeviceList.RemoveAll(false);
443                                 break;
444                         }
445                 }
446
447                 delete pList;
448         }
449         else
450         {
451                 r = E_SYSTEM;
452         }
453
454         if (r != E_SUCCESS)
455         {
456                 SysLogException(NID_NET_BT, r, "[%s] Internal error occurred on getting all paired device list from system.", GetErrorMessage(r));
457         }
458
459         return r;
460 }
461
462 const BluetoothDevice*
463 _BluetoothManagerImpl::GetPairedDeviceByAddress(const Tizen::Base::ByteBuffer& deviceAddress) const
464 {
465         const ByteBuffer* pAddress = null;
466         int numOfElements = 0;
467
468         numOfElements = __pairedDeviceList.GetCount();
469
470         for (int i = 0; i < numOfElements; i++)
471         {
472                 BluetoothDevice* pListDevice = (BluetoothDevice*) __pairedDeviceList.GetAt(i);
473
474                 if (pListDevice != null)
475                 {
476                         pAddress = pListDevice->GetAddress();
477
478                         if (pAddress->Equals(deviceAddress))
479                         {
480                                 return pListDevice;
481                         }
482                 }
483         }
484
485         SysLogException(NID_NET_BT, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] No device is matched by the input address.");
486         SetLastResult(E_OBJ_NOT_FOUND);
487
488         return null;
489 }
490
491 BluetoothDevice*
492 _BluetoothManagerImpl::GetPairedDeviceByAddressN(const Tizen::Base::ByteBuffer& deviceAddress) const
493 {
494         BluetoothDevice* pResultDevice = null;
495         const BluetoothDevice* pListedDevice = null;
496         result r = E_SUCCESS;
497
498         (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Acquire();
499
500         pListedDevice = dynamic_cast<const BluetoothDevice*>(__pairedDeviceMap.GetValue(deviceAddress));
501         if (pListedDevice != null)
502         {
503                 pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
504                 if (pResultDevice == null)
505                 {
506                         r = GetLastResult();
507                 }
508         }
509         else
510         {
511                 r = E_OBJ_NOT_FOUND;
512         }
513
514         (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Release();
515
516         if (r != E_SUCCESS)
517         {
518                 SysLogException(NID_NET_BT, r, "[%s] exception occurred.", GetErrorMessage(r));
519                 SetLastResult(r);
520         }
521
522         return pResultDevice;
523 }
524
525 Tizen::Base::Collection::IList*
526 _BluetoothManagerImpl::GetPairedDeviceByNameN(const Base::String& deviceName) const
527 {
528         unique_ptr<ArrayList, AllElementsDeleter> pSearchedList;
529         BluetoothDevice* pListedDevice = null;
530         BluetoothDevice* pResultDevice = null;
531         result r = E_SUCCESS;
532
533         pSearchedList.reset(new (std::nothrow) ArrayList);
534
535         if (pSearchedList != null)
536         {
537                 // initialize the capacity of the result list
538                 pSearchedList->Construct(2);
539                 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Acquire();
540
541                 IMapEnumerator* pMapEnum = __pairedDeviceMap.GetMapEnumeratorN();
542
543                 if (pMapEnum)
544                 {
545                         while (pMapEnum->MoveNext() == E_SUCCESS)
546                         {
547                                 pListedDevice = dynamic_cast<BluetoothDevice*>(pMapEnum->GetValue());
548                                 if (pListedDevice != null)
549                                 {
550                                         String listedDeviceName = pListedDevice->GetName();
551                                         int indexOf = 0;
552
553                                         if (listedDeviceName.IndexOf(deviceName, 0, indexOf) == E_SUCCESS)
554                                         {
555                                                 pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
556                                                 if (pResultDevice == null)
557                                                 {
558                                                         r = GetLastResult();
559                                                         break;
560                                                 }
561
562                                                 // doesn't check the result of Add() method to avoid the versioning issue
563                                                 (void) pSearchedList->Add(*pResultDevice);
564                                         }
565                                 }
566                         }
567
568                         delete pMapEnum;
569                 }
570                 else
571                 {
572                         r = E_OUT_OF_MEMORY;
573                 }
574
575                 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Release();
576         }
577         else
578         {
579                 r = E_OUT_OF_MEMORY;
580         }
581
582         if ((r == E_SUCCESS) && (pSearchedList->GetCount() <= 0))
583         {
584                 r = E_OBJ_NOT_FOUND;
585         }
586
587         if (r != E_SUCCESS)
588         {
589                 SysLogException(NID_NET_BT, r, "[%s] exception occurred.", GetErrorMessage(r));
590                 SetLastResult(r);
591                 return null;
592         }
593
594         return pSearchedList.release();
595 }
596
597 const BluetoothDevice*
598 _BluetoothManagerImpl::GetPairedDeviceAt(int index) const
599 {
600         BluetoothDevice* pResultDevice = null;
601
602         if ((index >= 0) && (index < __pairedDeviceList.GetCount()))
603         {
604                 pResultDevice = (BluetoothDevice*) __pairedDeviceList.GetAt(index);
605         }
606
607         if (pResultDevice == null)
608         {
609                 SysLogException(NID_NET_BT, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Index is not available.");
610                 SetLastResult(E_OBJ_NOT_FOUND);
611         }
612
613         return pResultDevice;
614 }
615
616 const Tizen::Base::Collection::IList*
617 _BluetoothManagerImpl::GetPairedDeviceList(void) const
618 {
619         return &__pairedDeviceList;
620 }
621
622 Tizen::Base::Collection::IList*
623 _BluetoothManagerImpl::GetPairedDeviceListN(void) const
624 {
625         unique_ptr<ArrayList, AllElementsDeleter> pResultList;
626         BluetoothDevice* pListedDevice = null;
627         BluetoothDevice* pResultDevice = null;
628         result r = E_SUCCESS;
629         int resultCount = 0;
630
631         pResultList.reset(new (std::nothrow) ArrayList);
632
633         if (pResultList)
634         {
635                 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Acquire();
636
637                 // initialize the capacity of the result list
638                 resultCount = __pairedDeviceMap.GetCount();
639                 pResultList->Construct(resultCount);
640
641                 IMapEnumerator* pMapEnum = __pairedDeviceMap.GetMapEnumeratorN();
642
643                 if (pMapEnum)
644                 {
645                         while (pMapEnum->MoveNext() == E_SUCCESS)
646                         {
647                                 pListedDevice = dynamic_cast<BluetoothDevice*>(pMapEnum->GetValue());
648                                 if (pListedDevice != null)
649                                 {
650                                         pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
651                                         if (pResultDevice == null)
652                                         {
653                                                 r = GetLastResult();
654                                                 break;
655                                         }
656
657                                         // doesn't check the result of Add() method to avoid the versioning issue
658                                         (void) pResultList->Add(*pResultDevice);
659                                 }
660                         }
661
662                         delete pMapEnum;
663                 }
664                 else
665                 {
666                         r = E_OUT_OF_MEMORY;
667                 }
668
669                 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Release();
670         }
671         else
672         {
673                 r = E_OUT_OF_MEMORY;
674         }
675
676         SysTryReturn(NID_NET_BT, r == E_SUCCESS, null, r, "[%s] error occurred.", GetErrorMessage(r));
677
678         return pResultList.release();
679 }
680
681 result
682 _BluetoothManagerImpl::SetBluetoothDeviceListener(IBluetoothDeviceEventListener* pListener)
683 {
684         result r = E_SUCCESS;
685
686         SysTryReturnResult(NID_NET_BT, __pDevEvtListener != pListener, E_SUCCESS, "the listener instance is already registered.");
687
688         if (__pDevEvtListener != null)
689         {
690                 // remove the previous IBluetoothDeviceEventListener instance from the _BluetoothDeviceEvent.
691                 r = __pDevEvent->RemoveListener(*__pDevEvtListener);
692                 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to remove the previous IBluetoothDeviceEventListener.");
693                 SysLog(NID_NET_BT, "Removing the previous IBluetoothDeviceEventListener is successful.");
694                 __pDevEvtListener = null;
695         }
696
697         if (pListener != null)
698         {
699                 // add the specified new IBluetoothDeviceEventListener instance to the _BluetoothDeviceEvent.
700                 r = __pDevEvent->AddListener(*pListener, true);
701                 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to add the new IBluetoothDeviceEventListener.");
702                 SysLog(NID_NET_BT, "Adding the new IBluetoothDeviceEventListener is successful.");
703                 __pDevEvtListener = pListener;
704         }
705
706         return r;
707 }
708
709 result
710 _BluetoothManagerImpl::SetBluetoothConnectionListener(_IBluetoothConnectionEventListener* pListener)
711 {
712         result r = E_SUCCESS;
713
714         SysTryReturnResult(NID_NET_BT, __pConEvtListener != pListener, E_SUCCESS, "the listener instance is already registered.");
715
716         if (__pConEvtListener != null)
717         {
718                 // remove the previous IBluetoothDeviceEventListener instance from the _BluetoothDeviceEvent.
719                 r = __pMgrEvent->RemoveListener(*__pConEvtListener);
720                 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to remove the previous _IBluetoothConnectionEventListener.");
721                 SysLog(NID_NET_BT, "Removing the previous _IBluetoothConnectionEventListener is successful.");
722                 __pConEvtListener = null;
723         }
724
725         if (pListener != null)
726         {
727                 // add the specified new IBluetoothDeviceEventListener instance to the _BluetoothDeviceEvent.
728                 r = __pMgrEvent->AddListener(*pListener, true);
729                 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to add the new _IBluetoothConnectionEventListener.");
730                 SysLog(NID_NET_BT, "Adding the new _IBluetoothConnectionEventListener is successful.");
731                 __pConEvtListener = pListener;
732         }
733
734         return r;
735 }
736
737 result
738 _BluetoothManagerImpl::StartDiscovery(void)
739 {
740         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
741
742         result r = E_SUCCESS;
743
744         __discoveredDeviceList.RemoveAll(true);
745
746         __stateMutex.Acquire();
747
748         switch (__currentState)
749         {
750         case _BT_MGR_STATE_ACTIVATED:
751                 r = __pGapAdapter->StartDiscovery();
752                 if (r == E_SUCCESS)
753                 {
754                         __currentState = _BT_MGR_STATE_DISCOVERY_REQUESTED;
755                 }
756                 break;
757
758         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
759         case _BT_MGR_STATE_ON_DISCOVERY:
760         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
761                 r = E_IN_PROGRESS;
762                 break;
763
764         default:
765                 r = E_INVALID_OPERATION;
766                 break;
767         }
768
769         __stateMutex.Release();
770
771         if (r != E_SUCCESS)
772         {
773                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on starting the device discovery.", GetErrorMessage(r));
774         }
775
776         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
777
778         return r;
779 }
780
781 result
782 _BluetoothManagerImpl::CancelDiscovery(void)
783 {
784         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
785
786         result r = E_SUCCESS;
787
788         __stateMutex.Acquire();
789
790         switch (__currentState)
791         {
792         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
793         case _BT_MGR_STATE_ON_DISCOVERY:
794                 r = __pGapAdapter->CancelDiscovery();
795                 if (r == E_SUCCESS)
796                 {
797                         __currentState = _BT_MGR_STATE_DISCOVERY_CANCELING;
798                 }
799                 break;
800
801         case _BT_MGR_STATE_DISCOVERY_CANCELING:
802                 r = E_IN_PROGRESS;
803                 break;
804
805         default:
806                 r = E_INVALID_OPERATION;
807                 break;
808         }
809
810         __stateMutex.Release();
811
812         if (r != E_SUCCESS)
813         {
814                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on canceling the device discovery.", GetErrorMessage(r));
815         }
816
817         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
818
819         return r;
820 }
821
822 result
823 _BluetoothManagerImpl::RetrieveServiceList(const BluetoothDevice& pairedDevice)
824 {
825         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
826
827         result r = E_SUCCESS;
828         const ByteBuffer* pKeyAddress = null;
829         BluetoothDevice* pListedDevice = null;
830
831         __stateMutex.Acquire();
832
833         switch (__currentState)
834         {
835         case _BT_MGR_STATE_ACTIVATED:
836                 // check paredDevice really exists in the paired device list
837                 pKeyAddress = pairedDevice.GetAddress();
838
839                 __pairedDevMapMutex.Acquire();
840
841                 // TODO: move to the following logic to _BluetoothSystemAdapter::IsPaired()
842                 pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(*pKeyAddress);
843                 if (pListedDevice)
844                 {
845                         r = __pGapAdapter->RetrieveServiceList(*pKeyAddress);
846                 }
847                 else
848                 {
849                         r = E_NOT_PAIRED;
850                 }
851
852                 __pairedDevMapMutex.Release();
853
854                 if (r == E_SUCCESS)
855                 {
856                         __currentState = _BT_MGR_STATE_SERVICE_RETRIEVING;
857                 }
858                 break;
859
860         case _BT_MGR_STATE_SERVICE_RETRIEVING:
861                 r = E_IN_PROGRESS;
862                 break;
863
864         default:
865                 r = E_INVALID_OPERATION;
866                 break;
867         }
868
869         __stateMutex.Release();
870
871         if (r != E_SUCCESS)
872         {
873                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on retrieving the service list from the remote device.", GetErrorMessage(r));
874         }
875
876         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
877
878         return r;
879 }
880
881 result
882 _BluetoothManagerImpl::Pair(const BluetoothDevice& remoteDevice)
883 {
884         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
885
886         result r = E_SUCCESS;
887         const ByteBuffer* pAddress = null;
888
889         __stateMutex.Acquire();
890
891         switch (__currentState)
892         {
893         case _BT_MGR_STATE_ACTIVATED:
894         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
895         case _BT_MGR_STATE_ON_DISCOVERY:
896         case _BT_MGR_STATE_DISCOVERY_CANCELING:
897         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
898         case _BT_MGR_STATE_SERVICE_RETRIEVING:
899                 if (__pairingState == _BT_PAIRING_STATE_NONE)
900                 {
901                         pAddress = remoteDevice.GetAddress();
902                         r = __pGapAdapter->Pair(*pAddress);
903                         if (r == E_SUCCESS)
904                         {
905                                 // copy the address of the pairing device to the local variable
906                                 __pairingTargetAddress.SetArray(pAddress->GetPointer(), 0, _BT_ADDRESS_LENGTH);
907                                 __pairingTargetAddress.SetPosition(0);
908
909                                 __pairingState = _BT_PAIRING_STATE_PAIRING;
910                         }
911                 }
912                 else
913                 {
914                         r = E_DEVICE_BUSY;
915                 }
916                 break;
917
918         default:
919                 r = E_INVALID_OPERATION;
920                 break;
921         }
922
923         __stateMutex.Release();
924
925         if (r != E_SUCCESS)
926         {
927                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on pairing with the remote device.", GetErrorMessage(r));
928         }
929
930         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [result:%s]", GetStringOfCurrentState(),
931                         GetStringOfPairingState(), GetErrorMessage(r));
932
933         return r;
934 }
935
936 result
937 _BluetoothManagerImpl::CancelPair(void)
938 {
939         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
940
941         result r = E_SUCCESS;
942
943         __stateMutex.Acquire();
944
945         switch (__currentState)
946         {
947         case _BT_MGR_STATE_ACTIVATED:
948         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
949         case _BT_MGR_STATE_ON_DISCOVERY:
950         case _BT_MGR_STATE_DISCOVERY_CANCELING:
951         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
952         case _BT_MGR_STATE_SERVICE_RETRIEVING:
953                 if (__pairingState == _BT_PAIRING_STATE_PAIRING)
954                 {
955                         r = __pGapAdapter->CancelPair();
956                         if (r == E_SUCCESS)
957                         {
958                                 __pairingState = _BT_PAIRING_STATE_CANCELING;
959                         }
960                 }
961                 else if (__pairingState == _BT_PAIRING_STATE_CANCELING)
962                 {
963                         r = E_IN_PROGRESS;
964                 }
965                 else
966                 {
967                         r = E_INVALID_OPERATION;
968                 }
969                 break;
970
971         default:
972                 r = E_INVALID_OPERATION;
973                 break;
974         }
975
976         __stateMutex.Release();
977
978         if (r != E_SUCCESS)
979         {
980                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on canceling the pairing process.", GetErrorMessage(r));
981         }
982
983         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [result:%s]", GetStringOfCurrentState(),
984                         GetStringOfPairingState(), GetErrorMessage(r));
985
986         return r;
987 }
988
989 result
990 _BluetoothManagerImpl::Unpair(const BluetoothDevice& pairedDevice)
991 {
992         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
993
994         result r = E_SUCCESS;
995         const ByteBuffer* pKeyAddress = null;
996         BluetoothDevice* pListedDevice = null;
997
998         __stateMutex.Acquire();
999
1000         switch (__currentState)
1001         {
1002         case _BT_MGR_STATE_ACTIVATED:
1003         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
1004         case _BT_MGR_STATE_ON_DISCOVERY:
1005         case _BT_MGR_STATE_DISCOVERY_CANCELING:
1006         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
1007         case _BT_MGR_STATE_SERVICE_RETRIEVING:
1008                 // check paredDevice really exists in the paired device list
1009                 pKeyAddress = pairedDevice.GetAddress();
1010
1011                 __pairedDevMapMutex.Acquire();
1012
1013                 // TODO: move to the following logic to _BluetoothSystemAdapter::IsPaired()
1014                 pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(*pKeyAddress);
1015                 if (pListedDevice)
1016                 {
1017                         r = __pGapAdapter->Unpair(*pKeyAddress);
1018                 }
1019                 else
1020                 {
1021                         r = E_NOT_PAIRED;
1022                 }
1023
1024                 __pairedDevMapMutex.Release();
1025
1026                 break;
1027
1028         default:
1029                 r = E_INVALID_OPERATION;
1030                 break;
1031         }
1032
1033         __stateMutex.Release();
1034
1035         if (r != E_SUCCESS)
1036         {
1037                 SysLogException(NID_NET_BT, r, "[%s] exception occurred on unpairing with the remote device.", GetErrorMessage(r));
1038         }
1039
1040         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [result:%s]", GetStringOfCurrentState(),
1041                         GetStringOfPairingState(), GetErrorMessage(r));
1042
1043         return r;
1044 }
1045
1046 void
1047 _BluetoothManagerImpl::OnBluetoothActivated(result r)
1048 {
1049         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
1050
1051         bool isFired = false;
1052         result resRefresh = E_SUCCESS;
1053
1054         __stateMutex.Acquire();
1055
1056         switch (__currentState)
1057         {
1058         case _BT_MGR_STATE_DEACTIVATED:
1059                 if (r == E_SUCCESS)
1060                 {
1061                         __currentState = _BT_MGR_STATE_ACTIVATED;
1062                         isFired = true;
1063                 }
1064                 break;
1065
1066         case _BT_MGR_STATE_ACTIVATING:
1067                 if (r == E_SUCCESS)
1068                 {
1069                         __currentState = _BT_MGR_STATE_ACTIVATED;
1070                 }
1071                 else
1072                 {
1073                         __currentState = _BT_MGR_STATE_DEACTIVATED;
1074                 }
1075                 isFired = true;
1076                 break;
1077
1078         default:
1079                 // ignore other cases
1080                 break;
1081         }
1082
1083         __stateMutex.Release();
1084
1085         // refresh the paired device list
1086         resRefresh = RefreshPairedDeviceList();
1087         SysLog(NID_NET_BT, "Refreshing the paired device list %s.", (resRefresh == E_SUCCESS) ? "is successful" : "fails");
1088
1089         // refresh the paired device map which is used since 2.0
1090         resRefresh = RefreshPairedDeviceMap();
1091         SysLog(NID_NET_BT, "Refreshing the paired device map %s.", (resRefresh == E_SUCCESS) ? "is successful" : "fails");
1092
1093         if (isFired)
1094         {
1095                 _BluetoothManagerEventArg* pArg = new (std::nothrow) _BluetoothManagerEventArg(_BT_MANAGER_EVENT_ACTIVATED, r);
1096                 if (pArg == null)
1097                 {
1098                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1099                         isFired = false;
1100                 }
1101                 else
1102                 {
1103                         __pMgrEvent->FireAsync(*pArg);
1104                 }
1105         }
1106
1107         if ((isFired) && (r != E_SUCCESS))
1108         {
1109                 SysLogException(NID_NET_BT, r, "[%s] exception occurred in Bluetooth activation callback.", GetErrorMessage(r));
1110         }
1111
1112         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ACTIVATED_Event:%s]",
1113                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1114 }
1115
1116 void
1117 _BluetoothManagerImpl::OnBluetoothDeactivated(result r)
1118 {
1119         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
1120
1121         bool isFired = false;
1122
1123         __stateMutex.Acquire();
1124
1125         switch (__currentState)
1126         {
1127         case _BT_MGR_STATE_ACTIVATED:
1128         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
1129         case _BT_MGR_STATE_ON_DISCOVERY:
1130         case _BT_MGR_STATE_DISCOVERY_CANCELING:
1131         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
1132         case _BT_MGR_STATE_SERVICE_RETRIEVING:
1133                 if (r == E_SUCCESS)
1134                 {
1135                         __currentState = _BT_MGR_STATE_DEACTIVATED;
1136                         isFired = true;
1137                 }
1138                 break;
1139
1140         case _BT_MGR_STATE_DEACTIVATING:
1141                 if (r == E_SUCCESS)
1142                 {
1143                         __currentState = _BT_MGR_STATE_DEACTIVATED;
1144                 }
1145                 else
1146                 {
1147                         __currentState = _BT_MGR_STATE_ACTIVATED;
1148                 }
1149                 isFired = true;
1150                 break;
1151
1152         default:
1153                 // ignore other cases
1154                 break;
1155         }
1156
1157         __stateMutex.Release();
1158
1159         if (isFired)
1160         {
1161                 _BluetoothManagerEventArg* pArg = new (std::nothrow) _BluetoothManagerEventArg(_BT_MANAGER_EVENT_DEACTIVATED, r);
1162                 if (pArg == null)
1163                 {
1164                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1165                         isFired = false;
1166                 }
1167                 else
1168                 {
1169                         __pMgrEvent->FireAsync(*pArg);
1170                 }
1171         }
1172
1173         if ((isFired) && (r != E_SUCCESS))
1174         {
1175                 SysLogException(NID_NET_BT, r, "[%s] exception occurred in Bluetooth deactivation callback.", GetErrorMessage(r));
1176         }
1177
1178         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ACTIVATED_Event:%s]",
1179                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1180 }
1181
1182 void
1183 _BluetoothManagerImpl::OnBluetoothDiscoverableModeChanged(BluetoothDiscoverableMode mode)
1184 {
1185         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
1186
1187         bool isFired = false;
1188
1189         _BluetoothManagerEventArg* pArg = new (std::nothrow) _BluetoothManagerEventArg(mode);
1190         if (pArg == null)
1191         {
1192                 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1193         }
1194         else
1195         {
1196                 __pMgrEvent->FireAsync(*pArg);
1197                 isFired = true;
1198         }
1199
1200         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [VISIBILITY_Event:%s]",
1201                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1202 }
1203
1204 void
1205 _BluetoothManagerImpl::OnBluetoothDiscoveryStarted(result r)
1206 {
1207         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
1208
1209         bool isFired = false;
1210
1211         __stateMutex.Acquire();
1212
1213         switch (__currentState)
1214         {
1215         case _BT_MGR_STATE_ACTIVATED:
1216                 if (r == E_SUCCESS)
1217                 {
1218                         __currentState = _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM;
1219                 }
1220                 break;
1221
1222         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
1223                 if (r == E_SUCCESS)
1224                 {
1225                         __currentState = _BT_MGR_STATE_ON_DISCOVERY;
1226                 }
1227                 else
1228                 {
1229                         __currentState = _BT_MGR_STATE_ACTIVATED;
1230                 }
1231
1232                 isFired = true;
1233                 break;
1234
1235         case _BT_MGR_STATE_DISCOVERY_CANCELING:
1236                 if (r == E_SUCCESS)
1237                 {
1238                         // the current status is not changed while firing
1239                         isFired = true;
1240                 }
1241                 break;
1242
1243         default:
1244                 // ignore other cases
1245                 break;
1246         }
1247
1248         __stateMutex.Release();
1249
1250         if (isFired)
1251         {
1252                 _BluetoothDeviceEventArg* pArg = new (std::nothrow) _BluetoothDeviceEventArg(r);
1253                 if (pArg == null)
1254                 {
1255                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1256                         isFired = false;
1257                 }
1258                 else
1259                 {
1260                         __pDevEvent->FireAsync(*pArg);
1261                 }
1262
1263                 if (r != E_SUCCESS)
1264                 {
1265                         SysLogException(NID_NET_BT, r, "[%s] exception occurred in the discovery discovery callback.", GetErrorMessage(r));
1266                 }
1267         }
1268
1269         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [DISCOVERY_STARTED_Event:%s]",
1270                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1271 }
1272
1273 void
1274 _BluetoothManagerImpl::OnBluetoothRemoteDeviceFound(const BluetoothDevice& foundDevice)
1275 {
1276         bool isFired = false;
1277         unique_ptr<ByteBuffer> pFoundDeviceAddress;
1278         _BluetoothDeviceEventArg* pArg = null;
1279
1280         switch (__currentState)
1281         {
1282         case _BT_MGR_STATE_ON_DISCOVERY:
1283         case _BT_MGR_STATE_DISCOVERY_CANCELING:
1284                 pFoundDeviceAddress.reset(new (std::nothrow) ByteBuffer());
1285                 if(pFoundDeviceAddress == null)
1286                 {
1287                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1288                         break;
1289                 }
1290                 pFoundDeviceAddress->Construct(*foundDevice.GetAddress());
1291
1292                 if(__discoveredDeviceList.Contains(*pFoundDeviceAddress.get()) == true)
1293                 {
1294                         SysLog(NID_NET_BT, "The found device already exist.");
1295                         break;
1296                 }
1297
1298                 (void) __discoveredDeviceList.Add(*pFoundDeviceAddress.release());
1299
1300                 pArg = new (std::nothrow) _BluetoothDeviceEventArg(_BT_DEVICE_EVENT_DEVICE_FOUND,
1301                                 foundDevice);
1302                 if (pArg == null)
1303                 {
1304                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1305                 }
1306                 else
1307                 {
1308                         __pDevEvent->FireAsync(*pArg);
1309                         isFired = true;
1310                 }
1311                 break;
1312
1313         default:
1314                 // ignore other cases
1315                 break;
1316         }
1317
1318         SysLog(NID_NET_BT, "[CurrentState:%s], [DEVICE_FOUND_Event:%s]", GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1319 }
1320
1321 void
1322 _BluetoothManagerImpl::OnBluetoothDiscoveryDone(bool isCompleted)
1323 {
1324         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [DiscoveryDone:%s]",
1325                                 GetStringOfCurrentState(), isCompleted ? "Completed" : "NotCompleted");
1326
1327         bool isFired = false;
1328
1329         __stateMutex.Acquire();
1330
1331         switch (__currentState)
1332         {
1333         case _BT_MGR_STATE_ON_DISCOVERY:
1334         case _BT_MGR_STATE_DISCOVERY_CANCELING:
1335                 __currentState = _BT_MGR_STATE_ACTIVATED;
1336                 isFired = true;
1337                 break;
1338
1339         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
1340                 __currentState = _BT_MGR_STATE_ACTIVATED;
1341                 break;
1342
1343         default:
1344                 // ignore other cases
1345                 break;
1346         }
1347
1348         __stateMutex.Release();
1349
1350         if (isFired)
1351         {
1352                 _BluetoothDeviceEventArg* pArg = new (std::nothrow) _BluetoothDeviceEventArg(isCompleted);
1353                 if (pArg == null)
1354                 {
1355                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1356                         isFired = false;
1357                 }
1358                 else
1359                 {
1360                         __pDevEvent->FireAsync(*pArg);
1361                 }
1362         }
1363
1364         __discoveredDeviceList.RemoveAll(true);
1365
1366         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [DISCOVERY_DONE_Event:%s]",
1367                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1368 }
1369
1370 void
1371 _BluetoothManagerImpl::OnBluetoothServiceListReceived(const Tizen::Base::ByteBuffer& address, unsigned long serviceList,
1372                                                                                                                 const Tizen::Base::Collection::IList* pServiceUuidList, result r)
1373 {
1374         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ServiceList:%ld], [result:%s]",
1375                                 GetStringOfCurrentState(), serviceList, GetErrorMessage(r));
1376
1377         bool isFired = false;
1378         BluetoothDevice* pListedDevice = null;
1379         BluetoothDevice* pResultDevice = null;
1380
1381         __stateMutex.Acquire();
1382
1383         switch (__currentState)
1384         {
1385         case _BT_MGR_STATE_SERVICE_RETRIEVING:
1386                 __currentState = _BT_MGR_STATE_ACTIVATED;
1387                 isFired = true;
1388                 break;
1389
1390         default:
1391                 // ignore other cases
1392                 break;
1393         }
1394
1395         __stateMutex.Release();
1396
1397         // updates internal information regardless of the current status if r is E_SUCCESS
1398         __pairedDevMapMutex.Acquire();
1399
1400         pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(address);
1401
1402         if (pListedDevice != null)
1403         {
1404                 if ((r == E_SUCCESS) && (pServiceUuidList != null))
1405                 {
1406                         _BluetoothDeviceImpl* pDevImpl = _BluetoothDeviceImpl::GetInstance(*pListedDevice);
1407                         pDevImpl->SetServiceList(serviceList);
1408                         pDevImpl->SetServiceUuidList(pServiceUuidList, true);
1409                 }
1410
1411                 if (isFired == true)
1412                 {
1413                         // target device instance should be copied
1414                         // because the original instance in the list is able to be removed out of the mutex span.
1415                         pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
1416                         if (pResultDevice == null)
1417                         {
1418                                 r = E_SYSTEM;
1419                         }
1420                 }
1421         }
1422         else
1423         {
1424                 r = E_SYSTEM;
1425         }
1426
1427         __pairedDevMapMutex.Release();
1428
1429         if (r != E_SUCCESS)
1430         {
1431                 serviceList = 0;
1432                 SysLogException(NID_NET_BT, r, "[%s] exception occurred in the service list reception callback.", GetErrorMessage(r));
1433         }
1434
1435         if (isFired)
1436         {
1437                 _BluetoothDeviceEventArg* pArg = null;
1438                 if (pResultDevice == null)
1439                 {
1440                         // It is almost impossible for this case to occur.
1441                         // The only case is that the paired device is unpaired and removed from the paired list
1442                         // during the service discovery.
1443                         // So, the below code is just to prevent system fault at some undefined exceptional cases.
1444                         BluetoothDevice dummyDevice(address, String("Dummy"), BT_COD_MAJ_DEV_CLS_UNCLASSIFIED,
1445                                                                                 BT_COD_MIN_DEV_CLS_UNCLASSIFIED, 0, 0);
1446                         pArg = new (std::nothrow) _BluetoothDeviceEventArg(dummyDevice, serviceList, r);
1447                 }
1448                 else
1449                 {
1450                         pArg = new (std::nothrow) _BluetoothDeviceEventArg(*pResultDevice, serviceList, r);
1451                         delete pResultDevice;
1452                 }
1453
1454                 if (pArg == null)
1455                 {
1456                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1457                         isFired = false;
1458                 }
1459                 else
1460                 {
1461                         __pDevEvent->FireAsync(*pArg);
1462                 }
1463         }
1464
1465         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s], [SERVICELIST_RECEIVED_Event:%s]",
1466                                 GetStringOfCurrentState(), GetErrorMessage(r), isFired ? "Fired" : "NotFired");
1467 }
1468
1469 void
1470 _BluetoothManagerImpl::OnBluetoothPaired(const BluetoothDevice* pPairedDevice, result r)
1471 {
1472         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s], [result:%s]",
1473                                 GetStringOfCurrentState(), GetStringOfPairingState(), GetErrorMessage(r));
1474
1475         result res = E_SUCCESS;
1476         bool isFired = false;
1477         const ByteBuffer* pKeyAddress = null;
1478         BluetoothDevice* pListedDevice = null;
1479         ByteBuffer* pCloneAddress = null;
1480         BluetoothDevice* pCloneDevice = null;
1481         _BluetoothDeviceEventArg* pArg = null;
1482
1483         pKeyAddress = pPairedDevice->GetAddress();
1484
1485         // update the paired list regardless whether the pairing operation is issued by this instance or not
1486         // when the pairing process is finished successfully.
1487         if (r == E_SUCCESS)
1488         {
1489                 __pairedDevMapMutex.Acquire();
1490
1491                 pListedDevice = dynamic_cast<BluetoothDevice*> (__pairedDeviceMap.GetValue(*pKeyAddress));
1492                 res = GetLastResult();
1493
1494                 // insert the clone instance of the paired device into the paired device list
1495                 if ((pListedDevice == null) && (res == E_OBJ_NOT_FOUND))
1496                 {
1497                         pCloneAddress = new (std::nothrow) ByteBuffer();
1498                         if (pCloneAddress)
1499                         {
1500                                 pCloneAddress->Construct(*pKeyAddress);
1501                                 pCloneDevice = new (std::nothrow) BluetoothDevice(*pPairedDevice);
1502                                 if (pCloneDevice)
1503                                 {
1504                                         r = __pairedDeviceMap.Add(*pCloneAddress, *pCloneDevice);
1505                                         // TODO: the following statement should be replaced by the special MACRO function.
1506                                         r = (r == E_SUCCESS) ? E_SUCCESS : E_OPERATION_FAILED;
1507                                 }
1508                                 else
1509                                 {
1510                                         delete pCloneAddress;
1511                                         r = E_OUT_OF_MEMORY;
1512                                 }
1513                         }
1514                         else
1515                         {
1516                                 r = E_OUT_OF_MEMORY;
1517                         }
1518                 }
1519                 // If the device is already contained in the paired list, the BluetoothDevice instance is updated.
1520                 else if (pListedDevice != null)
1521                 {
1522                         *pListedDevice = *pPairedDevice;
1523                 }
1524
1525                 __pairedDevMapMutex.Release();
1526         }
1527
1528         // send event if the pairing process is finished successfully.
1529         if (r == E_SUCCESS)
1530         {
1531                 __pairingState = _BT_PAIRING_STATE_NONE;
1532                 pArg = new (std::nothrow) _BluetoothDeviceEventArg(pPairedDevice, E_SUCCESS);
1533                 SysTryReturnVoidResult(NID_NET_BT, pArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient." );
1534                 isFired = true;
1535         }
1536         // send event if the pairing operation is issued by this instance although the pairing process fails.
1537         else if ((__pairingState != _BT_PAIRING_STATE_NONE) && (pPairedDevice->GetAddress()->Equals(__pairingTargetAddress)))
1538         {
1539                 SysLogException(NID_NET_BT, r, "[%s] exception occurred in the paired callback.", GetErrorMessage(r));
1540                 __pairingState = _BT_PAIRING_STATE_NONE;
1541                 pArg = new (std::nothrow) _BluetoothDeviceEventArg(null, r);
1542                 SysTryReturnVoidResult(NID_NET_BT, pArg != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient." );
1543                 isFired = true;
1544         }
1545
1546         if (isFired == true)
1547         {
1548                 __pDevEvent->FireAsync(*pArg);
1549         }
1550
1551         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [PAIRED_Event:%s]",
1552                                 GetStringOfCurrentState(), GetStringOfPairingState(), isFired ? "Fired" : "NotFired");
1553 }
1554
1555 void
1556 _BluetoothManagerImpl::OnBluetoothUnpaired(const Tizen::Base::ByteBuffer& address)
1557 {
1558         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
1559
1560         result r = E_SUCCESS;
1561         bool isFired = false;
1562         BluetoothDevice* pListedDevice = null;
1563         BluetoothDevice* pResultDevice = null;
1564
1565         __pairedDevMapMutex.Acquire();
1566
1567         pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(address);
1568         if (pListedDevice != null)
1569         {
1570                 // target device instance should be copied
1571                 // because the original instance in the list will be removed from the list and deleted below.
1572                 pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
1573                 if (pResultDevice != null)
1574                 {
1575                         isFired = true;
1576                 }
1577
1578                 // Removes the unpaired device from the paired device list regardless whether the listener exists or not.
1579                 (void) __pairedDeviceMap.Remove(address, true);
1580         }
1581         else
1582         {
1583                 r = GetLastResult();
1584         }
1585
1586         __pairedDevMapMutex.Release();
1587
1588         if (isFired)
1589         {
1590                 _BluetoothDeviceEventArg* pArg = new (std::nothrow) _BluetoothDeviceEventArg(_BT_DEVICE_EVENT_UNPAIRED, *pResultDevice);
1591                 if (pArg == null)
1592                 {
1593                         SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1594                         isFired = false;
1595                 }
1596                 else
1597                 {
1598                         __pDevEvent->FireAsync(*pArg);
1599                 }
1600         }
1601
1602         delete pResultDevice;
1603
1604         if (r != E_SUCCESS)
1605         {
1606                 SysLogException(NID_NET_BT, r, "[%s] exception occurred in the unpaired callback.", GetErrorMessage(r));
1607         }
1608
1609         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [UNPAIRED_Event:%s]",
1610                                 GetStringOfCurrentState(), GetStringOfPairingState(), isFired ? "Fired" : "NotFired");
1611 }
1612
1613 void
1614 _BluetoothManagerImpl::OnBluetoothDeviceConnected(const Tizen::Base::ByteBuffer& address)
1615 {
1616         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
1617
1618         bool isFired = false;
1619
1620         _BluetoothConnectionEventArg* pArg = new (std::nothrow) _BluetoothConnectionEventArg(_BT_CONNECTION_EVENT_CONNECTED, address);
1621         if (pArg == null)
1622         {
1623                 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1624         }
1625         else
1626         {
1627                 __pConEvent->FireAsync(*pArg);
1628                 isFired = true;
1629         }
1630
1631         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [CONNECTED_Event:%s]",
1632                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1633 }
1634
1635 void
1636 _BluetoothManagerImpl::OnBluetoothDeviceDisconnected(const Tizen::Base::ByteBuffer& address)
1637 {
1638         SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
1639
1640         bool isFired = false;
1641
1642         _BluetoothConnectionEventArg* pArg = new (std::nothrow) _BluetoothConnectionEventArg(_BT_CONNECTION_EVENT_DISCONNECTED, address);
1643         if (pArg == null)
1644         {
1645                 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1646         }
1647         else
1648         {
1649                 __pConEvent->FireAsync(*pArg);
1650                 isFired = true;
1651         }
1652
1653         SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [DISCONNECTED_Event:%s]",
1654                                 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1655 }
1656
1657 result
1658 _BluetoothManagerImpl::RefreshPairedDeviceMap(void)
1659 {
1660         result r = E_SUCCESS;
1661         BluetoothDevice* pPairedDevice = null;
1662         ByteBuffer* pKeyAddress = null;
1663         IList* pList = null;
1664
1665         pList = __pGapAdapter->GetAllPairedDeviceListN();
1666         SysTryReturn(NID_NET_BT, pList != null, E_SYSTEM, E_SYSTEM,
1667                                 "[E_SYSTEM] Internal error occurred on getting all paired device list from system.");
1668
1669         __pairedDevMapMutex.Acquire();
1670
1671         // remove all BluetoothDevice instance which the paired device map contains
1672         __pairedDeviceMap.RemoveAll(true);
1673
1674         for (int i = 0; i < pList->GetCount(); i++)
1675         {
1676                 pPairedDevice = (BluetoothDevice*) pList->GetAt(i);
1677
1678                 pKeyAddress = new (std::nothrow) ByteBuffer();
1679
1680                 if (pKeyAddress)
1681                 {
1682                         pKeyAddress->Construct(*(pPairedDevice->GetAddress()));
1683
1684                         r = __pairedDeviceMap.Add(*pKeyAddress, *pPairedDevice);
1685                         if (r != E_SUCCESS)
1686                         {
1687                                 // determines the exception type to be passed to the application.
1688                                 // propagates only E_OUT_OF_MEMORY exception.
1689                                 if (r != E_OUT_OF_MEMORY)
1690                                 {
1691                                         r = E_SYSTEM;
1692                                 }
1693
1694                                 delete pKeyAddress;
1695                                 break;
1696                         }
1697                 }
1698                 else
1699                 {
1700                         r = E_OUT_OF_MEMORY;
1701                         break;
1702                 }
1703         }
1704
1705         if (r != E_SUCCESS)
1706         {
1707                 SysLogException(NID_NET_BT, r, "[%s] Internal error occurred on getting all paired device list from system.", GetErrorMessage(r));
1708                 __pairedDeviceMap.RemoveAll(true);
1709         }
1710
1711         __pairedDevMapMutex.Release();
1712
1713         delete pList;
1714
1715         return r;
1716 }
1717
1718 const char*
1719 _BluetoothManagerImpl::GetStringOfCurrentState(void) const
1720 {
1721         const char* pStateString = null;
1722
1723         switch (__currentState)
1724         {
1725         case _BT_MGR_STATE_DEACTIVATED:
1726                 pStateString = "DEACTIVATED";
1727                 break;
1728
1729         case _BT_MGR_STATE_ACTIVATING:
1730                 pStateString = "ACTIVATING";
1731                 break;
1732
1733         case _BT_MGR_STATE_DEACTIVATING:
1734                 pStateString = "DEACTIVATING";
1735                 break;
1736
1737         case _BT_MGR_STATE_ACTIVATED:
1738                 pStateString = "ACTIVATED";
1739                 break;
1740
1741         case _BT_MGR_STATE_DISCOVERY_REQUESTED:
1742                 pStateString = "DISCOVERY_REQUESTED";
1743                 break;
1744
1745         case _BT_MGR_STATE_ON_DISCOVERY:
1746                 pStateString = "ON_DISCOVERY";
1747                 break;
1748
1749         case _BT_MGR_STATE_DISCOVERY_CANCELING:
1750                 pStateString = "DISCOVERY_CANCELING";
1751                 break;
1752
1753         case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
1754                 pStateString = "ON_DISCOVERY_BY_SYSTEM";
1755                 break;
1756
1757         case _BT_MGR_STATE_SERVICE_RETRIEVING:
1758                 pStateString = "SERVICE_RETRIEVING";
1759                 break;
1760
1761         default:
1762                 pStateString = "Unknown";
1763                 break;
1764         }
1765
1766         return pStateString;
1767 }
1768
1769 const char*
1770 _BluetoothManagerImpl::GetStringOfPairingState(void) const
1771 {
1772         const char* pStateString = null;
1773
1774         switch (__pairingState)
1775         {
1776         case _BT_PAIRING_STATE_NONE:
1777                 pStateString = "NONE";
1778                 break;
1779
1780         case _BT_PAIRING_STATE_PAIRING:
1781                 pStateString = "PAIRING";
1782                 break;
1783
1784         case _BT_PAIRING_STATE_CANCELING:
1785                 pStateString = "CANCELING";
1786                 break;
1787
1788         default:
1789                 pStateString = "Unknown";
1790                 break;
1791         }
1792
1793         return pStateString;
1794 }
1795
1796 _BluetoothManagerImpl*
1797 _BluetoothManagerImpl::GetInstance(BluetoothManager& bluetoothManager)
1798 {
1799         return bluetoothManager.__pImpl;
1800 }
1801
1802 const _BluetoothManagerImpl*
1803 _BluetoothManagerImpl::GetInstance(const BluetoothManager& bluetoothManager)
1804 {
1805         return bluetoothManager.__pImpl;
1806 }
1807
1808 } } } // Tizen::Net::Bluetooth
1809
1810
1811 #ifdef __cplusplus
1812 extern "C"
1813 {
1814 #endif
1815
1816 _OSP_EXPORT_ result
1817 _BluetoothManagerImpl_Activate(void)
1818 {
1819         Tizen::Net::Bluetooth::_BluetoothGapSystemAdapter* pGapAdapter =
1820                                         Tizen::Net::Bluetooth::_BluetoothGapSystemAdapter::GetInstance();
1821         SysTryReturnResult(NID_NET_BT, pGapAdapter != null, E_OUT_OF_MEMORY,
1822                                         "Failed to get an instance of _BluetoothGapSystemAdapter.");
1823
1824         return pGapAdapter->Activate();
1825 }
1826
1827 _OSP_EXPORT_ result
1828 _BluetoothManagerImpl_Deactivate(void)
1829 {
1830         Tizen::Net::Bluetooth::_BluetoothGapSystemAdapter* pGapAdapter =
1831                                         Tizen::Net::Bluetooth::_BluetoothGapSystemAdapter::GetInstance();
1832         SysTryReturnResult(NID_NET_BT, pGapAdapter != null, E_OUT_OF_MEMORY,
1833                                         "Failed to get an instance of _BluetoothGapSystemAdapter.");
1834
1835         return pGapAdapter->Deactivate();
1836 }
1837
1838 #ifdef __cplusplus
1839 }
1840 #endif
1841