[beken-72xx] Fix WiFi station connection

This commit is contained in:
Kuba Szczodrzyński
2022-06-29 19:00:15 +02:00
parent ded6638ef6
commit cf5dfb43d6
12 changed files with 54 additions and 31 deletions

View File

@@ -54,9 +54,9 @@ SoftwareSerial | ❌ | ❌
SPI | ❌ | ❌ SPI | ❌ | ❌
Wire | ❗ | ❌ Wire | ❗ | ❌
**OTHER LIBRARIES** | | **OTHER LIBRARIES** | |
Wi-Fi STA/AP/Mixed | ✔️ | /❌/❌ Wi-Fi STA/AP/Mixed | ✔️ | ✔️/❌/❌
Wi-Fi Events | ✔️ | ❌ Wi-Fi Events | ✔️ | ❌
TCP Client (SSL) | ✔️ (✔️) | TCP Client (SSL) | ✔️ (✔️) | ❗ (❓)
TCP Server | ✔️ | ❓ TCP Server | ✔️ | ❓
IPv6 | ❌ | ❌ IPv6 | ❌ | ❌
HTTP Client (SSL) | ✔️ (✔️) | ❓ HTTP Client (SSL) | ✔️ (✔️) | ❓

View File

@@ -58,9 +58,7 @@ WiFiMode WiFiClass::getMode() {
} }
WiFiStatus WiFiClass::status() { WiFiStatus WiFiClass::status() {
auto status = mhdr_get_station_status(); return eventTypeToStatus(mhdr_get_station_status());
LT_D_WG("mhdr_get_station_status()=%d", status);
return eventTypeToStatus(status);
} }
IPAddress WiFiClass::hostByName(const char *hostname) { IPAddress WiFiClass::hostByName(const char *hostname) {

View File

@@ -93,7 +93,9 @@ bool WiFiClass::reconnect(const uint8_t *bssid) {
} }
LT_D_WG("Starting WiFi..."); LT_D_WG("Starting WiFi...");
__wrap_bk_printf_disable();
bk_wlan_start_sta(&config); bk_wlan_start_sta(&config);
__wrap_bk_printf_enable();
LT_D_WG("bk_wlan_start() OK"); LT_D_WG("bk_wlan_start() OK");
return true; return true;
@@ -172,9 +174,8 @@ bool WiFiClass::setMacAddress(const uint8_t *mac) {
} }
const String WiFiClass::SSID() { const String WiFiClass::SSID() {
if (!isConnected() || !wpas_connect_ssid) bk_wlan_get_link_status(LINK_STATUS);
return ""; return (char *)LINK_STATUS->ssid;
return (char *)wpas_connect_ssid->ssid;
} }
const String WiFiClass::psk() { const String WiFiClass::psk() {
@@ -187,8 +188,6 @@ const String WiFiClass::psk() {
} }
uint8_t *WiFiClass::BSSID() { uint8_t *WiFiClass::BSSID() {
if (!isConnected())
return NULL;
bk_wlan_get_link_status(LINK_STATUS); bk_wlan_get_link_status(LINK_STATUS);
return LINK_STATUS->bssid; return LINK_STATUS->bssid;
} }

View File

@@ -15,33 +15,25 @@ static void scanHandler(void *ctx, uint8_t param) {
return; return;
} }
struct sta_scan_res *result; ScanResult_adv result;
uint8_t count = bk_wlan_get_scan_ap_result_numbers(); if (wlan_sta_scan_result(&result)) {
if (count == 0) { LT_E("Failed to get scan result");
LT_D_WG("No APs found");
goto end; goto end;
} }
LT_D_WG("Found %d APs", count); LT_D_WG("Found %d APs", result.ApNum);
result = (struct sta_scan_res *)malloc(sizeof(struct sta_scan_res) * count); cls->scanAlloc(result.ApNum);
if (!result) {
LT_W("sta_scan_res alloc failed");
goto end;
}
bk_wlan_get_scan_ap_result(result, count);
cls->scanAlloc(count);
if (!scan->ap) { if (!scan->ap) {
LT_W("scan->ap alloc failed"); LT_W("scan->ap alloc failed");
goto end; goto end;
} }
for (uint8_t i = 0; i < count; i++) { for (uint8_t i = 0; i < result.ApNum; i++) {
scan->ap[i].ssid = strdup(result[i].ssid); scan->ap[i].ssid = strdup(result.ApList[i].ssid);
scan->ap[i].auth = securityTypeToAuthMode(result[i].security); scan->ap[i].auth = securityTypeToAuthMode(result.ApList[i].security);
scan->ap[i].rssi = result[i].level; scan->ap[i].rssi = result.ApList[i].ApPower;
scan->ap[i].channel = result[i].channel; scan->ap[i].channel = result.ApList[i].channel;
memcpy(scan->ap[i].bssid.addr, result[i].bssid, 6); memcpy(scan->ap[i].bssid.addr, result.ApList[i].bssid, 6);
} }
end: end:

