Skip to content

Commit

Permalink
ntp code
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoLlobet committed Jan 18, 2024
1 parent 4f1e6ca commit de611b9
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 34 deletions.
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@
"miso_mbedtls_config.h": "c",
"freertos.h": "c",
"freertosconfig.h": "c",
"os_malloc.h": "c"
"os_malloc.h": "c",
"trace.h": "c",
"timers.h": "c",
"semphr.h": "c",
"task.h": "c",
"miso_ntp.h": "c"
},
"cmake.configureOnOpen": false
}
17 changes: 9 additions & 8 deletions csrc/inc/miso_ntp.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
/**
* miso_ntp.h
*
* Embedded implementation for sNTP client
*
* Implements RFC4430 (2006)
* Implements RFC4330 (2006)
* Simple Network Time Protocol (SNTP) Version 4 for IPv4, IPv6 and OSI
* https://www.rfc-editor.org/rfc/rfc4330
*
* Also reference SNTP v3
* Also references SNTP v3
* https://www.rfc-editor.org/rfc/rfc1769
*
* Created on: 13 nov 2022
Expand All @@ -33,8 +34,8 @@ struct ntp_packet_s
uint8_t stratum; /* Stratum Byte */
uint8_t poll_interval; /* Poll interval Byte */
uint8_t precision; /* Precision Byte */
uint8_t root_delay[4]; /* */
uint8_t root_dispersion[4];
uint8_t root_delay[4]; /* Root Delay (32-Bit) */
uint8_t root_dispersion[4]; /* Root dispersion */
uint8_t reference_indentifier[4];
uint8_t reference_timestamp_seconds[4];
uint8_t reference_timestamp_fraction[4];
Expand All @@ -44,8 +45,8 @@ struct ntp_packet_s
uint8_t recieve_timestamp_fraction[4];
uint8_t transmit_timestamp_seconds[4];
uint8_t transmit_timestamp_fraction[4];
// uint8_t key_identifier[4]; /* Optional */
// uint8_t message_digest[16]; /* Optional */
// uint8_t key_identifier[4]; /* Optional Key Identifier (v4) */
// uint8_t message_digest[16]; /* Message Digest (v4) */
};


Expand All @@ -68,7 +69,7 @@ struct ntp_packet_s

