Skip to content

Commit

Permalink
Improved the reconnection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLlobet committed Jan 22, 2024
1 parent bbeec90 commit 97ba143
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
43 changes: 34 additions & 9 deletions csrc/src/wifi_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,13 @@ void wifi_task(void *param)
retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 1, 0, 0, 1), NULL, 0);

// Think about scan parameters

/* Start Wifi Connection */
wifi_status.connection = wifi_connecting;

/*_u8 MacAddr[6] = 0xD0, 0x5F, 0xB8, 0x4B, 0xD3, 0x74 }; */
SlSecParams_t secParams;
secParams.Key = (signed char*) config_get_wifi_key();
secParams.KeyLen = strlen(config_get_wifi_key());
secParams.Type = SL_SEC_TYPE_WPA_WPA2;

retVal = sl_WlanConnect((_i8*) config_get_wifi_ssid(), (_i16) strlen(config_get_wifi_ssid()),
NULL, &secParams, NULL);
wifi_status.connection = wifi_connecting;
/* Start Wifi Connection */
xTaskNotify(wifi_task_handle, ((uint32_t )wifi_connecting | WIFI_PENDING_STATE), eSetBits);

/* Connection manager logic */
while(1)
Expand Down Expand Up @@ -188,7 +184,22 @@ void wifi_task(void *param)
sl_iostream_printf(sl_iostream_swo_handle, "Wifi Disconnected %x\n\r",
ulNotifiedValue);

break; // Break the loop
wifi_status.connection = wifi_connecting;
xTaskNotify(wifi_task_handle, ((uint32_t )wifi_connecting | WIFI_PENDING_STATE), eSetBits);
//break; // Break the loop
}

if(ulNotifiedValue & (uint32_t)wifi_connecting)
{
/*_u8 MacAddr[6] = 0xD0, 0x5F, 0xB8, 0x4B, 0xD3, 0x74 }; */
secParams.Key = (signed char*) config_get_wifi_key();
secParams.KeyLen = strlen(config_get_wifi_key());
secParams.Type = SL_SEC_TYPE_WPA_WPA2;

if(0 != sl_WlanConnect((_i8*) config_get_wifi_ssid(), (_i16) strlen(config_get_wifi_ssid()), NULL, &secParams, NULL))
{
break;
}
}

if (ulNotifiedValue & (uint32_t) wifi_ip_v4_acquired)
Expand All @@ -214,7 +225,21 @@ void wifi_task(void *param)
}
} else
{
// Wifi Service House-Keeping
sl_iostream_printf(sl_iostream_swo_handle, "No event\n\r");
// Wake-up services that need networking for house-keeping
if((wifi_status.connection == wifi_connected))
{
if(eTaskGetState(network_monitor_task_handle) == eSuspended)
{
vTaskResume(network_monitor_task_handle);
miso_notify_event(miso_connectivity_on);
}
}
else
{
// Reconnection logic
}
}

} //while (1);
Expand Down
4 changes: 2 additions & 2 deletions src/connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const board = @import("microzig").board;
const freertos = @import("freertos.zig");
const config = @import("config.zig");
const system = @import("system.zig");
pub const mbedtls = @import("mbedtls.zig");
//pub const mbedtls = @import("mbedtls.zig");

const c = @cImport({
@cInclude("network.h");
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn Connection(comptime id: connection_id, comptime sslType: type) type {
_ = self.ssl.init(self.proto) catch {
return connection_error.ssl_init_error;
};
_ = mbedtls.c.miso_network_register_ssl_context(@ptrCast(self.ctx), &self.ssl.context);
_ = c.miso_network_register_ssl_context(@ptrCast(self.ctx), @ptrCast(&self.ssl.context));
}

if (0 != c.miso_create_network_connection(self.ctx, @as([*c]const u8, host.ptr), host.len, port, local_port orelse 0, @as(c.enum_miso_protocol, @intFromEnum(self.proto)))) {
Expand Down
1 change: 0 additions & 1 deletion src/mbedtls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ pub fn TlsContext(comptime mode: connection.security_mode) type {
peer_crt: c.mbedtls_x509_crt,
/// Peer CRL context
peer_crl: c.mbedtls_x509_crl,

/// Own PK
own_pk: c.mbedtls_pk_context,
} else struct {},
Expand Down
13 changes: 5 additions & 8 deletions src/mqtt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const freertos = @import("freertos.zig");
const config = @import("config.zig");
const system = @import("system.zig");
const connection = @import("connection.zig");
const user = @import("user.zig");
const mbedtls = @import("mbedtls.zig");

const c = @cImport({
Expand Down Expand Up @@ -155,16 +154,16 @@ fn init() @This() {
}

/// Authentification callback for mbedTLS connections
fn authCallback(self: *mbedtls.TlsContext(.psk), security_mode: connection.security_mode) connection.mbedtls.auth_error!void {
fn authCallback(self: *mbedtls.TlsContext(.psk), security_mode: connection.security_mode) mbedtls.auth_error!void {
if (security_mode == .psk) {
var psk_buf: [64]u8 = undefined; // Request 64 Bytes for Base64 decoder

const psk = connection.mbedtls.base64Decode(c.config_get_mqtt_psk_key(), &psk_buf) catch return connection.mbedtls.auth_error.generic_error;
self.confPsk(psk, c.config_get_mqtt_psk_id()) catch return connection.mbedtls.auth_error.generic_error;
const psk = mbedtls.base64Decode(c.config_get_mqtt_psk_key(), &psk_buf) catch return mbedtls.auth_error.generic_error;
self.confPsk(psk, c.config_get_mqtt_psk_id()) catch return mbedtls.auth_error.generic_error;

@memset(&psk_buf, 0); // Sanitize the buffer to avoid the decoded psk to remain in stack
} else {
return connection.mbedtls.auth_error.unsuported_mode;
return mbedtls.auth_error.unsuported_mode;
}
}

Expand Down Expand Up @@ -723,9 +722,7 @@ fn loop(self: *@This(), uri: std.Uri) !void {
} else {
_ = c.printf("mqtt: %d\r\n", self.task.getStackHighWaterMark());
}

// Process the qos1 to 2 tx queue

// Check if the task has been suspended
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/network.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ pub fn start() void {
// Create the HTTP service
http.service.create();
}

pub export fn get_lwm2m_task_handle() callconv(.C) freertos.TaskHandle_t {
return lwm2m.service.getTaskHandle();
}

pub export fn get_mqtt_task_handle() callconv(.C) freertos.TaskHandle_t {
return mqtt.service.getTaskHandle();
}
4 changes: 2 additions & 2 deletions src/user.zig
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn myUserTaskFunction(self: *@This()) void {
_ = c.printf("NTP Sync: %d\r\n", system.time.now());
self.ntpTimer.changePeriod(nextSyncTime, null) catch unreachable;
self.state = .perform_firmware_download;
self.state = .start_mqtt;
//self.state = .start_mqtt;
} else |_| {
self.ntpSyncTime = 0;
self.task.delayTask(16000); // wait for 16
Expand All @@ -144,7 +144,7 @@ fn myUserTaskFunction(self: *@This()) void {
if (downloadAndVerify()) |_| {
// Happy path

nvm.setUpdateRequest() catch unreachable;
//nvm.setUpdateRequest() catch unreachable;

_ = c.printf("Firmware download complete\r\n");

Expand Down

0 comments on commit 97ba143

Please sign in to comment.