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