better solution to add possibility to delete eSocketNotifiers,
[vuplus_dvbapp] / lib / python / Plugins / Extensions / SocketMMI / src / socket_mmi.cpp
1 #include "socket_mmi.h"
2
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6
7 #include <lib/base/ebase.h>
8 #include <lib/base/init.h>
9 #include <lib/base/init_num.h>
10 #include <lib/base/eerror.h>
11 #include <lib/base/estring.h>
12 #include <lib/dvb_ci/dvbci_session.h>
13
14 #define MAX_LENGTH_BYTES 4
15 #define MIN_LENGTH_BYTES 1
16 //#define MMIDEBUG
17
18 eSocket_UI *eSocket_UI::instance;
19
20 eSocket_UI::eSocket_UI()
21         :eMMI_UI(1)
22 {
23         ASSERT(!instance);
24         instance = this;
25         CONNECT(handler.mmi_progress, eMMI_UI::processMMIData);
26 }
27
28 eSocket_UI *eSocket_UI::getInstance()
29 {
30         return instance;
31 }
32
33 void eSocket_UI::setInit(int)
34 {
35         //NYI
36 }
37
38 void eSocket_UI::setReset(int)
39 {
40         //NYI
41 }
42
43 int eSocket_UI::startMMI(int)
44 {
45         unsigned char buf[]={0x9F,0x80,0x22,0x00};  // ENTER MMI
46         if (handler.send_to_mmisock( buf, 4 ))
47         {
48                 eDebug("eSocket_UI::startMMI failed");
49                 return -1;
50         }
51         return 0;
52 }
53
54 int eSocket_UI::stopMMI(int)
55 {
56         unsigned char buf[]={0x9F,0x88,0x00,0x00};  // CLOSE MMI
57         if (handler.send_to_mmisock( buf, 4 ))
58         {
59                 eDebug("eSocket_UI::stopMMI failed");
60                 return -1;
61         }
62         return 0;
63 }
64
65 int eSocket_UI::answerMenu(int, int answer)
66 {
67         unsigned char data[]={0x9f,0x88,0x0B,0x01,0x00};
68         data[4] = answer & 0xff;
69         if (handler.send_to_mmisock( data, 5 ))
70         {
71                 eDebug("eSocket_UI::answerMenu failed");
72                 return -1;
73         }
74         return 0;
75 }
76
77 int eSocket_UI::answerEnq(int, char *answer)
78 {
79         unsigned int len = strlen(answer);
80         unsigned char data[4+len+MAX_LENGTH_BYTES];
81         data[0] = 0x9f;
82         data[1] = 0x88;
83         data[2] = 0x08;
84         int LengthBytes=eDVBCISession::buildLengthField(data+3, len+1);
85         data[3+LengthBytes] = 0x01;
86         memcpy(data+4+LengthBytes, answer, len);
87         if (handler.send_to_mmisock( data, len+4+LengthBytes ))
88         {
89                 eDebug("eSocket_UI::answerEnq failed");
90                 return -1;
91         }
92         return 0;
93 }
94
95 int eSocket_UI::cancelEnq(int)
96 {
97         unsigned char data[]={0x9f,0x88,0x08,0x01,0x00};
98         if (handler.send_to_mmisock( data, 5 ))
99         {
100                 eDebug("eSocket_UI::cancelEnq failed");
101                 return -1;
102         }
103         return 0;
104 }
105
106 int eSocket_UI::getState(int)
107 {
108         return handler.connected() ? 2 : 0;
109 }
110
111 int eSocket_UI::getMMIState(int)
112 {
113         return handler.connected();
114 }
115
116 //FIXME: correct "run/startlevel"
117 eAutoInitP0<eSocket_UI> init_socketui(eAutoInitNumbers::rc, "Socket MMI");
118
119 int eSocketMMIHandler::send_to_mmisock( void* buf, size_t len)
120 {
121         int ret = write(connfd, buf, len);
122         if ( ret < 0 )
123                 eDebug("[eSocketMMIHandler] write (%m)");
124         else if ( (uint)ret != len )
125                 eDebug("[eSocketMMIHandler] only %d bytes sent.. %d bytes should be sent", ret, len );
126         else
127                 return 0;
128         return ret;
129 }
130
131 eSocketMMIHandler::eSocketMMIHandler()
132         :buffer(512), connfd(-1), sockname("/tmp/mmi.socket"), name(0)
133 {
134         memset(&servaddr, 0, sizeof(struct sockaddr_un));
135         servaddr.sun_family = AF_UNIX;
136         unlink(sockname);
137         strcpy(servaddr.sun_path, sockname);
138         clilen = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
139         if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
140         {
141                 eDebug("[eSocketMMIHandler] socket (%m)");
142                 return;
143         }
144
145         int val = 1;
146         if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) == -1)
147                 eDebug("[eSocketMMIHandler] SO_REUSEADDR (%m)");
148         else if ((val = fcntl(listenfd, F_GETFL)) == -1)
149                 eDebug("[eSocketMMIHandler] F_GETFL (%m)");
150         else if (fcntl(listenfd, F_SETFL, val | O_NONBLOCK) == -1)
151                 eDebug("[eSocketMMIHandler] F_SETFL (%m)");
152         else if (bind(listenfd, (struct sockaddr *) &servaddr, clilen) == -1)
153                 eDebug("[eSocketMMIHandler] bind (%m)");
154         else if (listen(listenfd, 0) == -1)
155                 eDebug("[eSocketMMIHandler] listen (%m)");
156         else {
157                 listensn = eSocketNotifier::create( eApp, listenfd, POLLIN );
158                 listensn->start();
159                 CONNECT( listensn->activated, eSocketMMIHandler::listenDataAvail );
160                 eDebug("[eSocketMMIHandler] created successfully");
161                 return;
162         }
163
164         close(listenfd);
165         listenfd = -1;
166 }
167
168 #define CMD_SET_NAME "\x01\x02\x03\x04"
169
170 void eSocketMMIHandler::listenDataAvail(int what)
171 {
172         if (what & POLLIN) {
173                 if ( connsn ) {
174                         eDebug("[eSocketMMIHandler] connsn != NULL");
175                         return;
176                 }
177                 connfd = accept(listenfd, (struct sockaddr *) &servaddr, (socklen_t *) &clilen);
178                 if (connfd == -1) {
179                         eDebug("[eSocketMMIHandler] accept (%m)");
180                         return;
181                 }
182
183                 int val;
184                 if ((val = fcntl(connfd, F_GETFL)) == -1)
185                         eDebug("[eSocketMMIHandler] F_GETFL (%m)");
186                 else if (fcntl(connfd, F_SETFL, val | O_NONBLOCK) == -1)
187                         eDebug("[eSocketMMIHandler] F_SETFL (%m)");
188                 else {
189                         connsn = eSocketNotifier::create( eApp, connfd, POLLIN|POLLHUP|POLLERR );
190                         CONNECT( connsn->activated, eSocketMMIHandler::connDataAvail );
191                         return;
192                 }
193
194                 close(connfd);
195                 connfd = -1;
196         }
197 }
198
199 void eSocketMMIHandler::connDataAvail(int what)
200 {
201         if (what & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
202                 char msgbuffer[4096];
203                 ssize_t length = read(connfd, msgbuffer, sizeof(msgbuffer));
204
205                 if (length == -1) {
206                         if (errno != EAGAIN && errno != EINTR && errno != EBUSY) {
207                                 eDebug("[eSocketMMIHandler] read (%m)");
208                                 what |= POLLERR;
209                         }
210                 } else if (length == 0){
211                         what |= POLLHUP;
212                 } else if ((!name) && (length > 4) && (!memcmp(msgbuffer, CMD_SET_NAME, 4))) {
213                         length -= 4;
214                         delete [] name;
215                         name = new char[length + 1];
216                         memcpy(name, &msgbuffer[4], length);
217                         name[length] = '\0';
218                         eDebug("MMI NAME %s", name);
219                 } else {
220                         int len = length;
221                         unsigned char *data = (unsigned char*)msgbuffer;
222                         int clear = 1;
223         // If a new message starts, then the previous message
224         // should already have been processed. Otherwise the
225         // previous message was incomplete and should therefore
226         // be deleted.
227                         if ((len >= 1) && (data[0] != 0x9f))
228                                 clear = 0;
229                         if ((len >= 2) && (data[1] != 0x88))
230                                 clear = 0;
231                         if (clear)
232                         {
233                                 buffer.clear();
234 #ifdef MMIDEBUG
235                                 eDebug("clear buffer");
236 #endif
237                         }
238 #ifdef MMIDEBUG
239                         eDebug("Put to buffer:");
240                         for (int i=0; i < len; ++i)
241                                 eDebugNoNewLine("%02x ", data[i]);
242                         eDebug("\n--------");
243 #endif
244                         buffer.write( data, len );
245
246                         while ( buffer.size() >= (3 + MIN_LENGTH_BYTES) )
247                         {
248                                 unsigned char tmp[3+MAX_LENGTH_BYTES];
249                                 buffer.peek(tmp, 3+MIN_LENGTH_BYTES);
250                                 if (tmp[0] != 0x9f || tmp[1] != 0x88)
251                                 {
252                                         buffer.skip(1);
253 #ifdef MMIDEBUG
254                                         eDebug("skip %02x", tmp[0]);
255 #endif
256                                         continue;
257                                 }
258                                 if (tmp[3] & 0x80) {
259                                         int peekLength = (tmp[3] & 0x7f) + 4;
260                                         if (buffer.size() < peekLength)
261                                                 continue;
262                                         buffer.peek(tmp, peekLength);
263                                 }
264                                 int size=0;
265                                 int LengthBytes=eDVBCISession::parseLengthField(tmp+3, size);
266                                 int messageLength = 3+LengthBytes+size;
267                                 if ( buffer.size() >= messageLength )
268                                 {
269                                         unsigned char dest[messageLength];
270                                         buffer.read(dest, messageLength);
271 #ifdef MMIDEBUG
272                                         eDebug("dump mmi:");
273                                         for (int i=0; i < messageLength; ++i)
274                                                 eDebugNoNewLine("%02x ", dest[i]);
275                                         eDebug("\n--------");
276 #endif
277                                         /*emit*/ mmi_progress(0, dest, (const void*)(dest+3+LengthBytes), messageLength-3-LengthBytes);
278                                 }
279                         }
280                 }
281         }
282
283         if (what & (POLLERR | POLLHUP)) {
284                 eDebug("pollhup/pollerr");
285                 closeConn();
286                 /*emit*/ mmi_progress(0, (const unsigned char*)"\x9f\x88\x00", "\x00", 1);
287         }
288 }
289
290 void eSocketMMIHandler::closeConn()
291 {
292         if ( connfd != -1 )
293         {
294                 close(connfd);
295                 connfd=-1;
296         }
297         connsn=0;
298         if ( name )
299         {
300                 delete [] name;
301                 name=0;
302         }
303 }
304
305 eSocketMMIHandler::~eSocketMMIHandler()
306 {
307         closeConn();
308         unlink(sockname);
309 }
310
311 extern "C" {
312
313 static PyObject *
314 socketmmi_get_socket_state_changed_cb_list(PyObject *self)
315 {
316         return eSocket_UI::getInstance()->socketStateChanged.get();
317 }
318
319 static PyObject *
320 socketmmi_set_init(PyObject *self, PyObject *args)
321 {
322         int slot;
323         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
324                 return NULL;
325         eSocket_UI::getInstance()->setInit(slot);
326         Py_RETURN_NONE;
327 }
328
329 static PyObject *
330 socketmmi_set_reset(PyObject *self, PyObject *args)
331 {
332         int slot;
333         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
334                 return NULL;
335         eSocket_UI::getInstance()->setReset(slot);
336         Py_RETURN_NONE;
337 }
338
339 static PyObject *
340 socketmmi_available_mmi(PyObject *self, PyObject *args)
341 {
342         int slot;
343         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
344                 return NULL;
345         return PyInt_FromLong(eSocket_UI::getInstance()->availableMMI(slot));
346 }
347
348 static PyObject *
349 socketmmi_get_mmi_screen(PyObject *self, PyObject *args)
350 {
351         int slot;
352         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
353                 return NULL;
354         return eSocket_UI::getInstance()->getMMIScreen(slot);
355 }
356
357 static PyObject *
358 socketmmi_start_mmi(PyObject *self, PyObject *args)
359 {
360         int slot;
361         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
362                 return NULL;
363         return PyInt_FromLong(eSocket_UI::getInstance()->startMMI(slot));
364 }
365
366 static PyObject *
367 socketmmi_stop_mmi(PyObject *self, PyObject *args)
368 {
369         int slot;
370         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
371                 return NULL;
372         return PyInt_FromLong(eSocket_UI::getInstance()->stopMMI(slot));
373 }
374
375 static PyObject *
376 socketmmi_answer_menu(PyObject *self, PyObject *args)
377 {
378         int slot, answer;
379         if (PyTuple_Size(args) != 2 || !PyArg_ParseTuple(args, "ii", &slot, &answer))
380                 return NULL;
381         return PyInt_FromLong(eSocket_UI::getInstance()->answerMenu(slot, answer));
382 }
383
384 static PyObject *
385 socketmmi_answer_enq(PyObject *self, PyObject *args)
386 {
387         int slot;
388         char *answer;
389         if (PyTuple_Size(args) != 2 || !PyArg_ParseTuple(args, "is", &slot, &answer))
390                 return NULL;
391         return PyInt_FromLong(eSocket_UI::getInstance()->answerEnq(slot, answer));
392 }
393
394 static PyObject *
395 socketmmi_cancel_enq(PyObject *self, PyObject *args)
396 {
397         int slot;
398         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
399                 return NULL;
400         return PyInt_FromLong(eSocket_UI::getInstance()->cancelEnq(slot));
401 }
402
403 static PyObject *
404 socketmmi_get_state(PyObject *self, PyObject *args)
405 {
406         int slot;
407         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
408                 return NULL;
409         return PyInt_FromLong(eSocket_UI::getInstance()->getState(slot));
410 }
411
412 static PyObject *
413 socketmmi_get_mmi_state(PyObject *self, PyObject *args)
414 {
415         int slot;
416         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
417                 return NULL;
418         return PyInt_FromLong(eSocket_UI::getInstance()->getMMIState(slot));
419 }
420
421 static PyObject *
422 socketmmi_get_name(PyObject *self, PyObject *args)
423 {
424         int slot;
425         if (PyTuple_Size(args) != 1 || !PyArg_ParseTuple(args, "i", &slot))
426                 return NULL;
427         return PyString_FromString(eSocket_UI::getInstance()->getName(slot));
428 }
429
430 static PyMethodDef module_methods[] = {
431         {"getSocketStateChangedCallbackList", (PyCFunction)socketmmi_get_socket_state_changed_cb_list, METH_NOARGS,
432          "get socket state change callback list"
433         },
434         {"setInit", (PyCFunction)socketmmi_set_init, METH_VARARGS,
435          "set init"
436         },
437         {"setReset", (PyCFunction)socketmmi_set_reset, METH_VARARGS,
438          "set reset"
439         },
440         {"availableMMI", (PyCFunction)socketmmi_available_mmi, METH_VARARGS,
441          "available mmi"
442         },
443         {"getMMIScreen", (PyCFunction)socketmmi_get_mmi_screen, METH_VARARGS,
444          "get mmi screen"
445         },
446         {"startMMI", (PyCFunction)socketmmi_start_mmi, METH_VARARGS,
447          "start mmi"
448         },
449         {"stopMMI", (PyCFunction)socketmmi_stop_mmi, METH_VARARGS,
450          "start mmi"
451         },
452         {"answerMenu", (PyCFunction)socketmmi_answer_menu, METH_VARARGS,
453          "answer menu"
454         },
455         {"answerEnq", (PyCFunction)socketmmi_answer_enq, METH_VARARGS,
456          "answer enq"
457         },
458         {"cancelEnq", (PyCFunction)socketmmi_cancel_enq, METH_VARARGS,
459          "cancel enq"
460         },
461         {"getState", (PyCFunction)socketmmi_get_state, METH_VARARGS,
462          "get state of socket"
463         },
464         {"getMMIState", (PyCFunction)socketmmi_get_mmi_state, METH_VARARGS,
465          "get state of mmi"
466         },
467         {"getName", (PyCFunction)socketmmi_get_name, METH_VARARGS,
468          "get name of socket user"
469         },
470         {NULL, NULL, 0, NULL}   /* Sentinel */
471 };
472
473 PyMODINIT_FUNC
474 initsocketmmi(void)
475 {
476         Py_InitModule3("socketmmi", module_methods,
477                 "Module that implements mmi via unix domain socket.");
478 }
479 };