enum{

max_timer_interval_s = UINT32_MAX / 1000, // Using 32-Bit timer
max_timer_interval_s = (UINT32_MAX - 1) / 1000, // Using 32-Bit timer
max_timer_interval_ms = max_timer_interval_s * 1000,

min_polling_interval_ms = (1 << 4) * 1024,
Expand Down
3 changes: 1 addition & 2 deletions csrc/src/miso_ntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ enum sntp_return_codes_e miso_sntp_request(sntp_server_t *server,

if (sntp_success == sntp_rcode)
{
if ((uint64_t) 0
== *(uint64_t*) &ntp_packet.transmit_timestamp_seconds[0])
if (((uint32_t) 0 == *(uint32_t*) &ntp_packet.transmit_timestamp_seconds[0]) && ((uint32_t) 0 == *(uint32_t*) &ntp_packet.transmit_timestamp_fraction[0]))
{
sntp_rcode = sntp_server_transmit_zero;
}
Expand Down
2 changes: 0 additions & 2 deletions csrc/src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ struct miso_sockets_s
SlSocklen_t peer_len;

mbedtls_ssl_context *ssl_context; /* Optional SSL context */
// ssl connect callback
//int (*ssl_cleanup)(mbedtls_ssl_context *ssl); // optional SSL cleanup function

uint32_t last_send_time; // Last time we sent a packet
uint32_t last_recv_time; // Last time we received a packet
Expand Down
39 changes: 39 additions & 0 deletions src/boards/xdk110.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,49 @@ pub const c = @cImport({

pub const cpu_frequency = 48_000_000; // 48MHz

/// Get local time
pub fn getTime() u32 {
return c.sl_sleeptimer_get_time();
}

/// Get local Ntp time
pub fn getNtpTime() u32 {
var ntp_time: u32 = undefined;

return if (c.SL_STATUS_OK == c.sl_sleeptimer_convert_unix_time_to_ntp(c.sl_sleeptimer_get_time(), &ntp_time)) ntp_time else 0;
}

/// Set local time from NTP time
pub fn setTimeFromNtp(ntp_time: u32) !void {
var time_stamp: u32 = undefined;
//var rc : c.sl_status_t = undefined;
if (c.SL_STATUS_OK != c.sl_sleeptimer_convert_ntp_time_to_unix(ntp_time, &time_stamp)) {
// return
}
if (c.SL_STATUS_OK != c.sl_sleeptimer_set_time(time_stamp)) {
// return
}

var sl_date: c.sl_sleeptimer_date_t = undefined;

if (c.SL_STATUS_OK == c.sl_sleeptimer_get_datetime(&sl_date)) {
var simple_link_date: c.SlDateTime_t = undefined;

simple_link_date.sl_tm_day = sl_date.month_day;
simple_link_date.sl_tm_mon = sl_date.month + 1;
simple_link_date.sl_tm_year = sl_date.year + 1900;
simple_link_date.sl_tm_hour = sl_date.hour;
simple_link_date.sl_tm_min = sl_date.min;
simple_link_date.sl_tm_sec = sl_date.sec;

if (0 > c.sl_DevSet(c.SL_DEVICE_GENERAL_CONFIGURATION, c.SL_DEVICE_GENERAL_CONFIGURATION_DATE_TIME, @sizeOf(@TypeOf(simple_link_date)), @ptrCast(&simple_link_date))) {
//
}
} else {
// return
}
}

pub fn mcuReset() noreturn {
c.BOARD_MCU_Reset();
unreachable;
Expand Down
17 changes: 6 additions & 11 deletions src/connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,16 @@ 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) };
}

pub fn connect(self: *@This(), uri: std.Uri, local_port: ?u16, proto: ?protocol, mode: ?security_mode) !void {
_ = self;
_ = uri;
_ = local_port;
_ = proto;
_ = mode;
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) };
}

/// Create a connection to a host
///
pub fn create(self: *@This(), host: []const u8, port: u16, local_port: ?u16, proto: protocol, mode: ?security_mode) !void {
self.proto = proto;
pub fn create(self: *@This(), uri: std.Uri, local_port: ?u16, mode: ?security_mode) !void {
self.proto = schemes.match(uri.scheme).?.getProtocol();

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

if (self.proto.isSecure()) {
_ = self.ssl.init(self, self.proto, mode.?) catch {
Expand Down
4 changes: 2 additions & 2 deletions src/http.zig
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub fn filedownload(self: *@This(), url: []const u8, file_name: [*:0]const u8, c
// Change this to support TLS on HTTP
const proto = connection.schemes.match(uri.scheme).?.getProtocol();

try self.connection.create(uri.host.?, uri.port.?, null, proto, if (proto.isSecure()) .psk else null);
try self.connection.create(uri, null, if (proto.isSecure()) .psk else null);
defer {
self.connection.close() catch {};
}
Expand Down Expand Up @@ -388,7 +388,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.host.?, uri.port.?, null, proto, if (proto.isSecure()) .psk else null);
try self.connection.create(uri, null, if (proto.isSecure()) .psk else null);
} else {
// Keep Alive
}
Expand Down
22 changes: 15 additions & 7 deletions src/mbedtls.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const mbedtls_nok: i32 = -1;

pub const credential_callback_fn = *const fn (*@This(), connection.security_mode) auth_error!void;
pub const custom_init_callback_fn = *const fn (*@This(), connection.protocol, connection.security_mode) init_error!void;
pub const custom_cleanup_callback_fn = *const fn (*@This()) void;

/// Security mode
mode: connection.security_mode,
Expand All @@ -76,6 +77,9 @@ auth_callback: ?credential_callback_fn = defaultAuth,
/// Custom init callback
custom_init_callback: ?custom_init_callback_fn = null,

/// Custom cleanup callback
custom_cleanup_callback: ?custom_cleanup_callback_fn = null,

const tls_read_timeout: u32 = 5000;

pub fn deinit(self: *@This()) i32 {
Expand All @@ -94,19 +98,23 @@ fn defaultAuth(self: *@This(), security_mode: connection.security_mode) auth_err
}

pub fn cleanup(self: *@This()) void {
c.miso_mbedtls_deinit_timer(&self.timer);
c.mbedtls_ctr_drbg_free(&self.drbg);
c.mbedtls_entropy_free(&self.entropy);
c.mbedtls_ssl_free(&self.context);
c.mbedtls_ssl_config_free(&self.config);
if (self.custom_cleanup_callback) |custom| {
custom(self);
} else {
c.miso_mbedtls_deinit_timer(&self.timer);
c.mbedtls_ctr_drbg_free(&self.drbg);
c.mbedtls_entropy_free(&self.entropy);
c.mbedtls_ssl_free(&self.context);
c.mbedtls_ssl_config_free(&self.config);
}
}

fn get_ctx(self: *@This()) *c.mbedtls_ssl_context {
return &self.context;
}

pub fn create(auth_callback: ?credential_callback_fn, custom_init: ?custom_init_callback_fn) @This() {
return @This(){ .auth_callback = auth_callback orelse defaultAuth, .custom_init_callback = custom_init, .context = undefined, .timer = undefined, .config = undefined, .drbg = undefined, .entropy = undefined, .mode = connection.security_mode.no_sec, .entropy_seed = 0x55555555 };
pub fn create(auth_callback: ?credential_callback_fn, custom_init: ?custom_init_callback_fn, custom_cleanup: ?custom_cleanup_callback_fn) @This() {
return @This(){ .auth_callback = auth_callback orelse defaultAuth, .custom_init_callback = custom_init, .custom_cleanup_callback = custom_cleanup, .context = undefined, .timer = undefined, .config = undefined, .drbg = undefined, .entropy = undefined, .mode = connection.security_mode.no_sec, .entropy_seed = 0x55555555 };
}

/// Helper function to get the credential callback function in custom init
Expand Down
2 changes: 1 addition & 1 deletion src/mqtt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ pub fn connect(self: *@This(), uri: std.Uri) !void {
self.connection.close() catch {};
}

try self.connection.create(uri.host.?, uri.port.?, null, connection.schemes.match(uri.scheme).?.getProtocol(), .psk);
try self.connection.create(uri, null, .psk);

_ = try self.packet.prepareConnectPacket(self.device_id[0..c.strlen(self.device_id)], null, null);

Expand Down
Loading

0 comments on commit de611b9

Please sign in to comment.