129 lines
4.2 KiB
C
129 lines
4.2 KiB
C
#include "mesh_ota.h"
|
|
|
|
/*
|
|
* 999.999.999
|
|
* Return true if remote version is newer (higher) than local version
|
|
*/
|
|
bool bNewerVersion(const char* pu8Local, const char* pu8Remote)
|
|
{
|
|
char u8LocalTmp[12]; //local version
|
|
char u8RemoteTmp[12]; //remote version
|
|
char* pu8saveptrLocal; //context for strok_r
|
|
char* pu8saveptrRemote; //context for strok_r
|
|
bool bReturn = false; //flag to stop loop
|
|
uint8_t u8Index = 0; //numbers counter in version string
|
|
|
|
strcpy(u8LocalTmp, pu8Local); //copy in tmp
|
|
strcpy(u8RemoteTmp, pu8Remote); //copy in tmp
|
|
|
|
char* pu8TokenLocal = strtok_r(u8LocalTmp, ".", &pu8saveptrLocal); //split tokens
|
|
char* pu8TokenRemote = strtok_r(u8RemoteTmp, ".", &pu8saveptrRemote); //split tokens
|
|
|
|
while( (u8Index <= 2) && (bReturn == false)) //loop through tokens
|
|
{
|
|
u8Index++;
|
|
if(atoi(pu8TokenLocal) < atoi(pu8TokenRemote))
|
|
{
|
|
bReturn = true; //version number difference --> stop loop
|
|
}
|
|
pu8TokenLocal = strtok_r(NULL, ".", &pu8saveptrLocal); //split tokens
|
|
pu8TokenRemote = strtok_r(NULL, ".", &pu8saveptrRemote); //split tokens
|
|
}
|
|
return bReturn;
|
|
}
|
|
|
|
esp_err_t errFindImageStart(const char* pu8Data, uint32_t* pu32DataLenght, uint32_t* pu32StartOffset)
|
|
{
|
|
/*
|
|
Offset value
|
|
0 = 0xE9 (first byte in image --> magic byte)
|
|
48 = first digit of version number
|
|
*/
|
|
|
|
esp_err_t errReturn = ESP_OK;
|
|
bool bImageStartOffsetFound = false;
|
|
uint32_t u32DataIndex = 0;
|
|
uint32_t u32FirstDotOffset = 0;
|
|
uint32_t u32SecondDotOffset = 0;
|
|
uint8_t u8FirstDotIndex = 0;
|
|
uint8_t u8SecondDotIndex = 0;
|
|
|
|
*pu32StartOffset = 0U; //reset offset to zero
|
|
|
|
while((u32DataIndex < *pu32DataLenght) && (bImageStartOffsetFound == false))
|
|
{
|
|
//search for magic byte
|
|
if(pu8Data[u32DataIndex] == 0xe9)
|
|
{
|
|
//magic byte found
|
|
while ((u8FirstDotIndex < 3) && (u32FirstDotOffset == 0))
|
|
{
|
|
//search first dot in version number
|
|
if((u32DataIndex+49+u8FirstDotIndex) < *pu32DataLenght)
|
|
{
|
|
if((pu8Data[(u32DataIndex+49+u8FirstDotIndex)] == 0x2e))
|
|
{
|
|
//first dot found
|
|
u32FirstDotOffset = (u32DataIndex+49+u8FirstDotIndex);
|
|
}
|
|
}
|
|
u8FirstDotIndex++;
|
|
}
|
|
|
|
while ((u8SecondDotIndex < 3) && (u32SecondDotOffset == 0) && (u32FirstDotOffset != 0))
|
|
{
|
|
//search first dot in version number
|
|
if((u32FirstDotOffset+(u8SecondDotIndex+2)) < *pu32DataLenght)
|
|
{
|
|
if((pu8Data[(u32FirstDotOffset+(u8SecondDotIndex+2))] == 0x2e))
|
|
{
|
|
//second dot found
|
|
u32SecondDotOffset = (u32FirstDotOffset+(u8SecondDotIndex+2));
|
|
}
|
|
}
|
|
u8SecondDotIndex++;
|
|
}
|
|
|
|
if((u32FirstDotOffset != 0) && (u32SecondDotOffset != 0))
|
|
{
|
|
//image start found based on magic byte and version number systax
|
|
*pu32StartOffset = u32DataIndex; //store image start offset
|
|
bImageStartOffsetFound = true;
|
|
}
|
|
else
|
|
{
|
|
// this is propably not the magic byte --> reset
|
|
u32FirstDotOffset = 0;
|
|
u32SecondDotOffset = 0;
|
|
u8FirstDotIndex = 0;
|
|
u8SecondDotIndex = 0;
|
|
}
|
|
}
|
|
u32DataIndex++;
|
|
}
|
|
|
|
if(bImageStartOffsetFound == false)
|
|
{
|
|
errReturn = ESP_ERR_NOT_FOUND;
|
|
}
|
|
|
|
return errReturn;
|
|
}
|
|
|
|
esp_err_t errExtractVersionNumber(const char* pu8Data, uint32_t* pu32DataLenght, char* pc8RemoteVersionNumber)
|
|
{
|
|
uint32_t u32StartOffset;
|
|
esp_err_t err = ESP_OK;
|
|
|
|
strcpy(pc8RemoteVersionNumber, "999.999.999"); //init value
|
|
err = errFindImageStart(pu8Data, pu32DataLenght, &u32StartOffset); //get image start offset
|
|
|
|
if(err == ESP_OK)
|
|
{
|
|
//image found
|
|
strncpy(pc8RemoteVersionNumber, pu8Data+(u32StartOffset+48), 11); //copy version number
|
|
pc8RemoteVersionNumber[12] = '\0';
|
|
}
|
|
return err;
|
|
}
|