📄 siinfo.c
字号:
#include <sys/ioctl.h>#include <stdio.h>#include <stdint.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <time.h>#include <sys/poll.h>#include <time.h>#include <netinet/in.h>#include <ctype.h>#include <stdlib.h>#include <string.h>#include <linux/unistd.h>#include <unistd.h>#include <syslog.h>#include "linux/dvb/dmx.h"#include "siinfo.h"#define DATA_CAROUSEL_ID 0x13#define DATA_BROADCAST_ID 0x66#define DATA_STREAM 0x52#define MHP_BROADCAST_ID 0x6F#define UK_MHEG_DATA 0x106#define MHP_DATA 0xF0#define PACK __attribute__ ((__packed__))struct sect_header { uint8_t table_id PACK; uint16_t syntax_len PACK; uint16_t transport_stream_id PACK; uint8_t ver_cur PACK; uint8_t section_number PACK; uint8_t last_section_number PACK;};// H.222.0 2.4.4.3, Table 2-25struct PAT_sect { uint8_t table_id PACK; uint16_t syntax_len PACK; uint16_t transport_stream_id PACK; uint8_t vers_curr PACK; uint8_t sect_no PACK; uint8_t last_sect PACK; struct { uint16_t program_number PACK; uint16_t res_PMTPID PACK; } d[1] PACK;};// H.222.0 2.4.4.8, Table 2-28struct PMT_stream { uint8_t stream_type PACK; uint16_t res_PID PACK; uint16_t res_ES_info_len PACK; uint8_t descrs[2] PACK;};struct PMT_sect { uint8_t table_id PACK; uint16_t syntax_len PACK; uint16_t program_number PACK; uint8_t vers_curr PACK; uint8_t sect_no PACK; uint8_t last_sect PACK; uint16_t res_pcr PACK; uint16_t res_program_info_length PACK; struct PMT_stream s PACK;};struct Metadata_sect { unsigned int table_id; u_char section_syntax_indicator :1; u_char private_indicator :1; u_char random_access_indicator :1; u_char decoder_config_flag :1; unsigned int metadata_section_length: 12; unsigned int metadata_service_id; unsigned int reserved; u_char section_fragment_indication :2; u_char version_number :5; u_char current_next_indicator :1; unsigned int section_number; unsigned int last_section_number; char *metadata_byte; unsigned long CRC_32; int metadata_length;};struct app_profile { unsigned short profile; unsigned char major; unsigned char minor; unsigned char micro;};struct app_descr { unsigned short app_profile_len; struct app_profile *profiles; unsigned char flags; /* 11111223 - 3 - Service_bound_flag 2 - Visibility 1 - reserved */ unsigned char priority; char *proto_labels;};struct app_name { unsigned long iso_639_lang; unsigned char name_length; char *name;};struct app_name_descr { struct app_name *names;};struct app_icon_descr { unsigned char icon_loc_len; char *icon_loc; unsigned short flags; char *reserved;};struct app_auth { char app_identifier[6]; unsigned char priority;};struct app_auth_descr { struct app_auth *auths;};struct trans_proto_desc { unsigned short id; unsigned char proto_label; char *selector;};struct trans_proto_label { unsigned char len; char *label; unsigned char priority;};struct oc_prefetch_descr { unsigned char proto_label; struct trans_proto_label *labels;};struct oc_dii_descr { unsigned char proto_label; char *dii_location[4];};struct dvbj_app_descr { unsigned char param_len; char *param;};struct dvbj_loc_descr { unsigned char base_dir_len; char base_dir; unsigned char classpath_ext_len; char classpath_ext; char initial_class;};struct dvbh_app_descr { unsigned char appid_set_len; char application_id; char parameter;};struct dvbh_loc_descr { unsigned char physical_root_len; char *physical_root; char *inital_path;};struct dvbh_boundary_descr { unsigned char label_len; char *label; char *regular_expression;};struct route_ipv4_descr { unsigned char tag; unsigned long address; unsigned short port; unsigned long mask;};struct route_ipv6_descr { unsigned char tag; unsigned char address[16]; unsigned short port; unsigned char mask[16];}; struct mhp_descr { unsigned short tag; unsigned short len; union { struct app_descr app; struct app_name_descr app_name; struct app_icon_descr app_icon; struct app_auth_descr app_auth; struct trans_proto_desc trans_proto; struct trans_proto_label trans_label; struct oc_prefetch_descr oc_prefetch; struct dvbj_app_descr dvbj_app; struct dvbj_loc_descr dvbj_loc; struct dvbh_app_descr dvbh_app; struct dvbh_loc_descr dvbh_loc; struct dvbh_boundary_descr dvbh_bound; struct route_ipv4_descr route_ipv4; struct route_ipv6_descr route_ipv6; } descr;};struct app_info { unsigned long app_identifier; unsigned char app_control_code; unsigned short app_desc_length; struct mhp_descr *descr;};struct app_info_sect { unsigned char section_number; unsigned char last_section_number; unsigned short descr_length; struct mhp_descr *common_descrs; unsigned short app_loop_length; struct app_info *apps; unsigned long crc32; struct app_info_sect *next;};struct app_info_table { struct app_info_sect *sections;};static int SetSectFilt(int fd, unsigned short pid, uint8_t tnr, uint8_t mask){ int ret; struct dmx_sct_filter_params p; memset(&p, 0, sizeof(p)); p.filter.filter[0] = tnr; p.filter.mask[0] = mask; p.pid = pid; p.timeout = 800; p.flags = DMX_IMMEDIATE_START | DMX_CHECK_CRC; if ((ret = ioctl(fd, DMX_SET_FILTER, &p)) < 0) perror("DMX SET FILTER:"); return ret;}static int SetSectFilt2(int fd, unsigned short pid, uint8_t tnr, uint8_t mask){ int ret; struct dmx_sct_filter_params p; memset(&p, 0, sizeof(p)); p.filter.filter[0] = tnr; p.filter.mask[0] = mask; p.pid = pid; //p.timeout = 600; p.flags = DMX_IMMEDIATE_START | DMX_CHECK_CRC; if ((ret = ioctl(fd, DMX_SET_FILTER, &p)) < 0) perror("DMX SET FILTER:"); return ret;}/* * PID - pid to collect on * table_id - table id to filter out (H.222.0 table 2-26) * sects - pointer to char *[256], points to array of table sections, initially empty * numsects - number of sections in table * returns - 0 if everything ok */#define SECTSIZE 1024static int CollectSections(int card_no, int pid, int table_id, char **sects, int *numsects){ int fd; int ret = -1; int last_section = 0; int done = 0; char *p = NULL; int n; char name[100]; syslog(LOG_ERR, "COllectSections"); syslog(LOG_ERR, "opening adapter%d", card_no); snprintf(name, sizeof(name), "/dev/dvb/adapter%d/demux0", card_no); memset(sects, 0, sizeof(char*) * 256); if((fd = open(name, O_RDWR)) < 0){ syslog(LOG_ERR, "failed to open adapter%d", card_no); perror("DEMUX DEVICE 1: "); return -1; } if(SetSectFilt(fd, pid, table_id, 0xff)) { syslog(LOG_ERR, "setsectfilter failed"); ret = -1; goto bail; } do { struct sect_header *h; int i; if(p == NULL) p = (char *) malloc(SECTSIZE); n = read(fd, p, SECTSIZE); if(n < 8) { syslog(LOG_ERR, "n < 8"); break; } h = (struct sect_header *) p; if(n != ((ntohs(h->syntax_len) & 0xfff) + 3)) { fprintf(stderr, "bad section length: %x / %x!\n", n, ntohs(h->syntax_len) & 0xfff); continue; } if(!(h->ver_cur & 0x01)) // if not current continue; last_section = h->last_section_number; if(!sects[h->section_number]) { // section_number sects[h->section_number] = p; p = NULL; } for(i = 0; i <= last_section; i++) { if(!sects[i]) { syslog(LOG_ERR, "!sects[%d]", i); break; } if(i == last_section) { syslog(LOG_ERR, "Set numsects = %d", last_section+1); *numsects = last_section + 1; ret = 0; done = 1; } } } while (!done); syslog(LOG_ERR, "ret = %d", ret); bail: close(fd); return ret;}#define METADATA_SECT_SIZE 4096static int CollectMetadataSections(int card_no, int pid, int table_id, struct Metadata_sect **sects, int *numsects){ int fd; int ret = -1; int last_section = 0; int done = 0; char *p = NULL; int j, n; int len; char name[100]; if (table_id != 0x06) { perror("NOT A VALID Metadata Section: "); return -1; } snprintf(name, sizeof(name), "/dev/dvb/adapter%d/demux0", card_no); memset(sects, 0, sizeof(char*) * 256); if((fd = open(name, O_RDWR)) < 0){ perror("DEMUX DEVICE 1: "); return -1; } if(SetSectFilt2(fd, pid, table_id, 0xff)) { ret = -1; goto bail; } do { struct sect_header *h; int i; if(p == NULL) p = (char *) malloc(METADATA_SECT_SIZE); n = read(fd, p, METADATA_SECT_SIZE); printf("n = %d\n", n); if(n < 8) break; h = (struct sect_header *) p; if (n != ((ntohs(h->syntax_len) & 0xfff) + 3)) { fprintf(stderr, "bad section length: %x / %x!\n", n, ntohs(h->syntax_len) & 0xfff); continue; } if(!(h->ver_cur & 0x01)) // if not current continue; last_section = h->last_section_number; if(!sects[h->section_number]) { // section_number sects[h->section_number] = (struct Metadata_sect *)malloc(sizeof(struct Metadata_sect)); sects[h->section_number]->table_id = p[0]; sects[h->section_number]->section_syntax_indicator = (p[1] >> 7) & 0x01; sects[h->section_number]->private_indicator = (p[1] >> 6) & 0x01; sects[h->section_number]->random_access_indicator = (p[1] >> 5) & 0x01; sects[h->section_number]->decoder_config_flag = (p[1] >> 4) & 0x01; sects[h->section_number]->metadata_section_length = (0xFF & p[2]) | ((0x0F & p[1]) << 8); sects[h->section_number]->metadata_service_id = p[3]; sects[h->section_number]->reserved = p[4]; sects[h->section_number]->section_fragment_indication = (p[5] >> 6) & 0x03; sects[h->section_number]->version_number = (p[5] >> 3) & 0x1F; sects[h->section_number]->current_next_indicator = p[5] & 0x01; sects[h->section_number]->section_number = p[6]; sects[h->section_number]->last_section_number = p[7]; printf("table_id = %d\n", sects[h->section_number]->table_id); printf("section_syntax_indicator = %d\n", sects[h->section_number]->section_syntax_indicator); printf("private_indicator = %d\n", sects[h->section_number]->private_indicator); printf("random_access_indicator = %d\n", sects[h->section_number]->random_access_indicator); printf("decoder_config_flag = %d\n", sects[h->section_number]->decoder_config_flag); printf("metadata_section_length = %d\n", sects[h->section_number]->metadata_section_length); printf("metadata_service_id = %d\n", sects[h->section_number]->metadata_service_id); printf("reserved = %d\n", sects[h->section_number]->reserved); printf("section_fragment_indication = %d\n", sects[h->section_number]->section_fragment_indication); printf("version_number = %d\n", sects[h->section_number]->version_number); printf("current_next_indicator = %d\n", sects[h->section_number]->current_next_indicator); printf("section_number = %d\n", sects[h->section_number]->section_number); printf("last_section_number = %d\n", sects[h->section_number]->last_section_number); len = sects[h->section_number]->metadata_section_length - 9; sects[h->section_number]->metadata_length = len; printf("len = %d\n", len); sects[h->section_number]->metadata_byte = (char *)malloc(len); for (j = 0; j < len; j++) { sects[h->section_number]->metadata_byte[j] = p[8 + j]; }// printf("metadata_byte = %d\n", sects[h->section_number]->metadata_byte); sects[h->section_number]->CRC_32 = p[8 + len]; printf("CRC_32 = %ld\n", sects[h->section_number]->CRC_32); p = NULL; } for(i = 0; i <= last_section; i++) { if(!sects[i]) { break; } if(i == last_section) { *numsects = last_section + 1; ret = 0; done = 1; } } } while (!done); bail: close(fd); return ret;}static void FreeSects(char **sects)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -