2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FSys_DeviceManager.cpp
19 * @brief This is the implementation file for _DeviceManager class.
23 #include <system/media_key.h>
24 #include <bluetooth.h>
26 #include <FBaseSysLog.h>
27 #include <FSys_DeviceManagerEventProvider.h>
28 #include "FSys_DeviceManager.h"
30 using namespace Tizen::App;
31 using namespace Tizen::Io;
32 using namespace Tizen::System;
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
37 const static wchar_t* DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
38 const static wchar_t* DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
40 const static wchar_t* BLUETOOTH_A2DP_CONNECTED = L"Connected";
41 const static wchar_t* BLUETOOTH_A2DP_DISCONNECTED = L"Disconnected";
42 const static wchar_t* BLUETOOTH_A2DP_PLAY = L"Play";
43 const static wchar_t* BLUETOOTH_A2DP_STOP = L"Stop";
44 const static wchar_t* BLUETOOTH_A2DP_PAUSE = L"Pause";
45 const static wchar_t* BLUETOOTH_A2DP_RESUME = L"Resume";
46 const static wchar_t* BLUETOOTH_A2DP_FORWARD = L"Forward";
47 const static wchar_t* BLUETOOTH_A2DP_FASTFORWARD = L"FastForward";
48 const static wchar_t* BLUETOOTH_A2DP_BACKWARD = L"Backward";
49 const static wchar_t* BLUETOOTH_A2DP_REWIND = L"Rewind";
52 Tizen::System::_DeviceManager* Tizen::System::_DeviceManager::__pDeviceManager = null;
54 void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
56 SysLog(NID_SYS, "Bluetooth headset[%s] connection[%d] event", remote_address, connected);
58 _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
59 SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
61 IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
63 if(pDeviceManager->GetBluetoothStatus() != connected && pDeviceEventListener)
67 bt_event = BLUETOOTH_A2DP_CONNECTED;
71 bt_event = BLUETOOTH_A2DP_DISCONNECTED;
74 pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, bt_event);
76 pDeviceManager->SetBluetoothStatus(connected);
79 void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
82 SysLog(NID_SYS, "Bluetooth headset event is occured %d, %d", (int)key, (int)status);
83 _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
84 SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
86 if(status == MEDIA_KEY_STATUS_RELEASED)
91 event = BLUETOOTH_A2DP_PLAY;
94 event = BLUETOOTH_A2DP_STOP;
97 event = BLUETOOTH_A2DP_PAUSE;
99 case MEDIA_KEY_PREVIOUS:
100 event = BLUETOOTH_A2DP_BACKWARD;
103 event = BLUETOOTH_A2DP_FORWARD;
105 case MEDIA_KEY_FASTFORWARD:
106 event = BLUETOOTH_A2DP_FASTFORWARD;
108 case MEDIA_KEY_REWIND:
109 event = BLUETOOTH_A2DP_REWIND;
111 case MEDIA_KEY_UNKNOWN:
113 SysLog(NID_SYS, "Unsupported key[%d] is occured.", key);
117 IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
118 if (pDeviceEventListener)
120 pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, event);
125 _DeviceManager::_DeviceManager()
126 : isBluetoothHeadSetConnected(false)
127 , isInitBluetooth(false)
128 , __pDeviceManagerListener(null)
132 _DeviceManager::~_DeviceManager()
134 if(isInitBluetooth == true)
137 SysLog(NID_SYS, "Bluetooth headset event is released.");
139 btResult = bt_audio_unset_connection_state_changed_cb();
140 SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to unregister bluetooth headset connection event");
142 btResult = bt_audio_deinitialize();
143 SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to close bluetooth");
145 btResult = bt_deinitialize();
146 SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
147 isInitBluetooth = false;
152 _DeviceManager::GetInstance(void)
154 if(__pDeviceManager == null)
156 __pDeviceManager = new (std::nothrow) _DeviceManager();
158 return __pDeviceManager;
162 _DeviceManager::InitializeDevice(void)
164 result r = E_SUCCESS;
166 if(isInitBluetooth == false)
168 SysLog(NID_SYS, "Bluetooth headset event is reserved.");
171 btResult = bt_initialize();
173 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
175 btResult = bt_audio_initialize();
176 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth audio module");
178 btResult = bt_audio_set_connection_state_changed_cb(bluetooth_connection_state_changed, null);
179 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to register bluetooth audio event");
181 isInitBluetooth = true;
183 media_key_reserve(app_media_key_handler, null);
190 _DeviceManager::DeinitializeDevice(void)
192 result r = E_SUCCESS;
199 _DeviceManager::RegisterListner(IDeviceEventListener &listener)
201 __pDeviceManagerListener = &listener;
206 _DeviceManager::UnregisterListner(IDeviceEventListener &listener)
208 __pDeviceManagerListener = null;
212 IDeviceEventListener*
213 _DeviceManager::GetEventListener()
215 return __pDeviceManagerListener;
219 _DeviceManager::GetId(void)
221 return DEVICE_MANAGER_SERVICE_ID;
225 _DeviceManager::GetBluetoothStatus(void)
227 return isBluetoothHeadSetConnected;
230 _DeviceManager::SetBluetoothStatus(bool status)
232 isBluetoothHeadSetConnected = status;
235 extern "C" _OSP_EXPORT_ _IDeviceManager* DeviceManager_CreateInstance()
237 return _DeviceManager::GetInstance();