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>
27 #include <FBaseSysLog.h>
28 #include <FSys_DeviceManagerEventProvider.h>
29 #include "FSys_DeviceManager.h"
31 using namespace Tizen::App;
32 using namespace Tizen::Io;
33 using namespace Tizen::System;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Collection;
38 const static wchar_t* DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
39 const static wchar_t* DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
41 const static wchar_t* BLUETOOTH_A2DP_CONNECTED = L"Connected";
42 const static wchar_t* BLUETOOTH_A2DP_DISCONNECTED = L"Disconnected";
43 const static wchar_t* BLUETOOTH_A2DP_PLAY = L"Play";
44 const static wchar_t* BLUETOOTH_A2DP_STOP = L"Stop";
45 const static wchar_t* BLUETOOTH_A2DP_PAUSE = L"Pause";
46 const static wchar_t* BLUETOOTH_A2DP_RESUME = L"Resume";
47 const static wchar_t* BLUETOOTH_A2DP_FORWARD = L"Forward";
48 const static wchar_t* BLUETOOTH_A2DP_FASTFORWARD = L"FastForward";
49 const static wchar_t* BLUETOOTH_A2DP_BACKWARD = L"Backward";
50 const static wchar_t* BLUETOOTH_A2DP_REWIND = L"Rewind";
53 Tizen::System::_DeviceManager* Tizen::System::_DeviceManager::__pDeviceManager = null;
55 void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
57 SysLog(NID_SYS, "Bluetooth headset[%s] connection[%d] event", remote_address, connected);
59 _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
60 SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
62 IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
64 if(pDeviceEventListener)
68 bt_event = BLUETOOTH_A2DP_CONNECTED;
72 bt_event = BLUETOOTH_A2DP_DISCONNECTED;
75 pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, bt_event);
77 pDeviceManager->SetBluetoothStatus(connected);
80 void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
83 SysLog(NID_SYS, "Bluetooth headset event is occured %d, %d", (int)key, (int)status);
84 _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
85 SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
87 if(status == MEDIA_KEY_STATUS_RELEASED)
92 event = BLUETOOTH_A2DP_PLAY;
95 event = BLUETOOTH_A2DP_STOP;
98 event = BLUETOOTH_A2DP_PAUSE;
100 case MEDIA_KEY_PREVIOUS:
101 event = BLUETOOTH_A2DP_BACKWARD;
104 event = BLUETOOTH_A2DP_FORWARD;
106 case MEDIA_KEY_FASTFORWARD:
107 event = BLUETOOTH_A2DP_FASTFORWARD;
109 case MEDIA_KEY_REWIND:
110 event = BLUETOOTH_A2DP_REWIND;
112 case MEDIA_KEY_UNKNOWN:
114 SysLog(NID_SYS, "Unsupported key[%d] is occured.", key);
118 IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
119 if (pDeviceEventListener)
121 pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, event);
126 _DeviceManager::_DeviceManager()
127 : isBluetoothHeadSetConnected(false)
128 , isInitBluetooth(false)
129 , __pDeviceManagerListener(null)
133 _DeviceManager::~_DeviceManager()
135 if(isInitBluetooth == true)
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 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
155 if(__pDeviceManager == null)
157 pthread_once(&onceBlock, InitSingleton);
159 return __pDeviceManager;
163 _DeviceManager::InitSingleton(void)
165 SysLog(NID_SYS,"InitSingleton");
166 __pDeviceManager = new (std::nothrow) _DeviceManager();
167 SysTryReturn(NID_SYS, __pDeviceManager, , E_OUT_OF_MEMORY, "It is not enough memory.");
168 atexit(DestroySingleton);
173 _DeviceManager::DestroySingleton(void)
175 SysLog(NID_SYS,"DestorySingleton");
176 if (__pDeviceManager)
178 delete __pDeviceManager;
179 __pDeviceManager = null;
183 _DeviceManager::InitializeDevice(void)
185 result r = E_SUCCESS;
187 if(isInitBluetooth == false)
190 btResult = bt_initialize();
191 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
193 btResult = bt_audio_initialize();
194 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth audio module");
196 btResult = bt_audio_set_connection_state_changed_cb(bluetooth_connection_state_changed, null);
197 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to register bluetooth audio event");
198 isInitBluetooth = true;
201 media_key_reserve(app_media_key_handler, null);
207 _DeviceManager::DeinitializeDevice(void)
209 result r = E_SUCCESS;
216 _DeviceManager::RegisterListner(IDeviceEventListener &listener)
218 __pDeviceManagerListener = &listener;
223 _DeviceManager::UnregisterListner(IDeviceEventListener &listener)
225 __pDeviceManagerListener = null;
229 IDeviceEventListener*
230 _DeviceManager::GetEventListener()
232 return __pDeviceManagerListener;
236 _DeviceManager::GetId(void)
238 return DEVICE_MANAGER_SERVICE_ID;
242 _DeviceManager::GetBluetoothStatus(void)
245 int errorCode = vconf_get_int(VCONFKEY_BT_DEVICE, &ret);
246 SysTryReturn(NID_SYS, errorCode == 0, isBluetoothHeadSetConnected, E_SYSTEM, "It is failed to get bt status. errorCode [%d]", errorCode);
248 if (ret & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED || ret & VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED)
250 isBluetoothHeadSetConnected = true;
254 isBluetoothHeadSetConnected = false;
256 return isBluetoothHeadSetConnected;
259 _DeviceManager::SetBluetoothStatus(bool status)
261 isBluetoothHeadSetConnected = status;