Fix N_SE-56436 for Screen lock.
[platform/framework/native/appfw.git] / src / system / FSys_DeviceManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file                FSys_DeviceManagerImpl.cpp
19  * @brief               This is the implementation file for _DeviceManagerImpl class.
20  */
21
22 #include <pthread.h>
23 #include <unique_ptr.h>
24 #include <system_info.h>
25 #include <runtime_info.h>
26 #include <stdlib.h>
27 #include <vconf.h>
28
29 #include <FBaseColArrayList.h>
30 #include <FBaseRtEvent.h>
31 #include <FBaseRtIEventArg.h>
32 #include <FBaseStringComparer.h>
33 #include <FBaseSysLog.h>
34 #include <FSysIDeviceEventListener.h>
35 #include <FIo.h>
36
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>
43
44 #include "FSys_CommunicationDispatcherClient.h"
45 #include "FSys_DeviceManagerImpl.h"
46 #include "FSys_SystemServiceMessageClient.h"
47
48 #define VCONFKEY_APPSERVICE_MMC_STATUS  "memory/appservice/mmc"
49
50 using namespace std;
51
52 using namespace Tizen::App;
53 using namespace Tizen::Base;
54 using namespace Tizen::Base::Collection;
55 using namespace Tizen::Base::Runtime;
56 using namespace Tizen::Io;
57
58 namespace Tizen { namespace System
59 {
60 static const wchar_t* _DEVICE_MANAGER_SERVICE_ID = L"osp.sys.ipcserver.devicemanager";
61 static const wchar_t* _DEVICE_MANAGER_COMMAND_OPEN = L"osp.devicemanager.command.open";
62 static const wchar_t* _DEVICE_MANAGER_COMMAND_CLOSE = L"osp.devicemanager.command.close";
63 static const wchar_t* _DEVICE_MANAGER_COMMAND_STATUS = L"osp.devicemanager.command.status";
64 static const wchar_t* _DEVICE_MANAGER_COMMAND_EVENT = L"osp.devicemanager.command.event";
65 static const wchar_t* _DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
66
67 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID = 0;
68 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID = 1;
69 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID = 2;
70 const int _OSP_APP_SERVICE_IPC_MESSAGE_DATA = 3;
71
72 const int _DEVICE_MANAGER_BLUETOOTH_HEADSET = 0x01;
73 const int _DEVICE_MANAGER_CHARGER = 0x02;
74 const int _DEVICE_MANAGER_USB_CLIENT = 0x04;
75 const int _DEVICE_MANAGER_TV_OUT = 0x08;
76 const int _DEVICE_MANAGER_WIRED_HEADSET = 0x10;
77 const int _DEVICE_MANAGER_WIRED_HEADPHONE = 0x20;
78 const int _DEVICE_MANAGER_STORAGECARD = 0x40;
79 const int _DEVICE_MANAGER_KEYBOARD = 0x80;
80
81 static const wchar_t* _DEVICE_MANAGER_STATE_INSERTED = L"Inserted";
82 static const wchar_t* _DEVICE_MANAGER_STATE_REMOVED = L"Removed";
83 static const wchar_t* _DEVICE_MANAGER_STATE_MOUNTED = L"Mounted";
84 static const wchar_t* _DEVICE_MANAGER_STATE_UNMOUNTED = L"Unmounted";
85 static const wchar_t* _DEVICE_MANAGER_STATE_OPENED = L"Opened";
86 static const wchar_t* _DEVICE_MANAGER_STATE_CLOSED = L"Closed";
87
88 static const wchar_t* _SYSTEM_INFO_NETWORK_BLUETOOTH = L"http://tizen.org/feature/network.bluetooth";
89 static const wchar_t* _SYSTEM_INFO_INPUT_KEYBOARD = L"http://tizen.org/feature/input.keyboard";
90 static const wchar_t* _SYSTEM_INFO_TVOUT_SUPPORTED = L"http://tizen.org/feature/screen.output.rca";
91 static const wchar_t* _SYSTEM_INFO_HDMI = L"http://tizen.org/feature/screen.output.hdmi";
92
93 _DeviceManagerImpl* _DeviceManagerImpl::__pDeviceManagerImpl = null;
94
95 class _DeviceManagerEventArg : public IEventArg
96 {
97 public:
98         _DeviceManagerEventArg()
99         {
100         }
101         DeviceType deviceType;
102         String state;
103 };
104
105 class _DeviceManagerEvent : public Event
106 {
107 protected:
108         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
109         {
110                 IDeviceEventListener* pListener = dynamic_cast<IDeviceEventListener*> (&listener);
111                 const _DeviceManagerEventArg* pArg = dynamic_cast<const _DeviceManagerEventArg*>(&arg);
112
113                 SysTryReturnVoidResult(NID_SYS, pListener != null && pArg != null, E_SYSTEM, "Parameters are not available.");
114
115                 pListener->OnDeviceStateChanged(pArg->deviceType, pArg->state);
116         }
117 };
118
119
120 void
121 _DeviceManagerImpl::OnDeviceStateChanged(runtime_info_key_e key, void* pData)
122 {
123         if (__pDeviceManagerImpl != null)
124         {
125                 __pDeviceManagerImpl->SendEvent(key);
126         }
127 }
128
129 void
130 _DeviceManagerImpl::InitDeviceManagerImpl(void)
131 {
132         static _DeviceManagerImpl deviceManagerImpl;
133         __pDeviceManagerImpl = &deviceManagerImpl;
134 }
135 _DeviceManagerImpl*
136 _DeviceManagerImpl::GetInstance(void)
137 {
138         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
139         if(__pDeviceManagerImpl == null)
140         {
141                 pthread_once(&once_block, InitDeviceManagerImpl);
142         }
143         return __pDeviceManagerImpl;
144 }
145
146 _DeviceManagerImpl::_DeviceManagerImpl()
147         : __pIpcClient(null)
148         , __headSetType(DEVICE_TYPE_WIRED_HEADPHONE)
149         , __bluetoothReferenceCount(0)
150         , __pSystemServiceMessageClient(null)
151 {
152         result r = E_SUCCESS;
153         int headsetState = 0;
154         int ret = 0;
155         static String DEVICE_MANAGER_SERVICE_ID(_DEVICE_MANAGER_SERVICE_ID);
156
157         _SystemServiceMessageClient* pSystemServiceMessageClient = _SystemServiceMessageClient::CreateInstance(DEVICE_MANAGER_SERVICE_ID);
158         SysTryCatch(NID_SYS, pSystemServiceMessageClient, r = E_SYSTEM, r, "It is failed to get system service message client.");
159         r = pSystemServiceMessageClient->RegisterListener(DEVICE_MANAGER_SERVICE_ID, *this);
160         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on Listener.");
161
162         __pSystemServiceMessageClient = pSystemServiceMessageClient;
163         __pIpcClient = __pSystemServiceMessageClient->GetIpcClient();
164
165         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED, OnDeviceStateChanged, null);
166         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register USB event");
167
168         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, OnDeviceStateChanged, null);
169         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register DEVICE_TYPE_CHARGER event");
170
171         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, OnDeviceStateChanged, null);
172         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register TV out event");
173
174         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, OnDeviceStateChanged, null);
175         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register audio jack event");
176
177         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, OnDeviceStateChanged, null);
178         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register sliding keyboard event");
179
180         ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &headsetState);
181         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to get audio jack status");
182
183         if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
184         {
185                 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
186         }
187         else if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
188         {
189                 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
190         }
191
192         if (!_AppInfo::IsOspCompat() && !_AppInfo::IsVirtualRoot())
193         {
194                 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack, null);
195         }
196         else
197         {
198                 ret = vconf_notify_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback, null);
199         }
200         SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to register MMC event callback.");
201
202         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack, null);
203         SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to register HDMI event");
204
205         __deviceEventList.Construct();
206
207 CATCH:
208         SetLastResult(r);
209 }
210
211 _DeviceManagerImpl::~_DeviceManagerImpl()
212 {
213         result r = E_SUCCESS;
214         int ret = 0;
215         _CommunicationDispatcherClient* pCommunicationDispatcherClient = null;
216         String key(_DEVICE_MANAGER_SERVICE_ID);
217
218         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED);
219         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister USB event");
220
221         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED);
222         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister TV out event");
223
224         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS);
225         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister audio jack event");
226
227         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED);
228         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister sliding keyboard event");
229
230         if (!_AppInfo::IsOspCompat() && !_AppInfo::IsVirtualRoot())
231         {
232                 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack);
233         }
234         else
235         {
236                 ret = vconf_ignore_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback);
237         }
238         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister MMC event");
239
240         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack);
241         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister HDMI event");
242
243         if (__pSystemServiceMessageClient)
244         {
245                 delete __pSystemServiceMessageClient;
246         }
247         __pIpcClient = null;
248 CATCH:
249         SetLastResult(r);
250 }
251
252 void
253 _DeviceManagerImpl::DeviceEventVConfCallBack(keynode_t* node, void* userData)
254 {
255         int ret = 0;
256         int value = 0;
257         String event;
258
259         if (strcmp(VCONFKEY_SYSMAN_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
260         {
261                 SysLog(NID_SYS, "MMC callback is occured");
262                 if (__pDeviceManagerImpl != null)
263                 {
264                         ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
265                         if (value == 1)
266                         {
267                                 event = _DEVICE_MANAGER_STATE_MOUNTED;
268                         }
269                         else
270                         {
271                                 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
272                         }
273                         __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
274                 }
275         }
276         else if(strcmp(VCONFKEY_SYSMAN_HDMI, vconf_keynode_get_name(node)) == 0)
277         {
278                 if (__pDeviceManagerImpl != null)
279                 {
280                         ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
281                         if (value == 1)
282                         {
283                                 event = _DEVICE_MANAGER_STATE_INSERTED;
284                         }
285                         else
286                         {
287                                 event = _DEVICE_MANAGER_STATE_REMOVED;
288                         }
289                         __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_HDMI, event);
290                 }
291
292         }
293 }
294
295 void
296 _DeviceManagerImpl::MmcEventVconfCallback(keynode_t* node, void* userData)
297 {
298         if (strcmp(VCONFKEY_APPSERVICE_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
299         {
300                 SysLog(NID_SYS, "MMC event callback is called.");
301                 if (__pDeviceManagerImpl != NULL)
302                 {
303                         String event;
304                         int value = 0;
305                         int ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
306                         SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "VCONFKEY_APPSERVICE_MMC_STATUS is error");
307
308                         if (value == 1)
309                         {
310                                 event = _DEVICE_MANAGER_STATE_MOUNTED;
311                         }
312                         else
313                         {
314                                 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
315                         }
316                         __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
317                 }
318         }
319 }
320
321 void
322 _DeviceManagerImpl::SendEvent(DeviceType deviceType, String& state)
323 {
324         unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
325
326         if(__deviceEventList.GetCount() > 0)
327         {
328                 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
329                 SysTryReturnVoidResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
330
331                 while(pEnumerator->MoveNext() == E_SUCCESS)
332                 {
333                         _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
334                         pEnumerator->GetCurrent(pDeviceEventListenerContainer);
335                         SysTryReturnVoidResult(NID_SYS, pDeviceEventListenerContainer != null,E_SYSTEM, "Container is empty");
336
337                         switch(deviceType)
338                         {
339                         case DEVICE_TYPE_BLUETOOTH_HEADSET:
340                         {
341                                 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
342                                 {
343                                         return;
344                                 }
345                                 break;
346                         }
347                         case DEVICE_TYPE_CHARGER:
348                         {
349                                 if(pDeviceEventListenerContainer->__charger == false)
350                                 {
351                                         return;
352                                 }
353                                 break;
354                         }
355                         case DEVICE_TYPE_USB_CLIENT:
356                         {
357                                 if(pDeviceEventListenerContainer->__usbClient == false)
358                                 {
359                                         return;
360                                 }
361                                 break;
362                         }
363                         case DEVICE_TYPE_TV_OUT:
364                         {
365                                 if(pDeviceEventListenerContainer->__tvOut == false)
366                                 {
367                                         return;
368                                 }
369                                 break;
370                         }
371                         case DEVICE_TYPE_WIRED_HEADSET:
372                         {
373                                 if(pDeviceEventListenerContainer->__wiredHeadset == false)
374                                 {
375                                         return;
376                                 }
377                                 break;
378                         }
379                         case DEVICE_TYPE_WIRED_HEADPHONE:
380                         {
381                                 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
382                                 {
383                                         return;
384                                 }
385                                 break;
386                         }
387                         case DEVICE_TYPE_STORAGE_CARD:
388                         {
389                                 if(pDeviceEventListenerContainer->__storageCard == false)
390                                 {
391                                         return;
392                                 }
393                                 break;
394                         }
395                         case DEVICE_TYPE_KEYBOARD:
396                         {
397                                 if(pDeviceEventListenerContainer->__keyboard == false)
398                                 {
399                                         return;
400                                 }
401                                 break;
402                         }
403                         case DEVICE_TYPE_HDMI:
404                         {
405                                 if(pDeviceEventListenerContainer->__hdmi == false)
406                                 {
407                                         return;
408                                 }
409                                 break;
410                         }
411                         default:
412                                 return;
413                         }
414                         unique_ptr<_DeviceManagerEventArg> pDeviceManagerEventArg(new (std::nothrow) _DeviceManagerEventArg());
415                         SysTryReturnVoidResult(NID_SYS, pDeviceManagerEventArg != null, E_OUT_OF_MEMORY, "It is failed to create instance of DeviceManagerEventArg");
416
417                         pDeviceManagerEventArg->deviceType = deviceType;
418                         pDeviceManagerEventArg->state = state;
419
420                         if(pDeviceEventListenerContainer->__pEvent != null)
421                         {
422                                 pDeviceEventListenerContainer->__pEvent->Fire(*(pDeviceManagerEventArg.release()));
423                         }
424                 }
425         }
426 }
427
428 result
429 _DeviceManagerImpl::RequireBluetoothEvent(void)
430 {
431         result r = E_SUCCESS;
432
433         SysTryReturnResult(NID_SYS, GetIpcClient() != null, E_SYSTEM, "Ipc Client is not ready");
434
435         __bluetoothReferenceCount ++;
436
437         if(__bluetoothReferenceCount == 1)
438         {
439                 ArrayList requestMessages;
440                 ArrayList responseMessages;
441
442                 r = requestMessages.Construct();
443                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
444                 r = responseMessages.Construct();
445                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
446
447                 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
448                 String commandId = _DEVICE_MANAGER_COMMAND_OPEN;
449                 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
450
451                 r = requestMessages.Add(serviceId);
452                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
453                 r = requestMessages.Add(commandId);
454                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
455                 r = requestMessages.Add(deviceId);
456
457                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add device id (bluetooth)");
458
459                 unique_ptr<IoService_Request> pRequest(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
460                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send bluetooth event subscribe by IPC");
461
462                 r = GetIpcClient()->SendRequest(pRequest.get());
463                 SysTryReturnResult(NID_SYS, r == E_SUCCESS,E_SYSTEM, "It is failed to add bluetooth id");
464                 SysLog(NID_SYS, "It sent %ls", _DEVICE_MANAGER_COMMAND_OPEN);
465                 
466                 responseMessages.RemoveAll(true);
467         }
468         return r;
469 }
470
471 result
472 _DeviceManagerImpl::ReleaseBluetoothEvent(void)
473 {
474         result r = E_SUCCESS;
475         SysTryReturnResult(NID_SYS, GetIpcClient() != null, E_SYSTEM, "Ipc Client is not ready");
476
477         __bluetoothReferenceCount --;
478         SysTryReturnResult(NID_SYS, __bluetoothReferenceCount >= 0, E_SYSTEM, "Fail to manage reference count");
479
480         if(__bluetoothReferenceCount == 0)
481         {
482                 ArrayList requestMessages;
483                 ArrayList responseMessages;
484
485                 r = requestMessages.Construct();
486                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
487                 r = responseMessages.Construct();
488                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
489
490                 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
491                 String commandId = _DEVICE_MANAGER_COMMAND_CLOSE;
492                 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
493
494                 r = requestMessages.Add(serviceId);
495                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
496                 r = requestMessages.Add(commandId);
497                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
498                 r = requestMessages.Add(deviceId);
499                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add bluetooth id");
500
501                 unique_ptr<IoService_Request> pRequest(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
502                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send bluetooth event subscribe by IPC");
503
504                 r = GetIpcClient()->SendRequest(pRequest.get());
505                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add bluetooth id");
506
507                 responseMessages.RemoveAll(true);
508         }
509         return r;
510 }
511
512
513 _IpcClient*
514 _DeviceManagerImpl::GetIpcClient(void)
515 {
516         return  __pSystemServiceMessageClient->GetIpcClient();
517 }
518
519
520 result
521 _DeviceManagerImpl::AddOnExistedListener(DeviceType deviceType, const IDeviceEventListener* pListener)
522 {
523         result r = E_OBJ_NOT_FOUND;
524         unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
525
526         if(__deviceEventList.GetCount() > 0)
527         {
528                 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
529                 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
530
531                 while(pEnumerator->MoveNext() == E_SUCCESS)
532                 {
533                         _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
534                         pEnumerator->GetCurrent(pDeviceEventListenerContainer);
535                         SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
536                         if(pDeviceEventListenerContainer->__pListener == pListener)
537                         {
538                                 switch(deviceType)
539                                 {
540                                 case DEVICE_TYPE_BLUETOOTH_HEADSET:
541                                 {
542                                         if(pDeviceEventListenerContainer->__bluetoothHeadset == true)
543                                         {
544                                                 r = E_OBJ_ALREADY_EXIST;
545                                         }
546                                         else
547                                         {
548                                                 pDeviceEventListenerContainer->__bluetoothHeadset = true;
549                                                 RequireBluetoothEvent();
550                                                 r = E_SUCCESS;
551                                         }
552                                         break;
553                                 }
554                                 case DEVICE_TYPE_CHARGER:
555                                 {
556                                         if(pDeviceEventListenerContainer->__charger == true)
557                                         {
558                                                 r = E_OBJ_ALREADY_EXIST;
559                                         }
560                                         else
561                                         {
562                                                 pDeviceEventListenerContainer->__charger = true;
563                                                 r = E_SUCCESS;
564                                         }
565                                         break;
566                                 }
567                                 case DEVICE_TYPE_USB_CLIENT:
568                                 {
569                                         if(pDeviceEventListenerContainer->__usbClient == true)
570                                         {
571                                                 r = E_OBJ_ALREADY_EXIST;
572                                         }
573                                         else
574                                         {
575                                                 pDeviceEventListenerContainer->__usbClient = true;
576                                                 r = E_SUCCESS;
577                                         }
578                                         break;
579                                 }
580                                 case DEVICE_TYPE_TV_OUT:
581                                 {
582                                         bool tvOut = false;
583                                         _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
584
585                                         if (tvOut == false)
586                                         {
587                                                 if(!_AppInfo::IsOspCompat())
588                                                 {
589                                                         r = E_UNSUPPORTED_OPERATION;
590                                                 }
591                                                 else
592                                                 {
593                                                         r = E_DEVICE_UNAVAILABLE;
594                                                 }
595                                                 break;
596                                         }
597
598                                         if(pDeviceEventListenerContainer->__tvOut == true)
599                                         {
600                                                 r = E_OBJ_ALREADY_EXIST;
601                                         }
602                                         else
603                                         {
604                                                 pDeviceEventListenerContainer->__tvOut = true;
605                                                 r = E_SUCCESS;
606                                         }
607                                         break;
608                                 }
609                                 case DEVICE_TYPE_WIRED_HEADSET:
610                                 {
611                                         if(pDeviceEventListenerContainer->__wiredHeadset == true)
612                                         {
613                                                 r = E_OBJ_ALREADY_EXIST;
614                                         }
615                                         else
616                                         {
617                                                 pDeviceEventListenerContainer->__wiredHeadset = true;
618                                                 r = E_SUCCESS;
619                                         }
620                                         break;
621                                 }
622                                 case DEVICE_TYPE_WIRED_HEADPHONE:
623                                 {
624                                         if(pDeviceEventListenerContainer->__wiredHeadphone == true)
625                                         {
626                                                 r = E_OBJ_ALREADY_EXIST;
627                                         }
628                                         else
629                                         {
630                                                 pDeviceEventListenerContainer->__wiredHeadphone = true;
631                                                 r = E_SUCCESS;
632                                         }
633                                         break;
634                                 }
635                                 case DEVICE_TYPE_STORAGE_CARD:
636                                 {
637                                         if(pDeviceEventListenerContainer->__storageCard == true)
638                                         {
639                                                 r = E_OBJ_ALREADY_EXIST;
640                                         }
641                                         else
642                                         {
643                                                 pDeviceEventListenerContainer->__storageCard = true;
644                                                 r = E_SUCCESS;
645                                         }
646                                         break;
647                                 }
648                                 case DEVICE_TYPE_KEYBOARD:
649                                 {
650                                         bool keyboard = false;
651                                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
652
653                                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
654
655                                         if (keyboard == false)
656                                         {
657                                                 if(!_AppInfo::IsOspCompat())
658                                                 {
659                                                         return E_UNSUPPORTED_OPERATION;
660                                                 }
661                                                 else
662                                                 {
663                                                         return E_DEVICE_UNAVAILABLE;
664                                                 }
665                                         }
666
667                                         if(pDeviceEventListenerContainer->__keyboard == true)
668                                         {
669                                                 r = E_OBJ_ALREADY_EXIST;
670                                         }
671                                         else
672                                         {
673                                                 pDeviceEventListenerContainer->__keyboard = true;
674                                                 r = E_SUCCESS;
675                                         }
676                                         break;
677                                 }
678                                 case DEVICE_TYPE_HDMI:
679                                 {
680                                         int value = 0;
681                                         int ret = 0;
682                                         bool supported = false;
683                                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_HDMI, supported);
684                                         SysTryReturnResult(NID_SYS, supported == true, E_UNSUPPORTED_OPERATION, "Current device does not support HDMI.");
685
686                                         ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
687                                         SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
688                                         SysTryReturnResult(NID_SYS, value >= 0, E_UNSUPPORTED_OPERATION, "Current device does not support HDMI.");
689
690                                         if(pDeviceEventListenerContainer->__hdmi == true)
691                                         {
692                                                 r = E_OBJ_ALREADY_EXIST;
693                                         }
694                                         else
695                                         {
696                                                 pDeviceEventListenerContainer->__hdmi = true;
697                                                 r = E_SUCCESS;
698                                         }
699                                         break;
700                                 }
701                                 }
702                         }
703                 }
704         }
705         return r;
706 }
707
708 result
709 _DeviceManagerImpl::AddDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
710 {
711         SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
712         SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
713         result r = E_SUCCESS;
714
715         if(deviceType == DEVICE_TYPE_TV_OUT)
716         {
717                 bool tvOut = false;
718                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
719                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
720
721                 if (tvOut == false)
722                 {
723                         if(!_AppInfo::IsOspCompat())
724                         {
725                                 r = E_UNSUPPORTED_OPERATION;
726                         }
727                         else
728                         {
729                                 r = E_DEVICE_UNAVAILABLE;
730                         }
731                         return r;
732                 }
733         }
734         else if(deviceType == DEVICE_TYPE_KEYBOARD)
735         {
736                 bool keyboard = false;
737                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
738
739                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
740
741                 if (keyboard == false)
742                 {
743                         if(!_AppInfo::IsOspCompat())
744                         {
745                                 r = E_UNSUPPORTED_OPERATION;
746                         }
747                         else
748                         {
749                                 r = E_DEVICE_UNAVAILABLE;
750                         }
751                         return r;
752                 }
753         }
754         else if(deviceType ==  DEVICE_TYPE_HDMI)
755         {
756                 int value = 0;
757                 int ret = 0;
758                 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
759                 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
760                 if(value < 0)
761                 {
762                         return E_UNSUPPORTED_OPERATION;
763                 }
764         }
765
766         r = AddOnExistedListener(deviceType, pListener);
767
768         if(r == E_OBJ_NOT_FOUND)
769         {
770                 unique_ptr<_DeviceEventListenerContainer> pDeviceEventContainer(new (std::nothrow) _DeviceEventListenerContainer());
771                 SysTryReturnResult(NID_SYS, pDeviceEventContainer != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceEventListenerContainer");
772
773                 unique_ptr<_DeviceManagerEvent> pDeviceManagerEvent(new (std::nothrow) _DeviceManagerEvent());
774                 SysTryReturnResult(NID_SYS, pDeviceManagerEvent != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceManagerEvent");
775
776                 pDeviceManagerEvent->AddListener(*pListener);
777                 pDeviceEventContainer->__pListener = pListener;
778                 pDeviceEventContainer->__pEvent = pDeviceManagerEvent.release();
779
780                 switch(deviceType)
781                 {
782                 case DEVICE_TYPE_BLUETOOTH_HEADSET:
783                 {
784                         pDeviceEventContainer->__bluetoothHeadset = true;
785                         RequireBluetoothEvent();
786                         break;
787                 }
788                 case DEVICE_TYPE_CHARGER:
789                 {
790                         pDeviceEventContainer->__charger = true;
791                         break;
792                 }
793                 case DEVICE_TYPE_USB_CLIENT:
794                 {
795                         pDeviceEventContainer->__usbClient = true;
796                         break;
797                 }
798                 case DEVICE_TYPE_TV_OUT:
799                 {
800                         bool tvOut = false;
801                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
802                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
803
804                         if (tvOut == false)
805                         {
806                                 if(!_AppInfo::IsOspCompat())
807                                 {
808                                         return E_UNSUPPORTED_OPERATION;
809                                 }
810                                 else
811                                 {
812                                         return E_DEVICE_UNAVAILABLE;
813                                 }
814                         }
815
816                         pDeviceEventContainer->__tvOut = true;
817                         break;
818                 }
819                 case DEVICE_TYPE_WIRED_HEADSET:
820                 {
821                         pDeviceEventContainer->__wiredHeadset = true;
822                         break;
823                 }
824                 case DEVICE_TYPE_WIRED_HEADPHONE:
825                 {
826                         pDeviceEventContainer->__wiredHeadphone = true;
827                         break;
828                 }
829                 case DEVICE_TYPE_STORAGE_CARD:
830                 {
831                         pDeviceEventContainer->__storageCard = true;
832                         break;
833                 }
834                 case DEVICE_TYPE_KEYBOARD:
835                 {
836                         bool keyboard = false;
837                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
838
839                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
840
841                         if (keyboard == false)
842                         {
843                                 if(!_AppInfo::IsOspCompat())
844                                 {
845                                         return E_UNSUPPORTED_OPERATION;
846                                 }
847                                 else
848                                 {
849                                         return E_DEVICE_UNAVAILABLE;
850                                 }
851                         }
852
853                         pDeviceEventContainer->__keyboard = true;
854                         break;
855                 }
856                 case DEVICE_TYPE_HDMI:
857                 {
858                         int value = 0;
859                         int ret = 0;
860                         ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
861                         SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
862                         if(value < 0)
863                         {
864                                 return E_UNSUPPORTED_OPERATION;
865                         }
866                         pDeviceEventContainer->__hdmi = true;
867                         break;
868                 }
869                 }
870                 r = E_SUCCESS;
871                 __deviceEventList.Add(pDeviceEventContainer.release());
872         }
873
874         return r;
875 }
876
877 result
878 _DeviceManagerImpl::RemoveDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
879 {
880         result r = E_SUCCESS;
881
882         SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
883         SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
884         SysTryReturnResult(NID_SYS, __deviceEventList.GetCount() > 0, E_INVALID_ARG, "There is no registered listener.");
885
886         unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(__deviceEventList.GetEnumeratorN());
887
888         SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
889
890         if(deviceType == DEVICE_TYPE_TV_OUT)
891         {
892                 bool tvOut = false;
893                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
894                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
895
896                 if (tvOut == false)
897                 {
898                         if(!_AppInfo::IsOspCompat())
899                         {
900                                 r = E_UNSUPPORTED_OPERATION;
901                         }
902                         else
903                         {
904                                 r = E_DEVICE_UNAVAILABLE;
905                         }
906                 }
907                 return r;
908         }
909         else if(deviceType == DEVICE_TYPE_KEYBOARD)
910         {
911                 bool keyboard = false;
912                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
913
914                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
915
916                 if (keyboard == false)
917                 {
918                         if(!_AppInfo::IsOspCompat())
919                         {
920                                 r = E_UNSUPPORTED_OPERATION;
921                         }
922                         else
923                         {
924                                 r = E_DEVICE_UNAVAILABLE;
925                         }
926                 }
927         }
928         else if(deviceType ==  DEVICE_TYPE_HDMI)
929         {
930                 int value = 0;
931                 int ret = 0;
932                 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
933                 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
934                 if(value < 0)
935                 {
936                         return E_UNSUPPORTED_OPERATION;
937                 }
938         }
939
940         while(pEnumerator->MoveNext() == E_SUCCESS)
941         {
942                 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
943                 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
944                 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
945
946                 if(pDeviceEventListenerContainer->__pListener == pListener)
947                 {
948                         switch(deviceType)
949                         {
950                         case DEVICE_TYPE_BLUETOOTH_HEADSET:
951                         {
952                                 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
953                                 {
954                                         return E_OBJ_NOT_FOUND;
955                                 }
956                                 pDeviceEventListenerContainer->__bluetoothHeadset = false;
957                                 ReleaseBluetoothEvent();
958                                 break;
959                         }
960                         case DEVICE_TYPE_CHARGER:
961                         {
962                                 if(pDeviceEventListenerContainer->__charger == false)
963                                 {
964                                         return E_OBJ_NOT_FOUND;
965                                 }
966                                 pDeviceEventListenerContainer->__charger = false;
967                                 break;
968                         }
969                         case DEVICE_TYPE_USB_CLIENT:
970                         {
971                                 if(pDeviceEventListenerContainer->__usbClient == false)
972                                 {
973                                         return E_OBJ_NOT_FOUND;
974                                 }
975                                 pDeviceEventListenerContainer->__usbClient = false;
976                                 break;
977                         }
978                         case DEVICE_TYPE_TV_OUT:
979                         {
980                                 if(pDeviceEventListenerContainer->__tvOut == false)
981                                 {
982                                         return E_OBJ_NOT_FOUND;
983                                 }
984                                 pDeviceEventListenerContainer->__tvOut = false;
985                                 break;
986                         }
987                         case DEVICE_TYPE_WIRED_HEADSET:
988                         {
989                                 if(pDeviceEventListenerContainer->__wiredHeadset == false)
990                                 {
991                                         return E_OBJ_NOT_FOUND;
992                                 }
993                                 pDeviceEventListenerContainer->__wiredHeadset = false;
994                                 break;
995                         }
996                         case DEVICE_TYPE_WIRED_HEADPHONE:
997                         {
998                                 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
999                                 {
1000                                         return E_OBJ_NOT_FOUND;
1001                                 }
1002                                 pDeviceEventListenerContainer->__wiredHeadphone = false;
1003                                 break;
1004                         }
1005                         case DEVICE_TYPE_STORAGE_CARD:
1006                         {
1007                                 if(pDeviceEventListenerContainer->__storageCard == false)
1008                                 {
1009                                         return E_OBJ_NOT_FOUND;
1010                                 }
1011                                 pDeviceEventListenerContainer->__storageCard = false;
1012                                 break;
1013                         }
1014                         case DEVICE_TYPE_KEYBOARD:
1015                         {
1016                                 if(pDeviceEventListenerContainer->__keyboard == false)
1017                                 {
1018                                         return E_OBJ_NOT_FOUND;
1019                                 }
1020                                 pDeviceEventListenerContainer->__keyboard = false;
1021                                 break;
1022                         }
1023                         case DEVICE_TYPE_HDMI:
1024                         {
1025                                 if(pDeviceEventListenerContainer->__hdmi == false)
1026                                 {
1027                                         return E_OBJ_NOT_FOUND;
1028                                 }
1029                                 pDeviceEventListenerContainer->__hdmi = false;
1030                                 break;
1031                         }
1032                         }
1033
1034                         if(pDeviceEventListenerContainer->IsInvalid() == true)
1035                         {
1036                                 __deviceEventList.Remove(pDeviceEventListenerContainer);
1037                                 delete pDeviceEventListenerContainer;
1038                         }
1039                         return E_SUCCESS;
1040                 }
1041         }
1042         return E_OBJ_NOT_FOUND;
1043 }
1044
1045 result
1046 _DeviceManagerImpl::RemoveAllDeviceEventListeners(void)
1047 {
1048         result r = E_SUCCESS;
1049
1050         IEnumeratorT<_DeviceEventListenerContainer*>* pEnumerator = __deviceEventList.GetEnumeratorN();
1051         SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
1052
1053         while(pEnumerator->MoveNext() == E_SUCCESS)
1054         {
1055                 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
1056                 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
1057                 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
1058                 __deviceEventList.Remove(pDeviceEventListenerContainer);
1059                 delete pDeviceEventListenerContainer;
1060         }
1061         delete pEnumerator;
1062
1063         __bluetoothReferenceCount = 1;
1064
1065         ReleaseBluetoothEvent();
1066         return r;
1067 }
1068
1069 void
1070 _DeviceManagerImpl::SendEvent(runtime_info_key_e key)
1071 {
1072         DeviceType type = DEVICE_TYPE_USB_CLIENT;
1073         String value;
1074         int flag = 0;
1075         int ret = 0;
1076
1077         switch (key)
1078         {
1079         case RUNTIME_INFO_KEY_USB_CONNECTED:
1080         {
1081                 flag = _DEVICE_MANAGER_USB_CLIENT;
1082                 type = DEVICE_TYPE_USB_CLIENT;
1083
1084                 bool state = false;
1085                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &state);
1086                 SysTryReturnVoidResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get usb state.");
1087
1088                 if (state == true)
1089                 {
1090                         value = _DEVICE_MANAGER_STATE_INSERTED;
1091                 }
1092                 else
1093                 {
1094                         value = _DEVICE_MANAGER_STATE_REMOVED;
1095                 }
1096                 break;
1097         }
1098         case RUNTIME_INFO_KEY_CHARGER_CONNECTED:
1099         {
1100                 flag = _DEVICE_MANAGER_CHARGER;
1101                 type = DEVICE_TYPE_CHARGER;
1102
1103                 bool state = false;
1104                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &state);
1105                 SysTryReturnVoidResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get charger state.");
1106
1107                 if (state == true)
1108                 {
1109                         value = _DEVICE_MANAGER_STATE_INSERTED;
1110                 }
1111                 else
1112                 {
1113                         value = _DEVICE_MANAGER_STATE_REMOVED;
1114                 }
1115                 break;
1116         }
1117         case RUNTIME_INFO_KEY_TV_OUT_CONNECTED:
1118         {
1119                 flag = _DEVICE_MANAGER_TV_OUT;
1120                 type = DEVICE_TYPE_TV_OUT;
1121
1122                 bool state = false;
1123                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, &state);
1124                 SysTryReturnVoidResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get tv connect state.");
1125
1126                 if (state == true)
1127                 {
1128                         value = _DEVICE_MANAGER_STATE_INSERTED;
1129                 }
1130                 else
1131                 {
1132                         value = _DEVICE_MANAGER_STATE_REMOVED;
1133                 }
1134                 break;
1135         }
1136         case RUNTIME_INFO_KEY_AUDIO_JACK_STATUS:
1137         {
1138                 int state = 0;
1139                 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &state);
1140                 SysTryReturnVoidResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get audio connect state.");
1141
1142                 if (state == RUNTIME_INFO_AUDIO_JACK_STATUS_UNCONNECTED)
1143                 {
1144                         if (__headSetType == DEVICE_TYPE_WIRED_HEADPHONE)
1145                         {
1146                                 flag = _DEVICE_MANAGER_WIRED_HEADPHONE;
1147                                 type = DEVICE_TYPE_WIRED_HEADPHONE;
1148                         }
1149                         else
1150                         {
1151                                 flag = _DEVICE_MANAGER_WIRED_HEADSET;
1152                                 type = DEVICE_TYPE_WIRED_HEADSET;
1153                         }
1154
1155                         value = _DEVICE_MANAGER_STATE_REMOVED;
1156                 }
1157                 else
1158                 {
1159                         if (state == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
1160                         {
1161                                 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
1162                                 flag = _DEVICE_MANAGER_WIRED_HEADPHONE;
1163                                 type = DEVICE_TYPE_WIRED_HEADPHONE;
1164                         }
1165                         else
1166                         {
1167                                 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
1168                                 flag = _DEVICE_MANAGER_WIRED_HEADSET;
1169                                 type = DEVICE_TYPE_WIRED_HEADSET;
1170                         }
1171                         value = _DEVICE_MANAGER_STATE_INSERTED;
1172
1173                 }
1174                 break;
1175         }
1176         case RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED:
1177         {
1178                 flag = _DEVICE_MANAGER_KEYBOARD;
1179                 type = DEVICE_TYPE_KEYBOARD;
1180
1181                 bool state = false;
1182                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, &state);
1183                 SysTryReturnVoidResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get built-in keyboard state.");
1184
1185                 if (state == true)
1186                 {
1187                         value = _DEVICE_MANAGER_STATE_OPENED;
1188                 }
1189                 else
1190                 {
1191                         value = _DEVICE_MANAGER_STATE_CLOSED;
1192                 }
1193                 break;
1194         }
1195         default:
1196                 break;
1197         }
1198
1199         __pDeviceManagerImpl->SendEvent(type, value);
1200
1201 }
1202
1203 result
1204 _DeviceManagerImpl::GetState(DeviceType deviceType, String& state)
1205 {
1206         result r = E_SUCCESS;
1207         int value = 0;
1208         int ret = 0;
1209
1210         SysTryReturnResult(NID_SYS, GetIpcClient() != null, E_SYSTEM, "GetDeviceState Failed");
1211
1212         switch (deviceType)
1213         {
1214
1215         case DEVICE_TYPE_BLUETOOTH_HEADSET:
1216         {
1217                 bool bluetooth = false;
1218                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_NETWORK_BLUETOOTH, bluetooth);
1219                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option.");
1220                 if (bluetooth == false)
1221                 {
1222                         if(!_AppInfo::IsOspCompat())
1223                         {
1224                                 return E_UNSUPPORTED_OPERATION;
1225                         }
1226                         else
1227                         {
1228                                 return E_DEVICE_UNAVAILABLE;
1229                         }
1230                 }
1231
1232                 ArrayList requestMessages;
1233                 ArrayList responseMessages;
1234
1235                 requestMessages.Construct();
1236                 responseMessages.Construct();
1237
1238                 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
1239                 String commandId = _DEVICE_MANAGER_COMMAND_STATUS;
1240                 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
1241
1242                 requestMessages.Add(serviceId);
1243                 requestMessages.Add(commandId);
1244                 requestMessages.Add(deviceId);
1245
1246                 unique_ptr<IoService_Request> pMsg(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
1247
1248                 r = GetIpcClient()->SendRequest(*(pMsg.get()));
1249
1250                 String* pMessageData = (String*)responseMessages.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_DATA);
1251                 if (pMessageData != null)
1252                 {
1253                         state = *pMessageData;
1254                 }
1255                 responseMessages.RemoveAll(true);
1256                 break;
1257         }
1258
1259         case DEVICE_TYPE_CHARGER:
1260         {
1261                 bool chargerState = false;
1262                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_CHARGER_CONNECTED, &chargerState);
1263                 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get charger state");
1264
1265                 if (chargerState == true)
1266                 {
1267                         state = _DEVICE_MANAGER_STATE_INSERTED;
1268                 }
1269                 else
1270                 {
1271                         state = _DEVICE_MANAGER_STATE_REMOVED;
1272                 }
1273                 break;
1274         }
1275
1276         case DEVICE_TYPE_USB_CLIENT:
1277         {
1278                 bool cableState = false;
1279                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &cableState);
1280                 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get usb state");
1281
1282                 if (cableState == true)
1283                 {
1284                         state = _DEVICE_MANAGER_STATE_INSERTED;
1285                 }
1286                 else
1287                 {
1288                         state = _DEVICE_MANAGER_STATE_REMOVED;
1289                 }
1290                 break;
1291         }
1292
1293         case DEVICE_TYPE_TV_OUT:
1294         {
1295                 bool tvOut = false;
1296                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
1297                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
1298
1299                 if (tvOut == false)
1300                 {
1301                         if(!_AppInfo::IsOspCompat())
1302                         {
1303                                 return E_UNSUPPORTED_OPERATION;
1304                         }
1305                         else
1306                         {
1307                                 return E_DEVICE_UNAVAILABLE;
1308                         }
1309                 }
1310
1311                 bool tvState = false;
1312                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, &tvState);
1313                 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get tv state");
1314                 if (tvState == true)
1315                 {
1316                         state = _DEVICE_MANAGER_STATE_INSERTED;
1317                 }
1318                 else
1319                 {
1320                         state = _DEVICE_MANAGER_STATE_REMOVED;
1321                 }
1322                 break;
1323         }
1324
1325         case DEVICE_TYPE_WIRED_HEADSET:
1326         {
1327                 int audioState = 0;
1328                 ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &audioState);
1329                 SysTryReturnResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get audio state");
1330
1331                 if (audioState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
1332                 {
1333                         state = _DEVICE_MANAGER_STATE_INSERTED;
1334                 }
1335                 else
1336                 {
1337                         state = _DEVICE_MANAGER_STATE_REMOVED;
1338                 }
1339                 break;
1340         }
1341
1342         case DEVICE_TYPE_WIRED_HEADPHONE:
1343         {
1344                 int audioState = 0;
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");
1347
1348                 if (audioState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
1349                 {
1350                         state = _DEVICE_MANAGER_STATE_INSERTED;
1351                 }
1352                 else
1353                 {
1354                         state = _DEVICE_MANAGER_STATE_REMOVED;
1355                 }
1356                 break;
1357         }
1358
1359         case DEVICE_TYPE_STORAGE_CARD:
1360         {
1361                 int ret = 0;
1362                 if (!_AppInfo::IsOspCompat() && !_AppInfo::IsVirtualRoot())
1363                 {
1364                         ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
1365                 }
1366                 else
1367                 {
1368                         ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
1369                 }
1370                 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
1371                 if (value == 1)
1372                 {
1373                         state = _DEVICE_MANAGER_STATE_MOUNTED;
1374                 }
1375                 else
1376                 {
1377                         state = _DEVICE_MANAGER_STATE_UNMOUNTED;
1378                 }
1379                 break;
1380         }
1381
1382         case DEVICE_TYPE_KEYBOARD:
1383         {
1384                 bool keyboard = false;
1385                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
1386                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to get keyboard option.");
1387                 if (keyboard == false)
1388                 {
1389                         if(!_AppInfo::IsOspCompat())
1390                         {
1391                                 return E_UNSUPPORTED_OPERATION;
1392                         }
1393                         else
1394                         {
1395                                 return E_DEVICE_UNAVAILABLE;
1396                         }
1397                 }
1398                 break;
1399         }
1400         case DEVICE_TYPE_HDMI:
1401         {
1402                 int ret = 0;
1403                 bool supported = false;
1404                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_HDMI, supported);
1405                 SysTryReturnResult(NID_SYS, supported == true, E_UNSUPPORTED_OPERATION, "Current device does not support HDMI.");
1406
1407                 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
1408                 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
1409                 if (value == 1)
1410                 {
1411                         state = _DEVICE_MANAGER_STATE_INSERTED;
1412                 }
1413                 else if(value == 0)
1414                 {
1415                         state = _DEVICE_MANAGER_STATE_REMOVED;
1416                 }
1417                 else
1418                 {
1419                         return E_UNSUPPORTED_OPERATION;
1420                 }
1421
1422                 break;
1423         }
1424
1425         default:
1426                 return E_INVALID_ARG;
1427         }
1428         return E_SUCCESS;
1429 }
1430
1431 void
1432 _DeviceManagerImpl::OnDataReceived(const ArrayList& data)
1433 {
1434         StringComparer stringComparer;
1435
1436         int cmp = 0;
1437         String* pServiceId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID);
1438         String* pCommandId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID);
1439         String* pDeviceId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID);
1440         String* pEventId = (String*) data.GetAt(_OSP_APP_SERVICE_IPC_MESSAGE_DATA);
1441
1442         String serviceId = _DEVICE_MANAGER_SERVICE_ID;
1443         String commandId = _DEVICE_MANAGER_COMMAND_EVENT;
1444         String deviceId = _DEVICE_MANAGER_BLUETOOTH;
1445
1446         SysTryReturnVoidResult(NID_SYS, pServiceId != null && pCommandId != null && pDeviceId != null && pEventId != null, E_SYSTEM, "There is no device data.");
1447
1448         stringComparer.Compare(*pServiceId, serviceId, cmp);
1449         if (cmp == 0)
1450         {
1451                 stringComparer.Compare(*pCommandId, commandId, cmp);
1452                 if (cmp == 0)
1453                 {
1454                         stringComparer.Compare(*pDeviceId, deviceId, cmp);
1455                         if (cmp == 0)
1456                         {
1457                                 __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_BLUETOOTH_HEADSET, *pEventId);
1458                         }
1459                 }
1460         }
1461 }
1462
1463 } } // Tizen::System