Skip to content

Commit bcc8e3d

Browse files
committed
Update progress
1 parent 9716d5d commit bcc8e3d

16 files changed

+1381
-1213
lines changed

CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ set(ARDUINO_LIBRARY_WiFi_SRCS
217217
libraries/WiFi/src/WiFiGeneric.cpp
218218
libraries/WiFi/src/WiFiMulti.cpp
219219
libraries/WiFi/src/WiFiScan.cpp
220-
libraries/WiFi/src/WiFiSTA.cpp)
220+
libraries/WiFi/src/WiFiSTA.cpp
221+
libraries/WiFi/src/STA.cpp)
221222

222223
set(ARDUINO_LIBRARY_WiFiProv_SRCS libraries/WiFiProv/src/WiFiProv.cpp)
223224

libraries/Ethernet/src/ETH.cpp

+22-43
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info)
7070
}
7171
}
7272
if(index == 3){
73+
log_e("Could not find ETH interface with that handle!");
7374
return;
7475
}
75-
if (Network.getStatusBits() & ETH_WANT_IP6_BIT(index)){
76+
if (_ethernets[index]->getStatusBits() & ESP_NETIF_WANT_IP6_BIT){
7677
esp_err_t err = esp_netif_create_ip6_linklocal(_ethernets[index]->netif());
7778
if(err != ESP_OK){
7879
log_e("Failed to enable IPv6 Link Local on ETH: [%d] %s", err, esp_err_to_name(err));
@@ -83,34 +84,10 @@ static void onEthConnected(arduino_event_id_t event, arduino_event_info_t info)
8384
}
8485
}
8586

86-
esp_eth_handle_t ETHClass::handle(){
87+
esp_eth_handle_t ETHClass::handle() const {
8788
return _eth_handle;
8889
}
8990

90-
bool ETHClass::connected()
91-
{
92-
return Network.getStatusBits() & ETH_CONNECTED_BIT(_eth_index);
93-
}
94-
95-
bool ETHClass::started()
96-
{
97-
return Network.getStatusBits() & ETH_STARTED_BIT(_eth_index);
98-
}
99-
100-
bool ETHClass::enableIPv6(bool en)
101-
{
102-
// if(_esp_netif == NULL){
103-
// return false;
104-
// }
105-
if (en) {
106-
Network.setStatusBits(ETH_WANT_IP6_BIT(_eth_index));
107-
} else {
108-
Network.clearStatusBits(ETH_WANT_IP6_BIT(_eth_index));
109-
}
110-
return true;
111-
// return esp_netif_create_ip6_linklocal(_esp_netif) == 0;
112-
}
113-
11491
void ETHClass::_onEthEvent(int32_t event_id, void* event_data){
11592
arduino_event_t arduino_event;
11693
arduino_event.event_id = ARDUINO_EVENT_MAX;
@@ -119,19 +96,19 @@ void ETHClass::_onEthEvent(int32_t event_id, void* event_data){
11996
log_v("%s Connected", desc());
12097
arduino_event.event_id = ARDUINO_EVENT_ETH_CONNECTED;
12198
arduino_event.event_info.eth_connected = handle();
122-
Network.setStatusBits(ETH_CONNECTED_BIT(_eth_index));
99+
setStatusBits(ESP_NETIF_CONNECTED_BIT);
123100
} else if (event_id == ETHERNET_EVENT_DISCONNECTED) {
124101
log_v("%s Disconnected", desc());
125102
arduino_event.event_id = ARDUINO_EVENT_ETH_DISCONNECTED;
126-
Network.clearStatusBits(ETH_CONNECTED_BIT(_eth_index) | ETH_HAS_IP_BIT(_eth_index) | ETH_HAS_IP6_BIT(_eth_index));
103+
clearStatusBits(ESP_NETIF_CONNECTED_BIT | ESP_NETIF_HAS_IP_BIT | ESP_NETIF_HAS_LOCAL_IP6_BIT | ESP_NETIF_HAS_GLOBAL_IP6_BIT);
127104
} else if (event_id == ETHERNET_EVENT_START) {
128105
log_v("%s Started", desc());
129106
arduino_event.event_id = ARDUINO_EVENT_ETH_START;
130-
Network.setStatusBits(ETH_STARTED_BIT(_eth_index));
107+
setStatusBits(ESP_NETIF_STARTED_BIT);
131108
} else if (event_id == ETHERNET_EVENT_STOP) {
132109
log_v("%s Stopped", desc());
133110
arduino_event.event_id = ARDUINO_EVENT_ETH_STOP;
134-
Network.clearStatusBits(ETH_STARTED_BIT(_eth_index) | ETH_CONNECTED_BIT(_eth_index) | ETH_HAS_IP_BIT(_eth_index) | ETH_HAS_IP6_BIT(_eth_index));
111+
clearStatusBits(ESP_NETIF_STARTED_BIT | ESP_NETIF_CONNECTED_BIT | ESP_NETIF_HAS_IP_BIT | ESP_NETIF_HAS_LOCAL_IP6_BIT | ESP_NETIF_HAS_GLOBAL_IP6_BIT);
135112
}
136113

137114
if(arduino_event.event_id < ARDUINO_EVENT_MAX){
@@ -332,7 +309,7 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
332309
if(!perimanSetPinBus(_pin_power, ESP32_BUS_TYPE_ETHERNET_PWR, (void *)(this), -1, -1)){ goto err; }
333310
}
334311

335-
Network.onEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
312+
Network.onSysEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
336313

337314
// holds a few milliseconds to let DHCP start and enter into a good state
338315
// FIX ME -- adresses issue https://github.com/espressif/arduino-esp32/issues/5733
@@ -740,7 +717,7 @@ bool ETHClass::beginSPI(eth_phy_type_t type, int32_t phy_addr, int cs, int irq,
740717
if(!perimanSetPinBus(_pin_rst, ESP32_BUS_TYPE_ETHERNET_SPI, (void *)(this), -1, -1)){ goto err; }
741718
}
742719

743-
Network.onEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
720+
Network.onSysEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
744721

745722
return true;
746723

@@ -835,7 +812,7 @@ void ETHClass::end(void)
835812
}
836813
}
837814

838-
bool ETHClass::fullDuplex()
815+
bool ETHClass::fullDuplex() const
839816
{
840817
if(_eth_handle == NULL){
841818
return false;
@@ -845,7 +822,7 @@ bool ETHClass::fullDuplex()
845822
return (link_duplex == ETH_DUPLEX_FULL);
846823
}
847824

848-
bool ETHClass::autoNegotiation()
825+
bool ETHClass::autoNegotiation() const
849826
{
850827
if(_eth_handle == NULL){
851828
return false;
@@ -855,7 +832,7 @@ bool ETHClass::autoNegotiation()
855832
return auto_nego;
856833
}
857834

858-
uint32_t ETHClass::phyAddr()
835+
uint32_t ETHClass::phyAddr() const
859836
{
860837
if(_eth_handle == NULL){
861838
return 0;
@@ -865,7 +842,7 @@ uint32_t ETHClass::phyAddr()
865842
return phy_addr;
866843
}
867844

868-
uint8_t ETHClass::linkSpeed()
845+
uint8_t ETHClass::linkSpeed() const
869846
{
870847
if(_eth_handle == NULL){
871848
return 0;
@@ -882,17 +859,19 @@ uint8_t ETHClass::linkSpeed()
882859
// }
883860
// }
884861

885-
void ETHClass::printDriverInfo(Print & out){
886-
out.print(",");
887-
out.print(linkSpeed());
888-
out.print("M");
862+
size_t ETHClass::printDriverInfo(Print & out) const {
863+
size_t bytes = 0;
864+
bytes += out.print(",");
865+
bytes += out.print(linkSpeed());
866+
bytes += out.print("M");
889867
if(fullDuplex()){
890-
out.print(",FULL_DUPLEX");
868+
bytes += out.print(",FULL_DUPLEX");
891869
}
892870
if(autoNegotiation()){
893-
out.print(",AUTO");
871+
bytes += out.print(",AUTO");
894872
}
895-
out.printf(",ADDR:0x%lX", phyAddr());
873+
bytes += out.printf(",ADDR:0x%lX", phyAddr());
874+
return bytes;
896875
}
897876

898877
ETHClass ETH;

libraries/Ethernet/src/ETH.h

+7-12
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,14 @@ class ETHClass: public ESP_Network_Interface {
136136
}
137137

138138
void end();
139-
bool enableIPv6(bool en=true);
140-
141-
// Event based getters
142-
bool connected();
143-
bool started();
144139

145140
// ETH Handle APIs
146-
bool fullDuplex();
147-
uint8_t linkSpeed();
148-
bool autoNegotiation();
149-
uint32_t phyAddr();
141+
bool fullDuplex() const;
142+
uint8_t linkSpeed() const;
143+
bool autoNegotiation() const;
144+
uint32_t phyAddr() const;
150145

151-
esp_eth_handle_t handle();
146+
esp_eth_handle_t handle() const;
152147

153148
#if ETH_SPI_SUPPORTS_CUSTOM
154149
static esp_err_t _eth_spi_read(void *ctx, uint32_t cmd, uint32_t addr, void *data, uint32_t data_len);
@@ -162,8 +157,8 @@ class ETHClass: public ESP_Network_Interface {
162157
#endif
163158

164159
// void getMac(uint8_t* mac);
165-
void printDriverInfo(Print & out);
166-
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
160+
size_t printDriverInfo(Print & out) const;
161+
// static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
167162

168163
public:
169164
void _onEthEvent(int32_t event_id, void* event_data);

libraries/Networking/src/ESP_Network_Events.cpp

+79-47
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ static void _network_event_task(void * arg){
2727

2828
ESP_Network_Events::ESP_Network_Events()
2929
: _arduino_event_group(NULL)
30-
, _arduino_event_group_h(NULL)
3130
, _arduino_event_queue(NULL)
3231
, _arduino_event_task_handle(NULL)
3332
{}
@@ -41,10 +40,6 @@ ESP_Network_Events::~ESP_Network_Events(){
4140
vEventGroupDelete(_arduino_event_group);
4241
_arduino_event_group = NULL;
4342
}
44-
if(_arduino_event_group_h != NULL){
45-
vEventGroupDelete(_arduino_event_group_h);
46-
_arduino_event_group_h = NULL;
47-
}
4843
if(_arduino_event_queue != NULL){
4944
arduino_event_t *event = NULL;
5045
while(xQueueReceive(_arduino_event_queue, &event, 0) == pdTRUE){
@@ -66,13 +61,6 @@ bool ESP_Network_Events::initNetworkEvents(){
6661
}
6762
xEventGroupSetBits(_arduino_event_group, _initial_bits);
6863
}
69-
if(!_arduino_event_group_h){
70-
_arduino_event_group_h = xEventGroupCreate();
71-
if(!_arduino_event_group_h){
72-
log_e("Network Event Group 2 Create Failed!");
73-
return false;
74-
}
75-
}
7664

7765
if(!_arduino_event_queue){
7866
_arduino_event_queue = xQueueCreate(32, sizeof(arduino_event_t*));
@@ -189,6 +177,48 @@ network_event_handle_t ESP_Network_Events::onEvent(NetworkEventSysCb cbEvent, ar
189177
return newEventHandler.id;
190178
}
191179

180+
network_event_handle_t ESP_Network_Events::onSysEvent(NetworkEventCb cbEvent, arduino_event_id_t event)
181+
{
182+
if(!cbEvent) {
183+
return 0;
184+
}
185+
NetworkEventCbList_t newEventHandler;
186+
newEventHandler.cb = cbEvent;
187+
newEventHandler.fcb = NULL;
188+
newEventHandler.scb = NULL;
189+
newEventHandler.event = event;
190+
cbEventList.insert(cbEventList.begin(), newEventHandler);
191+
return newEventHandler.id;
192+
}
193+
194+
network_event_handle_t ESP_Network_Events::onSysEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event)
195+
{
196+
if(!cbEvent) {
197+
return 0;
198+
}
199+
NetworkEventCbList_t newEventHandler;
200+
newEventHandler.cb = NULL;
201+
newEventHandler.fcb = cbEvent;
202+
newEventHandler.scb = NULL;
203+
newEventHandler.event = event;
204+
cbEventList.insert(cbEventList.begin(), newEventHandler);
205+
return newEventHandler.id;
206+
}
207+
208+
network_event_handle_t ESP_Network_Events::onSysEvent(NetworkEventSysCb cbEvent, arduino_event_id_t event)
209+
{
210+
if(!cbEvent) {
211+
return 0;
212+
}
213+
NetworkEventCbList_t newEventHandler;
214+
newEventHandler.cb = NULL;
215+
newEventHandler.fcb = NULL;
216+
newEventHandler.scb = cbEvent;
217+
newEventHandler.event = event;
218+
cbEventList.insert(cbEventList.begin(), newEventHandler);
219+
return newEventHandler.id;
220+
}
221+
192222
void ESP_Network_Events::removeEvent(NetworkEventCb cbEvent, arduino_event_id_t event)
193223
{
194224
if(!cbEvent) {
@@ -203,6 +233,27 @@ void ESP_Network_Events::removeEvent(NetworkEventCb cbEvent, arduino_event_id_t
203233
}
204234
}
205235

236+
template<typename T, typename... U>
237+
static size_t getStdFunctionAddress(std::function<T(U...)> f) {
238+
typedef T(fnType)(U...);
239+
fnType ** fnPointer = f.template target<fnType*>();
240+
return (size_t) *fnPointer;
241+
}
242+
243+
void ESP_Network_Events::removeEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event)
244+
{
245+
if(!cbEvent) {
246+
return;
247+
}
248+
249+
for(uint32_t i = 0; i < cbEventList.size(); i++) {
250+
NetworkEventCbList_t entry = cbEventList[i];
251+
if(getStdFunctionAddress(entry.fcb) == getStdFunctionAddress(cbEvent) && entry.event == event) {
252+
cbEventList.erase(cbEventList.begin() + i);
253+
}
254+
}
255+
}
256+
206257
void ESP_Network_Events::removeEvent(NetworkEventSysCb cbEvent, arduino_event_id_t event)
207258
{
208259
if(!cbEvent) {
@@ -232,62 +283,42 @@ int ESP_Network_Events::setStatusBits(int bits){
232283
_initial_bits |= bits;
233284
return _initial_bits;
234285
}
235-
int l=bits & 0x00FFFFFF, h=(bits & 0xFF000000) >> 24;
236-
if(l){
237-
l = xEventGroupSetBits(_arduino_event_group, l);
238-
}
239-
if(h){
240-
h = xEventGroupSetBits(_arduino_event_group_h, h);
241-
}
242-
return l | (h << 24);
286+
return xEventGroupSetBits(_arduino_event_group, bits);
243287
}
244288

245289
int ESP_Network_Events::clearStatusBits(int bits){
246290
if(!_arduino_event_group){
247291
_initial_bits &= ~bits;
248292
return _initial_bits;
249293
}
250-
int l=bits & 0x00FFFFFF, h=(bits & 0xFF000000) >> 24;
251-
if(l){
252-
l = xEventGroupClearBits(_arduino_event_group, l);
253-
}
254-
if(h){
255-
h = xEventGroupClearBits(_arduino_event_group_h, h);
256-
}
257-
return l | (h << 24);
294+
return xEventGroupClearBits(_arduino_event_group, bits);
258295
}
259296

260297
int ESP_Network_Events::getStatusBits(){
261298
if(!_arduino_event_group){
262299
return _initial_bits;
263300
}
264-
return xEventGroupGetBits(_arduino_event_group) | (xEventGroupGetBits(_arduino_event_group_h) << 24);
301+
return xEventGroupGetBits(_arduino_event_group);
265302
}
266303

267304
int ESP_Network_Events::waitStatusBits(int bits, uint32_t timeout_ms){
268305
if(!_arduino_event_group){
269306
return 0;
270307
}
271-
int l=bits & 0x00FFFFFF, h=(bits & 0xFF000000) >> 24;
272-
if(l){
273-
l = xEventGroupWaitBits(
274-
_arduino_event_group, // The event group being tested.
275-
l, // The bits within the event group to wait for.
276-
pdFALSE, // bits should be cleared before returning.
277-
pdTRUE, // Don't wait for all bits, any bit will do.
278-
timeout_ms / portTICK_PERIOD_MS ) & l; // Wait a maximum of timeout_ms for any bit to be set.
279-
}
280-
if(h){
281-
h = xEventGroupWaitBits(
282-
_arduino_event_group, // The event group being tested.
283-
h, // The bits within the event group to wait for.
284-
pdFALSE, // bits should be cleared before returning.
285-
pdTRUE, // Don't wait for all bits, any bit will do.
286-
timeout_ms / portTICK_PERIOD_MS ) & h; // Wait a maximum of timeout_ms for any bit to be set.
287-
}
288-
return l | (h << 24);
308+
return xEventGroupWaitBits(
309+
_arduino_event_group, // The event group being tested.
310+
bits, // The bits within the event group to wait for.
311+
pdFALSE, // bits should be cleared before returning.
312+
pdTRUE, // Don't wait for all bits, any bit will do.
313+
timeout_ms / portTICK_PERIOD_MS ) & bits; // Wait a maximum of timeout_ms for any bit to be set.
289314
}
290315

316+
/**
317+
* @brief Convert arduino_event_id_t to a C string.
318+
* @param [in] id The event id to be converted.
319+
* @return A string representation of the event id.
320+
* @note: arduino_event_id_t values as of Mar 2023 (arduino-esp32 r2.0.7) are: 0-39 (ARDUINO_EVENT_MAX=40) and are defined in WiFiGeneric.h.
321+
*/
291322
const char * ESP_Network_Events::eventName(arduino_event_id_t id) {
292323
switch(id) {
293324
case ARDUINO_EVENT_ETH_START: return "ETH_START";
@@ -306,6 +337,7 @@ const char * ESP_Network_Events::eventName(arduino_event_id_t id) {
306337
// case ARDUINO_EVENT_PPP_LOST_IP: return "PPP_LOST_IP";
307338
// case ARDUINO_EVENT_PPP_GOT_IP6: return "PPP_GOT_IP6";
308339
#if SOC_WIFI_SUPPORTED
340+
case ARDUINO_EVENT_WIFI_OFF: return "WIFI_OFF";
309341
case ARDUINO_EVENT_WIFI_READY: return "WIFI_READY";
310342
case ARDUINO_EVENT_WIFI_SCAN_DONE: return "SCAN_DONE";
311343
case ARDUINO_EVENT_WIFI_STA_START: return "STA_START";

0 commit comments

Comments
 (0)