mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-04 15:14:22 +07:00
127 lines
4.3 KiB
C
127 lines
4.3 KiB
C
// Copyright (c) 2020 Cesanta Software Limited
|
|
// All rights reserved
|
|
|
|
#include "certs.h"
|
|
#include "mongoose.h"
|
|
#include <zephyr/net/net_mgmt.h>
|
|
|
|
static time_t s_boot_timestamp = 0;
|
|
static struct mg_connection *s_sntp_conn = NULL;
|
|
static const char *s_url = "https://example.org/";
|
|
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) {
|
|
if (ev == MG_EV_OPEN) {
|
|
// Connection created. Store connect expiration time in c->data
|
|
*(int64_t *) c->data = mg_millis() + s_timeout_ms;
|
|
} else if (ev == MG_EV_POLL) {
|
|
if (mg_millis() > *(int64_t *) c->data &&
|
|
(c->is_connecting || c->is_resolving)) {
|
|
mg_error(c, "Connect timeout");
|
|
}
|
|
} else if (ev == MG_EV_CONNECT) {
|
|
// Connected to server. Extract host name from URL
|
|
struct mg_str host = mg_url_host(s_url);
|
|
|
|
// If s_url is https://, init TLS client connection
|
|
if (c->is_tls) {
|
|
struct mg_tls_opts opts = {.ca = s_ca, .name = host};
|
|
mg_tls_init(c, &opts);
|
|
}
|
|
|
|
// Send request
|
|
int content_length = s_post_data ? strlen(s_post_data) : 0;
|
|
mg_printf(c,
|
|
"%s %s HTTP/1.0\r\n"
|
|
"Host: %.*s\r\n"
|
|
"Content-Type: octet-stream\r\n"
|
|
"Content-Length: %d\r\n"
|
|
"\r\n",
|
|
s_post_data ? "POST" : "GET", mg_url_uri(s_url), (int) host.len,
|
|
host.buf, content_length);
|
|
mg_send(c, s_post_data, content_length);
|
|
} else if (ev == MG_EV_HTTP_MSG) {
|
|
// Response is received. Print it
|
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
|
MG_INFO(("%.*s", (int) hm->message.len, hm->message.buf));
|
|
c->is_draining = 1; // Tell mongoose to close this connection
|
|
*(bool *) c->fn_data = true; // Tell event loop to stop
|
|
} else if (ev == MG_EV_ERROR) {
|
|
*(bool *) c->fn_data = true; // Error, tell event loop to stop
|
|
}
|
|
}
|
|
|
|
// 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 ourtime() correct
|
|
static void sfn(struct mg_connection *c, int ev, void *ev_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);
|
|
|
|
if (!s_connected) {
|
|
MG_INFO(("Connecting to : [%s]", s_url));
|
|
mg_http_connect(&s_mgr, s_url, fn, &done); // Create client connection
|
|
s_connected = 1;
|
|
}
|
|
} else if (ev == MG_EV_CLOSE) {
|
|
s_sntp_conn = NULL;
|
|
}
|
|
}
|
|
|
|
// Periodic timer syncs time via SNTP
|
|
static void timer_fn(void *arg) {
|
|
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
|
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
|
|
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,
|
|
// https://docs.zephyrproject.org/latest/releases/migration-guide-4.2.html#networking
|
|
#if ZEPHYR_VERSION_CODE < 0x40200
|
|
uint32_t mgmt_event,
|
|
#else
|
|
uint64_t mgmt_event,
|
|
#endif
|
|
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 event loop, exit when client is done
|
|
MG_INFO(("Mongoose version : v%s", MG_VERSION));
|
|
|
|
while (!done) mg_mgr_poll(&s_mgr, 1000);
|
|
mg_mgr_free(&s_mgr);
|
|
|
|
return 0;
|
|
}
|