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