2021-01-11 22:56:39 +01:00
# include "Mesh_OTA.h"
2021-01-02 00:30:13 +01:00
/*
* 999.999 .999
* Return true if remote version is newer ( higher ) than local version
*/
2021-01-09 17:41:40 +01:00
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
2021-01-06 18:10:12 +01:00
2021-01-09 17:41:40 +01:00
strcpy ( u8LocalTmp , pu8Local ) ; //copy in tmp
strcpy ( u8RemoteTmp , pu8Remote ) ; //copy in tmp
2021-01-06 18:10:12 +01:00
2021-01-09 17:41:40 +01:00
char * pu8TokenLocal = strtok_r ( u8LocalTmp , " . " , & pu8saveptrLocal ) ; //split tokens
char * pu8TokenRemote = strtok_r ( u8RemoteTmp , " . " , & pu8saveptrRemote ) ; //split tokens
2021-01-06 18:10:12 +01:00
2021-01-09 17:41:40 +01:00
while ( ( u8Index < = 2 ) & & ( bReturn = = false ) ) //loop through tokens
{
2021-01-02 00:30:13 +01:00
u8Index + + ;
if ( atoi ( pu8TokenLocal ) < atoi ( pu8TokenRemote ) )
{
2021-01-09 17:41:40 +01:00
bReturn = true ; //version number difference --> stop loop
2021-01-02 00:30:13 +01:00
}
2021-01-09 17:41:40 +01:00
pu8TokenLocal = strtok_r ( NULL , " . " , & pu8saveptrLocal ) ; //split tokens
pu8TokenRemote = strtok_r ( NULL , " . " , & pu8saveptrRemote ) ; //split tokens
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
2021-01-09 17:41:40 +01:00
0 = 0xE9 ( first byte in image - - > magic byte )
2021-01-06 18:10:12 +01:00
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
uint32_t u32DataIndex = 0 ;
uint32_t u32FirstDotOffset = 0 ;
uint32_t u32SecondDotOffset = 0 ;
uint8_t u8FirstDotIndex = 0 ;
uint8_t u8SecondDotIndex = 0 ;
2021-01-09 17:41:40 +01:00
* pu32StartOffset = 0U ; //reset offset to zero
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
while ( ( u8FirstDotIndex < 3 ) & & ( u32FirstDotOffset = = 0 ) )
{
//search first dot in version number
if ( ( u32DataIndex + 49 + u8FirstDotIndex ) < * pu32DataLenght )
{
if ( ( pu8Data [ ( u32DataIndex + 49 + u8FirstDotIndex ) ] = = 0x2e ) )
{
2021-01-09 17:41:40 +01:00
//first dot found
2021-01-06 18:10:12 +01:00
u32FirstDotOffset = ( u32DataIndex + 49 + u8FirstDotIndex ) ;
}
}
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
{
2021-01-09 17:41:40 +01:00
//second dot found
2021-01-07 17:10:25 +01:00
u32SecondDotOffset = ( u32FirstDotOffset + ( u8SecondDotIndex + 2 ) ) ;
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 + + ;
}
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
{
uint32_t u32StartOffset ;
2021-01-09 17:41:40 +01:00
esp_err_t err = ESP_OK ;
2021-01-06 18:10:12 +01:00
2021-01-09 17:41:40 +01:00
strcpy ( pc8RemoteVersionNumber , " 999.999.999 " ) ; //init value
err = errFindImageStart ( pu8Data , pu32DataLenght , & u32StartOffset ) ; //get image start offset
2021-01-06 18:10:12 +01:00
if ( err = = ESP_OK )
{
2021-01-09 17:41:40 +01:00
//image found
strncpy ( pc8RemoteVersionNumber , pu8Data + ( u32StartOffset + 48 ) , 11 ) ; //copy version number
pc8RemoteVersionNumber [ 12 ] = ' \0 ' ;
2021-01-06 18:10:12 +01:00
}
return err ;
2021-01-07 17:10:25 +01:00
}
2021-01-11 15:22:45 +01:00
/*
esp_err_t esp_mesh_ota_send ( mesh_addr_t * dest )
{
esp_err_t err = ESP_OK ;
static uint32_t u32index ;
const esp_partition_t * currentPartition = esp_ota_get_boot_partition ( ) ;
if ( ( * currentPartition ) . subtype = = 0 )
{
int data_read = 0 ;
struct ota_mesh_packet packet ;
packet . type = OTA_Data ;
if ( u32index = = 1024 )
{
//all data read
data_read = 0 ;
u32index = 0 ;
}
else
{
ESP_LOGI ( MESH_TAG , " OTA-Data read: %i " , u32index ) ;
err = esp_partition_read ( currentPartition , ( 1024 * u32index ) , packet . au8Payload , 1024 ) ;
ESP_ERROR_CHECK ( err ) ;
data_read = 1024 ;
u32index + + ;
}
if ( data_read > 0 )
{
//send ota fragemnt to node
esp_mesh_send_packet ( dest , & packet ) ;
}
ESP_ERROR_CHECK ( err ) ;
}
else
{
ESP_LOGI ( MESH_TAG , " Subtype: %d " , ( * currentPartition ) . subtype ) ;
}
return err ;
}
esp_err_t esp_mesh_ota_receive ( mesh_addr_t * dest , struct ota_mesh_packet * packet )
{
esp_err_t err = ESP_OK ;
static esp_ota_handle_t otaHandle ;
static uint32_t u32index ;
const esp_partition_t * currentPartition = esp_ota_get_boot_partition ( ) ;
const esp_partition_t * otaPartition = esp_ota_get_next_update_partition ( currentPartition ) ;
if ( u32index = = 0 )
{
//first run
err = esp_ota_begin ( otaPartition , OTA_SIZE_UNKNOWN , & otaHandle ) ;
ESP_ERROR_CHECK ( err ) ;
}
ESP_LOGI ( MESH_TAG , " OTA-Data write: %i " , u32index ) ;
err = esp_ota_write ( otaHandle , packet - > au8Payload , 1024 ) ;
if ( err ! = ESP_OK )
{
ESP_LOGE ( MESH_TAG , " OTA-Data write error: %i at %i " , err , u32index ) ;
}
ESP_ERROR_CHECK ( err ) ;
if ( u32index > = 1023 )
{
//ota update complete
ESP_LOGI ( MESH_TAG , " OTA-Data complete arrived: %i " , u32index ) ;
err = esp_ota_end ( otaHandle ) ;
ESP_ERROR_CHECK ( err ) ;
esp_app_desc_t otaPartitionDesc ;
err = esp_ota_get_partition_description ( otaPartition , & otaPartitionDesc ) ;
ESP_ERROR_CHECK ( err ) ;
ESP_LOGI ( MESH_TAG , " otaPartition project_name: %s " , ( otaPartitionDesc ) . project_name ) ;
err = esp_ota_set_boot_partition ( otaPartition ) ;
ESP_ERROR_CHECK ( err ) ;
struct ota_mesh_packet retPacket ;
retPacket . type = OTA_Complete ;
ESP_ERROR_CHECK ( esp_mesh_send_packet ( dest , & retPacket ) ) ; //send back to parent
//check if this node has children --> Update them
esp_restart ( ) ;
}
u32index + + ;
return err ;
}
*/