Seperate DeviceManagerService into System-service so.
[platform/framework/native/appfw.git] / src / system-server / dev / 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
26 #include <FBaseSysLog.h>
27 #include <FSys_DeviceManagerEventProvider.h>
28 #include "FSys_DeviceManager.h"
29
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;
35
36 namespace {
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";
39
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";
50 }
51
52 Tizen::System::_DeviceManager* Tizen::System::_DeviceManager::__pDeviceManager = null;
53
54 void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
55 {
56         SysLog(NID_SYS, "Bluetooth headset[%s] connection[%d] event", remote_address, connected);
57         String bt_event;
58         _DeviceManager* pDeviceManager = _DeviceManager::GetInstance();
59         SysTryReturn(NID_SYS, pDeviceManager, ,E_SYSTEM, "DeviceManager is not created");
60
61         IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
62
63         if(pDeviceManager->GetBluetoothStatus() != connected && pDeviceEventListener)
64         {
65                 if(connected == true)
66                 {
67                         bt_event = BLUETOOTH_A2DP_CONNECTED;
68                 }
69                 else
70                 {
71                         bt_event = BLUETOOTH_A2DP_DISCONNECTED;
72                 }
73
74                 pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, bt_event);
75         }
76         pDeviceManager->SetBluetoothStatus(connected);
77 }
78
79 void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
80 {
81         String event;
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");
85
86         if(status == MEDIA_KEY_STATUS_RELEASED)
87         {
88                 switch(key)
89                 {
90                         case MEDIA_KEY_PLAY:
91                                 event = BLUETOOTH_A2DP_PLAY;
92                                 break;
93                         case MEDIA_KEY_STOP:
94                                 event = BLUETOOTH_A2DP_STOP;
95                                 break;
96                         case MEDIA_KEY_PAUSE:
97                                 event = BLUETOOTH_A2DP_PAUSE;
98                                 break;
99                         case MEDIA_KEY_PREVIOUS:
100                                 event = BLUETOOTH_A2DP_BACKWARD;
101                                 break;
102                         case MEDIA_KEY_NEXT:
103                                 event = BLUETOOTH_A2DP_FORWARD;
104                                 break;
105                         case MEDIA_KEY_FASTFORWARD:
106                                 event = BLUETOOTH_A2DP_FASTFORWARD;
107                                 break;
108                         case MEDIA_KEY_REWIND:
109                                 event = BLUETOOTH_A2DP_REWIND;
110                                 break;
111                         case MEDIA_KEY_UNKNOWN:
112                         default:
113                                 SysLog(NID_SYS, "Unsupported key[%d] is occured.", key);
114                                 return;
115                 }
116
117                 IDeviceEventListener* pDeviceEventListener = pDeviceManager->GetEventListener();
118                 if (pDeviceEventListener)
119                 {
120                         pDeviceEventListener->OnDeviceStateChanged(DEVICE_TYPE_BLUETOOTH_HEADSET, event);
121                 }
122         }
123 }
124
125 _DeviceManager::_DeviceManager()
126         : isBluetoothHeadSetConnected(false)
127         , isInitBluetooth(false)
128         , __pDeviceManagerListener(null)
129 {
130 }
131
132 _DeviceManager::~_DeviceManager()
133 {
134         if(isInitBluetooth == true)
135         {
136                 int btResult = 0;
137                 SysLog(NID_SYS, "Bluetooth headset event is released.");
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         if(__pDeviceManager == null)
155         {
156                 __pDeviceManager = new (std::nothrow) _DeviceManager();
157         }
158         return __pDeviceManager;
159 }
160
161 result
162 _DeviceManager::InitializeDevice(void)
163 {
164         result r = E_SUCCESS;
165
166         if(isInitBluetooth == false)
167         {
168                 SysLog(NID_SYS, "Bluetooth headset event is reserved.");
169
170                 int btResult = 0;
171                 btResult = bt_initialize();
172
173                 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth module");
174
175                 btResult = bt_audio_initialize();
176                 SysTryReturnResult(NID_SYS, btResult == BT_ERROR_NONE, E_SYSTEM, "It is failed to init bluetooth audio module");
177
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");
180
181                 isInitBluetooth = true;
182         }
183         media_key_reserve(app_media_key_handler, null);
184
185         return r;
186 }
187
188
189 result
190 _DeviceManager::DeinitializeDevice(void)
191 {
192         result r = E_SUCCESS;
193         media_key_release();
194         return r;
195 }
196
197
198 result
199 _DeviceManager::RegisterListner(IDeviceEventListener &listener)
200 {
201         __pDeviceManagerListener = &listener;
202         return E_SUCCESS;
203 }
204
205 result
206 _DeviceManager::UnregisterListner(IDeviceEventListener &listener)
207 {
208         __pDeviceManagerListener = null;
209         return E_SUCCESS;
210 }
211
212 IDeviceEventListener*
213 _DeviceManager::GetEventListener()
214 {
215         return __pDeviceManagerListener;
216 }
217
218 String
219 _DeviceManager::GetId(void)
220 {
221         return DEVICE_MANAGER_SERVICE_ID;
222 }
223
224 bool
225 _DeviceManager::GetBluetoothStatus(void)
226 {
227         return isBluetoothHeadSetConnected;
228 }
229 void
230 _DeviceManager::SetBluetoothStatus(bool status)
231 {
232         isBluetoothHeadSetConnected = status;
233 }
234
235 extern "C" _OSP_EXPORT_ _IDeviceManager* DeviceManager_CreateInstance()
236 {
237         return _DeviceManager::GetInstance();
238 }