Hi everyone,
after I study OTA update, I choose to work with a custom solution that download the bin file from my server and copy where is my main code.
I've tried to use the file_download example, in which I set my server and my folder. I've modified the code to save the downloaded file in a directory
and after it delete my mcuimg file and create the new with downloaded code.
In the download example it works.
Then I've tried to import my custom GetData function in my project, based on MQTT client
static int GetData(HTTPCli_Handle cli)
{
long lRetVal = 0;
long fileHandle = -1;
long fileHandle2 = -1;
unsigned long Token = 0;
unsigned char string_to_write[SIZE_40K];
int id;
int len=0;
bool moreFlag = 0;
HTTPCli_Field fields[3] = {
{HTTPCli_FIELD_NAME_HOST, HOST_NAME},
{HTTPCli_FIELD_NAME_ACCEPT, "text/html, application/xhtml+xml, */*"},
{NULL, NULL}
};
const char *ids[4] = {
HTTPCli_FIELD_NAME_CONTENT_LENGTH,
HTTPCli_FIELD_NAME_TRANSFER_ENCODING,
HTTPCli_FIELD_NAME_CONNECTION,
NULL
};
UART_PRINT("Start downloading the file\r\n");
// Set request fields
HTTPCli_setRequestFields(cli, fields);
memset(g_buff, 0, sizeof(g_buff));
// Make HTTP 1.1 GET request
lRetVal = HTTPCli_sendRequest(cli, HTTPCli_METHOD_GET, PREFIX_BUFFER, 0);
if (lRetVal < 0)
{
// error
ASSERT_ON_ERROR(SEND_ERROR);
}
// Test getResponseStatus: handle
lRetVal = HTTPCli_getResponseStatus(cli);
if (lRetVal != 200)
{
FlushHTTPResponse(cli);
if(lRetVal == 404)
{
ASSERT_ON_ERROR(FILE_NOT_FOUND_ERROR);
}
ASSERT_ON_ERROR(INVALID_SERVER_RESPONSE);
}
HTTPCli_setResponseFields(cli, ids);
// Read response headers
while ((id = HTTPCli_getResponseField(cli, (char *)g_buff, sizeof(g_buff), &moreFlag))
!= HTTPCli_FIELD_ID_END)
{
if(id == 0)
{
UART_PRINT("Content length: %s\n\r", g_buff);
}
else if(id == 1)
{
if(!strncmp((const char *)g_buff, "chunked", sizeof("chunked")))
{
UART_PRINT("Chunked transfer encoding\n\r");
}
}
else if(id == 2)
{
if(!strncmp((const char *)g_buff, "close", sizeof("close")))
{
ASSERT_ON_ERROR(FORMAT_NOT_SUPPORTED);
}
}
}
// Open file to save the downloaded file
lRetVal = sl_FsOpen((_u8 *)FILE_NAME, FS_MODE_OPEN_WRITE, &Token, &fileHandle);
if(lRetVal < 0)
{
// File Doesn't exit create a new of 40 KB file
lRetVal = sl_FsOpen((unsigned char *)FILE_NAME, \
FS_MODE_OPEN_CREATE(SIZE_40K, \
_FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE),
&Token, &fileHandle);
ASSERT_ON_ERROR(lRetVal);
}
while(1)
{
len = HTTPCli_readResponseBody(cli, (char *)g_buff, sizeof(g_buff) - 1, &moreFlag);
if(len < 0)
{
// Close file without saving
lRetVal = sl_FsClose(fileHandle, 0, (unsigned char*) "A", 1);
return lRetVal;
}
lRetVal = sl_FsWrite(fileHandle, bytesReceived,
(unsigned char *)g_buff, len);
if(lRetVal < len)
{
UART_PRINT("Failed during writing the file, Error-code: %d\r\n", \
FILE_WRITE_ERROR);
// Close file without saving
lRetVal = sl_FsClose(fileHandle, 0, (unsigned char*) "A", 1);
return lRetVal;
}
bytesReceived +=len;
if ((len - 2) >= 0 && g_buff[len - 2] == '\r' && g_buff [len - 1] == '\n'){
break;
}
if(!moreFlag)
{
break;
}
}
//
// If user file has checksum which can be used to verify the temporary
// file then file should be verified
// In case of invalid file (FILE_NAME) should be closed without saving to
// recover the previous version of file
//
// Save and close file
UART_PRINT("Total bytes received: %d\n\r", bytesReceived);
lRetVal = sl_FsClose(fileHandle, 0, 0, 0);
ASSERT_ON_ERROR(lRetVal);
lRetVal=sl_FsDel("/sys/mcuimg2.bin",0);
UART_PRINT("Deleting file: %d\n\r", lRetVal);
bzero(stringa_da_scrivere,bytesReceived);
lRetVal = sl_FsOpen((unsigned char *)SYSTEM_IMAGE, \
FS_MODE_OPEN_CREATE(SIZE_40K, \
_FS_FILE_OPEN_FLAG_COMMIT|_FS_FILE_PUBLIC_WRITE),
&Token, &fileHandle2);
UART_PRINT("Create new mcuimgfile %d\n\r",lRetVal);
lRetVal = sl_FsOpen((_u8 *)FILE_NAME, FS_MODE_OPEN_READ, &Token, &fileHandle);
UART_PRINT("Open downloaded file %d\n\r",lRetVal);
lRetVal = sl_FsRead(fileHandle,0,string_to_write,bytesReceived);
UART_PRINT("Read from downloaded file %d\n\r",lRetVal);
lRetVal = sl_FsWrite(fileHandle2,0,string_to_write,bytesReceived);
UART_PRINT("Write new mcuimg %d\n\r",lRetVal);
lRetVal=sl_FsClose(fileHandle2,0,0,0);
lRetVal=sl_FsClose(fileHandle,0,0,0);
return SUCCESS;
}
the thread that calls this function is
void ServerFileDownload()
{
long lRetVal = -1;
struct sockaddr_in addr;
HTTPCli_Struct cli;
MAP_UtilsDelay(800000000);
while(1){
osi_LockObjLock(&semaphore, OSI_WAIT_FOREVER);
MAP_UtilsDelay(80000000);
lRetVal = sl_NetAppDnsGetHostByName((signed char *)HOST_NAME,
strlen((const char *)HOST_NAME),
&g_ulDestinationIP,SL_AF_INET);
if(lRetVal < 0)
{
ASSERT_ON_ERROR(GET_HOST_IP_FAILED);
}
// Set up the input parameters for HTTP Connection
addr.sin_family = AF_INET;
addr.sin_port = htons(HOST_PORT);
addr.sin_addr.s_addr = sl_Htonl(g_ulDestinationIP);
// Testing HTTPCli open call: handle, address params only
HTTPCli_construct(&cli);
lRetVal = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, NULL);
if (lRetVal < 0)
{
UART_PRINT("Connection to server failed\n\r");
//ASSERT_ON_ERROR(SERVER_CONNECTION_FAILED);
}
else
{
UART_PRINT("Connection to server created successfully\r\n");
}
// Download the file, verify the file and replace the exiting file
lRetVal = GetData(&cli);
if(lRetVal < 0)
{
UART_PRINT("Device couldn't download the file from the server\n\r");
}
else
{
RebootMCU();
}
HTTPCli_destruct(&cli);
osi_LockObjUnlock(&semaphore);
}
return SUCCESS;
}
The code blocks itself on
"lRetVal = HTTPCli_getResponseStatus(cli);"