update examples

This commit is contained in:
Sergio R. Caprile committed 2025-06-24 16:26:31 -03:00
1 parent 074ef5197b
commit f5e2c68c8b
27 files changed
+166 -47

No files matched your search

+1 -1
View File
@@ -33,7 +33,7 @@ static void *thread_function(void *param) {
struct thread_data *p = (struct thread_data *) param;
sleep(2); // Simulate long execution
mg_wakeup(p->mgr, p->conn_id, "hi!", 3); // Respond to parent
free((void *) p->message.buf); // Free all resources that were
mg_free((void *) p->message.buf); // Free all resources that were
free(p); // passed to us
return NULL;
}
+2 -2
View File
@@ -156,10 +156,10 @@ static void handle_settings_set(struct mg_connection *c, struct mg_str body) {
settings.log_level = (int) mg_json_get_long(body, "$.log_level", 0);
settings.brightness = mg_json_get_long(body, "$.brightness", 0);
if (s && strlen(s) < MAX_DEVICE_NAME) {
free(settings.device_name);
mg_free(settings.device_name);
settings.device_name = s;
} else {
free(s);
mg_free(s);
}
s_settings = settings; // Save to the device flash
mg_http_reply(c, 200, s_json_header,
+3 -3
View File
@@ -26,9 +26,9 @@ void app_main(void) {
char *ssid = mg_json_get_str(json, "$.ssid");
char *pass = mg_json_get_str(json, "$.pass");
while (!wifi_init(ssid, pass)) (void) 0;
free(ssid);
free(pass);
free((void *) json.buf);
mg_free(ssid);
mg_free(pass);
mg_free((void *) json.buf);
} else {
// If WiFi is not configured, run CLI until configured
MG_INFO(("WiFi is not configured, running CLI. Press enter"));
+2 -2
View File
@@ -157,7 +157,7 @@ static void timer_fn(void *param) {
static void update_string(struct mg_str json, const char *path, char **value) {
char *jval;
if ((jval = mg_json_get_str(json, path)) != NULL) {
free(*value);
mg_free(*value);
*value = strdup(jval);
}
}
@@ -189,7 +189,7 @@ void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN && c->is_listening) {
struct mg_str config = mg_file_read(&mg_fs_posix, "config.json");
if (config.buf != NULL) config_apply(config);
free(config.buf);
mg_free(config.buf);
s_state.tcp.url = strdup(DEFAULT_TCP);
s_state.websocket.url = strdup(DEFAULT_WEBSOCKET);
s_state.mqtt.url = strdup(DEFAULT_MQTT);
+1 -1
View File
@@ -44,7 +44,7 @@ static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
mg_send(c, data.buf, data.len);
mg_send(c, "\r\n", 2);
}
free((void *) data.buf);
mg_free((void *) data.buf);
}
static void timer_callback(void *arg) {
+3 -3
View File
@@ -43,7 +43,7 @@ static void set_device_id(void) {
struct mg_str id = mg_file_read(&mg_fs_posix, "/etc/machine-id");
if (id.buf != NULL) {
mg_snprintf(buf, sizeof(buf), "%s", id.buf);
free((void *) id.buf);
mg_free((void *) id.buf);
}
#endif
@@ -118,7 +118,7 @@ static void subscribe(struct mg_connection *c) {
sub_opts.qos = s_qos;
mg_mqtt_sub(c, &sub_opts);
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, (int) subt.len, subt.buf));
free(rx_topic);
mg_free(rx_topic);
}
static void rpc_config_set(struct mg_rpc_req *r) {
@@ -169,7 +169,7 @@ static void rpc_ota_upload(struct mg_rpc_req *r) {
} else {
mg_rpc_ok(r, "%m", MG_ESC("ok"));
}
free(buf);
mg_free(buf);
}
}
+3 -1
View File
@@ -78,7 +78,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
struct mg_str topic;
int num_topics = 0;
while ((pos = mg_mqtt_next_sub(mm, &topic, &qos, pos)) > 0) {
struct sub *sub = calloc(1, sizeof(*sub));
struct sub *sub = (struct sub *)calloc(1, sizeof(*sub));
sub->c = c;
sub->topic = mg_strdup(topic);
sub->qos = qos;
@@ -128,7 +128,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
next = sub->next;
if (c != sub->c) continue;
MG_INFO(("UNSUB %p [%.*s]", c->fd, (int) sub->topic.len, sub->topic.buf));
mg_free(sub->topic.buf);
LIST_DELETE(struct sub, &s_subs, sub);
free(sub);
}
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ bool web_load_settings(void *buf, size_t len) {
} else {
memcpy(buf, data.buf, len);
}
free((void *) data.buf);
mg_free((void *) data.buf);
return ok;
}
+2 -2
View File
@@ -41,7 +41,7 @@ static void setfromjson(struct mg_str json, const char *jsonpath, char *buf,
size_t len) {
char *val = mg_json_get_str(json, jsonpath);
if (val != NULL) mg_snprintf(buf, len, "%s", val);
free(val);
mg_free(val);
}
static void handle_settings_set(struct mg_connection *c, struct mg_str body) {
@@ -159,7 +159,7 @@ static struct mg_connection *start_modbus_request(struct mg_mgr *mgr,
cd->id = cid; // Store parent connection ID
cd->expiration_time = mg_millis() + timeout;
}
free(url);
mg_free(url);
return c;
}
+1 -1
View File
@@ -104,7 +104,7 @@ static void fn2(struct mg_connection *c, int ev, void *ev_data) {
} else if (ev == MG_EV_CONNECT) {
mg_printf(c, "GET %s HTTP/1.1\r\n\r\n", mg_url_uri((char *) c->fn_data));
} else if (ev == MG_EV_CLOSE) {
free(c->fn_data);
mg_free(c->fn_data);
}
}
+4 -4
View File
@@ -48,12 +48,12 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
if (ev == MG_TCPIP_EV_ST_CHG) {
MG_INFO(("State change: %u", *(uint8_t *) ev_data));
} else if (ev == MG_TCPIP_EV_DHCP_DNS) {
free(s_dns);
mg_free(s_dns);
s_dns = mg_mprintf("udp://%M:53", mg_print_ip4, (uint32_t *) ev_data);
ifp->mgr->dns4.url = s_dns;
MG_INFO(("Set DNS to %s", ifp->mgr->dns4.url));
} else if (ev == MG_TCPIP_EV_DHCP_SNTP) {
free(s_sntp);
mg_free(s_sntp);
s_sntp = mg_mprintf("udp://%M:123", mg_print_ip4, (uint32_t *) ev_data);
MG_INFO(("Set SNTP to %s", s_sntp));
}
@@ -128,8 +128,8 @@ int main(int argc, char *argv[]) {
web_init(&mgr);
while (s_signo == 0) mg_mgr_poll(&mgr, 100); // Infinite event loop
free(s_dns);
free(s_sntp);
mg_free(s_dns);
mg_free(s_sntp);
mg_mgr_free(&mgr);
close(fd);
printf("Exiting on signal %d\n", s_signo);
+7 -2
View File
@@ -9,14 +9,15 @@
static const char *s_url = "ws://localhost:8000/websocket";
static const char *s_ca_path = "ca.pem";
static struct mg_str s_ca;
// Print websocket response and signal that we're done
static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
c->is_hexdumping = 1;
} else if (c->is_tls && ev == MG_EV_CONNECT) {
struct mg_str ca = mg_file_read(&mg_fs_posix, s_ca_path);
struct mg_tls_opts opts = {.ca = ca, .name = mg_url_host(s_url)};
s_ca = mg_file_read(&mg_fs_posix, s_ca_path);
struct mg_tls_opts opts = {.ca = s_ca, .name = mg_url_host(s_url)};
mg_tls_init(c, &opts);
} else if (ev == MG_EV_ERROR) {
// On error, log error message
@@ -32,6 +33,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE || ev == MG_EV_WS_MSG) {
*(bool *) c->fn_data = true; // Signal that we're done
if (c->is_tls) {
mg_free(s_ca.buf);
s_ca.buf = NULL;
}
}
}
+10 -5
View File
@@ -10,6 +10,7 @@ static const char *s_web_root = ".";
static const char *s_ca_path = "ca.pem";
static const char *s_cert_path = "cert.pem";
static const char *s_key_path = "key.pem";
struct mg_str s_ca, s_cert, s_key;
// This RESTful server implements the following endpoints:
// /websocket - upgrade to Websocket, and implement websocket echo server
@@ -19,15 +20,15 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_OPEN) {
// c->is_hexdumping = 1;
} else if(c->is_tls && ev == MG_EV_ACCEPT) {
struct mg_str ca = mg_file_read(&mg_fs_posix, s_ca_path);
struct mg_str cert = mg_file_read(&mg_fs_posix, s_cert_path);
struct mg_str key = mg_file_read(&mg_fs_posix, s_key_path);
struct mg_tls_opts opts = {.ca = ca, .cert = cert, .key = key};
s_ca = mg_file_read(&mg_fs_posix, s_ca_path);
s_cert = mg_file_read(&mg_fs_posix, s_cert_path);
s_key = mg_file_read(&mg_fs_posix, s_key_path);
struct mg_tls_opts opts = {.ca = s_ca, .cert = s_cert, .key = s_key};
mg_tls_init(c, &opts);
} else if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_match(hm->uri, mg_str("/websocket"), NULL)) {
// Upgrade to websocket. From now on, a connection is a full-duplex
// Upgrade to websocket. From now on, connection is full-duplex
// Websocket connection, which will receive MG_EV_WS_MSG events.
mg_ws_upgrade(c, hm, NULL);
} else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
@@ -42,6 +43,10 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_TEXT);
} else if (ev == MG_EV_CLOSE && c->is_tls) {
mg_free(s_ca.buf);
mg_free(s_cert.buf);
mg_free(s_key.buf);
}
}
+5 -2
View File
@@ -24,8 +24,8 @@ static struct config {
static void update_config(struct mg_str json, const char *path, char **value) {
char *jval;
if ((jval = mg_json_get_str(json, path)) != NULL) {
free(*value);
*value = strdup(jval);
mg_free(*value);
*value = jval;
}
}
@@ -61,5 +61,8 @@ int main(void) {
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
mg_mgr_free(&mgr);
mg_free(s_config.url);
mg_free(s_config.pub);
mg_free(s_config.sub);
return 0;
}