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