#include "main.h" #include "flash.h" #include "setting.h" #define EEROM_SETTING_ADDRESS(x) FLASH_SETTING_ADDRESS((x)+(FLASH_EEROMBASE_ADDRESS)) const stProductionInformation* pProductInfo = (const stProductionInformation*)(FLASH_SETTING_ADDRESS(INFORMATION_ADDRESS)); const stRunStatus* pRunStatus = (const stRunStatus*)(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS)); const stRunStatus* pRunStatus2 = (const stRunStatus*)(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS2)); const stStationConfig* pDwsConfig = (const stStationConfig*)(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS)); const stStationConfig* pDwsConfigBak = (const stStationConfig*)(FLASH_SETTING_ADDRESS(BAK_DW1000_ADDRESS)); void asciiDotAddressTouint32(uint8_t *hexAddr, int8_t *asciiDotAddress) { uint8_t i; // loop counter uint8_t cBufPos; // which char in the ASCII representation uint8_t byteNum; // which byte in the 32BitHexAddress uint8_t cBuf[4]; // character buffer byteNum = 3; cBufPos = 0; for (i = 0; i < strlen((char *)asciiDotAddress); i++) { // this will take care of the first 3 octets if (asciiDotAddress[i] == '.') { // we are at the end of the address octet cBuf[cBufPos] = 0; // terminate the string cBufPos = 0; // reset for the next char hexAddr[byteNum--] = (uint8_t)atoi((char *)cBuf); // convert the strint to an integer } else { cBuf[cBufPos++] = asciiDotAddress[i]; } } // handle the last octet // // we are at the end of the string with no . cBuf[cBufPos] = asciiDotAddress[i]; // terminate the string hexAddr[byteNum] = (uint8_t)atoi((char *)cBuf); // convert the strint to an integer } int isSettingEmpty(void) { if ((0xffff == pDwsConfig->address && 0xffff == pDwsConfig->panID) || (0 == pDwsConfig->address && 0 == pDwsConfig->panID)) { return 1; } return 0; } int isSettingError(void) { if ((0 == pDwsConfig->address && 0 == pDwsConfig->panID)) { return 1; } if ((0 == ethServer.sip[0]) && (0 == ethServer.sip[1]) && (0 == ethServer.sip[2]) && (0 == ethServer.sip[3])) { return 1; } if ((0xFF == ethServer.sip[0]) && (0xFF == ethServer.sip[1]) && (0xFF == ethServer.sip[2]) && (0xFF == ethServer.sip[3])) { return 1; } if ((0 == ethServer.port) || (0xFF == ethServer.port) || (0xFFFF == ethServer.port)) { return 1; } if ((0 == ethConfig.lip[0]) && (0 == ethConfig.lip[1]) && (0 == ethConfig.lip[2]) && (0 == ethConfig.lip[3])) { return 1; } if ((0xFF == ethConfig.lip[0]) && (0xFF == ethConfig.lip[1]) && (0xFF == ethConfig.lip[2]) && (0xFF == ethConfig.lip[3])) { return 1; } return 0; } int isSettingBakError(void) { if ((0 == pDwsConfigBak->address && 0 == pDwsConfigBak->panID)) { return 1; } return 0; } int isRunStatusEmpty(void) { return (0xff == pRunStatus->status[0] && 0xff == pRunStatus->status[1] && 0xff == pRunStatus->status[2] && 0xff == pRunStatus->status[3]); } int isRunStatusHold(void) { return (1 == pRunStatus->status[0] || 1 == pRunStatus2->status[0]); } int SaveInformation(const uint8_t* buf, int len) { if(len < sizeof(stProductionInformation)) { return -1; } int ret=0; if(len > INFORMATION_LENGTH) { len = INFORMATION_LENGTH; } memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, buf, len); ret = ProgramFlashPage(FLASH_SETTING_ADDRESS(INFORMATION_ADDRESS), debugBuffer, len); return ret; } int SaveRunStatus(uint8_t pos, uint8_t val) { memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, pRunStatus, RUN_STATUS_LENGTH); debugBuffer[pos] = val; return ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS), debugBuffer, RUN_STATUS_LENGTH); } int SaveRunStatus2(uint8_t pos, uint8_t val) { memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, pRunStatus2, RUN_STATUS_LENGTH); debugBuffer[pos] = val; return ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS2), debugBuffer, RUN_STATUS_LENGTH); } int SaveDwmConfig(const uint8_t* buf) { int ret = 0; //get settings dwsConfig.panID = buf[0] + buf[1]*256; dwsConfig.address = buf[2] + buf[3]*256; uint32_t u = buf[4] + (buf[5]<<8) + (buf[6]<<16) + (buf[7]<<24); memcpy(&dwsConfig.signal, &u, 4); dwsConfig.fixPower = buf[8] + ((uint32_t)buf[9] << 8) + ((uint32_t)buf[10] << 16) + ((uint32_t)buf[11] << 24); debugLen = 0; memset(debugBuffer, 0, DEBUG_BUF_LEN ); //DW1000 config memcpy(debugBuffer, &dwsConfig, EEROM_DW1000_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS), debugBuffer, EEROM_DW1000_LENGTH); return ret; } int SaveEthernetServerConfig(const uint8_t* buf) { int ret = 0; stServerAddress tmpServer; memcpy(&tmpServer, ðServer, sizeof(stServerAddress)); //get settings tmpServer.sip[0] = buf[0]; tmpServer.sip[1] = buf[1]; tmpServer.sip[2] = buf[2]; tmpServer.sip[3] = buf[3]; tmpServer.port = buf[4]; tmpServer.port += buf[5]*256; debugLen = 0; memset(debugBuffer, 0, DEBUG_BUF_LEN ); //eth server memcpy(debugBuffer, &tmpServer, EEROM_ETHSVR_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHSVR_ADDRESS), debugBuffer, EEROM_ETHSVR_LENGTH); return ret; } int SaveEthernetDbgServerConfig(const uint8_t* buf) { int ret = 0; stServerAddress tmpServer; memcpy(&tmpServer, ðDbgServer, sizeof(stServerAddress)); //get settings tmpServer.sip[0] = buf[0]; tmpServer.sip[1] = buf[1]; tmpServer.sip[2] = buf[2]; tmpServer.sip[3] = buf[3]; tmpServer.port = buf[4]; tmpServer.port += buf[5]*256; debugLen = 0; memset(debugBuffer, 0, DEBUG_BUF_LEN ); //eth dbg server memcpy(debugBuffer, &tmpServer, EEROM_ETHDBGSVR_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHDBGSVR_ADDRESS), debugBuffer, EEROM_ETHDBGSVR_LENGTH); return ret; } int SaveEthernetLocalConfig(const uint8_t* buf) { int ret = 0; stEthernetConfig tmpCfg; memcpy(&tmpCfg, ðConfig, sizeof(stEthernetConfig)); tmpCfg.state = buf[0]; tmpCfg.extMode = buf[1]; tmpCfg.lip[0] = buf[2]; tmpCfg.lip[1] = buf[3]; tmpCfg.lip[2] = buf[4]; tmpCfg.lip[3] = buf[5]; tmpCfg.sub[0] = buf[6]; tmpCfg.sub[1] = buf[7]; tmpCfg.sub[2] = buf[8]; tmpCfg.sub[3] = buf[9]; tmpCfg.gw[0] = buf[10]; tmpCfg.gw[1] = buf[11]; tmpCfg.gw[2] = buf[12]; tmpCfg.gw[3] = buf[13]; debugLen = 0; memset(debugBuffer, 0, DEBUG_BUF_LEN ); //DW1000 config memcpy(debugBuffer, &tmpCfg, EEROM_ETHCFG_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHCFG_ADDRESS), debugBuffer, EEROM_ETHCFG_LENGTH); return ret; } int SaveEthernetMacAddressConfig(const uint8_t* buf) { int ret = 0; ethConfig.mac[0] = buf[0]; ethConfig.mac[1] = buf[1]; ethConfig.mac[2] = buf[2]; ethConfig.mac[3] = buf[3]; ethConfig.mac[4] = buf[4]; ethConfig.mac[5] = buf[5]; debugLen = 0; memset(debugBuffer, 0, DEBUG_BUF_LEN ); //DW1000 config memcpy(debugBuffer, ðConfig, EEROM_ETHCFG_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_ETHCFG_ADDRESS), debugBuffer, EEROM_ETHCFG_LENGTH); return ret; } int SaveWifiApNameConfig(const uint8_t* buf, uint8_t len) { int ret = 0; if(len > EEROM_WIFIAP_LENGTH + EEROM_WIFICONFIG_LENGTH) { len = EEROM_WIFIAP_LENGTH + EEROM_WIFICONFIG_LENGTH; } len -= EEROM_WIFICONFIG_LENGTH; //config len=4 stWifiConfig tmpWifiConfig; tmpWifiConfig.type = buf[0];// tmpWifiConfig.mode = buf[1];// tmpWifiConfig.rfpower = buf[2];// tmpWifiConfig.dhcp = buf[3];// memset((void*)tmpWifiConfig.name,0,WIFIAPSSIDLEN); strncpy((char*)tmpWifiConfig.name, (const char*)(buf + 4),len); //save wifi password below strncpy((char*)tmpWifiConfig.password, (const char*)(buf + 4 + 32),len); memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, &tmpWifiConfig, EEROM_WIFICONFIG_LENGTH); debugLen = EEROM_WIFIAP_ADDRESS - EEROM_WIFICONFIG_ADDRESS; memcpy(debugBuffer + debugLen, tmpWifiConfig.name, EEROM_WIFIAP_LENGTH); debugLen = EEROM_WIFIAP_ADDRESS + EEROM_WIFIAP_LENGTH - EEROM_WIFICONFIG_ADDRESS; ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFICONFIG_ADDRESS), debugBuffer, debugLen); //save wifi password below memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, tmpWifiConfig.password, EEROM_WIFIPSWD_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFIPSWD_ADDRESS), debugBuffer, EEROM_WIFIPSWD_LENGTH); return ret; } int SaveWifiApPasswordConfig(const uint8_t* buf, uint8_t len) { int ret = 0; if(len > EEROM_WIFIPSWD_LENGTH) len = EEROM_WIFIPSWD_LENGTH; memset(wifiConfig.password,0,WIFIAPPSWDLEN); strncpy((char*)wifiConfig.password, (const char*)buf,len); memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, wifiConfig.password, EEROM_WIFIPSWD_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFIPSWD_ADDRESS), debugBuffer, EEROM_WIFIPSWD_LENGTH); return ret; } int SaveWifiServerConfig(const uint8_t* buf) { int ret = 0; //get settings memset(&wifiServer,0,sizeof(wifiServer)); ret = sprintf((char*)debugBuffer,"%d.%d.%d.%d",buf[0],buf[1],buf[2],buf[3]); strncpy((char*)wifiServer.ip,(const char*)debugBuffer,ret); ret = sprintf((char*)debugBuffer,"%d",(buf[4] + buf[5]*256)); strncpy((char*)wifiServer.port,(const char*)debugBuffer,ret); memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, &wifiServer, EEROM_WIFISVR_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFISVR_ADDRESS), debugBuffer, EEROM_WIFISVR_LENGTH); return ret; } int SaveWifiNetworkConfig(const uint8_t* buf) { int ret = 0; memset(&wifiNetwork,0,sizeof(wifiNetwork)); ret = sprintf((char*)debugBuffer, "%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3]); strncpy((char*)wifiNetwork.ip, (const char*)debugBuffer, ret); ret = sprintf((char*)debugBuffer, "%d.%d.%d.%d", buf[4], buf[5], buf[6], buf[7]); strncpy((char*)wifiNetwork.subnetmask, (const char*)debugBuffer, ret); ret = sprintf((char*)debugBuffer, "%d.%d.%d.%d", buf[8], buf[9], buf[10], buf[11]); strncpy((char*)wifiNetwork.gateway, (const char*)debugBuffer, ret); memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, &wifiNetwork, EEROM_WIFINET_LENGTH); ret = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_WIFINET_ADDRESS), debugBuffer, EEROM_WIFINET_LENGTH); return ret; } void ReadConfigFromEEROM(void) { int len = 0; debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH; memcpy(debugBuffer, (void*)EEROM_SETTING_ADDRESS(0), debugLen); //DW1000 config len = sizeof(dwsConfig);//EEROM_DW1000_LENGTH memcpy(&dwsConfig, debugBuffer + EEROM_DW1000_ADDRESS, len); //Eth Server IP&Port len = sizeof(ethServer);//EEROM_ETHSVR_LENGTH memcpy(ðServer, debugBuffer + EEROM_ETHSVR_ADDRESS, len); //Eth Server IP&Port len = sizeof(ethDbgServer);//EEROM_ETHDBGSVR_LENGTH memcpy(ðDbgServer, debugBuffer + EEROM_ETHDBGSVR_ADDRESS, len); //EthConfig len = sizeof(ethConfig);//EEROM_ETHCFG_LENGTH memcpy(ðConfig, debugBuffer + EEROM_ETHCFG_ADDRESS,len); //WIFI config memcpy(&wifiConfig, debugBuffer + EEROM_WIFICONFIG_ADDRESS , EEROM_WIFICONFIG_LENGTH); //WIFI AP SSID memcpy(wifiConfig.name, debugBuffer + EEROM_WIFIAP_ADDRESS, EEROM_WIFIAP_LENGTH); //WIFI AP Password memcpy(wifiConfig.password, debugBuffer + EEROM_WIFIPSWD_ADDRESS, EEROM_WIFIPSWD_LENGTH); //WIFI Server IP&Port len = sizeof(wifiServer);//EEROM_WIFISVR_LENGTH memcpy(&wifiServer, debugBuffer + EEROM_WIFISVR_ADDRESS, len); //WIFI network len = sizeof(wifiNetwork);//EEROM_WIFINET_LENGTH memcpy(&wifiNetwork, debugBuffer + EEROM_WIFINET_ADDRESS, len); } void ReadBakConfigFromEEROM(void) { int len = 0; debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH; memcpy(debugBuffer, (void*)FLASH_SETTING_ADDRESS(0), debugLen); //DW1000 config len = sizeof(dwsConfig);//EEROM_DW1000_LENGTH memcpy(&dwsConfig, debugBuffer + EEROM_DW1000_ADDRESS, len); //Eth Server IP&Port len = sizeof(ethServer);//EEROM_ETHSVR_LENGTH memcpy(ðServer, debugBuffer + EEROM_ETHSVR_ADDRESS, len); //Eth Server IP&Port len = sizeof(ethDbgServer);//EEROM_ETHDBGSVR_LENGTH memcpy(ðDbgServer, debugBuffer + EEROM_ETHDBGSVR_ADDRESS, len); //EthConfig len = sizeof(ethConfig);//EEROM_ETHCFG_LENGTH memcpy(ðConfig, debugBuffer + EEROM_ETHCFG_ADDRESS,len); //WIFI config memcpy(&wifiConfig, debugBuffer + EEROM_WIFICONFIG_ADDRESS , EEROM_WIFICONFIG_LENGTH); //WIFI AP SSID memcpy(wifiConfig.name, debugBuffer + EEROM_WIFIAP_ADDRESS, EEROM_WIFIAP_LENGTH); //WIFI AP Password memcpy(wifiConfig.password, debugBuffer + EEROM_WIFIPSWD_ADDRESS, EEROM_WIFIPSWD_LENGTH); //WIFI Server IP&Port len = sizeof(wifiServer);//EEROM_WIFISVR_LENGTH memcpy(&wifiServer, debugBuffer + EEROM_WIFISVR_ADDRESS, len); //WIFI network len = sizeof(wifiNetwork);//EEROM_WIFINET_LENGTH memcpy(&wifiNetwork, debugBuffer + EEROM_WIFINET_ADDRESS, len); } extern void DWM_SetDefault(void); extern void Wifi_SetDefault(void); extern void Ethernet_SetDefault(void); void WriteDefaultConfigToEEROM(void) { DWM_SetDefault(); Ethernet_SetDefault(); Wifi_SetDefault(); memset(debugBuffer, 0, DEBUG_BUF_LEN ); debugLen = 0; //DW1000 config memcpy(debugBuffer + EEROM_DW1000_ADDRESS, &dwsConfig, EEROM_DW1000_LENGTH); //Eth Server IP&Port memcpy(debugBuffer + EEROM_ETHSVR_ADDRESS, ðServer, EEROM_ETHSVR_LENGTH); //Eth DBG Server IP&Port memcpy(debugBuffer + EEROM_ETHDBGSVR_ADDRESS, ðDbgServer, EEROM_ETHDBGSVR_LENGTH); //EthConfig memcpy(debugBuffer + EEROM_ETHCFG_ADDRESS,ðConfig, EEROM_ETHCFG_LENGTH); //WIFI config memcpy(debugBuffer + EEROM_WIFICONFIG_ADDRESS , &wifiConfig, EEROM_WIFICONFIG_LENGTH); //WIFI AP SSID memcpy(debugBuffer + EEROM_WIFIAP_ADDRESS, wifiConfig.name, EEROM_WIFIAP_LENGTH); //WIFI AP Password memcpy(debugBuffer + EEROM_WIFIPSWD_ADDRESS, wifiConfig.password, EEROM_WIFIPSWD_LENGTH); //WIFI Server IP&Port memcpy(debugBuffer + EEROM_WIFISVR_ADDRESS, &wifiServer, EEROM_WIFISVR_LENGTH); //WIFI network memcpy(debugBuffer + EEROM_WIFINET_ADDRESS, &wifiNetwork, EEROM_WIFINET_LENGTH); debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH - EEROM_DW1000_ADDRESS; int retval = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS), debugBuffer + EEROM_DW1000_ADDRESS, debugLen); } void WriteDefaultRunStatusToEEROM(void) { memset(debugBuffer, 0, DEBUG_BUF_LEN ); memcpy(debugBuffer, pRunStatus, RUN_STATUS_LENGTH); memcpy(debugBuffer, pRunStatus2, RUN_STATUS_LENGTH); ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS), debugBuffer, RUN_STATUS_LENGTH); ProgramFlashPage(FLASH_SETTING_ADDRESS(RUN_STATUS_ADDRESS2), debugBuffer, RUN_STATUS_LENGTH); } void WriteConfigToEEROM(void) { memset(debugBuffer, 0, DEBUG_BUF_LEN ); debugLen = 0; //DW1000 config memcpy(debugBuffer + EEROM_DW1000_ADDRESS, &dwsConfig, EEROM_DW1000_LENGTH); //Eth Server IP&Port memcpy(debugBuffer + EEROM_ETHSVR_ADDRESS, ðServer, EEROM_ETHSVR_LENGTH); //Eth DBG Server IP&Port memcpy(debugBuffer + EEROM_ETHDBGSVR_ADDRESS, ðDbgServer, EEROM_ETHDBGSVR_LENGTH); //EthConfig memcpy(debugBuffer + EEROM_ETHCFG_ADDRESS,ðConfig, EEROM_ETHCFG_LENGTH); //WIFI config memcpy(debugBuffer + EEROM_WIFICONFIG_ADDRESS , &wifiConfig, EEROM_WIFICONFIG_LENGTH); //WIFI AP SSID memcpy(debugBuffer + EEROM_WIFIAP_ADDRESS, wifiConfig.name, EEROM_WIFIAP_LENGTH); //WIFI AP Password memcpy(debugBuffer + EEROM_WIFIPSWD_ADDRESS, wifiConfig.password, EEROM_WIFIPSWD_LENGTH); //WIFI Server IP&Port memcpy(debugBuffer + EEROM_WIFISVR_ADDRESS, &wifiServer, EEROM_WIFISVR_LENGTH); //WIFI network memcpy(debugBuffer + EEROM_WIFINET_ADDRESS, &wifiNetwork, EEROM_WIFINET_LENGTH); debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH - EEROM_DW1000_ADDRESS; int retval = ProgramFlashPage(EEROM_SETTING_ADDRESS(EEROM_DW1000_ADDRESS), debugBuffer + EEROM_DW1000_ADDRESS, debugLen); } void WriteBakConfigToEEROM(void) { memset(debugBuffer, 0, DEBUG_BUF_LEN ); debugLen = 0; //DW1000 config memcpy(debugBuffer + EEROM_DW1000_ADDRESS, &dwsConfig, EEROM_DW1000_LENGTH); //Eth Server IP&Port memcpy(debugBuffer + EEROM_ETHSVR_ADDRESS, ðServer, EEROM_ETHSVR_LENGTH); //Eth DBG Server IP&Port memcpy(debugBuffer + EEROM_ETHDBGSVR_ADDRESS, ðDbgServer, EEROM_ETHDBGSVR_LENGTH); //EthConfig memcpy(debugBuffer + EEROM_ETHCFG_ADDRESS,ðConfig, EEROM_ETHCFG_LENGTH); //WIFI config memcpy(debugBuffer + EEROM_WIFICONFIG_ADDRESS , &wifiConfig, EEROM_WIFICONFIG_LENGTH); //WIFI AP SSID memcpy(debugBuffer + EEROM_WIFIAP_ADDRESS, wifiConfig.name, EEROM_WIFIAP_LENGTH); //WIFI AP Password memcpy(debugBuffer + EEROM_WIFIPSWD_ADDRESS, wifiConfig.password, EEROM_WIFIPSWD_LENGTH); //WIFI Server IP&Port memcpy(debugBuffer + EEROM_WIFISVR_ADDRESS, &wifiServer, EEROM_WIFISVR_LENGTH); //WIFI network memcpy(debugBuffer + EEROM_WIFINET_ADDRESS, &wifiNetwork, EEROM_WIFINET_LENGTH); debugLen = EEROM_WIFINET_ADDRESS + EEROM_WIFINET_LENGTH - EEROM_DW1000_ADDRESS; int retval = ProgramFlashPage(FLASH_SETTING_ADDRESS(BAK_DW1000_ADDRESS), debugBuffer + EEROM_DW1000_ADDRESS, debugLen); } int GetInformation(uint8_t* retbuf, uint32_t* len) { int size = sizeof(pProductInfo->serialID); memcpy(retbuf, pProductInfo->serialID, size); #if defined(STM32F405_BOOTLOADER) retbuf[size++] = BSP_BL_VERSION() & 0xff; retbuf[size++] = (BSP_BL_VERSION()>>8)&0xff; retbuf[size++] = (BSP_BL_VERSION()>>16)&0xff; retbuf[size++] = (BSP_BL_VERSION()>>24)&0xff; #else retbuf[size++] = BSP_APP_VERSION() & 0xff; retbuf[size++] = (BSP_APP_VERSION()>>8)&0xff; retbuf[size++] = (BSP_APP_VERSION()>>16)&0xff; retbuf[size++] = (BSP_APP_VERSION()>>24)&0xff; #endif *len = size; return 0; } uint8_t GetRunStatus(uint8_t pos) { if (pos >=0 && pos < RUN_STATUS_LENGTH) { return pRunStatus->status[pos]; } return 0; } int GetTimeSetResult(uint8_t* retbuf, uint32_t* len) { retbuf[0] = 1; retbuf[0] = 1; *len = 2; return 0; } int GetCurrentTime(uint8_t* retbuf, uint32_t* len) { retbuf[0] = stDateTime.year; retbuf[1] = stDateTime.month; retbuf[2] = stDateTime.day; retbuf[3] = stDateTime.hour; retbuf[4] = stDateTime.minute; retbuf[5] = stDateTime.second; *len = 6; return 0; } int GetRangeConfiguration(uint8_t* retbuf, uint32_t* len) { int index = 0; //get settings retbuf[index++] = (uint8_t)(dwsConfig.panID); retbuf[index++] = (uint8_t)(dwsConfig.panID>>8); retbuf[index++] = (uint8_t)(dwsConfig.address); retbuf[index++] = (uint8_t)(dwsConfig.address>>8); uint32_t u = 0; memcpy(&u, &dwsConfig.signal, 4); retbuf[index++] = (uint8_t)(u); retbuf[index++] = (uint8_t)(u>>8); retbuf[index++] = (uint8_t)(u>>16); retbuf[index++] = (uint8_t)(u>>24); retbuf[index++] = (uint8_t)(dwsConfig.fixPower & 0xff); retbuf[index++] = (uint8_t)(dwsConfig.fixPower >> 8); retbuf[index++] = (uint8_t)(dwsConfig.fixPower >> 16); retbuf[index++] = (uint8_t)(dwsConfig.fixPower >> 24); *len = index; return 0; } int GetEthernetServer(uint8_t* retbuf, uint32_t* len) { retbuf[0] = ethServer.sip[0]; retbuf[1] = ethServer.sip[1]; retbuf[2] = ethServer.sip[2]; retbuf[3] = ethServer.sip[3]; retbuf[4] = ethServer.port & 0xff; retbuf[5] = (ethServer.port>>8) & 0xff; *len = 6; return 0; } int GetEthernetDbgServer(uint8_t* retbuf, uint32_t* len) { retbuf[0] = ethDbgServer.sip[0]; retbuf[1] = ethDbgServer.sip[1]; retbuf[2] = ethDbgServer.sip[2]; retbuf[3] = ethDbgServer.sip[3]; retbuf[4] = ethDbgServer.port & 0xff; retbuf[5] = (ethDbgServer.port>>8) & 0xff; *len = 6; return 0; } int GetEthernetLocal(uint8_t* retbuf, uint32_t* len) { retbuf[0] = ethConfig.state; retbuf[1] = ethConfig.extMode; retbuf[2] = ethConfig.lip[0]; retbuf[3] = ethConfig.lip[1]; retbuf[4] = ethConfig.lip[2]; retbuf[5] = ethConfig.lip[3]; retbuf[6] = ethConfig.sub[0]; retbuf[7] = ethConfig.sub[1]; retbuf[8] = ethConfig.sub[2]; retbuf[9] = ethConfig.sub[3]; retbuf[10] = ethConfig.gw[0]; retbuf[11] = ethConfig.gw[1]; retbuf[12] = ethConfig.gw[2]; retbuf[13] = ethConfig.gw[3]; *len = 14; return 0; } int GetEthernetMAC(uint8_t* retbuf, uint32_t* len) { retbuf[0] = ethConfig.mac[0]; retbuf[1] = ethConfig.mac[1]; retbuf[2] = ethConfig.mac[2]; retbuf[3] = ethConfig.mac[3]; retbuf[4] = ethConfig.mac[4]; retbuf[5] = ethConfig.mac[5]; *len = 6; return 0; } int GetWifiConfigSSID(uint8_t* retbuf, uint32_t* len) { int index = 0; //config len=4 retbuf[index++] = wifiConfig.type;// retbuf[index++] = wifiConfig.mode;// retbuf[index++] = wifiConfig.rfpower;// retbuf[index++] = wifiConfig.dhcp;// memcpy((void*)(retbuf+index), (const void*)wifiConfig.name, WIFIAPSSIDLEN); index += WIFIAPSSIDLEN; memcpy((void*)(retbuf+index), (const void*)wifiConfig.password, WIFIAPPSWDLEN); index += WIFIAPPSWDLEN; *len = index; return 0; } int GetWifiPassword(uint8_t* retbuf, uint32_t* len) { memcpy((void*)retbuf, wifiConfig.password, WIFIAPPSWDLEN); *len = WIFIAPPSWDLEN; return 0; } int GetWifiServer(uint8_t* retbuf, uint32_t* len) { int index = 0; uConverterUint2Bytes converter; asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiServer.ip); //config ip=4 retbuf[index++] = converter.u8Data[3];// retbuf[index++] = converter.u8Data[2];// retbuf[index++] = converter.u8Data[1];// retbuf[index++] = converter.u8Data[0];// converter.u32Data = atoi((const char*)wifiServer.port); //config port=2 retbuf[index++] = converter.u8Data[0];// retbuf[index++] = converter.u8Data[1];// *len = index; return 0; } int GetWifiLocal(uint8_t* retbuf, uint32_t* len) { int index = 0; uConverterUint2Bytes converter; asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiNetwork.ip); //config ip=4 retbuf[index++] = converter.u8Data[3];// retbuf[index++] = converter.u8Data[2];// retbuf[index++] = converter.u8Data[1];// retbuf[index++] = converter.u8Data[0];// asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiNetwork.subnetmask); //config subnetmask=4 retbuf[index++] = converter.u8Data[3];// retbuf[index++] = converter.u8Data[2];// retbuf[index++] = converter.u8Data[1];// retbuf[index++] = converter.u8Data[0];// asciiDotAddressTouint32((uint8_t *)&converter, (int8_t*)wifiNetwork.gateway); //config gateway=4 retbuf[index++] = converter.u8Data[3];// retbuf[index++] = converter.u8Data[2];// retbuf[index++] = converter.u8Data[1];// retbuf[index++] = converter.u8Data[0];// *len = index; return 0; } int GetResetSignResponse(uint8_t* retbuf, uint32_t* len) { int index = 0; retbuf[index++] = (uint8_t)(dwsConfig.address | GATEWAY_ANCHOR_ADDR); retbuf[index++] = (uint8_t)((dwsConfig.address | GATEWAY_ANCHOR_ADDR)>>8); retbuf[index++] = 0xff;// retbuf[index++] = 0xff;// retbuf[index++] = 0xff;// retbuf[index++] = 0xff;// *len = index; return 0; } int BuildResponse(uint32_t cmd, uint8_t* retbuf, uint32_t* retlen) { uint32_t datalen; uint32_t len; int ret; //Build response memset(retbuf, 0, MAX_MESSAGE_LEN); retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_FLAG; switch(cmd) { case CFG_COMMAND_GETINFORM: ret = GetInformation(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_TIMESET: ret = GetTimeSetResult(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETTIME: ret = GetCurrentTime(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGRANGE: ret = GetRangeConfiguration(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGETHDBGSERVER: ret = GetEthernetDbgServer(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGETHSERVER: ret = GetEthernetServer(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGETHLOCAL: ret = GetEthernetLocal(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGETHMAC: ret = GetEthernetMAC(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGAPSSID: ret = GetWifiConfigSSID(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGAPPSWD: ret = GetWifiPassword(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGWIFISERVER: ret = GetWifiServer(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_GETCONFIGWIFILOCAL: ret = GetWifiLocal(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; case CFG_COMMAND_RESETSIGN: ret = GetResetSignResponse(retbuf + PROTOCAL_DATASTART_INDEX, &datalen); break; default: *retlen = 0; return -1; } len = datalen + PROTOCAL_CHECKSUM_LENGTH + 1; retbuf[PROTOCAL_LENLOW_INDEX] = len; retbuf[PROTOCAL_LENHIGH_INDEX] = len>>8; retbuf[PROTOCAL_CMD_INDEX] = cmd; uint32_t remainder = datalen % 8; if (remainder) { remainder = 8 - remainder; } while (remainder) { len++; datalen++; remainder--; } if (dwsConfig.signal.encrypt && CFG_COMMAND_RESETSIGN != cmd) { retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_ENCRYPT_FLAG; EncryptTEA(retbuf, PROTOCAL_DATASTART_INDEX, datalen); } retbuf[PROTOCAL_LENLOW_INDEX] = (uint8_t)len; retbuf[PROTOCAL_LENHIGH_INDEX] = (uint8_t)(len>>8); uint16_t checkval = CalculateCRC16(retbuf, datalen + PROTOCAL_HEAD_LENGTH); retbuf[datalen + PROTOCAL_HEAD_LENGTH] = checkval; retbuf[datalen + PROTOCAL_HEAD_LENGTH + 1] = checkval>>8; *retlen = datalen + PROTOCAL_HEAD_LENGTH + PROTOCAL_CHECKSUM_LENGTH; return ret; } int BuildSettingResponse(uint32_t cmd, int32_t status, uint8_t* retbuf, uint32_t* retlen) { int datalen = 2; int len = datalen + PROTOCAL_CHECKSUM_LENGTH + 1; //Build response memset(retbuf, 0, MAX_MESSAGE_LEN); retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_FLAG; retbuf[PROTOCAL_LENLOW_INDEX] = len; retbuf[PROTOCAL_LENHIGH_INDEX] = len>>8; retbuf[PROTOCAL_CMD_INDEX] = CFG_COMMAND_SETTINGRESPONES; retbuf[PROTOCAL_DATASTART_INDEX+0] = cmd; retbuf[PROTOCAL_DATASTART_INDEX+1] = status; uint32_t remainder = datalen % 8; if (remainder) { remainder = 8 - remainder; } while (remainder) { len++; datalen++; remainder--; } if (dwsConfig.signal.encrypt) { retbuf[PROTOCAL_START_INDEX] = PROTOCAL_HEAD_ENCRYPT_FLAG; EncryptTEA(retbuf, PROTOCAL_DATASTART_INDEX, datalen); } retbuf[PROTOCAL_LENLOW_INDEX] = (uint8_t)len; retbuf[PROTOCAL_LENHIGH_INDEX] = (uint8_t)(len>>8); uint16_t checkval = CalculateCRC16(retbuf, datalen + PROTOCAL_HEAD_LENGTH); retbuf[datalen + PROTOCAL_HEAD_LENGTH] = checkval; retbuf[datalen + PROTOCAL_HEAD_LENGTH + 1] = checkval>>8; *retlen = datalen + PROTOCAL_HEAD_LENGTH + PROTOCAL_CHECKSUM_LENGTH; return 0; }