kb_dc_section.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:10k
- //-----------------------------------------------------------------------------
- // Project include files:
- //-----------------------------------------------------------------------------
- #include "kb_dc_pub.h"
- DSMCCMessage dsmcMsg;
- DIIHead diiHead;
- ModuleInfo modInfo;
- DSIHead dsiHead;
- GroupInfo giInfo;
- DSMCCDownloadData dsmcDownloadData;
- DDBHead ddbHead;
- int getModInfoLength(unsigned char *section,int index);
- int GetGroupInfoLength(unsigned char *section,int index);
- unsigned short GetSectionLength(unsigned char *section)
- {
- unsigned short sectionLength=(section[1] & 0x0f)<<8|section[2];
- return sectionLength;
- }
- unsigned char GetSectionVersion(unsigned char *section)
- {
- unsigned char version=(section[5]& 0x3e)>>1;
- return version;
-
- }
- unsigned short GetExtTableID(unsigned char *section)
- {
- unsigned short extTableId=section[3]*256+section[4];
- return extTableId;
- }
- unsigned char GetTableID(unsigned char *section)
- {
- unsigned char tableId=section[0];
- return tableId;
- }
- unsigned char GetSectionNo(unsigned char *section)
- {
- unsigned char sectionNo=section[6];
- return sectionNo;
- }
- unsigned char GetLastSectionNo(unsigned char *section)
- {
- unsigned char lastSectionNo=section[7];
- return lastSectionNo;
- }
- int readSection(FILE *fp,unsigned char *section)
- {
- int readBytes;
- unsigned short sectionLength;
- readBytes=fread(section,1,3,fp);
- if(readBytes<0 || readBytes==0)
- return -1;
- sectionLength = GetSectionLength(section);
- readBytes=fread(section+3,1,sectionLength,fp);
- //DCDebug4("sectionLength=%dn",sectionLength);
- return sectionLength+3;
- }
- //=======================for DSMCC (DC)==============================
- int GetMsgHeaderAdapLength(unsigned char *section)
- {
- int adapLen;
- unsigned char *p=section;
- p=section+8+9;
- adapLen=*p;
- return adapLen;
- }
- int GetMsgHeaderLength(unsigned char *section)
- {
- int adapLen=GetMsgHeaderAdapLength(section);
- return 12+adapLen;
- }
- int GetDSMCCMessage(unsigned char *section,DSMCCMessage *msg)
- {
- unsigned char *p=section+8;
- msg->protocalDisminator=*p++;
- msg->dsmccType=*p++;
- msg->messageId=(p[0]<<8)|p[1];
- p+=2;
- msg->transactionId=(p[0]<<24 )|( p[1]<<16) |(p[2]<<8 )|p[3];
- p+=4;
- msg->reserved=*p++;
- msg->adaptationLength=*p++;
- msg->messageLength=(p[0]<<8)|p[1];
-
- /* for dubug
- DCDebug4("msg->protocalDisminator=%xn",msg->protocalDisminator);
- DCDebug4("msg->dsmccType=%xn",msg->dsmccType);
- DCDebug4("msg->messageId=%xn",msg->messageId);
- DCDebug4("msg->transactionId=%xn",msg->transactionId);
- DCDebug4("msg->reserved=%xn",msg->reserved);
- DCDebug4("msg->messageLength=%xn",msg->messageLength);
- */
-
- return 0;
- }
- int GetDIIHead(unsigned char *section,DIIHead *dii)
- {
- dii->downloadId=section[20]*(unsigned char)(16*16*16*16)+section[21]*(unsigned char)(16*16*16)+section[22]*256+section[23];
- dii->blockSize=section[24]*256+section[25];
- dii->windowSize=section[26];
- dii->ackperiod=section[27];
- dii->tCDownloadWindow=section[28]*(unsigned char)(16*16*16*16)+section[29]*(unsigned char)(16*16*16)+section[30]*256+section[31];
- dii->tCDownloadScenario=section[32]*(unsigned char)(16*16*16*16)+section[33]*(unsigned char)(16*16*16)+section[34]*256+section[35];
- dii->numberOfModules=section[38]*256+section[39];
-
- /*
- DCDebug3("dii->downloadId=%xn",dii->downloadId);
- DCDebug3("dii->blockSize=%xn",dii->blockSize);
- DCDebug3("dii->windowSize=%xn",dii->windowSize);
- DCDebug3("dii->ackperiod=%xn",dii->ackperiod);
- DCDebug3("dii->tCDownloadWindow=%xn",dii->tCDownloadWindow);
- DCDebug3("dii->tCDownloadScenario=%xn",dii->tCDownloadScenario);
- DCDebug3("dii->numberOfModules=%xn",dii->numberOfModules);
- DCDebug3("n");
- */
-
- return 0;
- }
- //======================for DII==========================
- int GetModuleInfo(unsigned char *section,int index,ModuleInfo *mi)
- {
- unsigned char *p;
- int i;
- p=section+8+GetMsgHeaderLength(section)+20;
-
- // DCDebug4("GetMsgHeaderLength=%dn",GetMsgHeaderLength(section));
- for(i=0;i<index;i++)
- {
- p=p+(8+p[7]);
- }
-
- mi->moduleId=(p[0]<<8)|p[1];
- //DCDebug4("mid[%d]n", mi->moduleId);
- p+=2; //(*p++)<<8|(*++p);
- //DCDebug4("[%02x][%02x][%02x][%02x]n", p[0], p[1], p[2], p[3]);
- mi->moduleSize=((unsigned int)(p[0]<<24))|((unsigned int)(p[1]<<16))|((unsigned int)(p[2]<<8))|p[3];
- p+=4; //(*p++)<<24|((*++p))<<16|(*++p)<<8|(*++p);
- mi->moduleVersion=*p++;
- mi->moduleInfoLength=*p++;
- mi->moduleInfoBytes=p;
- #if 1
- DCDebug3("n mi->moduleId=%xn",mi->moduleId);
- DCDebug3("mi->moduleSize=%xn",mi->moduleSize);
- DCDebug3("mi->moduleVersion=%xn",mi->moduleVersion);
- DCDebug3("mi->moduleInfoLength=%xn",mi->moduleInfoLength);
- DCDebug3("mi->moduleInfoBytes= %sn",mi->moduleInfoBytes+2);
- #endif
- return 8+mi->moduleInfoLength;
- }
- unsigned char*FindDescriptor(unsigned char*des,unsigned short dslen,unsigned char tag)
- {
- int i;
- unsigned char *p=des;
-
- for(i=0;i<dslen;i++)
- {
- unsigned char c=p[i];
- if(c==tag)
- {
- return p+i;
- }
- }
- return NULL;
- }
- int GetModuleName(ModuleInfo *mi,unsigned char *name)
- {
- int nameLen;
- unsigned char *modInfoByte=mi->moduleInfoBytes;
- unsigned char *nameDescriptor=FindDescriptor(modInfoByte,mi->moduleInfoLength,DSMCC_NAME_DESCRIPTOR_TAG);
- if(nameDescriptor==NULL) return 0;
- nameLen=nameDescriptor[1];
- KBDcMemcpy(name,nameDescriptor+2,nameLen);
- name[nameLen]=0;
- return nameLen;
- }
- //===========================for DSI==============================
- #if 0
- int GetGroupInfo(unsigned char*section,int index,GroupInfo *gi)
- {
- unsigned char *p;
- int i=0;
- p=section+8+GetMsgHeaderLength(section)+20+2+2;
-
- for(i=0;i<index;i++)
- {
- p=p+(12+p[10]<<8|p[11]);
- }
- gi->groupId=(*p++)<<24 | (*++p)<<16 | (*++p)<<8 |(*++p);
- gi->groupSize=(*p++)<<24 | (*++p)<<16 | (*++p)<<8 |(*++p);
- gi->GroupCompatibility=(char*)((*p++)<<8|(*++p));
- gi->GroupInfoLength=(*p++)<<8|(*++p);
- gi->GroupInfoBytes=p++;
- return 12+gi->GroupInfoLength;
- }
- #endif
- int GetDSIHead(unsigned char*section,DSIHead *dsi)
- {
-
- unsigned char cTemp[21];
- Descriptor dec;
- int i;
- KBDcMemset(cTemp,' ',21);
-
- for(i=0;i<21;i++)
- {
- cTemp[i]=section[20+i];
- }
- dsi->serverid=§ion[20];
- dec.tag=section[40];
- dec.len=section[41];
- if(dec.len==0)
- dec.descriptor=NULL;
- else
- {
- dec.descriptor=§ion[42];
- }
- dsi->compatibilityDescriptor=dec.descriptor;
- dsi->privateDataLength=section[42]<<8 | section[43];
- dsi->privateDataBytes=§ion[41+dec.len+2+1];
- DCDebug4("serverid:n");
- for(i=0;i<20;i++)
- {
- DCDebug4("%x ",*(dsi->serverid+i));
- }
- DCDebug4("n");
-
- /*printf("dsi->compatibilityDescriptor=%sn",dsi->compatibilityDescriptor);
- printf("dsi->privateDataLength=%xn",dsi->privateDataLength);
- printf("dsi->privateDataBytes:n");*/
- for(i=0;i<dsi->privateDataLength;i++)
- {
- DCDebug4("%x ",*(dsi->privateDataBytes+i));
- }
- DCDebug4("n");
- return 0;
- }
- int GetGroupCount(unsigned char*section)
- {
- int groupCount;
- DSIHead dsiHead;
- DSIHead *dsi;
- dsi=&dsiHead;
- dsi->privateDataBytes=§ion[44];
- groupCount=*(dsi->privateDataBytes)<<8 | *(dsi->privateDataBytes+1);
- DCDebug4("groupCount=%xn ",groupCount);
- return groupCount;
- }
- int getModInfoLength(unsigned char *section,int index)
- {
- int length=0;
- int sum=0;
- int i=0;
- int base=39;
- int step=8;
- if(index==0)
- length=section[base+step];
- else
- {
- for(i=1;i<=index;i++)
- {
- sum=sum+8+getModInfoLength(section,i-1);
- }
-
- length=section[base+sum+8];
- }
- return length;
- }
- int GetGroupInfoLength(unsigned char *section,int index)
- {
- int length=0;
- int sum=0;
- int i=0;
- int base=45;
- int step=11;
- if(index==0)
- length=section[base+step]<<8 |section[base+step+1];
- else
- {
- for(i=1;i<=index;i++)
- {
- sum=sum+12+GetGroupInfoLength(section,i-1);
- }
-
- length=section[base+sum+11]<<8 | section[base+sum+12];
- }
- return length;
- }
- //=========================for DDB================================
- int GetDSMCCDownloadData(unsigned char *section,DSMCCDownloadData *dsmcDownData)
- {
- unsigned short data1;
- unsigned long data2,data3;
-
- dsmcDownData->protocolDiscriminator=section[8];
- dsmcDownData->dsmccType=section[9];
- dsmcDownData->reserved=section[16];
- dsmcDownData->adaptationLength=section[17];
- data1=section[10];
- dsmcDownData->messageId=(data1<<8)|section[11];
- data3=section[12];
- data2=data3<<24;
- data3=section[13];
- data2=data2|(data3<<16);
- data3=section[14];
- data2=data2|(data3<<8);
- dsmcDownData->downloadId=data2|section[15];
- data1=section[18];
- dsmcDownData->messageLength=(data1<<8)|section[19];
- return 0;
- }
- int GetDDBHead(unsigned char *section,DDBHead *ddbHead)
- {
- unsigned char *p;
- int msgHeaderLen;
- if(section==NULL)
- {
- DCDebug1("n GetDDBHead section is NULL");
- return -1;
- }
-
- p=section;
- msgHeaderLen=GetMsgHeaderLength(section);
- p+=msgHeaderLen+8;
- ddbHead->moduleID=(p[0]<<8)|p[1];
- p+=2;
- ddbHead->moduleVersion=(*p++);
- ddbHead->reserved=(*p++);
- ddbHead->blockNumber=(p[0]<<8)|p[1];
- return 0;
- }
- unsigned int GetModuleId(unsigned char *sec)
- {
- DDBHead head;
- GetDDBHead(sec,&head);
- return head.moduleID;
- }
- //GetDDBData
- unsigned char* GetDDBData(unsigned char *section,int* pDataLen)
- {
- int msgLen;
- int blockNumber;
- int msgHeadLen;
- unsigned char *p;
- DSMCCDownloadData msg;
- p=section;
- GetDSMCCDownloadData(section,&msg);
- //GetDSMCCMessage(section,&msg);
- msgLen=msg.messageLength;
- msgHeadLen=GetMsgHeaderLength(section);
- p=section+msgHeadLen+8+4;
- blockNumber=(p[0]<<8)|p[1];
- p+=2;
- *pDataLen=msg.messageLength-6;//msgLen-6;
- /*for test
- p=section+8+msgHeadLen+6;
- if(*pDataLen!=4066)
- DCDebug4("the section length is %d,the datais %srn",*pDataLen,p);
- // for test end*/
- return p;
- }