Skip to content

Commit

Permalink
Modifying the connection to improve the dependency flow
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLlobet committed Jan 21, 2024
1 parent 3e5733f commit 0861eda
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 231 deletions.
47 changes: 44 additions & 3 deletions csrc/inc/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

#define UISO_PROTOCOL_MASK (UISO_PROTOCOL_BIT_MASK | UISO_UDP_SELECTION_BIT_MASK | UISO_TCP_SELECTION_BIT_MASK | UISO_IPV4_IPV6_SELECTION_BIT_MASK | UISO_SECURITY_BIT_MASK)

/**
* Protocol Selection Bit Masks
*
* Do not use directly
*/
enum
{
miso_protocol_udp = (UISO_UDP_SELECTION_BIT_MASK | UISO_PROTOCOL_BIT_MASK),
Expand All @@ -39,6 +44,9 @@ enum
miso_protocol_ip6 = (1 << UISO_IPV4_IPV6_SELECTION_BIT)| UISO_PROTOCOL_BIT_MASK,
};

/**
* MISO Protocol selction
*/
enum miso_protocol
{
miso_protocol_no_protocol = 0,
Expand All @@ -58,9 +66,28 @@ enum miso_protocol
miso_protocol_dtls_ip6 = (miso_protocol_udp_ip6 | UISO_SECURITY_BIT_MASK) & UISO_PROTOCOL_MASK,
miso_protocol_tls_ip6 = (miso_protocol_tcp_ip6 | UISO_SECURITY_BIT_MASK) & UISO_PROTOCOL_MASK,

miso_protocol_max = UISO_PROTOCOL_MASK,
miso_protocol_max = UISO_PROTOCOL_MASK, // Do not use
};

/**
* miso security mode
*/
enum miso_security_mode{
miso_security_mode_none = 0,
miso_security_mode_psk = 1,
miso_security_mode_ec = 2,
miso_security_mode_rsa = 3,
};


/**
* Socket ID
*
* Currently only 4 sockets are supported in the pool.
*
* Each socket is identified by an ID and associated with a service.
*
*/
enum wifi_socket_id_e
{
wifi_service_ntp_socket = 0,
Expand All @@ -70,10 +97,13 @@ enum wifi_socket_id_e
wifi_service_max
};

/**
* Network Context
*
* Forward declaration of the network context.
*/
typedef struct miso_sockets_s * miso_network_ctx_t;

// socket id?

enum miso_network_ret
{
UISO_NETWORK_OK = 0,
Expand Down Expand Up @@ -114,6 +144,10 @@ miso_network_ctx_t miso_get_network_ctx(enum wifi_socket_id_e id);
int miso_create_network_connection(miso_network_ctx_t ctx, const char *host, size_t host_len, uint16_t port, uint16_t local_port,
enum miso_protocol proto);

/**
* Close Connection
*
*/
int miso_close_network_connection(miso_network_ctx_t ctx);

/**
Expand All @@ -124,7 +158,14 @@ int miso_close_network_connection(miso_network_ctx_t ctx);
*/
int miso_network_register_ssl_context(miso_network_ctx_t ctx, mbedtls_ssl_context * ssl_ctx);

/**
* Read from socket
*/
int miso_network_read(miso_network_ctx_t ctx, unsigned char *buffer, size_t length);

/**
* Write (send) to socket
*/
int miso_network_send(miso_network_ctx_t ctx, const unsigned char *buffer, size_t length);

int miso_network_get_socket(miso_network_ctx_t ctx);
Expand Down
2 changes: 2 additions & 0 deletions csrc/inc/wifi_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ int enqueue_select_tx(enum wifi_socket_id_e id, uint32_t timeout_s);





void create_wifi_service_task(void);


Expand Down
1 change: 0 additions & 1 deletion csrc/src/lwm2m/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#define SIMPLE_LINK_MAX_SEND_MTU 1472

extern mbedtls_ssl_context ssl_context;
//extern miso_mbedtls_context_t net_context;

connection_t connection_create(connection_t connList, char *host, char *port,
int protocol)
Expand Down
2 changes: 0 additions & 2 deletions csrc/src/lwm2m/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ struct connection_s
miso_network_ctx_t ctx;
};

int create_socket(const char *portStr, int ai_family);

connection_t connection_find(connection_t connList, struct sockaddr_in *addr, size_t addrLen);
connection_t connection_new_incoming(connection_t connList,
struct miso_mbedtls_context_s *connection);
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 = true;
pub const enable_mqtt = false;
pub const enable_lwm2m = false;
pub const enable_mqtt = true;
pub const enable_http = true;

pub const rtos_prio_boot_app = @intFromEnum(task_priorities.rtos_prio_highest);
Expand Down
133 changes: 73 additions & 60 deletions src/connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const c = @cImport({
@cInclude("lwm2m_client.h");
});

/// Connection Errors
pub const connection_error = error{
/// Create Connection Error
/// Could not create a connection to the host
Expand All @@ -19,27 +20,34 @@ pub const connection_error = error{
/// SSL Init error
ssl_init_error,

/// Close Connection Error
close_error,

/// Send Error
send_error,

/// Recieve Error
recieve_error,

/// Possible buffer overflow
buffer_owerflow,
};

/// Network Context from C
const network_ctx = c.miso_network_ctx_t;

/// Connections are based on a fixed connection pool
///
/// Each connection has an id mapped to the protocol used
///
pub const connection_id = enum(usize) {
ntp = c.wifi_service_ntp_socket,
lwm2m = c.wifi_service_lwm2m_socket,
mqtt = c.wifi_service_mqtt_socket,
http = c.wifi_service_http_socket,
};

/// Protocol Enumerations
pub const protocol = enum(u32) {
no_protocol = c.miso_protocol_no_protocol,

Expand All @@ -55,6 +63,7 @@ pub const protocol = enum(u32) {
dtls_ip6 = c.miso_protocol_dtls_ip6,
tls_ip6 = c.miso_protocol_tls_ip6,

/// Checks if the protocol is secure (TLS/DTLS)
pub fn isSecure(self: @This()) bool {
return switch (self) {
.dtls_ip4, .tls_ip4, .dtls_ip6, .tls_ip6 => true,
Expand All @@ -63,11 +72,17 @@ pub const protocol = enum(u32) {
}
};

/// Security Mode Enumerations
///
pub const security_mode = enum(u32) {
no_sec = 0,
psk = 1,
certificate_ec,
certificate_rsa,
/// No security
no_sec = c.miso_security_mode_none,
/// Pre-Shared Key
psk = c.miso_security_mode_psk,
/// Certificate EC
certificate_ec = c.miso_security_mode_ec,
/// Certificate RSA
certificate_rsa = c.miso_security_mode_rsa,
};

pub const schemes = enum(u32) {
Expand Down Expand Up @@ -106,66 +121,64 @@ pub const schemes = enum(u32) {
}
};

ctx: network_ctx,
proto: protocol,
id: connection_id,
ssl: mbedtls,

pub fn init(id: connection_id, credential_callback: ?mbedtls.credential_callback_fn, custom_ssl_init: ?mbedtls.custom_init_callback_fn) @This() {
// Think about how to rewrite this for non-ssl connections
return @This(){ .id = id, .ctx = c.miso_get_network_ctx(@as(c_uint, @intCast(@intFromEnum(id)))), .proto = undefined, .ssl = mbedtls.create(credential_callback, custom_ssl_init, null) };
}
pub fn Connection(comptime id: connection_id, comptime sslType: type) type {
return struct {
ctx: network_ctx,
proto: protocol,
ssl: sslType,

/// Create a connection to a host
///
pub fn create(self: *@This(), uri: std.Uri, local_port: ?u16, mode: ?security_mode) !void {
self.proto = schemes.match(uri.scheme).?.getProtocol();
/// Initialize the connection
pub fn init(self: *@This()) void {
self.ctx = c.miso_get_network_ctx(@as(c_uint, @intCast(@intFromEnum(id))));
self.proto = protocol.no_protocol;
}

const host = uri.host.?;
const port = uri.port.?;
pub fn create(self: *@This(), uri: std.Uri, local_port: ?u16) !void {
self.proto = schemes.match(uri.scheme).?.getProtocol();

if (self.proto.isSecure()) {
_ = self.ssl.init(self, self.proto, mode.?) catch {
return connection_error.ssl_init_error;
};
}
const host = uri.host.?;
const port = uri.port.?;

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)))) {
return connection_error.create_error;
}
}
if (sslType != void) {
_ = 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);
}

pub fn close(self: *@This()) !void {
defer {
if (self.proto.isSecure()) {
_ = self.ssl.deinit();
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)))) {
return connection_error.create_error;
}
}
}

if (0 != c.miso_close_network_connection(self.ctx)) {
return connection_error.close_error;
}
}

/// Send Function
pub fn send(self: *@This(), buffer: []const u8) !usize {
const len: isize = c.miso_network_send(self.ctx, @as([*c]const u8, buffer.ptr), buffer.len);
return if (len <= 0) connection_error.send_error else @intCast(len);
}
/// Recieve Function
pub fn recieve(self: *@This(), buffer: []u8) ![]u8 {
const len: isize = c.miso_network_read(self.ctx, buffer.ptr, buffer.len);
return if (len <= 0)
connection_error.recieve_error
else if (@as(usize, @intCast(len)) > buffer.len)
connection_error.buffer_owerflow
else
buffer[0..@intCast(len)];
}

pub fn waitRx(self: *@This(), timeout_s: u32) i32 {
return c.wait_rx(self.ctx, timeout_s);
}
pub fn waitTx(self: *@This(), timeout_s: u32) i32 {
return c.wait_tx(self.ctx, timeout_s);
pub fn close(self: *@This()) !void {
defer {
if (sslType != void) {
_ = self.ssl.deinit();
}
}

if (0 != c.miso_close_network_connection(self.ctx)) {
return connection_error.close_error;
}
}
pub fn send(self: *@This(), buffer: []const u8) !usize {
const len: isize = c.miso_network_send(self.ctx, @as([*c]const u8, buffer.ptr), buffer.len);
return if (len <= 0) connection_error.send_error else @intCast(len);
}
pub fn recieve(self: *@This(), buffer: []u8) ![]u8 {
const len: isize = c.miso_network_read(self.ctx, buffer.ptr, buffer.len);
return if (len <= 0)
connection_error.recieve_error
else if (@as(usize, @intCast(len)) > buffer.len)
connection_error.buffer_owerflow
else
buffer[0..@intCast(len)];
}
pub fn waitRx(self: *@This(), timeout_s: u32) i32 {
return c.wait_rx(self.ctx, timeout_s);
}
pub fn waitTx(self: *@This(), timeout_s: u32) i32 {
return c.wait_tx(self.ctx, timeout_s);
}
};
}
11 changes: 4 additions & 7 deletions src/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const c = @cImport({
});

/// Connection instance
connection: connection,
connection: connection.Connection(.http, void),

/// Array to store parsed header information
headers: [24]c.phr_header,
Expand Down Expand Up @@ -295,10 +295,7 @@ pub fn filedownload(self: *@This(), url: []const u8, file_name: [*:0]const u8, c
self.connection.close() catch {};
}

// Change this to support TLS on HTTP
const proto = connection.schemes.match(uri.scheme).?.getProtocol();

try self.connection.create(uri, null, if (proto.isSecure()) .psk else null);
try self.connection.create(uri, null);
defer {
self.connection.close() catch {};
}
Expand Down Expand Up @@ -388,7 +385,7 @@ pub fn filedownload(self: *@This(), url: []const u8, file_name: [*:0]const u8, c
// Reconnect logic
try self.connection.close();

try self.connection.create(uri, null, if (proto.isSecure()) .psk else null);
try self.connection.create(uri, null);
} else {
// Keep Alive
}
Expand All @@ -408,7 +405,7 @@ pub fn eTag(self: *@This()) ?[]const u8 {

pub fn create(self: *@This()) void {
if (config.enable_http) {
//self.connection = connection.init(.http, authCallback, null); // mem corruption
self.connection.init(); // mem corruption
self.tx_mutex.create() catch unreachable;
self.rx_mutex.create() catch unreachable;
}
Expand Down
Loading

0 comments on commit 0861eda

Please sign in to comment.