Skip to content
Snippets Groups Projects
Verified Commit f9b7c3f1 authored by Vincent Coubard's avatar Vincent Coubard
Browse files

azure: Rework connection status reporting


Network status was not managed properly in azure client glue code:
- wait_for_event was unblocking for any event triggered not the
specific event required as input of the function. This causes issues
when the network stack trigger spurious event NETWORK_UP events.
- The state of the network was read from the events set. Unfortunatelly,
this can cause issues when the events are NETWORK_UP and NETWORK_DOWN
are triggered and the event not read in between.

To solve these issues, the following solutions have been implemented:
- the state of the network is stored in a local variable and updated
whenever NETWORK_UP or NETWORK_DOWN are received. This filters out
spurious events from the network stack. An event
AZURE_NETWORK_CHANGE_FLAG is triggered to inform the azure code
that the network state has changed.
- The function wait_for_event only waits for a specific event instead of
all events.

Signed-off-by: Vincent Coubard's avatarVincent Coubard <vincent.coubard@arm.com>
parent 729134f6
No related branches found
No related tags found
No related merge requests found
......@@ -11,6 +11,7 @@
#include "iotsdk/ip_network_api.h"
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
......@@ -18,36 +19,25 @@
#include "iot_ntp_client.h"
#endif // IOTSDK_AZURE_SDK_NTP_TIME
#define AZURE_NETWORK_UP_FLAG 0x00000001U
#define AZURE_NETWORK_DOWN_FLAG 0x00000002U
#define AZURE_TIME_READY_FLAG 0x00000004U
#define AZURE_ALL_EVENTS_FLAGS (AZURE_NETWORK_UP_FLAG | AZURE_NETWORK_DOWN_FLAG | AZURE_TIME_READY_FLAG)
#define AZURE_NETWORK_CHANGE_FLAG 0x00000001U
#define AZURE_TIME_READY_FLAG 0x00000002U
#define AZURE_EVENT_TIMEOUT 15000
static osEventFlagsId_t event_flags_id;
static osEventFlagsId_t event_flags_id = NULL;
static osMutexId_t network_state_lock = NULL;
static bool network_up = false;
/** Wait for specific platform event and check error */
static int wait_for_event(uint32_t event)
{
int res = 0;
int ret = osEventFlagsWait(event_flags_id, AZURE_ALL_EVENTS_FLAGS, osFlagsWaitAny, AZURE_EVENT_TIMEOUT);
int ret = osEventFlagsWait(event_flags_id, event, osFlagsWaitAny, AZURE_EVENT_TIMEOUT);
if (ret < 0) {
LogError("osEventFlagsWait failed %d", ret);
return MU_FAILURE;
}
if (!(ret & event)) {
res = MU_FAILURE;
}
ret = osEventFlagsClear(event_flags_id, AZURE_ALL_EVENTS_FLAGS);
if (ret < 0) {
LogError("osEventFlagsClear failed %d", ret);
return MU_FAILURE;
}
return res;
return 0;
}
/** This callback is called by the ip network task. It translates from a network event code
......@@ -57,11 +47,48 @@ static int wait_for_event(uint32_t event)
*/
static void network_state_callback(network_state_callback_event_t event)
{
uint32_t event_flag = (event == NETWORK_UP) ? AZURE_NETWORK_UP_FLAG : AZURE_NETWORK_DOWN_FLAG;
int res = osEventFlagsSet(event_flags_id, event_flag);
if (res < 0) {
LogError("osEventFlagsSet failed %d", res);
int res = osMutexAcquire(network_state_lock, osWaitForever);
if (res != osOK) {
LogError("osMutexAcquire failed %d", res);
return;
}
bool latest_network_up = (event == NETWORK_UP);
if (latest_network_up != network_up) {
LogInfo("network state changed");
network_up = latest_network_up;
res = osEventFlagsSet(event_flags_id, AZURE_NETWORK_CHANGE_FLAG);
if (res < 0) {
LogError("osEventFlagsSet failed %d", res);
}
}
res = osMutexRelease(network_state_lock);
if (res != osOK) {
LogError("osMutexRelease failed %d", res);
return;
}
}
static bool is_network_up()
{
int result = false;
int res = osMutexAcquire(network_state_lock, osWaitForever);
if (res != osOK) {
LogError("osMutexAcquire failed %d", res);
goto exit;
}
result = network_up;
res = osMutexRelease(network_state_lock);
if (res != osOK) {
LogError("osMutexRelease failed %d", res);
}
exit:
return result;
}
#ifdef IOTSDK_AZURE_SDK_NTP_TIME
......@@ -89,6 +116,14 @@ int platform_init(void)
return MU_FAILURE;
}
// The state of the network should be updated by the network task started below.
network_up = false;
network_state_lock = osMutexNew(NULL);
if (network_state_lock == NULL) {
LogError("Create mutex failed");
return MU_FAILURE;
}
#ifdef IOTSDK_AZURE_SDK_NTP_TIME
res = iotNtpClientInit(NULL);
if (res != IOT_NTP_CLIENT_OK) {
......@@ -103,10 +138,12 @@ int platform_init(void)
goto cleanup;
}
res = wait_for_event(AZURE_NETWORK_UP_FLAG);
if (res != 0) {
LogError("Network not up");
goto cleanup;
if (!is_network_up()) {
res = wait_for_event(AZURE_NETWORK_CHANGE_FLAG);
if (res != 0 || !is_network_up()) {
LogError("Network not up");
goto cleanup;
}
}
#ifdef IOTSDK_AZURE_SDK_NTP_TIME
......@@ -154,6 +191,11 @@ void platform_deinit(void)
}
#endif // IOTSDK_AZURE_SDK_NTP_TIME
res = osMutexDelete(network_state_lock);
if (res != osOK) {
LogError("Delete event flags failed %d", res);
}
res = osEventFlagsDelete(event_flags_id);
if (res != osOK) {
LogError("Delete event flags failed %d", res);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment