add getmmistate
[vuplus_dvbapp] / lib / dvb_ci / dvbci.cpp
1 #include <fcntl.h>
2 #include <sys/ioctl.h>
3
4 #include <lib/base/init.h>
5 #include <lib/base/init_num.h>
6 #include <lib/base/ebase.h>
7
8 #include <lib/base/eerror.h>
9 #include <lib/dvb_ci/dvbci.h>
10 #include <lib/dvb_ci/dvbci_session.h>
11
12 #include <lib/dvb_ci/dvbci_ui.h>
13 #include <lib/dvb_ci/dvbci_appmgr.h>
14 #include <lib/dvb_ci/dvbci_mmi.h>
15
16 eDVBCIInterfaces *eDVBCIInterfaces::instance = 0;
17
18 eDVBCIInterfaces::eDVBCIInterfaces()
19 {
20         int num_ci = 0;
21         
22         instance = this;
23         
24         eDebug("scanning for common interfaces..");
25
26         while (1)
27         {
28                 struct stat s;
29                 char filename[128];
30                 sprintf(filename, "/dev/ci%d", num_ci);
31
32                 if (stat(filename, &s))
33                         break;
34
35                 ePtr<eDVBCISlot> cislot;
36
37                 cislot = new eDVBCISlot(eApp, num_ci);
38                 m_slots.push_back(cislot);
39
40                 ++num_ci;
41         }
42
43         eDebug("done, found %d common interface slots", num_ci);
44 }
45
46 eDVBCIInterfaces::~eDVBCIInterfaces()
47 {
48 }
49
50 eDVBCIInterfaces *eDVBCIInterfaces::getInstance()
51 {
52         return instance;
53 }
54
55 eDVBCISlot *eDVBCIInterfaces::getSlot(int slotid)
56 {
57         for(eSmartPtrList<eDVBCISlot>::iterator i(m_slots.begin()); i != m_slots.end(); ++i)
58                 if(i->getSlotID() == slotid)
59                         return i;
60
61         printf("FIXME: request for unknown slot\n");
62                         
63         return 0;
64 }
65
66 int eDVBCIInterfaces::reset(int slotid)
67 {
68         eDVBCISlot *slot;
69
70         if( (slot = getSlot(slotid)) == 0 )
71                 return -1;
72         
73         return slot->reset();
74 }
75
76 int eDVBCIInterfaces::initialize(int slotid)
77 {
78         eDVBCISlot *slot;
79
80         if( (slot = getSlot(slotid)) == 0 )
81                 return -1;
82         
83         return slot->initialize();
84 }
85
86 int eDVBCIInterfaces::startMMI(int slotid)
87 {
88         eDVBCISlot *slot;
89
90         if( (slot = getSlot(slotid)) == 0 )
91                 return -1;
92         
93         return slot->startMMI();
94 }
95
96 int eDVBCIInterfaces::stopMMI(int slotid)
97 {
98         eDVBCISlot *slot;
99
100         if( (slot = getSlot(slotid)) == 0 )
101                 return -1;
102         
103         return slot->stopMMI();
104 }
105
106 int eDVBCIInterfaces::answerText(int slotid, int answer)
107 {
108         eDVBCISlot *slot;
109
110         if( (slot = getSlot(slotid)) == 0 )
111                 return -1;
112         
113         return slot->answerText(answer);
114 }
115
116 int eDVBCIInterfaces::answerEnq(int slotid, int answer, char *value)
117 {
118         eDVBCISlot *slot;
119
120         if( (slot = getSlot(slotid)) == 0 )
121                 return -1;
122         
123         return slot->answerEnq(answer, value);
124 }
125
126 int eDVBCIInterfaces::getMMIState(int slotid)
127 {
128         eDVBCISlot *slot;
129
130         if( (slot = getSlot(slotid)) == 0 )
131                 return -1;
132         
133         return slot->getMMIState();
134 }
135
136 int eDVBCISlot::send(const unsigned char *data, size_t len)
137 {
138         int res;
139         //int i;
140         //printf("< ");
141         //for(i=0;i<len;i++)
142         //      printf("%02x ",data[i]);
143         //printf("\n");
144
145         res = ::write(fd, data, len);
146
147         //printf("write() %d\n",res);
148
149         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
150
151         return res;
152 }
153
154 void eDVBCISlot::data(int what)
155 {
156         if(what == eSocketNotifier::Priority) {
157                 if(state != stateRemoved) {
158                         state = stateRemoved;
159                         printf("ci removed\n");
160                         notifier->setRequested(eSocketNotifier::Read);
161                         //HACK
162                         eDVBCI_UI::getInstance()->setState(0,0);
163                 }
164                 return;
165         }
166
167         __u8 data[4096];
168         int r;
169         r = ::read(fd, data, 4096);
170
171         if(state != stateInserted) {
172                 state = stateInserted;
173                 eDebug("ci inserted");
174
175                 //HACK
176                 eDVBCI_UI::getInstance()->setState(0,1);
177
178                 /* enable PRI to detect removal or errors */
179                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
180         }
181
182         if(r > 0) {
183                 //int i;
184                 //printf("> ");
185                 //for(i=0;i<r;i++)
186                 //      printf("%02x ",data[i]);
187                 //printf("\n");
188                 eDVBCISession::receiveData(this, data, r);
189                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
190                 return;
191         }
192
193         if(what == eSocketNotifier::Write) {
194                 if(eDVBCISession::pollAll() == 0) {
195                         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
196                 }
197         }
198 }
199
200 DEFINE_REF(eDVBCISlot);
201
202 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
203 {
204         char filename[128];
205
206         application_manager = 0;
207         mmi_session = 0;
208         
209         slotid = nr;
210
211         sprintf(filename, "/dev/ci%d", nr);
212
213         fd = ::open(filename, O_RDWR | O_NONBLOCK);
214
215         eDebug("eDVBCISlot has fd %d", fd);
216         
217         state = stateInserted;
218
219         if (fd >= 0)
220         {
221                 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
222                 CONNECT(notifier->activated, eDVBCISlot::data);
223         } else
224         {
225                 perror(filename);
226         }
227 }
228
229 eDVBCISlot::~eDVBCISlot()
230 {
231 }
232
233 int eDVBCISlot::getSlotID()
234 {
235         return slotid;
236 }
237
238 int eDVBCISlot::reset()
239 {
240         printf("edvbcislot: reset requested\n");
241
242         ioctl(fd, 0);
243
244         return 0;
245 }
246
247 int eDVBCISlot::initialize()
248 {
249         printf("edvbcislot: initialize()\n");
250         return 0;
251 }
252
253 int eDVBCISlot::startMMI()
254 {
255         printf("edvbcislot: startMMI()\n");
256         
257         if(application_manager)
258                 application_manager->startMMI();
259         
260         return 0;
261 }
262
263 int eDVBCISlot::stopMMI()
264 {
265         printf("edvbcislot: stopMMI()\n");
266
267         if(mmi_session)
268                 mmi_session->stopMMI();
269         
270         return 0;
271 }
272
273 int eDVBCISlot::answerText(int answer)
274 {
275         printf("edvbcislot: answerText(%d)\n", answer);
276
277         if(mmi_session)
278                 mmi_session->answerText(answer);
279
280         return 0;
281 }
282
283 int eDVBCISlot::getMMIState()
284 {
285         if(mmi_session)
286                 return 1;
287
288         return 0;
289 }
290
291 int eDVBCISlot::answerEnq(int answer, char *value)
292 {
293         printf("edvbcislot: answerMMI(%d,%s)\n", answer, value);
294         return 0;
295 }
296
297 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");