add ServiceInfo.py
[enigma2.git] / lib / dvb_ci / dvbci.cpp
1 #include <fcntl.h>
2
3 #include <lib/base/init.h>
4 #include <lib/base/init_num.h>
5
6 #include <lib/base/eerror.h>
7 #include <lib/dvb_ci/dvbci.h>
8 #include <lib/dvb_ci/dvbci_session.h>
9
10 #include <lib/dvb_ci/dvbci_ui.h>
11
12 eDVBCIInterfaces::eDVBCIInterfaces()
13 {
14         int num_ci = 0;
15
16         eDebug("scanning for common interfaces..");
17
18         while (1)
19         {
20                 struct stat s;
21                 char filename[128];
22                 sprintf(filename, "/dev/ci%d", num_ci);
23
24                 if (stat(filename, &s))
25                         break;
26
27                 ePtr<eDVBCISlot> cislot;
28
29                 cislot = new eDVBCISlot(eApp, num_ci);
30                 m_slots.push_back(cislot);
31
32                 ++num_ci;
33         }
34
35         eDebug("done, found %d common interface slots", num_ci);
36 }
37
38 eDVBCIInterfaces::~eDVBCIInterfaces()
39 {
40 }
41
42 int eDVBCISlot::send(const unsigned char *data, size_t len)
43 {
44         int res;
45         //int i;
46         //printf("< ");
47         //for(i=0;i<len;i++)
48         //      printf("%02x ",data[i]);
49         //printf("\n");
50
51         res = ::write(fd, data, len);
52
53         //printf("write() %d\n",res);
54
55         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority | eSocketNotifier::Write);
56
57         return res;
58 }
59
60 void eDVBCISlot::data(int what)
61 {
62         if(what == eSocketNotifier::Priority) {
63                 if(state != stateRemoved) {
64                         state = stateRemoved;
65                         printf("ci removed\n");
66                         notifier->setRequested(eSocketNotifier::Read);
67                         //HACK
68                         eDVBCI_UI::getInstance()->setState(0,0);
69                 }
70                 return;
71         }
72
73
74         __u8 data[4096];
75         int r;
76         r = ::read(fd, data, 4096);
77
78         if(state != stateInserted) {
79                 state = stateInserted;
80                 eDebug("ci inserted");
81
82                 //HACK
83                 eDVBCI_UI::getInstance()->setState(0,1);
84
85                 /* enable PRI to detect removal or errors */
86                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
87         }
88
89         if(r > 0) {
90                 //int i;
91                 //printf("> ");
92                 //for(i=0;i<r;i++)
93                 //      printf("%02x ",data[i]);
94                 //printf("\n");
95                 eDVBCISession::receiveData(this, data, r);
96                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
97                 return;
98         }
99
100         if(what == eSocketNotifier::Write) {
101                 if(eDVBCISession::pollAll() == 0) {
102                         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
103                 }
104         }
105 }
106
107 DEFINE_REF(eDVBCISlot);
108
109 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
110 {
111         char filename[128];
112
113         sprintf(filename, "/dev/ci%d", nr);
114
115         fd = ::open(filename, O_RDWR | O_NONBLOCK);
116
117         eDebug("eDVBCISlot has fd %d", fd);
118
119         if (fd >= 0)
120         {
121                 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
122                 CONNECT(notifier->activated, eDVBCISlot::data);
123         } else
124         {
125                 perror(filename);
126         }
127 }
128
129 eDVBCISlot::~eDVBCISlot()
130 {
131 }
132
133 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");