📄 dm642helper.cpp
字号:
#include<stdio.h>
#include<stdlib.h> //for exit()
#include<malloc.h> //for malloc()
#include<sys/time.h> //for time()
#include<string.h> //for memset(),bzero()
#include<unistd.h> //for close()
#include<pthread.h> //for pthread functions
#include<errno.h> //for error number use
#include <sys/stat.h>
#include<pthread.h> //for pthread functions
#include <sys/mman.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <wait.h>
#include <signal.h>
#include"common.h" //for golbal setting
#include "DM642Helper.h"
#include "transfer.h"
#include "utility.h"
#include "MibStructure.h"
#include "privateDataInsert.h"
#include "sdtParser.h"
#include "snmpHelper.h"
#include "snmpSendTrap.h"
#include "configfile.h"
#include "snmpfunc.h"
//extern global variables declare
#define BUFFER_LEN (1344*(1024*1024*2/1344))
//global declare
int fd[4];//for DM642 device file
int fd_mem;//for DM642 device mmap
char * mem_ptr[4];
DM642MAP map;
DM642STATUS status_DM642;
BOOL IsSingleBoard = TRUE;
UINT32 nSlaveDeadTimes = 0;
UINT32 nSlaves = 0;
UINT32 nTmp = 0;
//if IsSDTover = 1, then we can do hear-beat check for 1:1 backup
bool IsSDTover = 0;
//BOOL IsSDTover[MAXSUBBOARD] = {0};
extern UINT32 nSDTServiceNum[MAXSUBBOARD*MAXVIDEOPORT];
//for privateInsert
UINT32 nNowPrivateDataIndex[MAXSUBBOARD*MAXVIDEOPORT*MAXPROGRAM][MAXPRIVATEDATA] = {0};
extern struct_PrivateDataPerProgram g_PrivateDataPerPrograms[MAXSUBBOARD*MAXVIDEOPORT*MAXPROGRAM];
UINT8 nDataBlockIndex[MAXSUBBOARD*MAXVIDEOPORT*MAXPROGRAM] = {0};
void RemoveDriver()
{
UINT32 i;
for(i=0;i<map.board_num;i++)
{
close(fd[i]);
munmap(mem_ptr[i],2*1024*1024);
}
}
void ClearInterrupt(int fd)
{
ACCESS_REG accReg;
accReg.op = 0;
accReg.reg = PCI64_HSR;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
accReg.data |= BIT0;
ioctl(fd,0,(U32)&accReg);
}
void Enable_Dsp_Interrupt(int fd)
{
ACCESS_REG accReg;
accReg.op = 0;
accReg.reg = PCI64_PCIIEN;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
accReg.data |= BIT6;
ioctl(fd,0,(U32)&accReg);
}
void EnableInterrupt(int fd)
{
ACCESS_REG accReg;
accReg.op = 0;
accReg.reg = PCI64_HSR;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
accReg.data &= ~BIT2;
ioctl(fd,0,(U32)&accReg);
}
void ClearInterruptSource(int fd)
{
ACCESS_REG accReg;
accReg.op = 0;
accReg.reg = PCI64_PCIIS;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
ioctl(fd,0,(U32)&accReg);
}
void GenerateInterrupt(int fd)
{
ACCESS_REG accReg;
// printf("generate an interrupt!\n");
accReg.op = 0;
accReg.reg = PCI64_RSTSRC;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
accReg.data = BIT3;
ioctl(fd,0,(U32)&accReg);
// sleep(1);
accReg.data = BIT4;
ioctl(fd,0,(U32)&accReg);
}
void InterruptTarget(int fd)
{
ACCESS_REG accReg;
printf("interrupt the target board!\n");
accReg.op = 0;
accReg.reg = PCI64_HDCR;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
accReg.data |= BIT1;
ioctl(fd,0,(U32)&accReg);
}
void SetEnable(int fd)
{
ACCESS_REG accReg;
accReg.op = 0;
accReg.reg = PCI64_PCIIEN;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
accReg.op = 1;
accReg.data |= BIT9;
ioctl(fd,0,(U32)&accReg);
accReg.op = 0;
accReg.reg = PCI64_PCIIEN;
accReg.data = 0x0;
ioctl(fd,0,(U32)&accReg);
printf("read pciien=0x%x\n",accReg.data);
}
STATUS readSDTSectionFromFile(UINT8*pSDTSectionData,UINT32* pSDTSectionSize,char* inputFileName)
{
FILE* sdtFile;
struct stat filestat;
unsigned int filesize;
unsigned int readsize;
//DebugPrint(g_DbgLevel,g_fpLogFile,"Get section from file %s\n", inputFileName);
if(stat(inputFileName, &filestat)<0)
{
perror("stat");
return STATUS_ERROR;
}
filesize= filestat.st_size;
*pSDTSectionSize=filesize;
if((sdtFile=fopen(inputFileName,"rb"))==NULL)
{
DebugPrint(g_DbgLevel,g_fpLogFile, "Failed to open file %s \n",inputFileName);
return STATUS_ERROR;
}
if(pSDTSectionData!=NULL)
{
free(pSDTSectionData);
pSDTSectionData=NULL;
}
pSDTSectionData=(unsigned char*)malloc(sizeof(unsigned char)*filesize);
readsize=fread(pSDTSectionData,1,filesize,sdtFile);
if(filesize!=readsize)
{
DebugPrint(g_DbgLevel,g_fpLogFile,"fread error,filesize=%d,readsize=%d\n",filesize,readsize);
fclose(sdtFile);
return STATUS_ERROR;
}
//usleep(1000);
if(fclose(sdtFile))
{
DebugPrint(g_DbgLevel,g_fpLogFile, "Failed to close file %s \n",inputFileName);
return STATUS_ERROR;
}
return STATUS_OK;
}
void WriteToDM642(struct_MCU2DSP* pMCU2DSP,int nSubboardNo)
{
ACCESS_INFO Info;
Info.len = sizeof(struct_MCU2DSP);
UINT32 subboardindex;
memcpy(Info.data,(UINT8*)pMCU2DSP,sizeof(struct_MCU2DSP));
for(subboardindex=0;subboardindex<map.board_num;subboardindex++)
{
if(map.vector[subboardindex]==nSubboardNo+1)
{
ioctl(fd[subboardindex],3,(U32)&Info);
return;
}
}
DebugPrint(g_DbgLevel,g_fpLogFile, "Subboard %d doesn't exist\n");
}
static UINT32 sigcount=0;
void sigroutine(int signo) {
UINT8 subboardindex;
UINT8 videoportindex;
if(signo==SIGALRM)
{
sigcount++;
//use ioctl to get subboard status
for(subboardindex=0;subboardindex<map.board_num;subboardindex++)
{
/*
if(g_SubboardInfo[subboardindex].SubboardStatus!=SUBBOARD_NOSTREAM)
{
g_SubboardInfo[subboardindex].SubboardStatus=(enum_SubboardStatus)(status_DM642.vector[subboardindex]+1);
if(g_SubboardInfo[subboardindex].SubboardStatus==SUBBOARD_NOSTREAM)
{
DebugPrint(g_DbgLevel,g_fpLogFile,"SUBBOARD_NOSTREAM:subboardindex= %d\n",subboardindex);
//snmp_TrapSubboardChange(5,g_BladeInfo.BladeNo, subboardindex, g_SubboardInfo[subboardindex].SubboardStatus);
//snmpWriteSubboardTable(subboardindex);
}
}
*/
ioctl(fd[subboardindex],8,&status_DM642);
g_SubboardInfo[subboardindex].SubboardStatus=(enum_SubboardStatus)(status_DM642.vector[subboardindex]+1);
if(g_SubboardInfo[subboardindex].SubboardStatus==SUBBOARD_DEAD)
{
//snmp_set_BladeStatus(g_BladeInfo.BackBladeNo,SUBBOARD_WORK);
//snmp_set_SubboardStatus(g_BladeInfo.BackBladeNo, subboardindex,SUBBOARD_WORK);
//g_BladeInfo.BackBladeStatus=BLADE_STATUS_WAIT;
DebugPrint(g_DbgLevel,g_fpLogFile,"SUBBOARD_DEAD:subboardindex= %d\n",subboardindex);
//g_BladeInfo.MCUStatus=MCU_STATUS_DEAD;
g_BladeInfo.BladeStatus=BLADE_STATUS_WAIT;
//g_BladeInfo.BackBladeStatus=BLADE_STATUS_WORK;
//snmpWriteBladeInfo();
//snmpWriteSubboardTable(subboardindex);
//snmp_TrapSubboardChange(5,g_BladeInfo.BladeNo, subboardindex, g_SubboardInfo[subboardindex].SubboardStatus);
g_DbgLevel=DEBUG_NONE;
//system("killall mcu");
snmp_TrapPoll(5, g_BladeInfo.BackBladeNo,g_BladeInfo.BackBladeIPv4);
system("reboot");
}
}
/*
if(sigcount == 10)
{
IsSDTover = 1;
}*/
if(sigcount%5==0)
{
//use local snmp to set local MCU STATUS
if(snmp_set_MasterMCUStatus(g_BladeInfo.BladeNo, g_BladeInfo.MCUStatus)!=0)
{
//local snmp may die
DebugPrint(g_DbgLevel,g_fpLogFile,"local snmp may die\n");
g_BladeInfo.BladeStatus=BLADE_STATUS_WAIT;
g_DbgLevel=DEBUG_NONE;
//system("killall mcu");
system("reboot");
//g_BladeInfo.BackBladeStatus=BLADE_STATUS_WORK;
//g_BladeInfo.BladeStatus=BLADE_STATUS_WAIT;
//snmp_TrapBladeSuccess(5, g_BladeInfo.BladeNo);//no matter it goes
}
if(IsSingleBoard == FALSE)
{
//use neighbour snmp to get neighbour MCU STATUS
//printf("before:debug:g_BladeInfo.BackMCUStatus=%d\n",g_BladeInfo.BackMCUStatus);
//if((g_BladeInfo.BackMCUStatus==MCU_STATUS_ALIVE)&&(gCut < 3))
if(g_BladeInfo.BackMCUStatus==MCU_STATUS_ALIVE)
{
//printf("after:debug:g_BladeInfo.BackMCUStatus=%d\n",g_BladeInfo.BackMCUStatus);
if(snmp_get_MasterMCUStatus(g_BladeInfo.BackBladeNo, (long *)&g_BladeInfo.BackMCUStatus)!=0)
{
//neighbour snmp may die
//printf("come to snmp_get_MasterMCUStatus %d times,gCut=%d\n",++nSlaves,gCut);
DebugPrint(g_DbgLevel,g_fpLogFile,"neighbour snmp may die\n");
g_BladeInfo.BackMCUStatus=MCU_STATUS_DEAD;
g_BladeInfo.BackBladeStatus=BLADE_STATUS_WAIT;
g_BladeInfo.BladeStatus=BLADE_STATUS_WORK;
g_BladeInfo.MCUStatus=MCU_STATUS_ALIVE;
//gCut++;
snmpWriteBladeInfo();
/*
snmpWriteBladeInfo();
snmp_TrapReboot(5,g_BladeInfo.BackBladeNo, g_BladeInfo.BackBladeIPv4);
*/
snmp_TrapPoll(5, g_BladeInfo.BladeNo,g_BladeInfo.BladeIPv4);
}
else
{
//printf("snmp get right %d times\n",++nTmp);
if(g_BladeInfo.BackMCUStatus==MCU_STATUS_DEAD)
{
if(g_BladeInfo.BackBladeStatus==BLADE_STATUS_WAIT)
{
nSlaveDeadTimes++;
g_BladeInfo.BackMCUStatus=MCU_STATUS_ALIVE;
if(nSlaveDeadTimes==10)
{
nSlaveDeadTimes = 0;
//neighbour mcu may die
DebugPrint(g_DbgLevel,g_fpLogFile,"neighbour mcu may die\n");
g_BladeInfo.BladeStatus=BLADE_STATUS_WORK;
g_BladeInfo.BackBladeStatus=BLADE_STATUS_WAIT;
g_BladeInfo.BackMCUStatus=MCU_STATUS_DEAD;
snmpWriteBladeInfo();
snmp_TrapReboot(5,g_BladeInfo.BackBladeNo, g_BladeInfo.BackBladeIPv4);
snmp_TrapPoll(5, g_BladeInfo.BladeNo,g_BladeInfo.BladeIPv4);
/*
if(IsSingleBoard == FALSE)
snmp_TrapPoll(5, g_BladeInfo.BackBladeNo,g_BladeInfo.BackBladeIPv4);
*/
}
}
else
{
DebugPrint(g_DbgLevel,g_fpLogFile,"neighbour mcu may die\n");
g_BladeInfo.BladeStatus=BLADE_STATUS_WORK;
g_BladeInfo.BackBladeStatus=BLADE_STATUS_WAIT;
g_BladeInfo.BackMCUStatus=MCU_STATUS_DEAD;
snmpWriteBladeInfo();
snmp_TrapReboot(5,g_BladeInfo.BackBladeNo, g_BladeInfo.BackBladeIPv4);
snmp_TrapPoll(5, g_BladeInfo.BladeNo,g_BladeInfo.BladeIPv4);
/*
if(IsSingleBoard == FALSE)
snmp_TrapPoll(5, g_BladeInfo.BackBladeNo,g_BladeInfo.BackBladeIPv4);
*/
}
}
}
}
}
}
}
signal(SIGALRM, sigroutine);
return;
}
STATUS InitDM642()
{
int fd1;
int err;
ACCESS_REG accReg;
unsigned char buffer[1024];
UINT32 phy[4];
UINT8 subboardindex;
char* DM642_dev_path[4]={
"/dev/dm642/dm642_00",
"/dev/dm642/dm642_10",
"/dev/dm642/dm642_20",
"/dev/dm642/dm642_30"};
//for setitimer
struct itimerval value,ovalue;
//get subboard information
fd1 = open("/home/mcu/firmware",O_RDWR,S_IWUSR|S_IRUSR);
fd[0]=open(DM642_dev_path[0],O_RDWR,S_IWUSR|S_IRUSR);
fd_mem = open("/dev/mem",O_RDWR);
ioctl(fd[0],7,&map);
printf("board num=%d\n",map.board_num);
printf("00->board[%d]\n",map.vector[0]);
printf("10->board[%d]\n",map.vector[1]);
printf("20->board[%d]\n",map.vector[2]);
printf("30->board[%d]\n",map.vector[3]);
for(subboardindex=1;subboardindex<map.board_num;subboardindex++)
{
fd[subboardindex]=open(DM642_dev_path[subboardindex],O_RDWR,S_IWUSR|S_IRUSR);
}
for(subboardindex=0;subboardindex<map.board_num;subboardindex++)
{
accReg.data = 0x0;
accReg.op = 1;
accReg.reg = 0x01C1FFF8UL;
ioctl(fd[subboardindex],0,(U32)&accReg);
ioctl(fd[subboardindex],6,&phy[subboardindex]);
mem_ptr[subboardindex] =(char *)mmap(0,2*1024*1024,PROT_READ|PROT_WRITE,MAP_SHARED,fd_mem,phy[subboardindex]);
if(mem_ptr[subboardindex] == MAP_FAILED)
{
DebugPrint(g_DbgLevel,g_fpLogFile,"mmap failed on dev_%d\n",subboardindex);
return -1;
}
}
//download firmware for each subboard
while(1)
{
err = read(fd1,buffer,1024);
if(err < 0)
{
DebugPrint(g_DbgLevel,g_fpLogFile,"read firmware error!\n");
return STATUS_ERROR;
}
else if(err == 0)
{
DebugPrint(g_DbgLevel,g_fpLogFile,"read complete!\n");
break;
}
else
{
for(subboardindex=0;subboardindex<map.board_num;subboardindex++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -