Tizen 2.1 base
[platform/framework/native/app-service.git] / src / FSys_DeviceManagerService.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FSys_DeviceManagerService.cpp
20  * @brief               This is the implementation file for _DeviceManagerService class.
21  */
22
23 #include <new>
24 #include <system/media_key.h>
25 #include <bluetooth.h>
26
27 #include <FBaseSysLog.h>
28 #include <FSys_DeviceManagerEventProvider.h>
29
30 #include "FApp_CommunicationDispatcher.h"
31 #include "FSys_DeviceManagerService.h"
32
33 using namespace Tizen::App;
34 using namespace Tizen::Io;
35 using namespace Tizen::System;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38
39 namespace {
40         const String DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
41         const String DEVICE_MANAGER_COMMAND_OPEN = L"osp.devicemanager.command.open";
42         const String DEVICE_MANAGER_COMMAND_CLOSE = L"osp.devicemanager.command.close";
43         const String DEVICE_MANAGER_COMMAND_STATUS= L"osp.devicemanager.command.status";
44         const String DEVICE_MANAGER_COMMAND_EVENT = L"osp.devicemanager.command.event";
45         const String DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
46
47         const String BLUETOOTH_A2DP_CONNECTED = L"Connected";
48         const String BLUETOOTH_A2DP_DISCONNECTED = L"Disconnected";
49         const String BLUETOOTH_A2DP_PLAY = L"Play";
50         const String BLUETOOTH_A2DP_STOP = L"Stop";
51         const String BLUETOOTH_A2DP_PAUSE = L"Pause";
52         const String BLUETOOTH_A2DP_RESUME = L"Resume";
53         const String BLUETOOTH_A2DP_FORWARD = L"Forward";
54         const String BLUETOOTH_A2DP_FASTFORWARD = L"FastForward";
55         const String BLUETOOTH_A2DP_BACKWARD = L"Backward";
56         const String BLUETOOTH_A2DP_REWIND = L"Rewind";
57 }
58
59 Tizen::System::_DeviceManagerService* Tizen::System::_DeviceManagerService::__pDeviceManagerService = null;
60
61 void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
62 {
63         SysLog(NID_SYS, "Bluetooth headset connection event, %s,%d", remote_address, connected);
64         String bt_event;
65         _DeviceManagerService* pDeviceManagerService = _DeviceManagerService::GetInstance();
66
67         if(pDeviceManagerService != null)
68         {
69                 if(pDeviceManagerService->GetBluetoothStatus() != connected)
70                 {
71                         if(connected == true)
72                         {
73                                 bt_event = BLUETOOTH_A2DP_CONNECTED;
74                         }
75                         else
76                         {
77                                 bt_event = BLUETOOTH_A2DP_DISCONNECTED;
78                         }
79                         pDeviceManagerService->SendEvent(bt_event);
80                 }
81                 pDeviceManagerService->SetBluetoothStatus(connected);
82         }
83 }
84
85 void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
86 {
87         String event;
88         SysLog(NID_SYS, "Bluetooth headset event is occured %d, %d", (int)key, (int)status);
89         _DeviceManagerService* pDeviceManagerService = _DeviceManagerService::GetInstance();
90         if(pDeviceManagerService != null)
91         {
92                 if(status == MEDIA_KEY_STATUS_RELEASED)
93                 {
94                         switch(key)
95                         {
96                         case MEDIA_KEY_PLAY:
97                                 event = BLUETOOTH_A2DP_PLAY;
98                                 break;
99                         case MEDIA_KEY_STOP:
100                                 event = BLUETOOTH_A2DP_STOP;
101                                 break;
102                         case MEDIA_KEY_PAUSE:
103                                 event = BLUETOOTH_A2DP_PAUSE;
104                                 break;
105                         case MEDIA_KEY_PREVIOUS:
106                                 event = BLUETOOTH_A2DP_BACKWARD;
107                                 break;
108                         case MEDIA_KEY_NEXT:
109                                 event = BLUETOOTH_A2DP_FORWARD;
110                                 break;
111                         case MEDIA_KEY_FASTFORWARD:
112                                 event = BLUETOOTH_A2DP_FASTFORWARD;
113                                 break;
114                         case MEDIA_KEY_REWIND:
115                                 event = BLUETOOTH_A2DP_REWIND;
116                                 break;
117                         case MEDIA_KEY_UNKNOWN:
118                                 break;
119                         default:
120                                 SysLog(NID_SYS, "default");
121                         }
122                         pDeviceManagerService->SendEvent(event);
123                 }
124         }
125 }
126
127 _DeviceManagerService::_DeviceManagerService()
128         : _ICommunicationRequestListener()
129         , __pCommunicationDispatcher(null)
130         , isBluetoothHeadSetConnected(false)
131 {
132         result r = E_SUCCESS;
133         int btResult = 0;
134
135         __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
136         SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher initiate is failed");
137
138         r = __pCommunicationDispatcher->AddCommunicationEventListener(*this);
139         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to add event listener");
140
141         btResult = bt_initialize();
142         SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to init bluetooth module");
143
144         btResult = bt_audio_initialize();
145         SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to init bluetooth audio module");
146
147         btResult = bt_audio_set_connection_state_changed_cb(bluetooth_connection_state_changed, null);
148         SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to register bluetooth audio event");
149
150         r = __interestedAppList.Construct();
151         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to initiate bluetooth headset application list");
152 CATCH:
153         SetLastResult(r);
154 }
155
156 _DeviceManagerService::~_DeviceManagerService()
157 {
158         result r = E_SUCCESS;
159         int btResult = 0;
160
161         SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher is not ready");
162
163         r = __pCommunicationDispatcher->RemoveCommunicationEventListener(*this);
164         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to remove event listener");
165
166         btResult = bt_audio_unset_connection_state_changed_cb();
167         SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to unregister bluetooth headset connection event");
168
169         btResult = bt_audio_deinitialize();
170         SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to close bluetooth");
171
172         btResult = bt_deinitialize();
173         SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to init bluetooth module");
174
175 CATCH:
176         SetLastResult(r);
177 }
178
179 _DeviceManagerService*
180 _DeviceManagerService::GetInstance(void)
181 {
182         if(__pDeviceManagerService == null)
183         {
184                 __pDeviceManagerService = new (std::nothrow) _DeviceManagerService();
185         }
186         return __pDeviceManagerService;
187 }
188
189 String
190 _DeviceManagerService::GetId(void)
191 {
192         return DEVICE_MANAGER_SERVICE_ID;
193 }
194
195 void
196 _DeviceManagerService::SendEvent(String event)
197 {
198         if(__interestedAppList.GetCount() > 0)
199         {
200                 int count = 0;
201                 AppId* pAppId = null;
202                 ArrayList data;
203                 String serviceId(DEVICE_MANAGER_SERVICE_ID);
204                 String commandId(DEVICE_MANAGER_COMMAND_EVENT);
205                 String deviceId(DEVICE_MANAGER_BLUETOOTH);
206                 String eventId(event);
207
208                 data.Construct();
209                 data.Add(serviceId);
210                 data.Add(commandId);
211                 data.Add(deviceId);
212                 data.Add(eventId);
213
214                 for(count = 0 ; count < __interestedAppList.GetCount() ; count++)
215                 {
216                         pAppId = (AppId*)__interestedAppList.GetAt(count);
217                         if(pAppId == null)
218                         {
219                                 SysLogException(NID_SYS, E_SYSTEM, "fail to get appid from bluetooth headset app list");
220                                 return;
221                         }
222
223                         __pCommunicationDispatcher->SendData(*pAppId, data);
224                         SysLog(NID_SYS, "Bluetooth headset event is sended to %ls", pAppId->GetPointer());
225                 }
226
227         }
228         else
229         {
230                 SysLog(NID_SYS, "Bluetooth Headset Event is not required by any application");
231         }
232 }
233
234 void
235 _DeviceManagerService::OnBluetoothEventOccured(int code)
236 {
237         int count = 0;
238         AppId* pAppId = null;
239
240         String command = DEVICE_MANAGER_COMMAND_EVENT;
241         String device = DEVICE_MANAGER_BLUETOOTH;
242         String event = L"event test";
243
244         result r = E_SUCCESS;
245
246         if(__interestedAppList.GetCount() == 0)
247         {
248                 SysLog(NID_SYS, "Bluetooth Headset Event is not required by any application");
249                 return;
250         }
251
252         ArrayList eventData;
253         r = eventData.Construct();
254         SysTryCatch(NID_SYS, r == E_SUCCESS, , E_SYSTEM, "fail to create eventData, [%s] Propagated.", GetErrorMessage(r));
255
256         eventData.Add(command);
257         eventData.Add(device);
258         eventData.Add(event);
259
260         for(count = 0 ; count < __interestedAppList.GetCount() ; count++)
261         {
262                 pAppId = (AppId*)__interestedAppList.GetAt(count);
263                 if(pAppId == null)
264                 {
265                         SysLogException(NID_SYS, E_SYSTEM, "fail to get appid from bluetooth headset app list");
266                         return;
267                 }
268
269                 r = __pCommunicationDispatcher->SendData(*pAppId, eventData);
270                 if(r != E_SUCCESS)
271                 {
272                         SysLogException(NID_SYS, E_SYSTEM, "fail to send bluetooth event data");
273                 }
274         }
275
276 CATCH:
277         SetLastResult(r);
278 }
279
280 void
281 _DeviceManagerService::AddInterestedApp(AppId appId)
282 {
283         int count = 0;
284         for(count = 0; count < __interestedAppList.GetCount(); count++)
285         {
286                 AppId* pAppId = null;
287
288                 pAppId = (AppId*)__interestedAppList.GetAt(count);
289                 if(*pAppId == appId)
290                 {
291                         return;
292                 }
293         }
294
295         if(__interestedAppList.GetCount() == 0)
296         {
297                 SysLog(NID_SYS, "Bluetooth headset event is reserved.");
298                 media_key_reserve(app_media_key_handler, null);
299         }
300
301         AppId* pNewAppId = new AppId(appId);
302         __interestedAppList.Add(*pNewAppId);
303
304 }
305
306 void
307 _DeviceManagerService::RemoveInterestedApp(AppId appId)
308 {
309         int count = 0;
310         for(count = 0; count < __interestedAppList.GetCount(); count++)
311         {
312                 AppId* pAppId = null;
313
314                 pAppId = (AppId*)__interestedAppList.GetAt(count);
315                 if(*pAppId == appId)
316                 {
317                         __interestedAppList.RemoveAt(count, true);
318
319                         if(__interestedAppList.GetCount() == 0)
320                         {
321                                 SysLog(NID_SYS, "Bluetooth headset event is released.");
322                                 media_key_release();
323                         }
324
325                         return;
326                 }
327         }
328
329 }
330
331 void
332 _DeviceManagerService::OnRequestOccured(AppId appId, ArrayList* request, ArrayList* response)
333 {
334         StringComparer strComparer;
335         int cmp = 0;
336         result r = E_SUCCESS;
337         String* command = null;
338         String* device = null;
339
340         SysLog(NID_APP, "Request is forwarded to _DeviceManagerService");
341         SysTryCatch(NID_SYS, request != null && response != null, r = E_INVALID_ARG, E_INVALID_ARG, "Parameters are null");
342
343         command = (String*)request->GetAt(1);
344         device = (String*)request->GetAt(2);
345         SysTryCatch(NID_SYS, command!= null && device != null, r = E_INVALID_ARG, E_INVALID_ARG, "Parameters has no command %x, %x", command, device);
346
347         SysLog(NID_SYS, "command is %ls, device is %ls", command->GetPointer(), device->GetPointer());
348
349         strComparer.Compare(*command, DEVICE_MANAGER_COMMAND_OPEN, cmp);
350         if(cmp == 0)
351         {
352                 strComparer.Compare(*device, DEVICE_MANAGER_BLUETOOTH, cmp);
353                 if(cmp == 0)
354                 {
355                         SysLog(NID_SYS, "Bluetooth headset event is required");
356                         AddInterestedApp(appId);
357                 }
358         }
359
360         strComparer.Compare(*command, DEVICE_MANAGER_COMMAND_CLOSE, cmp);
361         if(cmp == 0)
362         {
363                 strComparer.Compare(*device, DEVICE_MANAGER_BLUETOOTH, cmp);
364                 if(cmp == 0)
365                 {
366                         RemoveInterestedApp(appId);
367                 }
368         }
369
370         strComparer.Compare(*command, DEVICE_MANAGER_COMMAND_STATUS, cmp);
371         if(cmp == 0)
372         {
373                 strComparer.Compare(*device, DEVICE_MANAGER_BLUETOOTH, cmp);
374                 if(cmp == 0)
375                 {
376
377                         SysLog(NID_SYS, "Bluetooth headset status is %d", isBluetoothHeadSetConnected);
378                         if(response != null)
379                         {
380                                 String* serviceId = new (std::nothrow) String(DEVICE_MANAGER_SERVICE_ID);
381                                 String* commandId = new (std::nothrow) String(DEVICE_MANAGER_COMMAND_STATUS);
382                                 String* deviceId = new (std::nothrow) String(DEVICE_MANAGER_BLUETOOTH);
383                                 String* status = null;
384                                 if(isBluetoothHeadSetConnected == true)
385                                 {
386                                         status = new (std::nothrow) String(BLUETOOTH_A2DP_CONNECTED);
387                                 }
388                                 else
389                                 {
390                                         status = new (std::nothrow) String(BLUETOOTH_A2DP_DISCONNECTED);
391                                 }
392                                 response->Add(*serviceId);
393                                 response->Add(*commandId);
394                                 response->Add(*deviceId);
395                                 response->Add(*status);
396                         }
397                 }
398         }
399
400 CATCH:
401         SetLastResult(r);
402 }
403
404 void
405 _DeviceManagerService::OnApplicationTerminated(const AppId& appId, _AppType type)
406 {
407         RemoveInterestedApp(appId);
408 }
409
410 bool
411 _DeviceManagerService::GetBluetoothStatus(void)
412 {
413         return isBluetoothHeadSetConnected;
414 }
415 void
416 _DeviceManagerService::SetBluetoothStatus(bool status)
417 {
418         isBluetoothHeadSetConnected = status;
419 }
420