Update Zephyr examples

This commit is contained in:
Sergio R. Caprile committed 2022-10-06 17:30:27 -03:00
1 parent 9c60a1bc10
commit d846e67ec1
28 files changed
+26641 -73

No files matched your search

+24 -9
View File
@@ -11,6 +11,7 @@ static const char *s_post_data = NULL; // POST data
static const int64_t s_timeout_ms = 1500; // Connect timeout in milliseconds
struct mg_mgr s_mgr;
static int s_connected = 0;
static bool done = false;
// Print HTTP response and signal that we're done
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
@@ -54,25 +55,23 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
}
}
// We have no valid system time(), and we need it for TLS. Implement it
time_t time(time_t *tp) {
time_t t = s_boot_timestamp + k_uptime_get() / 1000;
// example system time()-like function
time_t ourtime(time_t *tp) {
time_t t = s_boot_timestamp + mg_millis() / 1000;
if (tp != NULL) *tp = t;
return t;
}
// SNTP callback. Modifies s_boot_timestamp, to make time() correct
// SNTP callback. Modifies s_boot_timestamp, to make ourtime() correct
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_SNTP_TIME) {
int64_t t = *(int64_t *) ev_data;
MG_INFO(("Got SNTP time: %lld ms from epoch", t));
s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000);
// We need correct time in order to get HTTPs working, therefore,
// making https request from SMTP callback
if (!s_connected) {
MG_INFO(("Connecting to : [%s]", s_url));
mg_http_connect(&s_mgr, s_url, fn, NULL); // Create client connection
mg_http_connect(&s_mgr, s_url, fn, &done); // Create client connection
s_connected = 1;
}
} else if (ev == MG_EV_CLOSE) {
@@ -87,17 +86,33 @@ static void timer_fn(void *arg) {
if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn);
}
// Zephyr: Define a semaphore and network management callback to be able to wait
// until our IP address is ready. The main function will start and block on this
// semaphore until this event handler releases it when the network is ready
K_SEM_DEFINE(run, 0, 1);
static void zeh(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
struct net_if *iface) {
if (mgmt_event == NET_EVENT_L4_CONNECTED) k_sem_give(&run);
}
int main(int argc, char *argv[]) {
// Zephyr: Register the network management callback and block on the semaphore
struct net_mgmt_event_callback ncb;
net_mgmt_init_event_callback(&ncb, zeh, NET_EVENT_L4_CONNECTED);
net_mgmt_add_event_callback(&ncb);
k_sem_take(&run, K_FOREVER);
mg_log_set(MG_LL_DEBUG);
mg_mgr_init(&s_mgr);
mg_timer_add(&s_mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn,
&s_mgr);
// Start infinite event loop
// Start event loop, exit when client is done
MG_INFO(("Mongoose version : v%s", MG_VERSION));
for (;;) mg_mgr_poll(&s_mgr, 1000);
while (!done) mg_mgr_poll(&s_mgr, 1000);
mg_mgr_free(&s_mgr);
return 0;
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff