diff --git a/README.md b/README.md index 79a6fc0..4c3859a 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,9 @@ SoftwareSerial | ❌ | ❌ SPI | ❌ | ❌ Wire | ❗ | ❌ **OTHER LIBRARIES** | | -Wi-Fi STA/AP/Mixed | ✔️ | ❗/❌/❌ +Wi-Fi STA/AP/Mixed | ✔️ | ✔️/❌/❌ Wi-Fi Events | ✔️ | ❌ -TCP Client (SSL) | ✔️ (✔️) | ❓ +TCP Client (SSL) | ✔️ (✔️) | ❗ (❓) TCP Server | ✔️ | ❓ IPv6 | ❌ | ❌ HTTP Client (SSL) | ✔️ (✔️) | ❓ diff --git a/arduino/beken-72xx/libraries/WiFi/WiFiGeneric.cpp b/arduino/beken-72xx/libraries/WiFi/WiFiGeneric.cpp index 758e729..8bef505 100644 --- a/arduino/beken-72xx/libraries/WiFi/WiFiGeneric.cpp +++ b/arduino/beken-72xx/libraries/WiFi/WiFiGeneric.cpp @@ -58,9 +58,7 @@ WiFiMode WiFiClass::getMode() { } WiFiStatus WiFiClass::status() { - auto status = mhdr_get_station_status(); - LT_D_WG("mhdr_get_station_status()=%d", status); - return eventTypeToStatus(status); + return eventTypeToStatus(mhdr_get_station_status()); } IPAddress WiFiClass::hostByName(const char *hostname) { diff --git a/arduino/beken-72xx/libraries/WiFi/WiFiSTA.cpp b/arduino/beken-72xx/libraries/WiFi/WiFiSTA.cpp index 231c24a..cfefad7 100644 --- a/arduino/beken-72xx/libraries/WiFi/WiFiSTA.cpp +++ b/arduino/beken-72xx/libraries/WiFi/WiFiSTA.cpp @@ -93,7 +93,9 @@ bool WiFiClass::reconnect(const uint8_t *bssid) { } LT_D_WG("Starting WiFi..."); + __wrap_bk_printf_disable(); bk_wlan_start_sta(&config); + __wrap_bk_printf_enable(); LT_D_WG("bk_wlan_start() OK"); return true; @@ -172,9 +174,8 @@ bool WiFiClass::setMacAddress(const uint8_t *mac) { } const String WiFiClass::SSID() { - if (!isConnected() || !wpas_connect_ssid) - return ""; - return (char *)wpas_connect_ssid->ssid; + bk_wlan_get_link_status(LINK_STATUS); + return (char *)LINK_STATUS->ssid; } const String WiFiClass::psk() { @@ -187,8 +188,6 @@ const String WiFiClass::psk() { } uint8_t *WiFiClass::BSSID() { - if (!isConnected()) - return NULL; bk_wlan_get_link_status(LINK_STATUS); return LINK_STATUS->bssid; } diff --git a/arduino/beken-72xx/libraries/WiFi/WiFiScan.cpp b/arduino/beken-72xx/libraries/WiFi/WiFiScan.cpp index 02ec7e1..33aba52 100644 --- a/arduino/beken-72xx/libraries/WiFi/WiFiScan.cpp +++ b/arduino/beken-72xx/libraries/WiFi/WiFiScan.cpp @@ -15,33 +15,25 @@ static void scanHandler(void *ctx, uint8_t param) { return; } - struct sta_scan_res *result; - uint8_t count = bk_wlan_get_scan_ap_result_numbers(); - if (count == 0) { - LT_D_WG("No APs found"); + ScanResult_adv result; + if (wlan_sta_scan_result(&result)) { + LT_E("Failed to get scan result"); 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); - if (!result) { - LT_W("sta_scan_res alloc failed"); - goto end; - } - bk_wlan_get_scan_ap_result(result, count); - - cls->scanAlloc(count); + cls->scanAlloc(result.ApNum); if (!scan->ap) { LT_W("scan->ap alloc failed"); goto end; } - for (uint8_t i = 0; i < count; i++) { - scan->ap[i].ssid = strdup(result[i].ssid); - scan->ap[i].auth = securityTypeToAuthMode(result[i].security); - scan->ap[i].rssi = result[i].level; - scan->ap[i].channel = result[i].channel; - memcpy(scan->ap[i].bssid.addr, result[i].bssid, 6); + for (uint8_t i = 0; i < result.ApNum; i++) { + scan->ap[i].ssid = strdup(result.ApList[i].ssid); + scan->ap[i].auth = securityTypeToAuthMode(result.ApList[i].security); + scan->ap[i].rssi = result.ApList[i].ApPower; + scan->ap[i].channel = result.ApList[i].channel; + memcpy(scan->ap[i].bssid.addr, result.ApList[i].bssid, 6); } end: diff --git a/arduino/libretuya/core/LibreTuyaConfig.h b/arduino/libretuya/core/LibreTuyaConfig.h index 32d6637..85b0ce8 100644 --- a/arduino/libretuya/core/LibreTuyaConfig.h +++ b/arduino/libretuya/core/LibreTuyaConfig.h @@ -49,6 +49,11 @@ #define LT_LOG_HEAP 0 #endif +// Debug errno values using LT_ERRNO() +#ifndef LT_LOG_ERRNO +#define LT_LOG_ERRNO 0 +#endif + // Per-module debugging #ifndef LT_DEBUG_WIFI #define LT_DEBUG_WIFI 0 diff --git a/arduino/libretuya/core/lt_logger.h b/arduino/libretuya/core/lt_logger.h index 9765317..f6c6156 100644 --- a/arduino/libretuya/core/lt_logger.h +++ b/arduino/libretuya/core/lt_logger.h @@ -134,6 +134,16 @@ void lt_log(const uint8_t level, const char *format, ...); 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 #define LT_T_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__) #define LT_V_WG(...) LT_T_MOD(LT_DEBUG_WIFI, __VA_ARGS__) diff --git a/arduino/libretuya/libraries/NetUtils/lwip/LwIPClient.cpp b/arduino/libretuya/libraries/NetUtils/lwip/LwIPClient.cpp index d1e140e..290a105 100644 --- a/arduino/libretuya/libraries/NetUtils/lwip/LwIPClient.cpp +++ b/arduino/libretuya/libraries/NetUtils/lwip/LwIPClient.cpp @@ -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 sock = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock < 0) { + LT_D_WC("socket failed"); 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); + LT_ERRNO(); + struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); 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, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)); + LT_ERRNO(); + lwip_fcntl(sock, F_SETFL, lwip_fcntl(sock, F_GETFL, 0) & ~O_NONBLOCK); + LT_ERRNO(); + _connected = true; _sock = std::make_shared(sock); _rxBuffer = std::make_shared(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; } @@ -222,6 +230,7 @@ int LwIPClient::available() { return 0; int res = _rxBuffer->available(); if (_rxBuffer->failed()) { + LT_ERRNO(); stop(); } return res; diff --git a/arduino/libretuya/libraries/NetUtils/lwip/LwIPRxBuffer.cpp b/arduino/libretuya/libraries/NetUtils/lwip/LwIPRxBuffer.cpp index 4c2fb14..fecf26f 100644 --- a/arduino/libretuya/libraries/NetUtils/lwip/LwIPRxBuffer.cpp +++ b/arduino/libretuya/libraries/NetUtils/lwip/LwIPRxBuffer.cpp @@ -14,11 +14,13 @@ extern "C" { size_t LwIPRxBuffer::r_available() { if (_sock < 0) { + LT_D_WC("_sock < 0"); return 0; } uint16_t count = 0; int res = lwip_ioctl(_sock, FIONREAD, &count); if (res < 0) { + LT_D_WC("lwip_ioctl()=%d, errno=%d", res, errno); _failed = true; return 0; } @@ -29,7 +31,7 @@ size_t LwIPRxBuffer::fillBuffer() { if (!_buffer) { _buffer = (uint8_t *)malloc(_size); if (!_buffer) { - printf("[e] Not enough memory to allocate buffer\r\n"); + LT_E("buffer alloc failed"); _failed = true; return 0; } @@ -44,6 +46,7 @@ size_t LwIPRxBuffer::fillBuffer() { int res = lwip_recv(_sock, _buffer + _fill, _size - _fill, MSG_DONTWAIT); if (res < 0) { if (errno != EWOULDBLOCK) { + LT_ERRNO(); _failed = true; } return 0; diff --git a/builder/frameworks/beken-72xx-sdk.py b/builder/frameworks/beken-72xx-sdk.py index 1e943f0..7881846 100644 --- a/builder/frameworks/beken-72xx-sdk.py +++ b/builder/frameworks/beken-72xx-sdk.py @@ -75,6 +75,8 @@ env.Append( ("MBEDTLS_CONFIG_FILE", ""), ("WIFI_BLE_COEXIST", "1"), ("WOLFSSL_BEKEN", env.Cfg("CFG_WPA3")), + # LwIP options + ("LWIP_SO_RCVBUF", "1"), # for ioctl(FIONREAD) ], ASFLAGS=[ "-mcpu=arm968e-s", diff --git a/docs/config.md b/docs/config.md index 8218640..5fdc8a9 100644 --- a/docs/config.md +++ b/docs/config.md @@ -40,6 +40,7 @@ build_flags = - `LT_LOGGER_COLOR` - output ANSI terminal colors - `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_ERRNO` - print and clear errno value (if set) using `LT_ERRNO()` ### Debug logging diff --git a/platform/beken-7231t/fixups/sys_config.h b/platform/beken-7231t/fixups/sys_config.h index 687edf0..0291e51 100644 --- a/platform/beken-7231t/fixups/sys_config.h +++ b/platform/beken-7231t/fixups/sys_config.h @@ -87,7 +87,7 @@ #define CFG_WIFI_RAW_TX_CMD 0 #define CFG_WIFI_SENSOR 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_XTAL_FREQUENCE CFG_XTAL_FREQUENCE_26M #define CFG_XTAL_FREQUENCE_26M 26000000 diff --git a/platform/common/fixups/errno.h b/platform/common/fixups/errno.h index 3e47b85..20ef3df 100644 --- a/platform/common/fixups/errno.h +++ b/platform/common/fixups/errno.h @@ -16,3 +16,7 @@ #include // use system __errno() & error codes #undef errno // undefine __errno() macro 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