Skip to content

Commit 46bceb3

Browse files
committed
Peer review and demo fixes. Fix for tcp_ack() issue and t->S not getting reset.
1 parent a484a1c commit 46bceb3

15 files changed

Lines changed: 267 additions & 91 deletions

File tree

.github/workflows/stm32h563-m33mu.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
}
4747
trap cleanup EXIT
4848
49+
sudo modprobe tun
4950
sudo ip tuntap add dev tap0 mode tap
5051
sudo ip addr add 192.168.12.1/24 dev tap0
5152
sudo ip link set tap0 up
@@ -120,13 +121,13 @@ jobs:
120121
steps:
121122
- uses: actions/checkout@v4
122123

123-
- name: Clone wolfSSL, wolfSSH, wolfMQTT
124+
- name: Clone wolfSSL, wolfSSH, wolfMQTT (pinned to stable tags)
124125
run: |
125126
set -euo pipefail
126127
cd ..
127128
git clone --depth 1 https://github.com/wolfSSL/wolfssl.git
128129
git clone --depth 1 https://github.com/wolfSSL/wolfssh.git
129-
git clone --depth 1 https://github.com/wolfSSL/wolfMQTT.git wolfmqtt
130+
git clone --depth 1 https://github.com/wolfSSL/wolfmqtt.git
130131
131132
- name: Install host tools
132133
run: |
@@ -139,6 +140,7 @@ jobs:
139140
run: |
140141
set -euo pipefail
141142
make -C src/port/stm32h563 \
143+
WOLFSSL_ROOT=../../../../wolfssl \
142144
ENABLE_HTTPS=1 ENABLE_MQTT_BROKER=1 ENABLE_SSH=1 \
143145
CC=arm-none-eabi-gcc OBJCOPY=arm-none-eabi-objcopy
144146
@@ -160,6 +162,7 @@ jobs:
160162
}
161163
trap cleanup EXIT
162164
165+
sudo modprobe tun
163166
sudo ip tuntap add dev tap0 mode tap
164167
sudo ip addr add 192.168.12.1/24 dev tap0
165168
sudo ip link set tap0 up

src/port/stm32h563/Makefile

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ ENABLE_SSH ?= 0
2727
# Automatically enables TLS if needed
2828
ENABLE_MQTT ?= 0
2929

30+
# MQTT Broker: set ENABLE_MQTT_BROKER=1 to include wolfMQTT broker (requires TLS)
31+
ENABLE_MQTT_BROKER ?= 0
32+
3033
# Auto-enable TLS when any feature that requires it is enabled
3134
ifeq ($(ENABLE_TLS_CLIENT),1)
3235
ENABLE_TLS = 1
@@ -41,11 +44,8 @@ ifeq ($(ENABLE_MQTT),1)
4144
ENABLE_TLS = 1
4245
endif
4346

44-
# MQTT Broker: set ENABLE_MQTT_BROKER=1 to include wolfMQTT broker (requires TLS)
45-
ENABLE_MQTT_BROKER ?= 0
46-
4747
# Library paths - default to sibling directories (clone alongside pattern)
48-
WOLFSSL_ROOT ?= $(ROOT)/../wolfssl
48+
WOLFSSL_ROOT ?= $(ROOT)/../wolfssl-alt
4949
WOLFSSH_ROOT ?= $(ROOT)/../wolfssh
5050
WOLFMQTT_ROOT ?= $(ROOT)/../wolfmqtt
5151

@@ -91,12 +91,14 @@ CFLAGS += -I$(WOLFSSL_ROOT)
9191

9292
# TLS server, client and wolfIP-wolfSSL glue
9393
SRCS += tls_server.c
94+
SRCS += $(ROOT)/src/port/wolfssl_io.c
95+
9496
# TLS client (Google test)
9597
ifeq ($(ENABLE_TLS_CLIENT),1)
9698
CFLAGS += -DENABLE_TLS_CLIENT
9799
SRCS += tls_client.c
98100
endif
99-
SRCS += $(ROOT)/src/port/wolfssl_io.c
101+
100102

