use orbital position in isValidONIDTSID
[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         __u8 data[4096];
74         int r;
75         r = ::read(fd, data, 4096);
76
77         if(state != stateInserted) {
78                 state = stateInserted;
79                 eDebug("ci inserted");
80
81                 //HACK
82                 eDVBCI_UI::getInstance()->setState(0,1);
83
84                 /* enable PRI to detect removal or errors */
85                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
86         }
87
88         if(r > 0) {
89                 //int i;
90                 //printf("> ");
91                 //for(i=0;i<r;i++)
92                 //      printf("%02x ",data[i]);
93                 //printf("\n");
94                 eDVBCISession::receiveData(this, data, r);
95                 notifier->setRequested(eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Write);
96                 return;
97         }
98
99         if(what == eSocketNotifier::Write) {
100                 if(eDVBCISession::pollAll() == 0) {
101                         notifier->setRequested(eSocketNotifier::Read | eSocketNotifier::Priority);
102                 }
103         }
104 }
105
106 DEFINE_REF(eDVBCISlot);
107
108 eDVBCISlot::eDVBCISlot(eMainloop *context, int nr)
109 {
110         char filename[128];
111
112         sprintf(filename, "/dev/ci%d", nr);
113
114         fd = ::open(filename, O_RDWR | O_NONBLOCK);
115
116         eDebug("eDVBCISlot has fd %d", fd);
117         
118         state = stateInserted;
119
120         if (fd >= 0)
121         {
122                 notifier = new eSocketNotifier(context, fd, eSocketNotifier::Read | eSocketNotifier::Priority);
123                 CONNECT(notifier->activated, eDVBCISlot::data);
124         } else
125         {
126                 perror(filename);
127         }
128 }
129
130 eDVBCISlot::~eDVBCISlot()
131 {
132 }
133
134 eAutoInitP0<eDVBCIInterfaces> init_eDVBCIInterfaces(eAutoInitNumbers::dvb, "CI Slots");