ota project related from Wang_guozhu
it is OK
This commit is contained in:
@@ -21,8 +21,12 @@ CONFIG_BSP_USING_LPUART3=y
|
||||
CONFIG_SERIAL_BUS_NAME_3="uart3"
|
||||
CONFIG_SERIAL_DRV_NAME_3="uart3_drv"
|
||||
CONFIG_SERIAL_3_DEVICE_NAME_0="uart3_dev3"
|
||||
|
||||
# CONFIG_BSP_USING_LPUART4 is not set
|
||||
# CONFIG_BSP_USING_LPUART8 is not set
|
||||
CONFIG_BSP_USING_LPUART8=y
|
||||
CONFIG_SERIAL_BUS_NAME_8="uart8"
|
||||
CONFIG_SERIAL_DRV_NAME_8="uart8_drv"
|
||||
CONFIG_SERIAL_8_DEVICE_NAME_0="uart8_dev8"
|
||||
# CONFIG_BSP_USING_CH438 is not set
|
||||
CONFIG_BSP_USING_GPIO=y
|
||||
CONFIG_PIN_BUS_NAME="pin"
|
||||
@@ -63,7 +67,7 @@ CONFIG___STACKSIZE__=4096
|
||||
#
|
||||
CONFIG_RESOURCES_SERIAL=y
|
||||
CONFIG_SERIAL_USING_DMA=y
|
||||
CONFIG_SERIAL_RB_BUFSZ=128
|
||||
CONFIG_SERIAL_RB_BUFSZ=256
|
||||
CONFIG_RESOURCES_PIN=y
|
||||
|
||||
#
|
||||
@@ -125,7 +129,7 @@ CONFIG_ZOMBIE_KTASK_STACKSIZE=2048
|
||||
#
|
||||
CONFIG_KERNEL_CONSOLE=y
|
||||
CONFIG_KERNEL_BANNER=y
|
||||
CONFIG_KERNEL_CONSOLEBUF_SIZE=128
|
||||
CONFIG_KERNEL_CONSOLEBUF_SIZE=256
|
||||
|
||||
#
|
||||
# Kernel Hook
|
||||
@@ -231,6 +235,7 @@ CONFIG_ADD_XIZI_FEATURES=y
|
||||
# CONFIG_SUPPORT_KNOWING_FRAMEWORK is not set
|
||||
# CONFIG_SUPPORT_CONTROL_FRAMEWORK is not set
|
||||
|
||||
|
||||
#
|
||||
# Security
|
||||
#
|
||||
|
||||
@@ -507,7 +507,7 @@ static status_t flexspi_config_mcr1(uint32_t instance, flexspi_mem_config_t *con
|
||||
// Configure MCR1
|
||||
FLEXSPI->MCR1 = FLEXSPI_MCR1_SEQWAIT(seqWaitTicks) | FLEXSPI_MCR1_AHBBUSWAIT(ahbBusWaitTicks);
|
||||
|
||||
return kStatus_Success;
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
@@ -647,14 +647,14 @@ uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len)
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: FLASH_Read
|
||||
* 函 数 名: FLASH_ReadBuf
|
||||
* 功能描述: 读Flash内容
|
||||
* 形 参: addr:读取区域起始地址
|
||||
buf:数据存储区
|
||||
len:要读取的字节数
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
|
||||
status_t FLASH_ReadBuf(uint32_t addr, uint32_t *buf, uint32_t len)
|
||||
{
|
||||
status_t status;
|
||||
flexspi_xfer_t flashXfer;
|
||||
@@ -678,206 +678,28 @@ status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len)
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: flash_erase
|
||||
* 功能描述: 擦除Flash指定长度的空间
|
||||
* 形 参: addr:擦除区域起始地址
|
||||
byte_cnt:要擦除的字节数,以4k字节为最小擦除单位
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
* 注 释: 不满4k字节的,也需要擦除掉4k字节
|
||||
*******************************************************************************/
|
||||
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt)
|
||||
{
|
||||
uint32_t addr;
|
||||
status_t status;
|
||||
|
||||
addr = start_addr;
|
||||
while(addr < (byte_cnt + start_addr))
|
||||
{
|
||||
status = FLASH_EraseSector(addr);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
addr += FLASH_GetSectorSize();
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: flash_write
|
||||
* 功能描述: 在指定的flash起始地址写入指定长度的数据
|
||||
* 形 参: addr:写入区域起始地址
|
||||
buf:数据存储区
|
||||
byte_cnt:要写入的字节数
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt)
|
||||
{
|
||||
uint32_t size;
|
||||
status_t status;
|
||||
while(byte_cnt > 0)
|
||||
{
|
||||
size = byte_cnt > FLASH_PAGE_SIZE ? FLASH_PAGE_SIZE : byte_cnt;
|
||||
status = FLASH_WritePage(start_addr, (void *)buf, size);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
start_addr += size;
|
||||
buf += size;
|
||||
byte_cnt -= size;
|
||||
}
|
||||
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: flash_read
|
||||
* 功能描述: 读Flash内容
|
||||
* 形 参: addr:读取区域起始地址
|
||||
buf:数据存储区
|
||||
len:要读取的字节数
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len)
|
||||
{
|
||||
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
|
||||
if((addr >= 0x60000000) && (addr < 0x61000000))
|
||||
{
|
||||
return FLASH_Read(addr, (void *)buf, len);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
void* result = memcpy(buf, (void*)addr, len);
|
||||
if(result == NULL)
|
||||
{
|
||||
return (status_t)kStatus_Fail;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: flash_copy
|
||||
* 功能描述: 实现flash数据在分区之间的拷贝
|
||||
* 形 参: srcAddr:源flash的起始地址
|
||||
dstAddr:目标flash的起始地址;
|
||||
imageSize:要拷贝的flash空间大小,单位为字节
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
|
||||
{
|
||||
uint32_t PageNum, Remain, i;
|
||||
status_t status;
|
||||
|
||||
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
|
||||
{
|
||||
return (status_t)kStatus_Fail;
|
||||
}
|
||||
|
||||
status = flash_erase(dstAddr,imageSize);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
|
||||
return status;
|
||||
}
|
||||
|
||||
PageNum = imageSize/FLASH_PAGE_SIZE;
|
||||
Remain = imageSize%FLASH_PAGE_SIZE;
|
||||
|
||||
for(i=0;i<PageNum;i++)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
if(Remain)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
status = flash_read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
status = flash_write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: NOR_FLASH_Erase
|
||||
* 功能描述: 以扇区为擦除单位擦除Flash指定长度的空间,最终擦除的字节可能大于imageSize
|
||||
* 形 参: addr:擦除区域起始地址
|
||||
imageSize:要擦除的字节数
|
||||
* 返 回 值: None
|
||||
*******************************************************************************/
|
||||
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize)
|
||||
{
|
||||
uint16_t i;
|
||||
uint32_t sectorNum = (imageSize%SECTOR_SIZE != 0)? (imageSize/SECTOR_SIZE + 1):(imageSize/SECTOR_SIZE);
|
||||
|
||||
for(i=0;i<sectorNum;i++)
|
||||
{
|
||||
status_t status = FLASH_EraseSector(app_base_addr+i*SECTOR_SIZE);
|
||||
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: NorFlash_Write_PageProgram
|
||||
* 功能描述: 写入Flash指定长度的数据
|
||||
* 形 参: pBuffer:数据存储区
|
||||
WriteAddr:写入区域起始地址
|
||||
NumByteToWrite:要写入的字节数(最大256)
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
* 注 释: 在指定地址开始写入最大256字节的数据
|
||||
*******************************************************************************/
|
||||
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
{
|
||||
uint8_t temp_data[256] = {0xff};
|
||||
|
||||
memcpy(temp_data,pBuffer,NumByteToWrite);
|
||||
|
||||
status_t status = FLASH_WritePage(WriteAddr,(void *)temp_data,FLASH_PAGE_SIZE);
|
||||
if (status != kStatus_Success)
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
|
||||
}
|
||||
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
@@ -887,13 +709,14 @@ void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t Num
|
||||
* 形 参: pBuffer:数据存储区
|
||||
WriteAddr:开始写入的地址(24bit)
|
||||
NumByteToWrite:要写入的字节数(最大65535)
|
||||
* 返 回 值: 无
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
* 注 释: 必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
|
||||
具有自动换页功能,在指定地址开始写入指定长度的数据,但是要确保地址不越界!
|
||||
*******************************************************************************/
|
||||
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
status_t NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
{
|
||||
uint16_t pageRemain;
|
||||
uint16_t pageRemain;
|
||||
status_t status;
|
||||
|
||||
pageRemain = 256 - WriteAddr%256;//单页剩余的字节数
|
||||
|
||||
@@ -904,7 +727,12 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
|
||||
|
||||
while(1)
|
||||
{
|
||||
NorFlash_Write_PageProgram(pBuffer,WriteAddr,pageRemain);
|
||||
status = NorFlash_Write_PageProgram(pBuffer,WriteAddr,pageRemain);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
|
||||
return status;
|
||||
}
|
||||
if(NumByteToWrite == pageRemain)
|
||||
{
|
||||
break;//写入结束了
|
||||
@@ -925,25 +753,54 @@ void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByte
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: NorFlash_Write
|
||||
* 函 数 名: Flash_Erase
|
||||
* 功能描述: 以扇区为擦除单位擦除Flash指定长度的空间,最终擦除的字节可能大于imageSize
|
||||
* 形 参: start_addr:擦除区域起始地址
|
||||
imageSize:要擦除的字节数
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize)
|
||||
{
|
||||
uint16_t i;
|
||||
status_t status;
|
||||
uint32_t sectorNum = (imageSize%SECTOR_SIZE != 0)? (imageSize/SECTOR_SIZE + 1):(imageSize/SECTOR_SIZE);
|
||||
|
||||
for(i=0;i<sectorNum;i++)
|
||||
{
|
||||
status = FLASH_EraseSector(start_addr+i*SECTOR_SIZE);
|
||||
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Flash_Write
|
||||
* 功能描述: 写入W25QXX在指定地址开始写入指定长度的数据
|
||||
* 形 参: pBuffer:数据存储区
|
||||
WriteAddr:开始写入的地址(24bit)
|
||||
WriteAddr:开始写入的地址
|
||||
NumByteToWrite:要写入的字节数(最大65535)
|
||||
* 返 回 值: None
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
* 注 释: 该函数带擦除操作
|
||||
*******************************************************************************/
|
||||
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite)
|
||||
{
|
||||
uint32_t secPos;
|
||||
uint16_t secOff;
|
||||
uint16_t secRemain;
|
||||
uint16_t i;
|
||||
uint8_t *NorFlash_BUF = 0;
|
||||
status_t status;
|
||||
|
||||
NorFlash_BUF = NorFlash_BUFFER;//RAM缓冲区4K
|
||||
|
||||
@@ -959,7 +816,11 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
}
|
||||
while(1)
|
||||
{
|
||||
FLASH_Read(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
|
||||
status = FLASH_ReadBuf(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
for(i=0;i<secRemain;i++)//校验数据
|
||||
{
|
||||
if(NorFlash_BUF[secOff+i] != 0xFF)
|
||||
@@ -969,16 +830,28 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
}
|
||||
if(i < secRemain)//需要擦除
|
||||
{
|
||||
FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
|
||||
status = FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
for(i=0;i<secRemain;i++)//复制
|
||||
{
|
||||
NorFlash_BUF[i+secOff] = pBuffer[i];
|
||||
}
|
||||
NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
|
||||
status = NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
|
||||
status = NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
if(NumByteToWrite == secRemain)
|
||||
@@ -1003,33 +876,138 @@ void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Flash_Read
|
||||
* 功能描述: 读Flash内容
|
||||
* 形 参: addr:读取区域起始地址
|
||||
buf:数据存储区
|
||||
len:要读取的字节数
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len)
|
||||
{
|
||||
/* For FlexSPI Memory ReadBack, use IP Command instead of AXI command for security */
|
||||
if((addr >= 0x60000000) && (addr < 0x61000000))
|
||||
{
|
||||
return FLASH_ReadBuf(addr, (void *)buf, len);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
void* result = memcpy(buf, (void*)addr, len);
|
||||
if(result == NULL)
|
||||
{
|
||||
return (status_t)kStatus_Fail;
|
||||
}
|
||||
else
|
||||
{
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: Flash_Copy
|
||||
* 功能描述: 实现flash数据在分区之间的拷贝
|
||||
* 形 参: srcAddr:源flash的起始地址
|
||||
dstAddr:目标flash的起始地址;
|
||||
imageSize:要拷贝的flash空间大小,单位为字节
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
status_t Flash_Copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize)
|
||||
{
|
||||
uint32_t PageNum, Remain, i;
|
||||
status_t status;
|
||||
|
||||
if((srcAddr == dstAddr) || imageSize > APP_FLASH_SIZE)
|
||||
{
|
||||
return (status_t)kStatus_Fail;
|
||||
}
|
||||
|
||||
status = Flash_Erase(dstAddr,imageSize);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Erase flash 0x%08x failure !\r\n",dstAddr);
|
||||
return status;
|
||||
}
|
||||
|
||||
PageNum = imageSize/FLASH_PAGE_SIZE;
|
||||
Remain = imageSize%FLASH_PAGE_SIZE;
|
||||
|
||||
for(i=0;i<PageNum;i++)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
status = Flash_Read(srcAddr + i*FLASH_PAGE_SIZE, buffer, sizeof(buffer));
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
status = Flash_Write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, FLASH_PAGE_SIZE);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
if(Remain)
|
||||
{
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
status = Flash_Read(srcAddr + i*FLASH_PAGE_SIZE, buffer, Remain);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Read flash 0x%08x failure !\r\n", srcAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
status = Flash_Write(dstAddr+ i*FLASH_PAGE_SIZE, buffer, Remain);
|
||||
if(status != kStatus_Success)
|
||||
{
|
||||
KPrintf("Write flash 0x%08x failure !\r\n", dstAddr + i*FLASH_PAGE_SIZE);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: NOR_FLASH_Write
|
||||
* 功能描述: 写入W25QXX在指定地址开始写入指定长度的数据
|
||||
* 形 参: FlashAddress:用于存储当前写入Flash地址的指针,写入过程中会移动
|
||||
Data:要写入数据存储区
|
||||
DataLength:要写入的字节数
|
||||
* 返 回 值: 0
|
||||
* 返 回 值: 如果函数执行成功,状态值为 kStatus_Success,否则状态值为其他错误码
|
||||
*******************************************************************************/
|
||||
#ifndef USE_HIGHT_SPEED_TRANS
|
||||
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
|
||||
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength)
|
||||
{
|
||||
status_t status;
|
||||
uint32_t WriteAddr;
|
||||
WriteAddr = *FlashAddress;
|
||||
NorFlash_Write(Data,WriteAddr,DataLength);
|
||||
status = Flash_Write(WriteAddr,Data,DataLength);
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
*FlashAddress += DataLength;
|
||||
return 0;
|
||||
return (status_t)kStatus_Success;
|
||||
}
|
||||
#else
|
||||
uint8_t packetNum = 0;
|
||||
uint32_t dataLen = 0;
|
||||
uint32_t WriteAddr;
|
||||
uint8_t dataBuff[5*1024];
|
||||
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag)
|
||||
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag)
|
||||
{
|
||||
status_t status;
|
||||
if(!doneFlag)
|
||||
{
|
||||
memcpy(&dataBuff[dataLen],Data,DataLength);
|
||||
@@ -1042,7 +1020,11 @@ uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
|
||||
|
||||
if(dataLen>=SECTOR_SIZE)
|
||||
{
|
||||
NorFlash_Write(dataBuff,WriteAddr,dataLen);
|
||||
status = Flash_Write(WriteAddr,dataBuff,dataLen);
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
packetNum = 0;
|
||||
dataLen = 0;
|
||||
}
|
||||
@@ -1050,10 +1032,14 @@ uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
|
||||
}
|
||||
else
|
||||
{
|
||||
NorFlash_Write(dataBuff,WriteAddr,dataLen);
|
||||
status = Flash_Write(WriteAddr,dataBuff,dataLen);
|
||||
if (status != kStatus_Success)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
packetNum = 0;
|
||||
dataLen = 0;
|
||||
}
|
||||
return (0);
|
||||
return (status_t)kStatus_Success;;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -280,7 +280,7 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
|
||||
}
|
||||
/* erase user application area */
|
||||
|
||||
NOR_FLASH_Erase(addr,size);
|
||||
Flash_Erase(addr,size);
|
||||
Send_Byte(ACK);
|
||||
Send_Byte(CRC16);
|
||||
}
|
||||
@@ -300,9 +300,9 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
|
||||
|
||||
/* Write received data in Flash */
|
||||
#ifndef USE_HIGHT_SPEED_TRANS
|
||||
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == 0)
|
||||
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length) == kStatus_Success)
|
||||
#else
|
||||
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == 0)
|
||||
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t)packet_length, 0) == kStatus_Success)
|
||||
#endif
|
||||
{
|
||||
Send_Byte(ACK);
|
||||
@@ -349,7 +349,10 @@ int32_t Ymodem_Receive(uint8_t *buf, const uint32_t addr)
|
||||
}
|
||||
}
|
||||
#ifdef USE_HIGHT_SPEED_TRANS
|
||||
NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,1);
|
||||
if(NOR_FLASH_Write(&flashdestination, buf, (uint16_t) packet_length,1) != kStatus_Success)
|
||||
{
|
||||
return -4;
|
||||
}
|
||||
#endif
|
||||
return (int32_t)size;
|
||||
}
|
||||
@@ -370,13 +373,12 @@ int32_t SerialDownload(const uint32_t addr)
|
||||
Size = Ymodem_Receive(&tab_1024[0], addr);
|
||||
if(Size > 0)
|
||||
{
|
||||
Serial_PutString("\n\n\r Programming Completed Successfully!\n\r--------------------------------\r\n Name: ");
|
||||
Serial_PutString("\n\n\rProgramming Completed Successfully!\n\r\r\nName: ");
|
||||
Serial_PutString(FileName);
|
||||
Int2Str(Number, Size);
|
||||
Serial_PutString("\n\r Size: ");
|
||||
Serial_PutString("\n\rSize: ");
|
||||
Serial_PutString(Number);
|
||||
Serial_PutString(" Bytes\r\n");
|
||||
Serial_PutString("-------------------\n");
|
||||
}
|
||||
else if(Size == -1)
|
||||
{
|
||||
|
||||
@@ -63,21 +63,19 @@ void FLASH_Init(void);
|
||||
void FLASH_DeInit(void);
|
||||
uint8_t FLASH_EraseSector(uint32_t addr);
|
||||
uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len);
|
||||
status_t FLASH_Read(uint32_t addr, uint32_t *buf, uint32_t len);
|
||||
status_t flash_erase(uint32_t start_addr, uint32_t byte_cnt);
|
||||
status_t flash_write(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt);
|
||||
status_t flash_read(uint32_t addr, uint8_t *buf, uint32_t len);
|
||||
status_t flash_copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
|
||||
status_t FLASH_ReadBuf(uint32_t addr, uint32_t *buf, uint32_t len);
|
||||
status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
|
||||
status_t NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
|
||||
|
||||
status_t NOR_FLASH_Erase(uint32_t app_base_addr,uint32_t imageSize);
|
||||
void NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
|
||||
void NorFlash_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
|
||||
void NorFlash_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
|
||||
status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize);
|
||||
status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite);
|
||||
status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len);
|
||||
status_t Flash_Copy(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
|
||||
|
||||
#ifndef USE_HIGHT_SPEED_TRANS
|
||||
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength);
|
||||
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength);
|
||||
#else
|
||||
uint32_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag);
|
||||
status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLength,uint8_t doneFlag);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -596,6 +596,10 @@ KERNELPATHS +=-I$(KERNEL_ROOT)/tool/bootloader/flash \
|
||||
-I$(KERNEL_ROOT)/tool/bootloader/ota #
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_TOOL_USING_MQTT), y)
|
||||
KERNELPATHS +=-I$(KERNEL_ROOT)/../../APP_Framework/lib/mqtt
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_FS_LWEXT4),y)
|
||||
KERNELPATHS += -I$(KERNEL_ROOT)/fs/lwext4/lwext4_submodule/blockdev/xiuos #
|
||||
KERNELPATHS += -I$(KERNEL_ROOT)/fs/lwext4/lwext4_submodule/include #
|
||||
|
||||
@@ -16,6 +16,22 @@ menu "OTA function"
|
||||
bool "Config as application."
|
||||
endchoice
|
||||
|
||||
if MCUBOOT_APPLICATION
|
||||
choice
|
||||
prompt "The way of OTA firmware upgrade."
|
||||
default OTA_BY_PLATFORM
|
||||
|
||||
config OTA_BY_PLATFORM
|
||||
bool "Through IoT management platform."
|
||||
select TOOL_USING_MQTT
|
||||
|
||||
config OTA_BY_TCPSERVER
|
||||
bool "Through the public network TCP server."
|
||||
select SUPPORT_CONNECTION_FRAMEWORK
|
||||
select CONNECTION_ADAPTER_4G
|
||||
endchoice
|
||||
endif
|
||||
|
||||
|
||||
menu "Flash area address and size configuration."
|
||||
config CHIP_FLAH_BASE
|
||||
@@ -42,7 +58,18 @@ menu "OTA function"
|
||||
hex "Application package size,the default size is limited to 1M."
|
||||
default 0x00100000
|
||||
endmenu
|
||||
|
||||
|
||||
config OTA_RX_TIMEOUT
|
||||
int "OTA receive data timeout(ms)."
|
||||
default 600 if OTA_BY_PLATFORM
|
||||
default 10000 if OTA_BY_TCPSERVER
|
||||
default 10000 if MCUBOOT_BOOTLOADER
|
||||
|
||||
config OTA_RX_BUFFERSIZE
|
||||
int "OTA receive data buffer size."
|
||||
default 3072 if OTA_BY_PLATFORM
|
||||
default 2048 if OTA_BY_TCPSERVER
|
||||
default 256 if MCUBOOT_BOOTLOADER
|
||||
endif
|
||||
|
||||
endmenu
|
||||
|
||||
@@ -30,8 +30,8 @@ typedef struct
|
||||
void (*flash_deinit)(void);
|
||||
|
||||
/* flash operation */
|
||||
status_t (*op_flash_erase)(uint32_t start_addr, uint32_t byte_cnt);
|
||||
status_t (*op_flash_write)(uint32_t start_addr, uint8_t *buf, uint32_t byte_cnt);
|
||||
status_t (*op_flash_erase)(uint32_t start_addr, uint32_t imageSize);
|
||||
status_t (*op_flash_write)(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWrite);
|
||||
status_t (*op_flash_read)(uint32_t addr, uint8_t *buf, uint32_t len);
|
||||
status_t (*op_flash_copy)(uint32_t srcAddr,uint32_t dstAddr, uint32_t imageSize);
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,16 +1,22 @@
|
||||
/*
|
||||
* Copyright 2018-2020 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ota.h
|
||||
* @brief file ota.h
|
||||
* @version 2.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2023-04-03
|
||||
* @file: ota.h
|
||||
* @brief: file ota.h
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/4/23
|
||||
*
|
||||
*/
|
||||
#ifndef __OTA_DEF_H__
|
||||
#define __OTA_DEF_H__
|
||||
@@ -19,6 +25,10 @@
|
||||
|
||||
#define JUMP_FAILED_FLAG 0XABABABAB
|
||||
#define JUMP_SUCCESS_FLAG 0XCDCDCDCD
|
||||
#define STARTFLAG 0x1A2B //数据帧开始信号标记
|
||||
#define DATAFLAG 0x3C4D //数据帧数据信号标记
|
||||
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
|
||||
#define LENGTH 1024 //每帧数据的数据包长度
|
||||
|
||||
typedef enum {
|
||||
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级
|
||||
@@ -35,9 +45,8 @@ typedef enum {
|
||||
typedef struct {
|
||||
uint32_t size; // 应用程序大小,记录分区固件的大小
|
||||
uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值
|
||||
uint32_t version; // 应用程序版本号,记录分区固件的版本号
|
||||
uint32_t reserve; // 保留字段
|
||||
uint8_t description[128]; // 固件的描述信息,最多128个字符
|
||||
uint8_t version[32]; // 应用程序版本号,记录分区固件的版本
|
||||
uint8_t description[32]; // 固件的描述信息,最多32个字符
|
||||
} firmware_t;
|
||||
|
||||
|
||||
@@ -48,10 +57,45 @@ typedef struct {
|
||||
firmware_t down; // Download分区属性信息
|
||||
uint32_t status; // 升级状态,取值来自于ota_status_t类型
|
||||
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
|
||||
uint32_t reserve[2]; // 保留字段
|
||||
uint8_t error_message[128]; // 错误信息,最多128个字符
|
||||
uint8_t error_message[64]; // 错误信息,最多64个字符
|
||||
} ota_info_t;
|
||||
|
||||
|
||||
#ifdef OTA_BY_TCPSERVER
|
||||
/*bin包传输过程中的数据帧相关的结构体*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t frame_flag; // frame start flag 2 Bytes
|
||||
uint16_t dev_sid; // device software version
|
||||
uint32_t total_len; // send data total length caculated from each frame_len
|
||||
} ota_header_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t frame_id; // Current frame id
|
||||
uint8_t frame_data[LENGTH]; // Current frame data
|
||||
uint16_t frame_len; // Current frame data length
|
||||
uint16_t crc; // Current frame data crc
|
||||
} ota_frame_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ota_header_t header;
|
||||
ota_frame_t frame;
|
||||
} ota_data;
|
||||
#endif
|
||||
|
||||
#ifdef OTA_BY_PLATFORM
|
||||
typedef struct{
|
||||
uint32_t size; //OTA固件大小
|
||||
uint32_t streamId; //OTA固件下载时ID编号
|
||||
uint32_t counter; //OTA总下载次数
|
||||
uint32_t num; //OTA当前下载次数
|
||||
uint32_t downlen; //OTA当前下载次数的下载量
|
||||
uint8_t version[32]; //OTA下载时存储版本号的缓存区
|
||||
}OTA_TCB;
|
||||
#endif
|
||||
|
||||
void app_clear_jumpflag(void);
|
||||
void ota_entry(void);
|
||||
#endif
|
||||
|
||||
508
Ubiquitous/XiZi_IIoT/tool/bootloader/ota/server_tcp.c
Normal file
508
Ubiquitous/XiZi_IIoT/tool/bootloader/ota/server_tcp.c
Normal file
@@ -0,0 +1,508 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: ota_server.c
|
||||
* @brief: a application ota task of system running in Linux
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/5/26
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <libgen.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include <assert.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <ifaddrs.h>
|
||||
|
||||
#define STARTFLAG 0x1A2B //数据帧开始信号标记
|
||||
#define DATAFLAG 0x3C4D //数据帧数据信号标记
|
||||
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
|
||||
#define PORT 7777 //socket端口号
|
||||
#define SIZE 100 //socket链接限制为100
|
||||
#define LENGTH 1024 //每帧数据的数据包长度
|
||||
#define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t frame_flag; // frame start flag 2 Bytes
|
||||
uint16_t dev_sid; // device software version
|
||||
uint32_t total_len; // send data total length caculated from each frame_len
|
||||
} ota_header_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t frame_id; // Current frame id
|
||||
uint8_t frame_data[LENGTH]; // Current frame data
|
||||
uint16_t frame_len; // Current frame data length
|
||||
uint16_t crc; // Current frame data crc
|
||||
} ota_frame_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ota_header_t header;
|
||||
ota_frame_t frame;
|
||||
} ota_data;
|
||||
|
||||
|
||||
static int serverfd; // 服务器socket
|
||||
static int clientfd[SIZE] = {0}; // 客户端的socketfd,100个元素,clientfd[0]~clientfd[99]
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: calculate_crc16
|
||||
* 功能描述: 计算给定长度的数据的crc16的值,用于OTA传输过程中数据帧的校验
|
||||
* 形 参: data:数据buffer
|
||||
len:表示需要计算CRC16的数据长度
|
||||
* 返 回 值: 计算得到的CRC16值
|
||||
*******************************************************************************/
|
||||
static uint16_t calculate_crc16(uint8_t * data, uint32_t len)
|
||||
{
|
||||
uint16_t reg_crc=0xFFFF;
|
||||
while(len--)
|
||||
{
|
||||
reg_crc ^= *data++;
|
||||
for(int j=0;j<8;j++)
|
||||
{
|
||||
if(reg_crc & 0x01)
|
||||
reg_crc=reg_crc >>1 ^ 0xA001;
|
||||
else
|
||||
reg_crc=reg_crc >>1;
|
||||
}
|
||||
}
|
||||
printf("crc = [0x%x]\n",reg_crc);
|
||||
return reg_crc;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: sockt_init
|
||||
* 功能描述: 用于在TCP Server上创建socet监听
|
||||
* 形 参: data:数据buffer
|
||||
len:表示需要计算CRC16的数据长度
|
||||
* 返 回 值: 计算得到的CRC16值
|
||||
*******************************************************************************/
|
||||
void sockt_init(void)
|
||||
{
|
||||
struct sockaddr_in addr, *sa;//存储套接字的信息
|
||||
struct ifaddrs *ifap, *ifa;
|
||||
char *ipaddr;
|
||||
|
||||
serverfd = socket(AF_INET,SOCK_STREAM,0);
|
||||
|
||||
if(serverfd == -1)
|
||||
{
|
||||
perror("Failed to create socket");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//为套接字设置ip协议 设置端口号并自动获取本机ip转化为网络ip
|
||||
addr.sin_family = AF_INET;//地址族
|
||||
addr.sin_port = htons(PORT);//设置server端端口号,随便设置,当sin_port = 0时,系统随机选择一个未被使用的端口号
|
||||
addr.sin_addr.s_addr = htons(INADDR_ANY);//当sin_addr=INADDR_ANY时表示从本机的任一网卡接收数据
|
||||
|
||||
/*显示当前TCP server的*/
|
||||
getifaddrs(&ifap);
|
||||
for(ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
|
||||
{
|
||||
if(ifa->ifa_addr->sa_family == AF_INET)
|
||||
{
|
||||
sa = (struct sockaddr_in *) ifa->ifa_addr;
|
||||
ipaddr = inet_ntoa(sa->sin_addr);
|
||||
printf("Interface:%-16s Address:%-16s\n", ifa->ifa_name, ipaddr);
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifap);
|
||||
|
||||
//绑定套接字
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &timeout, sizeof(struct timeval)) < 0)
|
||||
{
|
||||
perror("Failed to set setsock option");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if(bind(serverfd,(struct sockaddr*)&addr,sizeof(addr)) == -1)
|
||||
{
|
||||
perror("Failed to bind socket port");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//监听最大连接数
|
||||
if(listen(serverfd,SIZE) == -1)
|
||||
{
|
||||
perror("Failed to set socket listening");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: ota_start_signal
|
||||
* 功能描述: 发送开始信号,等待接收端回应ready
|
||||
* 形 参: fd:监听的客户端连接的fd
|
||||
* 返 回 值: 0:成功,-1:失败
|
||||
*******************************************************************************/
|
||||
void ota_start_signal(int fd)
|
||||
{
|
||||
ota_data data;
|
||||
struct stat st;
|
||||
uint8_t buf[32];
|
||||
int file_size = 0, file_frame_cnt = 0, length = 0;
|
||||
|
||||
if (access(BIN_PATH, F_OK) == 0)
|
||||
{
|
||||
printf("%s exists.\n", basename(BIN_PATH));
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s does not exist,please cheack!\n", BIN_PATH);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//获取文件大小(以字节为单位)
|
||||
if(stat(BIN_PATH, &st) == 0)
|
||||
{
|
||||
file_size = st.st_size;
|
||||
file_frame_cnt = ((file_size%LENGTH) != 0)? (file_size/LENGTH + 1):(file_size/LENGTH);
|
||||
printf("%s size is %d bytes,frame count is %d!\n",basename(BIN_PATH), file_size, file_frame_cnt);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to get file size\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
while(1)
|
||||
{
|
||||
memset(&data, 0x0, sizeof(ota_data));
|
||||
data.header.frame_flag = STARTFLAG;
|
||||
//发送起始帧时把bin文件的大小一并发送出去
|
||||
data.header.total_len = file_size;
|
||||
|
||||
while(send(fd, &data, sizeof(data), MSG_NOSIGNAL) <= 0);
|
||||
printf("send start signal to client %d.\n", fd);
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length == 0)
|
||||
{
|
||||
printf("The current socket %d is disconnected,please check it!\n",fd);
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
}
|
||||
else if(length > 0 && (0 == strncmp(buf, "ready", length)))
|
||||
{
|
||||
printf("recv buf %s length %d from client %d.\n", buf, length, fd);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: ota_file_send
|
||||
* 功能描述: 用于在TCP Server发送bin文件
|
||||
* 形 参: fd:监听的客户端连接的fd
|
||||
* 返 回 值: 发送成功返回0,失败返回-1
|
||||
*******************************************************************************/
|
||||
int ota_file_send(int fd)
|
||||
{
|
||||
unsigned char buf[32] = { 0 };
|
||||
ota_data data;
|
||||
FILE *file_fd;
|
||||
int length = 0;
|
||||
int try_times;
|
||||
int recv_end_times = 3;
|
||||
int ret = 0;
|
||||
int frame_cnt = 0;
|
||||
int file_length = 0;
|
||||
char * file_buf = NULL;
|
||||
|
||||
file_fd = fopen(BIN_PATH, "r");
|
||||
if(NULL == file_fd)
|
||||
{
|
||||
printf("open file failed.\n");
|
||||
fclose(file_fd);
|
||||
return -1;
|
||||
}
|
||||
fseek(file_fd, 0, SEEK_SET);
|
||||
printf("start send bin file to client %d.\n", fd);
|
||||
|
||||
|
||||
while(!feof(file_fd))
|
||||
{
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
data.header.frame_flag = DATAFLAG;
|
||||
length = fread(data.frame.frame_data, 1, LENGTH, file_fd);
|
||||
if(length == LENGTH)
|
||||
{
|
||||
printf("read %d bytes\n",length);
|
||||
data.frame.frame_id = frame_cnt;
|
||||
data.frame.frame_len = length;
|
||||
data.frame.crc = calculate_crc16(data.frame.frame_data, length);
|
||||
file_length += length;
|
||||
}
|
||||
else if(length > 0 && length < LENGTH)
|
||||
{
|
||||
if(ferror(file_fd))
|
||||
{
|
||||
printf("read %s file error!\n", basename(BIN_PATH));
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("read %d bytes\n",length);
|
||||
data.frame.frame_id = frame_cnt;
|
||||
data.frame.frame_len = length;
|
||||
data.frame.crc = calculate_crc16(data.frame.frame_data, length);
|
||||
file_length += length;
|
||||
}
|
||||
}
|
||||
//fread返回值为0,此时是个空包,不需要再发送了否则是冗余数据
|
||||
else
|
||||
{
|
||||
printf("read %s file done!\n", basename(BIN_PATH));
|
||||
break;
|
||||
}
|
||||
|
||||
send_again:
|
||||
printf("send frame[%d] to client %d.\n", frame_cnt, fd);
|
||||
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if(length < 0)
|
||||
{
|
||||
printf("send frame[%d] to client %d failed,send again\n", frame_cnt, fd);
|
||||
goto send_again;
|
||||
}
|
||||
|
||||
recv_again:
|
||||
memset(buf, 0, sizeof(buf));
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length == 0)
|
||||
{
|
||||
printf("current socket %d is disconnected,please check it!\n", fd);
|
||||
ret = -1;
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
break;
|
||||
}
|
||||
else if(length < 0 )
|
||||
{
|
||||
printf("send frame[%d] to client %d waiting for ok timeout,receive again.\n", frame_cnt, fd);
|
||||
goto recv_again;
|
||||
}
|
||||
else if(0 == strncmp(buf, "ok", length))
|
||||
{
|
||||
printf("receive buf[%s] length %d from client %d.\n", buf, length, fd);
|
||||
try_times = 5;
|
||||
printf("send to client %d frame[%d] data send done.\n",fd, frame_cnt);
|
||||
frame_cnt++;
|
||||
continue;
|
||||
}
|
||||
|
||||
//接收到的回复不是ok,说明刚发的包有问题,需要再发一次
|
||||
else
|
||||
{
|
||||
if(try_times > 0)
|
||||
{
|
||||
try_times--;
|
||||
goto send_again;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("send to client %d frame[%d] 5 times failed.\n",fd, frame_cnt);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* finally,crc check total bin file.*/
|
||||
if(ret == 0)
|
||||
{
|
||||
printf("total send file length %d bytes, %d frames to client %d.\n", file_length, frame_cnt, fd);
|
||||
printf("now crc check total bin file.\n");
|
||||
file_buf = malloc(file_length);
|
||||
memset(file_buf, 0, file_length);
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
data.header.frame_flag = ENDTFLAG;
|
||||
|
||||
file_fd = fopen(BIN_PATH, "r");
|
||||
if(NULL == file_fd)
|
||||
{
|
||||
printf("open file failed.\n");
|
||||
return -1;
|
||||
}
|
||||
fseek(file_fd, 0, SEEK_SET);
|
||||
length = fread(file_buf,1, file_length, file_fd);
|
||||
printf("read file length = %d\n",length);
|
||||
if(length > 0)
|
||||
{
|
||||
data.frame.frame_id = frame_cnt;
|
||||
data.header.total_len = file_length;
|
||||
data.frame.crc = calculate_crc16(file_buf, length);
|
||||
}
|
||||
|
||||
send_end_signal:
|
||||
printf("send ota end signal to client %d.\n", fd);
|
||||
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if(length < 0)
|
||||
{
|
||||
printf("send to client %d ota end signal faile,send again\n",fd);
|
||||
goto send_end_signal;
|
||||
}
|
||||
|
||||
recv_end_signal:
|
||||
memset(buf, 0, sizeof(buf));
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length == 0)
|
||||
{
|
||||
printf("current socket %d is disconnected,please check it!\n",fd);
|
||||
ret = -1;
|
||||
free(file_buf);
|
||||
fclose(file_fd);
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
|
||||
}
|
||||
if(length < 0 || (0 != strncmp(buf, "ok", length)))
|
||||
{
|
||||
recv_end_times--;
|
||||
printf("from client %d end signal waiting for ok timeout,receive again.\n", fd);
|
||||
if(recv_end_times > 0)
|
||||
{
|
||||
goto recv_end_signal;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("client %d error end !!!\n", fd);
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
free(file_buf);
|
||||
}
|
||||
|
||||
fclose(file_fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: server_thread
|
||||
* 功能描述: TCP Server的服务线程入口函数
|
||||
* 形 参: p:入口函数的参数
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void* server_thread(void* p)
|
||||
{
|
||||
int fd = *(int*)p;
|
||||
unsigned char buf[32] = { 0 };
|
||||
ota_data data;
|
||||
|
||||
printf("pthread = %d\n",fd);
|
||||
sleep(3);
|
||||
while(1)
|
||||
{
|
||||
/* if ota failed then restart the ota process */
|
||||
ota_start_signal(fd);
|
||||
sleep(5);
|
||||
if(0 == ota_file_send(fd))
|
||||
{
|
||||
printf("ota file send to client %d successful.\n", fd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("exit fd = %d\n",fd);
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: server
|
||||
* 功能描述: TCP Server的服务函数
|
||||
* 形 参: 无
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void server(void)
|
||||
{
|
||||
int i = 0;
|
||||
printf("ota Server startup\n");
|
||||
while(1)
|
||||
{
|
||||
struct sockaddr_in fromaddr;
|
||||
socklen_t len = sizeof(fromaddr);
|
||||
int fd = accept(serverfd,(struct sockaddr*)&fromaddr,&len);//调用accept进入堵塞状态,等待客户端的连接
|
||||
|
||||
if(fd == -1)
|
||||
{
|
||||
printf("The client connection is wrong...\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
for(i = 0;i < SIZE;i++)
|
||||
{
|
||||
if(clientfd[i] == 0)
|
||||
{
|
||||
//记录客户端的socket
|
||||
clientfd[i] = fd;
|
||||
|
||||
//有客户端连接之后,启动线程给此客户服务
|
||||
pthread_t tid;
|
||||
pthread_create(&tid,0,server_thread,&fd);
|
||||
break;
|
||||
}
|
||||
|
||||
if(SIZE == i)
|
||||
{
|
||||
//发送给客户端聊天室满了
|
||||
char* str = "Devices full";
|
||||
printf("%s", str);
|
||||
send(fd,str,strlen(str),0);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
sockt_init();
|
||||
server();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user