mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-20 19:41:58 +00:00
Compare commits
46
Commits
d47cfa183d
...
802
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5d0abc50e0 | ||
|
|
9d0b015217 | ||
|
|
1620b2ae03 | ||
|
|
0914048424 | ||
|
|
0f319e3060 | ||
|
|
d612b7f1c4 | ||
|
|
6d5f4a5a20 | ||
|
|
cd10b35de4 | ||
|
|
f04cb5333e | ||
|
|
28e3b347d4 | ||
|
|
069ab78c02 | ||
|
|
91b87a6bd8 | ||
|
|
1ce7e416b0 | ||
|
|
63aaad829a | ||
|
|
9322227886 | ||
|
|
e3a78d513e | ||
|
|
ee338b7bba | ||
|
|
0f157bd23b | ||
|
|
49e71ba666 | ||
|
|
166c1a7708 | ||
|
|
2ef7f7a44a | ||
|
|
e7335e063b | ||
|
|
88096d3f4d | ||
|
|
1b9177ae64 | ||
|
|
053b8ed824 | ||
|
|
2ea1cf6817 | ||
|
|
323326ce8b | ||
|
|
c5344af9fc | ||
|
|
d73b009bbc | ||
|
|
dba6479b23 | ||
|
|
28ca01eb2b | ||
|
|
e999239ca7 | ||
|
|
d545526ba2 | ||
|
|
0b888e8f3f | ||
|
|
27a87dee8b | ||
|
|
94ccc4a130 | ||
|
|
454bf4036f | ||
|
|
c835c72202 | ||
|
|
52ebac833f | ||
|
|
5f1aaa21eb | ||
|
|
1c3a7a89dd | ||
|
|
d7a4130905 | ||
|
|
5b0675584c | ||
|
|
21b998c882 | ||
|
|
2cab36e882 | ||
|
|
3a4f044e41 |
No files matched your search
@@ -211,7 +211,7 @@ jobs:
|
|||||||
|
|
||||||
claude_args: |
|
claude_args: |
|
||||||
--allowedTools "Read,Glob,Grep,Bash(git:*),Bash(find:*),Bash(grep:*),Bash(sed:*),Bash(cat:*)"
|
--allowedTools "Read,Glob,Grep,Bash(git:*),Bash(find:*),Bash(grep:*),Bash(sed:*),Bash(cat:*)"
|
||||||
--model claude-opus-5
|
--model claude-opus-5-5
|
||||||
|
|
||||||
- name: Prepare private security scan outputs
|
- name: Prepare private security scan outputs
|
||||||
if: always() && steps.claude_scan.outputs.execution_file != ''
|
if: always() && steps.claude_scan.outputs.execution_file != ''
|
||||||
|
|||||||
@@ -108,4 +108,4 @@ jobs:
|
|||||||
|
|
||||||
claude_args: |
|
claude_args: |
|
||||||
--allowedTools "Read,Bash(cat:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr comment:*)"
|
--allowedTools "Read,Bash(cat:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr comment:*)"
|
||||||
--model claude-opus-5
|
--model claude-opus-5-5
|
||||||
@@ -560,8 +560,7 @@ jobs:
|
|||||||
# - run: make -C tutorials/${{ matrix.example.path }} build
|
# - run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
wizard_examples_arm:
|
wizard_examples_arm:
|
||||||
# Keep rp-ti on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
runs-on: ubuntu-latest
|
||||||
runs-on: ${{ matrix.group == 'rp-ti' && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
|
|||||||
@@ -201,8 +201,7 @@ jobs:
|
|||||||
run: make -C tutorials/${{ matrix.example.path }} build
|
run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
generic_examples_arm:
|
generic_examples_arm:
|
||||||
# Keep RP on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
runs-on: ubuntu-latest
|
||||||
runs-on: ${{ startsWith(matrix.example.path, 'rp/') && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -245,8 +244,7 @@ jobs:
|
|||||||
run: make -C tutorials/${{ matrix.example.path }} build
|
run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
device_dashboard_examples_arm:
|
device_dashboard_examples_arm:
|
||||||
# Keep RP on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
runs-on: ubuntu-latest
|
||||||
runs-on: ${{ startsWith(matrix.example.path, 'rp/') && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -272,8 +270,7 @@ jobs:
|
|||||||
run: make -C tutorials/${{ matrix.example.path }} build
|
run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
mqtt_dashboard_examples_arm:
|
mqtt_dashboard_examples_arm:
|
||||||
# Pico SDK 2.1.0 pioasm fails with GCC 15; update SDK to unpin
|
runs-on: ubuntu-latest
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
# Mongoose: a full-stack embedded networking library
|
# Mongoose: a full-stack embedded networking library
|
||||||
|
|
||||||
Mongoose is a full-stack embedded networking library for C/C++: a TCP/IP stack
|
Mongoose is a full-stack embedded networking library for C and C++. It includes
|
||||||
with drivers, TLS 1.3, an embedded web server (HTTP, WebSocket), MQTT and
|
a TCP/IP stack with drivers, TLS 1.3, an embedded web server with HTTP and
|
||||||
firmware OTA updates in two files, `mongoose.c` and `mongoose.h`. Mongoose runs on
|
WebSocket support, MQTT, and firmware OTA updates. All in just two files:
|
||||||
STM32, NXP and other microcontrollers, on bare metal or with an RTOS, with no
|
`mongoose.c` and `mongoose.h`. Mongoose runs on STM32, NXP and other
|
||||||
external dependencies. Use it for cloud connectivity, device web dashboards,
|
microcontrollers, on bare metal or with an RTOS, with no external dependencies.
|
||||||
secure firmware updates and remote control.
|
Ideal for cloud connectivity, device web dashboards, secure firmware updates
|
||||||
|
and remote control.
|
||||||
|
|
||||||
License: GPLv2 or commercial.
|
License: GPLv2 or commercial.
|
||||||
Website: https://mongoose.ws/
|
Website: https://mongoose.ws/
|
||||||
@@ -13,6 +14,7 @@ GitHub repo: https://github.com/cesanta/mongoose
|
|||||||
|
|
||||||
## General rules
|
## General rules
|
||||||
|
|
||||||
|
- All relative file names mentioned in this file are related to the repo root.
|
||||||
- Always re-read files before editing, so you do not overwrite existing changes.
|
- Always re-read files before editing, so you do not overwrite existing changes.
|
||||||
- Never guess. If you don't know, say you don't know and stop.
|
- Never guess. If you don't know, say you don't know and stop.
|
||||||
- Resolve relative paths from the repo root: https://github.com/cesanta/mongoose
|
- Resolve relative paths from the repo root: https://github.com/cesanta/mongoose
|
||||||
@@ -39,6 +41,7 @@ This is a navigation aid, not an API reference. Read `mongoose.h` for exact
|
|||||||
function signatures, structs, options, examples, and constraints.
|
function signatures, structs, options, examples, and constraints.
|
||||||
|
|
||||||
- `mg_mgr_*` - event manager and polling loop
|
- `mg_mgr_*` - event manager and polling loop
|
||||||
|
- `mg_dash_*` - dashboard API for device web UIs, fieldsets, and live updates
|
||||||
- `mg_http_*` - HTTP server, HTTP client, uploads, serving files
|
- `mg_http_*` - HTTP server, HTTP client, uploads, serving files
|
||||||
- `mg_ws_*` - WebSocket server and client
|
- `mg_ws_*` - WebSocket server and client
|
||||||
- `mg_mqtt_*` - MQTT client
|
- `mg_mqtt_*` - MQTT client
|
||||||
@@ -229,12 +232,41 @@ xTaskCreate(net_task, "net", 8192, NULL, tskIDLE_PRIORITY + 1, NULL);
|
|||||||
|
|
||||||
## HTTP / Web Device Dashboard
|
## HTTP / Web Device Dashboard
|
||||||
|
|
||||||
For a web UI with real-time device state over WebSocket, use the Mongoose
|
When building a device web UI, control panel, monitoring page, or live
|
||||||
device dashboard.
|
display, you MUST use the `mg_dash_*` dashboard framework unless the user
|
||||||
|
explicitly requests another architecture. Do not add your own HTTP event handler.
|
||||||
|
|
||||||
|
Read the dashboard declarations in `mongoose.h` before implementing the UI.
|
||||||
|
Use only the dashboard files listed below. Download missing reference files;
|
||||||
|
preserve existing files. Integrate the dashboard into the existing application
|
||||||
|
and build files.
|
||||||
|
|
||||||
|
Start from the minimal reference implementation and adapt its fields and UI.
|
||||||
|
Keep dashboard fieldsets, callbacks, update timers, and small metric helpers
|
||||||
|
in `dashboard.c`. Do not create `dashboard.h`, or other dashboard
|
||||||
|
helper modules. `mongoose.h` already declares `mg_dash_init()` and
|
||||||
|
`mg_dash_poll()`. Reuse existing board/HAL helpers for hardware measurements;
|
||||||
|
if none exists, put a small private helper in `dashboard.c` or extend the
|
||||||
|
existing board/HAL implementation.
|
||||||
|
|
||||||
|
Expose device state through mg_field / mg_field_set.
|
||||||
|
Call mg_dash_init() once and mg_dash_poll() in the event loop.
|
||||||
|
For live updates, call mg_dash_send_change() from a backend timer.
|
||||||
|
Display values using dashboard.js and HTML data-bind attributes.
|
||||||
|
|
||||||
|
Example: free RAM updated every 50 ms means a read-only fieldset pushed
|
||||||
|
over the dashboard WebSocket every 50 ms. Do not implement browser polling,
|
||||||
|
fetch(), setInterval(), or a custom WebSocket client for dashboard state.
|
||||||
|
|
||||||
### Required files
|
### Required files
|
||||||
|
|
||||||
The following files must exist at these exact paths, regardless of the build environment:
|
Keep all the following Mongoose files together in one project directory.
|
||||||
|
The example uses `mongoose/`; you may choose another directory name.
|
||||||
|
If Mongoose is already installed, use the directory containing the actual
|
||||||
|
`mongoose.c` and `mongoose.h` files, including an existing CubeMX middleware
|
||||||
|
directory. Add the dashboard files there and update the build paths. Do not
|
||||||
|
create a second Mongoose directory or use symlinks, forwarding headers, or
|
||||||
|
duplicate library copies to make files appear colocated.
|
||||||
|
|
||||||
```
|
```
|
||||||
your_project/
|
your_project/
|
||||||
@@ -244,6 +276,7 @@ your_project/
|
|||||||
├── mongoose.c # single source file
|
├── mongoose.c # single source file
|
||||||
├── dashboard.c # C-side dashboard logic
|
├── dashboard.c # C-side dashboard logic
|
||||||
├── dashboard.html # HTML/JS UI (source)
|
├── dashboard.html # HTML/JS UI (source)
|
||||||
|
├── html2c.js # HTML-to-C generator
|
||||||
└── file_data.c # generated from dashboard.html (see below)
|
└── file_data.c # generated from dashboard.html (see below)
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -255,16 +288,24 @@ node html2c.js dashboard.html -o file_data.c
|
|||||||
|
|
||||||
`html2c.js` is at https://github.com/cesanta/mongoose/blob/master/resources/html2c.js
|
`html2c.js` is at https://github.com/cesanta/mongoose/blob/master/resources/html2c.js
|
||||||
|
|
||||||
If the project does not yet have `dashboard.c` and `dashboard.html`, fetch the
|
For each missing `dashboard.c`, `dashboard.html`, or `html2c.js`, download its
|
||||||
minimal reference versions:
|
reference version from the Mongoose repository and place it beside `mongoose.c`
|
||||||
|
and `mongoose.h`:
|
||||||
|
|
||||||
```
|
```
|
||||||
https://github.com/cesanta/mongoose/blob/master/tutorials/device-dashboard/minimal/dashboard.c
|
https://github.com/cesanta/mongoose/blob/master/tutorials/device-dashboard/minimal/dashboard.c
|
||||||
https://github.com/cesanta/mongoose/blob/master/tutorials/device-dashboard/minimal/dashboard.html
|
https://github.com/cesanta/mongoose/blob/master/tutorials/device-dashboard/minimal/dashboard.html
|
||||||
|
https://github.com/cesanta/mongoose/blob/master/resources/html2c.js
|
||||||
```
|
```
|
||||||
|
|
||||||
If the project already has `dashboard.c` and `dashboard.html`, do **not** fetch
|
Add a build rule that regenerates `file_data.c` whenever `dashboard.html` or
|
||||||
anything from the repository.
|
`html2c.js` changes. Keep the upstream generator unchanged; it already inlines
|
||||||
|
the script referenced by the HTML, so the served page does not need internet
|
||||||
|
access. Do not download a separate `dashboard.js` or add a local `--use`
|
||||||
|
override unless the user explicitly requests offline builds or local assets.
|
||||||
|
Compile both `dashboard.c` and `file_data.c` into the
|
||||||
|
application. Call `mg_dash_init()` once and `mg_dash_poll()` on every iteration
|
||||||
|
of the existing Mongoose event loop, from the same task or thread.
|
||||||
|
|
||||||
### C integration (bare metal or RTOS main loop)
|
### C integration (bare metal or RTOS main loop)
|
||||||
|
|
||||||
@@ -283,15 +324,17 @@ for (;;) {
|
|||||||
|
|
||||||
### dashboard.html rules
|
### dashboard.html rules
|
||||||
|
|
||||||
- Use `dashboard.js` from `https://mongoose.ws/resources/dashboard.js`
|
- Reference `<script src="https://mongoose.ws/resources/dashboard.js"></script>`
|
||||||
|
in the source HTML. Let `html2c.js` inline it into the generated asset.
|
||||||
- Call `Dashboard.init({ data: { ... } })` once - this is the only direct
|
- Call `Dashboard.init({ data: { ... } })` once - this is the only direct
|
||||||
Dashboard API call. Treat the rest of Dashboard as a black box.
|
Dashboard API call. Treat the rest of Dashboard as a black box.
|
||||||
- Do **not** add vanilla JS event listeners, `fetch()` calls, or custom
|
- Do **not** add vanilla JS event listeners, `fetch()` calls, or custom
|
||||||
reactive logic to `dashboard.html`.
|
reactive logic to `dashboard.html`.
|
||||||
- Do **not** modify `dashboard.html` unless the user explicitly asks.
|
- Modify `dashboard.html` only for requested UI work. A request to create or
|
||||||
|
modify the web UI authorizes the necessary `dashboard.html` edits.
|
||||||
- Bind controls to device state using `data-bind` attributes
|
- Bind controls to device state using `data-bind` attributes
|
||||||
- The `__status` object in evaluations is read-only, do not alter it
|
- The `__status` object in evaluations is read-only, do not alter it
|
||||||
- If you need to pass data between the UI and backend.c, add extra
|
- If you need to pass data between the UI and `dashboard.c`, add extra
|
||||||
fieldsets/fields - see next section about it
|
fieldsets/fields - see next section about it
|
||||||
- If you want to display device data in HTML, use `${}` evaluations
|
- If you want to display device data in HTML, use `${}` evaluations
|
||||||
|
|
||||||
@@ -315,17 +358,24 @@ for (;;) {
|
|||||||
|
|
||||||
### dashboard.c rules
|
### dashboard.c rules
|
||||||
|
|
||||||
Mongoose backend exports device data as a collection of "fieldsets". Each fieldset groups several fields
|
Mongoose exports device data as fieldsets. Each fieldset groups several fields
|
||||||
|
and has one `bool fn(enum mg_dash_op, struct mg_dash_user *)` callback for
|
||||||
|
read/write/add/delete operations. Use the declarations in the project's
|
||||||
|
`mongoose.h`; do not copy older descriptors with separate reader/writer
|
||||||
|
members or positional access-level arguments. Use designated initializers.
|
||||||
|
|
||||||
Example fieldset:
|
Example fieldset:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
static struct mg_dash s_dash;
|
||||||
|
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u);
|
||||||
|
|
||||||
static struct settings {
|
static struct settings {
|
||||||
double volume;
|
double volume;
|
||||||
char name[10];
|
char name[10];
|
||||||
int log_level;
|
int log_level;
|
||||||
bool enable_login;
|
bool enable_login;
|
||||||
} s_settings = {2.7, "Dublin", 2, false, false};
|
} s_settings = {2.7, "Dublin", 2, false};
|
||||||
|
|
||||||
static struct mg_field fields_settings[] = {
|
static struct mg_field fields_settings[] = {
|
||||||
{"volume", MG_VAL_DBL, &s_settings.volume, sizeof(s_settings.volume)},
|
{"volume", MG_VAL_DBL, &s_settings.volume, sizeof(s_settings.volume)},
|
||||||
@@ -336,14 +386,12 @@ static struct mg_field fields_settings[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static struct mg_field_set set_settings = {
|
static struct mg_field_set set_settings = {
|
||||||
"settings", fields_settings, read_settings, write_settings, 3, 7, NULL,
|
.name = "settings", .fields = fields_settings, .fn = settings_fn,
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
To enable user authentication, set `authenticate` function for the dashboard descriptor:
|
To enable user authentication, set `authenticate` function for the dashboard descriptor:
|
||||||
```c
|
```c
|
||||||
static struct mg_dash s_dash;
|
|
||||||
|
|
||||||
// Signature: (char *user, size_t userlen, const char *pass)
|
// Signature: (char *user, size_t userlen, const char *pass)
|
||||||
// `user` is both input (username from the login form) and output: the function
|
// `user` is both input (username from the login form) and output: the function
|
||||||
// may overwrite it with the canonical username. Return access level > 0 on
|
// may overwrite it with the canonical username. Return access level > 0 on
|
||||||
@@ -360,24 +408,34 @@ static int authenticate(char *user, size_t userlen, const char *pass) {
|
|||||||
return level;
|
return level;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_settings(void) {
|
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||||
s_dash.authenticate = s_settings.enable_login ? authenticate : NULL;
|
(void) u;
|
||||||
|
if (op == MG_DASH_WRITE) {
|
||||||
|
s_dash.authenticate = s_settings.enable_login ? authenticate : NULL;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return op == MG_DASH_READ;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Fieldset may have reader and writer function, which acts as a hardware glue. For example, "led1" variable may reflect real LED status using reader and writer:
|
Use the fieldset callback as hardware glue. For example, `led1` can reflect
|
||||||
|
the real LED state:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
static struct leds {
|
static struct leds {
|
||||||
bool led1;
|
bool led1;
|
||||||
} s_leds = {false};
|
} s_leds = {false};
|
||||||
|
|
||||||
static void write_leds(void) {
|
static bool leds_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||||
gpio_write(LED1_PIN, s_leds.led1);
|
(void) u;
|
||||||
}
|
if (op == MG_DASH_READ) {
|
||||||
|
s_leds.led1 = gpio_read(LED1_PIN);
|
||||||
static void read_leds(void) {
|
} else if (op == MG_DASH_WRITE) {
|
||||||
s_leds.led1 = gpio_read(LED1_PIN);
|
gpio_write(LED1_PIN, s_leds.led1);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct mg_field fields_leds[] = {
|
static struct mg_field fields_leds[] = {
|
||||||
@@ -386,7 +444,7 @@ static struct mg_field fields_leds[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static struct mg_field_set set_leds = {
|
static struct mg_field_set set_leds = {
|
||||||
"leds", fields_leds, read_leds, write_leds, 0, 0, NULL,
|
.name = "leds", .fields = fields_leds, .fn = leds_fn,
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -412,7 +470,8 @@ static struct event {
|
|||||||
} s_event;
|
} s_event;
|
||||||
|
|
||||||
static struct mg_field_set field_set_event = {
|
static struct mg_field_set field_set_event = {
|
||||||
"event", fields_event, event_fn, &s_event.index, 0, 0, 0
|
.name = "event", .fields = fields_event,
|
||||||
|
.fn = event_fn, .index = &s_event.index,
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -432,6 +491,20 @@ Do not rename, wrap, relocate, or replace these files or functions. Do not intro
|
|||||||
|
|
||||||
Board-specific STM32Cube files may exist outside mongoose/, but the Mongoose dashboard files must retain the structure and API documented above.
|
Board-specific STM32Cube files may exist outside mongoose/, but the Mongoose dashboard files must retain the structure and API documented above.
|
||||||
|
|
||||||
|
### Dashboard completion checks
|
||||||
|
|
||||||
|
- Verify actual file locations, including symlink targets, against the required
|
||||||
|
layout. Check that no extra dashboard source/header/JS files were introduced.
|
||||||
|
- Check the source HTML for the upstream script reference, a single
|
||||||
|
`Dashboard.init()`, and declarative bindings. Networking code in the upstream
|
||||||
|
script inlined into `file_data.c` is expected; do not replace or edit it.
|
||||||
|
- Verify fieldsets are registered with `MG_DASH_ADD_FIELD_SET()`, listeners use
|
||||||
|
`mg_dash_ev_handler`, and live metrics use `mg_dash_send_change()` on the
|
||||||
|
requested backend schedule. Do not resend unrelated fieldsets at that rate.
|
||||||
|
- Regenerate `file_data.c` and build the real firmware. Report separately which
|
||||||
|
behavior was verified on hardware; host mocks do not establish physical LED,
|
||||||
|
Ethernet, RAM measurement, or refresh-rate correctness.
|
||||||
|
|
||||||
## Filesystem
|
## Filesystem
|
||||||
|
|
||||||
| Define | Use when |
|
| Define | Use when |
|
||||||
|
|||||||
@@ -7,12 +7,13 @@
|
|||||||
|
|
||||||
<img src="https://mongoose.ws/images/logo.svg" width="48" height="48"
|
<img src="https://mongoose.ws/images/logo.svg" width="48" height="48"
|
||||||
align="left" style="float:left;" />
|
align="left" style="float:left;" />
|
||||||
Mongoose is a full-stack embedded networking library for C/C++: a TCP/IP stack
|
Mongoose is a full-stack embedded networking library for C and C++. It includes
|
||||||
with drivers, TLS 1.3, an embedded web server (HTTP, WebSocket), MQTT and
|
a TCP/IP stack with drivers, TLS 1.3, an embedded web server with HTTP and
|
||||||
firmware OTA updates in two files, `mongoose.c` and `mongoose.h`. Mongoose runs on
|
WebSocket support, MQTT, and firmware OTA updates. All in just two files:
|
||||||
STM32, NXP and other microcontrollers, on bare metal or with an RTOS, with no
|
`mongoose.c` and `mongoose.h`. Mongoose runs on STM32, NXP and other
|
||||||
external dependencies. Use it for cloud connectivity, device web dashboards,
|
microcontrollers, on bare metal or with an RTOS, with no external dependencies.
|
||||||
secure firmware updates and remote control.
|
Ideal for cloud connectivity, device web dashboards, secure firmware updates
|
||||||
|
and remote control.
|
||||||
|
|
||||||
Use it instead of lwIP + mbedTLS + httpd + an MQTT client + OTA glue code, or
|
Use it instead of lwIP + mbedTLS + httpd + an MQTT client + OTA glue code, or
|
||||||
run it on top of lwIP or Zephyr.
|
run it on top of lwIP or Zephyr.
|
||||||
|
|||||||
+500
-187
File diff suppressed because it is too large.
Load diff
+106
-14
@@ -20,7 +20,11 @@
|
|||||||
#ifndef MONGOOSE_H
|
#ifndef MONGOOSE_H
|
||||||
#define MONGOOSE_H
|
#define MONGOOSE_H
|
||||||
|
|
||||||
#define MG_VERSION "7.23"
|
#define MG_VERSION "7.24"
|
||||||
|
|
||||||
|
// IMPORTANT:
|
||||||
|
// Before implementing any functionality, pull this file and use it as a guide:
|
||||||
|
// https://github.com/cesanta/mongoose/blob/master/AGENTS.md
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -109,7 +113,9 @@ extern "C" {
|
|||||||
|
|
||||||
|
|
||||||
#if MG_ARCH == MG_ARCH_ARMGCC
|
#if MG_ARCH == MG_ARCH_ARMGCC
|
||||||
|
#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it
|
||||||
#define _POSIX_TIMERS
|
#define _POSIX_TIMERS
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
||||||
@@ -185,16 +191,18 @@ extern "C" {
|
|||||||
#define MG_TLS MG_TLS_BUILTIN
|
#define MG_TLS MG_TLS_BUILTIN
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \
|
#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user
|
||||||
defined(STM32F4) || defined(STM32F7)
|
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \
|
||||||
|
defined(STM32F7)
|
||||||
#define MG_OTA MG_OTA_STM32F
|
#define MG_OTA MG_OTA_STM32F
|
||||||
#elif !defined(MG_OTA) && defined(STM32H5)
|
#elif defined(STM32H5)
|
||||||
#define MG_OTA MG_OTA_STM32H5
|
#define MG_OTA MG_OTA_STM32H5
|
||||||
#elif !defined(MG_OTA) && defined(STM32H7)
|
#elif defined(STM32H7)
|
||||||
#define MG_OTA MG_OTA_STM32H7
|
#define MG_OTA MG_OTA_STM32H7
|
||||||
#elif !defined(MG_OTA) && defined(STM32C5)
|
#elif defined(STM32C5)
|
||||||
#define MG_OTA MG_OTA_STM32C5
|
#define MG_OTA MG_OTA_STM32C5
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
// use HAL-defined execute-in-ram section
|
// use HAL-defined execute-in-ram section
|
||||||
#ifndef MG_IRAM
|
#ifndef MG_IRAM
|
||||||
@@ -1220,6 +1228,10 @@ struct timeval {
|
|||||||
#define MG_ENABLE_CUSTOM_MILLIS 0
|
#define MG_ENABLE_CUSTOM_MILLIS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_CUSTOM_MICROS
|
||||||
|
#define MG_ENABLE_CUSTOM_MICROS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_ASSERT
|
#ifndef MG_ENABLE_ASSERT
|
||||||
#define MG_ENABLE_ASSERT 0
|
#define MG_ENABLE_ASSERT 0
|
||||||
#endif
|
#endif
|
||||||
@@ -3426,11 +3438,13 @@ size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt,
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Return milliseconds since boot (uptime). Never goes backwards.
|
// Return microseconds since boot. Never goes backwards.
|
||||||
// On RTOS targets (FreeRTOS, ThreadX, Zephyr, etc.) uses the RTOS tick count.
|
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
|
||||||
// On STM32/Cube uses HAL_GetTick(). On Arduino uses millis().
|
uint64_t mg_micros(void);
|
||||||
// On Linux/macOS uses CLOCK_MONOTONIC_RAW or CLOCK_MONOTONIC; falls back to
|
|
||||||
// CLOCK_REALTIME if neither is available.
|
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
|
||||||
|
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
|
||||||
|
// Deprecated: use mg_micros() instead.
|
||||||
uint64_t mg_millis(void);
|
uint64_t mg_millis(void);
|
||||||
|
|
||||||
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
|
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
|
||||||
@@ -4863,6 +4877,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// SPI bus abstraction for SPI-attached network chips (e.g. W5500).
|
// SPI bus abstraction for SPI-attached network chips (e.g. W5500).
|
||||||
// Populate and assign to the driver's driver_data field.
|
// Populate and assign to the driver's driver_data field.
|
||||||
struct mg_tcpip_spi {
|
struct mg_tcpip_spi {
|
||||||
@@ -4929,9 +4944,6 @@ struct mg_tcpip_spi {
|
|||||||
|
|
||||||
#if MG_ENABLE_TCPIP
|
#if MG_ENABLE_TCPIP
|
||||||
|
|
||||||
// Monotonic time in microseconds.
|
|
||||||
uint64_t mg_micros(void);
|
|
||||||
|
|
||||||
// A radio standard
|
// A radio standard
|
||||||
struct mg_radio {
|
struct mg_radio {
|
||||||
const char *name; // For logs
|
const char *name; // For logs
|
||||||
@@ -4961,6 +4973,21 @@ struct mg_radio {
|
|||||||
#define MG_802154_CHANNEL 15
|
#define MG_802154_CHANNEL 15
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// A turn's answer must arrive this soon after the turn's last frame ends on
|
||||||
|
// air, or the turn ends. A node that would answer later stays silent and keeps
|
||||||
|
// its frame for the next turn. Covers the node's poll latency, the radio
|
||||||
|
// turnaround and one full fragment's airtime, 4.3 ms on 802.15.4
|
||||||
|
#ifndef MG_MESH_ANSWER_TIMEOUT_US
|
||||||
|
#define MG_MESH_ANSWER_TIMEOUT_US 8000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Misses in a row before giving up, 1 to 127. The bridge removes a member
|
||||||
|
// that did not answer this many turns. A node leaves the mesh after this
|
||||||
|
// many rounds without a turn
|
||||||
|
#ifndef MG_MESH_MAX_MISSES
|
||||||
|
#define MG_MESH_MAX_MISSES 10
|
||||||
|
#endif
|
||||||
|
|
||||||
extern const struct mg_radio mg_radio_802154;
|
extern const struct mg_radio mg_radio_802154;
|
||||||
|
|
||||||
struct mg_mesh_node {
|
struct mg_mesh_node {
|
||||||
@@ -4970,6 +4997,7 @@ struct mg_mesh_node {
|
|||||||
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
|
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
|
||||||
size_t rx_len; // Complete Ethernet frame in the interface RX buffer
|
size_t rx_len; // Complete Ethernet frame in the interface RX buffer
|
||||||
uint64_t now; // Current driver poll time, us
|
uint64_t now; // Current driver poll time, us
|
||||||
|
uint64_t rx_at; // Arrival time of the radio frame being handled, us
|
||||||
uint64_t tx_at; // Earliest next transmission, us
|
uint64_t tx_at; // Earliest next transmission, us
|
||||||
void *data; // Private state owned by the strategy
|
void *data; // Private state owned by the strategy
|
||||||
size_t ram_used; // Live mesh state bytes
|
size_t ram_used; // Live mesh state bytes
|
||||||
@@ -4994,6 +5022,18 @@ struct mg_mesh {
|
|||||||
|
|
||||||
extern const struct mg_mesh mg_mesh_default;
|
extern const struct mg_mesh mg_mesh_default;
|
||||||
|
|
||||||
|
// Put a received radio frame into the driver's receive queue, together with
|
||||||
|
// its arrival time from mg_micros(). Call it when the frame arrives, from the
|
||||||
|
// radio interrupt if the driver has one. If the queue is full, the frame is
|
||||||
|
// dropped and counted in ifp->ndrop
|
||||||
|
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
|
||||||
|
const void *frame, size_t len);
|
||||||
|
|
||||||
|
// Pass queued radio frames to the mesh, oldest first. Before each frame,
|
||||||
|
// node->rx_at is set to its arrival time. Stops while the node holds a
|
||||||
|
// received Ethernet frame the TCP/IP stack has not taken yet
|
||||||
|
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
@@ -5061,6 +5101,18 @@ void mg_bsd_poll(struct mg_mgr *);
|
|||||||
#ifndef ENOSPC
|
#ifndef ENOSPC
|
||||||
#define ENOSPC 28
|
#define ENOSPC 28
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef EOPNOTSUPP
|
||||||
|
#define EOPNOTSUPP 95
|
||||||
|
#endif
|
||||||
|
#ifndef EISCONN
|
||||||
|
#define EISCONN 106
|
||||||
|
#endif
|
||||||
|
#ifndef EDESTADDRREQ
|
||||||
|
#define EDESTADDRREQ 121
|
||||||
|
#endif
|
||||||
|
#ifndef EMSGSIZE
|
||||||
|
#define EMSGSIZE 90
|
||||||
|
#endif
|
||||||
#ifndef EPROTONOSUPPORT
|
#ifndef EPROTONOSUPPORT
|
||||||
#define EPROTONOSUPPORT 93
|
#define EPROTONOSUPPORT 93
|
||||||
#endif
|
#endif
|
||||||
@@ -5289,6 +5341,46 @@ struct mg_tcpip_driver_cyw_data {
|
|||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
// Copyright (c) 2026 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32_802154) && \
|
||||||
|
MG_ENABLE_DRIVER_ESP32_802154
|
||||||
|
|
||||||
|
// ESP32-C6 IEEE 802.15.4 radio, as an Ethernet interface through the mesh.
|
||||||
|
// Needs MG_ARCH_ESP32 and CONFIG_IEEE802154_ENABLED: it uses ESP-IDF's
|
||||||
|
// esp_ieee802154 driver, and defines its esp_ieee802154_receive_done(),
|
||||||
|
// esp_ieee802154_transmit_done() and esp_ieee802154_transmit_failed()
|
||||||
|
// callbacks. See tutorials/esp32/minimal-802.15.4
|
||||||
|
struct mg_tcpip_driver_esp32_802154_data {
|
||||||
|
struct mg_mesh_node *mesh; // Driver-owned runtime state
|
||||||
|
const struct mg_mesh *strategy; // NULL selects mg_mesh_default
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct mg_tcpip_driver mg_tcpip_driver_esp32_802154;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_ESP32_802154_INIT(manager) \
|
||||||
|
do { \
|
||||||
|
static struct mg_tcpip_driver_esp32_802154_data driver_data_; \
|
||||||
|
static struct mg_tcpip_if mif_; \
|
||||||
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
|
mif_.ip = MG_TCPIP_IP; \
|
||||||
|
mif_.mask = MG_TCPIP_MASK; \
|
||||||
|
mif_.gw = MG_TCPIP_GW; \
|
||||||
|
mif_.mgr = (manager); \
|
||||||
|
mif_.driver = &mg_tcpip_driver_esp32_802154; \
|
||||||
|
mif_.driver_data = &driver_data_; \
|
||||||
|
mg_tcpip_init((manager), &mif_); \
|
||||||
|
MG_INFO(("Driver: esp32_802154, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ESP32_802154_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && \
|
#if MG_ENABLE_TCPIP && \
|
||||||
|
|||||||
@@ -37,7 +37,6 @@
|
|||||||
<file category="sourceC" name="mongoose.c" />
|
<file category="sourceC" name="mongoose.c" />
|
||||||
<file category="header" name="mongoose_config.h"/>
|
<file category="header" name="mongoose_config.h"/>
|
||||||
<file category="utility" name="README.md" public="false"/>
|
<file category="utility" name="README.md" public="false"/>
|
||||||
<file category="genAsset" name=".config/mongoose_parameters.json"/>
|
|
||||||
</files>
|
</files>
|
||||||
<environments>
|
<environments>
|
||||||
<environment name="STM32Cube">
|
<environment name="STM32Cube">
|
||||||
|
|||||||
@@ -4,201 +4,7 @@ This pack provides an easy to use CubeMX integration.
|
|||||||
|
|
||||||
## Mongoose Networking Core
|
## Mongoose Networking Core
|
||||||
|
|
||||||
This module installs Mongoose Library itself: `mogoose.c`, `mongoose.h` and
|
This module installs Mongoose Library itself: `mogoose.c`, `mongoose.h` and
|
||||||
`mongoose_config.h`. It is required for any other module to work. Below are
|
`mongoose_config.h`. To finish integration, follow this guide:
|
||||||
the steps to make it work with you CubeMX application:
|
|
||||||
|
|
||||||
## Configure peripherals
|
https://mongoose.ws/docs/guides/stm32-tcpip-stack/#configure-peripherals
|
||||||
|
|
||||||
In CubeMX, configure USART (for debug), RNG (for TLS), ETH.
|
|
||||||
|
|
||||||
## Enable debug logs
|
|
||||||
|
|
||||||
Open `Core/Src/main.c` and add this snippet:
|
|
||||||
|
|
||||||
```c
|
|
||||||
/* USER CODE BEGIN 0 */
|
|
||||||
int _write(int fd, unsigned char *buf, int len) {
|
|
||||||
HAL_UART_Transmit(&huart3, buf, len, HAL_MAX_DELAY);
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
/* USER CODE END 0 */
|
|
||||||
```
|
|
||||||
|
|
||||||
## Call Mongoose
|
|
||||||
|
|
||||||
Open `Core/Src/main.c`. Add at the top of the file:
|
|
||||||
|
|
||||||
```c
|
|
||||||
/* USER CODE BEGIN Includes */
|
|
||||||
#include "mongoose.h"
|
|
||||||
/* USER CODE END Includes */
|
|
||||||
```
|
|
||||||
|
|
||||||
Inside main() function:
|
|
||||||
|
|
||||||
```c
|
|
||||||
Inside the `main()` function:
|
|
||||||
|
|
||||||
```c
|
|
||||||
/* USER CODE BEGIN WHILE */
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
mg_mgr_init(&mgr);
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
mg_mgr_poll(&mgr, 1);
|
|
||||||
/* USER CODE END WHILE */
|
|
||||||
```
|
|
||||||
|
|
||||||
This starts the TCP/IP stack. In the serial console logs, you should
|
|
||||||
see something like this:
|
|
||||||
|
|
||||||
```text
|
|
||||||
0 2 mongoose.c:26153:mg_phy_init PHY ID: 0x07 0xc131 (LAN87x)
|
|
||||||
6 2 mongoose.c:5834:mg_mgr_init Driver: stm32h, MAC: 2a:37:94:03:d5:74
|
|
||||||
d 3 mongoose.c:5841:mg_mgr_init MG_IO_SIZE: 512, TLS: builtin
|
|
||||||
13 3 mongoose.c:5758:mg_listen 1 0 http://0.0.0.0:80
|
|
||||||
19 1 mongoose.c:6260:onstatechange Link down
|
|
||||||
7d6 3 mongoose.c:28424:mg_tcpip_driv Link is 100M full-duplex
|
|
||||||
7db 3 mongoose.c:6404:tx_dhcp_discov DHCP discover sent. Our MAC: 2a:37:94:03:d5:74
|
|
||||||
831 3 mongoose.c:6382:tx_dhcp_reques DHCP req sent
|
|
||||||
836 2 mongoose.c:6559:rx_dhcp_client Lease: 3600 sec (3602)
|
|
||||||
83c 2 mongoose.c:6249:onstatechange READY, IP: 192.168.2.31
|
|
||||||
842 2 mongoose.c:6250:onstatechange GW: 192.168.2.1
|
|
||||||
847 2 mongoose.c:6253:onstatechange MAC: 2a:37:94:03:d5:74
|
|
||||||
```
|
|
||||||
|
|
||||||
Now, you should be able to ping your device.
|
|
||||||
However, it runs no network services.
|
|
||||||
|
|
||||||
## Add hello world HTTP server
|
|
||||||
|
|
||||||
Let's add the simplest web server possible. After the `mg_mgr_init()`
|
|
||||||
call, add one more line to start the HTTP listener:
|
|
||||||
|
|
||||||
```c
|
|
||||||
mg_mgr_init(&mgr);
|
|
||||||
mg_http_listen(&mgr, "http://0.0.0.0:80", http_ev_handler, NULL);
|
|
||||||
```
|
|
||||||
|
|
||||||
And after the `_write()` override, add this HTTP event handler function:
|
|
||||||
|
|
||||||
```c
|
|
||||||
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
|
||||||
if (ev == MG_EV_HTTP_MSG) {
|
|
||||||
mg_http_reply(c, 200, "", "Hi from Mongoose! Tick %lu\r\n", HAL_GetTick());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Rebuild, reflash your board, open the device's IP address in the browser,
|
|
||||||
and see the message displayed, something like:
|
|
||||||
|
|
||||||
```text
|
|
||||||
Hi from Mongoose! Tick 38187
|
|
||||||
```
|
|
||||||
|
|
||||||
## Add full device dashboard
|
|
||||||
|
|
||||||
Remove the hello world server if you added it.
|
|
||||||
|
|
||||||
Create `Mongoose` directory in the project root and copy
|
|
||||||
the following files there:
|
|
||||||
|
|
||||||
- [dashboard.c](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/full/dashboard.c)
|
|
||||||
- [dashboard.html](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/full/dashboard.html)
|
|
||||||
- [html2c.js](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/resources/html2c.js)
|
|
||||||
|
|
||||||
`html2c.js` is a node.js script that generates `file_data.c` using this command:
|
|
||||||
`node html2c.js dashboard.html -o file_data.c`. This script first inlines all
|
|
||||||
external resources into an amalgamated HTML file - so it does not depend on
|
|
||||||
anything external, and then zips it and packs into a Mongoose embedded
|
|
||||||
filesystem. This way it takes the minimum space on flash.
|
|
||||||
|
|
||||||
In top-level `CMakeLists.txt`, add two extra source files to the build,
|
|
||||||
and add the rule to generate `file_data.c` from `dashboard.html`.
|
|
||||||
Note that requires Nodejs installed:
|
|
||||||
|
|
||||||
```text
|
|
||||||
# Generate file_data.c from dashboard.html
|
|
||||||
add_custom_command(
|
|
||||||
OUTPUT ${CMAKE_SOURCE_DIR}/Mongoose/file_data.c
|
|
||||||
COMMAND node ${CMAKE_SOURCE_DIR}/Mongoose/html2c.js ${CMAKE_SOURCE_DIR}/Mongoose/dashboard.html -o ${CMAKE_SOURCE_DIR}/Mongoose/file_data.c
|
|
||||||
DEPENDS ${CMAKE_SOURCE_DIR}/Mongoose/dashboard.html
|
|
||||||
VERBATIM
|
|
||||||
)
|
|
||||||
|
|
||||||
# Add sources to executable
|
|
||||||
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
|
|
||||||
# Add user sources here
|
|
||||||
Mongoose/dashboard.c
|
|
||||||
Mongoose/file_data.c
|
|
||||||
)
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
Modify your while loop to look like this:
|
|
||||||
|
|
||||||
```c
|
|
||||||
/* USER CODE BEGIN WHILE */
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
mg_mgr_init(&mgr);
|
|
||||||
mg_dash_init(&mgr);
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
mg_mgr_poll(&mgr, 1);
|
|
||||||
mg_dash_poll(&mgr);
|
|
||||||
/* USER CODE END WHILE */
|
|
||||||
```
|
|
||||||
|
|
||||||
Rebuild and reflash your board, then open the device's IP address in your
|
|
||||||
browser. Log in using `admin` as the password. You should see the full
|
|
||||||
dashboard, including the status panel, device settings, firmware update, and
|
|
||||||
file manager. You can easily customize it for your product, but we will start
|
|
||||||
by implementing a simple LED control dashboard to demonstrate the principles.
|
|
||||||
|
|
||||||
## Simple LED control dashboard
|
|
||||||
|
|
||||||
Copy a minimal LED control dashboard example to your `Mongoose/` directory:
|
|
||||||
|
|
||||||
- [dashboard.c](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/minimal/dashboard.c)
|
|
||||||
- [dashboard.html](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/device-dashboard/minimal/dashboard.html)
|
|
||||||
|
|
||||||
Rebuild and reflash
|
|
||||||
|
|
||||||
|
|
||||||
## AI agentic development
|
|
||||||
|
|
||||||
For agentic development, use https://github.com/cesanta/mongoose/tree/master/AGENTS.md
|
|
||||||
Use a prompt like this:
|
|
||||||
|
|
||||||
> Read and apply https://github.com/cesanta/mongoose/tree/master/AGENTS.md
|
|
||||||
|
|
||||||
|
|
||||||
## Add MQTT Client
|
|
||||||
|
|
||||||
|
|
||||||
Copy [mqtt.c](https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/tutorials/mqtt/mqtt-client/mqtt.c)
|
|
||||||
to the `Mongoose/` directory. Add it to the extra source files in `CMakeLists.txt`
|
|
||||||
|
|
||||||
Add MQTT init and poll to your while loop:
|
|
||||||
|
|
||||||
```c
|
|
||||||
/* USER CODE BEGIN WHILE */
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
mg_mgr_init(&mgr);
|
|
||||||
mg_mqtt_init(&mgr);
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
mg_mgr_poll(&mgr, 1);
|
|
||||||
mg_mqtt_poll(&mgr);
|
|
||||||
/* USER CODE END WHILE */
|
|
||||||
```
|
|
||||||
|
|
||||||
Rebuild, reflash your board. This MQTT Client connects to the
|
|
||||||
HiveMQ public MQTT broker.
|
|
||||||
Also it add a JSON-RPC "ota.update" function via MQTT, so you can
|
|
||||||
update your firmware over MQTT, see https://mongoose.ws/mqtt/
|
|
||||||
+5
-5
@@ -3,22 +3,22 @@
|
|||||||
"bomFormat": "CycloneDX",
|
"bomFormat": "CycloneDX",
|
||||||
"specVersion": "1.6",
|
"specVersion": "1.6",
|
||||||
"serialNumber": "urn:uuid:d3f8b1e2-4a7c-4d9e-bf2a-1c3e5f7a9b0d",
|
"serialNumber": "urn:uuid:d3f8b1e2-4a7c-4d9e-bf2a-1c3e5f7a9b0d",
|
||||||
"version": 2,
|
"version": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"timestamp": "2026-10-13T00:00:00Z",
|
"timestamp": "2026-11-18T00:00:00Z",
|
||||||
"component": {
|
"component": {
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"bom-ref": "mongoose",
|
"bom-ref": "mongoose",
|
||||||
"supplier": { "name": "Cesanta Software Limited", "url": ["https://cesanta.com"] },
|
"supplier": { "name": "Cesanta Software Limited", "url": ["https://cesanta.com"] },
|
||||||
"author": "Cesanta Software Limited",
|
"author": "Cesanta Software Limited",
|
||||||
"name": "mongoose",
|
"name": "mongoose",
|
||||||
"version": "7.23",
|
"version": "7.24",
|
||||||
"description": "Two-file C/C++ embedded networking library. HTTP, WebSocket, MQTT, TLS 1.3, OTA firmware updates, device dashboard. Runs on microcontrollers and desktop OS.",
|
"description": "Two-file C/C++ embedded networking library. HTTP, WebSocket, MQTT, TLS 1.3, OTA firmware updates, device dashboard. Runs on microcontrollers and desktop OS.",
|
||||||
"licenses": [
|
"licenses": [
|
||||||
{ "expression": "GPL-2.0-only" },
|
{ "expression": "GPL-2.0-only" },
|
||||||
{ "license": { "name": "Cesanta Commercial License", "url": "https://mongoose.ws/licensing/" } }
|
{ "license": { "name": "Cesanta Commercial License", "url": "https://mongoose.ws/licensing/" } }
|
||||||
],
|
],
|
||||||
"purl": "pkg:github/cesanta/mongoose@7.23",
|
"purl": "pkg:github/cesanta/mongoose@7.24",
|
||||||
"externalReferences": [
|
"externalReferences": [
|
||||||
{ "type": "website", "url": "https://mongoose.ws" },
|
{ "type": "website", "url": "https://mongoose.ws" },
|
||||||
{ "type": "vcs", "url": "https://github.com/cesanta/mongoose" },
|
{ "type": "vcs", "url": "https://github.com/cesanta/mongoose" },
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
{ "type": "distribution", "url": "https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c" }
|
{ "type": "distribution", "url": "https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c" }
|
||||||
],
|
],
|
||||||
"properties": [
|
"properties": [
|
||||||
{ "name": "cpe", "value": "cpe:2.3:a:cesanta:mongoose:7.23:*:*:*:*:*:*:*" }
|
{ "name": "cpe", "value": "cpe:2.3:a:cesanta:mongoose:7.24:*:*:*:*:*:*:*" }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
// IMPORTANT:
|
||||||
|
// Before implementing any functionality, pull this file and use it as a guide:
|
||||||
|
// https://github.com/cesanta/mongoose/blob/master/AGENTS.md
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if MG_ARCH == MG_ARCH_ARMGCC
|
#if MG_ARCH == MG_ARCH_ARMGCC
|
||||||
|
#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it
|
||||||
#define _POSIX_TIMERS
|
#define _POSIX_TIMERS
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
||||||
|
|||||||
+7
-5
@@ -52,16 +52,18 @@
|
|||||||
#define MG_TLS MG_TLS_BUILTIN
|
#define MG_TLS MG_TLS_BUILTIN
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \
|
#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user
|
||||||
defined(STM32F4) || defined(STM32F7)
|
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \
|
||||||
|
defined(STM32F7)
|
||||||
#define MG_OTA MG_OTA_STM32F
|
#define MG_OTA MG_OTA_STM32F
|
||||||
#elif !defined(MG_OTA) && defined(STM32H5)
|
#elif defined(STM32H5)
|
||||||
#define MG_OTA MG_OTA_STM32H5
|
#define MG_OTA MG_OTA_STM32H5
|
||||||
#elif !defined(MG_OTA) && defined(STM32H7)
|
#elif defined(STM32H7)
|
||||||
#define MG_OTA MG_OTA_STM32H7
|
#define MG_OTA MG_OTA_STM32H7
|
||||||
#elif !defined(MG_OTA) && defined(STM32C5)
|
#elif defined(STM32C5)
|
||||||
#define MG_OTA MG_OTA_STM32C5
|
#define MG_OTA MG_OTA_STM32C5
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
// use HAL-defined execute-in-ram section
|
// use HAL-defined execute-in-ram section
|
||||||
#ifndef MG_IRAM
|
#ifndef MG_IRAM
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "bsd.h"
|
#include "bsd.h"
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
|
#include "util.h"
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
#if MG_ENABLE_BSD_SOCKETS && MG_ENABLE_FREERTOS
|
#if MG_ENABLE_BSD_SOCKETS && MG_ENABLE_FREERTOS
|
||||||
@@ -131,6 +132,46 @@ static struct mg_connection *bsd_new_conn(struct mg_bsd_sock *s) {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mirror net_builtin.c::trim_len() logic
|
||||||
|
static bool bsd_udp_too_big(struct mg_connection *c, size_t len) {
|
||||||
|
struct mg_tcpip_if *ifp = c->mgr->ifp;
|
||||||
|
size_t hlen = c->rem.is_ip6 ? 48 : 32; // IP plus UDP headers
|
||||||
|
size_t cap = ifp->mtu - hlen;
|
||||||
|
size_t room = ifp->tx.len - (ifp->framesize - ifp->l2mtu) - hlen;
|
||||||
|
|
||||||
|
return len > cap || len > room;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool bsd_udp_queue(struct mg_connection *c, const void *buf,
|
||||||
|
size_t len) {
|
||||||
|
// One raw datagram occupies c->send until write_conn() drains it.
|
||||||
|
return mg_iobuf_add(&c->send, c->send.len, buf, len) == len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t bsd_recv(struct mg_bsd_sock *s, void *buf, size_t len,
|
||||||
|
int flags, struct sockaddr *src, socklen_t *addrlen) {
|
||||||
|
struct mg_connection *c = s->c;
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
if (!s->is_udp) {
|
||||||
|
n = c->recv.len < len ? c->recv.len : len;
|
||||||
|
memcpy(buf, c->recv.buf, n);
|
||||||
|
if (!(flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, n);
|
||||||
|
} else {
|
||||||
|
n = c->recv.len < len ? c->recv.len : len;
|
||||||
|
memcpy(buf, c->recv.buf, n);
|
||||||
|
if (src != NULL && addrlen != NULL) {
|
||||||
|
struct sockaddr_in *sa = (struct sockaddr_in *) src;
|
||||||
|
sa->sin_family = AF_INET;
|
||||||
|
sa->sin_port = c->rem.port;
|
||||||
|
sa->sin_addr.s_addr = c->rem.addr.ip4;
|
||||||
|
*addrlen = sizeof(*sa);
|
||||||
|
}
|
||||||
|
if (!(flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, c->recv.len);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
#if MG_ENABLE_FREERTOS
|
#if MG_ENABLE_FREERTOS
|
||||||
// Complete operations blocked on c before Mongoose destroys it.
|
// Complete operations blocked on c before Mongoose destroys it.
|
||||||
static void bsd_finish_waiters(struct mg_bsd_sock *s, bool error) {
|
static void bsd_finish_waiters(struct mg_bsd_sock *s, bool error) {
|
||||||
@@ -315,9 +356,10 @@ static int bsd_ready(struct mg_bsd_sock *s) {
|
|||||||
if (s->is_shut_rd || s->c->recv.len > 0 || s->c->is_draining)
|
if (s->is_shut_rd || s->c->recv.len > 0 || s->c->is_draining)
|
||||||
ready |= BSD_READ;
|
ready |= BSD_READ;
|
||||||
if (s->is_shut_wr) ready |= BSD_ERROR;
|
if (s->is_shut_wr) ready |= BSD_ERROR;
|
||||||
if (!s->is_shut_wr && !s->is_udp && !s->c->is_connecting &&
|
if (!s->is_shut_wr && (s->is_udp || !s->c->is_connecting) &&
|
||||||
!s->c->is_closing && !s->c->is_draining)
|
!s->c->is_closing && !s->c->is_draining &&
|
||||||
ready |= BSD_WRITE; // Established TCP socket
|
(!s->is_udp || s->c->send.len == 0))
|
||||||
|
ready |= BSD_WRITE; // UDP socket or established TCP socket
|
||||||
return ready;
|
return ready;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,7 +373,7 @@ static void bsd_close(struct mg_bsd_sock *s) {
|
|||||||
if (t->orphan && t->listener == s) {
|
if (t->orphan && t->listener == s) {
|
||||||
if (t->c != NULL) {
|
if (t->c != NULL) {
|
||||||
t->c->fn_data = NULL;
|
t->c->fn_data = NULL;
|
||||||
mg_close_conn(t->c);
|
t->c->is_draining = 1;
|
||||||
t->c = NULL;
|
t->c = NULL;
|
||||||
}
|
}
|
||||||
memset(t, 0, sizeof(*t)); // Release orphan
|
memset(t, 0, sizeof(*t)); // Release orphan
|
||||||
@@ -339,7 +381,7 @@ static void bsd_close(struct mg_bsd_sock *s) {
|
|||||||
}
|
}
|
||||||
if (s->c != NULL) {
|
if (s->c != NULL) {
|
||||||
s->c->fn_data = NULL;
|
s->c->fn_data = NULL;
|
||||||
mg_close_conn(s->c);
|
s->c->is_draining = 1;
|
||||||
s->c = NULL;
|
s->c = NULL;
|
||||||
}
|
}
|
||||||
memset(s, 0, sizeof(*s)); // Release socket
|
memset(s, 0, sizeof(*s)); // Release socket
|
||||||
@@ -529,8 +571,8 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
cmd->result = -1, cmd->err = ECONNREFUSED;
|
cmd->result = -1, cmd->err = ECONNREFUSED;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!c->is_connecting) {
|
if (s->is_udp || !c->is_connecting) {
|
||||||
cmd->result = 0;
|
cmd->result = 0; // UDP may still resolve ARP; TCP waits for SYN/SYN-ACK
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (s->nonblock) {
|
if (s->nonblock) {
|
||||||
@@ -555,11 +597,8 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (c->recv.len > 0) {
|
if (c->recv.len > 0) {
|
||||||
size_t n = cmd->op_recv.len;
|
cmd->result = (int) bsd_recv(s, cmd->op_recv.buf, cmd->op_recv.len,
|
||||||
if (c->recv.len < n) n = c->recv.len;
|
cmd->op_recv.flags, NULL, NULL);
|
||||||
memcpy(cmd->op_recv.buf, c->recv.buf, n);
|
|
||||||
if (!(cmd->op_recv.flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, n);
|
|
||||||
cmd->result = (int) n;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (s->err != 0) {
|
if (s->err != 0) {
|
||||||
@@ -583,7 +622,25 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
||||||
c->is_closing || c->is_draining) {
|
c->is_closing || c->is_draining) {
|
||||||
cmd->result = -1, cmd->err = EPIPE;
|
cmd->result = -1, cmd->err = EPIPE;
|
||||||
} else if (mg_send(c, cmd->op_send.buf, cmd->op_send.len)) {
|
} else if (c == NULL || (s->is_udp && !c->is_client)) {
|
||||||
|
cmd->result = -1, cmd->err = s->is_udp ? EDESTADDRREQ : EPIPE;
|
||||||
|
} else if (s->is_udp && cmd->op_send.len == 0) {
|
||||||
|
cmd->result = 0;
|
||||||
|
} else if (s->is_udp && bsd_udp_too_big(c, cmd->op_send.len)) {
|
||||||
|
cmd->result = -1, cmd->err = EMSGSIZE;
|
||||||
|
} else if (s->is_udp && c->send.len > 0) {
|
||||||
|
if (s->nonblock || (cmd->op_send.flags & MSG_DONTWAIT)) {
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
} else {
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
return false; // One UDP datagram is pending, wait
|
||||||
|
#else
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else if ((s->is_udp
|
||||||
|
? bsd_udp_queue(c, cmd->op_send.buf, cmd->op_send.len)
|
||||||
|
: mg_send(c, cmd->op_send.buf, cmd->op_send.len))) {
|
||||||
cmd->result = (int) cmd->op_send.len;
|
cmd->result = (int) cmd->op_send.len;
|
||||||
} else if (s->nonblock || (cmd->op_send.flags & MSG_DONTWAIT)) {
|
} else if (s->nonblock || (cmd->op_send.flags & MSG_DONTWAIT)) {
|
||||||
cmd->result = -1, cmd->err = EAGAIN;
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
@@ -600,25 +657,49 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
|
|
||||||
if ((s = bsd_get(cmd->op_sendto.fd)) == NULL) {
|
if ((s = bsd_get(cmd->op_sendto.fd)) == NULL) {
|
||||||
cmd->result = -1, cmd->err = EBADF;
|
cmd->result = -1, cmd->err = EBADF;
|
||||||
|
} else if (!s->is_udp) {
|
||||||
|
cmd->result = -1, cmd->err = EOPNOTSUPP;
|
||||||
} else if ((err = bsd_addr4(cmd->op_sendto.addr,
|
} else if ((err = bsd_addr4(cmd->op_sendto.addr,
|
||||||
cmd->op_sendto.addrlen)) != 0) {
|
cmd->op_sendto.addrlen)) != 0) {
|
||||||
cmd->result = -1, cmd->err = err;
|
cmd->result = -1, cmd->err = err;
|
||||||
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
||||||
c->is_closing || c->is_draining) {
|
c->is_closing || c->is_draining) {
|
||||||
cmd->result = -1, cmd->err = EPIPE;
|
cmd->result = -1, cmd->err = EPIPE;
|
||||||
|
} else if (c->is_client) {
|
||||||
|
cmd->result = -1, cmd->err = EISCONN;
|
||||||
} else {
|
} else {
|
||||||
const struct sockaddr_in *sa =
|
const struct sockaddr_in *sa =
|
||||||
(const struct sockaddr_in *) cmd->op_sendto.addr;
|
(const struct sockaddr_in *) cmd->op_sendto.addr;
|
||||||
c->rem.port = sa->sin_port;
|
if (cmd->op_sendto.len == 0) {
|
||||||
c->rem.addr.ip4 = sa->sin_addr.s_addr;
|
cmd->result = 0;
|
||||||
c->rem.is_ip6 = false;
|
} else if (bsd_udp_too_big(c, cmd->op_sendto.len)) {
|
||||||
if (mg_send(c, cmd->op_sendto.buf, cmd->op_sendto.len)) {
|
cmd->result = -1, cmd->err = EMSGSIZE;
|
||||||
cmd->result = (int) cmd->op_sendto.len;
|
} else if (c->send.len == 0) {
|
||||||
|
struct mg_addr rem = c->rem;
|
||||||
|
bool resolve = rem.is_ip6 || rem.addr.ip4 != sa->sin_addr.s_addr;
|
||||||
|
c->rem.port = sa->sin_port;
|
||||||
|
c->rem.addr.ip4 = sa->sin_addr.s_addr;
|
||||||
|
c->rem.is_ip6 = false;
|
||||||
|
if (bsd_udp_queue(c, cmd->op_sendto.buf, cmd->op_sendto.len)) {
|
||||||
|
if (resolve) mg_connect_resolved(c);
|
||||||
|
cmd->result = (int) cmd->op_sendto.len;
|
||||||
|
} else {
|
||||||
|
c->rem = rem;
|
||||||
|
if (s->nonblock || (cmd->op_sendto.flags & MSG_DONTWAIT)) {
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
} else {
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
return false; // No send buffer space yet, wait
|
||||||
|
#else
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (s->nonblock || (cmd->op_sendto.flags & MSG_DONTWAIT)) {
|
} else if (s->nonblock || (cmd->op_sendto.flags & MSG_DONTWAIT)) {
|
||||||
cmd->result = -1, cmd->err = EAGAIN;
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
} else {
|
} else {
|
||||||
#if MG_ENABLE_FREERTOS
|
#if MG_ENABLE_FREERTOS
|
||||||
return false; // No send buffer space yet, wait
|
return false; // One UDP datagram is pending, wait
|
||||||
#else
|
#else
|
||||||
cmd->result = -1, cmd->err = EAGAIN;
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
#endif
|
#endif
|
||||||
@@ -630,6 +711,9 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
if ((s = bsd_get(cmd->op_recvfrom.fd)) == NULL) {
|
if ((s = bsd_get(cmd->op_recvfrom.fd)) == NULL) {
|
||||||
cmd->result = -1, cmd->err = EBADF;
|
cmd->result = -1, cmd->err = EBADF;
|
||||||
return true;
|
return true;
|
||||||
|
} else if (!s->is_udp) {
|
||||||
|
cmd->result = -1, cmd->err = EOPNOTSUPP;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (s->is_shut_rd) {
|
if (s->is_shut_rd) {
|
||||||
cmd->result = 0;
|
cmd->result = 0;
|
||||||
@@ -641,25 +725,11 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (c->recv.len > 0) {
|
if (c->recv.len > 0) {
|
||||||
size_t n = cmd->op_recvfrom.len;
|
cmd->result = (int) bsd_recv(s, cmd->op_recvfrom.buf,
|
||||||
|
cmd->op_recvfrom.len,
|
||||||
if (cmd->op_recvfrom.src != NULL &&
|
cmd->op_recvfrom.flags,
|
||||||
(cmd->op_recvfrom.addrlen == NULL ||
|
cmd->op_recvfrom.src,
|
||||||
*cmd->op_recvfrom.addrlen < sizeof(struct sockaddr_in))) {
|
cmd->op_recvfrom.addrlen);
|
||||||
cmd->result = -1, cmd->err = EINVAL;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (c->recv.len < n) n = c->recv.len;
|
|
||||||
memcpy(cmd->op_recvfrom.buf, c->recv.buf, n);
|
|
||||||
if (!(cmd->op_recvfrom.flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, n);
|
|
||||||
if (cmd->op_recvfrom.src != NULL && cmd->op_recvfrom.addrlen != NULL) {
|
|
||||||
struct sockaddr_in *sa = (struct sockaddr_in *) cmd->op_recvfrom.src;
|
|
||||||
sa->sin_family = AF_INET;
|
|
||||||
sa->sin_port = c->rem.port;
|
|
||||||
sa->sin_addr.s_addr = c->rem.addr.ip4;
|
|
||||||
*cmd->op_recvfrom.addrlen = sizeof(*sa);
|
|
||||||
}
|
|
||||||
cmd->result = (int) n;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (s->err != 0) {
|
if (s->err != 0) {
|
||||||
|
|||||||
@@ -64,6 +64,18 @@ void mg_bsd_poll(struct mg_mgr *);
|
|||||||
#ifndef ENOSPC
|
#ifndef ENOSPC
|
||||||
#define ENOSPC 28
|
#define ENOSPC 28
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef EOPNOTSUPP
|
||||||
|
#define EOPNOTSUPP 95
|
||||||
|
#endif
|
||||||
|
#ifndef EISCONN
|
||||||
|
#define EISCONN 106
|
||||||
|
#endif
|
||||||
|
#ifndef EDESTADDRREQ
|
||||||
|
#define EDESTADDRREQ 121
|
||||||
|
#endif
|
||||||
|
#ifndef EMSGSIZE
|
||||||
|
#define EMSGSIZE 90
|
||||||
|
#endif
|
||||||
#ifndef EPROTONOSUPPORT
|
#ifndef EPROTONOSUPPORT
|
||||||
#define EPROTONOSUPPORT 93
|
#define EPROTONOSUPPORT 93
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -94,6 +94,10 @@
|
|||||||
#define MG_ENABLE_CUSTOM_MILLIS 0
|
#define MG_ENABLE_CUSTOM_MILLIS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_CUSTOM_MICROS
|
||||||
|
#define MG_ENABLE_CUSTOM_MICROS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_ASSERT
|
#ifndef MG_ENABLE_ASSERT
|
||||||
#define MG_ENABLE_ASSERT 0
|
#define MG_ENABLE_ASSERT 0
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -599,8 +599,6 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
mg_http_reply(c, 200, MG_JSON_HEADERS, "hi\n");
|
mg_http_reply(c, 200, MG_JSON_HEADERS, "hi\n");
|
||||||
} else if (mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
} else if (mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
||||||
mg_handle_logout(c);
|
mg_handle_logout(c);
|
||||||
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%m:%m}", MG_ESC("method"),
|
|
||||||
MG_ESC("logout"));
|
|
||||||
} else if (mg_match(hm->uri, mg_str("/api/websocket"), NULL)) {
|
} else if (mg_match(hm->uri, mg_str("/api/websocket"), NULL)) {
|
||||||
d->u = u;
|
d->u = u;
|
||||||
mg_ws_upgrade(c, hm, NULL);
|
mg_ws_upgrade(c, hm, NULL);
|
||||||
|
|||||||
@@ -136,12 +136,13 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
|
|||||||
dm->txnid = mg_ntohs(h->txnid);
|
dm->txnid = mg_ntohs(h->txnid);
|
||||||
is_response = mg_ntohs(h->flags) & 0x8000;
|
is_response = mg_ntohs(h->flags) & 0x8000;
|
||||||
|
|
||||||
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
|
for (i = 0; i < mg_ntohs(h->num_questions); i++) { // advance past queries
|
||||||
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
||||||
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
|
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
|
||||||
|
dm->addr.is_ip6 = (rr.atype == MG_DNS_RTYPE_AAAA);
|
||||||
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
||||||
ofs += n;
|
ofs += n;
|
||||||
}
|
} // on unresolved responses, we got the name and query type, at least
|
||||||
|
|
||||||
if (!is_response) {
|
if (!is_response) {
|
||||||
// For queries, there is no need to parse the answers. In this way,
|
// For queries, there is no need to parse the answers. In this way,
|
||||||
|
|||||||
@@ -0,0 +1,189 @@
|
|||||||
|
#include "l2_mesh.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "net_builtin.h"
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32_802154) && \
|
||||||
|
MG_ENABLE_DRIVER_ESP32_802154
|
||||||
|
|
||||||
|
#if MG_ARCH != MG_ARCH_ESP32
|
||||||
|
#error "MG_ENABLE_DRIVER_ESP32_802154 needs MG_ARCH_ESP32 (ESP-IDF)"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ESP-IDF owns the PHY, coexistence and interrupts. Its callbacks run in
|
||||||
|
// interrupt context and pass radio frames through a lock-free queue, read by
|
||||||
|
// mgi_esp32_802154_poll(). In both directions frame[0] is the PHR, the length
|
||||||
|
// including FCS
|
||||||
|
#include <esp_attr.h>
|
||||||
|
#include <esp_ieee802154.h>
|
||||||
|
#include <esp_timer.h>
|
||||||
|
|
||||||
|
#define MG_ESP32_802154_TX_TIMEOUT_US 60000
|
||||||
|
|
||||||
|
static struct mg_tcpip_if *s_esp32_802154_ifp;
|
||||||
|
static struct mg_queue s_esp32_802154_rx;
|
||||||
|
static char s_esp32_802154_rxbuf[2048];
|
||||||
|
static volatile bool s_esp32_802154_txing, s_esp32_802154_tx_done;
|
||||||
|
static uint64_t s_esp32_802154_txstart; // Send start, us
|
||||||
|
// Frame for the radio: PHR, the length including FCS, then the PSDU
|
||||||
|
static uint8_t s_esp32_802154_txframe[128] __attribute__((aligned(4)));
|
||||||
|
|
||||||
|
// Queue a received PSDU without its FCS
|
||||||
|
static void mg_esp32_802154_rx_frame(const uint8_t *psdu, size_t len) {
|
||||||
|
mg_mesh_rx_queue(&s_esp32_802154_rx, s_esp32_802154_ifp, psdu, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A transmission refused by Wi-Fi coexistence, a busy channel or an abort is
|
||||||
|
// sent again after this pause, until MG_ESP32_802154_TX_TIMEOUT_US. Without
|
||||||
|
// it, the mesh would count the refused frame as sent
|
||||||
|
#define MG_ESP32_802154_RETRY_US 200
|
||||||
|
|
||||||
|
static volatile bool s_esp32_802154_retry; // A resend is pending
|
||||||
|
static volatile uint32_t s_esp32_802154_retry_at; // When to resend, us
|
||||||
|
|
||||||
|
void IRAM_ATTR esp_ieee802154_receive_done(uint8_t *frame,
|
||||||
|
esp_ieee802154_frame_info_t *info) {
|
||||||
|
(void) info;
|
||||||
|
if (frame[0] > 2) mg_esp32_802154_rx_frame(frame + 1, frame[0] - 2U);
|
||||||
|
esp_ieee802154_receive_handle_done(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRAM_ATTR esp_ieee802154_transmit_done(const uint8_t *frame,
|
||||||
|
const uint8_t *ack,
|
||||||
|
esp_ieee802154_frame_info_t *info) {
|
||||||
|
(void) frame, (void) info;
|
||||||
|
if (ack != NULL) esp_ieee802154_receive_handle_done(ack);
|
||||||
|
s_esp32_802154_tx_done = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void IRAM_ATTR esp_ieee802154_transmit_failed(const uint8_t *frame,
|
||||||
|
esp_ieee802154_tx_error_t err) {
|
||||||
|
(void) frame;
|
||||||
|
if (err == ESP_IEEE802154_TX_ERR_COEXIST ||
|
||||||
|
err == ESP_IEEE802154_TX_ERR_CCA_BUSY ||
|
||||||
|
err == ESP_IEEE802154_TX_ERR_ABORT) {
|
||||||
|
s_esp32_802154_retry_at =
|
||||||
|
(uint32_t) esp_timer_get_time() + MG_ESP32_802154_RETRY_US;
|
||||||
|
s_esp32_802154_retry = true; // Set last: the poll reads it first
|
||||||
|
} else {
|
||||||
|
s_esp32_802154_tx_done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mg_esp32_802154_hw_init(uint16_t address) {
|
||||||
|
// rx_when_idle: the driver returns to receive after each transmission
|
||||||
|
return esp_ieee802154_enable() == ESP_OK &&
|
||||||
|
esp_ieee802154_set_promiscuous(false) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_coordinator(false) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_rx_when_idle(true) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_panid(MG_802154_PAN_ID) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_short_address(address) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_channel(MG_802154_CHANNEL) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_txpower(0) == ESP_OK &&
|
||||||
|
esp_ieee802154_receive() == ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mg_esp32_802154_hw_tx(void) {
|
||||||
|
s_esp32_802154_retry = false;
|
||||||
|
return esp_ieee802154_transmit(s_esp32_802154_txframe, false) == ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mg_esp32_802154_hw_abort(void) {
|
||||||
|
s_esp32_802154_retry = false;
|
||||||
|
esp_ieee802154_receive(); // Stops the current operation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resend a refused frame when its pause is over. If the radio does not take
|
||||||
|
// it, give up: the frame counts as done
|
||||||
|
static void mg_esp32_802154_hw_poll(void) {
|
||||||
|
uint32_t now;
|
||||||
|
if (!s_esp32_802154_retry) return;
|
||||||
|
now = (uint32_t) esp_timer_get_time();
|
||||||
|
if ((int32_t) (now - s_esp32_802154_retry_at) < 0) return;
|
||||||
|
s_esp32_802154_retry = false;
|
||||||
|
if (esp_ieee802154_transmit(s_esp32_802154_txframe, false) != ESP_OK) {
|
||||||
|
s_esp32_802154_tx_done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mesh radio transmit. The hardware adds the FCS
|
||||||
|
static size_t mgi_esp32_802154_radio_tx(struct mg_tcpip_if *ifp,
|
||||||
|
const void *buf, size_t len) {
|
||||||
|
(void) ifp;
|
||||||
|
if (len > 125 || s_esp32_802154_txing) return 0;
|
||||||
|
s_esp32_802154_txframe[0] = (uint8_t) (len + 2);
|
||||||
|
memcpy(s_esp32_802154_txframe + 1, buf, len);
|
||||||
|
s_esp32_802154_tx_done = false;
|
||||||
|
s_esp32_802154_txing = true, s_esp32_802154_txstart = mg_micros();
|
||||||
|
if (!mg_esp32_802154_hw_tx()) {
|
||||||
|
s_esp32_802154_txing = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t mgi_esp32_802154_tx(const void *buf, size_t len,
|
||||||
|
struct mg_tcpip_if *ifp) {
|
||||||
|
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
|
||||||
|
return data->mesh->strategy->tx(data->mesh, buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t mgi_esp32_802154_rx(void *buf, size_t cap,
|
||||||
|
struct mg_tcpip_if *ifp) {
|
||||||
|
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
|
||||||
|
size_t len = data->mesh->rx_len;
|
||||||
|
if (len > cap) return 0;
|
||||||
|
if (len != 0 && buf != ifp->recv_queue.buf)
|
||||||
|
memcpy(buf, ifp->recv_queue.buf, len);
|
||||||
|
data->mesh->rx_len = 0;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mgi_esp32_802154_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||||
|
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
|
||||||
|
struct mg_mesh_node *node = data->mesh;
|
||||||
|
uint64_t now = mg_micros();
|
||||||
|
(void) s1;
|
||||||
|
node->now = now;
|
||||||
|
mg_esp32_802154_hw_poll();
|
||||||
|
if (s_esp32_802154_txing &&
|
||||||
|
(s_esp32_802154_tx_done ||
|
||||||
|
now - s_esp32_802154_txstart > MG_ESP32_802154_TX_TIMEOUT_US)) {
|
||||||
|
if (!s_esp32_802154_tx_done) mg_esp32_802154_hw_abort();
|
||||||
|
s_esp32_802154_txing = false;
|
||||||
|
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||||
|
}
|
||||||
|
mg_mesh_rx_drain(node, &s_esp32_802154_rx);
|
||||||
|
node->strategy->tick(node, now);
|
||||||
|
return node->joined;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mgi_esp32_802154_init(struct mg_tcpip_if *ifp) {
|
||||||
|
struct mg_tcpip_driver_esp32_802154_data *data = ifp->driver_data;
|
||||||
|
const struct mg_mesh *strategy = data->strategy;
|
||||||
|
uint16_t address =
|
||||||
|
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
||||||
|
if (strategy == NULL) strategy = &mg_mesh_default;
|
||||||
|
data->mesh = mg_calloc(1, sizeof(*data->mesh));
|
||||||
|
if (data->mesh == NULL) return false;
|
||||||
|
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
||||||
|
mg_free(data->mesh);
|
||||||
|
data->mesh = NULL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
data->mesh->strategy = strategy;
|
||||||
|
data->mesh->tx = mgi_esp32_802154_radio_tx;
|
||||||
|
mg_queue_init(&s_esp32_802154_rx, s_esp32_802154_rxbuf,
|
||||||
|
sizeof(s_esp32_802154_rxbuf));
|
||||||
|
s_esp32_802154_ifp = ifp;
|
||||||
|
if (mg_esp32_802154_hw_init(address)) return true;
|
||||||
|
MG_ERROR(("802.15.4 radio init failed"));
|
||||||
|
data->mesh->strategy->free(data->mesh);
|
||||||
|
mg_free(data->mesh);
|
||||||
|
data->mesh = NULL;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mg_tcpip_driver mg_tcpip_driver_esp32_802154 = {
|
||||||
|
mgi_esp32_802154_init, mgi_esp32_802154_tx, mgi_esp32_802154_rx,
|
||||||
|
mgi_esp32_802154_poll};
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
// Copyright (c) 2026 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32_802154) && \
|
||||||
|
MG_ENABLE_DRIVER_ESP32_802154
|
||||||
|
|
||||||
|
// ESP32-C6 IEEE 802.15.4 radio, as an Ethernet interface through the mesh.
|
||||||
|
// Needs MG_ARCH_ESP32 and CONFIG_IEEE802154_ENABLED: it uses ESP-IDF's
|
||||||
|
// esp_ieee802154 driver, and defines its esp_ieee802154_receive_done(),
|
||||||
|
// esp_ieee802154_transmit_done() and esp_ieee802154_transmit_failed()
|
||||||
|
// callbacks. See tutorials/esp32/minimal-802.15.4
|
||||||
|
struct mg_tcpip_driver_esp32_802154_data {
|
||||||
|
struct mg_mesh_node *mesh; // Driver-owned runtime state
|
||||||
|
const struct mg_mesh *strategy; // NULL selects mg_mesh_default
|
||||||
|
};
|
||||||
|
|
||||||
|
extern struct mg_tcpip_driver mg_tcpip_driver_esp32_802154;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_ESP32_802154_INIT(manager) \
|
||||||
|
do { \
|
||||||
|
static struct mg_tcpip_driver_esp32_802154_data driver_data_; \
|
||||||
|
static struct mg_tcpip_if mif_; \
|
||||||
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
|
mif_.ip = MG_TCPIP_IP; \
|
||||||
|
mif_.mask = MG_TCPIP_MASK; \
|
||||||
|
mif_.gw = MG_TCPIP_GW; \
|
||||||
|
mif_.mgr = (manager); \
|
||||||
|
mif_.driver = &mg_tcpip_driver_esp32_802154; \
|
||||||
|
mif_.driver_data = &driver_data_; \
|
||||||
|
mg_tcpip_init((manager), &mif_); \
|
||||||
|
MG_INFO(("Driver: esp32_802154, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ESP32_802154_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
+5
-18
@@ -110,15 +110,7 @@ void RF_802_15_4_IRQHandler(void) {
|
|||||||
n = n >= 2 ? n - 2 : 0;
|
n = n >= 2 ? n - 2 : 0;
|
||||||
if (n > 0 && n <= sizeof(frame)) {
|
if (n > 0 && n <= sizeof(frame)) {
|
||||||
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
|
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
|
||||||
{
|
mg_mesh_rx_queue(&s_mcxw71_rx, s_mcxw71_ifp, frame, n);
|
||||||
char *dst;
|
|
||||||
if (mg_queue_book(&s_mcxw71_rx, &dst, n) >= n) {
|
|
||||||
memcpy(dst, frame, n);
|
|
||||||
mg_queue_add(&s_mcxw71_rx, n);
|
|
||||||
} else {
|
|
||||||
s_mcxw71_ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mg_mcxw71_listen();
|
mg_mcxw71_listen();
|
||||||
@@ -179,8 +171,6 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
||||||
struct mg_mesh_node *node = data->mesh;
|
struct mg_mesh_node *node = data->mesh;
|
||||||
uint64_t now = mg_micros();
|
uint64_t now = mg_micros();
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
(void) s1;
|
||||||
if (s_mcxw71_txing &&
|
if (s_mcxw71_txing &&
|
||||||
mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) {
|
mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) {
|
||||||
@@ -199,10 +189,7 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||||
}
|
}
|
||||||
node->now = now;
|
node->now = now;
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_mcxw71_rx, &buf)) != 0) {
|
mg_mesh_rx_drain(node, &s_mcxw71_rx);
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_mcxw71_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
node->strategy->tick(node, now);
|
||||||
return node->joined;
|
return node->joined;
|
||||||
}
|
}
|
||||||
@@ -214,10 +201,10 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
|
|||||||
address =
|
address =
|
||||||
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
||||||
if (strategy == NULL) strategy = &mg_mesh_default;
|
if (strategy == NULL) strategy = &mg_mesh_default;
|
||||||
data->mesh = calloc(1, sizeof(*data->mesh));
|
data->mesh = mg_calloc(1, sizeof(*data->mesh));
|
||||||
if (data->mesh == NULL) return false;
|
if (data->mesh == NULL) return false;
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -226,7 +213,7 @@ static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
|
|||||||
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
|
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
|
||||||
if (mgi_mcxw71_radio_init(ifp)) return true;
|
if (mgi_mcxw71_radio_init(ifp)) return true;
|
||||||
strategy->free(data->mesh);
|
strategy->free(data->mesh);
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-16
@@ -23,7 +23,6 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
|
|||||||
RAIL_RxPacketInfo_t info;
|
RAIL_RxPacketInfo_t info;
|
||||||
uint8_t f[128];
|
uint8_t f[128];
|
||||||
size_t n;
|
size_t n;
|
||||||
char *dst;
|
|
||||||
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
|
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
|
||||||
RAIL_RX_PACKET_HANDLE_INVALID ||
|
RAIL_RX_PACKET_HANDLE_INVALID ||
|
||||||
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
|
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
|
||||||
@@ -33,12 +32,7 @@ static void mg_mg24_rx_frame(RAIL_Handle_t h) {
|
|||||||
RAIL_CopyRxPacket(f, &info);
|
RAIL_CopyRxPacket(f, &info);
|
||||||
n = info.packetBytes - 1U; // Strip PHR
|
n = info.packetBytes - 1U; // Strip PHR
|
||||||
if (n == f[0]) n -= 2; // FCS stored, strip it
|
if (n == f[0]) n -= 2; // FCS stored, strip it
|
||||||
if (mg_queue_book(&s_mg24_rx, &dst, n) >= n) {
|
mg_mesh_rx_queue(&s_mg24_rx, s_mg24_ifp, f + 1, n);
|
||||||
memcpy(dst, f + 1, n);
|
|
||||||
mg_queue_add(&s_mg24_rx, n);
|
|
||||||
} else {
|
|
||||||
s_mg24_ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RAIL event callback, runs in interrupt context
|
// RAIL event callback, runs in interrupt context
|
||||||
@@ -136,8 +130,6 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
||||||
struct mg_mesh_node *node = data->mesh;
|
struct mg_mesh_node *node = data->mesh;
|
||||||
uint64_t now = mg_micros();
|
uint64_t now = mg_micros();
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
(void) s1;
|
||||||
node->now = now;
|
node->now = now;
|
||||||
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
|
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
|
||||||
@@ -149,10 +141,7 @@ static bool mgi_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
s_mg24_txing = false;
|
s_mg24_txing = false;
|
||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||||
}
|
}
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_mg24_rx, &buf)) != 0) {
|
mg_mesh_rx_drain(node, &s_mg24_rx);
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_mg24_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
node->strategy->tick(node, now);
|
||||||
return node->joined;
|
return node->joined;
|
||||||
}
|
}
|
||||||
@@ -162,10 +151,10 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
|
|||||||
uint16_t address =
|
uint16_t address =
|
||||||
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
||||||
if (strategy == NULL) strategy = &mg_mesh_default;
|
if (strategy == NULL) strategy = &mg_mesh_default;
|
||||||
data->mesh = calloc(1, sizeof(*data->mesh));
|
data->mesh = mg_calloc(1, sizeof(*data->mesh));
|
||||||
if (data->mesh == NULL) return false;
|
if (data->mesh == NULL) return false;
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -174,7 +163,7 @@ static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
|
|||||||
mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf));
|
mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf));
|
||||||
if (mgi_mg24_radio_init(ifp)) return true;
|
if (mgi_mg24_radio_init(ifp)) return true;
|
||||||
data->mesh->strategy->free(data->mesh);
|
data->mesh->strategy->free(data->mesh);
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-16
@@ -45,13 +45,7 @@ void RADIO_IRQHandler(void) {
|
|||||||
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
|
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
|
||||||
0 &&
|
0 &&
|
||||||
(dst == 0xffffu || dst == node->radio_address)) {
|
(dst == 0xffffu || dst == node->radio_address)) {
|
||||||
char *dst;
|
mg_mesh_rx_queue(&s_nrf52_rx, s_nrf52_ifp, frame, len);
|
||||||
if (mg_queue_book(&s_nrf52_rx, &dst, len) >= len) {
|
|
||||||
memcpy(dst, frame, len);
|
|
||||||
mg_queue_add(&s_nrf52_rx, len);
|
|
||||||
} else {
|
|
||||||
s_nrf52_ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mgi_nrf52_listen();
|
mgi_nrf52_listen();
|
||||||
@@ -126,8 +120,6 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
||||||
struct mg_mesh_node *node = data->mesh;
|
struct mg_mesh_node *node = data->mesh;
|
||||||
uint64_t now = mg_micros();
|
uint64_t now = mg_micros();
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
(void) s1;
|
||||||
if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) {
|
if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) {
|
||||||
NVIC_DisableIRQ(RADIO_IRQn);
|
NVIC_DisableIRQ(RADIO_IRQn);
|
||||||
@@ -142,10 +134,7 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||||
}
|
}
|
||||||
node->now = now;
|
node->now = now;
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_nrf52_rx, &buf)) != 0) {
|
mg_mesh_rx_drain(node, &s_nrf52_rx);
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_nrf52_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
node->strategy->tick(node, now);
|
||||||
return node->joined;
|
return node->joined;
|
||||||
}
|
}
|
||||||
@@ -157,10 +146,10 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
|
|||||||
address =
|
address =
|
||||||
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
||||||
if (strategy == NULL) strategy = &mg_mesh_default;
|
if (strategy == NULL) strategy = &mg_mesh_default;
|
||||||
data->mesh = calloc(1, sizeof(*data->mesh));
|
data->mesh = mg_calloc(1, sizeof(*data->mesh));
|
||||||
if (data->mesh == NULL) return false;
|
if (data->mesh == NULL) return false;
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -169,7 +158,7 @@ static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
|
|||||||
mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue));
|
mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue));
|
||||||
if (mgi_nrf52_radio_init(ifp)) return true;
|
if (mgi_nrf52_radio_init(ifp)) return true;
|
||||||
strategy->free(data->mesh);
|
strategy->free(data->mesh);
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-18
@@ -138,15 +138,7 @@ static void mg_rw612_control(uint8_t subtype, uint32_t a, uint32_t b,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
|
static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
|
||||||
{
|
mg_mesh_rx_queue(&s_rw612w_rx, s_rw612.ifp, frame, len);
|
||||||
char *dst;
|
|
||||||
if (mg_queue_book(&s_rw612w_rx, &dst, len) >= len) {
|
|
||||||
memcpy(dst, frame, len);
|
|
||||||
mg_queue_add(&s_rw612w_rx, len);
|
|
||||||
} else {
|
|
||||||
s_rw612.ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spinel packed unsigned integer: 7 bits per byte, LSB first
|
// Spinel packed unsigned integer: 7 bits per byte, LSB first
|
||||||
@@ -457,8 +449,6 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
||||||
struct mg_mesh_node *node = data->mesh;
|
struct mg_mesh_node *node = data->mesh;
|
||||||
uint64_t now = mg_micros();
|
uint64_t now = mg_micros();
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
(void) s1;
|
||||||
bool was_sending = s_rw612.txing;
|
bool was_sending = s_rw612.txing;
|
||||||
mg_rw612_poll_imu();
|
mg_rw612_poll_imu();
|
||||||
@@ -470,10 +460,7 @@ static bool mgi_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
if (was_sending && !s_rw612.txing)
|
if (was_sending && !s_rw612.txing)
|
||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||||
node->now = now;
|
node->now = now;
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_rw612w_rx, &buf)) != 0) {
|
mg_mesh_rx_drain(node, &s_rw612w_rx);
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_rw612w_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
node->strategy->tick(node, now);
|
||||||
return node->joined;
|
return node->joined;
|
||||||
}
|
}
|
||||||
@@ -491,10 +478,10 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
|
|||||||
address =
|
address =
|
||||||
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
||||||
if (strategy == NULL) strategy = &mg_mesh_default;
|
if (strategy == NULL) strategy = &mg_mesh_default;
|
||||||
data->mesh = calloc(1, sizeof(*data->mesh));
|
data->mesh = mg_calloc(1, sizeof(*data->mesh));
|
||||||
if (data->mesh == NULL) return false;
|
if (data->mesh == NULL) return false;
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -503,7 +490,7 @@ static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
|
|||||||
mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue));
|
mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue));
|
||||||
if (mgi_rw612w_radio_init(ifp)) return true;
|
if (mgi_rw612w_radio_init(ifp)) return true;
|
||||||
strategy->free(data->mesh);
|
strategy->free(data->mesh);
|
||||||
free(data->mesh);
|
mg_free(data->mesh);
|
||||||
data->mesh = NULL;
|
data->mesh = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-4
@@ -752,11 +752,15 @@ static bool uri2path(const char *r, struct mg_str uri, char *buf, size_t len) {
|
|||||||
n = mg_snprintf(buf, len, "%.*s", (int) d.len, d.buf);
|
n = mg_snprintf(buf, len, "%.*s", (int) d.len, d.buf);
|
||||||
if (len == 0 || n + 2 >= len) return false; // Path overflow
|
if (len == 0 || n + 2 >= len) return false; // Path overflow
|
||||||
if (n > 0 && buf[n - 1] != '/') buf[n++] = '/', buf[n] = '\0'; // Add slash
|
if (n > 0 && buf[n - 1] != '/') buf[n++] = '/', buf[n] = '\0'; // Add slash
|
||||||
if (mg_url_decode(uri.buf + u.len, uri.len - u.len, buf + n, len - n, 0) < 0) {
|
{
|
||||||
return false;
|
int dn;
|
||||||
|
size_t p = n;
|
||||||
|
dn = mg_url_decode(uri.buf + u.len, uri.len - u.len, buf + n, len - n, 0);
|
||||||
|
if (dn < 0) return false; // reject on malformed
|
||||||
|
buf[len - 1] = '\0'; // Double-check
|
||||||
|
n = strlen(buf);
|
||||||
|
if (n != p + (size_t) dn) return false; // reject on embedded NULs
|
||||||
}
|
}
|
||||||
buf[len - 1] = '\0'; // Double-check
|
|
||||||
n = strlen(buf);
|
|
||||||
if (!mg_path_is_sane(mg_str_n(buf, n))) return false;
|
if (!mg_path_is_sane(mg_str_n(buf, n))) return false;
|
||||||
while (n > 1 && buf[n - 1] == '/') buf[--n] = 0; // Trim trailing slashes
|
while (n > 1 && buf[n - 1] == '/') buf[--n] = 0; // Trim trailing slashes
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+71
-10
@@ -108,6 +108,8 @@ static size_t mgi_mesh_b_queue(struct mg_mesh_node *node, struct mg_queue *q,
|
|||||||
#define MGI_JOIN_JITTER_US 640
|
#define MGI_JOIN_JITTER_US 640
|
||||||
#define MGI_DATA_QUEUE_SIZE 2048
|
#define MGI_DATA_QUEUE_SIZE 2048
|
||||||
#define MGI_TX_RETRY_US 32
|
#define MGI_TX_RETRY_US 32
|
||||||
|
#define MGI_ANSWER_MARGIN_US 500 // Node: finish the first answer frame this
|
||||||
|
// much before the bridge stops waiting
|
||||||
// Our frames: the radio header, with the radio addresses, then the mesh
|
// Our frames: the radio header, with the radio addresses, then the mesh
|
||||||
// header MG_MESH_HDR_LEN, then the payload
|
// header MG_MESH_HDR_LEN, then the payload
|
||||||
#define MGI_JOIN_UP_LEN \
|
#define MGI_JOIN_UP_LEN \
|
||||||
@@ -134,6 +136,7 @@ struct mgi_mesh_b_data {
|
|||||||
int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed
|
int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed
|
||||||
uint16_t coordinator; // Coordinator radio address
|
uint16_t coordinator; // Coordinator radio address
|
||||||
uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA
|
uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA
|
||||||
|
uint64_t answer_by; // Node: first answer frame must end on air by then
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mgi_mesh_b_peer {
|
struct mgi_mesh_b_peer {
|
||||||
@@ -246,11 +249,24 @@ static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
|
|||||||
} else {
|
} else {
|
||||||
mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0);
|
mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0);
|
||||||
}
|
}
|
||||||
|
// A node's first answer frame: if it cannot end on air before the bridge
|
||||||
|
// stops waiting, stay silent. The queued frame waits for the next turn
|
||||||
|
if (node->lan == NULL &&
|
||||||
|
(data->tx_offset == 0 || data->tx_offset == MGI_TX_DST_LEN) &&
|
||||||
|
now + mgi_mesh_b_airtime(len) > data->answer_by) {
|
||||||
|
data->tx_offset = 0;
|
||||||
|
data->tx_at = (uint64_t) -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) {
|
if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) {
|
||||||
data->tx_at = now + MGI_TX_RETRY_US;
|
data->tx_at = now + MGI_TX_RETRY_US;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) {
|
if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) {
|
||||||
|
// Bridge: the turn's last frame is out, wait for the answer
|
||||||
|
if (node->lan != NULL)
|
||||||
|
data->deadline =
|
||||||
|
now + mgi_mesh_b_airtime(len) + MG_MESH_ANSWER_TIMEOUT_US;
|
||||||
mgi_mesh_b_tx_finish(data);
|
mgi_mesh_b_tx_finish(data);
|
||||||
} else {
|
} else {
|
||||||
data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us;
|
data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us;
|
||||||
@@ -260,6 +276,7 @@ static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
|
|||||||
static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst,
|
static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst,
|
||||||
uint64_t now, uint64_t deadline) {
|
uint64_t now, uint64_t deadline) {
|
||||||
data->phase = MGI_TURN_DATA;
|
data->phase = MGI_TURN_DATA;
|
||||||
|
data->answer_by = (uint64_t) -1; // Node sets it after a grant
|
||||||
data->tx_offset = burst ? MGI_TX_DST_LEN : 0;
|
data->tx_offset = burst ? MGI_TX_DST_LEN : 0;
|
||||||
data->tx_at = now;
|
data->tx_at = now;
|
||||||
data->deadline = deadline;
|
data->deadline = deadline;
|
||||||
@@ -290,6 +307,11 @@ static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
|
|||||||
memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk);
|
memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk);
|
||||||
if (index < last) {
|
if (index < last) {
|
||||||
data->rx_expected = (uint8_t) (burst + 0x10);
|
data->rx_expected = (uint8_t) (burst + 0x10);
|
||||||
|
// Bridge: an answer is under way, wait for its next fragment
|
||||||
|
if (data->node->lan != NULL)
|
||||||
|
data->deadline =
|
||||||
|
now + mgi_mesh_b_airtime(s_mesh_default_radio->max_frame_size) +
|
||||||
|
s_mesh_default_radio->ifs_us + MG_MESH_ANSWER_TIMEOUT_US;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
data->rx_expected = 0xff;
|
data->rx_expected = 0xff;
|
||||||
@@ -299,7 +321,6 @@ static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
|
|||||||
|
|
||||||
// Bridge scheduling and Ethernet forwarding.
|
// Bridge scheduling and Ethernet forwarding.
|
||||||
|
|
||||||
#define MGI_MAX_MISSES 3 // Turns in a row without an answer: remove the member
|
|
||||||
|
|
||||||
static uint16_t mgi_mesh_b_join_field(const uint8_t *p) {
|
static uint16_t mgi_mesh_b_join_field(const uint8_t *p) {
|
||||||
return (uint16_t) ((unsigned) p[0] * 256 + p[1]);
|
return (uint16_t) ((unsigned) p[0] * 256 + p[1]);
|
||||||
@@ -400,13 +421,13 @@ static void mgi_mesh_b_bridge_rx(uint64_t now, struct mg_mesh_node *b,
|
|||||||
memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac));
|
memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Member peer did not answer its turn. After MGI_MAX_MISSES in a row, it is
|
// Member peer did not answer its turn. After MG_MESH_MAX_MISSES in a row, it
|
||||||
// gone: remove it, and its queued downlink frames
|
// is gone: remove it, and its queued downlink frames
|
||||||
static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data,
|
static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data,
|
||||||
struct mgi_mesh_b_peer *peer) {
|
struct mgi_mesh_b_peer *peer) {
|
||||||
if (++peer->misses < MGI_MAX_MISSES) return;
|
if (++peer->misses < MG_MESH_MAX_MISSES) return;
|
||||||
MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac,
|
MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac,
|
||||||
MGI_MAX_MISSES));
|
MG_MESH_MAX_MISSES));
|
||||||
if (data->fanout == peer) {
|
if (data->fanout == peer) {
|
||||||
data->fanout = peer->next;
|
data->fanout = peer->next;
|
||||||
if (data->fanout == NULL) {
|
if (data->fanout == NULL) {
|
||||||
@@ -499,7 +520,7 @@ static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
|
|||||||
if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN &&
|
if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN &&
|
||||||
dst == 0xffffu) {
|
dst == 0xffffu) {
|
||||||
if (n->joined && src == data->coordinator &&
|
if (n->joined && src == data->coordinator &&
|
||||||
++data->missed_rounds >= MGI_MAX_MISSES) {
|
++data->missed_rounds >= MG_MESH_MAX_MISSES) {
|
||||||
n->joined = false;
|
n->joined = false;
|
||||||
data->phase = MGI_TURN_IDLE;
|
data->phase = MGI_TURN_IDLE;
|
||||||
data->tx_at = (uint64_t) -1;
|
data->tx_at = (uint64_t) -1;
|
||||||
@@ -534,17 +555,22 @@ static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
|
|||||||
now + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
|
now + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
|
||||||
2 * s_mesh_default_radio->ifs_us +
|
2 * s_mesh_default_radio->ifs_us +
|
||||||
MGI_JOIN_TURN_US);
|
MGI_JOIN_TURN_US);
|
||||||
|
data->answer_by = now + MG_MESH_ANSWER_TIMEOUT_US - MGI_ANSWER_MARGIN_US;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Receive timing counts from the frame's arrival, see mg_mesh_rx_drain().
|
||||||
|
// Without an arrival time, e.g. when called directly, use the poll time
|
||||||
static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) {
|
static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) {
|
||||||
uint64_t ready = node->now + node->strategy->radio->turnaround_us;
|
uint64_t ready = node->now + node->strategy->radio->turnaround_us;
|
||||||
|
uint64_t arrived = node->rx_at != 0 ? node->rx_at : node->now;
|
||||||
if (node->tx_at < ready) node->tx_at = ready;
|
if (node->tx_at < ready) node->tx_at = ready;
|
||||||
node->next_tick = node->now;
|
node->next_tick = node->now;
|
||||||
if (node->lan != NULL)
|
if (node->lan != NULL) {
|
||||||
mgi_mesh_b_bridge_rx(node->now, node, (const uint8_t *) buf, len);
|
mgi_mesh_b_bridge_rx(arrived, node, (const uint8_t *) buf, len);
|
||||||
else
|
} else {
|
||||||
mgi_mesh_b_node_rx(node, node->now, (uint8_t *) buf, len);
|
mgi_mesh_b_node_rx(node, arrived, (uint8_t *) buf, len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) {
|
static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) {
|
||||||
@@ -601,6 +627,41 @@ static void mgi_mesh_b_free(struct mg_mesh_node *node) {
|
|||||||
node->data = NULL;
|
node->data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Receive queue record: arrival time, then the radio frame
|
||||||
|
#define MGI_RX_STAMP_LEN sizeof(uint64_t)
|
||||||
|
|
||||||
|
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
|
||||||
|
const void *frame, size_t len) {
|
||||||
|
uint64_t arrived = mg_micros();
|
||||||
|
size_t need = MGI_RX_STAMP_LEN + len;
|
||||||
|
char *dst;
|
||||||
|
|
||||||
|
if (mg_queue_book(q, &dst, need) < need) {
|
||||||
|
if (ifp != NULL) ifp->ndrop++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memcpy(dst, &arrived, MGI_RX_STAMP_LEN);
|
||||||
|
memcpy(dst + MGI_RX_STAMP_LEN, frame, len);
|
||||||
|
mg_queue_add(q, need);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q) {
|
||||||
|
char *record;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
while (node->rx_len == 0) {
|
||||||
|
len = mg_queue_next(q, &record);
|
||||||
|
if (len == 0) break;
|
||||||
|
if (len > MGI_RX_STAMP_LEN) {
|
||||||
|
memcpy(&node->rx_at, record, MGI_RX_STAMP_LEN);
|
||||||
|
node->strategy->rx(node, record + MGI_RX_STAMP_LEN,
|
||||||
|
len - MGI_RX_STAMP_LEN);
|
||||||
|
node->rx_at = 0;
|
||||||
|
}
|
||||||
|
mg_queue_del(q, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const struct mg_mesh mg_mesh_default = {
|
const struct mg_mesh mg_mesh_default = {
|
||||||
"default",
|
"default",
|
||||||
mgi_mesh_b_init,
|
mgi_mesh_b_init,
|
||||||
|
|||||||
+28
-3
@@ -12,9 +12,6 @@
|
|||||||
|
|
||||||
#if MG_ENABLE_TCPIP
|
#if MG_ENABLE_TCPIP
|
||||||
|
|
||||||
// Monotonic time in microseconds.
|
|
||||||
uint64_t mg_micros(void);
|
|
||||||
|
|
||||||
// A radio standard
|
// A radio standard
|
||||||
struct mg_radio {
|
struct mg_radio {
|
||||||
const char *name; // For logs
|
const char *name; // For logs
|
||||||
@@ -44,6 +41,21 @@ struct mg_radio {
|
|||||||
#define MG_802154_CHANNEL 15
|
#define MG_802154_CHANNEL 15
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// A turn's answer must arrive this soon after the turn's last frame ends on
|
||||||
|
// air, or the turn ends. A node that would answer later stays silent and keeps
|
||||||
|
// its frame for the next turn. Covers the node's poll latency, the radio
|
||||||
|
// turnaround and one full fragment's airtime, 4.3 ms on 802.15.4
|
||||||
|
#ifndef MG_MESH_ANSWER_TIMEOUT_US
|
||||||
|
#define MG_MESH_ANSWER_TIMEOUT_US 8000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Misses in a row before giving up, 1 to 127. The bridge removes a member
|
||||||
|
// that did not answer this many turns. A node leaves the mesh after this
|
||||||
|
// many rounds without a turn
|
||||||
|
#ifndef MG_MESH_MAX_MISSES
|
||||||
|
#define MG_MESH_MAX_MISSES 10
|
||||||
|
#endif
|
||||||
|
|
||||||
extern const struct mg_radio mg_radio_802154;
|
extern const struct mg_radio mg_radio_802154;
|
||||||
|
|
||||||
struct mg_mesh_node {
|
struct mg_mesh_node {
|
||||||
@@ -53,6 +65,7 @@ struct mg_mesh_node {
|
|||||||
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
|
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
|
||||||
size_t rx_len; // Complete Ethernet frame in the interface RX buffer
|
size_t rx_len; // Complete Ethernet frame in the interface RX buffer
|
||||||
uint64_t now; // Current driver poll time, us
|
uint64_t now; // Current driver poll time, us
|
||||||
|
uint64_t rx_at; // Arrival time of the radio frame being handled, us
|
||||||
uint64_t tx_at; // Earliest next transmission, us
|
uint64_t tx_at; // Earliest next transmission, us
|
||||||
void *data; // Private state owned by the strategy
|
void *data; // Private state owned by the strategy
|
||||||
size_t ram_used; // Live mesh state bytes
|
size_t ram_used; // Live mesh state bytes
|
||||||
@@ -77,4 +90,16 @@ struct mg_mesh {
|
|||||||
|
|
||||||
extern const struct mg_mesh mg_mesh_default;
|
extern const struct mg_mesh mg_mesh_default;
|
||||||
|
|
||||||
|
// Put a received radio frame into the driver's receive queue, together with
|
||||||
|
// its arrival time from mg_micros(). Call it when the frame arrives, from the
|
||||||
|
// radio interrupt if the driver has one. If the queue is full, the frame is
|
||||||
|
// dropped and counted in ifp->ndrop
|
||||||
|
void mg_mesh_rx_queue(struct mg_queue *q, struct mg_tcpip_if *ifp,
|
||||||
|
const void *frame, size_t len);
|
||||||
|
|
||||||
|
// Pass queued radio frames to the mesh, oldest first. Before each frame,
|
||||||
|
// node->rx_at is set to its arrival time. Stops while the node holds a
|
||||||
|
// received Ethernet frame the TCP/IP stack has not taken yet
|
||||||
|
void mg_mesh_rx_drain(struct mg_mesh_node *node, struct mg_queue *q);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+29
-12
@@ -1224,22 +1224,34 @@ static void proto_error(struct mg_connection *c, bool r, const char *f, ...) {
|
|||||||
static bool rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
static bool rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||||
struct mg_connection *c = getpeer(ifp->mgr, pkt, true);
|
struct mg_connection *c = getpeer(ifp->mgr, pkt, true);
|
||||||
struct connstate *s;
|
struct connstate *s;
|
||||||
|
struct mg_addr rem;
|
||||||
uint8_t *l2addr;
|
uint8_t *l2addr;
|
||||||
if (c == NULL) return false; // No UDP listener on this port
|
if (c == NULL) return false; // No UDP listener on this port
|
||||||
|
// BSD socket has an unread datagram: drop this one to keep boundaries
|
||||||
|
if (c->is_bsd && c->recv.len > 0) return true;
|
||||||
|
if (c->is_bsd && !c->is_client && c->send.len > 0) return true;
|
||||||
s = (struct connstate *) (c + 1);
|
s = (struct connstate *) (c + 1);
|
||||||
c->rem.port = pkt->udp->sport;
|
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->loc.is_ip6) { // matching of v4/v6 to dest is done bt getpeer()
|
if (c->loc.is_ip6) { // matching of v4/v6 to dest is done bt getpeer()
|
||||||
if (!udp6csum_ok(pkt->ip6, pkt->udp)) return false;
|
if (!udp6csum_ok(pkt->ip6, pkt->udp)) return false;
|
||||||
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
if (c->is_bsd && c->is_client &&
|
||||||
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
(c->rem.port != pkt->udp->sport ||
|
||||||
|
!MG_IP6MATCH(c->rem.addr.ip6, pkt->ip6->src)))
|
||||||
|
return true;
|
||||||
|
rem.addr.ip6[0] = pkt->ip6->src[0],
|
||||||
|
rem.addr.ip6[1] = pkt->ip6->src[1], rem.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
if (!udpcsum_ok(pkt->ip, pkt->udp)) return false;
|
if (!udpcsum_ok(pkt->ip, pkt->udp)) return false;
|
||||||
c->rem.addr.ip4 = pkt->ip->src;
|
if (c->is_bsd && c->is_client &&
|
||||||
|
(c->rem.port != pkt->udp->sport || c->rem.addr.ip4 != pkt->ip->src))
|
||||||
|
return true;
|
||||||
|
rem.addr.ip4 = pkt->ip->src, rem.is_ip6 = false;
|
||||||
}
|
}
|
||||||
if ((l2addr = get_return_l2addr(ifp, &c->rem, true, pkt)) == NULL)
|
rem.port = pkt->udp->sport;
|
||||||
|
c->rem = rem;
|
||||||
|
if ((l2addr = get_return_l2addr(ifp, &rem, true, pkt)) == NULL)
|
||||||
return false; // safety net for lousy networks
|
return false; // safety net for lousy networks
|
||||||
memcpy(s->mac, l2addr, sizeof(s->mac));
|
memcpy(s->mac, l2addr, sizeof(s->mac));
|
||||||
if (c->recv.len >= MG_MAX_RECV_SIZE) {
|
if (c->recv.len >= MG_MAX_RECV_SIZE) {
|
||||||
@@ -2345,20 +2357,25 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
s->retransmit = ifp->enable_tcp_retransmit;
|
s->retransmit = ifp->enable_tcp_retransmit;
|
||||||
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
||||||
c->is_resolving = 0;
|
c->is_resolving = 0;
|
||||||
if (ifp->eport < MG_EPHEMERAL_PORT_BASE) ifp->eport = MG_EPHEMERAL_PORT_BASE;
|
if (c->loc.port == 0) {
|
||||||
c->loc.port = mg_htons(ifp->eport++);
|
if (ifp->eport < MG_EPHEMERAL_PORT_BASE)
|
||||||
|
ifp->eport = MG_EPHEMERAL_PORT_BASE;
|
||||||
|
c->loc.port = mg_htons(ifp->eport++);
|
||||||
|
} // otherwise retain an existing bound port
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->rem.is_ip6) {
|
if (c->rem.is_ip6) {
|
||||||
if (c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
|
||||||
|
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
} else { // use global address
|
} else if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
|
||||||
|
// Use global address
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
||||||
}
|
} // otherwise retain an existing bound address
|
||||||
c->loc.is_ip6 = true;
|
c->loc.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
c->loc.addr.ip4 = ifp->ip;
|
if (c->loc.addr.ip4 == 0) c->loc.addr.ip4 = ifp->ip;
|
||||||
}
|
}
|
||||||
MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port,
|
MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port,
|
||||||
&c->rem));
|
&c->rem));
|
||||||
@@ -2389,7 +2406,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
uint32_t rem_ip = c->rem.addr.ip4;
|
uint32_t rem_ip = c->rem.addr.ip4;
|
||||||
if (ifp->ip && MG_IPISLOCAL(ifp, rem_ip) &&
|
if (ifp->ip && MG_IPISLOCAL(ifp, rem_ip) &&
|
||||||
rem_ip != ifp->gw) { // skip if gw (onstatechange -> ARP)
|
rem_ip != ifp->gw) { // skip if gw (onstatechange -> ARP)
|
||||||
// If we're in the same LAN, fire an ARP lookup.
|
// We're in the same LAN, fire an ARP lookup.
|
||||||
MG_DEBUG(("%lu ARP lookup...", c->id));
|
MG_DEBUG(("%lu ARP lookup...", c->id));
|
||||||
mg_tcpip_arp_request(ifp, rem_ip, NULL);
|
mg_tcpip_arp_request(ifp, rem_ip, NULL);
|
||||||
settmout(c, MIP_TTYPE_ARP);
|
settmout(c, MIP_TTYPE_ARP);
|
||||||
|
|||||||
@@ -159,6 +159,7 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
|||||||
#include "drivers/mcxw71.h"
|
#include "drivers/mcxw71.h"
|
||||||
#include "drivers/nrf52.h"
|
#include "drivers/nrf52.h"
|
||||||
#include "drivers/rw612w.h"
|
#include "drivers/rw612w.h"
|
||||||
|
#include "drivers/esp32_802154.h"
|
||||||
|
|
||||||
// SPI bus abstraction for SPI-attached network chips (e.g. W5500).
|
// SPI bus abstraction for SPI-attached network chips (e.g. W5500).
|
||||||
// Populate and assign to the driver's driver_data field.
|
// Populate and assign to the driver's driver_data field.
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ void mg_rpc_process(struct mg_rpc_req *r) {
|
|||||||
mg_rpc_call(r, mg_str("")); // JSON response! call "" method handler
|
mg_rpc_call(r, mg_str("")); // JSON response! call "" method handler
|
||||||
} else {
|
} else {
|
||||||
mg_rpc_err(r, -32700, "%m", mg_print_esc, (int) r->frame.len,
|
mg_rpc_err(r, -32700, "%m", mg_print_esc, (int) r->frame.len,
|
||||||
r->frame.buf); // Invalid
|
r->frame.len ? r->frame.buf : ""); // Invalid
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+7
-5
@@ -4,11 +4,13 @@
|
|||||||
|
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
// Return milliseconds since boot (uptime). Never goes backwards.
|
// Return microseconds since boot. Never goes backwards.
|
||||||
// On RTOS targets (FreeRTOS, ThreadX, Zephyr, etc.) uses the RTOS tick count.
|
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
|
||||||
// On STM32/Cube uses HAL_GetTick(). On Arduino uses millis().
|
uint64_t mg_micros(void);
|
||||||
// On Linux/macOS uses CLOCK_MONOTONIC_RAW or CLOCK_MONOTONIC; falls back to
|
|
||||||
// CLOCK_REALTIME if neither is available.
|
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
|
||||||
|
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
|
||||||
|
// Deprecated: use mg_micros() instead.
|
||||||
uint64_t mg_millis(void);
|
uint64_t mg_millis(void);
|
||||||
|
|
||||||
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
|
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
|
||||||
|
|||||||
+2
-2
@@ -297,9 +297,9 @@ static long recv_raw(struct mg_connection *c, void *buf, size_t len) {
|
|||||||
long n = 0;
|
long n = 0;
|
||||||
if (c->is_udp) {
|
if (c->is_udp) {
|
||||||
union usa usa;
|
union usa usa;
|
||||||
socklen_t slen = tousa(&c->rem, &usa);
|
socklen_t slen = sizeof(usa);
|
||||||
n = recvfrom(FD(c), (char *) buf, len, 0, &usa.sa, &slen);
|
n = recvfrom(FD(c), (char *) buf, len, 0, &usa.sa, &slen);
|
||||||
if (n > 0) tomgaddr(&usa, &c->rem, slen != sizeof(usa.sin));
|
if (n > 0) tomgaddr(&usa, &c->rem, usa.sa.sa_family == AF_INET6);
|
||||||
} else {
|
} else {
|
||||||
n = recv(FD(c), (char *) buf, len, MSG_NONBLOCKING);
|
n = recv(FD(c), (char *) buf, len, MSG_NONBLOCKING);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,8 +88,7 @@ bool mg_match(struct mg_str s, struct mg_str p, struct mg_str *caps) {
|
|||||||
if (caps && !caps->buf) caps->len = 0, caps->buf = &s.buf[j]; // Init cap
|
if (caps && !caps->buf) caps->len = 0, caps->buf = &s.buf[j]; // Init cap
|
||||||
ni = i++, nj = j + 1;
|
ni = i++, nj = j + 1;
|
||||||
} else if (nj > 0 && nj <= s.len &&
|
} else if (nj > 0 && nj <= s.len &&
|
||||||
((ni < p.len && p.buf[ni] == '#') ||
|
((ni < p.len && p.buf[ni] == '#') || s.buf[nj - 1] != '/')) {
|
||||||
(j < s.len && s.buf[j] != '/'))) {
|
|
||||||
i = ni, j = nj;
|
i = ni, j = nj;
|
||||||
if (caps && caps->buf == NULL && caps->len == 0) {
|
if (caps && caps->buf == NULL && caps->len == 0) {
|
||||||
caps--, caps->len = 0; // Restart previous cap
|
caps--, caps->len = 0; // Restart previous cap
|
||||||
|
|||||||
+35
-28
@@ -761,8 +761,8 @@ static bool mg_tls_server_send_hello(struct mg_connection *c) {
|
|||||||
|
|
||||||
// calculate P-256 keyshare and the shared secret
|
// calculate P-256 keyshare and the shared secret
|
||||||
pub[0] = 0x04; // uncompressed point 0x04 || X || Y
|
pub[0] = 0x04; // uncompressed point 0x04 || X || Y
|
||||||
if (!mg_p256_make_key(pub + 1, prv) ||
|
if (mg_p256_make_key(pub + 1, prv) == 0 ||
|
||||||
!mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec)) {
|
mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec) == 0) {
|
||||||
mg_bzero(prv, sizeof(prv));
|
mg_bzero(prv, sizeof(prv));
|
||||||
mg_error(c, "bad key");
|
mg_error(c, "bad key");
|
||||||
return false;
|
return false;
|
||||||
@@ -1177,7 +1177,7 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
|||||||
size_t sigsz, verifysz = 0;
|
size_t sigsz, verifysz = 0;
|
||||||
int adj1, adj2;
|
int adj1, adj2;
|
||||||
uint8_t sig[64] = {0};
|
uint8_t sig[64] = {0};
|
||||||
if (!mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig)) {
|
if (mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig) != 1) {
|
||||||
mg_error(c, "P-256 sign");
|
mg_error(c, "P-256 sign");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1188,11 +1188,11 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
|||||||
verify[10] = 0x02; // ASN.1 INTEGER
|
verify[10] = 0x02; // ASN.1 INTEGER
|
||||||
verify[11] = (uint8_t) (32 + adj1);
|
verify[11] = (uint8_t) (32 + adj1);
|
||||||
memmove(verify + (size_t) (12 + (adj1 > 0)), sig + (size_t) (adj1 < 0),
|
memmove(verify + (size_t) (12 + (adj1 > 0)), sig + (size_t) (adj1 < 0),
|
||||||
(size_t) (32 + (adj1 < 0)));
|
(size_t) (32 - (adj1 < 0)));
|
||||||
verify[12 + 32 + adj1] = 0x02; // ASN.1 INTEGER
|
verify[12 + 32 + adj1] = 0x02; // ASN.1 INTEGER
|
||||||
verify[13 + 32 + adj1] = (uint8_t) (32 + adj2);
|
verify[13 + 32 + adj1] = (uint8_t) (32 + adj2);
|
||||||
memmove(verify + (size_t) (14 + 32 + adj1 + (adj2 > 0)),
|
memmove(verify + (size_t) (14 + 32 + adj1 + (adj2 > 0)),
|
||||||
sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 + (adj2 < 0)));
|
sig + (size_t) (32 + (adj2 < 0)), (size_t) (32 - (adj2 < 0)));
|
||||||
sigsz = (size_t) (70 + adj1 + adj2);
|
sigsz = (size_t) (70 + adj1 + adj2);
|
||||||
verifysz = 8U + sigsz;
|
verifysz = 8U + sigsz;
|
||||||
verify[3] = (uint8_t) (sigsz + 4);
|
verify[3] = (uint8_t) (sigsz + 4);
|
||||||
@@ -1309,7 +1309,7 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
|
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
|
||||||
if (!mg_p256_make_key(msg_client_hello + 95, tls->p256_cli)) {
|
if (mg_p256_make_key(msg_client_hello + 95, tls->p256_cli) == 0) {
|
||||||
mg_error(c, "P-256 key");
|
mg_error(c, "P-256 key");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1399,8 +1399,8 @@ static int mg_tls_client_recv_hello(struct mg_connection *c) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (key_exchange_len != 65 || key_exchange[0] != 0x04 ||
|
if (key_exchange_len != 65 || key_exchange[0] != 0x04 ||
|
||||||
!mg_p256_shared_secret(key_exchange + 1, tls->p256_cli,
|
mg_p256_shared_secret(key_exchange + 1, tls->p256_cli,
|
||||||
tls->ecdhe_sec)) { // Validates the point
|
tls->ecdhe_sec) == 0) { // Validates the point
|
||||||
mg_error(c, "bad key");
|
mg_error(c, "bad key");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1677,17 +1677,20 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
|||||||
const char *server_name,
|
const char *server_name,
|
||||||
struct mg_addr *ref_ip) {
|
struct mg_addr *ref_ip) {
|
||||||
struct mg_der_tlv root, field, name;
|
struct mg_der_tlv root, field, name;
|
||||||
|
int r;
|
||||||
bool is_addr = ref_ip != NULL;
|
bool is_addr = ref_ip != NULL;
|
||||||
if (mg_der_parse((uint8_t *) der, dersz, &root) < 0) {
|
if (mg_der_parse((uint8_t *) der, dersz, &root) < 0) {
|
||||||
MG_ERROR(("failed to parse certificate"));
|
MG_ERROR(("failed to parse certificate"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (mg_der_find_oid(&root, (uint8_t *) "\x55\x1d\x11", 3, &field) <= 0) {
|
if ((r = mg_der_find_oid(&root, (uint8_t *) "\x55\x1d\x11", 3, &field)) < 0) {
|
||||||
MG_ERROR(("failed to extract SAN"));
|
MG_ERROR(("failed to extract SAN"));
|
||||||
return -1;
|
return -1;
|
||||||
|
} else if (r == 0) {
|
||||||
|
return 0; // No SAN
|
||||||
}
|
}
|
||||||
if (mg_der_parse(field.value, field.len, &field) < 0) {
|
if (mg_der_parse(field.value, field.len, &field) < 0 || field.type != 0x30) {
|
||||||
MG_ERROR(("SAN is not a constructed object"));
|
MG_ERROR(("SAN is not a constructed object")); // RFC 5280 4.2.1.6
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
while (mg_der_next(&field, &name) > 0) {
|
while (mg_der_next(&field, &name) > 0) {
|
||||||
@@ -1714,6 +1717,7 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
|||||||
return 1; // and matches the host name
|
return 1; // and matches the host name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MG_ERROR(("Found SAN but does not match IP or DNS name"));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1777,14 +1781,14 @@ static bool mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) {
|
static bool mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) {
|
||||||
struct mg_der_tlv v;
|
struct mg_der_tlv v;
|
||||||
int matched = 0;
|
bool matched = false;
|
||||||
if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) {
|
if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) {
|
||||||
struct mg_str hn, cn;
|
struct mg_str hn, cn;
|
||||||
MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host));
|
MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host));
|
||||||
hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len);
|
hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len);
|
||||||
matched = (int) (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL));
|
matched = (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL));
|
||||||
}
|
}
|
||||||
return matched;
|
return matched;
|
||||||
}
|
}
|
||||||
@@ -1901,12 +1905,15 @@ static int mg_tls_recv_cert(struct mg_connection *c, bool is_client) {
|
|||||||
// and store public key for further CertVerify step
|
// and store public key for further CertVerify step
|
||||||
if (tls->hostname[0] != '\0') {
|
if (tls->hostname[0] != '\0') {
|
||||||
struct mg_addr addr; // use aton_ so localhost evals as a name
|
struct mg_addr addr; // use aton_ so localhost evals as a name
|
||||||
bool is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
bool is_addr;
|
||||||
if (mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
int san;
|
||||||
is_addr ? &addr : NULL) <= 0 &&
|
is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
||||||
(!is_addr || // DNS CN fallback
|
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
||||||
mg_tls_verify_cert_cn(&ci->subj, tls->hostname) <= 0)) {
|
is_addr ? &addr : NULL);
|
||||||
mg_error(c, "failed to verify hostname");
|
if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN
|
||||||
|
(san == 0 &&
|
||||||
|
(is_addr || !mg_tls_verify_cert_cn(&ci->subj, tls->hostname)))) {
|
||||||
|
mg_error(c, "failed to verify host");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2741,8 +2748,8 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
|||||||
mg_error(c, "hostname too long");
|
mg_error(c, "hostname too long");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strncpy((char *) tls->hostname, opts->name.buf, sizeof(tls->hostname) - 1);
|
memcpy(tls->hostname, opts->name.buf, opts->name.len);
|
||||||
tls->hostname[opts->name.len] = 0;
|
tls->hostname[opts->name.len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
// server CA certificate; parse PEM [bundle] or DER
|
// server CA certificate; parse PEM [bundle] or DER
|
||||||
@@ -2755,15 +2762,16 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
|||||||
tls->ca_bundle_der = all_certs;
|
tls->ca_bundle_der = all_certs;
|
||||||
MG_VERBOSE(("%d-cert bundle", cert_count));
|
MG_VERBOSE(("%d-cert bundle", cert_count));
|
||||||
} else if (cert_count > 0) {
|
} else if (cert_count > 0) {
|
||||||
tls->ca_der.buf = all_certs[0].buf;
|
int i;
|
||||||
tls->ca_der.len = all_certs[0].len;
|
tls->ca_der = all_certs[0];
|
||||||
|
for (i = 1; i < cert_count; i++) mg_free((void *) all_certs[i].buf);
|
||||||
mg_free(all_certs);
|
mg_free(all_certs);
|
||||||
} else { // parse again for a possible DER (or a truncated begin string)
|
} else { // parse again for a possible DER (or a truncated begin string)
|
||||||
if (mg_parse_pem(opts->ca, mg_str_s("CERTIFICATE"), &tls->ca_der) < 0) {
|
if (mg_parse_pem(opts->ca, mg_str_s("CERTIFICATE"), &tls->ca_der) < 0) {
|
||||||
mg_error(c, "Failed to load CA certificate");
|
mg_error(c, "Failed to load CA certificate");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} // ca_bundle_len != 0 && ca_der.len = 0 => bundle
|
} // ca_bundle_len != 0 && ca_der.len = 0 => bundle
|
||||||
if (!c->is_client) tls->is_twoway = true; // server + CA: two-way auth
|
if (!c->is_client) tls->is_twoway = true; // server + CA: two-way auth
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2782,8 +2790,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
|||||||
int cert_count = mg_parse_pem_certs(opts->cert, &all_certs);
|
int cert_count = mg_parse_pem_certs(opts->cert, &all_certs);
|
||||||
|
|
||||||
if (cert_count > 0) {
|
if (cert_count > 0) {
|
||||||
tls->cert_der.buf = all_certs[0].buf;
|
tls->cert_der = all_certs[0];
|
||||||
tls->cert_der.len = all_certs[0].len;
|
|
||||||
if (cert_count > 1) {
|
if (cert_count > 1) {
|
||||||
tls->chain_len = (size_t) cert_count;
|
tls->chain_len = (size_t) cert_count;
|
||||||
tls->chain_der = all_certs;
|
tls->chain_der = all_certs;
|
||||||
|
|||||||
+29
-18
@@ -197,33 +197,38 @@ bool mg_path_is_sane(const struct mg_str path) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MG_ENABLE_CUSTOM_MILLIS
|
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
|
||||||
#else
|
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
|
||||||
uint64_t mg_millis(void) {
|
#if MG_ENABLE_CUSTOM_MICROS == 0
|
||||||
#if MG_ARCH == MG_ARCH_ESP32 || MG_ENABLE_FREERTOS
|
uint64_t mg_micros(void) {
|
||||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
#if MG_ARCH == MG_ARCH_ESP32
|
||||||
|
return (uint64_t) esp_timer_get_time();
|
||||||
|
#elif MG_ENABLE_FREERTOS
|
||||||
|
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_THREADX
|
#elif MG_ARCH == MG_ARCH_THREADX
|
||||||
return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
|
return (uint64_t) tx_time_get() *
|
||||||
|
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_TIRTOS
|
#elif MG_ARCH == MG_ARCH_TIRTOS
|
||||||
return (uint64_t) Clock_getTicks();
|
return (uint64_t) Clock_getTicks() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_ZEPHYR
|
#elif MG_ARCH == MG_ARCH_ZEPHYR
|
||||||
return (uint64_t) k_uptime_get();
|
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
|
||||||
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
|
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
|
||||||
return (uint64_t) rt_time_get();
|
return (uint64_t) rt_time_get() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
|
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
|
||||||
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq());
|
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
|
||||||
|
1000;
|
||||||
#elif MG_ARCH == MG_ARCH_RTTHREAD
|
#elif MG_ARCH == MG_ARCH_RTTHREAD
|
||||||
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND);
|
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_WIN32
|
#elif MG_ARCH == MG_ARCH_WIN32
|
||||||
return GetTickCount();
|
return (uint64_t) GetTickCount() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_PICOSDK
|
#elif MG_ARCH == MG_ARCH_PICOSDK
|
||||||
return time_us_64() / 1000;
|
return time_us_64();
|
||||||
#elif MG_ARCH == MG_ARCH_CUBE
|
#elif MG_ARCH == MG_ARCH_CUBE
|
||||||
return (uint64_t) HAL_GetTick();
|
return (uint64_t) HAL_GetTick() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
|
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
|
||||||
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
|
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
|
||||||
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
|
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
|
||||||
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
|
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_UNIX
|
#elif MG_ARCH == MG_ARCH_UNIX
|
||||||
struct timespec ts = {0, 0};
|
struct timespec ts = {0, 0};
|
||||||
// See #1615 - prefer monotonic clock
|
// See #1615 - prefer monotonic clock
|
||||||
@@ -238,15 +243,21 @@ uint64_t mg_millis(void) {
|
|||||||
// adjustments performed by adjtime and NTP
|
// adjustments performed by adjtime and NTP
|
||||||
clock_gettime(CLOCK_REALTIME, &ts);
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
#endif
|
#endif
|
||||||
return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000);
|
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
|
||||||
#elif defined(ARDUINO)
|
#elif defined(ARDUINO)
|
||||||
return (uint64_t) millis();
|
return (uint64_t) micros();
|
||||||
#else
|
#else
|
||||||
return (uint64_t) (time(NULL) * 1000);
|
return (uint64_t) time(NULL) * 1000000;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_CUSTOM_MILLIS == 0
|
||||||
|
uint64_t mg_millis(void) {
|
||||||
|
return mg_micros() / 1000;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// network format equates big endian order
|
// network format equates big endian order
|
||||||
uint16_t mg_ntohs(uint16_t net) {
|
uint16_t mg_ntohs(uint16_t net) {
|
||||||
return MG_LOAD_BE16(&net);
|
return MG_LOAD_BE16(&net);
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
#define MG_VERSION "7.23"
|
#define MG_VERSION "7.24"
|
||||||
+1
-1
@@ -98,7 +98,7 @@ mongoose.c: Makefile $(wildcard ../src/*.c) $(wildcard ../src/drivers/*.c)
|
|||||||
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c src/drivers/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
|
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c src/drivers/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
|
||||||
|
|
||||||
mongoose.h: $(HDRS) Makefile
|
mongoose.h: $(HDRS) Makefile
|
||||||
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/os_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/lfs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_aes128.h src/tls_p256.h src/tls_p384.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/modbus.h src/json.h src/jwt.h src/rpc.h src/dash.h src/ota.h src/health.h src/mdash.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/l2_mesh.h src/bsd.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
|
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; cat src/ai.h; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/os_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/lfs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_aes128.h src/tls_p256.h src/tls_p384.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/modbus.h src/json.h src/jwt.h src/rpc.h src/dash.h src/ota.h src/health.h src/mdash.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/l2_mesh.h src/bsd.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
|
||||||
|
|
||||||
# Check that all external (exported) symbols have "mg_" prefix
|
# Check that all external (exported) symbols have "mg_" prefix
|
||||||
mg_prefix: mongoose.c mongoose.h
|
mg_prefix: mongoose.c mongoose.h
|
||||||
|
|||||||
+34
-5
@@ -3,10 +3,39 @@
|
|||||||
PROJECTS ?= seeeduino-xiao esp32
|
PROJECTS ?= seeeduino-xiao esp32
|
||||||
ROOT ?= $(realpath $(CURDIR)/../..)
|
ROOT ?= $(realpath $(CURDIR)/../..)
|
||||||
ARD ?= $(realpath $(CURDIR))
|
ARD ?= $(realpath $(CURDIR))
|
||||||
|
MONGOOSE_C = $(ROOT)/tutorials/arduino/esp32-http/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/arduino/esp32-mqtt/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/arduino/nano-w5500-mqtt/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/arduino/sim800-mqtt/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/arduino/teensy41-http/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/arduino/w5500-http/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/arduino/w5500-mqtt/mongoose.c \
|
||||||
|
$(ROOT)/tutorials/nxp/teensy-4.1/arduino/mongoose.c
|
||||||
|
MONGOOSE_H = $(MONGOOSE_C:.c=.h)
|
||||||
|
TEENSY_FILES = $(ROOT)/tutorials/arduino/teensy41-http/net.c $(ROOT)/tutorials/arduino/teensy41-http/net.h $(ROOT)/tutorials/arduino/teensy41-http/packed_fs.c
|
||||||
|
|
||||||
all: $(PROJECTS)
|
all: $(PROJECTS)
|
||||||
echo
|
echo
|
||||||
|
|
||||||
|
prepare: $(MONGOOSE_C) $(MONGOOSE_H) $(TEENSY_FILES)
|
||||||
|
|
||||||
|
.PHONY: prepare
|
||||||
|
|
||||||
|
$(MONGOOSE_C): $(ROOT)/mongoose.c
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
$(MONGOOSE_H): $(ROOT)/mongoose.h
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
$(ROOT)/tutorials/arduino/teensy41-http/net.c: $(ROOT)/tutorials/http/device-dashboard/net.c $(ROOT)/tutorials/arduino/teensy41-http/net.h
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
$(ROOT)/tutorials/arduino/teensy41-http/net.h: $(ROOT)/tutorials/http/device-dashboard/net.h
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
$(ROOT)/tutorials/arduino/teensy41-http/packed_fs.c: $(ROOT)/tutorials/http/device-dashboard/packed_fs.c
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
arduino: ENV = -v $(CWD):/root
|
arduino: ENV = -v $(CWD):/root
|
||||||
arduino:
|
arduino:
|
||||||
curl -sL http://downloads.arduino.cc/arduino-1.8.19-linux64.tar.xz | unxz | tar -xf -
|
curl -sL http://downloads.arduino.cc/arduino-1.8.19-linux64.tar.xz | unxz | tar -xf -
|
||||||
@@ -28,26 +57,26 @@ teensy-board:
|
|||||||
./arduino/arduino --install-boards teensy:avr
|
./arduino/arduino --install-boards teensy:avr
|
||||||
touch teensy-board
|
touch teensy-board
|
||||||
|
|
||||||
seeeduino-xiao: arduino seeeduino-xiao-board
|
seeeduino-xiao: prepare arduino seeeduino-xiao-board
|
||||||
cd $(ROOT)/tutorials/arduino/sim800-mqtt && $(ARD)/arduino/arduino --verbose --verify --board Seeeduino:samd:seeed_XIAO_m0 sim800-mqtt.ino
|
cd $(ROOT)/tutorials/arduino/sim800-mqtt && $(ARD)/arduino/arduino --verbose --verify --board Seeeduino:samd:seeed_XIAO_m0 sim800-mqtt.ino
|
||||||
cd $(ROOT)/tutorials/arduino/w5500-http && $(ARD)/arduino/arduino --verbose --verify --board Seeeduino:samd:seeed_XIAO_m0 w5500-http.ino
|
cd $(ROOT)/tutorials/arduino/w5500-http && $(ARD)/arduino/arduino --verbose --verify --board Seeeduino:samd:seeed_XIAO_m0 w5500-http.ino
|
||||||
cd $(ROOT)/tutorials/arduino/w5500-mqtt && $(ARD)/arduino/arduino --verbose --verify --board Seeeduino:samd:seeed_XIAO_m0 w5500-mqtt.ino
|
cd $(ROOT)/tutorials/arduino/w5500-mqtt && $(ARD)/arduino/arduino --verbose --verify --board Seeeduino:samd:seeed_XIAO_m0 w5500-mqtt.ino
|
||||||
|
|
||||||
arduino-nano: arduino
|
arduino-nano: prepare arduino
|
||||||
cd $(ROOT)/tutorials/arduino/nano-w5500-mqtt && $(ARD)/arduino/arduino --pref compiler.cpp.extra_flags=-DMG_ENABLE_LOG=0 --verbose --verify --board arduino:avr:nano nano-w5500-mqtt.ino
|
cd $(ROOT)/tutorials/arduino/nano-w5500-mqtt && $(ARD)/arduino/arduino --pref compiler.cpp.extra_flags=-DMG_ENABLE_LOG=0 --verbose --verify --board arduino:avr:nano nano-w5500-mqtt.ino
|
||||||
|
|
||||||
esp32: arduino esp32-board
|
esp32: prepare arduino esp32-board
|
||||||
cd $(ROOT)/tutorials/arduino/esp32-mqtt && $(ARD)/arduino/arduino --verbose --verify --board esp32:esp32:node32s esp32-mqtt.ino
|
cd $(ROOT)/tutorials/arduino/esp32-mqtt && $(ARD)/arduino/arduino --verbose --verify --board esp32:esp32:node32s esp32-mqtt.ino
|
||||||
# Needs a board with 'LED_BUILTIN', yet somehow invocation is missing that definition (lack of include somehow amended by the IDE)
|
# Needs a board with 'LED_BUILTIN', yet somehow invocation is missing that definition (lack of include somehow amended by the IDE)
|
||||||
#cd $(ROOT)/tutorials/arduino/esp32-http && $(ARD)/arduino/arduino --verbose --verify --board esp32:esp32:esp32 esp32-http.ino
|
#cd $(ROOT)/tutorials/arduino/esp32-http && $(ARD)/arduino/arduino --verbose --verify --board esp32:esp32:esp32 esp32-http.ino
|
||||||
|
|
||||||
teensy41: arduino teensy-board
|
teensy41: prepare arduino teensy-board
|
||||||
cd $(ROOT)/tutorials/arduino/teensy41-http && $(ARD)/arduino/arduino --verbose --verify --board teensy41-http.ino
|
cd $(ROOT)/tutorials/arduino/teensy41-http && $(ARD)/arduino/arduino --verbose --verify --board teensy41-http.ino
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf arduino
|
rm -rf arduino $(MONGOOSE_C) $(MONGOOSE_H) $(TEENSY_FILES)
|
||||||
|
|
||||||
# Board support gets installed in your home dir.
|
# Board support gets installed in your home dir.
|
||||||
# We fool make satisfying a dependency using touch above, otherwise board installation fails as it is already installed
|
# We fool make satisfying a dependency using touch above, otherwise board installation fails as it is already installed
|
||||||
|
|||||||
+9
-6
@@ -13,7 +13,7 @@ else
|
|||||||
C_WARN ?= -Wno-deprecated
|
C_WARN ?= -Wno-deprecated
|
||||||
endif
|
endif
|
||||||
|
|
||||||
.PHONY: all bsd bsd_test bsd_client bsd_nortos bsd_nortos_test shm_tap_bridge \
|
.PHONY: all bsd bsd_test bsd_client bsd_udp bsd_nortos bsd_nortos_test shm_tap_bridge \
|
||||||
shm_port_bridge bsd_s390 bsd_nortos_s390 clean
|
shm_port_bridge bsd_s390 bsd_nortos_s390 clean
|
||||||
|
|
||||||
TEST_SOURCES = bsd_shm_test.c
|
TEST_SOURCES = bsd_shm_test.c
|
||||||
@@ -47,11 +47,11 @@ DOCKER ?= $(DOCKER_BIN) run --platform linux/amd64 --net=host --rm $(ENV) -v $(R
|
|||||||
CFLAGS_MONGOOSE += -DMG_ARCH=MG_ARCH_CUSTOM
|
CFLAGS_MONGOOSE += -DMG_ARCH=MG_ARCH_CUSTOM
|
||||||
|
|
||||||
|
|
||||||
all bsd: shm_tap_bridge bsd_client FreeRTOS-Kernel bsd_test
|
all bsd: shm_tap_bridge bsd_client bsd_udp FreeRTOS-Kernel bsd_test
|
||||||
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_test $(ARGS)
|
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_test $(ARGS)
|
||||||
|
|
||||||
# also
|
# also
|
||||||
bsd_nortos: shm_tap_bridge bsd_client bsd_nortos_test
|
bsd_nortos: shm_tap_bridge bsd_client bsd_udp bsd_nortos_test
|
||||||
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_nortos_test $(ARGS)
|
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_nortos_test $(ARGS)
|
||||||
|
|
||||||
# requires shm_tap_bridge to be running
|
# requires shm_tap_bridge to be running
|
||||||
@@ -70,6 +70,9 @@ bsd_nortos_test:
|
|||||||
bsd_client: bsd_client.c
|
bsd_client: bsd_client.c
|
||||||
$(CC) bsd_client.c -pthread -o $@
|
$(CC) bsd_client.c -pthread -o $@
|
||||||
|
|
||||||
|
bsd_udp: bsd_udp.c
|
||||||
|
$(CC) bsd_udp.c -o $@
|
||||||
|
|
||||||
# bridge shm queue <--> TAP interface
|
# bridge shm queue <--> TAP interface
|
||||||
shm_tap_bridge: shm_tap_bridge.c shm_queue.h
|
shm_tap_bridge: shm_tap_bridge.c shm_queue.h
|
||||||
$(CC) shm_tap_bridge.c -pthread -o $@
|
$(CC) shm_tap_bridge.c -pthread -o $@
|
||||||
@@ -79,7 +82,7 @@ bsd_s390: CFLAGS_MONGOOSE += -DMG_ENABLE_BSD_PROTOTYPES=0
|
|||||||
bsd_s390: ASAN=
|
bsd_s390: ASAN=
|
||||||
bsd_s390: CC = $(DOCKER) mdashnet/s390 cc
|
bsd_s390: CC = $(DOCKER) mdashnet/s390 cc
|
||||||
bsd_s390: RUN = $(DOCKER) --init mdashnet/s390
|
bsd_s390: RUN = $(DOCKER) --init mdashnet/s390
|
||||||
bsd_s390: shm_port_bridge bsd_client FreeRTOS-Kernel bsd_test
|
bsd_s390: shm_port_bridge bsd_client bsd_udp FreeRTOS-Kernel bsd_test
|
||||||
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
||||||
|
|
||||||
#also
|
#also
|
||||||
@@ -87,14 +90,14 @@ bsd_nortos_s390: CFLAGS_MONGOOSE += -DMG_ENABLE_BSD_PROTOTYPES=0
|
|||||||
bsd_nortos_s390: ASAN=
|
bsd_nortos_s390: ASAN=
|
||||||
bsd_nortos_s390: CC = $(DOCKER) mdashnet/s390 cc
|
bsd_nortos_s390: CC = $(DOCKER) mdashnet/s390 cc
|
||||||
bsd_nortos_s390: RUN = $(DOCKER) --init mdashnet/s390
|
bsd_nortos_s390: RUN = $(DOCKER) --init mdashnet/s390
|
||||||
bsd_nortos_s390: shm_port_bridge bsd_client bsd_nortos_test
|
bsd_nortos_s390: shm_port_bridge bsd_client bsd_udp bsd_nortos_test
|
||||||
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_nortos_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_nortos_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
||||||
|
|
||||||
shm_port_bridge: shm_port_bridge.c shm_queue.h
|
shm_port_bridge: shm_port_bridge.c shm_queue.h
|
||||||
$(CC) shm_port_bridge.c $(CFLAGS) -pthread -o $@
|
$(CC) shm_port_bridge.c $(CFLAGS) -pthread -o $@
|
||||||
|
|
||||||
clean: # Cleanup. Delete built program and all build artifacts
|
clean: # Cleanup. Delete built program and all build artifacts
|
||||||
rm -rf bsd_test bsd_client shm_port_bridge *.o *.obj FreeRTOS-Kernel
|
rm -rf bsd_test bsd_client bsd_udp shm_port_bridge *.o *.obj FreeRTOS-Kernel
|
||||||
|
|
||||||
FreeRTOS-Kernel: # FreeRTOS sources
|
FreeRTOS-Kernel: # FreeRTOS sources
|
||||||
git clone -q -c advice.detachedHead=false --depth 1 -b V11.2.0 https://github.com/FreeRTOS/FreeRTOS-Kernel $@
|
git clone -q -c advice.detachedHead=false --depth 1 -b V11.2.0 https://github.com/FreeRTOS/FreeRTOS-Kernel $@
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define PORT 1235
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
struct sockaddr_in sa;
|
||||||
|
struct timeval timeout;
|
||||||
|
char buf[8];
|
||||||
|
ssize_t n;
|
||||||
|
int fd, rogue;
|
||||||
|
|
||||||
|
if (argc != 2) return 1;
|
||||||
|
memset(&sa, 0, sizeof(sa));
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
sa.sin_port = htons(PORT);
|
||||||
|
if (inet_pton(AF_INET, argv[1], &sa.sin_addr) != 1) return 1;
|
||||||
|
timeout.tv_sec = 5, timeout.tv_usec = 0;
|
||||||
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
|
||||||
|
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0 ||
|
||||||
|
connect(fd, (struct sockaddr *) &sa, sizeof(sa)) != 0 ||
|
||||||
|
send(fd, "hello", 5, 0) != 5 || recv(fd, buf, sizeof(buf), 0) != 5 ||
|
||||||
|
memcmp(buf, "ready", 5) != 0)
|
||||||
|
goto fail;
|
||||||
|
if ((rogue = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
|
||||||
|
setsockopt(rogue, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) !=
|
||||||
|
0 ||
|
||||||
|
sendto(rogue, "rogueid", 7, 0, (struct sockaddr *) &sa, sizeof(sa)) !=
|
||||||
|
7 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 7 || memcmp(buf, "peerone", 7) != 0 ||
|
||||||
|
recv(rogue, buf, sizeof(buf), 0) != 7 || memcmp(buf, "rogueok", 7) != 0 ||
|
||||||
|
send(fd, "one", 3, 0) != 3 ||
|
||||||
|
sendto(rogue, "two2", 4, 0, (struct sockaddr *) &sa, sizeof(sa)) != 4 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 4 || memcmp(buf, "drop", 4) != 0 ||
|
||||||
|
send(fd, "truncate", 8, 0) != 8 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 5 || memcmp(buf, "short", 5) != 0 ||
|
||||||
|
send(fd, "peek", 4, 0) != 4 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 4 || memcmp(buf, "peek", 4) != 0 ||
|
||||||
|
sendto(rogue, "rogue", 5, 0, (struct sockaddr *) &sa, sizeof(sa)) != 5 ||
|
||||||
|
send(fd, "more", 4, 0) != 4 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 7 || memcmp(buf, "checked", 7) != 0 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 3 || memcmp(buf, "one", 3) != 0 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 4 || memcmp(buf, "two2", 4) != 0 ||
|
||||||
|
send(fd, "echo", 4, 0) != 4 || recv(fd, buf, sizeof(buf), 0) != 4 ||
|
||||||
|
memcmp(buf, "echo", 4) != 0)
|
||||||
|
goto fail_close_rogue;
|
||||||
|
close(rogue);
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fail_close_rogue:
|
||||||
|
close(rogue);
|
||||||
|
fail:
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
+253
-2
@@ -33,6 +33,198 @@ static int s_abort = 0;
|
|||||||
#define DASHBOARD(x) \
|
#define DASHBOARD(x) \
|
||||||
printf("HEALTH_DASHBOARD\t\"%s\": %s,\n", x, s_error ? "false" : "true")
|
printf("HEALTH_DASHBOARD\t\"%s\": %s,\n", x, s_error ? "false" : "true")
|
||||||
|
|
||||||
|
#define UDP_PORT 1235
|
||||||
|
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
static volatile bool s_tx_held, s_tx_waits;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int open_udp(void) {
|
||||||
|
struct sockaddr_in sa;
|
||||||
|
char buf;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
memset(&sa, 0, sizeof(sa));
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
sa.sin_port = htons(UDP_PORT);
|
||||||
|
sa.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
ASSERT((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0);
|
||||||
|
ASSERT(bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == 0);
|
||||||
|
ASSERT(send(fd, &buf, sizeof(buf), 0) < 0 && errno == EDESTADDRREQ);
|
||||||
|
ASSERT(fcntl(fd, F_SETFL, O_NONBLOCK) == 0);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void check_udp_oversize(struct mg_mgr *mgr, int fd,
|
||||||
|
const struct sockaddr *dst, socklen_t dstlen) {
|
||||||
|
static char buf[1600];
|
||||||
|
size_t len = mgr->ifp->mtu + 1;
|
||||||
|
|
||||||
|
ASSERT(len <= sizeof(buf));
|
||||||
|
if (dst == NULL) {
|
||||||
|
ASSERT(send(fd, buf, len, 0) < 0 && errno == EMSGSIZE);
|
||||||
|
} else {
|
||||||
|
ASSERT(sendto(fd, buf, len, 0, dst, dstlen) < 0 && errno == EMSGSIZE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void udp_exchange(struct mg_mgr *mgr, int fd,
|
||||||
|
void (*progress)(struct mg_mgr *)) {
|
||||||
|
struct sockaddr_in peer, sender;
|
||||||
|
socklen_t peerlen, senderlen;
|
||||||
|
uint64_t deadline;
|
||||||
|
struct timeval zero = {0, 0};
|
||||||
|
struct pollfd p;
|
||||||
|
fd_set r, w, e;
|
||||||
|
char buf[8];
|
||||||
|
ssize_t n;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
peerlen = sizeof(peer);
|
||||||
|
n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) &peer, &peerlen);
|
||||||
|
if (n >= 0) break;
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
ASSERT(n == 5 && memcmp(buf, "hello", 5) == 0);
|
||||||
|
ASSERT(sendto(fd, "ready", 5, 0, (struct sockaddr *) &peer, peerlen) ==
|
||||||
|
5);
|
||||||
|
check_udp_oversize(mgr, fd, (struct sockaddr *) &peer, peerlen);
|
||||||
|
do {
|
||||||
|
senderlen = sizeof(sender);
|
||||||
|
n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) &sender,
|
||||||
|
&senderlen);
|
||||||
|
if (n < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
} while (n < 0);
|
||||||
|
ASSERT(n == 7 && memcmp(buf, "rogueid", 7) == 0 &&
|
||||||
|
sender.sin_port != peer.sin_port);
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
s_tx_waits = false, s_tx_held = true;
|
||||||
|
#endif
|
||||||
|
ASSERT(sendto(fd, "peerone", 7, 0, (struct sockaddr *) &peer, peerlen) ==
|
||||||
|
7);
|
||||||
|
ASSERT(sendto(fd, "rogueok", 7, 0, (struct sockaddr *) &sender,
|
||||||
|
senderlen) < 0 && errno == EAGAIN);
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
while (!s_tx_waits) progress(mgr); // Keep peerone in the UDP slot
|
||||||
|
s_tx_held = false;
|
||||||
|
#endif
|
||||||
|
while ((n = sendto(fd, "rogueok", 7, 0, (struct sockaddr *) &sender,
|
||||||
|
senderlen)) < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
ASSERT(n == 7);
|
||||||
|
deadline = mg_millis() + 100;
|
||||||
|
while (mg_millis() < deadline) progress(mgr); // Queue both; drop the second
|
||||||
|
do {
|
||||||
|
senderlen = sizeof(sender);
|
||||||
|
n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *) &sender,
|
||||||
|
&senderlen);
|
||||||
|
if (n < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
} while (n < 0);
|
||||||
|
ASSERT(n == 3 && memcmp(buf, "one", 3) == 0 &&
|
||||||
|
sender.sin_port == peer.sin_port);
|
||||||
|
ASSERT(recvfrom(fd, buf, sizeof(buf), MSG_DONTWAIT, NULL, NULL) < 0 &&
|
||||||
|
errno == EAGAIN);
|
||||||
|
ASSERT(connect(fd, (struct sockaddr *) &peer, peerlen) == 0);
|
||||||
|
senderlen = sizeof(sender);
|
||||||
|
ASSERT(getsockname(fd, (struct sockaddr *) &sender, &senderlen) == 0 &&
|
||||||
|
sender.sin_port == htons(UDP_PORT));
|
||||||
|
ASSERT(sendto(fd, "no", 2, 0, (struct sockaddr *) &peer, peerlen) < 0 &&
|
||||||
|
errno == EISCONN);
|
||||||
|
check_udp_oversize(mgr, fd, NULL, 0);
|
||||||
|
ASSERT(send(fd, "drop", 4, 0) == 4);
|
||||||
|
do {
|
||||||
|
n = recv(fd, buf, 3, 0);
|
||||||
|
if (n < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
} while (n < 0);
|
||||||
|
ASSERT(n == 3 && memcmp(buf, "tru", 3) == 0);
|
||||||
|
ASSERT(recv(fd, buf, sizeof(buf), MSG_DONTWAIT) < 0 && errno == EAGAIN);
|
||||||
|
ASSERT(send(fd, "short", 5, 0) == 5);
|
||||||
|
do {
|
||||||
|
n = recv(fd, buf, sizeof(buf), MSG_PEEK);
|
||||||
|
if (n < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
} while (n < 0);
|
||||||
|
ASSERT(n == 4 && memcmp(buf, "peek", 4) == 0);
|
||||||
|
ASSERT(recv(fd, buf, sizeof(buf), 0) == 4 && memcmp(buf, "peek", 4) == 0);
|
||||||
|
ASSERT(send(fd, "peek", 4, 0) == 4);
|
||||||
|
deadline = mg_millis() + 100;
|
||||||
|
while (mg_millis() < deadline) progress(mgr); // Let rogue arrive; ignore it
|
||||||
|
do {
|
||||||
|
p.fd = fd, p.events = POLLIN | POLLOUT, p.revents = 0;
|
||||||
|
ASSERT(poll(&p, 1, 0) == 1);
|
||||||
|
if (!(p.revents & POLLIN)) progress(mgr);
|
||||||
|
} while (!(p.revents & POLLIN));
|
||||||
|
ASSERT(p.revents == (POLLIN | POLLOUT));
|
||||||
|
FD_ZERO(&r);
|
||||||
|
FD_ZERO(&w);
|
||||||
|
FD_ZERO(&e);
|
||||||
|
FD_SET(fd, &r);
|
||||||
|
FD_SET(fd, &w);
|
||||||
|
FD_SET(fd, &e);
|
||||||
|
ASSERT(select(fd + 1, &r, &w, &e, &zero) == 1);
|
||||||
|
ASSERT(FD_ISSET(fd, &r) && FD_ISSET(fd, &w) && !FD_ISSET(fd, &e));
|
||||||
|
do {
|
||||||
|
n = recv(fd, buf, sizeof(buf), 0);
|
||||||
|
if (n < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
} while (n < 0);
|
||||||
|
ASSERT(n == 4 && memcmp(buf, "more", 4) == 0);
|
||||||
|
p.events = POLLIN | POLLOUT, p.revents = 0;
|
||||||
|
ASSERT(poll(&p, 1, 0) == 1 && p.revents == POLLOUT);
|
||||||
|
while ((n = send(fd, "checked", 7, 0)) < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
ASSERT(n == 7);
|
||||||
|
while ((n = send(fd, "one", 3, MSG_DONTWAIT)) < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
ASSERT(n == 3);
|
||||||
|
p.fd = fd, p.events = POLLOUT, p.revents = 0;
|
||||||
|
ASSERT(poll(&p, 1, 0) == 0 && p.revents == 0);
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
s_tx_waits = false, s_tx_held = true;
|
||||||
|
#endif
|
||||||
|
ASSERT(send(fd, "two2", 4, MSG_DONTWAIT) < 0 && errno == EAGAIN);
|
||||||
|
ASSERT(send(fd, "", 0, 0) == 0);
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
while (!s_tx_waits) progress(mgr); // MG_IO_WAIT keeps one datagram queued
|
||||||
|
s_tx_held = false;
|
||||||
|
#endif
|
||||||
|
while ((n = send(fd, "two2", 4, MSG_DONTWAIT)) < 0) {
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
ASSERT(n == 4);
|
||||||
|
for (;;) {
|
||||||
|
n = recv(fd, buf, sizeof(buf), 0);
|
||||||
|
if (n >= 0) break;
|
||||||
|
ASSERT(errno == EAGAIN);
|
||||||
|
progress(mgr);
|
||||||
|
}
|
||||||
|
ASSERT(n == 4 && memcmp(buf, "echo", 4) == 0);
|
||||||
|
ASSERT(send(fd, "echo", 4, 0) == 4);
|
||||||
|
progress(mgr); // Drain the last queued datagram before close
|
||||||
|
ASSERT(close(fd) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
static pid_t start_client(const char *ip, const char *connections) {
|
static pid_t start_client(const char *ip, const char *connections) {
|
||||||
char *cmd[4];
|
char *cmd[4];
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
@@ -46,6 +238,18 @@ static pid_t start_client(const char *ip, const char *connections) {
|
|||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static pid_t start_udp(const char *ip) {
|
||||||
|
char *cmd[3];
|
||||||
|
pid_t pid;
|
||||||
|
extern char **environ;
|
||||||
|
|
||||||
|
cmd[0] = (char *) "./bsd_udp";
|
||||||
|
cmd[1] = (char *) ip;
|
||||||
|
cmd[2] = NULL;
|
||||||
|
ASSERT(posix_spawn(&pid, cmd[0], NULL, NULL, cmd, environ) == 0);
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
static bool client_done(pid_t pid, int *status) {
|
static bool client_done(pid_t pid, int *status) {
|
||||||
pid_t r = waitpid(pid, status, WNOHANG);
|
pid_t r = waitpid(pid, status, WNOHANG);
|
||||||
ASSERT(r >= 0);
|
ASSERT(r >= 0);
|
||||||
@@ -60,12 +264,17 @@ static void check_client(int status) {
|
|||||||
static int open_listener(int backlog, bool nonblock) {
|
static int open_listener(int backlog, bool nonblock) {
|
||||||
int lfd = socket(AF_INET, SOCK_STREAM, 0);
|
int lfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
struct sockaddr_in sa;
|
struct sockaddr_in sa;
|
||||||
|
char buf;
|
||||||
|
|
||||||
memset(&sa, 0, sizeof(sa));
|
memset(&sa, 0, sizeof(sa));
|
||||||
sa.sin_family = AF_INET;
|
sa.sin_family = AF_INET;
|
||||||
sa.sin_port = htons(1234);
|
sa.sin_port = htons(1234);
|
||||||
sa.sin_addr.s_addr = INADDR_ANY;
|
sa.sin_addr.s_addr = INADDR_ANY;
|
||||||
ASSERT(lfd >= 0);
|
ASSERT(lfd >= 0);
|
||||||
|
ASSERT(sendto(lfd, &buf, sizeof(buf), 0, (struct sockaddr *) &sa,
|
||||||
|
sizeof(sa)) < 0 && errno == EOPNOTSUPP);
|
||||||
|
ASSERT(recvfrom(lfd, &buf, sizeof(buf), 0, NULL, NULL) < 0 &&
|
||||||
|
errno == EOPNOTSUPP);
|
||||||
ASSERT(bind(lfd, (struct sockaddr *) &sa, sizeof(sa)) == 0);
|
ASSERT(bind(lfd, (struct sockaddr *) &sa, sizeof(sa)) == 0);
|
||||||
ASSERT(listen(lfd, backlog) == 0);
|
ASSERT(listen(lfd, backlog) == 0);
|
||||||
if (nonblock) ASSERT(fcntl(lfd, F_SETFL, O_NONBLOCK) == 0);
|
if (nonblock) ASSERT(fcntl(lfd, F_SETFL, O_NONBLOCK) == 0);
|
||||||
@@ -146,12 +355,13 @@ static volatile bool s_select_waiting, s_select_done;
|
|||||||
static volatile bool s_poll_waiting, s_poll_done;
|
static volatile bool s_poll_waiting, s_poll_done;
|
||||||
static volatile bool s_ready_waiting, s_ready_done;
|
static volatile bool s_ready_waiting, s_ready_done;
|
||||||
static volatile bool s_backlog_ready, s_backlog_full, s_backlog_accept;
|
static volatile bool s_backlog_ready, s_backlog_full, s_backlog_accept;
|
||||||
static volatile bool s_tx_held, s_tx_waits, s_tx_fail, s_tx_error;
|
static volatile bool s_tx_fail, s_tx_error;
|
||||||
static volatile bool s_send_waiting;
|
static volatile bool s_send_waiting;
|
||||||
static volatile bool s_send_done, s_send_expect_error, s_send_expect_shutdown;
|
static volatile bool s_send_done, s_send_expect_error, s_send_expect_shutdown;
|
||||||
static volatile bool s_recv_start, s_recv_waiting, s_recv_done;
|
static volatile bool s_recv_start, s_recv_waiting, s_recv_done;
|
||||||
static volatile bool s_recv_expect_error;
|
static volatile bool s_recv_expect_error;
|
||||||
static volatile bool s_shutdown_waiting, s_shutdown_done;
|
static volatile bool s_shutdown_waiting, s_shutdown_done;
|
||||||
|
static volatile bool s_udp_ready, s_udp_done;
|
||||||
static size_t (*s_tx)(const void *, size_t, struct mg_tcpip_if *);
|
static size_t (*s_tx)(const void *, size_t, struct mg_tcpip_if *);
|
||||||
|
|
||||||
struct client_data {
|
struct client_data {
|
||||||
@@ -170,6 +380,7 @@ static void readiness_task(void *args);
|
|||||||
static void backlog_task(void *args);
|
static void backlog_task(void *args);
|
||||||
static void tx_gate_task(void *args);
|
static void tx_gate_task(void *args);
|
||||||
static void shutdown_task(void *args);
|
static void shutdown_task(void *args);
|
||||||
|
static void udp_task(void *args);
|
||||||
|
|
||||||
struct atask_args {
|
struct atask_args {
|
||||||
bool listener_nonblock;
|
bool listener_nonblock;
|
||||||
@@ -329,6 +540,21 @@ static void tx_gate_task(void *args) {
|
|||||||
(void) args;
|
(void) args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void udp_delay(struct mg_mgr *mgr) {
|
||||||
|
(void) mgr;
|
||||||
|
vTaskDelay(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void udp_task(void *args) {
|
||||||
|
struct mg_mgr *mgr = (struct mg_mgr *) args;
|
||||||
|
int fd = open_udp();
|
||||||
|
|
||||||
|
s_udp_ready = true;
|
||||||
|
udp_exchange(mgr, fd, udp_delay);
|
||||||
|
s_udp_done = true;
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
static void recv_wait_task(void *args) {
|
static void recv_wait_task(void *args) {
|
||||||
char buf;
|
char buf;
|
||||||
int fd = (int) (uintptr_t) args;
|
int fd = (int) (uintptr_t) args;
|
||||||
@@ -599,12 +825,22 @@ static void task2(void *args) {
|
|||||||
struct mg_mgr *mgr = (struct mg_mgr *)args; // Event manager
|
struct mg_mgr *mgr = (struct mg_mgr *)args; // Event manager
|
||||||
char *ip;
|
char *ip;
|
||||||
int status;
|
int status;
|
||||||
pid_t first, second;
|
pid_t first, second, udp;
|
||||||
|
|
||||||
vTaskDelay(pdMS_TO_TICKS(500)); // 500 ms
|
vTaskDelay(pdMS_TO_TICKS(500)); // 500 ms
|
||||||
s_error = false;
|
s_error = false;
|
||||||
s_wait = false;
|
s_wait = false;
|
||||||
ip = mg_mprintf("%M", mg_print_ip4, &mgr->ifp->ip);
|
ip = mg_mprintf("%M", mg_print_ip4, &mgr->ifp->ip);
|
||||||
|
s_udp_ready = s_udp_done = false;
|
||||||
|
ASSERT(xTaskCreate(udp_task, "udp", 512, mgr, configMAX_PRIORITIES - 1,
|
||||||
|
NULL) == pdPASS);
|
||||||
|
while (!s_udp_ready) vTaskDelay(1);
|
||||||
|
udp = start_udp(ip);
|
||||||
|
while (!client_done(udp, &status)) vTaskDelay(1);
|
||||||
|
check_client(status);
|
||||||
|
while (!s_udp_done) vTaskDelay(1);
|
||||||
|
DASHBOARD("udp");
|
||||||
|
|
||||||
s_poll_waiting = s_poll_done = false;
|
s_poll_waiting = s_poll_done = false;
|
||||||
ASSERT(xTaskCreate(poll_task, "poll", 512, NULL,
|
ASSERT(xTaskCreate(poll_task, "poll", 512, NULL,
|
||||||
configMAX_PRIORITIES - 1, NULL) == pdPASS);
|
configMAX_PRIORITIES - 1, NULL) == pdPASS);
|
||||||
@@ -963,6 +1199,20 @@ static void nortos_echo(struct mg_mgr *mgr, struct nortos_echo_client *clients,
|
|||||||
nortos_wait_client(mgr, pid);
|
nortos_wait_client(mgr, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void udp_poll(struct mg_mgr *mgr) {
|
||||||
|
mg_mgr_poll(mgr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void nortos_udp(struct mg_mgr *mgr, const char *ip) {
|
||||||
|
pid_t pid;
|
||||||
|
int fd = open_udp();
|
||||||
|
|
||||||
|
pid = start_udp(ip);
|
||||||
|
udp_exchange(mgr, fd, udp_poll);
|
||||||
|
nortos_wait_client(mgr, pid);
|
||||||
|
DASHBOARD("nortos_udp");
|
||||||
|
}
|
||||||
|
|
||||||
static void nortos_test(struct mg_mgr *mgr) {
|
static void nortos_test(struct mg_mgr *mgr) {
|
||||||
static const char bootstrap = '\0';
|
static const char bootstrap = '\0';
|
||||||
static const char grow[512] = {0};
|
static const char grow[512] = {0};
|
||||||
@@ -1083,6 +1333,7 @@ static void nortos_test(struct mg_mgr *mgr) {
|
|||||||
ASSERT(close(fd) == 0);
|
ASSERT(close(fd) == 0);
|
||||||
mg_mgr_poll(mgr, 0);
|
mg_mgr_poll(mgr, 0);
|
||||||
DASHBOARD("nortos_nonblocking_connect");
|
DASHBOARD("nortos_nonblocking_connect");
|
||||||
|
nortos_udp(mgr, ip);
|
||||||
mg_free(ip);
|
mg_free(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@ HDRS = simulator.h $(ROOT)/mongoose.h
|
|||||||
WARN = -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common \
|
WARN = -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common \
|
||||||
-Wconversion -Wundef -Wmissing-prototypes -Wstrict-prototypes
|
-Wconversion -Wundef -Wmissing-prototypes -Wstrict-prototypes
|
||||||
DEFS = -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ENABLE_IPV6=0 \
|
DEFS = -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ENABLE_IPV6=0 \
|
||||||
-DMG_ENABLE_CUSTOM_MILLIS=1 -DMG_ENABLE_TCPIP_TCPRTX=1 \
|
-DMG_ENABLE_CUSTOM_MICROS=1 -DMG_ENABLE_TCPIP_TCPRTX=1 \
|
||||||
-DMG_IO_SIZE=512 \
|
-DMG_IO_SIZE=512 \
|
||||||
-DMG_TCPIP_ARP_MS=1000 -DMG_TCPIP_ACK_MS=500 # Slow radio: wait longer
|
-DMG_TCPIP_ARP_MS=1000 -DMG_TCPIP_ACK_MS=500 # Slow radio: wait longer
|
||||||
CFLAGS ?= -O2 -g0 $(WARN)
|
CFLAGS ?= -O2 -g0 $(WARN)
|
||||||
|
|||||||
+3
-3
@@ -277,7 +277,7 @@ The FRDM-RW612 example is the Ethernet bridge. The XIAO MG24 and
|
|||||||
FRDM-MCXW71 examples are radio clients. All three use the slotted mesh.
|
FRDM-MCXW71 examples are radio clients. All three use the slotted mesh.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
make -C tutorials/nxp/frdm-rw612/bridge
|
make -C tutorials/nxp/frdm-rw612/ethernet-to-802.15.4-bridge
|
||||||
make -C tutorials/silabs/xiao-mg24/minimal
|
make -C tutorials/silabs/xiao-mg24/minimal
|
||||||
make -C tutorials/nxp/frdm-mcxw71/minimal
|
make -C tutorials/nxp/frdm-mcxw71/minimal
|
||||||
```
|
```
|
||||||
@@ -292,7 +292,7 @@ MG24 is a node only; its radio address is derived from its MAC.
|
|||||||
The drivers in `src/drivers` send and receive raw 802.15.4 frames.
|
The drivers in `src/drivers` send and receive raw 802.15.4 frames.
|
||||||
They use the same slotted mesh implementation as the simulator.
|
They use the same slotted mesh implementation as the simulator.
|
||||||
The board driver is initialized by `mg_mgr_init()`. The RW612 bridge
|
The board driver is initialized by `mg_mgr_init()`. The RW612 bridge
|
||||||
has its own `bridge/main.c`, which initializes and polls the two drivers
|
has its own `ethernet-to-802.15.4-bridge/main.c`, which initializes and polls the two drivers
|
||||||
directly, without an event manager or local IP stack.
|
directly, without an event manager or local IP stack.
|
||||||
|
|
||||||
## Hardware integration
|
## Hardware integration
|
||||||
@@ -306,7 +306,7 @@ A NULL strategy selects `mg_mesh_default`, named `"default"`; assigning another
|
|||||||
raw radio frames; the selected strategy owns joining, scheduling and Ethernet
|
raw radio frames; the selected strategy owns joining, scheduling and Ethernet
|
||||||
fragmentation. Driver initialization goes through `mg_tcpip_init()`.
|
fragmentation. Driver initialization goes through `mg_tcpip_init()`.
|
||||||
|
|
||||||
Board examples are under `tutorials/nxp/frdm-rw612/bridge`,
|
Board examples are under `tutorials/nxp/frdm-rw612/ethernet-to-802.15.4-bridge`,
|
||||||
`tutorials/nxp/frdm-mcxw71/minimal`, `tutorials/silabs/xiao-mg24/minimal`, and
|
`tutorials/nxp/frdm-mcxw71/minimal`, `tutorials/silabs/xiao-mg24/minimal`, and
|
||||||
`tutorials/nordic/xiao-52840/minimal`. Their HALs supply `mg_micros()`.
|
`tutorials/nordic/xiao-52840/minimal`. Their HALs supply `mg_micros()`.
|
||||||
|
|
||||||
|
|||||||
@@ -18,10 +18,6 @@ uint64_t mg_micros(void) {
|
|||||||
return s_sim.now;
|
return s_sim.now;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t mg_millis(void) {
|
|
||||||
return mg_micros() / 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse "X,Y,Z", or "X,Y" with Z 0. Return false if it is not that
|
// Parse "X,Y,Z", or "X,Y" with Z 0. Return false if it is not that
|
||||||
static bool parse_pos(const char *s, struct pos *p) {
|
static bool parse_pos(const char *s, struct pos *p) {
|
||||||
int n = 0; // Characters parsed
|
int n = 0; // Characters parsed
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
PROJECTS ?= $(wildcard ../../tutorials/pico-sdk/*-picosdk-*)
|
PROJECTS ?= $(wildcard ../../tutorials/pico-sdk/*-picosdk-*)
|
||||||
SDK_PATH ?= $(realpath $(PWD))/pico-sdk
|
SDK_PATH ?= $(realpath $(PWD))/pico-sdk
|
||||||
SDK_VERSION ?= 2.1.0
|
SDK_VERSION ?= 2.2.0
|
||||||
|
|
||||||
all: $(PROJECTS)
|
all: $(PROJECTS)
|
||||||
echo
|
echo
|
||||||
|
|||||||
+54
-1
@@ -72,6 +72,20 @@ static void test_match(void) {
|
|||||||
ASSERT(mg_match(mg_str_n("caabxa", 6), mg_str_n("#aa#bb#", 7), NULL) == 0);
|
ASSERT(mg_match(mg_str_n("caabxa", 6), mg_str_n("#aa#bb#", 7), NULL) == 0);
|
||||||
ASSERT(mg_match(mg_str_n("a__b_c", 6), mg_str_n("a*b*c", 5), NULL) == 1);
|
ASSERT(mg_match(mg_str_n("a__b_c", 6), mg_str_n("a*b*c", 5), NULL) == 1);
|
||||||
|
|
||||||
|
ASSERT(mg_match(mg_str("a/b/d/c"), mg_str("a/*/c"), NULL) == false);
|
||||||
|
ASSERT(mg_match(mg_str("a/b/c"), mg_str("a/*/c"), NULL) == true);
|
||||||
|
ASSERT(mg_match(mg_str("a/b/d/c"), mg_str("a/#/c"), NULL) == true);
|
||||||
|
{
|
||||||
|
struct mg_str caps[2];
|
||||||
|
ASSERT(mg_match(mg_str("a/b/d/c"), mg_str("a/*/c"), caps) == false);
|
||||||
|
ASSERT(mg_match(mg_str("a/b/c"), mg_str("a/*/c"), caps) == true);
|
||||||
|
ASSERT(mg_strcmp(caps[0], mg_str("b")) == 0);
|
||||||
|
ASSERT(mg_strcmp(caps[1], mg_str("")) == 0);
|
||||||
|
ASSERT(mg_match(mg_str("a/b/d/c"), mg_str("a/#/c"), caps) == true);
|
||||||
|
ASSERT(mg_strcmp(caps[0], mg_str("b/d")) == 0);
|
||||||
|
ASSERT(mg_strcmp(caps[1], mg_str("")) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
char *ae_value = (char *) malloc(1); // Exact size for ASAN
|
char *ae_value = (char *) malloc(1); // Exact size for ASAN
|
||||||
ae_value[0] = 'g';
|
ae_value[0] = 'g';
|
||||||
@@ -1505,6 +1519,7 @@ static void test_http_404(void) {
|
|||||||
|
|
||||||
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n\n") == 200);
|
ASSERT(fetch(&mgr, buf, url, "GET /a.txt HTTP/1.0\n\n") == 200);
|
||||||
ASSERT(cmpbody(buf, "hello\n") == 0);
|
ASSERT(cmpbody(buf, "hello\n") == 0);
|
||||||
|
ASSERT(fetch(&mgr, buf, url, "GET /a.txt%%00x HTTP/1.0\n\n") == 400);
|
||||||
ASSERT(fetch(&mgr, buf, url, "GET /a/a.txt HTTP/1.0\n\n") == 200);
|
ASSERT(fetch(&mgr, buf, url, "GET /a/a.txt HTTP/1.0\n\n") == 200);
|
||||||
ASSERT(cmpbody(buf, "hello\n") == 0);
|
ASSERT(cmpbody(buf, "hello\n") == 0);
|
||||||
ASSERT(fetch(&mgr, buf, url, "GET /unmapped HTTP/1.0\n\n") == 400);
|
ASSERT(fetch(&mgr, buf, url, "GET /unmapped HTTP/1.0\n\n") == 400);
|
||||||
@@ -4975,6 +4990,7 @@ static void test_dash(void) {
|
|||||||
const char *url = "http://localhost:26352";
|
const char *url = "http://localhost:26352";
|
||||||
const char *ws_url = "ws://localhost:26352/api/websocket";
|
const char *ws_url = "ws://localhost:26352/api/websocket";
|
||||||
struct mg_mgr mgr;
|
struct mg_mgr mgr;
|
||||||
|
struct mg_connection *listener;
|
||||||
struct mg_dash dash;
|
struct mg_dash dash;
|
||||||
struct dash_ws_test ws_admin, ws_user_notify;
|
struct dash_ws_test ws_admin, ws_user_notify;
|
||||||
bool two = false;
|
bool two = false;
|
||||||
@@ -5103,7 +5119,44 @@ static void test_dash(void) {
|
|||||||
MG_DASH_ADD_FIELD_SET(&dash, &arr_set);
|
MG_DASH_ADD_FIELD_SET(&dash, &arr_set);
|
||||||
mg_mgr_init(&mgr);
|
mg_mgr_init(&mgr);
|
||||||
mg_mem_files = mg_packed_files;
|
mg_mem_files = mg_packed_files;
|
||||||
ASSERT(mg_http_listen(&mgr, url, mg_dash_ev_handler, &dash) != NULL);
|
listener = mg_http_listen(&mgr, url, mg_dash_ev_handler, &dash);
|
||||||
|
ASSERT(listener != NULL);
|
||||||
|
|
||||||
|
// Unauthenticated logout must leave a valid stream for the next response
|
||||||
|
{
|
||||||
|
const char *request = "GET /api/logout HTTP/1.1\r\nHost: h\r\n\r\n"
|
||||||
|
"GET /api/hi HTTP/1.1\r\nHost: h\r\n\r\n";
|
||||||
|
struct mg_dash logout_dash;
|
||||||
|
struct mg_connection c;
|
||||||
|
struct mg_http_message hm;
|
||||||
|
size_t first_len;
|
||||||
|
long n;
|
||||||
|
memset(&logout_dash, 0, sizeof(logout_dash));
|
||||||
|
logout_dash.authenticate = dash_authenticate;
|
||||||
|
memset(&c, 0, sizeof(c));
|
||||||
|
c.mgr = &mgr;
|
||||||
|
c.fn = mg_dash_ev_handler;
|
||||||
|
c.fn_data = &logout_dash;
|
||||||
|
c.pfn = listener->pfn;
|
||||||
|
c.is_accepted = 1;
|
||||||
|
mg_call(&c, MG_EV_OPEN, NULL);
|
||||||
|
mg_iobuf_add(&c.recv, 0, request, strlen(request));
|
||||||
|
n = (long) c.recv.len;
|
||||||
|
mg_call(&c, MG_EV_READ, &n);
|
||||||
|
ASSERT(!c.is_websocket && !c.is_closing && !c.is_draining);
|
||||||
|
ASSERT(c.recv.len == 0);
|
||||||
|
ASSERT(mg_http_parse((char *) c.send.buf, c.send.len, &hm) > 0);
|
||||||
|
ASSERT(mg_http_status(&hm) == 401);
|
||||||
|
ASSERT(mg_strcmp(hm.body, mg_str("Unauthorized\n")) == 0);
|
||||||
|
first_len = hm.message.len;
|
||||||
|
ASSERT(mg_http_parse((char *) c.send.buf + first_len,
|
||||||
|
c.send.len - first_len, &hm) > 0);
|
||||||
|
ASSERT(mg_http_status(&hm) == 200);
|
||||||
|
ASSERT(mg_strcmp(hm.body, mg_str("hi\n")) == 0);
|
||||||
|
ASSERT(first_len + hm.message.len == c.send.len);
|
||||||
|
mg_iobuf_free(&c.recv);
|
||||||
|
mg_iobuf_free(&c.send);
|
||||||
|
}
|
||||||
|
|
||||||
// Test general get / set functionality
|
// Test general get / set functionality
|
||||||
ASSERT(fetch(&mgr, buf, url, "GET /api/get HTTP/1.0\n\n") == 200);
|
ASSERT(fetch(&mgr, buf, url, "GET /api/get HTTP/1.0\n\n") == 200);
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../http/device-dashboard/net.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../http/device-dashboard/net.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../http/device-dashboard/packed_fs.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
PROG ?= example # Program we are building
|
PROG ?= example # Program we are building
|
||||||
DELETE = rm -rf # Command to remove files
|
DELETE = rm -rf # Command to remove files
|
||||||
OUT ?= -o $(PROG) # Compiler argument for output file
|
OUT ?= -o $(PROG) # Compiler argument for output file
|
||||||
SOURCES = main.c mongoose.c packed_fs.c # Source code files
|
REPO_ROOT ?= ../../..
|
||||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
SOURCES = main.c $(REPO_ROOT)/mongoose.c packed_fs.c # Source code files
|
||||||
|
CFLAGS = -I$(REPO_ROOT) -W -Wall -Wextra -g -I. # Build options
|
||||||
PACK = ./pack # Utility to pack files into a packed filesystem
|
PACK = ./pack # Utility to pack files into a packed filesystem
|
||||||
|
|
||||||
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
SOURCES = main.c mongoose.c # Source code files
|
REPO_ROOT ?= ../../../..
|
||||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
SOURCES = main.c $(REPO_ROOT)/mongoose.c # Source code files
|
||||||
|
CFLAGS = -I$(REPO_ROOT) -W -Wall -Wextra -g -I. # Build options
|
||||||
|
|
||||||
# O(1) Heap
|
# O(1) Heap
|
||||||
SOURCES += o1heap/o1heap/o1heap.c
|
SOURCES += o1heap/o1heap/o1heap.c
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../../mongoose.h
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
SOURCES = main.c mongoose.c # Source code files
|
REPO_ROOT ?= ../../..
|
||||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
SOURCES = main.c $(REPO_ROOT)/mongoose.c # Source code files
|
||||||
|
CFLAGS = -I$(REPO_ROOT) -W -Wall -Wextra -g -I. # Build options
|
||||||
|
|
||||||
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
||||||
CFLAGS_MONGOOSE +=
|
CFLAGS_MONGOOSE +=
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
SOURCES = main.c mongoose.c # Source code files
|
REPO_ROOT ?= ../../..
|
||||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
SOURCES = main.c $(REPO_ROOT)/mongoose.c # Source code files
|
||||||
|
CFLAGS = -I$(REPO_ROOT) -W -Wall -Wextra -g -I. # Build options
|
||||||
|
|
||||||
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
||||||
CFLAGS_MONGOOSE +=
|
CFLAGS_MONGOOSE +=
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
PROG ?= example # Program we are building
|
PROG ?= example # Program we are building
|
||||||
DELETE = rm -rf # Command to remove files
|
DELETE = rm -rf # Command to remove files
|
||||||
OUT ?= -o $(PROG) # Compiler argument for output file
|
OUT ?= -o $(PROG) # Compiler argument for output file
|
||||||
SOURCES = main.c mongoose.c # Source code files
|
REPO_ROOT ?= ../../..
|
||||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
SOURCES = main.c $(REPO_ROOT)/mongoose.c # Source code files
|
||||||
|
CFLAGS = -I$(REPO_ROOT) -W -Wall -Wextra -g -I. # Build options
|
||||||
|
|
||||||
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
||||||
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES
|
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -110,6 +110,11 @@
|
|||||||
setupAutoLogout();
|
setupAutoLogout();
|
||||||
document.body.style.visibility = 'visible';
|
document.body.style.visibility = 'visible';
|
||||||
refreshFiles();
|
refreshFiles();
|
||||||
|
// Static graph is not pushed by the device, so load it once here
|
||||||
|
Dashboard.call('get', 'graph1').then(r => {
|
||||||
|
mkGraph(graphs.graph1);
|
||||||
|
updateGraph1(r?.data);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Dashboard.login().catch(err => showLogin(true));
|
Dashboard.login().catch(err => showLogin(true));
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,18 @@ all: example
|
|||||||
example:
|
example:
|
||||||
true
|
true
|
||||||
|
|
||||||
build: Makefile $(wildcard main/*)
|
build: Makefile $(wildcard main/*) main/net.c main/net.h main/packed_fs.c
|
||||||
$(DOCKER) $(CMD)
|
$(DOCKER) $(CMD)
|
||||||
|
|
||||||
|
main/net.c: ../../http/device-dashboard/net.c main/net.h
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
main/net.h: ../../http/device-dashboard/net.h
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
main/packed_fs.c: ../../http/device-dashboard/packed_fs.c
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
flash:
|
flash:
|
||||||
flash: CMD = idf.py flash
|
flash: CMD = idf.py flash
|
||||||
flash: DA = --device $(PORT)
|
flash: DA = --device $(PORT)
|
||||||
@@ -23,7 +32,7 @@ dashboard.hex: build
|
|||||||
esputil mkhex \
|
esputil mkhex \
|
||||||
0x8000 build/partition_table/partition-table.bin \
|
0x8000 build/partition_table/partition-table.bin \
|
||||||
0x1000 build/bootloader/bootloader.bin \
|
0x1000 build/bootloader/bootloader.bin \
|
||||||
0x100000 build/mongoose-esp32-example.bin > $@
|
0x20000 build/mongoose-esp32-example.bin > $@
|
||||||
|
|
||||||
flash2: dashboard.hex
|
flash2: dashboard.hex
|
||||||
esputil -p $(PORT) -b 921600 -fp 0x220 flash dashboard.hex
|
esputil -p $(PORT) -b 921600 -fp 0x220 flash dashboard.hex
|
||||||
@@ -32,7 +41,8 @@ flash2: dashboard.hex
|
|||||||
ESPTOOL ?= esptool.py
|
ESPTOOL ?= esptool.py
|
||||||
|
|
||||||
flash3:
|
flash3:
|
||||||
cd build && $(ESPTOOL) --chip esp32 -p $(PORT) -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x100000 mongoose-esp32-example.bin
|
cd build && $(ESPTOOL) --chip esp32 -p $(PORT) -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x20000 mongoose-esp32-example.bin
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
test -d build && $(DOCKER) rm -rf build sdkconfig || true
|
test -d build && $(DOCKER) rm -rf build sdkconfig || true
|
||||||
|
rm -f main/net.c main/net.h main/packed_fs.c
|
||||||
@@ -2,6 +2,8 @@ idf_component_register(SRCS "main.c"
|
|||||||
"wifi.c"
|
"wifi.c"
|
||||||
"net.c"
|
"net.c"
|
||||||
"packed_fs.c"
|
"packed_fs.c"
|
||||||
"mongoose.c")
|
"../../../../mongoose.c"
|
||||||
|
INCLUDE_DIRS "."
|
||||||
|
"../../../..")
|
||||||
component_compile_options(-DHTTP_URL="http://0.0.0.0:80")
|
component_compile_options(-DHTTP_URL="http://0.0.0.0:80")
|
||||||
component_compile_options(-DHTTPS_URL="https://0.0.0.0:443")
|
component_compile_options(-DHTTPS_URL="https://0.0.0.0:443")
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../../mongoose.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../http/device-dashboard/net.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../http/device-dashboard/net.h
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../http/device-dashboard/packed_fs.c
|
|
||||||
@@ -2,5 +2,5 @@
|
|||||||
nvs, data, nvs, , 0x6000
|
nvs, data, nvs, , 0x6000
|
||||||
otadata, data, ota, , 0x2000,
|
otadata, data, ota, , 0x2000,
|
||||||
phy_init, data, phy, , 0x1000,
|
phy_init, data, phy, , 0x1000,
|
||||||
ota_0, app, ota_0, , 948K,
|
ota_0, app, ota_0, , 1536K,
|
||||||
ota_1, app, ota_1, , 948K,
|
ota_1, app, ota_1, , 1536K,
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||||
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
set(IDF_TARGET esp32c6)
|
||||||
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
|
project(mongoose-minimal-802154)
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Environment setup: https://mongoose.ws/documentation/tutorials/tools/
|
||||||
|
# Build: ESP-IDF from the espressif/idf Docker image. Flash: esptool.
|
||||||
|
# Monitor: esputil, https://github.com/cpq/esputil
|
||||||
|
|
||||||
|
THISDIR = $(realpath $(CURDIR))
|
||||||
|
ROOTDIR = $(realpath $(CURDIR)/../../..)
|
||||||
|
DOCKER ?= docker run --rm -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf
|
||||||
|
PORT ?= /dev/cu.usbmodem2101
|
||||||
|
|
||||||
|
all build:
|
||||||
|
$(DOCKER) idf.py build
|
||||||
|
|
||||||
|
flash: build
|
||||||
|
cd build && esptool --chip esp32c6 -p $(PORT) -b 460800 write-flash @flash_args
|
||||||
|
|
||||||
|
monitor:
|
||||||
|
esputil -p $(PORT) monitor
|
||||||
|
|
||||||
|
menuconfig:
|
||||||
|
docker run --rm -it -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf idf.py menuconfig
|
||||||
|
|
||||||
|
clean:
|
||||||
|
test -d build && $(DOCKER) rm -rf build sdkconfig || true
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
idf_component_register(SRCS "main.c" "../../../../mongoose.c"
|
||||||
|
"../../../mqtt/mqtt-client/mongoose_mqtt.c"
|
||||||
|
INCLUDE_DIRS "." "../../../.."
|
||||||
|
REQUIRES ieee802154 esp_timer esp_hw_support
|
||||||
|
esp_driver_gpio app_update)
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
// Copyright (c) 2026 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
#include <driver/gpio.h>
|
||||||
|
#include <esp_heap_caps.h>
|
||||||
|
|
||||||
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
void mg_mqtt_init(struct mg_mgr *mgr); // From tutorials/mqtt/mqtt-client
|
||||||
|
void mg_mqtt_poll(struct mg_mgr *mgr);
|
||||||
|
|
||||||
|
// Seeed XIAO ESP32-C6
|
||||||
|
#define LED1 GPIO_NUM_15 // User LED, active low
|
||||||
|
#define RF_SW_PWR GPIO_NUM_3 // RF switch power, active low
|
||||||
|
#define RF_SW_SEL GPIO_NUM_14 // Antenna select: low built-in, high U.FL
|
||||||
|
|
||||||
|
// Self-signed P-256 certificate and key for the HTTPS listener. Created by:
|
||||||
|
// openssl ecparam -genkey -name prime256v1 -noout -out key.pem
|
||||||
|
// openssl req -x509 -key key.pem -out cert.pem -days 3650 -subj /CN=mg
|
||||||
|
#define TLS_CERT \
|
||||||
|
"-----BEGIN CERTIFICATE-----\n" \
|
||||||
|
"MIIBgjCCASegAwIBAgIUIJOq77Qn7lHbH6WseMemeqorR50wCgYIKoZIzj0EAwIw\n" \
|
||||||
|
"FjEUMBIGA1UEAwwLbnVjbGVvLWg3MjMwHhcNMjYxMDEyMTUwMjE5WhcNMzYxMDA5\n" \
|
||||||
|
"MTUwMjE5WjAWMRQwEgYDVQQDDAtudWNsZW8taDcyMzBZMBMGByqGSM49AgEGCCqG\n" \
|
||||||
|
"SM49AwEHA0IABB6IQJNmzrsdQL+2BnZYS7GYOaj7sz8f6cjBmz3n8D9Em19XodlU\n" \
|
||||||
|
"uy4ybIvyhr766lL0ERoTmMUivb/lGmzx9C+jUzBRMB0GA1UdDgQWBBQqxMuc4gj7\n" \
|
||||||
|
"MTMzreWlRMNI3QMqTzAfBgNVHSMEGDAWgBQqxMuc4gj7MTMzreWlRMNI3QMqTzAP\n" \
|
||||||
|
"BgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQDY4xaJpuxq6b3rHK3b\n" \
|
||||||
|
"lOIQyfkQX+orCRqQUkseplqpqwIhAKswZFNn+2zUNRRNY8+cHmdIwmdpsmxtoWYP\n" \
|
||||||
|
"BCjX++LI\n" \
|
||||||
|
"-----END CERTIFICATE-----\n"
|
||||||
|
|
||||||
|
#define TLS_KEY \
|
||||||
|
"-----BEGIN EC PRIVATE KEY-----\n" \
|
||||||
|
"MHcCAQEEIJ/GBl0TlD52q9PMg1KWaNIdXqp8b/HyIsABmBpqC+O1oAoGCCqGSM49\n" \
|
||||||
|
"AwEHoUQDQgAEHohAk2bOux1Av7YGdlhLsZg5qPuzPx/pyMGbPefwP0SbX1eh2VS7\n" \
|
||||||
|
"LjJsi/KGvvrqUvQRGhOYxSK9v+UabPH0Lw==\n" \
|
||||||
|
"-----END EC PRIVATE KEY-----\n"
|
||||||
|
|
||||||
|
// Power the XIAO RF switch and select the built-in antenna
|
||||||
|
static void antenna_init(void) {
|
||||||
|
gpio_set_direction(RF_SW_PWR, GPIO_MODE_OUTPUT);
|
||||||
|
gpio_set_level(RF_SW_PWR, 0);
|
||||||
|
gpio_set_direction(RF_SW_SEL, GPIO_MODE_OUTPUT);
|
||||||
|
gpio_set_level(RF_SW_SEL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Toggle the user LED twice a second
|
||||||
|
static void blink_task(void) {
|
||||||
|
static uint64_t blink_timer = 0;
|
||||||
|
static int on = 0;
|
||||||
|
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
|
||||||
|
gpio_set_level(LED1, on ^= 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||||
|
if (ev == MG_EV_ACCEPT && c->is_tls) { // Accepted on the https:// listener
|
||||||
|
struct mg_tls_opts opts = {.cert = mg_str(TLS_CERT),
|
||||||
|
.key = mg_str(TLS_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("/api/tick"), NULL)) {
|
||||||
|
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), mg_millis());
|
||||||
|
} else if (mg_match(hm->uri, mg_str("/api/crash"), NULL)) {
|
||||||
|
__asm volatile("unimp"); // Illegal instruction: ESP-IDF prints a panic
|
||||||
|
} else {
|
||||||
|
mg_http_reply(c, 200, "",
|
||||||
|
"Hi from Mongoose!\n"
|
||||||
|
"tick: %llu\n"
|
||||||
|
"ram free: %zu\n"
|
||||||
|
"ram min: %zu\n",
|
||||||
|
mg_millis(), heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
|
||||||
|
heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_main(void) {
|
||||||
|
struct mg_mgr mgr;
|
||||||
|
|
||||||
|
antenna_init();
|
||||||
|
gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
|
||||||
|
|
||||||
|
mg_mgr_init(&mgr); // Also starts the 802.15.4 driver
|
||||||
|
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
|
||||||
|
mg_http_listen(&mgr, "https://0.0.0.0", http_ev_handler, NULL);
|
||||||
|
mg_mqtt_init(&mgr);
|
||||||
|
|
||||||
|
MG_INFO(("Initialised. RAM free: %zu",
|
||||||
|
heap_caps_get_free_size(MALLOC_CAP_DEFAULT)));
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
mg_mgr_poll(&mgr, 0);
|
||||||
|
mg_mqtt_poll(&mgr);
|
||||||
|
blink_task();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <esp_mac.h>
|
||||||
|
|
||||||
|
// See https://mongoose.ws/docs/getting-started/build-options/
|
||||||
|
#define MG_ARCH MG_ARCH_ESP32
|
||||||
|
#define MG_TLS MG_TLS_BUILTIN
|
||||||
|
#define MQTT_CLIENT_ID "esp32c6" // For tutorials/mqtt/mqtt-client
|
||||||
|
|
||||||
|
#define MG_ENABLE_TCPIP 1 // Built-in TCP/IP stack, not lwIP
|
||||||
|
#define MG_ENABLE_DRIVER_ESP32_802154 1 // ESP32-C6 802.15.4 radio
|
||||||
|
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit
|
||||||
|
|
||||||
|
// Stable MAC, so the DHCP address stays the same across reboots: the factory
|
||||||
|
// MAC, locally administered
|
||||||
|
#define MG_SET_MAC_ADDRESS(mac) \
|
||||||
|
do { \
|
||||||
|
esp_read_mac(mac, ESP_MAC_BASE); \
|
||||||
|
(mac)[0] |= 2; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
// mdash.net device management service support
|
||||||
|
#define MG_ENABLE_MDASH 0
|
||||||
|
#define MG_MDASH_KEY "..." // Set this - mDash device token
|
||||||
|
#define MG_FIRMWARE_VERSION "esp32c6.1.0.0"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
CONFIG_IDF_TARGET="esp32c6"
|
||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||||
|
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
|
||||||
|
CONFIG_IEEE802154_ENABLED=y
|
||||||
|
# Built-in TLS handshakes need a larger stack
|
||||||
|
CONFIG_ESP_MAIN_TASK_STACK_SIZE=12288
|
||||||
|
# The main task busy-loops the mesh, so the idle task does not run
|
||||||
|
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
|
||||||
@@ -17,6 +17,4 @@ flash:
|
|||||||
cd build && esptool --chip $(ARCH) --baud 460800 --port $(PORT) write-flash @flash_args
|
cd build && esptool --chip $(ARCH) --baud 460800 --port $(PORT) write-flash @flash_args
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
test -d build && (rm -rf build sdkconfig || true)
|
test -d build && $(DOCKER) rm -rf build sdkconfig || true
|
||||||
|
|
||||||
.PHONY: build
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
#define MG_ARCH MG_ARCH_ESP32
|
#define MG_ARCH MG_ARCH_ESP32
|
||||||
|
#define MG_OTA MG_OTA_ESP32
|
||||||
|
|
||||||
// Crash reports and mem usage stats
|
// Crash reports and mem usage stats
|
||||||
// Make sure to add to sdkconfig.defaults the following:
|
// Make sure to add to sdkconfig.defaults the following:
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
# Name, Type, SubType, Offset, Size, Flags
|
# Name, Type, SubType, Offset, Size, Flags
|
||||||
nvs, data, nvs, , 0x6000
|
nvs, data, nvs, , 0x6000
|
||||||
|
otadata, data, ota, , 0x2000
|
||||||
phy_init, data, phy, , 0x1000
|
phy_init, data, phy, , 0x1000
|
||||||
factory, app, factory, , 1200K
|
ota_0, app, ota_0, , 1536K
|
||||||
|
ota_1, app, ota_1, , 1536K
|
||||||
coredump, data, coredump,, 128K
|
coredump, data, coredump,, 128K
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||||
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
||||||
|
|||||||
@@ -8,9 +8,15 @@ all: example
|
|||||||
example:
|
example:
|
||||||
true
|
true
|
||||||
|
|
||||||
build: Makefile $(wildcard main/*)
|
build: Makefile $(wildcard main/*) main/net.c main/packed_fs.c
|
||||||
docker run --rm $(DA) -v $(MNT):$(MNT) -w $(CWD) espressif/idf idf.py $(CMD)
|
docker run --rm $(DA) -v $(MNT):$(MNT) -w $(CWD) espressif/idf idf.py $(CMD)
|
||||||
|
|
||||||
|
main/net.c: ../../http/uart-bridge/net.c
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
main/packed_fs.c: ../../http/uart-bridge/packed_fs.c
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
flash:
|
flash:
|
||||||
flash: CMD = flash
|
flash: CMD = flash
|
||||||
flash: DA = --device $(PORT)
|
flash: DA = --device $(PORT)
|
||||||
@@ -29,4 +35,4 @@ flash2: bridge.hex
|
|||||||
esputil -p $(PORT) monitor
|
esputil -p $(PORT) monitor
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf build
|
rm -rf build main/net.c main/packed_fs.c
|
||||||
Loaded 100 of 224 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user