Skip to content

Commit 044e287

Browse files
committed
[amb] Gate shared Realtek core for AmebaD; WiFi event queue fixes
1 parent dc5de53 commit 044e287

5 files changed

Lines changed: 58 additions & 11 deletions

File tree

cores/realtek-amb/arduino/libraries/WiFi/WiFiEvents.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
#define WIFI_EVENT_MAX_ROW 3
88

9+
// Lowest address treated as a real pointer in wifi_indication(). Drivers on
10+
// some families pass a small integer in buf instead of a pointer.
11+
#define WIFI_EVENT_BUF_MIN_ADDR 0x1000
12+
913
static xQueueHandle wifiEventQueueHandle = NULL;
1014
static xTaskHandle wifiEventTaskHandle = NULL;
1115

@@ -72,14 +76,32 @@ void wifi_indication(rtw_event_indicate_t event, char *buf, int buf_len, int fla
7276
return;
7377
if (wifiEventQueueHandle && wifiEventTaskHandle) {
7478
rtw_event_t *ev = (rtw_event_t *)malloc(sizeof(rtw_event_t));
75-
if (buf_len > 0) {
79+
if (!ev)
80+
return;
81+
// flags == -2 marks an Arduino event: buf points to an EventInfo and
82+
// buf_len carries the event id, so it is not a length. Some drivers
83+
// also pass a value rather than a pointer in buf (AmebaD sends buf=0x1,
84+
// buf_len=2 while switching modes), so copy only from a real address.
85+
// Check buf_len signed, before the cast: a negative length would become
86+
// SIZE_MAX and pass an unsigned test.
87+
char *bufCopy = NULL;
88+
if ((uintptr_t)buf >= WIFI_EVENT_BUF_MIN_ADDR && (flags == -2 || buf_len > 0)) {
89+
size_t copyLen = flags == -2 ? sizeof(EventInfo) : (size_t)buf_len;
7690
// copy data to allow freeing from calling scopes
77-
char *bufCopy = (char *)malloc(buf_len);
78-
memcpy(bufCopy, buf, buf_len);
79-
ev->buf = bufCopy;
80-
} else {
81-
ev->buf = NULL;
91+
bufCopy = (char *)malloc(copyLen);
92+
if (!bufCopy) {
93+
// drop the event instead of queueing a NULL payload: !bufCopy
94+
// below then means exactly "no payload was supplied"
95+
free(ev);
96+
return;
97+
}
98+
memcpy(bufCopy, buf, copyLen);
8299
}
100+
ev->buf = bufCopy;
101+
if (!bufCopy && flags != -2)
102+
// no payload survived, so the length must not be forwarded either;
103+
// on flags == -2 buf_len is the event id, not a length — keep it
104+
buf_len = 0;
83105
ev->event = event;
84106
ev->buf_len = buf_len;
85107
ev->flags = flags;
@@ -94,8 +116,9 @@ static void wifiEventTask(void *arg) {
94116
for (;;) {
95117
if (xQueueReceive(wifiEventQueueHandle, &data, portMAX_DELAY) == pdTRUE) {
96118
handleRtwEvent(data->event, data->buf, data->buf_len, data->flags);
119+
// the queue owns the copy made in wifi_indication, for every flags
120+
// value; handleRtwEvent() never frees what it is passed
97121
if (data->buf) {
98-
// free memory allocated in wifi_indication
99122
free(data->buf);
100123
}
101124
free(data);
@@ -123,8 +146,10 @@ void handleRtwEvent(uint16_t event, char *data, int len, int flags) {
123146
// already an Arduino event, just pass it
124147
EventId eventId = (EventId)len;
125148
EventInfo *eventInfo = (EventInfo *)data;
126-
pWiFi->postEvent(eventId, *eventInfo);
127-
free(eventInfo);
149+
EventInfo empty = {};
150+
// the caller owns data: the queue frees its copy in wifiEventTask(),
151+
// and direct callers free their own buffer after wifi_indication()
152+
pWiFi->postEvent(eventId, eventInfo ? *eventInfo : empty);
128153
return;
129154
}
130155

@@ -183,6 +208,8 @@ void handleRtwEvent(uint16_t event, char *data, int len, int flags) {
183208

184209
case WIFI_EVENT_STA_DISASSOC:
185210
// data(6) is MAC
211+
if (!data || len < 6)
212+
return;
186213
eventId = ARDUINO_EVENT_WIFI_AP_STADISCONNECTED;
187214
memcpy(eventInfo.wifi_ap_stadisconnected.mac, (const char *)data, 6);
188215
break;

cores/realtek-amb/arduino/libraries/WiFi/WiFiGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool WiFiClass::modePriv(WiFiMode mode, WiFiModeAction sta, WiFiModeAction ap) {
101101
LT_DM(WIFI, "Mode: %s DISABLE", WLAN1_NAME);
102102
netif_set_link_down(WLAN1_NETIF);
103103
netif_set_down(WLAN1_NETIF);
104-
#if !LT_RTL8720C
104+
#if !LT_RTL8720C && !LT_RTL8720D
105105
rltk_stop_softap(WLAN1_NAME);
106106
#else
107107
rltk_suspend_softap(WLAN1_NAME);

cores/realtek-amb/arduino/src/wiring_digital.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ void pinMode(pin_size_t pinNumber, PinMode pinMode) {
4646
mode = PullNone;
4747
break;
4848
case OUTPUT_OPENDRAIN:
49-
dir = PIN_OUTPUT;
49+
dir = PIN_OUTPUT;
50+
#if LT_RTL8720D
51+
// AmebaD's mbed PinMode enum has no OpenDrain member; the GPIO
52+
// block drives open-drain via PullNone + OUT with the bit low.
53+
mode = PullNone;
54+
#else
5055
mode = OpenDrain;
56+
#endif
5157
break;
5258
default:
5359
return;

cores/realtek-amb/base/api/lt_ota.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include <libretiny.h>
44
#include <sdk_private.h>
55

6+
// AmebaD selects OTA slots by image signature, not the bit flag;
7+
// see cores/realtek-ambd/base/api/lt_ota.c
8+
#if !LT_RTL8720D
9+
610
#define SYSTEM_DATA_LENGTH 128
711

812
/**
@@ -73,3 +77,5 @@ bool lt_ota_dual_switch_flag() {
7377
bit_flag <<= 1;
7478
return lt_flash_write(FLASH_SYSTEM_OFFSET + 4, (void *)&bit_flag, 4) == 4;
7579
}
80+
81+
#endif // !LT_RTL8720D

cores/realtek-amb/base/sdk_private.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ extern "C" {
2525
#include <hal_sys_ctrl.h>
2626
#include <rtl8710c.h>
2727
#endif
28+
#if LT_RTL8720D
29+
#include <ameba_soc.h>
30+
// AmebaD's osdep/wireless.h renames the wext mode enum (RTK_IW_MODE) with
31+
// RTW_-prefixed members; same values and order as the IW_ names used here.
32+
#define IW_MODE_INFRA RTW_MODE_INFRA
33+
#define IW_MODE_MASTER RTW_MODE_MASTER
34+
#endif
2835

2936
#include <cmsis_os.h>
3037
#undef malloc
@@ -89,4 +96,5 @@ extern flash_t lt_flash_obj;
8996

9097
#ifdef __cplusplus
9198
} // extern "C"
99+
92100
#endif

0 commit comments

Comments
 (0)