2021-01-02 00:30:13 +01:00
# include "mesh_ota.h"
/*
* 999.999 .999
* Return true if remote version is newer ( higher ) than local version
*/
2021-01-06 18:10:12 +01:00
bool bNewerVersion ( const char * pu8Local , const char * pu8Remote ) {
2021-01-02 00:30:13 +01:00
char u8LocalTmp [ 12 ] ;
char u8RemoteTmp [ 12 ] ;
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
char * pu8saveptrLocal ;
char * pu8saveptrRemote ;
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
strcpy ( u8LocalTmp , pu8Local ) ;
strcpy ( u8RemoteTmp , pu8Remote ) ;
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
char * pu8TokenLocal = strtok_r ( u8LocalTmp , " . " , & pu8saveptrLocal ) ;
char * pu8TokenRemote = strtok_r ( u8RemoteTmp , " . " , & pu8saveptrRemote ) ;
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
bool bReturn = false ;
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
uint8_t u8Index = 0 ;
2021-01-06 18:10:12 +01:00
while ( ( u8Index < = 2 ) & & ( bReturn = = false ) ) {
2021-01-02 00:30:13 +01:00
u8Index + + ;
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
if ( atoi ( pu8TokenLocal ) < atoi ( pu8TokenRemote ) )
{
bReturn = true ;
}
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
pu8TokenLocal = strtok_r ( NULL , " . " , & pu8saveptrLocal ) ;
pu8TokenRemote = strtok_r ( NULL , " . " , & pu8saveptrRemote ) ;
}
2021-01-06 18:10:12 +01:00
2021-01-02 00:30:13 +01:00
return bReturn ;
}
2021-01-06 18:10:12 +01:00
2021-01-07 23:10:36 +01:00
esp_err_t errFindImageStart ( const char * pu8Data , uint32_t * pu32DataLenght , uint32_t * pu32StartOffset )
2021-01-06 18:10:12 +01:00
{
/*
Offset value
0 = E9
48 = first digit of version number
*/
esp_err_t errReturn = ESP_OK ;
2021-01-07 17:10:25 +01:00
bool bImageStartOffsetFound = false ;
2021-01-06 18:10:12 +01:00
* pu32StartOffset = 0U ;
2021-01-07 17:10:25 +01:00
2021-01-06 18:10:12 +01:00
uint32_t u32DataIndex = 0 ;
uint32_t u32FirstDotOffset = 0 ;
uint32_t u32SecondDotOffset = 0 ;
uint8_t u8FirstDotIndex = 0 ;
uint8_t u8SecondDotIndex = 0 ;
2021-01-07 17:10:25 +01:00
while ( ( u32DataIndex < * pu32DataLenght ) & & ( bImageStartOffsetFound = = false ) )
2021-01-06 18:10:12 +01:00
{
//search for magic byte
if ( pu8Data [ u32DataIndex ] = = 0xe9 )
{
//magic byte found
2021-01-08 20:12:03 +01:00
printf ( " \n magic byte found: %i \n " , u32DataIndex ) ;
2021-01-06 18:10:12 +01:00
while ( ( u8FirstDotIndex < 3 ) & & ( u32FirstDotOffset = = 0 ) )
{
//search first dot in version number
if ( ( u32DataIndex + 49 + u8FirstDotIndex ) < * pu32DataLenght )
{
if ( ( pu8Data [ ( u32DataIndex + 49 + u8FirstDotIndex ) ] = = 0x2e ) )
{
//first do found
u32FirstDotOffset = ( u32DataIndex + 49 + u8FirstDotIndex ) ;
2021-01-07 23:10:36 +01:00
//printf("First dot offset: %i\n", u32FirstDotOffset);
2021-01-06 18:10:12 +01:00
}
}
u8FirstDotIndex + + ;
}
while ( ( u8SecondDotIndex < 3 ) & & ( u32SecondDotOffset = = 0 ) & & ( u32FirstDotOffset ! = 0 ) )
{
//search first dot in version number
2021-01-07 17:10:25 +01:00
if ( ( u32FirstDotOffset + ( u8SecondDotIndex + 2 ) ) < * pu32DataLenght )
2021-01-06 18:10:12 +01:00
{
2021-01-07 17:10:25 +01:00
if ( ( pu8Data [ ( u32FirstDotOffset + ( u8SecondDotIndex + 2 ) ) ] = = 0x2e ) )
2021-01-06 18:10:12 +01:00
{
//second do found
2021-01-07 17:10:25 +01:00
u32SecondDotOffset = ( u32FirstDotOffset + ( u8SecondDotIndex + 2 ) ) ;
2021-01-07 23:10:36 +01:00
//printf("Second dot offset: %i\n", u32SecondDotOffset);
2021-01-06 18:10:12 +01:00
}
}
u8SecondDotIndex + + ;
}
if ( ( u32FirstDotOffset ! = 0 ) & & ( u32SecondDotOffset ! = 0 ) )
{
//image start found based on magic byte and version number systax
* pu32StartOffset = u32DataIndex ; //store image start offset
2021-01-07 17:10:25 +01:00
bImageStartOffsetFound = true ;
2021-01-06 18:10:12 +01:00
}
else
{
// this is propably not the magic byte --> reset
u32FirstDotOffset = 0 ;
u32SecondDotOffset = 0 ;
u8FirstDotIndex = 0 ;
u8SecondDotIndex = 0 ;
}
}
u32DataIndex + + ;
//putchar(pu8Data[i]);
2021-01-07 17:10:25 +01:00
//printf("%x ", pu8Data[u32DataIndex]);
2021-01-06 18:10:12 +01:00
}
2021-01-07 17:10:25 +01:00
if ( bImageStartOffsetFound = = false )
2021-01-06 18:10:12 +01:00
{
errReturn = ESP_ERR_NOT_FOUND ;
}
return errReturn ;
}
2021-01-07 23:10:36 +01:00
esp_err_t errExtractVersionNumber ( const char * pu8Data , uint32_t * pu32DataLenght , char * pc8RemoteVersionNumber )
2021-01-06 18:10:12 +01:00
{
strcpy ( pc8RemoteVersionNumber , " 999.999.999 " ) ; //init value
uint32_t u32StartOffset ;
esp_err_t err = errFindImageStart ( pu8Data , pu32DataLenght , & u32StartOffset ) ;
if ( err = = ESP_OK )
{
2021-01-08 20:12:03 +01:00
printf ( " \n Image start found: %i \n " , u32StartOffset ) ;
2021-01-06 18:10:12 +01:00
2021-01-07 23:10:36 +01:00
strncpy ( pc8RemoteVersionNumber , pu8Data + ( u32StartOffset + 48 ) , 11 ) ;
pc8RemoteVersionNumber [ 12 ] = ' \0 ' ;
2021-01-08 20:12:03 +01:00
printf ( " remote version number %s \n \n " , pc8RemoteVersionNumber ) ;
2021-01-06 18:10:12 +01:00
}
return err ;
2021-01-07 17:10:25 +01:00
}