Merge branch 'nto-signal-stats'
[dvblast.git] / dvblastctl.c
blob20382bc652fab04e0e848f12faff5a76f204d70c
1 /*****************************************************************************
2 * dvblastctl.c
3 *****************************************************************************
4 * Copyright (C) 2008 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <sys/un.h>
38 #include <errno.h>
39 #include <getopt.h>
41 #include <iconv.h>
43 #include <bitstream/mpeg/psi.h>
44 #include <bitstream/dvb/si.h>
45 #include <bitstream/dvb/si_print.h>
46 #include <bitstream/mpeg/psi_print.h>
48 #include "dvblast.h"
49 #include "en50221.h"
50 #include "comm.h"
52 int i_verbose = 3;
53 int i_syslog = 0;
54 unsigned int i_timeout_seconds = 15;
56 print_type_t i_print_type = PRINT_TEXT;
57 mtime_t now;
59 int i_fd = -1;
60 char psz_client_socket[PATH_MAX] = {0};
62 static iconv_t iconv_handle = (iconv_t)-1;
64 static void clean_client_socket() {
65 if ( i_fd > -1 )
67 close( i_fd );
68 i_fd = -1;
70 if ( psz_client_socket[0] )
72 unlink( psz_client_socket );
73 psz_client_socket[0] = '\0';
77 /*****************************************************************************
78 * The following two functions are from biTStream's examples and are under the
79 * WTFPL (see LICENSE.WTFPL).
80 ****************************************************************************/
81 __attribute__ ((format(printf, 2, 3)))
82 static void psi_print(void *_unused, const char *psz_format, ...)
84 char psz_fmt[strlen(psz_format) + 2];
85 va_list args;
86 va_start(args, psz_format);
87 strcpy(psz_fmt, psz_format);
88 strcat(psz_fmt, "\n");
89 vprintf(psz_fmt, args);
90 va_end(args);
93 __attribute__ ((format(printf, 1, 2)))
94 void return_error( const char *psz_format, ... )
96 va_list args;
97 char psz_fmt[1024];
99 clean_client_socket();
101 va_start( args, psz_format );
102 if ( i_print_type == PRINT_XML )
103 snprintf( psz_fmt, sizeof(psz_fmt) - 1, "<ERROR msg=\"%s\"/>\n", psz_format );
104 else
105 snprintf( psz_fmt, sizeof(psz_fmt) - 1, "ERROR: %s\n", psz_format );
106 psz_fmt[sizeof(psz_fmt) - 1] = '\0';
107 vfprintf( stderr, psz_fmt, args );
108 va_end(args);
109 exit(255);
112 static char *iconv_append_null(const char *p_string, size_t i_length)
114 char *psz_string = malloc(i_length + 1);
115 memcpy(psz_string, p_string, i_length);
116 psz_string[i_length] = '\0';
117 return psz_string;
120 const char *psz_native_charset = "UTF-8//IGNORE";
122 char *psi_iconv(void *_unused, const char *psz_encoding,
123 char *p_string, size_t i_length)
125 static const char *psz_current_encoding = "";
127 char *psz_string, *p;
128 size_t i_out_length;
130 if (!strcmp(psz_encoding, psz_native_charset))
131 return iconv_append_null(p_string, i_length);
133 if (iconv_handle != (iconv_t)-1 &&
134 strcmp(psz_encoding, psz_current_encoding)) {
135 iconv_close(iconv_handle);
136 iconv_handle = (iconv_t)-1;
139 if (iconv_handle == (iconv_t)-1)
140 iconv_handle = iconv_open(psz_native_charset, psz_encoding);
141 if (iconv_handle == (iconv_t)-1) {
142 msg_Warn(NULL, "couldn't open converter from %s to %s (%m)", psz_encoding,
143 psz_native_charset);
144 return iconv_append_null(p_string, i_length);
146 psz_current_encoding = psz_encoding;
148 /* converted strings can be up to six times larger */
149 i_out_length = i_length * 6;
150 p = psz_string = malloc(i_out_length);
151 if (iconv(iconv_handle, &p_string, &i_length, &p, &i_out_length) == (size_t)-1) {
152 msg_Warn(NULL, "couldn't convert from %s to %s (%m)", psz_encoding,
153 psz_native_charset);
154 free(psz_string);
155 return iconv_append_null(p_string, i_length);
157 if (i_length)
158 msg_Warn(NULL, "partial conversion from %s to %s", psz_encoding,
159 psz_native_charset);
161 *p = '\0';
162 return psz_string;
165 void print_pids_header( void )
167 if ( i_print_type == PRINT_XML )
168 printf("<PIDS>\n");
171 void print_pids_footer( void )
173 if ( i_print_type == PRINT_XML )
174 printf("</PIDS>\n");
177 void print_pid(uint16_t i_pid, ts_pid_info_t *p_info)
179 if ( p_info->i_packets == 0 )
180 return;
181 if ( i_print_type == PRINT_TEXT )
182 printf("pid %d packn %lu ccerr %lu tserr %lu scramble %d Bps %lu seen %"PRId64"\n",
183 i_pid,
184 p_info->i_packets,
185 p_info->i_cc_errors,
186 p_info->i_transport_errors,
187 p_info->i_scrambling,
188 p_info->i_bytes_per_sec,
189 now - p_info->i_last_packet_ts
191 else
192 printf("<PID pid=\"%d\" packn=\"%lu\" ccerr=\"%lu\" tserr=\"%lu\" scramble=\"%d\" Bps=\"%lu\" seen=\"%"PRId64"\" />\n",
193 i_pid,
194 p_info->i_packets,
195 p_info->i_cc_errors,
196 p_info->i_transport_errors,
197 p_info->i_scrambling,
198 p_info->i_bytes_per_sec,
199 now - p_info->i_last_packet_ts
203 void print_pids( uint8_t *p_data )
205 int i_pid;
206 print_pids_header();
207 for ( i_pid = 0; i_pid < MAX_PIDS; i_pid++ ) {
208 ts_pid_info_t *p_info = (ts_pid_info_t *)(p_data + i_pid * sizeof(ts_pid_info_t));
209 print_pid( i_pid, p_info );
211 print_pids_footer();
214 void print_eit_events(uint8_t *p_eit, f_print pf_print, void *print_opaque, f_iconv pf_iconv, void *iconv_opaque, print_type_t i_print_type)
216 uint8_t *p_event;
217 uint8_t j = 0;
218 while ((p_event = eit_get_event(p_eit, j)) != NULL) {
219 j++;
220 char start_str[24];
221 int duration, hour, min, sec;
222 time_t start_ts;
224 start_ts = dvb_time_format_UTC(eitn_get_start_time(p_event), NULL, start_str);
226 dvb_time_decode_bcd(eitn_get_duration_bcd(p_event), &duration, &hour, &min, &sec);
228 switch (i_print_type) {
229 case PRINT_XML:
230 pf_print(print_opaque, "<EVENT id=\"%u\" start_time=\"%ld\" start_time_dec=\"%s\""
231 " duration=\"%u\" duration_dec=\"%u:%02u:%02u\""
232 " running=\"%d\" free_CA=\"%d\">",
233 eitn_get_event_id(p_event),
234 start_ts, start_str,
235 duration, hour, min, sec,
236 eitn_get_running(p_event),
237 eitn_get_ca(p_event)
239 break;
240 default:
241 pf_print(print_opaque, " * EVENT id=%u start_time=%ld start_time_dec=\"%s\" duration=%u duration_dec=%u:%02u:%02u running=%d free_CA=%d",
242 eitn_get_event_id(p_event),
243 start_ts, start_str,
244 duration, hour, min, sec,
245 eitn_get_running(p_event),
246 eitn_get_ca(p_event)
250 descs_print(eitn_get_descs(p_event), pf_print, print_opaque,
251 pf_iconv, iconv_opaque, i_print_type);
253 switch (i_print_type) {
254 case PRINT_XML:
255 pf_print(print_opaque, "</EVENT>");
256 break;
257 default:
258 break;
263 void print_eit(uint8_t *p_eit, unsigned int i_eit_size, f_print pf_print, void *print_opaque, f_iconv pf_iconv, void *iconv_opaque, print_type_t i_print_type)
265 uint8_t *p_eit_end = p_eit + i_eit_size;
266 uint8_t i_tid = psi_get_tableid(p_eit);
267 const char *psz_tid = "unknown";
269 if (i_tid == EIT_TABLE_ID_PF_ACTUAL)
270 psz_tid = "actual_pf";
271 else if (i_tid == EIT_TABLE_ID_PF_OTHER)
272 psz_tid = "other_pf";
273 else if (i_tid >= EIT_TABLE_ID_SCHED_ACTUAL_FIRST && i_tid <= EIT_TABLE_ID_SCHED_ACTUAL_LAST)
274 psz_tid = "actual_schedule";
275 else if (i_tid >= EIT_TABLE_ID_SCHED_OTHER_FIRST && i_tid <= EIT_TABLE_ID_SCHED_OTHER_LAST)
276 psz_tid = "other_schedule";
278 switch (i_print_type) {
279 case PRINT_XML:
280 pf_print(print_opaque,
281 "<EIT tableid=\"0x%02x\" type=\"%s\" service_id=\"%u\""
282 " version=\"%u\""
283 " current_next=\"%u\""
284 " tsid=\"%u\" onid=\"%u\">",
285 i_tid, psz_tid,
286 eit_get_sid(p_eit),
287 psi_get_version(p_eit),
288 !psi_get_current(p_eit) ? 0 : 1,
289 eit_get_tsid(p_eit),
290 eit_get_onid(p_eit)
292 break;
293 default:
294 pf_print(print_opaque,
295 "new EIT tableid=0x%02x type=%s service_id=%u version=%u%s"
296 " tsid=%u"
297 " onid=%u",
298 i_tid, psz_tid,
299 eit_get_sid(p_eit),
300 psi_get_version(p_eit),
301 !psi_get_current(p_eit) ? " (next)" : "",
302 eit_get_tsid(p_eit),
303 eit_get_onid(p_eit)
307 while (p_eit < p_eit_end) {
308 print_eit_events(p_eit, pf_print, print_opaque, pf_iconv, iconv_opaque, i_print_type);
309 p_eit += psi_get_length( p_eit ) + PSI_HEADER_SIZE;
312 switch (i_print_type) {
313 case PRINT_XML:
314 pf_print(print_opaque, "</EIT>");
315 break;
316 default:
317 pf_print(print_opaque, "end EIT");
321 struct dvblastctl_option {
322 char * opt;
323 int nparams;
324 ctl_cmd_t cmd;
327 static const struct dvblastctl_option options[] =
329 { "reload", 0, CMD_RELOAD },
330 { "shutdown", 0, CMD_SHUTDOWN },
332 { "fe_status", 0, CMD_FRONTEND_STATUS },
333 { "mmi_status", 0, CMD_MMI_STATUS },
335 { "mmi_slot_status", 1, CMD_MMI_SLOT_STATUS }, /* arg: slot */
336 { "mmi_open", 1, CMD_MMI_OPEN }, /* arg: slot */
337 { "mmi_close", 1, CMD_MMI_CLOSE }, /* arg: slot */
338 { "mmi_get", 1, CMD_MMI_RECV }, /* arg: slot */
339 { "mmi_send_text", 1, CMD_MMI_SEND_TEXT }, /* arg: slot, en50221_mmi_object_t */
340 { "mmi_send_choice", 2, CMD_MMI_SEND_CHOICE }, /* arg: slot, en50221_mmi_object_t */
342 { "get_pat", 0, CMD_GET_PAT },
343 { "get_cat", 0, CMD_GET_CAT },
344 { "get_nit", 0, CMD_GET_NIT },
345 { "get_sdt", 0, CMD_GET_SDT },
346 { "get_eit_pf", 1, CMD_GET_EIT_PF }, /* arg: service_id (uint16_t) */
347 { "get_eit_schedule", 1, CMD_GET_EIT_SCHEDULE }, /* arg: service_id (uint16_t) */
348 { "get_pmt", 1, CMD_GET_PMT }, /* arg: service_id (uint16_t) */
349 { "get_pids", 0, CMD_GET_PIDS },
350 { "get_pid", 1, CMD_GET_PID }, /* arg: pid (uint16_t) */
352 { NULL, 0, 0 }
355 void usage()
357 printf("DVBlastctl %s (%s)\n", VERSION, VERSION_EXTRA );
358 printf("Usage: dvblastctl -r <remote socket> [-x <text|xml>] [cmd]\n");
359 printf("Options:\n");
360 printf(" -r --remote-socket <name> Set socket name to <name>.\n" );
361 printf(" -t --timeout <seconds> Set socket read/write timeout in seconds (default 15).\n" );
362 printf(" -j --system-charset <name> Character set used for output (default UTF-8//IGNORE)\n" );
363 printf(" -x --print <text|xml> Choose output format for info commands.\n" );
364 printf("Control commands:\n");
365 printf(" reload Reload configuration.\n");
366 printf(" shutdown Shutdown DVBlast.\n");
367 #ifdef HAVE_DVB_SUPPORT
368 printf("Status commands:\n");
369 printf(" fe_status Read frontend status information.\n");
370 printf(" mmi_status Read CAM status.\n");
371 printf("MMI commands:\n");
372 printf(" mmi_slot_status <slot> Read MMI slot status.\n");
373 printf(" mmi_open <slot> Open MMI slot.\n");
374 printf(" mmi_close <slot> Close MMI slot.\n");
375 printf(" mmi_get <slot> Read MMI slot.\n");
376 printf(" mmi_send_text <slot> <text> Send text to MMI slot.\n");
377 printf(" mmi_send_choice <slot> <choice> Send choice to MMI slot.\n");
378 #endif
379 printf("Demux info commands:\n");
380 printf(" get_pat Return last PAT table.\n");
381 printf(" get_cat Return last CAT table.\n");
382 printf(" get_nit Return last NIT table.\n");
383 printf(" get_sdt Return last SDT table.\n");
384 printf(" get_eit_pf <service_id> Return last EIT present/following data.\n");
385 printf(" get_eit_schedule <service_id> Return last EIT schedule data.\n");
386 printf(" get_pmt <service_id> Return last PMT table.\n");
387 printf(" get_pids Return info about all pids.\n");
388 printf(" get_pid <pid> Return info for chosen pid only.\n");
389 printf("\n");
390 exit(1);
393 int main( int i_argc, char **ppsz_argv )
395 char *client_socket_tmpl = "dvblastctl.clientsock.XXXXXX";
396 char *psz_srv_socket = NULL;
397 int i;
398 char *p_cmd, *p_arg1 = NULL, *p_arg2 = NULL;
399 ssize_t i_size;
400 struct sockaddr_un sun_client, sun_server;
401 uint8_t p_buffer[COMM_BUFFER_SIZE];
402 uint8_t *p_data = p_buffer + COMM_HEADER_SIZE;
403 uint16_t i_pid = 0;
404 struct dvblastctl_option opt = { 0, 0, 0 };
406 for ( ; ; )
408 int c;
410 static const struct option long_options[] =
412 {"remote-socket", required_argument, NULL, 'r'},
413 {"timeout", required_argument, NULL, 't'},
414 {"system-charset", required_argument, NULL, 'j'},
415 {"print", required_argument, NULL, 'x'},
416 {"help", no_argument, NULL, 'h'},
417 {0, 0, 0, 0}
420 if ( (c = getopt_long(i_argc, ppsz_argv, "r:t:x:j:h", long_options, NULL)) == -1 )
421 break;
423 switch ( c )
425 case 'r':
426 psz_srv_socket = optarg;
427 break;
429 case 't':
430 i_timeout_seconds = (unsigned int)strtoul(optarg, NULL, 10);
431 break;
433 case 'j':
434 psz_native_charset = optarg;
435 break;
437 case 'x':
438 if ( !strcmp(optarg, "text") )
439 i_print_type = PRINT_TEXT;
440 else if ( !strcmp(optarg, "xml") )
441 i_print_type = PRINT_XML;
442 else
443 msg_Warn( NULL, "unrecognized print type %s", optarg );
444 /* Make stdout line-buffered */
445 setvbuf(stdout, NULL, _IOLBF, 0);
446 break;
448 case 'h':
449 default:
450 usage();
454 /* Validate commands */
455 #define usage_error(msg, ...) \
456 do { \
457 msg_Err( NULL, msg, ##__VA_ARGS__ ); \
458 usage(); \
459 } while(0)
460 p_cmd = ppsz_argv[optind];
461 p_arg1 = ppsz_argv[optind + 1];
462 p_arg2 = ppsz_argv[optind + 2];
464 if ( !psz_srv_socket )
465 usage_error( "Remote socket is not set.\n" );
467 if ( !p_cmd )
468 usage_error( "Command is not set.\n" );
470 i = 0;
471 do {
472 if ( streq(ppsz_argv[optind], options[i].opt) )
474 opt = options[i];
475 break;
477 } while ( options[++i].opt );
479 if ( !opt.opt )
480 usage_error( "Unknown command: %s\n", p_cmd );
482 if ( opt.nparams == 1 && !p_arg1 )
483 usage_error( "%s option needs parameter.\n", opt.opt );
485 if ( opt.nparams == 2 && (!p_arg1 || !p_arg2) )
486 usage_error( "%s option needs two parameters.\n", opt.opt );
487 #undef usage_error
489 /* Create client socket name */
490 char *tmpdir = getenv("TMPDIR");
491 snprintf( psz_client_socket, PATH_MAX - 1, "%s/%s",
492 tmpdir ? tmpdir : "/tmp", client_socket_tmpl );
493 psz_client_socket[PATH_MAX - 1] = '\0';
495 int tmp_fd = mkstemp(psz_client_socket);
496 if ( tmp_fd > -1 ) {
497 close(tmp_fd);
498 unlink(psz_client_socket);
499 } else {
500 return_error( "Cannot build UNIX socket %s (%s)", psz_client_socket, strerror(errno) );
503 if ( (i_fd = socket( AF_UNIX, SOCK_DGRAM, 0 )) < 0 )
504 return_error( "Cannot create UNIX socket (%s)", strerror(errno) );
506 i = COMM_MAX_MSG_CHUNK;
507 setsockopt( i_fd, SOL_SOCKET, SO_RCVBUF, &i, sizeof(i) );
509 memset( &sun_client, 0, sizeof(sun_client) );
510 sun_client.sun_family = AF_UNIX;
511 strncpy( sun_client.sun_path, psz_client_socket,
512 sizeof(sun_client.sun_path) );
513 sun_client.sun_path[sizeof(sun_client.sun_path) - 1] = '\0';
515 if ( bind( i_fd, (struct sockaddr *)&sun_client,
516 SUN_LEN(&sun_client) ) < 0 )
517 return_error( "Cannot bind (%s)", strerror(errno) );
519 memset( &sun_server, 0, sizeof(sun_server) );
520 sun_server.sun_family = AF_UNIX;
521 strncpy( sun_server.sun_path, psz_srv_socket, sizeof(sun_server.sun_path) );
522 sun_server.sun_path[sizeof(sun_server.sun_path) - 1] = '\0';
524 p_buffer[0] = COMM_HEADER_MAGIC;
525 p_buffer[1] = opt.cmd;
526 memset( p_buffer + 2, 0, COMM_HEADER_SIZE - 2 );
527 i_size = COMM_HEADER_SIZE;
529 /* Handle commands that send parameters */
530 switch ( opt.cmd )
532 case CMD_INVALID:
533 case CMD_RELOAD:
534 case CMD_SHUTDOWN:
535 case CMD_FRONTEND_STATUS:
536 case CMD_MMI_STATUS:
537 case CMD_GET_PAT:
538 case CMD_GET_CAT:
539 case CMD_GET_NIT:
540 case CMD_GET_SDT:
541 case CMD_GET_PIDS:
542 /* These commands need no special handling because they have no parameters */
543 break;
544 case CMD_GET_EIT_PF:
545 case CMD_GET_EIT_SCHEDULE:
546 case CMD_GET_PMT:
548 uint16_t i_sid = atoi(p_arg1);
549 i_size = COMM_HEADER_SIZE + 2;
550 p_data[0] = (uint8_t)((i_sid >> 8) & 0xff);
551 p_data[1] = (uint8_t)(i_sid & 0xff);
552 break;
554 case CMD_GET_PID:
556 i_pid = (uint16_t)atoi(p_arg1);
557 i_size = COMM_HEADER_SIZE + 2;
558 p_data[0] = (uint8_t)((i_pid >> 8) & 0xff);
559 p_data[1] = (uint8_t)(i_pid & 0xff);
560 break;
562 #ifdef HAVE_DVB_SUPPORT
563 case CMD_MMI_SEND_TEXT:
565 struct cmd_mmi_send *p_cmd = (struct cmd_mmi_send *)p_data;
566 p_cmd->i_slot = atoi(p_arg1);
568 en50221_mmi_object_t object;
569 object.i_object_type = EN50221_MMI_ANSW;
570 if ( !p_arg2 || p_arg2[0] == '\0' )
572 object.u.answ.b_ok = 0;
573 object.u.answ.psz_answ = "";
575 else
577 object.u.answ.b_ok = 1;
578 object.u.answ.psz_answ = p_arg2;
580 i_size = COMM_BUFFER_SIZE - COMM_HEADER_SIZE
581 - ((void *)&p_cmd->object - (void *)p_cmd);
582 if ( en50221_SerializeMMIObject( (uint8_t *)&p_cmd->object,
583 &i_size, &object ) == -1 )
584 return_error( "Comm buffer is too small" );
586 i_size += COMM_HEADER_SIZE
587 + ((void *)&p_cmd->object - (void *)p_cmd);
588 break;
590 case CMD_MMI_SEND_CHOICE:
592 struct cmd_mmi_send *p_cmd = (struct cmd_mmi_send *)p_data;
593 p_cmd->i_slot = atoi(p_arg1);
595 i_size = COMM_HEADER_SIZE + sizeof(struct cmd_mmi_send);
596 p_cmd->object.i_object_type = EN50221_MMI_MENU_ANSW;
597 p_cmd->object.u.menu_answ.i_choice = atoi(p_arg2);
598 break;
600 case CMD_MMI_SLOT_STATUS:
601 case CMD_MMI_OPEN:
602 case CMD_MMI_CLOSE:
603 case CMD_MMI_RECV:
605 p_data[0] = atoi(p_arg1);
606 i_size = COMM_HEADER_SIZE + 1;
607 break;
609 #endif
610 default:
611 /* This should not happen */
612 return_error( "Unhandled option (%d)", opt.cmd );
615 if ( i_timeout_seconds > 0 ) {
616 struct timeval tv_timeout = {
617 .tv_sec = i_timeout_seconds,
618 .tv_usec = 0,
620 if ( setsockopt( i_fd, SOL_SOCKET, SO_SNDTIMEO, &tv_timeout, sizeof( tv_timeout ) ) != 0 )
621 return_error( "Cannot set SO_SNDTIMEO (%s)", strerror(errno) );
623 if ( setsockopt( i_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof( tv_timeout ) ) != 0 )
624 return_error( "Cannot set SO_RCVTIMEO (%s)", strerror(errno) );
627 /* Send command and receive answer */
628 if ( sendto( i_fd, p_buffer, i_size, 0, (struct sockaddr *)&sun_server,
629 SUN_LEN(&sun_server) ) < 0 )
630 return_error( "Cannot send comm socket (%s)", strerror(errno) );
632 uint32_t i_packet_size = 0, i_received = 0;
633 do {
634 i_size = recv( i_fd, p_buffer + i_received, COMM_MAX_MSG_CHUNK, 0 );
635 if ( i_size == -1 )
636 break;
637 if ( !i_packet_size ) {
638 uint32_t *p_packet_size = (uint32_t *)&p_buffer[4];
639 i_packet_size = *p_packet_size;
640 if ( i_packet_size > COMM_BUFFER_SIZE ) {
641 i_size = -1;
642 break;
645 i_received += i_size;
646 } while ( i_received < i_packet_size );
648 clean_client_socket();
649 if ( i_packet_size < COMM_HEADER_SIZE )
650 return_error( "Cannot recv from comm socket, size:%"PRIu32" (%s)", i_packet_size, strerror(errno) );
652 /* Process answer */
653 if ( p_buffer[0] != COMM_HEADER_MAGIC )
654 return_error( "Wrong protocol version 0x%x", p_buffer[0] );
656 now = mdate();
658 ctl_cmd_answer_t c_answer = p_buffer[1];
659 switch ( c_answer )
661 case RET_OK:
662 break;
664 case RET_MMI_WAIT:
665 exit(252);
666 break;
668 case RET_ERR:
669 return_error( "Request failed" );
670 break;
672 case RET_HUH:
673 return_error( "Internal error" );
674 break;
676 case RET_NODATA:
677 return_error( "No data" );
678 break;
680 case RET_PAT:
681 case RET_CAT:
682 case RET_NIT:
683 case RET_SDT:
685 uint8_t *p_flat_data = p_buffer + COMM_HEADER_SIZE;
686 unsigned int i_flat_data_size = i_packet_size - COMM_HEADER_SIZE;
687 uint8_t **pp_sections = psi_unpack_sections( p_flat_data, i_flat_data_size );
688 if ( !pp_sections )
690 return_error( "Error unpacking PSI" );
691 break;
694 switch( c_answer )
696 case RET_PAT: pat_table_print( pp_sections, psi_print, NULL, i_print_type ); break;
697 case RET_CAT: cat_table_print( pp_sections, psi_print, NULL, i_print_type ); break;
698 case RET_NIT: nit_table_print( pp_sections, psi_print, NULL, psi_iconv, NULL, i_print_type ); break;
699 case RET_SDT: sdt_table_print( pp_sections, psi_print, NULL, psi_iconv, NULL, i_print_type ); break;
700 default: break; /* Can't happen */
703 psi_table_free( pp_sections );
704 free( pp_sections );
705 break;
708 case RET_EIT_PF:
709 case RET_EIT_SCHEDULE:
711 uint8_t *p_eit_data = p_buffer + COMM_HEADER_SIZE;
712 unsigned int i_eit_data_size = i_received - COMM_HEADER_SIZE;
713 print_eit( p_eit_data, i_eit_data_size, psi_print, NULL, psi_iconv, NULL, i_print_type );
714 break;
717 case RET_PMT:
719 pmt_print( p_data, psi_print, NULL, psi_iconv, NULL, i_print_type );
720 break;
723 case RET_PID:
725 print_pids_header();
726 print_pid( i_pid, (ts_pid_info_t *)p_data );
727 print_pids_footer();
728 break;
731 case RET_PIDS:
733 print_pids( p_data );
734 break;
737 #ifdef HAVE_DVB_SUPPORT
738 case RET_FRONTEND_STATUS:
740 int ret = 1;
741 struct ret_frontend_status *p_ret =
742 (struct ret_frontend_status *)&p_buffer[COMM_HEADER_SIZE];
743 if ( i_packet_size != COMM_HEADER_SIZE + sizeof(struct ret_frontend_status) )
744 return_error( "Bad frontend status" );
746 if ( i_print_type == PRINT_XML )
747 printf("<FRONTEND>\n");
749 #define PRINT_TYPE( x ) \
750 do { \
751 if ( i_print_type == PRINT_XML ) \
752 printf( " <TYPE type=\"%s\"/>\n", STRINGIFY(x) ); \
753 else \
754 printf( "type: %s\n", STRINGIFY(x) ); \
755 } while(0)
756 switch ( p_ret->info.type )
758 case FE_QPSK: PRINT_TYPE(QPSK); break;
759 case FE_QAM : PRINT_TYPE(QAM); break;
760 case FE_OFDM: PRINT_TYPE(OFDM); break;
761 case FE_ATSC: PRINT_TYPE(ATSC); break;
762 default : PRINT_TYPE(UNKNOWN); break;
764 #undef PRINT_TYPE
766 #define PRINT_INFO( x ) \
767 do { \
768 if ( i_print_type == PRINT_XML ) \
769 printf( " <SETTING %s=\"%u\"/>\n", STRINGIFY(x), p_ret->info.x ); \
770 else \
771 printf( "%s: %u\n", STRINGIFY(x), p_ret->info.x ); \
772 } while(0)
774 if ( i_print_type == PRINT_XML )
775 printf( " <SETTING name=\"%s\"/>\n", p_ret->info.name );
776 else
777 printf( "name: %s\n", p_ret->info.name );
779 PRINT_INFO( frequency_min );
780 PRINT_INFO( frequency_max );
781 PRINT_INFO( frequency_stepsize );
782 PRINT_INFO( frequency_tolerance );
783 PRINT_INFO( symbol_rate_min );
784 PRINT_INFO( symbol_rate_max );
785 PRINT_INFO( symbol_rate_tolerance );
786 PRINT_INFO( notifier_delay );
787 #undef PRINT_INFO
789 if ( i_print_type == PRINT_TEXT )
790 printf("\ncapability list:\n");
792 #define PRINT_CAPS( x ) \
793 do { \
794 if ( p_ret->info.caps & (FE_##x) ) { \
795 if ( i_print_type == PRINT_XML ) { \
796 printf( " <CAPABILITY %s=\"1\"/>\n", STRINGIFY(x) ); \
797 } else { \
798 printf( "%s\n", STRINGIFY(x) ); \
801 } while(0)
802 PRINT_CAPS( IS_STUPID );
803 PRINT_CAPS( CAN_INVERSION_AUTO );
804 PRINT_CAPS( CAN_FEC_1_2 );
805 PRINT_CAPS( CAN_FEC_2_3 );
806 PRINT_CAPS( CAN_FEC_3_4 );
807 PRINT_CAPS( CAN_FEC_4_5 );
808 PRINT_CAPS( CAN_FEC_5_6 );
809 PRINT_CAPS( CAN_FEC_6_7 );
810 PRINT_CAPS( CAN_FEC_7_8 );
811 PRINT_CAPS( CAN_FEC_8_9 );
812 PRINT_CAPS( CAN_FEC_AUTO );
813 PRINT_CAPS( CAN_QPSK );
814 PRINT_CAPS( CAN_QAM_16 );
815 PRINT_CAPS( CAN_QAM_32 );
816 PRINT_CAPS( CAN_QAM_64 );
817 PRINT_CAPS( CAN_QAM_128 );
818 PRINT_CAPS( CAN_QAM_256 );
819 PRINT_CAPS( CAN_QAM_AUTO );
820 PRINT_CAPS( CAN_TRANSMISSION_MODE_AUTO );
821 PRINT_CAPS( CAN_BANDWIDTH_AUTO );
822 PRINT_CAPS( CAN_GUARD_INTERVAL_AUTO );
823 PRINT_CAPS( CAN_HIERARCHY_AUTO );
824 PRINT_CAPS( CAN_MUTE_TS );
826 #if DVBAPI_VERSION >= 301
827 PRINT_CAPS( CAN_8VSB );
828 PRINT_CAPS( CAN_16VSB );
829 PRINT_CAPS( NEEDS_BENDING );
830 PRINT_CAPS( CAN_RECOVER );
831 #endif
832 #if DVBAPI_VERSION >= 500
833 PRINT_CAPS( HAS_EXTENDED_CAPS );
834 #endif
835 #if DVBAPI_VERSION >= 501
836 PRINT_CAPS( CAN_2G_MODULATION );
837 #endif
838 #if DVBAPI_VERSION >= 508
839 PRINT_CAPS( CAN_TURBO_FEC );
840 PRINT_CAPS( CAN_MULTISTREAM );
841 #endif
842 #undef PRINT_CAPS
844 if ( i_print_type == PRINT_TEXT )
845 printf("\nstatus:\n");
847 #define PRINT_STATUS( x ) \
848 do { \
849 if ( p_ret->i_status & (FE_##x) ) { \
850 if ( i_print_type == PRINT_XML ) { \
851 printf( " <STATUS status=\"%s\"/>\n", STRINGIFY(x) ); \
852 } else { \
853 printf( "%s\n", STRINGIFY(x) ); \
856 } while(0)
857 PRINT_STATUS( HAS_SIGNAL );
858 PRINT_STATUS( HAS_CARRIER );
859 PRINT_STATUS( HAS_VITERBI );
860 PRINT_STATUS( HAS_SYNC );
861 PRINT_STATUS( HAS_LOCK );
862 PRINT_STATUS( REINIT );
863 #undef PRINT_STATUS
865 if ( p_ret->i_status & FE_HAS_LOCK )
867 if ( i_print_type == PRINT_XML )
869 printf(" <VALUE bit_error_rate=\"%u\"/>\n", p_ret->i_ber);
870 printf(" <VALUE signal_strength=\"%u\"/>\n", p_ret->i_strength);
871 printf(" <VALUE SNR=\"%u\"/>\n", p_ret->i_snr);
872 } else {
873 printf("\nBit error rate: %u\n", p_ret->i_ber);
874 printf("Signal strength: %u\n", p_ret->i_strength);
875 printf("SNR: %u\n", p_ret->i_snr);
877 ret = 0;
880 if ( i_print_type == PRINT_XML )
881 printf("</FRONTEND>\n" );
883 exit(ret);
884 break;
887 case RET_MMI_STATUS:
889 struct ret_mmi_status *p_ret =
890 (struct ret_mmi_status *)&p_buffer[COMM_HEADER_SIZE];
891 if ( i_packet_size != COMM_HEADER_SIZE + sizeof(struct ret_mmi_status) )
892 return_error( "Bad MMI status" );
894 printf("CA interface with %d %s, type:\n", p_ret->caps.slot_num,
895 p_ret->caps.slot_num == 1 ? "slot" : "slots");
896 #define PRINT_CAPS( x, s ) \
897 if ( p_ret->caps.slot_type & (CA_##x) ) \
898 printf(s "\n");
899 PRINT_CAPS( CI, "CI high level interface" );
900 PRINT_CAPS( CI_LINK, "CI link layer level interface" );
901 PRINT_CAPS( CI_PHYS, "CI physical layer level interface (not supported)" );
902 PRINT_CAPS( DESCR, "built-in descrambler" );
903 PRINT_CAPS( SC, "simple smartcard interface" );
904 #undef PRINT_CAPS
906 printf("\n%d available %s\n", p_ret->caps.descr_num,
907 p_ret->caps.descr_num == 1 ? "descrambler (key)" :
908 "descramblers (keys)");
909 #define PRINT_DESC( x ) \
910 if ( p_ret->caps.descr_type & (CA_##x) ) \
911 printf( STRINGIFY(x) "\n" );
912 PRINT_DESC( ECD );
913 PRINT_DESC( NDS );
914 PRINT_DESC( DSS );
915 #undef PRINT_DESC
917 exit( p_ret->caps.slot_num );
918 break;
921 case RET_MMI_SLOT_STATUS:
923 struct ret_mmi_slot_status *p_ret =
924 (struct ret_mmi_slot_status *)&p_buffer[COMM_HEADER_SIZE];
925 if ( i_packet_size < COMM_HEADER_SIZE + sizeof(struct ret_mmi_slot_status) )
926 return_error( "Bad MMI slot status" );
928 printf("CA slot #%u: ", p_ret->sinfo.num);
930 #define PRINT_TYPE( x, s ) \
931 if ( p_ret->sinfo.type & (CA_##x) ) \
932 printf(s);
934 PRINT_TYPE( CI, "high level, " );
935 PRINT_TYPE( CI_LINK, "link layer level, " );
936 PRINT_TYPE( CI_PHYS, "physical layer level, " );
937 #undef PRINT_TYPE
939 if ( p_ret->sinfo.flags & CA_CI_MODULE_READY )
941 printf("module present and ready\n");
942 exit(0);
945 if ( p_ret->sinfo.flags & CA_CI_MODULE_PRESENT )
946 printf("module present, not ready\n");
947 else
948 printf("module not present\n");
950 exit(1);
951 break;
954 case RET_MMI_RECV:
956 struct ret_mmi_recv *p_ret =
957 (struct ret_mmi_recv *)&p_buffer[COMM_HEADER_SIZE];
958 if ( i_packet_size < COMM_HEADER_SIZE + sizeof(struct ret_mmi_recv) )
959 return_error( "Bad MMI recv" );
961 en50221_UnserializeMMIObject( &p_ret->object, i_packet_size
962 - COMM_HEADER_SIZE - ((void *)&p_ret->object - (void *)p_ret) );
964 switch ( p_ret->object.i_object_type )
966 case EN50221_MMI_ENQ:
967 printf("%s\n", p_ret->object.u.enq.psz_text);
968 printf("(empty to cancel)\n");
969 exit(p_ret->object.u.enq.b_blind ? 253 : 254);
970 break;
972 case EN50221_MMI_MENU:
973 printf("%s\n", p_ret->object.u.menu.psz_title);
974 printf("%s\n", p_ret->object.u.menu.psz_subtitle);
975 printf("0 - Cancel\n");
976 for ( i = 0; i < p_ret->object.u.menu.i_choices; i++ )
977 printf("%d - %s\n", i + 1,
978 p_ret->object.u.menu.ppsz_choices[i]);
979 printf("%s\n", p_ret->object.u.menu.psz_bottom);
980 exit(p_ret->object.u.menu.i_choices);
981 break;
983 case EN50221_MMI_LIST:
984 printf("%s\n", p_ret->object.u.menu.psz_title);
985 printf("%s\n", p_ret->object.u.menu.psz_subtitle);
986 for ( i = 0; i < p_ret->object.u.menu.i_choices; i++ )
987 printf("%s\n", p_ret->object.u.menu.ppsz_choices[i]);
988 printf("%s\n", p_ret->object.u.menu.psz_bottom);
989 printf("(0 to cancel)\n");
990 exit(0);
991 break;
993 default:
994 return_error( "Unknown MMI object" );
995 break;
998 exit(255);
999 break;
1001 #endif
1003 default:
1004 return_error( "Unknown command answer: %u", c_answer );
1007 if (iconv_handle != (iconv_t)-1) {
1008 iconv_close(iconv_handle);
1009 iconv_handle = (iconv_t)-1;
1012 return 0;