2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // @file FNetBt_BluetoothManagerImpl.cpp
18 // @brief This is the implementation file for the _BluetoothManagerImpl class.
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"
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Runtime;
42 using namespace Tizen::Base::Collection;
44 namespace Tizen { namespace Net { namespace Bluetooth
47 _BluetoothManagerImpl::_BluetoothManagerImpl(void)
53 , __pDevEvtListener(null)
54 , __pConEvtListener(null)
55 , __pLocalDevice(null)
56 , __pairedDeviceList()
59 , __pairedDevMapMutex()
60 , __currentState(_BT_MGR_STATE_DEACTIVATED)
61 , __pairingState(_BT_PAIRING_STATE_NONE)
62 , __pairingTargetAddress()
63 , __discoveredDeviceList()
67 _BluetoothManagerImpl::~_BluetoothManagerImpl(void)
69 if (__pGapAdapter != null)
71 if ((__currentState == _BT_MGR_STATE_DISCOVERY_REQUESTED)
72 || (__currentState == _BT_MGR_STATE_ON_DISCOVERY))
74 SysLog(NID_NET_BT, "Cancel the device discovery for resource clean.");
75 (void) CancelDiscovery();
78 __pGapAdapter->UnregisterManagerEventListener(*this);
79 __pGapAdapter->UnregisterDeviceEventListener(*this);
84 _BluetoothManagerImpl::Construct(IBluetoothManagerEventListener& listener)
87 unique_ptr<_BluetoothManagerEvent> pMgrEvent;
88 unique_ptr<_BluetoothDeviceEvent> pDevEvent;
89 unique_ptr<_BluetoothConnectionEvent> pConEvent;
91 r = __stateMutex.Create();
92 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to create the state mutex.");
94 r = __pairedDevMapMutex.Create();
95 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to create the map mutex.");
97 r = __pairingTargetAddress.Construct(_BT_ADDRESS_LENGTH);
98 SysTryReturnResult(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, "Failed to construct the address for pairing.");
100 __pIpcProxy = _BluetoothIpcProxy::GetInstance();
101 SysTryReturnResult(NID_NET_BT, __pIpcProxy != null, E_SYSTEM, "Failed to get an instance of _BluetoothIpcProxy.");
103 __pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
104 SysTryReturnResult(NID_NET_BT, __pGapAdapter != null, E_SYSTEM, "Failed to get an instance of _BluetoothGapSystemAdapter.");
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.");
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.");
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.");
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");
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));
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));
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.");
141 // registers this listener to the device event of the system adapter for device/service discovery
142 r = __pGapAdapter->RegisterDeviceEventListener(*this);
145 __pGapAdapter->UnregisterManagerEventListener(*this);
146 SysLogException(NID_NET_BT, E_SYSTEM,
147 "[E_SYSTEM] Failed to register the device event listener to _BluetoothSystemAdapter.");
151 // checks the initial state of local Bluetooth
152 __stateMutex.Acquire();
154 if (__pGapAdapter->IsActivated() == true)
156 if (__pGapAdapter->IsDiscoveryInProgress() == true)
158 __currentState = _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM;
162 __currentState = _BT_MGR_STATE_ACTIVATED;
166 __stateMutex.Release();
168 __pMgrEvent = move(pMgrEvent);
169 __pDevEvent = move(pDevEvent);
170 __pConEvent = move(pConEvent);
176 _BluetoothManagerImpl::Activate(void)
178 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
180 result r = E_SUCCESS;
182 __stateMutex.Acquire();
184 switch (__currentState)
186 case _BT_MGR_STATE_DEACTIVATED:
187 r = __pGapAdapter->Activate();
190 __currentState = _BT_MGR_STATE_ACTIVATING;
194 case _BT_MGR_STATE_ACTIVATING:
199 r = E_INVALID_OPERATION;
202 __stateMutex.Release();
206 SysLogException(NID_NET_BT, r, "[%s] exception occurred on activating Bluetooth.", GetErrorMessage(r));
209 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
215 _BluetoothManagerImpl::Deactivate(void)
217 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
219 result r = E_SUCCESS;
221 __stateMutex.Acquire();
223 switch (__currentState)
225 case _BT_MGR_STATE_DEACTIVATED:
226 case _BT_MGR_STATE_ACTIVATING:
227 r = E_INVALID_OPERATION;
230 case _BT_MGR_STATE_DEACTIVATING:
235 r = __pGapAdapter->Deactivate();
238 __currentState = _BT_MGR_STATE_DEACTIVATING;
242 __stateMutex.Release();
246 SysLogException(NID_NET_BT, r, "[%s] exception occurred on deactivating Bluetooth.", GetErrorMessage(r));
249 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
255 _BluetoothManagerImpl::IsAvailable(BluetoothConnectionType type) const
257 return __pGapAdapter->IsAvailable(type);
260 const BluetoothDevice*
261 _BluetoothManagerImpl::GetLocalDevice(void)
263 ByteBuffer localAddr;
266 result r = E_SUCCESS;
267 _BluetoothDeviceImpl* pImpl = null;
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.");
275 // Gets the Bluetooth local name.
276 localName = GetLocalDeviceName();
278 if (__pLocalDevice == null)
280 __pLocalDevice = new (std::nothrow) BluetoothDevice();
281 if (__pLocalDevice == null)
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);
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);
298 r = _BluetoothDeviceImpl::GetInstance(*__pLocalDevice)->SetAddress(localAddr);
301 _BluetoothDeviceImpl::GetInstance(*__pLocalDevice)->SetName(localName);
305 SysLogException(NID_NET_BT, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
306 SetLastResult(E_SYSTEM);
311 SetLastResult(E_SUCCESS);
313 return __pLocalDevice;
317 _BluetoothManagerImpl::GetLocalDeviceAddress(void) const
319 return __pGapAdapter->GetLocalDeviceAddress();
323 _BluetoothManagerImpl::GetLocalDeviceName(void) const
325 result r = E_SUCCESS;
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
336 BluetoothDeviceStateType
337 _BluetoothManagerImpl::GetLocalDeviceState() const
339 if (__pGapAdapter->IsActivated() == true)
341 if (__pGapAdapter->GetDiscoverableMode() == BT_DISC_MODE_DISCOVERABLE)
343 return BT_DEVICE_STATE_DISCOVERABLE;
346 return BT_DEVICE_STATE_NOT_DISCOVERABLE;
349 return BT_DEVICE_STATE_OFF;
353 _BluetoothManagerImpl::GetRemainingTimeAsDiscoverable(void) const
355 return __pGapAdapter->GetRemainingTimeAsDiscoverable();
359 _BluetoothManagerImpl::IsActivated(void) const
361 return __pGapAdapter->IsActivated();
364 BluetoothDiscoverableMode
365 _BluetoothManagerImpl::GetDiscoverableMode(void) const
367 return __pGapAdapter->GetDiscoverableMode();
371 _BluetoothManagerImpl::IsDiscoveryInProgress(void) const
373 return __pGapAdapter->IsDiscoveryInProgress();
377 _BluetoothManagerImpl::SetLocalDeviceName(const Tizen::Base::String& deviceName)
379 result r = E_SUCCESS;
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.");
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));
393 _BluetoothManagerImpl::SetDiscoverableMode(BluetoothDiscoverableMode mode, int seconds)
395 result r = E_SUCCESS;
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));
408 _BluetoothManagerImpl::RefreshPairedDeviceList(void)
410 result r = E_SUCCESS;
412 BluetoothDevice* pPairedDevice = null;
413 int numOfElements = 0;
415 pList = __pGapAdapter->GetAllPairedDeviceListN();
419 __pairedDeviceList.RemoveAll(true);
420 numOfElements = pList->GetCount();
422 for (int i = 0; i < numOfElements; i++)
424 pPairedDevice = (BluetoothDevice*) pList->GetAt(i);
426 if (pPairedDevice != null)
428 r = __pairedDeviceList.Add(*pPairedDevice);
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);
441 pList->RemoveAll(true);
442 __pairedDeviceList.RemoveAll(false);
456 SysLogException(NID_NET_BT, r, "[%s] Internal error occurred on getting all paired device list from system.", GetErrorMessage(r));
462 const BluetoothDevice*
463 _BluetoothManagerImpl::GetPairedDeviceByAddress(const Tizen::Base::ByteBuffer& deviceAddress) const
465 const ByteBuffer* pAddress = null;
466 int numOfElements = 0;
468 numOfElements = __pairedDeviceList.GetCount();
470 for (int i = 0; i < numOfElements; i++)
472 BluetoothDevice* pListDevice = (BluetoothDevice*) __pairedDeviceList.GetAt(i);
474 if (pListDevice != null)
476 pAddress = pListDevice->GetAddress();
478 if (pAddress->Equals(deviceAddress))
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);
492 _BluetoothManagerImpl::GetPairedDeviceByAddressN(const Tizen::Base::ByteBuffer& deviceAddress) const
494 BluetoothDevice* pResultDevice = null;
495 const BluetoothDevice* pListedDevice = null;
496 result r = E_SUCCESS;
498 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Acquire();
500 pListedDevice = dynamic_cast<const BluetoothDevice*>(__pairedDeviceMap.GetValue(deviceAddress));
501 if (pListedDevice != null)
503 pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
504 if (pResultDevice == null)
514 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Release();
518 SysLogException(NID_NET_BT, r, "[%s] exception occurred.", GetErrorMessage(r));
522 return pResultDevice;
525 Tizen::Base::Collection::IList*
526 _BluetoothManagerImpl::GetPairedDeviceByNameN(const Base::String& deviceName) const
528 unique_ptr<ArrayList, AllElementsDeleter> pSearchedList;
529 BluetoothDevice* pListedDevice = null;
530 BluetoothDevice* pResultDevice = null;
531 result r = E_SUCCESS;
533 pSearchedList.reset(new (std::nothrow) ArrayList);
535 if (pSearchedList != null)
537 // initialize the capacity of the result list
538 pSearchedList->Construct(2);
539 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Acquire();
541 IMapEnumerator* pMapEnum = __pairedDeviceMap.GetMapEnumeratorN();
545 while (pMapEnum->MoveNext() == E_SUCCESS)
547 pListedDevice = dynamic_cast<BluetoothDevice*>(pMapEnum->GetValue());
548 if (pListedDevice != null)
550 String listedDeviceName = pListedDevice->GetName();
553 if (listedDeviceName.IndexOf(deviceName, 0, indexOf) == E_SUCCESS)
555 pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
556 if (pResultDevice == null)
562 // doesn't check the result of Add() method to avoid the versioning issue
563 (void) pSearchedList->Add(*pResultDevice);
575 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Release();
582 if ((r == E_SUCCESS) && (pSearchedList->GetCount() <= 0))
589 SysLogException(NID_NET_BT, r, "[%s] exception occurred.", GetErrorMessage(r));
594 return pSearchedList.release();
597 const BluetoothDevice*
598 _BluetoothManagerImpl::GetPairedDeviceAt(int index) const
600 BluetoothDevice* pResultDevice = null;
602 if ((index >= 0) && (index < __pairedDeviceList.GetCount()))
604 pResultDevice = (BluetoothDevice*) __pairedDeviceList.GetAt(index);
607 if (pResultDevice == null)
609 SysLogException(NID_NET_BT, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] Index is not available.");
610 SetLastResult(E_OBJ_NOT_FOUND);
613 return pResultDevice;
616 const Tizen::Base::Collection::IList*
617 _BluetoothManagerImpl::GetPairedDeviceList(void) const
619 return &__pairedDeviceList;
622 Tizen::Base::Collection::IList*
623 _BluetoothManagerImpl::GetPairedDeviceListN(void) const
625 unique_ptr<ArrayList, AllElementsDeleter> pResultList;
626 BluetoothDevice* pListedDevice = null;
627 BluetoothDevice* pResultDevice = null;
628 result r = E_SUCCESS;
631 pResultList.reset(new (std::nothrow) ArrayList);
635 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Acquire();
637 // initialize the capacity of the result list
638 resultCount = __pairedDeviceMap.GetCount();
639 pResultList->Construct(resultCount);
641 IMapEnumerator* pMapEnum = __pairedDeviceMap.GetMapEnumeratorN();
645 while (pMapEnum->MoveNext() == E_SUCCESS)
647 pListedDevice = dynamic_cast<BluetoothDevice*>(pMapEnum->GetValue());
648 if (pListedDevice != null)
650 pResultDevice = new (std::nothrow) BluetoothDevice(*pListedDevice);
651 if (pResultDevice == null)
657 // doesn't check the result of Add() method to avoid the versioning issue
658 (void) pResultList->Add(*pResultDevice);
669 (const_cast<_BluetoothManagerImpl*>(this))->__pairedDevMapMutex.Release();
676 SysTryReturn(NID_NET_BT, r == E_SUCCESS, null, r, "[%s] error occurred.", GetErrorMessage(r));
678 return pResultList.release();
682 _BluetoothManagerImpl::SetBluetoothDeviceListener(IBluetoothDeviceEventListener* pListener)
684 result r = E_SUCCESS;
686 SysTryReturnResult(NID_NET_BT, __pDevEvtListener != pListener, E_SUCCESS, "the listener instance is already registered.");
688 if (__pDevEvtListener != null)
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;
697 if (pListener != null)
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;
710 _BluetoothManagerImpl::SetBluetoothConnectionListener(_IBluetoothConnectionEventListener* pListener)
712 result r = E_SUCCESS;
714 SysTryReturnResult(NID_NET_BT, __pConEvtListener != pListener, E_SUCCESS, "the listener instance is already registered.");
716 if (__pConEvtListener != null)
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;
725 if (pListener != null)
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;
738 _BluetoothManagerImpl::StartDiscovery(void)
740 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
742 result r = E_SUCCESS;
744 __discoveredDeviceList.RemoveAll(true);
746 __stateMutex.Acquire();
748 switch (__currentState)
750 case _BT_MGR_STATE_ACTIVATED:
751 r = __pGapAdapter->StartDiscovery();
754 __currentState = _BT_MGR_STATE_DISCOVERY_REQUESTED;
758 case _BT_MGR_STATE_DISCOVERY_REQUESTED:
759 case _BT_MGR_STATE_ON_DISCOVERY:
760 case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
765 r = E_INVALID_OPERATION;
769 __stateMutex.Release();
773 SysLogException(NID_NET_BT, r, "[%s] exception occurred on starting the device discovery.", GetErrorMessage(r));
776 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
782 _BluetoothManagerImpl::CancelDiscovery(void)
784 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
786 result r = E_SUCCESS;
788 __stateMutex.Acquire();
790 switch (__currentState)
792 case _BT_MGR_STATE_DISCOVERY_REQUESTED:
793 case _BT_MGR_STATE_ON_DISCOVERY:
794 r = __pGapAdapter->CancelDiscovery();
797 __currentState = _BT_MGR_STATE_DISCOVERY_CANCELING;
801 case _BT_MGR_STATE_DISCOVERY_CANCELING:
806 r = E_INVALID_OPERATION;
810 __stateMutex.Release();
814 SysLogException(NID_NET_BT, r, "[%s] exception occurred on canceling the device discovery.", GetErrorMessage(r));
817 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
823 _BluetoothManagerImpl::RetrieveServiceList(const BluetoothDevice& pairedDevice)
825 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
827 result r = E_SUCCESS;
828 const ByteBuffer* pKeyAddress = null;
829 BluetoothDevice* pListedDevice = null;
831 __stateMutex.Acquire();
833 switch (__currentState)
835 case _BT_MGR_STATE_ACTIVATED:
836 // check paredDevice really exists in the paired device list
837 pKeyAddress = pairedDevice.GetAddress();
839 __pairedDevMapMutex.Acquire();
841 // TODO: move to the following logic to _BluetoothSystemAdapter::IsPaired()
842 pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(*pKeyAddress);
845 r = __pGapAdapter->RetrieveServiceList(*pKeyAddress);
852 __pairedDevMapMutex.Release();
856 __currentState = _BT_MGR_STATE_SERVICE_RETRIEVING;
860 case _BT_MGR_STATE_SERVICE_RETRIEVING:
865 r = E_INVALID_OPERATION;
869 __stateMutex.Release();
873 SysLogException(NID_NET_BT, r, "[%s] exception occurred on retrieving the service list from the remote device.", GetErrorMessage(r));
876 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
882 _BluetoothManagerImpl::Pair(const BluetoothDevice& remoteDevice)
884 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
886 result r = E_SUCCESS;
887 const ByteBuffer* pAddress = null;
889 __stateMutex.Acquire();
891 switch (__currentState)
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)
901 pAddress = remoteDevice.GetAddress();
902 r = __pGapAdapter->Pair(*pAddress);
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);
909 __pairingState = _BT_PAIRING_STATE_PAIRING;
919 r = E_INVALID_OPERATION;
923 __stateMutex.Release();
927 SysLogException(NID_NET_BT, r, "[%s] exception occurred on pairing with the remote device.", GetErrorMessage(r));
930 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [result:%s]", GetStringOfCurrentState(),
931 GetStringOfPairingState(), GetErrorMessage(r));
937 _BluetoothManagerImpl::CancelPair(void)
939 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
941 result r = E_SUCCESS;
943 __stateMutex.Acquire();
945 switch (__currentState)
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)
955 r = __pGapAdapter->CancelPair();
958 __pairingState = _BT_PAIRING_STATE_CANCELING;
961 else if (__pairingState == _BT_PAIRING_STATE_CANCELING)
967 r = E_INVALID_OPERATION;
972 r = E_INVALID_OPERATION;
976 __stateMutex.Release();
980 SysLogException(NID_NET_BT, r, "[%s] exception occurred on canceling the pairing process.", GetErrorMessage(r));
983 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [result:%s]", GetStringOfCurrentState(),
984 GetStringOfPairingState(), GetErrorMessage(r));
990 _BluetoothManagerImpl::Unpair(const BluetoothDevice& pairedDevice)
992 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
994 result r = E_SUCCESS;
995 const ByteBuffer* pKeyAddress = null;
996 BluetoothDevice* pListedDevice = null;
998 __stateMutex.Acquire();
1000 switch (__currentState)
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();
1011 __pairedDevMapMutex.Acquire();
1013 // TODO: move to the following logic to _BluetoothSystemAdapter::IsPaired()
1014 pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(*pKeyAddress);
1017 r = __pGapAdapter->Unpair(*pKeyAddress);
1024 __pairedDevMapMutex.Release();
1029 r = E_INVALID_OPERATION;
1033 __stateMutex.Release();
1037 SysLogException(NID_NET_BT, r, "[%s] exception occurred on unpairing with the remote device.", GetErrorMessage(r));
1040 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [result:%s]", GetStringOfCurrentState(),
1041 GetStringOfPairingState(), GetErrorMessage(r));
1047 _BluetoothManagerImpl::OnBluetoothActivated(result r)
1049 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
1051 bool isFired = false;
1052 result resRefresh = E_SUCCESS;
1054 __stateMutex.Acquire();
1056 switch (__currentState)
1058 case _BT_MGR_STATE_DEACTIVATED:
1061 __currentState = _BT_MGR_STATE_ACTIVATED;
1066 case _BT_MGR_STATE_ACTIVATING:
1069 __currentState = _BT_MGR_STATE_ACTIVATED;
1073 __currentState = _BT_MGR_STATE_DEACTIVATED;
1079 // ignore other cases
1083 __stateMutex.Release();
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");
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");
1095 _BluetoothManagerEventArg* pArg = new (std::nothrow) _BluetoothManagerEventArg(_BT_MANAGER_EVENT_ACTIVATED, r);
1098 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1103 __pMgrEvent->FireAsync(*pArg);
1107 if ((isFired) && (r != E_SUCCESS))
1109 SysLogException(NID_NET_BT, r, "[%s] exception occurred in Bluetooth activation callback.", GetErrorMessage(r));
1112 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ACTIVATED_Event:%s]",
1113 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1117 _BluetoothManagerImpl::OnBluetoothDeactivated(result r)
1119 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
1121 bool isFired = false;
1123 __stateMutex.Acquire();
1125 switch (__currentState)
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:
1135 __currentState = _BT_MGR_STATE_DEACTIVATED;
1140 case _BT_MGR_STATE_DEACTIVATING:
1143 __currentState = _BT_MGR_STATE_DEACTIVATED;
1147 __currentState = _BT_MGR_STATE_ACTIVATED;
1153 // ignore other cases
1157 __stateMutex.Release();
1161 _BluetoothManagerEventArg* pArg = new (std::nothrow) _BluetoothManagerEventArg(_BT_MANAGER_EVENT_DEACTIVATED, r);
1164 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1169 __pMgrEvent->FireAsync(*pArg);
1173 if ((isFired) && (r != E_SUCCESS))
1175 SysLogException(NID_NET_BT, r, "[%s] exception occurred in Bluetooth deactivation callback.", GetErrorMessage(r));
1178 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ACTIVATED_Event:%s]",
1179 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1183 _BluetoothManagerImpl::OnBluetoothDiscoverableModeChanged(BluetoothDiscoverableMode mode)
1185 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
1187 bool isFired = false;
1189 _BluetoothManagerEventArg* pArg = new (std::nothrow) _BluetoothManagerEventArg(mode);
1192 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1196 __pMgrEvent->FireAsync(*pArg);
1200 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [VISIBILITY_Event:%s]",
1201 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1205 _BluetoothManagerImpl::OnBluetoothDiscoveryStarted(result r)
1207 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [result:%s]", GetStringOfCurrentState(), GetErrorMessage(r));
1209 bool isFired = false;
1211 __stateMutex.Acquire();
1213 switch (__currentState)
1215 case _BT_MGR_STATE_ACTIVATED:
1218 __currentState = _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM;
1222 case _BT_MGR_STATE_DISCOVERY_REQUESTED:
1225 __currentState = _BT_MGR_STATE_ON_DISCOVERY;
1229 __currentState = _BT_MGR_STATE_ACTIVATED;
1235 case _BT_MGR_STATE_DISCOVERY_CANCELING:
1238 // the current status is not changed while firing
1244 // ignore other cases
1248 __stateMutex.Release();
1252 _BluetoothDeviceEventArg* pArg = new (std::nothrow) _BluetoothDeviceEventArg(r);
1255 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1260 __pDevEvent->FireAsync(*pArg);
1265 SysLogException(NID_NET_BT, r, "[%s] exception occurred in the discovery discovery callback.", GetErrorMessage(r));
1269 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [DISCOVERY_STARTED_Event:%s]",
1270 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1274 _BluetoothManagerImpl::OnBluetoothRemoteDeviceFound(const BluetoothDevice& foundDevice)
1276 bool isFired = false;
1277 unique_ptr<ByteBuffer> pFoundDeviceAddress;
1278 _BluetoothDeviceEventArg* pArg = null;
1280 switch (__currentState)
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)
1287 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1290 pFoundDeviceAddress->Construct(*foundDevice.GetAddress());
1292 if(__discoveredDeviceList.Contains(*pFoundDeviceAddress.get()) == true)
1294 SysLog(NID_NET_BT, "The found device already exist.");
1298 (void) __discoveredDeviceList.Add(*pFoundDeviceAddress.release());
1300 pArg = new (std::nothrow) _BluetoothDeviceEventArg(_BT_DEVICE_EVENT_DEVICE_FOUND,
1304 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1308 __pDevEvent->FireAsync(*pArg);
1314 // ignore other cases
1318 SysLog(NID_NET_BT, "[CurrentState:%s], [DEVICE_FOUND_Event:%s]", GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1322 _BluetoothManagerImpl::OnBluetoothDiscoveryDone(bool isCompleted)
1324 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [DiscoveryDone:%s]",
1325 GetStringOfCurrentState(), isCompleted ? "Completed" : "NotCompleted");
1327 bool isFired = false;
1329 __stateMutex.Acquire();
1331 switch (__currentState)
1333 case _BT_MGR_STATE_ON_DISCOVERY:
1334 case _BT_MGR_STATE_DISCOVERY_CANCELING:
1335 __currentState = _BT_MGR_STATE_ACTIVATED;
1339 case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
1340 __currentState = _BT_MGR_STATE_ACTIVATED;
1344 // ignore other cases
1348 __stateMutex.Release();
1352 _BluetoothDeviceEventArg* pArg = new (std::nothrow) _BluetoothDeviceEventArg(isCompleted);
1355 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1360 __pDevEvent->FireAsync(*pArg);
1364 __discoveredDeviceList.RemoveAll(true);
1366 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [DISCOVERY_DONE_Event:%s]",
1367 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1371 _BluetoothManagerImpl::OnBluetoothServiceListReceived(const Tizen::Base::ByteBuffer& address, unsigned long serviceList,
1372 const Tizen::Base::Collection::IList* pServiceUuidList, result r)
1374 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ServiceList:%ld], [result:%s]",
1375 GetStringOfCurrentState(), serviceList, GetErrorMessage(r));
1377 bool isFired = false;
1378 BluetoothDevice* pListedDevice = null;
1379 BluetoothDevice* pResultDevice = null;
1381 __stateMutex.Acquire();
1383 switch (__currentState)
1385 case _BT_MGR_STATE_SERVICE_RETRIEVING:
1386 __currentState = _BT_MGR_STATE_ACTIVATED;
1391 // ignore other cases
1395 __stateMutex.Release();
1397 // updates internal information regardless of the current status if r is E_SUCCESS
1398 __pairedDevMapMutex.Acquire();
1400 pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(address);
1402 if (pListedDevice != null)
1404 if ((r == E_SUCCESS) && (pServiceUuidList != null))
1406 _BluetoothDeviceImpl* pDevImpl = _BluetoothDeviceImpl::GetInstance(*pListedDevice);
1407 pDevImpl->SetServiceList(serviceList);
1408 pDevImpl->SetServiceUuidList(pServiceUuidList, true);
1411 if (isFired == true)
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)
1427 __pairedDevMapMutex.Release();
1432 SysLogException(NID_NET_BT, r, "[%s] exception occurred in the service list reception callback.", GetErrorMessage(r));
1437 _BluetoothDeviceEventArg* pArg = null;
1438 if (pResultDevice == null)
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);
1450 pArg = new (std::nothrow) _BluetoothDeviceEventArg(*pResultDevice, serviceList, r);
1451 delete pResultDevice;
1456 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1461 __pDevEvent->FireAsync(*pArg);
1465 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [result:%s], [SERVICELIST_RECEIVED_Event:%s]",
1466 GetStringOfCurrentState(), GetErrorMessage(r), isFired ? "Fired" : "NotFired");
1470 _BluetoothManagerImpl::OnBluetoothPaired(const BluetoothDevice* pPairedDevice, result r)
1472 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s], [result:%s]",
1473 GetStringOfCurrentState(), GetStringOfPairingState(), GetErrorMessage(r));
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;
1483 pKeyAddress = pPairedDevice->GetAddress();
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.
1489 __pairedDevMapMutex.Acquire();
1491 pListedDevice = dynamic_cast<BluetoothDevice*> (__pairedDeviceMap.GetValue(*pKeyAddress));
1492 res = GetLastResult();
1494 // insert the clone instance of the paired device into the paired device list
1495 if ((pListedDevice == null) && (res == E_OBJ_NOT_FOUND))
1497 pCloneAddress = new (std::nothrow) ByteBuffer();
1500 pCloneAddress->Construct(*pKeyAddress);
1501 pCloneDevice = new (std::nothrow) BluetoothDevice(*pPairedDevice);
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;
1510 delete pCloneAddress;
1511 r = E_OUT_OF_MEMORY;
1516 r = E_OUT_OF_MEMORY;
1519 // If the device is already contained in the paired list, the BluetoothDevice instance is updated.
1520 else if (pListedDevice != null)
1522 *pListedDevice = *pPairedDevice;
1525 __pairedDevMapMutex.Release();
1528 // send event if the pairing process is finished successfully.
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." );
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)))
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." );
1546 if (isFired == true)
1548 __pDevEvent->FireAsync(*pArg);
1551 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [PAIRED_Event:%s]",
1552 GetStringOfCurrentState(), GetStringOfPairingState(), isFired ? "Fired" : "NotFired");
1556 _BluetoothManagerImpl::OnBluetoothUnpaired(const Tizen::Base::ByteBuffer& address)
1558 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [PairingState:%s]", GetStringOfCurrentState(), GetStringOfPairingState());
1560 result r = E_SUCCESS;
1561 bool isFired = false;
1562 BluetoothDevice* pListedDevice = null;
1563 BluetoothDevice* pResultDevice = null;
1565 __pairedDevMapMutex.Acquire();
1567 pListedDevice = (BluetoothDevice*) __pairedDeviceMap.GetValue(address);
1568 if (pListedDevice != null)
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)
1578 // Removes the unpaired device from the paired device list regardless whether the listener exists or not.
1579 (void) __pairedDeviceMap.Remove(address, true);
1583 r = GetLastResult();
1586 __pairedDevMapMutex.Release();
1590 _BluetoothDeviceEventArg* pArg = new (std::nothrow) _BluetoothDeviceEventArg(_BT_DEVICE_EVENT_UNPAIRED, *pResultDevice);
1593 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1598 __pDevEvent->FireAsync(*pArg);
1602 delete pResultDevice;
1606 SysLogException(NID_NET_BT, r, "[%s] exception occurred in the unpaired callback.", GetErrorMessage(r));
1609 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PairingState:%s], [UNPAIRED_Event:%s]",
1610 GetStringOfCurrentState(), GetStringOfPairingState(), isFired ? "Fired" : "NotFired");
1614 _BluetoothManagerImpl::OnBluetoothDeviceConnected(const Tizen::Base::ByteBuffer& address)
1616 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
1618 bool isFired = false;
1620 _BluetoothConnectionEventArg* pArg = new (std::nothrow) _BluetoothConnectionEventArg(_BT_CONNECTION_EVENT_CONNECTED, address);
1623 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1627 __pConEvent->FireAsync(*pArg);
1631 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [CONNECTED_Event:%s]",
1632 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1636 _BluetoothManagerImpl::OnBluetoothDeviceDisconnected(const Tizen::Base::ByteBuffer& address)
1638 SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());
1640 bool isFired = false;
1642 _BluetoothConnectionEventArg* pArg = new (std::nothrow) _BluetoothConnectionEventArg(_BT_CONNECTION_EVENT_DISCONNECTED, address);
1645 SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1649 __pConEvent->FireAsync(*pArg);
1653 SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [DISCONNECTED_Event:%s]",
1654 GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
1658 _BluetoothManagerImpl::RefreshPairedDeviceMap(void)
1660 result r = E_SUCCESS;
1661 BluetoothDevice* pPairedDevice = null;
1662 ByteBuffer* pKeyAddress = null;
1663 IList* pList = null;
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.");
1669 __pairedDevMapMutex.Acquire();
1671 // remove all BluetoothDevice instance which the paired device map contains
1672 __pairedDeviceMap.RemoveAll(true);
1674 for (int i = 0; i < pList->GetCount(); i++)
1676 pPairedDevice = (BluetoothDevice*) pList->GetAt(i);
1678 pKeyAddress = new (std::nothrow) ByteBuffer();
1682 pKeyAddress->Construct(*(pPairedDevice->GetAddress()));
1684 r = __pairedDeviceMap.Add(*pKeyAddress, *pPairedDevice);
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)
1700 r = E_OUT_OF_MEMORY;
1707 SysLogException(NID_NET_BT, r, "[%s] Internal error occurred on getting all paired device list from system.", GetErrorMessage(r));
1708 __pairedDeviceMap.RemoveAll(true);
1711 __pairedDevMapMutex.Release();
1719 _BluetoothManagerImpl::GetStringOfCurrentState(void) const
1721 const char* pStateString = null;
1723 switch (__currentState)
1725 case _BT_MGR_STATE_DEACTIVATED:
1726 pStateString = "DEACTIVATED";
1729 case _BT_MGR_STATE_ACTIVATING:
1730 pStateString = "ACTIVATING";
1733 case _BT_MGR_STATE_DEACTIVATING:
1734 pStateString = "DEACTIVATING";
1737 case _BT_MGR_STATE_ACTIVATED:
1738 pStateString = "ACTIVATED";
1741 case _BT_MGR_STATE_DISCOVERY_REQUESTED:
1742 pStateString = "DISCOVERY_REQUESTED";
1745 case _BT_MGR_STATE_ON_DISCOVERY:
1746 pStateString = "ON_DISCOVERY";
1749 case _BT_MGR_STATE_DISCOVERY_CANCELING:
1750 pStateString = "DISCOVERY_CANCELING";
1753 case _BT_MGR_STATE_ON_DISCOVERY_BY_SYSTEM:
1754 pStateString = "ON_DISCOVERY_BY_SYSTEM";
1757 case _BT_MGR_STATE_SERVICE_RETRIEVING:
1758 pStateString = "SERVICE_RETRIEVING";
1762 pStateString = "Unknown";
1766 return pStateString;
1770 _BluetoothManagerImpl::GetStringOfPairingState(void) const
1772 const char* pStateString = null;
1774 switch (__pairingState)
1776 case _BT_PAIRING_STATE_NONE:
1777 pStateString = "NONE";
1780 case _BT_PAIRING_STATE_PAIRING:
1781 pStateString = "PAIRING";
1784 case _BT_PAIRING_STATE_CANCELING:
1785 pStateString = "CANCELING";
1789 pStateString = "Unknown";
1793 return pStateString;
1796 _BluetoothManagerImpl*
1797 _BluetoothManagerImpl::GetInstance(BluetoothManager& bluetoothManager)
1799 return bluetoothManager.__pImpl;
1802 const _BluetoothManagerImpl*
1803 _BluetoothManagerImpl::GetInstance(const BluetoothManager& bluetoothManager)
1805 return bluetoothManager.__pImpl;
1808 } } } // Tizen::Net::Bluetooth
1817 _BluetoothManagerImpl_Activate(void)
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.");
1824 return pGapAdapter->Activate();
1828 _BluetoothManagerImpl_Deactivate(void)
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.");
1835 return pGapAdapter->Deactivate();