mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
76fbf3c713 | ||
|
|
a8d6573ccc | ||
|
|
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 | ||
|
|
1ce7e416b0 | ||
|
|
63aaad829a | ||
|
|
9322227886 | ||
|
|
e3a78d513e | ||
|
|
ee338b7bba | ||
|
|
0f157bd23b |
No files matched your search
@@ -560,8 +560,7 @@ jobs:
|
|||||||
# - run: make -C tutorials/${{ matrix.example.path }} build
|
# - run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
wizard_examples_arm:
|
wizard_examples_arm:
|
||||||
# Keep rp-ti on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
runs-on: ubuntu-latest
|
||||||
runs-on: ${{ matrix.group == 'rp-ti' && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
|
|||||||
@@ -201,8 +201,7 @@ jobs:
|
|||||||
run: make -C tutorials/${{ matrix.example.path }} build
|
run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
generic_examples_arm:
|
generic_examples_arm:
|
||||||
# Keep RP on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
runs-on: ubuntu-latest
|
||||||
runs-on: ${{ startsWith(matrix.example.path, 'rp/') && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -245,8 +244,7 @@ jobs:
|
|||||||
run: make -C tutorials/${{ matrix.example.path }} build
|
run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
device_dashboard_examples_arm:
|
device_dashboard_examples_arm:
|
||||||
# Keep RP on 24.04: Pico SDK 2.1.1 pioasm fails with GCC 15; update SDK
|
runs-on: ubuntu-latest
|
||||||
runs-on: ${{ startsWith(matrix.example.path, 'rp/') && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
@@ -272,8 +270,7 @@ jobs:
|
|||||||
run: make -C tutorials/${{ matrix.example.path }} build
|
run: make -C tutorials/${{ matrix.example.path }} build
|
||||||
|
|
||||||
mqtt_dashboard_examples_arm:
|
mqtt_dashboard_examples_arm:
|
||||||
# Pico SDK 2.1.0 pioasm fails with GCC 15; update SDK to unpin
|
runs-on: ubuntu-latest
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
|
|||||||
+2883
-1098
File diff suppressed because it is too large.
Load diff
+666
-194
File diff suppressed because it is too large.
Load diff
@@ -49,6 +49,8 @@
|
|||||||
#include "arch_armgcc.h"
|
#include "arch_armgcc.h"
|
||||||
#include "arch_cube.h"
|
#include "arch_cube.h"
|
||||||
#include "arch_esp32.h"
|
#include "arch_esp32.h"
|
||||||
|
#include "arch_mcuxpresso.h"
|
||||||
|
#include "arch_picosdk.h"
|
||||||
#include "arch_rtx.h"
|
#include "arch_rtx.h"
|
||||||
#include "arch_threadx.h"
|
#include "arch_threadx.h"
|
||||||
#include "arch_unix.h"
|
#include "arch_unix.h"
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#if MG_ARCH == MG_ARCH_ARMGCC
|
#if MG_ARCH == MG_ARCH_ARMGCC
|
||||||
|
#ifndef _POSIX_TIMERS // Some newlib builds, e.g. Espressif's, define it
|
||||||
#define _POSIX_TIMERS
|
#define _POSIX_TIMERS
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
#if !defined(MG_ENABLE_LWIP) || !MG_ENABLE_LWIP
|
||||||
|
|||||||
+7
-5
@@ -52,16 +52,18 @@
|
|||||||
#define MG_TLS MG_TLS_BUILTIN
|
#define MG_TLS MG_TLS_BUILTIN
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(MG_OTA) && defined(STM32F1) || defined(STM32F2) || \
|
#ifndef MG_OTA // Pick OTA for the STM32 family, unless set by the user
|
||||||
defined(STM32F4) || defined(STM32F7)
|
#if defined(STM32F1) || defined(STM32F2) || defined(STM32F4) || \
|
||||||
|
defined(STM32F7)
|
||||||
#define MG_OTA MG_OTA_STM32F
|
#define MG_OTA MG_OTA_STM32F
|
||||||
#elif !defined(MG_OTA) && defined(STM32H5)
|
#elif defined(STM32H5)
|
||||||
#define MG_OTA MG_OTA_STM32H5
|
#define MG_OTA MG_OTA_STM32H5
|
||||||
#elif !defined(MG_OTA) && defined(STM32H7)
|
#elif defined(STM32H7)
|
||||||
#define MG_OTA MG_OTA_STM32H7
|
#define MG_OTA MG_OTA_STM32H7
|
||||||
#elif !defined(MG_OTA) && defined(STM32C5)
|
#elif defined(STM32C5)
|
||||||
#define MG_OTA MG_OTA_STM32C5
|
#define MG_OTA MG_OTA_STM32C5
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
// use HAL-defined execute-in-ram section
|
// use HAL-defined execute-in-ram section
|
||||||
#ifndef MG_IRAM
|
#ifndef MG_IRAM
|
||||||
|
|||||||
+3
-2
@@ -12,15 +12,16 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include <pico/stdlib.h>
|
#include <hardware/structs/usb.h>
|
||||||
#include <pico/rand.h>
|
#include <pico/rand.h>
|
||||||
|
#include <pico/stdlib.h>
|
||||||
#include <pico/unique_id.h>
|
#include <pico/unique_id.h>
|
||||||
int mkdir(const char *, mode_t);
|
int mkdir(const char *, mode_t);
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_PICOSDK
|
#if MG_OTA == MG_OTA_PICOSDK
|
||||||
#include <hardware/flash.h>
|
#include <hardware/flash.h>
|
||||||
#include <hardware/watchdog.h>
|
|
||||||
#include <hardware/structs/watchdog.h>
|
#include <hardware/structs/watchdog.h>
|
||||||
|
#include <hardware/watchdog.h>
|
||||||
#include <pico/bootrom.h>
|
#include <pico/bootrom.h>
|
||||||
|
|
||||||
#ifndef MG_OTA_WATCHDOG_MS
|
#ifndef MG_OTA_WATCHDOG_MS
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "bsd.h"
|
#include "bsd.h"
|
||||||
#include "dns.h"
|
#include "dns.h"
|
||||||
|
#include "util.h"
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
#if MG_ENABLE_BSD_SOCKETS && MG_ENABLE_FREERTOS
|
#if MG_ENABLE_BSD_SOCKETS && MG_ENABLE_FREERTOS
|
||||||
@@ -131,6 +132,46 @@ static struct mg_connection *bsd_new_conn(struct mg_bsd_sock *s) {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mirror net_builtin.c::trim_len() logic
|
||||||
|
static bool bsd_udp_too_big(struct mg_connection *c, size_t len) {
|
||||||
|
struct mg_tcpip_if *ifp = c->mgr->ifp;
|
||||||
|
size_t hlen = c->rem.is_ip6 ? 48 : 32; // IP plus UDP headers
|
||||||
|
size_t cap = ifp->mtu - hlen;
|
||||||
|
size_t room = ifp->tx.len - (ifp->framesize - ifp->l2mtu) - hlen;
|
||||||
|
|
||||||
|
return len > cap || len > room;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool bsd_udp_queue(struct mg_connection *c, const void *buf,
|
||||||
|
size_t len) {
|
||||||
|
// One raw datagram occupies c->send until write_conn() drains it.
|
||||||
|
return mg_iobuf_add(&c->send, c->send.len, buf, len) == len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t bsd_recv(struct mg_bsd_sock *s, void *buf, size_t len,
|
||||||
|
int flags, struct sockaddr *src, socklen_t *addrlen) {
|
||||||
|
struct mg_connection *c = s->c;
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
if (!s->is_udp) {
|
||||||
|
n = c->recv.len < len ? c->recv.len : len;
|
||||||
|
memcpy(buf, c->recv.buf, n);
|
||||||
|
if (!(flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, n);
|
||||||
|
} else {
|
||||||
|
n = c->recv.len < len ? c->recv.len : len;
|
||||||
|
memcpy(buf, c->recv.buf, n);
|
||||||
|
if (src != NULL && addrlen != NULL) {
|
||||||
|
struct sockaddr_in *sa = (struct sockaddr_in *) src;
|
||||||
|
sa->sin_family = AF_INET;
|
||||||
|
sa->sin_port = c->rem.port;
|
||||||
|
sa->sin_addr.s_addr = c->rem.addr.ip4;
|
||||||
|
*addrlen = sizeof(*sa);
|
||||||
|
}
|
||||||
|
if (!(flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, c->recv.len);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
#if MG_ENABLE_FREERTOS
|
#if MG_ENABLE_FREERTOS
|
||||||
// Complete operations blocked on c before Mongoose destroys it.
|
// Complete operations blocked on c before Mongoose destroys it.
|
||||||
static void bsd_finish_waiters(struct mg_bsd_sock *s, bool error) {
|
static void bsd_finish_waiters(struct mg_bsd_sock *s, bool error) {
|
||||||
@@ -315,9 +356,10 @@ static int bsd_ready(struct mg_bsd_sock *s) {
|
|||||||
if (s->is_shut_rd || s->c->recv.len > 0 || s->c->is_draining)
|
if (s->is_shut_rd || s->c->recv.len > 0 || s->c->is_draining)
|
||||||
ready |= BSD_READ;
|
ready |= BSD_READ;
|
||||||
if (s->is_shut_wr) ready |= BSD_ERROR;
|
if (s->is_shut_wr) ready |= BSD_ERROR;
|
||||||
if (!s->is_shut_wr && !s->is_udp && !s->c->is_connecting &&
|
if (!s->is_shut_wr && (s->is_udp || !s->c->is_connecting) &&
|
||||||
!s->c->is_closing && !s->c->is_draining)
|
!s->c->is_closing && !s->c->is_draining &&
|
||||||
ready |= BSD_WRITE; // Established TCP socket
|
(!s->is_udp || s->c->send.len == 0))
|
||||||
|
ready |= BSD_WRITE; // UDP socket or established TCP socket
|
||||||
return ready;
|
return ready;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,7 +373,7 @@ static void bsd_close(struct mg_bsd_sock *s) {
|
|||||||
if (t->orphan && t->listener == s) {
|
if (t->orphan && t->listener == s) {
|
||||||
if (t->c != NULL) {
|
if (t->c != NULL) {
|
||||||
t->c->fn_data = NULL;
|
t->c->fn_data = NULL;
|
||||||
mg_close_conn(t->c);
|
t->c->is_draining = 1;
|
||||||
t->c = NULL;
|
t->c = NULL;
|
||||||
}
|
}
|
||||||
memset(t, 0, sizeof(*t)); // Release orphan
|
memset(t, 0, sizeof(*t)); // Release orphan
|
||||||
@@ -339,7 +381,7 @@ static void bsd_close(struct mg_bsd_sock *s) {
|
|||||||
}
|
}
|
||||||
if (s->c != NULL) {
|
if (s->c != NULL) {
|
||||||
s->c->fn_data = NULL;
|
s->c->fn_data = NULL;
|
||||||
mg_close_conn(s->c);
|
s->c->is_draining = 1;
|
||||||
s->c = NULL;
|
s->c = NULL;
|
||||||
}
|
}
|
||||||
memset(s, 0, sizeof(*s)); // Release socket
|
memset(s, 0, sizeof(*s)); // Release socket
|
||||||
@@ -529,8 +571,8 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
cmd->result = -1, cmd->err = ECONNREFUSED;
|
cmd->result = -1, cmd->err = ECONNREFUSED;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!c->is_connecting) {
|
if (s->is_udp || !c->is_connecting) {
|
||||||
cmd->result = 0;
|
cmd->result = 0; // UDP may still resolve ARP; TCP waits for SYN/SYN-ACK
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (s->nonblock) {
|
if (s->nonblock) {
|
||||||
@@ -555,11 +597,8 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (c->recv.len > 0) {
|
if (c->recv.len > 0) {
|
||||||
size_t n = cmd->op_recv.len;
|
cmd->result = (int) bsd_recv(s, cmd->op_recv.buf, cmd->op_recv.len,
|
||||||
if (c->recv.len < n) n = c->recv.len;
|
cmd->op_recv.flags, NULL, NULL);
|
||||||
memcpy(cmd->op_recv.buf, c->recv.buf, n);
|
|
||||||
if (!(cmd->op_recv.flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, n);
|
|
||||||
cmd->result = (int) n;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (s->err != 0) {
|
if (s->err != 0) {
|
||||||
@@ -583,7 +622,25 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
||||||
c->is_closing || c->is_draining) {
|
c->is_closing || c->is_draining) {
|
||||||
cmd->result = -1, cmd->err = EPIPE;
|
cmd->result = -1, cmd->err = EPIPE;
|
||||||
} else if (mg_send(c, cmd->op_send.buf, cmd->op_send.len)) {
|
} else if (c == NULL || (s->is_udp && !c->is_client)) {
|
||||||
|
cmd->result = -1, cmd->err = s->is_udp ? EDESTADDRREQ : EPIPE;
|
||||||
|
} else if (s->is_udp && cmd->op_send.len == 0) {
|
||||||
|
cmd->result = 0;
|
||||||
|
} else if (s->is_udp && bsd_udp_too_big(c, cmd->op_send.len)) {
|
||||||
|
cmd->result = -1, cmd->err = EMSGSIZE;
|
||||||
|
} else if (s->is_udp && c->send.len > 0) {
|
||||||
|
if (s->nonblock || (cmd->op_send.flags & MSG_DONTWAIT)) {
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
} else {
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
return false; // One UDP datagram is pending, wait
|
||||||
|
#else
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
} else if ((s->is_udp
|
||||||
|
? bsd_udp_queue(c, cmd->op_send.buf, cmd->op_send.len)
|
||||||
|
: mg_send(c, cmd->op_send.buf, cmd->op_send.len))) {
|
||||||
cmd->result = (int) cmd->op_send.len;
|
cmd->result = (int) cmd->op_send.len;
|
||||||
} else if (s->nonblock || (cmd->op_send.flags & MSG_DONTWAIT)) {
|
} else if (s->nonblock || (cmd->op_send.flags & MSG_DONTWAIT)) {
|
||||||
cmd->result = -1, cmd->err = EAGAIN;
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
@@ -600,27 +657,51 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
|
|
||||||
if ((s = bsd_get(cmd->op_sendto.fd)) == NULL) {
|
if ((s = bsd_get(cmd->op_sendto.fd)) == NULL) {
|
||||||
cmd->result = -1, cmd->err = EBADF;
|
cmd->result = -1, cmd->err = EBADF;
|
||||||
|
} else if (!s->is_udp) {
|
||||||
|
cmd->result = -1, cmd->err = EOPNOTSUPP;
|
||||||
} else if ((err = bsd_addr4(cmd->op_sendto.addr,
|
} else if ((err = bsd_addr4(cmd->op_sendto.addr,
|
||||||
cmd->op_sendto.addrlen)) != 0) {
|
cmd->op_sendto.addrlen)) != 0) {
|
||||||
cmd->result = -1, cmd->err = err;
|
cmd->result = -1, cmd->err = err;
|
||||||
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
} else if (s->is_shut_wr || (c = s->c) == NULL || s->err != 0 ||
|
||||||
c->is_closing || c->is_draining) {
|
c->is_closing || c->is_draining) {
|
||||||
cmd->result = -1, cmd->err = EPIPE;
|
cmd->result = -1, cmd->err = EPIPE;
|
||||||
|
} else if (c->is_client) {
|
||||||
|
cmd->result = -1, cmd->err = EISCONN;
|
||||||
} else {
|
} else {
|
||||||
const struct sockaddr_in *sa =
|
const struct sockaddr_in *sa =
|
||||||
(const struct sockaddr_in *) cmd->op_sendto.addr;
|
(const struct sockaddr_in *) cmd->op_sendto.addr;
|
||||||
|
if (cmd->op_sendto.len == 0) {
|
||||||
|
cmd->result = 0;
|
||||||
|
} else if (bsd_udp_too_big(c, cmd->op_sendto.len)) {
|
||||||
|
cmd->result = -1, cmd->err = EMSGSIZE;
|
||||||
|
} else if (c->send.len == 0) {
|
||||||
|
struct mg_addr rem = c->rem;
|
||||||
|
bool resolve = rem.is_ip6 || rem.addr.ip4 != sa->sin_addr.s_addr;
|
||||||
c->rem.port = sa->sin_port;
|
c->rem.port = sa->sin_port;
|
||||||
c->rem.addr.ip4 = sa->sin_addr.s_addr;
|
c->rem.addr.ip4 = sa->sin_addr.s_addr;
|
||||||
c->rem.is_ip6 = false;
|
c->rem.is_ip6 = false;
|
||||||
if (mg_send(c, cmd->op_sendto.buf, cmd->op_sendto.len)) {
|
if (bsd_udp_queue(c, cmd->op_sendto.buf, cmd->op_sendto.len)) {
|
||||||
|
if (resolve) mg_connect_resolved(c);
|
||||||
cmd->result = (int) cmd->op_sendto.len;
|
cmd->result = (int) cmd->op_sendto.len;
|
||||||
} else if (s->nonblock || (cmd->op_sendto.flags & MSG_DONTWAIT)) {
|
} else {
|
||||||
|
c->rem = rem;
|
||||||
|
if (s->nonblock || (cmd->op_sendto.flags & MSG_DONTWAIT)) {
|
||||||
cmd->result = -1, cmd->err = EAGAIN;
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
} else {
|
} else {
|
||||||
#if MG_ENABLE_FREERTOS
|
#if MG_ENABLE_FREERTOS
|
||||||
return false; // No send buffer space yet, wait
|
return false; // No send buffer space yet, wait
|
||||||
#else
|
#else
|
||||||
cmd->result = -1, cmd->err = EAGAIN;
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (s->nonblock || (cmd->op_sendto.flags & MSG_DONTWAIT)) {
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
|
} else {
|
||||||
|
#if MG_ENABLE_FREERTOS
|
||||||
|
return false; // One UDP datagram is pending, wait
|
||||||
|
#else
|
||||||
|
cmd->result = -1, cmd->err = EAGAIN;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -630,6 +711,9 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
if ((s = bsd_get(cmd->op_recvfrom.fd)) == NULL) {
|
if ((s = bsd_get(cmd->op_recvfrom.fd)) == NULL) {
|
||||||
cmd->result = -1, cmd->err = EBADF;
|
cmd->result = -1, cmd->err = EBADF;
|
||||||
return true;
|
return true;
|
||||||
|
} else if (!s->is_udp) {
|
||||||
|
cmd->result = -1, cmd->err = EOPNOTSUPP;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (s->is_shut_rd) {
|
if (s->is_shut_rd) {
|
||||||
cmd->result = 0;
|
cmd->result = 0;
|
||||||
@@ -641,25 +725,11 @@ static bool bsd_run(struct mg_bsd_cmd *cmd) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (c->recv.len > 0) {
|
if (c->recv.len > 0) {
|
||||||
size_t n = cmd->op_recvfrom.len;
|
cmd->result = (int) bsd_recv(s, cmd->op_recvfrom.buf,
|
||||||
|
cmd->op_recvfrom.len,
|
||||||
if (cmd->op_recvfrom.src != NULL &&
|
cmd->op_recvfrom.flags,
|
||||||
(cmd->op_recvfrom.addrlen == NULL ||
|
cmd->op_recvfrom.src,
|
||||||
*cmd->op_recvfrom.addrlen < sizeof(struct sockaddr_in))) {
|
cmd->op_recvfrom.addrlen);
|
||||||
cmd->result = -1, cmd->err = EINVAL;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (c->recv.len < n) n = c->recv.len;
|
|
||||||
memcpy(cmd->op_recvfrom.buf, c->recv.buf, n);
|
|
||||||
if (!(cmd->op_recvfrom.flags & MSG_PEEK)) mg_iobuf_del(&c->recv, 0, n);
|
|
||||||
if (cmd->op_recvfrom.src != NULL && cmd->op_recvfrom.addrlen != NULL) {
|
|
||||||
struct sockaddr_in *sa = (struct sockaddr_in *) cmd->op_recvfrom.src;
|
|
||||||
sa->sin_family = AF_INET;
|
|
||||||
sa->sin_port = c->rem.port;
|
|
||||||
sa->sin_addr.s_addr = c->rem.addr.ip4;
|
|
||||||
*cmd->op_recvfrom.addrlen = sizeof(*sa);
|
|
||||||
}
|
|
||||||
cmd->result = (int) n;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (s->err != 0) {
|
if (s->err != 0) {
|
||||||
|
|||||||
@@ -64,6 +64,18 @@ void mg_bsd_poll(struct mg_mgr *);
|
|||||||
#ifndef ENOSPC
|
#ifndef ENOSPC
|
||||||
#define ENOSPC 28
|
#define ENOSPC 28
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef EOPNOTSUPP
|
||||||
|
#define EOPNOTSUPP 95
|
||||||
|
#endif
|
||||||
|
#ifndef EISCONN
|
||||||
|
#define EISCONN 106
|
||||||
|
#endif
|
||||||
|
#ifndef EDESTADDRREQ
|
||||||
|
#define EDESTADDRREQ 121
|
||||||
|
#endif
|
||||||
|
#ifndef EMSGSIZE
|
||||||
|
#define EMSGSIZE 90
|
||||||
|
#endif
|
||||||
#ifndef EPROTONOSUPPORT
|
#ifndef EPROTONOSUPPORT
|
||||||
#define EPROTONOSUPPORT 93
|
#define EPROTONOSUPPORT 93
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -94,6 +94,10 @@
|
|||||||
#define MG_ENABLE_CUSTOM_MILLIS 0
|
#define MG_ENABLE_CUSTOM_MILLIS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_CUSTOM_MICROS
|
||||||
|
#define MG_ENABLE_CUSTOM_MICROS 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_ENABLE_ASSERT
|
#ifndef MG_ENABLE_ASSERT
|
||||||
#define MG_ENABLE_ASSERT 0
|
#define MG_ENABLE_ASSERT 0
|
||||||
#endif
|
#endif
|
||||||
@@ -179,6 +183,26 @@
|
|||||||
#define MG_ENABLE_TCPIP_TCPRTX 0 // TCP retransmission queue
|
#define MG_ENABLE_TCPIP_TCPRTX 0 // TCP retransmission queue
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_DRIVER_USB_RP
|
||||||
|
#define MG_ENABLE_DRIVER_USB_RP 0 // USB device, RP2040 / RP2350
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_DRIVER_USB_NRF52
|
||||||
|
#define MG_ENABLE_DRIVER_USB_NRF52 0 // USB device, nRF52840
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_DRIVER_USB_DWC2
|
||||||
|
#define MG_ENABLE_DRIVER_USB_DWC2 0 // USB device, Synopsys DWC2: STM32 OTG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_USB_ETHERNET
|
||||||
|
#define MG_ENABLE_USB_ETHERNET 0 // USB driver: CDC-NCM Ethernet interface
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MG_ENABLE_USB_SERIAL
|
||||||
|
#define MG_ENABLE_USB_SERIAL 0 // USB driver: CDC-ACM serial log console
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef MG_TCPIP_DHCPNAME_SIZE
|
#ifndef MG_TCPIP_DHCPNAME_SIZE
|
||||||
#define MG_TCPIP_DHCPNAME_SIZE 18 // struct mg_tcpip_if :: dhcp_name size
|
#define MG_TCPIP_DHCPNAME_SIZE 18 // struct mg_tcpip_if :: dhcp_name size
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -136,12 +136,13 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
|
|||||||
dm->txnid = mg_ntohs(h->txnid);
|
dm->txnid = mg_ntohs(h->txnid);
|
||||||
is_response = mg_ntohs(h->flags) & 0x8000;
|
is_response = mg_ntohs(h->flags) & 0x8000;
|
||||||
|
|
||||||
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
|
for (i = 0; i < mg_ntohs(h->num_questions); i++) { // advance past queries
|
||||||
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
||||||
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
|
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
|
||||||
|
dm->addr.is_ip6 = (rr.atype == MG_DNS_RTYPE_AAAA);
|
||||||
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
||||||
ofs += n;
|
ofs += n;
|
||||||
}
|
} // on unresolved responses, we got the name and query type, at least
|
||||||
|
|
||||||
if (!is_response) {
|
if (!is_response) {
|
||||||
// For queries, there is no need to parse the answers. In this way,
|
// For queries, there is no need to parse the answers. In this way,
|
||||||
@@ -440,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);
|
n = mg_dns_parse_rr(c->recv.buf, c->recv.len, 12, true, &rr);
|
||||||
MG_VERBOSE(("mDNS request parsed, result=%d", (int) n));
|
MG_VERBOSE(("mDNS request parsed, result=%d", (int) n));
|
||||||
if (n > 0) {
|
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];
|
uint8_t buf[sizeof(struct mg_dns_header) + 256 + sizeof(mdns_answer) + 4];
|
||||||
struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
||||||
uint8_t *p = &buf[sizeof(*h)];
|
uint8_t *p = &buf[sizeof(*h)];
|
||||||
@@ -599,7 +600,7 @@ static void handle_mdns_response(struct mg_connection *c) {
|
|||||||
struct mg_dns_rr rr, rr_[5]; // A, PTR, SRV, TXT, AAAA, in that order
|
struct mg_dns_rr rr, rr_[5]; // A, PTR, SRV, TXT, AAAA, in that order
|
||||||
struct mg_mdns_resp resp;
|
struct mg_mdns_resp resp;
|
||||||
size_t n, i, num, answers, roff_[5], roff = 12; // offset 12 is header size
|
size_t n, i, num, answers, roff_[5], roff = 12; // offset 12 is header size
|
||||||
// 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
|
||||||
char name[256], srvcproto[256], instance[256], host[256];
|
char name[256], srvcproto[256], instance[256], host[256];
|
||||||
uint16_t atype = 0;
|
uint16_t atype = 0;
|
||||||
answers = mg_ntohs(rh->num_answers);
|
answers = mg_ntohs(rh->num_answers);
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ struct mg_dns_message {
|
|||||||
char name[256]; // Host name
|
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.
|
// use mg_ntohs() to read them.
|
||||||
struct mg_dns_header {
|
struct mg_dns_header {
|
||||||
uint16_t txnid; // Transaction ID, matched between query and response
|
uint16_t txnid; // Transaction ID, matched between query and response
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "sntp.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ATCMD) && MG_ENABLE_DRIVER_ATCMD
|
||||||
|
|
||||||
|
|||||||
@@ -12,3 +12,31 @@ struct mg_tcpip_driver_atcmd_data {
|
|||||||
bool link; // internal use: done with AT processing
|
bool link; // internal use: done with AT processing
|
||||||
bool unstuffing; // internal use: unstuffing state
|
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
|
// 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
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_CMSIS) && MG_ENABLE_DRIVER_CMSIS
|
||||||
|
|
||||||
#include "cmsis.h"
|
#include "cmsis.h"
|
||||||
#include "net_builtin.h"
|
#include "log.h"
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
extern ARM_DRIVER_ETH_MAC Driver_ETH_MAC0;
|
extern ARM_DRIVER_ETH_MAC Driver_ETH_MAC0;
|
||||||
|
|||||||
@@ -5,4 +5,22 @@
|
|||||||
#include "Driver_ETH_MAC.h" // keep this include
|
#include "Driver_ETH_MAC.h" // keep this include
|
||||||
#include "Driver_ETH_PHY.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
|
#endif
|
||||||
+7
-2
@@ -1,5 +1,8 @@
|
|||||||
#include "cyw.h"
|
#include "net_builtin.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && \
|
#if MG_ENABLE_TCPIP && \
|
||||||
((defined(MG_ENABLE_DRIVER_CYW) && MG_ENABLE_DRIVER_CYW) || \
|
((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
|
// TODO(): read database, rebuild hash table
|
||||||
uint32_t val = 0;
|
uint32_t val = 0;
|
||||||
val = 1;
|
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),
|
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);
|
mg_delayms(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -4,6 +4,8 @@
|
|||||||
((defined(MG_ENABLE_DRIVER_CYW) && MG_ENABLE_DRIVER_CYW) || \
|
((defined(MG_ENABLE_DRIVER_CYW) && MG_ENABLE_DRIVER_CYW) || \
|
||||||
(defined(MG_ENABLE_DRIVER_CYW_SDIO) && MG_ENABLE_DRIVER_CYW_SDIO))
|
(defined(MG_ENABLE_DRIVER_CYW_SDIO) && MG_ENABLE_DRIVER_CYW_SDIO))
|
||||||
|
|
||||||
|
#include "wifi.h"
|
||||||
|
|
||||||
struct mg_tcpip_driver_cyw_firmware {
|
struct mg_tcpip_driver_cyw_firmware {
|
||||||
const uint8_t *code_addr;
|
const uint8_t *code_addr;
|
||||||
size_t code_len;
|
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)
|
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 { \
|
do { \
|
||||||
static struct mg_tcpip_driver_cyw_data driver_data_; \
|
static struct mg_tcpip_driver_cyw_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -32,8 +36,12 @@ struct mg_tcpip_driver_cyw_data {
|
|||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
mif_.recv_queue.size = 8192; \
|
mif_.recv_queue.size = 8192; \
|
||||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
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)); \
|
MG_INFO(("Driver: cyw, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_CYW_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
#include "log.h"
|
||||||
|
#include "net_builtin.h"
|
||||||
|
#include "radio.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32) && MG_ENABLE_DRIVER_ESP32
|
||||||
|
|
||||||
|
#if MG_RADIO != MG_RADIO_802154
|
||||||
|
#error "MG_ENABLE_DRIVER_ESP32 supports MG_RADIO_802154 only"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MG_ARCH != MG_ARCH_ESP32
|
||||||
|
#error "MG_ENABLE_DRIVER_ESP32 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_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_ifp;
|
||||||
|
static volatile bool s_esp32_txing, s_esp32_tx_done;
|
||||||
|
static uint64_t s_esp32_txstart; // Send start, us
|
||||||
|
// Frame for the radio: PHR, the length including FCS, then the PSDU
|
||||||
|
static uint8_t s_esp32_txframe[128] __attribute__((aligned(4)));
|
||||||
|
|
||||||
|
// 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_RADIO_TX_TIMEOUT_US. Without it, the mesh would count
|
||||||
|
// the refused frame as sent
|
||||||
|
#define MGI_ESP32_RETRY_US 200
|
||||||
|
|
||||||
|
static volatile bool s_esp32_retry; // A resend is pending
|
||||||
|
static volatile uint32_t s_esp32_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_radio_rx_queue(s_esp32_ifp, 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_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_retry_at = (uint32_t) esp_timer_get_time() + MGI_ESP32_RETRY_US;
|
||||||
|
s_esp32_retry = true; // Set last: the poll reads it first
|
||||||
|
} else {
|
||||||
|
s_esp32_tx_done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// rx_when_idle: the driver returns to receive after each transmission
|
||||||
|
static bool mgi_esp32_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
|
||||||
|
s_esp32_ifp = ifp;
|
||||||
|
if (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_RADIO_NETWORK) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_short_address(addr) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_channel(MG_RADIO_CHANNEL) == ESP_OK &&
|
||||||
|
esp_ieee802154_set_txpower(0) == ESP_OK &&
|
||||||
|
esp_ieee802154_receive() == ESP_OK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
MG_ERROR(("802.15.4 radio init failed"));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mg_esp32_hw_tx(void) {
|
||||||
|
s_esp32_retry = false;
|
||||||
|
return esp_ieee802154_transmit(s_esp32_txframe, false) == ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mg_esp32_hw_abort(void) {
|
||||||
|
s_esp32_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_hw_poll(void) {
|
||||||
|
uint32_t now;
|
||||||
|
if (!s_esp32_retry) return;
|
||||||
|
now = (uint32_t) esp_timer_get_time();
|
||||||
|
if ((int32_t) (now - s_esp32_retry_at) < 0) return;
|
||||||
|
s_esp32_retry = false;
|
||||||
|
if (esp_ieee802154_transmit(s_esp32_txframe, false) != ESP_OK) {
|
||||||
|
s_esp32_tx_done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mesh radio transmit. The hardware adds the FCS
|
||||||
|
static size_t mgi_esp32_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||||
|
size_t len) {
|
||||||
|
(void) ifp;
|
||||||
|
if (len > 125 || s_esp32_txing) return 0;
|
||||||
|
s_esp32_txframe[0] = (uint8_t) (len + 2);
|
||||||
|
memcpy(s_esp32_txframe + 1, buf, len);
|
||||||
|
s_esp32_tx_done = false;
|
||||||
|
s_esp32_txing = true, s_esp32_txstart = mg_micros();
|
||||||
|
if (!mg_esp32_hw_tx()) {
|
||||||
|
s_esp32_txing = false;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mgi_esp32_radio_poll(struct mg_tcpip_if *ifp) {
|
||||||
|
bool done = false;
|
||||||
|
(void) ifp;
|
||||||
|
mg_esp32_hw_poll();
|
||||||
|
if (s_esp32_txing && (s_esp32_tx_done || mg_micros() - s_esp32_txstart >
|
||||||
|
MG_RADIO_TX_TIMEOUT_US)) {
|
||||||
|
if (!s_esp32_tx_done) mg_esp32_hw_abort();
|
||||||
|
s_esp32_txing = false;
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mgi_esp32_init(struct mg_tcpip_if *ifp) {
|
||||||
|
return mg_radio_init(ifp, mgi_esp32_radio_init, mgi_esp32_radio_poll,
|
||||||
|
mgi_esp32_radio_tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mg_tcpip_driver mg_tcpip_driver_esp32 = {mgi_esp32_init, mg_radio_tx,
|
||||||
|
mg_radio_rx, mg_radio_poll};
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (c) 2026 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ESP32) && MG_ENABLE_DRIVER_ESP32
|
||||||
|
|
||||||
|
// 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
|
||||||
|
extern struct mg_tcpip_driver mg_tcpip_driver_esp32;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_ESP32_INIT(manager) \
|
||||||
|
do { \
|
||||||
|
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; \
|
||||||
|
mg_tcpip_init((manager), &mif_); \
|
||||||
|
MG_INFO(("Driver: esp32, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ESP32_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "phy.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && \
|
#if MG_ENABLE_TCPIP && \
|
||||||
(defined(MG_ENABLE_DRIVER_IMXRT10) && MG_ENABLE_DRIVER_IMXRT10) || \
|
(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
|
#define MG_DRIVER_MDC_CR 24
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_imxrt;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_IMXRT_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_imxrt_data driver_data_; \
|
static struct mg_tcpip_driver_imxrt_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -42,8 +44,12 @@ struct mg_tcpip_driver_imxrt_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_imxrt; \
|
mif_.driver = &mg_tcpip_driver_imxrt; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: imxrt, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_IMXRT_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+161
-112
@@ -1,10 +1,131 @@
|
|||||||
#include "l2_mesh.h"
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "radio.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
|
||||||
MG_ENABLE_DRIVER_MCXW71
|
MG_ENABLE_DRIVER_MCXW71
|
||||||
|
|
||||||
|
// 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_RF_BASE 0x58A00000U
|
||||||
|
#define MGI_PKT_RAM ((volatile uint8_t *) 0x58A08000)
|
||||||
|
#else
|
||||||
|
#define MGI_RF_BASE 0x48A00000U
|
||||||
|
#define MGI_PKT_RAM ((volatile uint8_t *) 0x48A08000)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MGI_NVIC_ISER ((volatile uint32_t *) 0xE000E100) // Enable IRQ
|
||||||
|
#define MGI_NVIC_ICER ((volatile uint32_t *) 0xE000E180) // Disable IRQ
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
#define MGI_IRQN 54 // RF_Generic_IRQn
|
||||||
|
#else
|
||||||
|
#define MGI_IRQN 53 // RF_802_15_4_IRQn
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Keep the IRQ handler out while we change the radio. The NVIC disable is
|
||||||
|
// atomic, so register 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct mg_tcpip_if *s_mcxw71_ifp;
|
||||||
|
static volatile bool s_mcxw71_txing, s_mcxw71_done;
|
||||||
|
static uint64_t s_mcxw71_sendstart;
|
||||||
|
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
|
||||||
|
// GENFSK Bluetooth LE uncoded mode, RM Table 489. S0 + 8-bit length, then
|
||||||
|
// our frame. The PHY supplies the preamble, whitening and 24-bit CRC.
|
||||||
|
struct mg_genfsk {
|
||||||
|
volatile uint32_t IRQ_CTRL, EVENT_TMR, T1_CMP, T2_CMP, TIMESTAMP, XCVR_CTRL,
|
||||||
|
XCVR_STS, XCVR_CFG, CHANNEL_NUM0, TX_POWER, NTW_ADR_CTRL, NTW_ADR[4],
|
||||||
|
RX_WATERMARK, DSM_CTRL, PART_ID, SLOT_PRELOAD, SLOT_TIME, TURNAROUND_TIME,
|
||||||
|
ACKDELAY, RXDELAY, TXDELAY, PACKET_CFG, H0_CFG, H1_CFG, CRC_CFG,
|
||||||
|
LENGTH_ADJ, TIMESTAMP_RX_DONE, TIMESTAMP_TX_DONE, MULT_PKT_CTRL,
|
||||||
|
RPA_WL_STATUS, LENGTH_MAX, EVENT_TMR_LD, EVENT_TMR_ADD, ENH_FEATURE;
|
||||||
|
uint32_t reserved[21];
|
||||||
|
volatile uint32_t SEQ_STS;
|
||||||
|
};
|
||||||
|
#define MGI_GFSK ((struct mg_genfsk *) (MGI_RF_BASE + 0x2000))
|
||||||
|
#define MGI_TX_PB MGI_PKT_RAM // Access address, S0, length, frame; no CRC
|
||||||
|
#define MGI_RX_PB (MGI_PKT_RAM + 0x1000) // S0, length, frame, CRC; no AA
|
||||||
|
#define MGI_RADIO_MAX 251
|
||||||
|
#define MGI_GFSK_IRQ_EN (MG_BIT(26) | MG_BIT(16)) // Master + sequence end
|
||||||
|
// BLE preamble starts with the access address's first bit; transmit LSBit
|
||||||
|
// first.
|
||||||
|
#define MGI_GFSK_CFG \
|
||||||
|
(MG_BIT(24) | (((MG_RADIO_NETWORK & 1U) ? 0x55U : 0xaaU) << 16) | (1U << 4))
|
||||||
|
|
||||||
|
static void mg_mcxw71_abort(void) {
|
||||||
|
// NXP CONNRF-767: do not abort during TX warmup.
|
||||||
|
while (MGI_GFSK->SEQ_STS & MG_BIT(2)) (void) 0;
|
||||||
|
MGI_GFSK->XCVR_CTRL = 11; // ABORT_ALL
|
||||||
|
while (MGI_GFSK->XCVR_CTRL & MG_BIT(31)) (void) 0;
|
||||||
|
MGI_GFSK->IRQ_CTRL = MGI_GFSK_IRQ_EN | 0xffffU; // W1C status
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mg_mcxw71_listen(void) {
|
||||||
|
MGI_GFSK->IRQ_CTRL = MGI_GFSK_IRQ_EN | 0xffffU;
|
||||||
|
MGI_GFSK->XCVR_CFG = MGI_GFSK_CFG;
|
||||||
|
MGI_GFSK->XCVR_CTRL = 5; // RX_START_NOW
|
||||||
|
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mg_mcxw71_go(void) {
|
||||||
|
mg_mcxw71_abort();
|
||||||
|
MGI_GFSK->XCVR_CFG = MGI_GFSK_CFG; // ABORT disables packet processing
|
||||||
|
MGI_GFSK->XCVR_CTRL = 1; // TX_START_NOW
|
||||||
|
}
|
||||||
|
|
||||||
|
void RF_Generic_IRQHandler(void);
|
||||||
|
void RF_Generic_IRQHandler(void) {
|
||||||
|
uint32_t status = MGI_GFSK->IRQ_CTRL;
|
||||||
|
if (!(status & MG_BIT(0))) return;
|
||||||
|
mg_mcxw71_abort();
|
||||||
|
if (s_mcxw71_txing) {
|
||||||
|
s_mcxw71_txing = false;
|
||||||
|
s_mcxw71_done = true;
|
||||||
|
} else if ((status & (MG_BIT(2) | MG_BIT(10))) ==
|
||||||
|
(MG_BIT(2) | MG_BIT(10))) { // RX and CRC valid
|
||||||
|
uint8_t frame[MGI_RADIO_MAX];
|
||||||
|
size_t i, n = MGI_RX_PB[1];
|
||||||
|
if (n > 0 && n <= sizeof(frame)) {
|
||||||
|
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i + 2];
|
||||||
|
mg_radio_rx_queue(s_mcxw71_ifp, frame, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mg_mcxw71_listen();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
|
||||||
|
s_mcxw71_ifp = ifp;
|
||||||
|
(void) addr; // Frames are filtered by mg_radio_rx_queue()
|
||||||
|
MGI_GFSK->IRQ_CTRL = 0xffffU;
|
||||||
|
mg_mcxw71_abort();
|
||||||
|
// GENFSK channels are MHz above 2360; MG_RADIO_CHANNEL is a BLE data index.
|
||||||
|
MGI_GFSK->CHANNEL_NUM0 = MG_RADIO_CHANNEL < 11 ? 44 + 2 * MG_RADIO_CHANNEL
|
||||||
|
: 46 + 2 * MG_RADIO_CHANNEL;
|
||||||
|
MGI_GFSK->TX_POWER = 0x3f;
|
||||||
|
MGI_GFSK->NTW_ADR_CTRL = 0x301; // 32-bit access address, match address 0
|
||||||
|
MGI_GFSK->NTW_ADR[0] = MG_RADIO_NETWORK;
|
||||||
|
MGI_GFSK->PACKET_CFG = 0x000800c8; // H0=8 bits, length=8, AA=4 bytes
|
||||||
|
MGI_GFSK->H0_CFG = MGI_GFSK->H1_CFG = 0; // No header filtering
|
||||||
|
MGI_GFSK->LENGTH_ADJ = 3; // Length excludes the three CRC bytes
|
||||||
|
MGI_GFSK->MULT_PKT_CTRL = 0;
|
||||||
|
MGI_GFSK->LENGTH_MAX = 16U << 16; // 256 bytes; software caps at 251
|
||||||
|
MGI_GFSK->ENH_FEATURE = 9 | MG_BIT(6); // BLE uncoded, alternate rate: 2M
|
||||||
|
MGI_GFSK->XCVR_CFG = MGI_GFSK_CFG;
|
||||||
|
mg_mcxw71_listen();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#define MGI_ZLL ((struct mg_zll *) (MGI_RF_BASE + 0x1000))
|
||||||
|
#define MGI_RADIO_MAX 125
|
||||||
|
|
||||||
// ZLL, 802.15.4 link layer. See MCXW71 reference manual and NXP
|
// ZLL, 802.15.4 link layer. See MCXW71 reference manual and NXP
|
||||||
// ieee_802_15_4 PHY sources: PhyPacketProcessor.c, PhyPlmeData.c, PhyISR.c
|
// ieee_802_15_4 PHY sources: PhyPacketProcessor.c, PhyPlmeData.c, PhyISR.c
|
||||||
struct mg_zll {
|
struct mg_zll {
|
||||||
@@ -19,16 +140,6 @@ struct mg_zll {
|
|||||||
COEX_PRIORITY, ENHACK_CTRL0;
|
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_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_RX_PB (MGI_PKT_RAM + 0x80) // Received frame, no length byte
|
||||||
|
|
||||||
@@ -41,9 +152,6 @@ struct mg_zll {
|
|||||||
#define MGI_PHY_CTRL_ALL_IRQ_MASKS 0x8007ff00U // Bits 8..18, 31: mask all IRQs
|
#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_SEQMSK MG_BIT(8) // Mask the sequence end IRQ
|
||||||
#define MGI_PHY_CTRL_TRCV_MSK MG_BIT(31) // Mask all transceiver IRQs
|
#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_MASK 0x1fU
|
||||||
#define MGI_SEQ_STATE_RX_WU 0x10U // RX warm up
|
#define MGI_SEQ_STATE_RX_WU 0x10U // RX warm up
|
||||||
#define MGI_SEQ_CTRL_NO_RX_RECYCLE MG_BIT(5)
|
#define MGI_SEQ_CTRL_NO_RX_RECYCLE MG_BIT(5)
|
||||||
@@ -51,13 +159,6 @@ struct mg_zll {
|
|||||||
|
|
||||||
// PHY_CTRL[XCVSEQ]
|
// PHY_CTRL[XCVSEQ]
|
||||||
enum { MGI_SEQ_IDLE = 0, MGI_SEQ_RX = 1, MGI_SEQ_TX = 2 };
|
enum { MGI_SEQ_IDLE = 0, MGI_SEQ_RX = 1, MGI_SEQ_TX = 2 };
|
||||||
#define MGI_SEND_TIMEOUT_MS 60
|
|
||||||
|
|
||||||
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()
|
// Stop the current sequence, wait for the sequencer to go idle. See PhyAbort()
|
||||||
static void mg_mcxw71_abort(void) {
|
static void mg_mcxw71_abort(void) {
|
||||||
@@ -78,13 +179,6 @@ static void mg_mcxw71_listen(void) {
|
|||||||
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
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
|
// Start the TX sequence for the loaded frame. Its end: see the IRQ handler
|
||||||
static void mg_mcxw71_go(void) {
|
static void mg_mcxw71_go(void) {
|
||||||
MGI_ZLL->PHY_CTRL |= MGI_PHY_CTRL_TRCV_MSK | MGI_PHY_CTRL_SEQMSK;
|
MGI_ZLL->PHY_CTRL |= MGI_PHY_CTRL_TRCV_MSK | MGI_PHY_CTRL_SEQMSK;
|
||||||
@@ -110,30 +204,49 @@ void RF_802_15_4_IRQHandler(void) {
|
|||||||
n = n >= 2 ? n - 2 : 0;
|
n = n >= 2 ? n - 2 : 0;
|
||||||
if (n > 0 && n <= sizeof(frame)) {
|
if (n > 0 && n <= sizeof(frame)) {
|
||||||
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
|
for (i = 0; i < n; i++) frame[i] = MGI_RX_PB[i];
|
||||||
{
|
mg_radio_rx_queue(s_mcxw71_ifp, frame, n);
|
||||||
char *dst;
|
|
||||||
if (mg_queue_book(&s_mcxw71_rx, &dst, n) >= n) {
|
|
||||||
memcpy(dst, frame, n);
|
|
||||||
mg_queue_add(&s_mcxw71_rx, n);
|
|
||||||
} else {
|
|
||||||
s_mcxw71_ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mg_mcxw71_listen();
|
mg_mcxw71_listen();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
|
||||||
|
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_RADIO_CHANNEL;
|
||||||
|
MGI_ZLL->MACSHORTADDRS0 = ((uint32_t) addr << 16) | MG_RADIO_NETWORK;
|
||||||
|
MGI_ZLL->PA_PWR = 0x3f;
|
||||||
|
mg_mcxw71_listen();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
static size_t mgi_mcxw71_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
static size_t mgi_mcxw71_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
size_t i;
|
size_t i;
|
||||||
(void) ifp;
|
(void) ifp;
|
||||||
if (len > 125 || s_mcxw71_txing) return 0;
|
if (len > MGI_RADIO_MAX || s_mcxw71_txing) return 0;
|
||||||
mg_mcxw71_lock();
|
mg_mcxw71_lock();
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
if (MGI_GFSK->SEQ_STS & MG_BIT(11)) { // Do not interrupt a received frame
|
||||||
|
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
MGI_TX_PB[i] = (uint8_t) (MG_RADIO_NETWORK >> (8 * i));
|
||||||
|
MGI_TX_PB[4] = 0, MGI_TX_PB[5] = (uint8_t) len;
|
||||||
|
for (i = 0; i < len; i++) MGI_TX_PB[i + 6] = ((const uint8_t *) buf)[i];
|
||||||
|
#else
|
||||||
MGI_TX_PB[0] = (uint8_t) (len + 2); // FCS is added by hardware
|
MGI_TX_PB[0] = (uint8_t) (len + 2); // FCS is added by hardware
|
||||||
for (i = 0; i < len; i++)
|
for (i = 0; i < len; i++)
|
||||||
MGI_TX_PB[i + 1] = ((const uint8_t *) buf)[i]; // 8-bit access only
|
MGI_TX_PB[i + 1] = ((const uint8_t *) buf)[i]; // 8-bit access only
|
||||||
|
#endif
|
||||||
s_mcxw71_done = false;
|
s_mcxw71_done = false;
|
||||||
s_mcxw71_txing = true;
|
s_mcxw71_txing = true;
|
||||||
s_mcxw71_sendstart = mg_millis();
|
s_mcxw71_sendstart = mg_millis();
|
||||||
@@ -142,48 +255,11 @@ static size_t mgi_mcxw71_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mgi_mcxw71_radio_init(struct mg_tcpip_if *ifp) {
|
static bool mgi_mcxw71_radio_poll(struct mg_tcpip_if *ifp) {
|
||||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
bool done;
|
||||||
struct mg_mesh_node *node = data->mesh;
|
(void) ifp;
|
||||||
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();
|
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
|
||||||
if (s_mcxw71_txing &&
|
if (s_mcxw71_txing &&
|
||||||
mg_millis() > s_mcxw71_sendstart + MGI_SEND_TIMEOUT_MS) {
|
mg_millis() > s_mcxw71_sendstart + MG_RADIO_TX_TIMEOUT_US / 1000) {
|
||||||
mg_mcxw71_lock();
|
mg_mcxw71_lock();
|
||||||
if (s_mcxw71_txing) {
|
if (s_mcxw71_txing) {
|
||||||
mg_mcxw71_abort();
|
mg_mcxw71_abort();
|
||||||
@@ -194,43 +270,16 @@ static bool mgi_mcxw71_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
MGI_NVIC_ISER[MGI_IRQN / 32] = MG_BIT(MGI_IRQN % 32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (s_mcxw71_done) {
|
done = s_mcxw71_done;
|
||||||
s_mcxw71_done = false;
|
s_mcxw71_done = false;
|
||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
return done;
|
||||||
}
|
|
||||||
node->now = now;
|
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_mcxw71_rx, &buf)) != 0) {
|
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_mcxw71_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
|
||||||
return node->joined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
|
static bool mgi_mcxw71_init(struct mg_tcpip_if *ifp) {
|
||||||
struct mg_tcpip_driver_mcxw71_data *data = ifp->driver_data;
|
return mg_radio_init(ifp, mgi_mcxw71_radio_init, mgi_mcxw71_radio_poll,
|
||||||
const struct mg_mesh *strategy = data->strategy;
|
mgi_mcxw71_radio_tx);
|
||||||
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 = calloc(1, sizeof(*data->mesh));
|
|
||||||
if (data->mesh == NULL) return false;
|
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
|
||||||
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);
|
|
||||||
free(data->mesh);
|
|
||||||
data->mesh = NULL;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mg_tcpip_driver mg_tcpip_driver_mcxw71 = {
|
struct mg_tcpip_driver mg_tcpip_driver_mcxw71 = {mgi_mcxw71_init, mg_radio_tx,
|
||||||
mgi_mcxw71_init, mgi_mcxw71_tx, mgi_mcxw71_rx, mgi_mcxw71_poll};
|
mg_radio_rx, mg_radio_poll};
|
||||||
#endif
|
#endif
|
||||||
+2
-10
@@ -6,25 +6,17 @@
|
|||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MCXW71) && \
|
||||||
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;
|
extern struct mg_tcpip_driver mg_tcpip_driver_mcxw71;
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_MCXW71_INIT(manager) \
|
#define MG_TCPIP_DRIVER_MCXW71_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_mcxw71_data driver_data_; \
|
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
mif_.ip = MG_TCPIP_IP; \
|
mif_.ip = MG_TCPIP_IP; \
|
||||||
mif_.mask = MG_TCPIP_MASK; \
|
mif_.mask = MG_TCPIP_MASK; \
|
||||||
mif_.gw = MG_TCPIP_GW; \
|
mif_.gw = MG_TCPIP_GW; \
|
||||||
mif_.mgr = (manager); \
|
|
||||||
mif_.driver = &mg_tcpip_driver_mcxw71; \
|
mif_.driver = &mg_tcpip_driver_mcxw71; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mg_tcpip_init((mgr), &mif_); \
|
||||||
mg_tcpip_init((manager), &mif_); \
|
|
||||||
MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \
|
MG_INFO(("Driver: mcxw71, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|||||||
+90
-94
@@ -1,44 +1,56 @@
|
|||||||
#include "net_builtin.h"
|
|
||||||
#include "l2_mesh.h"
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "net_builtin.h"
|
||||||
|
#include "radio.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MG24) && MG_ENABLE_DRIVER_MG24
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MG24) && MG_ENABLE_DRIVER_MG24
|
||||||
|
|
||||||
#include <rail.h>
|
#include <rail.h>
|
||||||
|
|
||||||
|
// A packet in the RAIL FIFOs: the radio header, then the frame
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
#include <rail_ble.h>
|
||||||
|
#define MGI_MG24_HDR 2 // S0 byte, unused, then the frame length
|
||||||
|
#define MGI_MG24_MAX 251 // Largest frame. RAIL adds and strips the CRC
|
||||||
|
// RAIL channel: the RF channel, 2402 MHz + 2 MHz steps. Data channels 0-10
|
||||||
|
// are RF channels 1-11, data channels 11-36 are RF channels 13-38
|
||||||
|
#define MGI_MG24_CHANNEL \
|
||||||
|
(MG_RADIO_CHANNEL < 11 ? MG_RADIO_CHANNEL + 1 : MG_RADIO_CHANNEL + 2)
|
||||||
|
#else
|
||||||
#include <rail_ieee802154.h>
|
#include <rail_ieee802154.h>
|
||||||
|
#define MGI_MG24_HDR 1 // PHR: the frame length, FCS included
|
||||||
|
#define MGI_MG24_MAX 125 // Largest frame, without the 2 byte FCS
|
||||||
|
#define MGI_MG24_CHANNEL MG_RADIO_CHANNEL
|
||||||
|
#endif
|
||||||
|
|
||||||
// RAIL delivers raw frames and transmit completion from interrupt context.
|
// RAIL delivers raw frames and transmit completion from interrupt context.
|
||||||
#define MG_MG24_TX_TIMEOUT_US 60000
|
|
||||||
|
|
||||||
static struct mg_tcpip_if *s_mg24_ifp;
|
static struct mg_tcpip_if *s_mg24_ifp;
|
||||||
static RAIL_Handle_t s_mg24_rail;
|
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 volatile bool s_mg24_txing, s_mg24_tx_done;
|
||||||
static RAIL_Time_t s_mg24_txstart; // Send start, us
|
static RAIL_Time_t s_mg24_txstart; // Send start, us
|
||||||
static uint8_t s_mg24_txfifo[256] __attribute__((aligned(4))); // Power of 2
|
static uint8_t s_mg24_txfifo[256] __attribute__((aligned(4))); // Power of 2
|
||||||
|
|
||||||
// Copy a received PSDU, stripping the PHR and any stored FCS.
|
// Copy a received frame, stripping the radio header and any stored CRC
|
||||||
static void mg_mg24_rx_frame(RAIL_Handle_t h) {
|
static void mg_mg24_rx_frame(RAIL_Handle_t h) {
|
||||||
|
static uint8_t f[MGI_MG24_HDR + MGI_MG24_MAX + 2]; // Interrupt use only
|
||||||
RAIL_RxPacketInfo_t info;
|
RAIL_RxPacketInfo_t info;
|
||||||
uint8_t f[128];
|
|
||||||
size_t n;
|
size_t n;
|
||||||
char *dst;
|
|
||||||
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
|
if (RAIL_GetRxPacketInfo(h, RAIL_RX_PACKET_HANDLE_NEWEST, &info) ==
|
||||||
RAIL_RX_PACKET_HANDLE_INVALID ||
|
RAIL_RX_PACKET_HANDLE_INVALID ||
|
||||||
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
|
info.packetStatus != RAIL_RX_PACKET_READY_SUCCESS ||
|
||||||
info.packetBytes <= 1 || info.packetBytes > sizeof(f)) {
|
info.packetBytes <= MGI_MG24_HDR || info.packetBytes > sizeof(f)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
RAIL_CopyRxPacket(f, &info);
|
RAIL_CopyRxPacket(f, &info);
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
n = f[1];
|
||||||
|
if (n + MGI_MG24_HDR > (size_t) info.packetBytes) return;
|
||||||
|
#else
|
||||||
n = info.packetBytes - 1U; // Strip PHR
|
n = info.packetBytes - 1U; // Strip PHR
|
||||||
if (n == f[0]) n -= 2; // FCS stored, strip it
|
if (n == f[0]) n -= 2; // FCS stored, strip it
|
||||||
if (mg_queue_book(&s_mg24_rx, &dst, n) >= n) {
|
#endif
|
||||||
memcpy(dst, f + 1, n);
|
mg_radio_rx_queue(s_mg24_ifp, f + MGI_MG24_HDR, n);
|
||||||
mg_queue_add(&s_mg24_rx, n);
|
|
||||||
} else {
|
|
||||||
s_mg24_ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RAIL event callback, runs in interrupt context
|
// RAIL event callback, runs in interrupt context
|
||||||
@@ -50,17 +62,21 @@ static void mg_mg24_events(RAIL_Handle_t h, RAIL_Events_t events) {
|
|||||||
if (events & RAIL_EVENTS_TX_COMPLETION) s_mg24_tx_done = true;
|
if (events & RAIL_EVENTS_TX_COMPLETION) s_mg24_tx_done = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// RAIL adds the FCS.
|
// RAIL adds the CRC or FCS
|
||||||
static size_t mgi_mg24_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
static size_t mgi_mg24_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
uint8_t phr = (uint8_t) (len + 2); // FCS is added by hardware
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
uint8_t hdr[MGI_MG24_HDR] = {0, (uint8_t) len};
|
||||||
|
#else
|
||||||
|
uint8_t hdr[MGI_MG24_HDR] = {(uint8_t) (len + 2)}; // FCS added by hardware
|
||||||
|
#endif
|
||||||
(void) ifp;
|
(void) ifp;
|
||||||
if (len > 125 || s_mg24_txing) return 0;
|
if (len > MGI_MG24_MAX || s_mg24_txing) return 0;
|
||||||
RAIL_WriteTxFifo(s_mg24_rail, &phr, 1, true); // Reset FIFO, write PHR
|
RAIL_WriteTxFifo(s_mg24_rail, hdr, sizeof(hdr), true); // Reset, write hdr
|
||||||
RAIL_WriteTxFifo(s_mg24_rail, (const uint8_t *) buf, (uint16_t) len, false);
|
RAIL_WriteTxFifo(s_mg24_rail, (const uint8_t *) buf, (uint16_t) len, false);
|
||||||
s_mg24_tx_done = false;
|
s_mg24_tx_done = false;
|
||||||
s_mg24_txing = true, s_mg24_txstart = RAIL_GetTime();
|
s_mg24_txing = true, s_mg24_txstart = RAIL_GetTime();
|
||||||
if (RAIL_StartTx(s_mg24_rail, MG_802154_CHANNEL, RAIL_TX_OPTIONS_DEFAULT,
|
if (RAIL_StartTx(s_mg24_rail, MGI_MG24_CHANNEL, RAIL_TX_OPTIONS_DEFAULT,
|
||||||
NULL) != RAIL_STATUS_NO_ERROR) {
|
NULL) != RAIL_STATUS_NO_ERROR) {
|
||||||
s_mg24_txing = false;
|
s_mg24_txing = false;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -68,11 +84,46 @@ static size_t mgi_mg24_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mgi_mg24_radio_init(struct mg_tcpip_if *ifp) {
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
// Raw BLE 2 Mbit/s PHY, standard packet as in nrf52.c. The 100 us state
|
||||||
struct mg_mesh_node *node = data->mesh;
|
// transitions fit within the mesh's 300 us gap between frames
|
||||||
RAIL_Config_t cfg = {0};
|
static bool mgi_mg24_protocol_init(uint16_t addr) {
|
||||||
|
RAIL_StateTiming_t timing = {0};
|
||||||
|
(void) addr;
|
||||||
|
timing.idleToRx = 100, timing.idleToTx = 100;
|
||||||
|
timing.rxToTx = 100, timing.txToRx = 100;
|
||||||
|
return RAIL_BLE_Init(s_mg24_rail) == RAIL_STATUS_NO_ERROR &&
|
||||||
|
RAIL_BLE_ConfigPhy2MbpsViterbi(s_mg24_rail) == RAIL_STATUS_NO_ERROR &&
|
||||||
|
RAIL_BLE_ConfigChannelRadioParams(s_mg24_rail, 0x555555,
|
||||||
|
MG_RADIO_NETWORK, MG_RADIO_CHANNEL,
|
||||||
|
false) == RAIL_STATUS_NO_ERROR &&
|
||||||
|
RAIL_SetStateTiming(s_mg24_rail, &timing) == RAIL_STATUS_NO_ERROR;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
// IEEE 802.15.4 with hardware filtering by PAN ID and our short address
|
||||||
|
static bool mgi_mg24_protocol_init(uint16_t addr) {
|
||||||
RAIL_IEEE802154_Config_t cfg154 = {0};
|
RAIL_IEEE802154_Config_t cfg154 = {0};
|
||||||
|
RAIL_StateTransitions_t to_rx = {RAIL_RF_STATE_RX, RAIL_RF_STATE_RX};
|
||||||
|
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;
|
||||||
|
return 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_RADIO_NETWORK, 0) ==
|
||||||
|
RAIL_STATUS_NO_ERROR &&
|
||||||
|
RAIL_IEEE802154_SetShortAddress(s_mg24_rail, addr, 0) ==
|
||||||
|
RAIL_STATUS_NO_ERROR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static bool mgi_mg24_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
|
||||||
|
RAIL_Config_t cfg = {0};
|
||||||
RAIL_TxPowerConfig_t pwr;
|
RAIL_TxPowerConfig_t pwr;
|
||||||
RAIL_StateTransitions_t to_rx = {RAIL_RF_STATE_RX, RAIL_RF_STATE_RX};
|
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_t events = RAIL_EVENT_CAL_NEEDED | RAIL_EVENT_RX_PACKET_RECEIVED |
|
||||||
@@ -82,25 +133,12 @@ static bool mgi_mg24_radio_init(struct mg_tcpip_if *ifp) {
|
|||||||
MG_ERROR(("RAIL_Init failed"));
|
MG_ERROR(("RAIL_Init failed"));
|
||||||
return false;
|
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.mode = RAIL_TX_POWER_MODE_2P4GIG_HP, pwr.voltage = 3300;
|
||||||
pwr.rampTime = 10;
|
pwr.rampTime = 10;
|
||||||
if (RAIL_ConfigCal(s_mg24_rail, RAIL_CAL_ALL) != RAIL_STATUS_NO_ERROR ||
|
if (RAIL_ConfigCal(s_mg24_rail, RAIL_CAL_ALL) != RAIL_STATUS_NO_ERROR ||
|
||||||
RAIL_ConfigEvents(s_mg24_rail, RAIL_EVENTS_ALL, events) !=
|
RAIL_ConfigEvents(s_mg24_rail, RAIL_EVENTS_ALL, events) !=
|
||||||
RAIL_STATUS_NO_ERROR ||
|
RAIL_STATUS_NO_ERROR ||
|
||||||
RAIL_IEEE802154_Config2p4GHzRadio(s_mg24_rail) != RAIL_STATUS_NO_ERROR ||
|
!mgi_mg24_protocol_init(addr) ||
|
||||||
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_SetTxTransitions(s_mg24_rail, &to_rx) != RAIL_STATUS_NO_ERROR ||
|
||||||
RAIL_SetRxTransitions(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_ConfigTxPower(s_mg24_rail, &pwr) != RAIL_STATUS_NO_ERROR ||
|
||||||
@@ -112,72 +150,30 @@ static bool mgi_mg24_radio_init(struct mg_tcpip_if *ifp) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
s_mg24_ifp = ifp;
|
s_mg24_ifp = ifp;
|
||||||
return RAIL_StartRx(s_mg24_rail, MG_802154_CHANNEL, NULL) ==
|
return RAIL_StartRx(s_mg24_rail, MGI_MG24_CHANNEL, NULL) ==
|
||||||
RAIL_STATUS_NO_ERROR;
|
RAIL_STATUS_NO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t mgi_mg24_tx(const void *buf, size_t len,
|
static bool mgi_mg24_radio_poll(struct mg_tcpip_if *ifp) {
|
||||||
struct mg_tcpip_if *ifp) {
|
bool done = false;
|
||||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
(void) ifp;
|
||||||
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();
|
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
|
||||||
node->now = now;
|
|
||||||
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
|
if (s_mg24_txing && (s_mg24_tx_done || RAIL_GetTime() - s_mg24_txstart >
|
||||||
MG_MG24_TX_TIMEOUT_US)) {
|
MG_RADIO_TX_TIMEOUT_US)) {
|
||||||
if (!s_mg24_tx_done) {
|
if (!s_mg24_tx_done) {
|
||||||
RAIL_Idle(s_mg24_rail, RAIL_IDLE_ABORT, true);
|
RAIL_Idle(s_mg24_rail, RAIL_IDLE_ABORT, true);
|
||||||
RAIL_StartRx(s_mg24_rail, MG_802154_CHANNEL, NULL);
|
RAIL_StartRx(s_mg24_rail, MGI_MG24_CHANNEL, NULL);
|
||||||
}
|
}
|
||||||
s_mg24_txing = false;
|
s_mg24_txing = false;
|
||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
done = true;
|
||||||
}
|
}
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_mg24_rx, &buf)) != 0) {
|
return done;
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_mg24_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
|
||||||
return node->joined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
|
static bool mgi_mg24_init(struct mg_tcpip_if *ifp) {
|
||||||
struct mg_tcpip_driver_mg24_data *data = ifp->driver_data;
|
return mg_radio_init(ifp, mgi_mg24_radio_init, mgi_mg24_radio_poll,
|
||||||
const struct mg_mesh *strategy = data->strategy;
|
mgi_mg24_radio_tx);
|
||||||
uint16_t address =
|
|
||||||
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
|
||||||
if (strategy == NULL) strategy = &mg_mesh_default;
|
|
||||||
data->mesh = calloc(1, sizeof(*data->mesh));
|
|
||||||
if (data->mesh == NULL) return false;
|
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
|
||||||
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);
|
|
||||||
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};
|
struct mg_tcpip_driver mg_tcpip_driver_mg24 = {mgi_mg24_init, mg_radio_tx,
|
||||||
|
mg_radio_rx, mg_radio_poll};
|
||||||
#endif
|
#endif
|
||||||
+2
-10
@@ -5,25 +5,17 @@
|
|||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_MG24) && MG_ENABLE_DRIVER_MG24
|
#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;
|
extern struct mg_tcpip_driver mg_tcpip_driver_mg24;
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_MG24_INIT(manager) \
|
#define MG_TCPIP_DRIVER_MG24_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_mg24_data driver_data_; \
|
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
mif_.ip = MG_TCPIP_IP; \
|
mif_.ip = MG_TCPIP_IP; \
|
||||||
mif_.mask = MG_TCPIP_MASK; \
|
mif_.mask = MG_TCPIP_MASK; \
|
||||||
mif_.gw = MG_TCPIP_GW; \
|
mif_.gw = MG_TCPIP_GW; \
|
||||||
mif_.mgr = (manager); \
|
|
||||||
mif_.driver = &mg_tcpip_driver_mg24; \
|
mif_.driver = &mg_tcpip_driver_mg24; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mg_tcpip_init((mgr), &mif_); \
|
||||||
mg_tcpip_init((manager), &mif_); \
|
|
||||||
MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \
|
MG_INFO(("Driver: mg24, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "phy.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP && \
|
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP && \
|
||||||
defined(MG_ENABLE_DRIVER_NETC) && MG_ENABLE_DRIVER_NETC
|
defined(MG_ENABLE_DRIVER_NETC) && MG_ENABLE_DRIVER_NETC
|
||||||
|
|||||||
+6
-3
@@ -26,10 +26,9 @@ struct mg_tcpip_driver_netc_data {
|
|||||||
#define MG_TCPIP_NETC_LINK 0
|
#define MG_TCPIP_NETC_LINK 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct mg_tcpip_driver;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
#define MG_TCPIP_DRIVER_NETC_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_netc_data driver_data_; \
|
static struct mg_tcpip_driver_netc_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -42,8 +41,12 @@ extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
|||||||
mif_.driver = &mg_tcpip_driver_netc; \
|
mif_.driver = &mg_tcpip_driver_netc; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
mg_tcpip_init(mgr, &mif_); \
|
mg_tcpip_init((mgr), &mif_); \
|
||||||
MG_INFO(("Driver: netc, MAC: %M", mg_print_mac, mif_.mac)); \
|
MG_INFO(("Driver: netc, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NETC_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+74
-85
@@ -1,15 +1,30 @@
|
|||||||
#include "net_builtin.h"
|
|
||||||
#include "l2_mesh.h"
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "net_builtin.h"
|
||||||
|
#include "radio.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NRF52) && MG_ENABLE_DRIVER_NRF52
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NRF52) && MG_ENABLE_DRIVER_NRF52
|
||||||
#include <nrf.h>
|
#include <nrf.h>
|
||||||
|
|
||||||
|
// A packet in RAM: the radio header, then the frame
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
#define MGI_NRF52_HDR 2 // S0 byte, unused, then the frame length
|
||||||
|
#define MGI_NRF52_MAX 251 // Largest frame. The CRC is not in RAM
|
||||||
|
#define MGI_NRF52_RXSTART EVENTS_ADDRESS // A frame is being received
|
||||||
|
#define MGI_NRF52_MHZ \
|
||||||
|
(MG_RADIO_CHANNEL < 11 ? 2404 + 2 * MG_RADIO_CHANNEL \
|
||||||
|
: 2406 + 2 * MG_RADIO_CHANNEL)
|
||||||
|
#else
|
||||||
|
#define MGI_NRF52_HDR 1 // PHR: the frame length, FCS included
|
||||||
|
#define MGI_NRF52_MAX 125 // Largest frame, without the 2 byte FCS
|
||||||
|
#define MGI_NRF52_RXSTART EVENTS_FRAMESTART // A frame is being received
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MGI_NRF52_IOLEN (MGI_NRF52_HDR + MGI_NRF52_MAX + 2)
|
||||||
|
|
||||||
static struct mg_tcpip_if *s_nrf52_ifp;
|
static struct mg_tcpip_if *s_nrf52_ifp;
|
||||||
static struct mg_queue s_nrf52_rx;
|
static uint8_t s_nrf52_rxbuf[MGI_NRF52_IOLEN] MG_8BYTE_ALIGNED;
|
||||||
static char s_nrf52_queue[2048];
|
static uint8_t s_nrf52_txbuf[MGI_NRF52_IOLEN] MG_8BYTE_ALIGNED;
|
||||||
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 volatile bool s_nrf52_txing, s_nrf52_done;
|
||||||
static uint64_t s_nrf52_started;
|
static uint64_t s_nrf52_started;
|
||||||
|
|
||||||
@@ -17,10 +32,11 @@ static void mgi_nrf52_listen(void) {
|
|||||||
NRF_RADIO->PACKETPTR = (uint32_t) s_nrf52_rxbuf;
|
NRF_RADIO->PACKETPTR = (uint32_t) s_nrf52_rxbuf;
|
||||||
NRF_RADIO->SHORTS =
|
NRF_RADIO->SHORTS =
|
||||||
RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk;
|
RADIO_SHORTS_READY_START_Msk | RADIO_SHORTS_END_DISABLE_Msk;
|
||||||
NRF_RADIO->EVENTS_FRAMESTART = 0;
|
NRF_RADIO->MGI_NRF52_RXSTART = 0;
|
||||||
NRF_RADIO->EVENTS_END = 0;
|
NRF_RADIO->EVENTS_END = 0;
|
||||||
NRF_RADIO->TASKS_RXEN = 1;
|
NRF_RADIO->TASKS_RXEN = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mgi_nrf52_disable(void) {
|
static void mgi_nrf52_disable(void) {
|
||||||
NRF_RADIO->SHORTS = 0;
|
NRF_RADIO->SHORTS = 0;
|
||||||
NRF_RADIO->EVENTS_DISABLED = 0;
|
NRF_RADIO->EVENTS_DISABLED = 0;
|
||||||
@@ -28,6 +44,7 @@ static void mgi_nrf52_disable(void) {
|
|||||||
while (!NRF_RADIO->EVENTS_DISABLED) (void) 0;
|
while (!NRF_RADIO->EVENTS_DISABLED) (void) 0;
|
||||||
NRF_RADIO->EVENTS_DISABLED = 0;
|
NRF_RADIO->EVENTS_DISABLED = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RADIO_IRQHandler(void);
|
void RADIO_IRQHandler(void);
|
||||||
void RADIO_IRQHandler(void) {
|
void RADIO_IRQHandler(void) {
|
||||||
if (!NRF_RADIO->EVENTS_DISABLED) return;
|
if (!NRF_RADIO->EVENTS_DISABLED) return;
|
||||||
@@ -36,38 +53,31 @@ void RADIO_IRQHandler(void) {
|
|||||||
s_nrf52_txing = false;
|
s_nrf52_txing = false;
|
||||||
s_nrf52_done = true;
|
s_nrf52_done = true;
|
||||||
} else if (NRF_RADIO->CRCSTATUS) {
|
} else if (NRF_RADIO->CRCSTATUS) {
|
||||||
struct mg_tcpip_driver_nrf52_data *data = s_nrf52_ifp->driver_data;
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
struct mg_mesh_node *node = data->mesh;
|
size_t len = s_nrf52_rxbuf[1];
|
||||||
|
#else
|
||||||
size_t len = s_nrf52_rxbuf[0] >= 2 ? s_nrf52_rxbuf[0] - 2U : 0;
|
size_t len = s_nrf52_rxbuf[0] >= 2 ? s_nrf52_rxbuf[0] - 2U : 0;
|
||||||
uint8_t *frame = s_nrf52_rxbuf + 1;
|
#endif
|
||||||
uint16_t dst, src;
|
mg_radio_rx_queue(s_nrf52_ifp, s_nrf52_rxbuf + MGI_NRF52_HDR, len);
|
||||||
if (len <= 125 &&
|
|
||||||
mg_radio_802154.parse_hdr(&mg_radio_802154, frame, len, &dst, &src) !=
|
|
||||||
0 &&
|
|
||||||
(dst == 0xffffu || dst == node->radio_address)) {
|
|
||||||
char *dst;
|
|
||||||
if (mg_queue_book(&s_nrf52_rx, &dst, len) >= len) {
|
|
||||||
memcpy(dst, frame, len);
|
|
||||||
mg_queue_add(&s_nrf52_rx, len);
|
|
||||||
} else {
|
|
||||||
s_nrf52_ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mgi_nrf52_listen();
|
mgi_nrf52_listen();
|
||||||
}
|
}
|
||||||
static size_t mgi_nrf52_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
static size_t mgi_nrf52_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
(void) ifp;
|
(void) ifp;
|
||||||
if (len > 125) return 0;
|
if (len > MGI_NRF52_MAX) return 0;
|
||||||
NVIC_DisableIRQ(RADIO_IRQn);
|
NVIC_DisableIRQ(RADIO_IRQn);
|
||||||
if (s_nrf52_txing || NRF_RADIO->EVENTS_FRAMESTART) {
|
if (s_nrf52_txing || NRF_RADIO->MGI_NRF52_RXSTART) {
|
||||||
NVIC_EnableIRQ(RADIO_IRQn);
|
NVIC_EnableIRQ(RADIO_IRQn);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
mgi_nrf52_disable();
|
mgi_nrf52_disable();
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
s_nrf52_txbuf[0] = 0, s_nrf52_txbuf[1] = (uint8_t) len;
|
||||||
|
#else
|
||||||
s_nrf52_txbuf[0] = (uint8_t) (len + 2);
|
s_nrf52_txbuf[0] = (uint8_t) (len + 2);
|
||||||
memcpy(s_nrf52_txbuf + 1, buf, len);
|
#endif
|
||||||
|
memcpy(s_nrf52_txbuf + MGI_NRF52_HDR, buf, len);
|
||||||
s_nrf52_done = false;
|
s_nrf52_done = false;
|
||||||
s_nrf52_txing = true;
|
s_nrf52_txing = true;
|
||||||
s_nrf52_started = mg_micros();
|
s_nrf52_started = mg_micros();
|
||||||
@@ -78,9 +88,33 @@ static size_t mgi_nrf52_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
|||||||
NVIC_EnableIRQ(RADIO_IRQn);
|
NVIC_EnableIRQ(RADIO_IRQn);
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
static bool mgi_nrf52_radio_init(struct mg_tcpip_if *ifp) {
|
static bool mgi_nrf52_radio_init(struct mg_tcpip_if *ifp, uint16_t addr) {
|
||||||
s_nrf52_ifp = ifp;
|
s_nrf52_ifp = ifp;
|
||||||
|
(void) addr; // Frames are filtered by mg_radio_rx_queue()
|
||||||
NRF_RADIO->POWER = 1;
|
NRF_RADIO->POWER = 1;
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
// Standard BLE packet: 2 byte preamble, access address, S0 byte, 8 bit
|
||||||
|
// length, payload, 24 bit CRC over everything after the access address,
|
||||||
|
// initial value 0x555555. Whitening seeded by the channel index
|
||||||
|
NRF_RADIO->MODE = RADIO_MODE_MODE_Ble_2Mbit;
|
||||||
|
NRF_RADIO->PCNF0 = (1UL << RADIO_PCNF0_S0LEN_Pos) |
|
||||||
|
(8UL << RADIO_PCNF0_LFLEN_Pos) |
|
||||||
|
(RADIO_PCNF0_PLEN_16bit << RADIO_PCNF0_PLEN_Pos);
|
||||||
|
NRF_RADIO->PCNF1 = ((uint32_t) MGI_NRF52_MAX << RADIO_PCNF1_MAXLEN_Pos) |
|
||||||
|
(3UL << RADIO_PCNF1_BALEN_Pos) | RADIO_PCNF1_WHITEEN_Msk;
|
||||||
|
NRF_RADIO->BASE0 = (uint32_t) MG_RADIO_NETWORK << 8;
|
||||||
|
NRF_RADIO->PREFIX0 = ((uint32_t) MG_RADIO_NETWORK >> 24) & 0xff;
|
||||||
|
NRF_RADIO->TXADDRESS = 0;
|
||||||
|
NRF_RADIO->RXADDRESSES = 1;
|
||||||
|
NRF_RADIO->CRCCNF =
|
||||||
|
3 | (RADIO_CRCCNF_SKIPADDR_Skip << RADIO_CRCCNF_SKIPADDR_Pos);
|
||||||
|
NRF_RADIO->CRCPOLY = 0x65b;
|
||||||
|
NRF_RADIO->CRCINIT = 0x555555;
|
||||||
|
NRF_RADIO->DATAWHITEIV = MG_RADIO_CHANNEL;
|
||||||
|
NRF_RADIO->FREQUENCY = MGI_NRF52_MHZ - 2400;
|
||||||
|
NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_DTX_Center << RADIO_MODECNF0_DTX_Pos) |
|
||||||
|
RADIO_MODECNF0_RU_Fast;
|
||||||
|
#else
|
||||||
NRF_RADIO->MODE = RADIO_MODE_MODE_Ieee802154_250Kbit;
|
NRF_RADIO->MODE = RADIO_MODE_MODE_Ieee802154_250Kbit;
|
||||||
NRF_RADIO->PCNF0 = (8UL << RADIO_PCNF0_LFLEN_Pos) |
|
NRF_RADIO->PCNF0 = (8UL << RADIO_PCNF0_LFLEN_Pos) |
|
||||||
(RADIO_PCNF0_PLEN_32bitZero << RADIO_PCNF0_PLEN_Pos) |
|
(RADIO_PCNF0_PLEN_32bitZero << RADIO_PCNF0_PLEN_Pos) |
|
||||||
@@ -92,44 +126,26 @@ static bool mgi_nrf52_radio_init(struct mg_tcpip_if *ifp) {
|
|||||||
NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_DTX_Center << RADIO_MODECNF0_DTX_Pos) |
|
NRF_RADIO->MODECNF0 = (RADIO_MODECNF0_DTX_Center << RADIO_MODECNF0_DTX_Pos) |
|
||||||
RADIO_MODECNF0_RU_Fast;
|
RADIO_MODECNF0_RU_Fast;
|
||||||
NRF_RADIO->SFD = 0xa7;
|
NRF_RADIO->SFD = 0xa7;
|
||||||
NRF_RADIO->FREQUENCY = 5 * (MG_802154_CHANNEL - 10);
|
NRF_RADIO->FREQUENCY = 5 * (MG_RADIO_CHANNEL - 10);
|
||||||
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos8dBm;
|
|
||||||
// Correlator threshold 45 matches the RADIO reset value. A threshold of
|
// Correlator threshold 45 matches the RADIO reset value. A threshold of
|
||||||
// 20 caused false busy detections and persistent L2 retries.
|
// 20 caused false busy detections and persistent L2 retries.
|
||||||
NRF_RADIO->CCACTRL =
|
NRF_RADIO->CCACTRL =
|
||||||
(RADIO_CCACTRL_CCAMODE_CarrierMode << RADIO_CCACTRL_CCAMODE_Pos) |
|
(RADIO_CCACTRL_CCAMODE_CarrierMode << RADIO_CCACTRL_CCAMODE_Pos) |
|
||||||
(2UL << RADIO_CCACTRL_CCACORRCNT_Pos) |
|
(2UL << RADIO_CCACTRL_CCACORRCNT_Pos) |
|
||||||
(45UL << RADIO_CCACTRL_CCACORRTHRES_Pos);
|
(45UL << RADIO_CCACTRL_CCACORRTHRES_Pos);
|
||||||
|
#endif
|
||||||
|
NRF_RADIO->TXPOWER = RADIO_TXPOWER_TXPOWER_Pos8dBm;
|
||||||
NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
|
NRF_RADIO->INTENSET = RADIO_INTENSET_DISABLED_Msk;
|
||||||
NVIC_SetPriority(RADIO_IRQn, 0);
|
NVIC_SetPriority(RADIO_IRQn, 0);
|
||||||
NVIC_EnableIRQ(RADIO_IRQn);
|
NVIC_EnableIRQ(RADIO_IRQn);
|
||||||
mgi_nrf52_listen();
|
mgi_nrf52_listen();
|
||||||
return true;
|
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) {
|
static bool mgi_nrf52_radio_poll(struct mg_tcpip_if *ifp) {
|
||||||
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
bool done;
|
||||||
size_t len = data->mesh->rx_len;
|
(void) ifp;
|
||||||
if (len > cap) return 0;
|
if (s_nrf52_txing && mg_micros() - s_nrf52_started > MG_RADIO_TX_TIMEOUT_US) {
|
||||||
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();
|
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
|
||||||
if (s_nrf52_txing && mg_micros() - s_nrf52_started > 60000) {
|
|
||||||
NVIC_DisableIRQ(RADIO_IRQn);
|
NVIC_DisableIRQ(RADIO_IRQn);
|
||||||
mgi_nrf52_disable();
|
mgi_nrf52_disable();
|
||||||
s_nrf52_txing = false;
|
s_nrf52_txing = false;
|
||||||
@@ -137,43 +153,16 @@ static bool mgi_nrf52_poll(struct mg_tcpip_if *ifp, bool s1) {
|
|||||||
mgi_nrf52_listen();
|
mgi_nrf52_listen();
|
||||||
NVIC_EnableIRQ(RADIO_IRQn);
|
NVIC_EnableIRQ(RADIO_IRQn);
|
||||||
}
|
}
|
||||||
if (s_nrf52_done) {
|
done = s_nrf52_done;
|
||||||
s_nrf52_done = false;
|
s_nrf52_done = false;
|
||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
return done;
|
||||||
}
|
|
||||||
node->now = now;
|
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_nrf52_rx, &buf)) != 0) {
|
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_nrf52_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
|
||||||
return node->joined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
|
static bool mgi_nrf52_init(struct mg_tcpip_if *ifp) {
|
||||||
struct mg_tcpip_driver_nrf52_data *data = ifp->driver_data;
|
return mg_radio_init(ifp, mgi_nrf52_radio_init, mgi_nrf52_radio_poll,
|
||||||
const struct mg_mesh *strategy = data->strategy;
|
mgi_nrf52_radio_tx);
|
||||||
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 = calloc(1, sizeof(*data->mesh));
|
|
||||||
if (data->mesh == NULL) return false;
|
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
|
||||||
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);
|
|
||||||
free(data->mesh);
|
|
||||||
data->mesh = NULL;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mg_tcpip_driver mg_tcpip_driver_nrf52 = {mgi_nrf52_init, mgi_nrf52_tx,
|
struct mg_tcpip_driver mg_tcpip_driver_nrf52 = {mgi_nrf52_init, mg_radio_tx,
|
||||||
mgi_nrf52_rx, mgi_nrf52_poll};
|
mg_radio_rx, mg_radio_poll};
|
||||||
#endif
|
#endif
|
||||||
+2
-10
@@ -5,25 +5,17 @@
|
|||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NRF52) && MG_ENABLE_DRIVER_NRF52
|
#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;
|
extern struct mg_tcpip_driver mg_tcpip_driver_nrf52;
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_NRF52_INIT(manager) \
|
#define MG_TCPIP_DRIVER_NRF52_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_nrf52_data driver_data_; \
|
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
mif_.ip = MG_TCPIP_IP; \
|
mif_.ip = MG_TCPIP_IP; \
|
||||||
mif_.mask = MG_TCPIP_MASK; \
|
mif_.mask = MG_TCPIP_MASK; \
|
||||||
mif_.gw = MG_TCPIP_GW; \
|
mif_.gw = MG_TCPIP_GW; \
|
||||||
mif_.mgr = (manager); \
|
|
||||||
mif_.driver = &mg_tcpip_driver_nrf52; \
|
mif_.driver = &mg_tcpip_driver_nrf52; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mg_tcpip_init((mgr), &mif_); \
|
||||||
mg_tcpip_init((manager), &mif_); \
|
|
||||||
MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \
|
MG_INFO(("Driver: nrf52, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#include "net_builtin.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NXP_WIFI) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_NXP_WIFI) && \
|
||||||
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
|
defined(MG_ENABLE_DRIVER_NXP_WIFI) && MG_ENABLE_DRIVER_NXP_WIFI
|
||||||
|
|
||||||
|
|
||||||
|
#include "wifi.h"
|
||||||
|
|
||||||
struct mg_tcpip_driver_nxp_wifi_data {
|
struct mg_tcpip_driver_nxp_wifi_data {
|
||||||
struct mg_wifi_data wifi;
|
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 { \
|
do { \
|
||||||
static struct mg_tcpip_driver_nxp_wifi_data driver_data_; \
|
static struct mg_tcpip_driver_nxp_wifi_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -21,8 +25,12 @@ struct mg_tcpip_driver_nxp_wifi_data {
|
|||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
mif_.recv_queue.size = 8192; \
|
mif_.recv_queue.size = 8192; \
|
||||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
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)); \
|
MG_INFO(("Driver: nxp wifi, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_NXP_WIFI_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "phy.h"
|
#include "phy.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
enum { // ID1 ID2
|
enum { // ID1 ID2
|
||||||
MG_PHY_KSZ8x = 0x22, // 0022 156x - KSZ8081RNB, KSZ8091RNB
|
MG_PHY_KSZ8x = 0x22, // 0022 156x - KSZ8081RNB, KSZ8091RNB
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
|
#include "net_builtin.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \
|
#if MG_ENABLE_TCPIP && MG_ARCH == MG_ARCH_PICOSDK && \
|
||||||
defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W
|
defined(MG_ENABLE_DRIVER_PICO_W) && MG_ENABLE_DRIVER_PICO_W
|
||||||
|
|
||||||
#include "drivers/pico-w.h"
|
#include "drivers/pico-w.h"
|
||||||
#include "net_builtin.h"
|
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
static struct mg_tcpip_if *s_ifp;
|
static struct mg_tcpip_if *s_ifp;
|
||||||
static uint32_t s_ip, s_mask;
|
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) {
|
if (ifp->update_mac_hash_table) {
|
||||||
// first call to _poll() is after _init(), so this is safe
|
// first call to _poll() is after _init(), so this is safe
|
||||||
cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *) mcast_addr,
|
uint8_t mac[sizeof(mcast_addr)]; // copy from const uint8_t* to uint8_t*
|
||||||
true);
|
memcpy(mac, mcast_addr, sizeof(mac));
|
||||||
|
cyw43_wifi_update_multicast_filter(&cyw43_state, mac, true);
|
||||||
ifp->update_mac_hash_table = false;
|
ifp->update_mac_hash_table = false;
|
||||||
}
|
}
|
||||||
if (!s1) return false;
|
if (!s1) return false;
|
||||||
|
|||||||
+10
-2
@@ -7,11 +7,15 @@
|
|||||||
#include "pico/cyw43_arch.h" // keep this include
|
#include "pico/cyw43_arch.h" // keep this include
|
||||||
#include "pico/unique_id.h" // keep this include
|
#include "pico/unique_id.h" // keep this include
|
||||||
|
|
||||||
|
#include "wifi.h"
|
||||||
|
|
||||||
struct mg_tcpip_driver_pico_w_data {
|
struct mg_tcpip_driver_pico_w_data {
|
||||||
struct mg_wifi_data wifi;
|
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 { \
|
do { \
|
||||||
static struct mg_tcpip_driver_pico_w_data driver_data_; \
|
static struct mg_tcpip_driver_pico_w_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -23,8 +27,12 @@ struct mg_tcpip_driver_pico_w_data {
|
|||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
mif_.recv_queue.size = 8192; \
|
mif_.recv_queue.size = 8192; \
|
||||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
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)); \
|
MG_INFO(("Driver: pico-w, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_PICO_W_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "phy.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && \
|
#if MG_ENABLE_TCPIP && \
|
||||||
(defined(MG_ENABLE_DRIVER_RA6) && MG_ENABLE_DRIVER_RA6) || \
|
(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
|
#define MG_TCPIP_PHY_ADDR 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_ra;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_RA_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_ra_data driver_data_; \
|
static struct mg_tcpip_driver_ra_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -36,8 +38,12 @@ struct mg_tcpip_driver_ra_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_ra; \
|
mif_.driver = &mg_tcpip_driver_ra; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: ra, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RA_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -0,0 +1,718 @@
|
|||||||
|
// Copyright (c) 2026 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
#include "radio.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP
|
||||||
|
|
||||||
|
#define MGI_ETH_MAX 1514
|
||||||
|
#define MGI_TX_DST_LEN 2
|
||||||
|
|
||||||
|
// The radio, chosen by MG_RADIO. A frame starts with the radio header, with
|
||||||
|
// the radio addresses, then the mesh header: type, then burst: the
|
||||||
|
// fragment's index << 4 | the last fragment's index. Then the payload
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
#define MGI_RADIO_MTU 251 // Largest frame: BLE 5 data length
|
||||||
|
#define MGI_RADIO_BITRATE 2000000 // Bits per second on air
|
||||||
|
#define MGI_RADIO_OVERHEAD 11 // Preamble, access address, length, CRC
|
||||||
|
#define MGI_RADIO_TURNAROUND_US 40 // Receive to transmit: nRF52840 fast ramp
|
||||||
|
#define MGI_RADIO_IFS_US 300 // Gap between transmissions
|
||||||
|
#define MGI_RADIO_HDR_LEN 4 // Destination, source
|
||||||
|
|
||||||
|
// Write the radio header. Little endian
|
||||||
|
static void mgi_radio_write_hdr(uint8_t *buf, uint16_t dst, uint16_t src) {
|
||||||
|
buf[0] = (uint8_t) dst, buf[1] = (uint8_t) (dst >> 8);
|
||||||
|
buf[2] = (uint8_t) src, buf[3] = (uint8_t) (src >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the radio header. The access address already keeps other networks out
|
||||||
|
static bool mgi_radio_parse_hdr(const uint8_t *buf, size_t len, uint16_t *dst,
|
||||||
|
uint16_t *src) {
|
||||||
|
if (len < MGI_RADIO_HDR_LEN) return false;
|
||||||
|
*dst = (uint16_t) (buf[0] | buf[1] << 8);
|
||||||
|
*src = (uint16_t) (buf[2] | buf[3] << 8);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define MGI_RADIO_MTU 125 // 127 minus FCS
|
||||||
|
#define MGI_RADIO_BITRATE 250000 // Bits per second on air
|
||||||
|
#define MGI_RADIO_OVERHEAD 8 // Preamble, SFD, PHR, FCS
|
||||||
|
#define MGI_RADIO_TURNAROUND_US 100 // Receive to transmit
|
||||||
|
#define MGI_RADIO_IFS_US 640 // Gap between transmissions
|
||||||
|
#define MGI_RADIO_HDR_LEN 9 // MAC header, short addresses
|
||||||
|
|
||||||
|
// MAC header: frame control 0x8841, a data frame, PAN ID compression, short
|
||||||
|
// destination and source addresses; sequence number, 0, as nothing uses it
|
||||||
|
// yet; PAN ID; destination; source. Little endian, as the standard says
|
||||||
|
static void mgi_radio_write_hdr(uint8_t *buf, uint16_t dst, uint16_t src) {
|
||||||
|
buf[0] = 0x41, buf[1] = 0x88, buf[2] = 0;
|
||||||
|
buf[3] = (uint8_t) MG_RADIO_NETWORK;
|
||||||
|
buf[4] = (uint8_t) (MG_RADIO_NETWORK >> 8);
|
||||||
|
buf[5] = (uint8_t) dst, buf[6] = (uint8_t) (dst >> 8);
|
||||||
|
buf[7] = (uint8_t) src, buf[8] = (uint8_t) (src >> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the MAC header. Other networks' frames fail the check
|
||||||
|
static bool mgi_radio_parse_hdr(const uint8_t *buf, size_t len, uint16_t *dst,
|
||||||
|
uint16_t *src) {
|
||||||
|
if (len < MGI_RADIO_HDR_LEN || buf[0] != 0x41 || buf[1] != 0x88 ||
|
||||||
|
buf[3] != (uint8_t) MG_RADIO_NETWORK ||
|
||||||
|
buf[4] != (uint8_t) (MG_RADIO_NETWORK >> 8)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*dst = (uint16_t) (buf[5] | buf[6] << 8);
|
||||||
|
*src = (uint16_t) (buf[7] | buf[8] << 8);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define MGI_FRAME_HDR_LEN (MGI_RADIO_HDR_LEN + 2) // Radio and mesh headers
|
||||||
|
#define MGI_FRAG_CAP (MGI_RADIO_MTU - MGI_FRAME_HDR_LEN) // Payload per frame
|
||||||
|
#define MGI_TX_QUEUE_SIZE 2048 // Ethernet frames to send, as bursts
|
||||||
|
#define MGI_RX_STAMP_LEN sizeof(uint64_t)
|
||||||
|
#define MGI_RX_QUEUE_SIZE (16 * (MGI_RX_STAMP_LEN + MGI_RADIO_MTU + 8))
|
||||||
|
|
||||||
|
// A radio interface's state, its driver_data, allocated by mg_radio_init()
|
||||||
|
struct mg_radio_node {
|
||||||
|
bool (*hw_poll)(struct mg_tcpip_if *); // Hardware: service it
|
||||||
|
size_t (*hw_tx)(struct mg_tcpip_if *, const void *, size_t); // Start TX
|
||||||
|
struct mg_tcpip_if *ifp; // Radio interface
|
||||||
|
struct mg_tcpip_if *lan; // Bridge: LAN interface. NULL: a node
|
||||||
|
struct mg_queue frames; // Received radio frames, with arrival times
|
||||||
|
struct mg_queue tx; // Ethernet frames to send, one burst each
|
||||||
|
uint64_t now; // Current poll time, us
|
||||||
|
uint64_t tx_at; // Earliest next radio TX, us
|
||||||
|
uint64_t burst_at; // Next fragment TX, us
|
||||||
|
uint64_t deadline; // End of the current turn, us
|
||||||
|
uint64_t expire_at; // Node: no turn by then, leave. 0: had one
|
||||||
|
uint64_t answer_by; // Node: first answer frame must end by then
|
||||||
|
size_t rx_len; // Ethernet frame waiting for the stack
|
||||||
|
size_t tx_offset; // Next fragment of the queued burst
|
||||||
|
struct mgi_radio_peer *members; // Bridge: members, round-robin order
|
||||||
|
struct mgi_radio_peer *current; // Bridge: member whose turn is active/next
|
||||||
|
struct mgi_radio_peer *fanout; // Bridge: next broadcast recipient
|
||||||
|
uint32_t random; // PRNG state, for join timing
|
||||||
|
uint16_t radio_address; // Our radio address, made from the MAC
|
||||||
|
uint16_t coordinator; // Node: the bridge's radio address
|
||||||
|
uint8_t phase; // Turn: idle, join or data
|
||||||
|
uint8_t rx_expected; // Next fragment: index << 4 | last index. 0xff: idle
|
||||||
|
bool joined; // Node: member of the mesh
|
||||||
|
bool granted; // Node: had a turn this round
|
||||||
|
uint8_t rx_buf[MGI_ETH_MAX]; // Ethernet frame being reassembled
|
||||||
|
char tx_buf[MGI_TX_QUEUE_SIZE]; // Storage of the tx queue
|
||||||
|
char frames_buf[MGI_RX_QUEUE_SIZE]; // Storage of the frames queue
|
||||||
|
};
|
||||||
|
|
||||||
|
// Time on air of a len byte frame, us
|
||||||
|
static uint64_t mgi_radio_airtime(size_t len) {
|
||||||
|
return ((uint64_t) (len + MGI_RADIO_OVERHEAD) * 8000000 + MGI_RADIO_BITRATE -
|
||||||
|
1) /
|
||||||
|
MGI_RADIO_BITRATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each queue record holds a destination and a complete burst.
|
||||||
|
static size_t mgi_radio_queue(struct mg_radio_node *node, struct mg_queue *q,
|
||||||
|
const void *buf, size_t len, uint16_t dst,
|
||||||
|
uint8_t type) {
|
||||||
|
const uint8_t *eth = (const uint8_t *) buf;
|
||||||
|
size_t hdr = MGI_FRAME_HDR_LEN, cap = MGI_RADIO_MTU - hdr;
|
||||||
|
size_t frames, size, i;
|
||||||
|
char *record;
|
||||||
|
uint8_t *out;
|
||||||
|
if (len < 14 || len > MGI_ETH_MAX) return 0;
|
||||||
|
frames = (len + cap - 1) / cap;
|
||||||
|
size = MGI_TX_DST_LEN + len + frames * (1 + hdr);
|
||||||
|
if (mg_queue_next(q, NULL) == 0) mg_queue_init(q, q->buf, q->size);
|
||||||
|
if (mg_queue_book(q, &record, size) < size) {
|
||||||
|
MG_ERROR(("TX queue full: need %u, head %u, tail %u, size %u",
|
||||||
|
(unsigned) size, (unsigned) q->head, (unsigned) q->tail,
|
||||||
|
(unsigned) q->size));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
out = (uint8_t *) record;
|
||||||
|
*out++ = (uint8_t) (dst >> 8);
|
||||||
|
*out++ = (uint8_t) dst;
|
||||||
|
for (i = 0; i < frames; i++) {
|
||||||
|
size_t chunk = len - i * cap;
|
||||||
|
if (chunk > cap) chunk = cap;
|
||||||
|
*out++ = (uint8_t) (hdr + chunk);
|
||||||
|
mgi_radio_write_hdr(out, dst, node->radio_address);
|
||||||
|
out[MGI_RADIO_HDR_LEN] = type;
|
||||||
|
out[MGI_RADIO_HDR_LEN + 1] = (uint8_t) (i << 4 | (frames - 1));
|
||||||
|
memcpy(out + hdr, eth + i * cap, chunk);
|
||||||
|
out += hdr + chunk;
|
||||||
|
}
|
||||||
|
mg_queue_add(q, size);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define MGI_JOIN_WINDOW_US 3000
|
||||||
|
#define MGI_JOIN_TURN_US 192
|
||||||
|
#define MGI_JOIN_JITTER_US 640
|
||||||
|
#define MGI_TX_RETRY_US 32
|
||||||
|
// Node: finish the first answer frame this much before the bridge stops
|
||||||
|
// waiting
|
||||||
|
#define MGI_ANSWER_MARGIN_US 500
|
||||||
|
#define MGI_JOIN_UP_LEN (MGI_FRAME_HDR_LEN + 6) // Payload: the MAC address
|
||||||
|
#define MGI_MAX_FRAGMENTS 14
|
||||||
|
|
||||||
|
enum { MGI_JOIN_DOWN = 1, MGI_JOIN_UP, MGI_DATA_DOWN, MGI_DATA_UP };
|
||||||
|
|
||||||
|
enum { MGI_TURN_IDLE, MGI_TURN_JOIN, MGI_TURN_DATA };
|
||||||
|
|
||||||
|
struct mgi_radio_peer {
|
||||||
|
struct mgi_radio_peer *next; // Next member in round-robin order
|
||||||
|
uint16_t radio_address; // Source address supplied in MGI_JOIN_UP
|
||||||
|
uint8_t mac[6]; // Ethernet MAC supplied in MGI_JOIN_UP
|
||||||
|
uint64_t expire_at; // First missed turn + MG_RADIO_TIMEOUT_MS, 0: answering
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t mgi_radio_random(struct mg_radio_node *node) {
|
||||||
|
uint32_t *random = &node->random;
|
||||||
|
if (*random == 0) {
|
||||||
|
size_t i;
|
||||||
|
*random = (uint32_t) mg_now();
|
||||||
|
for (i = 0; i < 6; i++) *random = *random * 33u + node->ifp->mac[i];
|
||||||
|
if (*random == 0) *random = 1;
|
||||||
|
}
|
||||||
|
*random ^= *random << 13;
|
||||||
|
*random ^= *random >> 17;
|
||||||
|
*random ^= *random << 5;
|
||||||
|
return *random;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a frame's radio addresses to dst and src, and return its type, from
|
||||||
|
// the mesh header. 0: not a frame of ours
|
||||||
|
static unsigned mgi_radio_frame_parse(const uint8_t *frame, size_t len,
|
||||||
|
uint16_t *dst, uint16_t *src) {
|
||||||
|
if (len < MGI_FRAME_HDR_LEN || !mgi_radio_parse_hdr(frame, len, dst, src)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return frame[MGI_RADIO_HDR_LEN];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write a frame's headers: the radio's, to dst from src, then ours: type,
|
||||||
|
// and burst: high nibble is the index, low nibble is the last index
|
||||||
|
static void mgi_radio_frame_header(uint8_t *frame, uint16_t dst, uint16_t src,
|
||||||
|
uint8_t type, uint8_t burst) {
|
||||||
|
mgi_radio_write_hdr(frame, dst, src);
|
||||||
|
frame[MGI_RADIO_HDR_LEN] = type, frame[MGI_RADIO_HDR_LEN + 1] = burst;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hand a radio frame to the hardware. The interface's fn sees it first
|
||||||
|
static bool mgi_radio_send(struct mg_radio_node *node, const void *frame,
|
||||||
|
size_t len) {
|
||||||
|
struct mg_str s = mg_str_n((const char *) frame, len);
|
||||||
|
mg_tcpip_call(node->ifp, MG_TCPIP_EV_PHY_TX, &s);
|
||||||
|
return node->hw_tx(node->ifp, frame, len) == len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool mgi_radio_control_send(struct mg_radio_node *node, uint16_t dst,
|
||||||
|
uint8_t command) {
|
||||||
|
uint8_t frame[MGI_JOIN_UP_LEN] = {0};
|
||||||
|
size_t len = MGI_FRAME_HDR_LEN;
|
||||||
|
mgi_radio_frame_header(frame, dst, node->radio_address, command, 0);
|
||||||
|
if (command == MGI_JOIN_UP) {
|
||||||
|
memcpy(frame + MGI_FRAME_HDR_LEN, node->ifp->mac, 6);
|
||||||
|
len = MGI_JOIN_UP_LEN;
|
||||||
|
}
|
||||||
|
return node->now >= node->tx_at && mgi_radio_send(node, frame, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time on air of a burst carrying len bytes, gaps between frames included
|
||||||
|
static uint64_t mgi_radio_burst_time(size_t len) {
|
||||||
|
size_t frames = len == 0 ? 1 : (len + MGI_FRAG_CAP - 1) / MGI_FRAG_CAP;
|
||||||
|
return (frames - 1) * (mgi_radio_airtime(MGI_RADIO_MTU) + MGI_RADIO_IFS_US) +
|
||||||
|
mgi_radio_airtime(MGI_FRAME_HDR_LEN + len -
|
||||||
|
(frames - 1) * MGI_FRAG_CAP);
|
||||||
|
}
|
||||||
|
|
||||||
|
// End the current turn. A queued burst is used up only when done: all its
|
||||||
|
// frames went out. Otherwise it stays queued, and a later turn sends it again
|
||||||
|
static void mgi_radio_tx_finish(struct mg_radio_node *node, bool done) {
|
||||||
|
if (node->tx_offset != 0 && done) {
|
||||||
|
if (node->fanout != NULL) node->fanout = node->fanout->next;
|
||||||
|
if (node->fanout == NULL) {
|
||||||
|
size_t len = mg_queue_next(&node->tx, NULL);
|
||||||
|
mg_queue_del(&node->tx, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node->tx_offset = 0;
|
||||||
|
node->burst_at = (uint64_t) -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_tx_step(struct mg_radio_node *node, uint64_t now) {
|
||||||
|
uint8_t empty[MGI_FRAME_HDR_LEN], *frame = empty;
|
||||||
|
size_t len = MGI_FRAME_HDR_LEN, record_len = 0;
|
||||||
|
uint16_t dst;
|
||||||
|
uint8_t command;
|
||||||
|
if (now < node->burst_at) return;
|
||||||
|
if (now >= node->deadline) {
|
||||||
|
mgi_radio_tx_finish(node, false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dst = node->lan == NULL ? node->coordinator : node->current->radio_address;
|
||||||
|
command = node->lan == NULL ? MGI_DATA_UP : MGI_DATA_DOWN;
|
||||||
|
if (node->tx_offset != 0) {
|
||||||
|
char *record;
|
||||||
|
uint8_t *part;
|
||||||
|
record_len = mg_queue_next(&node->tx, &record);
|
||||||
|
part = (uint8_t *) record + node->tx_offset;
|
||||||
|
len = part[0];
|
||||||
|
frame = part + 1;
|
||||||
|
mgi_radio_write_hdr(frame, dst, node->radio_address);
|
||||||
|
} else {
|
||||||
|
mgi_radio_frame_header(frame, dst, node->radio_address, command, 0);
|
||||||
|
}
|
||||||
|
// A node's first answer frame: if it cannot end on air before the bridge
|
||||||
|
// stops waiting, stay silent. The queued frame waits for the next turn
|
||||||
|
if (node->lan == NULL &&
|
||||||
|
(node->tx_offset == 0 || node->tx_offset == MGI_TX_DST_LEN) &&
|
||||||
|
now + mgi_radio_airtime(len) > node->answer_by) {
|
||||||
|
node->tx_offset = 0;
|
||||||
|
node->burst_at = (uint64_t) -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (now < node->tx_at || !mgi_radio_send(node, frame, len)) {
|
||||||
|
node->burst_at = now + MGI_TX_RETRY_US;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (node->tx_offset == 0 || (node->tx_offset += len + 1) == record_len) {
|
||||||
|
// Bridge: the turn's last frame is out, wait for the answer
|
||||||
|
if (node->lan != NULL)
|
||||||
|
node->deadline =
|
||||||
|
now + mgi_radio_airtime(len) + MG_RADIO_ANSWER_TIMEOUT_US;
|
||||||
|
mgi_radio_tx_finish(node, true);
|
||||||
|
} else {
|
||||||
|
node->burst_at = now + mgi_radio_airtime(len) + MGI_RADIO_IFS_US;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_tx_begin(struct mg_radio_node *node, bool burst,
|
||||||
|
uint64_t now, uint64_t deadline) {
|
||||||
|
node->phase = MGI_TURN_DATA;
|
||||||
|
node->answer_by = (uint64_t) -1; // Node sets it after a grant
|
||||||
|
node->tx_offset = burst ? MGI_TX_DST_LEN : 0;
|
||||||
|
node->burst_at = now;
|
||||||
|
node->deadline = deadline;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the assembled length, zero for an empty frame, or -1 if incomplete
|
||||||
|
// or invalid. Full non-final fragments make the write offset derivable.
|
||||||
|
static int mgi_radio_data_receive(struct mg_radio_node *node,
|
||||||
|
const uint8_t *frame, size_t len,
|
||||||
|
uint64_t now) {
|
||||||
|
unsigned burst = frame[MGI_FRAME_HDR_LEN - 1], index = burst >> 4,
|
||||||
|
last = burst & 15;
|
||||||
|
size_t chunk = len - MGI_FRAME_HDR_LEN, cap = MGI_FRAG_CAP;
|
||||||
|
size_t offset = index * cap;
|
||||||
|
if (index == 0) {
|
||||||
|
node->rx_expected = (uint8_t) burst;
|
||||||
|
if (node->lan == NULL)
|
||||||
|
node->deadline = now + mgi_radio_burst_time(MGI_ETH_MAX) +
|
||||||
|
2 * MGI_RADIO_IFS_US + MGI_JOIN_TURN_US;
|
||||||
|
}
|
||||||
|
if (last >= MGI_MAX_FRAGMENTS || index > last || burst != node->rx_expected ||
|
||||||
|
chunk > cap || now > node->deadline || (index < last && chunk != cap) ||
|
||||||
|
(last != 0 && chunk == 0) || offset + chunk > sizeof(node->rx_buf)) {
|
||||||
|
MG_ERROR(
|
||||||
|
("%s: fragment %u/%u dropped, expected %u/%u, %u bytes, %lld us "
|
||||||
|
"past deadline",
|
||||||
|
node->lan == NULL ? "Node" : "Bridge", index, last,
|
||||||
|
(unsigned) node->rx_expected >> 4, node->rx_expected & 15U,
|
||||||
|
(unsigned) chunk, (long long) (now - node->deadline)));
|
||||||
|
node->rx_expected = 0xff;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
memcpy(node->rx_buf + offset, frame + MGI_FRAME_HDR_LEN, chunk);
|
||||||
|
if (index < last) {
|
||||||
|
node->rx_expected = (uint8_t) (burst + 0x10);
|
||||||
|
// Bridge: an answer is under way, wait for its next fragment
|
||||||
|
if (node->lan != NULL)
|
||||||
|
node->deadline = now + mgi_radio_airtime(MGI_RADIO_MTU) +
|
||||||
|
MGI_RADIO_IFS_US + MG_RADIO_ANSWER_TIMEOUT_US;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
node->rx_expected = 0xff;
|
||||||
|
return offset + chunk == 0 || offset + chunk >= 14 ? (int) (offset + chunk)
|
||||||
|
: -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bridge scheduling and Ethernet forwarding.
|
||||||
|
|
||||||
|
static uint16_t mgi_radio_join_field(const uint8_t *p) {
|
||||||
|
return (uint16_t) ((unsigned) p[0] * 256 + p[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return chaddr for an Ethernet DHCP server reply, even when broadcast.
|
||||||
|
static const uint8_t *mgi_radio_dhcp_client(const uint8_t *frame, size_t len) {
|
||||||
|
size_t ip_len, ihl, udp_len;
|
||||||
|
const uint8_t *udp, *dhcp;
|
||||||
|
if (len < 34 || mgi_radio_join_field(frame + 12) != 0x0800 ||
|
||||||
|
(frame[14] >> 4) != 4 || frame[23] != 17 ||
|
||||||
|
(mgi_radio_join_field(frame + 20) & 0x3fff) != 0)
|
||||||
|
return NULL;
|
||||||
|
ip_len = mgi_radio_join_field(frame + 16);
|
||||||
|
ihl = (size_t) (frame[14] & 15) * 4;
|
||||||
|
if (ihl < 20 || ip_len > len - 14 || ip_len < ihl + 8 + 240) return NULL;
|
||||||
|
udp = frame + 14 + ihl;
|
||||||
|
udp_len = mgi_radio_join_field(udp + 4);
|
||||||
|
if (mgi_radio_join_field(udp) != 67 || mgi_radio_join_field(udp + 2) != 68 ||
|
||||||
|
udp_len < 8 + 240 || udp_len > ip_len - ihl)
|
||||||
|
return NULL;
|
||||||
|
dhcp = udp + 8;
|
||||||
|
if (dhcp[0] != 2 || dhcp[1] != 1 || dhcp[2] != 6 ||
|
||||||
|
memcmp(dhcp + 236, "\x63\x82\x53\x63", 4) != 0)
|
||||||
|
return NULL;
|
||||||
|
return dhcp + 28;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t mgi_radio_enqueue(struct mg_radio_node *node, const void *buf,
|
||||||
|
size_t len) {
|
||||||
|
const uint8_t *frame = (const uint8_t *) buf;
|
||||||
|
if (node->lan == NULL)
|
||||||
|
return mgi_radio_queue(node, &node->tx, buf, len, node->coordinator,
|
||||||
|
MGI_DATA_UP);
|
||||||
|
if (len >= 14 && len <= MGI_ETH_MAX) {
|
||||||
|
const uint8_t *client = mgi_radio_dhcp_client(frame, len);
|
||||||
|
const uint8_t *dst = client == NULL ? frame : client;
|
||||||
|
bool multicast = client == NULL && (frame[0] & 1) != 0;
|
||||||
|
struct mgi_radio_peer *peer;
|
||||||
|
for (peer = node->members; peer != NULL; peer = peer->next) {
|
||||||
|
if (multicast || memcmp(dst, peer->mac, 6) == 0) {
|
||||||
|
uint16_t addr = multicast ? 0xffffu : peer->radio_address;
|
||||||
|
return mgi_radio_queue(node, &node->tx, buf, len, addr, MGI_DATA_DOWN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_bridge_queue_lan(struct mg_radio_node *bridge) {
|
||||||
|
struct mg_queue *rx = &bridge->lan->recv_queue;
|
||||||
|
char *buf;
|
||||||
|
size_t len;
|
||||||
|
while ((len = mg_queue_next(rx, &buf)) > 0) {
|
||||||
|
bridge->ifp->driver->tx(buf, len, bridge->ifp);
|
||||||
|
mg_queue_del(rx, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_bridge_rx(uint64_t now, struct mg_radio_node *b,
|
||||||
|
const uint8_t *frame, size_t len) {
|
||||||
|
struct mgi_radio_peer *peer;
|
||||||
|
uint16_t dst = 0, src = 0;
|
||||||
|
unsigned command = mgi_radio_frame_parse(frame, len, &dst, &src);
|
||||||
|
if (command == MGI_DATA_UP && dst == b->radio_address &&
|
||||||
|
b->phase == MGI_TURN_DATA && b->current != NULL &&
|
||||||
|
src == b->current->radio_address && now <= b->deadline) {
|
||||||
|
int received = mgi_radio_data_receive(b, frame, len, now);
|
||||||
|
if (received < 0) return;
|
||||||
|
if (received != 0) {
|
||||||
|
b->lan->driver->tx(b->rx_buf, (size_t) received, b->lan);
|
||||||
|
}
|
||||||
|
b->current->expire_at = 0;
|
||||||
|
b->current = b->current->next;
|
||||||
|
b->phase = MGI_TURN_IDLE;
|
||||||
|
b->deadline = now;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (command == MGI_DATA_UP && dst == b->radio_address) {
|
||||||
|
MG_ERROR(
|
||||||
|
("Bridge: fragment %u/%u from %04x outside its turn: phase %u, "
|
||||||
|
"turn of %04x, %lld us past deadline",
|
||||||
|
frame[MGI_FRAME_HDR_LEN - 1] >> 4U, frame[MGI_FRAME_HDR_LEN - 1] & 15U,
|
||||||
|
src, b->phase, b->current == NULL ? 0 : b->current->radio_address,
|
||||||
|
(long long) (now - b->deadline)));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (len != MGI_JOIN_UP_LEN || command != MGI_JOIN_UP ||
|
||||||
|
dst != b->radio_address || b->phase != MGI_TURN_JOIN ||
|
||||||
|
now >= b->deadline) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (src == 0 || src == 0xffffu) return;
|
||||||
|
for (peer = b->members; peer != NULL; peer = peer->next) {
|
||||||
|
if (peer->radio_address == src) break;
|
||||||
|
}
|
||||||
|
if (peer == NULL) {
|
||||||
|
peer = (struct mgi_radio_peer *) mg_calloc(1, sizeof(*peer));
|
||||||
|
if (peer == NULL) return;
|
||||||
|
peer->radio_address = src;
|
||||||
|
peer->next = b->members;
|
||||||
|
b->members = peer;
|
||||||
|
MG_DEBUG(("Member %M joined, radio address %04x", mg_print_mac,
|
||||||
|
frame + MGI_FRAME_HDR_LEN, src));
|
||||||
|
}
|
||||||
|
memcpy(peer->mac, frame + MGI_FRAME_HDR_LEN, sizeof(peer->mac));
|
||||||
|
peer->expire_at = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Member peer did not answer its turn. The first miss starts the timeout. If
|
||||||
|
// it is still silent when that ends, it is gone: remove it, and its queued
|
||||||
|
// downlink frames
|
||||||
|
static void mgi_radio_member_missed(struct mg_radio_node *node,
|
||||||
|
struct mgi_radio_peer *peer, uint64_t now) {
|
||||||
|
if (peer->expire_at == 0) peer->expire_at = now + MG_RADIO_TIMEOUT_MS * 1000;
|
||||||
|
if (now < peer->expire_at) return;
|
||||||
|
MG_DEBUG(("Member %M removed: no answer for %d ms", mg_print_mac, peer->mac,
|
||||||
|
MG_RADIO_TIMEOUT_MS));
|
||||||
|
if (node->fanout == peer) {
|
||||||
|
node->fanout = peer->next;
|
||||||
|
if (node->fanout == NULL) {
|
||||||
|
size_t len = mg_queue_next(&node->tx, NULL);
|
||||||
|
mg_queue_del(&node->tx, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LIST_DELETE(struct mgi_radio_peer, &node->members, peer);
|
||||||
|
mg_free(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A broadcast occupies one record and is sent once to each member in list
|
||||||
|
// order.
|
||||||
|
static size_t mgi_radio_bridge_burst(struct mg_radio_node *node,
|
||||||
|
struct mgi_radio_peer *peer) {
|
||||||
|
char *record;
|
||||||
|
size_t len;
|
||||||
|
while ((len = mg_queue_next(&node->tx, &record)) != 0) {
|
||||||
|
uint16_t dst = mgi_radio_join_field((const uint8_t *) record);
|
||||||
|
struct mgi_radio_peer *member;
|
||||||
|
if (dst == 0xffffu) {
|
||||||
|
if (node->fanout == NULL) node->fanout = node->members;
|
||||||
|
return node->fanout == peer ? len : 0;
|
||||||
|
}
|
||||||
|
if (dst == peer->radio_address) return len;
|
||||||
|
for (member = node->members; member != NULL; member = member->next) {
|
||||||
|
if (member->radio_address == dst) return 0;
|
||||||
|
}
|
||||||
|
mg_queue_del(&node->tx, len); // Its destination has left.
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t mgi_radio_queued_burst_time(struct mg_radio_node *node,
|
||||||
|
size_t len) {
|
||||||
|
char *record;
|
||||||
|
size_t frames;
|
||||||
|
if (len == 0) return mgi_radio_burst_time(0);
|
||||||
|
mg_queue_next(&node->tx, &record);
|
||||||
|
// Fragment zero carries the final index. Count overhead and IFS per frame.
|
||||||
|
frames = ((uint8_t) record[MGI_TX_DST_LEN + MGI_FRAME_HDR_LEN] & 15u) + 1u;
|
||||||
|
return mgi_radio_airtime(len - MGI_TX_DST_LEN - frames +
|
||||||
|
(frames - 1) * MGI_RADIO_OVERHEAD) +
|
||||||
|
(frames - 1) * MGI_RADIO_IFS_US;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_bridge_io(uint64_t now, struct mg_radio_node *b) {
|
||||||
|
mgi_radio_bridge_queue_lan(b);
|
||||||
|
if (now >= b->deadline) {
|
||||||
|
if (b->burst_at != (uint64_t) -1) mgi_radio_tx_finish(b, false);
|
||||||
|
if (b->phase == MGI_TURN_DATA && b->current != NULL) {
|
||||||
|
struct mgi_radio_peer *peer = b->current;
|
||||||
|
b->current = peer->next;
|
||||||
|
mgi_radio_member_missed(b, peer, now);
|
||||||
|
} else if (b->phase == MGI_TURN_JOIN) {
|
||||||
|
b->current = b->members;
|
||||||
|
}
|
||||||
|
b->phase = MGI_TURN_IDLE;
|
||||||
|
if (b->current != NULL) {
|
||||||
|
size_t burst = mgi_radio_bridge_burst(b, b->current);
|
||||||
|
uint64_t down = mgi_radio_queued_burst_time(b, burst);
|
||||||
|
b->rx_expected = 0xff;
|
||||||
|
down += mgi_radio_burst_time(MGI_ETH_MAX);
|
||||||
|
mgi_radio_tx_begin(b, burst != 0, now,
|
||||||
|
now + down + 2 * MGI_RADIO_IFS_US + MGI_JOIN_TURN_US);
|
||||||
|
} else if (mgi_radio_control_send(b, 0xffffu, MGI_JOIN_DOWN)) {
|
||||||
|
b->phase = MGI_TURN_JOIN;
|
||||||
|
b->deadline = now + MGI_JOIN_WINDOW_US;
|
||||||
|
}
|
||||||
|
if (b->deadline <= now) b->deadline = now + MGI_JOIN_TURN_US;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_node_rx(struct mg_radio_node *n, uint64_t now,
|
||||||
|
uint8_t *frame, size_t len) {
|
||||||
|
uint16_t dst = 0, src = 0;
|
||||||
|
unsigned command = mgi_radio_frame_parse(frame, len, &dst, &src);
|
||||||
|
if (command == MGI_JOIN_DOWN && len == MGI_FRAME_HDR_LEN && dst == 0xffffu) {
|
||||||
|
// A round without our turn starts the timeout, a turn clears it
|
||||||
|
if (n->joined && src == n->coordinator && n->granted) {
|
||||||
|
n->granted = false;
|
||||||
|
} else if (n->joined && src == n->coordinator && n->expire_at == 0) {
|
||||||
|
n->expire_at = now + MG_RADIO_TIMEOUT_MS * 1000;
|
||||||
|
} else if (n->joined && src == n->coordinator && now >= n->expire_at) {
|
||||||
|
MG_INFO(("Left the mesh: no turn for %d ms", MG_RADIO_TIMEOUT_MS));
|
||||||
|
n->joined = false;
|
||||||
|
n->phase = MGI_TURN_IDLE;
|
||||||
|
n->burst_at = (uint64_t) -1;
|
||||||
|
n->tx_offset = 0;
|
||||||
|
n->rx_expected = 0xff;
|
||||||
|
}
|
||||||
|
if (!n->joined && (mgi_radio_random(n) % 64) == 0) {
|
||||||
|
// Spread contenders across windows as well as within the reply window.
|
||||||
|
n->coordinator = src;
|
||||||
|
n->phase = MGI_TURN_JOIN;
|
||||||
|
n->burst_at =
|
||||||
|
now + MGI_JOIN_TURN_US + mgi_radio_random(n) % MGI_JOIN_JITTER_US;
|
||||||
|
}
|
||||||
|
} else if (command == MGI_DATA_DOWN && n->coordinator != 0 &&
|
||||||
|
dst == n->radio_address && src == n->coordinator) {
|
||||||
|
int received = mgi_radio_data_receive(n, frame, len, now);
|
||||||
|
if (received < 0) return;
|
||||||
|
if (!n->joined) MG_INFO(("Joined the mesh, bridge address %04x", src));
|
||||||
|
n->joined = true;
|
||||||
|
n->granted = true;
|
||||||
|
n->expire_at = 0;
|
||||||
|
if (received != 0) {
|
||||||
|
struct mg_tcpip_if *ifp = n->ifp;
|
||||||
|
if (n->rx_len != 0 || (size_t) received > ifp->recv_queue.size) {
|
||||||
|
ifp->ndrop++;
|
||||||
|
} else {
|
||||||
|
memcpy(ifp->recv_queue.buf, n->rx_buf, (size_t) received);
|
||||||
|
n->rx_len = (size_t) received;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A grant that waited in the receive queue while the node was busy, e.g.
|
||||||
|
// computing TLS keys, is too old to answer. Leave the turn state alone:
|
||||||
|
// queued frames go out on the first grant that can still be answered
|
||||||
|
if (n->now + MGI_ANSWER_MARGIN_US >= now + MG_RADIO_ANSWER_TIMEOUT_US) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (n->burst_at != (uint64_t) -1) mgi_radio_tx_finish(n, false);
|
||||||
|
mgi_radio_tx_begin(n, mg_queue_next(&n->tx, NULL) != 0, now,
|
||||||
|
now + mgi_radio_burst_time(MGI_ETH_MAX) +
|
||||||
|
2 * MGI_RADIO_IFS_US + MGI_JOIN_TURN_US);
|
||||||
|
n->answer_by = now + MG_RADIO_ANSWER_TIMEOUT_US - MGI_ANSWER_MARGIN_US;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle a received radio frame. Its timing counts from its arrival, us
|
||||||
|
static void mgi_radio_input(struct mg_radio_node *node, uint64_t arrived,
|
||||||
|
void *buf, size_t len) {
|
||||||
|
uint64_t ready = node->now + MGI_RADIO_TURNAROUND_US;
|
||||||
|
if (node->tx_at < ready) node->tx_at = ready;
|
||||||
|
if (node->lan != NULL) {
|
||||||
|
mgi_radio_bridge_rx(arrived, node, (const uint8_t *) buf, len);
|
||||||
|
} else {
|
||||||
|
mgi_radio_node_rx(node, arrived, (uint8_t *) buf, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mgi_radio_tick(struct mg_radio_node *node, uint64_t now) {
|
||||||
|
node->now = now;
|
||||||
|
if (node->lan != NULL) {
|
||||||
|
mgi_radio_bridge_io(now, node);
|
||||||
|
mgi_radio_tx_step(node, now);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (node->phase == MGI_TURN_JOIN) {
|
||||||
|
if (node->burst_at <= now) {
|
||||||
|
if (now - node->burst_at <= MGI_JOIN_TURN_US && !node->joined)
|
||||||
|
mgi_radio_control_send(node, node->coordinator, MGI_JOIN_UP);
|
||||||
|
node->burst_at = (uint64_t) -1;
|
||||||
|
node->phase = MGI_TURN_IDLE;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mgi_radio_tx_step(node, now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mg_radio_init(struct mg_tcpip_if *ifp,
|
||||||
|
bool (*hw_init)(struct mg_tcpip_if *, uint16_t addr),
|
||||||
|
bool (*hw_poll)(struct mg_tcpip_if *),
|
||||||
|
size_t (*hw_tx)(struct mg_tcpip_if *, const void *,
|
||||||
|
size_t)) {
|
||||||
|
struct mg_radio_node *node =
|
||||||
|
(struct mg_radio_node *) mg_calloc(1, sizeof(*node));
|
||||||
|
if (node == NULL) return false;
|
||||||
|
node->hw_poll = hw_poll, node->hw_tx = hw_tx;
|
||||||
|
node->ifp = ifp;
|
||||||
|
node->radio_address =
|
||||||
|
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
||||||
|
node->burst_at = (uint64_t) -1;
|
||||||
|
node->rx_expected = 0xff;
|
||||||
|
mg_queue_init(&node->tx, node->tx_buf, sizeof(node->tx_buf));
|
||||||
|
mg_queue_init(&node->frames, node->frames_buf, sizeof(node->frames_buf));
|
||||||
|
ifp->driver_data = node;
|
||||||
|
if (hw_init(ifp, node->radio_address)) return true;
|
||||||
|
ifp->driver_data = NULL;
|
||||||
|
mg_free(node);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mg_radio_bridge(struct mg_tcpip_if *ifp, struct mg_tcpip_if *lan) {
|
||||||
|
struct mg_radio_node *node = (struct mg_radio_node *) ifp->driver_data;
|
||||||
|
node->lan = lan;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mg_radio_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||||
|
struct mg_radio_node *node = (struct mg_radio_node *) ifp->driver_data;
|
||||||
|
size_t n = mgi_radio_enqueue(node, buf, len);
|
||||||
|
if (n != len) { // Mongoose ignores a failed send: count and report it
|
||||||
|
ifp->nerr++;
|
||||||
|
MG_ERROR(("Mesh refused a %u byte frame", (unsigned) len));
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mg_radio_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||||
|
struct mg_radio_node *node = (struct mg_radio_node *) ifp->driver_data;
|
||||||
|
size_t n = node->rx_len;
|
||||||
|
if (n > len) return 0;
|
||||||
|
if (n != 0 && buf != ifp->recv_queue.buf) memcpy(buf, ifp->recv_queue.buf, n);
|
||||||
|
node->rx_len = 0;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mg_radio_rx_queue(struct mg_tcpip_if *ifp, const void *frame, size_t len) {
|
||||||
|
struct mg_radio_node *node = (struct mg_radio_node *) ifp->driver_data;
|
||||||
|
uint64_t arrived = mg_micros();
|
||||||
|
size_t need = MGI_RX_STAMP_LEN + len;
|
||||||
|
uint16_t dst, src;
|
||||||
|
char *p;
|
||||||
|
if (len > MGI_RADIO_MTU ||
|
||||||
|
!mgi_radio_parse_hdr((const uint8_t *) frame, len, &dst, &src) ||
|
||||||
|
(dst != 0xffffu && dst != node->radio_address)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mg_queue_book(&node->frames, &p, need) < need) {
|
||||||
|
ifp->ndrop++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memcpy(p, &arrived, MGI_RX_STAMP_LEN);
|
||||||
|
memcpy(p + MGI_RX_STAMP_LEN, frame, len);
|
||||||
|
mg_queue_add(&node->frames, need);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process queued radio frames, oldest first, each with its arrival time.
|
||||||
|
// Stop while the node holds an Ethernet frame the TCP/IP stack has not taken
|
||||||
|
// yet
|
||||||
|
bool mg_radio_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||||
|
struct mg_radio_node *node = (struct mg_radio_node *) ifp->driver_data;
|
||||||
|
bool tx_done = node->hw_poll(ifp);
|
||||||
|
uint64_t now = mg_micros();
|
||||||
|
char *record;
|
||||||
|
size_t len;
|
||||||
|
(void) s1;
|
||||||
|
if (tx_done) node->tx_at = now + MGI_RADIO_IFS_US;
|
||||||
|
node->now = now;
|
||||||
|
while (node->rx_len == 0 &&
|
||||||
|
(len = mg_queue_next(&node->frames, &record)) != 0) {
|
||||||
|
if (len > MGI_RX_STAMP_LEN) {
|
||||||
|
struct mg_str s =
|
||||||
|
mg_str_n(record + MGI_RX_STAMP_LEN, len - MGI_RX_STAMP_LEN);
|
||||||
|
uint64_t arrived;
|
||||||
|
memcpy(&arrived, record, MGI_RX_STAMP_LEN);
|
||||||
|
mg_tcpip_call(ifp, MG_TCPIP_EV_PHY_RX, &s);
|
||||||
|
mgi_radio_input(node, arrived, s.buf, s.len);
|
||||||
|
}
|
||||||
|
mg_queue_del(&node->frames, len);
|
||||||
|
}
|
||||||
|
mgi_radio_tick(node, now);
|
||||||
|
return node->joined;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
// Copyright (c) 2026 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
//
|
||||||
|
// Ethernet over radio: shared framing, scheduling and bridge forwarding.
|
||||||
|
// Hardware drivers pass their functions to mg_radio_init().
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "net_builtin.h"
|
||||||
|
|
||||||
|
#define MG_RADIO_802154 1 // IEEE 802.15.4, 250 kbit/s
|
||||||
|
#define MG_RADIO_BLE 2 // Bluetooth LE 2 Mbit/s PHY, raw: no BLE link layer
|
||||||
|
|
||||||
|
#ifndef MG_RADIO
|
||||||
|
#define MG_RADIO MG_RADIO_802154
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// The radio network: a channel, and an id that keeps other networks out.
|
||||||
|
// A bridge and its nodes must use the same values
|
||||||
|
#if MG_RADIO == MG_RADIO_BLE
|
||||||
|
#ifndef MG_RADIO_CHANNEL
|
||||||
|
#define MG_RADIO_CHANNEL 36 // Data channel 0-36. 36: 2478 MHz, clear of Wi-Fi
|
||||||
|
#endif
|
||||||
|
#ifndef MG_RADIO_NETWORK
|
||||||
|
#define MG_RADIO_NETWORK 0x71764129 // Access address
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#ifndef MG_RADIO_CHANNEL
|
||||||
|
#define MG_RADIO_CHANNEL 15 // Channel 11-26
|
||||||
|
#endif
|
||||||
|
#ifndef MG_RADIO_NETWORK
|
||||||
|
#define MG_RADIO_NETWORK 0xabcd // PAN ID
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// A turn's answer must arrive this soon after the turn's last frame ends on
|
||||||
|
// air, or the turn ends. A node that would answer later stays silent and keeps
|
||||||
|
// its frame for the next turn. Covers the node's poll latency, the radio
|
||||||
|
// turnaround and one full fragment's airtime, 4.3 ms on 802.15.4
|
||||||
|
#ifndef MG_RADIO_ANSWER_TIMEOUT_US
|
||||||
|
#define MG_RADIO_ANSWER_TIMEOUT_US 8000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// A radio driver aborts a transmission that has not finished this soon, and
|
||||||
|
// reports it done to mg_radio_poll()
|
||||||
|
#ifndef MG_RADIO_TX_TIMEOUT_US
|
||||||
|
#define MG_RADIO_TX_TIMEOUT_US 60000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Membership timeout. The bridge removes a member that has not answered its
|
||||||
|
// turns for this long, counted from the first missed turn. A node leaves the
|
||||||
|
// mesh when it has had no turn for this long
|
||||||
|
#ifndef MG_RADIO_TIMEOUT_MS
|
||||||
|
#define MG_RADIO_TIMEOUT_MS 1000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP
|
||||||
|
|
||||||
|
bool mg_radio_init(struct mg_tcpip_if *ifp,
|
||||||
|
bool (*hw_init)(struct mg_tcpip_if *, uint16_t addr),
|
||||||
|
bool (*hw_poll)(struct mg_tcpip_if *),
|
||||||
|
size_t (*hw_tx)(struct mg_tcpip_if *, const void *, size_t));
|
||||||
|
bool mg_radio_poll(struct mg_tcpip_if *, bool s1); // Run it. true: joined
|
||||||
|
size_t mg_radio_tx(const void *, size_t, struct mg_tcpip_if *); // Queue
|
||||||
|
size_t mg_radio_rx(void *, size_t, struct mg_tcpip_if *); // Received frame
|
||||||
|
|
||||||
|
// Bridge LAN and radio Ethernet frames. Call after both interfaces initialize.
|
||||||
|
void mg_radio_bridge(struct mg_tcpip_if *, struct mg_tcpip_if *lan);
|
||||||
|
|
||||||
|
// Queue a received radio frame with its arrival time. Call it from the
|
||||||
|
// receive interrupt. Frames for other radios are skipped
|
||||||
|
void mg_radio_rx_queue(struct mg_tcpip_if *, const void *, size_t);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#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
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612) && MG_ENABLE_DRIVER_RW612
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -27,8 +27,9 @@ struct mg_tcpip_driver_rw612_data {
|
|||||||
#define MG_DRIVER_MDC_HOLDTIME 3
|
#define MG_DRIVER_MDC_HOLDTIME 3
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MG_TCPIP_DRIVER_INIT
|
extern struct mg_tcpip_driver mg_tcpip_driver_rw612;
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
|
||||||
|
#define MG_TCPIP_DRIVER_RW612_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_rw612_data driver_data_; \
|
static struct mg_tcpip_driver_rw612_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -40,9 +41,12 @@ struct mg_tcpip_driver_rw612_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_rw612; \
|
mif_.driver = &mg_tcpip_driver_rw612; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: rw612, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_RW612_INIT(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+23
-77
@@ -1,10 +1,15 @@
|
|||||||
#include "net_builtin.h"
|
|
||||||
#include "l2_mesh.h"
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "net_builtin.h"
|
||||||
|
#include "radio.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612W) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612W) && \
|
||||||
MG_ENABLE_DRIVER_RW612W
|
MG_ENABLE_DRIVER_RW612W
|
||||||
|
|
||||||
|
#if MG_RADIO != MG_RADIO_802154
|
||||||
|
#error "MG_ENABLE_DRIVER_RW612W supports MG_RADIO_802154 only"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "sha256.h"
|
#include "sha256.h"
|
||||||
|
|
||||||
#ifdef ENET
|
#ifdef ENET
|
||||||
@@ -113,8 +118,6 @@ struct mg_rw612 {
|
|||||||
struct mg_tcpip_if *ifp; // Our interface, for the L2 receive pool
|
struct mg_tcpip_if *ifp; // Our interface, for the L2 receive pool
|
||||||
};
|
};
|
||||||
static struct mg_rw612 s_rw612;
|
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,
|
static void mg_rw612_imu_send(uint8_t type, uint8_t subtype,
|
||||||
const uint32_t *payload, uint8_t len) {
|
const uint32_t *payload, uint8_t len) {
|
||||||
@@ -138,15 +141,7 @@ static void mg_rw612_control(uint8_t subtype, uint32_t a, uint32_t b,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
|
static void mg_rw612_queue_rx(const uint8_t *frame, size_t len) {
|
||||||
{
|
mg_radio_rx_queue(s_rw612.ifp, frame, len);
|
||||||
char *dst;
|
|
||||||
if (mg_queue_book(&s_rw612w_rx, &dst, len) >= len) {
|
|
||||||
memcpy(dst, frame, len);
|
|
||||||
mg_queue_add(&s_rw612w_rx, len);
|
|
||||||
} else {
|
|
||||||
s_rw612.ifp->ndrop++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spinel packed unsigned integer: 7 bits per byte, LSB first
|
// Spinel packed unsigned integer: 7 bits per byte, LSB first
|
||||||
@@ -369,7 +364,7 @@ static size_t mgi_rw612w_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
|||||||
memcpy(f + n, buf, len), n += len;
|
memcpy(f + n, buf, len), n += len;
|
||||||
f[n++] = 0, f[n++] = 0; // FCS, filled in by the radio
|
f[n++] = 0, f[n++] = 0; // FCS, filled in by the radio
|
||||||
(void) ifp;
|
(void) ifp;
|
||||||
f[n++] = MG_802154_CHANNEL;
|
f[n++] = MG_RADIO_CHANNEL;
|
||||||
f[n++] = 0; // Max CSMA backoffs
|
f[n++] = 0; // Max CSMA backoffs
|
||||||
f[n++] = 0; // Max frame retries
|
f[n++] = 0; // Max frame retries
|
||||||
f[n++] = 0; // CSMA disabled for scheduled turns
|
f[n++] = 0; // CSMA disabled for scheduled turns
|
||||||
@@ -378,11 +373,9 @@ static size_t mgi_rw612w_radio_tx(struct mg_tcpip_if *ifp, const void *buf,
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool mgi_rw612w_radio_init(struct mg_tcpip_if *ifp) {
|
static bool mgi_rw612w_radio_init(struct mg_tcpip_if *ifp, uint16_t address) {
|
||||||
struct mg_rw612 *s = &s_rw612;
|
struct mg_rw612 *s = &s_rw612;
|
||||||
struct mg_tcpip_driver_rw612w_data *data = ifp->driver_data;
|
uint8_t f[8], one = 1, zero = 0, pan[2], addr[2], channel = MG_RADIO_CHANNEL;
|
||||||
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;
|
uint64_t expire;
|
||||||
if (MGI_MBOX->magic[0] != MGI_SYNC_MAGIC) {
|
if (MGI_MBOX->magic[0] != MGI_SYNC_MAGIC) {
|
||||||
MG_ERROR(("Radio firmware is not running on CPU2"));
|
MG_ERROR(("Radio firmware is not running on CPU2"));
|
||||||
@@ -421,10 +414,10 @@ static bool mgi_rw612w_radio_init(struct mg_tcpip_if *ifp) {
|
|||||||
if (!mg_rw612_set(MGI_PROP_PHY_ENABLED, &one, 1)) {
|
if (!mg_rw612_set(MGI_PROP_PHY_ENABLED, &one, 1)) {
|
||||||
return mg_rw612_fail("PHY enable failed");
|
return mg_rw612_fail("PHY enable failed");
|
||||||
}
|
}
|
||||||
pan[0] = (uint8_t) MG_802154_PAN_ID;
|
pan[0] = (uint8_t) MG_RADIO_NETWORK;
|
||||||
pan[1] = (uint8_t) (MG_802154_PAN_ID >> 8);
|
pan[1] = (uint8_t) (MG_RADIO_NETWORK >> 8);
|
||||||
addr[0] = (uint8_t) node->radio_address;
|
addr[0] = (uint8_t) address;
|
||||||
addr[1] = (uint8_t) (node->radio_address >> 8);
|
addr[1] = (uint8_t) (address >> 8);
|
||||||
if (!mg_rw612_set(MGI_PROP_PHY_CHAN, &channel, 1) ||
|
if (!mg_rw612_set(MGI_PROP_PHY_CHAN, &channel, 1) ||
|
||||||
!mg_rw612_set(MGI_PROP_PANID, pan, 2) ||
|
!mg_rw612_set(MGI_PROP_PANID, pan, 2) ||
|
||||||
!mg_rw612_set(MGI_PROP_SADDR, addr, 2) ||
|
!mg_rw612_set(MGI_PROP_SADDR, addr, 2) ||
|
||||||
@@ -437,77 +430,30 @@ static bool mgi_rw612w_radio_init(struct mg_tcpip_if *ifp) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t mgi_rw612w_tx(const void *buf, size_t len,
|
static bool mgi_rw612w_radio_poll(struct mg_tcpip_if *ifp) {
|
||||||
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();
|
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
(void) s1;
|
|
||||||
bool was_sending = s_rw612.txing;
|
bool was_sending = s_rw612.txing;
|
||||||
|
(void) ifp;
|
||||||
mg_rw612_poll_imu();
|
mg_rw612_poll_imu();
|
||||||
if (s_rw612.txing &&
|
if (s_rw612.txing &&
|
||||||
(s_rw612.rsp || mg_millis() > s_rw612.txstart + MGI_TX_TIMEOUT_MS)) {
|
(s_rw612.rsp || mg_millis() > s_rw612.txstart + MGI_TX_TIMEOUT_MS)) {
|
||||||
if (!s_rw612.rsp) MG_ERROR(("TX timeout"));
|
if (!s_rw612.rsp) MG_ERROR(("TX timeout"));
|
||||||
s_rw612.txing = false;
|
s_rw612.txing = false;
|
||||||
}
|
}
|
||||||
if (was_sending && !s_rw612.txing)
|
return was_sending && !s_rw612.txing;
|
||||||
node->tx_at = now + node->strategy->radio->ifs_us;
|
|
||||||
node->now = now;
|
|
||||||
while (node->rx_len == 0 && (len = mg_queue_next(&s_rw612w_rx, &buf)) != 0) {
|
|
||||||
node->strategy->rx(node, buf, len);
|
|
||||||
mg_queue_del(&s_rw612w_rx, len);
|
|
||||||
}
|
|
||||||
node->strategy->tick(node, now);
|
|
||||||
return node->joined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The MAC comes from the chip's unique ID
|
||||||
static bool mgi_rw612w_init(struct mg_tcpip_if *ifp) {
|
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];
|
uint8_t uid[FSL_OCOTP_UID_LENGTH] = {0}, digest[32];
|
||||||
uint32_t uid_len = sizeof(uid);
|
uint32_t uid_len = sizeof(uid);
|
||||||
uint16_t address;
|
|
||||||
OCOTP_ReadUniqueID(uid, &uid_len);
|
OCOTP_ReadUniqueID(uid, &uid_len);
|
||||||
mg_sha256(digest, uid, sizeof(uid));
|
mg_sha256(digest, uid, sizeof(uid));
|
||||||
memcpy(ifp->mac, digest, 6);
|
memcpy(ifp->mac, digest, 6);
|
||||||
ifp->mac[0] = 0x02;
|
ifp->mac[0] = 0x02;
|
||||||
address =
|
return mg_radio_init(ifp, mgi_rw612w_radio_init, mgi_rw612w_radio_poll,
|
||||||
(uint16_t) (2 + ((unsigned) ifp->mac[4] * 256 + ifp->mac[5]) % 65532u);
|
mgi_rw612w_radio_tx);
|
||||||
if (strategy == NULL) strategy = &mg_mesh_default;
|
|
||||||
data->mesh = calloc(1, sizeof(*data->mesh));
|
|
||||||
if (data->mesh == NULL) return false;
|
|
||||||
if (!strategy->init(data->mesh, ifp, NULL, address)) {
|
|
||||||
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);
|
|
||||||
free(data->mesh);
|
|
||||||
data->mesh = NULL;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct mg_tcpip_driver mg_tcpip_driver_rw612w = {
|
struct mg_tcpip_driver mg_tcpip_driver_rw612w = {mgi_rw612w_init, mg_radio_tx,
|
||||||
mgi_rw612w_init, mgi_rw612w_tx, mgi_rw612w_rx, mgi_rw612w_poll};
|
mg_radio_rx, mg_radio_poll};
|
||||||
#endif
|
#endif
|
||||||
+2
-10
@@ -6,25 +6,17 @@
|
|||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612W) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RW612W) && \
|
||||||
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;
|
extern struct mg_tcpip_driver mg_tcpip_driver_rw612w;
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_RW612W_INIT(manager) \
|
#define MG_TCPIP_DRIVER_RW612W_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_rw612w_data driver_data_; \
|
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
mif_.ip = MG_TCPIP_IP; \
|
mif_.ip = MG_TCPIP_IP; \
|
||||||
mif_.mask = MG_TCPIP_MASK; \
|
mif_.mask = MG_TCPIP_MASK; \
|
||||||
mif_.gw = MG_TCPIP_GW; \
|
mif_.gw = MG_TCPIP_GW; \
|
||||||
mif_.mgr = (manager); \
|
|
||||||
mif_.driver = &mg_tcpip_driver_rw612w; \
|
mif_.driver = &mg_tcpip_driver_rw612w; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mg_tcpip_init((mgr), &mif_); \
|
||||||
mg_tcpip_init((manager), &mif_); \
|
|
||||||
MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \
|
MG_INFO(("Driver: rw612w, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "phy.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_SAME54) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_SAME54) && \
|
||||||
MG_ENABLE_DRIVER_SAME54
|
MG_ENABLE_DRIVER_SAME54
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ struct mg_tcpip_driver_same54_data {
|
|||||||
uint8_t phy_addr;
|
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 { \
|
do { \
|
||||||
static struct mg_tcpip_driver_same54_data driver_data_; \
|
static struct mg_tcpip_driver_same54_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -28,8 +30,12 @@ struct mg_tcpip_driver_same54_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_same54; \
|
mif_.driver = &mg_tcpip_driver_same54; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: same54, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_SAME54_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
|
#include "log.h"
|
||||||
#include "sdio.h"
|
#include "sdio.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && \
|
#if MG_ENABLE_TCPIP && \
|
||||||
(defined(MG_ENABLE_DRIVER_CYW_SDIO) && MG_ENABLE_DRIVER_CYW_SDIO)
|
(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 "net.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ST67W6) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ST67W6) && \
|
||||||
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 struct mg_tcpip_if *s_ifp;
|
||||||
static uint32_t s_ip, s_mask;
|
static uint32_t s_ip, s_mask;
|
||||||
static bool s_link = false, s_connecting = false;
|
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);
|
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) {
|
static bool st67w6_init(uint8_t *mac) {
|
||||||
// struct mg_tcpip_driver_st67w6_data *d = (struct
|
// struct mg_tcpip_driver_st67w6_data *d = (struct
|
||||||
|
|||||||
+10
-2
@@ -3,6 +3,8 @@
|
|||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ST67W6) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_ST67W6) && \
|
||||||
MG_ENABLE_DRIVER_ST67W6
|
MG_ENABLE_DRIVER_ST67W6
|
||||||
|
|
||||||
|
#include "wifi.h"
|
||||||
|
|
||||||
struct mg_tcpip_driver_st67w6_data {
|
struct mg_tcpip_driver_st67w6_data {
|
||||||
struct mg_wifi_data wifi;
|
struct mg_wifi_data wifi;
|
||||||
void *spi;
|
void *spi;
|
||||||
@@ -10,7 +12,9 @@ struct mg_tcpip_driver_st67w6_data {
|
|||||||
struct mg_queue send_queue; // decouple tx calls from module polls
|
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 { \
|
do { \
|
||||||
static struct mg_tcpip_driver_st67w6_data driver_data_; \
|
static struct mg_tcpip_driver_st67w6_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -22,8 +26,12 @@ struct mg_tcpip_driver_st67w6_data {
|
|||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
mif_.recv_queue.size = 8192; \
|
mif_.recv_queue.size = 8192; \
|
||||||
mif_.mac[0] = 2; /* MAC read from OTP at driver init */ \
|
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)); \
|
MG_INFO(("Driver: st67w6, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_ST67W6_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "phy.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_STM32F) && \
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_STM32F) && \
|
||||||
MG_ENABLE_DRIVER_STM32F
|
MG_ENABLE_DRIVER_STM32F
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ struct mg_tcpip_driver_stm32f_data {
|
|||||||
#define MG_ENABLE_ETH_IRQ()
|
#define MG_ENABLE_ETH_IRQ()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_STM32F_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_stm32f_data driver_data_; \
|
static struct mg_tcpip_driver_stm32f_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -47,8 +49,12 @@ struct mg_tcpip_driver_stm32f_data {
|
|||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
MG_ENABLE_ETH_IRQ(); \
|
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)); \
|
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32F_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && (MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || \
|
#if MG_ENABLE_TCPIP && (MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || \
|
||||||
MG_ENABLE_DRIVER_STM32N)
|
MG_ENABLE_DRIVER_STM32N)
|
||||||
|
|||||||
+10
-2
@@ -19,6 +19,8 @@
|
|||||||
#endif
|
#endif
|
||||||
#if MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || MG_ENABLE_DRIVER_STM32N
|
#if MG_ENABLE_DRIVER_STM32H || MG_ENABLE_DRIVER_MCXN || MG_ENABLE_DRIVER_STM32N
|
||||||
|
|
||||||
|
#include "phy.h"
|
||||||
|
|
||||||
struct mg_tcpip_driver_stm32h_data {
|
struct mg_tcpip_driver_stm32h_data {
|
||||||
// MDC clock divider. MDC clock is derived from HCLK, must not exceed 2.5MHz
|
// MDC clock divider. MDC clock is derived from HCLK, must not exceed 2.5MHz
|
||||||
// HCLK range DIVIDER mdc_cr VALUE
|
// HCLK range DIVIDER mdc_cr VALUE
|
||||||
@@ -72,7 +74,9 @@ struct mg_tcpip_driver_stm32h_data {
|
|||||||
#define MG_IPV6_INIT(mif)
|
#define MG_IPV6_INIT(mif)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_STM32H_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_stm32h_data driver_data_; \
|
static struct mg_tcpip_driver_stm32h_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -86,10 +90,14 @@ struct mg_tcpip_driver_stm32h_data {
|
|||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
MG_IPV6_INIT(mif_); \
|
MG_IPV6_INIT(mif_); \
|
||||||
mg_tcpip_init(mgr, &mif_); \
|
mg_tcpip_init((mgr), &mif_); \
|
||||||
MG_ENABLE_ETH_IRQ(); \
|
MG_ENABLE_ETH_IRQ(); \
|
||||||
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
|
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_STM32H_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_TM4C) && MG_ENABLE_DRIVER_TM4C
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_TM4C) && MG_ENABLE_DRIVER_TM4C
|
||||||
struct tm4c_emac {
|
struct tm4c_emac {
|
||||||
|
|||||||
+8
-2
@@ -19,7 +19,9 @@ struct mg_tcpip_driver_tm4c_data {
|
|||||||
#define MG_DRIVER_MDC_CR 1
|
#define MG_DRIVER_MDC_CR 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_TM4C_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_tm4c_data driver_data_; \
|
static struct mg_tcpip_driver_tm4c_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -30,8 +32,12 @@ struct mg_tcpip_driver_tm4c_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_tm4c; \
|
mif_.driver = &mg_tcpip_driver_tm4c; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: tm4c, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TM4C_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#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
|
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_TMS570) && MG_ENABLE_DRIVER_TMS570
|
||||||
struct tms570_emac_ctrl {
|
struct tms570_emac_ctrl {
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ struct mg_tcpip_driver_tms570_data {
|
|||||||
#define MG_DRIVER_MDC_CR 1
|
#define MG_DRIVER_MDC_CR 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_tms570;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_TMS570_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_tms570_data driver_data_; \
|
static struct mg_tcpip_driver_tms570_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -26,8 +28,13 @@ struct mg_tcpip_driver_tms570_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_tms570; \
|
mif_.driver = &mg_tcpip_driver_tms570; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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));\
|
MG_INFO(("Driver: tms570, MAC: %M", mg_print_mac, mif_.mac));\
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_TMS570_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -0,0 +1,512 @@
|
|||||||
|
#include "net_builtin.h"
|
||||||
|
#include "drivers/usb.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
// USB Ethernet needs the TCP/IP stack, the serial console does not
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && \
|
||||||
|
(MG_ENABLE_DRIVER_USB_RP || MG_ENABLE_DRIVER_USB_NRF52 || \
|
||||||
|
MG_ENABLE_DRIVER_USB_DWC2) && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
|
||||||
|
// Interfaces: ACM control and data 0/1 if enabled, then NCM control and data.
|
||||||
|
// The serial port comes first, so that its name on macOS does not depend on
|
||||||
|
// Ethernet: /dev/cu.usbmodem...01
|
||||||
|
#define MGI_USB_ETH_IF (MG_USB_IF_ETH_DATA - 1)
|
||||||
|
#define MGI_USB_NUM_IFS (2 * MG_ENABLE_USB_ETHERNET + 2 * MG_ENABLE_USB_SERIAL)
|
||||||
|
#define MGI_USB_CONFIG_LEN \
|
||||||
|
(9 + 85 * MG_ENABLE_USB_ETHERNET + 66 * MG_ENABLE_USB_SERIAL)
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
#define MGI_USB_PRODUCT "MongooseUsbEthernet"
|
||||||
|
#else
|
||||||
|
#define MGI_USB_PRODUCT "MongooseUsbSerial"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// VID 0xadde, PID 0x40e4: an example, for development only
|
||||||
|
static const uint8_t s_device_desc[] = {
|
||||||
|
18, 1, 1, 2, 0xef, 2, 1, 64, 0xde, 0xad, 0xe4, 0x40, 0, 1, 1, 2, 3, 1};
|
||||||
|
|
||||||
|
// NCM: endpoints 0x81, 0x02/0x82. ACM: endpoints 0x83, 0x04/0x84
|
||||||
|
// clang-format off
|
||||||
|
static const uint8_t s_config_desc[] = {
|
||||||
|
9, 2, MGI_USB_CONFIG_LEN, 0, MGI_USB_NUM_IFS, 1, 0, 0x80,
|
||||||
|
50, // Config
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
8, 11, 0, 2, 2, 2, 1, 6, // ACM association
|
||||||
|
9, 4, 0, 0, 1, 2, 2, 1, 6, // ACM control interface
|
||||||
|
5, 0x24, 0, 0x20, 1, // CDC header
|
||||||
|
5, 0x24, 1, 0, 1, // Call management
|
||||||
|
4, 0x24, 2, 2, // ACM
|
||||||
|
5, 0x24, 6, 0, 1, // Union
|
||||||
|
7, 5, 0x83, 3, 16, 0, 16, // Notification endpoint
|
||||||
|
9, 4, 1, 0, 2, 10, 0, 0, 0, // Data interface
|
||||||
|
7, 5, 0x04, 2, 64, 0, 0, // Data OUT
|
||||||
|
7, 5, 0x84, 2, 64, 0, 0, // Data IN
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
8, 11, MGI_USB_ETH_IF, 2, 2, 13, 0, 4, // NCM association
|
||||||
|
9, 4, MGI_USB_ETH_IF, 0, 1, 2, 13, 0, 4, // NCM control interface
|
||||||
|
5, 0x24, 0, 0x20, 1, // CDC header
|
||||||
|
5, 0x24, 6, MGI_USB_ETH_IF, MGI_USB_ETH_IF + 1, // Union
|
||||||
|
13, 0x24, 15, 5, 0, 0, 0, 0, 0xea,
|
||||||
|
5, 0, 0, 0, // Eth, 1514 bytes
|
||||||
|
6, 0x24, 0x1a, 0, 1, 0x21, // NCM 1.0
|
||||||
|
7, 5, 0x81, 3, 16, 0, 16, // Notification endpoint
|
||||||
|
9, 4, MGI_USB_ETH_IF + 1, 0, 0, 10, 0, 1, 0, // Data interface, off
|
||||||
|
9, 4, MGI_USB_ETH_IF + 1, 1, 2, 10, 0, 1, 0, // Data interface, on
|
||||||
|
7, 5, 0x02, 2, 64, 0, 0, // Data OUT
|
||||||
|
7, 5, 0x82, 2, 64, 0, 0, // Data IN
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
// Endpoint addresses, for CLEAR_FEATURE(ENDPOINT_HALT)
|
||||||
|
static const uint8_t s_endpoints[] = {
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
0x81, 0x02, 0x82,
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
0x83, 0x04, 0x84,
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
// Windows 11 binds its NCM driver through a Microsoft OS 2.0 compatible ID
|
||||||
|
// clang-format off
|
||||||
|
static const uint8_t s_ms_os_desc[] = {
|
||||||
|
10, 0, 0, 0, 0, 0, 3, 6, 46, 0, // Set header, Windows 8.1+
|
||||||
|
8, 0, 1, 0, 0, 0, 36, 0, // Configuration subset
|
||||||
|
8, 0, 2, 0, MGI_USB_ETH_IF, 0, 28, 0, // Function subset, NCM
|
||||||
|
20, 0, 3, 0, 'W', 'I', 'N', 'N', 'C', 'M', // Compatible ID
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
|
// clang-format on
|
||||||
|
static const uint8_t s_bos_desc[] = {
|
||||||
|
5, 15, 33, 0, 1, 28, 16, 5, 0, 0xdf,
|
||||||
|
0x60, 0xdd, 0xd8, 0x89, 0x45, 0xc7, 0x4c, 0x9c, 0xd2, 0x65,
|
||||||
|
0x9d, 0x9e, 0x64, 0x8a, 0x9f, 0, 0, 3, 6, sizeof(s_ms_os_desc),
|
||||||
|
0, 1, 0};
|
||||||
|
|
||||||
|
// GET_NTB_PARAMETERS reply: NTB16 only, same limits both ways
|
||||||
|
static const uint8_t s_ntb_params[] = {
|
||||||
|
28, 0, 1, 0, MG_USB_NTB_SIZE & 255, MG_USB_NTB_SIZE >> 8, 0, 0, 4, 0, 0, 0,
|
||||||
|
4, 0, 0, 0, MG_USB_NTB_SIZE & 255, MG_USB_NTB_SIZE >> 8, 0, 0, 4, 0, 0, 0,
|
||||||
|
4, 0, 16, 0};
|
||||||
|
|
||||||
|
// Link notifications: speed 12 Mbit/s both ways, connected, disconnected
|
||||||
|
static const uint8_t s_speed[] = {0xa1, 0x2a, 0, 0, 0, 0, 8, 0,
|
||||||
|
0, 0x1b, 0xb7, 0, 0, 0x1b, 0xb7, 0};
|
||||||
|
static const uint8_t s_up[] = {0xa1, 0, 1, 0, 0, 0, 0, 0};
|
||||||
|
static const uint8_t s_down[] = {0xa1, 0, 0, 0, 0, 0, 0, 0};
|
||||||
|
|
||||||
|
// Transfer block header for one frame: NTH16, NDP16 with one datagram at
|
||||||
|
// offset 30, so that the IP header after the 14-byte Ethernet header lands
|
||||||
|
// on a 4-byte boundary
|
||||||
|
static const uint8_t s_nth[30] = {'N', 'C', 'M', 'H', 12, 0, 0, 0, 0, 0,
|
||||||
|
12, 0, 'N', 'C', 'M', '0', 16, 0, 0, 0,
|
||||||
|
30, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// An IN transfer on an endpoint, sent a packet at a time
|
||||||
|
struct mgi_usb_in {
|
||||||
|
const uint8_t *data; // Bytes still to send
|
||||||
|
size_t left; // How many
|
||||||
|
unsigned packets; // Packets still to send: a short or empty one ends it
|
||||||
|
bool busy; // A packet is waiting for the host
|
||||||
|
};
|
||||||
|
|
||||||
|
// The USB device. There is one
|
||||||
|
static struct mgi_usb {
|
||||||
|
const struct mg_usb_hw *hw; // Controller, NULL until mg_usb_init()
|
||||||
|
struct mgi_usb_in in[5]; // IN transfers, by endpoint
|
||||||
|
char serial[17]; // Serial number, from the controller
|
||||||
|
bool configured; // Host has set the configuration
|
||||||
|
uint8_t buf[66]; // Generated control replies
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
struct mg_tcpip_if *ifp; // Ethernet interface, NULL: serial only
|
||||||
|
bool alt; // Ethernet open: data interface setting 1
|
||||||
|
uint16_t filter, sequence; // Host packet filter, next IN block number
|
||||||
|
uint32_t input_size; // Largest IN block the host accepts
|
||||||
|
uint8_t notify; // Pending link notifications
|
||||||
|
struct mg_queue txq; // Transfer blocks for the host, see tx
|
||||||
|
char txq_buf[MG_USB_TX_SIZE]; // Its storage
|
||||||
|
uint8_t rx[MG_USB_NTB_SIZE]; // OUT transfer block being received
|
||||||
|
size_t rxlen; // How much of it arrived
|
||||||
|
bool discard; // It is too long: drop it
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
bool dtr; // Terminal open
|
||||||
|
uint64_t reboot; // mg_millis() to enter the bootloader, or 0
|
||||||
|
uint8_t line[7]; // Serial line coding
|
||||||
|
char log[MG_USB_LOG_SIZE]; // Log ring
|
||||||
|
unsigned head, tail; // Log ring write and read counters
|
||||||
|
char out[64]; // Log output being sent
|
||||||
|
#endif
|
||||||
|
} s_usb;
|
||||||
|
|
||||||
|
// Send the next packet of the IN transfer on ep. False if it is over
|
||||||
|
static bool mgi_usb_in_next(unsigned ep) {
|
||||||
|
struct mgi_usb_in *e = &s_usb.in[ep];
|
||||||
|
size_t n = e->left > 64 ? 64 : e->left;
|
||||||
|
if (e->packets == 0) return false;
|
||||||
|
s_usb.hw->in((enum mg_usb_ep) ep, e->data, n);
|
||||||
|
e->data += n, e->left -= n, e->packets--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start an IN transfer. zlp: end a multiple of 64 bytes with an empty packet
|
||||||
|
static void mgi_usb_in_start(unsigned ep, const void *data, size_t len,
|
||||||
|
bool zlp) {
|
||||||
|
struct mgi_usb_in *e = &s_usb.in[ep];
|
||||||
|
e->data = (const uint8_t *) data, e->left = len;
|
||||||
|
e->packets = (unsigned) (len / 64 + (len % 64 || len == 0 || zlp));
|
||||||
|
e->busy = mgi_usb_in_next(ep);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel the transfers on ep
|
||||||
|
static void mgi_usb_idle(unsigned ep) {
|
||||||
|
memset(&s_usb.in[ep], 0, sizeof(s_usb.in[ep]));
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (ep == MG_USB_EP_ETH_DATA) s_usb.rxlen = 0, s_usb.discard = false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unconfigured, on start, bus reset and SET_CONFIGURATION. Keeps the log
|
||||||
|
static void mgi_usb_unconfigure(void) {
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
static const uint8_t line[7] = {0, 0xc2, 1, 0, 0, 0, 8}; // 115200 8N1
|
||||||
|
s_usb.dtr = false;
|
||||||
|
memcpy(s_usb.line, line, sizeof(line));
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
s_usb.alt = false, s_usb.filter = s_usb.sequence = 0, s_usb.notify = 0;
|
||||||
|
s_usb.input_size = MG_USB_NTB_SIZE;
|
||||||
|
#endif
|
||||||
|
s_usb.configured = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build string descriptor i in s_usb.buf. Returns its length, 0 if none
|
||||||
|
static int mgi_usb_string(unsigned i) {
|
||||||
|
static const char *strings[] = {NULL, "Mongoose", MGI_USB_PRODUCT,
|
||||||
|
NULL, "USB Ethernet", NULL,
|
||||||
|
"USB Serial"};
|
||||||
|
const char *s = i < 7 ? strings[i] : NULL;
|
||||||
|
char tmp[33]; // Built strings: product name, host MAC
|
||||||
|
uint8_t *b = s_usb.buf;
|
||||||
|
size_t k, n;
|
||||||
|
if (i == 0) return b[0] = 4, b[1] = 3, b[2] = 9, b[3] = 4;
|
||||||
|
if (i == 2) { // Serial's last 4 digits: tell devices apart
|
||||||
|
size_t len = strlen(s_usb.serial);
|
||||||
|
mg_snprintf(tmp, sizeof(tmp), "%s_%s", MGI_USB_PRODUCT,
|
||||||
|
s_usb.serial + (len > 4 ? len - 4 : 0));
|
||||||
|
s = tmp;
|
||||||
|
}
|
||||||
|
if (i == 3) s = s_usb.serial;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (i == 5 && s_usb.ifp != NULL) { // Host end: our MAC, first byte changed
|
||||||
|
uint8_t *m = s_usb.ifp->mac;
|
||||||
|
mg_snprintf(tmp, sizeof(tmp), "%02X%02X%02X%02X%02X%02X",
|
||||||
|
((m[0] ^ 4) | 2) & ~1, m[1], m[2], m[3], m[4], m[5]);
|
||||||
|
s = tmp;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (s == NULL) return 0;
|
||||||
|
n = strlen(s) > 32 ? 32 : strlen(s);
|
||||||
|
memset(b, 0, sizeof(s_usb.buf));
|
||||||
|
for (k = 0; k < n; k++) b[2 + k * 2] = (uint8_t) s[k];
|
||||||
|
b[0] = (uint8_t) (2 + n * 2), b[1] = 3;
|
||||||
|
return b[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Answer a control request r, its OUT data at r + 8. Returns -1 to stall,
|
||||||
|
// else the IN reply length in *reply, 0 for status only
|
||||||
|
static int mgi_usb_request(const uint8_t *r, const void **reply) {
|
||||||
|
unsigned value = MG_LOAD_LE16(r + 2), index = MG_LOAD_LE16(r + 4);
|
||||||
|
unsigned wlen = MG_LOAD_LE16(r + 6), i;
|
||||||
|
const void *d = s_usb.buf;
|
||||||
|
int n = 0;
|
||||||
|
switch ((r[0] << 8) | r[1]) {
|
||||||
|
case 0x8006: // GET_DESCRIPTOR
|
||||||
|
if (value >> 8 == 1) d = s_device_desc, n = sizeof(s_device_desc);
|
||||||
|
if (value >> 8 == 2) d = s_config_desc, n = sizeof(s_config_desc);
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (value >> 8 == 15) d = s_bos_desc, n = sizeof(s_bos_desc);
|
||||||
|
#endif
|
||||||
|
if (value >> 8 == 3) n = mgi_usb_string(value & 255);
|
||||||
|
if (n == 0) return -1;
|
||||||
|
break;
|
||||||
|
case 0x0005: // SET_ADDRESS, applied by the controller
|
||||||
|
if (value > 127) return -1;
|
||||||
|
break;
|
||||||
|
case 0x0009: // SET_CONFIGURATION
|
||||||
|
if (value > 1) return -1;
|
||||||
|
mgi_usb_unconfigure();
|
||||||
|
for (i = 1; i < 5; i++) mgi_usb_idle(i);
|
||||||
|
s_usb.configured = value == 1;
|
||||||
|
break;
|
||||||
|
case 0x8008: // GET_CONFIGURATION
|
||||||
|
s_usb.buf[0] = s_usb.configured, n = 1;
|
||||||
|
break;
|
||||||
|
case 0x010b: // SET_INTERFACE: only the Ethernet data interface has 1
|
||||||
|
if (index >= MGI_USB_NUM_IFS ||
|
||||||
|
value > (unsigned) (MG_ENABLE_USB_ETHERNET &&
|
||||||
|
index == MG_USB_IF_ETH_DATA))
|
||||||
|
return -1;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (index == MG_USB_IF_ETH_DATA) {
|
||||||
|
s_usb.alt = value == 1, s_usb.notify = 1;
|
||||||
|
mgi_usb_idle(MG_USB_EP_ETH_DATA);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
case 0x810a: // GET_INTERFACE
|
||||||
|
s_usb.buf[0] = 0, n = 1;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (index == MG_USB_IF_ETH_DATA) s_usb.buf[0] = s_usb.alt;
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
case 0xa183: // GET_NTB_FORMAT: NTB16
|
||||||
|
#endif
|
||||||
|
case 0x8000: // GET_STATUS: device, interface, endpoint
|
||||||
|
case 0x8100:
|
||||||
|
case 0x8200:
|
||||||
|
s_usb.buf[0] = s_usb.buf[1] = 0, n = 2;
|
||||||
|
break;
|
||||||
|
case 0x0201: // CLEAR_FEATURE(ENDPOINT_HALT), applied by the controller
|
||||||
|
if (value || index > 255 ||
|
||||||
|
!memchr(s_endpoints, (int) index, sizeof(s_endpoints)))
|
||||||
|
return -1;
|
||||||
|
break;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
case 0xc001: // Microsoft OS 2.0 descriptor set
|
||||||
|
if (index != 7) return -1;
|
||||||
|
d = s_ms_os_desc, n = sizeof(s_ms_os_desc);
|
||||||
|
break;
|
||||||
|
case 0x2143: // SET_ETHERNET_PACKET_FILTER
|
||||||
|
s_usb.filter = (uint16_t) value;
|
||||||
|
break;
|
||||||
|
case 0xa180: // GET_NTB_PARAMETERS
|
||||||
|
d = s_ntb_params, n = sizeof(s_ntb_params);
|
||||||
|
break;
|
||||||
|
case 0x2184: // SET_NTB_FORMAT: NTB16 only
|
||||||
|
if (value) return -1;
|
||||||
|
break;
|
||||||
|
case 0xa185: // GET_NTB_INPUT_SIZE, one datagram per block
|
||||||
|
memset(s_usb.buf, 0, 8);
|
||||||
|
MG_STORE_LE32(s_usb.buf, s_usb.input_size);
|
||||||
|
s_usb.buf[4] = 1, n = 8;
|
||||||
|
break;
|
||||||
|
case 0x2186: // SET_NTB_INPUT_SIZE
|
||||||
|
if ((wlen != 4 && wlen != 8) || MG_LOAD_LE32(r + 8) < 2048 ||
|
||||||
|
MG_LOAD_LE32(r + 8) > 65535)
|
||||||
|
return -1;
|
||||||
|
s_usb.input_size = MG_LOAD_LE32(r + 8);
|
||||||
|
if (s_usb.input_size > MG_USB_NTB_SIZE)
|
||||||
|
s_usb.input_size = MG_USB_NTB_SIZE;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
case 0xa121: // GET_LINE_CODING
|
||||||
|
d = s_usb.line, n = sizeof(s_usb.line);
|
||||||
|
break;
|
||||||
|
case 0x2120: // SET_LINE_CODING
|
||||||
|
if (wlen != sizeof(s_usb.line)) return -1;
|
||||||
|
memcpy(s_usb.line, r + 8, sizeof(s_usb.line));
|
||||||
|
break;
|
||||||
|
case 0x2122: // SET_CONTROL_LINE_STATE. Closed at 1200 baud: bootloader,
|
||||||
|
s_usb.dtr = value & 1; // after a delay that lets this request complete
|
||||||
|
if (!s_usb.dtr && MG_LOAD_LE32(s_usb.line) == 1200)
|
||||||
|
s_usb.reboot = mg_millis() + 100;
|
||||||
|
break;
|
||||||
|
case 0x2123: // SEND_BREAK
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*reply = d;
|
||||||
|
return n > (int) wlen ? (int) wlen : n;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
// Queue the frames of a received transfer block to the TCP/IP stack.
|
||||||
|
// Offsets come from the host: bound every datagram by the block, and follow
|
||||||
|
// at most 16 datagram tables so that a cyclic chain cannot loop forever
|
||||||
|
static void mgi_usb_decode(const uint8_t *p, size_t len) {
|
||||||
|
size_t block, ndp, end, i, ofs, n, tables = 0;
|
||||||
|
if (s_usb.ifp == NULL || len < 12 || MG_LOAD_LE32(p) != 0x484d434e) return;
|
||||||
|
block = MG_LOAD_LE16(p + 8), ndp = MG_LOAD_LE16(p + 10);
|
||||||
|
if (block > len) return;
|
||||||
|
while (ndp && tables++ < 16 && ndp + 16 <= block &&
|
||||||
|
MG_LOAD_LE32(p + ndp) == 0x304d434e) { // "NCM0"
|
||||||
|
end = ndp + MG_LOAD_LE16(p + ndp + 4);
|
||||||
|
for (i = ndp + 8; i + 4 <= end && end <= block; i += 4) {
|
||||||
|
ofs = MG_LOAD_LE16(p + i), n = MG_LOAD_LE16(p + i + 2);
|
||||||
|
if (ofs == 0 || n == 0) break;
|
||||||
|
if (n >= 14 && n <= 1514 && ofs + n <= block)
|
||||||
|
mg_tcpip_qwrite((void *) (p + ofs), n, s_usb.ifp);
|
||||||
|
}
|
||||||
|
ndp = MG_LOAD_LE16(p + ndp + 6);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the Ethernet IN endpoint is idle, start sending the first queued
|
||||||
|
// transfer block. It stays queued until sent. Drop blocks the host cannot
|
||||||
|
// take now: the link closed, or it lowered its block size
|
||||||
|
static void mgi_usb_tx_next(void) {
|
||||||
|
char *block;
|
||||||
|
size_t len;
|
||||||
|
while (!s_usb.in[MG_USB_EP_ETH_DATA].busy &&
|
||||||
|
(len = mg_queue_next(&s_usb.txq, &block)) > 0) {
|
||||||
|
if (s_usb.alt && s_usb.filter && len <= s_usb.input_size) {
|
||||||
|
mgi_usb_in_start(MG_USB_EP_ETH_DATA, block, len, len < s_usb.input_size);
|
||||||
|
} else {
|
||||||
|
mg_queue_del(&s_usb.txq, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
// Log output function: keep the latest MG_USB_LOG_SIZE characters
|
||||||
|
static void mgi_usb_log(char ch, void *arg) {
|
||||||
|
if (s_usb.head - s_usb.tail >= MG_USB_LOG_SIZE) s_usb.tail++;
|
||||||
|
s_usb.log[s_usb.head++ % MG_USB_LOG_SIZE] = ch;
|
||||||
|
(void) arg;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Controller events, see enum mg_usb_ev
|
||||||
|
static int mgi_usb_event(enum mg_usb_ev ev, enum mg_usb_ep ep, void *buf,
|
||||||
|
size_t len) {
|
||||||
|
const uint8_t *p = (const uint8_t *) buf;
|
||||||
|
unsigned i;
|
||||||
|
if (ev == MG_USB_EV_RESET) {
|
||||||
|
mgi_usb_unconfigure();
|
||||||
|
for (i = 0; i < 5; i++) mgi_usb_idle(i);
|
||||||
|
} else if (ev == MG_USB_EV_SETUP) {
|
||||||
|
const void *reply;
|
||||||
|
unsigned wlen = len < 8 ? 0 : MG_LOAD_LE16(p + 6);
|
||||||
|
int n;
|
||||||
|
mgi_usb_idle(MG_USB_EP_CTL); // A SETUP ends the previous transfer
|
||||||
|
if (len < 8 || (!(p[0] & 0x80) && len < 8 + wlen)) return -1;
|
||||||
|
if ((n = mgi_usb_request(p, &reply)) < 0) return -1;
|
||||||
|
if (!(p[0] & 0x80) || wlen == 0) return 0;
|
||||||
|
mgi_usb_in_start(MG_USB_EP_CTL, reply, (size_t) n, n < (int) wlen);
|
||||||
|
return 1;
|
||||||
|
} else if (ev == MG_USB_EV_IN && s_usb.in[ep].busy) {
|
||||||
|
s_usb.in[ep].busy = mgi_usb_in_next(ep);
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (ep == MG_USB_EP_ETH_DATA && !s_usb.in[ep].busy) {
|
||||||
|
mg_queue_del(&s_usb.txq, mg_queue_next(&s_usb.txq, NULL)); // Sent
|
||||||
|
mgi_usb_tx_next();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return s_usb.in[ep].busy;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
} else if (ev == MG_USB_EV_OUT && ep == MG_USB_EP_ETH_DATA) {
|
||||||
|
// Add the packet to the transfer block. At the advertised maximum OUT
|
||||||
|
// size, the host need not send a ZLP. Other blocks end with a short
|
||||||
|
// packet, possibly empty
|
||||||
|
if (!s_usb.discard && s_usb.rxlen + len <= sizeof(s_usb.rx)) {
|
||||||
|
memcpy(s_usb.rx + s_usb.rxlen, buf, len);
|
||||||
|
s_usb.rxlen += len;
|
||||||
|
} else {
|
||||||
|
s_usb.discard = true;
|
||||||
|
}
|
||||||
|
if (len < 64 || s_usb.rxlen == sizeof(s_usb.rx)) {
|
||||||
|
if (!s_usb.discard) mgi_usb_decode(s_usb.rx, s_usb.rxlen);
|
||||||
|
s_usb.rxlen = 0, s_usb.discard = false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mg_usb_init(struct mg_tcpip_if *ifp, const struct mg_usb_hw *hw) {
|
||||||
|
if (s_usb.hw == NULL) {
|
||||||
|
s_usb.hw = hw;
|
||||||
|
mgi_usb_unconfigure();
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
mg_queue_init(&s_usb.txq, s_usb.txq_buf, sizeof(s_usb.txq_buf));
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
mg_log_set_fn(mgi_usb_log, NULL); // Log to the USB serial port
|
||||||
|
#endif
|
||||||
|
hw->init(s_usb.serial);
|
||||||
|
}
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (ifp != NULL) s_usb.ifp = ifp;
|
||||||
|
#else
|
||||||
|
(void) ifp;
|
||||||
|
#endif
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t mg_usb_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
// Queue the frame wrapped in its transfer block, ready to send
|
||||||
|
size_t n = sizeof(s_nth) + len;
|
||||||
|
char *p;
|
||||||
|
if (!s_usb.alt || !s_usb.filter || len < 14 || len > 1514) return 0;
|
||||||
|
if (mg_queue_next(&s_usb.txq, NULL) == 0) {
|
||||||
|
mg_queue_init(&s_usb.txq, s_usb.txq.buf, s_usb.txq.size); // Unwrap
|
||||||
|
}
|
||||||
|
if (mg_queue_book(&s_usb.txq, &p, n) < n) {
|
||||||
|
ifp->nerr++; // Queue full: the host is not reading
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
memcpy(p, s_nth, sizeof(s_nth));
|
||||||
|
MG_STORE_LE16(p + 6, s_usb.sequence);
|
||||||
|
s_usb.sequence++;
|
||||||
|
MG_STORE_LE16(p + 8, n);
|
||||||
|
MG_STORE_LE16(p + 22, len);
|
||||||
|
memcpy(p + sizeof(s_nth), buf, len);
|
||||||
|
mg_queue_add(&s_usb.txq, n);
|
||||||
|
mgi_usb_tx_next();
|
||||||
|
return len;
|
||||||
|
#else
|
||||||
|
(void) ifp, (void) buf, (void) len;
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mg_usb_poll(struct mg_tcpip_if *ifp, bool s1) {
|
||||||
|
(void) ifp;
|
||||||
|
if (s_usb.hw == NULL) return false; // mg_usb_init() not called yet
|
||||||
|
s_usb.hw->poll(mgi_usb_event);
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
if (s_usb.reboot && mg_millis() > s_usb.reboot) {
|
||||||
|
s_usb.reboot = 0;
|
||||||
|
MG_USB_REBOOT();
|
||||||
|
}
|
||||||
|
if (s_usb.configured && s_usb.dtr && !s_usb.in[MG_USB_EP_SER_DATA].busy) {
|
||||||
|
size_t n = 0;
|
||||||
|
while (n < sizeof(s_usb.out) && s_usb.tail != s_usb.head) {
|
||||||
|
s_usb.out[n++] = s_usb.log[s_usb.tail++ % MG_USB_LOG_SIZE];
|
||||||
|
}
|
||||||
|
if (n > 0) mgi_usb_in_start(MG_USB_EP_SER_DATA, s_usb.out, n, true);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (s_usb.notify && !s_usb.in[MG_USB_EP_ETH_EV].busy) {
|
||||||
|
// Link notifications: the speed first if the link is up, then the state
|
||||||
|
if (s_usb.notify == 1 && s_usb.alt) {
|
||||||
|
s_usb.notify = 2;
|
||||||
|
mgi_usb_in_start(MG_USB_EP_ETH_EV, s_speed, sizeof(s_speed), false);
|
||||||
|
} else {
|
||||||
|
s_usb.notify = 0;
|
||||||
|
mgi_usb_in_start(MG_USB_EP_ETH_EV, s_usb.alt ? s_up : s_down,
|
||||||
|
sizeof(s_up), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mgi_usb_tx_next();
|
||||||
|
return s1 && s_usb.alt && s_usb.filter;
|
||||||
|
#else
|
||||||
|
(void) s1;
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// USB device: a CDC-NCM Ethernet interface, enabled by MG_ENABLE_USB_ETHERNET,
|
||||||
|
// and a CDC-ACM serial port for logs, enabled by MG_ENABLE_USB_SERIAL.
|
||||||
|
|
||||||
|
#ifndef MG_USB_NTB_SIZE
|
||||||
|
#define MG_USB_NTB_SIZE 2048 // Transfer block, each direction, >= 2048
|
||||||
|
#endif
|
||||||
|
#ifndef MG_USB_TX_SIZE
|
||||||
|
#define MG_USB_TX_SIZE 4096 // Ethernet frames waiting to go to the host
|
||||||
|
#endif
|
||||||
|
#ifndef MG_USB_QUEUE_SIZE
|
||||||
|
#define MG_USB_QUEUE_SIZE 4096 // Ethernet frames from the host
|
||||||
|
#endif
|
||||||
|
#ifndef MG_USB_LOG_SIZE
|
||||||
|
#define MG_USB_LOG_SIZE 1024 // Serial log ring
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Restart into the board's bootloader when the host opens the serial port at
|
||||||
|
// 1200 baud and closes it, the "1200 baud touch" that flashing tools use
|
||||||
|
#ifndef MG_USB_REBOOT
|
||||||
|
#define MG_USB_REBOOT() (void) 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Endpoints, numbered as in the configuration descriptor
|
||||||
|
enum mg_usb_ep {
|
||||||
|
MG_USB_EP_CTL, // control
|
||||||
|
MG_USB_EP_ETH_EV, // Ethernet link notifications, IN
|
||||||
|
MG_USB_EP_ETH_DATA, // Ethernet data, IN and OUT
|
||||||
|
MG_USB_EP_SER_EV, // serial notifications, IN, unused
|
||||||
|
MG_USB_EP_SER_DATA, // serial data, IN and OUT
|
||||||
|
};
|
||||||
|
|
||||||
|
// Interfaces: the serial port's control and data first, then Ethernet's. The
|
||||||
|
// Ethernet data interface has setting 1: Ethernet open
|
||||||
|
#define MG_USB_IF_ETH_DATA (2 * MG_ENABLE_USB_SERIAL + 1)
|
||||||
|
|
||||||
|
// Controller events, reported by mg_usb_hw::poll() to its fn. What fn returns
|
||||||
|
enum mg_usb_ev {
|
||||||
|
MG_USB_EV_RESET, // Bus reset or detach. Returns 0
|
||||||
|
MG_USB_EV_SETUP, // Control request, buf: the SETUP packet, then its OUT
|
||||||
|
// data. Returns -1: stall, 0: no IN data stage, send
|
||||||
|
// the status stage, 1: IN data stage started on ep 0
|
||||||
|
MG_USB_EV_IN, // An IN packet on ep went out. Returns 1: next packet
|
||||||
|
// sent, 0: transfer done
|
||||||
|
MG_USB_EV_OUT // An OUT packet arrived on ep, buf and len. Returns 0
|
||||||
|
};
|
||||||
|
|
||||||
|
// A USB device controller, implemented by its driver. Packets: up to 64 bytes
|
||||||
|
struct mg_usb_hw {
|
||||||
|
void (*init)(char serial[17]); // Start, write the serial number
|
||||||
|
void (*poll)(int (*fn)(enum mg_usb_ev, enum mg_usb_ep, void *buf, size_t));
|
||||||
|
void (*in)(enum mg_usb_ep, const void *buf, size_t len); // Write IN packet
|
||||||
|
};
|
||||||
|
|
||||||
|
struct mg_tcpip_if; // Without MG_ENABLE_TCPIP: serial console only
|
||||||
|
|
||||||
|
// Start the controller hw, and attach ifp: NULL for the serial console only.
|
||||||
|
// Call again to attach ifp later; the first call's hw stays in use
|
||||||
|
bool mg_usb_init(struct mg_tcpip_if *ifp, const struct mg_usb_hw *hw);
|
||||||
|
|
||||||
|
// Queue an Ethernet frame for the host. The controller driver's tx
|
||||||
|
size_t mg_usb_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp);
|
||||||
|
|
||||||
|
// Run the controller. The controller driver's poll; returns true when the
|
||||||
|
// host has the Ethernet interface open and s1 is true. Serial only: ifp NULL
|
||||||
|
bool mg_usb_poll(struct mg_tcpip_if *ifp, bool s1);
|
||||||
@@ -0,0 +1,365 @@
|
|||||||
|
#include "net_builtin.h"
|
||||||
|
#include "drivers/usb.h"
|
||||||
|
#include "drivers/usb_dwc2.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && \
|
||||||
|
MG_ENABLE_DRIVER_USB_DWC2 && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
|
||||||
|
// DWC2 device controller, polled: no interrupt handler, no DMA. Received
|
||||||
|
// packets come through one shared RX FIFO, each IN endpoint has a TX FIFO of
|
||||||
|
// its own. Register layout: STM32 reference manuals, OTG chapter.
|
||||||
|
|
||||||
|
#ifndef MG_USB_DWC2_BASE
|
||||||
|
#error "MG_ENABLE_DRIVER_USB_DWC2 needs MG_USB_DWC2_BASE, the core's address"
|
||||||
|
#endif
|
||||||
|
#ifndef MG_USB_DWC2_ULPI
|
||||||
|
#define MG_USB_DWC2_ULPI 0
|
||||||
|
#endif
|
||||||
|
#ifndef MG_USB_DWC2_UTMI
|
||||||
|
#define MG_USB_DWC2_UTMI 0
|
||||||
|
#endif
|
||||||
|
#if MG_USB_DWC2_ULPI && MG_USB_DWC2_UTMI
|
||||||
|
#error "MG_USB_DWC2_ULPI and MG_USB_DWC2_UTMI: choose one PHY"
|
||||||
|
#endif
|
||||||
|
#define MGI_DWC2_HS_PHY (MG_USB_DWC2_ULPI || MG_USB_DWC2_UTMI) // At full speed
|
||||||
|
|
||||||
|
// Register at offset off
|
||||||
|
#define MGI_DWC2(off) \
|
||||||
|
(*(volatile uint32_t *) ((uintptr_t) (MG_USB_DWC2_BASE) + (off)))
|
||||||
|
#define MGI_DWC2_GOTGCTL MGI_DWC2(0x000)
|
||||||
|
#define MGI_DWC2_GUSBCFG MGI_DWC2(0x00c)
|
||||||
|
#define MGI_DWC2_GRSTCTL MGI_DWC2(0x010)
|
||||||
|
#define MGI_DWC2_GINTSTS MGI_DWC2(0x014)
|
||||||
|
#define MGI_DWC2_GRXSTSP MGI_DWC2(0x020)
|
||||||
|
#define MGI_DWC2_GRXFSIZ MGI_DWC2(0x024)
|
||||||
|
#define MGI_DWC2_DIEPTXF0 MGI_DWC2(0x028)
|
||||||
|
#define MGI_DWC2_GCCFG MGI_DWC2(0x038)
|
||||||
|
#define MGI_DWC2_GSNPSID MGI_DWC2(0x040)
|
||||||
|
#define MGI_DWC2_DIEPTXF(n) MGI_DWC2(0x100 + 4 * (n)) // n: 1..
|
||||||
|
#define MGI_DWC2_DCFG MGI_DWC2(0x800)
|
||||||
|
#define MGI_DWC2_DCTL MGI_DWC2(0x804)
|
||||||
|
#define MGI_DWC2_DIEPMSK MGI_DWC2(0x810)
|
||||||
|
#define MGI_DWC2_DOEPMSK MGI_DWC2(0x814)
|
||||||
|
#define MGI_DWC2_DAINT MGI_DWC2(0x818)
|
||||||
|
#define MGI_DWC2_DAINTMSK MGI_DWC2(0x81c)
|
||||||
|
#define MGI_DWC2_DIEPCTL(n) MGI_DWC2(0x900 + 0x20 * (n))
|
||||||
|
#define MGI_DWC2_DIEPINT(n) MGI_DWC2(0x908 + 0x20 * (n))
|
||||||
|
#define MGI_DWC2_DIEPTSIZ(n) MGI_DWC2(0x910 + 0x20 * (n))
|
||||||
|
#define MGI_DWC2_DOEPCTL(n) MGI_DWC2(0xb00 + 0x20 * (n))
|
||||||
|
#define MGI_DWC2_DOEPINT(n) MGI_DWC2(0xb08 + 0x20 * (n))
|
||||||
|
#define MGI_DWC2_DOEPTSIZ(n) MGI_DWC2(0xb10 + 0x20 * (n))
|
||||||
|
#define MGI_DWC2_PCGCCTL MGI_DWC2(0xe00)
|
||||||
|
#define MGI_DWC2_FIFO(n) MGI_DWC2(0x1000 * ((n) + 1))
|
||||||
|
|
||||||
|
// Endpoint control bits, DIEPCTL and DOEPCTL
|
||||||
|
#define MGI_DWC2_USBAEP MG_BIT(15) // Active
|
||||||
|
#define MGI_DWC2_STALL MG_BIT(21)
|
||||||
|
#define MGI_DWC2_CNAK MG_BIT(26)
|
||||||
|
#define MGI_DWC2_SNAK MG_BIT(27)
|
||||||
|
#define MGI_DWC2_SD0PID MG_BIT(28) // Next packet is DATA0
|
||||||
|
#define MGI_DWC2_EPDIS MG_BIT(30)
|
||||||
|
#define MGI_DWC2_EPENA MG_BIT(31)
|
||||||
|
|
||||||
|
// FIFOs, in 32-bit words: RX first, then a packet's room per IN endpoint.
|
||||||
|
// 208 words in all, so it fits a full-speed core's 320
|
||||||
|
#define MGI_DWC2_RX_WORDS 128
|
||||||
|
|
||||||
|
// usb.c's event handler, see mg_usb_hw::poll()
|
||||||
|
#define MGI_DWC2_FN int (*fn)(enum mg_usb_ev, enum mg_usb_ep, void *, size_t)
|
||||||
|
|
||||||
|
static uint32_t s_mgi_dwc2_pkt[64 / 4]; // A received packet
|
||||||
|
static uint8_t s_mgi_dwc2_req[16]; // SETUP packet, then its OUT data
|
||||||
|
static size_t s_mgi_dwc2_req_len; // OUT data received so far
|
||||||
|
static bool s_mgi_dwc2_req_out; // Waiting for that OUT data stage
|
||||||
|
|
||||||
|
// Wait until the mask bits of reg are set, or clear. Bounded
|
||||||
|
static void mgi_dwc2_wait(volatile uint32_t *reg, uint32_t mask, bool set) {
|
||||||
|
unsigned i;
|
||||||
|
for (i = 0; ((*reg & mask) != 0) != set && i < 1000000; i++) (void) 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush the TX FIFO of endpoint n, 0x10: all of them
|
||||||
|
static void mgi_dwc2_flush_tx(unsigned n) {
|
||||||
|
MGI_DWC2_GRSTCTL = MG_BIT(5) | (n << 6); // TXFFLSH, TXFNUM
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GRSTCTL, MG_BIT(5), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::in(): load one packet, then let the endpoint send it
|
||||||
|
static void mgi_dwc2_in(enum mg_usb_ep ep, const void *buf, size_t len) {
|
||||||
|
const uint8_t *p = (const uint8_t *) buf;
|
||||||
|
size_t i, k;
|
||||||
|
MGI_DWC2_DIEPTSIZ(ep) = MG_BIT(19) | (uint32_t) len; // One packet
|
||||||
|
MGI_DWC2_DIEPCTL(ep) |= MGI_DWC2_CNAK | MGI_DWC2_EPENA;
|
||||||
|
for (i = 0; i < len; i += 4) {
|
||||||
|
uint32_t w = 0;
|
||||||
|
for (k = 0; k < 4 && i + k < len; k++) w |= (uint32_t) p[i + k] << (8 * k);
|
||||||
|
MGI_DWC2_FIFO(ep) = w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop an IN transfer on endpoint ep, and empty its TX FIFO
|
||||||
|
static void mgi_dwc2_in_stop(unsigned ep) {
|
||||||
|
if (MGI_DWC2_DIEPCTL(ep) & MGI_DWC2_EPENA) {
|
||||||
|
MGI_DWC2_DIEPCTL(ep) |= MGI_DWC2_SNAK;
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_DIEPINT(ep), MG_BIT(6), true); // INEPNE
|
||||||
|
MGI_DWC2_DIEPCTL(ep) |= MGI_DWC2_EPDIS;
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_DIEPINT(ep), MG_BIT(1), true); // EPDISD
|
||||||
|
MGI_DWC2_DIEPINT(ep) = MG_BIT(1);
|
||||||
|
}
|
||||||
|
mgi_dwc2_flush_tx(ep);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let OUT endpoint ep take one packet. Endpoint 0 also takes SETUPs
|
||||||
|
static void mgi_dwc2_arm(unsigned ep) {
|
||||||
|
MGI_DWC2_DOEPTSIZ(ep) = (ep == 0 ? 3UL << 29 : 0) | MG_BIT(19) | 64;
|
||||||
|
MGI_DWC2_DOEPCTL(ep) |= MGI_DWC2_CNAK | MGI_DWC2_EPENA;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint ep active, idle, DATA0 next. type: 2 bulk, 3 interrupt. out:
|
||||||
|
// also its OUT direction, ready to receive
|
||||||
|
static void mgi_dwc2_open(unsigned ep, uint32_t type, bool out) {
|
||||||
|
uint32_t ctl = 64 | MGI_DWC2_USBAEP | (type << 18) | MGI_DWC2_SD0PID;
|
||||||
|
mgi_dwc2_in_stop(ep);
|
||||||
|
MGI_DWC2_DIEPCTL(ep) = ctl | (ep << 22) | MGI_DWC2_SNAK; // Own TX FIFO
|
||||||
|
if (out) {
|
||||||
|
MGI_DWC2_DOEPCTL(ep) = ctl;
|
||||||
|
mgi_dwc2_arm(ep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stall endpoint 0, until the next SETUP
|
||||||
|
static void mgi_dwc2_stall(void) {
|
||||||
|
MGI_DWC2_DIEPCTL(0) |= MGI_DWC2_STALL;
|
||||||
|
MGI_DWC2_DOEPCTL(0) |= MGI_DWC2_STALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// USB reset: address 0, endpoints off, FIFOs laid out again
|
||||||
|
static void mgi_dwc2_bus_reset(void) {
|
||||||
|
unsigned ep;
|
||||||
|
for (ep = 0; ep < 5; ep++) {
|
||||||
|
MGI_DWC2_DOEPCTL(ep) |= MGI_DWC2_SNAK;
|
||||||
|
if (ep > 0) MGI_DWC2_DOEPCTL(ep) &= ~MGI_DWC2_USBAEP;
|
||||||
|
if (ep > 0) MGI_DWC2_DIEPCTL(ep) &= ~MGI_DWC2_USBAEP;
|
||||||
|
mgi_dwc2_in_stop(ep);
|
||||||
|
}
|
||||||
|
MGI_DWC2_DAINTMSK = 0x1f; // IN endpoints 0-4: transfer complete
|
||||||
|
MGI_DWC2_DIEPMSK = MG_BIT(0); // XFRC
|
||||||
|
MGI_DWC2_DOEPMSK = MG_BIT(3) | 1U; // STUP, XFRC
|
||||||
|
MGI_DWC2_GRSTCTL = MG_BIT(4); // RXFFLSH
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GRSTCTL, MG_BIT(4), false);
|
||||||
|
MGI_DWC2_GRXFSIZ = MGI_DWC2_RX_WORDS;
|
||||||
|
MGI_DWC2_DIEPTXF0 = (16UL << 16) | MGI_DWC2_RX_WORDS;
|
||||||
|
for (ep = 1; ep < 5; ep++) {
|
||||||
|
MGI_DWC2_DIEPTXF(ep) = (16UL << 16) | (MGI_DWC2_RX_WORDS + 16 * ep);
|
||||||
|
}
|
||||||
|
MGI_DWC2_DCFG &= ~(0x7fUL << 4); // Address 0
|
||||||
|
MGI_DWC2_DIEPCTL(0) &= ~3UL; // Endpoint 0: 64-byte packets
|
||||||
|
MGI_DWC2_DOEPCTL(0) &= ~3UL;
|
||||||
|
MGI_DWC2_DOEPTSIZ(0) = 3UL << 29; // Up to 3 SETUPs in a row
|
||||||
|
s_mgi_dwc2_req_out = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A complete control request: usb.c answers it, then the controller follows
|
||||||
|
static void mgi_dwc2_request(MGI_DWC2_FN) {
|
||||||
|
uint8_t *r = s_mgi_dwc2_req;
|
||||||
|
size_t out = (r[0] & 0x80) ? 0 : MG_LOAD_LE16(r + 6); // OUT data stage
|
||||||
|
int res = fn(MG_USB_EV_SETUP, MG_USB_EP_CTL, r, 8 + out);
|
||||||
|
if (res < 0) {
|
||||||
|
mgi_dwc2_stall();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch ((r[0] << 8) | r[1]) {
|
||||||
|
case 0x0005: // SET_ADDRESS: the new address goes before the status stage
|
||||||
|
MGI_DWC2_DCFG =
|
||||||
|
(MGI_DWC2_DCFG & ~(0x7fUL << 4)) | ((uint32_t) (r[2] & 0x7f) << 4);
|
||||||
|
break;
|
||||||
|
case 0x0009: // SET_CONFIGURATION
|
||||||
|
if (r[2] != 1) break;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
mgi_dwc2_open(MG_USB_EP_ETH_EV, 3, false);
|
||||||
|
mgi_dwc2_open(MG_USB_EP_ETH_DATA, 2, true);
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
mgi_dwc2_open(MG_USB_EP_SER_EV, 3, false);
|
||||||
|
mgi_dwc2_open(MG_USB_EP_SER_DATA, 2, true);
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
case 0x010b: // SET_INTERFACE: Ethernet data endpoints back to DATA0
|
||||||
|
if (r[4] == MG_USB_IF_ETH_DATA) {
|
||||||
|
mgi_dwc2_open(MG_USB_EP_ETH_DATA, 2, r[2] == 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case 0x0201: // CLEAR_FEATURE(ENDPOINT_HALT)
|
||||||
|
if (r[4] & 0x80) {
|
||||||
|
MGI_DWC2_DIEPCTL(r[4] & 15) =
|
||||||
|
(MGI_DWC2_DIEPCTL(r[4] & 15) & ~MGI_DWC2_STALL) | MGI_DWC2_SD0PID;
|
||||||
|
} else {
|
||||||
|
MGI_DWC2_DOEPCTL(r[4] & 15) =
|
||||||
|
(MGI_DWC2_DOEPCTL(r[4] & 15) & ~MGI_DWC2_STALL) | MGI_DWC2_SD0PID;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (res == 0) mgi_dwc2_in(MG_USB_EP_CTL, NULL, 0); // Status stage
|
||||||
|
}
|
||||||
|
|
||||||
|
// A SETUP packet: run it, or first receive its OUT data stage
|
||||||
|
static void mgi_dwc2_setup(MGI_DWC2_FN) {
|
||||||
|
uint8_t *r = s_mgi_dwc2_req;
|
||||||
|
unsigned wlen = MG_LOAD_LE16(r + 6);
|
||||||
|
mgi_dwc2_in_stop(0); // A SETUP ends the previous control transfer
|
||||||
|
s_mgi_dwc2_req_out = false;
|
||||||
|
if ((r[0] & 0x80) || wlen == 0) {
|
||||||
|
mgi_dwc2_request(fn);
|
||||||
|
} else if (wlen > sizeof(s_mgi_dwc2_req) - 8) {
|
||||||
|
mgi_dwc2_stall();
|
||||||
|
} else {
|
||||||
|
s_mgi_dwc2_req_out = true, s_mgi_dwc2_req_len = 0;
|
||||||
|
mgi_dwc2_arm(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// One entry of the RX FIFO: a SETUP, OUT data, or the end of either
|
||||||
|
static void mgi_dwc2_rx(MGI_DWC2_FN) {
|
||||||
|
uint32_t st = MGI_DWC2_GRXSTSP, i;
|
||||||
|
unsigned ep = st & 15, kind = (st >> 17) & 15;
|
||||||
|
size_t n = (st >> 4) & 0x7ff;
|
||||||
|
uint8_t *r = s_mgi_dwc2_req;
|
||||||
|
for (i = 0; i < (n + 3) / 4; i++) { // Take the packet off the FIFO
|
||||||
|
uint32_t w = MGI_DWC2_FIFO(0);
|
||||||
|
if (i < sizeof(s_mgi_dwc2_pkt) / 4) s_mgi_dwc2_pkt[i] = w;
|
||||||
|
}
|
||||||
|
if (n > sizeof(s_mgi_dwc2_pkt)) n = sizeof(s_mgi_dwc2_pkt);
|
||||||
|
if (kind == 6) { // SETUP packet
|
||||||
|
memcpy(r, s_mgi_dwc2_pkt, 8);
|
||||||
|
} else if (kind == 4) { // SETUP stage done
|
||||||
|
MGI_DWC2_DOEPTSIZ(0) |= 3UL << 29;
|
||||||
|
MGI_DWC2_DOEPINT(0) = MG_BIT(3); // STUP
|
||||||
|
mgi_dwc2_setup(fn);
|
||||||
|
} else if (kind == 2 && ep == 0 && s_mgi_dwc2_req_out) { // Control OUT
|
||||||
|
size_t room = sizeof(s_mgi_dwc2_req) - 8 - s_mgi_dwc2_req_len;
|
||||||
|
memcpy(r + 8 + s_mgi_dwc2_req_len, s_mgi_dwc2_pkt, n < room ? n : room);
|
||||||
|
s_mgi_dwc2_req_len += n;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
} else if (kind == 2 && ep == MG_USB_EP_ETH_DATA) {
|
||||||
|
fn(MG_USB_EV_OUT, MG_USB_EP_ETH_DATA, s_mgi_dwc2_pkt, n);
|
||||||
|
#endif
|
||||||
|
} else if (kind == 3 && ep == 0 && s_mgi_dwc2_req_out) { // OUT stage done
|
||||||
|
s_mgi_dwc2_req_out = false;
|
||||||
|
if (s_mgi_dwc2_req_len != MG_LOAD_LE16(r + 6)) {
|
||||||
|
mgi_dwc2_stall();
|
||||||
|
} else {
|
||||||
|
mgi_dwc2_request(fn);
|
||||||
|
}
|
||||||
|
} else if (kind == 3 && ep != 0) { // An OUT packet taken: take the next
|
||||||
|
mgi_dwc2_arm(ep); // Serial input is dropped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::poll()
|
||||||
|
static void mgi_dwc2_poll(MGI_DWC2_FN) {
|
||||||
|
uint32_t sts = MGI_DWC2_GINTSTS;
|
||||||
|
if (sts & MG_BIT(12)) { // USBRST
|
||||||
|
MGI_DWC2_GINTSTS = MG_BIT(12);
|
||||||
|
mgi_dwc2_bus_reset();
|
||||||
|
fn(MG_USB_EV_RESET, MG_USB_EP_CTL, NULL, 0);
|
||||||
|
}
|
||||||
|
if (sts & MG_BIT(13)) MGI_DWC2_GINTSTS = MG_BIT(13); // ENUMDNE
|
||||||
|
while (MGI_DWC2_GINTSTS & MG_BIT(4)) mgi_dwc2_rx(fn); // RXFLVL
|
||||||
|
if (sts & MG_BIT(18)) { // IEPINT
|
||||||
|
uint32_t daint = MGI_DWC2_DAINT;
|
||||||
|
unsigned ep;
|
||||||
|
for (ep = 0; ep < 5; ep++) {
|
||||||
|
uint32_t ints;
|
||||||
|
if (!(daint & MG_BIT(ep))) continue;
|
||||||
|
ints = MGI_DWC2_DIEPINT(ep);
|
||||||
|
MGI_DWC2_DIEPINT(ep) = ints;
|
||||||
|
if (!(ints & 1U)) continue; // XFRC: the packet went out
|
||||||
|
if (!fn(MG_USB_EV_IN, (enum mg_usb_ep) ep, NULL, 0) && ep == 0 &&
|
||||||
|
(s_mgi_dwc2_req[0] & 0x80) && MG_LOAD_LE16(s_mgi_dwc2_req + 6)) {
|
||||||
|
mgi_dwc2_arm(0); // IN data stage over: the host's OUT status stage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Core soft reset. From core v4.20a, the reset done bit says when it is over
|
||||||
|
static void mgi_dwc2_core_reset(void) {
|
||||||
|
bool done_bit = (MGI_DWC2_GSNPSID & 0xffff) >= 0x420a;
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GRSTCTL, MG_BIT(31), true); // AHBIDL
|
||||||
|
MGI_DWC2_GRSTCTL |= 1U; // CSRST
|
||||||
|
if (done_bit) {
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GRSTCTL, MG_BIT(29), true);
|
||||||
|
MGI_DWC2_GRSTCTL = (MGI_DWC2_GRSTCTL & ~1U) | MG_BIT(29);
|
||||||
|
} else {
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GRSTCTL, 1U, false);
|
||||||
|
}
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GRSTCTL, MG_BIT(31), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::init(): the serial number, the core as a full-speed device,
|
||||||
|
// then attach
|
||||||
|
static void mgi_dwc2_start(char serial[17]) {
|
||||||
|
#if defined(UID_BASE) // STM32: from the 96-bit unique ID
|
||||||
|
const volatile uint32_t *uid = (const volatile uint32_t *) UID_BASE;
|
||||||
|
uint32_t id[2] = {uid[0] ^ uid[2], uid[1]};
|
||||||
|
mg_snprintf(serial, 17, "%M", mg_print_hex, (int) sizeof(id), (uint8_t *) id);
|
||||||
|
#else
|
||||||
|
mg_snprintf(serial, 17, "%s", "0000000000000000");
|
||||||
|
#endif
|
||||||
|
#if MG_USB_DWC2_ULPI // 8-bit single data rate, internal VBUS indicator
|
||||||
|
MGI_DWC2_GUSBCFG =
|
||||||
|
(MGI_DWC2_GUSBCFG & ~(MG_BIT(3) | MG_BIT(6) | MG_BIT(7) | MG_BIT(17) |
|
||||||
|
MG_BIT(19) | MG_BIT(20) | MG_BIT(21))) |
|
||||||
|
MG_BIT(4); // ULPI
|
||||||
|
#elif MG_USB_DWC2_UTMI
|
||||||
|
// The core's own high-speed PHY: UTMI, not ULPI or full-speed; no TSDPS
|
||||||
|
MGI_DWC2_GUSBCFG &= ~(MG_BIT(4) | MG_BIT(6) | MG_BIT(22));
|
||||||
|
#else
|
||||||
|
MGI_DWC2_GUSBCFG |= MG_BIT(6); // The core's own full-speed PHY
|
||||||
|
#endif
|
||||||
|
#ifdef MG_USB_DWC2_GCCFG
|
||||||
|
MGI_DWC2_GCCFG = MG_USB_DWC2_GCCFG;
|
||||||
|
#endif
|
||||||
|
mgi_dwc2_core_reset();
|
||||||
|
#ifdef MG_USB_DWC2_GCCFG
|
||||||
|
MGI_DWC2_GCCFG = MG_USB_DWC2_GCCFG;
|
||||||
|
#endif
|
||||||
|
// Turnaround time: 9 for a high-speed PHY, 6 for a fast AHB otherwise.
|
||||||
|
// Timeout calibration: the maximum. Then device mode, which takes a while
|
||||||
|
MGI_DWC2_GUSBCFG = (MGI_DWC2_GUSBCFG & ~((0xfUL << 10) | 7U | MG_BIT(29))) |
|
||||||
|
((MGI_DWC2_HS_PHY ? 9UL : 6UL) << 10) | 7U | MG_BIT(30);
|
||||||
|
mgi_dwc2_wait(&MGI_DWC2_GINTSTS, 1U, false); // CMOD: device mode
|
||||||
|
MGI_DWC2_PCGCCTL = 0; // PHY clock on
|
||||||
|
// Full speed, on a high-speed PHY (DSPD 1) or the core's own full-speed
|
||||||
|
// one (DSPD 3). Stall a non-empty status stage. XCVRDLY: some ULPI PHYs
|
||||||
|
// need it to chirp
|
||||||
|
MGI_DWC2_DCFG = (MGI_DWC2_DCFG & ~3U) | MG_BIT(2) |
|
||||||
|
(MGI_DWC2_HS_PHY ? 1U : 3U) |
|
||||||
|
(MG_USB_DWC2_ULPI ? MG_BIT(14) : 0U);
|
||||||
|
MGI_DWC2_DCTL |= MG_BIT(1); // SDIS: detached while setting up
|
||||||
|
// No VBUS sensing: B-session valid, always
|
||||||
|
MGI_DWC2_GOTGCTL = (MGI_DWC2_GOTGCTL & ~MG_BIT(4)) | MG_BIT(6) | MG_BIT(7);
|
||||||
|
MGI_DWC2_GINTSTS = 0xffffffff;
|
||||||
|
mgi_dwc2_flush_tx(0x10);
|
||||||
|
mgi_dwc2_bus_reset();
|
||||||
|
MGI_DWC2_DCTL &= ~MG_BIT(1); // Attach
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mg_usb_hw mg_usb_hw_dwc2 = {mgi_dwc2_start, mgi_dwc2_poll,
|
||||||
|
mgi_dwc2_in};
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
static bool mgi_dwc2_init(struct mg_tcpip_if *ifp) {
|
||||||
|
return mg_usb_init(ifp, &mg_usb_hw_dwc2);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mg_tcpip_driver mg_tcpip_driver_usb_dwc2 = {mgi_dwc2_init, mg_usb_tx,
|
||||||
|
NULL, mg_usb_poll};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Synopsys DesignWare USB 2.0 OTG controller, "DWC2", as a device, see usb.h:
|
||||||
|
// CDC-NCM Ethernet and a CDC-ACM serial log console. Polled, no DMA, full
|
||||||
|
// speed. STM32 OTG_FS and OTG_HS (F2, F4, F7, H7, L4+, U5) are DWC2. Set in
|
||||||
|
// mongoose_config.h:
|
||||||
|
// - MG_USB_DWC2_BASE: the core's address, e.g. USB1_OTG_HS_PERIPH_BASE. The
|
||||||
|
// application enables its clocks, power and pins
|
||||||
|
// - MG_USB_DWC2_ULPI 1: an external ULPI PHY, run at full speed. Default: the
|
||||||
|
// core's own full-speed PHY
|
||||||
|
// - MG_USB_DWC2_UTMI 1: the core's own high-speed PHY (UTMI), run at full
|
||||||
|
// speed, e.g. STM32U59x/U5Ax OTG_HS. The application powers and clocks it
|
||||||
|
// - MG_USB_DWC2_GCCFG: optional, written to the STM32 GCCFG register, e.g.
|
||||||
|
// its PWRDWN bit to power the core's own full-speed PHY
|
||||||
|
// On STM32, include the device header in mongoose_config.h: the USB serial
|
||||||
|
// number is then the chip's unique ID. Examples:
|
||||||
|
// tutorials/stm32/portenta-h7/minimal-usb (ULPI),
|
||||||
|
// tutorials/stm32/nucleo-u5a5zj-q/minimal-usb (UTMI)
|
||||||
|
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && \
|
||||||
|
MG_ENABLE_DRIVER_USB_DWC2 && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
|
||||||
|
// The controller. Without USB Ethernet, run the serial console from main():
|
||||||
|
// mg_usb_init(NULL, &mg_usb_hw_dwc2) once, mg_usb_poll(NULL, false) in
|
||||||
|
// the main loop
|
||||||
|
extern const struct mg_usb_hw mg_usb_hw_dwc2;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && MG_ENABLE_DRIVER_USB_DWC2 && MG_ENABLE_USB_ETHERNET
|
||||||
|
|
||||||
|
extern struct mg_tcpip_driver mg_tcpip_driver_usb_dwc2;
|
||||||
|
|
||||||
|
// Set MG_TCPIP_IP to give the device a static address and serve DHCP to the
|
||||||
|
// USB host. Leave it unset to get an address over DHCP, e.g. from macOS
|
||||||
|
// Internet Sharing. The device MAC comes from MG_SET_MAC_ADDRESS, random if
|
||||||
|
// it is not set; the host end of the link gets a MAC derived from it
|
||||||
|
#define MG_TCPIP_DRIVER_USB_DWC2_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_.enable_dhcp_server = mif_.ip != 0; \
|
||||||
|
mif_.driver = &mg_tcpip_driver_usb_dwc2; \
|
||||||
|
mif_.recv_queue.size = MG_USB_QUEUE_SIZE; \
|
||||||
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
|
mg_tcpip_init(mgr, &mif_); \
|
||||||
|
MG_INFO(("Driver: usb_dwc2, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(mgr) MG_TCPIP_DRIVER_USB_DWC2_INIT(mgr)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,257 @@
|
|||||||
|
#include "net_builtin.h"
|
||||||
|
#include "drivers/usb.h"
|
||||||
|
#include "drivers/usb_nrf52.h"
|
||||||
|
#include "printf.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && \
|
||||||
|
MG_ENABLE_DRIVER_USB_NRF52 && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
#include <nrf.h>
|
||||||
|
|
||||||
|
// nRF52840 USB device controller, polled, full speed. Endpoint buffers are
|
||||||
|
// inside the controller: EasyDMA moves each packet between them and RAM,
|
||||||
|
// and reads RAM only, so every packet goes through s_mgi_usb_nrf52_pkt. The
|
||||||
|
// controller handles SET_ADDRESS by itself. Errata workarounds follow
|
||||||
|
// Nordic's nrfx_usbd.c: 171, 187 when enabling, 199 around each DMA
|
||||||
|
|
||||||
|
#define MGI_USB_NRF52_ERRATA_199 (*(volatile uint32_t *) 0x40027c1c)
|
||||||
|
|
||||||
|
// usb.c's event handler, see mg_usb_hw::poll()
|
||||||
|
#define MGI_USB_NRF52_FN \
|
||||||
|
int (*fn)(enum mg_usb_ev, enum mg_usb_ep, void *, size_t)
|
||||||
|
|
||||||
|
static uint32_t s_mgi_usb_nrf52_pkt[64 / 4]; // One packet in RAM
|
||||||
|
static uint8_t s_mgi_usb_nrf52_req[16]; // SETUP packet, then its OUT data
|
||||||
|
|
||||||
|
// Write an undocumented register the way Nordic's errata workarounds do
|
||||||
|
static void mgi_usb_nrf52_errata(uint32_t addr, uint32_t value) {
|
||||||
|
volatile uint32_t *unlock = (volatile uint32_t *) 0x4006ec00;
|
||||||
|
bool locked = *unlock == 0;
|
||||||
|
if (locked) *unlock = 0x9375;
|
||||||
|
*(volatile uint32_t *) addr = value;
|
||||||
|
if (locked) *unlock = 0x9375;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run one EasyDMA transfer and wait for its end event
|
||||||
|
static void mgi_usb_nrf52_dma(volatile uint32_t *task, volatile uint32_t *end) {
|
||||||
|
unsigned i;
|
||||||
|
*end = 0;
|
||||||
|
MGI_USB_NRF52_ERRATA_199 = 0x82;
|
||||||
|
*task = 1;
|
||||||
|
for (i = 0; *end == 0 && i < 100000; i++) (void) 0; // Microseconds
|
||||||
|
*end = 0;
|
||||||
|
MGI_USB_NRF52_ERRATA_199 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data toggle of endpoint address ep back to DATA0
|
||||||
|
static void mgi_usb_nrf52_dtoggle(unsigned ep) {
|
||||||
|
NRF_USBD->DTOGGLE = ep; // Select the endpoint, then set its toggle
|
||||||
|
NRF_USBD->DTOGGLE = (USBD_DTOGGLE_VALUE_Data0 << USBD_DTOGGLE_VALUE_Pos) | ep;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::in(): copy the packet to RAM, then into the endpoint
|
||||||
|
static void mgi_usb_nrf52_in(enum mg_usb_ep ep, const void *buf, size_t len) {
|
||||||
|
if (len > 0) memcpy(s_mgi_usb_nrf52_pkt, buf, len);
|
||||||
|
NRF_USBD->EPIN[ep].PTR = (uint32_t) (uintptr_t) s_mgi_usb_nrf52_pkt;
|
||||||
|
NRF_USBD->EPIN[ep].MAXCNT = (uint32_t) len;
|
||||||
|
mgi_usb_nrf52_dma(&NRF_USBD->TASKS_STARTEPIN[ep],
|
||||||
|
&NRF_USBD->EVENTS_ENDEPIN[ep]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move the packet on OUT endpoint ep to RAM, which also lets the endpoint
|
||||||
|
// accept the next one. Returns its length
|
||||||
|
static size_t mgi_usb_nrf52_out(unsigned ep) {
|
||||||
|
size_t n = NRF_USBD->SIZE.EPOUT[ep];
|
||||||
|
if (n == 0) {
|
||||||
|
NRF_USBD->SIZE.EPOUT[ep] = 0; // Nothing to move: accept the next
|
||||||
|
} else {
|
||||||
|
NRF_USBD->EPOUT[ep].PTR = (uint32_t) (uintptr_t) s_mgi_usb_nrf52_pkt;
|
||||||
|
NRF_USBD->EPOUT[ep].MAXCNT = (uint32_t) n;
|
||||||
|
mgi_usb_nrf52_dma(&NRF_USBD->TASKS_STARTEPOUT[ep],
|
||||||
|
&NRF_USBD->EVENTS_ENDEPOUT[ep]);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
// Ethernet data endpoints back to DATA0, OUT ready to receive
|
||||||
|
static void mgi_usb_nrf52_data_reset(void) {
|
||||||
|
mgi_usb_nrf52_dtoggle(0x02), mgi_usb_nrf52_dtoggle(0x82);
|
||||||
|
NRF_USBD->SIZE.EPOUT[2] = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Endpoints 1-4 on and idle if configured, else off
|
||||||
|
static void mgi_usb_nrf52_endpoints(bool on) {
|
||||||
|
NRF_USBD->EPINEN = 1, NRF_USBD->EPOUTEN = 1;
|
||||||
|
if (!on) return;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
NRF_USBD->EPINEN |= MG_BIT(1) | MG_BIT(2), NRF_USBD->EPOUTEN |= MG_BIT(2);
|
||||||
|
mgi_usb_nrf52_dtoggle(0x81);
|
||||||
|
mgi_usb_nrf52_data_reset();
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
NRF_USBD->EPINEN |= MG_BIT(3) | MG_BIT(4), NRF_USBD->EPOUTEN |= MG_BIT(4);
|
||||||
|
mgi_usb_nrf52_dtoggle(0x83), mgi_usb_nrf52_dtoggle(0x04);
|
||||||
|
mgi_usb_nrf52_dtoggle(0x84);
|
||||||
|
NRF_USBD->SIZE.EPOUT[4] = 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// A complete control request: usb.c answers it, then the controller follows
|
||||||
|
static void mgi_usb_nrf52_request(MGI_USB_NRF52_FN) {
|
||||||
|
uint8_t *r = s_mgi_usb_nrf52_req;
|
||||||
|
size_t out = (r[0] & 0x80) ? 0 : MG_LOAD_LE16(r + 6); // OUT data stage
|
||||||
|
int res = fn(MG_USB_EV_SETUP, MG_USB_EP_CTL, r, 8 + out);
|
||||||
|
if (res < 0) {
|
||||||
|
NRF_USBD->TASKS_EP0STALL = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch ((r[0] << 8) | r[1]) {
|
||||||
|
case 0x0009: // SET_CONFIGURATION
|
||||||
|
mgi_usb_nrf52_endpoints(r[2] == 1);
|
||||||
|
break;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
case 0x010b: // SET_INTERFACE
|
||||||
|
if (r[4] == MG_USB_IF_ETH_DATA) mgi_usb_nrf52_data_reset();
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case 0x0201: // CLEAR_FEATURE(ENDPOINT_HALT)
|
||||||
|
mgi_usb_nrf52_dtoggle(r[4]);
|
||||||
|
NRF_USBD->EPSTALL =
|
||||||
|
(USBD_EPSTALL_STALL_UnStall << USBD_EPSTALL_STALL_Pos) | r[4];
|
||||||
|
if (!(r[4] & 0x80)) NRF_USBD->SIZE.EPOUT[r[4] & 15] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (res == 0) NRF_USBD->TASKS_EP0STATUS = 1; // No IN data stage
|
||||||
|
}
|
||||||
|
|
||||||
|
// A SETUP packet: run it, or first receive its OUT data stage
|
||||||
|
static void mgi_usb_nrf52_setup(MGI_USB_NRF52_FN) {
|
||||||
|
const volatile uint32_t *regs = &NRF_USBD->BMREQUESTTYPE;
|
||||||
|
uint8_t *r = s_mgi_usb_nrf52_req;
|
||||||
|
unsigned i, wlen;
|
||||||
|
for (i = 0; i < 8; i++) r[i] = (uint8_t) regs[i];
|
||||||
|
wlen = MG_LOAD_LE16(r + 6);
|
||||||
|
if (r[0] == 0 && r[1] == 5) return; // SET_ADDRESS: the controller's
|
||||||
|
if ((r[0] & 0x80) || wlen == 0) {
|
||||||
|
mgi_usb_nrf52_request(fn);
|
||||||
|
} else if (wlen > sizeof(s_mgi_usb_nrf52_req) - 8) {
|
||||||
|
NRF_USBD->TASKS_EP0STALL = 1;
|
||||||
|
} else {
|
||||||
|
NRF_USBD->TASKS_EP0RCVOUT = 1; // Accept the OUT data stage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A control data stage packet: an IN one acknowledged, or an OUT one arrived
|
||||||
|
static void mgi_usb_nrf52_ep0_data(MGI_USB_NRF52_FN) {
|
||||||
|
uint8_t *r = s_mgi_usb_nrf52_req;
|
||||||
|
size_t n = NRF_USBD->SIZE.EPOUT[0];
|
||||||
|
if (r[0] & 0x80) {
|
||||||
|
if (!fn(MG_USB_EV_IN, MG_USB_EP_CTL, NULL, 0)) {
|
||||||
|
NRF_USBD->TASKS_EP0STATUS = 1;
|
||||||
|
}
|
||||||
|
} else if (n != MG_LOAD_LE16(r + 6)) {
|
||||||
|
NRF_USBD->TASKS_EP0STALL = 1;
|
||||||
|
} else {
|
||||||
|
mgi_usb_nrf52_out(0);
|
||||||
|
memcpy(r + 8, s_mgi_usb_nrf52_pkt, n);
|
||||||
|
mgi_usb_nrf52_request(fn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Follow USB power: enable the controller when VBUS appears, attach to the
|
||||||
|
// bus once both are ready, and detach when VBUS goes. True while attached
|
||||||
|
static bool mgi_usb_nrf52_attach(MGI_USB_NRF52_FN) {
|
||||||
|
uint32_t power = NRF_POWER->USBREGSTATUS;
|
||||||
|
if (!(power & POWER_USBREGSTATUS_VBUSDETECT_Msk)) {
|
||||||
|
if (NRF_USBD->ENABLE) {
|
||||||
|
NRF_USBD->USBPULLUP = 0, NRF_USBD->ENABLE = 0;
|
||||||
|
fn(MG_USB_EV_RESET, MG_USB_EP_CTL, NULL, 0);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!NRF_USBD->ENABLE) {
|
||||||
|
mgi_usb_nrf52_errata(0x4006ed14, 3); // Errata 187
|
||||||
|
mgi_usb_nrf52_errata(0x4006ec14, 0xc0); // Errata 171
|
||||||
|
NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
|
||||||
|
NRF_USBD->ENABLE = 1;
|
||||||
|
}
|
||||||
|
if (NRF_USBD->USBPULLUP) return true;
|
||||||
|
if (!(NRF_USBD->EVENTCAUSE & USBD_EVENTCAUSE_READY_Msk) ||
|
||||||
|
!(power & POWER_USBREGSTATUS_OUTPUTRDY_Msk))
|
||||||
|
return false;
|
||||||
|
NRF_USBD->EVENTCAUSE = USBD_EVENTCAUSE_READY_Msk;
|
||||||
|
mgi_usb_nrf52_errata(0x4006ec14, 0);
|
||||||
|
mgi_usb_nrf52_errata(0x4006ed14, 0);
|
||||||
|
NRF_USBD->USBPULLUP = 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::poll()
|
||||||
|
static void mgi_usb_nrf52_poll(MGI_USB_NRF52_FN) {
|
||||||
|
if (!mgi_usb_nrf52_attach(fn)) return;
|
||||||
|
if (NRF_USBD->EVENTS_USBRESET) {
|
||||||
|
NRF_USBD->EVENTS_USBRESET = 0;
|
||||||
|
NRF_USBD->EVENTS_EP0DATADONE = NRF_USBD->EVENTS_EPDATA = 0;
|
||||||
|
NRF_USBD->EPDATASTATUS = NRF_USBD->EPDATASTATUS;
|
||||||
|
fn(MG_USB_EV_RESET, MG_USB_EP_CTL, NULL, 0);
|
||||||
|
mgi_usb_nrf52_endpoints(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (NRF_USBD->EVENTS_EP0DATADONE) { // Before a new SETUP replaces req
|
||||||
|
NRF_USBD->EVENTS_EP0DATADONE = 0;
|
||||||
|
mgi_usb_nrf52_ep0_data(fn);
|
||||||
|
}
|
||||||
|
if (NRF_USBD->EVENTS_EP0SETUP) {
|
||||||
|
NRF_USBD->EVENTS_EP0SETUP = 0;
|
||||||
|
mgi_usb_nrf52_setup(fn);
|
||||||
|
}
|
||||||
|
if (NRF_USBD->EVENTS_EPDATA) { // Bit n: IN n acknowledged, 16+n: OUT n
|
||||||
|
uint32_t done = NRF_USBD->EPDATASTATUS;
|
||||||
|
unsigned i;
|
||||||
|
NRF_USBD->EVENTS_EPDATA = 0;
|
||||||
|
NRF_USBD->EPDATASTATUS = done;
|
||||||
|
for (i = 1; i < 5; i++) {
|
||||||
|
if (done & MG_BIT(i)) fn(MG_USB_EV_IN, (enum mg_usb_ep) i, NULL, 0);
|
||||||
|
}
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
if (done & MG_BIT(16 + MG_USB_EP_ETH_DATA)) {
|
||||||
|
size_t n = mgi_usb_nrf52_out(MG_USB_EP_ETH_DATA);
|
||||||
|
fn(MG_USB_EV_OUT, MG_USB_EP_ETH_DATA, s_mgi_usb_nrf52_pkt, n);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
if (done & MG_BIT(16 + MG_USB_EP_SER_DATA)) {
|
||||||
|
NRF_USBD->SIZE.EPOUT[MG_USB_EP_SER_DATA] = 0; // Serial input: dropped
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::init(): the serial number, and the crystal that USB needs. The
|
||||||
|
// controller itself starts in poll, once USB power is there
|
||||||
|
static void mgi_usb_nrf52_start(char serial[17]) {
|
||||||
|
uint32_t id[2], xtal = CLOCK_HFCLKSTAT_SRC_Msk | CLOCK_HFCLKSTAT_STATE_Msk;
|
||||||
|
id[0] = NRF_FICR->DEVICEID[0], id[1] = NRF_FICR->DEVICEID[1];
|
||||||
|
mg_snprintf(serial, 17, "%M", mg_print_hex, (int) sizeof(id), (uint8_t *) id);
|
||||||
|
if ((NRF_CLOCK->HFCLKSTAT & xtal) != xtal) {
|
||||||
|
NRF_CLOCK->TASKS_HFCLKSTART = 1;
|
||||||
|
while ((NRF_CLOCK->HFCLKSTAT & xtal) != xtal) (void) 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mg_usb_hw mg_usb_hw_nrf52 = {
|
||||||
|
mgi_usb_nrf52_start, mgi_usb_nrf52_poll, mgi_usb_nrf52_in};
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
static bool mgi_usb_nrf52_init(struct mg_tcpip_if *ifp) {
|
||||||
|
return mg_usb_init(ifp, &mg_usb_hw_nrf52);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mg_tcpip_driver mg_tcpip_driver_usb_nrf52 = {
|
||||||
|
mgi_usb_nrf52_init, mg_usb_tx, NULL, mg_usb_poll};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// nRF52840 USB device controller, see usb.h: CDC-NCM Ethernet and a CDC-ACM
|
||||||
|
// serial log console. No TinyUSB, polled. Needs the nRF52840 device headers
|
||||||
|
// from nrfx/mdk on the include path. Example: tutorials/nordic/xiao-nrf52840
|
||||||
|
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && \
|
||||||
|
MG_ENABLE_DRIVER_USB_NRF52 && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
|
||||||
|
// The controller. Without USB Ethernet, run the serial console from main():
|
||||||
|
// mg_usb_init(NULL, &mg_usb_hw_nrf52) once, mg_usb_poll(NULL, false) in
|
||||||
|
// the main loop
|
||||||
|
extern const struct mg_usb_hw mg_usb_hw_nrf52;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && MG_ENABLE_DRIVER_USB_NRF52 && MG_ENABLE_USB_ETHERNET
|
||||||
|
|
||||||
|
extern struct mg_tcpip_driver mg_tcpip_driver_usb_nrf52;
|
||||||
|
|
||||||
|
// Set MG_TCPIP_IP to give the device a static address and serve DHCP to the
|
||||||
|
// USB host. Leave it unset to get an address over DHCP, e.g. from macOS
|
||||||
|
// Internet Sharing. The device MAC comes from MG_SET_MAC_ADDRESS, random if
|
||||||
|
// it is not set; the host end of the link gets a MAC derived from it
|
||||||
|
#define MG_TCPIP_DRIVER_USB_NRF52_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_.enable_dhcp_server = mif_.ip != 0; \
|
||||||
|
mif_.driver = &mg_tcpip_driver_usb_nrf52; \
|
||||||
|
mif_.recv_queue.size = MG_USB_QUEUE_SIZE; \
|
||||||
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
|
mg_tcpip_init(mgr, &mif_); \
|
||||||
|
MG_INFO(("Driver: usb_nrf52, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
// Like nrf52.h: both nRF52 drivers can be enabled at once, e.g. in a bridge
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(mgr) MG_TCPIP_DRIVER_USB_NRF52_INIT(mgr)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,235 @@
|
|||||||
|
#include "net_builtin.h"
|
||||||
|
#include "drivers/usb.h"
|
||||||
|
#include "drivers/usb_rp.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && MG_ENABLE_DRIVER_USB_RP && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
|
||||||
|
// RP2040 / RP2350 USB device controller: single core, polled, full speed,
|
||||||
|
// 64-byte single-buffered endpoints in USB DPRAM
|
||||||
|
|
||||||
|
// usb.c's event handler, see mg_usb_hw::poll()
|
||||||
|
#define MGI_USB_RP_FN int (*fn)(enum mg_usb_ev, enum mg_usb_ep, void *, size_t)
|
||||||
|
|
||||||
|
struct mgi_usb_rp_ep {
|
||||||
|
uint8_t *buf; // Endpoint buffer in USB DPRAM
|
||||||
|
io_rw_32 *ctrl; // Buffer control register
|
||||||
|
uint8_t pid; // Next DATA PID, 0 or 1
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct mgi_usb_rp_ep s_mgi_usb_rp_ep[5][2]; // Endpoint, OUT=0 IN=1
|
||||||
|
static uint8_t s_mgi_usb_rp_req[16]; // SETUP packet, then its OUT data
|
||||||
|
static bool s_mgi_usb_rp_req_out; // Waiting for that OUT data stage
|
||||||
|
static uint8_t s_mgi_usb_rp_address; // Address to set after the status stage
|
||||||
|
|
||||||
|
// Hand one packet buffer to the controller. OUT: accept up to len bytes
|
||||||
|
static void mgi_usb_rp_packet(unsigned ep, bool in, const void *data,
|
||||||
|
unsigned len) {
|
||||||
|
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[ep][in];
|
||||||
|
uint32_t v = len | (e->pid ? USB_BUF_CTRL_DATA1_PID : 0) |
|
||||||
|
(in ? USB_BUF_CTRL_FULL : 0);
|
||||||
|
if (in && len) memcpy(e->buf, data, len);
|
||||||
|
e->pid ^= 1;
|
||||||
|
*e->ctrl = v;
|
||||||
|
// RP2040 datasheet: allow at least 12 cycles between writing buffer
|
||||||
|
// metadata and handing ownership to the controller through AVAIL
|
||||||
|
busy_wait_at_least_cycles(12);
|
||||||
|
*e->ctrl = v | USB_BUF_CTRL_AVAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::in()
|
||||||
|
static void mgi_usb_rp_in(enum mg_usb_ep ep, const void *buf, size_t len) {
|
||||||
|
mgi_usb_rp_packet(ep, true, buf, (unsigned) len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up endpoint ep, direction in, idle. Endpoints 1 and 3 are interrupt,
|
||||||
|
// 2 and 4 bulk, each with a 64-byte DPRAM buffer of its own
|
||||||
|
static void mgi_usb_rp_ep_init(unsigned ep, bool in) {
|
||||||
|
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[ep][in];
|
||||||
|
e->ctrl =
|
||||||
|
in ? &usb_dpram->ep_buf_ctrl[ep].in : &usb_dpram->ep_buf_ctrl[ep].out;
|
||||||
|
e->buf = ep ? &usb_dpram->epx_data[(ep * 2 + in) * 64] : usb_dpram->ep0_buf_a;
|
||||||
|
e->pid = 0;
|
||||||
|
*e->ctrl = 0;
|
||||||
|
if (ep) {
|
||||||
|
io_rw_32 *ctrl =
|
||||||
|
in ? &usb_dpram->ep_ctrl[ep - 1].in : &usb_dpram->ep_ctrl[ep - 1].out;
|
||||||
|
*ctrl = EP_CTRL_ENABLE_BITS | EP_CTRL_INTERRUPT_PER_BUFFER |
|
||||||
|
((ep & 1 ? 3U : 2U) << EP_CTRL_BUFFER_TYPE_LSB) |
|
||||||
|
(uint32_t) ((uintptr_t) e->buf - (uintptr_t) usb_dpram);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
// Ethernet data endpoints idle, OUT armed to receive if Ethernet is open
|
||||||
|
static void mgi_usb_rp_data_reset(bool open) {
|
||||||
|
mgi_usb_rp_ep_init(2, false), mgi_usb_rp_ep_init(2, true);
|
||||||
|
if (open) mgi_usb_rp_packet(2, false, NULL, 64);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Endpoints 1-4 idle. If configured, the serial port armed to receive
|
||||||
|
static void mgi_usb_rp_endpoints(bool on) {
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
mgi_usb_rp_ep_init(1, true);
|
||||||
|
mgi_usb_rp_data_reset(false);
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
mgi_usb_rp_ep_init(3, true);
|
||||||
|
mgi_usb_rp_ep_init(4, false), mgi_usb_rp_ep_init(4, true);
|
||||||
|
if (on) mgi_usb_rp_packet(4, false, NULL, 64);
|
||||||
|
#endif
|
||||||
|
(void) on;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stall endpoint 0 until the next SETUP
|
||||||
|
static void mgi_usb_rp_stall(void) {
|
||||||
|
usb_hw->ep_stall_arm =
|
||||||
|
USB_EP_STALL_ARM_EP0_IN_BITS | USB_EP_STALL_ARM_EP0_OUT_BITS;
|
||||||
|
*s_mgi_usb_rp_ep[0][1].ctrl = USB_BUF_CTRL_STALL;
|
||||||
|
*s_mgi_usb_rp_ep[0][0].ctrl = USB_BUF_CTRL_STALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A complete control request: usb.c answers it, then the controller follows
|
||||||
|
static void mgi_usb_rp_request(MGI_USB_RP_FN) {
|
||||||
|
uint8_t *r = s_mgi_usb_rp_req;
|
||||||
|
size_t out = (r[0] & 0x80) ? 0 : MG_LOAD_LE16(r + 6); // OUT data stage
|
||||||
|
int res = fn(MG_USB_EV_SETUP, MG_USB_EP_CTL, r, 8 + out);
|
||||||
|
if (res < 0) {
|
||||||
|
mgi_usb_rp_stall();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch ((r[0] << 8) | r[1]) {
|
||||||
|
case 0x0005: // SET_ADDRESS, set after the status stage
|
||||||
|
s_mgi_usb_rp_address = r[2];
|
||||||
|
break;
|
||||||
|
case 0x0009: // SET_CONFIGURATION
|
||||||
|
mgi_usb_rp_endpoints(r[2] == 1);
|
||||||
|
break;
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
case 0x010b: // SET_INTERFACE
|
||||||
|
if (r[4] == MG_USB_IF_ETH_DATA) mgi_usb_rp_data_reset(r[2] == 1);
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case 0x0201: { // CLEAR_FEATURE(ENDPOINT_HALT)
|
||||||
|
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[r[4] & 15][r[4] >> 7];
|
||||||
|
*e->ctrl &= ~USB_BUF_CTRL_STALL, e->pid = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (res == 0) mgi_usb_rp_packet(0, true, NULL, 0); // Status stage, DATA1
|
||||||
|
}
|
||||||
|
|
||||||
|
// A SETUP packet: run it, or first receive its OUT data stage
|
||||||
|
static void mgi_usb_rp_setup(MGI_USB_RP_FN) {
|
||||||
|
uint8_t *r = s_mgi_usb_rp_req;
|
||||||
|
unsigned wlen;
|
||||||
|
memcpy(r, (const void *) usb_dpram->setup_packet, 8);
|
||||||
|
wlen = MG_LOAD_LE16(r + 6);
|
||||||
|
// A SETUP cancels the preceding control transfer. Both its data and
|
||||||
|
// status stages start with DATA1
|
||||||
|
mgi_usb_rp_ep_init(0, false), mgi_usb_rp_ep_init(0, true);
|
||||||
|
s_mgi_usb_rp_ep[0][0].pid = s_mgi_usb_rp_ep[0][1].pid = 1;
|
||||||
|
s_mgi_usb_rp_address = 0, s_mgi_usb_rp_req_out = false;
|
||||||
|
if ((r[0] & 0x80) || wlen == 0) {
|
||||||
|
mgi_usb_rp_request(fn);
|
||||||
|
} else if (wlen > sizeof(s_mgi_usb_rp_req) - 8) {
|
||||||
|
mgi_usb_rp_stall();
|
||||||
|
} else {
|
||||||
|
s_mgi_usb_rp_req_out = true;
|
||||||
|
mgi_usb_rp_packet(0, false, NULL, 64);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A buffer the controller has finished with
|
||||||
|
static void mgi_usb_rp_complete(unsigned ep, bool in, MGI_USB_RP_FN) {
|
||||||
|
struct mgi_usb_rp_ep *e = &s_mgi_usb_rp_ep[ep][in];
|
||||||
|
uint8_t *r = s_mgi_usb_rp_req;
|
||||||
|
unsigned n = *e->ctrl & USB_BUF_CTRL_LEN_MASK;
|
||||||
|
if (in && fn(MG_USB_EV_IN, (enum mg_usb_ep) ep, NULL, 0)) return;
|
||||||
|
if (ep == 0 && in) { // Control IN data or status stage sent
|
||||||
|
if (s_mgi_usb_rp_address) {
|
||||||
|
usb_hw->dev_addr_ctrl = s_mgi_usb_rp_address, s_mgi_usb_rp_address = 0;
|
||||||
|
} else if ((r[0] & 0x80) && MG_LOAD_LE16(r + 6)) {
|
||||||
|
mgi_usb_rp_packet(0, false, NULL, 64); // Host's OUT status stage
|
||||||
|
}
|
||||||
|
} else if (ep == 0 && !in && s_mgi_usb_rp_req_out) { // OUT data stage
|
||||||
|
s_mgi_usb_rp_req_out = false;
|
||||||
|
if (n != MG_LOAD_LE16(r + 6)) {
|
||||||
|
mgi_usb_rp_stall();
|
||||||
|
} else {
|
||||||
|
memcpy(r + 8, e->buf, n);
|
||||||
|
mgi_usb_rp_request(fn);
|
||||||
|
}
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
} else if (ep == MG_USB_EP_ETH_DATA && !in) {
|
||||||
|
fn(MG_USB_EV_OUT, MG_USB_EP_ETH_DATA, e->buf, n);
|
||||||
|
mgi_usb_rp_packet(ep, false, NULL, 64);
|
||||||
|
#endif
|
||||||
|
#if MG_ENABLE_USB_SERIAL
|
||||||
|
} else if (ep == MG_USB_EP_SER_DATA && !in) {
|
||||||
|
mgi_usb_rp_packet(ep, false, NULL, 64); // Serial input: dropped
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::poll()
|
||||||
|
static void mgi_usb_rp_poll(MGI_USB_RP_FN) {
|
||||||
|
uint32_t status = usb_hw->intr, done;
|
||||||
|
unsigned i;
|
||||||
|
if (status & USB_INTR_BUS_RESET_BITS) {
|
||||||
|
usb_hw->sie_status = USB_SIE_STATUS_BUS_RESET_BITS;
|
||||||
|
usb_hw->dev_addr_ctrl = 0;
|
||||||
|
usb_hw->buf_status = 0xffffffff;
|
||||||
|
fn(MG_USB_EV_RESET, MG_USB_EP_CTL, NULL, 0);
|
||||||
|
mgi_usb_rp_ep_init(0, false), mgi_usb_rp_ep_init(0, true);
|
||||||
|
mgi_usb_rp_endpoints(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (status & USB_INTR_SETUP_REQ_BITS) {
|
||||||
|
// Discard stale EP0 completions from an interrupted control transfer
|
||||||
|
usb_hw->buf_status = 3;
|
||||||
|
usb_hw->sie_status = USB_SIE_STATUS_SETUP_REC_BITS;
|
||||||
|
mgi_usb_rp_setup(fn);
|
||||||
|
}
|
||||||
|
done = usb_hw->buf_status;
|
||||||
|
for (i = 0; i < 10; i++) { // Bits: EP0 IN, EP0 OUT, EP1 IN, ...
|
||||||
|
if (!(done & (1U << i))) continue;
|
||||||
|
usb_hw->buf_status = 1U << i;
|
||||||
|
if (s_mgi_usb_rp_ep[i / 2][!(i & 1)].ctrl) {
|
||||||
|
mgi_usb_rp_complete(i / 2, !(i & 1), fn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mg_usb_hw::init(): the serial number, then the controller, attached
|
||||||
|
static void mgi_usb_rp_start(char serial[17]) {
|
||||||
|
pico_get_unique_board_id_string(serial, 17);
|
||||||
|
reset_block(RESETS_RESET_USBCTRL_BITS);
|
||||||
|
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
|
||||||
|
memset(usb_dpram, 0, sizeof(*usb_dpram));
|
||||||
|
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
|
||||||
|
usb_hw->pwr =
|
||||||
|
USB_USB_PWR_VBUS_DETECT_BITS | USB_USB_PWR_VBUS_DETECT_OVERRIDE_EN_BITS;
|
||||||
|
usb_hw->main_ctrl = USB_MAIN_CTRL_CONTROLLER_EN_BITS;
|
||||||
|
usb_hw->sie_ctrl = USB_SIE_CTRL_EP0_INT_1BUF_BITS;
|
||||||
|
usb_hw->inte = 0; // Poll raw status instead of an interrupt handler
|
||||||
|
mgi_usb_rp_ep_init(0, false), mgi_usb_rp_ep_init(0, true);
|
||||||
|
mgi_usb_rp_endpoints(false);
|
||||||
|
hw_set_bits(&usb_hw->sie_ctrl, USB_SIE_CTRL_PULLUP_EN_BITS);
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct mg_usb_hw mg_usb_hw_rp = {mgi_usb_rp_start, mgi_usb_rp_poll,
|
||||||
|
mgi_usb_rp_in};
|
||||||
|
|
||||||
|
#if MG_ENABLE_USB_ETHERNET
|
||||||
|
static bool mgi_usb_rp_init(struct mg_tcpip_if *ifp) {
|
||||||
|
return mg_usb_init(ifp, &mg_usb_hw_rp);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct mg_tcpip_driver mg_tcpip_driver_usb_rp = {mgi_usb_rp_init, mg_usb_tx,
|
||||||
|
NULL, mg_usb_poll};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// RP2040 / RP2350 native USB controller, see usb.h: CDC-NCM Ethernet and
|
||||||
|
// a CDC-ACM serial log console. No TinyUSB, polled. Example: tutorials/rp/usb
|
||||||
|
|
||||||
|
#if (MG_ENABLE_TCPIP || !MG_ENABLE_USB_ETHERNET) && MG_ENABLE_DRIVER_USB_RP && \
|
||||||
|
(MG_ENABLE_USB_ETHERNET || MG_ENABLE_USB_SERIAL)
|
||||||
|
|
||||||
|
// The controller. Without USB Ethernet, run the serial console from main():
|
||||||
|
// mg_usb_init(NULL, &mg_usb_hw_rp) once, mg_usb_poll(NULL, false) in
|
||||||
|
// the main loop
|
||||||
|
extern const struct mg_usb_hw mg_usb_hw_rp;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_TCPIP && MG_ENABLE_DRIVER_USB_RP && MG_ENABLE_USB_ETHERNET
|
||||||
|
|
||||||
|
extern struct mg_tcpip_driver mg_tcpip_driver_usb_rp;
|
||||||
|
|
||||||
|
// Set MG_TCPIP_IP to give the device a static address and serve DHCP to the
|
||||||
|
// USB host. Leave it unset to get an address over DHCP, e.g. from macOS
|
||||||
|
// Internet Sharing. The device MAC comes from MG_SET_MAC_ADDRESS, random if
|
||||||
|
// it is not set; the host end of the link gets a MAC derived from it
|
||||||
|
#define MG_TCPIP_DRIVER_USB_RP_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_.enable_dhcp_server = mif_.ip != 0; \
|
||||||
|
mif_.driver = &mg_tcpip_driver_usb_rp; \
|
||||||
|
mif_.recv_queue.size = MG_USB_QUEUE_SIZE; \
|
||||||
|
MG_SET_MAC_ADDRESS(mif_.mac); \
|
||||||
|
mg_tcpip_init(mgr, &mif_); \
|
||||||
|
MG_INFO(("Driver: usb_rp, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(mgr) MG_TCPIP_DRIVER_USB_RP_INIT(mgr)
|
||||||
|
#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 "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
|
#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
|
#define MG_DRIVER_MDC_CR 4
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_xmc;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_XMC_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_xmc_data driver_data_; \
|
static struct mg_tcpip_driver_xmc_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -40,8 +42,12 @@ struct mg_tcpip_driver_xmc_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_xmc; \
|
mif_.driver = &mg_tcpip_driver_xmc; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: xmc, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
#include "net_builtin.h"
|
#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
|
#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
|
#define MG_DRIVER_MDC_CR 3
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MG_TCPIP_DRIVER_INIT(mgr) \
|
extern struct mg_tcpip_driver mg_tcpip_driver_xmc7;
|
||||||
|
|
||||||
|
#define MG_TCPIP_DRIVER_XMC7_INIT(mgr) \
|
||||||
do { \
|
do { \
|
||||||
static struct mg_tcpip_driver_xmc7_data driver_data_; \
|
static struct mg_tcpip_driver_xmc7_data driver_data_; \
|
||||||
static struct mg_tcpip_if mif_; \
|
static struct mg_tcpip_if mif_; \
|
||||||
@@ -27,9 +29,13 @@ struct mg_tcpip_driver_xmc7_data {
|
|||||||
mif_.driver = &mg_tcpip_driver_xmc7; \
|
mif_.driver = &mg_tcpip_driver_xmc7; \
|
||||||
mif_.driver_data = &driver_data_; \
|
mif_.driver_data = &driver_data_; \
|
||||||
MG_SET_MAC_ADDRESS(mif_.mac); \
|
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)); \
|
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
#ifndef MG_TCPIP_DRIVER_INIT
|
||||||
|
#define MG_TCPIP_DRIVER_INIT(x) MG_TCPIP_DRIVER_XMC7_INIT(x)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
#ifdef MG_OTA_PUBLIC_KEY
|
#ifdef MG_OTA_PUBLIC_KEY
|
||||||
#include "sha256.h"
|
#include "sha256.h"
|
||||||
#include "tls_p256.h"
|
#include "tls_p256.h"
|
||||||
|
|||||||
+1
-1
@@ -1197,7 +1197,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
// POST and PUT should include an entity body. Therefore, they should
|
// POST and PUT should include an entity body. Therefore, they should
|
||||||
// contain a Content-length header (unless the body length is 0, in
|
// contain a Content-length header (unless the body length is 0, in
|
||||||
// which case it can be omitted). Other requests can also contain a
|
// which case it can be omitted). Other requests can also contain a
|
||||||
// body, but their content has no defined semantics (RFC 7231)
|
// body, but their content has no defined semantics (RFC-7231)
|
||||||
// It is a non-chunked request, so the offset (ofs) is updated after
|
// It is a non-chunked request, so the offset (ofs) is updated after
|
||||||
// exiting this branch.
|
// exiting this branch.
|
||||||
if (hm.body.len != 0) require_content_len = true;
|
if (hm.body.len != 0) require_content_len = true;
|
||||||
|
|||||||
@@ -94,13 +94,6 @@ size_t mg_l2_eth_trailer(struct mg_tcpip_if *ifp, size_t len, uint8_t *cur) {
|
|||||||
struct mg_l2addr *mg_l2_eth_mapip(enum mg_l2addrtype addrtype,
|
struct mg_l2addr *mg_l2_eth_mapip(enum mg_l2addrtype addrtype,
|
||||||
struct mg_addr *addr);
|
struct mg_addr *addr);
|
||||||
|
|
||||||
// Read an unaligned little-endian value from byte pointer p into a native integer.
|
|
||||||
// Safe on architectures that forbid unaligned access (e.g. Cortex-M0).
|
|
||||||
#define MG_LOAD_LE32(p) \
|
|
||||||
((uint32_t) (((uint32_t) MG_U8P(p)[3] << 24U) | \
|
|
||||||
((uint32_t) MG_U8P(p)[2] << 16U) | \
|
|
||||||
((uint32_t) MG_U8P(p)[1] << 8U) | MG_U8P(p)[0]))
|
|
||||||
|
|
||||||
bool mg_l2_eth_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
bool mg_l2_eth_rx(struct mg_tcpip_if *ifp, enum mg_l2proto *proto,
|
||||||
struct mg_str *pay, struct mg_str *raw) {
|
struct mg_str *pay, struct mg_str *raw) {
|
||||||
struct eth *eth = (struct eth *) raw->buf;
|
struct eth *eth = (struct eth *) raw->buf;
|
||||||
|
|||||||
-616
@@ -1,616 +0,0 @@
|
|||||||
// Copyright (c) 2026 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
|
|
||||||
#include "l2_mesh.h"
|
|
||||||
#include "log.h"
|
|
||||||
#include "printf.h"
|
|
||||||
#include "util.h"
|
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP
|
|
||||||
|
|
||||||
#define MGI_ETH_MAX 1514
|
|
||||||
#define MGI_TX_DST_LEN 2
|
|
||||||
|
|
||||||
// Mesh header, after the radio header: type, then burst: the fragment's
|
|
||||||
// index << 4 | the last fragment's index. Common to every radio
|
|
||||||
#define MG_MESH_HDR_LEN 2
|
|
||||||
|
|
||||||
#define MG_MESH_FRAME_HDR_LEN (MG_802154_HEADER_LEN + MG_MESH_HDR_LEN)
|
|
||||||
|
|
||||||
// MAC header, 9 bytes: frame control 0x8841, a data frame, PAN ID
|
|
||||||
// compression, short destination and source addresses; sequence number, 0,
|
|
||||||
// as nothing uses it yet; PAN ID; destination; source. Little endian, as
|
|
||||||
// the standard says
|
|
||||||
static void mgi_802154_print_hdr(const struct mg_radio *radio, uint8_t *buf,
|
|
||||||
uint16_t dst, uint16_t src) {
|
|
||||||
buf[0] = 0x41, buf[1] = 0x88, buf[2] = 0;
|
|
||||||
buf[3] = (uint8_t) MG_802154_PAN_ID;
|
|
||||||
buf[4] = (uint8_t) (MG_802154_PAN_ID >> 8);
|
|
||||||
buf[5] = (uint8_t) dst, buf[6] = (uint8_t) (dst >> 8);
|
|
||||||
buf[7] = (uint8_t) src, buf[8] = (uint8_t) (src >> 8);
|
|
||||||
(void) radio;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t mgi_802154_parse_hdr(const struct mg_radio *radio,
|
|
||||||
const uint8_t *buf, size_t len,
|
|
||||||
uint16_t *dst, uint16_t *src) {
|
|
||||||
(void) radio;
|
|
||||||
if (len < MG_802154_HEADER_LEN || buf[0] != 0x41 || buf[1] != 0x88 ||
|
|
||||||
buf[3] != (uint8_t) MG_802154_PAN_ID ||
|
|
||||||
buf[4] != (uint8_t) (MG_802154_PAN_ID >> 8)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
*dst = (uint16_t) (buf[5] | buf[6] << 8);
|
|
||||||
*src = (uint16_t) (buf[7] | buf[8] << 8);
|
|
||||||
return MG_802154_HEADER_LEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
const struct mg_radio mg_radio_802154 = {
|
|
||||||
"802.15.4", // name
|
|
||||||
125, // max_frame_size: 127 minus FCS
|
|
||||||
250000, // bitrate
|
|
||||||
8, // overhead
|
|
||||||
320, // slot_us
|
|
||||||
128, // cca_us
|
|
||||||
100, // turnaround_us
|
|
||||||
640, // ifs_us
|
|
||||||
3, // backoff_min
|
|
||||||
5, // backoff_max
|
|
||||||
true, // csma
|
|
||||||
MG_802154_HEADER_LEN, // header_len
|
|
||||||
mgi_802154_print_hdr, // print_hdr
|
|
||||||
mgi_802154_parse_hdr // parse_hdr
|
|
||||||
};
|
|
||||||
|
|
||||||
static uint64_t mgi_mesh_b_airtime(size_t len) {
|
|
||||||
const struct mg_radio *radio = &mg_radio_802154;
|
|
||||||
return ((uint64_t) (len + radio->overhead) * 8000000 + radio->bitrate - 1) /
|
|
||||||
radio->bitrate;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Each queue record holds a destination and a complete burst.
|
|
||||||
static size_t mgi_mesh_b_queue(struct mg_mesh_node *node, struct mg_queue *q,
|
|
||||||
const void *buf, size_t len, uint16_t dst,
|
|
||||||
uint8_t type) {
|
|
||||||
const struct mg_radio *radio = &mg_radio_802154;
|
|
||||||
const uint8_t *eth = (const uint8_t *) buf;
|
|
||||||
size_t hdr = MG_MESH_FRAME_HDR_LEN, cap = radio->max_frame_size - hdr;
|
|
||||||
size_t frames, size, i;
|
|
||||||
char *record;
|
|
||||||
uint8_t *out;
|
|
||||||
if (len < 14 || len > MGI_ETH_MAX) return 0;
|
|
||||||
frames = (len + cap - 1) / cap;
|
|
||||||
size = MGI_TX_DST_LEN + len + frames * (1 + hdr);
|
|
||||||
if (mg_queue_next(q, NULL) == 0) mg_queue_init(q, q->buf, q->size);
|
|
||||||
if (mg_queue_book(q, &record, size) < size) return 0;
|
|
||||||
out = (uint8_t *) record;
|
|
||||||
*out++ = (uint8_t) (dst >> 8);
|
|
||||||
*out++ = (uint8_t) dst;
|
|
||||||
for (i = 0; i < frames; i++) {
|
|
||||||
size_t chunk = len - i * cap;
|
|
||||||
if (chunk > cap) chunk = cap;
|
|
||||||
*out++ = (uint8_t) (hdr + chunk);
|
|
||||||
radio->print_hdr(radio, out, dst, node->radio_address);
|
|
||||||
out[radio->header_len] = type;
|
|
||||||
out[radio->header_len + 1] = (uint8_t) (i << 4 | (frames - 1));
|
|
||||||
memcpy(out + hdr, eth + i * cap, chunk);
|
|
||||||
out += hdr + chunk;
|
|
||||||
}
|
|
||||||
mg_queue_add(q, size);
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copyright (c) 2026 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
|
|
||||||
#define MGI_JOIN_WINDOW_US 3000
|
|
||||||
#define MGI_JOIN_TURN_US 192
|
|
||||||
#define MGI_JOIN_JITTER_US 640
|
|
||||||
#define MGI_DATA_QUEUE_SIZE 2048
|
|
||||||
#define MGI_TX_RETRY_US 32
|
|
||||||
// Our frames: the radio header, with the radio addresses, then the mesh
|
|
||||||
// header MG_MESH_HDR_LEN, then the payload
|
|
||||||
#define MGI_JOIN_UP_LEN \
|
|
||||||
(MG_MESH_FRAME_HDR_LEN + 6) // MGI_JOIN_UP's payload: MAC addr
|
|
||||||
#define MGI_MAX_FRAGMENTS 14
|
|
||||||
|
|
||||||
enum { MGI_JOIN_DOWN = 1, MGI_JOIN_UP, MGI_DATA_DOWN, MGI_DATA_UP };
|
|
||||||
|
|
||||||
enum { MGI_TURN_IDLE, MGI_TURN_JOIN, MGI_TURN_DATA };
|
|
||||||
|
|
||||||
struct mgi_mesh_b_data {
|
|
||||||
uint8_t rx_buf[MGI_ETH_MAX]; // Ethernet frame being reassembled
|
|
||||||
uint8_t rx_expected; // Expected index | last index, 0xff if idle
|
|
||||||
struct mg_mesh_node *node; // Owner of the TCP/IP interface
|
|
||||||
struct mg_queue tx; // One record per burst of RF fragments
|
|
||||||
size_t tx_offset; // Next queued fragment; zero for an empty frame
|
|
||||||
uint64_t tx_at; // Next fragment or JOIN transmission time
|
|
||||||
uint64_t deadline; // End of the current JOIN or DATA turn
|
|
||||||
struct mgi_mesh_b_peer *fanout; // Next broadcast recipient
|
|
||||||
char tx_buf[MGI_DATA_QUEUE_SIZE]; // Transmit queue storage
|
|
||||||
uint32_t random; // PRNG state for join timing
|
|
||||||
struct mgi_mesh_b_peer *members; // Bridge membership list
|
|
||||||
struct mgi_mesh_b_peer *current; // Member whose turn is active or next
|
|
||||||
int8_t missed_rounds; // -1 after a grant; otherwise completed rounds missed
|
|
||||||
uint16_t coordinator; // Coordinator radio address
|
|
||||||
uint8_t phase; // MGI_TURN_IDLE, MGI_TURN_JOIN, or MGI_TURN_DATA
|
|
||||||
};
|
|
||||||
|
|
||||||
struct mgi_mesh_b_peer {
|
|
||||||
struct mgi_mesh_b_peer *next; // Next member in round-robin order
|
|
||||||
uint16_t radio_address; // Source address supplied in MGI_JOIN_UP
|
|
||||||
uint8_t mac[6]; // Ethernet MAC supplied in MGI_JOIN_UP
|
|
||||||
uint8_t misses; // Turns in a row it did not answer
|
|
||||||
};
|
|
||||||
|
|
||||||
static uint32_t mgi_mesh_b_random(struct mg_mesh_node *node,
|
|
||||||
struct mgi_mesh_b_data *data) {
|
|
||||||
uint32_t *random = &data->random;
|
|
||||||
if (*random == 0) {
|
|
||||||
size_t i;
|
|
||||||
*random = (uint32_t) mg_now();
|
|
||||||
for (i = 0; i < 6; i++) *random = *random * 33u + node->radio->mac[i];
|
|
||||||
if (*random == 0) *random = 1;
|
|
||||||
}
|
|
||||||
*random ^= *random << 13;
|
|
||||||
*random ^= *random >> 17;
|
|
||||||
*random ^= *random << 5;
|
|
||||||
return *random;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct mg_radio *const s_mesh_default_radio = &mg_radio_802154;
|
|
||||||
|
|
||||||
// Read a frame's radio addresses to dst and src, see mg_radio::parse_hdr(), and
|
|
||||||
// return its type, from the mesh header. 0: not a frame of ours
|
|
||||||
static unsigned mgi_mesh_b_frame_parse(const uint8_t *frame, size_t len,
|
|
||||||
uint16_t *dst, uint16_t *src) {
|
|
||||||
if (len < MG_MESH_FRAME_HDR_LEN ||
|
|
||||||
s_mesh_default_radio->parse_hdr(s_mesh_default_radio, frame, len, dst,
|
|
||||||
src) !=
|
|
||||||
s_mesh_default_radio->header_len) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return frame[s_mesh_default_radio->header_len];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write a frame's headers: the radio's, to dst from src, then ours: type,
|
|
||||||
// and burst: high nibble is the index, low nibble is the last index
|
|
||||||
static void mgi_mesh_b_frame_header(uint8_t *frame, uint16_t dst, uint16_t src,
|
|
||||||
uint8_t type, uint8_t burst) {
|
|
||||||
size_t h = s_mesh_default_radio->header_len;
|
|
||||||
s_mesh_default_radio->print_hdr(s_mesh_default_radio, frame, dst, src);
|
|
||||||
frame[h] = type, frame[h + 1] = burst;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool mgi_mesh_b_control_send(struct mg_mesh_node *node, uint16_t dst,
|
|
||||||
uint8_t command) {
|
|
||||||
struct mg_tcpip_if *ifp = node->radio;
|
|
||||||
uint8_t frame[MGI_JOIN_UP_LEN] = {0};
|
|
||||||
size_t len = MG_MESH_FRAME_HDR_LEN;
|
|
||||||
mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0);
|
|
||||||
if (command == MGI_JOIN_UP) {
|
|
||||||
memcpy(frame + MG_MESH_FRAME_HDR_LEN, node->radio->mac, 6);
|
|
||||||
len = MGI_JOIN_UP_LEN;
|
|
||||||
}
|
|
||||||
return node->now >= node->tx_at && node->tx(ifp, frame, len) == len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t mgi_mesh_b_fragment_capacity(void) {
|
|
||||||
return s_mesh_default_radio->max_frame_size - MG_MESH_FRAME_HDR_LEN;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64_t mgi_mesh_b_burst_time(size_t len) {
|
|
||||||
size_t cap = mgi_mesh_b_fragment_capacity();
|
|
||||||
size_t frames = len == 0 ? 1 : (len + cap - 1) / cap;
|
|
||||||
return (frames - 1) *
|
|
||||||
(mgi_mesh_b_airtime(s_mesh_default_radio->max_frame_size) +
|
|
||||||
s_mesh_default_radio->ifs_us) +
|
|
||||||
mgi_mesh_b_airtime(MG_MESH_FRAME_HDR_LEN + len - (frames - 1) * cap);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_tx_finish(struct mgi_mesh_b_data *data) {
|
|
||||||
if (data->tx_offset != 0) {
|
|
||||||
if (data->fanout != NULL) data->fanout = data->fanout->next;
|
|
||||||
if (data->fanout == NULL) {
|
|
||||||
size_t len = mg_queue_next(&data->tx, NULL);
|
|
||||||
mg_queue_del(&data->tx, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
data->tx_offset = 0;
|
|
||||||
data->tx_at = (uint64_t) -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_tx_step(struct mgi_mesh_b_data *data, uint64_t now) {
|
|
||||||
struct mg_mesh_node *node = data->node;
|
|
||||||
struct mg_tcpip_if *radio = node->radio;
|
|
||||||
uint8_t empty[MG_MESH_FRAME_HDR_LEN], *frame = empty;
|
|
||||||
size_t len = MG_MESH_FRAME_HDR_LEN, record_len = 0;
|
|
||||||
uint16_t dst;
|
|
||||||
uint8_t command;
|
|
||||||
if (now < data->tx_at) return;
|
|
||||||
if (now >= data->deadline) {
|
|
||||||
mgi_mesh_b_tx_finish(data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
dst = node->lan == NULL ? data->coordinator : data->current->radio_address;
|
|
||||||
command = node->lan == NULL ? MGI_DATA_UP : MGI_DATA_DOWN;
|
|
||||||
if (data->tx_offset != 0) {
|
|
||||||
char *record;
|
|
||||||
uint8_t *part;
|
|
||||||
record_len = mg_queue_next(&data->tx, &record);
|
|
||||||
part = (uint8_t *) record + data->tx_offset;
|
|
||||||
len = part[0];
|
|
||||||
frame = part + 1;
|
|
||||||
s_mesh_default_radio->print_hdr(s_mesh_default_radio, frame, dst,
|
|
||||||
node->radio_address);
|
|
||||||
} else {
|
|
||||||
mgi_mesh_b_frame_header(frame, dst, node->radio_address, command, 0);
|
|
||||||
}
|
|
||||||
if (now < data->node->tx_at || data->node->tx(radio, frame, len) != len) {
|
|
||||||
data->tx_at = now + MGI_TX_RETRY_US;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (data->tx_offset == 0 || (data->tx_offset += len + 1) == record_len) {
|
|
||||||
mgi_mesh_b_tx_finish(data);
|
|
||||||
} else {
|
|
||||||
data->tx_at = now + mgi_mesh_b_airtime(len) + s_mesh_default_radio->ifs_us;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_tx_begin(struct mgi_mesh_b_data *data, bool burst,
|
|
||||||
uint64_t now, uint64_t deadline) {
|
|
||||||
data->phase = MGI_TURN_DATA;
|
|
||||||
data->tx_offset = burst ? MGI_TX_DST_LEN : 0;
|
|
||||||
data->tx_at = now;
|
|
||||||
data->deadline = deadline;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the assembled length, zero for an empty frame, or -1 if incomplete
|
|
||||||
// or invalid. Full non-final fragments make the write offset derivable.
|
|
||||||
static int mgi_mesh_b_data_receive(struct mgi_mesh_b_data *data,
|
|
||||||
const uint8_t *frame, size_t len,
|
|
||||||
uint64_t now) {
|
|
||||||
unsigned burst = frame[MG_MESH_FRAME_HDR_LEN - 1], index = burst >> 4,
|
|
||||||
last = burst & 15;
|
|
||||||
size_t chunk = len - MG_MESH_FRAME_HDR_LEN,
|
|
||||||
cap = mgi_mesh_b_fragment_capacity();
|
|
||||||
size_t offset = index * cap;
|
|
||||||
if (index == 0) {
|
|
||||||
data->rx_expected = (uint8_t) burst;
|
|
||||||
if (data->node->lan == NULL)
|
|
||||||
data->deadline = now + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
|
|
||||||
2 * s_mesh_default_radio->ifs_us + MGI_JOIN_TURN_US;
|
|
||||||
}
|
|
||||||
if (last >= MGI_MAX_FRAGMENTS || index > last || burst != data->rx_expected ||
|
|
||||||
chunk > cap || now > data->deadline || (index < last && chunk != cap) ||
|
|
||||||
(last != 0 && chunk == 0) || offset + chunk > sizeof(data->rx_buf)) {
|
|
||||||
data->rx_expected = 0xff;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
memcpy(data->rx_buf + offset, frame + MG_MESH_FRAME_HDR_LEN, chunk);
|
|
||||||
if (index < last) {
|
|
||||||
data->rx_expected = (uint8_t) (burst + 0x10);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
data->rx_expected = 0xff;
|
|
||||||
return offset + chunk == 0 || offset + chunk >= 14 ? (int) (offset + chunk)
|
|
||||||
: -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bridge scheduling and Ethernet forwarding.
|
|
||||||
|
|
||||||
#define MGI_MAX_MISSES 3 // Turns in a row without an answer: remove the member
|
|
||||||
|
|
||||||
static uint16_t mgi_mesh_b_join_field(const uint8_t *p) {
|
|
||||||
return (uint16_t) ((unsigned) p[0] * 256 + p[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return chaddr for an Ethernet DHCP server reply, even when broadcast.
|
|
||||||
static const uint8_t *mgi_mesh_b_dhcp_client(const uint8_t *frame, size_t len) {
|
|
||||||
size_t ip_len, ihl, udp_len;
|
|
||||||
const uint8_t *udp, *dhcp;
|
|
||||||
if (len < 34 || mgi_mesh_b_join_field(frame + 12) != 0x0800 ||
|
|
||||||
(frame[14] >> 4) != 4 || frame[23] != 17 ||
|
|
||||||
(mgi_mesh_b_join_field(frame + 20) & 0x3fff) != 0)
|
|
||||||
return NULL;
|
|
||||||
ip_len = mgi_mesh_b_join_field(frame + 16);
|
|
||||||
ihl = (size_t) (frame[14] & 15) * 4;
|
|
||||||
if (ihl < 20 || ip_len > len - 14 || ip_len < ihl + 8 + 240) return NULL;
|
|
||||||
udp = frame + 14 + ihl;
|
|
||||||
udp_len = mgi_mesh_b_join_field(udp + 4);
|
|
||||||
if (mgi_mesh_b_join_field(udp) != 67 ||
|
|
||||||
mgi_mesh_b_join_field(udp + 2) != 68 || udp_len < 8 + 240 ||
|
|
||||||
udp_len > ip_len - ihl)
|
|
||||||
return NULL;
|
|
||||||
dhcp = udp + 8;
|
|
||||||
if (dhcp[0] != 2 || dhcp[1] != 1 || dhcp[2] != 6 ||
|
|
||||||
memcmp(dhcp + 236, "\x63\x82\x53\x63", 4) != 0)
|
|
||||||
return NULL;
|
|
||||||
return dhcp + 28;
|
|
||||||
}
|
|
||||||
|
|
||||||
static size_t mgi_mesh_b_enqueue(struct mg_mesh_node *node, const void *buf,
|
|
||||||
size_t len) {
|
|
||||||
struct mgi_mesh_b_data *data = (struct mgi_mesh_b_data *) node->data;
|
|
||||||
const uint8_t *frame = (const uint8_t *) buf;
|
|
||||||
if (node->lan == NULL)
|
|
||||||
return mgi_mesh_b_queue(node, &data->tx, buf, len, data->coordinator,
|
|
||||||
MGI_DATA_UP);
|
|
||||||
if (len >= 14 && len <= MGI_ETH_MAX) {
|
|
||||||
const uint8_t *client = mgi_mesh_b_dhcp_client(frame, len);
|
|
||||||
const uint8_t *dst = client == NULL ? frame : client;
|
|
||||||
bool multicast = client == NULL && (frame[0] & 1) != 0;
|
|
||||||
struct mgi_mesh_b_peer *peer;
|
|
||||||
for (peer = data->members; peer != NULL; peer = peer->next) {
|
|
||||||
if (multicast || memcmp(dst, peer->mac, 6) == 0) {
|
|
||||||
uint16_t addr = multicast ? 0xffffu : peer->radio_address;
|
|
||||||
return mgi_mesh_b_queue(node, &data->tx, buf, len, addr, MGI_DATA_DOWN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_bridge_queue_lan(struct mg_mesh_node *bridge) {
|
|
||||||
struct mg_queue *rx = &bridge->lan->recv_queue;
|
|
||||||
char *buf;
|
|
||||||
size_t len;
|
|
||||||
while ((len = mg_queue_next(rx, &buf)) > 0) {
|
|
||||||
bridge->strategy->tx(bridge, buf, len);
|
|
||||||
mg_queue_del(rx, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_bridge_rx(uint64_t now, struct mg_mesh_node *b,
|
|
||||||
const uint8_t *frame, size_t len) {
|
|
||||||
struct mgi_mesh_b_data *data = (struct mgi_mesh_b_data *) b->data;
|
|
||||||
struct mgi_mesh_b_peer *peer;
|
|
||||||
uint16_t dst = 0, src = 0;
|
|
||||||
unsigned command = mgi_mesh_b_frame_parse(frame, len, &dst, &src);
|
|
||||||
if (command == MGI_DATA_UP && dst == b->radio_address &&
|
|
||||||
data->phase == MGI_TURN_DATA && data->current != NULL &&
|
|
||||||
src == data->current->radio_address && now <= data->deadline) {
|
|
||||||
int received = mgi_mesh_b_data_receive(data, frame, len, now);
|
|
||||||
if (received < 0) return;
|
|
||||||
if (received != 0) {
|
|
||||||
b->lan->driver->tx(data->rx_buf, (size_t) received, b->lan);
|
|
||||||
}
|
|
||||||
data->current->misses = 0;
|
|
||||||
data->current = data->current->next;
|
|
||||||
data->phase = MGI_TURN_IDLE;
|
|
||||||
data->deadline = now;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (len != MGI_JOIN_UP_LEN || command != MGI_JOIN_UP ||
|
|
||||||
dst != b->radio_address || data->phase != MGI_TURN_JOIN ||
|
|
||||||
now >= data->deadline) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (src == 0 || src == 0xffffu) return;
|
|
||||||
for (peer = data->members; peer != NULL; peer = peer->next) {
|
|
||||||
if (peer->radio_address == src) break;
|
|
||||||
}
|
|
||||||
if (peer == NULL) {
|
|
||||||
peer = (struct mgi_mesh_b_peer *) mg_calloc(1, sizeof(*peer));
|
|
||||||
if (peer == NULL) return;
|
|
||||||
peer->radio_address = src;
|
|
||||||
peer->next = data->members;
|
|
||||||
data->members = peer;
|
|
||||||
}
|
|
||||||
memcpy(peer->mac, frame + MG_MESH_FRAME_HDR_LEN, sizeof(peer->mac));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Member peer did not answer its turn. After MGI_MAX_MISSES in a row, it is
|
|
||||||
// gone: remove it, and its queued downlink frames
|
|
||||||
static void mgi_mesh_b_member_missed(struct mgi_mesh_b_data *data,
|
|
||||||
struct mgi_mesh_b_peer *peer) {
|
|
||||||
if (++peer->misses < MGI_MAX_MISSES) return;
|
|
||||||
MG_INFO(("Member %M removed: no answer %d times", mg_print_mac, peer->mac,
|
|
||||||
MGI_MAX_MISSES));
|
|
||||||
if (data->fanout == peer) {
|
|
||||||
data->fanout = peer->next;
|
|
||||||
if (data->fanout == NULL) {
|
|
||||||
size_t len = mg_queue_next(&data->tx, NULL);
|
|
||||||
mg_queue_del(&data->tx, len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LIST_DELETE(struct mgi_mesh_b_peer, &data->members, peer);
|
|
||||||
mg_free(peer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// A broadcast occupies one record and is sent once to each member in list
|
|
||||||
// order.
|
|
||||||
static size_t mgi_mesh_b_bridge_burst(struct mgi_mesh_b_data *data,
|
|
||||||
struct mgi_mesh_b_peer *peer) {
|
|
||||||
char *record;
|
|
||||||
size_t len;
|
|
||||||
while ((len = mg_queue_next(&data->tx, &record)) != 0) {
|
|
||||||
uint16_t dst = mgi_mesh_b_join_field((const uint8_t *) record);
|
|
||||||
struct mgi_mesh_b_peer *member;
|
|
||||||
if (dst == 0xffffu) {
|
|
||||||
if (data->fanout == NULL) data->fanout = data->members;
|
|
||||||
return data->fanout == peer ? len : 0;
|
|
||||||
}
|
|
||||||
if (dst == peer->radio_address) return len;
|
|
||||||
for (member = data->members; member != NULL; member = member->next) {
|
|
||||||
if (member->radio_address == dst) return 0;
|
|
||||||
}
|
|
||||||
mg_queue_del(&data->tx, len); // Its destination has left.
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64_t mgi_mesh_b_queued_burst_time(struct mgi_mesh_b_data *data,
|
|
||||||
size_t len) {
|
|
||||||
char *record;
|
|
||||||
size_t frames;
|
|
||||||
if (len == 0) return mgi_mesh_b_burst_time(0);
|
|
||||||
mg_queue_next(&data->tx, &record);
|
|
||||||
// Fragment zero carries the final index. Count overhead and IFS per frame.
|
|
||||||
frames =
|
|
||||||
((uint8_t) record[MGI_TX_DST_LEN + MG_MESH_FRAME_HDR_LEN] & 15u) + 1u;
|
|
||||||
return mgi_mesh_b_airtime(len - MGI_TX_DST_LEN - frames +
|
|
||||||
(frames - 1) * s_mesh_default_radio->overhead) +
|
|
||||||
(frames - 1) * s_mesh_default_radio->ifs_us;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_bridge_io(uint64_t now, struct mg_mesh_node *b) {
|
|
||||||
struct mgi_mesh_b_data *data = (struct mgi_mesh_b_data *) b->data;
|
|
||||||
mgi_mesh_b_bridge_queue_lan(b);
|
|
||||||
if (now >= data->deadline) {
|
|
||||||
if (data->tx_at != (uint64_t) -1) mgi_mesh_b_tx_finish(data);
|
|
||||||
if (data->phase == MGI_TURN_DATA && data->current != NULL) {
|
|
||||||
struct mgi_mesh_b_peer *peer = data->current;
|
|
||||||
data->current = peer->next;
|
|
||||||
mgi_mesh_b_member_missed(data, peer);
|
|
||||||
} else if (data->phase == MGI_TURN_JOIN) {
|
|
||||||
data->current = data->members;
|
|
||||||
}
|
|
||||||
data->phase = MGI_TURN_IDLE;
|
|
||||||
if (data->current != NULL) {
|
|
||||||
size_t burst = mgi_mesh_b_bridge_burst(data, data->current);
|
|
||||||
uint64_t down = mgi_mesh_b_queued_burst_time(data, burst);
|
|
||||||
data->rx_expected = 0xff;
|
|
||||||
mgi_mesh_b_tx_begin(data, burst != 0, now,
|
|
||||||
now + down + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
|
|
||||||
2 * s_mesh_default_radio->ifs_us +
|
|
||||||
MGI_JOIN_TURN_US);
|
|
||||||
} else if (mgi_mesh_b_control_send(b, 0xffffu, MGI_JOIN_DOWN)) {
|
|
||||||
data->phase = MGI_TURN_JOIN;
|
|
||||||
data->deadline = now + MGI_JOIN_WINDOW_US;
|
|
||||||
}
|
|
||||||
if (data->deadline <= now) data->deadline = now + MGI_JOIN_TURN_US;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_bridge_free(struct mgi_mesh_b_data *data) {
|
|
||||||
while (data->members != NULL) {
|
|
||||||
struct mgi_mesh_b_peer *peer = data->members;
|
|
||||||
data->members = peer->next;
|
|
||||||
mg_free(peer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_node_rx(struct mg_mesh_node *n, uint64_t now,
|
|
||||||
uint8_t *frame, size_t len) {
|
|
||||||
struct mgi_mesh_b_data *data = (struct mgi_mesh_b_data *) n->data;
|
|
||||||
uint16_t dst = 0, src = 0;
|
|
||||||
unsigned command = mgi_mesh_b_frame_parse(frame, len, &dst, &src);
|
|
||||||
if (command == MGI_JOIN_DOWN && len == MG_MESH_FRAME_HDR_LEN &&
|
|
||||||
dst == 0xffffu) {
|
|
||||||
if (n->joined && src == data->coordinator &&
|
|
||||||
++data->missed_rounds >= MGI_MAX_MISSES) {
|
|
||||||
n->joined = false;
|
|
||||||
data->phase = MGI_TURN_IDLE;
|
|
||||||
data->tx_at = (uint64_t) -1;
|
|
||||||
data->tx_offset = 0;
|
|
||||||
data->rx_expected = 0xff;
|
|
||||||
}
|
|
||||||
if (!n->joined && (mgi_mesh_b_random(n, data) % 64) == 0) {
|
|
||||||
// Spread contenders across windows as well as within the reply window.
|
|
||||||
data->coordinator = src;
|
|
||||||
data->phase = MGI_TURN_JOIN;
|
|
||||||
data->tx_at = now + MGI_JOIN_TURN_US +
|
|
||||||
mgi_mesh_b_random(n, data) % MGI_JOIN_JITTER_US;
|
|
||||||
}
|
|
||||||
} else if (command == MGI_DATA_DOWN && data->coordinator != 0 &&
|
|
||||||
dst == n->radio_address && src == data->coordinator) {
|
|
||||||
int received = mgi_mesh_b_data_receive(data, frame, len, now);
|
|
||||||
if (received < 0) return;
|
|
||||||
n->joined = true;
|
|
||||||
data->missed_rounds =
|
|
||||||
-1; // This round had a grant; do not count it as missed.
|
|
||||||
if (received != 0) {
|
|
||||||
struct mg_tcpip_if *ifp = n->radio;
|
|
||||||
if (n->rx_len != 0 || (size_t) received > ifp->recv_queue.size) {
|
|
||||||
ifp->ndrop++;
|
|
||||||
} else {
|
|
||||||
memcpy(ifp->recv_queue.buf, data->rx_buf, (size_t) received);
|
|
||||||
n->rx_len = (size_t) received;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (data->tx_at != (uint64_t) -1) mgi_mesh_b_tx_finish(data);
|
|
||||||
mgi_mesh_b_tx_begin(data, mg_queue_next(&data->tx, NULL) != 0, now,
|
|
||||||
now + mgi_mesh_b_burst_time(MGI_ETH_MAX) +
|
|
||||||
2 * s_mesh_default_radio->ifs_us +
|
|
||||||
MGI_JOIN_TURN_US);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_input(struct mg_mesh_node *node, void *buf, size_t len) {
|
|
||||||
uint64_t ready = node->now + node->strategy->radio->turnaround_us;
|
|
||||||
if (node->tx_at < ready) node->tx_at = ready;
|
|
||||||
node->next_tick = node->now;
|
|
||||||
if (node->lan != NULL)
|
|
||||||
mgi_mesh_b_bridge_rx(node->now, node, (const uint8_t *) buf, len);
|
|
||||||
else
|
|
||||||
mgi_mesh_b_node_rx(node, node->now, (uint8_t *) buf, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_tick(struct mg_mesh_node *node, uint64_t now) {
|
|
||||||
struct mgi_mesh_b_data *data = (struct mgi_mesh_b_data *) node->data;
|
|
||||||
node->now = now;
|
|
||||||
if (node->lan != NULL) {
|
|
||||||
mgi_mesh_b_bridge_io(now, node);
|
|
||||||
mgi_mesh_b_tx_step(data, now);
|
|
||||||
node->next_tick = data->deadline;
|
|
||||||
if (data->tx_at < node->next_tick) node->next_tick = data->tx_at;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (data->phase == MGI_TURN_JOIN) {
|
|
||||||
if (data->tx_at <= now) {
|
|
||||||
if (now - data->tx_at <= MGI_JOIN_TURN_US && !node->joined)
|
|
||||||
mgi_mesh_b_control_send(node, data->coordinator, MGI_JOIN_UP);
|
|
||||||
data->tx_at = (uint64_t) -1;
|
|
||||||
data->phase = MGI_TURN_IDLE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mgi_mesh_b_tx_step(data, now);
|
|
||||||
}
|
|
||||||
node->next_tick = data->tx_at;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool mgi_mesh_b_init(struct mg_mesh_node *node,
|
|
||||||
struct mg_tcpip_if *radio, struct mg_tcpip_if *lan,
|
|
||||||
uint16_t radio_address) {
|
|
||||||
struct mgi_mesh_b_data *data =
|
|
||||||
(struct mgi_mesh_b_data *) mg_calloc(1, sizeof(*data));
|
|
||||||
memset(node, 0, sizeof(*node));
|
|
||||||
node->radio = radio;
|
|
||||||
node->lan = lan;
|
|
||||||
node->radio_address = radio_address;
|
|
||||||
node->next_tick = (uint64_t) -1;
|
|
||||||
node->ram_used = node->ram_peak = sizeof(*node);
|
|
||||||
if (data == NULL) return false;
|
|
||||||
node->ram_used += sizeof(*data);
|
|
||||||
node->ram_peak = node->ram_used;
|
|
||||||
data->rx_expected = 0xff;
|
|
||||||
node->data = data;
|
|
||||||
data->node = node;
|
|
||||||
data->tx_at = (uint64_t) -1;
|
|
||||||
mg_queue_init(&data->tx, data->tx_buf, sizeof(data->tx_buf));
|
|
||||||
if (lan != NULL) node->next_tick = 0;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mgi_mesh_b_free(struct mg_mesh_node *node) {
|
|
||||||
struct mgi_mesh_b_data *data = (struct mgi_mesh_b_data *) node->data;
|
|
||||||
if (data == NULL) return;
|
|
||||||
mgi_mesh_b_bridge_free(data);
|
|
||||||
mg_free(data);
|
|
||||||
node->data = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
const struct mg_mesh mg_mesh_default = {
|
|
||||||
"default",
|
|
||||||
mgi_mesh_b_init,
|
|
||||||
mgi_mesh_b_tick,
|
|
||||||
mgi_mesh_b_free,
|
|
||||||
mgi_mesh_b_enqueue,
|
|
||||||
mgi_mesh_b_input,
|
|
||||||
true,
|
|
||||||
true,
|
|
||||||
&mg_radio_802154,
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
// Copyright (c) 2026 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
//
|
|
||||||
// Ethernet over radio, through a mesh. Mongoose sees an Ethernet interface,
|
|
||||||
// the mesh splits Ethernet frames into radio frames. Two parts plug in:
|
|
||||||
// - struct mg_radio: a radio standard, e.g. mg_radio_802154
|
|
||||||
// - struct mg_mesh: how the mesh is built and routed
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "net_builtin.h"
|
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP
|
|
||||||
|
|
||||||
// Monotonic time in microseconds.
|
|
||||||
uint64_t mg_micros(void);
|
|
||||||
|
|
||||||
// A radio standard
|
|
||||||
struct mg_radio {
|
|
||||||
const char *name; // For logs
|
|
||||||
size_t max_frame_size; // Largest radio frame, bytes
|
|
||||||
uint32_t bitrate; // Bits per second on air
|
|
||||||
size_t overhead; // Bytes on air besides the frame: preamble, FCS
|
|
||||||
uint32_t slot_us; // Random backoff slot duration, us
|
|
||||||
uint32_t cca_us; // Channel sensing duration, us
|
|
||||||
uint32_t turnaround_us; // Idle check to transmission start, us
|
|
||||||
uint32_t ifs_us; // Inter-frame spacing in us: gap between transmits
|
|
||||||
uint8_t backoff_min; // Initial backoff exponent
|
|
||||||
uint8_t backoff_max; // Maximum backoff exponent
|
|
||||||
bool csma; // Use channel sensing and backoff before TX
|
|
||||||
size_t header_len; // Radio hardware header length
|
|
||||||
// Write the radio header, with 0xffff as the broadcast address.
|
|
||||||
void (*print_hdr)(const struct mg_radio *radio, uint8_t *buf, uint16_t dst,
|
|
||||||
uint16_t src);
|
|
||||||
// Return the parsed header length, or zero for an invalid header.
|
|
||||||
size_t (*parse_hdr)(const struct mg_radio *radio, const uint8_t *buf,
|
|
||||||
size_t len, uint16_t *dst, uint16_t *src);
|
|
||||||
};
|
|
||||||
|
|
||||||
#define MG_802154_HEADER_LEN 9 // MAC header, short addresses
|
|
||||||
#define MG_802154_PAN_ID 0xabcd // Our network's PAN ID
|
|
||||||
|
|
||||||
#ifndef MG_802154_CHANNEL
|
|
||||||
#define MG_802154_CHANNEL 15
|
|
||||||
#endif
|
|
||||||
|
|
||||||
extern const struct mg_radio mg_radio_802154;
|
|
||||||
|
|
||||||
struct mg_mesh_node {
|
|
||||||
struct mg_tcpip_if *radio; // Owning radio driver interface
|
|
||||||
struct mg_tcpip_if *lan; // Bridge LAN interface, NULL for a client
|
|
||||||
const struct mg_mesh *strategy; // Selected strategy
|
|
||||||
size_t (*tx)(struct mg_tcpip_if *, const void *, size_t); // Radio frame TX
|
|
||||||
size_t rx_len; // Complete Ethernet frame in the interface RX buffer
|
|
||||||
uint64_t now; // Current driver poll time, us
|
|
||||||
uint64_t tx_at; // Earliest next transmission, us
|
|
||||||
void *data; // Private state owned by the strategy
|
|
||||||
size_t ram_used; // Live mesh state bytes
|
|
||||||
size_t ram_peak; // Peak live mesh state bytes
|
|
||||||
uint64_t next_tick; // Next deadline in us, or UINT64_MAX
|
|
||||||
uint16_t radio_address; // Local address supplied by the radio
|
|
||||||
bool joined; // Client is a member of the mesh
|
|
||||||
};
|
|
||||||
|
|
||||||
struct mg_mesh {
|
|
||||||
const char *name;
|
|
||||||
bool (*init)(struct mg_mesh_node *node, struct mg_tcpip_if *radio,
|
|
||||||
struct mg_tcpip_if *lan, uint16_t radio_address);
|
|
||||||
void (*tick)(struct mg_mesh_node *node, uint64_t now_microseconds);
|
|
||||||
void (*free)(struct mg_mesh_node *node);
|
|
||||||
size_t (*tx)(struct mg_mesh_node *, const void *, size_t); // ETH frame TX
|
|
||||||
void (*rx)(struct mg_mesh_node *, void *, size_t); // Radio frame RX
|
|
||||||
bool single_bridge; // Require exactly one bridge
|
|
||||||
bool scheduled_tx; // The mesh schedules transmission turns
|
|
||||||
const struct mg_radio *radio; // Radio configuration for this strategy
|
|
||||||
};
|
|
||||||
|
|
||||||
extern const struct mg_mesh mg_mesh_default;
|
|
||||||
|
|
||||||
#endif
|
|
||||||
+3
-6
@@ -117,8 +117,7 @@ struct mg_mqtt_message {
|
|||||||
// struct mg_mqtt_opts opts = {.client_id = mg_str("device1")};
|
// struct mg_mqtt_opts opts = {.client_id = mg_str("device1")};
|
||||||
// mg_mqtt_connect(&mgr, "mqtt://broker:1883", &opts, fn, NULL);
|
// mg_mqtt_connect(&mgr, "mqtt://broker:1883", &opts, fn, NULL);
|
||||||
// Full examples:
|
// Full examples:
|
||||||
// tutorials/mqtt/mqtt-client, tutorials/mqtt/mqtt-client-aws-iot,
|
// tutorials/mqtt/mqtt-client, tutorials/mqtt/ota-over-mqtt
|
||||||
// tutorials/mqtt/ota-over-mqtt
|
|
||||||
// Related APIs:
|
// Related APIs:
|
||||||
// mg_mqtt_pub(), mg_mqtt_sub(), mg_tls_init(), mg_mgr_poll()
|
// mg_mqtt_pub(), mg_mqtt_sub(), mg_tls_init(), mg_mgr_poll()
|
||||||
// Notes:
|
// Notes:
|
||||||
@@ -153,8 +152,7 @@ void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts);
|
|||||||
// };
|
// };
|
||||||
// uint16_t id = mg_mqtt_pub(c, &opts);
|
// uint16_t id = mg_mqtt_pub(c, &opts);
|
||||||
// Full examples:
|
// Full examples:
|
||||||
// tutorials/mqtt/mqtt-client, tutorials/mqtt/mqtt-client-aws-iot,
|
// tutorials/mqtt/mqtt-client, tutorials/mqtt/ota-over-mqtt
|
||||||
// tutorials/mqtt/ota-over-mqtt
|
|
||||||
// Related APIs:
|
// Related APIs:
|
||||||
// mg_mqtt_connect(), mg_mqtt_sub(), mg_mqtt_send_header()
|
// mg_mqtt_connect(), mg_mqtt_sub(), mg_mqtt_send_header()
|
||||||
// Notes:
|
// Notes:
|
||||||
@@ -169,8 +167,7 @@ uint16_t mg_mqtt_pub(struct mg_connection *c, const struct mg_mqtt_opts *opts);
|
|||||||
// struct mg_mqtt_opts opts = {.topic = mg_str("device/rx"), .qos = 1};
|
// struct mg_mqtt_opts opts = {.topic = mg_str("device/rx"), .qos = 1};
|
||||||
// mg_mqtt_sub(c, &opts);
|
// mg_mqtt_sub(c, &opts);
|
||||||
// Full examples:
|
// Full examples:
|
||||||
// tutorials/mqtt/mqtt-client, tutorials/mqtt/mqtt-client-aws-iot,
|
// tutorials/mqtt/mqtt-client, tutorials/mqtt/ota-over-mqtt
|
||||||
// tutorials/mqtt/ota-over-mqtt
|
|
||||||
// Related APIs:
|
// Related APIs:
|
||||||
// mg_mqtt_connect(), mg_mqtt_pub(), mg_mqtt_unsub()
|
// mg_mqtt_connect(), mg_mqtt_pub(), mg_mqtt_unsub()
|
||||||
// Notes:
|
// Notes:
|
||||||
|
|||||||
+43
-20
@@ -1,6 +1,11 @@
|
|||||||
#include "net_builtin.h"
|
#include "net_builtin.h"
|
||||||
#include "bsd.h"
|
#include "bsd.h"
|
||||||
|
#include "drivers/usb.h"
|
||||||
|
#include "mdash.h"
|
||||||
|
#include "ota.h"
|
||||||
|
#include "printf.h"
|
||||||
#include "profile.h"
|
#include "profile.h"
|
||||||
|
#include "tls.h"
|
||||||
|
|
||||||
#if MG_ENABLE_TCPIP
|
#if MG_ENABLE_TCPIP
|
||||||
#define MG_EPHEMERAL_PORT_BASE 32768
|
#define MG_EPHEMERAL_PORT_BASE 32768
|
||||||
@@ -238,13 +243,14 @@ uint8_t mg_l2_ip6put(enum mg_l2type type, uint8_t *addr, uint8_t *opts);
|
|||||||
#endif
|
#endif
|
||||||
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
|
bool mg_l2_poll(struct mg_tcpip_if *ifp, bool expired_1000ms);
|
||||||
|
|
||||||
static void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
|
void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
|
||||||
#if 0 && MG_ENABLE_PROFILE
|
#if 0 && MG_ENABLE_PROFILE
|
||||||
const char *names[] = {"TCPIP_EV_STATE_CHANGE", "TCPIP_EV_DHCP_DNS",
|
const char *names[] = {"TCPIP_EV_STATE_CHANGE", "TCPIP_EV_DHCP_DNS",
|
||||||
"TCPIP_EV_DHCP_SNTP", "TCPIP_EV_ARP",
|
"TCPIP_EV_DHCP_SNTP", "TCPIP_EV_ARP",
|
||||||
"TCPIP_EV_TIMER_1S", "TCPIP_EV_WIFI_SCAN_RESULT",
|
"TCPIP_EV_TIMER_1S", "TCPIP_EV_WIFI_SCAN_RESULT",
|
||||||
"TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR",
|
"TCPIP_EV_WIFI_SCAN_END", "TCPIP_EV_WIFI_CONNECT_ERR",
|
||||||
"TCPIP_EV_DRIVER", "TCPIP_EV_STATE6_CHANGE",
|
"TCPIP_EV_DRIVER", "TCPIP_EV_STATE6_CHANGE",
|
||||||
|
"TCPIP_EV_PHY_RX", "TCPIP_EV_PHY_TX",
|
||||||
"TCPIP_EV_USER"};
|
"TCPIP_EV_USER"};
|
||||||
if (ev != MG_TCPIP_EV_TIMER_1S && ev < (int) (sizeof(names) / sizeof(names[0]))) {
|
if (ev != MG_TCPIP_EV_TIMER_1S && ev < (int) (sizeof(names) / sizeof(names[0]))) {
|
||||||
MG_PROF_ADD(ifp, names[ev]); // TODO(): call MG_PROF_DUMP() MG_PROF_FREE()
|
MG_PROF_ADD(ifp, names[ev]); // TODO(): call MG_PROF_DUMP() MG_PROF_FREE()
|
||||||
@@ -395,7 +401,7 @@ static size_t driver_output(struct mg_tcpip_if *ifp, size_t len) {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC826, ARP assumes Ethernet MAC addresses
|
// RFC-826, ARP assumes Ethernet MAC addresses
|
||||||
void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
|
void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac) {
|
||||||
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
uint8_t *l2p = (uint8_t *) ifp->tx.buf;
|
||||||
struct arp *arp = (struct arp *) mg_l2_header(
|
struct arp *arp = (struct arp *) mg_l2_header(
|
||||||
@@ -536,7 +542,7 @@ static void tx_dhcp(struct mg_tcpip_if *ifp, uint8_t *l2_dst, uint32_t ip_src,
|
|||||||
sizeof(dhcp));
|
sizeof(dhcp));
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC-2131 #4.3.6, #4.4.1; RFC-2132 #9.8
|
// RFC-2131 4.3.6, 4.4.1; RFC-2132 9.8
|
||||||
static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
|
static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
|
||||||
uint32_t ip_srv) {
|
uint32_t ip_srv) {
|
||||||
uint8_t extra = (uint8_t) ((ifp->enable_req_dns ? 1 : 0) +
|
uint8_t extra = (uint8_t) ((ifp->enable_req_dns ? 1 : 0) +
|
||||||
@@ -563,7 +569,7 @@ static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
|
|||||||
MG_DEBUG(("DHCP req sent"));
|
MG_DEBUG(("DHCP req sent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC-2131 #4.3.6, #4.4.5 (renewing: unicast, rebinding: bcast)
|
// RFC-2131 4.3.6, 4.4.5 (renewing: unicast, rebinding: bcast)
|
||||||
static void tx_dhcp_request_re(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
|
static void tx_dhcp_request_re(struct mg_tcpip_if *ifp, uint8_t *l2_dst,
|
||||||
uint32_t ip_src, uint32_t ip_dst) {
|
uint32_t ip_src, uint32_t ip_dst) {
|
||||||
uint8_t opts[] = {
|
uint8_t opts[] = {
|
||||||
@@ -617,7 +623,7 @@ static void l2addr_resolved(struct mg_connection *c);
|
|||||||
static uint8_t *get_return_l2addr(struct mg_tcpip_if *ifp, struct mg_addr *rem,
|
static uint8_t *get_return_l2addr(struct mg_tcpip_if *ifp, struct mg_addr *rem,
|
||||||
bool is_udp, struct pkt *pkt);
|
bool is_udp, struct pkt *pkt);
|
||||||
|
|
||||||
// RFC826, ARP assumes Ethernet MAC addresses
|
// RFC-826, ARP assumes Ethernet MAC addresses
|
||||||
static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
static void rx_arp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||||
if (pkt->arp->op == mg_htons(1) && pkt->arp->tpa == ifp->ip) {
|
if (pkt->arp->op == mg_htons(1) && pkt->arp->tpa == ifp->ip) {
|
||||||
// ARP request. Make a response, then send
|
// ARP request. Make a response, then send
|
||||||
@@ -1023,13 +1029,13 @@ static void rx_ndp_ra(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
|
|
||||||
if (pkt->pay.len < sizeof(*ra)) return;
|
if (pkt->pay.len < sizeof(*ra)) return;
|
||||||
if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
if (ifp->state6 == MG_TCPIP_STATE_UP) {
|
||||||
MG_DEBUG(("Received NDP RA")); // fill gw6 address if RFC 4861 6.1.2
|
MG_DEBUG(("Received NDP RA")); // fill gw6 address if RFC-4861 6.1.2
|
||||||
if (memcmp(pkt->ip6->src, locpx, 8) != 0 || pkt->ip6->hops != 255) return;
|
if (memcmp(pkt->ip6->src, locpx, 8) != 0 || pkt->ip6->hops != 255) return;
|
||||||
// parse options
|
// parse options
|
||||||
while (opt_left >= 2) {
|
while (opt_left >= 2) {
|
||||||
uint8_t type = opts[0], len = opts[1];
|
uint8_t type = opts[0], len = opts[1];
|
||||||
size_t length = (size_t) len * 8;
|
size_t length = (size_t) len * 8;
|
||||||
if (length == 0 || length > opt_left) return; // RFC 4861 6.1.2
|
if (length == 0 || length > opt_left) return; // RFC-4861 6.1.2
|
||||||
if (type == 1 && length >= 8) {
|
if (type == 1 && length >= 8) {
|
||||||
// Received router's L2 address
|
// Received router's L2 address
|
||||||
if (!mg_l2_ip6get(ifp->l2type, l2, opts + 2, len)) break;
|
if (!mg_l2_ip6get(ifp->l2type, l2, opts + 2, len)) break;
|
||||||
@@ -1110,7 +1116,7 @@ static void rx_icmp6(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
|||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case 134: // Router Advertisement
|
case 134: // Router Advertisement
|
||||||
if (pkt->icmp6->code == 0) rx_ndp_ra(ifp, pkt); // RFC 4861 6.1.2
|
if (pkt->icmp6->code == 0) rx_ndp_ra(ifp, pkt); // RFC-4861 6.1.2
|
||||||
break;
|
break;
|
||||||
case 135: // Neighbor Solicitation
|
case 135: // Neighbor Solicitation
|
||||||
rx_ndp_ns(ifp, pkt);
|
rx_ndp_ns(ifp, pkt);
|
||||||
@@ -1224,22 +1230,34 @@ static void proto_error(struct mg_connection *c, bool r, const char *f, ...) {
|
|||||||
static bool rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
static bool rx_udp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
|
||||||
struct mg_connection *c = getpeer(ifp->mgr, pkt, true);
|
struct mg_connection *c = getpeer(ifp->mgr, pkt, true);
|
||||||
struct connstate *s;
|
struct connstate *s;
|
||||||
|
struct mg_addr rem;
|
||||||
uint8_t *l2addr;
|
uint8_t *l2addr;
|
||||||
if (c == NULL) return false; // No UDP listener on this port
|
if (c == NULL) return false; // No UDP listener on this port
|
||||||
|
// BSD socket has an unread datagram: drop this one to keep boundaries
|
||||||
|
if (c->is_bsd && c->recv.len > 0) return true;
|
||||||
|
if (c->is_bsd && !c->is_client && c->send.len > 0) return true;
|
||||||
s = (struct connstate *) (c + 1);
|
s = (struct connstate *) (c + 1);
|
||||||
c->rem.port = pkt->udp->sport;
|
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->loc.is_ip6) { // matching of v4/v6 to dest is done bt getpeer()
|
if (c->loc.is_ip6) { // matching of v4/v6 to dest is done bt getpeer()
|
||||||
if (!udp6csum_ok(pkt->ip6, pkt->udp)) return false;
|
if (!udp6csum_ok(pkt->ip6, pkt->udp)) return false;
|
||||||
c->rem.addr.ip6[0] = pkt->ip6->src[0],
|
if (c->is_bsd && c->is_client &&
|
||||||
c->rem.addr.ip6[1] = pkt->ip6->src[1], c->rem.is_ip6 = true;
|
(c->rem.port != pkt->udp->sport ||
|
||||||
|
!MG_IP6MATCH(c->rem.addr.ip6, pkt->ip6->src)))
|
||||||
|
return true;
|
||||||
|
rem.addr.ip6[0] = pkt->ip6->src[0],
|
||||||
|
rem.addr.ip6[1] = pkt->ip6->src[1], rem.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
if (!udpcsum_ok(pkt->ip, pkt->udp)) return false;
|
if (!udpcsum_ok(pkt->ip, pkt->udp)) return false;
|
||||||
c->rem.addr.ip4 = pkt->ip->src;
|
if (c->is_bsd && c->is_client &&
|
||||||
|
(c->rem.port != pkt->udp->sport || c->rem.addr.ip4 != pkt->ip->src))
|
||||||
|
return true;
|
||||||
|
rem.addr.ip4 = pkt->ip->src, rem.is_ip6 = false;
|
||||||
}
|
}
|
||||||
if ((l2addr = get_return_l2addr(ifp, &c->rem, true, pkt)) == NULL)
|
rem.port = pkt->udp->sport;
|
||||||
|
c->rem = rem;
|
||||||
|
if ((l2addr = get_return_l2addr(ifp, &rem, true, pkt)) == NULL)
|
||||||
return false; // safety net for lousy networks
|
return false; // safety net for lousy networks
|
||||||
memcpy(s->mac, l2addr, sizeof(s->mac));
|
memcpy(s->mac, l2addr, sizeof(s->mac));
|
||||||
if (c->recv.len >= MG_MAX_RECV_SIZE) {
|
if (c->recv.len >= MG_MAX_RECV_SIZE) {
|
||||||
@@ -2345,20 +2363,25 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
s->retransmit = ifp->enable_tcp_retransmit;
|
s->retransmit = ifp->enable_tcp_retransmit;
|
||||||
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
s->txq.align = MG_TCPIP_TXQ_ALIGN;
|
||||||
c->is_resolving = 0;
|
c->is_resolving = 0;
|
||||||
if (ifp->eport < MG_EPHEMERAL_PORT_BASE) ifp->eport = MG_EPHEMERAL_PORT_BASE;
|
if (c->loc.port == 0) {
|
||||||
|
if (ifp->eport < MG_EPHEMERAL_PORT_BASE)
|
||||||
|
ifp->eport = MG_EPHEMERAL_PORT_BASE;
|
||||||
c->loc.port = mg_htons(ifp->eport++);
|
c->loc.port = mg_htons(ifp->eport++);
|
||||||
|
} // otherwise retain an existing bound port
|
||||||
#if MG_ENABLE_IPV6
|
#if MG_ENABLE_IPV6
|
||||||
if (c->rem.is_ip6) {
|
if (c->rem.is_ip6) {
|
||||||
if (c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0 &&
|
||||||
|
c->rem.addr.ip6[0] == ifp->ip6ll[0]) { // same local LAN, use ll
|
||||||
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
c->loc.addr.ip6[0] = ifp->ip6ll[0], c->loc.addr.ip6[1] = ifp->ip6ll[1];
|
||||||
} else { // use global address
|
} else if (c->loc.addr.ip6[0] == 0 && c->loc.addr.ip6[1] == 0) {
|
||||||
|
// Use global address
|
||||||
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
c->loc.addr.ip6[0] = ifp->ip6[0], c->loc.addr.ip6[1] = ifp->ip6[1];
|
||||||
}
|
} // otherwise retain an existing bound address
|
||||||
c->loc.is_ip6 = true;
|
c->loc.is_ip6 = true;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
c->loc.addr.ip4 = ifp->ip;
|
if (c->loc.addr.ip4 == 0) c->loc.addr.ip4 = ifp->ip;
|
||||||
}
|
}
|
||||||
MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port,
|
MG_DEBUG(("%lu %M -> %M", c->id, mg_print_ip_port, &c->loc, mg_print_ip_port,
|
||||||
&c->rem));
|
&c->rem));
|
||||||
@@ -2389,7 +2412,7 @@ void mg_connect_resolved(struct mg_connection *c) {
|
|||||||
uint32_t rem_ip = c->rem.addr.ip4;
|
uint32_t rem_ip = c->rem.addr.ip4;
|
||||||
if (ifp->ip && MG_IPISLOCAL(ifp, rem_ip) &&
|
if (ifp->ip && MG_IPISLOCAL(ifp, rem_ip) &&
|
||||||
rem_ip != ifp->gw) { // skip if gw (onstatechange -> ARP)
|
rem_ip != ifp->gw) { // skip if gw (onstatechange -> ARP)
|
||||||
// If we're in the same LAN, fire an ARP lookup.
|
// We're in the same LAN, fire an ARP lookup.
|
||||||
MG_DEBUG(("%lu ARP lookup...", c->id));
|
MG_DEBUG(("%lu ARP lookup...", c->id));
|
||||||
mg_tcpip_arp_request(ifp, rem_ip, NULL);
|
mg_tcpip_arp_request(ifp, rem_ip, NULL);
|
||||||
settmout(c, MIP_TTYPE_ARP);
|
settmout(c, MIP_TTYPE_ARP);
|
||||||
@@ -2514,7 +2537,7 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t mcast_addr[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb};
|
const uint8_t mcast_addr[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb};
|
||||||
void mg_multicast_add(struct mg_connection *c, char *ip) {
|
void mg_multicast_add(struct mg_connection *c, char *ip) {
|
||||||
(void) ip; // ip4/6_mcastmac(mcast_mac, &ip); ipv6 param
|
(void) ip; // ip4/6_mcastmac(mcast_mac, &ip); ipv6 param
|
||||||
// TODO(): actual IP -> MAC; check database, update
|
// TODO(): actual IP -> MAC; check database, update
|
||||||
|
|||||||
+34
-23
@@ -40,6 +40,8 @@ enum {
|
|||||||
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connection failed driver and chip specific
|
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connection failed driver and chip specific
|
||||||
MG_TCPIP_EV_DRIVER, // Driver-defined event driver specific
|
MG_TCPIP_EV_DRIVER, // Driver-defined event driver specific
|
||||||
MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6)
|
MG_TCPIP_EV_STATE6_CHANGE, // IPv6 link state changed uint8_t *(&ifp->state6)
|
||||||
|
MG_TCPIP_EV_PHY_RX, // Radio frame received struct mg_str *
|
||||||
|
MG_TCPIP_EV_PHY_TX, // Radio frame about to be sent struct mg_str *
|
||||||
MG_TCPIP_EV_USER // Starting ID for user-defined events
|
MG_TCPIP_EV_USER // Starting ID for user-defined events
|
||||||
};
|
};
|
||||||
// clang-format on
|
// clang-format on
|
||||||
@@ -128,37 +130,42 @@ void mg_tcpip_free(struct mg_tcpip_if *);
|
|||||||
// the queue is full.
|
// the queue is full.
|
||||||
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
|
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
|
||||||
|
|
||||||
|
// Delivers an interface event to the protocol and user handlers
|
||||||
|
void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data);
|
||||||
|
|
||||||
// Sends a broadcast ARP who-has request for ip. mac may be NULL for a plain
|
// Sends a broadcast ARP who-has request for ip. mac may be NULL for a plain
|
||||||
// broadcast, or a non-NULL hint to set the target MAC field in the request.
|
// broadcast, or a non-NULL hint to set the target MAC field in the request.
|
||||||
void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac);
|
void mg_tcpip_arp_request(struct mg_tcpip_if *ifp, uint32_t ip, uint8_t *mac);
|
||||||
|
|
||||||
// Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling
|
extern const uint8_t mcast_addr[6]; // Multicast MAC address programmed by drivers
|
||||||
// mg_tcpip_init(). Each driver also requires a corresponding driver_data
|
|
||||||
// struct; see the matching driver header for details.
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_w5100;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_tms570;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_imxrt;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_same54;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_cmsis;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_ra;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_xmc;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_xmc7;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_pico_w;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_rw612;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_cyw;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_nxp_wifi;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_st67w6;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_atcmd;
|
|
||||||
extern struct mg_tcpip_driver mg_tcpip_driver_netc;
|
|
||||||
|
|
||||||
#include "drivers/mg24.h"
|
// Built-in hardware drivers. Assign one to mg_tcpip_if.driver before calling
|
||||||
|
// mg_tcpip_init(). See the matching driver header for configuration and
|
||||||
|
// initialisation helpers.
|
||||||
|
#include "drivers/at_cmd.h"
|
||||||
|
#include "drivers/cmsis.h"
|
||||||
|
#include "drivers/cyw.h"
|
||||||
|
#include "drivers/esp32.h"
|
||||||
|
#include "drivers/imxrt.h"
|
||||||
#include "drivers/mcxw71.h"
|
#include "drivers/mcxw71.h"
|
||||||
|
#include "drivers/mg24.h"
|
||||||
|
#include "drivers/netc.h"
|
||||||
#include "drivers/nrf52.h"
|
#include "drivers/nrf52.h"
|
||||||
|
#include "drivers/nxp_wifi.h"
|
||||||
|
#include "drivers/pico-w.h"
|
||||||
|
#include "drivers/ra.h"
|
||||||
|
#include "drivers/rw612.h"
|
||||||
#include "drivers/rw612w.h"
|
#include "drivers/rw612w.h"
|
||||||
|
#include "drivers/same54.h"
|
||||||
|
#include "drivers/st67w6.h"
|
||||||
|
#include "drivers/stm32f.h"
|
||||||
|
#include "drivers/stm32h.h"
|
||||||
|
#include "drivers/tm4c.h"
|
||||||
|
#include "drivers/tms570.h"
|
||||||
|
#include "drivers/w5100.h"
|
||||||
|
#include "drivers/w5500.h"
|
||||||
|
#include "drivers/xmc.h"
|
||||||
|
#include "drivers/xmc7.h"
|
||||||
|
|
||||||
// SPI bus abstraction for SPI-attached network chips (e.g. W5500).
|
// SPI bus abstraction for SPI-attached network chips (e.g. W5500).
|
||||||
// Populate and assign to the driver's driver_data field.
|
// Populate and assign to the driver's driver_data field.
|
||||||
@@ -169,6 +176,10 @@ struct mg_tcpip_spi {
|
|||||||
void (*txn)(void *, uint8_t *write, uint8_t *read, size_t); // Full-duplex transfer: write and read len bytes simultaneously
|
void (*txn)(void *, uint8_t *write, uint8_t *read, size_t); // Full-duplex transfer: write and read len bytes simultaneously
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// SPI bus for SPI-attached network chips, defined by the application.
|
||||||
|
// Used by the drivers' INIT macros, e.g. MG_TCPIP_DRIVER_W5500_INIT
|
||||||
|
extern struct mg_tcpip_spi mg_spi;
|
||||||
|
|
||||||
// Alignment attributes for DMA-capable Ethernet descriptor buffers.
|
// Alignment attributes for DMA-capable Ethernet descriptor buffers.
|
||||||
// Use these on driver RX/TX descriptor arrays where the hardware requires
|
// Use these on driver RX/TX descriptor arrays where the hardware requires
|
||||||
// specific alignment.
|
// specific alignment.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_CH32V307
|
#if MG_OTA == MG_OTA_CH32V307
|
||||||
// RM: https://www.wch-ic.com/downloads/CH32FV2x_V3xRM_PDF.html
|
// RM: https://www.wch-ic.com/downloads/CH32FV2x_V3xRM_PDF.html
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "arch.h"
|
#include "arch.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "ota.h"
|
||||||
|
|
||||||
#if MG_ARCH == MG_ARCH_ESP32 && MG_OTA == MG_OTA_ESP32
|
#if MG_ARCH == MG_ARCH_ESP32 && MG_OTA == MG_OTA_ESP32
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA >= MG_OTA_RT1020 && MG_OTA <= MG_OTA_RT1180
|
#if MG_OTA >= MG_OTA_RT1020 && MG_OTA <= MG_OTA_RT1180
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_MCXN
|
#if MG_OTA == MG_OTA_MCXN
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_MCXW71
|
#if MG_OTA == MG_OTA_MCXW71
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_PICOSDK
|
#if MG_OTA == MG_OTA_PICOSDK
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_RW612
|
#if MG_OTA == MG_OTA_RW612
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_STM32F
|
#if MG_OTA == MG_OTA_STM32F
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_STM32H5 || MG_OTA == MG_OTA_STM32C5
|
#if MG_OTA == MG_OTA_STM32H5 || MG_OTA == MG_OTA_STM32C5
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ota.h"
|
#include "ota.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
#if MG_OTA == MG_OTA_STM32H7 || MG_OTA == MG_OTA_STM32H7_DUAL_CORE
|
#if MG_OTA == MG_OTA_STM32H7 || MG_OTA == MG_OTA_STM32H7_DUAL_CORE
|
||||||
|
|
||||||
|
|||||||
+7
-5
@@ -4,11 +4,13 @@
|
|||||||
|
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
// Return milliseconds since boot (uptime). Never goes backwards.
|
// Return microseconds since boot. Never goes backwards.
|
||||||
// On RTOS targets (FreeRTOS, ThreadX, Zephyr, etc.) uses the RTOS tick count.
|
// To define your own implementation, set MG_ENABLE_CUSTOM_MICROS=1
|
||||||
// On STM32/Cube uses HAL_GetTick(). On Arduino uses millis().
|
uint64_t mg_micros(void);
|
||||||
// On Linux/macOS uses CLOCK_MONOTONIC_RAW or CLOCK_MONOTONIC; falls back to
|
|
||||||
// CLOCK_REALTIME if neither is available.
|
// Return milliseconds since boot. Never goes backwards. Calls mg_micros().
|
||||||
|
// To define your own implementation, set MG_ENABLE_CUSTOM_MILLIS=1
|
||||||
|
// Deprecated: use mg_micros() instead.
|
||||||
uint64_t mg_millis(void);
|
uint64_t mg_millis(void);
|
||||||
|
|
||||||
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
|
// Boot timestamp in ms since Epoch. 0 until the first successful SNTP sync;
|
||||||
|
|||||||
+2
-2
@@ -297,9 +297,9 @@ static long recv_raw(struct mg_connection *c, void *buf, size_t len) {
|
|||||||
long n = 0;
|
long n = 0;
|
||||||
if (c->is_udp) {
|
if (c->is_udp) {
|
||||||
union usa usa;
|
union usa usa;
|
||||||
socklen_t slen = tousa(&c->rem, &usa);
|
socklen_t slen = sizeof(usa);
|
||||||
n = recvfrom(FD(c), (char *) buf, len, 0, &usa.sa, &slen);
|
n = recvfrom(FD(c), (char *) buf, len, 0, &usa.sa, &slen);
|
||||||
if (n > 0) tomgaddr(&usa, &c->rem, slen != sizeof(usa.sin));
|
if (n > 0) tomgaddr(&usa, &c->rem, usa.sa.sa_family == AF_INET6);
|
||||||
} else {
|
} else {
|
||||||
n = recv(FD(c), (char *) buf, len, MSG_NONBLOCKING);
|
n = recv(FD(c), (char *) buf, len, MSG_NONBLOCKING);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ struct mg_tls_opts {
|
|||||||
// ```
|
// ```
|
||||||
// Full examples:
|
// Full examples:
|
||||||
// tutorials/http/http-server, tutorials/http/http-client,
|
// tutorials/http/http-server, tutorials/http/http-client,
|
||||||
// tutorials/mqtt/mqtt-client-aws-iot, tutorials/websocket/websocket-client
|
// tutorials/mqtt/mqtt-client, tutorials/websocket/websocket-client
|
||||||
// Related APIs:
|
// Related APIs:
|
||||||
// mg_http_listen(), mg_http_connect(), mg_ws_connect(), mg_mqtt_connect()
|
// mg_http_listen(), mg_http_connect(), mg_ws_connect(), mg_mqtt_connect()
|
||||||
// Notes:
|
// Notes:
|
||||||
|
|||||||
+26
-26
@@ -29,14 +29,14 @@ static const uint8_t mg_secp256r1_oid[] = {
|
|||||||
0x3d, 0x03, 0x01, 0x07 // 1.2.840.10045.3.1.7 secp256r1
|
0x3d, 0x03, 0x01, 0x07 // 1.2.840.10045.3.1.7 secp256r1
|
||||||
};
|
};
|
||||||
|
|
||||||
/* TLS 1.3 Record Content Type (RFC8446 B.1) */
|
/* TLS 1.3 Record Content Type (RFC-8446 B.1) */
|
||||||
#define MG_TLS_CHANGE_CIPHER 20
|
#define MG_TLS_CHANGE_CIPHER 20
|
||||||
#define MG_TLS_ALERT 21
|
#define MG_TLS_ALERT 21
|
||||||
#define MG_TLS_HANDSHAKE 22
|
#define MG_TLS_HANDSHAKE 22
|
||||||
#define MG_TLS_APP_DATA 23
|
#define MG_TLS_APP_DATA 23
|
||||||
#define MG_TLS_HEARTBEAT 24
|
#define MG_TLS_HEARTBEAT 24
|
||||||
|
|
||||||
/* TLS 1.3 Handshake Message Type (RFC8446 B.3) */
|
/* TLS 1.3 Handshake Message Type (RFC-8446 B.3) */
|
||||||
#define MG_TLS_CLIENT_HELLO 1
|
#define MG_TLS_CLIENT_HELLO 1
|
||||||
#define MG_TLS_SERVER_HELLO 2
|
#define MG_TLS_SERVER_HELLO 2
|
||||||
#define MG_TLS_ENCRYPTED_EXTENSIONS 8
|
#define MG_TLS_ENCRYPTED_EXTENSIONS 8
|
||||||
@@ -48,7 +48,7 @@ static const uint8_t mg_secp256r1_oid[] = {
|
|||||||
#define MG_TLS_RSA_USE_CRT 1 // CRT instead of naive RSA
|
#define MG_TLS_RSA_USE_CRT 1 // CRT instead of naive RSA
|
||||||
|
|
||||||
// handshake is re-entrant, so we need to keep track of its state state names
|
// handshake is re-entrant, so we need to keep track of its state state names
|
||||||
// refer to RFC8446#A.1
|
// refer to RFC-8446 A.1
|
||||||
enum mg_tls_hs_state {
|
enum mg_tls_hs_state {
|
||||||
// Client state machine:
|
// Client state machine:
|
||||||
MG_TLS_STATE_CLIENT_START, // Send ClientHello
|
MG_TLS_STATE_CLIENT_START, // Send ClientHello
|
||||||
@@ -761,8 +761,8 @@ static bool mg_tls_server_send_hello(struct mg_connection *c) {
|
|||||||
|
|
||||||
// calculate P-256 keyshare and the shared secret
|
// calculate P-256 keyshare and the shared secret
|
||||||
pub[0] = 0x04; // uncompressed point 0x04 || X || Y
|
pub[0] = 0x04; // uncompressed point 0x04 || X || Y
|
||||||
if (!mg_p256_make_key(pub + 1, prv) ||
|
if (mg_p256_make_key(pub + 1, prv) == 0 ||
|
||||||
!mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec)) {
|
mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec) == 0) {
|
||||||
mg_bzero(prv, sizeof(prv));
|
mg_bzero(prv, sizeof(prv));
|
||||||
mg_error(c, "bad key");
|
mg_error(c, "bad key");
|
||||||
return false;
|
return false;
|
||||||
@@ -773,7 +773,7 @@ static bool mg_tls_server_send_hello(struct mg_connection *c) {
|
|||||||
hello[0] = 0x02; // server hello, tls 1.2
|
hello[0] = 0x02; // server hello, tls 1.2
|
||||||
MG_STORE_BE24(hello + 1, n - 4);
|
MG_STORE_BE24(hello + 1, n - 4);
|
||||||
hello[4] = 0x03, hello[5] = 0x03;
|
hello[4] = 0x03, hello[5] = 0x03;
|
||||||
if (!mg_random(hello + 6, 32)) { // server random, RFC 8446 4.1.3
|
if (!mg_random(hello + 6, 32)) { // server random, RFC-8446 4.1.3
|
||||||
mg_error(c, "RNG");
|
mg_error(c, "RNG");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -807,14 +807,14 @@ static bool mg_tls_server_send_hello(struct mg_connection *c) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// HelloRetryRequest random, SHA-256 of "HelloRetryRequest", RFC 8446 4.1.3
|
// HelloRetryRequest random, SHA-256 of "HelloRetryRequest", RFC-8446 4.1.3
|
||||||
static const uint8_t hrr_random[32] = {
|
static const uint8_t hrr_random[32] = {
|
||||||
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c,
|
0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c,
|
||||||
0x02, 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb,
|
0x02, 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb,
|
||||||
0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c};
|
0x8c, 0x5e, 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c};
|
||||||
|
|
||||||
// put HelloRetryRequest record into wio buffer, asking for a P-256 key share.
|
// put HelloRetryRequest record into wio buffer, asking for a P-256 key share.
|
||||||
// Replace ClientHello in the transcript with message_hash, RFC 8446 4.4.1
|
// Replace ClientHello in the transcript with message_hash, RFC-8446 4.4.1
|
||||||
static bool mg_tls_server_send_hrr(struct mg_connection *c) {
|
static bool mg_tls_server_send_hrr(struct mg_connection *c) {
|
||||||
struct tls_data *tls = (struct tls_data *) c->tls;
|
struct tls_data *tls = (struct tls_data *) c->tls;
|
||||||
struct mg_iobuf *wio = &tls->send;
|
struct mg_iobuf *wio = &tls->send;
|
||||||
@@ -1177,7 +1177,7 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
|
|||||||
size_t sigsz, verifysz = 0;
|
size_t sigsz, verifysz = 0;
|
||||||
int adj1, adj2;
|
int adj1, adj2;
|
||||||
uint8_t sig[64] = {0};
|
uint8_t sig[64] = {0};
|
||||||
if (!mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig)) {
|
if (mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig) != 1) {
|
||||||
mg_error(c, "P-256 sign");
|
mg_error(c, "P-256 sign");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1309,7 +1309,7 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
|
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
|
||||||
if (!mg_p256_make_key(msg_client_hello + 95, tls->p256_cli)) {
|
if (mg_p256_make_key(msg_client_hello + 95, tls->p256_cli) == 0) {
|
||||||
mg_error(c, "P-256 key");
|
mg_error(c, "P-256 key");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1399,8 +1399,8 @@ static int mg_tls_client_recv_hello(struct mg_connection *c) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (key_exchange_len != 65 || key_exchange[0] != 0x04 ||
|
if (key_exchange_len != 65 || key_exchange[0] != 0x04 ||
|
||||||
!mg_p256_shared_secret(key_exchange + 1, tls->p256_cli,
|
mg_p256_shared_secret(key_exchange + 1, tls->p256_cli,
|
||||||
tls->ecdhe_sec)) { // Validates the point
|
tls->ecdhe_sec) == 0) { // Validates the point
|
||||||
mg_error(c, "bad key");
|
mg_error(c, "bad key");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -1690,7 +1690,7 @@ static int mg_tls_verify_cert_san(const uint8_t *der, size_t dersz,
|
|||||||
return 0; // No SAN
|
return 0; // No SAN
|
||||||
}
|
}
|
||||||
if (mg_der_parse(field.value, field.len, &field) < 0 || field.type != 0x30) {
|
if (mg_der_parse(field.value, field.len, &field) < 0 || field.type != 0x30) {
|
||||||
MG_ERROR(("SAN is not a constructed object")); // RFC 5280 4.2.1.6
|
MG_ERROR(("SAN is not a constructed object")); // RFC-5280 4.2.1.6
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
while (mg_der_next(&field, &name) > 0) {
|
while (mg_der_next(&field, &name) > 0) {
|
||||||
@@ -1781,14 +1781,14 @@ static bool mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) {
|
static bool mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) {
|
||||||
struct mg_der_tlv v;
|
struct mg_der_tlv v;
|
||||||
int matched = 0;
|
bool matched = false;
|
||||||
if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) {
|
if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) {
|
||||||
struct mg_str hn, cn;
|
struct mg_str hn, cn;
|
||||||
MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host));
|
MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host));
|
||||||
hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len);
|
hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len);
|
||||||
matched = (int) (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL));
|
matched = (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL));
|
||||||
}
|
}
|
||||||
return matched;
|
return matched;
|
||||||
}
|
}
|
||||||
@@ -1910,9 +1910,9 @@ static int mg_tls_recv_cert(struct mg_connection *c, bool is_client) {
|
|||||||
is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
is_addr = mg_aton_(mg_str_s(tls->hostname), &addr);
|
||||||
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
|
||||||
is_addr ? &addr : NULL);
|
is_addr ? &addr : NULL);
|
||||||
if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN
|
if (san < 0 || // RFC-6125 6.4.4 allow legacy fallback to CN
|
||||||
(san == 0 && (is_addr || mg_tls_verify_cert_cn(
|
(san == 0 &&
|
||||||
&ci->subj, tls->hostname) <= 0))) {
|
(is_addr || !mg_tls_verify_cert_cn(&ci->subj, tls->hostname)))) {
|
||||||
mg_error(c, "failed to verify host");
|
mg_error(c, "failed to verify host");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -2345,7 +2345,7 @@ static int mg_rsa_parse_der_int(const uint8_t **p, const uint8_t *end,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC 5915 ECPrivateKey ::= SEQUENCE {
|
// RFC-5915 ECPrivateKey ::= SEQUENCE {
|
||||||
// version INTEGER { ecPrivkeyVer1(1) },
|
// version INTEGER { ecPrivkeyVer1(1) },
|
||||||
// privateKey OCTET STRING,
|
// privateKey OCTET STRING,
|
||||||
// parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
|
// parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
|
||||||
@@ -2748,8 +2748,8 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
|||||||
mg_error(c, "hostname too long");
|
mg_error(c, "hostname too long");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strncpy((char *) tls->hostname, opts->name.buf, sizeof(tls->hostname) - 1);
|
memcpy(tls->hostname, opts->name.buf, opts->name.len);
|
||||||
tls->hostname[opts->name.len] = 0;
|
tls->hostname[opts->name.len] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
// server CA certificate; parse PEM [bundle] or DER
|
// server CA certificate; parse PEM [bundle] or DER
|
||||||
@@ -2762,8 +2762,9 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
|||||||
tls->ca_bundle_der = all_certs;
|
tls->ca_bundle_der = all_certs;
|
||||||
MG_VERBOSE(("%d-cert bundle", cert_count));
|
MG_VERBOSE(("%d-cert bundle", cert_count));
|
||||||
} else if (cert_count > 0) {
|
} else if (cert_count > 0) {
|
||||||
tls->ca_der.buf = all_certs[0].buf;
|
int i;
|
||||||
tls->ca_der.len = all_certs[0].len;
|
tls->ca_der = all_certs[0];
|
||||||
|
for (i = 1; i < cert_count; i++) mg_free((void *) all_certs[i].buf);
|
||||||
mg_free(all_certs);
|
mg_free(all_certs);
|
||||||
} else { // parse again for a possible DER (or a truncated begin string)
|
} else { // parse again for a possible DER (or a truncated begin string)
|
||||||
if (mg_parse_pem(opts->ca, mg_str_s("CERTIFICATE"), &tls->ca_der) < 0) {
|
if (mg_parse_pem(opts->ca, mg_str_s("CERTIFICATE"), &tls->ca_der) < 0) {
|
||||||
@@ -2789,8 +2790,7 @@ void mg_tls_init(struct mg_connection *c, const struct mg_tls_opts *opts) {
|
|||||||
int cert_count = mg_parse_pem_certs(opts->cert, &all_certs);
|
int cert_count = mg_parse_pem_certs(opts->cert, &all_certs);
|
||||||
|
|
||||||
if (cert_count > 0) {
|
if (cert_count > 0) {
|
||||||
tls->cert_der.buf = all_certs[0].buf;
|
tls->cert_der = all_certs[0];
|
||||||
tls->cert_der.len = all_certs[0].len;
|
|
||||||
if (cert_count > 1) {
|
if (cert_count > 1) {
|
||||||
tls->chain_len = (size_t) cert_count;
|
tls->chain_len = (size_t) cert_count;
|
||||||
tls->chain_der = all_certs;
|
tls->chain_der = all_certs;
|
||||||
|
|||||||
+29
-18
@@ -197,33 +197,38 @@ bool mg_path_is_sane(const struct mg_str path) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MG_ENABLE_CUSTOM_MILLIS
|
// The clock lives in mg_micros(), mg_millis() is derived from it. Either one
|
||||||
#else
|
// can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS
|
||||||
uint64_t mg_millis(void) {
|
#if MG_ENABLE_CUSTOM_MICROS == 0
|
||||||
#if MG_ARCH == MG_ARCH_ESP32 || MG_ENABLE_FREERTOS
|
uint64_t mg_micros(void) {
|
||||||
return xTaskGetTickCount() * portTICK_PERIOD_MS;
|
#if MG_ARCH == MG_ARCH_ESP32
|
||||||
|
return (uint64_t) esp_timer_get_time();
|
||||||
|
#elif MG_ENABLE_FREERTOS
|
||||||
|
return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_THREADX
|
#elif MG_ARCH == MG_ARCH_THREADX
|
||||||
return tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND);
|
return (uint64_t) tx_time_get() *
|
||||||
|
(1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_TIRTOS
|
#elif MG_ARCH == MG_ARCH_TIRTOS
|
||||||
return (uint64_t) Clock_getTicks();
|
return (uint64_t) Clock_getTicks() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_ZEPHYR
|
#elif MG_ARCH == MG_ARCH_ZEPHYR
|
||||||
return (uint64_t) k_uptime_get();
|
return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks());
|
||||||
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
|
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS1
|
||||||
return (uint64_t) rt_time_get();
|
return (uint64_t) rt_time_get() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
|
#elif MG_ARCH == MG_ARCH_CMSIS_RTOS2
|
||||||
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq());
|
return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) *
|
||||||
|
1000;
|
||||||
#elif MG_ARCH == MG_ARCH_RTTHREAD
|
#elif MG_ARCH == MG_ARCH_RTTHREAD
|
||||||
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND);
|
return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_WIN32
|
#elif MG_ARCH == MG_ARCH_WIN32
|
||||||
return GetTickCount();
|
return (uint64_t) GetTickCount() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_PICOSDK
|
#elif MG_ARCH == MG_ARCH_PICOSDK
|
||||||
return time_us_64() / 1000;
|
return time_us_64();
|
||||||
#elif MG_ARCH == MG_ARCH_CUBE
|
#elif MG_ARCH == MG_ARCH_CUBE
|
||||||
return (uint64_t) HAL_GetTick();
|
return (uint64_t) HAL_GetTick() * 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
|
#elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__)
|
||||||
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
|
// Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux
|
||||||
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
|
// Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux
|
||||||
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
|
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
|
||||||
#elif MG_ARCH == MG_ARCH_UNIX
|
#elif MG_ARCH == MG_ARCH_UNIX
|
||||||
struct timespec ts = {0, 0};
|
struct timespec ts = {0, 0};
|
||||||
// See #1615 - prefer monotonic clock
|
// See #1615 - prefer monotonic clock
|
||||||
@@ -238,15 +243,21 @@ uint64_t mg_millis(void) {
|
|||||||
// adjustments performed by adjtime and NTP
|
// adjustments performed by adjtime and NTP
|
||||||
clock_gettime(CLOCK_REALTIME, &ts);
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
#endif
|
#endif
|
||||||
return ((uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000);
|
return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000);
|
||||||
#elif defined(ARDUINO)
|
#elif defined(ARDUINO)
|
||||||
return (uint64_t) millis();
|
return (uint64_t) micros();
|
||||||
#else
|
#else
|
||||||
return (uint64_t) (time(NULL) * 1000);
|
return (uint64_t) time(NULL) * 1000000;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if MG_ENABLE_CUSTOM_MILLIS == 0
|
||||||
|
uint64_t mg_millis(void) {
|
||||||
|
return mg_micros() / 1000;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// network format equates big endian order
|
// network format equates big endian order
|
||||||
uint16_t mg_ntohs(uint16_t net) {
|
uint16_t mg_ntohs(uint16_t net) {
|
||||||
return MG_LOAD_BE16(&net);
|
return MG_LOAD_BE16(&net);
|
||||||
|
|||||||
+20
@@ -136,6 +136,26 @@ uint64_t mg_timegm(unsigned int year, unsigned int month, unsigned int day,
|
|||||||
MG_U8P(p)[7] = (uint8_t) (n); \
|
MG_U8P(p)[7] = (uint8_t) (n); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
// Little-endian counterparts, e.g. for USB. Safe for unaligned access
|
||||||
|
#define MG_LOAD_LE16(p) \
|
||||||
|
((uint16_t) (((uint16_t) MG_U8P(p)[1] << 8U) | MG_U8P(p)[0]))
|
||||||
|
#define MG_LOAD_LE32(p) \
|
||||||
|
((uint32_t) (((uint32_t) MG_U8P(p)[3] << 24U) | \
|
||||||
|
((uint32_t) MG_U8P(p)[2] << 16U) | \
|
||||||
|
((uint32_t) MG_U8P(p)[1] << 8U) | MG_U8P(p)[0]))
|
||||||
|
#define MG_STORE_LE16(p, n) \
|
||||||
|
do { \
|
||||||
|
MG_U8P(p)[0] = (uint8_t) (n); \
|
||||||
|
MG_U8P(p)[1] = (uint8_t) ((n) >> 8U); \
|
||||||
|
} while (0)
|
||||||
|
#define MG_STORE_LE32(p, n) \
|
||||||
|
do { \
|
||||||
|
MG_U8P(p)[0] = (uint8_t) (n); \
|
||||||
|
MG_U8P(p)[1] = (uint8_t) ((n) >> 8U); \
|
||||||
|
MG_U8P(p)[2] = (uint8_t) ((n) >> 16U); \
|
||||||
|
MG_U8P(p)[3] = (uint8_t) ((n) >> 24U); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
// Network / host byte-order conversion (big-endian <-> native).
|
// Network / host byte-order conversion (big-endian <-> native).
|
||||||
// mg_htons/mg_htonl/mg_htonll are aliases for the same operation (symmetric).
|
// mg_htons/mg_htonl/mg_htonll are aliases for the same operation (symmetric).
|
||||||
uint16_t mg_ntohs(uint16_t net);
|
uint16_t mg_ntohs(uint16_t net);
|
||||||
|
|||||||
@@ -192,6 +192,10 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
op = msg.flags & 15;
|
op = msg.flags & 15;
|
||||||
// MG_VERBOSE ("fin %d op %d len %d [%.*s]", final, op,
|
// MG_VERBOSE ("fin %d op %d len %d [%.*s]", final, op,
|
||||||
// (int) m.data.len, (int) m.data.len, m.data.buf));
|
// (int) m.data.len, (int) m.data.len, m.data.buf));
|
||||||
|
if (op >= WEBSOCKET_OP_CLOSE && (!final || msg.data_len > 125)) {
|
||||||
|
mg_error(c, "invalid WS control frame");
|
||||||
|
return;
|
||||||
|
} // RFC-6455 5.5: control frames must be final and at most 125 bytes
|
||||||
if ((op == WEBSOCKET_OP_CONTINUE && ofs == 0) ||
|
if ((op == WEBSOCKET_OP_CONTINUE && ofs == 0) ||
|
||||||
((op == WEBSOCKET_OP_TEXT || op == WEBSOCKET_OP_BINARY) &&
|
((op == WEBSOCKET_OP_TEXT || op == WEBSOCKET_OP_BINARY) &&
|
||||||
ofs != 0)) {
|
ofs != 0)) {
|
||||||
@@ -222,7 +226,7 @@ static void mg_ws_cb(struct mg_connection *c, int ev, void *ev_data) {
|
|||||||
c->is_draining = 1;
|
c->is_draining = 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Per RFC6455, close conn when an unknown op is recvd
|
// Per RFC-6455, close conn when an unknown op is recvd
|
||||||
mg_error(c, "unknown WS op %d", op);
|
mg_error(c, "unknown WS op %d", op);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ void mg_ws_upgrade(struct mg_connection *, struct mg_http_message *,
|
|||||||
// mg_ws_connect(), mg_ws_upgrade(), mg_ws_printf(), mg_ws_wrap()
|
// mg_ws_connect(), mg_ws_upgrade(), mg_ws_printf(), mg_ws_wrap()
|
||||||
// Notes:
|
// Notes:
|
||||||
// op is one of WEBSOCKET_OP_*. Client connections are automatically masked
|
// op is one of WEBSOCKET_OP_*. Client connections are automatically masked
|
||||||
// per RFC 6455. On OOM, the return value can be smaller than header + len.
|
// per RFC-6455. On OOM, the return value can be smaller than header + len.
|
||||||
// Data is appended to c->send and sent by a later mg_mgr_poll() call.
|
// Data is appended to c->send and sent by a later mg_mgr_poll() call.
|
||||||
size_t mg_ws_send(struct mg_connection *, const void *buf, size_t len, int op);
|
size_t mg_ws_send(struct mg_connection *, const void *buf, size_t len, int op);
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -98,7 +98,7 @@ mongoose.c: Makefile $(wildcard ../src/*.c) $(wildcard ../src/drivers/*.c)
|
|||||||
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c src/drivers/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
|
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c src/drivers/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
|
||||||
|
|
||||||
mongoose.h: $(HDRS) Makefile
|
mongoose.h: $(HDRS) Makefile
|
||||||
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; cat src/ai.h; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/os_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/lfs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_aes128.h src/tls_p256.h src/tls_p384.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/modbus.h src/json.h src/jwt.h src/rpc.h src/dash.h src/ota.h src/health.h src/mdash.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/l2_mesh.h src/bsd.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
|
cd .. && (export LC_ALL=C ; cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; cat src/ai.h; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/os_*.h src/net_ft.h src/net_lwip.h src/net_rl.h src/config.h src/profile.h src/str.h src/queue.h src/fmt.h src/printf.h src/log.h src/timer.h src/fs.h src/lfs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/sha256.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_aes128.h src/tls_p256.h src/tls_p384.h src/tls_chacha20.h src/tls_rsa.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/modbus.h src/json.h src/jwt.h src/rpc.h src/dash.h src/ota.h src/health.h src/mdash.h src/flash.h src/wifi.h src/l2.h src/net_builtin.h src/bsd.h src/drivers/*.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
|
||||||
|
|
||||||
# Check that all external (exported) symbols have "mg_" prefix
|
# Check that all external (exported) symbols have "mg_" prefix
|
||||||
mg_prefix: mongoose.c mongoose.h
|
mg_prefix: mongoose.c mongoose.h
|
||||||
|
|||||||
+9
-6
@@ -13,7 +13,7 @@ else
|
|||||||
C_WARN ?= -Wno-deprecated
|
C_WARN ?= -Wno-deprecated
|
||||||
endif
|
endif
|
||||||
|
|
||||||
.PHONY: all bsd bsd_test bsd_client bsd_nortos bsd_nortos_test shm_tap_bridge \
|
.PHONY: all bsd bsd_test bsd_client bsd_udp bsd_nortos bsd_nortos_test shm_tap_bridge \
|
||||||
shm_port_bridge bsd_s390 bsd_nortos_s390 clean
|
shm_port_bridge bsd_s390 bsd_nortos_s390 clean
|
||||||
|
|
||||||
TEST_SOURCES = bsd_shm_test.c
|
TEST_SOURCES = bsd_shm_test.c
|
||||||
@@ -47,11 +47,11 @@ DOCKER ?= $(DOCKER_BIN) run --platform linux/amd64 --net=host --rm $(ENV) -v $(R
|
|||||||
CFLAGS_MONGOOSE += -DMG_ARCH=MG_ARCH_CUSTOM
|
CFLAGS_MONGOOSE += -DMG_ARCH=MG_ARCH_CUSTOM
|
||||||
|
|
||||||
|
|
||||||
all bsd: shm_tap_bridge bsd_client FreeRTOS-Kernel bsd_test
|
all bsd: shm_tap_bridge bsd_client bsd_udp FreeRTOS-Kernel bsd_test
|
||||||
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_test $(ARGS)
|
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_test $(ARGS)
|
||||||
|
|
||||||
# also
|
# also
|
||||||
bsd_nortos: shm_tap_bridge bsd_client bsd_nortos_test
|
bsd_nortos: shm_tap_bridge bsd_client bsd_udp bsd_nortos_test
|
||||||
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_nortos_test $(ARGS)
|
ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./bsd_nortos_test $(ARGS)
|
||||||
|
|
||||||
# requires shm_tap_bridge to be running
|
# requires shm_tap_bridge to be running
|
||||||
@@ -70,6 +70,9 @@ bsd_nortos_test:
|
|||||||
bsd_client: bsd_client.c
|
bsd_client: bsd_client.c
|
||||||
$(CC) bsd_client.c -pthread -o $@
|
$(CC) bsd_client.c -pthread -o $@
|
||||||
|
|
||||||
|
bsd_udp: bsd_udp.c
|
||||||
|
$(CC) bsd_udp.c -o $@
|
||||||
|
|
||||||
# bridge shm queue <--> TAP interface
|
# bridge shm queue <--> TAP interface
|
||||||
shm_tap_bridge: shm_tap_bridge.c shm_queue.h
|
shm_tap_bridge: shm_tap_bridge.c shm_queue.h
|
||||||
$(CC) shm_tap_bridge.c -pthread -o $@
|
$(CC) shm_tap_bridge.c -pthread -o $@
|
||||||
@@ -79,7 +82,7 @@ bsd_s390: CFLAGS_MONGOOSE += -DMG_ENABLE_BSD_PROTOTYPES=0
|
|||||||
bsd_s390: ASAN=
|
bsd_s390: ASAN=
|
||||||
bsd_s390: CC = $(DOCKER) mdashnet/s390 cc
|
bsd_s390: CC = $(DOCKER) mdashnet/s390 cc
|
||||||
bsd_s390: RUN = $(DOCKER) --init mdashnet/s390
|
bsd_s390: RUN = $(DOCKER) --init mdashnet/s390
|
||||||
bsd_s390: shm_port_bridge bsd_client FreeRTOS-Kernel bsd_test
|
bsd_s390: shm_port_bridge bsd_client bsd_udp FreeRTOS-Kernel bsd_test
|
||||||
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
||||||
|
|
||||||
#also
|
#also
|
||||||
@@ -87,14 +90,14 @@ bsd_nortos_s390: CFLAGS_MONGOOSE += -DMG_ENABLE_BSD_PROTOTYPES=0
|
|||||||
bsd_nortos_s390: ASAN=
|
bsd_nortos_s390: ASAN=
|
||||||
bsd_nortos_s390: CC = $(DOCKER) mdashnet/s390 cc
|
bsd_nortos_s390: CC = $(DOCKER) mdashnet/s390 cc
|
||||||
bsd_nortos_s390: RUN = $(DOCKER) --init mdashnet/s390
|
bsd_nortos_s390: RUN = $(DOCKER) --init mdashnet/s390
|
||||||
bsd_nortos_s390: shm_port_bridge bsd_client bsd_nortos_test
|
bsd_nortos_s390: shm_port_bridge bsd_client bsd_udp bsd_nortos_test
|
||||||
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_nortos_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
$(RUN) sh -c './shm_port_bridge & pid=$$!; sleep 1; ./bsd_nortos_test; rc=$$?; kill $$pid >/dev/null 2>&1; wait $$pid >/dev/null 2>&1; exit $$rc'
|
||||||
|
|
||||||
shm_port_bridge: shm_port_bridge.c shm_queue.h
|
shm_port_bridge: shm_port_bridge.c shm_queue.h
|
||||||
$(CC) shm_port_bridge.c $(CFLAGS) -pthread -o $@
|
$(CC) shm_port_bridge.c $(CFLAGS) -pthread -o $@
|
||||||
|
|
||||||
clean: # Cleanup. Delete built program and all build artifacts
|
clean: # Cleanup. Delete built program and all build artifacts
|
||||||
rm -rf bsd_test bsd_client shm_port_bridge *.o *.obj FreeRTOS-Kernel
|
rm -rf bsd_test bsd_client bsd_udp shm_port_bridge *.o *.obj FreeRTOS-Kernel
|
||||||
|
|
||||||
FreeRTOS-Kernel: # FreeRTOS sources
|
FreeRTOS-Kernel: # FreeRTOS sources
|
||||||
git clone -q -c advice.detachedHead=false --depth 1 -b V11.2.0 https://github.com/FreeRTOS/FreeRTOS-Kernel $@
|
git clone -q -c advice.detachedHead=false --depth 1 -b V11.2.0 https://github.com/FreeRTOS/FreeRTOS-Kernel $@
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define PORT 1235
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
struct sockaddr_in sa;
|
||||||
|
struct timeval timeout;
|
||||||
|
char buf[8];
|
||||||
|
ssize_t n;
|
||||||
|
int fd, rogue;
|
||||||
|
|
||||||
|
if (argc != 2) return 1;
|
||||||
|
memset(&sa, 0, sizeof(sa));
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
sa.sin_port = htons(PORT);
|
||||||
|
if (inet_pton(AF_INET, argv[1], &sa.sin_addr) != 1) return 1;
|
||||||
|
timeout.tv_sec = 5, timeout.tv_usec = 0;
|
||||||
|
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
|
||||||
|
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0 ||
|
||||||
|
connect(fd, (struct sockaddr *) &sa, sizeof(sa)) != 0 ||
|
||||||
|
send(fd, "hello", 5, 0) != 5 || recv(fd, buf, sizeof(buf), 0) != 5 ||
|
||||||
|
memcmp(buf, "ready", 5) != 0)
|
||||||
|
goto fail;
|
||||||
|
if ((rogue = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ||
|
||||||
|
setsockopt(rogue, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) !=
|
||||||
|
0 ||
|
||||||
|
sendto(rogue, "rogueid", 7, 0, (struct sockaddr *) &sa, sizeof(sa)) !=
|
||||||
|
7 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 7 || memcmp(buf, "peerone", 7) != 0 ||
|
||||||
|
recv(rogue, buf, sizeof(buf), 0) != 7 || memcmp(buf, "rogueok", 7) != 0 ||
|
||||||
|
send(fd, "one", 3, 0) != 3 ||
|
||||||
|
sendto(rogue, "two2", 4, 0, (struct sockaddr *) &sa, sizeof(sa)) != 4 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 4 || memcmp(buf, "drop", 4) != 0 ||
|
||||||
|
send(fd, "truncate", 8, 0) != 8 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 5 || memcmp(buf, "short", 5) != 0 ||
|
||||||
|
send(fd, "peek", 4, 0) != 4 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 4 || memcmp(buf, "peek", 4) != 0 ||
|
||||||
|
sendto(rogue, "rogue", 5, 0, (struct sockaddr *) &sa, sizeof(sa)) != 5 ||
|
||||||
|
send(fd, "more", 4, 0) != 4 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 7 || memcmp(buf, "checked", 7) != 0 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 3 || memcmp(buf, "one", 3) != 0 ||
|
||||||
|
recv(fd, buf, sizeof(buf), 0) != 4 || memcmp(buf, "two2", 4) != 0 ||
|
||||||
|
send(fd, "echo", 4, 0) != 4 || recv(fd, buf, sizeof(buf), 0) != 4 ||
|
||||||
|
memcmp(buf, "echo", 4) != 0)
|
||||||
|
goto fail_close_rogue;
|
||||||
|
close(rogue);
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
fail_close_rogue:
|
||||||
|
close(rogue);
|
||||||
|
fail:
|
||||||
|
close(fd);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Loaded 100 of 208 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user