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_DeviceManagerImpl.cpp
20 * @brief This is the implementation file for _DeviceManagerImpl class.
24 #include <unique_ptr.h>
25 #include <system_info.h>
26 #include <runtime_info.h>
30 #include <FBaseColArrayList.h>
31 #include <FBaseRtEvent.h>
32 #include <FBaseRtIEventArg.h>
33 #include <FBaseStringComparer.h>
34 #include <FBaseSysLog.h>
35 #include <FSysIDeviceEventListener.h>
37 #include <FApp_AppInfo.h>
38 #include <FBase_NativeError.h>
39 #include <FIo_AppServiceIpcMessages.h>
40 #include <FIo_IpcClient.h>
41 #include <FSys_SystemInfoImpl.h>
42 #include <FSys_PowerManagerImpl.h>
44 #include "FSys_CommunicationDispatcherClient.h"
45 #include "FSys_DeviceManagerImpl.h"
47 #define VCONFKEY_APPSERVICE_MMC_STATUS "memory/appservice/mmc"
49 using namespace Tizen::App;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Collection;
52 using namespace Tizen::Base::Runtime;
53 using namespace Tizen::Io;
55 namespace Tizen { namespace System
58 static const wchar_t* _DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
59 static const wchar_t* _DEVICE_MANAGER_COMMAND_OPEN = L"osp.devicemanager.command.open";
60 static const wchar_t* _DEVICE_MANAGER_COMMAND_CLOSE = L"osp.devicemanager.command.close";
61 static const wchar_t* _DEVICE_MANAGER_COMMAND_STATUS = L"osp.devicemanager.command.status";
62 static const wchar_t* _DEVICE_MANAGER_COMMAND_EVENT = L"osp.devicemanager.command.event";
63 static const wchar_t* _DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
65 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID = 0;
66 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID = 1;
67 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID = 2;
68 const int _OSP_APP_SERVICE_IPC_MESSAGE_DATA = 3;
70 const int _DEVICE_MANAGER_BLUETOOTH_HEADSET = 0x01;
71 const int _DEVICE_MANAGER_CHARGER = 0x02;
72 const int _DEVICE_MANAGER_USB_CLIENT = 0x04;
73 const int _DEVICE_MANAGER_TV_OUT = 0x08;
74 const int _DEVICE_MANAGER_WIRED_HEADSET = 0x10;
75 const int _DEVICE_MANAGER_WIRED_HEADPHONE = 0x20;
76 const int _DEVICE_MANAGER_STORAGECARD = 0x40;
77 const int _DEVICE_MANAGER_KEYBOARD = 0x80;
79 static const wchar_t* _DEVICE_MANAGER_STATE_INSERTED = L"Inserted";
80 static const wchar_t* _DEVICE_MANAGER_STATE_REMOVED = L"Removed";
81 static const wchar_t* _DEVICE_MANAGER_STATE_MOUNTED = L"Mounted";
82 static const wchar_t* _DEVICE_MANAGER_STATE_UNMOUNTED = L"Unmounted";
83 static const wchar_t* _DEVICE_MANAGER_STATE_OPENED = L"Opened";
84 static const wchar_t* _DEVICE_MANAGER_STATE_CLOSED = L"Closed";
86 static const wchar_t* _SYSTEM_INFO_NETWORK_BLUETOOTH = L"http://tizen.org/feature/network.bluetooth";
87 static const wchar_t* _SYSTEM_INFO_INPUT_KEYBOARD = L"http://tizen.org/feature/input.keyboard";
88 static const wchar_t* _SYSTEM_INFO_TVOUT_SUPPORTED = L"http://tizen.org/feature/screen.output.rca";
90 _DeviceManagerImpl* _DeviceManagerImpl::__pDeviceManagerImpl = null;
93 class _DeviceManagerEventArg : public IEventArg
96 _DeviceManagerEventArg()
99 DeviceType deviceType;
103 class _DeviceManagerEvent : public Event
106 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
108 IDeviceEventListener* pListener = dynamic_cast<IDeviceEventListener*> (&listener);
109 const _DeviceManagerEventArg* pArg = dynamic_cast<const _DeviceManagerEventArg*>(&arg);
111 if(pListener == null || pArg == null)
113 SysLogException(NID_SYS, E_SYSTEM, "listener or arg is null.");
117 pListener->OnDeviceStateChanged(pArg->deviceType, pArg->state);
123 _DeviceManagerImpl::OnDeviceStateChanged(runtime_info_key_e key, void* pData)
125 if (__pDeviceManagerImpl != null)
127 __pDeviceManagerImpl->SendEvent(key);
132 _DeviceManagerImpl::InitDeviceManagerImpl(void)
134 static _DeviceManagerImpl deviceManagerImpl;
135 __pDeviceManagerImpl = &deviceManagerImpl;
138 _DeviceManagerImpl::GetInstance(void)
140 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
141 if(__pDeviceManagerImpl == null)
143 pthread_once(&once_block, InitDeviceManagerImpl);
145 return __pDeviceManagerImpl;
148 _DeviceManagerImpl::_DeviceManagerImpl()
150 , __headSetType(DEVICE_TYPE_WIRED_HEADPHONE)
151 , __bluetoothReferenceCount(0)
153 result r = E_SUCCESS;
154 int headsetState = 0;
156 static String DEVICE_MANAGER_SERVICE_ID(_DEVICE_MANAGER_SERVICE_ID);
158 _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
159 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
161 r = pCommunicationDispatcherClient->RegisterCommunicationListener(DEVICE_MANAGER_SERVICE_ID, *this);
162 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
164 __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
166 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED, OnDeviceStateChanged, null);
167 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register USB event");
169 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, OnDeviceStateChanged, null);
170 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register DEVICE_TYPE_CHARGER event");
172 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, OnDeviceStateChanged, null);
173 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register TV out event");
175 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, OnDeviceStateChanged, null);
176 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register audio jack event");
178 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, OnDeviceStateChanged, null);
179 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register sliding keyboard event");
181 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &headsetState);
182 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to get audio jack status");
184 if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
186 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
188 else if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
190 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
193 if (!_AppInfo::IsOspCompat())
195 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack, null);
199 ret = vconf_notify_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback, null);
201 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to register MMC event callback.");
203 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack, null);
204 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to register HDMI event");
206 __deviceEventList.Construct();
212 _DeviceManagerImpl::~_DeviceManagerImpl()
214 result r = E_SUCCESS;
216 _CommunicationDispatcherClient* pCommunicationDispatcherClient = null;
217 String key(_DEVICE_MANAGER_SERVICE_ID);
219 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED);
220 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister USB event");
222 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED);
223 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister TV out event");
225 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS);
226 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister audio jack event");
228 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED);
229 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister sliding keyboard event");
231 if (!_AppInfo::IsOspCompat())
233 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack);
237 ret = vconf_ignore_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback);
239 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister MMC event");
241 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack);
242 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister HDMI event");
245 pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
246 SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
248 r = pCommunicationDispatcherClient->UnregisterCommunicationListener(key);
249 SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
258 _DeviceManagerImpl::DeviceEventVConfCallBack(keynode_t* node, void* userData)
264 if (strcmp(VCONFKEY_SYSMAN_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
266 SysLog(NID_SYS, "MMC callback is occured");
267 if (__pDeviceManagerImpl != null)
269 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
272 event = _DEVICE_MANAGER_STATE_MOUNTED;
276 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
278 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
281 else if(strcmp(VCONFKEY_SYSMAN_HDMI, vconf_keynode_get_name(node)) == 0)
283 if (__pDeviceManagerImpl != null)
285 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
288 event = _DEVICE_MANAGER_STATE_INSERTED;
292 event = _DEVICE_MANAGER_STATE_REMOVED;
294 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_HDMI, event);
301 _DeviceManagerImpl::MmcEventVconfCallback(keynode_t* node, void* userData)
303 if (strcmp(VCONFKEY_APPSERVICE_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
305 SysLog(NID_SYS, "MMC event callback is called.");
306 if (__pDeviceManagerImpl != NULL)
310 int ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
313 SysLogException(NID_SYS, E_SYSTEM, "VCONFKEY_APPSERVICE_MMC_STATUS is error");
317 event = _DEVICE_MANAGER_STATE_MOUNTED;
321 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
323 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
329 _DeviceManagerImpl::SendEvent(DeviceType deviceType, String& state)
331 std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
333 if(__deviceEventList.GetCount() > 0)
335 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
336 SysTryReturnVoidResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
338 while(pEnumerator->MoveNext() == E_SUCCESS)
340 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
341 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
342 SysTryReturnVoidResult(NID_SYS, pDeviceEventListenerContainer != null,E_SYSTEM, "Container is empty");
346 case DEVICE_TYPE_BLUETOOTH_HEADSET:
348 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
354 case DEVICE_TYPE_CHARGER:
356 if(pDeviceEventListenerContainer->__charger == false)
362 case DEVICE_TYPE_USB_CLIENT:
364 if(pDeviceEventListenerContainer->__usbClient == false)
370 case DEVICE_TYPE_TV_OUT:
372 if(pDeviceEventListenerContainer->__tvOut == false)
378 case DEVICE_TYPE_WIRED_HEADSET:
380 if(pDeviceEventListenerContainer->__wiredHeadset == false)
386 case DEVICE_TYPE_WIRED_HEADPHONE:
388 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
394 case DEVICE_TYPE_STORAGE_CARD:
396 if(pDeviceEventListenerContainer->__storageCard == false)
402 case DEVICE_TYPE_KEYBOARD:
404 if(pDeviceEventListenerContainer->__keyboard == false)
410 case DEVICE_TYPE_HDMI:
412 if(pDeviceEventListenerContainer->__hdmi == false)
421 std::unique_ptr<_DeviceManagerEventArg> pDeviceManagerEventArg(new (std::nothrow) _DeviceManagerEventArg());
422 if(pDeviceManagerEventArg == null)
424 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of DeviceManagerEventArg");
427 pDeviceManagerEventArg->deviceType = deviceType;
428 pDeviceManagerEventArg->state = state;
430 if(pDeviceEventListenerContainer->__pEvent != null)
432 pDeviceEventListenerContainer->__pEvent->Fire(*(pDeviceManagerEventArg.release()));
439 _DeviceManagerImpl::RequireBluetoothEvent(void)
441 result r = E_SUCCESS;
442 std::unique_ptr<IoService_Request> pRequest(null);
444 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "Ipc Client is not ready");
446 __bluetoothReferenceCount ++;
448 if(__bluetoothReferenceCount == 1)
450 ArrayList requestMessages;
451 ArrayList responseMessages;
453 r = requestMessages.Construct();
454 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
455 r = responseMessages.Construct();
456 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
458 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
459 String commandId = _DEVICE_MANAGER_COMMAND_OPEN;
460 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
462 r = requestMessages.Add(serviceId);
463 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
464 r = requestMessages.Add(commandId);
465 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
466 r = requestMessages.Add(deviceId);
468 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add device id (bluetooth)");
470 pRequest.reset(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
471 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send bluetooth event subscribe by IPC");
473 r = __pIpcClient->SendRequest(pRequest.get());
474 SysTryReturnResult(NID_SYS, r == E_SUCCESS,E_SYSTEM, "It is failed to add bluetooth id");
480 _DeviceManagerImpl::ReleaseBluetoothEvent(void)
482 result r = E_SUCCESS;
483 std::unique_ptr<IoService_Request> pRequest(null);
484 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "Ipc Client is not ready");
486 __bluetoothReferenceCount --;
487 SysTryReturnResult(NID_SYS, __bluetoothReferenceCount >= 0, E_SYSTEM, "Fail to manage reference count");
489 if(__bluetoothReferenceCount == 0)
491 ArrayList requestMessages;
492 ArrayList responseMessages;
494 r = requestMessages.Construct();
495 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
496 r = responseMessages.Construct();
497 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
499 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
500 String commandId = _DEVICE_MANAGER_COMMAND_CLOSE;
501 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
503 r = requestMessages.Add(serviceId);
504 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
505 r = requestMessages.Add(commandId);
506 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
507 r = requestMessages.Add(deviceId);
508 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add bluetooth id");
510 pRequest.reset(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
511 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send bluetooth event subscribe by IPC");
513 r = __pIpcClient->SendRequest(pRequest.get());
514 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add bluetooth id");
520 _DeviceManagerImpl::AddOnExistedListener(DeviceType deviceType, const IDeviceEventListener* pListener)
522 result r = E_OBJ_NOT_FOUND;
523 std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
525 if(__deviceEventList.GetCount() > 0)
527 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
528 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
530 while(pEnumerator->MoveNext() == E_SUCCESS)
532 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
533 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
534 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
535 if(pDeviceEventListenerContainer->__pListener == pListener)
539 case DEVICE_TYPE_BLUETOOTH_HEADSET:
541 if(pDeviceEventListenerContainer->__bluetoothHeadset == true)
543 r = E_OBJ_ALREADY_EXIST;
547 pDeviceEventListenerContainer->__bluetoothHeadset = true;
548 RequireBluetoothEvent();
553 case DEVICE_TYPE_CHARGER:
555 if(pDeviceEventListenerContainer->__charger == true)
557 r = E_OBJ_ALREADY_EXIST;
561 pDeviceEventListenerContainer->__charger = true;
566 case DEVICE_TYPE_USB_CLIENT:
568 if(pDeviceEventListenerContainer->__usbClient == true)
570 r = E_OBJ_ALREADY_EXIST;
574 pDeviceEventListenerContainer->__usbClient = true;
579 case DEVICE_TYPE_TV_OUT:
582 _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
586 if(!_AppInfo::IsOspCompat())
588 r = E_UNSUPPORTED_OPERATION;
592 r = E_DEVICE_UNAVAILABLE;
597 if(pDeviceEventListenerContainer->__tvOut == true)
599 r = E_OBJ_ALREADY_EXIST;
603 pDeviceEventListenerContainer->__tvOut = true;
608 case DEVICE_TYPE_WIRED_HEADSET:
610 if(pDeviceEventListenerContainer->__wiredHeadset == true)
612 r = E_OBJ_ALREADY_EXIST;
616 pDeviceEventListenerContainer->__wiredHeadset = true;
621 case DEVICE_TYPE_WIRED_HEADPHONE:
623 if(pDeviceEventListenerContainer->__wiredHeadphone == true)
625 r = E_OBJ_ALREADY_EXIST;
629 pDeviceEventListenerContainer->__wiredHeadphone = true;
634 case DEVICE_TYPE_STORAGE_CARD:
636 if(pDeviceEventListenerContainer->__storageCard == true)
638 r = E_OBJ_ALREADY_EXIST;
642 pDeviceEventListenerContainer->__storageCard = true;
647 case DEVICE_TYPE_KEYBOARD:
649 bool keyboard = false;
650 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
652 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
654 if (keyboard == false)
656 if(!_AppInfo::IsOspCompat())
658 return E_UNSUPPORTED_OPERATION;
662 return E_DEVICE_UNAVAILABLE;
666 if(pDeviceEventListenerContainer->__keyboard == true)
668 r = E_OBJ_ALREADY_EXIST;
672 pDeviceEventListenerContainer->__keyboard = true;
677 case DEVICE_TYPE_HDMI:
681 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
682 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
685 return E_UNSUPPORTED_OPERATION;
687 if(pDeviceEventListenerContainer->__hdmi == true)
689 r = E_OBJ_ALREADY_EXIST;
693 pDeviceEventListenerContainer->__hdmi = true;
706 _DeviceManagerImpl::AddDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
708 SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
709 SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
710 result r = E_SUCCESS;
712 if(deviceType == DEVICE_TYPE_TV_OUT)
715 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
716 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
720 if(!_AppInfo::IsOspCompat())
722 r = E_UNSUPPORTED_OPERATION;
726 r = E_DEVICE_UNAVAILABLE;
731 else if(deviceType == DEVICE_TYPE_KEYBOARD)
733 bool keyboard = false;
734 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
736 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
738 if (keyboard == false)
740 if(!_AppInfo::IsOspCompat())
742 r = E_UNSUPPORTED_OPERATION;
746 r = E_DEVICE_UNAVAILABLE;
751 else if(deviceType == DEVICE_TYPE_HDMI)
755 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
756 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
759 return E_UNSUPPORTED_OPERATION;
763 r = AddOnExistedListener(deviceType, pListener);
765 if(r == E_OBJ_NOT_FOUND)
767 std::unique_ptr<_DeviceEventListenerContainer> pDeviceEventContainer(new (std::nothrow) _DeviceEventListenerContainer());
768 SysTryReturnResult(NID_SYS, pDeviceEventContainer != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceEventListenerContainer");
770 std::unique_ptr<_DeviceManagerEvent> pDeviceManagerEvent(new (std::nothrow) _DeviceManagerEvent());
771 SysTryReturnResult(NID_SYS, pDeviceManagerEvent != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceManagerEvent");
773 pDeviceManagerEvent->AddListener(*pListener);
774 pDeviceEventContainer->__pListener = pListener;
775 pDeviceEventContainer->__pEvent = pDeviceManagerEvent.release();
779 case DEVICE_TYPE_BLUETOOTH_HEADSET:
781 pDeviceEventContainer->__bluetoothHeadset = true;
782 RequireBluetoothEvent();
785 case DEVICE_TYPE_CHARGER:
787 pDeviceEventContainer->__charger = true;
790 case DEVICE_TYPE_USB_CLIENT:
792 pDeviceEventContainer->__usbClient = true;
795 case DEVICE_TYPE_TV_OUT:
798 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
799 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
803 if(!_AppInfo::IsOspCompat())
805 return E_UNSUPPORTED_OPERATION;
809 return E_DEVICE_UNAVAILABLE;
813 pDeviceEventContainer->__tvOut = true;
816 case DEVICE_TYPE_WIRED_HEADSET:
818 pDeviceEventContainer->__wiredHeadset = true;
821 case DEVICE_TYPE_WIRED_HEADPHONE:
823 pDeviceEventContainer->__wiredHeadphone = true;
826 case DEVICE_TYPE_STORAGE_CARD:
828 pDeviceEventContainer->__storageCard = true;
831 case DEVICE_TYPE_KEYBOARD:
833 bool keyboard = false;
834 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
836 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
838 if (keyboard == false)
840 if(!_AppInfo::IsOspCompat())
842 return E_UNSUPPORTED_OPERATION;
846 return E_DEVICE_UNAVAILABLE;
850 pDeviceEventContainer->__keyboard = true;
853 case DEVICE_TYPE_HDMI:
857 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
858 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
861 return E_UNSUPPORTED_OPERATION;
863 pDeviceEventContainer->__hdmi = true;
868 __deviceEventList.Add(pDeviceEventContainer.release());
875 _DeviceManagerImpl::RemoveDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
877 result r = E_SUCCESS;
879 SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
880 SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
881 SysTryReturnResult(NID_SYS, __deviceEventList.GetCount() > 0, E_INVALID_ARG, "There is no registered listener.");
883 std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(__deviceEventList.GetEnumeratorN());
885 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
887 if(deviceType == DEVICE_TYPE_TV_OUT)
890 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
891 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
895 if(!_AppInfo::IsOspCompat())
897 r = E_UNSUPPORTED_OPERATION;
901 r = E_DEVICE_UNAVAILABLE;
906 else if(deviceType == DEVICE_TYPE_KEYBOARD)
908 bool keyboard = false;
909 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
911 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
913 if (keyboard == false)
915 if(!_AppInfo::IsOspCompat())
917 r = E_UNSUPPORTED_OPERATION;
921 r = E_DEVICE_UNAVAILABLE;
925 else if(deviceType == DEVICE_TYPE_HDMI)
929 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
930 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
933 return E_UNSUPPORTED_OPERATION;
937 while(pEnumerator->MoveNext() == E_SUCCESS)
939 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
940 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
941 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
943 if(pDeviceEventListenerContainer->__pListener == pListener)
947 case DEVICE_TYPE_BLUETOOTH_HEADSET:
949 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
951 return E_OBJ_NOT_FOUND;
953 pDeviceEventListenerContainer->__bluetoothHeadset = false;
954 ReleaseBluetoothEvent();
957 case DEVICE_TYPE_CHARGER:
959 if(pDeviceEventListenerContainer->__charger == false)
961 return E_OBJ_NOT_FOUND;
963 pDeviceEventListenerContainer->__charger = false;
966 case DEVICE_TYPE_USB_CLIENT:
968 if(pDeviceEventListenerContainer->__usbClient == false)
970 return E_OBJ_NOT_FOUND;
972 pDeviceEventListenerContainer->__usbClient = false;
975 case DEVICE_TYPE_TV_OUT:
977 if(pDeviceEventListenerContainer->__tvOut == false)
979 return E_OBJ_NOT_FOUND;
981 pDeviceEventListenerContainer->__tvOut = false;
984 case DEVICE_TYPE_WIRED_HEADSET:
986 if(pDeviceEventListenerContainer->__wiredHeadset == false)
988 return E_OBJ_NOT_FOUND;
990 pDeviceEventListenerContainer->__wiredHeadset = false;
993 case DEVICE_TYPE_WIRED_HEADPHONE:
995 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
997 return E_OBJ_NOT_FOUND;
999 pDeviceEventListenerContainer->__wiredHeadphone = false;
1002 case DEVICE_TYPE_STORAGE_CARD:
1004 if(pDeviceEventListenerContainer->__storageCard == false)
1006 return E_OBJ_NOT_FOUND;
1008 pDeviceEventListenerContainer->__storageCard = false;
1011 case DEVICE_TYPE_KEYBOARD:
1013 if(pDeviceEventListenerContainer->__keyboard == false)
1015 return E_OBJ_NOT_FOUND;
1017 pDeviceEventListenerContainer->__keyboard = false;
1020 case DEVICE_TYPE_HDMI:
1022 if(pDeviceEventListenerContainer->__hdmi == false)
1024 return E_OBJ_NOT_FOUND;
1026 pDeviceEventListenerContainer->__hdmi = false;
1031 if(pDeviceEventListenerContainer->IsInvalid() == true)
1033 __deviceEventList.Remove(pDeviceEventListenerContainer);
1034 delete pDeviceEventListenerContainer;
1039 return E_OBJ_NOT_FOUND;
1043 _DeviceManagerImpl::RemoveAllDeviceEventListeners(void)
1045 result r = E_SUCCESS;
1047 IEnumeratorT<_DeviceEventListenerContainer*>* pEnumerator = __deviceEventList.GetEnumeratorN();
1048 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
1050 while(pEnumerator->MoveNext() == E_SUCCESS)
1052 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
1053 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
1054 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
1055 __deviceEventList.Remove(pDeviceEventListenerContainer);
1056 delete pDeviceEventListenerContainer;
1060 __bluetoothReferenceCount = 1;
1062 ReleaseBluetoothEvent();
1067 _DeviceManagerImpl::SendEvent(runtime_info_key_e key)
1069 DeviceType type = DEVICE_TYPE_USB_CLIENT;
1076 case RUNTIME_INFO_KEY_USB_CONNECTED:
1078 flag = _DEVICE_MANAGER_USB_CLIENT;
1079 type = DEVICE_TYPE_USB_CLIENT;
1082 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &state);
1083 if (ret != RUNTIME_INFO_ERROR_NONE)
1085 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get usb state");
1091 value = _DEVICE_MANAGER_STATE_INSERTED;
1095 value = _DEVICE_MANAGER_STATE_REMOVED;
1099 case RUNTIME_INFO_KEY_CHARGER_CONNECTED:
1101 flag = _DEVICE_MANAGER_CHARGER;
1102 type = DEVICE_TYPE_CHARGER;
1105 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &state);
1106 if (ret != RUNTIME_INFO_ERROR_NONE)
1108 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get charger state");
1114 value = _DEVICE_MANAGER_STATE_INSERTED;
1118 value = _DEVICE_MANAGER_STATE_REMOVED;
1122 case RUNTIME_INFO_KEY_TV_OUT_CONNECTED:
1124 flag = _DEVICE_MANAGER_TV_OUT;
1125 type = DEVICE_TYPE_TV_OUT;
1128 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, &state);
1129 if (ret != RUNTIME_INFO_ERROR_NONE)
1131 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get tv connect state");
1137 value = _DEVICE_MANAGER_STATE_INSERTED;
1141 value = _DEVICE_MANAGER_STATE_REMOVED;
1145 case RUNTIME_INFO_KEY_AUDIO_JACK_STATUS:
1148 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &state);
1149 if (ret != RUNTIME_INFO_ERROR_NONE)
1151 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get audion jack state");
1155 if (state == RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED)
1157 if (__headSetType == DEVICE_TYPE_WIRED_HEADPHONE)
1159 flag = _DEVICE_MANAGER_WIRED_HEADPHONE;
1160 type = DEVICE_TYPE_WIRED_HEADPHONE;
1164 flag = _DEVICE_MANAGER_WIRED_HEADSET;
1165 type = DEVICE_TYPE_WIRED_HEADSET;
1168 value = _DEVICE_MANAGER_STATE_REMOVED;
1172 if (state == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
1174 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
1175 flag = _DEVICE_MANAGER_WIRED_HEADPHONE;
1176 type = DEVICE_TYPE_WIRED_HEADPHONE;
1180 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
1181 flag = _DEVICE_MANAGER_WIRED_HEADSET;
1182 type = DEVICE_TYPE_WIRED_HEADSET;
1184 value = _DEVICE_MANAGER_STATE_INSERTED;
1189 case RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED:
1191 flag = _DEVICE_MANAGER_KEYBOARD;
1192 type = DEVICE_TYPE_KEYBOARD;
1195 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, &state);
1196 if (ret != RUNTIME_INFO_ERROR_NONE)
1198 SysLogException(NID_SYS, E_SYSTEM, "It is failed to get keyboard state");
1204 value = _DEVICE_MANAGER_STATE_OPENED;
1208 value = _DEVICE_MANAGER_STATE_CLOSED;
1216 __pDeviceManagerImpl->SendEvent(type, value);
1221 _DeviceManagerImpl::GetState(DeviceType deviceType, String& state)
1223 result r = E_SUCCESS;
1227 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "GetDeviceState Failed");
1232 case DEVICE_TYPE_BLUETOOTH_HEADSET:
1234 bool bluetooth = false;
1235 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_NETWORK_BLUETOOTH, bluetooth);
1236 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option.");
1237 if (bluetooth == false)
1239 if(!_AppInfo::IsOspCompat())
1241 return E_UNSUPPORTED_OPERATION;
1245 return E_DEVICE_UNAVAILABLE;
1249 ArrayList requestMessages;
1250 ArrayList responseMessages;
1252 requestMessages.Construct();
1253 responseMessages.Construct();
1255 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
1256 String commandId = _DEVICE_MANAGER_COMMAND_STATUS;
1257 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
1259 requestMessages.Add(serviceId);
1260 requestMessages.Add(commandId);
1261 requestMessages.Add(deviceId);
1263 IoService_Request* pMsg = new (std::nothrow) IoService_Request(requestMessages, &responseMessages);
1265 r = __pIpcClient->SendRequest(*pMsg);
1267 String* pMessageData = (String*)responseMessages.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_DATA);
1268 if (pMessageData != null)
1270 state = *pMessageData;
1276 case DEVICE_TYPE_CHARGER:
1278 bool chargerState = false;
1279 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &chargerState);
1280 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get charger state");
1282 if (chargerState == true)
1284 state = _DEVICE_MANAGER_STATE_INSERTED;
1288 state = _DEVICE_MANAGER_STATE_REMOVED;
1293 case DEVICE_TYPE_USB_CLIENT:
1295 bool cableState = false;
1296 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &cableState);
1297 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get usb state");
1299 if (cableState == true)
1301 state = _DEVICE_MANAGER_STATE_INSERTED;
1305 state = _DEVICE_MANAGER_STATE_REMOVED;
1310 case DEVICE_TYPE_TV_OUT:
1313 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
1314 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
1318 if(!_AppInfo::IsOspCompat())
1320 return E_UNSUPPORTED_OPERATION;
1324 return E_DEVICE_UNAVAILABLE;
1328 bool tvState = false;
1329 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, &tvState);
1330 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get tv state");
1331 if (tvState == true)
1333 state = _DEVICE_MANAGER_STATE_INSERTED;
1337 state = _DEVICE_MANAGER_STATE_REMOVED;
1342 case DEVICE_TYPE_WIRED_HEADSET:
1345 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audioState);
1346 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get audio state");
1348 if (audioState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
1350 state = _DEVICE_MANAGER_STATE_INSERTED;
1354 state = _DEVICE_MANAGER_STATE_REMOVED;
1359 case DEVICE_TYPE_WIRED_HEADPHONE:
1362 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audioState);
1363 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get audio state");
1365 if (audioState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
1367 state = _DEVICE_MANAGER_STATE_INSERTED;
1371 state = _DEVICE_MANAGER_STATE_REMOVED;
1376 case DEVICE_TYPE_STORAGE_CARD:
1379 if (!_AppInfo::IsOspCompat())
1381 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
1385 ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
1387 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
1390 state = _DEVICE_MANAGER_STATE_MOUNTED;
1394 state = _DEVICE_MANAGER_STATE_UNMOUNTED;
1399 case DEVICE_TYPE_KEYBOARD:
1401 bool keyboard = false;
1402 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
1403 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option.");
1404 if (keyboard == false)
1406 if(!_AppInfo::IsOspCompat())
1408 return E_UNSUPPORTED_OPERATION;
1412 return E_DEVICE_UNAVAILABLE;
1417 case DEVICE_TYPE_HDMI:
1420 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
1421 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
1424 state = _DEVICE_MANAGER_STATE_INSERTED;
1428 state = _DEVICE_MANAGER_STATE_REMOVED;
1432 return E_UNSUPPORTED_OPERATION;
1439 return E_INVALID_ARG;
1445 _DeviceManagerImpl::OnDataReceived(const ArrayList& data)
1447 StringComparer stringComparer;
1450 String* pServiceId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID);
1451 String* pCommandId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID);
1452 String* pDeviceId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID);
1453 String* pEventId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_DATA);
1455 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
1456 String commandId = _DEVICE_MANAGER_COMMAND_EVENT;
1457 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
1459 SysTryReturnVoidResult(NID_SYS, pServiceId != null && pCommandId != null && pDeviceId != null && pEventId != null, E_SYSTEM, "There is no device data.");
1461 stringComparer.Compare(*pServiceId, serviceId, cmp);
1464 stringComparer.Compare(*pCommandId, commandId, cmp);
1467 stringComparer.Compare(*pDeviceId, deviceId, cmp);
1470 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_BLUETOOTH_HEADSET, *pEventId);
1476 } } // Tizen::System