fixes
[enigma2.git] / lib / dvb_ci / dvbci_mmi.cpp
1 /* DVB CI MMI */
2
3 #include <lib/dvb_ci/dvbci_mmi.h>
4 #include <lib/dvb_ci/dvbci_ui.h>
5
6 /*
7 PyObject *list = PyList_New(len);
8 for (i=0; i<len; ++i) {
9         PyObject *tuple = PyTuple_New(3); // 3 eintrge im tuple
10         PyTuple_SetItem(tuple, 0, PyString_FromString("eintrag 1"))
11         PyTuple_SetItem(tuple, 1, PyInt_FromLong(31337));
12         PyTuple_SetItem(tuple, 2, PyString_FromString("eintrag 3"))
13         PyList_SetItem(list, i, tuple);
14 }
15 return list;
16 */
17
18 eDVBCIMMISession::eDVBCIMMISession(eDVBCISlot *tslot)
19 {
20         slot = tslot;
21         slot->mmi_session = this;
22 }
23
24 eDVBCIMMISession::~eDVBCIMMISession()
25 {
26         slot->mmi_session = 0;
27 }
28
29 int eDVBCIMMISession::receivedAPDU(const unsigned char *tag, const void *data, int len)
30 {
31         printf("SESSION(%d)/MMI %02x %02x %02x: ", session_nb, tag[0], tag[1],tag[2]);
32         for (int i=0; i<len; i++)
33                 printf("%02x ", ((const unsigned char*)data)[i]);
34         printf("\n");
35
36         if ((tag[0]==0x9f) && (tag[1]==0x88))
37         {
38                 switch (tag[2])
39                 {
40                         case 0x01:
41                         printf("MMI display control\n");
42                         if (((unsigned char*)data)[0] != 1)
43                                 printf("kann ich nicht. aber das sag ich dem modul nicht.\n");
44                         state=stateDisplayReply;
45                         return 1;
46                 case 0x07:              //Tmenu_enq
47                 {
48                         unsigned char *d=(unsigned char*)data;
49                         unsigned char *max=((unsigned char*)d) + len;
50                         int textlen = len - 2;
51                         int i;
52
53                         printf("in enq\n");
54                         
55                         if ((d+2) > max)
56                                 break;
57                                 
58                         int blind = *d++ & 1;
59                         int alen = *d++;
60
61                         printf("%d bytes text\n", textlen);
62                         if ((d+textlen) > max)
63                                 break;
64                         
65                         char str[textlen + 1];
66                         memcpy(str, ((char*)d), textlen);
67                         str[textlen] = '\0';
68                         
69                         printf("enq-text: %s\n",str);
70                         
71                         eDVBCI_UI::getInstance()->mmiScreenEnq(0, blind, alen, str);
72
73                         break;          
74                 }
75                 case 0x09:              //Tmenu_last
76                 case 0x0c:              //Tlist_last
77                 {
78                         unsigned char *d=(unsigned char*)data;
79                         unsigned char *max=((unsigned char*)d) + len;
80                         int pos = 0;
81                         printf("Tmenu_last\n");
82                         if (d > max)
83                                 break;
84                         int n=*d++;
85                         
86                         //FIXME: slotid
87                         if(tag[2] == 0x09)      //menu
88                                 eDVBCI_UI::getInstance()->mmiScreenBegin(0, 0);
89                         else                                                            //list
90                                 eDVBCI_UI::getInstance()->mmiScreenBegin(0, 1);
91                         
92                         if (n == 0xFF)
93                                 n=0;
94                         else
95                                 n++;
96                         printf("%d texts\n", n);
97                         for (int i=0; i < (n+3); ++i)
98                         {
99                                 int textlen;
100                                 if ((d+3) > max)
101                                         break;
102                                 printf("text tag: %02x %02x %02x\n", d[0], d[1], d[2]);
103                                 d+=3;
104                                 d+=parseLengthField(d, textlen);
105                                 printf("%d bytes text\n", textlen);
106                                 if ((d+textlen) > max)
107                                         break;
108                                         
109                                 char str[textlen + 1];
110                                 memcpy(str, ((char*)d), textlen);
111                                 str[textlen] = '\0';
112                                 
113                                 eDVBCI_UI::getInstance()->mmiScreenAddText(0, pos++, str);
114                                         
115                                 while (textlen--)
116                                         printf("%c", *d++);
117                                 printf("\n");
118                         }
119                         //FIXME: slotid
120                         eDVBCI_UI::getInstance()->mmiScreenFinish(0);
121                         break;
122                 }
123                 default:
124                         printf("unknown APDU tag 9F 88 %02x\n", tag[2]);
125                         break;
126                 }
127         }
128         return 0;
129 }
130
131 int eDVBCIMMISession::doAction()
132 {
133         switch (state)
134         {
135         case stateStarted:
136                 state=stateIdle;
137                 break;
138         case stateDisplayReply:
139         {
140                 unsigned char tag[]={0x9f, 0x88, 0x02};
141                 unsigned char data[]={0x01, 0x01};
142                 sendAPDU(tag, data, 2);
143                 state=stateFakeOK;
144                 return 1;
145                 break;
146         }
147         case stateFakeOK:
148         {
149                 unsigned char tag[]={0x9f, 0x88, 0x0b};
150                 unsigned char data[]={5};
151                 sendAPDU(tag, data, 1);
152                 state=stateIdle;
153                 break;
154         }
155         case stateIdle:
156                 break;
157         default:
158                 break;
159         }
160         return 0;
161 }
162
163 int eDVBCIMMISession::stopMMI()
164 {
165         printf("eDVBCIMMISession::stopMMI()\n");
166
167         unsigned char tag[]={0x9f, 0x88, 0x00};
168         unsigned char data[]={0x00};
169         sendAPDU(tag, data, 1);
170         
171         return 0;
172 }
173
174 int eDVBCIMMISession::answerText(int answer)
175 {
176         printf("eDVBCIMMISession::answerText(%d)\n",answer);
177
178         unsigned char tag[]={0x9f, 0x88, 0x0B};
179         unsigned char data[]={0x00};
180         data[0] = answer & 0xff;
181         sendAPDU(tag, data, 1);
182         
183         return 0;
184 }
185
186 int eDVBCIMMISession::cancelEnq()
187 {
188         printf("eDVBCIMMISession::cancelEnq()\n");
189
190         unsigned char tag[]={0x9f, 0x88, 0x08};
191         unsigned char data[]={0x00};
192         sendAPDU(tag, data, 1);
193         
194         return 0;
195 }
196