dbg.h file is removed.
[profile/tv/apps/native/source.git] / src / mgr / source_mgr.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 <AppCommon.h>
18 #include "external.h"
19 #include "source_mgr.h"
20 #include "aul.h"
21 #include "util.h"
22 #include "usb.h"
23
24 #define DEFAULT_USB_NAME        "USB Dev"
25 #define DEFAULT_EXT_NAME        "External"
26 #define USB_CONN_TYPE           "USB"
27 #define TV_CONN_TYPE            "TV ANTENNA"
28
29
30 CSourceMgr *CSourceMgr::instance = NULL;
31
32
33 struct SSourceMgr {
34         Eina_List *plugged_list;
35         Eina_List *nearby_conn_list;
36         Eina_List *nearby_disc_list;
37
38         struct SCallback {
39                 TUpdateUI cbUpdateSourceList;
40                 void *data;
41         } cb;
42
43         CUsb *usb;
44         CExternal ext;
45
46         SSourceMgr() {
47                 plugged_list = NULL;
48                 nearby_conn_list = NULL;
49                 nearby_disc_list = NULL;
50                 memset(&cb, 0, sizeof(cb));
51         }
52 };
53
54
55 void CSourceMgr::t_FreeAllSource(void)
56 {
57         CSourceInfo *si;
58         void *obj;
59
60         EINA_LIST_FREE(m->plugged_list, obj) {
61                 si = (CSourceInfo *)obj;
62                 switch (si->Type()) {
63                 case CONN_TYPE_USB:
64                         t_Free(si);
65                         break;
66                 case CONN_TYPE_TV:
67                         t_Free(si);
68                         break;
69                 default:
70                         break;
71                 }
72         }
73 }
74
75
76 void CSourceMgr::t_Free(CSourceInfo *si)
77 {
78         delete si;
79 }
80
81
82 void CSourceMgr::t_UpdateUI(void *data, enum update_ui update_type)
83 {
84         if (m->cb.cbUpdateSourceList)
85                 m->cb.cbUpdateSourceList(m->cb.data, data, update_type);
86 }
87
88 CSourceInfo *CSourceMgr::m_AllocUsbSI(void *data)
89 {
90         CUsbListener::SUsbHostDeviceInfo *devInfo;
91         CSourceInfo *si;
92         const char *name;
93
94         devInfo = (CUsbListener::SUsbHostDeviceInfo *)data;
95
96         si = new CSourceInfo;
97         if (!si) {
98                 _ERR("alloc failed");
99                 return NULL;
100         }
101
102         si->SetId(devInfo->vendorId);
103         si->SetType(CONN_TYPE_USB);
104         name = devInfo->product;
105         si->SetName(name ? name : DEFAULT_USB_NAME);
106         si->SetTypeName(USB_CONN_TYPE);
107
108         return si;
109 }
110
111 void CSourceMgr::sm_CbUsbPlug(void *cookie, int status, void *data)
112 {
113         CSourceMgr *root = (CSourceMgr *)cookie;
114
115         if (root)
116                 root->m_OnUsbPlug(status, data);
117 }
118
119 void CSourceMgr::m_OnUsbPlug(int status, void *data)
120 {
121         CUsbListener::SUsbHostDeviceInfo *devInfo;
122         CSourceInfo *si;
123         void *obj;
124         Eina_List *list, *list_next;
125
126         devInfo = (CUsbListener::SUsbHostDeviceInfo *)data;
127
128         if (status == CUsbListener::USB_HOST_DEV_CONNECTED) {
129                 si = m_AllocUsbSI(devInfo);
130                 if (!si)
131                         return;
132
133                 m->plugged_list = eina_list_append(m->plugged_list, si);
134                 t_UpdateUI(si, SRC_PLUG_ADD_ONE);
135         } else {
136                 EINA_LIST_FOREACH_SAFE(m->plugged_list, list, list_next, obj) {
137                         si = (CSourceInfo *)obj;
138                         if (si->Type() == CONN_TYPE_USB &&
139                                         si->Id() == devInfo->vendorId) {
140                                 m->plugged_list = eina_list_remove(
141                                                 m->plugged_list, si);
142
143                                 t_UpdateUI(si, SRC_PLUG_DEL_ONE);
144                                 t_Free(si);
145                                 break;
146                         }
147                 }
148         }
149 }
150
151 void CSourceMgr::sm_CbExternalPlug(int is_plugged, void *cbdata, void *data)
152 {
153         CSourceMgr *root = (CSourceMgr*)cbdata;
154         if (root)
155                 root->m_OnExternalPlug(is_plugged, cbdata);
156 }
157
158
159 void CSourceMgr::m_OnExternalPlug(int is_plugged, void *data)
160 {
161         CSourceInfo *si;
162         void *obj;
163         enum ext_type *ext_type;
164         Eina_List *list, *list_next;
165
166         ext_type = (enum ext_type *)data;
167
168         if (is_plugged) {
169                 si = m_AllocSI(*ext_type);
170                 if (!si)
171                         return;
172
173                 m->plugged_list = eina_list_append(m->plugged_list, si);
174                 t_UpdateUI(si, SRC_PLUG_ADD_ONE);
175         }
176         else {
177                 EINA_LIST_FOREACH_SAFE(m->plugged_list, list, list_next, obj) {
178                         si = (CSourceInfo *)obj;
179                         if (si->Type() == *ext_type) {
180                                 m->plugged_list = eina_list_remove(
181                                         m->plugged_list, si);
182
183                                 t_UpdateUI(si, SRC_PLUG_DEL_ONE);
184                                 t_Free(si);
185                                 break;
186                         }
187                 }
188         }
189 }
190
191
192 CSourceInfo *CSourceMgr::m_AllocSI(int type)
193 {
194         CSourceInfo *si;
195         const char *name;
196
197         si = (CSourceInfo *)calloc(1, sizeof(*si));
198         if (!si) {
199                 _ERR("calloc failed");
200                 return NULL;
201         }
202
203         name = CExternal::Name((ext_type)type);
204         si->SetName(name ? name : DEFAULT_EXT_NAME);
205         if (type == CONN_TYPE_TV)
206                 si->SetTypeName(TV_CONN_TYPE);
207
208         si->SetType((ext_type)type);
209
210         return si;
211 }
212
213
214 bool CSourceMgr::t_Create(TUpdateUI cb, void *data)
215 {
216         ASSERT(!m);
217
218         CUsb::SCallback usbCb;
219         CExternal::SCallback extCb;
220
221         m = new SSourceMgr;
222         if (!m) {
223                 _ERR("Create src_mgr failed.");
224                 return false;
225         }
226
227         m->usb = new CUsb;
228         if (!m->usb) {
229                 _ERR("Create usb failed.");
230                 return false;
231         }
232
233         usbCb.cb = sm_CbUsbPlug;
234         usbCb.cookie = this;
235
236         if (!m->usb->Create(&usbCb)) {
237                 _ERR("Init usb failed.");
238                 delete m;
239                 m = NULL;
240                 return false;
241         }
242
243         extCb.proc_cb = sm_CbExternalPlug;
244         extCb.cbdata = m;
245
246         if (!m->ext.Create(&extCb)) {
247                 _ERR("Init external failed.");
248
249                 m->usb->Destroy();
250
251                 delete m;
252                 m = NULL;
253                 return false;
254         }
255
256         m->cb.cbUpdateSourceList = cb;
257         m->cb.data = data;
258
259         return true;
260 }
261
262
263 void CSourceMgr::t_Destroy(void)
264 {
265         ASSERT(m);
266
267         t_FreeAllSource();
268
269         m->usb->Destroy();
270         m->ext.Destroy();
271
272         delete m;
273         m = NULL;
274 }
275
276
277 bool CSourceMgr::Initialize(TUpdateUI cb, void *data)
278 {
279         ASSERT(!instance);
280
281         instance = new CSourceMgr;
282         if (!instance)
283                 return false;
284         if (!instance->t_Create(cb, data)) {
285                 delete instance;
286                 instance = NULL;
287                 return false;
288         }
289         return true;
290 }
291
292
293 void CSourceMgr::Finalize(void)
294 {
295         ASSERT(instance);
296
297         instance->t_Destroy();
298         delete instance;
299         instance = NULL;
300 }
301
302
303 CSourceMgr *CSourceMgr::GetInstance(void)
304 {
305         return instance;
306 }
307
308
309 void CSourceMgr::Refresh(void)
310 {
311         ASSERT(m);
312
313         t_FreeAllSource();
314         t_UpdateUI(NULL, SRC_UPDATE_ALL);
315
316         m->usb->GetConnected();
317         m->ext.GetConnected();
318 }
319
320
321 Eina_List *CSourceMgr::PluggedList(void)
322 {
323         ASSERT(m);
324
325         return m->plugged_list;
326 }
327
328
329 Eina_List *CSourceMgr::NearbyConnList(void)
330 {
331         ASSERT(m);
332
333         return m->nearby_conn_list;
334 }
335
336
337 Eina_List *CSourceMgr::NearbyDiscList(void)
338 {
339         ASSERT(m);
340
341         return m->nearby_disc_list;
342 }
343
344
345 CSourceInfo *CSourceMgr::SourceInfoByData(void *data)
346 {
347         ASSERT(m);
348
349         Eina_List *list;
350         CSourceInfo *si;
351         void *obj;
352
353         EINA_LIST_FOREACH(m->plugged_list, list, obj) {
354                 si = (CSourceInfo *)obj;
355                 if (si && si->Data() == data)
356                         return si;
357         }
358
359         EINA_LIST_FOREACH(m->nearby_conn_list, list, obj) {
360                 si = (CSourceInfo *)obj;
361                 if (si && si->Data() == data)
362                         return si;
363         }
364
365         EINA_LIST_FOREACH(m->nearby_disc_list, list, obj) {
366                 si = (CSourceInfo *)obj;
367                 if (si && si->Data() == data)
368                         return si;
369         }
370
371         return NULL;
372 }