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