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