26 #include <sys/types.h>
29 #include <libxml/encoding.h>
30 #include <libxml/xmlwriter.h>
47 #define DEFAULT_SI_CACHE_FILENAME "SICache"
49 #define XML_DECLARATION "1.0", "", ""
50 #define XML_SIDB_ELEMENT_NAME "SIDB"
51 #define XML_PGM_ELEMENT_NAME "program"
52 #define XML_SOURCE_ID_KEY "sourceid"
53 #define XML_FREQ_KEY "carrier_frequency"
54 #define XML_MODULATION_KEY "modulation_mode"
55 #define XML_PGM_NO_KEY "program_number"
56 #define XML_VIRT_CHAN_NO_KEY "virtual_channel_number"
57 #define XML_NO_OF_ATRIBUTES 5
59 SiManager::SiManager()
84 xmlNodePtr cur_node = parent;
87 while (cur_node != NULL)
90 if ( ( !xmlStrcmp ( cur_node->name, (
const xmlChar* ) XML_PGM_ELEMENT_NAME ) ) )
92 pVal = xmlGetProp(cur_node, (
const xmlChar*)XML_SOURCE_ID_KEY);
96 programDetails.source_id = atoi((
const char*)pVal);
101 printf(XML_SOURCE_ID_KEY
" pVal NULL\n");
104 pVal = xmlGetProp(cur_node, (
const xmlChar*)XML_PGM_NO_KEY);
108 programDetails.program_number = atoi((
const char*)pVal);
113 printf(XML_PGM_NO_KEY
" pVal NULL\n");
116 pVal = xmlGetProp(cur_node, (
const xmlChar*)XML_FREQ_KEY);
120 programDetails.carrier_frequency = atoi((
const char*)pVal)*HERTZ;
125 printf(XML_FREQ_KEY
" pVal NULL\n");
128 pVal = xmlGetProp(cur_node, (
const xmlChar*)XML_MODULATION_KEY);
132 programDetails.modulation_mode = atoi((
const char*)pVal);
137 printf(XML_MODULATION_KEY
" pVal NULL\n");
140 pVal = xmlGetProp(cur_node, (
const xmlChar*)XML_VIRT_CHAN_NO_KEY);
144 programDetails.virtual_channel_number = atoi((
const char*)pVal);
149 printf(XML_VIRT_CHAN_NO_KEY
" pVal NULL\n");
152 m_program_list.push_back(programDetails);
155 cur_node = cur_node->next;
158 numOfServices = m_program_list.size();
174 xmlNodePtr root_element;
177 doc = xmlParseFile ( xml_file.c_str() );
180 fprintf ( stderr,
"Document not parsed successfully. \n" );
184 root_element = xmlDocGetRootElement ( doc );
185 if ( root_element == NULL )
187 fprintf ( stderr,
"empty document\n" );
192 if ( xmlStrcmp ( root_element->name, (
const xmlChar * )XML_SIDB_ELEMENT_NAME) )
194 fprintf ( stderr,
"document of the wrong type, root node != %s", XML_SIDB_ELEMENT_NAME);
199 parent = root_element->xmlChildrenNode;
216 for (i = 0; i < 256; i++)
219 for (j = 0; j < 8; j++)
221 if (crc & 0x80000000)
222 crc = (crc << 1) ^ SI_CACHE_CRC_QUOTIENT;
252 result = *data++ << 24;
253 result |= *data++ << 16;
254 result |= *data++ << 8;
259 for (i = 0; i < len; i++)
261 result = (result << 8 | *data++) ^ crctab[result >> 24];
279 if (0 == stat(location, &buff))
281 size = (
unsigned int) buff.st_size;
298 uint32_t sizeOfSICache = 0;
299 uint32_t crcFileSize = 0;
300 uint32_t readSISize = 0;
301 uint32_t crcValue = 0xFFFFFFFF;
302 unsigned char *pCRCData = NULL;
308 readSISize = sizeOfSICache;
309 crcFileSize = sizeOfSICache;
311 pCRCData = (
unsigned char*)malloc(crcFileSize*
sizeof(
unsigned char));
312 memset(pCRCData, 0 , (crcFileSize*
sizeof(
unsigned char)));
314 fd = open( sidbFileName.c_str(), O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
317 perror(
"File open failed");
323 readSISize = read(fd, pCRCData, readSISize);
327 perror(
"SNS File Read Failed");
335 printf(
"Calculated CRC is [%u]\n", crcValue);
339 fd = open( sidbFileName.c_str(), O_RDWR | O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
342 perror(
"File open failed");
346 crcFileSize = write( fd, &crcValue,
sizeof (crcValue ));
348 if ( -1 == crcFileSize)
350 perror(
"File append failed");
370 uint32_t sizeOfSICache = 0;
371 uint32_t sizeOfSISNSCache = 0;
372 uint32_t crcFileSize = 0;
373 uint32_t readSNSSize = 0;
374 uint32_t readSISize = 0;
375 uint32_t crcValue = 0xFFFFFFFF;
376 unsigned char *pCRCData = NULL;
383 readSNSSize = sizeOfSISNSCache;
384 readSISize = sizeOfSICache;
386 crcFileSize = sizeOfSISNSCache + sizeOfSICache;
388 pCRCData = (
unsigned char*)malloc(crcFileSize*
sizeof(
unsigned char));
389 memset(pCRCData, 0 , (crcFileSize*
sizeof(
unsigned char)));
391 fd = open( snsdbFileName.c_str(), O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
394 perror(
"File open failed");
400 readSNSSize = read(fd, pCRCData, readSNSSize);
404 perror(
"SNS File Read Failed");
410 fd = open( sidbFileName.c_str(), O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
413 perror(
"File open failed");
419 readSISize = read(fd, (pCRCData+readSNSSize), readSISize);
423 perror(
"SNS File Read Failed");
431 printf(
"Calculated CRC is [%u]\n", crcValue);
435 fd = open( sidbFileName.c_str(), O_RDWR | O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
438 perror(
"File open failed");
442 crcFileSize = write( fd, &crcValue,
sizeof (crcValue ));
444 if ( -1 == crcFileSize)
446 perror(
"File append failed");
468 si_entry->ref_count = 1;
470 si_entry->ptime_service = 0LL;
472 si_entry->
next = NULL;
475 si_entry->app_id = 0;
480 si_entry->major_channel_number = SI_DEFAULT_CHANNEL_NUMBER;
486 si_entry->state = SIENTRY_UNSPECIFIED;
487 si_entry->virtual_channel_number = VIRTUAL_CHANNEL_UNKNOWN;
507 list<ProgramDetails>::iterator pProgramDetails;
512 for( i=0,pProgramDetails=m_program_list.begin(); i<m_program_list.size(); i++,pProgramDetails++ )
520 si_entry->virtual_channel_number = pProgramDetails->virtual_channel_number;
522 si_entry->
source_id = pProgramDetails->source_id;
525 for (
int i = 0; i < f_index; i++)
527 if ( pProgramDetails->carrier_frequency == frequency[i])
537 frequency[ f_index ] = pProgramDetails->carrier_frequency;
542 for (
int i = 0; i <m_index; i++)
544 if ( pProgramDetails->modulation_mode == modulation[i])
554 modulation[ m_index ] = pProgramDetails->modulation_mode;
559 if (*si_entry_head == NULL)
561 *si_entry_head = si_entry;
565 for(si_walker=*si_entry_head; si_walker->
next!=NULL; si_walker = si_walker->
next);
567 si_walker->
next = si_entry;
586 uint32_t frequency[SI_CACHE_MAX_FREQUENCIES + 1];
587 uint32_t modulation[SI_CACHE_MAX_FREQUENCIES + 1];
588 int f_index = 0, m_index=0;
590 unsigned char * buf = NULL;
594 buf = (
unsigned char*) malloc(
sizeof(version)+
sizeof(frequency)+
sizeof(modulation)+ 500*
sizeof(
siTableEntry));
595 memset( frequency, 0,
sizeof(frequency));
596 memset( modulation, 0,
sizeof(modulation));
600 perror(
"cache_si_data() failed");
604 fd = open( sidbFileName.c_str(), O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
607 perror(
"File open failed");
610 version = SI_CACHE_FILE_VERSION;
612 ret = write( fd, &version,
sizeof (version ));
615 perror(
"File write failed");
619 memcpy(buf, &version,
sizeof (version ));
620 buf+=
sizeof (version );
622 ret = write( fd, frequency,
sizeof (frequency ));
625 perror(
"File write failed");
629 memcpy(buf, frequency,
sizeof (frequency ));
630 buf+=
sizeof (frequency );
632 ret = write( fd, modulation,
sizeof (modulation ));
635 perror(
"File write failed");
639 memcpy(buf, modulation,
sizeof (modulation ));
640 buf+=
sizeof (modulation );
642 si_entry = si_entry_head;
646 ret = write( fd, si_entry,
sizeof (*si_entry ));
649 perror(
"File write failed");
653 memcpy(buf, si_entry,
sizeof (*si_entry ));
654 buf+=
sizeof (*si_entry );
655 si_entry = si_entry->
next;
676 list<ProgramDetails>::iterator pProgramDetails;
678 if(m_program_list.size()==0)
680 perror(
"File open failed");
684 for( i=0,pProgramDetails=m_program_list.begin(); i<m_program_list.size()-1; i++,pProgramDetails++ )
688 sn_entry->appType = FALSE;
689 sn_entry->mapped =
false;
690 sn_entry->
id = pProgramDetails->source_id;
692 sn_entry->source_names = NULL;
694 if (sn_entry_head == NULL)
696 sn_entry_head = sn_entry;
700 sn_entry->
next = sn_entry_head;
701 sn_entry_head = sn_entry;
705 fd = open( snsdbFileName.c_str(), O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
708 perror(
"File open failed");
713 sn_entry = sn_entry_head;
717 ret = write( fd, sn_entry,
sizeof (*sn_entry ));
720 perror(
"File write failed");
725 sn_entry = sn_entry->
next;
746 int fd=0, version=0, count=0, size=0;
747 unsigned char *buf=(
unsigned char*)malloc(
sizeof(
siTableEntry)*
sizeof(
unsigned char));
750 uint32_t frequency[SI_CACHE_MAX_FREQUENCIES + 1];
751 uint32_t modulation[SI_CACHE_MAX_FREQUENCIES + 1];
761 fd = open( sidbFileName.c_str(), O_RDONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
764 perror(
"File open failed");
768 count =
sizeof(version);
769 count = read(fd, (
void *)&version, count);
771 if (count ==
sizeof(version))
773 if (version != SI_CACHE_FILE_VERSION)
775 printf(
"Invalid cache version 0x%x, expected 0x%x\n", version, SI_CACHE_FILE_VERSION);
781 printf(
"Read version 0x%x\n", version);
786 printf(
"reading Version [%s] failed count:%d version:0x%x\n", sidbFileName.c_str(), count, version);
791 count =
sizeof(frequency);
792 count = read(fd, (
void *)frequency, count);
793 if (count !=
sizeof(frequency))
795 printf(
"reading FREQ from [%s] failed\n", sidbFileName.c_str());
800 count =
sizeof(modulation);
801 count = read(fd, (
void *)modulation, count);
802 if (count !=
sizeof(modulation))
804 printf(
"reading MODULATION from [%s] failed\n", sidbFileName.c_str());
813 count = read(fd, (
void*)buf, size);
816 printf(
"Reached end of file [%s]\n", sidbFileName.c_str());
820 else if((count < size) && (count ==
sizeof(crcValue)))
823 crcValue = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | (buf[0] & 0xFF);
824 printf(
"CRC Value: [%u]\n", crcValue);
828 else if((count < 0) || (count < size))
830 printf(
"Error in reading file [%s] \n", sidbFileName.c_str());
837 programDetails.source_id = si_entry->
source_id;
839 programDetails.carrier_frequency = frequency[si_entry->
freq_index]/HERTZ;
840 programDetails.modulation_mode = modulation[si_entry->
mode_index];
841 programDetails.virtual_channel_number = si_entry->virtual_channel_number;
843 m_program_list.push_back(programDetails);
850 for(
int i=0 ; i< name_count; i++)
852 unsigned char name_len_buf[4] = {0};
853 int buf_count =
sizeof(name_len_buf);
855 count = read(fd, (
void *)name_len_buf, buf_count);
857 int name_len = (name_len_buf[0]) | (name_len_buf[1] << 8) | (name_len_buf[2] << 16) | (name_len_buf[3] << 24);
858 lseek(fd, name_len, SEEK_CUR);
863 numOfServices = m_program_list.size();
890 case SI_FILETYPE_XML:
892 printf(
"Loading program data from XML file [%s]\n", file_name.c_str());
895 printf(
"No:of Services Available: %d\n", numOfServices);
898 case SI_FILETYPE_BINARY:
900 printf(
"Loading program data from binary cache file [%s]\n", file_name.c_str());
903 printf(
"No:of Services Available: %d\n", numOfServices);
910 cout<<
"Mode not implemented"<<endl;
921 list<ProgramDetails>::iterator p;
924 for( i=0,p=m_program_list.begin(); i<m_program_list.size(); i++,p++ )
926 printf(
"RChannelVCN[%06d]-SRCID[%06x]-Freq[%09d]-Mode[%04d]-Prog[%08d]\n",p->virtual_channel_number, p->source_id, p->carrier_frequency*HERTZ,p->modulation_mode,p->program_number);
943 perror(
"Failed to cache si data");
949 perror(
"Failed to append CRC");
953 printf(
"Binary file [%s] generated successfully\n", sidbFileName.c_str());
967 printf(
"Generating SI Cache [%s] from extracted program details\n", sidbFileName.c_str());
970 perror(
"Failed to cache si data");
974 printf(
"Generating SNS Cache [%s] from extracted program details\n", snsdbFileName.c_str());
977 perror(
"Failed to cache si data");
983 perror(
"Failed to append CRC");
987 printf(
"Binary files [%s] and [%s] generated successfully\n", sidbFileName.c_str(), snsdbFileName.c_str());
1007 xmlTextWriterPtr writer;
1008 list<ProgramDetails>::iterator p;
1011 printf(
"Generating XML file [%s] from extracted program data\n", sixmlFileName.c_str());
1012 writer = xmlNewTextWriterFilename(sixmlFileName.c_str(), 0);
1013 xmlTextWriterStartDocument(writer, NULL,
"UTF-8", NULL);
1015 xmlTextWriterStartElement(writer, BAD_CAST XML_SIDB_ELEMENT_NAME);
1016 xmlTextWriterSetIndent(writer, 1);
1017 xmlTextWriterSetIndentString(writer, BAD_CAST
"");
1019 for( i=0,p=m_program_list.begin(); i<m_program_list.size(); p++, i++ )
1021 xmlTextWriterStartElement(writer, BAD_CAST XML_PGM_ELEMENT_NAME);
1022 xmlTextWriterSetIndentString(writer, BAD_CAST
"");
1024 xmlTextWriterWriteFormatAttribute(writer, BAD_CAST XML_SOURCE_ID_KEY,
"%d", p->source_id);
1025 xmlTextWriterWriteFormatAttribute(writer, BAD_CAST XML_PGM_NO_KEY,
"%d", p->program_number);
1026 xmlTextWriterWriteFormatAttribute(writer, BAD_CAST XML_FREQ_KEY,
"%d", p->carrier_frequency);
1027 xmlTextWriterWriteFormatAttribute(writer, BAD_CAST XML_MODULATION_KEY,
"%d", p->modulation_mode);
1028 xmlTextWriterWriteFormatAttribute(writer, BAD_CAST XML_VIRT_CHAN_NO_KEY,
"%d",p->virtual_channel_number);
1030 xmlTextWriterEndElement(writer);
1032 xmlTextWriterEndElement(writer);
1033 xmlTextWriterEndDocument(writer);
1034 xmlFreeTextWriter(writer);
1035 printf(
"XML file [%s] generated successfully\n", sixmlFileName.c_str());