Skip to content

Commit

Permalink
OTAP: better handle delay for process scratchpad
Browse files Browse the repository at this point in the history
The process scratchpad call is finished only when the connection
with the stack is available again.
Wait for up to 60 sec for the gateway to answer.
  • Loading branch information
GwendalRaoul committed May 16, 2019
1 parent c84678f commit 1adb7f3
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions lib/wpc/wpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "wpc.h" // For DEFAULT_BITRATE
#include "wpc_internal.h"
#include "platform.h" // For Platform_get_timestamp_ms_epoch()

/**
* \brief Macro to convert dual_mcu return code
Expand All @@ -31,6 +32,13 @@
ret; \
})

/** \brief Timeout to get a valid stack response after
* a stop stack. It can be quite long in case of
* the bootloader is processing a scratchpad
* (especially from an external memory)
*/
#define TIMEOUT_AFTER_STOP_STACK_S 60

app_res_e WPC_initialize(char * port_name, unsigned long bitrate)
{
return WPC_Int_initialize(port_name, bitrate) == 0 ? APP_RES_OK : APP_RES_INTERNAL_ERROR;
Expand Down Expand Up @@ -448,23 +456,23 @@ app_res_e WPC_get_sink_cost(uint8_t * cost_p)
return convert_error_code(SINK_COST_ERROR_CODE_LUT, res);
}

// Define the maximum number of attempt to get status after a
// stop or start of stack
#define MAX_GET_STATUS_ATTEMPT 20

static bool get_statck_status()
static bool get_statck_status(uint16_t timeout_s)
{
uint8_t status;
uint8_t max_attempt = MAX_GET_STATUS_ATTEMPT;
app_res_e res = APP_RES_INTERNAL_ERROR;

while (WPC_get_stack_status(&status) != APP_RES_OK && max_attempt-- > 0)
// Compute timeout
unsigned long long timeout = Platform_get_timestamp_ms_epoch() + timeout_s * 1000;

while (res != APP_RES_OK && Platform_get_timestamp_ms_epoch() < timeout)
{
res = WPC_get_stack_status(&status);
LOGD("Cannot get status after start/stop, try again...\n");
}

if (max_attempt == 0)
if (res != APP_RES_OK)
{
LOGE("Cannot get stack status after %d attempts\n", MAX_GET_STATUS_ATTEMPT);
LOGE("Cannot get stack status after %d seconds\n", timeout_s);
return false;
}

Expand All @@ -473,6 +481,13 @@ static bool get_statck_status()
app_res_e WPC_start_stack(void)
{
int res = msap_stack_start_request(0);

if (res < 0)
{
// Error in communication
return APP_RES_INTERNAL_ERROR;
}

if ((res & 0x1) == 0x01)
{
return APP_RES_STACK_ALREADY_STARTED;
Expand All @@ -495,10 +510,10 @@ app_res_e WPC_start_stack(void)
}

// A start of the stack shouldn't create any interruption
// of service but let's poll for it to be symetric with stop
// of service but let's poll for it to be symmetric with stop
// and for some reason it was seen on some platforms that the
// first request following a start is lost
if (!get_statck_status())
if (!get_statck_status(2))
{
return APP_RES_INTERNAL_ERROR;
}
Expand All @@ -510,6 +525,12 @@ app_res_e WPC_stop_stack(void)
{
int res = msap_stack_stop_request();

if (res < 0)
{
// Error in communication
return APP_RES_INTERNAL_ERROR;
}

if (res == 1)
{
return APP_RES_STACK_ALREADY_STOPPED;
Expand All @@ -521,7 +542,8 @@ app_res_e WPC_stop_stack(void)

// A stop of the stack will reboot the device
// Wait for the stack to be up again
if (!get_statck_status())
// It can be quite long in case a scratchpad is processed
if (!get_statck_status(TIMEOUT_AFTER_STOP_STACK_S))
{
return APP_RES_INTERNAL_ERROR;
}
Expand Down

0 comments on commit 1adb7f3

Please sign in to comment.