Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mqtt-client-C-Zephyr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode/
build/
build-*/
certs/
docs/
compile_commands.json
6 changes: 6 additions & 0 deletions mqtt-client-C-Zephyr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mqtt-client-C-Zephyr)

target_sources(app PRIVATE src/main.c)
942 changes: 942 additions & 0 deletions mqtt-client-C-Zephyr/README.md

Large diffs are not rendered by default.

Binary file added mqtt-client-C-Zephyr/assets/build-nsos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/build-tap-tls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/certs-dir.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/conn-dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/conn-success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/dashboard-topic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/pub-sub-demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/run-tap-tls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/tls-conn-success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mqtt-client-C-Zephyr/assets/tls-pub-success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions mqtt-client-C-Zephyr/prj-nsos.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# NSOS mode: use native_sim offloaded sockets from the host.
# This mode does not support Zephyr socket TLS, so MQTT is TCP-only.
CONFIG_NET_DRIVERS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y
# CONFIG_ETH_NATIVE_TAP is not set
28 changes: 28 additions & 0 deletions mqtt-client-C-Zephyr/prj-tap-tls.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# TAP mode: Zephyr owns the network stack and creates zeth.
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"

# MQTT and socket TLS
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_MQTT_LIB_TLS=y
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y

# mbedTLS
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=30000
CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN=2048
CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN=2048
CONFIG_MBEDTLS_CIPHERSUITE_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256=y
CONFIG_MBEDTLS_PEM_PARSE_C=y
CONFIG_MBEDTLS_PEM_WRITE_C=y

# PSA public key types
CONFIG_PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY=y
CONFIG_PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY=y

CONFIG_NET_LOG=y
CONFIG_LOG=y
30 changes: 30 additions & 0 deletions mqtt-client-C-Zephyr/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Shared system config.
# Select the runtime mode with an overlay:
# - prj-nsos.conf : TCP-only over native_sim offloaded sockets
# - prj-tap-tls.conf : TAP networking with Zephyr socket TLS

# General System Config
CONFIG_PRINTK=y
CONFIG_CONSOLE=y
CONFIG_LOG=y
CONFIG_HEAP_MEM_POOL_SIZE=49152

# Shared networking
CONFIG_NETWORKING=y
CONFIG_NET_IPV4=y
CONFIG_NET_TCP=y
CONFIG_NET_SOCKETS=y

# DNS 解析
CONFIG_DNS_RESOLVER=y
CONFIG_DNS_SERVER_IP_ADDRESSES=y
CONFIG_DNS_SERVER1="8.8.8.8"

# 应用组件
CONFIG_MQTT_LIB=y
CONFIG_SHELL=y
CONFIG_GETOPT=y
CONFIG_GETOPT_LONG=y
CONFIG_SHELL_GETOPT=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_UART_NATIVE_PTY_0_ON_STDINOUT=y
18 changes: 18 additions & 0 deletions mqtt-client-C-Zephyr/run-zephyr-nsos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
BUILD_DIR=${BUILD_DIR:-"$SCRIPT_DIR/build-nsos"}
APP_PATH=${APP_PATH:-"$BUILD_DIR/zephyr/zephyr.exe"}

if [[ ! -x "$APP_PATH" ]]; then
echo "❌ NSOS executable not found: $APP_PATH"
echo " Run the build first:"
echo " west build -d build-nsos -p always -b native_sim/native/64 . -- -DOVERLAY_CONFIG=prj-nsos.conf"
exit 1
fi

echo "🟢 Starting Zephyr (NSOS/TCP-only)..."
echo " (Type commands at uart:~$, Ctrl+C to exit)"
echo ""

exec "$APP_PATH"
77 changes: 77 additions & 0 deletions mqtt-client-C-Zephyr/run-zephyr-tap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash
# Do NOT use set -e; interactive fg switching must not abort the script

SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
BUILD_DIR=${BUILD_DIR:-"$SCRIPT_DIR/build-tap-tls"}
APP_PATH=${APP_PATH:-"$BUILD_DIR/zephyr/zephyr.exe"}

if [[ ! -x "$APP_PATH" ]]; then
echo "❌ TAP/TLS executable not found: $APP_PATH"
echo " Run the build first:"
echo " west build -d build-tap-tls -p always -b native_sim/native/64 . -- -DOVERLAY_CONFIG=prj-tap-tls.conf"
exit 1
fi

cleanup() {
echo ""
echo "🛑 Stopping Zephyr..."
kill $ZEPHYR_PID 2>/dev/null
wait $ZEPHYR_PID 2>/dev/null
echo "✅ Zephyr stopped"
exit 0
}
trap cleanup INT TERM

# ─── Clean up leftover processes and interfaces ───
echo "🧹 Cleaning up old processes and interfaces..."
pkill -9 -f zephyr.exe 2>/dev/null || true
sleep 1
ip link del zeth 2>/dev/null || true

# ─── Start Zephyr ───
echo "🟢 Starting Zephyr..."
set -m # enable job control (required for fg)
"$APP_PATH" &
ZEPHYR_PID=$!

# Verify the process is alive
sleep 1
if ! kill -0 $ZEPHYR_PID 2>/dev/null; then
echo "❌ Zephyr failed to start (process already exited)"
exit 1
fi

# ─── Wait for the zeth interface ───
echo "⚙️ Waiting for zeth interface..."
for i in $(seq 1 10); do
ip link show zeth 2>/dev/null && break
echo " Waiting for zeth ($i/10)..."
sleep 1
done

if ! ip link show zeth > /dev/null 2>&1; then
echo "❌ zeth interface did not appear within timeout"
kill $ZEPHYR_PID 2>/dev/null
exit 1
fi

# ─── Configure networking ───
echo "⚙️ Configuring zeth interface..."
ip addr add 192.0.2.2/24 dev zeth 2>/dev/null || true
ip link set zeth up 2>/dev/null || true
sysctl -w net.ipv4.ip_forward=1 2>/dev/null || \
echo " ⚠️ sysctl failed (container may be read-only); ensure forwarding is enabled on the host"

iptables -t nat -C POSTROUTING -s 192.0.2.0/24 -j MASQUERADE 2>/dev/null || \
iptables -t nat -A POSTROUTING -s 192.0.2.0/24 -j MASQUERADE
iptables -C FORWARD -i zeth -o eth0 -j ACCEPT 2>/dev/null || \
iptables -A FORWARD -i zeth -o eth0 -j ACCEPT
iptables -C FORWARD -i eth0 -o zeth -j ACCEPT 2>/dev/null || \
iptables -A FORWARD -i eth0 -o zeth -j ACCEPT

echo "✅ Network ready | Zephyr: 192.0.2.1 | Host: 192.0.2.2"
echo " (Type commands at uart:~$, Ctrl+C to exit)"
echo ""

# ─── Bring Zephyr to foreground for interactive use ───
fg %1
Loading