123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738 |
- /********************************************************************************
- * File Name: WiredLanTask
- * Function Describe:The Wired Lan Task
- * Relate Module:
- * Explain:
- * Writer: slw
- * Date:
- *******************************************************************************/
- #define THIS_FILE_ID 11
- //---------------------------------------------------------------------------------
- #include "includes.h"
- #include "spi.h"
- #include "socket.h"
- #include "Internet/TcpEchoServer.h"
- #include "Internet/dhcp.h"
- #include "Internet/dns.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- //SOCKET 分配
- #define SOCK_UDPS_DHCP 0 //DHCP 用于动态分配本地IP
- #define SOCK_UDPS_DNS 1 //DNS Clinet
- #define SOCK_TCPS_APP1 2 //Clinet
- #define SOCK_TCPS_LOOPBACK 7 //回环测试
- //uint8_t DHCP_MSG_BUF[548];
- #define DATA_BUF_SIZE 1050
- uint8_t gDATABUF[DATA_BUF_SIZE];
- SUT_WL_STATUS sutWLanStatus;
- /* Private macro -------------------------------------------------------------*/
- OS_TID idWLanTask=0;
- U64 stkWLANTask[WIRE_LAN_TASK_STK_SIZE];
- // Default Network Configuration
- wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc,0x00, 0xab, 0xcd},
- .ip = {0, 0, 0, 0},
- .sn = {255,255,255,0},
- .gw = {0, 0, 0, 0},
- .dns = {0,0,0,0},
- .dhcp = NETINFO_DHCP };//dhcp:=NETINFO_STATIC or NETINFO_DHCP
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- void network_init(void); // Initialize Network information and display it
- void cb_ip_assign(void); //callback func when IP is assigned from DHCP server first
- void cb_ip_update(void); // callback func when IP is changed
- void cb_ip_conflict(void); // callback func when the assigned IP is conflict with others.
- uint8_t ClientInit(void);
- void ClientRun(void);
- int32_t WiredSendTcpData(uint8_t *data,uint16_t len);
- void SetIoMode(uint8_t iomode);//SOCK_IO_NONBLOCK;//SOCK_IO_BLOCK;
-
- int SendLogin2Server(void);
- int SendCurPosition2Server(void);
- /* ************************************************************************
- W5500 PMODE配置
- 111: ALL CAPABLE,Auto-Negotiation enable
- * ************************************************************************/
- void W5500_PMODE_CONFIG(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- //PB3->PMODE0,PB4->PMODE1,PB5->PMODE2
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4|GPIO_Pin_5;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_SetBits(GPIOB,(GPIO_Pin_3 | GPIO_Pin_4|GPIO_Pin_5));
- }
- /* ************************************************************************
- W5500 RESET 配置
- (Active low) RESET should be held low at least 500 us for W550
- * ************************************************************************/
- void W5500_RESET_CONFIG(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- //PB2->RESET
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);
- os_dly_wait(1);
- GPIO_SetBits(GPIOB,GPIO_Pin_2);
- }
- /*
- 链接服务器
- type=0 app服务器
- type=1 server服务器
- 成功 返回1
- 失败 返回0
- */
- int8_t Connect2Server(int type)
- {
- uint8_t mode;
- char tracebuf[22];
- int8_t r;
- if(type){
- SlwTrace(DEBUG,"[WL]Connect2FoTaServer\r\n");
- if(sutServerIp.FotaServerIP[0]==0){
- SlwTrace(DEBUG,"SOCKERR_IPINVALID\r\n");
- os_dly_wait(500);
- return 0;
- }
- r=connect(SOCK_TCPS_APP1,sutServerIp.FotaServerIP,sutProductPara.FotaPort);
- }else{
- SlwTrace(DEBUG,"[WL]Connect2AppServer\r\n");
- if(sutServerIp.AppServerIP[0]==0){
- SlwTrace(DEBUG,"SOCKERR_IPINVALID\r\n");
- os_dly_wait(500);
- return 0;
- }
- r=connect(SOCK_TCPS_APP1,sutServerIp.AppServerIP,sutProductPara.AppPort);
- }
- if(r==SOCK_OK){
- SlwTrace(DEBUG,"Connect OK!\r\n");
- return 1;
- }else if(r==SOCKERR_SOCKINIT || r==SOCKERR_SOCKMODE){//socket未初始化,初始化之
- SlwTrace(DEBUG,"socket init...\r\n");
- mode=SOCK_IO_NONBLOCK;
- socket(SOCK_TCPS_APP1,Sn_MR_TCP,6000,SF_TCP_NODELAY);
- ctlsocket(SOCK_TCPS_APP1,CS_SET_IOMODE,&mode);
- }else if(r==SOCKERR_TIMEOUT){
- SlwTrace(DEBUG,"Connect timeout!\r\n");
- //os_dly_wait(1000);
- }else {
- sprintf(tracebuf,"connect err=%d\r\n",r);
- SlwTrace(DEBUG,tracebuf);
- }
- return 0;
- }
- /*
- */
- void Disconnect2Server(void)
- {
- disconnect(SOCK_TCPS_APP1);
- SlwTrace(DEBUG,"[WL]CloseSocket!\r\n");
- }
- //int32_t SendReg2Server(void)
- //{
- // int r,len;
- // SlwTrace(DEBUG,">>SendReg>>\r\n");
- // sutModemStatus.TcpSendTimeCt++;
- // sutTestData.NetMode=2;//LAN模式上传
- // len=TcpPackingReg(g_ucTcpSendBuf);
- // if(len==0)return -1;
- // Debugsend(g_ucTcpSendBuf, len);//打印发送的数据
- // r = WiredSendTcpData(g_ucTcpSendBuf,len);
- // return r;
- //}
- //int32_t SendGetFileInfo2Server(void)
- //{
- // int r,len;
- // SlwTrace(DEBUG,">>SendGetFileInfo>>\r\n");
- // len=PacketGetFileInfo(g_ucTcpSendBuf);
- // r = WiredSendTcpData(g_ucTcpSendBuf,len);
- // return r;
- //}
- //uint8_t TcpData[400];
- //int32_t RecvDataFromServer()
- //{
- // char tracebuf[20];
- // int32_t r;
- // r=recv(SOCK_TCPS_APP1,gDATABUF, sizeof(gDATABUF));
- // if(r>0){
- // snprintf(tracebuf,sizeof(tracebuf),"<Recv[%d]<\r\n",r);
- // SlwTrace(DEBUG,tracebuf);
- // gDATABUF[r]=0;
- // SlwTrace(DEBUG,(char *)gDATABUF);
- // TcpRecvData(gDATABUF,r);
- // }
- // return r;
- //}
- //int32_t SendTest2Server(void)
- //{
- // int16_t len;
- // SlwTrace(DEBUG,">>SendUpload>>\r\n");
- // sutTestData.CSQ=0;
- // sutTestData.NetMode=2;
- // len=TcpPackingSendTest(g_ucTcpSendBuf);
- // return WiredSendTcpData(g_ucTcpSendBuf,len);
- //}
- void WiredLanRecv(unsigned char *pData,unsigned short len)
- {
- unsigned short UploadInterval,SamplingInterval;
- unsigned short AckID,AckNum;
- unsigned char Result;
- unsigned short NewVer;
- int cmd;
- int i;
- char buf[30];
- sutWLanStatus.ServerOfflineCt=0;
- sprintf(buf,"[WL]Recv<<%d\r\n",len);
- SlwTrace(DEBUG,buf);
- //FoTa升级接收数据处理
- if(sutFotaPara.fotaStatus!=FOTA_END){
- FoTaRecvData(pData,len);
- return;
- }
- //正常应用接收数据处理
- cmd=CheckPacket(pData,len);
- if(cmd<0){
- sprintf(buf,"[WL]CheckPacket Err=%d\r\n",cmd);
- SlwTrace(DEBUG,buf);
- return;
- }
- switch(cmd)
- {
- case 0x8200://上传应答
- //pData=8001 0005 000000000000 0000 01 02 03 04 86
- // ID 属性 设备编号 流水号 结果 红外使能 摄像头使能 复位使能 校验
- SlwTrace(DEBUG,"[WL]<<UploadAck\r\n");
- AckNum=((unsigned short)pData[10]<<8) | pData[11];
- sutWLanStatus.RecvCmdNum=AckNum;
- if(pData[12]!=0){//结果 0--成功
- return;
- };
- sutTestData.InfraredEn=pData[13];
- sutTestData.CameraEn=pData[14];
- sutTestData.ResetEn=pData[15];
- //sprintf(buf,"[WL]IEn=%02X CEn=%02X REn=%02X\r\n",sutTestData.InfraredEn,sutTestData.CameraEn,sutTestData.ResetEn);
- //SlwTrace(DEBUG,buf);
- break;
- case 0x8929://登陆应答
- //pData=8929 000E 000190210001 0018 190217142738 01 00 0000 0500 0500 01
- // ID Len 编码 流水号 时间 验证 升级 新版本 采样间隔 上传间隔 校验
- SlwTrace(DEBUG,"[WL]<<LoginAck\r\n");
- //TraceData(pData,25);
- AckNum=((unsigned short)pData[10]<<8)|pData[11];
- sutWLanStatus.RecvCmdNum=AckNum;
- // sprintf(buf,"RFlowId=%d\r\n",sutWLanStatus.RecvCmdNum);
- // SlwTrace(DEBUG,buf);
- RTC_Set(&pData[12]);
- //鉴权结果 0--成功 >0 未成功需要继续等N分钟再重新登陆
- sutWLanStatus.UnauthorizedCt=(unsigned short)pData[18];
- if(sutWLanStatus.UnauthorizedCt>0){
- sutWLanStatus.UnauthorizedCt*=60;
- SlwTrace(DEBUG,"Unauthorized!\r\n");
- sprintf(buf,"UCt=%d\r\n",sutWLanStatus.UnauthorizedCt);
- SlwTrace(DEBUG,buf);
- }
- if(pData[19]==1){//使能升级
- NewVer=((unsigned short)pData[20]<<8)|pData[21];
- if(NewVer!=0 && NewVer!=sutProductPara.SoftwareVer && sutModemStatus.FoTaGetFileInfoCt<5){
- sprintf(buf,"[WL]CurVer=%d NewVer=%d\r\n",sutProductPara.SoftwareVer,NewVer );
- SlwTrace(DEBUG,buf);
- SlwTrace(DEBUG,"[WL]Upgrade now!\r\n");
- sutFotaPara.targetVersion=NewVer;
- sutFotaPara.fotaStatus=FOTA_GET_FILEINFO;
- Disconnect2Server();
- sutWLanStatus.Step=WL_STEP_CONNETING;
- return;
- }else{
- sutWLanStatus.FoTaGetFileInfoCt=0;
- }
- }else sutWLanStatus.FoTaGetFileInfoCt=0;
- UploadInterval=((unsigned short)pData[22]<<8)|pData[23];
- SamplingInterval=((unsigned short)pData[24]<<8)|pData[25];
- if((sutDeviceConfig.UploadInterval!=UploadInterval&& UploadInterval>=5) ||
- (sutDeviceConfig.SamplingInterval!=SamplingInterval && SamplingInterval>=5)){
- sutDeviceConfig.UploadInterval=UploadInterval;
- sutDeviceConfig.SamplingInterval=SamplingInterval;
- SaveDeviceConfigToFlash();
- }
- break;
- }
- }
- void WireLanInit(void)
- {
- uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}};
- W5500_PMODE_CONFIG();
- W5500_RESET_CONFIG();
- SPI_Configuration();//Config SPI
- reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit); //注册临界区函数
- #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_
- reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数
- #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_
- reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW.
- #else
- #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_
- #error "Unknown _WIZCHIP_IO_MODE_"
- #else
- reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect);
- #endif
- #endif
- /* SPI Read & Write callback function */
- reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte); //注册读写函数
- /* WIZCHIP SOCKET Buffer initialize */
-
- if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){
- SlwTrace(DEBUG,"WIZCHIP Initialized fail.\r\n");
- GPIO_ResetBits(GPIOB,GPIO_Pin_2);//set W5500 Reset
- os_dly_wait(6000);
- }
- }
- void WLNextStep(WLAN_STEP step)
- {
- sutWLanStatus.StepCt=0;
- sutWLanStatus.Step=step;
- }
- int WLanRecvProcess(void)
- {
- int8_t flag=1;
- int ret;
- ret=recv(SOCK_TCPS_APP1,gDATABUF, sizeof(gDATABUF));
- if(ret>0)WiredLanRecv(gDATABUF,ret);
- return ret;
- }
- /*******************************************************************
- *WiredLanTask
- *有线网络任务
- ********************************************************************/
- __task void WiredLanTask(void)
- {
- static unsigned long lastTick=100;
- unsigned long curTick;
- static unsigned short susSendUploadCt=0;
- static unsigned char sucSendTickCt=0;
- static unsigned char sucSendWaitAckCt=0;//发送后等待ACK计数器
- char tracebuf[50];
- static uint8_t TraceCt=0;
- uint8_t tmp;
- int r;
- // int32_t ret = 0;
-
- // uint8_t flag=1;
- SlwTrace(DEBUG,"WiredLanTask Start...\r\n");
- memset(&sutWLanStatus,0,sizeof(SUT_WL_STATUS));
- while(1){
- if(++TraceCt>2)TraceCt=0;
- switch(sutWLanStatus.Step)
- {
- case WL_STEP_INIT:
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_INIT]\r\n");
- WireLanInit();
- WLNextStep(WL_STEP_GET_PHYLINK);
- break;
- case WL_STEP_GET_PHYLINK://检查IC是否工作(启动后需要插入网线才工作)
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_PHYLINK]\r\n");
- os_dly_wait(100);
- if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){
- SlwTrace(DEBUG,"Unknown PHY Link stauts.\r\n");
- if(++sutWLanStatus.StepCt>6000)WLNextStep(WL_STEP_INIT);
- }
- if(PHY_LINK_ON==tmp){
- network_init();
- WLNextStep(WL_STEP_CK);
- }else if(++sutWLanStatus.StepCt>30000)WLNextStep(WL_STEP_INIT);
- break;
- case WL_STEP_CK://持续检查网线是否插入
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_CK]\r\n");
- os_dly_wait(100);
- tmp=getPHYCFGR();
- if((tmp&PHYCFGR_LNK_ON)==PHYCFGR_LNK_ON){
- gWIZNETINFO.ip[0]=0;gWIZNETINFO.ip[1]=0;gWIZNETINFO.ip[2]=0;gWIZNETINFO.ip[3]=0;
- //DHCP client initialization
- DHCP_init(SOCK_UDPS_DHCP,gDATABUF);//DHCP_init(SOCK_UDPS_DHCP,DHCP_MSG_BUF);
- //Register DHCP call back function
- reg_dhcp_cbfunc(cb_ip_assign,cb_ip_update,cb_ip_conflict);
- WLNextStep(WL_STEP_DHCP);
- }
- else if(++sutWLanStatus.StepCt>30000)WLNextStep(WL_STEP_GET_PHYLINK);
- break;
- case WL_STEP_DHCP:
- os_dly_wait(1);
- DHCP_run();
- //控制一秒执行一次
- curTick=os_time_get();
- if(curTick>lastTick){
- lastTick=curTick+100;
- }else continue;
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_DHCP]\r\n");
- DHCP_time_handler();
- if(gWIZNETINFO.ip[0]!=0)WLNextStep(WL_STEP_IDEL);
- else if(++sutWLanStatus.StepCt>3000)WLNextStep(WL_STEP_IDEL);
- break;
- case WL_STEP_IDEL:
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_IDEL]\r\n");
- os_dly_wait(100);
- //IP为0 不登陆,需要等4G获取域名解析到IP才连接服务器
- if(sutServerIp.AppServerIP[0]==0){
- continue;
- }
- //4G离线时间超过125秒才登陆服务器
- if(sutModemStatus.ServerOfflineCt>WLAN_CONNECT_AFTER_4G_TIME){
- WLNextStep(WL_STEP_CONNETING);
- }
- break;
- case WL_STEP_CONNETING:
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_CONNETING]\r\n");
- os_dly_wait(100);
- //连接服务器
- if(sutFotaPara.fotaStatus!=FOTA_END){//远程升级
- r=Connect2Server(1);
- }else{//正常
- r=Connect2Server(0);
- }
-
- if(r){
- sutWLanStatus.CmdNum=sutWLanStatus.RecvCmdNum+5;//保证不相等
- sutModemStatus.CtrlReSendCt=5;//使进入WL_STEP_LOGIN后马上发送
- WLNextStep(WL_STEP_LOGIN);
- }else{
- //5秒一次,连接10次不成功,
- if(++sutWLanStatus.StepCt>10)WLNextStep(WL_STEP_CK);
- else os_dly_wait(500);
- }
- break;
- case WL_STEP_LOGIN:
- os_dly_wait(1);
- r=WLanRecvProcess();
- if(r<0){
- sprintf(tracebuf,"[WL]Recv Err=%d\r\n",r);
- SlwTrace(DEBUG,tracebuf);
- Disconnect2Server();
- WLNextStep(WL_STEP_CONNETING);
- continue;
- }else if(r>0)continue;
- //控制一秒执行一次
- curTick=os_time_get();
- if(curTick>lastTick){
- lastTick=curTick+100;
- }else continue;
- SlwTrace(DEBUG,"[WL_LOGIN]\r\n");
- //远程升级
- if(sutFotaPara.fotaStatus==FOTA_GET_FILEINFO){
- if(FoTaGetFileInfo(1)<0){
- Disconnect2Server();
- WLNextStep(WL_STEP_CONNETING);
- continue;
- }
- if(++sutWLanStatus.CtrlReSendCt>5){//连续发几次都收不到服务器应答,退出升级
- sutWLanStatus.CtrlReSendCt=0;
- sutFotaPara.fotaStatus=FOTA_END;
- Disconnect2Server();
- WLNextStep(WL_STEP_CONNETING);
- continue;
- }
- }else if(sutFotaPara.fotaStatus==FOTA_GET_FILEDATA){
- sutFotaPara.getFileInfoCt=0;
- if(++sutFotaPara.reSendCt>FOTA_RE_SEND_CT_MAX){
- sutFotaPara.reSendCt=0;
- if(FoTaGetFileData(1)<0){
- Disconnect2Server();
- WLNextStep(WL_STEP_CONNETING);
- continue;
- }
- }
- }else{//sutFotaPara.fotaStatus==FOTA_END
- //----正常工作
- if(sutWLanStatus.CmdNum!=sutWLanStatus.RecvCmdNum){
- if(++sutWLanStatus.CtrlReSendCt>5){
- sutWLanStatus.CtrlReSendCt=0;
- r=SendLogin2Server();
- if(r<=0){
- Disconnect2Server();
- WLNextStep(WL_STEP_CONNETING);
- continue;
- }
- }
- }else{
- if(sutWLanStatus.UnauthorizedCt>0){
- sutWLanStatus.UnauthorizedCt--;
- if(sutWLanStatus.UnauthorizedCt==0){
- sutWLanStatus.CmdNum=sutWLanStatus.RecvCmdNum+1;
- }
- }else{
- sutWLanStatus.Step=WL_STEP_UPLOAD;
- sutWLanStatus.CtrlIntervalSendCt=sutDeviceConfig.UploadInterval;
- }
- }
-
- }
- break;
- case WL_STEP_UPLOAD:
- os_dly_wait(1);
- r=WLanRecvProcess();
- if(r<0){
- sprintf(tracebuf,"[WL]Recv Err=%d\r\n",r);
- SlwTrace(DEBUG,tracebuf);
- Disconnect2Server();
- WLNextStep(WL_STEP_CONNETING);
- continue;
- }else if(r>0)continue;
- //控制一秒执行一次
- curTick=os_time_get();
- if(curTick>lastTick){
- lastTick=curTick+100;
- }else continue;
- if(TraceCt==1)SlwTrace(DEBUG,"[WL_UPLOAD]\r\n");
- if(++sutWLanStatus.CtrlIntervalSendCt>=sutDeviceConfig.UploadInterval || sutTestData.TcUpdated){
- sutWLanStatus.CtrlIntervalSendCt=0;
- sutWLanStatus.CmdNum++;
- sutTestData.TcUpdated=0;
- sutWLanStatus.CtrlReSendCt=3;
- }
-
- if(sutWLanStatus.CmdNum!=sutWLanStatus.RecvCmdNum){
- if(++sutWLanStatus.CtrlReSendCt>3){
- sutWLanStatus.CtrlReSendCt=0;
- SendCurPosition2Server();
- }
- }
- if(sutFotaPara.fotaStatus==FOTA_END && sutModemStatus.ServerOfflineCt<WLAN_CONNECT_AFTER_4G_TIME){
- Disconnect2Server();
- WLNextStep(WL_STEP_IDEL);
- }
- break;
- }
- }
- }
- void DhcpDisplayIP(void)
- {
- #if 0
- uint8_t ip[4];
- char tracebuf[30];
- getIPfromDHCP(ip);
- sprintf(tracebuf,"LC IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
- SlwTrace(DEBUG,tracebuf);
- getGWfromDHCP(ip);
- sprintf(tracebuf,"GW IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
- SlwTrace(DEBUG,tracebuf);
- getSNfromDHCP(ip);
- sprintf(tracebuf,"SN IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
- SlwTrace(DEBUG,tracebuf);
- getDNSfromDHCP(ip);
- sprintf(tracebuf,"DNS IP=%d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
- SlwTrace(DEBUG,tracebuf);
- #else
- char tracebuf[30];
- sprintf(tracebuf,"LC IP=%d.%d.%d.%d\r\n",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
- SlwTrace(DEBUG,tracebuf);
- sprintf(tracebuf,"GW IP=%d.%d.%d.%d\r\n",gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
- SlwTrace(DEBUG,tracebuf);
- sprintf(tracebuf,"SN IP=%d.%d.%d.%d\r\n",gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
- SlwTrace(DEBUG,tracebuf);
- sprintf(tracebuf,"DNS IP=%d.%d.%d.%d\r\n",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
- SlwTrace(DEBUG,tracebuf);
- #endif
- }
- /**************************************************************************************
- cb_ip_assign
- callback func when IP is assigned from DHCP server first
- ***************************************************************************************/
- void cb_ip_assign(void)
- {
- default_ip_assign();
- SlwTrace(DEBUG,"IP is assigned from DHCP!\r\n");
- ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
- DhcpDisplayIP();
- }
- /**************************************************************************************
- cb_ip_update
- callback func when IP is changed
- ***************************************************************************************/
- void cb_ip_update(void)
- {
- default_ip_update();
- SlwTrace(DEBUG,"IP is update from DHCP!\r\n");
- ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
- DhcpDisplayIP();
- }
- /**************************************************************************************
- cb_ip_conflict
- callback func when the assigned IP is conflict with others.
- ***************************************************************************************/
- void cb_ip_conflict(void)
- {
- SlwTrace(DEBUG,"IP Conflict!\r\n");
- default_ip_conflict();
- }
- /**
- * @brief Intialize the network information to be used in WIZCHIP
- * @retval None
- */
- void network_init(void)
- {
- char tacebuf[100];
- uint8_t tmpstr[6];
- //MAC
- memcpy(gWIZNETINFO.mac,sutProductPara.MAC,6);
- ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
- ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
- if(0!=memcmp(gWIZNETINFO.mac,sutProductPara.MAC,6)){
- W5500_RESET_CONFIG();
- os_dly_wait(20);
- memcpy(gWIZNETINFO.mac,sutProductPara.MAC,6);
- ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
- ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO);
- if(0!=memcmp(gWIZNETINFO.mac,sutProductPara.MAC,6)){
- SlwTrace(DEBUG,"W5500 Init fail!\r\nWiredLan Stop!\r\n");
- while(1){
- os_dly_wait(10000);
- }
- }
- }
- // Display Network Information
- ctlwizchip(CW_GET_ID,(void*)tmpstr);
- snprintf(tacebuf,sizeof(tacebuf),"\r\n=== %s NET CONF ===\r\n",(char*)tmpstr);
- SlwTrace(DEBUG,tacebuf);
- snprintf(tacebuf,sizeof(tacebuf),"MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],
- gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
- SlwTrace(DEBUG,tacebuf);
- snprintf(tacebuf,sizeof(tacebuf),"SIP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
- SlwTrace(DEBUG,tacebuf);
- snprintf(tacebuf,sizeof(tacebuf),"GAR: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
- SlwTrace(DEBUG,tacebuf);
- snprintf(tacebuf,sizeof(tacebuf),"SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
- SlwTrace(DEBUG,tacebuf);
- snprintf(tacebuf,sizeof(tacebuf),"DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
- SlwTrace(DEBUG,tacebuf);
- SlwTrace(DEBUG,"======================\r\n");
- }
- /*
- SOCK_IO_NONBLOCK;//SOCK_IO_BLOCK;
- */
- void SetIoMode(uint8_t iomode)
- {
- static uint8_t mode;
- mode=iomode;
- ctlsocket(SOCK_TCPS_APP1,CS_SET_IOMODE,&mode);
- }
- /*
- 通过有线通道发送TCP数据
- */
- int32_t WiredSendTcpData(uint8_t *data,uint16_t len)
- {
- char buf[30];
- int i,flag;
- int32_t r;
- sprintf(buf,">WD Send %d>\r\n",len);
- SlwTrace(DEBUG,buf);
- //-------------------------------
- //sutModemStatus.TcpSendTimeCt++;
- r=send(SOCK_TCPS_APP1,data,len);
- switch(r){
- case SOCKERR_TIMEOUT://- Timeout occurred \n
- SlwTrace(DEBUG,"err:SOCKERR_TIMEOUT\r\n");
- break;
- case SOCKERR_SOCKMODE://Invalid operation in the socket \n
- SlwTrace(DEBUG,"err:SOCKERR_SOCKMODE\r\n");
- break;
- case SOCKERR_SOCKNUM://Invalid socket number \n
- SlwTrace(DEBUG,"err:SOCKERR_SOCKNUM\r\n");
- break;
- case SOCKERR_DATALEN://zero data length \n
- SlwTrace(DEBUG,"err:SOCKERR_DATALEN\r\n");
- break;
- case SOCK_BUSY://Socket is busy.
- SlwTrace(DEBUG,"err:SOCK_BUSY\r\n");
- break;
- }
-
- if(sutDeviceConfig.DebugPrintEn != 0 && r>0){
- snprintf(buf,sizeof(buf),"Send[%d/%d] OK!\r\n",r,len);
- SlwTrace(DEBUG,buf);
- }
- return r;
- }
- /*
- 发登陆包
- 成功 >0
- 失败 <=0
- */
- int SendLogin2Server(void)
- {
- char buf[20];
- unsigned short len;
- len=PacketLogin(sutWLanStatus.CmdNum);
- sprintf(buf,"[WL]SLogin>>%d\r\n",len);
- SlwTrace(DEBUG,buf);
- if(len==0)return -20;
- return WiredSendTcpData(g_ucPropacketBuf,len);
- }
- /*
- 发当前点位置包
- 成功 >0
- 失败 <=0
- */
- int SendCurPosition2Server(void)
- {
- char buf[20];
- unsigned short i,len;
- SUT_POSITION sutPosition;
- memset(&sutPosition,0,sizeof(SUT_POSITION));
- if(sutGpsInfo.isGpsValid)sutPosition.Status.GNSS=1;
- else sutPosition.Status.GNSS=0;
- sutPosition.Status.ACC=1;
- sutPosition.Status.BD=sutGpsInfo.isBDWork;
- sutPosition.Status.GPS=sutGpsInfo.isGpsWork;
- sutPosition.Status.West=sutGpsInfo.West;
- sutPosition.Status.South=sutGpsInfo.South;
- sutPosition.Latitude=sutGpsInfo.Latitude;//22620926;//114035268; //114.035268
- sutPosition.Longitude=sutGpsInfo.Longitude; //114035268;//22620926; //22.620926
- sutPosition.Speed=sutGpsInfo.Speed;//0;
- sutPosition.Aspect=sutGpsInfo.Aspect;//123;
- sutPosition.Altitude=sutGpsInfo.Altitude;
- RTC_Get(sutPosition.Time);
- sutPosition.ExtraMsgId=0x3A;//RT231固定3A
- sutPosition.NetMode=2;//WL
- sutPosition.CSQ =0;
- sutPosition.Infrared=sutTestData.Infrared;
- sutPosition.Camera=sutTestData.Camera;
- for(i=0;i<sutTestData.TcLen;i++){
- sutPosition.TcData[i]=sutTestData.TcData[i];
- }
- sutPosition.TcLen=sutTestData.TcLen;
- sutPosition.ExtraMsgLen=sutPosition.TcLen+4;//
- len=PacketPosition(sutWLanStatus.CmdNum,sutPosition);
- if(len==0)return -20;
- sprintf(buf,"[WL]STest>>%d\r\n",len);
- SlwTrace(DEBUG,buf);
- return WiredSendTcpData(g_ucPropacketBuf,len);
- }
|