101103
# HTTPS web server - uses existing wolfIP httpd
102104
ifeq ($(ENABLE_HTTPS),1)
@@ -122,28 +124,26 @@ WOLFSSL_SRCS := \
122124
$(WOLFSSL_ROOT)/wolfcrypt/src/memory.c \
123125
$(WOLFSSL_ROOT)/wolfcrypt/src/wolfmath.c \
124126
$(WOLFSSL_ROOT)/wolfcrypt/src/sp_int.c \
127+
$(WOLFSSL_ROOT)/wolfcrypt/src/sp_c32.c \
128+
$(WOLFSSL_ROOT)/wolfcrypt/src/sp_cortexm.c \
125129
$(WOLFSSL_ROOT)/src/ssl.c \
126130
$(WOLFSSL_ROOT)/src/tls.c \
127131
$(WOLFSSL_ROOT)/src/tls13.c \
128132
$(WOLFSSL_ROOT)/src/internal.c \
129133
$(WOLFSSL_ROOT)/src/keys.c \
130134
$(WOLFSSL_ROOT)/src/wolfio.c
131135

132-
# ChaCha20-Poly1305 (optional, comment out to save space)
136+
# ChaCha20-Poly1305 (optional)
133137
WOLFSSL_SRCS += \
134138
$(WOLFSSL_ROOT)/wolfcrypt/src/chacha.c \
135139
$(WOLFSSL_ROOT)/wolfcrypt/src/chacha20_poly1305.c \
136140
$(WOLFSSL_ROOT)/wolfcrypt/src/poly1305.c
137141

138142
# RSA for certificate verification (most servers use RSA certs)
139143
WOLFSSL_SRCS += \
140-
$(WOLFSSL_ROOT)/wolfcrypt/src/rsa.c
144+
$(WOLFSSL_ROOT)/wolfcrypt/src/rsa.c \
145+
$(WOLFSSL_ROOT)/wolfcrypt/src/signature.c
141146

142-
# Signature verification (required for wolfSSH)
143-
ifeq ($(ENABLE_SSH),1)
144-
WOLFSSL_SRCS += \
145-
$(WOLFSSL_ROOT)/wolfcrypt/src/signature.c
146-
endif
147147

148148
SRCS += $(WOLFSSL_SRCS)
149149

src/port/stm32h563/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060

6161
#if WOLFIP_ENABLE_DHCP
6262
#define DHCP
63+
/* Reduce DHCP retries for faster fallback to static IP on demo boards */
64+
#define DHCP_DISCOVER_RETRIES 1
65+
#define DHCP_REQUEST_RETRIES 1
6366
#endif
6467

6568
#endif /* WOLF_CONFIG_H */

src/port/stm32h563/demo.sh

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@
55
# Demonstrates HTTPS server, SSH server, and MQTT broker running on
66
# a bare-metal Cortex-M33 with wolfIP + wolfSSL + wolfSSH + wolfMQTT.
77
#
8-
# Usage: ./demo.sh [board-ip]
8+
# Usage: ./demo.sh [--auto] [board-ip]
9+
# --auto Skip pauses and interactive prompts (for automated testing)
910
# board-ip defaults to 192.168.12.11
1011
#
1112

12-
BOARD_IP="${1:-1192.168.12.11}"
13+
AUTO=0
14+
if [[ "$1" == "--auto" ]]; then
15+
AUTO=1
16+
shift
17+
fi
18+
BOARD_IP="${1:-192.168.12.11}"
19+
20+
# Validate BOARD_IP to block shell metacharacter injection via eval
21+
if ! [[ "$BOARD_IP" =~ ^[A-Za-z0-9._-]+$ ]]; then
22+
echo "Error: Invalid board IP/hostname: $BOARD_IP" >&2
23+
exit 1
24+
fi
1325

