[2.2.1] Apply reviewed header file (Base to Collection)
[platform/framework/native/appfw.git] / src / system-server / device / FSys_DeviceManager.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSys_DeviceManager.cpp
19  * @brief               This is the implementation file for _DeviceManager class.
20  */
21
22 #include <new>
23 #include <system/media_key.h>
24 #include <bluetooth.h>
25 #include <vconf.h>
26
27 #include <FBaseSysLog.h>
28 #include <FSys_DeviceManagerEventProvider.h>
29 #include "FSys_DeviceManager.h"
30
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;
36
37 namespace {
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";
40
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";
51 }
52
53 Tizen::System::_DeviceManager* Tizen::System::_DeviceManager::__pDeviceManager = null;
54
55 void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
56 {
57         SysLog(NID_SYS, "Bluetooth headset[%s] connection[%d] event", remote_address, connected);
58         String bt_event;
59         _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
60         SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
61
62         IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
63
64         if(pDeviceEventListener)
65         {
66                 if(connected == true)
67                 {
68                         bt_event = BLUETOOTH_A2DP_CONNECTED;
69                 }
70                 else
71                 {
72                         bt_event = BLUETOOTH_A2DP_DISCONNECTED;
73                 }
74
75                 pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, bt_event);
76         }
77         pDeviceManager->SetBluetoothStatus(connected);
78 }
79
80 void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
81 {
82         String event;
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");
86
87         if(status == MEDIA_KEY_STATUS_RELEASED)
88         {
89                 switch(key)
90                 {
91                         case MEDIA_KEY_PLAY:
92                                 event = BLUETOOTH_A2DP_PLAY;
93                                 break;
94                         case MEDIA_KEY_STOP:
95                                 event = BLUETOOTH_A2DP_STOP;
96                                 break;
97                         case MEDIA_KEY_PAUSE:
98                                 event = BLUETOOTH_A2DP_PAUSE;
99                                 break;
100                         case MEDIA_KEY_PREVIOUS:
101                                 event = BLUETOOTH_A2DP_BACKWARD;
102                                 break;
103                         case MEDIA_KEY_NEXT:
104                                 event = BLUETOOTH_A2DP_FORWARD;
105                                 break;
106                         case MEDIA_KEY_FASTFORWARD:
107                                 event = BLUETOOTH_A2DP_FASTFORWARD;
108                                 break;
109                         case MEDIA_KEY_REWIND:
110                                 event = BLUETOOTH_A2DP_REWIND;
111                                 break;
112                         case MEDIA_KEY_UNKNOWN:
113                         default:
114                                 SysLog(NID_SYS, "Unsupported key[%d] is occured.", key);
115                                 return;
116                 }
117
118                 IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
119                 if (pDeviceEventListener)
120                 {
121                         pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, event);
122                 }
123         }
124 }
125
126 _DeviceManager::_DeviceManager()
127         : isBluetoothHeadSetConnected(false)
128         , isInitBluetooth(false)
129         , __pDeviceManagerListener(null)
130 {
131 }
132
133 _DeviceManager::~_DeviceManager()
134 {
135         if(isInitBluetooth == true)
136         {
137                 int btResult = 0;
138                 media_key_release();
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");
141
142                 btResult = bt_audio_deinitialize();
143                 SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to close bluetooth");
144
145                 btResult = bt_deinitialize();
146                 SysTryReturnVoidResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
147                 isInitBluetooth = false;
148         }
149 }
150
151 _DeviceManager*
152 _DeviceManager::GetInstance(void)
153 {
154         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
155         if(__pDeviceManager == null)
156         {
157                 pthread_once(&onceBlock, InitSingleton);
158         }
159         return __pDeviceManager;
160 }
161
162 void
163 _DeviceManager::InitSingleton(void)
164 {
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);
169 }
170
171
172 void
173 _DeviceManager::DestroySingleton(void)
174 {
175         SysLog(NID_SYS,"DestorySingleton");
176         if (__pDeviceManager)
177         {
178                 delete __pDeviceManager;
179                 __pDeviceManager = null;
180         }
181 }
182 result
183 _DeviceManager::InitializeDevice(void)
184 {
185         result r = E_SUCCESS;
186
187         if(isInitBluetooth == false)
188         {
189                 int btResult = 0;
190                 btResult = bt_initialize();
191                 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
192
193                 btResult = bt_audio_initialize();
194                 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth audio module");
195
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;
199         }
200
201         media_key_reserve(app_media_key_handler, null);
202         return r;
203 }
204
205
206 result
207 _DeviceManager::DeinitializeDevice(void)
208 {
209         result r = E_SUCCESS;
210         media_key_release();
211         return r;
212 }
213
214
215 result
216 _DeviceManager::RegisterListner(IDeviceEventListener &listener)
217 {
218         __pDeviceManagerListener = &listener;
219         return E_SUCCESS;
220 }
221
222 result
223 _DeviceManager::UnregisterListner(IDeviceEventListener &listener)
224 {
225         __pDeviceManagerListener = null;
226         return E_SUCCESS;
227 }
228
229 IDeviceEventListener*
230 _DeviceManager::GetEventListener()
231 {
232         return __pDeviceManagerListener;
233 }
234
235 String
236 _DeviceManager::GetId(void)
237 {
238         return DEVICE_MANAGER_SERVICE_ID;
239 }
240
241 bool
242 _DeviceManager::GetBluetoothStatus(void)
243 {
244         int ret = -1;
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);
247
248         if (ret & VCONFKEY_BT_DEVICE_HEADSET_CONNECTED || ret & VCONFKEY_BT_DEVICE_A2DP_HEADSET_CONNECTED)
249         {
250                 isBluetoothHeadSetConnected = true;
251         }
252         else
253         {
254                 isBluetoothHeadSetConnected = false;
255         }
256         return isBluetoothHeadSetConnected;
257 }
258 void
259 _DeviceManager::SetBluetoothStatus(bool status)
260 {
261         isBluetoothHeadSetConnected = status;
262 }