feat: WLAN-Treiber mit Onboarding-AP, Build-Credentials und Befehl net

This commit is contained in:
Michael Sauer
2026-09-11 22:50:43 +02:00
parent 2f7cadaeee
commit 4c2f9e5374
12 changed files with 361 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
#include "net/nl_mdns.h"
/* Placeholder until the mDNS responder lands (plan 2, task 7). */
void nl_mdns_on_ip_up(void) {}
void nl_mdns_set_name(const char *name) { (void)name; }
+9
View File
@@ -0,0 +1,9 @@
#ifndef NL_MDNS_H
#define NL_MDNS_H
/* Called by the network task once the station got an IP (again). */
void nl_mdns_on_ip_up(void);
/* Device name for the TXT record; re-announces when mDNS is running. */
void nl_mdns_set_name(const char *name);
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef NL_WIFI_H
#define NL_WIFI_H
#include <stdbool.h>
#include <stdint.h>
#include "config/nl_settings.h"
typedef struct {
const char *mode; /* "sta" | "ap" | "apsta" */
char ssid[NL_SSID_LEN]; /* configured station SSID */
int rssi; /* dBm, 0 when not connected */
char ip[16]; /* station IP, else the AP IP when the AP is up, else "" */
char sta_ip[16]; /* "" when the station has no IP */
const char *error; /* result of the last onboarding test or NULL */
} nl_wifi_status_t;
/* Starts the network task. An empty ssid starts the onboarding AP. Strings are copied. */
void nl_wifi_start(const char *ssid, const char *pass, const char *ap_pass);
/* Starts an onboarding test with new credentials (from the HTTP task). 0 = queued. */
int nl_wifi_request_onboard(const char *ssid, const char *pass);
/* True while the unconfigured onboarding AP is open (API without token). */
bool nl_wifi_open_access(void);
void nl_wifi_status(nl_wifi_status_t *out);
/* "ninlight-<mac4>", valid after nl_wifi_start(). */
const char *nl_wifi_hostname(void);
void nl_wifi_mac(uint8_t mac[6]);
#endif
+250
View File
@@ -0,0 +1,250 @@
#include <stdio.h>
#include <string.h>
#include "wm_include.h"
#include "wm_efuse.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h" /* struct netif is only forward-declared by wm_netif2.0.3.h */
#include "light/nl_engine.h"
#include "net/nl_mdns.h"
#include "net/nl_names.h"
#include "net/nl_wifi.h"
#include "net/nl_wifi_sm.h"
#include "sys/nl_app.h"
#define NL_NET_PRIO 35
#define NL_NET_QUEUE 8
#define MSG_NETIF 0x100u /* | netif status */
#define MSG_ONBOARD 0x200u
static u32 s_stack[768];
static tls_os_queue_t *s_queue;
static tls_os_mutex_t *s_lock; /* guards s_sm and the credential buffers */
static nl_wifi_sm_t s_sm;
static bool s_started;
static uint8_t s_mac[6];
static char s_hostname[24];
static char s_ssid[NL_SSID_LEN], s_pass[NL_PASS_LEN];
static char s_test_ssid[NL_SSID_LEN], s_test_pass[NL_PASS_LEN];
static char s_ap_pass[NL_PASS_LEN];
static void lock(void) { tls_os_mutex_acquire(s_lock, 0); }
static void unlock(void) { tls_os_mutex_release(s_lock); }
static void copy(char *dst, const char *src, size_t size)
{
strncpy(dst, src, size - 1);
dst[size - 1] = '\0';
}
/* SDK system task context: only hand the event over. */
static void netif_event(u8 status)
{
tls_os_queue_send(s_queue, (void *)(MSG_NETIF | status), 0);
}
static void start_ap(void)
{
struct tls_softap_info_t ap;
struct tls_ip_info_t ip;
size_t n = strlen(s_ap_pass);
int r;
memset(&ap, 0, sizeof ap);
memset(&ip, 0, sizeof ip);
copy((char *)ap.ssid, s_hostname, sizeof ap.ssid);
ap.encrypt = IEEE80211_ENCRYT_CCMP_WPA2;
ap.channel = 6;
ap.keyinfo.format = 1; /* ascii */
ap.keyinfo.index = 1;
ap.keyinfo.key_len = (u8)n;
memcpy(ap.keyinfo.key, s_ap_pass, n);
ip.ip_addr[0] = 192; ip.ip_addr[1] = 168; ip.ip_addr[2] = 4; ip.ip_addr[3] = 1;
ip.netmask[0] = 255; ip.netmask[1] = 255; ip.netmask[2] = 255; ip.netmask[3] = 0;
memcpy(ip.dnsname, "local", 6);
r = tls_wifi_softap_create(&ap, &ip);
printf("wifi: AP %s %s\r\n", s_hostname, r == WM_SUCCESS ? "up" : "failed");
}
static void connect_station(bool test)
{
char ssid[NL_SSID_LEN], pass[NL_PASS_LEN];
lock();
copy(ssid, test ? s_test_ssid : s_ssid, sizeof ssid);
copy(pass, test ? s_test_pass : s_pass, sizeof pass);
unlock();
printf("wifi: connecting to %s\r\n", ssid);
tls_wifi_connect((u8 *)ssid, (u8)strlen(ssid), (u8 *)pass, (u8)strlen(pass));
}
static void pulse(bool on)
{
nl_app_lock();
nl_engine_pulse(on);
nl_app_unlock();
}
static void execute(nl_wifi_actions_t a)
{
if (a.save_test_creds) {
char ssid[NL_SSID_LEN], pass[NL_PASS_LEN];
lock();
copy(s_ssid, s_test_ssid, sizeof s_ssid);
copy(s_pass, s_test_pass, sizeof s_pass);
copy(ssid, s_ssid, sizeof ssid);
copy(pass, s_pass, sizeof pass);
unlock();
printf("wifi: onboarding ok, saving credentials for %s\r\n", ssid);
nl_app_set_wifi_credentials(ssid, pass);
}
if (a.pulse_on)
pulse(true);
if (a.pulse_off)
pulse(false);
if (a.start_ap)
start_ap();
if (a.stop_ap) {
printf("wifi: AP down\r\n");
tls_wifi_softap_destroy();
}
if (a.connect)
connect_station(a.use_test_creds);
}
static nl_wifi_error_t join_error(void)
{
switch (tls_wifi_get_errno()) {
case WM_WIFI_EKEY: return NL_WIFI_ERR_AUTH;
case WM_WIFI_ENOAP: return NL_WIFI_ERR_NOT_FOUND;
default: return NL_WIFI_ERR_TIMEOUT;
}
}
static void net_task(void *arg)
{
(void)arg;
for (;;) {
void *msg = NULL;
nl_wifi_actions_t ev, tick;
uint32_t m = 0, now;
bool ip_up = false;
memset(&ev, 0, sizeof ev);
if (tls_os_queue_receive(s_queue, &msg, 0, HZ / 10) == TLS_OS_SUCCESS)
m = (uint32_t)msg;
now = nl_app_now_ms();
lock();
if (m == (MSG_NETIF | NETIF_IP_NET_UP)) {
ev = nl_wifi_sm_event(&s_sm, NL_WIFI_EV_CONNECTED, NL_WIFI_ERR_NONE, now);
ip_up = true;
} else if (m == (MSG_NETIF | NETIF_WIFI_JOIN_FAILED)) {
ev = nl_wifi_sm_event(&s_sm, NL_WIFI_EV_JOIN_FAILED, join_error(), now);
} else if (m == (MSG_NETIF | NETIF_WIFI_DISCONNECTED)) {
ev = nl_wifi_sm_event(&s_sm, NL_WIFI_EV_DISCONNECTED, NL_WIFI_ERR_NONE, now);
} else if (m == MSG_ONBOARD) {
ev = nl_wifi_sm_onboard(&s_sm, now);
}
tick = nl_wifi_sm_tick(&s_sm, now);
unlock();
if (m & MSG_NETIF)
printf("wifi: netif event %u\r\n", (unsigned)(m & 0xFFu));
execute(ev);
execute(tick);
if (ip_up)
nl_mdns_on_ip_up();
}
}
void nl_wifi_start(const char *ssid, const char *pass, const char *ap_pass)
{
u8 auto_mode = WIFI_AUTO_CNT_OFF;
nl_wifi_actions_t a;
struct netif *nif;
tls_get_mac_addr(s_mac);
nl_names_hostname(s_mac, s_hostname, sizeof s_hostname);
copy(s_ssid, ssid, sizeof s_ssid);
copy(s_pass, pass, sizeof s_pass);
copy(s_ap_pass, ap_pass, sizeof s_ap_pass);
/* The SDK would otherwise reconnect on its own with credentials from its parameter area. */
tls_wifi_auto_connect_flag(WIFI_AUTO_CNT_FLAG_GET, &auto_mode);
if (auto_mode != WIFI_AUTO_CNT_OFF) {
auto_mode = WIFI_AUTO_CNT_OFF;
tls_wifi_auto_connect_flag(WIFI_AUTO_CNT_FLAG_SET, &auto_mode);
}
tls_wifi_disconnect();
nif = tls_get_netif();
if (nif)
nif->hostname = s_hostname; /* DHCP host name */
tls_os_mutex_create(0, &s_lock);
tls_os_queue_create(&s_queue, NL_NET_QUEUE);
tls_netif_add_status_event(netif_event);
lock();
a = nl_wifi_sm_start(&s_sm, s_ssid[0] != '\0', nl_app_now_ms());
unlock();
s_started = true;
printf("wifi: %s, hostname %s\r\n", s_ssid[0] ? "station" : "onboarding AP", s_hostname);
execute(a);
tls_os_task_create(NULL, "nl_net", net_task, NULL, (u8 *)s_stack, sizeof(s_stack), NL_NET_PRIO, 0);
}
int nl_wifi_request_onboard(const char *ssid, const char *pass)
{
if (!s_started)
return -1;
lock();
copy(s_test_ssid, ssid, sizeof s_test_ssid);
copy(s_test_pass, pass, sizeof s_test_pass);
unlock();
return tls_os_queue_send(s_queue, (void *)MSG_ONBOARD, 0) == TLS_OS_SUCCESS ? 0 : -1;
}
bool nl_wifi_open_access(void)
{
bool r;
if (!s_started)
return false;
lock();
r = nl_wifi_sm_open_access(&s_sm);
unlock();
return r;
}
void nl_wifi_status(nl_wifi_status_t *out)
{
struct tls_ethif *e = tls_netif_get_ethif();
bool ap_up = false;
memset(out, 0, sizeof *out);
out->mode = "sta";
if (s_started) {
lock();
out->mode = nl_wifi_sm_mode(&s_sm);
out->error = nl_wifi_error_name(s_sm.error);
copy(out->ssid, s_ssid, sizeof out->ssid);
ap_up = s_sm.ap_up || s_sm.state == NL_WIFI_ST_AP_ONBOARDING;
unlock();
}
if (e && e->status) {
struct tls_curr_bss_t bss;
ip4addr_ntoa_r(ip_2_ip4(&e->ip_addr), out->sta_ip, sizeof out->sta_ip);
memset(&bss, 0, sizeof bss);
tls_wifi_get_current_bss(&bss);
out->rssi = (int)(int8_t)bss.rssi;
}
if (out->sta_ip[0])
copy(out->ip, out->sta_ip, sizeof out->ip);
else if (ap_up)
copy(out->ip, "192.168.4.1", sizeof out->ip);
}
const char *nl_wifi_hostname(void) { return s_hostname; }
void nl_wifi_mac(uint8_t mac[6]) { memcpy(mac, s_mac, 6); }