Skip to content

Commit 47dd345

Browse files
committed
Add OTA over mqtt example
1 parent 46ad050 commit 47dd345

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
PROG ?= example # Program we are building
2+
DELETE = rm -rf # Command to remove files
3+
OUT ?= -o $(PROG) # Compiler argument for output file
4+
SOURCES = main.c mongoose.c # Source code files, packed_fs.c contains ca.pem, which contains CA certs for TLS
5+
CFLAGS = -W -Wall -Wextra -g -I. # Build options
6+
7+
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
8+
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1
9+
CFLAGS_MONGOOSE += -DMG_TLS=MG_TLS_BUILTIN
10+
#CFLAGS_MONGOOSE += -DMG_TLS=MG_TLS_OPENSSL -L/opt/homebrew/opt/openssl/lib -I/opt/homebrew/opt/openssl/include -lssl -lcrypto
11+
#CFLAGS_MONGOOSE += -DMG_TLS=MG_TLS_MBED -L/opt/homebrew/opt/mbedtls/lib -I/opt/homebrew/opt/mbedtls/include -lmbedtls -lmbedcrypto -lmbedx509
12+
13+
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
14+
PROG ?= example.exe # Use .exe suffix for the binary
15+
CC = gcc # Use MinGW gcc compiler
16+
CFLAGS += -lws2_32 # Link against Winsock library
17+
DELETE = cmd /C del /Q /F /S # Command prompt command to delete files
18+
OUT ?= -o $(PROG) # Build output
19+
endif
20+
21+
all: $(PROG) # Default target. Build and run program
22+
$(RUN) ./$(PROG) $(ARGS)
23+
24+
$(PROG): $(SOURCES) # Build program from sources
25+
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT)
26+
27+
clean: # Cleanup. Delete built program and all build artifacts
28+
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright (c) 2013-2026 Cesanta Software Limited
2+
// All rights reserved
3+
//
4+
// This example implements a JSON-RPC server that connects to the MQTT server
5+
// and expects requests on MQTT_RX_TOPIC, and sends responses to the
6+
// MQTT_TX_TOPIC. Only a single RPC method is implemented, "ota.update",
7+
// which expects parameters {"total": XX, "offset": XX, "chunk": "..."}
8+
// The rpc_ota_update() function handles those requests and calls Mongoose
9+
// ota_begin(), ota_write(), ota_end() respectively.
10+
//
11+
// Visit https://mongoose.ws/mqtt/ web page to push the firmware binary.
12+
13+
#include "mongoose.h"
14+
15+
#define MQTT_SERVER_URL "mqtt://broker.hivemq.com:1883"
16+
#define MQTT_RX_TOPIC "mg/123/rx"
17+
#define MQTT_TX_TOPIC "mg/123/tx"
18+
19+
static uint8_t s_qos = 1; // MQTT QoS
20+
static struct mg_connection *s_conn; // MQTT Client connection
21+
static struct mg_rpc *s_rpc = NULL; // List of registered RPC methods
22+
23+
static void rpc_ota_update(struct mg_rpc_req *r) {
24+
long ofs = mg_json_get_long(r->frame, "$.params.offset", -1);
25+
long tot = mg_json_get_long(r->frame, "$.params.total", -1);
26+
int len = 0;
27+
char *buf = mg_json_get_b64(r->frame, "$.params.chunk", &len);
28+
if (buf == NULL) {
29+
mg_rpc_err(r, 1, "%m", MG_ESC("Chunk decoding error"));
30+
} else if (ofs < 0 || tot < 0) {
31+
mg_rpc_err(r, 1, "%m", MG_ESC("offset and total not set"));
32+
} else if (ofs == 0 && mg_ota_begin((size_t) tot) == false) {
33+
mg_rpc_err(r, 1, "\"mg_ota_begin(%ld) failed\"", tot);
34+
} else if (len > 0 && mg_ota_write(buf, len) == false) {
35+
mg_rpc_err(r, 1, "\"mg_ota_write(%lu) @%ld failed\"", len, ofs);
36+
mg_ota_end();
37+
} else if (len == 0 && mg_ota_end() == false) {
38+
mg_rpc_err(r, 1, "\"mg_ota_end() failed\"", tot);
39+
} else {
40+
mg_rpc_ok(r, "%m", MG_ESC("ok"));
41+
}
42+
mg_free(buf);
43+
}
44+
45+
static void subscribe(struct mg_connection *c, struct mg_str topic) {
46+
struct mg_mqtt_opts opts = {};
47+
memset(&opts, 0, sizeof(opts));
48+
opts.topic = topic;
49+
opts.qos = s_qos;
50+
mg_mqtt_sub(c, &opts);
51+
MG_INFO(("%lu SUBSCRIBED to %.*s", c->id, topic.len, topic.buf));
52+
}
53+
54+
static void publish(struct mg_connection *c, struct mg_str topic,
55+
struct mg_str message) {
56+
struct mg_mqtt_opts opts = {};
57+
memset(&opts, 0, sizeof(opts));
58+
opts.topic = topic;
59+
opts.message = message;
60+
opts.qos = s_qos;
61+
mg_mqtt_pub(c, &opts);
62+
MG_INFO(("%lu PUBLISHED %.*s -> %.*s", c->id, topic.len, topic.buf,
63+
message.len, message.buf));
64+
}
65+
66+
static void ev_handler(struct mg_connection *c, int ev, void *ev_data) {
67+
if (ev == MG_EV_OPEN) {
68+
MG_INFO(("%lu CREATED", c->id));
69+
// c->is_hexdumping = 1;
70+
} else if (ev == MG_EV_CONNECT) {
71+
MG_INFO(("Connected"));
72+
} else if (ev == MG_EV_ERROR) {
73+
// On error, log error message
74+
MG_ERROR(("%lu ERROR %s", c->id, (char *) ev_data));
75+
} else if (ev == MG_EV_MQTT_OPEN) {
76+
// MQTT connect is successful
77+
MG_INFO(("%lu CONNECTED", c->id));
78+
subscribe(c, mg_str(MQTT_RX_TOPIC));
79+
} else if (ev == MG_EV_MQTT_MSG) {
80+
// When we get echo response, print it
81+
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
82+
struct mg_iobuf io = {0, 0, 0, 512};
83+
struct mg_rpc_req r = {&s_rpc, NULL, mg_pfn_iobuf,
84+
&io, NULL, {mm->data.buf, mm->data.len}};
85+
size_t clipped_len = mm->data.len > 512 ? 512 : mm->data.len;
86+
MG_INFO(("%lu RECEIVED %.*s <- %.*s", c->id, clipped_len, mm->data.buf,
87+
mm->topic.len, mm->topic.buf));
88+
mg_rpc_process(&r);
89+
if (io.buf != NULL && io.len > 0) {
90+
publish(c, mg_str(MQTT_TX_TOPIC), mg_str_n((char *) io.buf, io.len));
91+
}
92+
mg_iobuf_free(&io);
93+
} else if (ev == MG_EV_CLOSE) {
94+
MG_INFO(("%lu CLOSED", c->id));
95+
s_conn = NULL; // Mark that we're closed
96+
}
97+
}
98+
99+
static void timer_sntp(void *param) { // SNTP timer function. Sync up time
100+
static uint64_t hourly_timer = 0;
101+
uint64_t t1 = mg_now(), t2 = mg_millis();
102+
if (t1 < t2 + 3600 || mg_timer_expired(&hourly_timer, 3600000, t2)) {
103+
mg_sntp_connect(param, "udp://time.google.com:123", NULL, NULL);
104+
}
105+
}
106+
107+
static void timer_fn(void *arg) {
108+
if (s_conn == NULL) {
109+
struct mg_mgr *mgr = (struct mg_mgr *) arg;
110+
struct mg_mqtt_opts opts = {.clean = true,
111+
.qos = s_qos,
112+
.topic = mg_str(MQTT_TX_TOPIC),
113+
.keepalive = 5,
114+
.version = 4,
115+
.message = mg_str("{\"method\":\"bye\"}")};
116+
s_conn = mg_mqtt_connect(mgr, MQTT_SERVER_URL, &opts, ev_handler, NULL);
117+
} else {
118+
mg_mqtt_ping(s_conn);
119+
}
120+
}
121+
122+
int main(void) {
123+
struct mg_mgr mgr;
124+
mg_log_set(MG_LL_INFO);
125+
mg_mgr_init(&mgr);
126+
127+
mg_timer_add(&mgr, 3000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
128+
mg_timer_add(&mgr, 100, MG_TIMER_REPEAT, timer_sntp, &mgr);
129+
130+
mg_rpc_add(&s_rpc, mg_str("ota.update"), rpc_ota_update, NULL);
131+
132+
for (;;) mg_mgr_poll(&mgr, 100);
133+
mg_mgr_free(&mgr);
134+
135+
return 0;
136+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../mongoose.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../mongoose.h

0 commit comments

Comments
 (0)