⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 为了满足每个项目终端的需求
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************		FileName:       main.c*		Revision:       2008/7/25*		Author:			Du Biao***************************************************************************** DESCRIPTION:	main program,init rom and channel, process incomming pkt** COPYRIGHT:	(C) HEBEI FAR-EAST HARRIS COMMUNICATIONS COMPANY LTD.* HISTORY:	08/07/25:		Initial verison by Du Biao**/#include <stdio.h>#include <stdlib.h>#include <strings.h>#include <string.h>#include <time.h>#include <sys/time.h>#include <unistd.h>#include <sys/stat.h>#include "../inc/version.h"#include "../inc/gendef.h"#include "../inc/rom_drv.h"#include "../inc/msgpkt.h"#include "../inc/channel.h"//#undef MAIN_DEBUG#define MAIN_DEBUG#define TIME_DEBUG//#define NTOHS_DEBUG#ifdef MAIN_DEBUG	#define MAIN_DPRINTF( x... )	printf("MAIN:    \t" ##x)#else	#define MAIN_DPRINTF( x... )#endif/****************************************************************************	LOCAL DEFINITIONS AND DECLARATIONS******************************************************************************/LOCAL int gChannelHandle = 0;LOCAL int gFlashHandle = 0;LOCAL int gHELLO_FLAG = 0;LOCAL int gERASE_FLAG = 0;LOCAL int gKEY_FLAG = 0;LOCAL int gSTORAGE_FLAG = 0;//LOCAL U16 gData_Len = 0;LOCAL U8  gTemp_Msgtype = 0;LOCAL U16 gWrite_Endnum = 0;LOCAL U16 gSumera_Num = 0;LOCAL U8  gRom_Type = 0x01;LOCAL U32 gBlock_Size = BLOCK_SIZE;LOCAL RAM_BUFFER gFileRAM;LOCAL void VersionInfo(void);LOCAL void Mbox_boot_check_yafdir(void);LOCAL int APP_ProcessPkt(PKT *pkt);LOCAL int APP_Hello(PKT *pkt);LOCAL int APP_StorageBlock(PKT* pkt);LOCAL int APP_EraseBlock(PKT* pkt);LOCAL int APP_ReadCfg(PKT* pkt);LOCAL int APP_WriteCfg(PKT* pkt);LOCAL int APP_WriteBlock(PKT* pkt);LOCAL int APP_CutLink(PKT* pkt);/**************************************************************************** main***************************************************************************** FUNCTION: program entry,process rom init,channel init,buffer init		then enter an inifinite message process circle* INPUTS:	argc - num of paras, argv - pointer to args* OUTPUTS:	none* RETURN:	err number* HISTORY:	08/07/25	Initial version by Du Biao*/int main(void){	int ret;	PKT pkt;#if 0//dubug	PKT debug_pkt;	char *s = "1999050710245810025###54######2#######1#######5#######0001####222#####";	gHELLO_FLAG = 1;	//gSTORAGE_FLAG =1;	//gERASE_FLAG = 1;	gKEY_FLAG = 1;	debug_pkt.Start = START_BYTE;	debug_pkt.MsgType = CMD_CFG_DATA;	debug_pkt.Msg.Index = 1;	//MAIN_DPRINTF("debug_pkt.Msg.Index is %d\n",debug_pkt.Msg.Index);	debug_pkt.Msg.Sum = 0;	debug_pkt.Msg.DataLen = 78;	memset(debug_pkt.Msg.Data,'\0',MAX_FILE_DATA_LEN+1);	memcpy(debug_pkt.Msg.Data,s,MAX_CFG_LENGTH);	//memset(debug_pkt.Msg.Data,'0',MAX_CFG_LENGTH);	//MAIN_DPRINTF("the debug_pkt in is %s\n",debug_pkt.Msg.Data);	debug_pkt.Msg.BCC = PktCalcCRC(debug_pkt.Msg.Data,debug_pkt.Msg.DataLen);	//debug_pkt.Msg.Data[0] = "#";	//debug_pkt.Msg.Data[1] = "*";//////////////////#endif	VersionInfo();	Mbox_boot_check_yafdir();	gChannelHandle = CHN_Open(ARM_PROGRAM_PORT);	if(gChannelHandle== -1)	{		MAIN_DPRINTF("Open port err\n");		return -1;	}	MAIN_DPRINTF("CREATE SOCKET OK\n");	gFlashHandle = ROM_Init(0x01);	if(gFlashHandle == -1)	{		printf("\nFIRMWARE UPDATE: main,ROM init err");		return -1;	}	gFileRAM.Buffer = NULL;	gFileRAM.BufferBak = NULL;	//MAIN_DPRINTF("APP_ReadCfg\n");	//APP_ProcessPkt(&debug_pkt);	MAIN_DPRINTF("APP_ReadCfg sizeof is %d\n",sizeof(PKT));#if 1	while(1)	{		memset(pkt.Msg.Data,'\0',MAX_FILE_DATA_LEN+1);		if(ReadPkt(gChannelHandle, &pkt, sizeof(PKT)-1) > 0)		{			MAIN_DPRINTF("pkt.Start is %d\n",pkt.Start);			MAIN_DPRINTF("pkt.MsgType is %d\n",pkt.MsgType);			MAIN_DPRINTF("pkt.Msg.Index is %d\n",pkt.Msg.Index);			MAIN_DPRINTF("pkt.Msg.Sum is %d\n",pkt.Msg.Sum);			MAIN_DPRINTF("pkt.Msg.Datalen is %d\n",pkt.Msg.DataLen);			MAIN_DPRINTF("pkt.Msg.BCC is %d\n",pkt.Msg.BCC);			//MAIN_DPRINTF("pkt.Msg.Data is %s\n",pkt.Msg.Data);#if 1			if(CheckPkt(&pkt) != 0)			{				MAIN_DPRINTF("CheckPkt err\n");				if((ret = SendPkt (gChannelHandle,STA_PACKET_ERR,0,0))== -1)				{					MAIN_DPRINTF  ("SendPkt err\n");					return -1;				}			}			else			{				//MAIN_DPRINTF("APP_ProcessPkt()\n");				APP_ProcessPkt(&pkt);			}#endif		}		else		{			MAIN_DPRINTF("ReadPkt err\n");			return -1;		}	}#endif	return 0;}/************************************************************************* APP_ProcessPkt*************************************************************************FUNCTION: process incomming pktINPUTS:   pktOUTPUTS:  noneRETURN:   0 is OK, otherwise error**/int APP_ProcessPkt(PKT *pkt){	switch(pkt->MsgType)	{		case CMD_HELLO:			APP_Hello(pkt);//debug			MAIN_DPRINTF  ("test hello OK\n");////////////			break;		case CMD_PPCBOOT_DATA:		case CMD_KERNEL_DATA:		case CMD_RAMDISK_DATA:			if(gHELLO_FLAG== 1)			{				 APP_StorageBlock(pkt);				 gSTORAGE_FLAG = 1;//debug			MAIN_DPRINTF  ("test APP_StorageBlock OK\n");////////////			}			break;		case CMD_ERASE_BLOCK:			if(gSTORAGE_FLAG== 1)			{				 APP_EraseBlock(pkt);				 gERASE_FLAG = 1;//debug			MAIN_DPRINTF  ("test CMD_ERASE_BLOCK OK\n");////////////			}			break;		case CMD_WRITE_BLOCK:			if(gERASE_FLAG== 1)			{				 APP_WriteBlock(pkt);				 gERASE_FLAG = 0;//debug			MAIN_DPRINTF  ("test CMD_WRITE_BLOCK OK\n");////////////			}			break;		case CMD_READCFG_ACK:			if(gHELLO_FLAG== 1)			{				 APP_ReadCfg(pkt);				 gKEY_FLAG = 1;			}//debug			MAIN_DPRINTF  ("test CMD_READCFG_ACK OK\n");////////////			break;		case CMD_CFG_DATA:			if(gHELLO_FLAG== 1)			{				 APP_WriteCfg(pkt);			}//debug			MAIN_DPRINTF  ("test CMD_CFG_DATA OK\n");////////////			break;		case CMD_CUT_LINK:			APP_CutLink(pkt);//debug			MAIN_DPRINTF  ("test APP_CutLink OK\n");////////////			break;		case CMD_RESTART_ACK:			if(gFileRAM.BufferBak != NULL)			{				free(gFileRAM.BufferBak);				gFileRAM.Buffer = NULL;				gFileRAM.BufferBak = NULL;			}			system("reboot");			break;		default:			MAIN_DPRINTF("APP_ProcessPkt: Receive CMD error\n");			break;	}	return 0;}/************************************************************************* APP_Hello*************************************************************************FUNCTION: clear file buffer and sent back version number			to confirm connectionINPUTS:   pkt pointerOUTPUTS:  noneRETURN:   0 is OK, otherwise error**/int APP_Hello(PKT *pkt){	int ret;	HELLO_DATA	*hello_info;	FILE_DATA  hello_tempbuffer;	hello_info->Version = 1;	hello_info->ResBYTE = RESERVED_BYTE;	//MAIN_DPRINTF("pkt->Msg.Index is %d\n",pkt->Msg.Index);	//MAIN_DPRINTF("pkt->Msg.Sum is %d\n",pkt->Msg.Sum);	//MAIN_DPRINTF("pkt->Msg.DataLen is %d\n",pkt->Msg.DataLen);	//MAIN_DPRINTF("pkt->Msg.Data is %s\n",pkt->Msg.Data);	//MAIN_DPRINTF("pkt->Msg.BCC is %d\n",pkt->Msg.BCC);#ifdef NTOHS_DEBUG	hello_tempbuffer.Index = ntohs(pkt->Msg.Index);	hello_tempbuffer.Sum = ntohs(pkt->Msg.Sum);#else	hello_tempbuffer.Index = pkt->Msg.Index;	hello_tempbuffer.Sum = pkt->Msg.Sum;#endif#if 1	hello_tempbuffer.DataLen = sizeof(HELLO_DATA);	memset(hello_tempbuffer.Data,'\0',MAX_FILE_DATA_LEN+1);	memcpy(hello_tempbuffer.Data,(U8*)hello_info,hello_tempbuffer.DataLen);	hello_tempbuffer.BCC = PktCalcCRC(hello_tempbuffer.Data,hello_tempbuffer.DataLen);	//MAIN_DPRINTF("hello_tempbuffer.Index is %d\n",hello_tempbuffer.Index);	//MAIN_DPRINTF("hello_tempbuffer.Sum is %d\n",hello_tempbuffer.Sum);	//MAIN_DPRINTF("hello_tempbuffer.DataLen is %d\n",hello_tempbuffer.DataLen);	//MAIN_DPRINTF("hello_tempbuffer.Data is %s\n",hello_tempbuffer.Data);	//MAIN_DPRINTF("hello_tempbuffer.BCC is %d\n",hello_tempbuffer.BCC);	//MAIN_DPRINTF("hello_tempbuffer.Index is %d\n",hello_tempbuffer.Index);	if((ret = SendPkt (gChannelHandle,STA_HELLO_ACK,&hello_tempbuffer,(hello_tempbuffer.DataLen+7)))== -1)	{		MAIN_DPRINTF  ("hello_ack err\n");		return -1;	}#endif	if(gFileRAM.BufferBak != NULL)	{		free(gFileRAM.BufferBak);		gFileRAM.Buffer = NULL;		gFileRAM.BufferBak = NULL;	}	gHELLO_FLAG = 1;	gERASE_FLAG = 0;	gSTORAGE_FLAG = 0;	return 0;}/************************************************************************** APP_StorageBlock**************************************************************************FUNCTION: save the file data in pkt to file buffer,prepare to write			notify peer file received.INPUTS:   pkt pointerOUTPUTS:  noneRETURN:   0 is OK, otherwise error**/int APP_StorageBlock(PKT* pkt){	int ret;	U8 send_statype_ok,send_statype_err;	U16 pkt_Len,storage_index,storage_pktsum;	FILE_DATA *storage_buffer,sto_returntemp;	storage_buffer = &(pkt->Msg);#ifdef NTOHS_DEBUG	gTemp_Msgtype = ntohs(pkt->MsgType);	storage_index = ntohs(pkt->Msg.Index);	storage_pktsum = ntohs(pkt->Msg.Sum);	pkt_Len = ntohs(pkt->Msg.DataLen);#else	gTemp_Msgtype = pkt->MsgType;	storage_index = pkt->Msg.Index;	storage_pktsum = pkt->Msg.Sum;	pkt_Len = pkt->Msg.DataLen;#endif	sto_returntemp.Index = storage_index;	sto_returntemp.Sum = storage_pktsum;	sto_returntemp.DataLen = MAX_FILE_DATA_LEN;	memset(sto_returntemp.Data,'\0',MAX_FILE_DATA_LEN+1);	memset(sto_returntemp.Data,'0',MAX_FILE_DATA_LEN);	sto_returntemp.BCC = PktCalcCRC(sto_returntemp.Data,sto_returntemp.DataLen);	MAIN_DPRINTF("sto_returntemp.Sum is %d\n",sto_returntemp.Sum);	/*storage pkt type,ppc,kernel,or ramdisk?*/	if(gTemp_Msgtype== CMD_PPCBOOT_DATA)	{		send_statype_ok = STA_PPCBOOTFILE_OK;		send_statype_err = STA_PPCBOOTFILE_ERR;		//gWrite_Index = storage_index - 1;    }    else if(gTemp_Msgtype== CMD_KERNEL_DATA)    {		send_statype_ok = STA_KERNELFILE_OK;		send_statype_err = STA_KERNELFILE_ERR;		//gWrite_Index = storage_index + 15;    }    else if(gTemp_Msgtype== CMD_RAMDISK_DATA)	{		send_statype_ok = STA_RAMDISKFILE_OK;		send_statype_err = STA_RAMDISKFILE_ERR;		//gWrite_Index = storage_index + 79;    }    else	{    	return -1;	}	/*if client send err kinds of datapkt,storage a new one,free the buffer*/	if((storage_index== 1)&&(gFileRAM.BufferBak!= NULL))	{		free(gFileRAM.BufferBak);		gFileRAM.Buffer = NULL;		gFileRAM.BufferBak = NULL;		MAIN_DPRINTF("the sb client send err Datapkt,and storage a new Datapkt\n");	}	/*init the gfileram.buffer*/	if(gFileRAM.Buffer == NULL)	{		gFileRAM.Buffer = malloc(MAX_BUFFER_SIZE);		if(gFileRAM.Buffer == NULL)		{			MAIN_DPRINTF("not enough memory\n");			return -1;		}		gFileRAM.BufferBak = gFileRAM.Buffer;		gFileRAM.TotalSize = MAX_BUFFER_SIZE;		gFileRAM.WritenSize = 0;		gFileRAM.UsedSize = 0;		memset(gFileRAM.Buffer, '\0', gFileRAM.TotalSize);		gFileRAM.DataPktIndexBak = 0;	}	if((storage_index > storage_pktsum) || (pkt_Len > MAX_FILE_DATA_LEN) ||(storage_pktsum > MAX_PKTSUM) )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -