Skip to content

Commit

Permalink
Making it compilable
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLlobet committed Feb 10, 2024
1 parent 9bce4bb commit 10c628e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 71 deletions.
49 changes: 8 additions & 41 deletions src/connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub const schemes = enum(u32) {
};

pub fn Connection(comptime id: connection_id, comptime sslType: type) type {
_ = id;
// Compile time checks
if (sslType != void) {
if (!@hasDecl(sslType, "init")) {
Expand All @@ -164,62 +165,28 @@ pub fn Connection(comptime id: connection_id, comptime sslType: type) type {
}

return struct {
ctx: network_ctx,
proto: proto,
ssl: sslType,

/// Initialize the connection
pub fn init(self: *@This()) void {
self.ctx = c.miso_get_network_ctx(@as(c_uint, @intCast(@intFromEnum(id))));
self.proto = .no_protocol;
_ = self;
//self.ssl.init();
}

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

const host = uri.host.?;
const port = uri.port.?;

if (sslType != void) {
_ = self.ssl.init(self.proto) catch {
return connection_error.ssl_init_error;
};
_ = 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)))) {
return connection_error.create_error;
}
try self.ssl.open(uri, local_port);
}
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;
}
try self.ssl.close();
}
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);
return self.ssl.send(buffer);
}
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)];
return self.ssl.recieve(buffer);
}
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);
return self.waitRx(timeout_s);
}
};
}
4 changes: 2 additions & 2 deletions src/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const system = @import("system.zig");
const connection = @import("connection.zig");
const file = @import("fatfs.zig").file;
const led = @import("leds.zig");

const simpleConnection = @import("simpleConnection.zig");
const c = @cImport({
@cInclude("board.h");
@cInclude("picohttpparser.h");
});

/// Connection instance
connection: connection.Connection(.http, void),
connection: connection.Connection(.http, simpleConnection.SimpleLinkConnection(.tcp_ip4)),

/// Array to store parsed header information
headers: [24]c.phr_header,
Expand Down
52 changes: 25 additions & 27 deletions src/mbedtls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,31 @@ pub fn TlsContext(comptime T: type, comptime conn: type, comptime mode: connecti

try self.init(proto);
errdefer {
self.deinit();
_ = self.deinit();
}

try self.connection.open(uri, local_port);
}
pub fn close(self: *@This()) !void {
defer {
_ = self.deinit();
}
try self.connection.close();
}

pub fn send(self: *@This(), buffer: []const u8) !usize {
if(self.conn.getProtocol().isTls())
{
//
}
else {
return self.connection.send_dtls(buffer);
if (self.connection.getProto().isTls()) {
return 1;
} else {
return self.send_dtls(buffer);
}
}
fn send_dtls(self: *@This(), buffer: []const u8) !usize {
var offset: usize = 0;
var cid_enabled: c_int = c.MBEDTLS_SSL_CID_DISABLED;

var ret: i32 = c.mbedtls_ssl_get_peer_cid(&self.context, &cid_enabled, &self.cid, null, 0);
if (cid_enabled == c.MBEDTLS_CID_DISABLED) {
var ret: i32 = c.mbedtls_ssl_get_peer_cid(&self.context, &cid_enabled, null, 0);
if (cid_enabled == c.MBEDTLS_SSL_CID_DISABLED) {
//
} else {
ret = 0;
Expand All @@ -171,41 +175,35 @@ pub fn TlsContext(comptime T: type, comptime conn: type, comptime mode: connecti
while (offset != buffer.len) {
const slice = buffer[offset..];
const num_bytes = c.mbedtls_ssl_write(&self.context, @ptrCast(slice.ptr), @intCast(slice.len));
if ((c.MBEDTLS_ERR_SSL_WANT_READ == num_bytes) || (c.MBEDTLS_ERR_SSL_WANT_WRITE == num_bytes) || (c.MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS == num_bytes) || (c.MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS == num_bytes))
{
if ((c.MBEDTLS_ERR_SSL_WANT_READ == num_bytes) or (c.MBEDTLS_ERR_SSL_WANT_WRITE == num_bytes) or (c.MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS == num_bytes) or (c.MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS == num_bytes)) {
continue;
}
else if (num_bytes < 0)
{
ret = -1;
break;
}
else{
} else if (num_bytes < 0) {
ret = -1;
break;
} else {
offset += @as(usize, @intCast(num_bytes));
}
}
}

return if(ret == 0) offset else 0;
return if (ret == 0) offset else connection.connection_error.send_error;
}

fn read_dlts(self: *@This(), buffer: []u8) ![]u8 {
fn read_dtls(self: *@This(), buffer: []u8) ![]u8 {
var numBytes: isize = c.MBEDTLS_ERR_SSL_WANT_READ;

while ((numBytes == c.MBEDTLS_ERR_SSL_WANT_READ) or (numBytes == c.MBEDTLS_ERR_SSL_WANT_WRITE) or (numBytes == c.MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) or (numBytes == c.MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) or (numBytes == c.MBEDTLS_ERR_SSL_CLIENT_RECONNECT)) {
numBytes = c.mbedtls_ssl_read(&self.context, @ptrCast(buffer.ptr), @intCast(buffer.len));
}

return if (numBytes < 0) 0 else buffer[0..@intCast(numBytes)];
return if (numBytes < 0) connection.connection_error.recieve_error else buffer[0..@intCast(numBytes)];
}

pub fn recieve(self: *@This(), buffer: []u8) ![]u8 {
if(self.conn.getProtocol().isTls())
{
//
}
else {
return self.connection.read_dtls(buffer);
if (self.connection.getProto().isTls()) {
return buffer;
} else {
return self.read_dtls(buffer);
}
}
/// Send data callback for MbedTLS
Expand Down
12 changes: 11 additions & 1 deletion src/simpleConnection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ pub fn SimpleLinkConnection(comptime proto: connection.proto) type {
};
}

pub fn init(self: *@This()) !void {
self.sd = @intFromEnum(sd_e.invalid);
}
pub fn deinit(self: *@This()) !void {
_ = self;
//if (self.sd != @intFromEnum(sd_e.invalid)) {
// _ = self.close();
//}
}

/// Open a connection to designated peer
pub fn open(self: *@This(), uri: std.Uri, local_port: ?u16) !void {
const host = uri.host.?;
Expand Down Expand Up @@ -195,7 +205,7 @@ pub fn SimpleLinkConnection(comptime proto: connection.proto) type {
}

/// Send
pub fn send(self: *@This(), data: []u8) !usize {
pub fn send(self: *@This(), data: []const u8) !usize {
var ret: isize = undefined;
if (comptime proto.isTcp()) {
ret = c.sl_Send(self.sd, @ptrCast(data.ptr), @intCast(data.len), 0);
Expand Down

0 comments on commit 10c628e

Please sign in to comment.