Merge "Flow control for DataControl" into tizen_2.1
[platform/framework/native/appfw.git] / src / system / FSys_DeviceManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_DeviceManagerImpl.cpp
20  * @brief               This is the implementation file for _DeviceManagerImpl class.
21  */
22
23 #include <pthread.h>
24 #include <unique_ptr.h>
25 #include <system_info.h>
26 #include <runtime_info.h>
27 #include <stdlib.h>
28 #include <vconf.h>
29
30 #include <FBaseColArrayList.h>
31 #include <FBaseRtEvent.h>
32 #include <FBaseRtIEventArg.h>
33 #include <FBaseStringComparer.h>
34 #include <FBaseSysLog.h>
35 #include <FSysIDeviceEventListener.h>
36
37 #include <FApp_AppInfo.h>
38 #include <FBase_NativeError.h>
39 #include <FIo_AppServiceIpcMessages.h>
40 #include <FIo_IpcClient.h>
41 #include <FSys_SystemInfoImpl.h>
42 #include <FSys_PowerManagerImpl.h>
43
44 #include "FSys_CommunicationDispatcherClient.h"
45 #include "FSys_DeviceManagerImpl.h"
46
47 #define VCONFKEY_APPSERVICE_MMC_STATUS  "memory/appservice/mmc"
48
49 using namespace Tizen::App;
50 using namespace Tizen::Base;
51 using namespace Tizen::Base::Collection;
52 using namespace Tizen::Base::Runtime;
53 using namespace Tizen::Io;
54
55 namespace Tizen { namespace System
56 {
57
58 static const wchar_t* _DEVICE_MANAGER_SERVICE_ID = L"osp.devicemanager.service";
59 static const wchar_t* _DEVICE_MANAGER_COMMAND_OPEN = L"osp.devicemanager.command.open";
60 static const wchar_t* _DEVICE_MANAGER_COMMAND_CLOSE = L"osp.devicemanager.command.close";
61 static const wchar_t* _DEVICE_MANAGER_COMMAND_STATUS = L"osp.devicemanager.command.status";
62 static const wchar_t* _DEVICE_MANAGER_COMMAND_EVENT = L"osp.devicemanager.command.event";
63 static const wchar_t* _DEVICE_MANAGER_BLUETOOTH = L"osp.devicemanager.bluetooth";
64
65 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_SERVICE_ID = 0;
66 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_COMMAND_ID = 1;
67 const int _OSP_APP_SERVICE_IPC_MESSAGE_HEAD_DEVICE_ID = 2;
68 const int _OSP_APP_SERVICE_IPC_MESSAGE_DATA = 3;
69
70 const int _DEVICE_MANAGER_BLUETOOTH_HEADSET = 0x01;
71 const int _DEVICE_MANAGER_CHARGER = 0x02;
72 const int _DEVICE_MANAGER_USB_CLIENT = 0x04;
73 const int _DEVICE_MANAGER_TV_OUT = 0x08;
74 const int _DEVICE_MANAGER_WIRED_HEADSET = 0x10;
75 const int _DEVICE_MANAGER_WIRED_HEADPHONE = 0x20;
76 const int _DEVICE_MANAGER_STORAGECARD = 0x40;
77 const int _DEVICE_MANAGER_KEYBOARD = 0x80;
78
79 static const wchar_t* _DEVICE_MANAGER_STATE_INSERTED = L"Inserted";
80 static const wchar_t* _DEVICE_MANAGER_STATE_REMOVED = L"Removed";
81 static const wchar_t* _DEVICE_MANAGER_STATE_MOUNTED = L"Mounted";
82 static const wchar_t* _DEVICE_MANAGER_STATE_UNMOUNTED = L"Unmounted";
83 static const wchar_t* _DEVICE_MANAGER_STATE_OPENED = L"Opened";
84 static const wchar_t* _DEVICE_MANAGER_STATE_CLOSED = L"Closed";
85
86 static const wchar_t* _SYSTEM_INFO_NETWORK_BLUETOOTH = L"http://tizen.org/feature/network.bluetooth";
87 static const wchar_t* _SYSTEM_INFO_INPUT_KEYBOARD = L"http://tizen.org/feature/input.keyboard";
88 static const wchar_t* _SYSTEM_INFO_TVOUT_SUPPORTED = L"http://tizen.org/feature/screen.output.rca";
89 static const wchar_t* _SYSTEM_INFO_HDMI = L"http://tizen.org/feature/screen.output.hdmi";
90
91 _DeviceManagerImpl* _DeviceManagerImpl::__pDeviceManagerImpl = null;
92
93 class _DeviceManagerEventArg : public IEventArg
94 {
95 public:
96         _DeviceManagerEventArg()
97         {
98         }
99         DeviceType deviceType;
100         String state;
101 };
102
103 class _DeviceManagerEvent : public Event
104 {
105 protected:
106         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
107         {
108                 IDeviceEventListener* pListener = dynamic_cast<IDeviceEventListener*> (&listener);
109                 const _DeviceManagerEventArg* pArg = dynamic_cast<const _DeviceManagerEventArg*>(&arg);
110
111                 SysTryReturnVoidResult(NID_SYS, pListener != null && pArg != null, E_SYSTEM, "Parameters are not available.");
112
113                 pListener->OnDeviceStateChanged(pArg->deviceType, pArg->state);
114         }
115 };
116
117
118 void
119 _DeviceManagerImpl::OnDeviceStateChanged(runtime_info_key_e key, void* pData)
120 {
121         if (__pDeviceManagerImpl != null)
122         {
123                 __pDeviceManagerImpl->SendEvent(key);
124         }
125 }
126
127 void
128 _DeviceManagerImpl::InitDeviceManagerImpl(void)
129 {
130         static _DeviceManagerImpl deviceManagerImpl;
131         __pDeviceManagerImpl = &deviceManagerImpl;
132 }
133 _DeviceManagerImpl*
134 _DeviceManagerImpl::GetInstance(void)
135 {
136         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
137         if(__pDeviceManagerImpl == null)
138         {
139                 pthread_once(&once_block, InitDeviceManagerImpl);
140         }
141         return __pDeviceManagerImpl;
142 }
143
144 _DeviceManagerImpl::_DeviceManagerImpl()
145         : __pIpcClient(null)
146         , __headSetType(DEVICE_TYPE_WIRED_HEADPHONE)
147         , __bluetoothReferenceCount(0)
148 {
149         result r = E_SUCCESS;
150         int headsetState = 0;
151         int ret = 0;
152         static String DEVICE_MANAGER_SERVICE_ID(_DEVICE_MANAGER_SERVICE_ID);
153
154         _CommunicationDispatcherClient* pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
155         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
156
157         r = pCommunicationDispatcherClient->RegisterCommunicationListener(DEVICE_MANAGER_SERVICE_ID, *this);
158         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
159
160         __pIpcClient = pCommunicationDispatcherClient->GetIpcClient();
161
162         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED, OnDeviceStateChanged, null);
163         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register USB event");
164
165         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_CHARGER_CONNECTED, OnDeviceStateChanged, null);
166         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register DEVICE_TYPE_CHARGER event");
167
168         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED, OnDeviceStateChanged, null);
169         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register TV out event");
170
171         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, OnDeviceStateChanged, null);
172         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register audio jack event");
173
174         ret = runtime_info_set_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED, OnDeviceStateChanged, null);
175         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to register sliding keyboard event");
176
177         ret = runtime_info_get_value_int(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS, &headsetState);
178         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to get audio jack status");
179
180         if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_3WIRE)
181         {
182                 __headSetType = DEVICE_TYPE_WIRED_HEADPHONE;
183         }
184         else if (headsetState == RUNTIME_INFO_AUDIO_JACK_STATUS_CONNECTED_4WIRE)
185         {
186                 __headSetType = DEVICE_TYPE_WIRED_HEADSET;
187         }
188
189         if (!_AppInfo::IsOspCompat())
190         {
191                 ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack, null);
192         }
193         else
194         {
195                 ret = vconf_notify_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback, null);
196         }
197         SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to register MMC event callback.");
198
199         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack, null);
200         SysTryCatch(NID_SYS, ret == 0, r = E_SYSTEM, r, "It is failed to register HDMI event");
201
202         __deviceEventList.Construct();
203
204 CATCH:
205         SetLastResult(r);
206 }
207
208 _DeviceManagerImpl::~_DeviceManagerImpl()
209 {
210         result r = E_SUCCESS;
211         int ret = 0;
212         _CommunicationDispatcherClient* pCommunicationDispatcherClient = null;
213         String key(_DEVICE_MANAGER_SERVICE_ID);
214
215         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_USB_CONNECTED);
216         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister USB event");
217
218         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_TV_OUT_CONNECTED);
219         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister TV out event");
220
221         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_AUDIO_JACK_STATUS);
222         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister audio jack event");
223
224         ret = runtime_info_unset_changed_cb(RUNTIME_INFO_KEY_SLIDING_KEYBOARD_OPENED);
225         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister sliding keyboard event");
226
227         if (!_AppInfo::IsOspCompat())
228         {
229                 ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, DeviceEventVConfCallBack);
230         }
231         else
232         {
233                 ret = vconf_ignore_key_changed(VCONFKEY_APPSERVICE_MMC_STATUS, MmcEventVconfCallback);
234         }
235         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister MMC event");
236
237         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_HDMI, DeviceEventVConfCallBack);
238         SysTryCatch(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, r = E_SYSTEM, r, "It is failed to unregister HDMI event");
239
240
241         pCommunicationDispatcherClient = _CommunicationDispatcherClient::GetInstance();
242         SysTryCatch(NID_SYS, pCommunicationDispatcherClient != null, r = E_SYSTEM, r, "It is failed to get CommunicationDispatcherClient.");
243
244         r = pCommunicationDispatcherClient->UnregisterCommunicationListener(key);
245         SysTryCatch(NID_SYS, r == E_SUCCESS, r = E_SYSTEM, r, "It is failed to register on CommunicationDispatcherClient.");
246
247         __pIpcClient = null;
248
249 CATCH:
250         SetLastResult(r);
251 }
252
253 void
254 _DeviceManagerImpl::DeviceEventVConfCallBack(keynode_t* node, void* userData)
255 {
256         int ret = 0;
257         int value = 0;
258         String event;
259
260         if (strcmp(VCONFKEY_SYSMAN_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
261         {
262                 SysLog(NID_SYS, "MMC callback is occured");
263                 if (__pDeviceManagerImpl != null)
264                 {
265                         ret = vconf_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &value);
266                         if (value == 1)
267                         {
268                                 event = _DEVICE_MANAGER_STATE_MOUNTED;
269                         }
270                         else
271                         {
272                                 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
273                         }
274                         __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
275                 }
276         }
277         else if(strcmp(VCONFKEY_SYSMAN_HDMI, vconf_keynode_get_name(node)) == 0)
278         {
279                 if (__pDeviceManagerImpl != null)
280                 {
281                         ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
282                         if (value == 1)
283                         {
284                                 event = _DEVICE_MANAGER_STATE_INSERTED;
285                         }
286                         else
287                         {
288                                 event = _DEVICE_MANAGER_STATE_REMOVED;
289                         }
290                         __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_HDMI, event);
291                 }
292
293         }
294 }
295
296 void
297 _DeviceManagerImpl::MmcEventVconfCallback(keynode_t* node, void* userData)
298 {
299         if (strcmp(VCONFKEY_APPSERVICE_MMC_STATUS, vconf_keynode_get_name(node)) == 0)
300         {
301                 SysLog(NID_SYS, "MMC event callback is called.");
302                 if (__pDeviceManagerImpl != NULL)
303                 {
304                         String event;
305                         int value = 0;
306                         int ret = vconf_get_int(VCONFKEY_APPSERVICE_MMC_STATUS, &value);
307                         SysTryReturnVoidResult(NID_SYS, ret == 0, E_SYSTEM, "VCONFKEY_APPSERVICE_MMC_STATUS is error");
308
309                         if (value == 1)
310                         {
311                                 event = _DEVICE_MANAGER_STATE_MOUNTED;
312                         }
313                         else
314                         {
315                                 event = _DEVICE_MANAGER_STATE_UNMOUNTED;
316                         }
317                         __pDeviceManagerImpl->SendEvent(DEVICE_TYPE_STORAGE_CARD, event);
318                 }
319         }
320 }
321
322 void
323 _DeviceManagerImpl::SendEvent(DeviceType deviceType, String& state)
324 {
325         std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
326
327         if(__deviceEventList.GetCount() > 0)
328         {
329                 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
330                 SysTryReturnVoidResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
331
332                 while(pEnumerator->MoveNext() == E_SUCCESS)
333                 {
334                         _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
335                         pEnumerator->GetCurrent(pDeviceEventListenerContainer);
336                         SysTryReturnVoidResult(NID_SYS, pDeviceEventListenerContainer != null,E_SYSTEM, "Container is empty");
337
338                         switch(deviceType)
339                         {
340                         case DEVICE_TYPE_BLUETOOTH_HEADSET:
341                         {
342                                 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
343                                 {
344                                         return;
345                                 }
346                                 break;
347                         }
348                         case DEVICE_TYPE_CHARGER:
349                         {
350                                 if(pDeviceEventListenerContainer->__charger == false)
351                                 {
352                                         return;
353                                 }
354                                 break;
355                         }
356                         case DEVICE_TYPE_USB_CLIENT:
357                         {
358                                 if(pDeviceEventListenerContainer->__usbClient == false)
359                                 {
360                                         return;
361                                 }
362                                 break;
363                         }
364                         case DEVICE_TYPE_TV_OUT:
365                         {
366                                 if(pDeviceEventListenerContainer->__tvOut == false)
367                                 {
368                                         return;
369                                 }
370                                 break;
371                         }
372                         case DEVICE_TYPE_WIRED_HEADSET:
373                         {
374                                 if(pDeviceEventListenerContainer->__wiredHeadset == false)
375                                 {
376                                         return;
377                                 }
378                                 break;
379                         }
380                         case DEVICE_TYPE_WIRED_HEADPHONE:
381                         {
382                                 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
383                                 {
384                                         return;
385                                 }
386                                 break;
387                         }
388                         case DEVICE_TYPE_STORAGE_CARD:
389                         {
390                                 if(pDeviceEventListenerContainer->__storageCard == false)
391                                 {
392                                         return;
393                                 }
394                                 break;
395                         }
396                         case DEVICE_TYPE_KEYBOARD:
397                         {
398                                 if(pDeviceEventListenerContainer->__keyboard == false)
399                                 {
400                                         return;
401                                 }
402                                 break;
403                         }
404                         case DEVICE_TYPE_HDMI:
405                         {
406                                 if(pDeviceEventListenerContainer->__hdmi == false)
407                                 {
408                                         return;
409                                 }
410                                 break;
411                         }
412                         default:
413                                 return;
414                         }
415                         std::unique_ptr<_DeviceManagerEventArg> pDeviceManagerEventArg(new (std::nothrow) _DeviceManagerEventArg());
416                         SysTryReturnVoidResult(NID_SYS, pDeviceManagerEventArg != null, E_OUT_OF_MEMORY, "It is failed to create instance of DeviceManagerEventArg");
417
418                         pDeviceManagerEventArg->deviceType = deviceType;
419                         pDeviceManagerEventArg->state = state;
420
421                         if(pDeviceEventListenerContainer->__pEvent != null)
422                         {
423                                 pDeviceEventListenerContainer->__pEvent->Fire(*(pDeviceManagerEventArg.release()));
424                         }
425                 }
426         }
427 }
428
429 result
430 _DeviceManagerImpl::RequireBluetoothEvent(void)
431 {
432         result r = E_SUCCESS;
433         std::unique_ptr<IoService_Request> pRequest(null);
434
435         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "Ipc Client is not ready");
436
437         __bluetoothReferenceCount ++;
438
439         if(__bluetoothReferenceCount == 1)
440         {
441                 ArrayList requestMessages;
442                 ArrayList responseMessages;
443
444                 r = requestMessages.Construct();
445                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
446                 r = responseMessages.Construct();
447                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
448
449                 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
450                 String commandId = _DEVICE_MANAGER_COMMAND_OPEN;
451                 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
452
453                 r = requestMessages.Add(serviceId);
454                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
455                 r = requestMessages.Add(commandId);
456                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
457                 r = requestMessages.Add(deviceId);
458
459                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add device id (bluetooth)");
460
461                 pRequest.reset(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
462                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send bluetooth event subscribe by IPC");
463
464                 r = __pIpcClient->SendRequest(pRequest.get());
465                 SysTryReturnResult(NID_SYS, r == E_SUCCESS,E_SYSTEM, "It is failed to add bluetooth id");
466         }
467         return r;
468 }
469
470 result
471 _DeviceManagerImpl::ReleaseBluetoothEvent(void)
472 {
473         result r = E_SUCCESS;
474         std::unique_ptr<IoService_Request> pRequest(null);
475         SysTryReturnResult(NID_SYS, __pIpcClient != null, E_SYSTEM, "Ipc Client is not ready");
476
477         __bluetoothReferenceCount --;
478         SysTryReturnResult(NID_SYS, __bluetoothReferenceCount >= 0, E_SYSTEM, "Fail to manage reference count");
479
480         if(__bluetoothReferenceCount == 0)
481         {
482                 ArrayList requestMessages;
483                 ArrayList responseMessages;
484
485                 r = requestMessages.Construct();
486                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create request instance");
487                 r = responseMessages.Construct();
488                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to create response instance");
489
490                 String serviceId = _DEVICE_MANAGER_SERVICE_ID;
491                 String commandId = _DEVICE_MANAGER_COMMAND_CLOSE;
492                 String deviceId = _DEVICE_MANAGER_BLUETOOTH;
493
494                 r = requestMessages.Add(serviceId);
495                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add service id");
496                 r = requestMessages.Add(commandId);
497                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add command id");
498                 r = requestMessages.Add(deviceId);
499                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add bluetooth id");
500
501                 pRequest.reset(new (std::nothrow) IoService_Request(requestMessages, &responseMessages));
502                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send bluetooth event subscribe by IPC");
503
504                 r = __pIpcClient->SendRequest(pRequest.get());
505                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add bluetooth id");
506         }
507         return r;
508 }
509
510 result
511 _DeviceManagerImpl::AddOnExistedListener(DeviceType deviceType, const IDeviceEventListener* pListener)
512 {
513         result r = E_OBJ_NOT_FOUND;
514         std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(null);
515
516         if(__deviceEventList.GetCount() > 0)
517         {
518                 pEnumerator.reset(__deviceEventList.GetEnumeratorN());
519                 SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
520
521                 while(pEnumerator->MoveNext() == E_SUCCESS)
522                 {
523                         _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
524                         pEnumerator->GetCurrent(pDeviceEventListenerContainer);
525                         SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
526                         if(pDeviceEventListenerContainer->__pListener == pListener)
527                         {
528                                 switch(deviceType)
529                                 {
530                                 case DEVICE_TYPE_BLUETOOTH_HEADSET:
531                                 {
532                                         if(pDeviceEventListenerContainer->__bluetoothHeadset == true)
533                                         {
534                                                 r = E_OBJ_ALREADY_EXIST;
535                                         }
536                                         else
537                                         {
538                                                 pDeviceEventListenerContainer->__bluetoothHeadset = true;
539                                                 RequireBluetoothEvent();
540                                                 r = E_SUCCESS;
541                                         }
542                                         break;
543                                 }
544                                 case DEVICE_TYPE_CHARGER:
545                                 {
546                                         if(pDeviceEventListenerContainer->__charger == true)
547                                         {
548                                                 r = E_OBJ_ALREADY_EXIST;
549                                         }
550                                         else
551                                         {
552                                                 pDeviceEventListenerContainer->__charger = true;
553                                                 r = E_SUCCESS;
554                                         }
555                                         break;
556                                 }
557                                 case DEVICE_TYPE_USB_CLIENT:
558                                 {
559                                         if(pDeviceEventListenerContainer->__usbClient == true)
560                                         {
561                                                 r = E_OBJ_ALREADY_EXIST;
562                                         }
563                                         else
564                                         {
565                                                 pDeviceEventListenerContainer->__usbClient = true;
566                                                 r = E_SUCCESS;
567                                         }
568                                         break;
569                                 }
570                                 case DEVICE_TYPE_TV_OUT:
571                                 {
572                                         bool tvOut = false;
573                                         _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
574
575                                         if (tvOut == false)
576                                         {
577                                                 if(!_AppInfo::IsOspCompat())
578                                                 {
579                                                         r = E_UNSUPPORTED_OPERATION;
580                                                 }
581                                                 else
582                                                 {
583                                                         r = E_DEVICE_UNAVAILABLE;
584                                                 }
585                                                 break;
586                                         }
587
588                                         if(pDeviceEventListenerContainer->__tvOut == true)
589                                         {
590                                                 r = E_OBJ_ALREADY_EXIST;
591                                         }
592                                         else
593                                         {
594                                                 pDeviceEventListenerContainer->__tvOut = true;
595                                                 r = E_SUCCESS;
596                                         }
597                                         break;
598                                 }
599                                 case DEVICE_TYPE_WIRED_HEADSET:
600                                 {
601                                         if(pDeviceEventListenerContainer->__wiredHeadset == true)
602                                         {
603                                                 r = E_OBJ_ALREADY_EXIST;
604                                         }
605                                         else
606                                         {
607                                                 pDeviceEventListenerContainer->__wiredHeadset = true;
608                                                 r = E_SUCCESS;
609                                         }
610                                         break;
611                                 }
612                                 case DEVICE_TYPE_WIRED_HEADPHONE:
613                                 {
614                                         if(pDeviceEventListenerContainer->__wiredHeadphone == true)
615                                         {
616                                                 r = E_OBJ_ALREADY_EXIST;
617                                         }
618                                         else
619                                         {
620                                                 pDeviceEventListenerContainer->__wiredHeadphone = true;
621                                                 r = E_SUCCESS;
622                                         }
623                                         break;
624                                 }
625                                 case DEVICE_TYPE_STORAGE_CARD:
626                                 {
627                                         if(pDeviceEventListenerContainer->__storageCard == true)
628                                         {
629                                                 r = E_OBJ_ALREADY_EXIST;
630                                         }
631                                         else
632                                         {
633                                                 pDeviceEventListenerContainer->__storageCard = true;
634                                                 r = E_SUCCESS;
635                                         }
636                                         break;
637                                 }
638                                 case DEVICE_TYPE_KEYBOARD:
639                                 {
640                                         bool keyboard = false;
641                                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
642
643                                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
644
645                                         if (keyboard == false)
646                                         {
647                                                 if(!_AppInfo::IsOspCompat())
648                                                 {
649                                                         return E_UNSUPPORTED_OPERATION;
650                                                 }
651                                                 else
652                                                 {
653                                                         return E_DEVICE_UNAVAILABLE;
654                                                 }
655                                         }
656
657                                         if(pDeviceEventListenerContainer->__keyboard == true)
658                                         {
659                                                 r = E_OBJ_ALREADY_EXIST;
660                                         }
661                                         else
662                                         {
663                                                 pDeviceEventListenerContainer->__keyboard = true;
664                                                 r = E_SUCCESS;
665                                         }
666                                         break;
667                                 }
668                                 case DEVICE_TYPE_HDMI:
669                                 {
670                                         int value = 0;
671                                         int ret = 0;
672                                         bool supported = false;
673                                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_HDMI, supported);
674                                         SysTryReturnResult(NID_SYS, supported == true, E_UNSUPPORTED_OPERATION, "Current device does not support HDMI.");
675
676                                         ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
677                                         SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
678                                         SysTryReturnResult(NID_SYS, value >= 0, E_UNSUPPORTED_OPERATION, "Current device does not support HDMI.");
679
680                                         if(pDeviceEventListenerContainer->__hdmi == true)
681                                         {
682                                                 r = E_OBJ_ALREADY_EXIST;
683                                         }
684                                         else
685                                         {
686                                                 pDeviceEventListenerContainer->__hdmi = true;
687                                                 r = E_SUCCESS;
688                                         }
689                                         break;
690                                 }
691                                 }
692                         }
693                 }
694         }
695         return r;
696 }
697
698 result
699 _DeviceManagerImpl::AddDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
700 {
701         SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
702         SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
703         result r = E_SUCCESS;
704
705         if(deviceType == DEVICE_TYPE_TV_OUT)
706         {
707                 bool tvOut = false;
708                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
709                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
710
711                 if (tvOut == false)
712                 {
713                         if(!_AppInfo::IsOspCompat())
714                         {
715                                 r = E_UNSUPPORTED_OPERATION;
716                         }
717                         else
718                         {
719                                 r = E_DEVICE_UNAVAILABLE;
720                         }
721                         return r;
722                 }
723         }
724         else if(deviceType == DEVICE_TYPE_KEYBOARD)
725         {
726                 bool keyboard = false;
727                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
728
729                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
730
731                 if (keyboard == false)
732                 {
733                         if(!_AppInfo::IsOspCompat())
734                         {
735                                 r = E_UNSUPPORTED_OPERATION;
736                         }
737                         else
738                         {
739                                 r = E_DEVICE_UNAVAILABLE;
740                         }
741                         return r;
742                 }
743         }
744         else if(deviceType ==  DEVICE_TYPE_HDMI)
745         {
746                 int value = 0;
747                 int ret = 0;
748                 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
749                 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
750                 if(value < 0)
751                 {
752                         return E_UNSUPPORTED_OPERATION;
753                 }
754         }
755
756         r = AddOnExistedListener(deviceType, pListener);
757
758         if(r == E_OBJ_NOT_FOUND)
759         {
760                 std::unique_ptr<_DeviceEventListenerContainer> pDeviceEventContainer(new (std::nothrow) _DeviceEventListenerContainer());
761                 SysTryReturnResult(NID_SYS, pDeviceEventContainer != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceEventListenerContainer");
762
763                 std::unique_ptr<_DeviceManagerEvent> pDeviceManagerEvent(new (std::nothrow) _DeviceManagerEvent());
764                 SysTryReturnResult(NID_SYS, pDeviceManagerEvent != null, E_OUT_OF_MEMORY, "It is failed to create instance of _DeviceManagerEvent");
765
766                 pDeviceManagerEvent->AddListener(*pListener);
767                 pDeviceEventContainer->__pListener = pListener;
768                 pDeviceEventContainer->__pEvent = pDeviceManagerEvent.release();
769
770                 switch(deviceType)
771                 {
772                 case DEVICE_TYPE_BLUETOOTH_HEADSET:
773                 {
774                         pDeviceEventContainer->__bluetoothHeadset = true;
775                         RequireBluetoothEvent();
776                         break;
777                 }
778                 case DEVICE_TYPE_CHARGER:
779                 {
780                         pDeviceEventContainer->__charger = true;
781                         break;
782                 }
783                 case DEVICE_TYPE_USB_CLIENT:
784                 {
785                         pDeviceEventContainer->__usbClient = true;
786                         break;
787                 }
788                 case DEVICE_TYPE_TV_OUT:
789                 {
790                         bool tvOut = false;
791                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
792                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
793
794                         if (tvOut == false)
795                         {
796                                 if(!_AppInfo::IsOspCompat())
797                                 {
798                                         return E_UNSUPPORTED_OPERATION;
799                                 }
800                                 else
801                                 {
802                                         return E_DEVICE_UNAVAILABLE;
803                                 }
804                         }
805
806                         pDeviceEventContainer->__tvOut = true;
807                         break;
808                 }
809                 case DEVICE_TYPE_WIRED_HEADSET:
810                 {
811                         pDeviceEventContainer->__wiredHeadset = true;
812                         break;
813                 }
814                 case DEVICE_TYPE_WIRED_HEADPHONE:
815                 {
816                         pDeviceEventContainer->__wiredHeadphone = true;
817                         break;
818                 }
819                 case DEVICE_TYPE_STORAGE_CARD:
820                 {
821                         pDeviceEventContainer->__storageCard = true;
822                         break;
823                 }
824                 case DEVICE_TYPE_KEYBOARD:
825                 {
826                         bool keyboard = false;
827                         r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
828
829                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
830
831                         if (keyboard == false)
832                         {
833                                 if(!_AppInfo::IsOspCompat())
834                                 {
835                                         return E_UNSUPPORTED_OPERATION;
836                                 }
837                                 else
838                                 {
839                                         return E_DEVICE_UNAVAILABLE;
840                                 }
841                         }
842
843                         pDeviceEventContainer->__keyboard = true;
844                         break;
845                 }
846                 case DEVICE_TYPE_HDMI:
847                 {
848                         int value = 0;
849                         int ret = 0;
850                         ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
851                         SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
852                         if(value < 0)
853                         {
854                                 return E_UNSUPPORTED_OPERATION;
855                         }
856                         pDeviceEventContainer->__hdmi = true;
857                         break;
858                 }
859                 }
860                 r = E_SUCCESS;
861                 __deviceEventList.Add(pDeviceEventContainer.release());
862         }
863
864         return r;
865 }
866
867 result
868 _DeviceManagerImpl::RemoveDeviceEventListener(DeviceType deviceType, IDeviceEventListener* pListener)
869 {
870         result r = E_SUCCESS;
871
872         SysTryReturnResult(NID_SYS, pListener != null, E_INVALID_ARG, "There is no listener isntance.");
873         SysTryReturnResult(NID_SYS, (deviceType >= DEVICE_TYPE_BLUETOOTH_HEADSET && deviceType <= DEVICE_TYPE_HDMI), E_INVALID_ARG, "There is no listener isntance.");
874         SysTryReturnResult(NID_SYS, __deviceEventList.GetCount() > 0, E_INVALID_ARG, "There is no registered listener.");
875
876         std::unique_ptr< IEnumeratorT<_DeviceEventListenerContainer*> > pEnumerator(__deviceEventList.GetEnumeratorN());
877
878         SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
879
880         if(deviceType == DEVICE_TYPE_TV_OUT)
881         {
882                 bool tvOut = false;
883                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_TVOUT_SUPPORTED, tvOut);
884                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get tvout option.");
885
886                 if (tvOut == false)
887                 {
888                         if(!_AppInfo::IsOspCompat())
889                         {
890                                 r = E_UNSUPPORTED_OPERATION;
891                         }
892                         else
893                         {
894                                 r = E_DEVICE_UNAVAILABLE;
895                         }
896                 }
897                 return r;
898         }
899         else if(deviceType == DEVICE_TYPE_KEYBOARD)
900         {
901                 bool keyboard = false;
902                 r = _SystemInfoImpl::GetSysInfo(_SYSTEM_INFO_INPUT_KEYBOARD, keyboard);
903
904                 SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "Fail to get keyboard option");
905
906                 if (keyboard == false)
907                 {
908                         if(!_AppInfo::IsOspCompat())
909                         {
910                                 r = E_UNSUPPORTED_OPERATION;
911                         }
912                         else
913                         {
914                                 r = E_DEVICE_UNAVAILABLE;
915                         }
916                 }
917         }
918         else if(deviceType ==  DEVICE_TYPE_HDMI)
919         {
920                 int value = 0;
921                 int ret = 0;
922                 ret = vconf_get_int(VCONFKEY_SYSMAN_HDMI, &value);
923                 SysTryReturnResult(NID_SYS, !(ret < 0), E_SYSTEM, "vconf_get_int failed Output value is %d", value);
924                 if(value < 0)
925                 {
926                         return E_UNSUPPORTED_OPERATION;
927                 }
928         }
929
930         while(pEnumerator->MoveNext() == E_SUCCESS)
931         {
932                 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
933                 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
934                 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
935
936                 if(pDeviceEventListenerContainer->__pListener == pListener)
937                 {
938                         switch(deviceType)
939                         {
940                         case DEVICE_TYPE_BLUETOOTH_HEADSET:
941                         {
942                                 if(pDeviceEventListenerContainer->__bluetoothHeadset == false)
943                                 {
944                                         return E_OBJ_NOT_FOUND;
945                                 }
946                                 pDeviceEventListenerContainer->__bluetoothHeadset = false;
947                                 ReleaseBluetoothEvent();
948                                 break;
949                         }
950                         case DEVICE_TYPE_CHARGER:
951                         {
952                                 if(pDeviceEventListenerContainer->__charger == false)
953                                 {
954                                         return E_OBJ_NOT_FOUND;
955                                 }
956                                 pDeviceEventListenerContainer->__charger = false;
957                                 break;
958                         }
959                         case DEVICE_TYPE_USB_CLIENT:
960                         {
961                                 if(pDeviceEventListenerContainer->__usbClient == false)
962                                 {
963                                         return E_OBJ_NOT_FOUND;
964                                 }
965                                 pDeviceEventListenerContainer->__usbClient = false;
966                                 break;
967                         }
968                         case DEVICE_TYPE_TV_OUT:
969                         {
970                                 if(pDeviceEventListenerContainer->__tvOut == false)
971                                 {
972                                         return E_OBJ_NOT_FOUND;
973                                 }
974                                 pDeviceEventListenerContainer->__tvOut = false;
975                                 break;
976                         }
977                         case DEVICE_TYPE_WIRED_HEADSET:
978                         {
979                                 if(pDeviceEventListenerContainer->__wiredHeadset == false)
980                                 {
981                                         return E_OBJ_NOT_FOUND;
982                                 }
983                                 pDeviceEventListenerContainer->__wiredHeadset = false;
984                                 break;
985                         }
986                         case DEVICE_TYPE_WIRED_HEADPHONE:
987                         {
988                                 if(pDeviceEventListenerContainer->__wiredHeadphone == false)
989                                 {
990                                         return E_OBJ_NOT_FOUND;
991                                 }
992                                 pDeviceEventListenerContainer->__wiredHeadphone = false;
993                                 break;
994                         }
995                         case DEVICE_TYPE_STORAGE_CARD:
996                         {
997                                 if(pDeviceEventListenerContainer->__storageCard == false)
998                                 {
999                                         return E_OBJ_NOT_FOUND;
1000                                 }
1001                                 pDeviceEventListenerContainer->__storageCard = false;
1002                                 break;
1003                         }
1004                         case DEVICE_TYPE_KEYBOARD:
1005                         {
1006                                 if(pDeviceEventListenerContainer->__keyboard == false)
1007                                 {
1008                                         return E_OBJ_NOT_FOUND;
1009                                 }
1010                                 pDeviceEventListenerContainer->__keyboard = false;
1011                                 break;
1012                         }
1013                         case DEVICE_TYPE_HDMI:
1014                         {
1015                                 if(pDeviceEventListenerContainer->__hdmi == false)
1016                                 {
1017                                         return E_OBJ_NOT_FOUND;
1018                                 }
1019                                 pDeviceEventListenerContainer->__hdmi = false;
1020                                 break;
1021                         }
1022                         }
1023
1024                         if(pDeviceEventListenerContainer->IsInvalid() == true)
1025                         {
1026                                 __deviceEventList.Remove(pDeviceEventListenerContainer);
1027                                 delete pDeviceEventListenerContainer;
1028                         }
1029                         return E_SUCCESS;
1030                 }
1031         }
1032         return E_OBJ_NOT_FOUND;
1033 }
1034
1035 result
1036 _DeviceManagerImpl::RemoveAllDeviceEventListeners(void)
1037 {
1038         result r = E_SUCCESS;
1039
1040         IEnumeratorT<_DeviceEventListenerContainer*>* pEnumerator = __deviceEventList.GetEnumeratorN();
1041         SysTryReturnResult(NID_SYS, pEnumerator != null, E_SYSTEM, "Enumerator is empty.");
1042
1043         while(pEnumerator->MoveNext() == E_SUCCESS)
1044         {
1045                 _DeviceEventListenerContainer* pDeviceEventListenerContainer = null;
1046                 pEnumerator->GetCurrent(pDeviceEventListenerContainer);
1047                 SysTryReturnResult(NID_SYS, pDeviceEventListenerContainer != null, E_SYSTEM, "[E_SYSTEM] Device Event Container is empty.");
1048                 __deviceEventList.Remove(pDeviceEventListenerContainer);
1049                 delete pDeviceEventListenerContainer;
1050         }
1051         delete pEnumerator;
1052
1053         __bluetoothReferenceCount = 1;
1054
1055         ReleaseBluetoothEvent();
1056         return r;
1057 }
1058
1059 void
1060 _DeviceManagerImpl::SendEvent(runtime_info_key_e key)
1061 {
1062         DeviceType type = DEVICE_TYPE_USB_CLIENT;
1063         String value;
1064         int flag = 0;
1065         int ret = 0;
1066
1067         switch (key)
1068         {
1069         case RUNTIME_INFO_KEY_USB_CONNECTED:
1070         {
1071                 flag = _DEVICE_MANAGER_USB_CLIENT;
1072                 type = DEVICE_TYPE_USB_CLIENT;
1073
1074                 bool state = false;
1075                 ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_USB_CONNECTED, &state);
1076                 SysTryReturnVoidResult(NID_SYS, ret == RUNTIME_INFO_ERROR_NONE, E_SYSTEM, "It is failed to get usb state.");
1077
1078                 if (state == true)
1079                 {
1080                         value = _DEVICE_MANAGER_STATE_INSERTED;
1081                 }
1082                 else
1083                 {
1084                         value = _DEVICE_MANAGER_STATE_REMOVED;
1085                 }
1086                 break;
1087         }
1088         case RUNTIME_INFO_KEY_CHARGER_CONNECTED:
1089         {
1090                 flag = _DEVICE_MANAGER_CHARGER;
1091                 type = DEVICE_TYPE_CHARGER;
1092
1093                 bool state = false;
1094                 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