View File

@@ -49,6 +49,11 @@
#define LT_LOG_HEAP 0 #define LT_LOG_HEAP 0
#endif #endif
// Debug errno values using LT_ERRNO()
#ifndef LT_LOG_ERRNO
#define LT_LOG_ERRNO 0
#endif
// Per-module debugging // Per-module debugging
#ifndef LT_DEBUG_WIFI #ifndef LT_DEBUG_WIFI
#define LT_DEBUG_WIFI 0 #define LT_DEBUG_WIFI 0

View File

@@ -134,6 +134,16 @@ void lt_log(const uint8_t level, const char *format, ...);
return ret; \ return ret; \
} }
#if LT_LOG_ERRNO
#define LT_ERRNO() \
if (errno) { \
LT_E("errno=%d", errno); \
errno = 0; \
}
#else
#define LT_ERRNO()
#endif
// WiFi.cpp // WiFi.cpp
#define LT_T_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__) #define LT_T_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__)
#define LT_V_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__) #define LT_V_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__)

View File

@@ -87,6 +87,7 @@ int LwIPClient::connect(const char *host, uint16_t port, int32_t timeout) {
int LwIPClient::connect(IPAddress ip, uint16_t port, int32_t timeout) { int LwIPClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
int sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) { if (sock < 0) {
LT_D_WC("socket failed");
return -1; return -1;
} }
@@ -95,6 +96,8 @@ int LwIPClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) | O_NONBLOCK); lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) | O_NONBLOCK);
LT_ERRNO();
struct sockaddr_in addr; struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@@ -142,8 +145,12 @@ int LwIPClient::connect(IPAddress ip, uint16_t port, int32_t timeout) {
lwip_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); lwip_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable));
lwip_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); lwip_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable));
LT_ERRNO();
lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) & ~O_NONBLOCK); lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) & ~O_NONBLOCK);
LT_ERRNO();
_connected = true; _connected = true;
_sock = std::make_shared<SocketHandle>(sock); _sock = std::make_shared<SocketHandle>(sock);
_rxBuffer = std::make_shared<LwIPRxBuffer>(sock); _rxBuffer = std::make_shared<LwIPRxBuffer>(sock);
@@ -214,6 +221,7 @@ size_t LwIPClient::write(const uint8_t *buf, size_t size) {
} }
} }
} }
LT_D_WC("wrote %d bytes", written);
return written; return written;
} }
@@ -222,6 +230,7 @@ int LwIPClient::available() {
return 0; return 0;
int res = _rxBuffer->available(); int res = _rxBuffer->available();
if (_rxBuffer->failed()) { if (_rxBuffer->failed()) {
LT_ERRNO();
stop(); stop();
} }
return res; return res;

View File

@@ -14,11 +14,13 @@ extern "C" {
size_t LwIPRxBuffer::r_available() { size_t LwIPRxBuffer::r_available() {
if (_sock < 0) { if (_sock < 0) {
LT_D_WC("_sock < 0");
return 0; return 0;
} }
uint16_t count = 0; uint16_t count = 0;
int res = lwip_ioctl(_sock, FIONREAD, &count); int res = lwip_ioctl(_sock, FIONREAD, &count);
if (res < 0) { if (res < 0) {
LT_D_WC("lwip_ioctl()=%d, errno=%d", res, errno);
_failed = true; _failed = true;
return 0; return 0;
} }
@@ -29,7 +31,7 @@ size_t LwIPRxBuffer::fillBuffer() {
if (!_buffer) { if (!_buffer) {
_buffer = (uint8_t *)malloc(_size); _buffer = (uint8_t *)malloc(_size);
if (!_buffer) { if (!_buffer) {
printf("[e] Not enough memory to allocate buffer\r\n"); LT_E("buffer alloc failed");
_failed = true; _failed = true;
return 0; return 0;
} }
@@ -44,6 +46,7 @@ size_t LwIPRxBuffer::fillBuffer() {
int res = lwip_recv(_sock, _buffer + _fill, _size - _fill, MSG_DONTWAIT); int res = lwip_recv(_sock, _buffer + _fill, _size - _fill, MSG_DONTWAIT);
if (res < 0) { if (res < 0) {
if (errno != EWOULDBLOCK) { if (errno != EWOULDBLOCK) {
LT_ERRNO();
_failed = true; _failed = true;
} }
return 0; return 0;

View File

@@ -75,6 +75,8 @@ env.Append(
("MBEDTLS_CONFIG_FILE", "<tls_config.h>"), ("MBEDTLS_CONFIG_FILE", "<tls_config.h>"),
("WIFI_BLE_COEXIST", "1"), ("WIFI_BLE_COEXIST", "1"),
("WOLFSSL_BEKEN", env.Cfg("CFG_WPA3")), ("WOLFSSL_BEKEN", env.Cfg("CFG_WPA3")),
# LwIP options
("LWIP_SO_RCVBUF", "1"), # for ioctl(FIONREAD)
], ],
ASFLAGS=[ ASFLAGS=[
"-mcpu=arm968e-s", "-mcpu=arm968e-s",

View File

@@ -40,6 +40,7 @@ build_flags =
- `LT_LOGGER_COLOR` - output ANSI terminal colors - `LT_LOGGER_COLOR` - output ANSI terminal colors
- `LT_PRINTF_BROKEN` - whether printf outputs "0." for floats with value 0 - `LT_PRINTF_BROKEN` - whether printf outputs "0." for floats with value 0
- `LT_LOG_HEAP` - print free heap size using `LT_HEAP_I()` - `LT_LOG_HEAP` - print free heap size using `LT_HEAP_I()`
- `LT_LOG_ERRNO` - print and clear errno value (if set) using `LT_ERRNO()`
### Debug logging ### Debug logging

View File

@@ -87,7 +87,7 @@
#define CFG_WIFI_RAW_TX_CMD 0 #define CFG_WIFI_RAW_TX_CMD 0
#define CFG_WIFI_SENSOR 0 #define CFG_WIFI_SENSOR 0
#define CFG_WLAN_FAST_CONNECT 0 #define CFG_WLAN_FAST_CONNECT 0
#define CFG_WPA_CTRL_IFACE 0 #define CFG_WPA_CTRL_IFACE 1
#define CFG_WPA3 0 #define CFG_WPA3 0
#define CFG_XTAL_FREQUENCE CFG_XTAL_FREQUENCE_26M #define CFG_XTAL_FREQUENCE CFG_XTAL_FREQUENCE_26M
#define CFG_XTAL_FREQUENCE_26M 26000000 #define CFG_XTAL_FREQUENCE_26M 26000000

View File

@@ -16,3 +16,7 @@
#include <sys/errno.h> // use system __errno() & error codes #include <sys/errno.h> // use system __errno() & error codes
#undef errno // undefine __errno() macro #undef errno // undefine __errno() macro
extern int errno; // use a global errno variable extern int errno; // use a global errno variable
#define errno errno // for #ifdef errno in lwIP
// make sure lwIP never defines its own error codes
#undef LWIP_PROVIDE_ERRNO