Skip to content

Commit

Permalink
Stabilizing re-connection logic in lwm2m
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLlobet committed Jan 22, 2024
1 parent 97ba143 commit cdd5853
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 87 deletions.
2 changes: 1 addition & 1 deletion csrc/config/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ extern "C" {
#define configCPU_CLOCK_HZ (( unsigned long )48000000 )
#define configMAX_PRIORITIES ( 7 )
#define configMINIMAL_STACK_SIZE (( unsigned short ) 160)
#define configTOTAL_HEAP_SIZE (( size_t )(62 * 1024 )) // MQTT BUILD: 62kB heap
#define configTOTAL_HEAP_SIZE (( size_t )(53 * 1024 )) // MQTT BUILD: 62kB heap, LWM2M BUILD: 53kB heap
#define configMAX_TASK_NAME_LEN ( 10 )
#define configUSE_TRACE_FACILITY ( 0 )
#define configUSE_16_BIT_TICKS ( 0 )
Expand Down
12 changes: 7 additions & 5 deletions csrc/src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,15 @@ int _send_dtls(miso_network_ctx_t ctx, const unsigned char *buffer, size_t lengt
{
if ((current_time - ctx->last_send_time) > 120)
{
// Not supported when watchdog is enabled

/* Attempt re-negotiation */
do
{
ret = mbedtls_ssl_renegotiate(ctx->ssl_context);
} while ((MBEDTLS_ERR_SSL_WANT_READ == ret) || (MBEDTLS_ERR_SSL_WANT_WRITE == ret) || (MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS == ret));
//do
//{
// ret = mbedtls_ssl_renegotiate(ctx->ssl_context);
//} while ((MBEDTLS_ERR_SSL_WANT_READ == ret) || (MBEDTLS_ERR_SSL_WANT_WRITE == ret) || (MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS == ret));

if (0 != ret)
if (0 == ret)
{
/* This code tries to handle issues seen when the server does not support renegociation */
ret = mbedtls_ssl_close_notify(ctx->ssl_context);
Expand Down
4 changes: 2 additions & 2 deletions src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const task_priorities = enum(freertos.BaseType_t) {
pub const min_task_stack_depth: u16 = freertos.c.configMINIMAL_STACK_SIZE;

// Enable or Disable features at compile time
pub const enable_lwm2m = false;
pub const enable_mqtt = true;
pub const enable_lwm2m = true;
pub const enable_mqtt = false;
pub const enable_http = true;

pub const rtos_prio_boot_app = @intFromEnum(task_priorities.rtos_prio_highest);
Expand Down
28 changes: 7 additions & 21 deletions src/mbedtls.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
//const freertos = @import("freertos.zig");
const connection = @import("connection.zig");
pub const c = @cImport({
const c = @cImport({
@cDefine("MBEDTLS_CONFIG_FILE", "\"miso_mbedtls_config.h\"");
@cInclude("miso_config.h");
@cInclude("network.h");
Expand All @@ -12,7 +12,7 @@ pub const c = @cImport({
@cInclude("mbedtls/base64.h");
@cInclude("mbedtls/net_sockets.h");
@cInclude("mbedtls/entropy.h");
}); // pub in order to be able to import from client modules
});

const ciphersuites_psk = [_]c_int{
c.MBEDTLS_TLS_PSK_WITH_AES_128_GCM_SHA256,
Expand Down Expand Up @@ -41,10 +41,7 @@ pub const auth_error = error{
unsuported_mode,
};

pub const mbedtls_error = error{
psk_conf_error,
generic_error,
};
pub const mbedtls_error = error{ psk_conf_error, generic_error, init_error, no_sec };

pub const init_error = error{};

Expand Down Expand Up @@ -96,7 +93,7 @@ pub fn TlsContext(comptime mode: connection.security_mode) type {
custom_cleanup_callback: ?custom_cleanup_callback_fn = null,

// Default auth callback
pub fn create(auth_callback: credential_callback_fn, custom_init: ?custom_init_callback_fn, custom_cleanup: ?custom_cleanup_callback_fn) @This() {
pub fn create(comptime auth_callback: credential_callback_fn, custom_init: ?custom_init_callback_fn, custom_cleanup: ?custom_cleanup_callback_fn) @This() {
return @This(){ .auth_callback = auth_callback, .custom_init_callback = custom_init, .custom_cleanup_callback = custom_cleanup, .context = undefined, .timer = undefined, .config = undefined, .drbg = undefined, .entropy = undefined, .entropy_seed = 0x55555555, .ec = undefined };
}
pub fn cleanup(self: *@This()) void {
Expand All @@ -120,12 +117,12 @@ pub fn TlsContext(comptime mode: connection.security_mode) type {
c.mbedtls_ssl_config_free(&self.config);
}
}
pub fn init(self: *@This(), protocol: connection.protocol) !i32 {
pub fn init(self: *@This(), protocol: connection.protocol) !void {
var ret: i32 = mbedtls_nok;

if (!connection.protocol.isSecure(protocol))
// Running init on a non secure protocol
return mbedtls_nok; // should return a different error code
return mbedtls_error.no_sec;

// custom init callback
if (self.custom_init_callback) |custom| {
Expand Down Expand Up @@ -214,10 +211,8 @@ pub fn TlsContext(comptime mode: connection.security_mode) type {
}

if (ret != mbedtls_ok) {
// init failure
return mbedtls_error.init_error;
}

return ret;
}
pub fn deinit(self: *@This()) i32 {
var ret: i32 = 0;
Expand All @@ -232,15 +227,6 @@ pub fn TlsContext(comptime mode: connection.security_mode) type {
};
}

/// Helper function to get the credential callback function in custom init
//pub fn getCredentialCallbackFn(self: *@This()) ?credential_callback_fn {
// return self.auth_callback;
//}

pub fn confPsk(self: *@This(), psk: []u8, psk_id: [*:0]u8) !void {
return if (mbedtls_ok != c.mbedtls_ssl_conf_psk(&self.config, psk.ptr, psk.len, &psk_id[0], c.strlen(psk_id))) mbedtls_error.psk_conf_error else {};
}

pub fn base64Decode(input: [*:0]u8, output: []u8) ![]u8 {
var len: usize = 0;

Expand Down
58 changes: 0 additions & 58 deletions src/newConnection.zig

This file was deleted.

0 comments on commit cdd5853

Please sign in to comment.