969f94ab9a987cb7508da14f5228010c323c243f
[profile/tv/apps/native/source.git] / src / mgr / usb.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "dbg.h"
18 #include <AppCommon.h>
19 #include "aul.h"
20 #include "usb.h"
21
22 #define FILE_BROWSER_APP "org.tizen.file-browser-tv-ref"
23 #define FILE_BROWSER_USB_KEY "source"
24 #define FILE_BROWSER_USB_VALUE "usb"
25
26 struct SUsb {
27         CUsb::SCallback conCb;
28 };
29
30
31 bool CUsb::Create(const SCallback *cb)
32 {
33         ASSERT(!m);
34
35         m = new SUsb;
36         if (!m) {
37                 _ERR("alloc failed");
38                 return false;
39         }
40
41         if (!CUsbListener::Create())
42                 _DBG("UsbListener creation failed");
43
44         m->conCb = *cb;
45
46         return true;
47 }
48
49 void CUsb::Destroy(void)
50 {
51         ASSERT(m);
52
53         if (CUsbListener::FlagCreate())
54                 CUsbListener::Destroy();
55
56         delete m;
57         m = NULL;
58 }
59
60 bool CUsb::SwitchTo(void)
61 {
62         bundle *b;
63         int err;
64
65         b = bundle_create();
66         if (!b)
67                 return false;
68
69         err = bundle_add(b, FILE_BROWSER_USB_KEY, FILE_BROWSER_USB_VALUE);
70         if (err) {
71                 _ERR("Add bundle value failed. err:%d", err);
72                 bundle_free(b);
73                 return false;
74         }
75
76         aul_launch_app(FILE_BROWSER_APP, b);
77         bundle_free(b);
78
79         return true;
80 }
81
82 void CUsb::GetConnected(void)
83 {
84         int count;
85         SUsbHostDeviceInfo devInfo;
86
87         if (!CUsbListener::FlagCreate())
88                 return;
89
90         count = ConnectionCount();
91         for (int i = 0; i < count; i++) {
92                 GetConnectedDevInfo(i, &devInfo);
93                 m->conCb.cb(m->conCb.cookie, USB_HOST_DEV_CONNECTED, &devInfo);
94         }
95 }
96
97 void CUsb::OnStatusChanged(SUsbHostDeviceInfo *changedDevice,
98                         SUsbHostDeviceStatus status)
99 {
100         m->conCb.cb(m->conCb.cookie, status, (void *)changedDevice);
101 }