1426
# Colors
1527
BLD='\033[1m'
@@ -37,7 +49,7 @@ BAMCA0gAMEUCIEUB8ArsbYI58PGtcy9KIdR6A3z5KCQblTXZWnIE7EDUAiEA8Oyi
3749
LwVAHQ4M2+TcVwe4LQ+xG9F6uSmu4t/psG0IT+s=
3850
-----END CERTIFICATE-----
3951
CERTEOF
40-
trap "rm -f $CERT_FILE" EXIT
52+
trap "rm -f $CERT_FILE /tmp/wolfip_sub.*" EXIT
4153

4254
banner() {
4355
echo ""
@@ -56,6 +68,10 @@ cmd_show() {
5668
}
5769

5870
pause() {
71+
if [[ $AUTO -eq 1 ]]; then
72+
sleep 1
73+
return
74+
fi
5975
echo ""
6076
echo -ne " ${DIM}[Press Enter to continue]${RST}"
6177
read -r
@@ -106,7 +122,7 @@ pause
106122
banner "2. TCP Echo Server (Port 7)"
107123

108124
step "Send a message to the plaintext echo server"
109-
run_cmd "echo 'Hello wolfIP!' | nc -q 1 ${BOARD_IP} 7"
125+
run_cmd "echo 'Hello wolfIP!' | nc -w 2 ${BOARD_IP} 7"
110126

111127
pause
112128

@@ -115,16 +131,22 @@ pause
115131
# ---------------------------------------------------------------------------
116132
banner "3. HTTPS Web Server (Port 443) - TLS 1.3"
117133

118-
step "Fetch the status page with curl"
119-
run_cmd "curl -s -k https://${BOARD_IP}/ | sed 's/<[^>]*>//g; s/^[[:space:]]*//; /^$/d'"
120-
121-
echo ""
122-
step "Inspect the TLS 1.3 handshake"
123-
cmd_show "echo | openssl s_client -connect ${BOARD_IP}:443 -tls1_3 -brief 2>&1"
134+
step "Fetch the status page and inspect TLS 1.3 handshake"
135+
cmd_show "curl -vsk --max-time 10 https://${BOARD_IP}/"
124136
echo ""
125-
echo | openssl s_client -connect "${BOARD_IP}":443 -tls1_3 -brief 2>&1 | \
126-
grep -E '(Protocol|Ciphersuite|Peer certificate|Server certificate|subject|issuer|Verification)' | \
127-
sed 's/^/ /'
137+
CURL_OUT=$(curl -vsk --max-time 10 "https://${BOARD_IP}/" 2>&1)
138+
RC=$?
139+
if [[ $RC -ne 0 && -z "$CURL_OUT" ]]; then
140+
echo -e " ${RED}Connection failed (curl exit $RC)${RST}"
141+
else
142+
# Show TLS handshake details (lines starting with "* ")
143+
echo "$CURL_OUT" | grep -E '^\* +(SSL|Server cert|subject|issuer|start date|expire)' | sed 's/^/ /'
144+
echo ""
145+
# Show page content (lines not starting with *, >, <space, or <header)
146+
echo "$CURL_OUT" | grep -v '^[*><{} ]' | \
147+
sed 's/<\/\(tr\|h1\|title\)>/\n/g; s/<[^>]*>//g; s/^[[:space:]]*//; /^$/d' | \
148+
sed 's/^/ /'
149+
fi
128150
echo ""
129151

130152
pause
@@ -138,8 +160,12 @@ step "Connect and run commands (admin/wolfip)"
138160
echo -e " ${DIM}NOTE: This opens an interactive SSH session.${RST}"
139161
echo -e " ${DIM}Try: help, info, uptime, then exit${RST}"
140162
echo ""
141-
echo -ne " ${YLW}>>>${RST} Open SSH session? ${DIM}[Enter=yes, s=skip]${RST} "
142-
read -r ssh_choice
163+
if [[ $AUTO -eq 1 ]]; then
164+
ssh_choice="s"
165+
else
166+
echo -ne " ${YLW}>>>${RST} Open SSH session? ${DIM}[Enter=yes, s=skip]${RST} "
167+
read -r ssh_choice
168+
fi
143169
if [[ "$ssh_choice" != "s" ]]; then
144170
cmd_show "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null admin@${BOARD_IP}"
145171
echo ""
@@ -160,9 +186,10 @@ step "Start a subscriber in the background"
160186
cmd_show "mosquitto_sub -h ${BOARD_IP} -p 8883 --cafile cert.pem --insecure -t 'demo/#' -v"
161187
echo ""
162188

189+
SUB_OUT=$(mktemp /tmp/wolfip_sub.XXXXXX)
163190
mosquitto_sub -h "${BOARD_IP}" -p 8883 \
164191
--cafile "$CERT_FILE" --insecure \
165-
-t "demo/#" -v 2>/dev/null &
192+
-t "demo/#" -v > "$SUB_OUT" 2>/dev/null &
166193
SUB_PID=$!
167194

168195
echo -e " ${DIM}Subscriber listening on demo/# (pid ${SUB_PID})${RST}"
@@ -183,11 +210,17 @@ done
183210
echo ""
184211
step "Subscriber received:"
185212
sleep 2
213+
if [[ -s "$SUB_OUT" ]]; then
214+
sed 's/^/ /' "$SUB_OUT"
215+
else
216+
echo -e " ${DIM}(no messages received)${RST}"
217+
fi
186218
echo ""
187219

188220
# Cleanup subscriber
189221
kill $SUB_PID 2>/dev/null
190222
wait $SUB_PID 2>/dev/null
223+
rm -f "$SUB_OUT"
191224

192225
pause
193226

src/port/stm32h563/main.c

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,76 @@ static int tls_client_test_started = 0;
6464
static int tls_client_test_done = 0;
6565
#endif
6666

67+
/* Forward declarations */
68+
static void uart_puts(const char *s);
69+
70+
/* =========================================================================
71+
* HardFault Handler - prints crash info via UART
72+
* ========================================================================= */
73+
#define SCB_HFSR (*(volatile uint32_t *)0xE000ED2CUL)
74+
#define SCB_CFSR (*(volatile uint32_t *)0xE000ED28UL)
75+
#define SCB_BFAR (*(volatile uint32_t *)0xE000ED38UL)
76+
#define SCB_MMFAR (*(volatile uint32_t *)0xE000ED34UL)
77+
78+
#define FAULT_USART3_ISR (*(volatile uint32_t *)(0x40004800u + 0x1Cu))
79+
#define FAULT_USART3_TDR (*(volatile uint32_t *)(0x40004800u + 0x28u))
80+
81+
static void fault_uart_putc(char c)
82+
{
83+
while ((FAULT_USART3_ISR & (1u << 7)) == 0) { }
84+
FAULT_USART3_TDR = (uint32_t)c;
85+
}
86+
static void fault_uart_puts(const char *s)
87+
{
88+
while (*s) {
89+
if (*s == '\n') fault_uart_putc('\r');
90+
fault_uart_putc(*s++);
91+
}
92+
}
93+
static void fault_uart_puthex(uint32_t val)
94+
{
95+
const char hex[] = "0123456789ABCDEF";
96+
fault_uart_puts("0x");
97+
for (int i = 28; i >= 0; i -= 4)
98+
fault_uart_putc(hex[(val >> i) & 0xF]);
99+
}
100+
101+
void hard_fault_handler_c(uint32_t *frame)
102+
{
103+
fault_uart_puts("\n\n*** HARD FAULT ***\n");
104+
fault_uart_puts(" PC: "); fault_uart_puthex(frame[6]); fault_uart_puts("\n");
105+
fault_uart_puts(" LR: "); fault_uart_puthex(frame[5]); fault_uart_puts("\n");
106+
fault_uart_puts(" R0: "); fault_uart_puthex(frame[0]); fault_uart_puts("\n");
107+
fault_uart_puts(" R1: "); fault_uart_puthex(frame[1]); fault_uart_puts("\n");
108+
fault_uart_puts(" R2: "); fault_uart_puthex(frame[2]); fault_uart_puts("\n");
109+
fault_uart_puts(" R3: "); fault_uart_puthex(frame[3]); fault_uart_puts("\n");
110+
fault_uart_puts(" R12: "); fault_uart_puthex(frame[4]); fault_uart_puts("\n");
111+
fault_uart_puts(" xPSR: "); fault_uart_puthex(frame[7]); fault_uart_puts("\n");
112+
fault_uart_puts(" HFSR: "); fault_uart_puthex(SCB_HFSR); fault_uart_puts("\n");
113+
fault_uart_puts(" CFSR: "); fault_uart_puthex(SCB_CFSR); fault_uart_puts("\n");
114+
if (SCB_CFSR & 0x00008200u) {
115+
fault_uart_puts(" BFAR: "); fault_uart_puthex(SCB_BFAR); fault_uart_puts("\n");
116+
}
117+
if (SCB_CFSR & 0x00000082u) {
118+
fault_uart_puts(" MMFAR:"); fault_uart_puthex(SCB_MMFAR); fault_uart_puts("\n");
119+
}
120+
/* Turn off LED2 (PF4) as fault indicator */
121+
(*(volatile uint32_t *)(0x42021400u + 0x18u)) = (1u << (4u + 16u));
122+
while (1) { }
123+
}
124+
125+
void HardFault_Handler(void) __attribute__((naked));
126+
void HardFault_Handler(void)
127+
{
128+
__asm volatile(
129+
"tst lr, #4 \n"
130+
"ite eq \n"
131+
"mrseq r0, msp \n"
132+
"mrsne r0, psp \n"
133+
"b hard_fault_handler_c \n"
134+
);
135+
}
136+
67137
#ifdef ENABLE_HTTPS
68138
/* HTTPS server using wolfIP httpd */
69139
static struct httpd https_server;
@@ -81,7 +151,12 @@ static int https_status_handler(struct httpd *httpd, struct http_client *hc,
81151
int len;
82152

83153
(void)httpd;
84-
(void)req;
154+
155+
uart_puts("HTTPS: ");
156+
uart_puts(req->method);
157+
uart_puts(" ");
158+
uart_puts(req->path);
159+
uart_puts("\n");
85160

86161
/* Format IP address (stored in network byte order) */
87162
{
@@ -331,12 +406,15 @@ static void uart_puts(const char *s)
331406
* Uses vsnprintf from newlib-nano + uart_puts. */
332407
void wolfmqtt_log(const char *fmt, ...)
333408
{
334-
char buf[128];
409+
char buf[256];
335410
va_list ap;
411+
int n;
336412
va_start(ap, fmt);
337-
vsnprintf(buf, sizeof(buf), fmt, ap);
413+
n = vsnprintf(buf, sizeof(buf), fmt, ap);
338414
va_end(ap);
339415
uart_puts(buf);
416+
if (n >= (int)sizeof(buf))
417+
uart_puts("...[truncated]\n");
340418
}
341419

342420
static void uart_puthex(uint32_t val)

src/port/stm32h563/mqtt_broker.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static int broker_tls_init(void)
108108
/* Load server certificate from embedded PEM */
109109
if (wolfSSL_CTX_use_certificate_buffer(ctx.ssl_ctx,
110110
(const unsigned char *)server_cert_pem,
111-
(long)server_cert_pem_len,
111+
server_cert_pem_len - 1,
112112
WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
113113
debug_print("MQTT Broker: Load cert failed\n");
114114
wolfSSL_CTX_free(ctx.ssl_ctx);
@@ -119,7 +119,7 @@ static int broker_tls_init(void)
119119
/* Load server private key from embedded PEM */
120120
if (wolfSSL_CTX_use_PrivateKey_buffer(ctx.ssl_ctx,
121121
(const unsigned char *)server_key_pem,
122-
(long)server_key_pem_len,
122+
server_key_pem_len - 1,
123123
WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
124124
debug_print("MQTT Broker: Load key failed\n");
125125
wolfSSL_CTX_free(ctx.ssl_ctx);

0 commit comments

Comments
 (0)