epg.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:27k
- #include <string.h>
- #include "epg.h"
- #include "sipsi.h"
- #include "db.h"
- #include "appltype.h"
- #include "dmd.h"
- #include "timeclk.h"
- #include "db.h"
- #include "kb_machblue_client_task.h"
- #include "Machblue_defines.h"
- #include "Avplay.h"
- #include "Nvod.h"
- extern KB_DMDTunerParameters CurrentSignal;
- static UINT32 gEPGTaskID;
- static UINT32 gEpgQueID;
- static KB_EPGSchInfo gEpgSchInfo[KB_EPG_MAX_TS_NUM];
- static KB_SIEitSch gEpgSCH[KB_EPG_MAX_TS_NUM][KB_EPG_MAX_SCH];
- static KB_EpgPFInfo gEpgPFInfo;
- static KB_SIEitPF *gpEpgPF;
- static INT32 gEpgToNvod;
- static KB_SIEitSchEvent *gEpgSCHEvent;
- static time_t epgTime;
- static KB_EPGCallBackFunc gpEpgCallback;
- static void EpgMsgDisPatch(KB_OSPMsgNode *pInMsg);
- static INT32 EpgGetFreeSchNum(UINT32 TsNum);
- static INT32 KD_EPGGetSchInfoNumByServiceId(UINT32 TsNum, UINT32 nSvcID);
- static void EpgHandlerEvent(KB_OSPMsgNode *pInMsg);
- static INT32 EpgResetPF(void);
- static INT32 EpgResetSCH(UINT32 nTsNum, INT32 nSrvNum);
- static void EpgResetUnusedSCH(UINT32 TsNum,
- INT32 nSvcCnt,
- UINT32 *pSvcID);
- static void EpgHandleSI(KB_OSPMsgNode *pInMsg);
- static INT32 EpgSortSCH(UINT32 TsNum,
- KB_SIEitSch *pSCHInfo,
- INT32 nEvtNum);
- static void KD_EpgTask(void);
- static void QuickSort(KB_SIEitSchEvent *pList, int nLow, int nHigh);
- static int Partition(KB_SIEitSchEvent *pList, int nLow, int nHigh);
- static UINT8 KB_EPG_GetFreeEpgSchInfo(void);
- extern mb_error_t KB_mb_PF_Update(KB_SIEitPF *Cur_ptEitPf,KB_SIEitPF *Fol_ptEitPf);
- extern void KB_MENUTranslateMsg(KB_OSPMsgNode* pInMsg);
- INT32 KB_EPGGetEITPF(UINT32 nSvcID)
- {
- //KB_OSPMsgNode msgNode;
-
- EpgResetPF();
- KB_SIGetEitPF(nSvcID , KB_SI_EIT_CURRENT_TS,
- gEpgPFInfo.pPresent,
- gEpgPFInfo.pFollow,
- KB_SI_RECEIVE_ONCE,
- -1);
- gEpgPFInfo.nEnable = FALSE;
- gEpgPFInfo.nSvcID = nSvcID;
- gEpgPFInfo.nState = EPG_Receiving;
- return RETOK;
- }
- INT32 KB_EPGGetSch(UINT32 nTsId, INT32 nSvcCnt, UINT32 *pSvcID)
- {
- UINT32 i, nNumber, CurrentTsNum;
- KB_OSPMsgNode msgNode;
-
- if (nSvcCnt < 0 || nSvcCnt > KB_EPG_MAX_SCH
- || pSvcID == NULL)
- {
- return RETFAIL3;
- }
- CurrentTsNum = KB_EPG_GetCurrentTsNum();
- if (CurrentTsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nKB_EPGGetSch::Serious Error.CurrentTs is NULLn");
- return RETFAIL3;
- }
-
- if (nTsId != gEpgSchInfo[CurrentTsNum].nTsId)
- {
- printf("nKB_EPGGetSch::Serious Error.Ts(%d) is not CurrentTsn", nTsId);
- return RETFAIL3;
- }
- else
- {
- //printf("nKB_EPGGetSch::CurrentTs(%d) search! SrvNum(%d)n",
- // nTsId, nSvcCnt);
- EpgResetUnusedSCH(CurrentTsNum, nSvcCnt, pSvcID);
- }
-
- for (i = 0; i < nSvcCnt; i++)
- {
- nNumber = KD_EPGGetSchInfoNumByServiceId(CurrentTsNum, *(pSvcID + i));
- if (nNumber != 0xFFFFFFFF)
- {
- switch (gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nState)
- {
- case EPG_Empty:
- case EPG_Timeout:
- KB_SIGetEitSCH(*(pSvcID + i),
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].pDataStruct);
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nEnable = FALSE;
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nState = EPG_Receiving;
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nSvcID = *(pSvcID + i);
- break;
- case EPG_Recieved:
- msgNode.Word1 = J_EPG_MODULE;
- msgNode.Word2 = EPG_MSG_SCHEDULE_DONE;
- msgNode.Word3 = (UINT32)(gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].pDataStruct);
- msgNode.Word4 = *(pSvcID + i);
- if (gpEpgCallback)
- {
- gpEpgCallback(&msgNode);
- }
- break;
- case EPG_Receiving:
- KB_SIStopEitSCH(*(pSvcID + i));
- KB_SIGetEitSCH(*(pSvcID + i),
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].pDataStruct);
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nEnable = FALSE;
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nState = EPG_Receiving;
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nSvcID = *(pSvcID + i);
- break;
-
- default:
- break;
- }
- }
- else
- {
- nNumber = (UINT32)EpgGetFreeSchNum(CurrentTsNum);
- if (nNumber != 0xFFFFFFFF)
- {
- //printf("nKB_EPGGetSch::start to search ShiftSrv(%d)n",
- //*(pSvcID + i));
- KB_SIGetEitSCH(*(pSvcID + i),
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].pDataStruct);
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nEnable = FALSE;
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nState = EPG_Receiving;
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nSvcID = *(pSvcID + i);
- }
- }
- }
- return RETOK;
- }
- KB_EPGCallBackFunc KB_EpgCallBack(KB_EPGCallBackFunc callback)
- {
- KB_EPGCallBackFunc ptPreCallback;
- ptPreCallback = gpEpgCallback;
- gpEpgCallback = callback;
- return ptPreCallback;
- }
- INT32 KB_EPGStopAllEITPF(void)
- {
- KB_SIStopEitPF(KB_SI_STOP_ALL, KB_SI_EIT_CURRENT_TS);
-
- EpgResetPF();
- return RETOK;
- }
- INT32 KB_EPGStopAllSCH()
- {
- INT32 i, j;
- KB_SIStopEitSCH(KB_SI_STOP_ALL);
-
- //gEpgSchInfo.nTsId = 0xFFFFFFFF;
-
- for (i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- for (j = 0; j < KB_EPG_MAX_SCH; j++)
- {
- EpgResetSCH(i, j);
- }
- }
- return RETOK;
- }
- #if 0
- INT32 KB_EPGStopOneTsAllSCH(UINT32 TsNum)
- {
- INT32 i;
- KB_SIStopEitSCH(KB_SI_STOP_ALL);
-
- //gEpgSchInfo.nTsId = 0xFFFFFFFF;
-
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- EpgResetSCH(TsNum, i);
- }
-
- return RETOK;
- }
- #endif
- INT32 KD_EPGStopPF(UINT32 nSvcID)
- {
- KB_SIStopEitPF(nSvcID, KB_SI_EIT_CURRENT_TS);
- EpgResetPF();
- return RETOK;
- }
- INT32 KB_EPGStopOneSrvSCH(UINT32 TsNum, UINT32 nSvcID)
- {
- int i;
-
- if (TsNum >= KB_EPG_MAX_TS_NUM)
- {
- return RETFIAL1;
- }
-
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- if (gEpgSchInfo[TsNum].nSchInfo[i].nEnable == FALSE
- && gEpgSchInfo[TsNum].nSchInfo[i].nSvcID == nSvcID)
- {
- KB_SIStopEitSCH(nSvcID);
- EpgResetSCH(TsNum, i);
- return RETOK;
- }
- }
- return RETFIAL1;
- }
- INT32 KD_EPGTaskInit(void)
- {
- int i, j;
- unsigned short PrgNO;
- unsigned char PrgType;
- KB_DBPrgInfo PrgInfo;
- KB_DBChnnlInfo ChnnlInfo;
- UINT8 Track;
- UINT8 Volume;
- UINT8 LockFlag;
-
- KB_OSPQueInit("EPG", 128, &gEpgQueID);
- KB_OSPTaskInit("EPG",1024 * 32,(void (*)(void*))KD_EpgTask,EPG_PRIORITY,NULL,&gEPGTaskID);
- KB_SIRegisterEitCallBack(EpgHandleSI);
-
- gpEpgPF = NULL;
- gEpgSCHEvent = NULL;
- gpEpgCallback = NULL;
- gEpgToNvod = FALSE;
- memset(gEpgSchInfo, 0, (sizeof(KB_EPGSchInfo)*KB_EPG_MAX_TS_NUM));
-
- gpEpgPF = KB_OSPMalloc(sizeof(KB_SIEitPF) * 2);
- if (gpEpgPF == NULL)
- {
- return RETFAIL2;
- }
- else
- {
- gEpgPFInfo.nTsId = 0xFFFFFFFF;
- gEpgPFInfo.nEnable = TRUE;
- gEpgPFInfo.nState = EPG_Empty;
- gEpgPFInfo.nSvcID = 0xFFFFFFFF;
- gEpgPFInfo.pPresent = gpEpgPF;
- gEpgPFInfo.pFollow = gpEpgPF+ 1;
- }
-
- for (i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- for (j = 0; j < KB_EPG_MAX_SCH; j++)
- {
- gEpgSCH[i][j].service_id = 0xffff;
- gEpgSCH[i][j].num = 0xffff;
- gEpgSCH[i][j].ptEvent = NULL;
- }
- }
-
-
- gEpgSCHEvent = KB_OSPMalloc(sizeof(KB_SIEitSchEvent) * KB_EPG_MAX_TS_NUM * KB_EPG_MAX_SCH * KB_SI_MAX_EVENT_NUM_PER_SERVICE);
- if (gEpgSCHEvent == NULL)
- {
- return RETFAIL2;
- }
- else
- {
- for (i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- gEpgSchInfo[i].nTsId = 0xFFFFFFFF;
- gEpgSchInfo[i].nCurrentTsTag = FALSE;
-
- for (j = 0; j < KB_EPG_MAX_SCH; j++)
- {
- gEpgSchInfo[i].nSchInfo[j].nEnable = TRUE;
- gEpgSchInfo[i].nSchInfo[j].nState = EPG_Empty;
- gEpgSchInfo[i].nSchInfo[j].nSvcID = 0xFFFFFFFF;
- gEpgSchInfo[i].nSchInfo[j].pDataStruct = &gEpgSCH[i][j];
- gEpgSchInfo[i].nSchInfo[j].pDataStruct->ptEvent = gEpgSCHEvent
- + i*KB_EPG_MAX_SCH*KB_SI_MAX_EVENT_NUM_PER_SERVICE
- + j*KB_SI_MAX_EVENT_NUM_PER_SERVICE;
- }
- }
-
- KB_DBGetCurPrgInfo(&PrgNO, &PrgType);
- KB_DBGetPrgInfo(PrgNO,
- PrgType,
- &PrgInfo,
- &ChnnlInfo,
- &Track,
- &Volume,
- &LockFlag);
- KB_EPG_SetCurrentTsNum(ChnnlInfo.TSID);
- printf("nKD_EPGTaskInit::sizeof(gEpgSchInfo)(%d), sizeof(gEpgSCH)(%d), sizeof(gEpgSCHEvent)(%d)",
- sizeof(gEpgSchInfo), sizeof(gEpgSCH), (sizeof(KB_SIEitSchEvent) * KB_EPG_MAX_TS_NUM * KB_EPG_MAX_SCH * KB_SI_MAX_EVENT_NUM_PER_SERVICE));
- return RETOK;
- }
- }
- static INT32 EpgGetFreeSchNum(UINT32 TsNum)
- {
- INT32 i, nNumber;
- if (TsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nEpgGetFreeSchNum::serious error! input TsNum(%d) is too large.",
- TsNum);
- return 0xFFFFFFFF;
- }
- nNumber = 0xFFFFFFFF;
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- if (gEpgSchInfo[TsNum].nSchInfo[i].nEnable == TRUE)
- {
- nNumber = i;
- break;
- }
- }
- if (0xFFFFFFFF == nNumber)
- {
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- EpgResetSCH(TsNum, i);
- }
- nNumber = 0;
- }
-
- return nNumber;
- }
- KB_EPGDayInfo *KD_EPGGetSCH(UINT32 TsNum, INT32 nSvcID, INT32 nWeekday)
- {
- int i;
-
- if (TsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nKD_EPGGetSCH::serious error! input TsNum(%d) is too large.",
- TsNum);
- return NULL;
- }
-
- for(i=0; i<KB_EPG_MAX_SCH; i++)
- {
- if(gEpgSchInfo[TsNum].nSchInfo[i].nSvcID == nSvcID)
- {
- return &gEpgSchInfo[TsNum].nSchInfo[i].nDayInfo[nWeekday];
- }
- }
- return NULL;
- }
- static INT32 KD_EPGGetSchInfoNumByServiceId(UINT32 TsNum, UINT32 nSvcID)
- {
- INT32 i, nNumber;
- nNumber = 0xFFFFFFFF;
- if (TsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nKD_EPGGetSchInfoNumByServiceId::serious error! input TsNum(%d) is too large.",
- TsNum);
- return 0xFFFFFFFF;
- }
-
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- if (gEpgSchInfo[TsNum].nSchInfo[i].nSvcID == nSvcID)
- {
- nNumber = i;
- break;
- }
- }
- return nNumber;
- }
- static void EpgHandlerEvent(KB_OSPMsgNode *pInMsg)
- {
- KB_OSPMsgNode msgNode;
- KB_SIEitPF *ptEitPf;
- KB_SIEitSch *ptEitSch = NULL;
- INT32 nNumber;
- UINT32 CurrentTsNum;
- CurrentTsNum = KB_EPG_GetCurrentTsNum();
- if ((UINT32)(-1) == CurrentTsNum)
- {
- printf("nEpgHandlerEvent::CurrentTsNum ERROR!n");
- return;
- }
-
- switch (pInMsg->Word1)
- {
- case MOD_SIPSI:
-
- msgNode.Word1 = J_EPG_MODULE;
- if (pInMsg->Word2 >= KB_SI_MSG_EIT_PF_DONE
- && pInMsg->Word2 <= KB_SI_MSG_EIT_SCHEDULE_TIMEOUT)
- {
- switch (pInMsg->Word2)
- {
- case KB_SI_MSG_EIT_PF_DONE:
- ptEitPf = (KB_SIEitPF*)pInMsg->Word3;
- if (ptEitPf != NULL)
- {
- gEpgPFInfo.nState = EPG_Recieved;
- }
- ptEitPf = (KB_SIEitPF*)pInMsg->Word4;
- KB_mb_PF_Update((KB_SIEitPF*)pInMsg->Word3,(KB_SIEitPF*)pInMsg->Word4);
- break;
- case KB_SI_MSG_EIT_PF_TIMEOUT:
- ptEitPf = (KB_SIEitPF*)pInMsg->Word3;
- if (ptEitPf != NULL)
- {
- gEpgPFInfo.nState = EPG_Timeout;
- }
- break;
-
- case KB_SI_MSG_EIT_SCHEDULE_EVENT_DONE:
- //EpgSortSCH((KB_SIEitSch *)pInMsg->Word3, pInMsg->Word4);
- break;
-
- case KB_SI_MSG_EIT_SCHEDULE_DONE:
- ptEitSch = (KB_SIEitSch*)pInMsg->Word3;
- EpgSortSCH(CurrentTsNum, ptEitSch, ptEitSch->num);
- //printf("nEpgHandlerEvent::epg_task get KB_SI_MSG_EIT_SCHEDULE_DONE, SrvId(%d), num(%d)",
- // pInMsg->Word4, ptEitSch->num);
- nNumber = KD_EPGGetSchInfoNumByServiceId(KB_EPG_GetCurrentTsNum(),
- pInMsg->Word4);
- if (nNumber != 0xFFFFFFFF)
- {
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nState = EPG_Recieved;
- }
- {
- KB_OSPMsgNode movie_msg;
- movie_msg.Word1=KB_MOVIE_SCHEDULE_CREATE;
- movie_msg.Word2=pInMsg->Word4;
- kb_machblue_task_post(&movie_msg);
- }
- break;
- case KB_SI_MSG_EIT_SCHEDULE_TIMEOUT:
- nNumber = KD_EPGGetSchInfoNumByServiceId(KB_EPG_GetCurrentTsNum(),
- pInMsg->Word4);
- if (nNumber != 0xFFFFFFFF)
- {
- gEpgSchInfo[CurrentTsNum].nSchInfo[nNumber].nState = EPG_Timeout;
- }
- break;
-
- default:
- break;
- }
- if (pInMsg->Word2 != KB_SI_MSG_EIT_SCHEDULE_EVENT_DONE)
- {
- msgNode.Word2 = pInMsg->Word2 - KB_SI_MSG_EIT_PF_DONE;
- msgNode.Word3 = pInMsg->Word3;
- msgNode.Word4 = pInMsg->Word4;
- //EpgMsgDisPatch
- if (gpEpgCallback)
- {
- if (EPG_MSG_SCHEDULE_DONE == msgNode.Word2)
- {
- //printf("nEpgHandlerEvent::now send EPG_MSG_SCHEDULE_DONE to osd, SrvId(%d)",
- // pInMsg->Word4);
- }
- gpEpgCallback(&msgNode);
- }
- }
- }
-
- break;
- default:
- break;
- }
- }
- static INT32 EpgResetPF()
- {
- gEpgPFInfo.nTsId = 0xFFFFFFFF;
- gEpgPFInfo.nEnable = TRUE;
- gEpgPFInfo.nState = EPG_Empty;
- gEpgPFInfo.nSvcID = 0xFFFFFFFF;
- memset(gEpgPFInfo.pPresent, 0, sizeof(KB_SIEitPF));
- memset(gEpgPFInfo.pFollow, 0, sizeof(KB_SIEitPF));
-
- return RETOK;
- }
- static INT32 EpgResetSCH(UINT32 nTsNum, INT32 nSrvNum)
- {
- UINT16 loop = 0;
- if (nTsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nEpgResetSCH::serious error! nTsNum(%d) is too large!", nTsNum);
- return RETFIAL1;
- }
- gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].nEnable = TRUE;
- gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].nState = EPG_Empty;
- gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].nSvcID = 0xFFFFFFFF;
- if (gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->num > KB_SI_MAX_EVENT_NUM_PER_SERVICE)
- {
- gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->num = KB_SI_MAX_EVENT_NUM_PER_SERVICE;
- }
-
- for (loop = 0; loop < gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->num; loop++)
- {
- if (NULL != (gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->ptEvent + loop)->chinese.text)
- {
- KB_OSPFree((gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->ptEvent + loop)->chinese.text);
- (gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->ptEvent + loop)->chinese.text = NULL;
- }
- }
-
- gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->num = 0;
- gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].pDataStruct->service_id = 65535;
- memset(gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].nDayInfo,
- 0,
- sizeof(gEpgSchInfo[nTsNum].nSchInfo[nSrvNum].nDayInfo));
- return RETOK;
- }
- static void EpgResetUnusedSCH(UINT32 TsNum,
- INT32 nSvcCnt,
- UINT32 *pSvcID)
- {
- int i, j;
- if (TsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nEpgResetSCH::serious error! nTsNum(%d) is too large!", TsNum);
- return;
- }
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- for (j = 0; j < nSvcCnt; j++)
- {
- if (gEpgSchInfo[TsNum].nSchInfo[i].nSvcID == *(pSvcID + j))
- {
- break;
- }
- }
- if (j == nSvcCnt && gEpgSchInfo[TsNum].nSchInfo[i].nEnable == FALSE)
- {
- KB_SIStopEitSCH(gEpgSchInfo[TsNum].nSchInfo[i].nSvcID);
- EpgResetSCH(TsNum, i);
- }
- }
- }
- static void EpgHandleSI(KB_OSPMsgNode *pInMsg)
- {
- KB_OSPMsgSend(gEpgQueID, pInMsg);
- }
- static INT32 EpgSortSCH(UINT32 TsNum,
- KB_SIEitSch *pSCHInfo,
- INT32 nEvtNum)
- {
- //KB_OSPMsgNode msgNode;
- KB_SIEitSchEvent *pList;
- int i;
- struct tm tmNow;
- time_t now;
- int count = 0,weekday = -1,index;
- //KB_SIEitSchEvent *ptFirstList;
- //struct tm tmFirst;
- //printf("-=service_id=%x, nEvtNum=%d=-n", pSCHInfo->service_id, nEvtNum);
- if(nEvtNum == 0)
- {
- printf("nEpgSortSCH::nEvtNum is NULL!");
- return RETFAIL3;
- }
- if (TsNum >= KB_EPG_MAX_TS_NUM)
- {
- printf("nEpgSortSCH::serious error.TsNum(%d) is too large", TsNum);
- }
-
- pList = pSCHInfo->ptEvent;
- QuickSort(pList, 0, (int)nEvtNum-1);
-
- for(i=0;i<KB_EPG_MAX_SCH;i++)
- {
- if(gEpgSchInfo[TsNum].nSchInfo[i].nSvcID == (UINT32)pSCHInfo->service_id)
- {
- index = i;
- break;
- }
- }
- if(i == KB_EPG_MAX_SCH)
- {
- printf("nEpgSortSCH::serious error. SrvId proved to be not exist!");
- return RETFIAL1;
- }
- for(i=0;i<nEvtNum;i++)
- {
- now = KB_TimeGMTTime(pList[i].start_time);
- tmNow = *localtime(&now);
- if(tmNow.tm_wday != weekday)
- {
- gEpgSchInfo[TsNum].nSchInfo[index].nDayInfo[tmNow.tm_wday].ptEvent = &pList[i];
- if(weekday != -1)
- {
- gEpgSchInfo[TsNum].nSchInfo[index].nDayInfo[weekday].nEventCount = count;
- count = 0;
- }
- weekday = tmNow.tm_wday;
- }
- count++;
- }
- gEpgSchInfo[TsNum].nSchInfo[index].nDayInfo[weekday].nEventCount = count;
- /* msgNode.Word1 = J_EPG_MODULE;
- msgNode.Word2 = EPG_MSG_SCHEDULE_EVENT_DONE;
- msgNode.Word3 = count;//0;
- msgNode.Word4 = gEpgSchInfo.nSchInfo[index].nSvcID;
- if (gpEpgCallback)
- {
- gpEpgCallback(&msgNode);
- }
- */
- return RETOK;
- }
- static void QuickSort(KB_SIEitSchEvent *pList, int nLow, int nHigh)
- {
- int nSort;
- if (nLow < nHigh)
- {
- nSort = Partition(pList, nLow, nHigh);
- QuickSort(pList, nLow, nSort-1);
- QuickSort(pList, nSort+1, nHigh);
- }
- }
- static int Partition(KB_SIEitSchEvent *pList, int nLow, int nHigh)
- {
- KB_SIEitSchEvent nSort;
- UINT8 text[KB_SI_MAX_EVENT_TEXT_LEN + 1];
- time_t PivotKey,TempKey;
- int low,high;
- nSort.chinese.text = text;
-
- low = nLow;
- high = nHigh;
- nSort = pList[nLow];
- PivotKey = nSort.start_time;
-
- while (low < high)
- {
- TempKey = pList[high].start_time;
- while (low<high && TempKey >=PivotKey)
- {
- --high;
- TempKey = pList[high].start_time;
- }
- pList[low] = pList[high];
- TempKey = pList[low].start_time;
- while (low<high && TempKey<=PivotKey)
- {
- ++low;
- TempKey = pList[low].start_time;
- }
- pList[high] = pList[low];
- }
-
- pList[low] = nSort;
- return low;
- }
- INT32 KB_EPGToNvod(INT32 nMute)
- {
- if (nMute == TRUE || nMute == FALSE)
- {
- gEpgToNvod = nMute;
- }
- return RETOK;
- }
- void* KB_EPGGetMemAddress(void)
- {
- return gEpgSCHEvent;
- }
- INT32 KB_EPGInit(void)
- {
- INT32 nReturn;
-
- nReturn = KD_EPGTaskInit();
- return nReturn;
- }
- INT32 KB_EPGCreate(void)
- {
- KB_SIGetTime(&epgTime);
- KB_EpgCallBack(EpgMsgDisPatch);
- return RETOK;
- }
- INT32 KB_EPGDestroy(void)
- {
- return RETOK;
- }
- static void EpgMsgDisPatch(KB_OSPMsgNode *pInMsg)
- {
- KB_MENUTranslateMsg(pInMsg);
- }
- static void KD_EpgTask(void)
- {
- int i, nReturn;
- KB_OSPMsgNode msgNode;
- i = 1;
- while (i)
- {
- nReturn = KB_OSPMsgGet(gEpgQueID, KB_Wait, 0, &msgNode);
- if (nReturn != Ret_OK)
- {
- continue;
- }
- if (gEpgToNvod)
- {
- KD_NVODHandleMsg2(&msgNode);
- }
- else
- {
- EpgHandlerEvent(&msgNode);
- }
- }
- }
- KB_EPGDayInfo *KB_EPGGetSchInfo(UINT16 TsId, INT32 nSvcID, INT32 nWeekday)
- {
- UINT32 TsNum;
-
- TsNum = KB_EPG_GetTsNumFromTsId(TsId);
-
- if ((UINT32)(-1) == TsNum)
- {
- return NULL;
- }
- else
- {
- return KD_EPGGetSCH(TsNum, nSvcID, nWeekday);
- }
- }
- INT32 KB_EPGHandleMsg(KB_OSPMsgNode *pInMsg)
- {
- switch (pInMsg->Word1)
- {
- case MOD_SIPSI:
- {
- switch(pInMsg->Word2)
- {
- case KB_SI_MSG_TDT_DONE:
- {
- KB_TimeSetCurTime(*((time_t*)pInMsg->Word3));
- }
- break;
- }
- }
- KB_DTVHandleMsg(pInMsg);
- break;
- case MOD_TUNER:
- KB_DTVAVHandleMsg(pInMsg);
- break;
-
- default:
- break;
- }
- return RETOK;
- }
- UINT32 KB_EPGGetPrgSCH(unsigned short nPrgType,unsigned short i_nChannel)
- {
- INT32 nSvcID[KB_EPG_MAX_SCH] = {0};
- INT32 nSvcID2;
- UINT16 nTsID;
- UINT32 nFreq;
- UINT32 nSymbol;
- UINT8 qamtype;
- if (0 == KB_DBGetPrgTotal((UINT8)nPrgType))
- {
- printf("nKB_EPGGetPrgSCH::KB_DBGetPrgTotal return 0. No Services, no search!");
- return -1;
- }
-
- nSvcID[0] = KB_DBGetSvcIDByNO(nPrgType, i_nChannel - 1);
- if (nSvcID[0] != KB_DB_INVALID_PROGRAMNO)
- {
- KB_DBGetChnlInfoByPrgNO(nPrgType, i_nChannel, &nFreq, &nSymbol, &qamtype);
- KB_DBGetTsIDByFre(&nTsID, nFreq / 100000);
- nSvcID2 = KB_DBGetSvc2_TSID(nTsID, nPrgType,
- &nSvcID[1], KB_EPG_MAX_SCH - 1,
- &nSvcID[0], 1);
- KB_EPGGetSch((UINT32)nTsID, nSvcID2 + 1, (UINT32*)nSvcID);
- }
- return nSvcID[0];
- }
- UINT32 KB_EPG_GetCurrentTsNum(void)
- {
- int i;
- for(i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- if (TRUE == gEpgSchInfo[i].nCurrentTsTag)
- {
- return i;
- }
- }
- return (UINT32)(-1);
- }
- #if 0
- void KB_EPG_CurrentInfo(void)
- {
- int i, j;
- for (i = 0; i < 7; i++)
- {
- printf("nn----------Srv(%d), totalNum(%d)------------", i, gEpgSchInfo[KB_EPG_GetCurrentTsNum()].nSchInfo[i].pDataStruct->num);
- for (j = 0; j < 7; j++)
- {
- printf("n-----day(%d), EventNum(%d)", j, gEpgSchInfo[KB_EPG_GetCurrentTsNum()].nSchInfo[i].nDayInfo[j].nEventCount);
- }
- }
- return;
- }
- #endif
- UINT32 KB_EPG_GetTsNumFromTsId(UINT16 TsId)
- {
- int i;
- for(i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- if (TsId == (UINT16)gEpgSchInfo[i].nTsId)
- {
- return i;
- }
- }
- return (UINT32)(-1);
- }
- UINT32 KB_EPG_GetTsIdFromTsNum(UINT32 TsNum)
- {
- if (TsNum < KB_EPG_MAX_TS_NUM)
- {
- return gEpgSchInfo[TsNum].nTsId;
- }
- else
- {
- printf("nKB_EPG_GetTsIdFromTsNum::input error TsNum(%d)", TsNum);
- return 0xFFFFFFFF;
- }
- }
- //当其他模块切换ts流的时候,要记得通知EPG设置当前流标志
- INT32 KB_EPG_SetCurrentTsNum(UINT32 TsId)
- {
- UINT8 i;
- UINT32 preTsNum, NowTsNum;
- preTsNum = KB_EPG_GetCurrentTsNum();
-
- //将原当前流标志修改;如果是第一次搜索,当前流还没有设置
- if ((UINT32)(-1) != preTsNum)
- {
- gEpgSchInfo[preTsNum].nCurrentTsTag = FALSE;
- }
- for (i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- if (gEpgSchInfo[i].nTsId == TsId)
- {
- //该ts流已经申请了gEpgSchInfo,修改当前流标志后return
- gEpgSchInfo[i].nCurrentTsTag = TRUE;
- return RET_OK;
- }
- }
- //该ts流未申请gEpgSchInfo
- NowTsNum = KB_EPG_GetFreeEpgSchInfo();
-
- if (0xFF == NowTsNum)
- {
- printf("nKB_EPG_SetCurrentTsNum::KB_EPG_GetFreeEpgSchInfo fail!n");
- return RET_FAIL;
- }
- else
- {
- gEpgSchInfo[NowTsNum].nTsId = TsId;
- gEpgSchInfo[NowTsNum].nCurrentTsTag = TRUE;
- return RET_OK;
- }
-
- }
- static UINT8 KB_EPG_GetFreeEpgSchInfo(void)
- {
- UINT8 i, ret = 0xFF;
-
- for (i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- if (0xFFFFFFFF == gEpgSchInfo[i].nTsId)
- {
- ret = i;
- break;
- }
- }
- return ret;
- }
- void KB_EPG_ResetSchInfoByTsid(UINT16 TsId)
- {
- UINT8 i = 0;
- UINT8 j = 0;
- UINT32 TsIndex = 0xffffffff;
- //对全体复位的预先处理
- if (0xffff == TsId)
- {
- for (i = 0; i < KB_EPG_MAX_TS_NUM; i++)
- {
- for (j = 0; j < KB_EPG_MAX_SCH; j++)
- {
- EpgResetSCH(i, j);
- }
- }
- return;
- }
- TsIndex = KB_EPG_GetTsNumFromTsId(TsId);
- if (TsIndex >= KB_EPG_MAX_TS_NUM)
- {
- printf("nKB_EPG_ResetSchInfoByTsid::input tsid(%d) donot register in gEpgSchInfo");
- return;
- }
- for (i = 0; i < KB_EPG_MAX_SCH; i++)
- {
- EpgResetSCH(TsIndex, i);
- }
-
- return;
- }
- UINT16 KB_EPG_GetCurrentTsid(void)
- {
- UINT16 CurTsid = 0xffff;
- UINT32 TmpTsid = 0xffffffff;
- UINT32 EpgTsIndex = 0xffffffff;
-
- if (0 == CurrentSignal.frequency
- || RET_OK != KB_DBGetTsIDByFre(&CurTsid, CurrentSignal.frequency))
- {
- printf("nKB_EPG_GetCurrentTsid::CurrentSignal fre(%ld) error",
- CurrentSignal.frequency);
- return 0xffff;
- }
-
- EpgTsIndex = KB_EPG_GetCurrentTsNum();
- TmpTsid = KB_EPG_GetTsIdFromTsNum(EpgTsIndex);
- if (0xffffffff == TmpTsid || TmpTsid != (UINT32)CurTsid)
- {
- KB_EPG_SetCurrentTsNum((UINT32)CurTsid);
- }
-
- return CurTsid;
- }
- /* EOF */