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.
23 #include <unique_ptr.h>
24 #include <system_info.h>
25 #include <runtime_info.h>
29 #include <FBaseColArrayList.h>
30 #include <FBaseRtEvent.h>
31 #include <FBaseRtIEventArg.h>
32 #include <FBaseStringComparer.h>
33 #include <FBaseSysLog.h>
34 #include <FSysIDeviceEventListener.h>
36 #include <FApp_AppInfo.h>
37 #include <FBase_NativeError.h>
38 #include <FIo_AppServiceIpcMessages.h>
39 #include <FIo_IpcClient.h>
40 #include <FSys_SystemInfoImpl.h>
41 #include <FSys_PowerManagerImpl.h>
42 #include "FSys_DeviceManagerImpl.h"
44 #define VCONFKEY_APPSERVICE_MMC_STATUS "memory/appservice/mmc"
46 using namespace Tizen::App;
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Base::Runtime;
50 using namespace Tizen::Io;
52 namespace Tizen { namespace System
55 static const wchar_t* _COMMUNICATION_DISPATCHER_IPC_ID = L"osp.app.ipcserver.communicationdispatcher";
56 static const wchar_t* _DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
57 static const wchar_t* _DEVICE_MANAGER_COMMAND_OPEN = L"osp.devicemanager.command.open";
58 static const wchar_t* _DEVICE_MANAGER_COMMAND_CLOSE = L"osp.devicemanager.command.close";
59 static const wchar_t* _DEVICE_MANAGER_COMMAND_STATUS = L"osp.devicemanager.command.status";
60 static const wchar_t* _DEVICE_MANAGER_COMMAND_EVENT = L"osp.devicemanager.command.event";
61 static const wchar_t* _DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
63 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID = 0;
64 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID = 1;
65 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID = 2;
66 const int _OSP_APP_SERVICE_IPC_MESSAGE_DATA = 3;
68 const int _DEVICE_MANAGER_BLUETOOTH_HEADSET = 0x01;
69 const int _DEVICE_MANAGER_CHARGER = 0x02;
70 const int _DEVICE_MANAGER_USB_CLIENT = 0x04;
71 const int _DEVICE_MANAGER_TV_OUT = 0x08;
72 const int _DEVICE_MANAGER_WIRED_HEADSET = 0x10;
73 const int _DEVICE_MANAGER_WIRED_HEADPHONE = 0x20;
74 const int _DEVICE_MANAGER_STORAGECARD = 0x40;
75 const int _DEVICE_MANAGER_KEYBOARD = 0x80;
77 static const wchar_t* _DEVICE_MANAGER_STATE_INSERTED = L"Inserted";
78 static const wchar_t* _DEVICE_MANAGER_STATE_REMOVED = L"Removed";
79 static const wchar_t* _DEVICE_MANAGER_STATE_MOUNTED = L"Mounted";
80 static const wchar_t* _DEVICE_MANAGER_STATE_UNMOUNTED = L"Unmounted";
81 static const wchar_t* _DEVICE_MANAGER_STATE_OPENED = L"Opened";
82 static const wchar_t* _DEVICE_MANAGER_STATE_CLOSED = L"Closed";
84 static const wchar_t* _SYSTEM_INFO_INPUT_KEYBOARD = L"http://tizen.org/feature/input.keyboard";
85 static const wchar_t* _SYSTEM_INFO_TVOUT_SUPPORTED = L"http://tizen.org/feature/screen.output.rca";
87 _DeviceManagerImpl* _DeviceManagerImpl::__pDeviceManagerImpl = null;
90 class _DeviceManagerEventArg : public IEventArg
93 _DeviceManagerEventArg()
96 DeviceType deviceType;
100 class _DeviceManagerEvent : public Event
103 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
105 IDeviceEventListener* pListener = dynamic_cast<IDeviceEventListener*> (&listener);
106 const _DeviceManagerEventArg* pArg = dynamic_cast<const _DeviceManagerEventArg*>(&arg);
108 if(pListener == null || pArg == null)
110 SysLogException(NID_SYS, E_SYSTEM, "listener or arg is null.");
114 pListener->OnDeviceStateChanged(pArg->deviceType, pArg->state);
120 _DeviceManagerImpl::OnDeviceStateChanged(runtime_info_key_e key, void* pData)
122 if (__pDeviceManagerImpl != null)
124 __pDeviceManagerImpl->SendEvent(key);
129 _DeviceManagerImpl::InitDeviceManagerImpl(void)
131 static _DeviceManagerImpl deviceManagerImpl;
132 __pDeviceManagerImpl = &deviceManagerImpl;
135 _DeviceManagerImpl::GetInstance(void)
137 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
138 if(__pDeviceManagerImpl == null)
140 pthread_once(&once_block, InitDeviceManagerImpl);
142 return __pDeviceManagerImpl;
145 _DeviceManagerImpl::_DeviceManagerImpl()
147 , __headSetType(DEVICE_TYPE_WIRED_HEADPHONE)
148 , __bluetoothReferenceCount(0)
150 result r = E_SUCCESS;
151 int headsetState = 0;
154 __pIpcClient = new (std::nothrow) _IpcClient();
155 SysTryCatch(NID_SYS, __pIpcClient != null, , r, "Fail to create IPC client");
157 r = __pIpcClient->Construct(_COMMUNICATION_DISPATCHER_IPC_ID, this);
158 SysTryCatch(NID_SYS, r == E_SUCCESS, , r, "Propagated. [%s]", GetErrorMessage(r));
160 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED, OnDeviceStateChanged, null);
161 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to register USB event");
163 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, OnDeviceStateChanged, null);
164 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to register DEVICE_TYPE_CHARGER event");
166 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, OnDeviceStateChanged, null);
167 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to register TV out event");
169 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, OnDeviceStateChanged, null);
170 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to register audio jack event");
172 ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, OnDeviceStateChanged, null);
173 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to register sliding keyboard event");
175 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &headsetState);
176 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to get audio jack status");
178 if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
180 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
182 else if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
184 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
187 if (!_AppInfo::IsOspCompat())
189 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack, null);
193 ret = vconf_notify_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback, null);
195 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "Failed to register MMC event callback.");
197 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack, null);
198 SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "Failed to register HDMI event");
200 __deviceEventList.Construct();
206 _DeviceManagerImpl::~_DeviceManagerImpl()
208 result r = E_SUCCESS;
211 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED);
212 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to unregister USB event");
214 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED);
215 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to unregister TV out event");
217 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS);
218 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to unregister audio jack event");
220 ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED);
221 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to unregister sliding keyboard event");
223 if (!_AppInfo::IsOspCompat())
225 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack);
229 ret = vconf_ignore_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback);
231 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to unregister MMC event");
233 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack);
234 SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "Failed to unregister HDMI event");
242 _DeviceManagerImpl::GetIpcClient(void)
244 if (__pDeviceManagerImpl != null)
246 return __pDeviceManagerImpl->__pIpcClient;
249 SysLogException(NID_SYS, E_SYSTEM, "IPC component is not ready");
255 _DeviceManagerImpl::DeviceEventVConfCallBack(keynode_t* node, void* userData)
261 if (strcmp(VCONFKEY_SYSMAN_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
263 SysLog(NID_SYS, "MMC callback is occured");
264 if (__pDeviceManagerImpl != null)
266 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
269 event = _DEVICE_MANAGER_STATE_MOUNTED;
273 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
275 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
278 else if(strcmp(VCONFKEY_SYSMAN_HDMI, vconf_keynode_get_name(node)) == 0)
280 if (__pDeviceManagerImpl != null)
282 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
285 event = _DEVICE_MANAGER_STATE_INSERTED;
289 event = _DEVICE_MANAGER_STATE_REMOVED;
291 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_HDMI, event);
298 _DeviceManagerImpl::MmcEventVconfCallback(keynode_t* node, void* userData)
300 if (strcmp(VCONFKEY_APPSERVICE_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
302 SysLog(NID_SYS, "MMC event callback is called.");
303 if (__pDeviceManagerImpl != NULL)
307 int ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
310 SysLogException(NID_SYS, E_SYSTEM, "VCONFKEY_APPSERVICE_MMC_STATUS is error");
314 event = _DEVICE_MANAGER_STATE_MOUNTED;
318 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
320 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
326 _DeviceManagerImpl::SendEvent(DeviceType deviceType, String& state)
328 std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
330 if(__deviceEventList.GetCount() > 0)
332 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
333 SysTryReturnVoidResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
335 while(pEnumerator->MoveNext() == E_SUCCESS)
337 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
338 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
339 SysTryReturnVoidResult(NID_SYS, pDeviceEventListenerContainer != null,E_SYSTEM, "Container is empty");
343 case DEVICE_TYPE_BLUETOOTH_HEADSET:
345 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
351 case DEVICE_TYPE_CHARGER:
353 if(pDeviceEventListenerContainer->__charger == false)
359 case DEVICE_TYPE_USB_CLIENT:
361 if(pDeviceEventListenerContainer->__usbClient == false)
367 case DEVICE_TYPE_TV_OUT:
369 if(pDeviceEventListenerContainer->__tvOut == false)
375 case DEVICE_TYPE_WIRED_HEADSET:
377 if(pDeviceEventListenerContainer->__wiredHeadset == false)
383 case DEVICE_TYPE_WIRED_HEADPHONE:
385 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
391 case DEVICE_TYPE_STORAGE_CARD:
393 if(pDeviceEventListenerContainer->__storageCard == false)
399 case DEVICE_TYPE_KEYBOARD:
401 if(pDeviceEventListenerContainer->__keyboard == false)
407 case DEVICE_TYPE_HDMI:
409 if(pDeviceEventListenerContainer->__hdmi == false)
418 std::unique_ptr<_DeviceManagerEventArg> pDeviceManagerEventArg(new (std::nothrow) _DeviceManagerEventArg());
419 if(pDeviceManagerEventArg == null)
421 SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of DeviceManagerEventArg");
424 pDeviceManagerEventArg->deviceType = deviceType;
425 pDeviceManagerEventArg->state = state;
427 if(pDeviceEventListenerContainer->__pEvent != null)
429 pDeviceEventListenerContainer->__pEvent->Fire(*(pDeviceManagerEventArg.release()));
436 _DeviceManagerImpl::RequireBluetoothEvent(void)
438 result r = E_SUCCESS;
439 std::unique_ptr<IoService_Request> pRequest(null);
441 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "Ipc Client is not ready");
443 __bluetoothReferenceCount ++;
445 if(__bluetoothReferenceCount == 1)
447 ArrayList requestMessages;
448 ArrayList responseMessages;
450 r = requestMessages.Construct();
451 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to create request instance");
452 r = responseMessages.Construct();
453 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to create response instance");
455 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
456 String commandId = _DEVICE_MANAGER_COMMAND_OPEN;
457 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
459 r = requestMessages.Add(serviceId);
460 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add service id");
461 r = requestMessages.Add(commandId);
462 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add command id");
463 r = requestMessages.Add(deviceId);
465 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add device id (bluetooth)");
467 pRequest.reset(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
468 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to send bluetooth event subscribe by IPC");
470 r = __pIpcClient->SendRequest(pRequest.get());
471 SysTryReturnResult(NID_SYS, r == E_SUCCESS,E_SYSTEM, "Failed to add bluetooth id");
477 _DeviceManagerImpl::ReleaseBluetoothEvent(void)
479 result r = E_SUCCESS;
480 std::unique_ptr<IoService_Request> pRequest(null);
481 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "Ipc Client is not ready");
483 __bluetoothReferenceCount --;
484 SysTryReturnResult(NID_SYS, __bluetoothReferenceCount >= 0, E_SYSTEM, "Fail to manage reference count");
486 if(__bluetoothReferenceCount == 0)
488 ArrayList requestMessages;
489 ArrayList responseMessages;
491 r = requestMessages.Construct();
492 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to create request instance");
493 r = responseMessages.Construct();
494 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to create response instance");
496 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
497 String commandId = _DEVICE_MANAGER_COMMAND_CLOSE;
498 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
500 r = requestMessages.Add(serviceId);
501 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add service id");
502 r = requestMessages.Add(commandId);
503 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add command id");
504 r = requestMessages.Add(deviceId);
505 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add bluetooth id");
507 pRequest.reset(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
508 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to send bluetooth event subscribe by IPC");
510 r = __pIpcClient->SendRequest(pRequest.get());
511 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Failed to add bluetooth id");
517 _DeviceManagerImpl::AddOnExistedListener(DeviceType deviceType, const IDeviceEventListener* pListener)
519 result r = E_OBJ_NOT_FOUND;
520 std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
522 if(__deviceEventList.GetCount() > 0)
524 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
525 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
527 while(pEnumerator->MoveNext() == E_SUCCESS)
529 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
530 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
531 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
532 if(pDeviceEventListenerContainer->__pListener == pListener)
536 case DEVICE_TYPE_BLUETOOTH_HEADSET:
538 if(pDeviceEventListenerContainer->__bluetoothHeadset == true)
540 r = E_OBJ_ALREADY_EXIST;
544 pDeviceEventListenerContainer->__bluetoothHeadset = true;
545 RequireBluetoothEvent();
550 case DEVICE_TYPE_CHARGER:
552 if(pDeviceEventListenerContainer->__charger == true)
554 r = E_OBJ_ALREADY_EXIST;
558 pDeviceEventListenerContainer->__charger = true;
563 case DEVICE_TYPE_USB_CLIENT:
565 if(pDeviceEventListenerContainer->__usbClient == true)
567 r = E_OBJ_ALREADY_EXIST;
571 pDeviceEventListenerContainer->__usbClient = true;
576 case DEVICE_TYPE_TV_OUT:
579 _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
583 if(!_AppInfo::IsOspCompat())
585 r = E_UNSUPPORTED_OPERATION;
589 r = E_DEVICE_UNAVAILABLE;
594 if(pDeviceEventListenerContainer->__tvOut == true)
596 r = E_OBJ_ALREADY_EXIST;
600 pDeviceEventListenerContainer->__tvOut = true;
605 case DEVICE_TYPE_WIRED_HEADSET:
607 if(pDeviceEventListenerContainer->__wiredHeadset == true)
609 r = E_OBJ_ALREADY_EXIST;
613 pDeviceEventListenerContainer->__wiredHeadset = true;
618 case DEVICE_TYPE_WIRED_HEADPHONE:
620 if(pDeviceEventListenerContainer->__wiredHeadphone == true)
622 r = E_OBJ_ALREADY_EXIST;
626 pDeviceEventListenerContainer->__wiredHeadphone = true;
631 case DEVICE_TYPE_STORAGE_CARD:
633 if(pDeviceEventListenerContainer->__storageCard == true)
635 r = E_OBJ_ALREADY_EXIST;
639 pDeviceEventListenerContainer->__storageCard = true;
644 case DEVICE_TYPE_KEYBOARD:
646 bool keyboard = false;
647 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
649 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
651 if (keyboard == false)
653 if(!_AppInfo::IsOspCompat())
655 return E_UNSUPPORTED_OPERATION;
659 return E_DEVICE_UNAVAILABLE;
663 if(pDeviceEventListenerContainer->__keyboard == true)
665 r = E_OBJ_ALREADY_EXIST;
669 pDeviceEventListenerContainer->__keyboard = true;
674 case DEVICE_TYPE_HDMI:
678 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
679 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
682 return E_UNSUPPORTED_OPERATION;
684 if(pDeviceEventListenerContainer->__hdmi == true)
686 r = E_OBJ_ALREADY_EXIST;
690 pDeviceEventListenerContainer->__hdmi = true;
703 _DeviceManagerImpl::AddDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
705 SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
706 SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
707 result r = E_SUCCESS;
709 if(deviceType == DEVICE_TYPE_TV_OUT)
712 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
713 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
717 if(!_AppInfo::IsOspCompat())
719 r = E_UNSUPPORTED_OPERATION;
723 r = E_DEVICE_UNAVAILABLE;
728 else if(deviceType == DEVICE_TYPE_KEYBOARD)
730 bool keyboard = false;
731 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
733 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
735 if (keyboard == false)
737 if(!_AppInfo::IsOspCompat())
739 r = E_UNSUPPORTED_OPERATION;
743 r = E_DEVICE_UNAVAILABLE;
748 else if(deviceType == DEVICE_TYPE_HDMI)
752 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
753 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
756 return E_UNSUPPORTED_OPERATION;
760 r = AddOnExistedListener(deviceType, pListener);
762 if(r == E_OBJ_NOT_FOUND)
764 std::unique_ptr<_DeviceEventListenerContainer> pDeviceEventContainer(new (std::nothrow) _DeviceEventListenerContainer());
765 SysTryReturnResult(NID_SYS, pDeviceEventContainer != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceEventListenerContainer");
767 std::unique_ptr<_DeviceManagerEvent> pDeviceManagerEvent(new (std::nothrow) _DeviceManagerEvent());
768 SysTryReturnResult(NID_SYS, pDeviceManagerEvent != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceManagerEvent");
770 pDeviceManagerEvent->AddListener(*pListener);
771 pDeviceEventContainer->__pListener = pListener;
772 pDeviceEventContainer->__pEvent = pDeviceManagerEvent.release();
776 case DEVICE_TYPE_BLUETOOTH_HEADSET:
778 pDeviceEventContainer->__bluetoothHeadset = true;
779 RequireBluetoothEvent();
782 case DEVICE_TYPE_CHARGER:
784 pDeviceEventContainer->__charger = true;
787 case DEVICE_TYPE_USB_CLIENT:
789 pDeviceEventContainer->__usbClient = true;
792 case DEVICE_TYPE_TV_OUT:
795 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
796 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
800 if(!_AppInfo::IsOspCompat())
802 return E_UNSUPPORTED_OPERATION;
806 return E_DEVICE_UNAVAILABLE;
810 pDeviceEventContainer->__tvOut = true;
813 case DEVICE_TYPE_WIRED_HEADSET:
815 pDeviceEventContainer->__wiredHeadset = true;
818 case DEVICE_TYPE_WIRED_HEADPHONE:
820 pDeviceEventContainer->__wiredHeadphone = true;
823 case DEVICE_TYPE_STORAGE_CARD:
825 pDeviceEventContainer->__storageCard = true;
828 case DEVICE_TYPE_KEYBOARD:
830 bool keyboard = false;
831 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
833 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
835 if (keyboard == false)
837 if(!_AppInfo::IsOspCompat())
839 return E_UNSUPPORTED_OPERATION;
843 return E_DEVICE_UNAVAILABLE;
847 pDeviceEventContainer->__keyboard = true;
850 case DEVICE_TYPE_HDMI:
854 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
855 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
858 return E_UNSUPPORTED_OPERATION;
860 pDeviceEventContainer->__hdmi = true;
865 __deviceEventList.Add(pDeviceEventContainer.release());
872 _DeviceManagerImpl::RemoveDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
874 result r = E_SUCCESS;
876 SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
877 SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
878 SysTryReturnResult(NID_SYS, __deviceEventList.GetCount() > 0, E_INVALID_ARG, "There is no registered listener.");
880 std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(__deviceEventList.GetEnumeratorN());
882 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
884 if(deviceType == DEVICE_TYPE_TV_OUT)
887 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
888 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
892 if(!_AppInfo::IsOspCompat())
894 r = E_UNSUPPORTED_OPERATION;
898 r = E_DEVICE_UNAVAILABLE;
903 else if(deviceType == DEVICE_TYPE_KEYBOARD)
905 bool keyboard = false;
906 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
908 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
910 if (keyboard == false)
912 if(!_AppInfo::IsOspCompat())
914 r = E_UNSUPPORTED_OPERATION;
918 r = E_DEVICE_UNAVAILABLE;
922 else if(deviceType == DEVICE_TYPE_HDMI)
926 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
927 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
930 return E_UNSUPPORTED_OPERATION;
934 while(pEnumerator->MoveNext() == E_SUCCESS)
936 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
937 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
938 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
940 if(pDeviceEventListenerContainer->__pListener == pListener)
944 case DEVICE_TYPE_BLUETOOTH_HEADSET:
946 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
948 return E_OBJ_NOT_FOUND;
950 pDeviceEventListenerContainer->__bluetoothHeadset = false;
951 ReleaseBluetoothEvent();
954 case DEVICE_TYPE_CHARGER:
956 if(pDeviceEventListenerContainer->__charger == false)
958 return E_OBJ_NOT_FOUND;
960 pDeviceEventListenerContainer->__charger = false;
963 case DEVICE_TYPE_USB_CLIENT:
965 if(pDeviceEventListenerContainer->__usbClient == false)
967 return E_OBJ_NOT_FOUND;
969 pDeviceEventListenerContainer->__usbClient = false;
972 case DEVICE_TYPE_TV_OUT:
974 if(pDeviceEventListenerContainer->__tvOut == false)
976 return E_OBJ_NOT_FOUND;
978 pDeviceEventListenerContainer->__tvOut = false;
981 case DEVICE_TYPE_WIRED_HEADSET:
983 if(pDeviceEventListenerContainer->__wiredHeadset == false)
985 return E_OBJ_NOT_FOUND;
987 pDeviceEventListenerContainer->__wiredHeadset = false;
990 case DEVICE_TYPE_WIRED_HEADPHONE:
992 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
994 return E_OBJ_NOT_FOUND;
996 pDeviceEventListenerContainer->__wiredHeadphone = false;
999 case DEVICE_TYPE_STORAGE_CARD:
1001 if(pDeviceEventListenerContainer->__storageCard == false)
1003 return E_OBJ_NOT_FOUND;
1005 pDeviceEventListenerContainer->__storageCard = false;
1008 case DEVICE_TYPE_KEYBOARD:
1010 if(pDeviceEventListenerContainer->__keyboard == false)
1012 return E_OBJ_NOT_FOUND;
1014 pDeviceEventListenerContainer->__keyboard = false;
1017 case DEVICE_TYPE_HDMI:
1019 if(pDeviceEventListenerContainer->__hdmi == false)
1021 return E_OBJ_NOT_FOUND;
1023 pDeviceEventListenerContainer->__hdmi = false;
1028 if(pDeviceEventListenerContainer->IsInvalid() == true)
1030 __deviceEventList.Remove(pDeviceEventListenerContainer);
1031 delete pDeviceEventListenerContainer;
1036 return E_OBJ_NOT_FOUND;
1040 _DeviceManagerImpl::RemoveAllDeviceEventListeners(void)
1042 result r = E_SUCCESS;
1044 IEnumeratorT<_DeviceEventListenerContainer*>* pEnumerator = __deviceEventList.GetEnumeratorN();
1045 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
1047 while(pEnumerator->MoveNext() == E_SUCCESS)
1049 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
1050 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
1051 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
1052 __deviceEventList.Remove(pDeviceEventListenerContainer);
1053 delete pDeviceEventListenerContainer;
1057 __bluetoothReferenceCount = 1;
1059 ReleaseBluetoothEvent();
1064 _DeviceManagerImpl::SendEvent(runtime_info_key_e key)
1066 DeviceType type = DEVICE_TYPE_USB_CLIENT;
1073 case RUNTIME_INFO_KEY_USB_CONNECTED:
1075 flag = _DEVICE_MANAGER_USB_CLIENT;
1076 type = DEVICE_TYPE_USB_CLIENT;
1079 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &state);
1080 if (ret != RUNTIME_INFO_ERROR_NONE)
1082 SysLogException(NID_SYS, E_SYSTEM, "Failed to get usb state");
1088 value = _DEVICE_MANAGER_STATE_INSERTED;
1092 value = _DEVICE_MANAGER_STATE_REMOVED;
1096 case RUNTIME_INFO_KEY_CHARGER_CONNECTED:
1098 flag = _DEVICE_MANAGER_CHARGER;
1099 type = DEVICE_TYPE_CHARGER;
1102 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &state);
1103 if (ret != RUNTIME_INFO_ERROR_NONE)
1105 SysLogException(NID_SYS, E_SYSTEM, "Failed to get charger state");
1111 value = _DEVICE_MANAGER_STATE_INSERTED;
1115 value = _DEVICE_MANAGER_STATE_REMOVED;
1119 case RUNTIME_INFO_KEY_TV_OUT_CONNECTED:
1121 flag = _DEVICE_MANAGER_TV_OUT;
1122 type = DEVICE_TYPE_TV_OUT;
1125 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, &state);
1126 if (ret != RUNTIME_INFO_ERROR_NONE)
1128 SysLogException(NID_SYS, E_SYSTEM, "Failed to get tv connect state");
1134 value = _DEVICE_MANAGER_STATE_INSERTED;
1138 value = _DEVICE_MANAGER_STATE_REMOVED;
1142 case RUNTIME_INFO_KEY_AUDIO_JACK_STATUS:
1145 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &state);
1146 if (ret != RUNTIME_INFO_ERROR_NONE)
1148 SysLogException(NID_SYS, E_SYSTEM, "Failed to get audion jack state");
1152 if (state == RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED)
1154 if (__headSetType == DEVICE_TYPE_WIRED_HEADPHONE)
1156 flag = _DEVICE_MANAGER_WIRED_HEADPHONE;
1157 type = DEVICE_TYPE_WIRED_HEADPHONE;
1161 flag = _DEVICE_MANAGER_WIRED_HEADSET;
1162 type = DEVICE_TYPE_WIRED_HEADSET;
1165 value = _DEVICE_MANAGER_STATE_REMOVED;
1169 if (state == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
1171 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
1172 flag = _DEVICE_MANAGER_WIRED_HEADPHONE;
1173 type = DEVICE_TYPE_WIRED_HEADPHONE;
1177 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
1178 flag = _DEVICE_MANAGER_WIRED_HEADSET;
1179 type = DEVICE_TYPE_WIRED_HEADSET;
1181 value = _DEVICE_MANAGER_STATE_INSERTED;
1186 case RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED:
1188 flag = _DEVICE_MANAGER_KEYBOARD;
1189 type = DEVICE_TYPE_KEYBOARD;
1192 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, &state);
1193 if (ret != RUNTIME_INFO_ERROR_NONE)
1195 SysLogException(NID_SYS, E_SYSTEM, "Failed to get keyboard state");
1201 value = _DEVICE_MANAGER_STATE_OPENED;
1205 value = _DEVICE_MANAGER_STATE_CLOSED;
1213 __pDeviceManagerImpl->SendEvent(type, value);
1218 _DeviceManagerImpl::GetState(DeviceType deviceType, String& state)
1220 result r = E_SUCCESS;
1224 SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "GetDeviceState Failed");
1229 case DEVICE_TYPE_BLUETOOTH_HEADSET:
1231 ArrayList requestMessages;
1232 ArrayList responseMessages;
1234 requestMessages.Construct();
1235 responseMessages.Construct();
1237 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
1238 String commandId = _DEVICE_MANAGER_COMMAND_STATUS;
1239 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
1241 requestMessages.Add(serviceId);
1242 requestMessages.Add(commandId);
1243 requestMessages.Add(deviceId);
1245 IoService_Request* pMsg = new (std::nothrow) IoService_Request(requestMessages, &responseMessages);
1247 r = __pIpcClient->SendRequest(*pMsg);
1249 String* pMessageData = (String*)responseMessages.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_DATA);
1250 if (pMessageData != null)
1252 state = *pMessageData;
1258 case DEVICE_TYPE_CHARGER:
1260 bool chargerState = false;
1261 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &chargerState);
1262 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "Failed to get charger state");
1264 if (chargerState == true)
1266 state = _DEVICE_MANAGER_STATE_INSERTED;
1270 state = _DEVICE_MANAGER_STATE_REMOVED;
1275 case DEVICE_TYPE_USB_CLIENT:
1277 bool cableState = false;
1278 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &cableState);
1279 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "Failed to get usb state");
1281 if (cableState == true)
1283 state = _DEVICE_MANAGER_STATE_INSERTED;
1287 state = _DEVICE_MANAGER_STATE_REMOVED;
1292 case DEVICE_TYPE_TV_OUT:
1295 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
1296 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
1300 if(!_AppInfo::IsOspCompat())
1302 return E_UNSUPPORTED_OPERATION;
1306 return E_DEVICE_UNAVAILABLE;
1310 bool tvState = false;
1311 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, &tvState);
1312 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "Failed to get tv state");
1313 if (tvState == true)
1315 state = _DEVICE_MANAGER_STATE_INSERTED;
1319 state = _DEVICE_MANAGER_STATE_REMOVED;
1324 case DEVICE_TYPE_WIRED_HEADSET:
1327 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audioState);
1328 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "Failed to get audio state");
1330 if (audioState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
1332 state = _DEVICE_MANAGER_STATE_INSERTED;
1336 state = _DEVICE_MANAGER_STATE_REMOVED;
1341 case DEVICE_TYPE_WIRED_HEADPHONE:
1344 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audioState);
1345 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "Failed to get audio state");
1347 if (audioState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
1349 state = _DEVICE_MANAGER_STATE_INSERTED;
1353 state = _DEVICE_MANAGER_STATE_REMOVED;
1358 case DEVICE_TYPE_STORAGE_CARD:
1361 if (!_AppInfo::IsOspCompat())
1363 ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
1367 ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
1369 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
1372 state = _DEVICE_MANAGER_STATE_MOUNTED;
1376 state = _DEVICE_MANAGER_STATE_UNMOUNTED;
1381 case DEVICE_TYPE_KEYBOARD:
1383 bool keyboard = false;
1384 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
1385 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option.");
1386 if (keyboard == false)
1388 if(!_AppInfo::IsOspCompat())
1390 return E_UNSUPPORTED_OPERATION;
1394 return E_DEVICE_UNAVAILABLE;
1399 case DEVICE_TYPE_HDMI:
1402 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
1403 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
1406 state = _DEVICE_MANAGER_STATE_INSERTED;
1410 state = _DEVICE_MANAGER_STATE_REMOVED;
1414 return E_UNSUPPORTED_OPERATION;
1421 return E_INVALID_ARG;
1427 _DeviceManagerImpl::OnIpcResponseReceived(_IpcClient& client, const IPC::Message& message)
1429 SysLog(NID_SYS, "Enter.");
1430 IPC_BEGIN_MESSAGE_MAP(_DeviceManagerImpl, message)
1431 IPC_MESSAGE_HANDLER_EX(IoService_Data, &client, OnDataReceived)
1432 IPC_END_MESSAGE_MAP_EX()
1433 SysLog(NID_SYS, "Exit.");
1437 _DeviceManagerImpl::OnDataReceived(const ArrayList& data)
1439 StringComparer stringComparer;
1442 String* pServiceId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID);
1443 String* pCommandId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID);
1444 String* pDeviceId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID);
1445 String* pEventId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_DATA);
1447 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
1448 String commandId = _DEVICE_MANAGER_COMMAND_EVENT;
1449 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
1451 SysTryReturnVoidResult(NID_SYS, pServiceId != null && pCommandId != null && pDeviceId != null && pEventId != null, E_SYSTEM, "There is no device data.");
1453 stringComparer.Compare(*pServiceId, serviceId, cmp);
1456 stringComparer.Compare(*pCommandId, commandId, cmp);
1459 stringComparer.Compare(*pDeviceId, deviceId, cmp);
1462 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_BLUETOOTH_HEADSET, *pEventId);
1468 } } // Tizen::System