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