2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSys_DeviceManagerService.cpp
20 * @brief This is the implementation file for _DeviceManagerService class.
24 #include <system/media_key.h>
25 #include <bluetooth.h>
27 #include <FBaseSysLog.h>
28 #include <FSys_DeviceManagerEventProvider.h>
30 #include "FApp_CommunicationDispatcher.h"
31 #include "FSys_DeviceManagerService.h"
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;
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";
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";
59 Tizen::System::_DeviceManagerService* Tizen::System::_DeviceManagerService::__pDeviceManagerService = null;
61 void bluetooth_connection_state_changed(int result, bool connected, const char* remote_address, bt_audio_profile_type_e type, void* user_data)
63 SysLog(NID_SYS, "Bluetooth headset connection event, %s,%d", remote_address, connected);
65 _DeviceManagerService* pDeviceManagerService = _DeviceManagerService::GetInstance();
67 if(pDeviceManagerService != null)
69 if(pDeviceManagerService->GetBluetoothStatus() != connected)
73 bt_event = BLUETOOTH_A2DP_CONNECTED;
77 bt_event = BLUETOOTH_A2DP_DISCONNECTED;
79 pDeviceManagerService->SendEvent(bt_event);
81 pDeviceManagerService->SetBluetoothStatus(connected);
85 void app_media_key_handler(media_key_e key, media_key_event_e status, void* pUserData)
88 SysLog(NID_SYS, "Bluetooth headset event is occured %d, %d", (int)key, (int)status);
89 _DeviceManagerService* pDeviceManagerService = _DeviceManagerService::GetInstance();
90 if(pDeviceManagerService != null)
92 if(status == MEDIA_KEY_STATUS_RELEASED)
97 event = BLUETOOTH_A2DP_PLAY;
100 event = BLUETOOTH_A2DP_STOP;
102 case MEDIA_KEY_PAUSE:
103 event = BLUETOOTH_A2DP_PAUSE;
105 case MEDIA_KEY_PREVIOUS:
106 event = BLUETOOTH_A2DP_BACKWARD;
109 event = BLUETOOTH_A2DP_FORWARD;
111 case MEDIA_KEY_FASTFORWARD:
112 event = BLUETOOTH_A2DP_FASTFORWARD;
114 case MEDIA_KEY_REWIND:
115 event = BLUETOOTH_A2DP_REWIND;
117 case MEDIA_KEY_UNKNOWN:
120 SysLog(NID_SYS, "default");
122 pDeviceManagerService->SendEvent(event);
127 _DeviceManagerService::_DeviceManagerService()
128 : _ICommunicationRequestListener()
129 , __pCommunicationDispatcher(null)
130 , isBluetoothHeadSetConnected(false)
132 result r = E_SUCCESS;
135 __pCommunicationDispatcher = _CommunicationDispatcher::GetInstance();
136 SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher initiate is failed");
138 r = __pCommunicationDispatcher->AddCommunicationEventListener(*this);
139 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to add event listener");
141 btResult = bt_initialize();
142 SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to init bluetooth module");
144 btResult = bt_audio_initialize();
145 SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to init bluetooth audio module");
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");
150 r = __interestedAppList.Construct();
151 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to initiate bluetooth headset application list");
156 _DeviceManagerService::~_DeviceManagerService()
158 result r = E_SUCCESS;
161 SysTryCatch(NID_SYS, __pCommunicationDispatcher != null, r = E_SYSTEM, E_SYSTEM, "_CommunicationDispatcher is not ready");
163 r = __pCommunicationDispatcher->RemoveCommunicationEventListener(*this);
164 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, E_SYSTEM, "fail to remove event listener");
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");
169 btResult = bt_audio_deinitialize();
170 SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to close bluetooth");
172 btResult = bt_deinitialize();
173 SysTryCatch(NID_SYS, btResult == BT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "Failed to init bluetooth module");
179 _DeviceManagerService*
180 _DeviceManagerService::GetInstance(void)
182 if(__pDeviceManagerService == null)
184 __pDeviceManagerService = new (std::nothrow) _DeviceManagerService();
186 return __pDeviceManagerService;
190 _DeviceManagerService::GetId(void)
192 return DEVICE_MANAGER_SERVICE_ID;
196 _DeviceManagerService::SendEvent(String event)
198 if(__interestedAppList.GetCount() > 0)
201 AppId* pAppId = null;
203 String serviceId(DEVICE_MANAGER_SERVICE_ID);
204 String commandId(DEVICE_MANAGER_COMMAND_EVENT);
205 String deviceId(DEVICE_MANAGER_BLUETOOTH);
206 String eventId(event);
214 for(count = 0 ; count < __interestedAppList.GetCount() ; count++)
216 pAppId = (AppId*)__interestedAppList.GetAt(count);
219 SysLogException(NID_SYS, E_SYSTEM, "fail to get appid from bluetooth headset app list");
223 __pCommunicationDispatcher->SendData(*pAppId, data);
224 SysLog(NID_SYS, "Bluetooth headset event is sended to %ls", pAppId->GetPointer());
230 SysLog(NID_SYS, "Bluetooth Headset Event is not required by any application");
235 _DeviceManagerService::OnBluetoothEventOccured(int code)
238 AppId* pAppId = null;
240 String command = DEVICE_MANAGER_COMMAND_EVENT;
241 String device = DEVICE_MANAGER_BLUETOOTH;
242 String event = L"event test";
244 result r = E_SUCCESS;
246 if(__interestedAppList.GetCount() == 0)
248 SysLog(NID_SYS, "Bluetooth Headset Event is not required by any application");
253 r = eventData.Construct();
254 SysTryCatch(NID_SYS, r == E_SUCCESS, , E_SYSTEM, "fail to create eventData, [%s] Propagated.", GetErrorMessage(r));
256 eventData.Add(command);
257 eventData.Add(device);
258 eventData.Add(event);
260 for(count = 0 ; count < __interestedAppList.GetCount() ; count++)
262 pAppId = (AppId*)__interestedAppList.GetAt(count);
265 SysLogException(NID_SYS, E_SYSTEM, "fail to get appid from bluetooth headset app list");
269 r = __pCommunicationDispatcher->SendData(*pAppId, eventData);
272 SysLogException(NID_SYS, E_SYSTEM, "fail to send bluetooth event data");
281 _DeviceManagerService::AddInterestedApp(AppId appId)
284 for(count = 0; count < __interestedAppList.GetCount(); count++)
286 AppId* pAppId = null;
288 pAppId = (AppId*)__interestedAppList.GetAt(count);
295 if(__interestedAppList.GetCount() == 0)
297 SysLog(NID_SYS, "Bluetooth headset event is reserved.");
298 media_key_reserve(app_media_key_handler, null);
301 AppId* pNewAppId = new AppId(appId);
302 __interestedAppList.Add(*pNewAppId);
307 _DeviceManagerService::RemoveInterestedApp(AppId appId)
310 for(count = 0; count < __interestedAppList.GetCount(); count++)
312 AppId* pAppId = null;
314 pAppId = (AppId*)__interestedAppList.GetAt(count);
317 __interestedAppList.RemoveAt(count, true);
319 if(__interestedAppList.GetCount() == 0)
321 SysLog(NID_SYS, "Bluetooth headset event is released.");
332 _DeviceManagerService::OnRequestOccured(AppId appId, ArrayList* request, ArrayList* response)
334 StringComparer strComparer;
336 result r = E_SUCCESS;
337 String* command = null;
338 String* device = null;
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");
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);
347 SysLog(NID_SYS, "command is %ls, device is %ls", command->GetPointer(), device->GetPointer());
349 strComparer.Compare(*command, DEVICE_MANAGER_COMMAND_OPEN, cmp);
352 strComparer.Compare(*device, DEVICE_MANAGER_BLUETOOTH, cmp);
355 SysLog(NID_SYS, "Bluetooth headset event is required");
356 AddInterestedApp(appId);
360 strComparer.Compare(*command, DEVICE_MANAGER_COMMAND_CLOSE, cmp);
363 strComparer.Compare(*device, DEVICE_MANAGER_BLUETOOTH, cmp);
366 RemoveInterestedApp(appId);
370 strComparer.Compare(*command, DEVICE_MANAGER_COMMAND_STATUS, cmp);
373 strComparer.Compare(*device, DEVICE_MANAGER_BLUETOOTH, cmp);
377 SysLog(NID_SYS, "Bluetooth headset status is %d", isBluetoothHeadSetConnected);
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)
386 status = new (std::nothrow) String(BLUETOOTH_A2DP_CONNECTED);
390 status = new (std::nothrow) String(BLUETOOTH_A2DP_DISCONNECTED);
392 response->Add(*serviceId);
393 response->Add(*commandId);
394 response->Add(*deviceId);
395 response->Add(*status);
405 _DeviceManagerService::OnApplicationTerminated(const AppId& appId, _AppType type)
407 RemoveInterestedApp(appId);
411 _DeviceManagerService::GetBluetoothStatus(void)
413 return isBluetoothHeadSetConnected;
416 _DeviceManagerService::SetBluetoothStatus(bool status)
418 isBluetoothHeadSetConnected = status;