mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-24 20:21:49 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bae9e53f00 | ||
|
|
9f4e6a25bd | ||
|
|
4f5997a8b4 | ||
|
|
542f7b6c2f | ||
|
|
65a6582d96 | ||
|
|
071cd50ef9 | ||
|
|
8d616998fa | ||
|
|
255b62572c | ||
|
|
bdf0de79c0 | ||
|
|
5e7edde7e5 | ||
|
|
ecedc31e34 | ||
|
|
8996d96443 | ||
|
|
2cf96bcb19 | ||
|
|
93f2939655 | ||
|
|
277741ca53 | ||
|
|
9d0b015217 | ||
|
|
5aa320658a | ||
|
|
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 | ||
|
|
d47cfa183d | ||
|
|
0905b93b97 | ||
|
|
65d15bb002 | ||
|
|
c77e578a19 | ||
|
|
080159a50e | ||
|
|
25294a5ecc | ||
|
|
9e9693db6f | ||
|
|
b47bd89cbd | ||
|
|
49cd3153ce | ||
|
|
c1f8dafc72 | ||
|
|
7d77d8f3d7 | ||
|
|
0ec2a0252b | ||
|
|
c01a1b613f | ||
|
|
73950e863a | ||
|
|
b4ca50b3c6 | ||
|
|
069024b41c | ||
|
|
706f4787e2 | ||
|
|
4d8831f2b6 | ||
|
|
5b6c08acfb | ||
|
|
528be76a23 | ||
|
|
b6ba6a56d9 | ||
|
|
1efc211a51 | ||
|
|
ca5ca3403c | ||
|
|
7a86c401b9 | ||
|
|
52cc8ef1d3 | ||
|
|
b975a8e761 | ||
|
|
2d7e44201d | ||
|
|
b415c23bc5 | ||
|
|
cba6c4f8df | ||
|
|
9d6d064b39 | ||
|
|
de037d6ef9 | ||
|
|
e7c8aa017d | ||
|
|
fe8ebced03 | ||
|
|
62cd82df81 | ||
|
|
e2408a2cfc | ||
|
|
da33868409 | ||
|
|
aaf202ba2f | ||
|
|
5c7b7b68bc | ||
|
|
28f88f016c | ||
|
|
e8bb01b547 | ||
|
|
324f59abc5 | ||
|
|
3eb33ab349 | ||
|
|
66eb0384be | ||
|
|
1f276ba70a | ||
|
|
1a9bc9d3f3 | ||
|
|
d39519b92f | ||
|
|
3e11429cac | ||
|
|
9337a78cd0 | ||
|
|
d99cc0959b | ||
|
|
02b5504e60 | ||
|
|
822c6a9489 | ||
|
|
03c022080f | ||
|
|
84198c7e70 | ||
|
|
f3d531938c | ||
|
|
b6975ab08f | ||
|
|
66afa5e551 | ||
|
|
fc79fd0be5 | ||
|
|
7886a73807 | ||
|
|
ccc953318a | ||
|
|
684ae27dc2 | ||
|
|
fc71bc0588 | ||
|
|
08305b9000 | ||
|
|
69e8c0a92f | ||
|
|
8eceacc0d3 | ||
|
|
b6df410570 | ||
|
|
26b971b4a8 | ||
|
|
b766c65615 | ||
|
|
8296b88fb6 | ||
|
|
a5efbf0592 | ||
|
|
37c98dd893 | ||
|
|
770a4ff27e | ||
|
|
b6560306ba | ||
|
|
5b39c93478 | ||
|
|
086008b716 | ||
|
|
35eb50a1e6 | ||
|
|
431e35ae8c | ||
|
|
25e2a99d8e | ||
|
|
88b01ef6f0 | ||
|
|
bd8ee86f9e | ||
|
|
ef96f7a28c | ||
|
|
03f240a244 | ||
|
|
c775f31ca7 | ||
|
|
1ca4fd1fef | ||
|
|
22a94c0906 | ||
|
|
a6b6a0d779 | ||
|
|
38c326644f | ||
|
|
e6e7a690a7 | ||
|
|
c76f092ba1 | ||
|
|
60840d0d89 | ||
|
|
298ee549da | ||
|
|
5e4ae64e1b | ||
|
|
c5778aa608 | ||
|
|
6d32a7c238 | ||
|
|
497a3f5e73 | ||
|
|
10e67fdf2b | ||
|
|
2f2a5f2f02 | ||
|
|
aa21ed443a | ||
|
|
10adde595c | ||
|
|
c957f29f23 | ||
|
|
37f3ed5ba4 | ||
|
|
b68da1548b | ||
|
|
a9a94a6904 | ||
|
|
3a4660cd40 | ||
|
|
fc478a70f7 | ||
|
|
a6b5565e9d | ||
|
|
e7113408a9 | ||
|
|
fdf12844e3 | ||
|
|
bc5afa4519 | ||
|
|
a4607933a4 | ||
|
|
fd7db76324 | ||
|
|
b1e6cb320f | ||
|
|
2b9d805d84 | ||
|
|
a9a843d6e2 | ||
|
|
aae179177c | ||
|
|
f347dbf2b5 | ||
|
|
300011a882 | ||
|
|
eda9a5726c | ||
|
|
16a5a7d9fb | ||
|
|
69c3bb8d12 | ||
|
|
8fc5651044 | ||
|
|
338756d0b0 | ||
|
|
c30afffcee | ||
|
|
76c0953210 | ||
|
|
381e31198b | ||
|
|
28ba381bd9 | ||
|
|
567963b034 | ||
|
|
7d3fe4f9cb | ||
|
|
c8bf39dcd8 | ||
|
|
393c811f31 | ||
|
|
be40827e17 | ||
|
|
ad45248327 | ||
|
|
c2d95cc19b | ||
|
|
4aa0a243f9 | ||
|
|
65a7996ef9 | ||
|
|
5879c3cbf2 | ||
|
|
cf9d56be79 | ||
|
|
da82df2041 | ||
|
|
954eb07b6d | ||
|
|
d7e708b9bc | ||
|
|
ce35cbf586 | ||
|
|
3aa5f2a1fd | ||
|
|
1b0906510b | ||
|
|
8a116d9289 | ||
|
|
71e0176944 | ||
|
|
bba5a0ca3a | ||
|
|
9c388b5f19 | ||
|
|
8afa940acf | ||
|
|
df1a409b21 | ||
|
|
7ec403341c | ||
|
|
1f2b14bb66 | ||
|
|
8e0c765c2c | ||
|
|
883e0a981f | ||
|
|
ff1fb5c745 | ||
|
|
795f16d5fb | ||
|
|
51773ecea3 | ||
|
|
2834aa57ee | ||
|
|
e2b5d7175f | ||
|
|
a419e16f26 | ||
|
|
789372c5b3 | ||
|
|
81f6acbce7 | ||
|
|
7fa96f3a38 | ||
|
|
876cf5605f | ||
|
|
8df146b1b8 | ||
|
|
f62f3545b4 | ||
|
|
4d6fa4b91c | ||
|
|
1be11a66e3 | ||
|
|
d1060d247b | ||
|
|
002c9df3de | ||
|
|
fa34b7439a | ||
|
|
60c75a22b8 | ||
|
|
bbee8ae61f | ||
|
|
9173c8dfda | ||
|
|
b93cbbeb13 | ||
|
|
aa1dfe8000 | ||
|
|
e8a82d1821 | ||
|
|
201a81a315 | ||
|
|
aae31aba9c | ||
|
|
29aa53cebd | ||
|
|
5d4fffbb14 | ||
|
|
ed5ef09d1c | ||
|
|
1e99974b83 | ||
|
|
6814b44b03 | ||
|
|
7e9ddc2d09 | ||
|
|
c13d5d7a56 | ||
|
|
555c5a76be | ||
|
|
863aea34c4 | ||
|
|
830f6c589b | ||
|
|
cc64c41737 | ||
|
|
4660f2ca0f | ||
|
|
ffcad7ee47 | ||
|
|
09a97b915a | ||
|
|
35a9242b67 | ||
|
|
5c34018899 | ||
|
|
c857ddae57 | ||
|
|
830acc19ee | ||
|
|
f87d439710 | ||
|
|
86adf12a77 | ||
|
|
a561f3e096 | ||
|
|
2b0a5e9701 | ||
|
|
39a3478429 | ||
|
|
7fccc2b75f | ||
|
|
8346a60971 | ||
|
|
140fa6cebf | ||
|
|
cd05b162a8 | ||
|
|
3fde04137c | ||
|
|
5591b7d131 | ||
|
|
9ddac8faa7 | ||
|
|
9123330f72 | ||
|
|
b938728c2a | ||
|
|
fab922230c | ||
|
|
90418ffb1b | ||
|
|
5a317e81d9 | ||
|
|
7e8c3a59e2 | ||
|
|
6093ac12a0 | ||
|
|
e7cfaa70c7 | ||
|
|
897d26edfb | ||
|
|
6b5d358ede | ||
|
|
0d97280af4 | ||
|
|
afc84b7c92 | ||
|
|
66bf5952a4 | ||
|
|
f002c30cbb | ||
|
|
07e580d956 | ||
|
|
53d2356472 | ||
|
|
e312219e80 | ||
|
|
2aff813795 | ||
|
|
9c4506763b | ||
|
|
ec3b9e7c4f | ||
|
|
ddd9651f91 | ||
|
|
304ea2a649 | ||
|
|
83bac34094 | ||
|
|
02bdbb9ed6 | ||
|
|
84553d06a0 | ||
|
|
de7cd48a79 | ||
|
|
5df2445bc9 | ||
|
|
90890e4c63 | ||
|
|
a6deed5868 | ||
|
|
5cdb25f05a | ||
|
|
d8764746a4 | ||
|
|
fa4a2563e5 | ||
|
|
9ccfc02a86 | ||
|
|
f58f29b9a0 | ||
|
|
282ae0d968 | ||
|
|
9c6b4edd37 | ||
|
|
735038c98c | ||
|
|
2696c1ff94 | ||
|
|
9f16806a22 | ||
|
|
8d1b9047b6 | ||
|
|
2988bc9df3 | ||
|
|
5e733a1461 | ||
|
|
34813493a4 | ||
|
|
f220223947 | ||
|
|
758e301f0c | ||
|
|
7fa830fc0c | ||
|
|
fc699a0b0b | ||
|
|
4e3e7ac4b2 | ||
|
|
2838c85342 | ||
|
|
bc17f1667b | ||
|
|
309cb1006f | ||
|
|
501c5a4bcd | ||
|
|
765bb237b7 | ||
|
|
b0f5e9cec4 | ||
|
|
125b79bd70 | ||
|
|
7821fb9ab4 | ||
|
|
03be16803c | ||
|
|
3c11521446 | ||
|
|
4faa3d5bf1 | ||
|
|
bfdc14ce0a | ||
|
|
ac9ebdfb1a | ||
|
|
85d77ec3ac | ||
|
|
9b7d318df5 | ||
|
|
6cbd810206 | ||
|
|
771f5f2ed9 | ||
|
|
6b8d8b5caa | ||
|
|
ba942dbd7a | ||
|
|
2db7119080 | ||
|
|
6a3599682b | ||
|
|
77e0faf020 | ||
|
|
08e01cb312 | ||
|
|
d8fa55e1d6 | ||
|
|
61638caeae | ||
|
|
f78cb1b02f | ||
|
|
f8e785f56b | ||
|
|
e473f2189c | ||
|
|
66eac28c20 | ||
|
|
cdd957572e | ||
|
|
af7eaaa9b1 | ||
|
|
8d7bbda48d | ||
|
|
17a25373db | ||
|
|
b86f255407 | ||
|
|
cb57182ee0 | ||
|
|
72ff0dfb07 | ||
|
|
dc52d8b1dc | ||
|
|
e2e2db8900 | ||
|
|
b6a353cae9 | ||
|
|
cc7c6d2d18 | ||
|
|
7a2a3fc136 | ||
|
|
2ec27994f4 | ||
|
|
bc69625a5e | ||
|
|
a8b4ae95db | ||
|
|
f52e4f4bb0 | ||
|
|
0aa25dbd18 | ||
|
|
72a0787161 | ||
|
|
37234ad26f | ||
|
|
96050c2587 | ||
|
|
09254c9e64 | ||
|
|
a43599c081 | ||
|
|
f391c4eea0 | ||
|
|
6709c028aa | ||
|
|
f84598f66a | ||
|
|
02625d3bb6 | ||
|
|
957b9a991c | ||
|
|
875c0fcd4f | ||
|
|
ad269fa4d5 | ||
|
|
9448625662 | ||
|
|
ba2abbc444 | ||
|
|
a865890a09 | ||
|
|
ad2ffa8e45 | ||
|
|
b424eb79cf | ||
|
|
ac8fe85478 | ||
|
|
38b6b13547 | ||
|
|
9bc4d9741e | ||
|
|
cee80a8391 | ||
|
|
9d3926a25a | ||
|
|
28cd3439dd | ||
|
|
6fcf32f203 | ||
|
|
9879f4d715 | ||
|
|
8fb3aa26cd | ||
|
|
dfbbe5fa8b | ||
|
|
751f14742d | ||
|
|
8bb65c6492 | ||
|
|
f8816d67c3 | ||
|
|
d897b32c64 | ||
|
|
f055e45755 | ||
|
|
5bf953cb4a | ||
|
|
8e7f5ab00e | ||
|
|
e5e13ff156 | ||
|
|
fdcefb087e | ||
|
|
699067ff58 | ||
|
|
ed19d5290e | ||
|
|
848ef126a3 | ||
|
|
80670e9ebc | ||
|
|
b607a4209a | ||
|
|
b3ab9c618e | ||
|
|
8c0b6e335e | ||
|
|
d8e629a048 |
No files matched your search
@@ -1,16 +1,42 @@
|
||||
name: Claude Full Codebase Security Scan
|
||||
name: Claude Codebase Security Scan
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
prompt_path:
|
||||
description: "Path to the repo prompt file Claude should read"
|
||||
required: true
|
||||
default: "resources/specs/claude-security-scan.md"
|
||||
target_ref:
|
||||
description: "Branch, tag, or SHA to scan"
|
||||
required: false
|
||||
default: ""
|
||||
common_misc:
|
||||
description: "Run common/misc security scan"
|
||||
required: false
|
||||
type: boolean
|
||||
default: true
|
||||
mongoose_core:
|
||||
description: "Run Mongoose core security scan"
|
||||
required: false
|
||||
type: boolean
|
||||
default: true
|
||||
web_http_auth:
|
||||
description: "Run web/HTTP/auth security scan"
|
||||
required: false
|
||||
type: boolean
|
||||
default: true
|
||||
tls:
|
||||
description: "Run TLS security scan"
|
||||
required: false
|
||||
type: boolean
|
||||
default: true
|
||||
net_builtin:
|
||||
description: "Run built-in network security scan"
|
||||
required: false
|
||||
type: boolean
|
||||
default: true
|
||||
protocols:
|
||||
description: "Run protocol security scan"
|
||||
required: false
|
||||
type: boolean
|
||||
default: true
|
||||
#schedule:
|
||||
# Weekly scan: Sunday 02:00 UTC
|
||||
# - cron: "0 2 * * 0"
|
||||
@@ -20,73 +46,163 @@ permissions:
|
||||
id-token: write
|
||||
|
||||
concurrency:
|
||||
group: claude-full-codebase-security-scan-${{ github.ref }}
|
||||
group: claude-security-scans
|
||||
cancel-in-progress: false
|
||||
queue: max
|
||||
|
||||
jobs:
|
||||
scan:
|
||||
name: Full codebase security scan
|
||||
prepare:
|
||||
name: Prepare selected scan matrix
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
|
||||
env:
|
||||
DEFAULT_PROMPT_PATH: "resources/specs/claude-security-scan.md"
|
||||
outputs:
|
||||
target_ref: ${{ steps.plan.outputs.target_ref }}
|
||||
matrix: ${{ steps.plan.outputs.matrix }}
|
||||
has_scans: ${{ steps.plan.outputs.has_scans }}
|
||||
|
||||
steps:
|
||||
- name: Resolve scan inputs
|
||||
id: vars
|
||||
- name: Resolve scan inputs and build matrix
|
||||
id: plan
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
PROMPT_PATH="${{ inputs.prompt_path }}"
|
||||
TARGET_REF="${{ inputs.target_ref }}"
|
||||
RUN_COMMON_MISC="${{ inputs.common_misc }}"
|
||||
RUN_MONGOOSE_CORE="${{ inputs.mongoose_core }}"
|
||||
RUN_WEB_HTTP_AUTH="${{ inputs.web_http_auth }}"
|
||||
RUN_TLS="${{ inputs.tls }}"
|
||||
RUN_NET_BUILTIN="${{ inputs.net_builtin }}"
|
||||
RUN_PROTOCOLS="${{ inputs.protocols }}"
|
||||
else
|
||||
PROMPT_PATH="${DEFAULT_PROMPT_PATH}"
|
||||
TARGET_REF=""
|
||||
RUN_COMMON_MISC="true"
|
||||
RUN_MONGOOSE_CORE="true"
|
||||
RUN_WEB_HTTP_AUTH="true"
|
||||
RUN_TLS="true"
|
||||
RUN_NET_BUILTIN="true"
|
||||
RUN_PROTOCOLS="true"
|
||||
fi
|
||||
|
||||
if [ -z "$TARGET_REF" ]; then
|
||||
TARGET_REF="${{ github.ref }}"
|
||||
fi
|
||||
|
||||
echo "prompt_path=$PROMPT_PATH" >> "$GITHUB_OUTPUT"
|
||||
echo "target_ref=$TARGET_REF" >> "$GITHUB_OUTPUT"
|
||||
MATRIX="$(jq -cn \
|
||||
--arg common_misc "$RUN_COMMON_MISC" \
|
||||
--arg mongoose_core "$RUN_MONGOOSE_CORE" \
|
||||
--arg web_http_auth "$RUN_WEB_HTTP_AUTH" \
|
||||
--arg tls "$RUN_TLS" \
|
||||
--arg net_builtin "$RUN_NET_BUILTIN" \
|
||||
--arg protocols "$RUN_PROTOCOLS" \
|
||||
'{
|
||||
include: ([
|
||||
{
|
||||
enabled: ($common_misc == "true"),
|
||||
area: "common_misc",
|
||||
display_name: "Common / Misc",
|
||||
area_prompt: "resources/specs/areas/common_misc.md"
|
||||
},
|
||||
{
|
||||
enabled: ($mongoose_core == "true"),
|
||||
area: "mongoose_core",
|
||||
display_name: "Mongoose Core",
|
||||
area_prompt: "resources/specs/areas/mongoose_core.md"
|
||||
},
|
||||
{
|
||||
enabled: ($web_http_auth == "true"),
|
||||
area: "web_http_auth",
|
||||
display_name: "Web / HTTP / Auth",
|
||||
area_prompt: "resources/specs/areas/web_http_auth.md"
|
||||
},
|
||||
{
|
||||
enabled: ($tls == "true"),
|
||||
area: "tls",
|
||||
display_name: "TLS",
|
||||
area_prompt: "resources/specs/areas/tls.md"
|
||||
},
|
||||
{
|
||||
enabled: ($net_builtin == "true"),
|
||||
area: "net_builtin",
|
||||
display_name: "Built-In Network",
|
||||
area_prompt: "resources/specs/areas/net_builtin.md"
|
||||
},
|
||||
{
|
||||
enabled: ($protocols == "true"),
|
||||
area: "protocols",
|
||||
display_name: "Protocols",
|
||||
area_prompt: "resources/specs/areas/protocols.md"
|
||||
}
|
||||
] | map(select(.enabled)) | map(del(.enabled)))
|
||||
}'
|
||||
)"
|
||||
|
||||
HAS_SCANS="$(jq -r '.include | length > 0' <<< "$MATRIX")"
|
||||
|
||||
echo "target_ref=$TARGET_REF" >> "$GITHUB_OUTPUT"
|
||||
echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT"
|
||||
echo "has_scans=$HAS_SCANS" >> "$GITHUB_OUTPUT"
|
||||
|
||||
scan:
|
||||
name: ${{ matrix.display_name }} security scan
|
||||
needs: prepare
|
||||
if: needs.prepare.outputs.has_scans == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1
|
||||
matrix: ${{ fromJSON(needs.prepare.outputs.matrix) }}
|
||||
|
||||
env:
|
||||
MASTER_PROMPT_PATH: "resources/specs/claude-security-scan.md"
|
||||
FILTERING_PROMPT_PATH: "resources/specs/claude-false-positive-filtering.md"
|
||||
SCAN_AREA: ${{ matrix.area }}
|
||||
AREA_PROMPT_PATH: ${{ matrix.area_prompt }}
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v6
|
||||
with:
|
||||
ref: ${{ steps.vars.outputs.target_ref }}
|
||||
ref: ${{ needs.prepare.outputs.target_ref }}
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Verify prompt file exists
|
||||
- name: Verify required prompt files exist
|
||||
shell: bash
|
||||
run: |
|
||||
test -f "${{ steps.vars.outputs.prompt_path }}" || {
|
||||
echo "Prompt file not found: ${{ steps.vars.outputs.prompt_path }}"
|
||||
for prompt_file in "$MASTER_PROMPT_PATH" "$AREA_PROMPT_PATH" "$FILTERING_PROMPT_PATH"; do
|
||||
test -f "$prompt_file" || {
|
||||
echo "Required prompt file not found: $prompt_file"
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
|
||||
- name: Run Claude full codebase security scan
|
||||
- name: Run Claude ${{ matrix.display_name }} security scan
|
||||
id: claude_scan
|
||||
uses: anthropics/claude-code-action@v1
|
||||
timeout-minutes: 30
|
||||
with:
|
||||
anthropic_api_key: ${{ secrets.CLAUDE_API_KEY }}
|
||||
|
||||
prompt: |
|
||||
REPO: ${{ github.repository }}
|
||||
REF: ${{ steps.vars.outputs.target_ref }}
|
||||
PROMPT_FILE: ${{ steps.vars.outputs.prompt_path }}
|
||||
REF: ${{ needs.prepare.outputs.target_ref }}
|
||||
MASTER_PROMPT_FILE: ${{ env.MASTER_PROMPT_PATH }}
|
||||
SCAN_AREA: ${{ matrix.area }}
|
||||
AREA_PROMPT_FILE: ${{ matrix.area_prompt }}
|
||||
FILTERING_PROMPT_FILE: ${{ env.FILTERING_PROMPT_PATH }}
|
||||
|
||||
You are running a full-codebase security scan in GitHub Actions.
|
||||
You are running a focused security scan for the assigned area in GitHub Actions.
|
||||
|
||||
First, read the main prompt file at:
|
||||
${{ steps.vars.outputs.prompt_path }}
|
||||
First, read the master prompt at ${{ env.MASTER_PROMPT_PATH }}.
|
||||
Then read the assigned area prompt at ${{ matrix.area_prompt }}.
|
||||
Read the false-positive filtering prompt at ${{ env.FILTERING_PROMPT_PATH }} as required by the master prompt.
|
||||
|
||||
Treat that file as the authoritative security-review instruction set. You will also
|
||||
read and analyze additional prompt files mentioned in the main prompt file by their absolute paths in the repo,
|
||||
as per the instructions found in it.
|
||||
Perform the security review for the ${{ matrix.area }} area. Follow relevant supporting code when necessary, but do not independently scan unrelated areas.
|
||||
Return only the JSON required by the master prompt.
|
||||
|
||||
Scan the checked-out repository as a whole codebase.
|
||||
Do not modify files.
|
||||
Do not create commits.
|
||||
Do not open pull requests.
|
||||
@@ -94,9 +210,8 @@ jobs:
|
||||
Do not create GitHub issues.
|
||||
|
||||
claude_args: |
|
||||
--max-turns 20
|
||||
--allowedTools "Read,Glob,Grep,Bash(git:*),Bash(find:*),Bash(grep:*),Bash(sed:*),Bash(cat:*)"
|
||||
--model claude-opus-4-8
|
||||
--model claude-opus-5-5
|
||||
|
||||
- name: Prepare private security scan outputs
|
||||
if: always() && steps.claude_scan.outputs.execution_file != ''
|
||||
@@ -106,8 +221,8 @@ jobs:
|
||||
set -u
|
||||
mkdir -p private-scan-results
|
||||
TODAY="$(date -u +%Y%m%d)"
|
||||
EXECUTION_OUTPUT_FILE="claude-execution-output_${TODAY}.json"
|
||||
FINDINGS_FILE="security-findings_${TODAY}.json"
|
||||
EXECUTION_OUTPUT_FILE="claude-execution-output_${SCAN_AREA}_${TODAY}.json"
|
||||
FINDINGS_FILE="security-findings_${SCAN_AREA}_${TODAY}.json"
|
||||
|
||||
HAS_EXECUTION_OUTPUT="false"
|
||||
HAS_JSON_FINDINGS="false"
|
||||
@@ -177,5 +292,5 @@ jobs:
|
||||
exit 0
|
||||
fi
|
||||
|
||||
git commit -m "Add security scan results ${{ steps.prepare_outputs.outputs.date_suffix }}"
|
||||
git commit -m "Add ${SCAN_AREA} security scan results ${{ steps.prepare_outputs.outputs.date_suffix }}"
|
||||
git push
|
||||
@@ -1,9 +1,6 @@
|
||||
name: Claude PR Security Review
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened]
|
||||
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
pr_number:
|
||||
@@ -22,7 +19,7 @@ permissions:
|
||||
issues: write
|
||||
|
||||
concurrency:
|
||||
group: claude-pr-security-review-${{ github.event.pull_request.number || inputs.pr_number }}
|
||||
group: claude-pr-security-review-${{ inputs.pr_number }}
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
@@ -33,7 +30,7 @@ jobs:
|
||||
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }}
|
||||
PR_NUMBER: ${{ inputs.pr_number }}
|
||||
|
||||
steps:
|
||||
- name: Resolve workflow variables
|
||||
@@ -111,4 +108,4 @@ jobs:
|
||||
|
||||
claude_args: |
|
||||
--allowedTools "Read,Bash(cat:*),Bash(gh pr diff:*),Bash(gh pr view:*),Bash(gh pr comment:*)"
|
||||
--model claude-opus-4-8
|
||||
--model claude-opus-5-5
|
||||
@@ -12,13 +12,99 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [fuzz, fuzz_tls]
|
||||
name: ${{ matrix.target }}
|
||||
ipv6: [0, 1] # fuzz code always feeds IPv6, so this fuzzes ignoring while going parallel in sync
|
||||
name: fuzz IPV6=${{ matrix.ipv6 }}
|
||||
env:
|
||||
TFLAGS: -DMG_ENABLE_IPV6=${{ matrix.ipv6 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: make -C test ${{ matrix.target }} ARGS="-max_total_time=14400"
|
||||
- name: Restore Combined Fuzzing Corpus
|
||||
uses: dawidd6/action-download-artifact@v21
|
||||
with:
|
||||
name: combined-fuzz-corpus
|
||||
path: test/corpus_dir
|
||||
branch: ${{ github.base_ref || github.ref_name }}
|
||||
search_artifacts: true
|
||||
if_no_artifact_found: warn
|
||||
- name: Run Matrix Fuzzers
|
||||
run: mkdir -p test/corpus_dir && make -C test fuzz ARGS="corpus_dir -seed=${{ github.run_id }} -max_total_time=14400"
|
||||
# Keep failures split by matrix leg so the offending input is easy to find.
|
||||
- name: Upload Fuzz Failure Artifacts
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: fuzz-failure-ipv6-${{ matrix.ipv6 }}
|
||||
path: |
|
||||
test/corpus_dir
|
||||
test/crash-*
|
||||
test/leak-*
|
||||
test/oom-*
|
||||
test/timeout-*
|
||||
if-no-files-found: ignore
|
||||
retention-days: 14
|
||||
# v4 artifacts are immutable, so each parallel leg uploads its own corpus.
|
||||
- name: Upload Corpus Shard
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: fuzz-corpus-ipv6-${{ matrix.ipv6 }}
|
||||
path: test/corpus_dir
|
||||
retention-days: 14
|
||||
|
||||
# as we're not getting access to test units causing a problem, convert from the log:
|
||||
merge_fuzz_corpus:
|
||||
runs-on: ubuntu-latest
|
||||
needs: fuzz
|
||||
name: merge fuzz corpus
|
||||
steps:
|
||||
# Merge both parallel fuzzing corpuses into the shared seed for next run.
|
||||
- name: Merge Corpus Shards
|
||||
uses: actions/upload-artifact/merge@v7
|
||||
with:
|
||||
name: combined-fuzz-corpus
|
||||
pattern: fuzz-corpus-ipv6-*
|
||||
delete-merged: true
|
||||
retention-days: 14
|
||||
# see NOTE
|
||||
|
||||
fuzz_tls:
|
||||
runs-on: ubuntu-latest
|
||||
name: fuzz_tls
|
||||
env:
|
||||
TFLAGS: -DMG_ENABLE_IPV6=0
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- name: Restore Fuzzing Corpus
|
||||
uses: actions/cache/restore@v6
|
||||
with:
|
||||
path: test/corpus_dir
|
||||
key: fuzz-corpus-${{ github.head_ref || github.ref_name }}-${{ github.run_id }}
|
||||
restore-keys: |
|
||||
fuzz-corpus-${{ github.head_ref || github.ref_name }}-
|
||||
fuzz-corpus-main-
|
||||
fuzz-corpus-
|
||||
- name: Run Fuzzer
|
||||
run: mkdir -p test/corpus_dir && make -C test fuzz_tls ARGS="corpus_dir -seed=${{ github.run_id }} -max_total_time=14400"
|
||||
- name: Upload Fuzz TLS Failure Artifacts
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: fuzz-tls-failure-artifacts
|
||||
path: |
|
||||
test/corpus_dir
|
||||
test/crash-*
|
||||
test/leak-*
|
||||
test/oom-*
|
||||
test/timeout-*
|
||||
if-no-files-found: ignore
|
||||
retention-days: 14
|
||||
- name: Save Updated Corpus to Cache
|
||||
uses: actions/cache/save@v6
|
||||
with:
|
||||
path: test/corpus_dir
|
||||
key: fuzz-corpus-${{ github.head_ref || github.ref_name }}-${{ github.run_id }}
|
||||
|
||||
# NOTE
|
||||
# In case we can't get access to test units causing a problem, convert from the log:
|
||||
# base64 -d > dataofdeath
|
||||
# then paste the base64 data, enter, Ctrl-D; maybe check with hexdump -C dataofdeath
|
||||
+401
-105
@@ -1,7 +1,7 @@
|
||||
name: Full build
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 22 * * *' # run at 10 PM UTC
|
||||
- cron: '17 22 * * *' # run at 10:17 PM UTC
|
||||
# Allow manual runs
|
||||
workflow_dispatch:
|
||||
env:
|
||||
@@ -51,6 +51,25 @@ jobs:
|
||||
cat log
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/linux_${{ matrix.target }}_${{ matrix.cc }}_${{ matrix.ssl }}_${{ matrix.select }}_$(date +"%Y%m%d").json"
|
||||
|
||||
c89:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [diabish, ppc_diabish]
|
||||
ssl: ["", BUILTIN]
|
||||
name: C89 ${{ matrix.target }} SSL=${{ matrix.ssl }}
|
||||
env:
|
||||
SSL: ${{ matrix.ssl }}
|
||||
TFLAGS: -DMG_ENABLE_POLL=0 -DMG_ENABLE_EPOLL=0 -DMG_ENABLE_CHACHA20=0 -DMQTT_LOCALHOST -DNO_ZU_TEST
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: | # GH runners seem to overflow the 32-bit FS for the QEMU PPC (works for me in RHEL9)
|
||||
if [[ "${{ matrix.target }}" = ppc_* ]]; then export TFLAGS="$TFLAGS -D_FILE_OFFSET_BITS=64"; fi
|
||||
sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }}
|
||||
|
||||
mip:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
@@ -59,10 +78,6 @@ jobs:
|
||||
cc: [gcc, clang, g++, clang++]
|
||||
target: [mip_test, mip_tap_test]
|
||||
ssl: ["", BUILTIN, MBEDTLS, OPENSSL, WOLFSSL]
|
||||
# #3226: built-in TCP is currently not working with WolfSSL (builds fine)
|
||||
exclude:
|
||||
- ssl: WOLFSSL
|
||||
target: mip_tap_test
|
||||
name: ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }}
|
||||
env:
|
||||
CC: ${{ matrix.cc }}
|
||||
@@ -75,6 +90,26 @@ jobs:
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
# WolfSSL version (5.6.6) in current GH runner (Ubuntu 24.04) is broken for EC
|
||||
# https://github.com/wolfSSL/wolfssl/pull/10065
|
||||
- name: Build non-broken WolfSSL version
|
||||
if: matrix.ssl == 'WOLFSSL'
|
||||
run: |
|
||||
sudo apt -y update
|
||||
sudo apt -y install autoconf automake libtool
|
||||
git clone --depth 1 --branch v5.9.1-stable https://github.com/wolfSSL/wolfssl
|
||||
cd wolfssl
|
||||
./autogen.sh
|
||||
./configure --prefix="$GITHUB_WORKSPACE/wolfssl-install" --enable-all
|
||||
make -j"$(nproc)"
|
||||
make install
|
||||
echo "WOLFSSL=$GITHUB_WORKSPACE/wolfssl-install" >> "$GITHUB_ENV"
|
||||
echo "LD_LIBRARY_PATH=$GITHUB_WORKSPACE/wolfssl-install/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" >> "$GITHUB_ENV"
|
||||
if [ "${{ matrix.cc }}" = "g++" ] || [ "${{ matrix.cc }}" = "clang++" ]; then
|
||||
echo "TFLAGS=$TFLAGS -DWOLFSSL_USE_OPTIONS_H -Wno-pedantic" >> "$GITHUB_ENV"
|
||||
else
|
||||
echo "TFLAGS=$TFLAGS -DWOLFSSL_USE_OPTIONS_H" >> "$GITHUB_ENV"
|
||||
fi
|
||||
- run: if [ "${{ matrix.target }}" == "mip_tap_test" ]; then source ./test/setup_ga_network.sh ; sed -i -e "s/127.0.0.1//" test/mosquitto.conf; cat test/mosquitto.conf; ./test/setup_mqtt_server.sh; fi && sudo apt -y update ; sudo apt -y install libmbedtls-dev libwolfssl-dev && make -C test ${{ matrix.target }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
@@ -82,20 +117,233 @@ jobs:
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/${{ matrix.target }}_${{ matrix.cc }}_${{ matrix.ssl }}_$(date +"%Y%m%d").json"
|
||||
|
||||
mip89:
|
||||
miprtx:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1 # no parallel runs, to minimize MQTT errors
|
||||
matrix:
|
||||
target: [mip_vc98]
|
||||
name: mip89 ${{ matrix.target }}
|
||||
ssl: ["", BUILTIN]
|
||||
name: MIP RTX TAP SSL=${{ matrix.ssl }}
|
||||
env:
|
||||
IPV6: 0
|
||||
RTX: 1
|
||||
SSL: ${{ matrix.ssl }}
|
||||
TFLAGS: -DMQTT_HOST -DIPV6_NOROUTING -DNO_ABORT
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: make -C test ${{ matrix.target }} IPV6=1
|
||||
s390:
|
||||
- run: |
|
||||
source ./test/setup_ga_network.sh ; sed -i -e "s/127.0.0.1//" test/mosquitto.conf ; ./test/setup_mqtt_server.sh
|
||||
make -C test mip_tap_test
|
||||
|
||||
mip89:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [mip_vc98, mip_diabish, mip_ppc_diabish]
|
||||
ipv6: [0, 1]
|
||||
name: MIP C89 ${{ matrix.target }} IPV6=${{ matrix.ipv6 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }} IPV6=${{ matrix.ipv6 }}
|
||||
mipbe:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [mip_s390, mip_port_s390, mip_ppc, mip_port_ppc, mip_mips, mip_port_mips]
|
||||
ipv6: [0, 1]
|
||||
ssl: ["", BUILTIN]
|
||||
name: MIP be ${{ matrix.target }} IPV6=${{ matrix.ipv6 }} SSL=${{ matrix.ssl }}
|
||||
env:
|
||||
TFLAGS: -DMQTT_HOST -DIPV6_NOROUTING
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
|
||||
if [[ "${{ matrix.target }}" == *port* ]]; then source ./test/setup_ga_network.sh ; export ENV="-e Tmp=. -e WINEDEBUG=-all -e HOST_IP -e HOST_IPV6 -e HOST_IFC" ; sed -i -e "s/127.0.0.1//" test/mosquitto.conf; cat test/mosquitto.conf; ./test/setup_mqtt_server.sh ; make -C test port_tap_bridge ; test/port_tap_bridge & sleep 2 ; fi
|
||||
make -C test ${{ matrix.target }} IPV6=${{ matrix.ipv6 }} SSL=${{ matrix.ssl }}
|
||||
|
||||
miprtxbe:
|
||||
runs-on: ubuntu-24.04
|
||||
name: MIP RTX be s390 port
|
||||
env:
|
||||
IPV6: 0
|
||||
RTX: 1
|
||||
TFLAGS: -DMQTT_HOST -DIPV6_NOROUTING -DNO_ABORT
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
|
||||
source ./test/setup_ga_network.sh ; export ENV="-e Tmp=. -e WINEDEBUG=-all -e HOST_IP -e HOST_IPV6 -e HOST_IFC -e RTX" ; sed -i -e "s/127.0.0.1//" test/mosquitto.conf ; ./test/setup_mqtt_server.sh ; make -C test port_tap_bridge ; test/port_tap_bridge & sleep 2
|
||||
make -C test mip_port_s390
|
||||
|
||||
l2:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
cc: [gcc, clang, g++, clang++]
|
||||
target: [eth_test, ppp_test]
|
||||
ipv6: [0, 1]
|
||||
name: L2 ${{ matrix.target }} CC=${{ matrix.cc }} IPV6=${{ matrix.ipv6 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
- run: make -C test ${{ matrix.target }} IPV6=${{ matrix.ipv6 }} CC=${{ matrix.cc }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
cat log
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/${{ matrix.target }}_${{ matrix.cc }}_${{ matrix.ipv6 }}_$(date +"%Y%m%d").json"
|
||||
l289:
|
||||
runs-on: ubuntu-24.04 # Ubuntu 26.04 Docker 29.4.2 breaks 32-bit Wine; switch at >= 29.4.3
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [eth_vc98, ppp_vc98]
|
||||
ipv6: [0, 1]
|
||||
name: L2 C89 ${{ matrix.target }} IPV6=${{ matrix.ipv6 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: make -C test ${{ matrix.target }} IPV6=${{ matrix.ipv6 }}
|
||||
l2be:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [eth_s390, ppp_s390]
|
||||
ipv6: [0, 1]
|
||||
name: L2 be ${{ matrix.target }} IPV6=${{ matrix.ipv6 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }} IPV6=${{ matrix.ipv6 }}
|
||||
pppoe_vlan: # run tests, build tests performed by the l2 job may not catch runtime alignment and protocol issues, etc.
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
cc: [gcc, clang, g++, clang++]
|
||||
ipv6: [0, 1]
|
||||
name: PPPoE over VLAN CC=${{ matrix.cc }} IPV6=${{ matrix.ipv6 }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
- run: |
|
||||
source ./test/setup_ga_network.sh
|
||||
make -C test pppoe_tap_test CC=${{ matrix.cc }} IPV6=${{ matrix.ipv6 }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
cat log
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/pppoe-vlan_${{ matrix.cc }}_${{ matrix.ipv6 }}_$(date +"%Y%m%d").json"
|
||||
|
||||
atcmd:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
cc: [gcc, clang, g++, clang++]
|
||||
name: ATcmd CC=${{ matrix.cc }}
|
||||
env:
|
||||
IPV6: 0 # makes no sense, shorten code
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
- run: make -C test atcmd_test CC=${{ matrix.cc }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
cat log
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/ATcmd_${{ matrix.cc }}_$(date +"%Y%m%d").json"
|
||||
atcmd89:
|
||||
runs-on: ubuntu-24.04 # Ubuntu 26.04 Docker 29.4.2 breaks 32-bit Wine; switch at >= 29.4.3
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [atcmd_vc98]
|
||||
name: ATcmd C89 ${{ matrix.target }}
|
||||
env:
|
||||
IPV6: 0 # makes no sense, shorten code
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: make -C test ${{ matrix.target }}
|
||||
atcmdbe:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [atcmd_s390]
|
||||
name: ATcmd be ${{ matrix.target }}
|
||||
env:
|
||||
IPV6: 0 # makes no sense, shorten code
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }}
|
||||
|
||||
bsd:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [bsd, bsd_nortos]
|
||||
cc: [gcc, clang, g++, clang++]
|
||||
name: bsd CC=${{ matrix.cc }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
- run: |
|
||||
source ./test/setup_ga_network.sh
|
||||
make -C test/bsd shm_tap_bridge
|
||||
test/bsd/shm_tap_bridge &
|
||||
sleep 2
|
||||
make -C test/bsd ${{ matrix.target }} CC=${{ matrix.cc }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
cat log
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/bsd_${{ matrix.cc }}_$(date +"%Y%m%d").json"
|
||||
|
||||
bsdbe:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [bsd_s390]
|
||||
name: bsd be ${{ matrix.target }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
|
||||
source ./test/setup_ga_network.sh
|
||||
make -C test port_tap_bridge
|
||||
test/port_tap_bridge &
|
||||
sleep 2
|
||||
make -C test/bsd ${{ matrix.target }}
|
||||
|
||||
s390:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -106,9 +354,9 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test s390 SSL=${{ matrix.ssl }} MULTIREC=NO
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test s390 SSL=${{ matrix.ssl }}
|
||||
armhf:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -119,7 +367,7 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test armhf SSL=${{ matrix.ssl }} MULTIREC=NO
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test armhf SSL=${{ matrix.ssl }}
|
||||
unamalgamated-mg_prefix:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
@@ -156,8 +404,6 @@ jobs:
|
||||
exclude:
|
||||
- ssl: MBEDTLS
|
||||
select: -DMG_ENABLE_POLL=0
|
||||
- ssl: MBEDTLS # see issue#2694
|
||||
select: -DMG_ENABLE_POLL=1
|
||||
- ssl: OPENSSL
|
||||
select: -DMG_ENABLE_POLL=0
|
||||
- ssl: WOLFSSL
|
||||
@@ -183,12 +429,12 @@ jobs:
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/macos_test_cc_${{ matrix.ssl }}_${{ matrix.select }}_$(date +"%Y%m%d").json"
|
||||
|
||||
windows:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04 # Ubuntu 26.04 Docker 29.4.2 breaks 32-bit Wine; switch at >= 29.4.3
|
||||
strategy:
|
||||
fail-fast: false
|
||||
max-parallel: 1 # no parallel runs, to minimize MQTT errors
|
||||
matrix:
|
||||
target: [vc98, vc17, vc22, mingw, mingw++]
|
||||
target: [vc98, vc17, vc22, mingw, mingw++] # current mdash/mingw image compiler does not have support for %zu; test disabled below
|
||||
ssl: ["", BUILTIN]
|
||||
select: [-DMG_ENABLE_POLL=0, -DMG_ENABLE_POLL=1]
|
||||
exclude:
|
||||
@@ -204,7 +450,7 @@ jobs:
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
- run: ./test/setup_mqtt_server.sh && make -C test ${{ matrix.target }} > log
|
||||
- run: if [ "${{ matrix.target }}" == "mingw" ]; then export TFLAGS="$TFLAGS -DNO_ZU_TEST"; fi && ./test/setup_mqtt_server.sh && make -C test ${{ matrix.target }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
cat log
|
||||
@@ -233,7 +479,7 @@ jobs:
|
||||
- run: make -C test riscv SSL=${{ matrix.ssl }}
|
||||
|
||||
tutorials:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04 # MicroPython v1.22.2 fails with Ubuntu 26.04 GCC 15
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -318,41 +564,69 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: infineon/xmc47_relax-make-baremetal-builtin
|
||||
- path: infineon/xmc_plt2go_4400-make-baremetal-builtin
|
||||
- path: infineon/xmc72_evk-make-baremetal-builtin
|
||||
- path: nxp/frdm-mcxn947-make-baremetal-builtin
|
||||
- path: nxp/frdm-mcxn947-make-freertos-builtin
|
||||
- path: nxp/rt1020-evk-make-baremetal-builtin
|
||||
- path: nxp/rt1060-evk-make-baremetal-builtin
|
||||
- path: nxp/rt1170-evk-make-baremetal-builtin
|
||||
- path: rp/pico-w
|
||||
- path: rp/w5500-evb-pico
|
||||
- path: rp/w55rp20-evb-pico
|
||||
- path: rp/pico-rndis
|
||||
- path: stm32/nucleo-f429zi/minimal
|
||||
- path: stm32/nucleo-f746zg/minimal
|
||||
- path: stm32/nucleo-f746zg/minimal-pppoe
|
||||
- path: stm32/nucleo-f756zg/minimal
|
||||
- path: stm32/nucleo-h563zi/minimal
|
||||
- path: stm32/nucleo-h723zg/minimal
|
||||
- path: stm32/nucleo-h743zi/minimal
|
||||
- path: stm32/nucleo-n657x0-q/minimal
|
||||
- path: stm32/nucleo-u5a5zj-q/minimal
|
||||
- path: stm32/portenta-h7/minimal
|
||||
- path: stm32/stm32h573i-dk/minimal
|
||||
- path: stm32/stm32h747i-disco/minimal
|
||||
- path: ti/ek-tm4c1294xl-make-baremetal-builtin
|
||||
- path: ti/ek-tm4c1294xl-make-freertos-builtin
|
||||
name: ${{ matrix.example.path }}
|
||||
include:
|
||||
- group: infineon-nxp
|
||||
examples: |
|
||||
infineon/xmc47_relax-make-baremetal-builtin
|
||||
infineon/xmc_plt2go_4400-make-baremetal-builtin
|
||||
infineon/xmc72_evk-make-baremetal-builtin
|
||||
nxp/frdm-imxrt1186/minimal
|
||||
nxp/frdm-mcxe247/minimal
|
||||
nxp/frdm-mcxn947/minimal
|
||||
nxp/frdm-rw612/minimal
|
||||
nxp/rt1060-evkb/minimal
|
||||
nxp/rt1170-evkb/minimal
|
||||
- group: rp-ti
|
||||
examples: |
|
||||
rp/pico-w
|
||||
rp/w5500-evb-pico
|
||||
rp/w55rp20-evb-pico
|
||||
rp/pico-rndis
|
||||
ti/ek-tm4c1294xl-make-baremetal-builtin
|
||||
ti/ek-tm4c1294xl-make-freertos-builtin
|
||||
- group: stm32-1
|
||||
examples: |
|
||||
stm32/nucleo-f429zi/minimal
|
||||
stm32/nucleo-f746zg/minimal
|
||||
stm32/nucleo-f746zg/minimal-pppoe
|
||||
stm32/nucleo-f756zg/minimal
|
||||
stm32/nucleo-h723zg/minimal
|
||||
stm32/nucleo-h723zg/minimal-bsd-baremetal
|
||||
stm32/nucleo-h723zg/minimal-bsd-freertos
|
||||
- group: stm32-2
|
||||
examples: |
|
||||
stm32/nucleo-h563zi/minimal
|
||||
stm32/nucleo-h743zi/minimal
|
||||
stm32/nucleo-n657x0-q/minimal
|
||||
stm32/nucleo-u5a5zj-q/minimal
|
||||
stm32/portenta-h7/minimal
|
||||
stm32/stm32h573i-dk/minimal
|
||||
stm32/stm32h747i-disco/minimal
|
||||
name: wizard ARM ${{ matrix.group }}
|
||||
env:
|
||||
GO: 0
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/${{ matrix.example.path }} build
|
||||
- run: |
|
||||
failed=0
|
||||
sep=
|
||||
examples='${{ matrix.examples }}'
|
||||
for x in $examples; do
|
||||
echo "========== $x =========="
|
||||
if make -C "tutorials/$x" build; then
|
||||
ok=true
|
||||
echo "SUCCESS: $x"
|
||||
else
|
||||
ok=false
|
||||
failed=1
|
||||
echo "FAILURE: $x"
|
||||
fi
|
||||
printf 'HEALTH_DASHBOARD\t%s"%s": %s\n' "$sep" "$x" "$ok"
|
||||
sep=,
|
||||
done
|
||||
exit $failed
|
||||
|
||||
|
||||
generic_examples:
|
||||
@@ -362,8 +636,8 @@ jobs:
|
||||
matrix:
|
||||
example:
|
||||
- path: micropython/esp32
|
||||
- path: http/uart-bridge/esp32
|
||||
- path: http/http-client/esp8266/http-client-server
|
||||
- path: esp32/uart-bridge
|
||||
- path: esp32/minimal
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
@@ -378,40 +652,41 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: mqtt/mqtt-client/microchip/same54-xpro
|
||||
- path: nxp/nxp-frdmk66f-freertos
|
||||
- path: nxp/nxp-lpcxpresso54s018m-freertos
|
||||
- path: nxp/nxp-mimxrt1020-freertos
|
||||
- path: nxp/nxp-evkbimxrt1050-lwip-freertos
|
||||
- path: nxp/nxp-evkmimxrt1020-lwip-freertos
|
||||
- path: nxp/nxp-evkmimxrt1024-lwip-freertos
|
||||
- path: nxp/nxp-evkmimxrt1060-lwip-freertos
|
||||
- path: nxp/nxp-evkmimxrt1064-lwip-freertos
|
||||
- path: nxp/nxp-evkmimxrt1160-cm7-lwip-freertos
|
||||
- path: nxp/nxp-evkmimxrt1170-cm7-lwip-freertos
|
||||
- path: nxp/nxp-frdmk64f-lwip-freertos
|
||||
- path: nxp/nxp-frdmk66f-lwip-freertos
|
||||
- path: nxp/nxp-lpcxpresso54018-lwip-freertos
|
||||
- path: nxp/nxp-lpcxpresso54608-lwip-freertos
|
||||
- path: nxp/nxp-lpcxpresso54618-lwip-freertos
|
||||
- path: nxp/nxp-lpcxpresso54628-lwip-freertos
|
||||
- path: nxp/nxp-twrk65f180m-lwip-freertos
|
||||
- path: nxp/nxp-twrkv58f220m-lwip-freertos
|
||||
- path: rp/pico-rmii
|
||||
- path: stm32/nucleo-f746zg/minimal-rm2
|
||||
- path: stm32/nucleo-f746zg/minimal-rndis
|
||||
- path: stm32/nucleo-f429zi/minimal-rm2
|
||||
- path: stm32/nucleo-f429zi/minimal-rndis
|
||||
- path: stm32/nucleo-g031k8/minimal-w5500
|
||||
- path: ti/ti-ek-tm4c1294xl-http-server
|
||||
- path: ti/ek-tm4c1294xl-make-baremetal-builtin-rndis
|
||||
name: ${{ matrix.example.path }}
|
||||
include:
|
||||
- group: other
|
||||
examples: |
|
||||
microchip/same54-xpro/mqtt-client
|
||||
rp/pico-rmii
|
||||
stm32/nucleo-f746zg/minimal-rm2
|
||||
stm32/nucleo-f746zg/minimal-rndis
|
||||
stm32/nucleo-f429zi/minimal-rm2
|
||||
stm32/nucleo-f429zi/minimal-rndis
|
||||
stm32/nucleo-g031k8/minimal-w5500
|
||||
ti/ti-ek-tm4c1294xl-http-server
|
||||
ti/ek-tm4c1294xl-make-baremetal-builtin-rndis
|
||||
name: generic ARM ${{ matrix.group }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/${{ matrix.example.path }} build
|
||||
- run: |
|
||||
failed=0
|
||||
sep=
|
||||
examples='${{ matrix.examples }}'
|
||||
for x in $examples; do
|
||||
echo "========== $x =========="
|
||||
if make -C "tutorials/$x" build; then
|
||||
ok=true
|
||||
echo "SUCCESS: $x"
|
||||
else
|
||||
ok=false
|
||||
failed=1
|
||||
echo "FAILURE: $x"
|
||||
fi
|
||||
printf 'HEALTH_DASHBOARD\t%s"%s": %s\n' "$sep" "$x" "$ok"
|
||||
sep=,
|
||||
done
|
||||
exit $failed
|
||||
|
||||
device_dashboard_examples:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -419,13 +694,13 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: esp32
|
||||
- path: esp32/device-dashboard
|
||||
name: ${{ matrix.example.path }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: echo # nothing specific to install or do
|
||||
- run: make -C tutorials/http/device-dashboard/${{ matrix.example.path }} build
|
||||
- run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
|
||||
device_dashboard_examples_arm:
|
||||
@@ -433,33 +708,54 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
ssl: ["", -DMG_TLS=MG_TLS_BUILTIN]
|
||||
example:
|
||||
- path: http/device-dashboard/microchip/same54-xpro
|
||||
- path: nxp/rt1020-evk-make-freertos-builtin
|
||||
- path: nxp/rt1060-evk-make-freertos-builtin
|
||||
- path: nxp/rt1170-evk-make-freertos-builtin
|
||||
- path: renesas/ek-ra6m4-make-baremetal-builtin
|
||||
name: ${{ matrix.example.path }} ${{ matrix.ssl }}
|
||||
include:
|
||||
- group: none
|
||||
cflags: ""
|
||||
examples: |
|
||||
microchip/same54-xpro/device-dashboard
|
||||
renesas/ek-ra6m4-make-baremetal-builtin
|
||||
- group: builtin
|
||||
cflags: -DMG_TLS=MG_TLS_BUILTIN
|
||||
examples: |
|
||||
microchip/same54-xpro/device-dashboard
|
||||
renesas/ek-ra6m4-make-baremetal-builtin
|
||||
name: device dashboard ARM ${{ matrix.group }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/${{ matrix.example.path }} build CFLAGS_EXTRA="${{ matrix.ssl }}"
|
||||
- run: |
|
||||
failed=0
|
||||
sep=
|
||||
examples='${{ matrix.examples }}'
|
||||
for x in $examples; do
|
||||
echo "========== $x =========="
|
||||
if make -C "tutorials/$x" build CFLAGS_EXTRA="${{ matrix.cflags }}"; then
|
||||
ok=true
|
||||
echo "SUCCESS: $x"
|
||||
else
|
||||
ok=false
|
||||
failed=1
|
||||
echo "FAILURE: $x"
|
||||
fi
|
||||
printf 'HEALTH_DASHBOARD\t%s"%s": %s\n' "$sep" "$x" "$ok"
|
||||
sep=,
|
||||
done
|
||||
exit $failed
|
||||
|
||||
# mqtt_dashboard_examples:
|
||||
# runs-on: ubuntu-latest
|
||||
# strategy:
|
||||
# fail-fast: false
|
||||
# matrix:
|
||||
# example:
|
||||
# - path:
|
||||
# name: ${{ matrix.example.path }}
|
||||
# steps:
|
||||
# - uses: actions/checkout@v5
|
||||
# with: { fetch-depth: 2 }
|
||||
# - run: echo # nothing specific to install or do
|
||||
# - run: make -C tutorials/${{ matrix.example.path }} build
|
||||
mqtt_dashboard_examples_arm:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: rp/pico-rndis-device
|
||||
name: ${{ matrix.example.path }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
|
||||
# dual_examples:
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
name: Retry failed nightly jobs
|
||||
on:
|
||||
schedule:
|
||||
- cron: '17 2 * * *'
|
||||
workflow_dispatch:
|
||||
permissions:
|
||||
actions: write
|
||||
jobs:
|
||||
retry:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
min=$(date -u -d '5 hours ago' +%s)
|
||||
max=$(date -u -d '2 hours ago' +%s)
|
||||
gh api --paginate "/repos/$GITHUB_REPOSITORY/actions/workflows/nightly.yml/runs?status=failure&per_page=100" --jq '.workflow_runs[] | select(.run_attempt == 1) | [.id, .created_at] | @tsv' |
|
||||
while IFS=$'\t' read -r id updated; do
|
||||
when=$(date -u -d "$updated" +%s)
|
||||
if [ "$when" -ge "$min" ] && [ "$when" -le "$max" ]; then
|
||||
gh api --method POST "/repos/$GITHUB_REPOSITORY/actions/runs/$id/rerun-failed-jobs"
|
||||
fi
|
||||
done
|
||||
@@ -53,16 +53,6 @@ jobs:
|
||||
run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/stm32/nucleo-h563zi/minimal test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
|
||||
test_rt1020:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 3 }
|
||||
- name: baremetal
|
||||
run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/nxp/rt1020-evk-make-baremetal-builtin test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/nxp/rt1020-evk-make-freertos-builtin test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
|
||||
test_rt1060:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -70,8 +60,7 @@ jobs:
|
||||
with: { fetch-depth: 3 }
|
||||
- name: baremetal
|
||||
run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/nxp/rt1060-evk-make-baremetal-builtin test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/nxp/rt1060-evk-make-freertos-builtin test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/nxp/rt1060-evkb/minimal build
|
||||
|
||||
test_rt1170:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -80,8 +69,7 @@ jobs:
|
||||
with: { fetch-depth: 3 }
|
||||
- name: baremetal
|
||||
run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/nxp/rt1170-evk-make-baremetal-builtin test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/nxp/rt1170-evk-make-freertos-builtin test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/nxp/rt1170-evkb/minimal build
|
||||
|
||||
test_ra6m4:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -110,8 +98,8 @@ jobs:
|
||||
with: { fetch-depth: 2 }
|
||||
- name: baremetal
|
||||
run: sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
- run: make -C tutorials/http/device-dashboard/microchip/same54-xpro test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/mqtt/mqtt-client/microchip/same54-xpro test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/microchip/same54-xpro/device-dashboard test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
- run: make -C tutorials/microchip/same54-xpro/mqtt-client test VCON_API_KEY=${{secrets.VCON_API_KEY}}
|
||||
|
||||
test_pico_w5500:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -4,35 +4,47 @@ on:
|
||||
env:
|
||||
IPV6: 0
|
||||
jobs:
|
||||
mip:
|
||||
atcmd:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
cc: [gcc]
|
||||
target: [mip_tap_test]
|
||||
ssl: ["", BUILTIN, MBEDTLS, OPENSSL, WOLFSSL]
|
||||
# #3226: built-in TCP is currently not working with WolfSSL (builds fine)
|
||||
exclude:
|
||||
- ssl: WOLFSSL
|
||||
target: mip_tap_test
|
||||
name: ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }}
|
||||
env:
|
||||
CC: ${{ matrix.cc }}
|
||||
SSL: ${{ matrix.ssl }}
|
||||
TFLAGS: -DMQTT_HOST -DIPV6_NOROUTING -DNO_ABORT # -DMQTT_HOST_NAME=\"server\"
|
||||
cc: [gcc, clang, g++, clang++]
|
||||
name: ATcmd CC=${{ matrix.cc }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- uses: webfactory/ssh-agent@v0.10.0
|
||||
with:
|
||||
ssh-private-key: ${{ secrets.HEALTH_TESTS_SSH_KEY }}
|
||||
- run: if [ "${{ matrix.target }}" == "mip_tap_test" ]; then source ./test/setup_ga_network.sh ; export IPV6=1; sed -i -e "s/127.0.0.1//" test/mosquitto.conf; cat test/mosquitto.conf; ./test/setup_mqtt_server.sh; else export IPV6=1 ; fi && sudo apt -y update ; sudo apt -y install libmbedtls-dev libwolfssl-dev && make -C test ${{ matrix.target }} > log
|
||||
- run: make -C test atcmd_test CC=${{ matrix.cc }} > log
|
||||
- if: success() || failure()
|
||||
run: |
|
||||
cat log
|
||||
test/health.awk < log > json
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/${{ matrix.target }}_${{ matrix.cc }}_${{ matrix.ssl }}_$(date +"%Y%m%d").json"
|
||||
scp -o "StrictHostKeyChecking=no" json "root@176.9.217.245:/data/downloads/health/atcmd_${{ matrix.cc }}_$(date +"%Y%m%d").json"
|
||||
atcmd89:
|
||||
runs-on: ubuntu-24.04 # Ubuntu 26.04 Docker 29.4.2 breaks 32-bit Wine; switch at >= 29.4.3
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [atcmd_vc98]
|
||||
name: ATcmd C89 ${{ matrix.target }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: make -C test ${{ matrix.target }}
|
||||
atcmdbe:
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [atcmd_s390]
|
||||
name: ATcmd be ${{ matrix.target }}
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test ${{ matrix.target }}
|
||||
|
||||
|
||||
# macos:
|
||||
|
||||
@@ -16,8 +16,13 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
cc: [gcc, clang++]
|
||||
target: [test, mip_test]
|
||||
target: [test, mip_test, eth_test, ppp_test]
|
||||
ssl: ["", BUILTIN]
|
||||
exclude:
|
||||
- target: eth_test
|
||||
ssl: BUILTIN
|
||||
- target: ppp_test
|
||||
ssl: BUILTIN
|
||||
name: linux ${{ matrix.target }} CC=${{ matrix.cc }} SSL=${{ matrix.ssl }}
|
||||
env:
|
||||
CC: ${{ matrix.cc }}
|
||||
@@ -30,7 +35,7 @@ jobs:
|
||||
- if: ${{ env.GO == 1 }}
|
||||
run: if [ "${{ matrix.target }}" == "test" ]; then ./test/setup_mqtt_server.sh ; fi && make -C test ${{ matrix.target }}
|
||||
s390:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04
|
||||
env:
|
||||
TFLAGS: -DMQTT_LOCALHOST
|
||||
steps:
|
||||
@@ -40,7 +45,7 @@ jobs:
|
||||
- if: ${{ env.GO == 1 }}
|
||||
run: sudo apt -y update ; sudo apt -y install binfmt-support qemu-user-static && ./test/setup_mqtt_server.sh && docker run --rm --privileged multiarch/qemu-user-static --reset -p yes && make -C test s390
|
||||
armhf:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04
|
||||
env:
|
||||
TFLAGS: -DMQTT_LOCALHOST
|
||||
steps:
|
||||
@@ -78,7 +83,7 @@ jobs:
|
||||
/opt/homebrew/opt/mosquitto/sbin/mosquitto -c /Users/runner/work/mongoose/mongoose/test/mosquitto.conf.macos &
|
||||
make -C test test ASAN_OPTIONS=
|
||||
windows:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04 # Ubuntu 26.04 Docker 29.4.2 breaks 32-bit Wine; switch at >= 29.4.3
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -143,15 +148,18 @@ jobs:
|
||||
- path: infineon/xmc47_relax-make-baremetal-builtin
|
||||
- path: infineon/xmc_plt2go_4400-make-baremetal-builtin
|
||||
- path: infineon/xmc72_evk-make-baremetal-builtin
|
||||
- path: nxp/frdm-mcxn947-make-baremetal-builtin
|
||||
- path: nxp/frdm-mcxn947-make-freertos-builtin
|
||||
- path: nxp/rt1020-evk-make-baremetal-builtin
|
||||
- path: nxp/rt1060-evk-make-baremetal-builtin
|
||||
- path: nxp/rt1170-evk-make-baremetal-builtin
|
||||
- path: nxp/frdm-imxrt1186/minimal
|
||||
- path: nxp/frdm-mcxe247/minimal
|
||||
- path: nxp/frdm-mcxn947/minimal
|
||||
- path: nxp/frdm-rw612/minimal
|
||||
- path: nxp/rt1060-evkb/minimal
|
||||
- path: nxp/rt1170-evkb/minimal
|
||||
- path: stm32/nucleo-f429zi/minimal
|
||||
- path: stm32/nucleo-f746zg/minimal
|
||||
- path: stm32/nucleo-h563zi/minimal
|
||||
- path: stm32/nucleo-h723zg/minimal
|
||||
- path: stm32/nucleo-h723zg/minimal-bsd-baremetal
|
||||
- path: stm32/nucleo-h723zg/minimal-bsd-freertos
|
||||
- path: stm32/nucleo-h743zi/minimal
|
||||
- path: stm32/stm32h573i-dk/minimal
|
||||
- path: ti/ek-tm4c1294xl-make-baremetal-builtin
|
||||
@@ -163,7 +171,7 @@ jobs:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
if ./test/match_changed_files.sh '^src'; then
|
||||
if ./test/match_changed_files.sh '^src|^tutorials/${{ matrix.example.path }}'; then
|
||||
echo GO=1 >> $GITHUB_ENV
|
||||
sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
fi
|
||||
@@ -177,8 +185,8 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: http/uart-bridge/esp32
|
||||
- path: http/http-client/esp8266/http-client-server
|
||||
- path: esp32/uart-bridge
|
||||
- path: esp32/minimal
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
@@ -220,7 +228,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: esp32
|
||||
- path: esp32/device-dashboard
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
@@ -228,12 +236,12 @@ jobs:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
if ./test/match_changed_files.sh '^src|tutorials/http/device-dashboard'; then
|
||||
if ./test/match_changed_files.sh '^src|^tutorials/http/device-dashboard|^tutorials/${{ matrix.example.path }}'; then
|
||||
echo GO=1 >> $GITHUB_ENV
|
||||
# nothing specific to install or do
|
||||
fi
|
||||
- if: ${{ env.GO == 1 }}
|
||||
run: make -C tutorials/http/device-dashboard/${{ matrix.example.path }} build
|
||||
run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
device_dashboard_examples_arm:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -241,10 +249,7 @@ jobs:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: http/device-dashboard/microchip/same54-xpro
|
||||
- path: nxp/rt1020-evk-make-freertos-builtin
|
||||
- path: nxp/rt1060-evk-make-freertos-builtin
|
||||
- path: nxp/rt1170-evk-make-freertos-builtin
|
||||
- path: microchip/same54-xpro/device-dashboard
|
||||
- path: renesas/ek-ra6m4-make-baremetal-builtin
|
||||
- path: rp/pico-rndis
|
||||
- path: rp/pico-w
|
||||
@@ -263,3 +268,24 @@ jobs:
|
||||
fi
|
||||
- if: ${{ env.GO == 1 }}
|
||||
run: make -C tutorials/${{ matrix.example.path }} build
|
||||
|
||||
mqtt_dashboard_examples_arm:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
example:
|
||||
- path: rp/pico-rndis-device
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with: { fetch-depth: 2 }
|
||||
- run: |
|
||||
if ./test/match_changed_files.sh '^src|^tutorials/mqtt/mqtt-dashboard/device|^tutorials/${{ matrix.example.path }}'; then
|
||||
echo GO=1 >> $GITHUB_ENV
|
||||
sudo apt -y update && sudo apt -y install gcc-arm-none-eabi
|
||||
fi
|
||||
- if: ${{ env.GO == 1 }}
|
||||
run: make -C tutorials/${{ matrix.example.path }} build
|
||||
@@ -179,7 +179,6 @@ jobs:
|
||||
example:
|
||||
- path: esp32/uart-bridge
|
||||
- path: esp32/micropython
|
||||
- path: esp8266/http-client-server
|
||||
name: ${{ matrix.example.path }}
|
||||
env:
|
||||
GO: 0
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
# Mongoose - Embedded Network Library
|
||||
# Mongoose: a full-stack embedded networking library
|
||||
|
||||
Mongoose is a full-stack embedded networking library for C and C++. It includes
|
||||
a TCP/IP stack with drivers, TLS 1.3, an embedded web server with HTTP and
|
||||
WebSocket support, MQTT, and firmware OTA updates. All in just two files:
|
||||
`mongoose.c` and `mongoose.h`. Mongoose runs on STM32, NXP and other
|
||||
microcontrollers, on bare metal or with an RTOS, with no external dependencies.
|
||||
Ideal for cloud connectivity, device web dashboards, secure firmware updates
|
||||
and remote control.
|
||||
|
||||
Mongoose is an open source, two-file C networking library and embedded web server for
|
||||
microcontrollers that combines TCP/IP stack, HTTP, WebSocket, MQTT, TLS 1.3
|
||||
stack, built-in firmware OTA updates, and device-dashboard infrastructure.
|
||||
License: GPLv2 or commercial.
|
||||
Website: https://mongoose.ws/
|
||||
GitHub repo: https://github.com/cesanta/mongoose
|
||||
|
||||
## 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.
|
||||
- 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
|
||||
@@ -26,6 +32,8 @@ GitHub repo: https://github.com/cesanta/mongoose
|
||||
functionality such as HTTP, MQTT, Modbus, or device-dashboard logic.
|
||||
- Once Mongoose is integrated, desktop examples from `tutorials/http`,
|
||||
`tutorials/mqtt`, and similar directories also work on embedded systems.
|
||||
- For STM32, NXP IMXRT, RP2040/2350 use Mongoose's built-in TCP/IP stack
|
||||
- When building an STM32 project from scratch, use the closest tutorials/stm32/*/cube/*.ioc file as a base
|
||||
|
||||
## API prefix map
|
||||
|
||||
@@ -33,6 +41,7 @@ This is a navigation aid, not an API reference. Read `mongoose.h` for exact
|
||||
function signatures, structs, options, examples, and constraints.
|
||||
|
||||
- `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_ws_*` - WebSocket server and client
|
||||
- `mg_mqtt_*` - MQTT client
|
||||
@@ -49,14 +58,17 @@ function signatures, structs, options, examples, and constraints.
|
||||
- `MG_OTA_*`, `mg_ota_*` - firmware OTA support
|
||||
|
||||
|
||||
## Integration
|
||||
## How to integrate Mongoose into an existing project
|
||||
|
||||
Copy `mongoose.h` and `mongoose.c` into a `mongoose/` subdirectory of the
|
||||
project and add `mongoose/mongoose.c` to the build. The easiest is to
|
||||
copy from GitHub:
|
||||
Create `mongoose/` directory in your project.
|
||||
Download `mongoose.h` and `mongoose.c` into a `mongoose/` directory.
|
||||
|
||||
https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/mongoose.c
|
||||
https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/mongoose.h
|
||||
```sh
|
||||
curl --fail --silent --create-dirs -o mongoose/mongoose.c https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/mongoose.c
|
||||
curl --fail --silent --create-dirs -o mongoose/mongoose.h https://raw.githubusercontent.com/cesanta/mongoose/refs/heads/master/mongoose.h
|
||||
```
|
||||
|
||||
Add `mongoose/mongoose.c` to the build.
|
||||
|
||||
**Desktop/server** (Linux, macOS, Windows): two files are sufficient.
|
||||
|
||||
@@ -89,6 +101,27 @@ Minimal `mongoose_config.h` should set `MG_ARCH`. For example, for STM32:
|
||||
#define MG_ARCH MG_ARCH_CUBE
|
||||
```
|
||||
|
||||
## How to generate a new STM32 project from scratch
|
||||
|
||||
Download and unzip the pre-generated project which is the closest to your MCU:
|
||||
|
||||
- https://mongoose.ws/downloads/nucleo-h723zg-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/nucleo-f429zi-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/nucleo-h563zi-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/nucleo-f756zg-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/nucleo-n657x0-q-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/nucleo-u5a5zj-q-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/portenta-h7-dashboard-full.zip
|
||||
|
||||
## How to generate a new RP2040 / RP2350 project from scratch
|
||||
|
||||
Download and unzip the pre-generated project which is the closest to your MCU:
|
||||
|
||||
- https://mongoose.ws/downloads/w5500-evb-pico-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/w55rp20-evb-pico-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/pico-w-dashboard-full.zip
|
||||
- https://mongoose.ws/downloads/pico-rndis-dashboard-full.zip
|
||||
|
||||
## Core API
|
||||
|
||||
```c
|
||||
@@ -199,19 +232,51 @@ xTaskCreate(net_task, "net", 8192, NULL, tskIDLE_PRIORITY + 1, NULL);
|
||||
|
||||
## HTTP / Web Device Dashboard
|
||||
|
||||
For a web UI with real-time device state over WebSocket, use the Mongoose
|
||||
device dashboard.
|
||||
When building a device web UI, control panel, monitoring page, or live
|
||||
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
|
||||
|
||||
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/
|
||||
├── main.c # your code
|
||||
├── ... # IDE-specific project scaffolding
|
||||
└── mongoose/
|
||||
├── mongoose.h # single header
|
||||
├── mongoose.c # single source file
|
||||
├── dashboard.c # C-side dashboard logic
|
||||
├── dashboard.html # HTML/JS UI (source)
|
||||
├── html2c.js # HTML-to-C generator
|
||||
└── file_data.c # generated from dashboard.html (see below)
|
||||
```
|
||||
|
||||
@@ -223,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
|
||||
|
||||
If the project does not yet have `dashboard.c` and `dashboard.html`, fetch the
|
||||
minimal reference versions:
|
||||
For each missing `dashboard.c`, `dashboard.html`, or `html2c.js`, download its
|
||||
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.html
|
||||
https://github.com/cesanta/mongoose/blob/master/resources/html2c.js
|
||||
```
|
||||
|
||||
If the project already has `dashboard.c` and `dashboard.html`, do **not** fetch
|
||||
anything from the repository.
|
||||
Add a build rule that regenerates `file_data.c` whenever `dashboard.html` or
|
||||
`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)
|
||||
|
||||
@@ -251,15 +324,17 @@ for (;;) {
|
||||
|
||||
### 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
|
||||
Dashboard API call. Treat the rest of Dashboard as a black box.
|
||||
- Do **not** add vanilla JS event listeners, `fetch()` calls, or custom
|
||||
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
|
||||
- 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
|
||||
- If you want to display device data in HTML, use `${}` evaluations
|
||||
|
||||
@@ -283,17 +358,24 @@ for (;;) {
|
||||
|
||||
### 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:
|
||||
|
||||
```c
|
||||
static struct mg_dash s_dash;
|
||||
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u);
|
||||
|
||||
static struct settings {
|
||||
double volume;
|
||||
char name[10];
|
||||
int log_level;
|
||||
bool enable_login;
|
||||
} s_settings = {2.7, "Dublin", 2, false, false};
|
||||
} s_settings = {2.7, "Dublin", 2, false};
|
||||
|
||||
static struct mg_field fields_settings[] = {
|
||||
{"volume", MG_VAL_DBL, &s_settings.volume, sizeof(s_settings.volume)},
|
||||
@@ -304,14 +386,12 @@ static struct mg_field fields_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:
|
||||
```c
|
||||
static struct mg_dash s_dash;
|
||||
|
||||
// Signature: (char *user, size_t userlen, const char *pass)
|
||||
// `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
|
||||
@@ -328,24 +408,34 @@ static int authenticate(char *user, size_t userlen, const char *pass) {
|
||||
return level;
|
||||
}
|
||||
|
||||
static void write_settings(void) {
|
||||
static bool settings_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||
(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
|
||||
static struct leds {
|
||||
bool led1;
|
||||
} s_leds = {false};
|
||||
|
||||
static void write_leds(void) {
|
||||
gpio_write(LED1_PIN, s_leds.led1);
|
||||
}
|
||||
|
||||
static void read_leds(void) {
|
||||
static bool leds_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||
(void) u;
|
||||
if (op == MG_DASH_READ) {
|
||||
s_leds.led1 = gpio_read(LED1_PIN);
|
||||
} else if (op == MG_DASH_WRITE) {
|
||||
gpio_write(LED1_PIN, s_leds.led1);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct mg_field fields_leds[] = {
|
||||
@@ -354,7 +444,7 @@ static struct mg_field fields_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,
|
||||
};
|
||||
```
|
||||
|
||||
@@ -380,7 +470,8 @@ static struct event {
|
||||
} s_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,
|
||||
};
|
||||
```
|
||||
|
||||
@@ -394,6 +485,25 @@ Each fieldset is exported via the get/set JSON-RPC interface, as well via the RE
|
||||
|
||||
Do not use the API directly, dashboard.js interfaces with the UI via the data-* attributes.
|
||||
|
||||
The dashboard backend must expose these exact functions: mg_dash_init() and mg_dash_poll().
|
||||
|
||||
Do not rename, wrap, relocate, or replace these files or functions. Do not introduce alternatives such as app_dashboard_init(), app_dashboard_poll(), web/dashboard.html, or Core/Src/dashboard.c.
|
||||
|
||||
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
|
||||
|
||||
@@ -453,7 +563,7 @@ embedded devices and needs only a few lines added to `mongoose_config.h`:
|
||||
|
||||
```c
|
||||
#define MG_OTA_URL "https://mongoose.ws/ota/u/PREFIX/ota.json" // Copy from instructions panel
|
||||
#define MG_OTA_FIRMWARE_VERSION "MY_PRODUCT_1.4.2" // Change this
|
||||
#define MG_FIRMWARE_VERSION "MY_PRODUCT_1.4.2" // Change this
|
||||
```
|
||||
|
||||
No backend programming required. The device registers itself and checks in at
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Mongoose - Embedded Web Server / Embedded Network Library
|
||||
# Mongoose: a full-stack embedded networking library
|
||||
|
||||
[](https://opensource.org/licenses/gpl-2.0.php)
|
||||
[](https://github.com/cesanta/mongoose/actions)
|
||||
@@ -7,11 +7,18 @@
|
||||
|
||||
<img src="https://mongoose.ws/images/logo.svg" width="48" height="48"
|
||||
align="left" style="float:left;" />
|
||||
Two files. Drop `mongoose.c` and `mongoose.h` into any C/C++ project and get a
|
||||
full network stack — HTTP, WebSocket, MQTT, TLS, firmware OTA
|
||||
updates, device dashboard — on any microcontroller or desktop OS.
|
||||
Mongoose is a full-stack embedded networking library for C and C++. It includes
|
||||
a TCP/IP stack with drivers, TLS 1.3, an embedded web server with HTTP and
|
||||
WebSocket support, MQTT, and firmware OTA updates. All in just two files:
|
||||
`mongoose.c` and `mongoose.h`. Mongoose runs on STM32, NXP and other
|
||||
microcontrollers, on bare metal or with an RTOS, with no external dependencies.
|
||||
Ideal for cloud connectivity, device web dashboards, secure firmware updates
|
||||
and remote control.
|
||||
|
||||
On the market since 2004, used by vast number of open source and commercial
|
||||
Use it instead of lwIP + mbedTLS + httpd + an MQTT client + OTA glue code, or
|
||||
run it on top of lwIP or Zephyr.
|
||||
|
||||
On the market since 2004, used by a vast number of open source and commercial
|
||||
products - it even runs on the International Space Station! Features include:
|
||||
|
||||
- Cross-platform:
|
||||
@@ -24,7 +31,7 @@ products - it even runs on the International Space Station! Features include:
|
||||
- Source code is both ISO C and ISO C++ compliant
|
||||
- Very easy to integrate
|
||||
- Own TCP/IP stack for bare metal / RTOS, or run on top of any existing one (lwIP, Zephyr, ...)
|
||||
- Built-in TLS 1.3 ECC stack. Also can use external TLS libraries - mbedTLS, OpenSSL, or other
|
||||
- Built-in TLS 1.3 stack. Also can use external TLS libraries - mbedTLS, OpenSSL, or other
|
||||
- Does not depend on any other software to implement networking
|
||||
- Built-in firmware updates for STM32 H5, STM32 H7, NXP IMXRT, RP2040/2350, ESP32 and more
|
||||
- Commercial support with proactive security updates; [CRA-compliant](https://mongoose.ws/security/cra-compliance/)
|
||||
@@ -35,8 +42,32 @@ See https://mongoose.ws/ for complete documentation, videos, case studies, etc.
|
||||
|
||||
## Supported platforms
|
||||
|
||||
Mongoose can work on top of any TCP/IP stack that supports BSD sockets API.
|
||||
Platforms supported by the 3rd party TCP/IP stacks:
|
||||
Mongoose has its own built-in TCP/IP stack with drivers, so it needs no
|
||||
additional software to implement networking. The built-in stack runs on bare
|
||||
metal and under any RTOS. Platforms supported by the Mongoose built-in TCP/IP
|
||||
stack:
|
||||
|
||||
| MCU | Connectivity | Notes |
|
||||
| :-- | :----------- | :---- |
|
||||
| **STM32** | Built-in Ethernet | Ethernet-capable STM32F, STM32H5, STM32H7, STM32N6, STM32C5 variants |
|
||||
| **NXP i.MX RT** | Built-in Ethernet | RT102x, RT104x, RT105x, RT106x, RT117x, RT118x (NETC) |
|
||||
| **NXP MCX** | Built-in Ethernet | MCXN94x, MCXE247 |
|
||||
| **NXP RW612** | Eth, WiFi, 802.15.4 | Separate Ethernet and radio drivers |
|
||||
| **Microchip ATSAME54** | Built-in Ethernet | GMAC driver |
|
||||
| **Renesas RA** | Built-in Ethernet | RA6M, RA8M |
|
||||
| **Infineon XMC** | Built-in Ethernet | XMC4, XMC7 |
|
||||
| **Texas Instruments** | Built-in Ethernet | TM4C, TMS570 |
|
||||
| **RP2040, RP2350, STM32, others** | CYW43xx Wi-Fi chips | SPI or SDIO; Pico W, Pico 2 W, Pico RM2, Arduino Portenta H7 |
|
||||
| **STM32, others** | ST67W6 Wi-Fi modules | SPI; for example, STM32U5 with the ST67W611M1 module |
|
||||
| **MCUs with an NXP Wi-Fi SDK port** | NXP Wi-Fi | Requires the SDK's Wi-Fi port implementing the driver's network hooks |
|
||||
| **NXP MCXW71** | Built-in 802.15.4 radio | IPv4 over IEEE 802.15.4 |
|
||||
| **NXP RW612** | Built-in 802.15.4 radio | IPv4 over IEEE 802.15.4; requires NXP radio firmware |
|
||||
| **Any MCU with SPI** | W5500 or W5100 Ethernet chips | External Wiznet MAC+PHY |
|
||||
| **MCUs connected to a cellular modem** | Cellular | AT commands and PPP; for example, SIM800 |
|
||||
| **MCUs with CMSIS Ethernet drivers** | Ethernet via CMSIS-Driver | Requires CMSIS Ethernet MAC and PHY drivers |
|
||||
|
||||
Mongoose can also run on top of any TCP/IP stack that supports the BSD
|
||||
sockets API. Platforms supported via 3rd party TCP/IP stacks:
|
||||
|
||||
| TCP/IP stack | Notes |
|
||||
| :-------------- | :------- |
|
||||
@@ -45,23 +76,6 @@ Platforms supported by the 3rd party TCP/IP stacks:
|
||||
| **Other** | Any other TCP/IP stack that supports BSD socket API, for example Amazon FreeRTOS-TCP |
|
||||
| **Linux, Mac, Windows** | Workstations, server, single board computers, embedded Linux devices running on MPUs or FPGAs |
|
||||
|
||||
Optionally, Mongoose provides its own built-in TCP/IP stack, eliminating the
|
||||
need for additional software to implement networking functionality. The
|
||||
built-in stack supports operation in both bare-metal and RTOS environments.
|
||||
Platforms supported by the Mongoose built-in TCP/IP stack:
|
||||
|
||||
| Hardware | Notes |
|
||||
| :------------- | :------- |
|
||||
| **STM32** | All STM32 MCUs with built-in Ethernet: STM32Fxx, STM32H5xx, STM32H7xx |
|
||||
| **NXP** | All NXP MCUs with built-in Ethernet: IMXRT102x, IMXRT104x, IMXRT105x, IMXRT106x, IMXRT117x, RW612, MCXN94x |
|
||||
| **Microchip** | ATSAME54 MCUs with built-int Ethernet |
|
||||
| **Renesas** | RA5M, RA6M, RA8M MCUs with built-in Ethernet |
|
||||
| **Infineon** | XMC4, XMC7 MCUs with built-in Ethernet |
|
||||
| **Texas Instruments** | TM4C, TMS570 MCUs with built-in Ethernet |
|
||||
| **Cypress WiFi** | Any MCU with CY43xx WiFi chips, like RP2040 Pico-W, RP2350 Pico2-W, Arduino Portenta |
|
||||
| **Wiznet Ethernet** | Any MCU that use Wiznet W5500 or Wiznet 5100 MAC+PHY chips |
|
||||
| **Cellular** | NRF9160, SIM800 |
|
||||
|
||||
## Usage Examples
|
||||
|
||||
Below are quick snippets that should give an idea how simple the API is and
|
||||
@@ -158,6 +172,17 @@ int main() {
|
||||
}
|
||||
```
|
||||
|
||||
## Licensing
|
||||
|
||||
Mongoose is dual-licensed:
|
||||
- GPLv2: free for open source projects. Many popular open source projects use
|
||||
Mongoose, for example
|
||||
[SWUpdate](https://github.com/sbabic/swupdate/tree/master/mongoose), the
|
||||
embedded Linux update framework
|
||||
- Commercial: for closed-source products. Includes support directly from the
|
||||
engineers who wrote the whole stack, and early security fixes.
|
||||
See [licensing](https://mongoose.ws/licensing/)
|
||||
|
||||
## Commercial use
|
||||
- Mongoose is used by hundreds of businesses, from Fortune500 giants like
|
||||
Siemens, Schneider Electric, Broadcom, Bosch, Google, Samsung, Qualcomm, Caterpillar to the small businesses
|
||||
@@ -220,6 +245,15 @@ Technical guides and deep dives into embedded web servers, WebUI integration and
|
||||
- [STM32 Ethernet and caches](https://mongoose.ws/articles/stm32-ethernet-and-cache/)
|
||||
- [NXP RW612 OTA Firmware Update](https://mongoose.ws/articles/rw612-ota-firmware-update/)
|
||||
- [lwIP vs Mongoose - TCP/IP Stack Integration Benchmark](https://mongoose.ws/articles/lwip-vs-mongoose-tcpip-stack-integration/)
|
||||
- [Embedded TCP/IP Stacks Comparison](https://mongoose.ws/embedded-tcpip-stack-comparison/)
|
||||
- [STM32H5 File System on Internal Flash: LittleFS + Mongoose](https://mongoose.ws/articles/stm32h5-file-system-on-internal-flash/)
|
||||
- [Firmware OTA and remote control on NXP FRDM-RW612](https://mongoose.ws/articles/rw612-mdash/)
|
||||
- [25000 WebSocket messages per second on STM32 Nucleo-H723ZG](https://mongoose.ws/articles/stm32-websocket-benchmark/)
|
||||
- [How to connect STM32 to AWS IoT Core](https://mongoose.ws/articles/stm32-aws-iot/)
|
||||
- [How to connect STM32 to Azure IoT Hub](https://mongoose.ws/articles/stm32-azure-iot-hub/)
|
||||
- [How to connect STM32 to EMQX](https://mongoose.ws/articles/stm32-emqx/)
|
||||
- [STM32 Device Management deep dive: Secure OTA, Remote Control, health monitor](https://mongoose.ws/articles/stm32-mdash-deep-dive/)
|
||||
- [STM32 TCP/IP footprint: how much flash and RAM does Mongoose take?](https://mongoose.ws/articles/tcp-ip-footprint/)
|
||||
|
||||
|
||||
## Contributions
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
# Security Policy
|
||||
|
||||
Mongoose is a full-stack embedded networking library by Cesanta Software Ltd,
|
||||
<https://mongoose.ws>. This policy covers how to report vulnerabilities
|
||||
and how Cesanta handles them.
|
||||
|
||||
## Supported versions
|
||||
|
||||
Security fixes go to the `master` branch and the latest release.
|
||||
|
||||
## Reporting a vulnerability
|
||||
|
||||
Do not report security issues in public GitHub issues. Report them
|
||||
privately using GitHub Security Advisories:
|
||||
<https://github.com/cesanta/mongoose/security/advisories/new>
|
||||
|
||||
Include the Mongoose version or commit, steps or a proof of concept to reproduce, and
|
||||
the impact. Cesanta usually responds the same business day.
|
||||
|
||||
## Disclosure process
|
||||
|
||||
1. Cesanta reviews the report and coordinates a fix with the reporter.
|
||||
2. Eligible commercial customers receive a notification and a private fix
|
||||
one month before the CVE becomes public.
|
||||
3. The fix and the CVE are then published. The reporter is credited in the
|
||||
GitHub Security Advisory.
|
||||
|
||||
## Security testing
|
||||
|
||||
- CI on every push, unit tests built with ASAN and UBSan
|
||||
- CodeQL static analysis on every commit
|
||||
- Continuous fuzzing on OSS-Fuzz, plus nightly libFuzzer runs
|
||||
- AI-assisted security review on every pull request
|
||||
- Regular review by independent security research groups worldwide
|
||||
|
||||
## EU Cyber Resilience Act (CRA)
|
||||
|
||||
Mongoose supports CRA compliance for connected products: a documented
|
||||
vulnerability handling process (this file), TLS 1.3, signed OTA firmware
|
||||
updates, and an SBOM (`sbom.cdx.json`). Details:
|
||||
<https://mongoose.ws/security/cra-compliance/>
|
||||
+12382
-5496
File diff suppressed because it is too large.
Load diff
+1626
-965
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema",
|
||||
"version": "0.0.1",
|
||||
"componentid": "::Mongoose:Networking Core",
|
||||
"type": "object",
|
||||
"grammarVersion": "3.8.0",
|
||||
"license": "Copyright (c) Cesanta Software Limited",
|
||||
"properties": {
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"info": {
|
||||
"type": "object",
|
||||
"title": "General information",
|
||||
"properties": {
|
||||
"description": {
|
||||
"title": "Middleware description",
|
||||
"type": "string",
|
||||
"actions": [
|
||||
{
|
||||
"type": "disable",
|
||||
"condition": "true"
|
||||
}
|
||||
],
|
||||
"default": "Mongoose networking library: TCP/IP, TLS, HTTP, WebSocket, MQTT, OTA. Edit mongoose_config.h to configure it."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package schemaVersion="1.7.40" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="PACK.xsd">
|
||||
<package schemaVersion="1.7.56" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stm32="https://developer.st.com/schemas/stm32cube/1.0.0" xsi:noNamespaceSchemaLocation="PACK.xsd">
|
||||
<vendor>Cesanta</vendor>
|
||||
<name>I-CUBE-Mongoose</name>
|
||||
<description>Mongoose - a lightweight networking library providing TCP/IP stack, TLS, HTTP (Embedded Web Server), WebSocket, MQTT, and Firmware OTA updates. Build production-ready web device dashboards and MQTT cloud connectivity without stitching multiple components together.
|
||||
</description>
|
||||
<description>Mongoose - a full-stack networking library for microcontrollers: Ethernet and Wi-Fi drivers, TCP/IP stack, TLS 1.3, HTTP, WebSocket, MQTT, Modbus, OTA updates and device management, in a two-file C/C++ library, on any RTOS or bare metal.</description>
|
||||
<url>https://mongoose.ws/downloads/stm32pack/</url>
|
||||
<license>License.txt</license>
|
||||
<supportContact>https://mongoose.ws/contact/</supportContact>
|
||||
<environments>
|
||||
<environment name="STM32Cube">
|
||||
<stm32:pack type="MW" friendlyName="Mongoose" shortDesc="Mongoose full-stack networking library">
|
||||
<relocation-info parent="middleware"/>
|
||||
<no-compatibility-constraint/>
|
||||
</stm32:pack>
|
||||
</environment>
|
||||
</environments>
|
||||
<releases>
|
||||
<release date="2026-10-21" version="7.23.3">See https://github.com/cesanta/mongoose/releases/tag/7.23</release>
|
||||
<release date="2026-10-19" version="7.23.2">See https://github.com/cesanta/mongoose/releases/tag/7.23</release>
|
||||
<release date="2026-09-28" version="7.23.1">See https://github.com/cesanta/mongoose/releases/tag/7.23</release>
|
||||
<release date="2026-05-11" version="7.21.7">See https://github.com/cesanta/mongoose/releases/tag/7.21</release>
|
||||
<release date="2026-05-05" version="7.21.5">See https://github.com/cesanta/mongoose/releases/tag/7.21</release>
|
||||
<release date="2026-04-24" version="7.21.4">See https://github.com/cesanta/mongoose/releases/tag/7.21</release>
|
||||
@@ -19,7 +29,7 @@
|
||||
<keyword>IMPLEMENTED_WITH_STM32CUBEMX</keyword>
|
||||
</keywords>
|
||||
<components>
|
||||
<component Cclass="Mongoose" Cgroup="Networking Core" Cversion="7.21.7" maxInstances="1">
|
||||
<component Cclass="Mongoose" Cgroup="Networking Core" Cversion="7.23.3" maxInstances="1">
|
||||
<description>Networking Core</description>
|
||||
<files>
|
||||
<file category="doc" name="README.md"/>
|
||||
@@ -28,6 +38,13 @@
|
||||
<file category="header" name="mongoose_config.h"/>
|
||||
<file category="utility" name="README.md" public="false"/>
|
||||
</files>
|
||||
<environments>
|
||||
<environment name="STM32Cube">
|
||||
<stm32:component>
|
||||
<middleware-entry-point id="Mongoose" name="Mongoose" category="TCP/IP"/>
|
||||
</stm32:component>
|
||||
</environment>
|
||||
</environments>
|
||||
</component>
|
||||
</components>
|
||||
</package>
|
||||
@@ -1,10 +1,16 @@
|
||||
VERSION = 7.21.7
|
||||
VERSION = 7.23.3
|
||||
REPO_ROOT = ../..
|
||||
MX ?= /Applications/STMicroelectronics/STM32CubeMX.app/Contents/MacOs/STM32CubeMX
|
||||
FILES = Cesanta.I-CUBE-Mongoose.pdsc Release_Notes.html mongoose_config.h README.md
|
||||
FILES = Cesanta.I-CUBE-Mongoose.pdsc Release_Notes.html mongoose_config.h README.md .config/mongoose_parameters.json
|
||||
GENERATED = License.txt mongoose.h mongoose.c
|
||||
MX2 ?= $(HOME)/STMicroelectronics/STM32CubeMX2_1.1.2/.bin/cube
|
||||
PACKSDIR ?= $(HOME)/Library/Application Support/stm32cube/packs
|
||||
MX2CPN ?= STM32C5A3ZGT6
|
||||
MX2PROJ = $(CURDIR)/mx2test/test.ioc2
|
||||
|
||||
NAME = Cesanta.I-CUBE-Mongoose.$(VERSION)
|
||||
PACK = $(NAME).pack
|
||||
|
||||
PACK = Cesanta.I-CUBE-Mongoose.$(VERSION).pack
|
||||
all: $(PACK)
|
||||
|
||||
Cesanta.I-CUBE-Mongoose.$(VERSION).pack: $(FILES) $(GENERATED)
|
||||
@@ -44,23 +50,19 @@ mongoose.h: Mongoose
|
||||
mongoose.c: Mongoose
|
||||
cp $(REPO_ROOT)/mongoose.c $@
|
||||
|
||||
pk: $(PACK)
|
||||
rm -f gen.mx
|
||||
echo swmgr remove Cesanta.I-CUBE-Mongoose.$(VERSION) >> gen.mx
|
||||
echo swmgr install `pwd`/Cesanta.I-CUBE-Mongoose.$(VERSION).pack ask >> gen.mx
|
||||
echo exit >> gen.mx
|
||||
$(MX) -q `pwd`/gen.mx
|
||||
|
||||
|
||||
mx: $(PACK)
|
||||
mkdir -p $@
|
||||
cp ../downloads/nucleo_h563zi.ioc $@
|
||||
V=`grep 'Mcu.ThirdParty0' $@/nucleo_h563zi.ioc | sed 's,^.*se.,,'`; sed -i '' "s,$$V,$(VERSION),g" $@/nucleo_h563zi.ioc
|
||||
rm -f gen.mx
|
||||
echo config load `pwd`/$@/nucleo_h563zi.ioc >> gen.mx
|
||||
echo project generate >> gen.mx
|
||||
echo exit >> gen.mx
|
||||
$(MX) -q `pwd`/gen.mx
|
||||
test_mx2: $(PACK)
|
||||
-$(MX2) pack remove --cmsis-pack-root "$(PACKSDIR)" $(NAME)
|
||||
rm -f "$(PACKSDIR)/.Download/$(PACK)"
|
||||
$(MX2) pack install --cmsis-pack-root "$(PACKSDIR)" "$(CURDIR)/$(PACK)"
|
||||
export PATH="$(dir $(MX2)):$$PATH"; \
|
||||
PORT=$$(cube lifecycle-mx start-only | sed -n 's/^BACKEND_PORT *//p'); \
|
||||
cube mx project close --port $$PORT -p "$(MX2PROJ)" --discard > /dev/null 2>&1; \
|
||||
rm -rf mx2test && mkdir mx2test && \
|
||||
cube mx project create-from-mcu --port $$PORT --cpn $(MX2CPN) -l "$(CURDIR)/mx2test" -n test --empty-project && \
|
||||
cube mx middleware enable-middleware --port $$PORT -p "$(MX2PROJ)" -n Mongoose | tee mx2test/enable.json && \
|
||||
grep -q '"status": "success"' mx2test/enable.json && \
|
||||
cube mx project save --port $$PORT -p "$(MX2PROJ)" && \
|
||||
cube mx project close --port $$PORT -p "$(MX2PROJ)" --discard
|
||||
|
||||
clean:
|
||||
rm -rf *.pack STM32CubeMX/*.$(VERSION)_*.xml $(GENERATED) gen.mx mx
|
||||
rm -rf *.pack STM32CubeMX/*.$(VERSION)_*.xml $(GENERATED) mx2test
|
||||
@@ -5,200 +5,6 @@ This pack provides an easy to use CubeMX integration.
|
||||
## Mongoose Networking Core
|
||||
|
||||
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
|
||||
the steps to make it work with you CubeMX application:
|
||||
`mongoose_config.h`. To finish integration, follow this guide:
|
||||
|
||||
## 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/
|
||||
https://mongoose.ws/docs/guides/stm32-tcpip-stack/#configure-peripherals
|
||||
@@ -39,8 +39,7 @@ patch_linker_script() {
|
||||
HUART=`perl -nle 'print \$1 if /^UART_HandleTypeDef (.+);/' $MAIN_C`
|
||||
grep -q mongoose.h $MAIN_C || perl -i -ne 'print; print "#include \"mongoose.h\"\n" if /BEGIN Includes/' $MAIN_C
|
||||
grep -q '^static void log_fn' $MAIN_C || perl -i -ne "print; print \"static void log_fn(char ch, void *param) {\n HAL_UART_Transmit(param, (unsigned char *) &ch, 1, HAL_MAX_DELAY);\n}\n\" if /USER CODE BEGIN 0/" $MAIN_C
|
||||
grep -q 'mg_mgr_init' $MAIN_C || perl -i -ne 'print; print " struct mg_mgr mgr;\n mg_mgr_init(&mgr);\n mg_log_set_fn(log_fn, &'"${HUART}"');\n\n" if /USER CODE BEGIN WHILE/' $MAIN_C
|
||||
grep -q 'mg_mgr_init' $MAIN_C && grep -q 'mg_log_set_fn' $MAIN_C || perl -i -ne 'print; print " mg_log_set_fn(log_fn, &'"${HUART}"');\n" if /mg_mgr_init/' $MAIN_C
|
||||
grep -q 'mg_mgr_init' $MAIN_C || perl -i -ne 'print; print " struct mg_mgr mgr;\n mg_log_set_fn(log_fn, &'"${HUART}"');\n mg_mgr_init(&mgr);\n mg_log_set_fn(log_fn, &'"${HUART}"');\n\n" if /USER CODE BEGIN WHILE/' $MAIN_C
|
||||
grep -q 'mg_mgr_poll' $MAIN_C || perl -i -ne 'print " mg_mgr_poll(&mgr, 0);\n" if /USER CODE END WHILE/; print;' $MAIN_C
|
||||
patch_linker_script $DIR/STM32H723XG_FLASH.ld
|
||||
patch_linker_script $DIR/STM32H743XX_FLASH.ld
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# Security Scan Area: common_misc
|
||||
|
||||
## Target
|
||||
|
||||
Area: common_misc
|
||||
|
||||
This area covers common utility code, parsing/string/formatting helpers, URL handling, hash/base64 helpers, filesystem backends, little filesystem integration, and flash support.
|
||||
|
||||
Primary files:
|
||||
- `src/base64.c`
|
||||
- `src/fmt.c`
|
||||
- `src/json.c`
|
||||
- `src/log.c`
|
||||
- `src/md5.c`
|
||||
- `src/printf.c`
|
||||
- `src/sha1.c`
|
||||
- `src/sha256.c`
|
||||
- `src/str.c`
|
||||
- `src/url.c`
|
||||
- `src/util.c`
|
||||
- `src/fs*.c`
|
||||
- `src/lfs.c`
|
||||
- `src/flash.c`
|
||||
|
||||
Perform a focused security review centered on these files and this security surface.
|
||||
|
||||
Supporting code elsewhere under `src/` may be inspected when necessary to establish reachability, data flow, state, validation, mitigation, or impact, but do not inspect or report code outside `src/` or independently review unrelated areas.
|
||||
|
||||
## Area-Specific Security Guidance
|
||||
|
||||
**Filesystem Backends:**
|
||||
- Review packed filesystem code and filesystem backends for traversal, unauthorized file read/write, overwrite, or truncation.
|
||||
- Check path canonicalization across POSIX, Windows, embedded filesystems, packed filesystems, and custom `mg_fs` implementations.
|
||||
- Look for trailing slash confusion, NUL byte truncation, and case sensitivity differences in backend path handling.
|
||||
|
||||
**URL Parsing:**
|
||||
- Review URL parsing for scheme, host, port, IPv6 literal, userinfo, percent-encoding, embedded NUL, empty host, default port, and path/query boundary confusion.
|
||||
- Check for SSRF-relevant behavior only when attacker input can control the destination host, protocol, or network boundary. Path-only control is not sufficient.
|
||||
@@ -0,0 +1,30 @@
|
||||
# Security Scan Area: mongoose_core
|
||||
|
||||
## Target
|
||||
|
||||
Area: mongoose_core
|
||||
|
||||
This area covers the core event/network/connection machinery, iobuf handling, queues, socket transport, timers, and BSD abstraction.
|
||||
|
||||
Primary files:
|
||||
- `src/event.c`
|
||||
- `src/iobuf.c`
|
||||
- `src/net.c`
|
||||
- `src/queue.c`
|
||||
- `src/sock.c`
|
||||
- `src/timer.c`
|
||||
- `src/bsd.c`
|
||||
|
||||
Perform a focused security review centered on these files and this security surface.
|
||||
|
||||
Supporting code elsewhere under `src/` may be inspected when necessary to establish reachability, data flow, state, validation, mitigation, or impact, but do not inspect or report code outside `src/` or independently review unrelated areas.
|
||||
|
||||
## Area-Specific Security Guidance
|
||||
|
||||
**Protocol State Machine and Event-Driven Lifecycle Bugs:**
|
||||
- Review transitions between listening, accepted, connecting, resolving, TLS handshaking, HTTP, WebSocket, MQTT, draining, closing, and closed states.
|
||||
- Check whether malformed input can cause protocol handlers to process data in the wrong state, process stale data after close, or invoke callbacks with invalid event data.
|
||||
- Look for use-after-free or stale pointer use when callbacks call `mg_close_conn`, mark a connection closing, mutate iobufs, change protocol handlers, or initiate TLS/WebSocket/MQTT transitions.
|
||||
- Verify that partial reads/writes, backpressure, `is_full`, draining, TLS pending states, and send-buffer mutations cannot cause duplicated sends, skipped validation, or parser confusion.
|
||||
- Review timer and wakeup handling for stale user data pointers, cross-thread queue misuse, and callbacks firing after associated connections/state have been freed.
|
||||
- Only report concurrency or race issues when a concrete event ordering can trigger memory corruption, unauthorized action, or reliable device/process crash.
|
||||
@@ -0,0 +1,26 @@
|
||||
# Security Scan Area: net_builtin
|
||||
|
||||
## Target
|
||||
|
||||
Area: net_builtin
|
||||
|
||||
This area covers the built-in TCP/IP stack and its selected L2 implementations.
|
||||
|
||||
Primary files:
|
||||
- `src/l2_*.c`
|
||||
- `src/net_builtin.c`
|
||||
|
||||
Perform a focused security review centered on these files and this security surface.
|
||||
|
||||
Supporting code elsewhere under `src/` may be inspected when necessary to establish reachability, data flow, state, validation, mitigation, or impact, but do not inspect or report code outside `src/` or independently review unrelated areas.
|
||||
|
||||
## Area-Specific Security Guidance
|
||||
|
||||
**Built-In TCP/IP Stack, Packet Parsing, and Embedded Network Drivers:**
|
||||
- Review Ethernet, ARP, IP, IPv6, ICMP, UDP, TCP, DHCP, SNTP, and driver receive/transmit paths for packet-length validation before reading protocol headers.
|
||||
- Check IP/TCP/UDP header length fields, options length, fragmentation/reassembly behavior, checksum handling, and payload offset calculations for underflow, overflow, or out-of-bounds access.
|
||||
- Look for malformed packets that can corrupt descriptor rings, RX/TX buffers, queue state, ARP tables, connection state, or timers.
|
||||
- Review interrupt handlers and bare-metal driver code for bounded processing loops. Flag IRQ paths where attacker-controlled traffic can cause livelock, starvation, descriptor ownership loss, or repeated processing of the same malformed frame.
|
||||
- Check DMA descriptor ownership transitions, memory barriers, cache coherency handling, and buffer length fields for race-like bugs that can cause memory corruption or stale packet reuse.
|
||||
- Review platform-specific drivers such as STM32, NXP, TI, Microchip, Renesas, Infineon, Wiznet, Cypress WiFi, cellular, and other built-in stack integrations for inconsistent bounds checks or endian conversions.
|
||||
- Treat a remotely reachable malformed Ethernet/IP/TCP/UDP packet that crashes, wedges, or corrupts an embedded target as reportable.
|
||||
@@ -0,0 +1,38 @@
|
||||
# Security Scan Area: protocols
|
||||
|
||||
## Target
|
||||
|
||||
Area: protocols
|
||||
|
||||
This area covers the smaller externally controlled protocol parsers and state machines other than HTTP, WebSocket, and TLS.
|
||||
|
||||
Primary files:
|
||||
- `src/modbus.c`
|
||||
- `src/rpc.c`
|
||||
- `src/sntp.c`
|
||||
- `src/mqtt.c`
|
||||
- `src/dns.c`
|
||||
|
||||
Perform a focused security review centered on these files and this security surface.
|
||||
|
||||
Supporting code elsewhere under `src/` may be inspected when necessary to establish reachability, data flow, state, validation, mitigation, or impact, but do not inspect or report code outside `src/` or independently review unrelated areas.
|
||||
|
||||
## Area-Specific Security Guidance
|
||||
|
||||
**MQTT Parser and Broker/Client State Machine Vulnerabilities:**
|
||||
- Review MQTT fixed header parsing, remaining-length decoding, variable-length integer loops, packet identifier handling, QoS transitions, topic extraction, property parsing, and MQTT 3.x vs MQTT 5 differences.
|
||||
- Look for malformed MQTT packets that can trigger out-of-bounds reads/writes, stack buffer overflow, heap corruption, assertion/crash, or parser desynchronization.
|
||||
- Specifically check SUBSCRIBE and UNSUBSCRIBE handling for unbounded numbers of topics, malformed topic length fields, missing QoS bytes, invalid QoS values, and writes to fixed-size response arrays.
|
||||
- Verify that PUBLISH handling enforces topic length, packet length, QoS rules, retained flag handling, packet identifier requirements, and message bounds before accessing fields.
|
||||
- Check protocol handlers for authorization bypass, cross-topic data exposure, wildcard topic mishandling, `$SYS` or reserved topic confusion, and unintended publish/subscribe access.
|
||||
- Review MQTT reconnect/resubscribe logic for stale connection state, packet identifier reuse bugs, and memory lifetime problems after close/reconnect.
|
||||
- Treat unauthenticated single-packet broker crash, client crash from malicious broker response, or memory corruption during MQTT parsing as reportable.
|
||||
|
||||
**DNS and mDNS Parsing and Name Resolution:**
|
||||
- Review DNS and mDNS message parsing for label length validation, compression pointer loops, pointer bounds, recursive expansion limits, integer overflow, malformed resource records, and out-of-bounds reads.
|
||||
- Check that DNS answers are matched to outstanding requests by transaction ID, question name/type/class, and source expectations where applicable.
|
||||
- Look for cache poisoning, response spoofing, or name confusion if DNS response validation is weak in client or resolver code.
|
||||
- Verify that DNS failures, timeouts, and retries cannot leave dangling active request state or corrupt connection state.
|
||||
|
||||
**Modbus, RPC, and SNTP:**
|
||||
- Review externally controlled lengths, framing, message parsing, and protocol state for malformed peer input that can cause parser desynchronization, state corruption, or a remotely triggerable crash or wedge.
|
||||
@@ -0,0 +1,44 @@
|
||||
# Security Scan Area: tls
|
||||
|
||||
## Target
|
||||
|
||||
Area: tls
|
||||
|
||||
This area covers the complete Mongoose TLS implementation represented by the primary files.
|
||||
|
||||
Primary files:
|
||||
- `src/tls_aes128.c`
|
||||
- `src/tls_builtin.c`
|
||||
- `src/tls_chacha20.c`
|
||||
- `src/tls_dummy.c`
|
||||
- `src/tls_mbed.c`
|
||||
- `src/tls_openssl.c`
|
||||
- `src/tls_p256_cortex.c`
|
||||
- `src/tls_p256.c`
|
||||
- `src/tls_p384.c`
|
||||
- `src/tls_rsa.c`
|
||||
|
||||
Perform a focused security review centered on these files and this security surface.
|
||||
|
||||
Supporting code elsewhere under `src/` may be inspected when necessary to establish reachability, data flow, state, validation, mitigation, or impact, but do not inspect or report code outside `src/` or independently review unrelated areas.
|
||||
|
||||
Connection, core, network, and helper code under `src/` may be inspected when needed to understand the complete TLS execution path.
|
||||
|
||||
## Area-Specific Security Guidance
|
||||
|
||||
### Mandatory High-Risk Coverage: Built-In TLS
|
||||
|
||||
The built-in TLS implementation (`MG_TLS_BUILTIN`) is a mandatory high-risk review target. Perform a substantial security analysis of all security-relevant built-in TLS code, including parsing, cryptographic processing, authentication and certificate validation, length and buffer handling, protocol state, cross-call behavior, and failure paths.
|
||||
|
||||
Trace attacker-controlled TLS peer input through normal client and server execution, including traffic received before peer authentication is complete. Report any credible memory-safety, authentication, confidentiality, integrity, state-corruption, or remotely triggerable availability vulnerability.
|
||||
|
||||
**TLS, Certificate, Hostname Verification, and Cryptographic Validation:**
|
||||
- Review all TLS backends, including built-in TLS, mbedTLS, OpenSSL, WolfSSL, and custom TLS integration, for equivalent security behavior where possible.
|
||||
- Flag certificate verification bypasses, accidental `skip_verification` use in security-sensitive defaults, missing hostname verification when `name`/SNI is available, or inconsistent validation across TLS backends.
|
||||
- For hostname verification, check wildcard handling carefully. A wildcard certificate identity must not match multiple DNS labels. For example, `*.example.com` must not match `a.b.example.com`.
|
||||
- Verify that wildcard matching is applied only to certificate presented identifiers, not to arbitrary reference identifiers or general DNS glob matching.
|
||||
- Review `mg_match` or other glob/matching helpers if used for certificate identity checks, authorization checks, filesystem paths, routing, or topic matching. Flag cases where glob semantics are broader than the security policy requires.
|
||||
- Check certificate parsing for DER/PEM boundary errors, length overflows, malformed ASN.1, extension parsing mistakes, name constraints, SAN vs CN precedence, embedded NUL, and invalid string type handling.
|
||||
- Review TLS record parsing, handshake transcript handling, key schedule, random generation, ECC (P-256, P-384) operations, AES-GCM nonce/tag handling, and error handling for memory safety and authentication failures.
|
||||
- Flag any behavior that silently falls back from verified TLS to unverified TLS or plaintext.
|
||||
- Check that TLS receive/send error paths cannot corrupt connection state, reuse freed buffers, or continue processing unauthenticated plaintext as encrypted data.
|
||||
@@ -0,0 +1,61 @@
|
||||
# Security Scan Area: web_http_auth
|
||||
|
||||
## Target
|
||||
|
||||
Area: web_http_auth
|
||||
|
||||
This area covers HTTP parsing and framing, HTTP-facing filesystem behavior, SSI, WebSocket, authentication/token/dashboard code, and HTTP-based OTA behavior.
|
||||
|
||||
Primary files:
|
||||
- `src/http.c`
|
||||
- `src/ssi.c`
|
||||
- `src/ws.c`
|
||||
- `src/jwt.c`
|
||||
- `src/dash.c`
|
||||
- `src/ota.c`
|
||||
|
||||
Perform a focused security review centered on these files and this security surface.
|
||||
|
||||
Supporting code elsewhere under `src/` may be inspected when necessary to establish reachability, data flow, state, validation, mitigation, or impact, but do not inspect or report code outside `src/` or independently review unrelated areas.
|
||||
|
||||
For OTA behavior, follow operations under `src/` into TLS, flash, filesystem, JSON, URL, or other support code when necessary, while keeping the review centered on the `web_http_auth` surface.
|
||||
|
||||
## Area-Specific Security Guidance
|
||||
|
||||
**HTTP Parser, Request Smuggling, and Message Framing:**
|
||||
- Verify that HTTP requests or responses containing both `Content-Length` and `Transfer-Encoding` are rejected or handled in a way that cannot cause front-end/back-end desynchronization. Treat CL+TE coexistence as security-relevant request smuggling risk.
|
||||
- Verify that duplicate `Content-Length`, duplicate `Transfer-Encoding`, conflicting transfer codings, malformed chunk sizes, chunk extensions, premature EOF, and body length mismatches cannot cause inconsistent body parsing.
|
||||
- Check whether HTTP/1.0, HTTP/1.1, and malformed protocol versions alter CL/TE handling unsafely. A message with both `Content-Length` and `Transfer-Encoding` should not be accepted merely because of version differences.
|
||||
- Look for parsing paths where headers are normalized differently from later lookup paths, including case folding, whitespace around colon, obs-fold/line folding, embedded NUL, duplicate headers, or partial header names.
|
||||
- Review URL decoding, query parsing, multipart parsing, upload handling, range handling, and chunked responses for bounds mistakes, truncation, or inconsistent decoded-vs-raw path decisions.
|
||||
- Check static file serving and SSI handling for path traversal through percent-encoding, double decoding, backslashes on Windows, mixed separators, dot-dot segments, absolute paths, drive letters, symlinks, embedded NUL, or alias/root_dir confusion.
|
||||
- Review HTTP authentication helper paths for parsing ambiguities in Basic auth, header extraction, credential truncation, empty username/password acceptance, or incorrect comparison behavior.
|
||||
- Check that HTTP upgrade to WebSocket validates method, headers, key, version, and connection semantics consistently before switching protocol state.
|
||||
|
||||
**WebSocket Framing and Upgrade Vulnerabilities:**
|
||||
- Review WebSocket frame parsing for extended payload length handling, 16-bit/64-bit length conversion, masking rules, fragmentation, continuation frames, control-frame length limits, close/ping/pong handling, and compression-related assumptions if present.
|
||||
- Check that client-to-server frames require masking and that malformed mask/payload boundaries cannot read or write out of bounds.
|
||||
- Look for state-machine confusion after HTTP upgrade, especially where partial frames, fragmented messages, or mixed HTTP/WebSocket bytes can cause parser desynchronization.
|
||||
- Check whether large or malformed frame lengths can cause integer overflow, memory corruption, uncontrolled allocation, connection lockup, or single-connection/device crash.
|
||||
- Verify that WebSocket message APIs preserve attacker-controlled binary data length correctly and do not pass non-null-terminated payloads to C-string functions.
|
||||
|
||||
**HTTP-Facing Filesystem Serving, Uploads, and SSI:**
|
||||
- Review `mg_http_serve_dir`, `mg_http_serve_file`, upload APIs, and SSI expansion for traversal, unauthorized file read/write, overwrite, truncation, or unexpected executable/include behavior.
|
||||
- Look for inconsistent handling of decoded and undecoded paths, double decoding, query string leakage into path, trailing slash confusion, NUL byte truncation, and case sensitivity differences.
|
||||
- Review multipart upload parsing for boundary confusion, filename traversal, field length overflow, partial-write behavior, and max-size enforcement.
|
||||
- Check SSI include handling for directory escape, recursive include loops, unintended disclosure of local files, and parsing of attacker-controlled SSI directives.
|
||||
- Verify that range requests and static response headers cannot cause out-of-bounds reads or disclose memory.
|
||||
|
||||
**OTA/Firmware Update and Device Dashboard Security:**
|
||||
- Review firmware update endpoints and code paths for missing authentication, authorization bypass, path confusion, arbitrary write, unsafe flash offset/length handling, and failure to verify firmware authenticity where the code claims secure update behavior.
|
||||
- Check update chunk parsing for integer overflow, flash boundary crossing, rollback/downgrade exposure, incomplete image bootability, and crash/power-loss unsafe state transitions.
|
||||
- Flag any code path where unauthenticated network input can write firmware, configuration, filesystem contents, flash pages, bootloader state, or device identity material.
|
||||
- Review device dashboard code for security-sensitive default routes, credentials, upload handlers, debug endpoints, config mutation endpoints, and CORS/header behavior.
|
||||
|
||||
**Authentication, Authorization, and Access-Control Patterns in Mongoose Integrations:**
|
||||
- Review helper APIs and code paths that implement HTTP auth, cookies, sessions, tokens, device dashboards, REST APIs, upload endpoints, and firmware update endpoints.
|
||||
- Flag authentication bypasses caused by prefix route matching, glob matching, decoded-vs-raw URI confusion, case normalization mistakes, missing boundary checks, or default-open sensitive endpoints.
|
||||
- Check for authorization decisions made in client-side JavaScript only. Client-side checks are not security boundaries.
|
||||
- Review CORS and origin checks only when they protect credentialed sensitive operations and the exploit path is concrete.
|
||||
- Look for credential comparison bugs, truncation of usernames/passwords, accepting empty credentials, parsing only part of an Authorization header, or treating malformed credentials as anonymous-but-authorized.
|
||||
- Flag sensitive data exposure when secrets, credentials, tokens, private keys, firmware signing material, PII, or device identity values can be read by unauthorized remote clients or logged from attacker-triggered paths.
|
||||
@@ -5,7 +5,7 @@
|
||||
3. The exploit depends on NULL, dangling, fake, or corrupted pointers/objects supplied by a local caller, including invalid `mg_connection`, `mg_mgr`, `mg_str`, iobufs, or callback data.
|
||||
4. The exploit requires manually mutating internal structs, flags, parser offsets, descriptor indexes, connection state, TLS state, MQTT/WebSocket state, or filesystem state into impossible combinations.
|
||||
5. The bug exists only in tests, fuzz harnesses, benchmarks, synthetic unit tests, or debug-only code, with no shared production logic.
|
||||
6. The issue is only in a tutorial/demo/example with no realistic production relevance or sensitive operation.
|
||||
6. The affected file is outside `src/`.
|
||||
7. The attacker must edit source code, build flags, compile-time macros, TLS backend choices, filesystem roots, or local device configuration.
|
||||
8. The attacker must directly write hardware registers, MMIO, DMA descriptors, descriptor ownership bits, IRQ state, cache registers, or peripheral configuration.
|
||||
9. Generic DoS from high traffic volume, many valid connections, slow clients, missing quotas, missing rate limits, or ordinary resource saturation.
|
||||
@@ -14,6 +14,8 @@
|
||||
12. Generic crypto advice with no concrete impact on authentication, confidentiality, integrity, downgrade resistance, hostname verification, certificate validation, key handling, nonces/tags, or plaintext fallback.
|
||||
13. Speculative protocol claims without a specific malformed input shape, reachable parser path, and concrete security impact.
|
||||
14. Pure application policy gaps: no default auth, no CSRF, no user access model, no account lockout, no audit logs, or no password policy.
|
||||
15. OTA findings based only on allowing plain HTTP, lacking firmware hash/signature verification, or permitting update URLs to change scheme/host when this is documented/configurable behavior and no existing security check is bypassed.
|
||||
16. Filter TCP findings that rely primarily on predictability or insufficient entropy in protocol sequence-state generation, including attacks based on guessing or reconstructing connection state without observing the traffic.
|
||||
|
||||
# SIGNAL QUALITY CRITERIA
|
||||
**For remaining findings, assess:**
|
||||
@@ -25,7 +27,7 @@
|
||||
6. Does the report provide exact files, functions, line numbers, data flow, bounds reasoning, state reasoning, or a minimal malformed input?
|
||||
7. Is the malformed input shape clear, such as CL+TE HTTP, oversized MQTT SUBSCRIBE, malformed WebSocket length, DNS compression loop, wildcard TLS certificate, invalid packet header length, or encoded path traversal?
|
||||
8. Is the severity justified by reachability and impact?
|
||||
9. Is this a library/helper/default/example-pattern vulnerability rather than only arbitrary application misuse?
|
||||
9. Is this a library/helper/default vulnerability rather than only arbitrary application misuse?
|
||||
10. Is the fix specific enough to act on: reject CL+TE, bound arrays, validate DNS pointers, enforce one-label wildcard matching, check packet lengths, canonicalize paths, or bound RX loops?
|
||||
11. Is a DoS finding caused by a concrete implementation flaw rather than volume, slow clients, missing quotas, or capacity limits?
|
||||
12. In embedded context, can a small malformed input reliably crash, wedge, corrupt, or permanently disrupt a device/process?
|
||||
@@ -42,24 +44,26 @@
|
||||
8. Keep unsafe `mg_match` or glob use at security boundaries: certificate identity, route authorization, filesystem policy, MQTT topic authorization, or access control.
|
||||
9. Keep DNS/mDNS label, compression pointer, pointer loop, RR length, transaction matching, spoofing, OOB, or infinite-loop issues with external reachability.
|
||||
10. Keep filesystem traversal through exposed serving/upload/SSI paths, including encoding, double decoding, backslashes, dot-dot, absolute paths, drive letters, symlinks, NULs, alias/root confusion, and upload filename traversal.
|
||||
11. Keep OTA/update issues allowing unauthenticated or improperly validated writes to firmware, flash, config, filesystem, boot state, device identity, or update metadata.
|
||||
12. Keep embedded packet parser bugs in Ethernet, ARP, IP, IPv6, ICMP, UDP, TCP, DHCP, SNTP, or drivers when malformed traffic causes OOB, overflow, descriptor corruption, queue corruption, state corruption, or crash.
|
||||
13. Keep IRQ, polling, RX descriptor, and packet-loop livelock/starvation/wedge bugs caused by received traffic.
|
||||
14. Keep event-lifecycle bugs reachable from external input: UAF, stale pointer, double close, callback lifetime error, iobuf mutation after close, protocol transition confusion, timer/wakeup after free.
|
||||
15. Keep externally reachable `mg_str` null-termination mistakes causing OOB read, data leak, auth bypass, parser confusion, or crash.
|
||||
16. Keep integer conversion bugs where external lengths affect allocation, copying, parsing, or bounds checks.
|
||||
17. Filter parser/helper reports based only on a local caller passing invalid pointers, fake lengths, fake structs, or corrupted objects.
|
||||
18. Filter reports requiring direct mutation of connection internals such as protocol flags, closing flags, handler pointers, iobuf fields, callback pointers, or user data.
|
||||
19. Filter reports requiring attacker control of `MG_ENABLE_*`, TLS backend macros, filesystem backend macros, compiler flags, debug flags, sanitizer settings, or platform defines.
|
||||
20. Filter direct hardware-access reports requiring writes to registers, DMA descriptors, MMIO, IRQ state, cache controls, or peripheral configuration.
|
||||
21. Filter generic “Mongoose lacks authentication” reports unless a helper, example, route pattern, auth parser, MQTT broker behavior, dashboard, upload handler, or OTA endpoint creates a concrete issue.
|
||||
22. Filter generic rate-limit/quota/connection-cap/upload-cap findings unless a specific bug lets a small malformed input trigger crash, corruption, infinite loop, or disproportionate exhaustion.
|
||||
23. Filter purely theoretical UB, portability, unaligned-access, or signed-overflow reports with no supported-platform impact and no realistic external trigger.
|
||||
24. Filter RFC non-compliance without concrete smuggling, bypass, memory corruption, data exposure, state corruption, or crash.
|
||||
25. Keep security-relevant production-like examples: dashboards, upload servers, MQTT brokers, firmware update flows, auth examples, TLS examples, and filesystem-serving examples.
|
||||
26. Keep client-side findings caused by malicious servers, brokers, DNS responders, TLS peers, WebSocket peers, or HTTP peers.
|
||||
27. Keep callback-related findings only when Mongoose invokes the callback normally and then unsafely continues using freed or mutated state.
|
||||
28. Keep availability findings when one malformed packet, request, certificate, DNS response, MQTT message, WebSocket frame, or small sequence can reliably crash, wedge, corrupt, or disrupt a Mongoose target.
|
||||
29. Keep custom backend findings only when the issue is in Mongoose’s backend interface, default backend, documented backend contract, or common backend implementation.
|
||||
30. When uncertain, keep findings with clear external input, reachable code path, and concrete impact in Mongoose library/protocol/driver code; filter internal-access, impossible-state, local-misuse, generic-hardening, and speculative reports.
|
||||
31. For dashboard reports, do not infer unauthenticated access from a route-specific check alone; account for the broader authentication flow. Keep concrete reachable bugs such as path traversal, unsafe decoded paths, or file read/write/delete issues.
|
||||
11. Keep embedded packet parser bugs in Ethernet, ARP, IP, IPv6, ICMP, UDP, TCP, DHCP, SNTP, or drivers when malformed traffic causes OOB, overflow, descriptor corruption, queue corruption, state corruption, or crash.
|
||||
12. Keep IRQ, polling, RX descriptor, and packet-loop livelock/starvation/wedge bugs caused by received traffic.
|
||||
13. Keep event-lifecycle bugs reachable from external input: UAF, stale pointer, double close, callback lifetime error, iobuf mutation after close, protocol transition confusion, timer/wakeup after free.
|
||||
14. Keep externally reachable `mg_str` null-termination mistakes causing OOB read, data leak, auth bypass, parser confusion, or crash.
|
||||
15. Keep integer conversion bugs where external lengths affect allocation, copying, parsing, or bounds checks.
|
||||
16. Filter parser/helper reports based only on a local caller passing invalid pointers, fake lengths, fake structs, or corrupted objects.
|
||||
17. Filter reports requiring direct mutation of connection internals such as protocol flags, closing flags, handler pointers, iobuf fields, callback pointers, or user data.
|
||||
18. Filter reports requiring attacker control of `MG_ENABLE_*`, TLS backend macros, filesystem backend macros, compiler flags, debug flags, sanitizer settings, or platform defines.
|
||||
19. Filter direct hardware-access reports requiring writes to registers, DMA descriptors, MMIO, IRQ state, cache controls, or peripheral configuration.
|
||||
20. Filter generic “Mongoose lacks authentication” reports unless a helper, route pattern, auth parser, MQTT broker behavior, dashboard, upload handler, or OTA endpoint creates a concrete issue.
|
||||
21. Filter generic rate-limit/quota/connection-cap/upload-cap findings unless a specific bug lets a small malformed input trigger crash, corruption, infinite loop, or disproportionate exhaustion.
|
||||
22. Filter purely theoretical UB, portability, unaligned-access, or signed-overflow reports with no supported-platform impact and no realistic external trigger.
|
||||
23. Filter RFC non-compliance without concrete smuggling, bypass, memory corruption, data exposure, state corruption, or crash.
|
||||
24. Keep security-relevant production library code under `src/` for dashboards, uploads, MQTT, firmware updates, authentication, TLS, and filesystem serving.
|
||||
25. Keep client-side findings caused by malicious servers, brokers, DNS responders, TLS peers, WebSocket peers, or HTTP peers.
|
||||
26. Keep callback-related findings only when Mongoose invokes the callback normally and then unsafely continues using freed or mutated state.
|
||||
27. Keep availability findings when one malformed packet, request, certificate, DNS response, MQTT message, WebSocket frame, or small sequence can reliably crash, wedge, corrupt, or disrupt a Mongoose target.
|
||||
28. Keep custom backend findings only when the issue is in Mongoose’s backend interface, default backend, documented backend contract, or common backend implementation.
|
||||
29. When uncertain, keep findings with clear external input, reachable code path, and concrete impact in Mongoose library/protocol/driver code; filter internal-access, impossible-state, local-misuse, generic-hardening, and speculative reports.
|
||||
30. For dashboard reports, do not infer unauthenticated access from a route-specific check alone; account for the broader authentication flow. Keep concrete reachable bugs such as path traversal, unsafe decoded paths, or file read/write/delete issues.
|
||||
31. Filter reports regarding DNS transaction ID predictability or lack of strict question-name matching in DNS response processing, as these are acknowledged design limitations of the current implementation.
|
||||
32. Filter reports that unauthenticated SNTP time affects TLS certificate validity when Mongoose only performs the SNTP sync if mg_boot_timestamp_ms has not already been initialized.
|
||||
33. Filter TCP ISN predictability, weak sequence-number generation, RFC 6528 non-compliance, blind/off-path handshake spoofing, forged RST, and sequence-number-guessing reports in the built-in TCP/IP stack.
|
||||
@@ -1,24 +1,35 @@
|
||||
Review the complete PR diff. This contains all code changes in the PR.
|
||||
Review only changes under `src/` in the complete PR diff. Ignore all other changes in the PR.
|
||||
|
||||
# OBJECTIVE
|
||||
|
||||
Perform a security-focused code review to identify HIGH-CONFIDENCE security vulnerabilities that could have real exploitation potential.
|
||||
|
||||
This is not a general code review - focus ONLY on security implications newly added by this PR. Do not comment on existing security concerns.
|
||||
This is not a general code review - focus ONLY on security implications newly added by this PR under `src/`. Do not analyze, count, or report changes outside `src/`. Do not comment on existing security concerns.
|
||||
|
||||
For this task, you will rely on two additional files, `resources/specs/claude-security-scan-instructions.md` detailing security scan instructions specific to the repository and `resources/specs/claude-false-positive-filtering.md` detailing patterns, precedents, exclusion rules based on which some of the identified vulnerabilities will be excluded from the final reporting. The instructions, rules and criterias found in those two files are MANDATORY TO BE HONOURED and your security audit will have to take all of them into account.
|
||||
For this task, you will rely on the specified shared security-review sections in `resources/specs/claude-security-scan.md`, the specified security-guidance sections in the six `resources/specs/areas/*.md` prompts listed below, and `resources/specs/claude-false-positive-filtering.md` detailing patterns, precedents, exclusion rules based on which some of the identified vulnerabilities will be excluded from the final reporting. The instructions, rules and criterias found in those referenced sections and the filtering file are MANDATORY TO BE HONOURED and your security audit will have to take all of them into account.
|
||||
|
||||
YOU MUST READ AND ANALYZE BOTH OF THESE FILES BEFORE COMMENCING THE SECURITY AUDIT. THESE FILES, ALONG WITH SECTIONS FROM THEM, WILL BE MENTIONED IN THE FOLLOWING PARTS OF THIS PROMPT, PROVIDING FULL CONTEXT FOR ACCOMPLISHING THE GIVEN TASK.
|
||||
YOU MUST READ AND ANALYZE THE SPECIFIED SECTIONS AND THE FILTERING FILE BEFORE COMMENCING THE SECURITY AUDIT. THESE SOURCES WILL BE MENTIONED IN THE FOLLOWING PARTS OF THIS PROMPT, PROVIDING FULL CONTEXT FOR ACCOMPLISHING THE GIVEN TASK.
|
||||
|
||||
# CRITICAL INSTRUCTIONS
|
||||
1. MINIMIZE FALSE POSITIVES: Only flag issues where you're >80% confident of actual exploitability
|
||||
2. AVOID NOISE: Skip theoretical issues, style concerns, or low-impact findings
|
||||
3. UNDERSTANDING CONTEXT AND SCOPE OF REVIEW: In the security scan instructions file `resources/specs/claude-security-scan-instructions.md`, read the `External Attacker Threat Model` and `Mongoose-Specific Security Review Scope` subsections found in the `SECURITY SCAN CONTEXT` main section for a full understanding of the scanning model, code review context and the perspective from which the scanning is going to take place.
|
||||
3. UNDERSTANDING CONTEXT AND SCOPE OF REVIEW: In `resources/specs/claude-security-scan.md`, read only the `External Attacker Threat Model` and `Mongoose-Specific Security Review Scope` subsections found in the `SECURITY SCAN CONTEXT` main section for a full understanding of the scanning model, code review context and the perspective from which the scanning is going to take place. Do not apply unrelated full-scan instructions from that file. In particular, do not inherit assigned-area scan scope, full-scan JSON output requirements, full-scan `review_completed` semantics, instructions to scan long-standing code independently of the PR, or area-selection workflow behavior. This PR prompt's scope, methodology, validation, and output rules remain authoritative.
|
||||
4. TREAT EXCLUSIONS AS MANDATORY. The exclusions section below is mandatory. If a potential finding falls under an exclusion, do not report it. If a finding partially overlaps an exclusion, only report it if there is a concrete, non-excluded security impact.
|
||||
|
||||
# SECURITY CATEGORIES TO EXAMINE
|
||||
|
||||
In the security scan instructions file `resources/specs/claude-security-scan-instructions.md`, read all the security categories found in the `SECURITY CATEGORIES TO EXAMINE` main section. THESE ARE THE SECURITY CATEGORIES WHICH NEED TO BE EXAMINED.
|
||||
Read the general/common security categories in the `GENERAL SECURITY CATEGORIES TO EXAMINE` section of `resources/specs/claude-security-scan.md`.
|
||||
|
||||
Also read the `Area-Specific Security Guidance` section from every area prompt:
|
||||
|
||||
* `resources/specs/areas/common_misc.md`
|
||||
* `resources/specs/areas/mongoose_core.md`
|
||||
* `resources/specs/areas/web_http_auth.md`
|
||||
* `resources/specs/areas/tls.md`
|
||||
* `resources/specs/areas/net_builtin.md`
|
||||
* `resources/specs/areas/protocols.md`
|
||||
|
||||
Use only the security guidance from the area prompts and ignore their full-scan-specific `Target` sections. Consider the complete set of common and area-specific security categories for every file the PR modifies under `src/`. Do not choose one area or make this PR review area-scoped. Ignore all other changed paths and report only security vulnerabilities introduced by the PR under `src/`.
|
||||
|
||||
Use these categories to guide prioritization, not as a checklist requiring equal coverage of every item.
|
||||
|
||||
@@ -37,7 +48,7 @@ Phase 2 - Comparative Analysis:
|
||||
- Flag code that introduces new attack surfaces
|
||||
|
||||
Phase 3 - Vulnerability Assessment:
|
||||
- Examine each modified file for security implications
|
||||
- Examine each modified file under `src/` for security implications
|
||||
- Trace data flow from user inputs to sensitive operations
|
||||
- Look for privilege boundaries being crossed unsafely
|
||||
- Identify injection points and unsafe deserialization
|
||||
@@ -110,6 +121,6 @@ Better to miss some theoretical issues than flood the report with false positive
|
||||
|
||||
Read the `HARD EXCLUSIONS` section found in the `resources/specs/claude-false-positive-filtering.md` filtering file, for each possbile vulnerability found, check if it matches any items from the patterns listed in that section and if that is the case, exclude that vulnerability from the report. DO NOT REPORT IT if a match is found. Also, take into account the project-specific filtering and reporting precedents found in the `PRECEDENTS` section of the filtering file.
|
||||
|
||||
Begin your analysis now. Use the repository exploration tools to understand the codebase context, then analyze the PR changes for security implications.
|
||||
Begin your analysis now. Use the repository exploration tools to understand the `src/` codebase context, then analyze only the PR changes under `src/` for security implications.
|
||||
|
||||
Your final action must be posting the PR comment. You should not reply again after posting the PR comment.
|
||||
@@ -1,121 +0,0 @@
|
||||
# SECURITY SCAN CONTEXT
|
||||
|
||||
**External Attacker Threat Model:**
|
||||
- Analyze vulnerabilities from the perspective of an external attacker, not from the perspective of a developer, local user, debugger, test harness, or code running inside the process.
|
||||
- The attacker can provide bytes through real external interfaces exposed by Mongoose or a Mongoose-based application: TCP, UDP, HTTP, WebSocket, MQTT, DNS, mDNS, TLS handshakes/certificates, Ethernet/IP/TCP/UDP packets, uploaded files, request paths, query strings, headers, message bodies, malicious server responses, and other protocol inputs.
|
||||
- The attacker cannot directly call internal C functions, pass arbitrary invalid pointers, mutate internal structs, invoke callbacks manually, edit compile-time macros, alter build flags, write to hardware registers, control DMA descriptors directly, attach a debugger, or run code inside the target process.
|
||||
- For a finding to be valid, show how attacker-controlled external input reaches the vulnerable code path through normal library/application execution.
|
||||
- Do not report findings that require impossible internal states unless the finding also explains how an external input can cause that state.
|
||||
- Do not report “API misuse” as a library vulnerability unless the API contract makes the insecure use likely, undocumented, or unavoidable.
|
||||
- For embedded networking and driver code, assume the attacker can send malformed network traffic to the device, but cannot directly manipulate hardware registers, memory-mapped I/O, DMA rings, or interrupt state except as a consequence of packets processed by the driver.
|
||||
|
||||
**Mongoose-Specific Security Review Scope:**
|
||||
- Treat this repository as a security-sensitive embedded C/C++ networking stack. Mongoose processes attacker-controlled bytes from TCP, UDP, HTTP, WebSocket, MQTT, DNS, mDNS, TLS, filesystem upload/download paths, and built-in TCP/IP drivers.
|
||||
- In this repository, remotely triggerable denial of service is security-relevant when it is caused by a concrete implementation flaw such as memory corruption, parser abort, assertion failure, stack exhaustion, infinite loop, unbounded recursion, IRQ livelock, connection state corruption, descriptor-ring corruption, or a single unauthenticated packet causing process/device crash.
|
||||
- Prioritize HIGH and MEDIUM severity findings. Include LOW only when unusually concrete, externally relevant, and useful. Do not report vague robustness concerns. Every finding must identify the attacker-controlled input, affected parser/state machine/buffer, concrete impact, and the exact code path.
|
||||
- Prefer findings with a plausible packet/request/message shape. For network protocol issues, describe the relevant malformed HTTP request, MQTT packet, WebSocket frame, DNS message, TLS handshake/certificate input, Ethernet/IP/TCP/UDP packet, or filesystem request.
|
||||
- Because this is C/C++, memory safety vulnerabilities are in scope. Buffer overflows, stack overflows, heap overflows, out-of-bounds reads/writes, use-after-free, double free, integer overflow leading to memory corruption, invalid pointer lifetime, and unsafe length conversions must be reviewed carefully.
|
||||
- Do not dismiss issues merely because they are “only DoS” if the issue is remotely triggerable against an embedded server, broker, device dashboard, firmware-update endpoint, or network-facing parser.
|
||||
|
||||
# SECURITY CATEGORIES TO EXAMINE
|
||||
|
||||
**C Memory Safety and Length-Handling Vulnerabilities:**
|
||||
- Look for writes to fixed-size stack or heap buffers where the loop bound is derived from attacker-controlled protocol fields, including topic counts, header counts, chunk counts, multipart parts, DNS labels, WebSocket fragments, TCP/IP options, or filesystem path components.
|
||||
- Check all conversions between `size_t`, `int`, `long`, `uint16_t`, `uint32_t`, and signed protocol lengths. Flag integer truncation, wraparound, negative-to-large conversion, or off-by-one behavior that can affect allocation, parsing, copying, or bounds checks.
|
||||
- Review any `memcpy`, `memmove`, `memcmp`, `snprintf`, `mg_snprintf`, `mg_xprintf`, iobuf append/delete/resize operation, and manual pointer increment where source length can originate from network input.
|
||||
- Treat `struct mg_str` values as non-null-terminated unless the code proves otherwise. Flag use of `strlen`, `%s`, `strcmp`, `strstr`, or C-string APIs on attacker-controlled `mg_str.buf` unless length and termination are guaranteed.
|
||||
- Check for parser code that advances pointers without proving that enough bytes remain. Flag any read of fixed-size fields, variable-length integers, DNS labels, TLS vectors, MQTT remaining length, WebSocket extended lengths, or HTTP chunks before bounds are validated.
|
||||
- Look for inconsistent ownership and lifetime of buffers referenced by `mg_str`, connection receive/send iobufs, TLS buffers, filesystem buffers, and event callback data.
|
||||
- Check for reentrancy hazards in event callbacks where user handlers may close a connection, mutate iobufs, start TLS, change protocol handlers, or free state while protocol code continues using stale pointers.
|
||||
|
||||
**HTTP Parser, Request Smuggling, and Message Framing:**
|
||||
- Verify that HTTP requests or responses containing both `Content-Length` and `Transfer-Encoding` are rejected or handled in a way that cannot cause front-end/back-end desynchronization. Treat CL+TE coexistence as security-relevant request smuggling risk.
|
||||
- Verify that duplicate `Content-Length`, duplicate `Transfer-Encoding`, conflicting transfer codings, malformed chunk sizes, chunk extensions, premature EOF, and body length mismatches cannot cause inconsistent body parsing.
|
||||
- Check whether HTTP/1.0, HTTP/1.1, and malformed protocol versions alter CL/TE handling unsafely. A message with both `Content-Length` and `Transfer-Encoding` should not be accepted merely because of version differences.
|
||||
- Look for parsing paths where headers are normalized differently from later lookup paths, including case folding, whitespace around colon, obs-fold/line folding, embedded NUL, duplicate headers, or partial header names.
|
||||
- Review URL decoding, query parsing, multipart parsing, upload handling, range handling, and chunked responses for bounds mistakes, truncation, or inconsistent decoded-vs-raw path decisions.
|
||||
- Check static file serving and SSI handling for path traversal through percent-encoding, double decoding, backslashes on Windows, mixed separators, dot-dot segments, absolute paths, drive letters, symlinks, embedded NUL, or alias/root_dir confusion.
|
||||
- Review HTTP authentication helper paths for parsing ambiguities in Basic auth, header extraction, credential truncation, empty username/password acceptance, or incorrect comparison behavior.
|
||||
- Check that HTTP upgrade to WebSocket validates method, headers, key, version, and connection semantics consistently before switching protocol state.
|
||||
|
||||
**MQTT Parser and Broker/Client State Machine Vulnerabilities:**
|
||||
- Review MQTT fixed header parsing, remaining-length decoding, variable-length integer loops, packet identifier handling, QoS transitions, topic extraction, property parsing, and MQTT 3.x vs MQTT 5 differences.
|
||||
- Look for malformed MQTT packets that can trigger out-of-bounds reads/writes, stack buffer overflow, heap corruption, assertion/crash, or parser desynchronization.
|
||||
- Specifically check SUBSCRIBE and UNSUBSCRIBE handling for unbounded numbers of topics, malformed topic length fields, missing QoS bytes, invalid QoS values, and writes to fixed-size response arrays.
|
||||
- Verify that PUBLISH handling enforces topic length, packet length, QoS rules, retained flag handling, packet identifier requirements, and message bounds before accessing fields.
|
||||
- Check broker examples and protocol handlers for authorization bypass, cross-topic data exposure, wildcard topic mishandling, `$SYS` or reserved topic confusion, and unintended publish/subscribe access.
|
||||
- Review MQTT reconnect/resubscribe logic for stale connection state, packet identifier reuse bugs, and memory lifetime problems after close/reconnect.
|
||||
- Treat unauthenticated single-packet broker crash, client crash from malicious broker response, or memory corruption during MQTT parsing as reportable.
|
||||
|
||||
**WebSocket Framing and Upgrade Vulnerabilities:**
|
||||
- Review WebSocket frame parsing for extended payload length handling, 16-bit/64-bit length conversion, masking rules, fragmentation, continuation frames, control-frame length limits, close/ping/pong handling, and compression-related assumptions if present.
|
||||
- Check that client-to-server frames require masking and that malformed mask/payload boundaries cannot read or write out of bounds.
|
||||
- Look for state-machine confusion after HTTP upgrade, especially where partial frames, fragmented messages, or mixed HTTP/WebSocket bytes can cause parser desynchronization.
|
||||
- Check whether large or malformed frame lengths can cause integer overflow, memory corruption, uncontrolled allocation, connection lockup, or single-connection/device crash.
|
||||
- Verify that WebSocket message APIs preserve attacker-controlled binary data length correctly and do not pass non-null-terminated payloads to C-string functions.
|
||||
|
||||
**TLS, Certificate, Hostname Verification, and Cryptographic Validation:**
|
||||
- Review all TLS backends, including built-in TLS, mbedTLS, OpenSSL, WolfSSL, and custom TLS integration, for equivalent security behavior where possible.
|
||||
- Flag certificate verification bypasses, accidental `skip_verification` use in security-sensitive examples or defaults, missing hostname verification when `name`/SNI is available, or inconsistent validation across TLS backends.
|
||||
- For hostname verification, check wildcard handling carefully. A wildcard certificate identity must not match multiple DNS labels. For example, `*.example.com` must not match `a.b.example.com`.
|
||||
- Verify that wildcard matching is applied only to certificate presented identifiers, not to arbitrary reference identifiers or general DNS glob matching.
|
||||
- Review `mg_match` or other glob/matching helpers if used for certificate identity checks, authorization checks, filesystem paths, routing, or topic matching. Flag cases where glob semantics are broader than the security policy requires.
|
||||
- Check certificate parsing for DER/PEM boundary errors, length overflows, malformed ASN.1, extension parsing mistakes, name constraints, SAN vs CN precedence, embedded NUL, and invalid string type handling.
|
||||
- Review TLS record parsing, handshake transcript handling, key schedule, random generation, ECC/X25519 operations, AES-GCM nonce/tag handling, and error handling for memory safety and authentication failures.
|
||||
- Flag any behavior that silently falls back from verified TLS to unverified TLS or plaintext.
|
||||
- Check that TLS receive/send error paths cannot corrupt connection state, reuse freed buffers, or continue processing unauthenticated plaintext as encrypted data.
|
||||
|
||||
**DNS, mDNS, URL Parsing, and Name Resolution:**
|
||||
- Review DNS and mDNS message parsing for label length validation, compression pointer loops, pointer bounds, recursive expansion limits, integer overflow, malformed resource records, and out-of-bounds reads.
|
||||
- Check that DNS answers are matched to outstanding requests by transaction ID, question name/type/class, and source expectations where applicable.
|
||||
- Look for cache poisoning, response spoofing, or name confusion if DNS response validation is weak in client or resolver code.
|
||||
- Review URL parsing for scheme, host, port, IPv6 literal, userinfo, percent-encoding, embedded NUL, empty host, default port, and path/query boundary confusion.
|
||||
- Check for SSRF-relevant behavior only when attacker input can control the destination host, protocol, or network boundary. Path-only control is not sufficient.
|
||||
- Verify that DNS failures, timeouts, and retries cannot leave dangling active request state or corrupt connection state.
|
||||
|
||||
**Built-In TCP/IP Stack, Packet Parsing, and Embedded Network Drivers:**
|
||||
- Review Ethernet, ARP, IP, IPv6, ICMP, UDP, TCP, DHCP, SNTP, and driver receive/transmit paths for packet-length validation before reading protocol headers.
|
||||
- Check IP/TCP/UDP header length fields, options length, fragmentation/reassembly behavior, checksum handling, and payload offset calculations for underflow, overflow, or out-of-bounds access.
|
||||
- Look for malformed packets that can corrupt descriptor rings, RX/TX buffers, queue state, ARP tables, connection state, or timers.
|
||||
- Review interrupt handlers and bare-metal driver code for bounded processing loops. Flag IRQ paths where attacker-controlled traffic can cause livelock, starvation, descriptor ownership loss, or repeated processing of the same malformed frame.
|
||||
- Check DMA descriptor ownership transitions, memory barriers, cache coherency handling, and buffer length fields for race-like bugs that can cause memory corruption or stale packet reuse.
|
||||
- Review platform-specific drivers such as STM32, NXP, TI, Microchip, Renesas, Infineon, Wiznet, Cypress WiFi, cellular, and other built-in stack integrations for inconsistent bounds checks or endian conversions.
|
||||
- Treat a remotely reachable malformed Ethernet/IP/TCP/UDP packet that crashes, wedges, or corrupts an embedded target as reportable.
|
||||
|
||||
**Filesystem Serving, Uploads, Packed Filesystems, and SSI:**
|
||||
- Review `mg_http_serve_dir`, `mg_http_serve_file`, upload APIs, packed filesystem code, and SSI expansion for traversal, unauthorized file read/write, overwrite, truncation, or unexpected executable/include behavior.
|
||||
- Check path canonicalization across POSIX, Windows, embedded filesystems, packed filesystems, and custom `mg_fs` implementations.
|
||||
- Look for inconsistent handling of decoded and undecoded paths, double decoding, query string leakage into path, trailing slash confusion, NUL byte truncation, and case sensitivity differences.
|
||||
- Review multipart upload parsing for boundary confusion, filename traversal, field length overflow, partial-write behavior, and max-size enforcement.
|
||||
- Check SSI include handling for directory escape, recursive include loops, unintended disclosure of local files, and parsing of attacker-controlled SSI directives.
|
||||
- Verify that range requests and static response headers cannot cause out-of-bounds reads or disclose memory.
|
||||
|
||||
**OTA/Firmware Update and Device Dashboard Security:**
|
||||
- Review firmware update endpoints and examples for missing authentication, authorization bypass, path confusion, arbitrary write, unsafe flash offset/length handling, and failure to verify firmware authenticity where the code claims secure update behavior.
|
||||
- Check update chunk parsing for integer overflow, flash boundary crossing, rollback/downgrade exposure, incomplete image bootability, and crash/power-loss unsafe state transitions.
|
||||
- Flag any code path where unauthenticated network input can write firmware, configuration, filesystem contents, flash pages, bootloader state, or device identity material.
|
||||
- Review device dashboard examples for security-sensitive default routes, credentials, upload handlers, debug endpoints, config mutation endpoints, and CORS/header behavior when they are likely to be copied into production.
|
||||
- Do not report ordinary insecure demo behavior unless it creates a realistic copy-paste production vulnerability or affects library code.
|
||||
|
||||
**Protocol State Machine and Event-Driven Lifecycle Bugs:**
|
||||
- Review transitions between listening, accepted, connecting, resolving, TLS handshaking, HTTP, WebSocket, MQTT, draining, closing, and closed states.
|
||||
- Check whether malformed input can cause protocol handlers to process data in the wrong state, process stale data after close, or invoke callbacks with invalid event data.
|
||||
- Look for use-after-free or stale pointer use when callbacks call `mg_close_conn`, mark a connection closing, mutate iobufs, change protocol handlers, or initiate TLS/WebSocket/MQTT transitions.
|
||||
- Verify that partial reads/writes, backpressure, `is_full`, draining, TLS pending states, and send-buffer mutations cannot cause duplicated sends, skipped validation, or parser confusion.
|
||||
- Review timer and wakeup handling for stale user data pointers, cross-thread queue misuse, and callbacks firing after associated connections/state have been freed.
|
||||
- Only report concurrency or race issues when a concrete event ordering can trigger memory corruption, unauthorized action, or reliable device/process crash.
|
||||
|
||||
**Cross-Platform C Portability With Security Impact:**
|
||||
- Review code paths whose security depends on struct packing, alignment, endianness, pointer size, signedness of `char`, integer width, or platform-specific filesystem/network behavior.
|
||||
- Check for unaligned memory access on embedded targets when parsing attacker-controlled network packets.
|
||||
- Verify endian conversions for Ethernet/IP/TCP/UDP/DNS/MQTT/TLS fields before bounds checks and allocations.
|
||||
- Look for platform branches where one backend performs validation and another does not, especially across UNIX, Windows, lwIP, Zephyr, FreeRTOS-like systems, bare-metal, and built-in TCP/IP stack modes.
|
||||
- Flag portability bugs only when they create a realistic vulnerability on a supported platform, not merely theoretical undefined behavior.
|
||||
|
||||
**Authentication, Authorization, and Access-Control Patterns in Mongoose Integrations:**
|
||||
- Review examples and helper APIs that implement HTTP auth, cookies, sessions, tokens, device dashboards, REST APIs, MQTT topic access, upload endpoints, and firmware update endpoints.
|
||||
- Flag authentication bypasses caused by prefix route matching, glob matching, decoded-vs-raw URI confusion, case normalization mistakes, missing boundary checks, or default-open sensitive endpoints.
|
||||
- Check for authorization decisions made in client-side JavaScript only. Client-side checks are not security boundaries.
|
||||
- Review CORS and origin checks only when they protect credentialed sensitive operations and the exploit path is concrete.
|
||||
- Look for credential comparison bugs, truncation of usernames/passwords, accepting empty credentials, parsing only part of an Authorization header, or treating malformed credentials as anonymous-but-authorized.
|
||||
- Flag sensitive data exposure when secrets, credentials, tokens, private keys, firmware signing material, PII, or device identity values can be read by unauthorized remote clients or logged from attacker-triggered paths.
|
||||
@@ -1,24 +1,34 @@
|
||||
# Full Codebase Security Scan Prompt
|
||||
# Security Scan Master Prompt
|
||||
|
||||
You are an expert security reviewer performing a full-codebase security audit of the checked-out repository.
|
||||
You are an expert security reviewer performing a focused security audit centered on one assigned scan area in the checked-out repository.
|
||||
|
||||
You must analyze the repository as a whole and identify credible, externally reachable security vulnerabilities in the current implementation.
|
||||
You must analyze the assigned target and security surface and identify credible, externally reachable security vulnerabilities in the current implementation.
|
||||
|
||||
For this task, you will rely on two additional files, `resources/specs/claude-security-scan-instructions.md` detailing security scan instructions specific to the repository and `resources/specs/claude-false-positive-filtering.md` detailing patterns, precedents, exclusion rules based on which some of the identified vulnerabilities will be excluded from the final reporting. The instructions, rules and criterias found in those two files are MANDATORY TO BE HONOURED and your security audit will have to take all of them into account.
|
||||
Every full security scan uses:
|
||||
|
||||
YOU MUST READ AND ANALYZE BOTH OF THESE FILES BEFORE COMMENCING THE SECURITY AUDIT. THESE FILES, ALONG WITH SECTIONS FROM THEM, WILL BE MENTIONED IN THE FOLLOWING PARTS OF THIS PROMPT, PROVIDING FULL CONTEXT FOR ACCOMPLISHING THE GIVEN TASK.
|
||||
1. the shared master prompt: `resources/specs/claude-security-scan.md`;
|
||||
2. one area prompt selected by the workflow: `resources/specs/areas/<area>.md`;
|
||||
3. the common filtering prompt: `resources/specs/claude-false-positive-filtering.md`.
|
||||
|
||||
The area prompt defines what is being scanned. This master prompt defines how security analysis and reporting are performed. The filtering prompt defines what must not be reported and which historical precedents must be honored.
|
||||
|
||||
YOU MUST READ AND ANALYZE THE ASSIGNED AREA PROMPT AND THE COMMON FILTERING PROMPT BEFORE COMMENCING THE SECURITY AUDIT. THE INSTRUCTIONS, RULES, AND CRITERIA IN ALL THREE PROMPTS ARE MANDATORY TO BE HONORED.
|
||||
|
||||
## OBJECTIVE
|
||||
|
||||
You are reviewing a full source repository for security vulnerabilities.
|
||||
You are reviewing one assigned security area in a source repository for security vulnerabilities.
|
||||
|
||||
The repository may contain application code, protocol parsers, networking code, embedded code, library code, examples, tests, build scripts, documentation, generated files, and third-party/vendor code.
|
||||
The `src/` directory contains the protocol parsers, networking code, embedded code, library code, generated code, and platform-specific code in scope for this review.
|
||||
|
||||
Your task is to determine which parts of the repository are security-relevant, identify externally reachable attack surfaces, and report only findings with a concrete vulnerability pattern, credible attacker influence, and meaningful security impact.
|
||||
The area-specific prompt defines the primary files and security surface for this invocation. Your task is to identify externally reachable attack surfaces within that assigned area and report only findings with a concrete vulnerability pattern, credible attacker influence, and meaningful security impact.
|
||||
|
||||
The security review target is limited to code under `src/`. Do not analyze, count, or report code outside `src/` as part of the security review.
|
||||
|
||||
You may inspect code outside the listed target files only when it is also under `src/` and is necessary to understand data flow, reachability, call chains, state transitions, memory ownership, mitigations, authentication or authorization, protocol interaction, exploitability, or impact. Inspecting supporting code must not turn into an independent security review of unrelated areas. Findings must remain tied to the assigned target and security surface and must identify an affected file under `src/`.
|
||||
|
||||
DO NOT modify files, create commits, open PRs, create issues or post comments! You must only produce the final JSON report requested below.
|
||||
|
||||
Identify high-confidence security vulnerabilities in the full codebase. For this prompt, high-confidence means confidence >= 0.7 under the scoring rules below. Focus on vulnerabilities that are reachable through normal execution paths and attacker-controlled inputs. Report only findings that have a concrete security impact.
|
||||
Identify high-confidence security vulnerabilities in the assigned scan area. For this prompt, high-confidence means confidence >= 0.7 under the scoring rules below. Focus on vulnerabilities that are reachable through normal execution paths and attacker-controlled inputs. Report only findings that have a concrete security impact.
|
||||
|
||||
Do not report vague best-practice concerns, speculative hardening suggestions, style issues, maintainability concerns, generic bug risks, or findings without a clear attacker-controlled path.
|
||||
|
||||
@@ -26,9 +36,26 @@ Existing vulnerabilities in the current codebase are in scope.
|
||||
|
||||
Previously introduced vulnerabilities are in scope.
|
||||
|
||||
Any security-relevant issue present in the checked-out repository is in scope unless explicitly excluded below.
|
||||
Any security-relevant issue tied to the assigned target or security surface is in scope unless explicitly excluded below.
|
||||
|
||||
In the security scan instructions file `resources/specs/claude-security-scan-instructions.md`, read the `External Attacker Threat Model` and `Mongoose-Specific Security Review Scope` subsections found in the `SECURITY SCAN CONTEXT` main section for a full understanding of the scanning model and the code review context.
|
||||
## SECURITY SCAN CONTEXT
|
||||
|
||||
**External Attacker Threat Model:**
|
||||
- Analyze vulnerabilities from the perspective of an external attacker, not from the perspective of a developer, local user, debugger, test harness, or code running inside the process.
|
||||
- The attacker can provide bytes through real external interfaces exposed by Mongoose or a Mongoose-based application: TCP, UDP, HTTP, WebSocket, MQTT, DNS, mDNS, TLS handshakes/certificates, Ethernet/IP/TCP/UDP packets, uploaded files, request paths, query strings, headers, message bodies, malicious server responses, and other protocol inputs.
|
||||
- The attacker cannot directly call internal C functions, pass arbitrary invalid pointers, mutate internal structs, invoke callbacks manually, edit compile-time macros, alter build flags, write to hardware registers, control DMA descriptors directly, attach a debugger, or run code inside the target process.
|
||||
- For a finding to be valid, show how attacker-controlled external input reaches the vulnerable code path through normal library/application execution.
|
||||
- Do not report findings that require impossible internal states unless the finding also explains how an external input can cause that state.
|
||||
- Do not report “API misuse” as a library vulnerability unless the API contract makes the insecure use likely, undocumented, or unavoidable.
|
||||
- For embedded networking and driver code, assume the attacker can send malformed network traffic to the device, but cannot directly manipulate hardware registers, memory-mapped I/O, DMA rings, or interrupt state except as a consequence of packets processed by the driver.
|
||||
|
||||
**Mongoose-Specific Security Review Scope:**
|
||||
- Treat this repository as a security-sensitive embedded C/C++ networking stack. Mongoose processes attacker-controlled bytes from TCP, UDP, HTTP, WebSocket, MQTT, DNS, mDNS, TLS, filesystem upload/download paths, and built-in TCP/IP drivers.
|
||||
- In this repository, remotely triggerable denial of service is security-relevant when it is caused by a concrete implementation flaw such as memory corruption, parser abort, assertion failure, stack exhaustion, infinite loop, unbounded recursion, IRQ livelock, connection state corruption, descriptor-ring corruption, or a single unauthenticated packet causing process/device crash.
|
||||
- Prioritize HIGH and MEDIUM severity findings. Include LOW only when unusually concrete, externally relevant, and useful. Do not report vague robustness concerns. Every finding must identify the attacker-controlled input, affected parser/state machine/buffer, concrete impact, and the exact code path.
|
||||
- Prefer findings with a plausible packet/request/message shape. For network protocol issues, describe the relevant malformed HTTP request, MQTT packet, WebSocket frame, DNS message, TLS handshake/certificate input, Ethernet/IP/TCP/UDP packet, or filesystem request.
|
||||
- Because this is C/C++, memory safety vulnerabilities are in scope. Buffer overflows, stack overflows, heap overflows, out-of-bounds reads/writes, use-after-free, double free, integer overflow leading to memory corruption, invalid pointer lifetime, and unsafe length conversions must be reviewed carefully.
|
||||
- Do not dismiss issues merely because they are “only DoS” if the issue is remotely triggerable against an embedded server, broker, device dashboard, firmware-update endpoint, or network-facing parser.
|
||||
|
||||
## CRITICAL INSTRUCTIONS
|
||||
|
||||
@@ -56,12 +83,14 @@ Do not report:
|
||||
* Purely defensive hardening suggestions.
|
||||
* Issues that only matter under unrealistic assumptions.
|
||||
|
||||
### 3. Analyze the repository as a whole
|
||||
### 3. Analyze the assigned scan area
|
||||
|
||||
This is a full-codebase scan. You should inspect the repository structure, identify major components, and prioritize security-sensitive code paths.
|
||||
This is a targeted scan centered on the primary files and security surface defined by the assigned area prompt. Follow relevant supporting code under `src/` when necessary to establish reachability, data flow, state, validation, mitigation, exploitability, or impact, but do not inspect code outside `src/` as part of the security review or independently review unrelated areas.
|
||||
|
||||
Do not restrict analysis to a diff. Do not assume only changed files matter. Do not assume an issue is irrelevant because it appears in old or long-standing code.
|
||||
|
||||
Findings must remain tied to the assigned target and security surface.
|
||||
|
||||
### 4. Prioritize externally reachable attack surfaces
|
||||
|
||||
Prioritize code reachable from attacker-controlled or peer-controlled inputs. Relevant inputs may include, but are not limited to:
|
||||
@@ -94,25 +123,40 @@ The exclusions section below is mandatory. If a potential finding falls under an
|
||||
|
||||
If a finding partially overlaps an exclusion, only report it if there is a concrete, non-excluded security impact.
|
||||
|
||||
## SECURITY CATEGORIES TO EXAMINE
|
||||
## GENERAL SECURITY CATEGORIES TO EXAMINE
|
||||
|
||||
In the security scan instructions file `resources/specs/claude-security-scan-instructions.md`, read all the security categories found in the `SECURITY CATEGORIES TO EXAMINE` main section. THESE ARE THE SECURITY CATEGORIES WHICH NEED TO BE EXAMINED.
|
||||
The following common security categories apply to every assigned scan area. The assigned area prompt provides additional area-specific security guidance.
|
||||
|
||||
Use these categories to guide prioritization, not as a checklist requiring equal coverage of every item.
|
||||
|
||||
**C Memory Safety and Length-Handling Vulnerabilities:**
|
||||
- Look for writes to fixed-size stack or heap buffers where the loop bound is derived from attacker-controlled protocol fields, including topic counts, header counts, chunk counts, multipart parts, DNS labels, WebSocket fragments, TCP/IP options, or filesystem path components.
|
||||
- Check all conversions between `size_t`, `int`, `long`, `uint16_t`, `uint32_t`, and signed protocol lengths. Flag integer truncation, wraparound, negative-to-large conversion, or off-by-one behavior that can affect allocation, parsing, copying, or bounds checks.
|
||||
- Review any `memcpy`, `memmove`, `memcmp`, `snprintf`, `mg_snprintf`, `mg_xprintf`, iobuf append/delete/resize operation, and manual pointer increment where source length can originate from network input.
|
||||
- Treat `struct mg_str` values as non-null-terminated unless the code proves otherwise. Flag use of `strlen`, `%s`, `strcmp`, `strstr`, or C-string APIs on attacker-controlled `mg_str.buf` unless length and termination are guaranteed.
|
||||
- Check for parser code that advances pointers without proving that enough bytes remain. Flag any read of fixed-size fields, variable-length integers, DNS labels, TLS vectors, MQTT remaining length, WebSocket extended lengths, or HTTP chunks before bounds are validated.
|
||||
- Look for inconsistent ownership and lifetime of buffers referenced by `mg_str`, connection receive/send iobufs, TLS buffers, filesystem buffers, and event callback data.
|
||||
- Check for reentrancy hazards in event callbacks where user handlers may close a connection, mutate iobufs, start TLS, change protocol handlers, or free state while protocol code continues using stale pointers.
|
||||
|
||||
**Cross-Platform C Portability With Security Impact:**
|
||||
- Review code paths whose security depends on struct packing, alignment, endianness, pointer size, signedness of `char`, integer width, or platform-specific filesystem/network behavior.
|
||||
- Check for unaligned memory access on embedded targets when parsing attacker-controlled network packets.
|
||||
- Verify endian conversions for Ethernet/IP/TCP/UDP/DNS/MQTT/TLS fields before bounds checks and allocations.
|
||||
- Look for platform branches where one backend performs validation and another does not, especially across UNIX, Windows, lwIP, Zephyr, FreeRTOS-like systems, bare-metal, and built-in TCP/IP stack modes.
|
||||
- Flag portability bugs only when they create a realistic vulnerability on a supported platform, not merely theoretical undefined behavior.
|
||||
|
||||
## ANALYSIS METHODOLOGY
|
||||
|
||||
Follow this methodology before producing the final JSON.
|
||||
|
||||
### Phase 1: Repository context research
|
||||
|
||||
First, understand the repository.
|
||||
First, understand the assigned area within `src/`.
|
||||
|
||||
Identify:
|
||||
|
||||
* Main source directories.
|
||||
* Build systems.
|
||||
* Public APIs.
|
||||
* Relevant directories under `src/`.
|
||||
* Public APIs declared under `src/`.
|
||||
* Protocol implementations.
|
||||
* Network-facing components.
|
||||
* File parsing components.
|
||||
@@ -120,14 +164,13 @@ Identify:
|
||||
* TLS, crypto, or certificate validation logic.
|
||||
* Memory management patterns.
|
||||
* Platform-specific code.
|
||||
* Example, test, fuzz, generated, and vendor directories.
|
||||
* Components most likely to process attacker-controlled input.
|
||||
* Components in the assigned area most likely to process attacker-controlled input.
|
||||
|
||||
Use repository files to infer the actual architecture instead of assuming a generic web application model.
|
||||
Use files under `src/` to infer the actual architecture instead of assuming a generic web application model.
|
||||
|
||||
### Phase 2: Attack surface mapping
|
||||
|
||||
Map the externally reachable attack surfaces.
|
||||
Map the externally reachable attack surfaces within the assigned area.
|
||||
|
||||
For each important surface, determine:
|
||||
|
||||
@@ -166,6 +209,7 @@ Before including any finding in the final output, confirm:
|
||||
* The severity matches the impact.
|
||||
* The finding is not a duplicate of another finding.
|
||||
* The finding is not excluded.
|
||||
* The finding is tied to the assigned target or security surface.
|
||||
|
||||
If you cannot provide this level of specificity, do not report the finding.
|
||||
|
||||
@@ -218,6 +262,8 @@ The value of `files_reviewed` should reflect the number of files substantially i
|
||||
|
||||
Do not inflate this number by counting files that were only listed or briefly skimmed.
|
||||
|
||||
The value `review_completed: true` means the assigned scan area was meaningfully reviewed, not that the entire repository was reviewed.
|
||||
|
||||
## SEVERITY GUIDELINES
|
||||
|
||||
Use these severity definitions.
|
||||
@@ -313,10 +359,9 @@ Take into account the project-specific filtering and reporting precedents found
|
||||
|
||||
## DIRECTORY AND FILE EXCLUSIONS
|
||||
|
||||
Do not analyze or report findings from the following paths unless they are directly used by production/library code:
|
||||
- .github/
|
||||
- resources/
|
||||
- test/
|
||||
Only analyze and report findings from files under `src/`.
|
||||
|
||||
Do not analyze or count code outside `src/` as part of the security review.
|
||||
|
||||
## OUTPUT REQUIREMENTS
|
||||
|
||||
@@ -336,4 +381,4 @@ Do not include commentary.
|
||||
|
||||
Do not reply again after outputting the JSON.
|
||||
|
||||
Begin the full-codebase security analysis now.
|
||||
Begin the assigned-area security analysis now.
|
||||
+29
-23
@@ -3,22 +3,22 @@
|
||||
"bomFormat": "CycloneDX",
|
||||
"specVersion": "1.6",
|
||||
"serialNumber": "urn:uuid:d3f8b1e2-4a7c-4d9e-bf2a-1c3e5f7a9b0d",
|
||||
"version": 1,
|
||||
"version": 3,
|
||||
"metadata": {
|
||||
"timestamp": "2026-06-01T00:00:00Z",
|
||||
"timestamp": "2026-11-18T00:00:00Z",
|
||||
"component": {
|
||||
"type": "library",
|
||||
"bom-ref": "mongoose",
|
||||
"supplier": { "name": "Cesanta Software Limited", "url": ["https://cesanta.com"] },
|
||||
"author": "Cesanta Software Limited",
|
||||
"name": "mongoose",
|
||||
"version": "7.21",
|
||||
"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.",
|
||||
"licenses": [
|
||||
{ "expression": "GPL-2.0-only" },
|
||||
{ "license": { "name": "Cesanta Commercial License", "url": "https://mongoose.ws/licensing/" } }
|
||||
],
|
||||
"purl": "pkg:github/cesanta/mongoose@7.21",
|
||||
"purl": "pkg:github/cesanta/mongoose@7.24",
|
||||
"externalReferences": [
|
||||
{ "type": "website", "url": "https://mongoose.ws" },
|
||||
{ "type": "vcs", "url": "https://github.com/cesanta/mongoose" },
|
||||
@@ -27,21 +27,38 @@
|
||||
{ "type": "distribution", "url": "https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c" }
|
||||
],
|
||||
"properties": [
|
||||
{ "name": "cpe", "value": "cpe:2.3:a:cesanta:mongoose:7.21:*:*:*:*:*:*:*" }
|
||||
{ "name": "cpe", "value": "cpe:2.3:a:cesanta:mongoose:7.24:*:*:*:*:*:*:*" }
|
||||
]
|
||||
}
|
||||
},
|
||||
"components": [
|
||||
{
|
||||
"type": "library",
|
||||
"bom-ref": "micro-ecc",
|
||||
"author": "Kenneth MacKay",
|
||||
"name": "micro-ecc",
|
||||
"description": "ECC point operations on GF(p) curves (secp256r1). Used by Mongoose built-in TLS 1.3 for ECDH key exchange and ECDSA signatures. Source: src/tls_uecc.c, src/tls_uecc.h.",
|
||||
"licenses": [ { "expression": "BSD-2-Clause" } ],
|
||||
"purl": "pkg:github/kmackay/micro-ecc",
|
||||
"bom-ref": "bearssl-ec",
|
||||
"author": "Thomas Pornin",
|
||||
"name": "BearSSL EC (P-256, P-384, ECDSA)",
|
||||
"description": "P-256 (ec_p256_m31) and P-384 (ec_prime_i31) elliptic curve arithmetic, ECDSA sign and verify, and the i31 bignum code they use, derived from BearSSL. Used by Mongoose built-in TLS 1.3 for ECDHE key exchange and ECDSA signatures, by JWT ES256 and by OTA firmware signature verification. Source: src/tls_p256.c, src/tls_p256.h, src/tls_p384.c, src/tls_p384.h.",
|
||||
"licenses": [ { "expression": "MIT" } ],
|
||||
"externalReferences": [
|
||||
{ "type": "vcs", "url": "https://github.com/kmackay/micro-ecc" }
|
||||
{ "type": "website", "url": "https://bearssl.org" }
|
||||
],
|
||||
"properties": [
|
||||
{ "name": "upstream-commit", "value": "7bea48e5e850ab4cafbe68d3765cdaba13a86d6f" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "library",
|
||||
"bom-ref": "p256-cortex-m4",
|
||||
"author": "Emil Lenngren, Shortcut Labs AB",
|
||||
"name": "P256-Cortex-M4",
|
||||
"description": "P-256 ECDH and ECDSA in Cortex-M assembly. Used by Mongoose built-in TLS 1.3 instead of the BearSSL P-256 code on Cortex-M cores with the DSP extension (M4, M7, M33, M55, M85). Source: src/tls_p256_cortex.c.",
|
||||
"licenses": [ { "expression": "MIT" } ],
|
||||
"purl": "pkg:github/Emill/P256-Cortex-M4@ecd3ec2222fc9e18b6b44c86bb7183971a2041fc",
|
||||
"externalReferences": [
|
||||
{ "type": "vcs", "url": "https://github.com/Emill/P256-Cortex-M4" }
|
||||
],
|
||||
"properties": [
|
||||
{ "name": "upstream-commit", "value": "ecd3ec2222fc9e18b6b44c86bb7183971a2041fc" }
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -55,17 +72,6 @@
|
||||
{ "type": "website", "url": "https://bearssl.org" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "library",
|
||||
"bom-ref": "strobe-x25519",
|
||||
"author": "Cryptography Research, Inc. / Mike Hamburg",
|
||||
"name": "STROBE x25519 (adapted)",
|
||||
"description": "X25519 Diffie-Hellman function adapted from the STROBE project. Used by Mongoose built-in TLS 1.3 for ECDH key exchange. Source: src/tls_x25519.c.",
|
||||
"licenses": [ { "expression": "MIT" } ],
|
||||
"externalReferences": [
|
||||
{ "type": "website", "url": "https://strobe.sourceforge.io/" }
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "library",
|
||||
"bom-ref": "sha1-reid",
|
||||
|
||||
@@ -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
|
||||
+3
-2
@@ -4,7 +4,6 @@
|
||||
#define MG_ARCH_UNIX 1 // Linux, BSD, Mac, ...
|
||||
#define MG_ARCH_WIN32 2 // Windows
|
||||
#define MG_ARCH_ESP32 3 // ESP32
|
||||
#define MG_ARCH_ESP8266 4 // ESP8266
|
||||
|
||||
#define MG_ARCH_FREERTOS 5 // FreeRTOS <-- DEPRECATED !!!
|
||||
|
||||
@@ -19,6 +18,7 @@
|
||||
#define MG_ARCH_RTTHREAD 14 // RT-Thread RTOS
|
||||
#define MG_ARCH_ARMCGT 15 // Texas Semi ARM-CGT
|
||||
#define MG_ARCH_CUBE 16 // STM32Cube environment
|
||||
#define MG_ARCH_MCUXPRESSO 17 // MCUXpresso environment
|
||||
|
||||
#define MG_ARCH_NEWLIB MG_ARCH_ARMGCC // Alias, deprecate in 2025
|
||||
|
||||
@@ -49,7 +49,8 @@
|
||||
#include "arch_armgcc.h"
|
||||
#include "arch_cube.h"
|
||||
#include "arch_esp32.h"
|
||||
#include "arch_esp8266.h"
|
||||
#include "arch_mcuxpresso.h"
|
||||
#include "arch_picosdk.h"
|
||||
#include "arch_rtx.h"
|
||||
#include "arch_threadx.h"
|
||||
#include "arch_unix.h"
|
||||
|
||||
+2
-1
@@ -12,7 +12,8 @@
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifndef MG_PATH_MAX
|
||||
#define MG_PATH_MAX 100
|
||||
#define MG_ENABLE_DIRLIST 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+4
-1
@@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#if MG_ARCH == MG_ARCH_ARMGCC
|
||||
#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it
|
||||
#define _POSIX_TIMERS
|
||||
#endif
|
||||
|
||||
#include <ctype.h>
|
||||
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
||||
@@ -18,7 +20,8 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef MG_PATH_MAX
|
||||
#define MG_PATH_MAX 100
|
||||
#define MG_ENABLE_DIRLIST 0
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+10
-71
@@ -25,10 +25,6 @@
|
||||
#define MG_PATH_MAX 100
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_DIRLIST
|
||||
#define MG_ENABLE_DIRLIST 0
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef MG_ENABLE_TCPIP
|
||||
#define MG_ENABLE_TCPIP 1 // Enable built-in TCP/IP stack
|
||||
@@ -36,6 +32,7 @@
|
||||
|
||||
#if MG_ENABLE_TCPIP && !defined(MG_ENABLE_DRIVER_STM32F) && \
|
||||
!defined(MG_ENABLE_DRIVER_STM32H) && !defined(MG_ENABLE_DRIVER_STM32N) && \
|
||||
!defined(MG_ENABLE_DRIVER_STM32C) && \
|
||||
!defined(MG_ENABLE_DRIVER_CYW) && !defined(MG_ENABLE_DRIVER_CYW_SDIO) && \
|
||||
!defined(MG_ENABLE_DRIVER_ST67W6)
|
||||
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || defined(STM32F7)
|
||||
@@ -44,6 +41,8 @@
|
||||
#define MG_ENABLE_DRIVER_STM32H 1
|
||||
#elif defined(STM32N6)
|
||||
#define MG_ENABLE_DRIVER_STM32N 1
|
||||
#elif defined(STM32C5)
|
||||
#define MG_ENABLE_DRIVER_STM32C 1
|
||||
#else
|
||||
#error Select a driver in mongoose_config.h
|
||||
#endif
|
||||
@@ -53,76 +52,16 @@
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#endif
|
||||
|
||||
#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \
|
||||
defined(STM32F4) || defined(STM32F7)
|
||||
#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user
|
||||
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \
|
||||
defined(STM32F7)
|
||||
#define MG_OTA MG_OTA_STM32F
|
||||
#elif !defined(MG_OTA) && defined(STM32H5)
|
||||
#elif defined(STM32H5)
|
||||
#define MG_OTA MG_OTA_STM32H5
|
||||
#elif !defined(MG_OTA) && defined(STM32H7)
|
||||
#elif defined(STM32H7)
|
||||
#define MG_OTA MG_OTA_STM32H7
|
||||
#endif
|
||||
|
||||
// OTA rollback timer via IWDG. Fixed 10 s hang-detection window (prescaler
|
||||
// /1024, LSI ~32 kHz: reload = 10 * 32000 / 1024 = 312). mg_ota_poll() feeds
|
||||
// the watchdog every 500 ms while in MG_OTA_TESTING state; if the event loop
|
||||
// hangs for 10 s, the IWDG fires and the device rolls back.
|
||||
// STM32H7 names the peripheral IWDG1; all others use IWDG.
|
||||
// The total evaluation period is set by MG_OTA_ROLLBACK_TIMEOUT_SECONDS.
|
||||
// STM32H7 names the watchdog peripheral IWDG1; all other families use IWDG.
|
||||
#if defined(IWDG1) && !defined(IWDG)
|
||||
#define MG_IWDG IWDG1
|
||||
#else
|
||||
#define MG_IWDG IWDG
|
||||
#endif
|
||||
// PR=6 → /256 prescaler, RLR=1250 → ~10 s at LSI 32 kHz.
|
||||
// Works on F4/F7/H5/H7; uses direct register writes so it is safe before HAL_Init().
|
||||
#ifndef MG_OTA_ROLLBACK_TIMER_START
|
||||
#define MG_OTA_ROLLBACK_TIMER_START() \
|
||||
do { \
|
||||
MG_IWDG->KR = 0xCCCCU; /* start + LSI */ \
|
||||
MG_IWDG->KR = 0x5555U; /* unlock PR/RLR */ \
|
||||
MG_IWDG->PR = 6U; /* /256 */ \
|
||||
MG_IWDG->RLR = 1250U; /* 10 s */ \
|
||||
while (MG_IWDG->SR & (IWDG_SR_PVU | IWDG_SR_RVU)) (void) 0; \
|
||||
MG_IWDG->KR = 0xAAAAU; /* reload */ \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
// Feed (reload) the IWDG. Called from mg_ota_poll() every 500 ms.
|
||||
#ifndef MG_OTA_ROLLBACK_TIMER_FEED
|
||||
#define MG_OTA_ROLLBACK_TIMER_FEED() (MG_IWDG->KR = 0xAAAAU)
|
||||
#endif
|
||||
|
||||
// OTA state in a backup register that survives warm resets but clears on POR.
|
||||
// STATE_GET reads work without any setup. STATE_SET enables the RTC APB clock
|
||||
// and unlocks backup domain write access via direct register writes, so it is
|
||||
// safe to call from any context (before or after HAL_Init).
|
||||
// H5 (TAMP->BKP0R), H7 (RTC->BKP0R), F4/F7 (RTC->BKP0R).
|
||||
#ifndef MG_OTA_STATE_GET
|
||||
#if defined(PWR_DBPCR_DBP)
|
||||
#define MG_OTA_DBP_ENABLE() (PWR->DBPCR |= PWR_DBPCR_DBP)
|
||||
#elif defined(PWR_CR1_DBP)
|
||||
#define MG_OTA_DBP_ENABLE() (PWR->CR1 |= PWR_CR1_DBP)
|
||||
#else
|
||||
#define MG_OTA_DBP_ENABLE() (PWR->CR |= PWR_CR_DBP)
|
||||
#endif
|
||||
|
||||
#if defined(TAMP)
|
||||
#define MG_OTA_STATE_GET() \
|
||||
(RCC->APB3ENR |= RCC_APB3ENR_RTCAPBEN, TAMP->BKP0R)
|
||||
#define MG_OTA_STATE_SET(v) \
|
||||
(RCC->APB3ENR |= RCC_APB3ENR_RTCAPBEN, MG_OTA_DBP_ENABLE(), \
|
||||
(TAMP->BKP0R = (uint32_t) (v)), TAMP->BKP0R)
|
||||
#elif defined(RCC_APB4ENR_RTCAPBEN)
|
||||
#define MG_OTA_STATE_GET() (RTC->BKP0R)
|
||||
#define MG_OTA_STATE_SET(v) \
|
||||
(RCC->APB4ENR |= RCC_APB4ENR_RTCAPBEN, MG_OTA_DBP_ENABLE(), \
|
||||
(RTC->BKP0R = (uint32_t) (v)))
|
||||
#else
|
||||
#define MG_OTA_STATE_GET() (RTC->BKP0R)
|
||||
#define MG_OTA_STATE_SET(v) \
|
||||
(RCC->APB1ENR |= RCC_APB1ENR_PWREN, MG_OTA_DBP_ENABLE(), \
|
||||
(RTC->BKP0R = (uint32_t) (v)))
|
||||
#elif defined(STM32C5)
|
||||
#define MG_OTA MG_OTA_STM32C5
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
+6
-4
@@ -21,6 +21,12 @@
|
||||
#include <esp_timer.h> // amalgamation ditching them
|
||||
#include <esp_system.h>
|
||||
#include <esp_mac.h>
|
||||
#include <esp_idf_version.h>
|
||||
#if MG_ENABLE_HEALTH
|
||||
#include <esp_memory_utils.h>
|
||||
#include <esp_core_dump.h>
|
||||
#include <esp_partition.h>
|
||||
#endif
|
||||
|
||||
#define MG_PATH_MAX 128
|
||||
|
||||
@@ -28,8 +34,4 @@
|
||||
#define MG_ENABLE_POSIX_FS 1
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_DIRLIST
|
||||
#define MG_ENABLE_DIRLIST 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#if MG_ARCH == MG_ARCH_ESP8266
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <netdb.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <esp_system.h>
|
||||
|
||||
#define MG_PATH_MAX 128
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,229 @@
|
||||
#pragma once
|
||||
|
||||
#if MG_ARCH == MG_ARCH_MCUXPRESSO
|
||||
#define _POSIX_TIMERS
|
||||
|
||||
#include <ctype.h>
|
||||
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
||||
#include <errno.h>
|
||||
#endif
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// MCUXpresso-generated header, includes the SoC peripheral definitions.
|
||||
// NOTE: use angle brackets to prevent amalgamator ditching it
|
||||
#include <fsl_device_registers.h>
|
||||
|
||||
#ifndef MG_PATH_MAX
|
||||
#define MG_PATH_MAX 100
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_TCPIP
|
||||
#define MG_ENABLE_TCPIP 1 // Enable built-in TCP/IP stack
|
||||
#endif
|
||||
|
||||
#if MG_ENABLE_TCPIP && !defined(MG_ENABLE_DRIVER_IMXRT10) && \
|
||||
!defined(MG_ENABLE_DRIVER_IMXRT11) && !defined(MG_ENABLE_DRIVER_NETC)
|
||||
#if defined(CPU_MIMXRT1021DAG5A) || defined(CPU_MIMXRT1024DAG5A) || \
|
||||
defined(CPU_MIMXRT1042XJM5B) || defined(CPU_MIMXRT1052DVL6B) || \
|
||||
defined(CPU_MIMXRT1062DVL6B) || defined(CPU_MIMXRT1064DVL6B) || \
|
||||
defined(CPU_MIMXRT1062DVL6A) || defined(CPU_MIMXRT1064DVL6A) || \
|
||||
defined(CPU_MIMXRT1062DVMAA_cm7) || defined(CPU_MIMXRT1062DVL6A_cm7) || \
|
||||
defined(CPU_MIMXRT1062DVL6B_cm7) || defined(_MIMXRT1021_H_) || \
|
||||
defined(_MIMXRT1024_H_) || defined(_MIMXRT1042_H_) || \
|
||||
defined(_MIMXRT1052_H_) || defined(_MIMXRT1062_H_) || \
|
||||
defined(_MIMXRT1064_H_)
|
||||
#define MG_ENABLE_DRIVER_IMXRT10 1
|
||||
#elif defined(CPU_MIMXRT1176DVMAA_cm7) || defined(CPU_MIMXRT1176DVMAA_cm4) || \
|
||||
defined(_MIMXRT1176_cm7_H_) || defined(_MIMXRT1176_cm4_H_) || \
|
||||
defined(_MIMXRT1176_H_)
|
||||
#define MG_ENABLE_DRIVER_IMXRT11 1
|
||||
#elif defined(CPU_MIMXRT1186CVJ8C_cm33) || defined(MIMXRT1186_cm33_H_)
|
||||
#define MG_ENABLE_DRIVER_NETC 1
|
||||
#else
|
||||
#error Select an Ethernet driver in mongoose_config.h
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MG_TLS
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#endif
|
||||
|
||||
#if !defined(MG_OTA) && \
|
||||
(defined(CPU_MIMXRT1021DAG5A) || defined(CPU_MIMXRT1024DAG5A) || \
|
||||
defined(_MIMXRT1021_H_) || defined(_MIMXRT1024_H_))
|
||||
#define MG_OTA MG_OTA_RT1020
|
||||
#elif !defined(MG_OTA) && \
|
||||
(defined(CPU_MIMXRT1052DVL6B) || defined(_MIMXRT1052_H_))
|
||||
#define MG_OTA MG_OTA_RT1050
|
||||
#elif !defined(MG_OTA) && \
|
||||
(defined(CPU_MIMXRT1062DVL6B) || defined(CPU_MIMXRT1062DVL6A) || \
|
||||
defined(CPU_MIMXRT1042XJM5B) || defined(_MIMXRT1042_H_) || \
|
||||
defined(CPU_MIMXRT1062DVMAA_cm7) || defined(CPU_MIMXRT1062DVL6A_cm7) || \
|
||||
defined(CPU_MIMXRT1062DVL6B_cm7) || defined(_MIMXRT1062_H_))
|
||||
#define MG_OTA MG_OTA_RT1060
|
||||
#elif !defined(MG_OTA) && \
|
||||
(defined(CPU_MIMXRT1064DVL6B) || defined(CPU_MIMXRT1064DVL6A) || \
|
||||
defined(_MIMXRT1064_H_))
|
||||
#define MG_OTA MG_OTA_RT1064
|
||||
#elif !defined(MG_OTA) && \
|
||||
(defined(CPU_MIMXRT1176DVMAA_cm7) || defined(CPU_MIMXRT1176DVMAA_cm4) || \
|
||||
defined(_MIMXRT1176_cm7_H_) || defined(_MIMXRT1176_cm4_H_) || \
|
||||
defined(_MIMXRT1176_H_))
|
||||
#define MG_OTA MG_OTA_RT1170
|
||||
#elif !defined(MG_OTA) && \
|
||||
(defined(CPU_MIMXRT1186CVJ8C_cm33) || defined(MIMXRT1186_cm33_H_))
|
||||
#define MG_OTA MG_OTA_RT1180
|
||||
#endif
|
||||
|
||||
#ifndef MG_IRAM
|
||||
#if defined(CPU_MIMXRT1186CVJ8C_cm33) || defined(MIMXRT1186_cm33_H_)
|
||||
#define MG_IRAM \
|
||||
__attribute__((noinline, section(".ramfunc.$SRAM_ITC_cm33")))
|
||||
#else
|
||||
#define MG_IRAM __attribute__((noinline, section(".data_RAM2")))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MG_ETH_RAM
|
||||
#if defined(CPU_MIMXRT1186CVJ8C_cm33) || defined(MIMXRT1186_cm33_H_)
|
||||
#define MG_ETH_RAM __attribute__((section(".bss.$ETH_RAM")))
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MG_SET_MAC_ADDRESS
|
||||
#if defined(MG_ENABLE_DRIVER_NETC) && MG_ENABLE_DRIVER_NETC
|
||||
// No OCOTP_FUSES present on RT1186 to generate the UUID
|
||||
#define MG_SET_MAC_ADDRESS(mac) (void) (mac)
|
||||
#else
|
||||
#if defined(MG_ENABLE_DRIVER_IMXRT11) && MG_ENABLE_DRIVER_IMXRT11
|
||||
#define MG_OCOTP_FUSES ((volatile uint32_t *) 0x40cac900)
|
||||
#else
|
||||
#define MG_OCOTP_FUSES ((volatile uint32_t *) 0x401f4410)
|
||||
#endif
|
||||
#define MG_SET_MAC_ADDRESS(mac) \
|
||||
do { \
|
||||
mac[0] = 2; \
|
||||
mac[1] = (MG_OCOTP_FUSES[0] >> 0) & 255; \
|
||||
mac[2] = (MG_OCOTP_FUSES[0] >> 10) & 255; \
|
||||
mac[3] = ((MG_OCOTP_FUSES[0] >> 19) ^ (MG_OCOTP_FUSES[4] >> 19)) & 255; \
|
||||
mac[4] = (MG_OCOTP_FUSES[4] >> 10) & 255; \
|
||||
mac[5] = (MG_OCOTP_FUSES[4] >> 0) & 255; \
|
||||
} while (0)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MG_IMXRT_WDOG1_TIMEOUT_MS
|
||||
#define MG_IMXRT_WDOG1_TIMEOUT_MS 10000U
|
||||
#endif
|
||||
|
||||
#if defined(CPU_MIMXRT1186CVJ8C_cm33) || defined(MIMXRT1186_cm33_H_)
|
||||
|
||||
#define MG_IMXRT_RTWDOG1_UNLOCK() \
|
||||
do { \
|
||||
if ((RTWDOG1->CS & RTWDOG_CS_CMD32EN_MASK) != 0U) { \
|
||||
RTWDOG1->CNT = RTWDOG_UPDATE_KEY; \
|
||||
} else { \
|
||||
RTWDOG1->CNT = RTWDOG_UPDATE_KEY & 0xffffU; \
|
||||
RTWDOG1->CNT = RTWDOG_UPDATE_KEY >> 16; \
|
||||
} \
|
||||
while ((RTWDOG1->CS & RTWDOG_CS_ULK_MASK) == 0U) (void) 0; \
|
||||
} while (0)
|
||||
|
||||
#define MG_IMXRT_RTWDOG1_FEED() \
|
||||
do { \
|
||||
uint32_t primask_ = __get_PRIMASK(); \
|
||||
__disable_irq(); \
|
||||
if ((RTWDOG1->CS & RTWDOG_CS_CMD32EN_MASK) != 0U) { \
|
||||
RTWDOG1->CNT = RTWDOG_REFRESH_KEY; \
|
||||
} else { \
|
||||
RTWDOG1->CNT = RTWDOG_REFRESH_KEY & 0xffffU; \
|
||||
RTWDOG1->CNT = RTWDOG_REFRESH_KEY >> 16; \
|
||||
} \
|
||||
if (primask_ == 0U) __enable_irq(); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_OTA_ROLLBACK_TIMER_START
|
||||
#define MG_OTA_ROLLBACK_TIMER_START() \
|
||||
do { \
|
||||
uint32_t primask_ = __get_PRIMASK(); \
|
||||
SRC_GENERAL_REG->SRMASK &= ~SRC_GENERAL_SRMASK_WDOG1_MASK_MASK; \
|
||||
__disable_irq(); \
|
||||
MG_IMXRT_RTWDOG1_UNLOCK(); \
|
||||
RTWDOG1->WIN = 0U; \
|
||||
RTWDOG1->TOVAL = (MG_IMXRT_WDOG1_TIMEOUT_MS + 7U) / 8U; \
|
||||
RTWDOG1->CS = RTWDOG_CS_EN_MASK | RTWDOG_CS_UPDATE_MASK | \
|
||||
RTWDOG_CS_CLK(1U) | RTWDOG_CS_PRES_MASK | \
|
||||
RTWDOG_CS_CMD32EN_MASK; \
|
||||
while ((RTWDOG1->CS & RTWDOG_CS_RCS_MASK) == 0U) (void) 0; \
|
||||
if (primask_ == 0U) __enable_irq(); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_ROLLBACK_TIMER_FEED
|
||||
#define MG_OTA_ROLLBACK_TIMER_FEED() MG_IMXRT_RTWDOG1_FEED()
|
||||
#endif
|
||||
|
||||
// GPR5 survives RTWDOG global resets and is not reserved by the boot ROM.
|
||||
#ifndef MG_OTA_STATE_GET
|
||||
#define MG_OTA_STATE_GET() SRC_GENERAL_REG->GPR[5]
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_STATE_SET
|
||||
#define MG_OTA_STATE_SET(v) (SRC_GENERAL_REG->GPR[5] = (uint32_t) (v))
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define MG_IMXRT_WDOG1_FEED() \
|
||||
do { \
|
||||
WDOG1->WSR = 0x5555; \
|
||||
WDOG1->WSR = 0xaaaa; \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_OTA_ROLLBACK_TIMER_START
|
||||
#define MG_OTA_ROLLBACK_TIMER_START() \
|
||||
do { \
|
||||
uint16_t wt_ = (uint16_t) ((MG_IMXRT_WDOG1_TIMEOUT_MS / 500) - 1); \
|
||||
if ((WDOG1->WCR & WDOG_WCR_WDE_MASK) == 0) { \
|
||||
WDOG1->WMCR = 0; \
|
||||
WDOG1->WCR = (uint16_t) (WDOG_WCR_WDZST_MASK | WDOG_WCR_WDBG_MASK | \
|
||||
WDOG_WCR_WDE_MASK | WDOG_WCR_WDT_MASK | \
|
||||
WDOG_WCR_SRS_MASK | WDOG_WCR_WDW_MASK | \
|
||||
WDOG_WCR_WT(wt_)); \
|
||||
} \
|
||||
MG_IMXRT_WDOG1_FEED(); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_ROLLBACK_TIMER_FEED
|
||||
#define MG_OTA_ROLLBACK_TIMER_FEED() MG_IMXRT_WDOG1_FEED()
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_STATE_GET
|
||||
#define MG_OTA_STATE_GET() SNVS->LPGPR[0]
|
||||
#endif
|
||||
|
||||
#ifndef MG_OTA_STATE_SET
|
||||
#define MG_OTA_STATE_SET(v) \
|
||||
do { \
|
||||
if (SNVS->LPSR & (1u << 3)) { \
|
||||
SNVS->LPLVDR = 0x41736166u; \
|
||||
SNVS->LPSR = 1u << 3; \
|
||||
} \
|
||||
SNVS->LPGPR[0] = (uint32_t) (v); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -50,10 +50,6 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifndef MG_ENABLE_DIRLIST
|
||||
#define MG_ENABLE_DIRLIST 1
|
||||
#endif
|
||||
|
||||
#ifndef MG_PATH_MAX
|
||||
#define MG_PATH_MAX FILENAME_MAX
|
||||
#endif
|
||||
|
||||
+1
-4
@@ -159,16 +159,13 @@ typedef int socklen_t;
|
||||
#define realpath(a, b) _fullpath((b), (a), MG_PATH_MAX)
|
||||
#define sleep(x) Sleep((x) *1000)
|
||||
#define mkdir(a, b) _mkdir(a)
|
||||
#define rmdir(a) _rmdir(a)
|
||||
#define timegm(x) _mkgmtime(x)
|
||||
|
||||
#ifndef S_ISDIR
|
||||
#define S_ISDIR(x) (((x) &_S_IFMT) == _S_IFDIR)
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_DIRLIST
|
||||
#define MG_ENABLE_DIRLIST 1
|
||||
#endif
|
||||
|
||||
#ifndef SIGPIPE
|
||||
#define SIGPIPE 0
|
||||
#endif
|
||||
|
||||
+49
-3
@@ -12,16 +12,16 @@ static int mg_base64_encode_single(int c) {
|
||||
}
|
||||
}
|
||||
|
||||
static int mg_base64_decode_single(int c) {
|
||||
static int base64_decode_single(int c, char plus, char slash) {
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
return c - 'A';
|
||||
} else if (c >= 'a' && c <= 'z') {
|
||||
return c + 26 - 'a';
|
||||
} else if (c >= '0' && c <= '9') {
|
||||
return c + 52 - '0';
|
||||
} else if (c == '+') {
|
||||
} else if (c == plus) {
|
||||
return 62;
|
||||
} else if (c == '/') {
|
||||
} else if (c == slash) {
|
||||
return 63;
|
||||
} else if (c == '=') {
|
||||
return 64;
|
||||
@@ -30,6 +30,14 @@ static int mg_base64_decode_single(int c) {
|
||||
}
|
||||
}
|
||||
|
||||
static int mg_base64_decode_single(int c) {
|
||||
return base64_decode_single(c, '+', '/');
|
||||
}
|
||||
|
||||
static int mg_base64url_decode_single(int c) {
|
||||
return base64_decode_single(c, '-', '_');
|
||||
}
|
||||
|
||||
size_t mg_base64_update(unsigned char ch, char *to, size_t n) {
|
||||
unsigned long rem = (n & 3) % 3;
|
||||
if (rem == 0) {
|
||||
@@ -91,3 +99,41 @@ fail:
|
||||
if (dl > 0) dst[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t mg_base64url_encode(const unsigned char *p, size_t n, char *to,
|
||||
size_t dl) {
|
||||
size_t i, len = mg_base64_encode(p, n, to, dl);
|
||||
if (len == 0) return 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (to[i] == '+') {
|
||||
to[i] = '-';
|
||||
} else if (to[i] == '/') {
|
||||
to[i] = '_';
|
||||
}
|
||||
}
|
||||
while (len > 0 && to[len - 1] == '=') to[--len] = '\0';
|
||||
return len;
|
||||
}
|
||||
|
||||
size_t mg_base64url_decode(const char *src, size_t n, char *dst, size_t dl) {
|
||||
size_t i, len = 0;
|
||||
unsigned int bits = 0, v = 0;
|
||||
if (dl == 0 || (n & 3) == 1) goto fail;
|
||||
for (i = 0; src != NULL && i < n; i++) {
|
||||
int c = mg_base64url_decode_single(src[i]);
|
||||
if (c == 64) break;
|
||||
if (c < 0) goto fail;
|
||||
v = (v << 6) | (unsigned int) c;
|
||||
bits += 6;
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
if (len + 1 >= dl) goto fail;
|
||||
dst[len++] = (char) ((v >> bits) & 255U);
|
||||
}
|
||||
}
|
||||
dst[len] = '\0';
|
||||
return len;
|
||||
fail:
|
||||
if (dl > 0) dst[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
@@ -4,3 +4,6 @@ size_t mg_base64_update(unsigned char input_byte, char *buf, size_t len);
|
||||
size_t mg_base64_final(char *buf, size_t len);
|
||||
size_t mg_base64_encode(const unsigned char *p, size_t n, char *buf, size_t);
|
||||
size_t mg_base64_decode(const char *src, size_t n, char *dst, size_t);
|
||||
size_t mg_base64url_encode(const unsigned char *p, size_t n, char *buf,
|
||||
size_t);
|
||||
size_t mg_base64url_decode(const char *src, size_t n, char *dst, size_t);
|
||||
@@ -4,22 +4,85 @@
|
||||
#include "net_builtin.h"
|
||||
|
||||
#if MG_ENABLE_BSD_SOCKETS
|
||||
void mg_bsd_init(struct mg_mgr *);
|
||||
void mg_bsd_poll(struct mg_mgr *);
|
||||
|
||||
#ifndef MG_ENABLE_BSD_PROTOTYPES
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/un.h>
|
||||
#else
|
||||
#include <errno.h>
|
||||
|
||||
#ifndef EAGAIN
|
||||
#define EAGAIN 11
|
||||
#endif
|
||||
#ifndef EWOULDBLOCK
|
||||
#define EWOULDBLOCK EAGAIN
|
||||
#endif
|
||||
#ifndef EINPROGRESS
|
||||
#define EINPROGRESS 36
|
||||
#endif
|
||||
#ifndef ENOTCONN
|
||||
#define ENOTCONN 107
|
||||
#endif
|
||||
#ifndef ECONNREFUSED
|
||||
#define ECONNREFUSED 111
|
||||
#endif
|
||||
#ifndef ECONNRESET
|
||||
#define ECONNRESET 104
|
||||
#endif
|
||||
#ifndef EADDRINUSE
|
||||
#define EADDRINUSE 98
|
||||
#endif
|
||||
#ifndef ETIMEDOUT
|
||||
#define ETIMEDOUT 110
|
||||
#endif
|
||||
#ifndef EBADF
|
||||
#define EBADF 9
|
||||
#endif
|
||||
#ifndef EMFILE
|
||||
#define EMFILE 24
|
||||
#endif
|
||||
#ifndef ENOMEM
|
||||
#define ENOMEM 12
|
||||
#endif
|
||||
#ifndef EIO
|
||||
#define EIO 5
|
||||
#endif
|
||||
#ifndef EINVAL
|
||||
#define EINVAL 22
|
||||
#endif
|
||||
#ifndef EPERM
|
||||
#define EPERM 1
|
||||
#endif
|
||||
#ifndef EPIPE
|
||||
#define EPIPE 32
|
||||
#endif
|
||||
#ifndef EAFNOSUPPORT
|
||||
#define EAFNOSUPPORT 97
|
||||
#endif
|
||||
#ifndef ENOPROTOOPT
|
||||
#define ENOPROTOOPT 92
|
||||
#endif
|
||||
#ifndef ENOSPC
|
||||
#define ENOSPC 28
|
||||
#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
|
||||
#define EPROTONOSUPPORT 93
|
||||
#endif
|
||||
|
||||
#if MG_ENABLE_BSD_PROTOTYPES
|
||||
typedef unsigned int socklen_t;
|
||||
typedef unsigned long nfds_t;
|
||||
typedef uint32_t in_addr_t;
|
||||
struct in_addr { in_addr_t s_addr; };
|
||||
struct in6_addr { uint8_t s6_addr[16]; };
|
||||
@@ -45,7 +108,6 @@ struct hostent {
|
||||
int h_length;
|
||||
char **h_addr_list;
|
||||
};
|
||||
#define h_addr h_addr_list[0]
|
||||
struct addrinfo {
|
||||
int ai_flags;
|
||||
int ai_family;
|
||||
@@ -57,6 +119,32 @@ struct addrinfo {
|
||||
struct addrinfo *ai_next;
|
||||
};
|
||||
struct pollfd { int fd; short events; short revents; };
|
||||
|
||||
#if !defined(FD_SET) && !defined(FD_ZERO) && !defined(FD_ISSET)
|
||||
#define MG_BSD_SELECT_H
|
||||
typedef unsigned long __fd_mask;
|
||||
#ifndef FD_SETSIZE
|
||||
#define FD_SETSIZE 64
|
||||
#endif
|
||||
#define _NFDBITS ((int) sizeof(__fd_mask) * 8)
|
||||
#define _howmany(x, y) (((x) + ((y) - 1)) / (y))
|
||||
typedef struct fd_set {
|
||||
__fd_mask __fds_bits[_howmany(FD_SETSIZE, _NFDBITS)];
|
||||
} fd_set;
|
||||
#define __fdset_mask(n) ((__fd_mask) 1 << ((n) % _NFDBITS))
|
||||
#define FD_CLR(n, p) ((p)->__fds_bits[(n) / _NFDBITS] &= ~__fdset_mask(n))
|
||||
#define FD_ISSET(n, p) \
|
||||
(((p)->__fds_bits[(n) / _NFDBITS] & __fdset_mask(n)) != 0)
|
||||
#define FD_SET(n, p) ((p)->__fds_bits[(n) / _NFDBITS] |= __fdset_mask(n))
|
||||
#define FD_ZERO(p) \
|
||||
do { \
|
||||
fd_set *_p = (p); \
|
||||
size_t _n = _howmany(FD_SETSIZE, _NFDBITS); \
|
||||
while (_n > 0) _p->__fds_bits[--_n] = 0; \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
#define h_addr h_addr_list[0]
|
||||
#define AF_INET 2
|
||||
#define AF_INET6 10
|
||||
#define AF_UNSPEC 0
|
||||
@@ -69,6 +157,9 @@ struct pollfd { int fd; short events; short revents; };
|
||||
#define IPPROTO_UDP 17
|
||||
#define INADDR_ANY 0
|
||||
#define INADDR_LOOPBACK 0x7f000001
|
||||
#define INADDR_NONE ((in_addr_t) -1)
|
||||
#define INET_ADDRSTRLEN 16
|
||||
#define INET6_ADDRSTRLEN 46
|
||||
#define SOL_SOCKET 0xffff
|
||||
#define SO_REUSEADDR 2
|
||||
#define SO_REUSEPORT 15
|
||||
@@ -77,79 +168,37 @@ struct pollfd { int fd; short events; short revents; };
|
||||
#define SO_BROADCAST 6
|
||||
#define SO_RCVBUF 8
|
||||
#define SO_SNDBUF 7
|
||||
#define SO_NOSIGPIPE 0x1022
|
||||
#define TCP_NODELAY 1
|
||||
#define MSG_DONTWAIT 0x40
|
||||
#define MSG_NOSIGNAL 0x4000
|
||||
#define MSG_PEEK 0x02
|
||||
#ifndef F_GETFL
|
||||
#define F_GETFL 3
|
||||
#endif
|
||||
#ifndef F_SETFL
|
||||
#define F_SETFL 4
|
||||
#define O_NONBLOCK 0x0004
|
||||
#endif
|
||||
#ifndef O_NONBLOCK
|
||||
#define O_NONBLOCK 0x4000
|
||||
#endif
|
||||
#define SHUT_RD 0
|
||||
#define SHUT_WR 1
|
||||
#define SHUT_RDWR 2
|
||||
#define AI_PASSIVE 0x0001
|
||||
#define NI_MAXHOST 1025
|
||||
#define NI_MAXSERV 32
|
||||
#ifndef EAGAIN
|
||||
#define EAGAIN 11
|
||||
#endif
|
||||
#ifndef EWOULDBLOCK
|
||||
#define EWOULDBLOCK EAGAIN
|
||||
#endif
|
||||
#ifndef EINPROGRESS
|
||||
#define EINPROGRESS 36
|
||||
#endif
|
||||
#ifndef ENOTCONN
|
||||
#define ENOTCONN 107
|
||||
#endif
|
||||
#ifndef ECONNREFUSED
|
||||
#define ECONNREFUSED 111
|
||||
#endif
|
||||
#ifndef ECONNRESET
|
||||
#define ECONNRESET 104
|
||||
#endif
|
||||
#ifndef EADDRINUSE
|
||||
#define EADDRINUSE 98
|
||||
#endif
|
||||
#ifndef ETIMEDOUT
|
||||
#define ETIMEDOUT 110
|
||||
#endif
|
||||
#define POLLIN 0x001
|
||||
#define POLLOUT 0x004
|
||||
#define POLLERR 0x008
|
||||
#define POLLHUP 0x010
|
||||
#define POLLNVAL 0x020
|
||||
uint16_t htons(uint16_t);
|
||||
uint16_t ntohs(uint16_t);
|
||||
uint32_t htonl(uint32_t);
|
||||
uint32_t ntohl(uint32_t);
|
||||
#endif // MG_ENABLE_BSD_PROTOTYPES
|
||||
|
||||
#define closesocket(a) close(a)
|
||||
#define htons(x) mg_ntohs(x)
|
||||
#define ntohs(x) mg_ntohs(x)
|
||||
#define htonl(x) mg_ntohl(x)
|
||||
#define ntohl(x) mg_ntohl(x)
|
||||
|
||||
#ifndef MG_BSD_BACKLOG
|
||||
#define MG_BSD_BACKLOG 5
|
||||
#endif
|
||||
|
||||
#ifndef MG_BSD_CONNECT_TIMEOUT_MS
|
||||
#define MG_BSD_CONNECT_TIMEOUT_MS 10000
|
||||
#endif
|
||||
|
||||
// Mongoose-specific API
|
||||
void mg_bsd_init(void); // must be called before socket()
|
||||
void mg_bsd_poll(struct mg_mgr *); // process pending BSD commands
|
||||
|
||||
// Transport backend: implement these OR define MG_ENABLE_FREERTOS for the built-in backend
|
||||
void *mg_bsd_transport_new(int domain, int type, int proto);
|
||||
void mg_bsd_transport_free(void *t);
|
||||
int mg_bsd_transport_listen(void *t, const struct sockaddr_in *addr);
|
||||
void *mg_bsd_transport_accept(void *t, struct sockaddr_in *peer, bool nonblock);
|
||||
ssize_t mg_bsd_transport_recv(void *t, void *buf, size_t len, bool nonblock);
|
||||
ssize_t mg_bsd_transport_send(void *t, const void *buf, size_t len, bool nonblock);
|
||||
int mg_bsd_transport_connect(void *t, const struct sockaddr_in *addr, bool nonblock);
|
||||
void mg_bsd_transport_close(void *t);
|
||||
|
||||
// Standard BSD socket API
|
||||
int socket(int, int, int);
|
||||
int bind(int, const struct sockaddr *, socklen_t);
|
||||
int listen(int, int);
|
||||
@@ -157,23 +206,40 @@ int accept(int, struct sockaddr *, socklen_t *);
|
||||
int connect(int, const struct sockaddr *, socklen_t);
|
||||
ssize_t send(int, const void *, size_t, int);
|
||||
ssize_t recv(int, void *, size_t, int);
|
||||
ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
|
||||
ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *,
|
||||
socklen_t);
|
||||
ssize_t recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
|
||||
int close(int);
|
||||
int shutdown(int, int);
|
||||
int fcntl(int, int, int);
|
||||
int setsockopt(int, int, int, const void *, socklen_t);
|
||||
int getsockopt(int, int, int, void *, socklen_t *);
|
||||
int getsockname(int, struct sockaddr *, socklen_t *);
|
||||
int getpeername(int, struct sockaddr *, socklen_t *);
|
||||
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
|
||||
int poll(struct pollfd *, unsigned int, int);
|
||||
int poll(struct pollfd *, nfds_t, int);
|
||||
struct hostent *gethostbyname(const char *);
|
||||
int getaddrinfo(const char *, const char *, const struct addrinfo *, struct addrinfo **);
|
||||
int getaddrinfo(const char *, const char *, const struct addrinfo *,
|
||||
struct addrinfo **);
|
||||
void freeaddrinfo(struct addrinfo *);
|
||||
int inet_pton(int, const char *, void *);
|
||||
const char *inet_ntop(int, const void *, char *, socklen_t);
|
||||
in_addr_t inet_addr(const char *);
|
||||
char *inet_ntoa(struct in_addr);
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <poll.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#endif // MG_ENABLE_BSD_PROTOTYPES
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
#else
|
||||
#define mg_bsd_init(mgr)
|
||||
#define mg_bsd_poll(mgr)
|
||||
#endif // MG_ENABLE_BSD_SOCKETS
|
||||
+39
-9
@@ -12,6 +12,10 @@
|
||||
#define MG_ENABLE_CUSTOM_LOG 0 // Let user define their own MG_LOG
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_CUSTOM_USAGE
|
||||
#define MG_ENABLE_CUSTOM_USAGE 0 // Let user define mg_usage
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_FREERTOS
|
||||
#define MG_ENABLE_FREERTOS 0 // FreeRTOS RTOS
|
||||
#endif
|
||||
@@ -82,10 +86,6 @@
|
||||
#define MG_ENABLE_WINSOCK 1
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_DIRLIST
|
||||
#define MG_ENABLE_DIRLIST 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_CUSTOM_RANDOM
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 0
|
||||
#endif
|
||||
@@ -94,6 +94,10 @@
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_CUSTOM_MICROS
|
||||
#define MG_ENABLE_CUSTOM_MICROS 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_ASSERT
|
||||
#define MG_ENABLE_ASSERT 0
|
||||
#endif
|
||||
@@ -135,7 +139,7 @@
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_POSIX_FS
|
||||
#define MG_ENABLE_POSIX_FS 0
|
||||
#define MG_ENABLE_POSIX_FS MG_ENABLE_LFS
|
||||
#endif
|
||||
|
||||
#ifndef MG_INVALID_SOCKET
|
||||
@@ -175,6 +179,14 @@
|
||||
#define MG_ENABLE_TCPIP_DRIVER_INIT 1 // enabled built-in driver for
|
||||
#endif // Mongoose built-in network stack
|
||||
|
||||
#ifndef MG_ENABLE_TCPIP_TCPRTX
|
||||
#define MG_ENABLE_TCPIP_TCPRTX 0 // TCP retransmission queue
|
||||
#endif
|
||||
|
||||
#ifndef MG_TCPIP_DHCPNAME_SIZE
|
||||
#define MG_TCPIP_DHCPNAME_SIZE 18 // struct mg_tcpip_if :: dhcp_name size
|
||||
#endif
|
||||
|
||||
#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223)
|
||||
#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
|
||||
#endif
|
||||
@@ -211,10 +223,6 @@
|
||||
#define MG_SET_MAC_ADDRESS(mac)
|
||||
#endif
|
||||
|
||||
#ifndef MG_TCPIP_DHCPNAME_SIZE
|
||||
#define MG_TCPIP_DHCPNAME_SIZE 18 // struct mg_tcpip_if :: dhcp_name size
|
||||
#endif
|
||||
|
||||
#ifndef MG_SET_WIFI_CONFIG
|
||||
#define MG_SET_WIFI_CONFIG(ifp, driver_data)
|
||||
#endif
|
||||
@@ -223,6 +231,28 @@
|
||||
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
|
||||
#endif
|
||||
|
||||
#ifndef MG_TLS_RECORD_SIZE
|
||||
#define MG_TLS_RECORD_SIZE 16384 // Max plaintext per sent TLS record, builtin
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_CHACHA20
|
||||
#define MG_ENABLE_CHACHA20 1 // When set to 0, GCM is used. For MG_TLS_BUILTIN
|
||||
#endif
|
||||
|
||||
// P-256 implementation, for MG_TLS_BUILTIN
|
||||
#define MG_P256_LENNGREN 1 // Cortex-M4/M7/M33 asm, see tls_p256_cortex.c
|
||||
#define MG_P256_BEAR 3 // Portable C, BearSSL, see tls_p256.c
|
||||
|
||||
#ifndef MG_P256
|
||||
#if defined(__GNUC__) && defined(__ARM_ARCH_PROFILE) && \
|
||||
__ARM_ARCH_PROFILE == 'M' && defined(__ARM_FEATURE_DSP) && \
|
||||
!defined(__ARM_BIG_ENDIAN)
|
||||
#define MG_P256 MG_P256_LENNGREN // Cortex-M with DSP: M4, M7, M33, M55, M85
|
||||
#else
|
||||
#define MG_P256 MG_P256_BEAR
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef MG_ENABLE_HEALTH
|
||||
#define MG_ENABLE_HEALTH 0
|
||||
#endif
|
||||
+49
-183
@@ -1,5 +1,6 @@
|
||||
#include "dash.h"
|
||||
#include "http.h"
|
||||
#include "ota.h"
|
||||
#include "util.h"
|
||||
|
||||
#define MG_NO_CACHE_HEADERS "Cache-Control: no-cache\r\n"
|
||||
@@ -77,12 +78,7 @@ static int mg_dash_array_size(struct mg_field_set *set,
|
||||
struct mg_dash_user *u) {
|
||||
int saved = *set->index, sz = -1;
|
||||
*set->index = -1;
|
||||
if (set->fn) {
|
||||
if (set->fn(MG_DASH_READ, u)) sz = *set->index;
|
||||
} else if (set->get_dir) {
|
||||
mg_dash_dir_read(set, u);
|
||||
sz = *set->index;
|
||||
}
|
||||
if (set->fn && set->fn(MG_DASH_READ, u)) sz = *set->index;
|
||||
*set->index = saved;
|
||||
return sz;
|
||||
}
|
||||
@@ -100,10 +96,7 @@ static size_t mg_dash_print_array(mg_pfn_t fn, void *arg, va_list *ap) {
|
||||
for (;;) {
|
||||
bool done = to >= 0 && *set->index > to;
|
||||
if (!done) {
|
||||
if (set->fn)
|
||||
set->fn(MG_DASH_READ, u);
|
||||
else if (set->get_dir)
|
||||
mg_dash_dir_read(set, u);
|
||||
if (set->fn) set->fn(MG_DASH_READ, u);
|
||||
done = *set->index < 0;
|
||||
}
|
||||
if (done) break;
|
||||
@@ -167,9 +160,16 @@ static size_t mg_dash_print_endpoint(mg_pfn_t fn, void *arg, va_list *ap) {
|
||||
void mg_dash_send_change(struct mg_mgr *mgr, struct mg_field_set *set) {
|
||||
struct mg_connection *c;
|
||||
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||
struct mg_dash_cdata *d = (struct mg_dash_cdata *) c->data;
|
||||
struct mg_dash_user *u = d->u;
|
||||
struct mg_dash_cdata *d;
|
||||
struct mg_dash_user *u;
|
||||
if (!c->is_websocket) continue;
|
||||
if (c->send.len > MG_DASH_MAX_SEND_BUF_SIZE) { // Skip slow consumers
|
||||
MG_ERROR(("skipping, send buf %lu > %d", (unsigned long) c->send.len,
|
||||
MG_DASH_MAX_SEND_BUF_SIZE));
|
||||
continue;
|
||||
}
|
||||
d = (struct mg_dash_cdata *) c->data;
|
||||
u = d->u;
|
||||
if (u == NULL) continue;
|
||||
if (set->index != NULL && *set->index < 0) {
|
||||
int sz = mg_dash_array_size(set, u);
|
||||
@@ -178,8 +178,7 @@ void mg_dash_send_change(struct mg_mgr *mgr, struct mg_field_set *set) {
|
||||
MG_ESC("change"), MG_ESC("params"), MG_ESC(set->name), sz);
|
||||
} else {
|
||||
int saved_idx = set->index != NULL ? *set->index : 0;
|
||||
bool ok = set->fn ? set->fn(MG_DASH_READ, u)
|
||||
: (set->get_dir ? mg_dash_dir_read(set, u) : true);
|
||||
bool ok = set->fn ? set->fn(MG_DASH_READ, u) : true;
|
||||
if (!ok) {
|
||||
if (set->index != NULL) *set->index = saved_idx;
|
||||
continue;
|
||||
@@ -226,8 +225,7 @@ static int mg_dash_parse_field(struct mg_str json, struct mg_field *f) {
|
||||
mg_json_get_num(json, json_path, (double *) f->value);
|
||||
} else if (f->type == MG_VAL_STR && f->value_size > 0) {
|
||||
struct mg_str tok = mg_json_get_tok(json, json_path);
|
||||
ok = tok.len >= 2 && tok.buf[0] == '"' &&
|
||||
tok.buf[tok.len - 1] == '"';
|
||||
ok = tok.len >= 2 && tok.buf[0] == '"' && tok.buf[tok.len - 1] == '"';
|
||||
if (ok) mg_json_unescape(json, json_path, (char *) f->value, f->value_size);
|
||||
} else if (f->type == MG_VAL_RAW && f->value_size > 0) {
|
||||
ok = mg_snprintf((char *) f->value, f->value_size, "%.*s", json.len,
|
||||
@@ -264,68 +262,6 @@ static int mg_dash_apply(struct mg_connection *c, struct mg_dash *dash,
|
||||
return total_count;
|
||||
}
|
||||
|
||||
bool mg_dash_dir_read(struct mg_field_set *set, struct mg_dash_user *u) {
|
||||
char dir[256], fname[128] = "";
|
||||
struct mg_fs *fs = u->dash->upload_fs ? u->dash->upload_fs : &mg_fs_posix;
|
||||
struct mg_field *name_field = NULL, *size_field = NULL;
|
||||
size_t i;
|
||||
|
||||
if (!set->get_dir(u, dir, sizeof(dir))) return false;
|
||||
|
||||
for (i = 0; set->fields[i].name != NULL; i++) {
|
||||
if (name_field == NULL && set->fields[i].type == MG_VAL_STR &&
|
||||
strcmp(set->fields[i].name, "name") == 0)
|
||||
name_field = &set->fields[i];
|
||||
if (size_field == NULL && strcmp(set->fields[i].name, "size") == 0)
|
||||
size_field = &set->fields[i];
|
||||
}
|
||||
if (name_field == NULL) return false;
|
||||
|
||||
if (*set->index == -1) { // Size query: count all files
|
||||
int count = 0;
|
||||
while (mg_fs_ls(fs, dir, fname, sizeof(fname))) count++;
|
||||
*set->index = count;
|
||||
return true;
|
||||
}
|
||||
|
||||
{ // Regular read: scan to *set->index
|
||||
int target = *set->index, cur = 0;
|
||||
while (mg_fs_ls(fs, dir, fname, sizeof(fname))) {
|
||||
if (cur++ == target) {
|
||||
mg_snprintf((char *) name_field->value, name_field->value_size, "%s",
|
||||
fname);
|
||||
if (size_field != NULL) {
|
||||
char path[512];
|
||||
size_t sz = 0;
|
||||
mg_snprintf(path, sizeof(path), "%s/%s", dir, fname);
|
||||
fs->st(path, &sz, NULL);
|
||||
if (size_field->type == MG_VAL_UINT64)
|
||||
*(uint64_t *) size_field->value = (uint64_t) sz;
|
||||
else if (size_field->type == MG_VAL_INT)
|
||||
*(int *) size_field->value = (int) sz;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
*set->index = -1; // No more entries
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool mg_dash_set_file_name(struct mg_field_set *set,
|
||||
struct mg_str name) {
|
||||
size_t i;
|
||||
for (i = 0; set->fields[i].name != NULL; i++) {
|
||||
struct mg_field *f = &set->fields[i];
|
||||
if (f->type == MG_VAL_STR && strcmp(f->name, "name") == 0) {
|
||||
mg_snprintf((char *) f->value, f->value_size, "%.*s", (int) name.len,
|
||||
name.buf);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline void mg_log_http_req(struct mg_connection *c,
|
||||
struct mg_http_message *hm) {
|
||||
int len = 0;
|
||||
@@ -358,33 +294,6 @@ static inline void mg_log_http_req(struct mg_connection *c,
|
||||
c->send.buf + n));
|
||||
}
|
||||
|
||||
static void mg_dash_ota_cb(struct mg_connection *c, const char *errmsg) {
|
||||
mg_http_reply(c, errmsg ? 500 : 200, NULL, errmsg ? errmsg : "ok\n");
|
||||
c->is_draining = 1;
|
||||
}
|
||||
|
||||
static void mg_dash_upload_cb(struct mg_connection *c, const char *errmsg) {
|
||||
if (errmsg) {
|
||||
mg_http_reply(c, 500, NULL, "%s\n", errmsg);
|
||||
} else {
|
||||
// mg_http_start_upload() repurposes c->data for its own bookkeeping,
|
||||
// so the field set can't be cached there. Re-derive the dashboard from
|
||||
// c->fn_data instead, and notify every file-backed array: the upload
|
||||
// could belong to any of them, and re-querying get_dir() per recipient
|
||||
// is what mg_dash_send_change() does anyway (directories can be
|
||||
// user-specific)
|
||||
struct mg_dash *dash = (struct mg_dash *) c->fn_data;
|
||||
struct mg_field_set *fs;
|
||||
mg_http_reply(c, 200, NULL, "ok\n");
|
||||
for (fs = dash->sets; fs != NULL; fs = fs->next) {
|
||||
if (fs->get_dir == NULL) continue;
|
||||
*fs->index = -1; // Signal mg_dash_send_change() to broadcast new size
|
||||
mg_dash_send_change(c->mgr, fs);
|
||||
}
|
||||
}
|
||||
c->is_draining = 1;
|
||||
}
|
||||
|
||||
static uint64_t mg_dash_make_expiration_time(struct mg_dash *dash) {
|
||||
unsigned t = (unsigned) dash->session_auto_expiration_seconds;
|
||||
if (t == 0) t = 3600; // Default expiration time in seconds
|
||||
@@ -624,6 +533,25 @@ static void mg_dash_handle_add(struct mg_connection *c, struct mg_dash *dash,
|
||||
}
|
||||
}
|
||||
|
||||
static void mg_serve_fs(struct mg_connection *c, struct mg_http_message *hm,
|
||||
struct mg_dash_user *u, bool stream) {
|
||||
struct mg_dash *dash = (struct mg_dash *) c->fn_data;
|
||||
char root[128] = "/fs/=";
|
||||
if (u == NULL) {
|
||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||
} else if (dash->files_dir(u, root + 5, sizeof(root) - 5) == false) {
|
||||
mg_http_reply(c, 500, MG_JSON_HEADERS, "Upload dir error\n");
|
||||
} else {
|
||||
struct mg_http_serve_opts o;
|
||||
memset(&o, 0, sizeof(o));
|
||||
o.root_dir = root;
|
||||
o.fs = dash->upload_fs ? dash->upload_fs : &mg_fs_posix;
|
||||
o.enable_delete = o.enable_upload = o.enable_dir_listing = true;
|
||||
if (stream) mg_http_serve_upload(c, hm, &o);
|
||||
else mg_http_serve_dir(c, hm, &o);
|
||||
}
|
||||
}
|
||||
|
||||
void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
struct mg_dash *dash = (struct mg_dash *) c->fn_data;
|
||||
struct mg_dash_cdata *d = (struct mg_dash_cdata *) c->data;
|
||||
@@ -637,9 +565,6 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
// Received headers - check authentication and possibly start uploads/ota
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
struct mg_dash_user *u = mg_dash_authenticate(c, hm, dash);
|
||||
struct mg_str parts[3];
|
||||
memset(parts, 0, sizeof(parts));
|
||||
|
||||
if (mg_match(hm->uri, mg_str("/api/hi"), NULL) ||
|
||||
mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
||||
// Do nothing, handle them MG_EV_HTTP_MSG. We bypass auth for those
|
||||
@@ -649,49 +574,21 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
} else if (mg_match(hm->uri, mg_str("/api/login"), NULL) && u != NULL) {
|
||||
mg_handle_login(c, u);
|
||||
d->marker = CONN_HANDLED;
|
||||
} else if (u != NULL && dash->files_dir != NULL &&
|
||||
mg_match(hm->uri, mg_str("/fs/#"), NULL)) {
|
||||
mg_serve_fs(c, hm, u, true);
|
||||
return; // Important - the serve_fs() call can invalidate hm
|
||||
} else if (mg_match(hm->uri, mg_str("/api/ota"), NULL)) {
|
||||
mg_http_start_ota(c, hm, mg_dash_ota_cb);
|
||||
} else if (mg_match(hm->uri, mg_str("/fs/*/*"), parts) &&
|
||||
(mg_strcasecmp(hm->method, mg_str("POST")) == 0 ||
|
||||
mg_strcasecmp(hm->method, mg_str("PUT")) == 0)) {
|
||||
struct mg_field_set *set = mg_dash_find_field_set(dash, parts[0]);
|
||||
struct mg_str name = parts[1];
|
||||
int len =
|
||||
mg_url_decode(name.buf, name.len, (char *) name.buf, name.len + 1, 0);
|
||||
if (len > 0 && (size_t) len <= name.len) name.len = (size_t) len;
|
||||
if (set == NULL || set->get_dir == NULL) {
|
||||
mg_http_reply(c, 404, MG_JSON_HEADERS, "Not Found\n");
|
||||
d->marker = CONN_HANDLED;
|
||||
} else if (u == NULL) {
|
||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||
d->marker = CONN_HANDLED;
|
||||
} else if (!mg_path_is_sane(name)) {
|
||||
mg_http_reply(c, 400, MG_JSON_HEADERS, "Bad file name\n");
|
||||
d->marker = CONN_HANDLED;
|
||||
} else {
|
||||
mg_dash_set_file_name(set, name);
|
||||
if (set->fn != NULL && !set->fn(MG_DASH_WRITE, u)) {
|
||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||
d->marker = CONN_HANDLED;
|
||||
} else {
|
||||
char dir[256];
|
||||
struct mg_fs *fs = dash->upload_fs ? dash->upload_fs : &mg_fs_posix;
|
||||
if (!set->get_dir(u, dir, sizeof(dir))) {
|
||||
mg_http_reply(c, 500, MG_JSON_HEADERS, "Upload dir error\n");
|
||||
d->marker = CONN_HANDLED;
|
||||
} else {
|
||||
mg_http_start_upload(c, hm, name, mg_str(dir), fs,
|
||||
mg_dash_upload_cb);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Authenticated above. Streaming owns the connection and c->data.
|
||||
mg_http_stream_body(c, hm, mg_http_ota_cb, NULL);
|
||||
return;
|
||||
}
|
||||
if (d->marker != '\0') mg_log_http_req(c, hm);
|
||||
} else if (ev == MG_EV_HTTP_MSG && d->marker != '\0') {
|
||||
// The response has been send in EV_HDRS path, so we're not reponding
|
||||
// anything but clearing the marker for the next request.
|
||||
d->marker = 0;
|
||||
c->is_resp = 0;
|
||||
c->pfn_data = NULL; // Response done
|
||||
} else if (ev == MG_EV_HTTP_MSG && d->marker == '\0') {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
struct mg_dash_user *u = mg_dash_authenticate(c, hm, dash);
|
||||
@@ -702,50 +599,19 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
mg_http_reply(c, 200, MG_JSON_HEADERS, "hi\n");
|
||||
} else if (mg_match(hm->uri, mg_str("/api/logout"), NULL)) {
|
||||
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)) {
|
||||
d->u = u;
|
||||
mg_ws_upgrade(c, hm, NULL);
|
||||
} else if (mg_match(hm->uri, mg_str("/fs/*/*"), parts)) {
|
||||
struct mg_field_set *set = mg_dash_find_field_set(dash, parts[0]);
|
||||
if (set == NULL || set->get_dir == NULL) {
|
||||
mg_http_reply(c, 404, MG_JSON_HEADERS, "Not Found");
|
||||
} else if (u == NULL) {
|
||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||
} else {
|
||||
char dir[256], path[512];
|
||||
struct mg_fs *fs = dash->upload_fs ? dash->upload_fs : &mg_fs_posix;
|
||||
struct mg_str name = parts[1];
|
||||
int len = mg_url_decode(name.buf, name.len, (char *) name.buf,
|
||||
name.len + 1, 0);
|
||||
if (len > 0 && (size_t) len <= name.len) name.len = (size_t) len;
|
||||
if (!mg_path_is_sane(name)) {
|
||||
mg_http_reply(c, 400, MG_JSON_HEADERS, "Bad file name\n");
|
||||
return;
|
||||
}
|
||||
if (!set->get_dir(u, dir, sizeof(dir))) {
|
||||
mg_http_reply(c, 500, MG_JSON_HEADERS, "Upload dir error\n");
|
||||
return;
|
||||
}
|
||||
mg_snprintf(path, sizeof(path), "%s/%.*s", dir, name.len, name.buf);
|
||||
if (mg_strcasecmp(hm->method, mg_str("DELETE")) == 0) {
|
||||
mg_dash_set_file_name(set, name);
|
||||
if (set->fn != NULL && !set->fn(MG_DASH_DELETE, u)) {
|
||||
mg_http_reply(c, 403, MG_JSON_HEADERS, "Not Authorised\n");
|
||||
} else {
|
||||
fs->rm(path);
|
||||
*set->index = -1; // Signal mg_dash_send_change() to send new size
|
||||
mg_dash_send_change(c->mgr, set);
|
||||
mg_http_reply(c, 200, NULL, "true");
|
||||
}
|
||||
} else {
|
||||
mg_http_serve_file(c, hm, path, NULL);
|
||||
}
|
||||
}
|
||||
} else if (dash->files_dir != NULL &&
|
||||
mg_match(hm->uri, mg_str("/fs/#"), NULL)) {
|
||||
mg_serve_fs(c, hm, u, false);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/del/*/*/*"), parts) ||
|
||||
mg_match(hm->uri, mg_str("/api/del/*/*"), parts)) {
|
||||
if (mg_strcmp(hm->method, mg_str("DELETE")) == 0) {
|
||||
mg_dash_handle_del(c, dash, u, parts);
|
||||
} else {
|
||||
mg_http_reply(c, 405, "Allow: DELETE\r\n", "Method Not Allowed\n");
|
||||
}
|
||||
} else if (mg_match(hm->uri, mg_str("/api/add/*"), parts)) {
|
||||
mg_dash_handle_add(c, dash, u, parts, hm->body);
|
||||
} else if (mg_match(hm->uri, mg_str("/api/get/*/*"), parts) &&
|
||||
@@ -773,7 +639,7 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ch == NULL) mg_http_reply(c, 404, MG_JSON_HEADERS, "Not Found");
|
||||
if (ch == NULL) mg_http_reply(c, 404, MG_JSON_HEADERS, "Not Found\n");
|
||||
mg_log_http_req(c, hm);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-39
@@ -73,8 +73,6 @@ struct mg_field_set {
|
||||
// fn sets *index = -1 to signal end of iteration.
|
||||
// Size query: framework sets *index = -1 before fn(READ); fn
|
||||
// sets *index = total size
|
||||
bool (*get_dir)(const struct mg_dash_user *u, char *buf,
|
||||
size_t len); // Non-NULL: file array
|
||||
struct mg_field_set *next; // Next set in the list
|
||||
};
|
||||
|
||||
@@ -96,6 +94,11 @@ struct mg_dash {
|
||||
int session_auto_expiration_seconds; // Session lifetime, 0: 1 hour default
|
||||
struct mg_dash_user *guest; // Used when authenticate == NULL
|
||||
struct mg_fs *upload_fs; // FS for uploads/listing, default &mg_fs_posix
|
||||
// Resolves the built-in file manager's root dir for user u, e.g. per-user
|
||||
// upload directories. NULL: file manager disabled
|
||||
// - The returned directory must be nonempty and must not contain ',' or '=',
|
||||
// because it is used as an mg_http_serve_opts::root_dir mapping target.
|
||||
bool (*files_dir)(const struct mg_dash_user *u, char *buf, size_t len);
|
||||
};
|
||||
|
||||
#define MG_DASH_ADD_FIELD_SET(dash_, set_) \
|
||||
@@ -121,43 +124,6 @@ void mg_dash_ev_handler(struct mg_connection *c, int ev, void *ev_data);
|
||||
// array size if *set->index < 0, or a single element update otherwise
|
||||
void mg_dash_send_change(struct mg_mgr *mgr, struct mg_field_set *);
|
||||
|
||||
// Default reader for file-backed array sets (set->get_dir != NULL)
|
||||
// Usage example - this is a complete implemenation of the file manager:
|
||||
// ```c
|
||||
// static struct file {
|
||||
// int index;
|
||||
// char name[64];
|
||||
// size_t size;
|
||||
// uint64_t checksum;
|
||||
// } s_file;
|
||||
// static struct mg_field_set set_files;
|
||||
//
|
||||
// static bool get_dir(const struct mg_dash_user *u, char *buf, size_t len) {
|
||||
// (void) u;
|
||||
// mkdir("/tmp/dashboard", 0755);
|
||||
// mg_snprintf(buf, len, "%s", "/tmp/dashboard");
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// static struct mg_field fields_files[] = {
|
||||
// {"name", MG_VAL_STR, s_file.name, sizeof(s_file.name)},
|
||||
// {"size", MG_VAL_UINT64, &s_file.size, 0},
|
||||
// {"checksum", MG_VAL_UINT64, &s_file.checksum, sizeof(s_file.checksum)},
|
||||
// {NULL, MG_VAL_INT, NULL, 0},
|
||||
// };
|
||||
//
|
||||
// static bool files_fn(enum mg_dash_op op, struct mg_dash_user *u) {
|
||||
// if (op == MG_DASH_WRITE) return s_uploads_enabled && u->level >= 7;
|
||||
// if (op != MG_DASH_READ) return false;
|
||||
// if (!mg_dash_dir_read(&set_files, u)) return false;
|
||||
// s_file.checksum = 0; // Mock to show how to set custom fields
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// static struct mg_field_set set_files = {"files", fields_files, files_fn, &s_file.index, get_dir, NULL};
|
||||
// ```
|
||||
bool mg_dash_dir_read(struct mg_field_set *set, struct mg_dash_user *u);
|
||||
|
||||
// Helper forward declarations for Mongoose CMSIS pack modules
|
||||
extern struct mg_mgr g_mgr;
|
||||
extern void mg_dash_init(struct mg_mgr *);
|
||||
|
||||
@@ -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);
|
||||
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;
|
||||
// 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));
|
||||
ofs += n;
|
||||
}
|
||||
} // on unresolved responses, we got the name and query type, at least
|
||||
|
||||
if (!is_response) {
|
||||
// For queries, there is no need to parse the answers. In this way,
|
||||
@@ -228,16 +229,16 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
|
||||
uint16_t txnid, bool ipv6) {
|
||||
struct {
|
||||
static bool dns_send(struct mg_connection *c, const struct mg_str *name,
|
||||
unsigned int rtype, uint16_t txnid, uint16_t flags) {
|
||||
struct { // RFC-1035 4.1.2
|
||||
struct mg_dns_header header;
|
||||
uint8_t data[256];
|
||||
} pkt;
|
||||
size_t i, n;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
pkt.header.txnid = mg_htons(txnid);
|
||||
pkt.header.flags = mg_htons(0x100);
|
||||
pkt.header.flags = mg_htons(flags);
|
||||
pkt.header.num_questions = mg_htons(1);
|
||||
for (i = n = 0; i < sizeof(pkt.data) - 5; i++) {
|
||||
if (name->buf[i] == '.' || i >= name->len) {
|
||||
@@ -247,25 +248,30 @@ static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
|
||||
}
|
||||
if (i >= name->len) break;
|
||||
}
|
||||
memcpy(&pkt.data[n], "\x00\x00\x01\x00\x01", 5); // A query
|
||||
memset(&pkt.data[n], 0, 5); // nul, QTYPE, QCLASS
|
||||
pkt.data[n + 2] = (uint8_t) rtype; // QTYPE = rtype, only 0-255 supported
|
||||
pkt.data[n + 4] = 1; // QCLASS = IN
|
||||
n += 5;
|
||||
if (ipv6) pkt.data[n - 3] = 0x1c; // AAAA query
|
||||
// memcpy(&pkt.data[n], "\xc0\x0c\x00\x1c\x00\x01", 6); // AAAA query
|
||||
// n += 6;
|
||||
return mg_send(c, &pkt, sizeof(pkt.header) + n);
|
||||
}
|
||||
|
||||
static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
|
||||
uint16_t txnid, bool ipv6) {
|
||||
return dns_send(c, name, ipv6 ? MG_DNS_RTYPE_AAAA : MG_DNS_RTYPE_A, txnid,
|
||||
0x100); // RD
|
||||
}
|
||||
|
||||
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc);
|
||||
bool mg_dnsc_init(struct mg_mgr *mgr, struct mg_dns *dnsc) {
|
||||
if (dnsc->url == NULL) {
|
||||
mg_error(0, "DNS server URL is NULL. Call mg_mgr_init()");
|
||||
MG_ERROR(("DNS server URL is NULL. Call mg_mgr_init()"));
|
||||
return false;
|
||||
}
|
||||
if (dnsc->c == NULL) {
|
||||
dnsc->c = mg_connect(mgr, dnsc->url, NULL, NULL);
|
||||
if (dnsc->c == NULL) return false;
|
||||
dnsc->c->pfn = dns_cb;
|
||||
}
|
||||
dnsc->c->pfn = dns_cb; // do it always, DHCP-assigned DNS bootstrap needs it
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -435,7 +441,7 @@ static void handle_mdns_query(struct mg_connection *c) {
|
||||
n = mg_dns_parse_rr(c->recv.buf, c->recv.len, 12, true, &rr);
|
||||
MG_VERBOSE(("mDNS request parsed, result=%d", (int) n));
|
||||
if (n > 0) {
|
||||
// RFC-6762 Appendix C, RFC2181 11: m(n + 1-63), max 255 + 0x0
|
||||
// RFC-6762 Appendix C, RFC-2181 11: m(n + 1-63), max 255 + 0x0
|
||||
uint8_t buf[sizeof(struct mg_dns_header) + 256 + sizeof(mdns_answer) + 4];
|
||||
struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
||||
uint8_t *p = &buf[sizeof(*h)];
|
||||
@@ -458,7 +464,6 @@ static void handle_mdns_query(struct mg_connection *c) {
|
||||
MG_VERBOSE(("RR %u %u %s", (unsigned int) rr.atype,
|
||||
(unsigned int) rr.aclass, name));
|
||||
if (rr.atype == MG_DNS_RTYPE_A) {
|
||||
// TODO(): ensure c->fn_data ends in \0
|
||||
// if we have a name to match, go; otherwise users will match and fill
|
||||
// req.r.name and set req.is_resp
|
||||
if (c->fn_data != NULL && mg_casecmp((char *) c->fn_data, name) != 0)
|
||||
@@ -574,44 +579,150 @@ static void handle_mdns_query(struct mg_connection *c) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static size_t srvtoname(char *svc) {
|
||||
char *dot = strchr(svc, '.');
|
||||
if (dot == NULL) return 0;
|
||||
*dot = '\0';
|
||||
return (size_t) (dot - svc);
|
||||
}
|
||||
|
||||
#define MG_MAX_MDNS_RECORDS 8
|
||||
// Condense one useful mDNS/DNS-SD chain from a response datagram into a single
|
||||
// MG_EV_MDNS_RESP event. A/AAAA as the first supported record are terminal
|
||||
// address answers (AAAA currently ignored). For PTR/SRV/TXT, subsequent
|
||||
// records are attached only when their label matches the service instance, or
|
||||
// the SRV target for address records.
|
||||
// Some devices send SRV/TXT as extra Answer RRs instead of Additional RRs, so
|
||||
// both sections are parsed as one chain.
|
||||
static void handle_mdns_response(struct mg_connection *c) {
|
||||
struct mg_dns_header *rh = (struct mg_dns_header *) c->recv.buf;
|
||||
struct mg_dns_rr rr;
|
||||
size_t n;
|
||||
// Parse first response, offset 12 is header size
|
||||
n = mg_dns_parse_rr(c->recv.buf, c->recv.len, 12, false, &rr);
|
||||
MG_VERBOSE(("mDNS response parsed, result=%d", (int) n));
|
||||
if (n > 0) {
|
||||
// RFC-6762 Appendix C, RFC2181 11: m(n + 1-63), max 255 + 0x0
|
||||
char name[256];
|
||||
uint8_t name_len;
|
||||
struct mg_dns_rr rr, rr_[5]; // A, PTR, SRV, TXT, AAAA, in that order
|
||||
struct mg_mdns_resp resp;
|
||||
size_t n, i, num, answers, roff_[5], roff = 12; // offset 12 is header size
|
||||
// RFC-6762 Appendix C, RFC-2181 11: m(n + 1-63), max 255 + 0x0
|
||||
char name[256], srvcproto[256], instance[256], host[256];
|
||||
uint16_t atype = 0;
|
||||
answers = mg_ntohs(rh->num_answers);
|
||||
num = answers + mg_ntohs(rh->num_other_prs);
|
||||
if (answers == 0) return;
|
||||
MG_VERBOSE(("mDNS response: %u answer, %u additional", (unsigned) answers,
|
||||
(unsigned) (num - answers)));
|
||||
if (num > MG_MAX_MDNS_RECORDS) {
|
||||
num = MG_MAX_MDNS_RECORDS;
|
||||
MG_DEBUG(("ignoring > %u records", MG_MAX_MDNS_RECORDS));
|
||||
}
|
||||
memset(roff_, 0, sizeof(roff_));
|
||||
memset(&rr_, 0, sizeof(rr_));
|
||||
memset(&resp, 0, sizeof(resp));
|
||||
if (rh->num_answers > mg_htons(1)) MG_DEBUG(("ignoring > 1 answers"));
|
||||
mg_dns_parse_name(c->recv.buf, c->recv.len, 12, name, sizeof(name));
|
||||
name_len = (uint8_t) strlen(name);
|
||||
MG_VERBOSE(("RR %u %u %s", (unsigned int) rr.atype,
|
||||
(unsigned int) rr.aclass, name));
|
||||
if (rr.alen == 4 && rr.atype == MG_DNS_RTYPE_A &&
|
||||
(rr.aclass & 0x7FFF) == 1) {
|
||||
instance[0] = host[0] = '\0';
|
||||
for (i = 0; i < num; i++) { // First Answer RR is primary; the rest must match it
|
||||
n = mg_dns_parse_rr(c->recv.buf, c->recv.len, roff, false, &rr);
|
||||
MG_VERBOSE(("mDNS record parsed, result=%u", n));
|
||||
if (n == 0) return;
|
||||
if (mg_dns_parse_name(c->recv.buf, c->recv.len, roff, name, sizeof(name)) == 0) return;
|
||||
MG_VERBOSE(("RR %u %u %s", (unsigned int) rr.atype, (unsigned int) rr.aclass, name));
|
||||
if (rr.alen == 4 && rr.atype == MG_DNS_RTYPE_A && (rr.aclass & 0x7FFF) == 1 &&
|
||||
(i == 0 || (host[0] != '\0' && strcmp(name, host) == 0) ||
|
||||
(host[0] == '\0' && instance[0] != '\0' && strcmp(name, instance) == 0))) {
|
||||
if (rr_[0].atype != 0) break;
|
||||
if (i == 0) atype = rr.atype;
|
||||
resp.name = mg_str(name);
|
||||
resp.addr.is_ip6 = false;
|
||||
memcpy(resp.addr.addr.ip, (char *) (rh + 1) + n - 4, 4);
|
||||
MG_DEBUG(("A response from %.*s = %M", name_len, name, mg_print_ip,
|
||||
&resp.addr));
|
||||
// } else if (rr.alen == 16 && rr.atype == MG_DNS_RTYPE_AAAA &&
|
||||
// (rr.aclass & 0x7FFF) == 1) {
|
||||
// resp.addr.is_ip6 = true;
|
||||
// memcpy(resp.addr.addr.ip, (char *)(rh + 1) + n - 16], 16);
|
||||
// MG_DEBUG(("AAAA response from %.*s = %M", name_len, name,
|
||||
// mg_print_ip, &resp.addr));
|
||||
memcpy(resp.addr.addr.ip, c->recv.buf + roff + n - 4, 4);
|
||||
MG_VERBOSE(("A record"));
|
||||
roff_[0] = roff, rr_[0] = rr;
|
||||
if (atype == MG_DNS_RTYPE_A) break;
|
||||
} else if (rr.alen == 16 && rr.atype == MG_DNS_RTYPE_AAAA && (rr.aclass & 0x7FFF) == 1 &&
|
||||
(i == 0 || (host[0] != '\0' && strcmp(name, host) == 0) ||
|
||||
(host[0] == '\0' && instance[0] != '\0' && strcmp(name, instance) == 0))) {
|
||||
# if 0
|
||||
if (rr_[4].atype != 0) break;
|
||||
resp.addr.is_ip6 = true;
|
||||
if (i == 0) atype = rr.atype;
|
||||
resp.name = mg_str(name);
|
||||
memcpy(resp.addr.addr.ip, c->recv.buf + roff + n - 16, 16);
|
||||
MG_VERBOSE(("AAAA record"));
|
||||
roff_[4] = roff, rr_[4] = rr;
|
||||
if (atype == MG_DNS_RTYPE_AAAA) break;
|
||||
#else
|
||||
MG_VERBOSE(("ignored AAAA record"));
|
||||
#endif
|
||||
} else if (rr.atype == MG_DNS_RTYPE_PTR && (rr.aclass & 0x7FFF) == 1 && i == 0) {
|
||||
if (rr_[1].atype != 0) break;
|
||||
atype = rr.atype;
|
||||
if (mg_dns_parse_name(c->recv.buf, c->recv.len, roff + rr.nlen + 10,
|
||||
instance, sizeof(instance)) == 0) return;
|
||||
MG_VERBOSE(("PTR record"));
|
||||
roff_[1] = roff, rr_[1] = rr;
|
||||
} else if (rr.atype == MG_DNS_RTYPE_SRV && rr.alen >= 6 && (rr.aclass & 0x7FFF) == 1 &&
|
||||
(i == 0 || (instance[0] != '\0' && strcmp(name, instance) == 0))) {
|
||||
if (rr_[2].atype != 0) break;
|
||||
if (i == 0) atype = rr.atype;
|
||||
resp.sd.port = MG_LOAD_BE16(c->recv.buf + roff + rr.nlen + 14);
|
||||
resp.addr.port = mg_htons(resp.sd.port);
|
||||
if (instance[0] == '\0') mg_snprintf(instance, sizeof(instance), "%s", name);
|
||||
if (mg_dns_parse_name(c->recv.buf, c->recv.len, roff + rr.nlen + 16,
|
||||
host, sizeof(host)) == 0) return;
|
||||
MG_VERBOSE(("SRV record"));
|
||||
roff_[2] = roff, rr_[2] = rr;
|
||||
} else if (rr.atype == MG_DNS_RTYPE_TXT && (rr.aclass & 0x7FFF) == 1 &&
|
||||
(i == 0 || (instance[0] != '\0' && strcmp(name, instance) == 0))) {
|
||||
if (rr_[3].atype != 0) break;
|
||||
if (i == 0) atype = rr.atype;
|
||||
if (instance[0] == '\0') mg_snprintf(instance, sizeof(instance), "%s", name);
|
||||
resp.sd.txt = mg_str_n((char *) c->recv.buf + roff + rr.nlen + 10, rr.alen);
|
||||
MG_VERBOSE(("TXT record"));
|
||||
roff_[3] = roff, rr_[3] = rr;
|
||||
}
|
||||
roff += n;
|
||||
if (i == 0 && atype == 0) return;
|
||||
}
|
||||
if (atype == MG_DNS_RTYPE_A) {
|
||||
resp.rr = &rr_[0];
|
||||
MG_DEBUG(("A response from %s = %M", name, mg_print_ip, &resp.addr));
|
||||
#if 0
|
||||
} else if (atype == MG_DNS_RTYPE_AAAA) {
|
||||
resp.rr = &rr_[4];
|
||||
MG_DEBUG(("AAAA response from %s = %M", name, mg_print_ip6, resp.addr.addr.ip));
|
||||
#endif
|
||||
} else if (atype == MG_DNS_RTYPE_PTR) {
|
||||
size_t len;
|
||||
resp.rr = &rr_[1];
|
||||
if (mg_dns_parse_name(c->recv.buf, c->recv.len, roff_[1], srvcproto,
|
||||
sizeof(srvcproto)) == 0) return;
|
||||
len = strlen(srvcproto);
|
||||
if (len <= 6) return;
|
||||
resp.sd.srvcproto = mg_str_n(srvcproto, len - 6); // remove .local
|
||||
if (host[0] != '\0') {
|
||||
resp.name = mg_str(host);
|
||||
} else if (rr_[0].atype == 0) {
|
||||
mg_snprintf(name, sizeof(name), "%s", instance);
|
||||
resp.name = mg_str_n(name, srvtoname(name));
|
||||
} else {
|
||||
if (mg_dns_parse_name(c->recv.buf, c->recv.len, roff_[0], name,
|
||||
sizeof(name)) == 0) return;
|
||||
resp.name = mg_str(name);
|
||||
}
|
||||
MG_DEBUG(("PTR response for %s from %s", srvcproto, name));
|
||||
} else if (atype == MG_DNS_RTYPE_SRV || atype == MG_DNS_RTYPE_TXT) {
|
||||
size_t len, name_len;
|
||||
resp.rr = atype == MG_DNS_RTYPE_SRV ? &rr_[2] : &rr_[3];
|
||||
if (mg_dns_parse_name(c->recv.buf, c->recv.len,
|
||||
atype == MG_DNS_RTYPE_SRV ? roff_[2] : roff_[3],
|
||||
srvcproto, sizeof(srvcproto)) == 0)
|
||||
return;
|
||||
len = strlen(srvcproto);
|
||||
name_len = srvtoname(srvcproto);
|
||||
if (name_len == 0 || len <= name_len + 7) return; // remove .local
|
||||
resp.sd.srvcproto = mg_str_n(srvcproto + name_len + 1, len - name_len - 7);
|
||||
resp.name = mg_str_n(srvcproto, name_len);
|
||||
MG_DEBUG(("%s response for %s from %s", atype == MG_DNS_RTYPE_SRV ? "SRV" : "TXT", srvcproto, name));
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
resp.name = mg_str_n(name, name_len);
|
||||
resp.rr = &rr;
|
||||
mg_call(c, MG_EV_MDNS_RESP, &resp);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_mdns_record(struct mg_connection *c) {
|
||||
struct mg_dns_header *h = (struct mg_dns_header *) c->recv.buf;
|
||||
@@ -684,8 +795,7 @@ struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn,
|
||||
static bool mdns_query(struct mg_connection *c, struct mg_str *name,
|
||||
unsigned int rtype) {
|
||||
mg_multicast_restore(c, (uint8_t *) &c->loc);
|
||||
(void) rtype;
|
||||
return mg_dns_send(c, name, 0, false);
|
||||
return dns_send(c, name, rtype, 0, 0); // RFC-6762 18.1 id = 0, 18.6 RD = 0
|
||||
}
|
||||
|
||||
bool mg_mdns_query(struct mg_connection *c, const char *name,
|
||||
|
||||
@@ -21,7 +21,7 @@ struct mg_dns_message {
|
||||
char name[256]; // Host name
|
||||
};
|
||||
|
||||
// DNS wire-format header (RFC 1035). All fields are in network byte order;
|
||||
// DNS wire-format header (RFC-1035). All fields are in network byte order;
|
||||
// use mg_ntohs() to read them.
|
||||
struct mg_dns_header {
|
||||
uint16_t txnid; // Transaction ID, matched between query and response
|
||||
@@ -60,11 +60,12 @@ struct mg_mdns_req {
|
||||
bool is_unicast; // True if the client requested a unicast (QU) response
|
||||
};
|
||||
|
||||
// ev_data for MG_EV_MDNS_RESP, carrying the resolved address from an mDNS response.
|
||||
// ev_data for MG_EV_MDNS_RESP, carrying address/data from a mDNS response
|
||||
struct mg_mdns_resp {
|
||||
struct mg_dns_rr *rr; // Resource record from the response
|
||||
struct mg_dns_rr *rr; // Resource record from the response (1st in chain)
|
||||
struct mg_str name; // Resolved hostname, without the .local suffix
|
||||
struct mg_addr addr; // Resolved IP address
|
||||
struct mg_dnssd_record sd; // Service Discovery data
|
||||
};
|
||||
|
||||
// Parses a DNS query or response from buf/len into dm. Returns true on success.
|
||||
@@ -81,6 +82,7 @@ size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
|
||||
// Creates an mDNS listener on UDP multicast 224.0.0.251:5353. Registers it as
|
||||
// the manager's mDNS resolver (mgr->mdns). Fires MG_EV_MDNS_REQ for incoming
|
||||
// queries and MG_EV_MDNS_RESP for incoming responses. Returns NULL on error.
|
||||
// fn_data is a NUL-terminated server name, or NULL to pass all requests to fn.
|
||||
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, mg_event_handler_t fn,
|
||||
void *fn_data);
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "sntp.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD
|
||||
|
||||
@@ -9,7 +11,9 @@
|
||||
|
||||
static size_t print_atcmd(void (*out)(char, void *), void *arg, va_list *ap) {
|
||||
struct mg_str s = va_arg(*ap, struct mg_str);
|
||||
for (size_t i = 0; i < s.len; i++) out(s.buf[i] < 0x20 ? '.' : s.buf[i], arg);
|
||||
size_t i;
|
||||
for (i = 0; i < s.len; i++)
|
||||
out((char) (s.buf[i] < 0x20 ? '.' : s.buf[i]), arg);
|
||||
return s.len;
|
||||
}
|
||||
|
||||
@@ -27,7 +31,7 @@ static bool mg_atcmd_handle(struct mg_tcpip_if *ifp) {
|
||||
if (dd->script_index % 2 == 0) { // send AT command
|
||||
const char *cmd = dd->script[dd->script_index];
|
||||
MG_DEBUG(("send AT[%d]: %M", dd->script_index, print_atcmd, mg_str(cmd)));
|
||||
while (*cmd) dd->tx(dd->usart, *cmd++);
|
||||
while (*cmd != '\0') dd->tx(dd->usart, (uint8_t) *cmd++);
|
||||
dd->script_index++;
|
||||
ifp->recv_queue.head = 0;
|
||||
} else { // check AT command response
|
||||
@@ -43,7 +47,7 @@ static bool mg_atcmd_handle(struct mg_tcpip_if *ifp) {
|
||||
return false; // FAIL
|
||||
}
|
||||
if ((c = dd->rx(dd->usart)) < 0) return false; // no data
|
||||
q->buf[q->head++] = c;
|
||||
q->buf[q->head++] = (char) c;
|
||||
if (mg_match(mg_str_n(q->buf, q->head), mg_str(expect), NULL)) {
|
||||
MG_DEBUG(("recv AT[%d]: %M", dd->script_index, print_atcmd,
|
||||
mg_str_n(q->buf, q->head)));
|
||||
@@ -84,7 +88,7 @@ static size_t mg_atcmd_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||
}
|
||||
}
|
||||
if (!dd->no_byte_stuff && !byte_unstuff(dd, &b)) continue;
|
||||
q->buf[q->head++] = b;
|
||||
q->buf[q->head++] = (char) b;
|
||||
}
|
||||
len = (q->head <= len) ? q->head : 0;
|
||||
memmove(buf, q->buf, len);
|
||||
@@ -130,7 +134,7 @@ struct mg_tcpip_driver mg_tcpip_driver_atcmd = {mg_atcmd_init, mg_atcmd_tx, mg_a
|
||||
static void byte_stuff(struct mg_tcpip_driver_atcmd_data *dd, uint8_t b) {
|
||||
if ((b < 0x20) || (b == MG_HDLC_ESC) || (b == MG_HDLC_FLAG)) {
|
||||
dd->tx(dd->usart, MG_HDLC_ESC);
|
||||
dd->tx(dd->usart, b ^ 0x20);
|
||||
dd->tx(dd->usart, (uint8_t) (b ^ 0x20));
|
||||
} else {
|
||||
dd->tx(dd->usart, b);
|
||||
}
|
||||
|
||||
@@ -12,3 +12,31 @@ struct mg_tcpip_driver_atcmd_data {
|
||||
bool link; // internal use: done with AT processing
|
||||
bool unstuffing; // internal use: unstuffing state
|
||||
};
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD
|
||||
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_atcmd;
|
||||
|
||||
// Modem callbacks and AT script, defined by the application.
|
||||
// See tutorials/arduino/sim800-mqtt for modem configuration.
|
||||
extern struct mg_tcpip_driver_atcmd_data mg_atcmd;
|
||||
|
||||
#define MG_TCPIP_DRIVER_ATCMD_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_atcmd; \
|
||||
mif_.driver_data = &mg_atcmd; \
|
||||
mif_.l2type = MG_TCPIP_L2_PPP; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: atcmd, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ATCMD_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+3
-1
@@ -1,9 +1,11 @@
|
||||
// https://arm-software.github.io/CMSIS_5/Driver/html/index.html
|
||||
|
||||
#include "net_builtin.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_CMSIS) && MG_ENABLE_DRIVER_CMSIS
|
||||
|
||||
#include "cmsis.h"
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "queue.h"
|
||||
|
||||
extern ARM_DRIVER_ETH_MAC Driver_ETH_MAC0;
|
||||
|
||||
@@ -5,4 +5,22 @@
|
||||
#include "Driver_ETH_MAC.h" // keep this include
|
||||
#include "Driver_ETH_PHY.h" // keep this include
|
||||
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_cmsis;
|
||||
|
||||
#define MG_TCPIP_DRIVER_CMSIS_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_cmsis; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: cmsis, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CMSIS_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+7
-2
@@ -1,5 +1,8 @@
|
||||
#include "cyw.h"
|
||||
#include "net_builtin.h"
|
||||
#include "net.h"
|
||||
#include "printf.h"
|
||||
#include "util.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#if MG_ENABLE_TCPIP && \
|
||||
((defined(MG_ENABLE_DRIVER_CYW) && MG_ENABLE_DRIVER_CYW) || \
|
||||
@@ -875,8 +878,10 @@ static void cyw_update_hash_table(void) {
|
||||
// TODO(): read database, rebuild hash table
|
||||
uint32_t val = 0;
|
||||
val = 1;
|
||||
uint8_t mac[sizeof(mcast_addr)]; // copy from const uint8_t* to uint8_t*
|
||||
memcpy(mac, mcast_addr, sizeof(mac));
|
||||
cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *) &val, sizeof(val),
|
||||
(uint8_t *) mcast_addr, sizeof(mcast_addr));
|
||||
mac, sizeof(mac));
|
||||
mg_delayms(50);
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -4,6 +4,8 @@
|
||||
((defined(MG_ENABLE_DRIVER_CYW) && MG_ENABLE_DRIVER_CYW) || \
|
||||
(defined(MG_ENABLE_DRIVER_CYW_SDIO) && MG_ENABLE_DRIVER_CYW_SDIO))
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
struct mg_tcpip_driver_cyw_firmware {
|
||||
const uint8_t *code_addr;
|
||||
size_t code_len;
|
||||
@@ -20,7 +22,9 @@ struct mg_tcpip_driver_cyw_data {
|
||||
bool hs; // use chip "high-speed" mode; otherwise SPI CPOL0 CPHA0 (DS 4.2.3 Table 6)
|
||||
};
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_cyw;
|
||||
|
||||
#define MG_TCPIP_DRIVER_CYW_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_cyw_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -32,8 +36,12 @@ struct mg_tcpip_driver_cyw_data {
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mif_.recv_queue.size = 8192; \
|
||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: cyw, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CYW_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,192 @@
|
||||
#include "l2_mesh.h"
|
||||
#include "log.h"
|
||||
#include "util.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>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// ESP-IDF only: it accepts a frame and may refuse it later, because of Wi-Fi
|
||||
// coexistence, a busy channel or an abort. Such a frame is sent again after
|
||||
// this pause, until MG_MESH_TX_TIMEOUT_US. Without it, the mesh would count
|
||||
// the refused frame as sent
|
||||
#define MGI_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
|
||||
|
||||
// ESP-IDF's esp_ieee802154 driver calls the three functions below by name,
|
||||
// from interrupt context. They are global only for that reason, and IRAM_ATTR
|
||||
// keeps them in IRAM
|
||||
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() + MGI_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_MESH_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,38 @@
|
||||
// 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.
|
||||
// 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
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && \
|
||||
(defined(MG_ENABLE_DRIVER_IMXRT10) && MG_ENABLE_DRIVER_IMXRT10) || \
|
||||
|
||||
+8
-2
@@ -30,7 +30,9 @@ struct mg_tcpip_driver_imxrt_data {
|
||||
#define MG_DRIVER_MDC_CR 24
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_imxrt;
|
||||
|
||||
#define MG_TCPIP_DRIVER_IMXRT_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_imxrt_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -42,8 +44,12 @@ struct mg_tcpip_driver_imxrt_data {
|
||||
mif_.driver = &mg_tcpip_driver_imxrt; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: imxrt, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_IMXRT_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,223 @@
|
||||
#include "l2_mesh.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
#include "net_builtin.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
|
||||
MG_ENABLE_DRIVER_MCXW71
|
||||
|
||||
// ZLL, 802.15.4 link layer. See MCXW71 reference manual and NXP
|
||||
// ieee_802_15_4 PHY sources: PhyPacketProcessor.c, PhyPlmeData.c, PhyISR.c
|
||||
struct mg_zll {
|
||||
volatile uint32_t IRQSTS, PHY_CTRL, EVENT_TMR, TIMESTAMP, T1CMP, T2CMP,
|
||||
T2PRIMECMP, T3CMP, T4CMP, PA_PWR, CHANNEL_NUM0, LQI_AND_RSSI,
|
||||
MACSHORTADDRS0, MACLONGADDRS0_LSB, MACLONGADDRS0_MSB, RX_FRAME_FILTER,
|
||||
CCA_LQI_CTRL, CCA2_CTRL, RESERVED_0, DSM_CTRL, RESERVED_1, MACSHORTADDRS1,
|
||||
MACLONGADDRS1_LSB, MACLONGADDRS1_MSB, DUAL_PAN_CTRL, CHANNEL_NUM1,
|
||||
SAM_CTRL, SAM_TABLE, SAM_MATCH, SAM_FREE_IDX, SEQ_CTRL_STS, ACKDELAY,
|
||||
FILTERFAIL_CODE, RX_WTR_MARK, RESERVED_2, SLOT_PRELOAD, SEQ_STATE,
|
||||
TMR_PRESCALE, LENIENCY_LSB, LENIENCY_MSB, PART_ID, COEX_CTRL,
|
||||
COEX_PRIORITY, ENHACK_CTRL0;
|
||||
};
|
||||
|
||||
// TrustZone: code built for the secure state (-mcmse) uses the secure alias
|
||||
// of the radio registers, 0x5xxxxxxx. Otherwise, the non-secure alias
|
||||
#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE & 2)
|
||||
#define MGI_ZLL ((struct mg_zll *) 0x58A01000)
|
||||
#define MGI_PKT_RAM ((volatile uint8_t *) 0x58A08000)
|
||||
#else
|
||||
#define MGI_ZLL ((struct mg_zll *) 0x48A01000)
|
||||
#define MGI_PKT_RAM ((volatile uint8_t *) 0x48A08000)
|
||||
#endif
|
||||
|
||||
#define MGI_TX_PB MGI_PKT_RAM // Length byte (incl. FCS), then frame
|
||||
#define MGI_RX_PB (MGI_PKT_RAM + 0x80) // Received frame, no length byte
|
||||
|
||||
#define MGI_IRQSTS_SEQIRQ MG_BIT(0)
|
||||
#define MGI_IRQSTS_RXIRQ MG_BIT(2)
|
||||
#define MGI_IRQSTS_FILTERFAIL MG_BIT(5) // Frame not for us
|
||||
#define MGI_IRQSTS_CRCVALID MG_BIT(15)
|
||||
#define MGI_IRQSTS_RX_LEN(x) (((x) >> 24) & 0x7f) // Frame length incl. FCS
|
||||
#define MGI_PHY_CTRL_XCVSEQ 7U
|
||||
#define MGI_PHY_CTRL_ALL_IRQ_MASKS 0x8007ff00U // Bits 8..18, 31: mask all IRQs
|
||||
#define MGI_PHY_CTRL_SEQMSK MG_BIT(8) // Mask the sequence end IRQ
|
||||
#define MGI_PHY_CTRL_TRCV_MSK MG_BIT(31) // Mask all transceiver IRQs
|
||||
#define MGI_IRQN 53 // RF_802_15_4_IRQn
|
||||
#define MGI_NVIC_ISER ((volatile uint32_t *) 0xE000E100) // Enable IRQ
|
||||
#define MGI_NVIC_ICER ((volatile uint32_t *) 0xE000E180) // Disable IRQ
|
||||
#define MGI_SEQ_STATE_MASK 0x1fU
|
||||
#define MGI_SEQ_STATE_RX_WU 0x10U // RX warm up
|
||||
#define MGI_SEQ_CTRL_NO_RX_RECYCLE MG_BIT(5)
|
||||
#define MGI_FRAME_FILTER 0x70bU // Beacon, data, command; versions 0..2
|
||||
|
||||
// PHY_CTRL[XCVSEQ]
|
||||
enum { MGI_SEQ_IDLE = 0, MGI_SEQ_RX = 1, MGI_SEQ_TX = 2 };
|
||||
|
||||
static struct mg_tcpip_if *s_mcxw71_ifp;
|
||||
static struct mg_queue s_mcxw71_rx;
|
||||
static char s_mcxw71_queue[2048];
|
||||
static volatile bool s_mcxw71_txing, s_mcxw71_done;
|
||||
static uint64_t s_mcxw71_sendstart;
|
||||
|
||||
// Stop the current sequence, wait for the sequencer to go idle. See PhyAbort()
|
||||
static void mg_mcxw71_abort(void) {
|
||||
if (MGI_ZLL->PHY_CTRL & MGI_PHY_CTRL_XCVSEQ) {
|
||||
while ((MGI_ZLL->SEQ_STATE & MGI_SEQ_STATE_MASK) == MGI_SEQ_STATE_RX_WU)
|
||||
(void) 0;
|
||||
MGI_ZLL->PHY_CTRL &= ~MGI_PHY_CTRL_XCVSEQ;
|
||||
while (MGI_ZLL->SEQ_STATE & MGI_SEQ_STATE_MASK) (void) 0;
|
||||
}
|
||||
MGI_ZLL->IRQSTS = MGI_ZLL->IRQSTS;
|
||||
}
|
||||
|
||||
// Start receiving, with the sequence end IRQ on, see RF_802_15_4_IRQHandler()
|
||||
static void mg_mcxw71_listen(void) {
|
||||
MGI_ZLL->IRQSTS = MGI_ZLL->IRQSTS;
|
||||
MGI_ZLL->PHY_CTRL |= MGI_SEQ_RX;
|
||||
MGI_ZLL->PHY_CTRL &= ~(MGI_PHY_CTRL_SEQMSK | MGI_PHY_CTRL_TRCV_MSK);
|
||||
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||
}
|
||||
|
||||
// Keep the IRQ handler out while we change the radio. The NVIC disable is
|
||||
// atomic, so PHY_CTRL updates here and there do not race
|
||||
static void mg_mcxw71_lock(void) {
|
||||
MGI_NVIC_ICER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||
__asm volatile("dsb; isb" ::: "memory");
|
||||
}
|
||||
|
||||
// Start the TX sequence for the loaded frame. Its end: see the IRQ handler
|
||||
static void mg_mcxw71_go(void) {
|
||||
MGI_ZLL->PHY_CTRL |= MGI_PHY_CTRL_TRCV_MSK | MGI_PHY_CTRL_SEQMSK;
|
||||
mg_mcxw71_abort();
|
||||
MGI_ZLL->IRQSTS = MGI_ZLL->IRQSTS;
|
||||
MGI_ZLL->PHY_CTRL |= MGI_SEQ_TX;
|
||||
MGI_ZLL->PHY_CTRL &= ~(MGI_PHY_CTRL_SEQMSK | MGI_PHY_CTRL_TRCV_MSK);
|
||||
}
|
||||
|
||||
void RF_802_15_4_IRQHandler(void);
|
||||
void RF_802_15_4_IRQHandler(void) {
|
||||
uint32_t irqsts = MGI_ZLL->IRQSTS;
|
||||
if ((irqsts & MGI_IRQSTS_SEQIRQ) != 0) {
|
||||
mg_mcxw71_abort();
|
||||
if (s_mcxw71_txing) {
|
||||
s_mcxw71_txing = false;
|
||||
s_mcxw71_done = true;
|
||||
} else if ((irqsts & (MGI_IRQSTS_RXIRQ | MGI_IRQSTS_FILTERFAIL |
|
||||
MGI_IRQSTS_CRCVALID)) ==
|
||||
(MGI_IRQSTS_RXIRQ | MGI_IRQSTS_CRCVALID)) {
|
||||
uint8_t frame[125];
|
||||
size_t i, n = MGI_IRQSTS_RX_LEN(irqsts);
|
||||
n = n >= 2 ? n - 2 : 0;
|
||||
if (n > 0 && n <= sizeof(frame)) {
|
||||
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
|
||||
mg_mesh_rx_queue(&s_mcxw71_rx, s_mcxw71_ifp, frame, n);
|
||||
}
|
||||
}
|
||||
mg_mcxw71_listen();
|
||||
}
|
||||
}
|
||||
|
||||
static size_t mgi_mcxw71_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||
size_t len) {
|
||||
size_t i;
|
||||
(void) ifp;
|
||||
if (len > 125 || s_mcxw71_txing) return 0;
|
||||
mg_mcxw71_lock();
|
||||
MGI_TX_PB[0] = (uint8_t) (len + 2); // FCS is added by hardware
|
||||
for (i = 0; i < len; i++)
|
||||
MGI_TX_PB[i + 1] = ((const uint8_t *) buf)[i]; // 8-bit access only
|
||||
s_mcxw71_done = false;
|
||||
s_mcxw71_txing = true;
|
||||
s_mcxw71_sendstart = mg_millis();
|
||||
mg_mcxw71_go();
|
||||
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
s_mcxw71_ifp = ifp;
|
||||
MGI_ZLL->SEQ_CTRL_STS |= MGI_SEQ_CTRL_NO_RX_RECYCLE;
|
||||
MGI_ZLL->PHY_CTRL = MGI_PHY_CTRL_ALL_IRQ_MASKS;
|
||||
MGI_ZLL->IRQSTS = MGI_ZLL->IRQSTS;
|
||||
MGI_ZLL->SAM_CTRL = 0;
|
||||
MGI_ZLL->RX_FRAME_FILTER = MGI_FRAME_FILTER;
|
||||
MGI_ZLL->CHANNEL_NUM0 = MG_802154_CHANNEL;
|
||||
MGI_ZLL->MACSHORTADDRS0 =
|
||||
((uint32_t) node->radio_address << 16) | MG_802154_PAN_ID;
|
||||
MGI_ZLL->PA_PWR = 0x3f;
|
||||
mg_mcxw71_listen();
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t mgi_mcxw71_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
||||
return data->mesh->strategy->tx(data->mesh, buf, len);
|
||||
}
|
||||
|
||||
static size_t mgi_mcxw71_rx(void *buf, size_t cap, struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mcxw71_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_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
uint64_t now = mg_micros();
|
||||
(void) s1;
|
||||
if (s_mcxw71_txing &&
|
||||
mg_millis() > s_mcxw71_sendstart + MG_MESH_TX_TIMEOUT_US / 1000) {
|
||||
mg_mcxw71_lock();
|
||||
if (s_mcxw71_txing) {
|
||||
mg_mcxw71_abort();
|
||||
s_mcxw71_txing = false;
|
||||
s_mcxw71_done = true;
|
||||
mg_mcxw71_listen();
|
||||
} else {
|
||||
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||
}
|
||||
}
|
||||
if (s_mcxw71_done) {
|
||||
s_mcxw71_done = false;
|
||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||
}
|
||||
node->now = now;
|
||||
mg_mesh_rx_drain(node, &s_mcxw71_rx);
|
||||
node->strategy->tick(node, now);
|
||||
return node->joined;
|
||||
}
|
||||
|
||||
static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
||||
const struct mg_mesh *strategy = data->strategy;
|
||||
uint16_t address;
|
||||
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_mcxw71_radio_tx;
|
||||
mg_queue_init(&s_mcxw71_rx, s_mcxw71_queue, sizeof(s_mcxw71_queue));
|
||||
if (mgi_mcxw71_radio_init(ifp)) return true;
|
||||
strategy->free(data->mesh);
|
||||
mg_free(data->mesh);
|
||||
data->mesh = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mg_tcpip_driver mg_tcpip_driver_mcxw71 = {
|
||||
mgi_mcxw71_init, mgi_mcxw71_tx, mgi_mcxw71_rx, mgi_mcxw71_poll};
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#pragma once
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
|
||||
MG_ENABLE_DRIVER_MCXW71
|
||||
|
||||
struct mg_tcpip_driver_mcxw71_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_mcxw71;
|
||||
|
||||
#define MG_TCPIP_DRIVER_MCXW71_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_mcxw71_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_.driver = &mg_tcpip_driver_mcxw71; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_MCXW71_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,172 @@
|
||||
#include "net_builtin.h"
|
||||
#include "l2_mesh.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MG24) && MG_ENABLE_DRIVER_MG24
|
||||
|
||||
#include <rail.h>
|
||||
#include <rail_ieee802154.h>
|
||||
|
||||
// RAIL delivers raw frames and transmit completion from interrupt context.
|
||||
|
||||
static struct mg_tcpip_if *s_mg24_ifp;
|
||||
static RAIL_Handle_t s_mg24_rail;
|
||||
static struct mg_queue s_mg24_rx;
|
||||
static char s_mg24_rxbuf[2048];
|
||||
static volatile bool s_mg24_txing, s_mg24_tx_done;
|
||||
static RAIL_Time_t s_mg24_txstart; // Send start, us
|
||||
static uint8_t s_mg24_txfifo[256] __attribute__((aligned(4))); // Power of 2
|
||||
|
||||
// Copy a received PSDU, stripping the PHR and any stored FCS.
|
||||
static void mg_mg24_rx_frame(RAIL_Handle_t h) {
|
||||
RAIL_RxPacketInfo_t info;
|
||||
uint8_t f[128];
|
||||
size_t n;
|
||||
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
|
||||
RAIL_RX_PACKET_HANDLE_INVALID ||
|
||||
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
|
||||
info.packetBytes <= 1 || info.packetBytes > sizeof(f)) {
|
||||
return;
|
||||
}
|
||||
RAIL_CopyRxPacket(f, &info);
|
||||
n = info.packetBytes - 1U; // Strip PHR
|
||||
if (n == f[0]) n -= 2; // FCS stored, strip it
|
||||
mg_mesh_rx_queue(&s_mg24_rx, s_mg24_ifp, f + 1, n);
|
||||
}
|
||||
|
||||
// RAIL event callback, runs in interrupt context
|
||||
static void mg_mg24_events(RAIL_Handle_t h, RAIL_Events_t events) {
|
||||
if (events & RAIL_EVENT_CAL_NEEDED) {
|
||||
RAIL_Calibrate(h, NULL, RAIL_CAL_ALL_PENDING);
|
||||
}
|
||||
if (events & RAIL_EVENT_RX_PACKET_RECEIVED) mg_mg24_rx_frame(h);
|
||||
if (events & RAIL_EVENTS_TX_COMPLETION) s_mg24_tx_done = true;
|
||||
}
|
||||
|
||||
// RAIL adds the FCS.
|
||||
static size_t mgi_mg24_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||
size_t len) {
|
||||
uint8_t phr = (uint8_t) (len + 2); // FCS is added by hardware
|
||||
(void) ifp;
|
||||
if (len > 125 || s_mg24_txing) return 0;
|
||||
RAIL_WriteTxFifo(s_mg24_rail, &phr, 1, true); // Reset FIFO, write PHR
|
||||
RAIL_WriteTxFifo(s_mg24_rail, (const uint8_t *) buf, (uint16_t) len, false);
|
||||
s_mg24_tx_done = false;
|
||||
s_mg24_txing = true, s_mg24_txstart = RAIL_GetTime();
|
||||
if (RAIL_StartTx(s_mg24_rail, MG_802154_CHANNEL, RAIL_TX_OPTIONS_DEFAULT,
|
||||
NULL) != RAIL_STATUS_NO_ERROR) {
|
||||
s_mg24_txing = false;
|
||||
return 0;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool mgi_mg24_radio_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
RAIL_Config_t cfg = {0};
|
||||
RAIL_IEEE802154_Config_t cfg154 = {0};
|
||||
RAIL_TxPowerConfig_t pwr;
|
||||
RAIL_StateTransitions_t to_rx = {RAIL_RF_STATE_RX, RAIL_RF_STATE_RX};
|
||||
RAIL_Events_t events = RAIL_EVENT_CAL_NEEDED | RAIL_EVENT_RX_PACKET_RECEIVED |
|
||||
RAIL_EVENTS_TX_COMPLETION;
|
||||
cfg.eventsCallback = mg_mg24_events;
|
||||
if ((s_mg24_rail = RAIL_Init(&cfg, NULL)) == NULL) {
|
||||
MG_ERROR(("RAIL_Init failed"));
|
||||
return false;
|
||||
}
|
||||
cfg154.ackConfig.enable = false;
|
||||
cfg154.ackConfig.ackTimeout = 864;
|
||||
cfg154.ackConfig.rxTransitions = to_rx;
|
||||
cfg154.ackConfig.txTransitions = to_rx;
|
||||
cfg154.timings.idleToRx = 100, cfg154.timings.idleToTx = 100;
|
||||
cfg154.timings.rxToTx = 192, cfg154.timings.txToRx = 182;
|
||||
cfg154.framesMask = RAIL_IEEE802154_ACCEPT_STANDARD_FRAMES;
|
||||
cfg154.promiscuousMode = false; // Filter by PAN ID and our short address
|
||||
pwr.mode = RAIL_TX_POWER_MODE_2P4GIG_HP, pwr.voltage = 3300;
|
||||
pwr.rampTime = 10;
|
||||
if (RAIL_ConfigCal(s_mg24_rail, RAIL_CAL_ALL) != RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_ConfigEvents(s_mg24_rail, RAIL_EVENTS_ALL, events) !=
|
||||
RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_IEEE802154_Config2p4GHzRadio(s_mg24_rail) != RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_IEEE802154_Init(s_mg24_rail, &cfg154) != RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_IEEE802154_SetPanId(s_mg24_rail, MG_802154_PAN_ID, 0) !=
|
||||
RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_IEEE802154_SetShortAddress(s_mg24_rail, node->radio_address, 0) !=
|
||||
RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_SetTxTransitions(s_mg24_rail, &to_rx) != RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_SetRxTransitions(s_mg24_rail, &to_rx) != RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_ConfigTxPower(s_mg24_rail, &pwr) != RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_SetTxPower(s_mg24_rail, RAIL_TX_POWER_LEVEL_2P4_HP_MAX) !=
|
||||
RAIL_STATUS_NO_ERROR ||
|
||||
RAIL_SetTxFifo(s_mg24_rail, s_mg24_txfifo, 0, sizeof(s_mg24_txfifo)) !=
|
||||
sizeof(s_mg24_txfifo)) {
|
||||
MG_ERROR(("RAIL setup failed"));
|
||||
return false;
|
||||
}
|
||||
s_mg24_ifp = ifp;
|
||||
return RAIL_StartRx(s_mg24_rail, MG_802154_CHANNEL, NULL) ==
|
||||
RAIL_STATUS_NO_ERROR;
|
||||
}
|
||||
|
||||
static size_t mgi_mg24_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
||||
return data->mesh->strategy->tx(data->mesh, buf, len);
|
||||
}
|
||||
|
||||
static size_t mgi_mg24_rx(void *buf, size_t cap, struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mg24_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_mg24_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
uint64_t now = mg_micros();
|
||||
(void) s1;
|
||||
node->now = now;
|
||||
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
|
||||
MG_MESH_TX_TIMEOUT_US)) {
|
||||
if (!s_mg24_tx_done) {
|
||||
RAIL_Idle(s_mg24_rail, RAIL_IDLE_ABORT, true);
|
||||
RAIL_StartRx(s_mg24_rail, MG_802154_CHANNEL, NULL);
|
||||
}
|
||||
s_mg24_txing = false;
|
||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||
}
|
||||
mg_mesh_rx_drain(node, &s_mg24_rx);
|
||||
node->strategy->tick(node, now);
|
||||
return node->joined;
|
||||
}
|
||||
static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_mg24_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_mg24_radio_tx;
|
||||
mg_queue_init(&s_mg24_rx, s_mg24_rxbuf, sizeof(s_mg24_rxbuf));
|
||||
if (mgi_mg24_radio_init(ifp)) return true;
|
||||
data->mesh->strategy->free(data->mesh);
|
||||
mg_free(data->mesh);
|
||||
data->mesh = NULL;
|
||||
return false;
|
||||
}
|
||||
struct mg_tcpip_driver mg_tcpip_driver_mg24 = {mgi_mg24_init, mgi_mg24_tx,
|
||||
mgi_mg24_rx, mgi_mg24_poll};
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#pragma once
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MG24) && MG_ENABLE_DRIVER_MG24
|
||||
|
||||
struct mg_tcpip_driver_mg24_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_mg24;
|
||||
|
||||
#define MG_TCPIP_DRIVER_MG24_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_mg24_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_.driver = &mg_tcpip_driver_mg24; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_MG24_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,471 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP && \
|
||||
defined(MG_ENABLE_DRIVER_NETC) && MG_ENABLE_DRIVER_NETC
|
||||
|
||||
// IMXRT1180 RM - Chapter 53 - Ethernet Controller (NETC)
|
||||
|
||||
#define ETH_DESC_CNT 8 // Descriptor count must be multiple of 8 (53.4.6.11.97)
|
||||
#define ETH_PKT_SIZE 1536
|
||||
|
||||
struct netc_pci_function { // 53.4.6.1.1
|
||||
volatile uint32_t DID_VID;
|
||||
volatile uint16_t CMD, STAT;
|
||||
volatile uint8_t reserved0[0x40];
|
||||
volatile uint16_t DEV_CTL, DEV_STAT;
|
||||
volatile uint8_t reserved1[0x34];
|
||||
volatile uint16_t MSIX_CAP_LIST, MSIX_MSG_CTL;
|
||||
};
|
||||
|
||||
struct netc_emdio { // 53.4.6.12
|
||||
volatile uint32_t EMDIO_CFG, EMDIO_CTL, EMDIO_DATA, EMDIO_ADDR, EMDIO_STAT;
|
||||
};
|
||||
|
||||
struct netc_enetc { // 53.4.6.6
|
||||
volatile uint32_t ECAPR0, ECAPR1, ECAPR2, reserved0, PMR;
|
||||
volatile uint8_t reserved1[0x1ec];
|
||||
volatile uint32_t PSIPMMR, PSIPVMR;
|
||||
volatile uint8_t reserved2[0x5f8];
|
||||
volatile uint32_t SMCAPR;
|
||||
volatile uint8_t reserved3[0x17fc];
|
||||
struct {
|
||||
volatile uint32_t PSIPMAR0, PSIPMAR1, PSIVLANR, reserved0, PSICFGR0,
|
||||
PSICFGR1, PSICFGR2;
|
||||
volatile uint8_t reserved1[0x3c];
|
||||
volatile uint32_t PSIMMHFR0, PSIMMHFR1;
|
||||
volatile uint8_t reserved2[0x20];
|
||||
} PSI[1];
|
||||
};
|
||||
|
||||
struct netc_port { // 53.4.6.8
|
||||
volatile uint32_t PCAPR, PMCAPR, PIOCAPR, reserved0, PCR;
|
||||
volatile uint8_t reserved1[0x0c];
|
||||
volatile uint32_t PMAR0, PMAR1;
|
||||
volatile uint8_t reserved2[0xd8];
|
||||
volatile uint32_t POR, PSR;
|
||||
volatile uint8_t reserved3[0x30];
|
||||
volatile uint32_t PDGSR;
|
||||
volatile uint8_t reserved4[0xc4];
|
||||
struct {
|
||||
volatile uint32_t PTGSTCSR, reserved0, PTCTMSDUR, reserved1, PTCCBSR0,
|
||||
PTCCBSR1;
|
||||
volatile uint8_t reserved2[8];
|
||||
} TCT[8];
|
||||
volatile uint8_t reserved5[0x220];
|
||||
volatile uint32_t BPSTGSR;
|
||||
};
|
||||
|
||||
struct netc_port_mac { // 53.4.6.9
|
||||
volatile uint8_t reserved0[8];
|
||||
volatile uint32_t COMMAND_CONFIG, MAC_ADDR_0, MAC_ADDR_1, MAXFRM, MINFRM;
|
||||
volatile uint8_t reserved1[0x24];
|
||||
volatile uint32_t IEVENT;
|
||||
volatile uint8_t reserved2[0x1dc];
|
||||
volatile uint64_t TFRMN;
|
||||
volatile uint8_t reserved3[0xd8];
|
||||
volatile uint32_t IF_MODE;
|
||||
volatile uint8_t reserved4[0xfc];
|
||||
};
|
||||
|
||||
struct netc_eth_link {
|
||||
struct netc_port_mac PM[2];
|
||||
};
|
||||
|
||||
struct netc_pseudo_link {
|
||||
volatile uint32_t PPMSR;
|
||||
volatile uint8_t reserved0[0x0c];
|
||||
volatile uint32_t PPMCR;
|
||||
};
|
||||
|
||||
struct netc_switch { // 53.4.6.5
|
||||
volatile uint8_t reserved0[0x400];
|
||||
volatile uint32_t MPCR;
|
||||
volatile uint8_t reserved1[0x1c0c];
|
||||
volatile uint32_t VFHTDECR0, VFHTDECR1, VFHTDECR2;
|
||||
};
|
||||
|
||||
struct netc_bdr {
|
||||
volatile uint32_t TBMR, TBSR;
|
||||
volatile uint8_t reserved0[8];
|
||||
volatile uint32_t TBBAR0, TBBAR1, TBPIR, TBCIR, TBLENR;
|
||||
volatile uint8_t reserved1[0x7c];
|
||||
volatile uint32_t TBIER, TBIDR, TBICR0, TBICR1;
|
||||
volatile uint8_t reserved2[0x50];
|
||||
volatile uint32_t RBMR, RBSR, RBBSR, RBCIR, RBBAR0, RBBAR1, RBPIR,
|
||||
reserved3, RBLENR;
|
||||
volatile uint8_t reserved4[0x5c];
|
||||
volatile uint32_t RBDCR;
|
||||
volatile uint8_t reserved5[0x1c];
|
||||
volatile uint32_t RBIER, RBIDR, RBICR0, RBICR1;
|
||||
volatile uint8_t reserved6[0x50];
|
||||
};
|
||||
|
||||
struct netc_si { // 53.4.6.11
|
||||
volatile uint32_t SIMR, SISR;
|
||||
volatile uint8_t reserved0[0x30];
|
||||
volatile uint32_t SIRBGCR;
|
||||
volatile uint8_t reserved1[0x2ec];
|
||||
volatile uint32_t SITFRM0;
|
||||
volatile uint8_t reserved2[0x6fc];
|
||||
volatile uint32_t SIRXIDR0;
|
||||
volatile uint8_t reserved3[0x154];
|
||||
volatile uint32_t SIMSIRRVR[1];
|
||||
volatile uint8_t reserved4[0x747c];
|
||||
struct netc_bdr BDR[1];
|
||||
};
|
||||
|
||||
struct netc_msix_entry { // 53.4.2.11
|
||||
volatile uint32_t MSG_ADDR, MSG_UPPER_ADDR, MSG_DATA, VECTOR_CTRL;
|
||||
};
|
||||
|
||||
struct netc_msgintr { // 54.4
|
||||
struct {
|
||||
volatile uint32_t MSIIR, MSIR;
|
||||
} MSI[3];
|
||||
};
|
||||
|
||||
#define NETC_EMDIO_PCI \
|
||||
((struct netc_pci_function *) (uintptr_t) 0x60001000U)
|
||||
#define NETC_SWITCH_PCI \
|
||||
((struct netc_pci_function *) (uintptr_t) 0x60002000U)
|
||||
#define NETC_ENETC1_PCI \
|
||||
((struct netc_pci_function *) (uintptr_t) 0x60004000U)
|
||||
#define NETC_EMDIO ((struct netc_emdio *) (uintptr_t) 0x60ba1c00U)
|
||||
|
||||
#define NETC_ENETC1 ((struct netc_enetc *) (uintptr_t) 0x60b50000U)
|
||||
#define NETC_ENETC1_PORT ((struct netc_port *) (uintptr_t) 0x60b54000U)
|
||||
#define NETC_ENETC1_PSEUDO_LINK \
|
||||
((struct netc_pseudo_link *) (uintptr_t) 0x60b55000U)
|
||||
#define NETC_ENETC1_SI0 ((struct netc_si *) (uintptr_t) 0x60b40000U)
|
||||
#define NETC_ENETC1_MSIX \
|
||||
((struct netc_msix_entry *) (uintptr_t) 0x60c00000U)
|
||||
|
||||
#define NETC_SWITCH ((struct netc_switch *) (uintptr_t) 0x60a00000U)
|
||||
#define NETC_SW0_PORT(link) \
|
||||
((struct netc_port *) (uintptr_t) (0x60a04000U + \
|
||||
(uint32_t) (link) * 0x4000U))
|
||||
#define NETC_SW0_ETH(link) \
|
||||
((struct netc_eth_link *) (uintptr_t) (0x60a05000U + \
|
||||
(uint32_t) (link) * 0x4000U))
|
||||
#define NETC_SW0_PORT4 ((struct netc_port *) (uintptr_t) 0x60a14000U)
|
||||
#define NETC_MSGINTR1 \
|
||||
((struct netc_msgintr *) (uintptr_t) 0x428a0000U)
|
||||
|
||||
enum {
|
||||
NETC_RX_MSIX_ENTRY = 0,
|
||||
NETC_RX_MSG_INDEX = 2,
|
||||
};
|
||||
|
||||
struct netc_tx_bd {
|
||||
uint64_t buffer_address;
|
||||
uint16_t buffer_length, frame_length;
|
||||
uint32_t control;
|
||||
};
|
||||
|
||||
struct netc_rx_descriptor {
|
||||
uint64_t buffer_address, reserved;
|
||||
};
|
||||
|
||||
struct netc_rx_writeback {
|
||||
uint16_t internet_checksum, parser_summary;
|
||||
uint32_t rss_hash;
|
||||
uint16_t buffer_length, vlan;
|
||||
uint32_t status;
|
||||
};
|
||||
|
||||
union netc_rx_bd {
|
||||
struct netc_rx_descriptor descriptor;
|
||||
struct netc_rx_writeback writeback;
|
||||
};
|
||||
|
||||
static volatile struct netc_tx_bd s_txdesc[ETH_DESC_CNT] MG_ETH_RAM
|
||||
__attribute__((aligned(128)));
|
||||
static volatile union netc_rx_bd s_rxdesc[ETH_DESC_CNT] MG_ETH_RAM
|
||||
__attribute__((aligned(128)));
|
||||
static uint8_t s_txbuf[ETH_DESC_CNT][ETH_PKT_SIZE] MG_ETH_RAM
|
||||
__attribute__((aligned(64)));
|
||||
static uint8_t s_rxbuf[ETH_DESC_CNT][ETH_PKT_SIZE] MG_ETH_RAM
|
||||
__attribute__((aligned(64)));
|
||||
|
||||
static uint16_t s_tx_prod;
|
||||
static uint16_t s_tx_clean;
|
||||
static uint16_t s_rx_cons;
|
||||
static struct mg_tcpip_if *s_ifp;
|
||||
|
||||
static uint16_t enet_read_phy(uint8_t addr, uint8_t reg) {
|
||||
NETC_EMDIO->EMDIO_CFG &= ~MG_BIT(6); // ENC45: use Clause 22
|
||||
NETC_EMDIO->EMDIO_CTL = MG_BIT(15) | ((uint32_t) (addr & 0x1fU) << 5) |
|
||||
(reg & 0x1fU); // READ, PORT_ADDR, DEV_ADDR
|
||||
while (NETC_EMDIO->EMDIO_CFG & MG_BIT(31)) (void) 0; // BSY1
|
||||
return (uint16_t) (NETC_EMDIO->EMDIO_DATA & 0xffffU);
|
||||
}
|
||||
|
||||
static void enet_write_phy(uint8_t addr, uint8_t reg, uint16_t value) {
|
||||
NETC_EMDIO->EMDIO_CFG &= ~MG_BIT(6); // ENC45: use Clause 22
|
||||
NETC_EMDIO->EMDIO_CTL = ((uint32_t) (addr & 0x1fU) << 5) |
|
||||
(reg & 0x1fU); // PORT_ADDR, DEV_ADDR
|
||||
NETC_EMDIO->EMDIO_DATA = value;
|
||||
while (NETC_EMDIO->EMDIO_CFG & MG_BIT(31)) (void) 0; // BSY1
|
||||
}
|
||||
|
||||
static bool mg_tcpip_driver_netc_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_netc_data *d =
|
||||
(struct mg_tcpip_driver_netc_data *) ifp->driver_data;
|
||||
struct mg_phy phy = {enet_read_phy, enet_write_phy};
|
||||
struct netc_port *switch_port;
|
||||
struct netc_eth_link *eth_link;
|
||||
uint64_t tx_base = (uint64_t) (uintptr_t) s_txdesc;
|
||||
uint64_t rx_base = (uint64_t) (uintptr_t) s_rxdesc;
|
||||
uint32_t mac0 = (uint32_t) ifp->mac[0] |
|
||||
((uint32_t) ifp->mac[1] << 8) |
|
||||
((uint32_t) ifp->mac[2] << 16) |
|
||||
((uint32_t) ifp->mac[3] << 24);
|
||||
uint32_t mac1 = (uint32_t) ifp->mac[4] |
|
||||
((uint32_t) ifp->mac[5] << 8);
|
||||
uint32_t mdc_cr =
|
||||
(uint32_t) (d->mdc_cr < 0 ? MG_DRIVER_MDC_CR : d->mdc_cr);
|
||||
|
||||
switch_port = NETC_SW0_PORT(d->link);
|
||||
eth_link = NETC_SW0_ETH(d->link);
|
||||
s_ifp = ifp;
|
||||
for (int i = 0; i < ETH_DESC_CNT; i++) {
|
||||
s_txdesc[i].buffer_address = (uint64_t) (uintptr_t) s_txbuf[i];
|
||||
s_rxdesc[i].descriptor.buffer_address =
|
||||
(uint64_t) (uintptr_t) s_rxbuf[i];
|
||||
}
|
||||
|
||||
NETC_EMDIO_PCI->DEV_CTL |= MG_BIT(15); // INIT_FLR
|
||||
while (NETC_EMDIO_PCI->DEV_CTL & MG_BIT(15)) (void) 0;
|
||||
NETC_EMDIO_PCI->CMD |= MG_BIT(2) | MG_BIT(1); // BUS_MASTER_EN, MEM_ACCESS
|
||||
NETC_EMDIO->EMDIO_CFG =
|
||||
((mdc_cr & 0x1ffU) << 7) | MG_BIT(2); // MDIO_CLK_DIV, MDIO_HOLD
|
||||
mg_phy_init(&phy, d->phy_addr, 0);
|
||||
|
||||
// Initialize ENETC1 PSI0 and port
|
||||
NETC_ENETC1_PCI->CMD |= MG_BIT(2) | MG_BIT(1); // BUS_MASTER_EN, MEM_ACCESS
|
||||
NETC_ENETC1_PCI->MSIX_MSG_CTL = MG_BIT(14); // FUNC_MASK
|
||||
NETC_ENETC1_PORT->POR |= MG_BIT(1) | MG_BIT(0); // RXDIS, TXDIS
|
||||
NETC_ENETC1_PCI->DEV_CTL |= MG_BIT(15); // INIT_FLR
|
||||
while (NETC_ENETC1_PCI->DEV_CTL & MG_BIT(15)) (void) 0;
|
||||
NETC_ENETC1_PCI->CMD |= MG_BIT(2) | MG_BIT(1); // BUS_MASTER_EN, MEM_ACCESS
|
||||
NETC_ENETC1_PCI->MSIX_MSG_CTL = MG_BIT(14); // FUNC_MASK
|
||||
NETC_ENETC1_PORT->PCR = 99U << 16; // PSPEED: 1 Gbps
|
||||
NETC_ENETC1_PORT->PMAR0 = mac0;
|
||||
NETC_ENETC1_PORT->PMAR1 = mac1;
|
||||
NETC_ENETC1_PSEUDO_LINK->PPMCR = MG_BIT(16); // TXPAD
|
||||
NETC_ENETC1->PSIPMMR &= ~(MG_BIT(16) | MG_BIT(0));
|
||||
NETC_ENETC1_SI0->SIMR &=
|
||||
~(MG_BIT(31) | MG_BIT(3) | MG_BIT(2) | MG_BIT(1));
|
||||
NETC_ENETC1_SI0->SIRBGCR = 0;
|
||||
NETC_ENETC1->PSI[0].PSICFGR0 =
|
||||
MG_BIT(16) | MG_BIT(0); // one Rx BDR, one Tx BDR
|
||||
|
||||
NETC_ENETC1->PSI[0].PSICFGR2 = 0; // NUM_MSIX: one entry
|
||||
NETC_ENETC1_MSIX[NETC_RX_MSIX_ENTRY].VECTOR_CTRL = MG_BIT(0); // MASK
|
||||
NETC_ENETC1_MSIX[NETC_RX_MSIX_ENTRY].MSG_ADDR =
|
||||
(uint32_t) (uintptr_t) &NETC_MSGINTR1->MSI[0].MSIIR;
|
||||
NETC_ENETC1_MSIX[NETC_RX_MSIX_ENTRY].MSG_UPPER_ADDR = 0;
|
||||
NETC_ENETC1_MSIX[NETC_RX_MSIX_ENTRY].MSG_DATA = NETC_RX_MSG_INDEX;
|
||||
NETC_ENETC1_SI0->SIMSIRRVR[0] = NETC_RX_MSIX_ENTRY; // Rx ring 0 -> entry 0
|
||||
|
||||
// Initialize switch function 2 to drive the selected physical port
|
||||
NETC_SWITCH_PCI->CMD |= MG_BIT(2) | MG_BIT(1);
|
||||
NETC_SWITCH_PCI->MSIX_MSG_CTL = MG_BIT(14); // FUNC_MASK
|
||||
switch_port->POR |= MG_BIT(1) | MG_BIT(0);
|
||||
eth_link->PM[0].COMMAND_CONFIG |= MG_BIT(26); // SWR
|
||||
while (eth_link->PM[0].COMMAND_CONFIG & MG_BIT(26)) (void) 0;
|
||||
eth_link->PM[1].COMMAND_CONFIG |= MG_BIT(26); // SWR
|
||||
while (eth_link->PM[1].COMMAND_CONFIG & MG_BIT(26)) (void) 0;
|
||||
NETC_SWITCH_PCI->DEV_CTL |= MG_BIT(15); // INIT_FLR
|
||||
while (NETC_SWITCH_PCI->DEV_CTL & MG_BIT(15)) (void) 0;
|
||||
NETC_SWITCH_PCI->CMD |= MG_BIT(2) | MG_BIT(1);
|
||||
NETC_SWITCH_PCI->MSIX_MSG_CTL = MG_BIT(14); // FUNC_MASK
|
||||
|
||||
// Configure switch physical port and ethernet link (ETH_X)
|
||||
switch_port->PCR = 99U << 16; // PSPEED: 1 Gbps
|
||||
switch_port->PMAR0 = mac0;
|
||||
switch_port->PMAR1 = mac1;
|
||||
switch_port->TCT[0].PTCTMSDUR = MG_BIT(24) | MG_BIT(16) | ETH_PKT_SIZE;
|
||||
eth_link->PM[0].MAXFRM = ETH_PKT_SIZE;
|
||||
eth_link->PM[0].MINFRM = 64;
|
||||
eth_link->PM[0].IF_MODE = MG_BIT(14) | 4U; // 1G RGMII
|
||||
eth_link->PM[0].COMMAND_CONFIG =
|
||||
MG_BIT(15) | MG_BIT(1) | MG_BIT(0); // TXP, RX_EN, TX_EN
|
||||
eth_link->PM[1].MAXFRM = ETH_PKT_SIZE;
|
||||
eth_link->PM[1].MINFRM = 64;
|
||||
eth_link->PM[1].IF_MODE = MG_BIT(14) | 4U;
|
||||
eth_link->PM[1].COMMAND_CONFIG =
|
||||
MG_BIT(15) | MG_BIT(1) | MG_BIT(0);
|
||||
|
||||
// Bridge the selected physical port with management port 4 (54.4.1.1)
|
||||
NETC_SWITCH->VFHTDECR0 = MG_BIT(4) | MG_BIT(d->link);
|
||||
NETC_SWITCH->VFHTDECR2 = (1UL << 27) | (1UL << 24); // MFO=1, MLO=1
|
||||
switch_port->BPSTGSR = 2U; // STG0: forwarding
|
||||
NETC_SW0_PORT4->BPSTGSR = 2U; // STG0: forwarding
|
||||
switch_port->POR &= ~(MG_BIT(1) | MG_BIT(0));
|
||||
NETC_SW0_PORT4->POR &= ~(MG_BIT(1) | MG_BIT(0));
|
||||
|
||||
// Initialize Tx BD ring 0 (direct switch enqueue)
|
||||
NETC_ENETC1_SI0->BDR[0].TBBAR0 = (uint32_t) tx_base;
|
||||
NETC_ENETC1_SI0->BDR[0].TBBAR1 = (uint32_t) (tx_base >> 32);
|
||||
NETC_ENETC1_SI0->BDR[0].TBLENR = ETH_DESC_CNT;
|
||||
|
||||
// Initialize Rx BD ring 0
|
||||
NETC_ENETC1_SI0->BDR[0].RBICR0 =
|
||||
MG_BIT(31) | MG_BIT(0); // ICEN, interrupt after one frame
|
||||
NETC_ENETC1_SI0->BDR[0].RBBAR0 = (uint32_t) rx_base;
|
||||
NETC_ENETC1_SI0->BDR[0].RBBAR1 = (uint32_t) (rx_base >> 32);
|
||||
NETC_ENETC1_SI0->BDR[0].RBBSR = ETH_PKT_SIZE;
|
||||
NETC_ENETC1_SI0->BDR[0].RBLENR = ETH_DESC_CNT;
|
||||
|
||||
// Clear stale device and MSGINTR state before the ring becomes active
|
||||
NETC_ENETC1_SI0->SIRXIDR0 = MG_BIT(0); // RX0, W1C
|
||||
(void) NETC_MSGINTR1->MSI[0].MSIR; // clear pending messages
|
||||
NETC_ENETC1_SI0->BDR[0].RBIER = MG_BIT(0); // RXTIE
|
||||
NETC_ENETC1_SI0->BDR[0].TBMR = MG_BIT(31);
|
||||
NETC_ENETC1_SI0->BDR[0].RBMR = MG_BIT(31); // EN, CRC=0: strip FCS
|
||||
NETC_ENETC1_PORT->POR &= ~(MG_BIT(1) | MG_BIT(0));
|
||||
NETC_ENETC1->PMR |= MG_BIT(16); // SI0EN
|
||||
NETC_ENETC1_SI0->SIMR |= MG_BIT(31); // EN
|
||||
MG_DSB();
|
||||
if ((NETC_ENETC1->SMCAPR & MG_BIT(0)) == 0U ||
|
||||
(NETC_SWITCH->MPCR & 0x1fU) != 4U) {
|
||||
MG_ERROR(("NETC fixed management path unavailable: SMCAPR=%08lx MPCR=%08lx",
|
||||
(unsigned long) NETC_ENETC1->SMCAPR,
|
||||
(unsigned long) NETC_SWITCH->MPCR));
|
||||
return false;
|
||||
}
|
||||
|
||||
NETC_ENETC1_PCI->MSIX_MSG_CTL =
|
||||
MG_BIT(15); // Enable RX interrupt, MSIX_EN=1, FUNC_MASK=0
|
||||
MG_DSB();
|
||||
NETC_ENETC1_MSIX[NETC_RX_MSIX_ENTRY].VECTOR_CTRL = 0; // MASK=0, last
|
||||
MG_DSB();
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t mg_tcpip_driver_netc_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_netc_data *d =
|
||||
(struct mg_tcpip_driver_netc_data *) ifp->driver_data;
|
||||
uint16_t next;
|
||||
s_tx_clean =
|
||||
(uint16_t) (NETC_ENETC1_SI0->BDR[0].TBCIR & 0xffffU); // BDR_INDEX
|
||||
next = (uint16_t) (s_tx_prod + 1U);
|
||||
if (next >= ETH_DESC_CNT) next = 0;
|
||||
if (len > sizeof(s_txbuf[0])) {
|
||||
MG_ERROR(("Frame too big, %ld", (long) len));
|
||||
len = (size_t) -1; // fail
|
||||
} else if (next == s_tx_clean) {
|
||||
ifp->nerr++;
|
||||
MG_ERROR(("No descriptors available"));
|
||||
len = 0; // retry later
|
||||
} else {
|
||||
memcpy(s_txbuf[s_tx_prod], buf, len); // Copy data
|
||||
s_txdesc[s_tx_prod].buffer_address =
|
||||
(uint64_t) (uintptr_t) s_txbuf[s_tx_prod];
|
||||
s_txdesc[s_tx_prod].buffer_length = (uint16_t) len; // BUFF_LEN
|
||||
s_txdesc[s_tx_prod].frame_length = (uint16_t) len; // FRAME_LEN
|
||||
s_txdesc[s_tx_prod].control =
|
||||
MG_BIT(31) | (2UL << 24) | MG_BIT(23) | ((uint32_t) d->link << 16);
|
||||
// F=1, FLQ=2, SMSO=1, EGR_PORT=link, IPV=0, DR=0
|
||||
MG_DSB();
|
||||
if (++s_tx_prod >= ETH_DESC_CNT) s_tx_prod = 0;
|
||||
NETC_ENETC1_SI0->BDR[0].TBPIR = s_tx_prod; // BDR_INDEX
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static void mg_tcpip_driver_netc_update_hash_table(struct mg_tcpip_if *ifp) {
|
||||
// mcast address has NETC XOR hash index 56 (RM 53.4.2.3.1.3.8.4.1)
|
||||
NETC_ENETC1->PSI[0].PSIMMHFR0 = 0;
|
||||
NETC_ENETC1->PSI[0].PSIMMHFR1 = MG_BIT(24);
|
||||
(void) ifp;
|
||||
}
|
||||
|
||||
static bool mg_tcpip_driver_netc_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
if (ifp->update_mac_hash_table) {
|
||||
mg_tcpip_driver_netc_update_hash_table(ifp);
|
||||
ifp->update_mac_hash_table = false;
|
||||
}
|
||||
if (!s1) return false;
|
||||
struct mg_tcpip_driver_netc_data *d =
|
||||
(struct mg_tcpip_driver_netc_data *) ifp->driver_data;
|
||||
struct netc_port *switch_port = NETC_SW0_PORT(d->link);
|
||||
struct netc_eth_link *eth_link = NETC_SW0_ETH(d->link);
|
||||
uint8_t speed = MG_PHY_SPEED_10M;
|
||||
bool up, full_duplex = false;
|
||||
struct mg_phy phy = {enet_read_phy, enet_write_phy};
|
||||
|
||||
up = mg_phy_up(&phy, d->phy_addr, &full_duplex, &speed);
|
||||
if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) {
|
||||
uint16_t port_speed = speed == MG_PHY_SPEED_1000M ? 99U
|
||||
: speed == MG_PHY_SPEED_100M ? 9U
|
||||
: 0U;
|
||||
uint32_t config = switch_port->PCR;
|
||||
uint32_t mode = eth_link->PM[0].IF_MODE;
|
||||
config &= ~(0x3fffU << 16); // PCR[29:16] PSPEED
|
||||
config |= (uint32_t) port_speed << 16;
|
||||
mode &= ~(MG_BIT(14) | MG_BIT(13) | MG_BIT(6)); // SSP, HD
|
||||
if (speed == MG_PHY_SPEED_1000M) {
|
||||
mode |= MG_BIT(14); // SSP: 1 Gbps, full duplex
|
||||
} else {
|
||||
if (speed == MG_PHY_SPEED_10M) mode |= MG_BIT(13); // SSP
|
||||
if (!full_duplex) mode |= MG_BIT(6); // HD
|
||||
}
|
||||
switch_port->PCR = config;
|
||||
eth_link->PM[0].IF_MODE = mode;
|
||||
eth_link->PM[1].IF_MODE = mode;
|
||||
|
||||
// TX_CSD, RX_CSD, and SPD_DUP are latched while the PHY is reset/down.
|
||||
eth_link->PM[0].IEVENT = MG_BIT(23) | MG_BIT(22) | MG_BIT(21);
|
||||
MG_DSB();
|
||||
MG_DEBUG(("Link is %uM %s-duplex",
|
||||
speed == MG_PHY_SPEED_1000M ? 1000U
|
||||
: speed == MG_PHY_SPEED_100M ? 100U
|
||||
: 10U,
|
||||
full_duplex ? "full" : "half"));
|
||||
}
|
||||
return up;
|
||||
}
|
||||
|
||||
void MSGINTR1_IRQHandler(void);
|
||||
void MSGINTR1_IRQHandler(void) {
|
||||
if (NETC_MSGINTR1->MSI[0].MSIR & MG_BIT(NETC_RX_MSG_INDEX)) {
|
||||
NETC_ENETC1_SI0->SIRXIDR0 = MG_BIT(0); // RX0, W1C
|
||||
MG_DSB();
|
||||
for (int i = 0; i < 10; i++) { // Read as they arrive but not forever
|
||||
uint32_t status;
|
||||
size_t len;
|
||||
if (s_rx_cons ==
|
||||
(uint16_t) (NETC_ENETC1_SI0->BDR[0].RBPIR & 0xffffU))
|
||||
break;
|
||||
status = s_rxdesc[s_rx_cons].writeback.status;
|
||||
if (!(status & MG_BIT(30))) break; // R: writeback is ready
|
||||
len = s_rxdesc[s_rx_cons].writeback.buffer_length;
|
||||
if ((status & (MG_BIT(31) | MG_BIT(30))) ==
|
||||
(MG_BIT(31) | MG_BIT(30)) && // F, R
|
||||
!(status & (0xffU << 16)) && // ERROR
|
||||
!(status & (0xfU << 2)) && // HR=0: regular frame
|
||||
len > 0 && len <= sizeof(s_rxbuf[0])) {
|
||||
mg_tcpip_qwrite(s_rxbuf[s_rx_cons], len, s_ifp); // FCS stripped
|
||||
}
|
||||
s_rxdesc[s_rx_cons].descriptor.buffer_address =
|
||||
(uint64_t) (uintptr_t) s_rxbuf[s_rx_cons];
|
||||
s_rxdesc[s_rx_cons].descriptor.reserved = 0;
|
||||
MG_DSB();
|
||||
if (++s_rx_cons >= ETH_DESC_CNT) s_rx_cons = 0;
|
||||
NETC_ENETC1_SI0->BDR[0].RBCIR = s_rx_cons;
|
||||
}
|
||||
}
|
||||
MG_DSB();
|
||||
}
|
||||
|
||||
struct mg_tcpip_driver mg_tcpip_driver_netc = {mg_tcpip_driver_netc_init,
|
||||
mg_tcpip_driver_netc_tx, NULL,
|
||||
mg_tcpip_driver_netc_poll};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP && \
|
||||
defined(MG_ENABLE_DRIVER_NETC) && MG_ENABLE_DRIVER_NETC
|
||||
|
||||
struct mg_tcpip_driver_netc_data {
|
||||
int mdc_cr;
|
||||
uint8_t phy_addr;
|
||||
|
||||
// Selects the physical Ethernet link (ETH0 through ETH3)
|
||||
// Its switch port is bridged to pseudo-port 4,
|
||||
// which connects internally to ENETC1
|
||||
// IMXRT1180 RM 53.4.1.1 NETC Block diagram
|
||||
uint8_t link;
|
||||
};
|
||||
|
||||
#ifndef MG_TCPIP_PHY_ADDR
|
||||
#define MG_TCPIP_PHY_ADDR 5
|
||||
#endif
|
||||
|
||||
#ifndef MG_DRIVER_MDC_CR
|
||||
#define MG_DRIVER_MDC_CR 48
|
||||
#endif
|
||||
|
||||
#ifndef MG_TCPIP_NETC_LINK
|
||||
#define MG_TCPIP_NETC_LINK 0
|
||||
#endif
|
||||
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
||||
|
||||
#define MG_TCPIP_DRIVER_NETC_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_netc_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
|
||||
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
|
||||
driver_data_.link = MG_TCPIP_NETC_LINK; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_netc; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: netc, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NETC_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,169 @@
|
||||
#include "net_builtin.h"
|
||||
#include "l2_mesh.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NRF52) && MG_ENABLE_DRIVER_NRF52
|
||||
#include <nrf.h>
|
||||
|
||||
static struct mg_tcpip_if *s_nrf52_ifp;
|
||||
static struct mg_queue s_nrf52_rx;
|
||||
static char s_nrf52_queue[2048];
|
||||
static uint8_t s_nrf52_rxbuf[128] __attribute__((aligned(4)));
|
||||
static uint8_t s_nrf52_txbuf[128] __attribute__((aligned(4)));
|
||||
static volatile bool s_nrf52_txing, s_nrf52_done;
|
||||
static uint64_t s_nrf52_started;
|
||||
|
||||
static void mgi_nrf52_listen(void) {
|
||||
NRF_RADIO->PACKETPTR = (uint32_t) s_nrf52_rxbuf;
|
||||
NRF_RADIO->SHORTS =
|
||||
RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk;
|
||||
NRF_RADIO->EVENTS_FRAMESTART = 0;
|
||||
NRF_RADIO->EVENTS_END = 0;
|
||||
NRF_RADIO->TASKS_RXEN = 1;
|
||||
}
|
||||
static void mgi_nrf52_disable(void) {
|
||||
NRF_RADIO->SHORTS = 0;
|
||||
NRF_RADIO->EVENTS_DISABLED = 0;
|
||||
NRF_RADIO->TASKS_DISABLE = 1;
|
||||
while (!NRF_RADIO->EVENTS_DISABLED) (void) 0;
|
||||
NRF_RADIO->EVENTS_DISABLED = 0;
|
||||
}
|
||||
void RADIO_IRQHandler(void);
|
||||
void RADIO_IRQHandler(void) {
|
||||
if (!NRF_RADIO->EVENTS_DISABLED) return;
|
||||
NRF_RADIO->EVENTS_DISABLED = 0;
|
||||
if (s_nrf52_txing) {
|
||||
s_nrf52_txing = false;
|
||||
s_nrf52_done = true;
|
||||
} else if (NRF_RADIO->CRCSTATUS) {
|
||||
struct mg_tcpip_driver_nrf52_data *data = s_nrf52_ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
size_t len = s_nrf52_rxbuf[0] >= 2 ? s_nrf52_rxbuf[0] - 2U : 0;
|
||||
uint8_t *frame = s_nrf52_rxbuf + 1;
|
||||
uint16_t dst, src;
|
||||
if (len <= 125 &&
|
||||
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
|
||||
0 &&
|
||||
(dst == 0xffffu || dst == node->radio_address)) {
|
||||
mg_mesh_rx_queue(&s_nrf52_rx, s_nrf52_ifp, frame, len);
|
||||
}
|
||||
}
|
||||
mgi_nrf52_listen();
|
||||
}
|
||||
static size_t mgi_nrf52_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||
size_t len) {
|
||||
(void) ifp;
|
||||
if (len > 125) return 0;
|
||||
NVIC_DisableIRQ(RADIO_IRQn);
|
||||
if (s_nrf52_txing || NRF_RADIO->EVENTS_FRAMESTART) {
|
||||
NVIC_EnableIRQ(RADIO_IRQn);
|
||||
return 0;
|
||||
}
|
||||
mgi_nrf52_disable();
|
||||
s_nrf52_txbuf[0] = (uint8_t) (len + 2);
|
||||
memcpy(s_nrf52_txbuf + 1, buf, len);
|
||||
s_nrf52_done = false;
|
||||
s_nrf52_txing = true;
|
||||
s_nrf52_started = mg_micros();
|
||||
NRF_RADIO->PACKETPTR = (uint32_t) s_nrf52_txbuf;
|
||||
NRF_RADIO->SHORTS =
|
||||
RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk;
|
||||
NRF_RADIO->TASKS_TXEN = 1;
|
||||
NVIC_EnableIRQ(RADIO_IRQn);
|
||||
return len;
|
||||
}
|
||||
static bool mgi_nrf52_radio_init(struct mg_tcpip_if *ifp) {
|
||||
s_nrf52_ifp = ifp;
|
||||
NRF_RADIO->POWER = 1;
|
||||
NRF_RADIO->MODE = RADIO_MODE_MODE_Ieee802154_250Kbit;
|
||||
NRF_RADIO->PCNF0 = (8UL << RADIO_PCNF0_LFLEN_Pos) |
|
||||
(RADIO_PCNF0_PLEN_32bitZero << RADIO_PCNF0_PLEN_Pos) |
|
||||
RADIO_PCNF0_CRCINC_Msk;
|
||||
NRF_RADIO->PCNF1 = 127;
|
||||
NRF_RADIO->CRCCNF = 0x202;
|
||||
NRF_RADIO->CRCPOLY = 0x11021;
|
||||
NRF_RADIO->CRCINIT = 0;
|
||||
NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_DTX_Center << RADIO_MODECNF0_DTX_Pos) |
|
||||
RADIO_MODECNF0_RU_Fast;
|
||||
NRF_RADIO->SFD = 0xa7;
|
||||
NRF_RADIO->FREQUENCY = 5 * (MG_802154_CHANNEL - 10);
|
||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos8dBm;
|
||||
// Correlator threshold 45 matches the RADIO reset value. A threshold of
|
||||
// 20 caused false busy detections and persistent L2 retries.
|
||||
NRF_RADIO->CCACTRL =
|
||||
(RADIO_CCACTRL_CCAMODE_CarrierMode << RADIO_CCACTRL_CCAMODE_Pos) |
|
||||
(2UL << RADIO_CCACTRL_CCACORRCNT_Pos) |
|
||||
(45UL << RADIO_CCACTRL_CCACORRTHRES_Pos);
|
||||
NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
|
||||
NVIC_SetPriority(RADIO_IRQn, 0);
|
||||
NVIC_EnableIRQ(RADIO_IRQn);
|
||||
mgi_nrf52_listen();
|
||||
return true;
|
||||
}
|
||||
static size_t mgi_nrf52_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
||||
return data->mesh->strategy->tx(data->mesh, buf, len);
|
||||
}
|
||||
|
||||
static size_t mgi_nrf52_rx(void *buf, size_t cap, struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_nrf52_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_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
uint64_t now = mg_micros();
|
||||
(void) s1;
|
||||
if (s_nrf52_txing && mg_micros() - s_nrf52_started > MG_MESH_TX_TIMEOUT_US) {
|
||||
NVIC_DisableIRQ(RADIO_IRQn);
|
||||
mgi_nrf52_disable();
|
||||
s_nrf52_txing = false;
|
||||
s_nrf52_done = true;
|
||||
mgi_nrf52_listen();
|
||||
NVIC_EnableIRQ(RADIO_IRQn);
|
||||
}
|
||||
if (s_nrf52_done) {
|
||||
s_nrf52_done = false;
|
||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||
}
|
||||
node->now = now;
|
||||
mg_mesh_rx_drain(node, &s_nrf52_rx);
|
||||
node->strategy->tick(node, now);
|
||||
return node->joined;
|
||||
}
|
||||
|
||||
static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
||||
const struct mg_mesh *strategy = data->strategy;
|
||||
uint16_t address;
|
||||
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_nrf52_radio_tx;
|
||||
mg_queue_init(&s_nrf52_rx, s_nrf52_queue, sizeof(s_nrf52_queue));
|
||||
if (mgi_nrf52_radio_init(ifp)) return true;
|
||||
strategy->free(data->mesh);
|
||||
mg_free(data->mesh);
|
||||
data->mesh = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mg_tcpip_driver mg_tcpip_driver_nrf52 = {mgi_nrf52_init, mgi_nrf52_tx,
|
||||
mgi_nrf52_rx, mgi_nrf52_poll};
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#pragma once
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NRF52) && MG_ENABLE_DRIVER_NRF52
|
||||
|
||||
struct mg_tcpip_driver_nrf52_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_nrf52;
|
||||
|
||||
#define MG_TCPIP_DRIVER_NRF52_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_nrf52_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_.driver = &mg_tcpip_driver_nrf52; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NRF52_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,5 @@
|
||||
#include "net_builtin.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NXP_WIFI) && \
|
||||
MG_ENABLE_DRIVER_NXP_WIFI
|
||||
|
||||
|
||||
+10
-2
@@ -4,12 +4,16 @@
|
||||
defined(MG_ENABLE_DRIVER_NXP_WIFI) && MG_ENABLE_DRIVER_NXP_WIFI
|
||||
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
struct mg_tcpip_driver_nxp_wifi_data {
|
||||
struct mg_wifi_data wifi;
|
||||
};
|
||||
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_nxp_wifi;
|
||||
|
||||
#define MG_TCPIP_DRIVER_NXP_WIFI_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_nxp_wifi_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -21,8 +25,12 @@ struct mg_tcpip_driver_nxp_wifi_data {
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mif_.recv_queue.size = 8192; \
|
||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: nxp wifi, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NXP_WIFI_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+32
-2
@@ -1,4 +1,6 @@
|
||||
#include "phy.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
enum { // ID1 ID2
|
||||
MG_PHY_KSZ8x = 0x22, // 0022 156x - KSZ8081RNB, KSZ8091RNB
|
||||
@@ -11,7 +13,9 @@ enum { // ID1 ID2
|
||||
MG_PHY_RTL8201 = 0xc816, // 001c c816 - RTL8201F
|
||||
MG_PHY_RTL8211 = 0xc916, // 001c c916 - RTL8211F
|
||||
MG_PHY_ICS1894x = 0x15,
|
||||
MG_PHY_ICS189432 = 0xf450 // 0015 f450 - ICS1894
|
||||
MG_PHY_ICS189432 = 0xf450, // 0015 f450 - ICS1894
|
||||
MG_PHY_YT85x = 0x4f51,
|
||||
MG_PHY_YT8531 = 0xe91a // 4f51 e91a - Motorcomm YT8531
|
||||
};
|
||||
|
||||
enum {
|
||||
@@ -29,7 +33,10 @@ enum {
|
||||
MG_PHY_RTL82x_REG_PAGESEL = 31,
|
||||
MG_PHY_RTL8201_REG_RMSR = 16, // in page 7
|
||||
MG_PHY_RTL8211_REG_PHYSR = 26, // in page a43
|
||||
MG_PHY_ICS189432_REG_POLL = 17
|
||||
MG_PHY_ICS189432_REG_POLL = 17,
|
||||
MG_PHY_YT8531_REG_PHYSTS = 17,
|
||||
MG_PHY_YT8531_REG_EXT_ADDR = 30,
|
||||
MG_PHY_YT8531_REG_EXT_DATA = 31
|
||||
};
|
||||
|
||||
static const char *mg_phy_id_to_str(uint16_t id1, uint16_t id2) {
|
||||
@@ -60,6 +67,8 @@ static const char *mg_phy_id_to_str(uint16_t id1, uint16_t id2) {
|
||||
}
|
||||
case MG_PHY_ICS1894x:
|
||||
return "ICS1894x";
|
||||
case MG_PHY_YT85x:
|
||||
return id2 == MG_PHY_YT8531 ? "YT8531" : "YT85x";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
@@ -75,6 +84,18 @@ void mg_phy_init(struct mg_phy *phy, uint8_t phy_addr, uint8_t config) {
|
||||
id2 = phy->read_reg(phy_addr, MG_PHY_REG_ID2);
|
||||
MG_INFO(("PHY ID: %#04x %#04x (%s)", id1, id2, mg_phy_id_to_str(id1, id2)));
|
||||
|
||||
if (id1 == MG_PHY_YT85x && id2 == MG_PHY_YT8531) {
|
||||
uint16_t value;
|
||||
// TODO (robertc2000): Autonegotiation remains disabled after PHY is reset
|
||||
// We temporarily force enabling autoneg, until a workaround is found.
|
||||
phy->write_reg(phy_addr, MG_PHY_REG_BCR, MG_BIT(12));
|
||||
phy->write_reg(phy_addr, MG_PHY_YT8531_REG_EXT_ADDR, 0xa003);
|
||||
value = phy->read_reg(phy_addr, MG_PHY_YT8531_REG_EXT_DATA);
|
||||
value &= (uint16_t) ~(MG_BIT(3) | MG_BIT(2) | MG_BIT(1) | MG_BIT(0));
|
||||
phy->write_reg(phy_addr, MG_PHY_YT8531_REG_EXT_DATA,
|
||||
(uint16_t) (value | MG_BIT(1))); // TXDLY: 2 * 150 ps
|
||||
}
|
||||
|
||||
if (id1 == MG_PHY_DP83x && id2 == MG_PHY_DP83867) {
|
||||
phy->write_reg(phy_addr, 0x0d, 0x1f); // write 0x10d to IO_MUX_CFG (0x0170)
|
||||
phy->write_reg(phy_addr, 0x0e, 0x170);
|
||||
@@ -168,6 +189,15 @@ bool mg_phy_up(struct mg_phy *phy, uint8_t phy_addr, bool *full_duplex,
|
||||
uint16_t poll_reg = phy->read_reg(phy_addr, MG_PHY_ICS189432_REG_POLL);
|
||||
*full_duplex = poll_reg & MG_BIT(14);
|
||||
*speed = (poll_reg & MG_BIT(15)) ? MG_PHY_SPEED_100M : MG_PHY_SPEED_10M;
|
||||
} else if (id1 == MG_PHY_YT85x) {
|
||||
uint16_t id2 = phy->read_reg(phy_addr, MG_PHY_REG_ID2);
|
||||
if (id2 == MG_PHY_YT8531) {
|
||||
uint16_t physts = phy->read_reg(phy_addr, MG_PHY_YT8531_REG_PHYSTS);
|
||||
*full_duplex = physts & MG_BIT(13);
|
||||
*speed = (physts & MG_BIT(15)) ? MG_PHY_SPEED_1000M
|
||||
: (physts & MG_BIT(14)) ? MG_PHY_SPEED_100M
|
||||
: MG_PHY_SPEED_10M;
|
||||
}
|
||||
}
|
||||
}
|
||||
return up;
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#include "net_builtin.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \
|
||||
defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W
|
||||
|
||||
#include "drivers/pico-w.h"
|
||||
#include "net_builtin.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include "printf.h"
|
||||
#include "util.h"
|
||||
|
||||
static struct mg_tcpip_if *s_ifp;
|
||||
static uint32_t s_ip, s_mask;
|
||||
@@ -68,8 +71,9 @@ static bool mg_tcpip_driver_pico_w_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
}
|
||||
if (ifp->update_mac_hash_table) {
|
||||
// first call to _poll() is after _init(), so this is safe
|
||||
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *) mcast_addr,
|
||||
true);
|
||||
uint8_t mac[sizeof(mcast_addr)]; // copy from const uint8_t* to uint8_t*
|
||||
memcpy(mac, mcast_addr, sizeof(mac));
|
||||
cyw43_wifi_update_multicast_filter(&cyw43_state, mac, true);
|
||||
ifp->update_mac_hash_table = false;
|
||||
}
|
||||
if (!s1) return false;
|
||||
|
||||
+10
-2
@@ -7,11 +7,15 @@
|
||||
#include "pico/cyw43_arch.h" // keep this include
|
||||
#include "pico/unique_id.h" // keep this include
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
struct mg_tcpip_driver_pico_w_data {
|
||||
struct mg_wifi_data wifi;
|
||||
};
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_pico_w;
|
||||
|
||||
#define MG_TCPIP_DRIVER_PICO_W_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_pico_w_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -23,8 +27,12 @@ struct mg_tcpip_driver_pico_w_data {
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mif_.recv_queue.size = 8192; \
|
||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: pico-w, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_PICO_W_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && \
|
||||
(defined(MG_ENABLE_DRIVER_RA6) && MG_ENABLE_DRIVER_RA6) || \
|
||||
|
||||
+8
-2
@@ -23,7 +23,9 @@ struct mg_tcpip_driver_ra_data {
|
||||
#define MG_TCPIP_PHY_ADDR 0
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_ra;
|
||||
|
||||
#define MG_TCPIP_DRIVER_RA_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_ra_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -36,8 +38,12 @@ struct mg_tcpip_driver_ra_data {
|
||||
mif_.driver = &mg_tcpip_driver_ra; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: ra, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RA_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612) && MG_ENABLE_DRIVER_RW612
|
||||
|
||||
|
||||
+8
-2
@@ -27,7 +27,9 @@ struct mg_tcpip_driver_rw612_data {
|
||||
#define MG_DRIVER_MDC_HOLDTIME 3
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_rw612;
|
||||
|
||||
#define MG_TCPIP_DRIVER_RW612_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_rw612_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -39,8 +41,12 @@ struct mg_tcpip_driver_rw612_data {
|
||||
mif_.driver = &mg_tcpip_driver_rw612; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: rw612, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RW612_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,501 @@
|
||||
#include "net_builtin.h"
|
||||
#include "l2_mesh.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612W) && \
|
||||
MG_ENABLE_DRIVER_RW612W
|
||||
|
||||
#include "sha256.h"
|
||||
|
||||
#ifdef ENET
|
||||
#undef ENET
|
||||
#endif
|
||||
#include <fsl_ocotp.h>
|
||||
|
||||
// The 802.15.4 radio runs NXP combo firmware on CPU2, an OpenThread RCP. We
|
||||
// are CPU3. Messages go through the IMU mailbox: IMU messages carry IMUMC
|
||||
// messages, IMUMC messages carry HDLC framed Spinel. See NXP sources:
|
||||
// mcuxsdk-core drivers/imu/fsl_imu.c, mcux-component
|
||||
// imu_adapter/fsl_adapter_imu.c, connectivity framework
|
||||
// platform/rw61x/fwk_platform_hdlc.c; OpenThread src/lib/spinel/spinel.h
|
||||
|
||||
// IMU message FIFO registers, one block per direction. See fsl_imu.h
|
||||
struct mg_rw612_imu {
|
||||
volatile uint32_t WR_MSG, RD_MSG, STATUS, CNTL, RD_MSG_DBG;
|
||||
};
|
||||
#define MGI_IMU ((struct mg_rw612_imu *) 0x443ff804) // CPU3 -> CPU2
|
||||
#define MGI_IMU_PEER ((struct mg_rw612_imu *) 0x443ff818) // CPU2 -> CPU3
|
||||
#define MGI_IMU_LOCKED MG_BIT(0) // STATUS: FIFO locked
|
||||
#define MGI_IMU_ALMOST_FULL MG_BIT(1) // STATUS: FIFO almost full
|
||||
#define MGI_IMU_EMPTY MG_BIT(3) // STATUS: FIFO empty
|
||||
#define MGI_IMU_WR_PTR(s) (((s) >> 16) & 15U)
|
||||
#define MGI_IMU_INT_CLR MG_BIT(0) // CNTL: clear message ready IRQ
|
||||
#define MGI_IMU_FLUSH MG_BIT(16) // CNTL: flush send FIFO
|
||||
#define MGI_IMU_WAIT_FOR_ACK MG_BIT(17) // CNTL: lock send FIFO
|
||||
#define MGI_IMU_WATERMARK(x) ((uint32_t) (x) << 20)
|
||||
#define MGI_IMU_WATERMARK_MASK (15U << 20)
|
||||
|
||||
// IMU message: header, then payload words. See IMU_Msg_t
|
||||
struct mg_rw612_msg {
|
||||
uint8_t type, subtype, seq, len; // len: number of payload words
|
||||
uint32_t payload[8];
|
||||
};
|
||||
|
||||
// Mailbox in SMU2 shared memory, at the address CPU2 firmware expects
|
||||
struct mg_rw612_mbox {
|
||||
uint32_t magic[2]; // Sync pattern: [0] CPU2, [1] us
|
||||
struct mg_rw612_msg q[2][16]; // Message slots: [0] CPU2 -> us, [1] ours
|
||||
};
|
||||
#define MGI_MBOX ((volatile struct mg_rw612_mbox *) 0x443c0000)
|
||||
#define MGI_TXBUF 0x443c1508U // Our IMUMC buffers: 16 x 264 bytes
|
||||
#define MGI_TXBUF_CNT 16
|
||||
#define MGI_TXBUF_LEN 264U
|
||||
#define MGI_SLEEP_FLAG (*(volatile uint32_t *) 0x443c2588)
|
||||
#define MGI_SYNC_MAGIC 0xabcdef89U
|
||||
|
||||
// IMUMC message, carried in IMU payload words. See imumc_std_msg
|
||||
struct mg_rw612_imumc {
|
||||
uint32_t src, dst; // Endpoint addresses
|
||||
uint16_t rfu, idx; // Reserved
|
||||
uint16_t len; // Payload length, bytes
|
||||
uint16_t flags; // Message flags
|
||||
uint32_t data; // Payload buffer address
|
||||
};
|
||||
#define MGI_EPT_LOCAL 10 // Our 802.15.4 endpoint
|
||||
#define MGI_EPT_REMOTE 20 // Firmware 802.15.4 endpoint
|
||||
|
||||
enum { MGI_MSG_CONTROL = 0, MGI_MSG_IMUMC = 8 }; // IMU message types
|
||||
enum { // Control message subtypes
|
||||
MGI_CTL_SYNC = 1,
|
||||
MGI_CTL_SYNC_ACK = 2,
|
||||
MGI_CTL_ACK_FOR_UNLOCK = 3,
|
||||
MGI_CTL_CMD_BUF_ADDR = 6,
|
||||
MGI_CTL_EPT_QUERY = 11,
|
||||
MGI_CTL_EPT_QUERY_RSP = 12,
|
||||
MGI_CTL_BUF_FREE = 13
|
||||
};
|
||||
|
||||
// Spinel commands, properties and statuses we use
|
||||
enum { MGI_CMD_RESET = 1, MGI_CMD_GET = 2, MGI_CMD_SET = 3, MGI_CMD_IS = 6 };
|
||||
enum {
|
||||
MGI_PROP_LAST_STATUS = 0,
|
||||
MGI_PROP_NCP_VERSION = 2,
|
||||
MGI_PROP_PHY_ENABLED = 0x20,
|
||||
MGI_PROP_PHY_CHAN = 0x21,
|
||||
MGI_PROP_SADDR = 0x35, // Short address, little endian
|
||||
MGI_PROP_PANID = 0x36,
|
||||
MGI_PROP_RAW_STREAM = 0x37,
|
||||
MGI_PROP_PROMISCUOUS = 0x38,
|
||||
MGI_PROP_STREAM_RAW = 0x71
|
||||
};
|
||||
#define MGI_RESET_STACK 2 // RESET argument: reset the RCP stack only
|
||||
#define MGI_STATUS_RESET_MIN 112 // LAST_STATUS: RCP has reset
|
||||
#define MGI_STATUS_RESET_MAX 127
|
||||
#define MGI_TX_TIMEOUT_MS 200 // Wait for transmit done, incl. retries
|
||||
#define MGI_RSP_TIMEOUT_MS 500
|
||||
|
||||
// Driver state, one radio
|
||||
struct mg_rw612 {
|
||||
bool up; // IMU link synchronised
|
||||
bool ept_ready; // IMUMC endpoints connected
|
||||
bool reset; // RCP reported a reset
|
||||
uint8_t seq; // IMU message sequence number
|
||||
uint16_t free_bufs; // Free IMUMC buffers, bit per buffer
|
||||
uint8_t hdlc[300]; // HDLC frame being received, unescaped
|
||||
size_t hlen; // Bytes in hdlc
|
||||
bool esc; // Previous byte was an escape
|
||||
uint8_t tid; // Spinel transaction ID of the pending request
|
||||
bool rsp; // Response to the pending request received
|
||||
uint32_t rsp_status; // Its status, if LAST_STATUS, else 0
|
||||
char version[64]; // RCP version string
|
||||
bool txing; // A send is on, see mg_rw612_start()
|
||||
uint64_t txstart; // Its start, ms
|
||||
struct mg_tcpip_if *ifp; // Our interface, for the L2 receive pool
|
||||
};
|
||||
static struct mg_rw612 s_rw612;
|
||||
static struct mg_queue s_rw612w_rx;
|
||||
static char s_rw612w_queue[2048];
|
||||
|
||||
static void mg_rw612_imu_send(uint8_t type, uint8_t subtype,
|
||||
const uint32_t *payload, uint8_t len) {
|
||||
volatile struct mg_rw612_msg *m;
|
||||
uint64_t expire = mg_millis() + 100;
|
||||
uint8_t i;
|
||||
while ((MGI_IMU->STATUS & (MGI_IMU_ALMOST_FULL | MGI_IMU_LOCKED)) &&
|
||||
mg_millis() < expire) {
|
||||
(void) 0;
|
||||
}
|
||||
m = &MGI_MBOX->q[1][MGI_IMU_WR_PTR(MGI_IMU->STATUS)];
|
||||
m->type = type, m->subtype = subtype, m->seq = s_rw612.seq++, m->len = len;
|
||||
for (i = 0; i < len; i++) m->payload[i] = payload[i];
|
||||
MGI_IMU->WR_MSG = (uint32_t) (uintptr_t) m;
|
||||
}
|
||||
|
||||
static void mg_rw612_control(uint8_t subtype, uint32_t a, uint32_t b,
|
||||
uint8_t len) {
|
||||
uint32_t payload[2] = {a, b};
|
||||
mg_rw612_imu_send(MGI_MSG_CONTROL, subtype, payload, 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);
|
||||
}
|
||||
|
||||
// Spinel packed unsigned integer: 7 bits per byte, LSB first
|
||||
static size_t mg_rw612_unpack(const uint8_t *p, size_t len, uint32_t *v) {
|
||||
size_t i;
|
||||
*v = 0;
|
||||
for (i = 0; i < len && i < 4; i++) {
|
||||
*v |= (uint32_t) (p[i] & 0x7f) << (7 * i);
|
||||
if ((p[i] & 0x80) == 0) return i + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Handle a received Spinel frame: header, command, property, value
|
||||
static void mg_rw612_spinel(const uint8_t *f, size_t len) {
|
||||
struct mg_rw612 *s = &s_rw612;
|
||||
uint32_t cmd, prop, status = 0;
|
||||
size_t n, ofs = 1;
|
||||
uint8_t tid = f[0] & 15;
|
||||
if (len < 3 || (n = mg_rw612_unpack(f + ofs, len - ofs, &cmd)) == 0) return;
|
||||
ofs += n;
|
||||
if ((n = mg_rw612_unpack(f + ofs, len - ofs, &prop)) == 0) return;
|
||||
ofs += n;
|
||||
if (cmd != MGI_CMD_IS) return;
|
||||
if (prop == MGI_PROP_STREAM_RAW && tid == 0 && len - ofs >= 2) {
|
||||
size_t flen = (size_t) (f[ofs] | (f[ofs + 1] << 8)); // Incl. FCS
|
||||
if (flen >= 2 && ofs + 2 + flen <= len) {
|
||||
mg_rw612_queue_rx(f + ofs + 2, flen - 2);
|
||||
}
|
||||
} else if (prop == MGI_PROP_LAST_STATUS) {
|
||||
mg_rw612_unpack(f + ofs, len - ofs, &status);
|
||||
if (tid == 0 && status >= MGI_STATUS_RESET_MIN &&
|
||||
status <= MGI_STATUS_RESET_MAX) {
|
||||
s->reset = true;
|
||||
}
|
||||
} else if (prop == MGI_PROP_NCP_VERSION) {
|
||||
n = len - ofs < sizeof(s->version) - 1 ? len - ofs : sizeof(s->version) - 1;
|
||||
memcpy(s->version, f + ofs, n);
|
||||
s->version[n] = '\0';
|
||||
}
|
||||
if (tid != 0 && tid == s->tid) s->rsp = true, s->rsp_status = status;
|
||||
}
|
||||
|
||||
// HDLC-lite: 0x7e delimits frames, 0x7d escapes, FCS-16 at the end
|
||||
static uint16_t mg_rw612_fcs(uint16_t fcs, uint8_t c) {
|
||||
int i;
|
||||
fcs ^= c;
|
||||
for (i = 0; i < 8; i++) fcs = (fcs & 1) ? (fcs >> 1) ^ 0x8408 : fcs >> 1;
|
||||
return fcs;
|
||||
}
|
||||
|
||||
static void mg_rw612_hdlc_rx(const uint8_t *p, size_t len) {
|
||||
struct mg_rw612 *s = &s_rw612;
|
||||
size_t i, j;
|
||||
for (i = 0; i < len; i++) {
|
||||
uint8_t c = p[i];
|
||||
if (c == 0x7e) {
|
||||
if (s->hlen >= 3) {
|
||||
uint16_t fcs = 0xffff;
|
||||
for (j = 0; j < s->hlen; j++) fcs = mg_rw612_fcs(fcs, s->hdlc[j]);
|
||||
if (fcs == 0xf0b8) mg_rw612_spinel(s->hdlc, s->hlen - 2);
|
||||
}
|
||||
s->hlen = 0, s->esc = false;
|
||||
} else if (c == 0x7d) {
|
||||
s->esc = true;
|
||||
} else if (s->hlen < sizeof(s->hdlc)) {
|
||||
s->hdlc[s->hlen++] = s->esc ? c ^ 0x20 : c;
|
||||
s->esc = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void mg_rw612_control_rx(volatile struct mg_rw612_msg *m) {
|
||||
struct mg_rw612 *s = &s_rw612;
|
||||
switch (m->subtype) {
|
||||
case MGI_CTL_ACK_FOR_UNLOCK:
|
||||
MGI_IMU->CNTL &= ~MGI_IMU_WAIT_FOR_ACK;
|
||||
break;
|
||||
case MGI_CTL_SYNC: // As HAL_ImuCtrlHandler(): acknowledge twice
|
||||
mg_rw612_control(MGI_CTL_SYNC_ACK, 0, 0, 0);
|
||||
mg_rw612_control(MGI_CTL_SYNC_ACK, 0, 0, 0);
|
||||
s->up = true;
|
||||
break;
|
||||
case MGI_CTL_SYNC_ACK:
|
||||
case MGI_CTL_CMD_BUF_ADDR:
|
||||
s->up = true;
|
||||
break;
|
||||
case MGI_CTL_EPT_QUERY: { // Firmware asks if our endpoint exists
|
||||
bool ours = m->payload[0] == MGI_EPT_LOCAL;
|
||||
mg_rw612_control(MGI_CTL_EPT_QUERY_RSP, m->payload[0], ours, 2);
|
||||
if (ours) s->ept_ready = true;
|
||||
break;
|
||||
}
|
||||
case MGI_CTL_EPT_QUERY_RSP:
|
||||
if (m->payload[0] == MGI_EPT_REMOTE && m->payload[1] != 0) {
|
||||
s->ept_ready = true;
|
||||
}
|
||||
break;
|
||||
case MGI_CTL_BUF_FREE: { // Firmware is done with one of our buffers
|
||||
uint32_t i = (m->payload[0] - MGI_TXBUF) / MGI_TXBUF_LEN;
|
||||
if (i < MGI_TXBUF_CNT) s->free_bufs |= (uint16_t) MG_BIT(i);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Process all received IMU messages. See HAL_ImuReceive()
|
||||
static void mg_rw612_poll_imu(void) {
|
||||
bool unlock = false;
|
||||
while ((MGI_IMU_PEER->STATUS & MGI_IMU_EMPTY) == 0) {
|
||||
struct mg_rw612_msg m;
|
||||
volatile struct mg_rw612_msg *p;
|
||||
uint8_t i;
|
||||
MGI_SLEEP_FLAG &= ~1U;
|
||||
p = (volatile struct mg_rw612_msg *) (uintptr_t) MGI_IMU->RD_MSG;
|
||||
if (MGI_IMU_PEER->STATUS & MGI_IMU_LOCKED) unlock = true;
|
||||
if (p == NULL) continue;
|
||||
m.type = p->type, m.subtype = p->subtype, m.len = p->len;
|
||||
for (i = 0; i < 8; i++) m.payload[i] = p->payload[i];
|
||||
if (m.type == MGI_MSG_CONTROL) {
|
||||
mg_rw612_control_rx(&m);
|
||||
} else if (m.type == MGI_MSG_IMUMC) {
|
||||
struct mg_rw612_imumc im;
|
||||
memcpy(&im, m.payload, sizeof(im));
|
||||
if (im.dst == MGI_EPT_LOCAL) {
|
||||
mg_rw612_hdlc_rx((const uint8_t *) (uintptr_t) im.data, im.len);
|
||||
}
|
||||
mg_rw612_control(MGI_CTL_BUF_FREE, im.data, 0, 1);
|
||||
}
|
||||
}
|
||||
MGI_IMU->CNTL |= MGI_IMU_INT_CLR;
|
||||
MGI_SLEEP_FLAG |= 1U;
|
||||
if (unlock) mg_rw612_control(MGI_CTL_ACK_FOR_UNLOCK, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Send bytes to the firmware endpoint, in IMUMC buffer sized chunks
|
||||
static bool mg_rw612_imumc_send(const uint8_t *p, size_t len) {
|
||||
struct mg_rw612 *s = &s_rw612;
|
||||
while (len > 0) {
|
||||
size_t n = len < MGI_TXBUF_LEN ? len : MGI_TXBUF_LEN;
|
||||
uint64_t expire = mg_millis() + 100;
|
||||
struct mg_rw612_imumc im;
|
||||
uint32_t i;
|
||||
while (s->free_bufs == 0) {
|
||||
mg_rw612_poll_imu();
|
||||
if (mg_millis() > expire) return false;
|
||||
}
|
||||
for (i = 0; (s->free_bufs & MG_BIT(i)) == 0; i++) (void) 0;
|
||||
s->free_bufs &= (uint16_t) ~MG_BIT(i);
|
||||
memset(&im, 0, sizeof(im));
|
||||
im.src = MGI_EPT_LOCAL, im.dst = MGI_EPT_REMOTE, im.len = (uint16_t) n;
|
||||
im.data = MGI_TXBUF + i * MGI_TXBUF_LEN;
|
||||
memcpy((void *) (uintptr_t) im.data, p, n);
|
||||
mg_rw612_imu_send(MGI_MSG_IMUMC, 0, (uint32_t *) &im, sizeof(im) / 4);
|
||||
p += n, len -= n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Send a Spinel frame, HDLC encoded, with a new transaction ID. Its
|
||||
// response sets rsp, see mg_rw612_spinel()
|
||||
static bool mg_rw612_post(uint8_t *f, size_t len) {
|
||||
struct mg_rw612 *s = &s_rw612;
|
||||
uint8_t buf[2 * 160 + 6];
|
||||
uint16_t fcs = 0xffff;
|
||||
size_t i, n = 0;
|
||||
s->tid = (uint8_t) (s->tid % 15 + 1);
|
||||
f[0] = (uint8_t) (0x80 | s->tid); // Header: flag, interface 0, TID
|
||||
s->rsp = false;
|
||||
buf[n++] = 0x7e;
|
||||
for (i = 0; i < len + 2 && n + 3 < sizeof(buf); i++) {
|
||||
uint8_t c;
|
||||
if (i < len) {
|
||||
c = f[i], fcs = mg_rw612_fcs(fcs, c);
|
||||
} else {
|
||||
c = (uint8_t) (i == len ? ~fcs & 0xff : ~fcs >> 8);
|
||||
}
|
||||
if (c == 0x7e || c == 0x7d || c == 0x11 || c == 0x13 || c == 0xf8) {
|
||||
buf[n++] = 0x7d, c ^= 0x20;
|
||||
}
|
||||
buf[n++] = c;
|
||||
}
|
||||
buf[n++] = 0x7e;
|
||||
return mg_rw612_imumc_send(buf, n);
|
||||
}
|
||||
|
||||
// Send a Spinel frame and wait for its response. Return false on timeout
|
||||
static bool mg_rw612_request(uint8_t *f, size_t len, uint32_t timeout_ms) {
|
||||
uint64_t expire = mg_millis() + timeout_ms;
|
||||
if (!mg_rw612_post(f, len)) return false;
|
||||
while (!s_rw612.rsp && mg_millis() < expire) mg_rw612_poll_imu();
|
||||
return s_rw612.rsp;
|
||||
}
|
||||
|
||||
static bool mg_rw612_set(uint32_t prop, const uint8_t *val, size_t len) {
|
||||
uint8_t f[16];
|
||||
if (len > sizeof(f) - 3) return false;
|
||||
f[1] = MGI_CMD_SET, f[2] = (uint8_t) prop; // Properties used are < 0x80
|
||||
memcpy(f + 3, val, len);
|
||||
return mg_rw612_request(f, len + 3, MGI_RSP_TIMEOUT_MS) &&
|
||||
s_rw612.rsp_status == 0;
|
||||
}
|
||||
|
||||
// Log an init error, return false
|
||||
static bool mg_rw612_fail(const char *msg) {
|
||||
MG_ERROR(("%s", msg));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Start one raw transmission. The RCP adds the FCS and reports completion.
|
||||
static size_t mgi_rw612w_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||
size_t len) {
|
||||
uint8_t f[3 + 2 + 127 + 4];
|
||||
size_t n = 3;
|
||||
if (len > 125 || s_rw612.txing) return 0;
|
||||
f[1] = MGI_CMD_SET, f[2] = MGI_PROP_STREAM_RAW;
|
||||
f[n++] = (uint8_t) (len + 2), f[n++] = 0; // PSDU length, incl. FCS
|
||||
memcpy(f + n, buf, len), n += len;
|
||||
f[n++] = 0, f[n++] = 0; // FCS, filled in by the radio
|
||||
(void) ifp;
|
||||
f[n++] = MG_802154_CHANNEL;
|
||||
f[n++] = 0; // Max CSMA backoffs
|
||||
f[n++] = 0; // Max frame retries
|
||||
f[n++] = 0; // CSMA disabled for scheduled turns
|
||||
if (!mg_rw612_post(f, n)) return 0;
|
||||
s_rw612.txing = true, s_rw612.txstart = mg_millis();
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool mgi_rw612w_radio_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_rw612 *s = &s_rw612;
|
||||
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
uint8_t f[8], one = 1, zero = 0, pan[2], addr[2], channel = MG_802154_CHANNEL;
|
||||
uint64_t expire;
|
||||
if (MGI_MBOX->magic[0] != MGI_SYNC_MAGIC) {
|
||||
MG_ERROR(("Radio firmware is not running on CPU2"));
|
||||
return false;
|
||||
}
|
||||
memset(s, 0, sizeof(*s));
|
||||
s->free_bufs = 0xffff, s->ifp = ifp;
|
||||
// Reset our send FIFO, drop stale messages. See IMU_Init()
|
||||
MGI_IMU->CNTL |= MGI_IMU_FLUSH;
|
||||
MGI_IMU->CNTL &= ~(MGI_IMU_FLUSH | MGI_IMU_WAIT_FOR_ACK);
|
||||
MGI_IMU->CNTL =
|
||||
(MGI_IMU->CNTL & ~MGI_IMU_WATERMARK_MASK) | MGI_IMU_WATERMARK(15);
|
||||
while ((MGI_IMU_PEER->STATUS & MGI_IMU_EMPTY) == 0) (void) MGI_IMU->RD_MSG;
|
||||
// Synchronise the IMU link, then connect the IMUMC endpoints
|
||||
MGI_MBOX->magic[1] = MGI_SYNC_MAGIC;
|
||||
mg_rw612_control(MGI_CTL_SYNC, 0, 0, 0);
|
||||
expire = mg_millis() + 1000;
|
||||
while (!s->up && mg_millis() < expire) mg_rw612_poll_imu();
|
||||
if (!s->up) return mg_rw612_fail("IMU sync timeout");
|
||||
expire = mg_millis() + 2000;
|
||||
while (!s->ept_ready && mg_millis() < expire) {
|
||||
uint64_t retry = mg_millis() + 100;
|
||||
mg_rw612_control(MGI_CTL_EPT_QUERY, MGI_EPT_REMOTE, 0, 1);
|
||||
while (!s->ept_ready && mg_millis() < retry) mg_rw612_poll_imu();
|
||||
}
|
||||
if (!s->ept_ready) return mg_rw612_fail("IMUMC endpoint timeout");
|
||||
// Reset the RCP stack, wait for its reset status, read its version
|
||||
f[1] = MGI_CMD_RESET, f[2] = MGI_RESET_STACK;
|
||||
mg_rw612_request(f, 3, MGI_RSP_TIMEOUT_MS);
|
||||
expire = mg_millis() + 1000;
|
||||
while (!s->reset && mg_millis() < expire) mg_rw612_poll_imu();
|
||||
if (!s->reset) return mg_rw612_fail("RCP reset timeout");
|
||||
f[1] = MGI_CMD_GET, f[2] = MGI_PROP_NCP_VERSION;
|
||||
mg_rw612_request(f, 3, MGI_RSP_TIMEOUT_MS);
|
||||
MG_INFO(("RCP: %s", s->version));
|
||||
if (!mg_rw612_set(MGI_PROP_PHY_ENABLED, &one, 1)) {
|
||||
return mg_rw612_fail("PHY enable failed");
|
||||
}
|
||||
pan[0] = (uint8_t) MG_802154_PAN_ID;
|
||||
pan[1] = (uint8_t) (MG_802154_PAN_ID >> 8);
|
||||
addr[0] = (uint8_t) node->radio_address;
|
||||
addr[1] = (uint8_t) (node->radio_address >> 8);
|
||||
if (!mg_rw612_set(MGI_PROP_PHY_CHAN, &channel, 1) ||
|
||||
!mg_rw612_set(MGI_PROP_PANID, pan, 2) ||
|
||||
!mg_rw612_set(MGI_PROP_SADDR, addr, 2) ||
|
||||
!mg_rw612_set(MGI_PROP_PROMISCUOUS, &zero, 1)) {
|
||||
return mg_rw612_fail("Channel, PAN ID or address set failed");
|
||||
}
|
||||
if (!mg_rw612_set(MGI_PROP_RAW_STREAM, &one, 1)) {
|
||||
return mg_rw612_fail("Raw stream enable failed");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t mgi_rw612w_tx(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
||||
return data->mesh->strategy->tx(data->mesh, buf, len);
|
||||
}
|
||||
|
||||
static size_t mgi_rw612w_rx(void *buf, size_t cap, struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_rw612w_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_rw612w_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
||||
struct mg_mesh_node *node = data->mesh;
|
||||
uint64_t now = mg_micros();
|
||||
(void) s1;
|
||||
bool was_sending = s_rw612.txing;
|
||||
mg_rw612_poll_imu();
|
||||
if (s_rw612.txing &&
|
||||
(s_rw612.rsp || mg_millis() > s_rw612.txstart + MGI_TX_TIMEOUT_MS)) {
|
||||
if (!s_rw612.rsp) MG_ERROR(("TX timeout"));
|
||||
s_rw612.txing = false;
|
||||
}
|
||||
if (was_sending && !s_rw612.txing)
|
||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
||||
node->now = now;
|
||||
mg_mesh_rx_drain(node, &s_rw612w_rx);
|
||||
node->strategy->tick(node, now);
|
||||
return node->joined;
|
||||
}
|
||||
|
||||
static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
|
||||
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
||||
const struct mg_mesh *strategy = data->strategy;
|
||||
uint8_t uid[FSL_OCOTP_UID_LENGTH] = {0}, digest[32];
|
||||
uint32_t uid_len = sizeof(uid);
|
||||
uint16_t address;
|
||||
OCOTP_ReadUniqueID(uid, &uid_len);
|
||||
mg_sha256(digest, uid, sizeof(uid));
|
||||
memcpy(ifp->mac, digest, 6);
|
||||
ifp->mac[0] = 0x02;
|
||||
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_rw612w_radio_tx;
|
||||
mg_queue_init(&s_rw612w_rx, s_rw612w_queue, sizeof(s_rw612w_queue));
|
||||
if (mgi_rw612w_radio_init(ifp)) return true;
|
||||
strategy->free(data->mesh);
|
||||
mg_free(data->mesh);
|
||||
data->mesh = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mg_tcpip_driver mg_tcpip_driver_rw612w = {
|
||||
mgi_rw612w_init, mgi_rw612w_tx, mgi_rw612w_rx, mgi_rw612w_poll};
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#pragma once
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612W) && \
|
||||
MG_ENABLE_DRIVER_RW612W
|
||||
|
||||
struct mg_tcpip_driver_rw612w_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_rw612w;
|
||||
|
||||
#define MG_TCPIP_DRIVER_RW612W_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_rw612w_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_.driver = &mg_tcpip_driver_rw612w; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RW612W_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_SAME54) && \
|
||||
MG_ENABLE_DRIVER_SAME54
|
||||
|
||||
@@ -16,7 +16,9 @@ struct mg_tcpip_driver_same54_data {
|
||||
uint8_t phy_addr;
|
||||
};
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_same54;
|
||||
|
||||
#define MG_TCPIP_DRIVER_SAME54_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_same54_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -28,8 +30,12 @@ struct mg_tcpip_driver_same54_data {
|
||||
mif_.driver = &mg_tcpip_driver_same54; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: same54, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_SAME54_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "log.h"
|
||||
#include "sdio.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && \
|
||||
(defined(MG_ENABLE_DRIVER_CYW_SDIO) && MG_ENABLE_DRIVER_CYW_SDIO)
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#include "st67w6.h"
|
||||
#include "net_builtin.h"
|
||||
#include "net.h"
|
||||
#include "printf.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ST67W6) && \
|
||||
MG_ENABLE_DRIVER_ST67W6
|
||||
|
||||
bool mg_to_size_t(struct mg_str str, size_t *val);
|
||||
|
||||
static struct mg_tcpip_if *s_ifp;
|
||||
static uint32_t s_ip, s_mask;
|
||||
static bool s_link = false, s_connecting = false;
|
||||
@@ -344,7 +348,6 @@ static bool st67w6_at_cmd(char *cmd, size_t len) {
|
||||
}
|
||||
|
||||
static bool st67w6_spi_init(void);
|
||||
bool mg_to_size_t(struct mg_str str, size_t *val);
|
||||
|
||||
static bool st67w6_init(uint8_t *mac) {
|
||||
// struct mg_tcpip_driver_st67w6_data *d = (struct
|
||||
@@ -365,7 +368,7 @@ static bool st67w6_init(uint8_t *mac) {
|
||||
}
|
||||
// set clock, who cares ???
|
||||
if (!st67w6_at_cmd("AT+GET_CLOCK\r\n", 14)) return false;
|
||||
MG_DEBUG(("%.*s", s_at_resp_len, at_resp)); // TODO(scaprile): --> VERBOSE
|
||||
MG_VERBOSE(("%.*s", s_at_resp_len, at_resp));
|
||||
// BT-ENABLED DEPENDENCY
|
||||
|
||||
// MODULE DEPENDENCY
|
||||
|
||||
+10
-2
@@ -3,6 +3,8 @@
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ST67W6) && \
|
||||
MG_ENABLE_DRIVER_ST67W6
|
||||
|
||||
#include "wifi.h"
|
||||
|
||||
struct mg_tcpip_driver_st67w6_data {
|
||||
struct mg_wifi_data wifi;
|
||||
void *spi;
|
||||
@@ -10,7 +12,9 @@ struct mg_tcpip_driver_st67w6_data {
|
||||
struct mg_queue send_queue; // decouple tx calls from module polls
|
||||
};
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_st67w6;
|
||||
|
||||
#define MG_TCPIP_DRIVER_ST67W6_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_st67w6_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -22,8 +26,12 @@ struct mg_tcpip_driver_st67w6_data {
|
||||
mif_.driver_data = &driver_data_; \
|
||||
mif_.recv_queue.size = 8192; \
|
||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: st67w6, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ST67W6_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_STM32F) && \
|
||||
MG_ENABLE_DRIVER_STM32F
|
||||
|
||||
@@ -34,7 +34,9 @@ struct mg_tcpip_driver_stm32f_data {
|
||||
#define MG_ENABLE_ETH_IRQ()
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
|
||||
|
||||
#define MG_TCPIP_DRIVER_STM32F_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_stm32f_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -47,8 +49,12 @@ struct mg_tcpip_driver_stm32f_data {
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
MG_ENABLE_ETH_IRQ(); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32F_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,10 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && (MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || \
|
||||
MG_ENABLE_DRIVER_STM32N)
|
||||
// STM32C: vendor modded single-queue Synopsys v5.4, same settings as STM32H
|
||||
// STM32H: vendor modded single-queue Synopsys v4.2
|
||||
// STM32N: dual-queue GbE Synopsys v5.2 with no hash table option, 64-bit AXI
|
||||
// MCXNx4x: dual-queue Synopsys v5.2 with no hash table option
|
||||
@@ -310,7 +313,7 @@ static uint32_t s_rxno;
|
||||
#if MG_ENABLE_DRIVER_MCXN
|
||||
void ETHERNET_IRQHandler(void);
|
||||
void ETHERNET_IRQHandler(void) {
|
||||
#elif MG_ENABLE_DRIVER_STM32H
|
||||
#elif MG_ENABLE_DRIVER_STM32H && !MG_ENABLE_DRIVER_STM32C
|
||||
void ETH_IRQHandler(void);
|
||||
void ETH_IRQHandler(void) {
|
||||
#else
|
||||
@@ -339,8 +342,10 @@ void ETH1_IRQHandler(void) {
|
||||
}
|
||||
ETH->DMACSR =
|
||||
MG_BIT(7) | MG_BIT(8); // Clear possible RBU RPS while processing
|
||||
ETH->DMACRDTPR =
|
||||
(uint32_t) (uintptr_t) &s_rxdesc[ETH_DESC_CNT - 1]; // and resume RX
|
||||
// Resume RX: tail is the last descriptor given back to DMA. It must differ
|
||||
// from the DMA current descriptor, or some DMAs, e.g. STM32C5, stay suspended
|
||||
ETH->DMACRDTPR = (uint32_t) (uintptr_t) &s_rxdesc[(s_rxno + ETH_DESC_CNT - 1) %
|
||||
ETH_DESC_CNT];
|
||||
}
|
||||
|
||||
struct mg_tcpip_driver mg_tcpip_driver_stm32h = {
|
||||
|
||||
+21
-4
@@ -4,6 +4,13 @@
|
||||
#if !defined(MG_ENABLE_DRIVER_STM32H)
|
||||
#define MG_ENABLE_DRIVER_STM32H 0
|
||||
#endif
|
||||
#if !defined(MG_ENABLE_DRIVER_STM32C)
|
||||
#define MG_ENABLE_DRIVER_STM32C 0
|
||||
#endif
|
||||
#if MG_ENABLE_DRIVER_STM32C // STM32C5: STM32H5 MAC, the IRQ is ETH1
|
||||
#undef MG_ENABLE_DRIVER_STM32H
|
||||
#define MG_ENABLE_DRIVER_STM32H 1
|
||||
#endif
|
||||
#if !defined(MG_ENABLE_DRIVER_MCXN)
|
||||
#define MG_ENABLE_DRIVER_MCXN 0
|
||||
#endif
|
||||
@@ -12,6 +19,8 @@
|
||||
#endif
|
||||
#if MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || MG_ENABLE_DRIVER_STM32N
|
||||
|
||||
#include "phy.h"
|
||||
|
||||
struct mg_tcpip_driver_stm32h_data {
|
||||
// MDC clock divider. MDC clock is derived from HCLK, must not exceed 2.5MHz
|
||||
// HCLK range DIVIDER mdc_cr VALUE
|
||||
@@ -43,9 +52,11 @@ struct mg_tcpip_driver_stm32h_data {
|
||||
#define MG_DRIVER_MDC_CR 4
|
||||
#endif
|
||||
|
||||
#if MG_ENABLE_DRIVER_STM32H && MG_ARCH == MG_ARCH_CUBE && !defined(MG_ENABLE_ETH_IRQ)
|
||||
#if MG_ENABLE_DRIVER_STM32H && !MG_ENABLE_DRIVER_STM32C && \
|
||||
MG_ARCH == MG_ARCH_CUBE && !defined(MG_ENABLE_ETH_IRQ)
|
||||
#define MG_ENABLE_ETH_IRQ() NVIC_EnableIRQ(ETH_IRQn)
|
||||
#elif MG_ENABLE_DRIVER_STM32N && MG_ARCH == MG_ARCH_CUBE && !defined(MG_ENABLE_ETH_IRQ)
|
||||
#elif (MG_ENABLE_DRIVER_STM32N || MG_ENABLE_DRIVER_STM32C) && \
|
||||
MG_ARCH == MG_ARCH_CUBE && !defined(MG_ENABLE_ETH_IRQ)
|
||||
#define MG_ENABLE_ETH_IRQ() NVIC_EnableIRQ(ETH1_IRQn)
|
||||
#elif !defined(MG_ENABLE_ETH_IRQ)
|
||||
#define MG_ENABLE_ETH_IRQ()
|
||||
@@ -63,7 +74,9 @@ struct mg_tcpip_driver_stm32h_data {
|
||||
#define MG_IPV6_INIT(mif)
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
|
||||
|
||||
#define MG_TCPIP_DRIVER_STM32H_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_stm32h_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -77,10 +90,14 @@ struct mg_tcpip_driver_stm32h_data {
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
MG_IPV6_INIT(mif_); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_ENABLE_ETH_IRQ(); \
|
||||
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32H_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_TM4C) && MG_ENABLE_DRIVER_TM4C
|
||||
struct tm4c_emac {
|
||||
|
||||
+8
-2
@@ -19,7 +19,9 @@ struct mg_tcpip_driver_tm4c_data {
|
||||
#define MG_DRIVER_MDC_CR 1
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
|
||||
|
||||
#define MG_TCPIP_DRIVER_TM4C_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_tm4c_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -30,8 +32,12 @@ struct mg_tcpip_driver_tm4c_data {
|
||||
mif_.driver = &mg_tcpip_driver_tm4c; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: tm4c, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TM4C_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_TMS570) && MG_ENABLE_DRIVER_TMS570
|
||||
struct tms570_emac_ctrl {
|
||||
|
||||
@@ -14,7 +14,9 @@ struct mg_tcpip_driver_tms570_data {
|
||||
#define MG_DRIVER_MDC_CR 1
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_tms570;
|
||||
|
||||
#define MG_TCPIP_DRIVER_TMS570_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_tms570_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -26,8 +28,13 @@ struct mg_tcpip_driver_tms570_data {
|
||||
mif_.driver = &mg_tcpip_driver_tms570; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: tms570, MAC: %M", mg_print_mac, mif_.mac));\
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TMS570_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5100) && \
|
||||
MG_ENABLE_DRIVER_W5100
|
||||
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_w5100;
|
||||
|
||||
// Initialises W5100 on mgr. The application defines
|
||||
// struct mg_tcpip_spi mg_spi
|
||||
#define MG_TCPIP_DRIVER_W5100_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_w5100; \
|
||||
mif_.driver_data = &mg_spi; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: w5100, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_W5100_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && \
|
||||
MG_ENABLE_DRIVER_W5500
|
||||
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
|
||||
|
||||
// Initialises W5500 on mgr. The application defines
|
||||
// struct mg_tcpip_spi mg_spi. Example: tutorials/rp/w5500-evb-pico
|
||||
#define MG_TCPIP_DRIVER_W5500_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
mif_.ip = MG_TCPIP_IP; \
|
||||
mif_.mask = MG_TCPIP_MASK; \
|
||||
mif_.gw = MG_TCPIP_GW; \
|
||||
mif_.driver = &mg_tcpip_driver_w5500; \
|
||||
mif_.driver_data = &mg_spi; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: w5500, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_W5500_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC) && MG_ENABLE_DRIVER_XMC
|
||||
|
||||
|
||||
+8
-2
@@ -28,7 +28,9 @@ struct mg_tcpip_driver_xmc_data {
|
||||
#define MG_DRIVER_MDC_CR 4
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_xmc;
|
||||
|
||||
#define MG_TCPIP_DRIVER_XMC_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_xmc_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -40,8 +42,12 @@ struct mg_tcpip_driver_xmc_data {
|
||||
mif_.driver = &mg_tcpip_driver_xmc; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,7 @@
|
||||
#include "net_builtin.h"
|
||||
#include "log.h"
|
||||
#include "phy.h"
|
||||
#include "util.h"
|
||||
|
||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
|
||||
|
||||
|
||||
+8
-2
@@ -15,7 +15,9 @@ struct mg_tcpip_driver_xmc7_data {
|
||||
#define MG_DRIVER_MDC_CR 3
|
||||
#endif
|
||||
|
||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
||||
extern struct mg_tcpip_driver mg_tcpip_driver_xmc7;
|
||||
|
||||
#define MG_TCPIP_DRIVER_XMC7_INIT(mgr) \
|
||||
do { \
|
||||
static struct mg_tcpip_driver_xmc7_data driver_data_; \
|
||||
static struct mg_tcpip_if mif_; \
|
||||
@@ -27,9 +29,13 @@ struct mg_tcpip_driver_xmc7_data {
|
||||
mif_.driver = &mg_tcpip_driver_xmc7; \
|
||||
mif_.driver_data = &driver_data_; \
|
||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||
mg_tcpip_init(mgr, &mif_); \
|
||||
mg_tcpip_init((mgr), &mif_); \
|
||||
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||
} while (0)
|
||||
|
||||
#ifndef MG_TCPIP_DRIVER_INIT
|
||||
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC7_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
+7
-4
@@ -2,9 +2,10 @@
|
||||
#include "flash.h"
|
||||
#include "log.h"
|
||||
#include "ota.h"
|
||||
#include "util.h"
|
||||
#ifdef MG_OTA_PUBLIC_KEY
|
||||
#include "sha256.h"
|
||||
#include "tls_uecc.h"
|
||||
#include "tls_p256.h"
|
||||
#endif
|
||||
|
||||
#if MG_OTA != MG_OTA_NONE && MG_OTA != MG_OTA_CUSTOM
|
||||
@@ -37,6 +38,9 @@ bool mg_ota_flash_write(const void *buf, size_t len, struct mg_flash *flash) {
|
||||
bool ok = false;
|
||||
if (s_size == 0) {
|
||||
MG_ERROR(("OTA is not started, call mg_ota_begin()"));
|
||||
} else if (s_addr + MG_ROUND_UP(len, flash->align) >
|
||||
(char *) flash->start + flash->size) {
|
||||
MG_ERROR(("Flash overflow: attempting to write past the flash boundary"));
|
||||
} else {
|
||||
size_t len_aligned_down = MG_ROUND_DOWN(len, flash->align);
|
||||
if (len_aligned_down) ok = flash->write_fn(s_addr, buf, len_aligned_down);
|
||||
@@ -73,9 +77,8 @@ bool mg_ota_flash_end(struct mg_flash *flash) {
|
||||
uint8_t hash[32];
|
||||
size_t fw_size = s_size - 68; // strip 64-byte sig + 4-byte magic
|
||||
mg_sha256(hash, (uint8_t *) base, fw_size);
|
||||
ok = mg_uecc_verify(s_pubkey, hash, sizeof(hash),
|
||||
(uint8_t *) base + fw_size,
|
||||
mg_uecc_secp256r1()) == 1;
|
||||
ok = mg_p256_verify(s_pubkey, hash, sizeof(hash),
|
||||
(uint8_t *) base + fw_size) == 1;
|
||||
MG_INFO(("Signature: %s", ok ? "ok" : "fail"));
|
||||
} else {
|
||||
ok = false;
|
||||
|
||||
@@ -173,6 +173,7 @@ size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt,
|
||||
is_long++, c = fmt[++i];
|
||||
if (c == 'l') is_long++, c = fmt[++i];
|
||||
}
|
||||
if (c == 'z') is_long = 1, c = fmt[++i];
|
||||
if (c == 'p') x = 1, is_long = 1;
|
||||
if (c == 'd' || c == 'u' || c == 'x' || c == 'X' || c == 'p' ||
|
||||
c == 'g' || c == 'f') {
|
||||
@@ -213,7 +214,12 @@ size_t mg_vxprintf(void (*out)(char, void *), void *param, const char *fmt,
|
||||
n++;
|
||||
} else if (c == 's') {
|
||||
char *p = va_arg(*ap, char *);
|
||||
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
||||
if (p != NULL && pr != ~0U) {
|
||||
for (j = 0; j < pr && p[j] != '\0'; j++) ; // portable strnlen()
|
||||
pr = j;
|
||||
} else {
|
||||
pr = p == NULL ? 0 : strlen(p);
|
||||
}
|
||||
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
||||
n += scpy(out, param, &pad, 1);
|
||||
n += scpy(out, param, p, pr);
|
||||
|
||||
@@ -34,8 +34,8 @@ typedef size_t (*mg_pm_t)(mg_pfn_t fn, void *arg, va_list *);
|
||||
// - mg_print_base64 - prints a buffer as a base64-encoded string
|
||||
// - mg_print_esc - prints a JSON-escaped string
|
||||
// - mg_print_hex - prints a buffer as a hex string
|
||||
// - mg_print_ip - prints an IP address in a struct mg_str
|
||||
// - mg_print_ip_port - prints IP address and port in a struct mg_str
|
||||
// - mg_print_ip - prints an IP address in a struct mg_addr
|
||||
// - mg_print_ip_port - prints IP address and port in a struct mg_addr
|
||||
// - mg_print_ip4 - prints an IPv4 address
|
||||
// - mg_print_ip6 - prints an IPv6 address
|
||||
// - mg_print_mac - prints a MAC address
|
||||
|
||||
Loaded 100 of 3130 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user