|
43 | 43 | #include <SPI.h> |
44 | 44 | #endif |
45 | 45 |
|
46 | | - |
47 | 46 | const unsigned long READ_TIMEOUT_MS = 2000; |
48 | 47 |
|
49 | 48 | class ClientWrapper { |
@@ -357,37 +356,37 @@ class WiFiAdapter : public ArduinoNetworkIf { |
357 | 356 |
|
358 | 357 | bool begin() override { |
359 | 358 | // Put ESP32 in station mode and try to keep things deterministic during connect |
360 | | - WiFi.persistent(false); // avoid writing credentials repeatedly to NVS |
| 359 | + WiFi.persistent(false); // avoid writing credentials repeatedly to NVS |
361 | 360 | WiFi.mode(WIFI_STA); |
362 | | - WiFi.setSleep(false); // keep radio awake during provisioning/UDP XCP |
363 | | - // Optional set TX power (requires Arduino-ESP32 supporting WiFi.setTxPower) |
364 | | -#ifdef XCP_ON_WIFI_TX_POWER |
| 361 | + WiFi.setSleep(false); // keep radio awake during provisioning/UDP XCP |
| 362 | + // Optional set TX power (requires Arduino-ESP32 supporting WiFi.setTxPower) |
| 363 | + #ifdef XCP_ON_WIFI_TX_POWER |
365 | 364 | WiFi.setTxPower(XCP_ON_WIFI_TX_POWER); |
366 | | -#endif |
| 365 | + #endif |
367 | 366 |
|
368 | 367 | // Optional static IP configuration (do this BEFORE begin()) |
369 | 368 | { |
370 | | -#if defined(XCP_ON_ETHERNET_IP_OCTETS) |
| 369 | + #if defined(XCP_ON_ETHERNET_IP_OCTETS) |
371 | 370 | IPAddress ip(XCP_ON_ETHERNET_IP_OCTETS); |
372 | 371 | if (!WiFi.config(ip)) { |
373 | 372 | Serial.println("[WiFi] Failed to apply static IP config"); |
374 | 373 | } |
375 | | -#elif defined(XCP_ON_ETHERNET_IP) |
| 374 | + #elif defined(XCP_ON_ETHERNET_IP) |
376 | 375 | IPAddress ip; |
377 | 376 | ip.fromString(XCP_ON_ETHERNET_IP); |
378 | 377 | if (!WiFi.config(ip)) { |
379 | 378 | Serial.println("[WiFi] Failed to apply static IP config"); |
380 | 379 | } |
381 | | -#endif |
| 380 | + #endif |
382 | 381 | } |
383 | 382 |
|
384 | 383 | Serial.print("[WiFi] Connecting to SSID: "); |
385 | 384 | Serial.println(m_ssid ? m_ssid : "<null>"); |
386 | 385 | wl_status_t startStatus = WiFi.begin(m_ssid, m_pass); |
387 | | - (void)startStatus; // not all cores return a meaningful value here |
| 386 | + (void)startStatus; // not all cores return a meaningful value here |
388 | 387 |
|
389 | | - const unsigned long timeoutMs = 15000; // allow a bit more time on congested APs |
390 | | - unsigned long start = millis(); |
| 388 | + const unsigned long timeoutMs = 15000; // allow a bit more time on congested APs |
| 389 | + unsigned long start = millis(); |
391 | 390 | wl_status_t st; |
392 | 391 | while ((st = WiFi.status()) != WL_CONNECTED && (millis() - start < timeoutMs)) { |
393 | 392 | switch (st) { |
@@ -455,7 +454,7 @@ class WiFiAdapter : public ArduinoNetworkIf { |
455 | 454 | Serial.println("[WiFi] UDP listener started after reconnect"); |
456 | 455 | } |
457 | 456 | } |
458 | | - m_reconnect_backoff_ms = 2000; // reset backoff on success |
| 457 | + m_reconnect_backoff_ms = 2000; // reset backoff on success |
459 | 458 | } else { |
460 | 459 | // Ensure UDP is not used while disconnected/connecting |
461 | 460 | if (m_udp_active) { |
@@ -491,8 +490,8 @@ class WiFiAdapter : public ArduinoNetworkIf { |
491 | 490 | const char* m_ssid; |
492 | 491 | const char* m_pass; |
493 | 492 | WiFiUdpClientWrapper m_udpClientWrapper; |
494 | | - bool m_udp_active = false; |
495 | | - unsigned long m_reconnect_backoff_ms = 2000; // start with 2s backoff |
| 493 | + bool m_udp_active = false; |
| 494 | + unsigned long m_reconnect_backoff_ms = 2000; // start with 2s backoff |
496 | 495 | }; |
497 | 496 | #endif // XCP_ON_ETHERNET_ARDUINO_DRIVER == XCP_ON_ETHERNET_DRIVER_WIFI |
498 | 497 |
|
@@ -554,63 +553,80 @@ class FrameParser { |
554 | 553 | /* TCP client wrapper and adapter for WiFi (ESP32) */ |
555 | 554 | class WiFiClientWrapper : public ClientWrapper { |
556 | 555 | public: |
557 | | - explicit WiFiClientWrapper(WiFiClient c = WiFiClient()) : m_client(c) {} |
558 | 556 |
|
559 | | - void set(WiFiClient c) { m_client = c; } |
| 557 | + explicit WiFiClientWrapper(WiFiClient c = WiFiClient()) : m_client(c) { |
| 558 | + } |
560 | 559 |
|
561 | | - int available() override { return m_client ? m_client.available() : 0; } |
| 560 | + void set(WiFiClient c) { |
| 561 | + m_client = c; |
| 562 | + } |
562 | 563 |
|
563 | | - int read_bytes(uint8_t* buf, size_t len) override { return m_client.read(buf, len); } |
| 564 | + int available() override { |
| 565 | + return m_client ? m_client.available() : 0; |
| 566 | + } |
564 | 567 |
|
565 | | - size_t write_bytes(const uint8_t* buf, size_t len) override { return m_client.write(buf, len); } |
| 568 | + int read_bytes(uint8_t* buf, size_t len) override { |
| 569 | + return m_client.read(buf, len); |
| 570 | + } |
566 | 571 |
|
567 | | - bool connected() override { return m_client && m_client.connected(); } |
| 572 | + size_t write_bytes(const uint8_t* buf, size_t len) override { |
| 573 | + return m_client.write(buf, len); |
| 574 | + } |
| 575 | + |
| 576 | + bool connected() override { |
| 577 | + return m_client && m_client.connected(); |
| 578 | + } |
568 | 579 |
|
569 | 580 | void stop() override { |
570 | 581 | if (m_client) { |
571 | 582 | m_client.stop(); |
572 | 583 | } |
573 | 584 | } |
574 | 585 |
|
575 | | - bool is_datagram() const override { return false; } |
| 586 | + bool is_datagram() const override { |
| 587 | + return false; |
| 588 | + } |
576 | 589 |
|
577 | 590 | private: |
| 591 | + |
578 | 592 | WiFiClient m_client; |
579 | 593 | }; |
580 | 594 |
|
581 | 595 | class WiFiTcpAdapter : public ArduinoNetworkIf { |
582 | 596 | public: |
583 | | - WiFiTcpAdapter(const char* s, const char* p, uint16_t port) : m_port(port), m_ssid(s), m_pass(p), m_server(port) {} |
| 597 | + |
| 598 | + WiFiTcpAdapter(const char* s, const char* p, uint16_t port) : m_port(port), m_ssid(s), m_pass(p), m_server(port) { |
| 599 | + } |
584 | 600 |
|
585 | 601 | bool begin() override { |
586 | 602 | // Station mode tweaks |
587 | 603 | WiFi.persistent(false); |
588 | 604 | WiFi.mode(WIFI_STA); |
589 | 605 | WiFi.setSleep(false); |
590 | | -#ifdef XCP_ON_WIFI_TX_POWER |
| 606 | + #ifdef XCP_ON_WIFI_TX_POWER |
591 | 607 | WiFi.setTxPower(XCP_ON_WIFI_TX_POWER); |
592 | | -#endif |
| 608 | + #endif |
593 | 609 | Serial.begin(115200); |
594 | 610 | while (!Serial) { |
595 | 611 | ; |
596 | 612 | } |
597 | 613 | Serial.println("\nBlueparrot XCP (TCP) on WiFi"); |
598 | 614 |
|
599 | 615 | // Optional static IP configuration |
600 | | -#if defined(XCP_ON_ETHERNET_IP_OCTETS) |
| 616 | + #if defined(XCP_ON_ETHERNET_IP_OCTETS) |
601 | 617 | IPAddress ip(XCP_ON_ETHERNET_IP_OCTETS); |
602 | 618 | if (!WiFi.config(ip)) { |
603 | 619 | Serial.println("[WiFi] Failed to apply static IP config"); |
604 | 620 | } |
605 | | -#elif defined(XCP_ON_ETHERNET_IP) |
| 621 | + #elif defined(XCP_ON_ETHERNET_IP) |
606 | 622 | { |
607 | 623 | IPAddress ip; |
608 | 624 | ip.fromString(XCP_ON_ETHERNET_IP); |
609 | 625 | if (!WiFi.config(ip)) { |
610 | 626 | Serial.println("[WiFi] Failed to apply static IP config (string)"); |
611 | 627 | } |
612 | 628 | } |
613 | | -#endif |
| 629 | + #endif |
614 | 630 | WiFi.begin(m_ssid, m_pass); |
615 | 631 | unsigned long start = millis(); |
616 | 632 | while (WiFi.status() != WL_CONNECTED && (millis() - start) < 15000UL) { |
@@ -657,11 +673,12 @@ class WiFiTcpAdapter : public ArduinoNetworkIf { |
657 | 673 | } |
658 | 674 |
|
659 | 675 | private: |
660 | | - uint16_t m_port; |
661 | | - const char* m_ssid; |
662 | | - const char* m_pass; |
663 | | - WiFiServer m_server; |
664 | | - WiFiClient m_client; |
| 676 | + |
| 677 | + uint16_t m_port; |
| 678 | + const char* m_ssid; |
| 679 | + const char* m_pass; |
| 680 | + WiFiServer m_server; |
| 681 | + WiFiClient m_client; |
665 | 682 | WiFiClientWrapper m_wrapper; |
666 | 683 | }; |
667 | 684 |
|
@@ -722,9 +739,9 @@ extern "C" { |
722 | 739 | /* Initialize backend network adapter and start listening. */ |
723 | 740 | #if XCP_ON_ETHERNET_ARDUINO_DRIVER == XCP_ON_ETHERNET_DRIVER_WIFI |
724 | 741 | #if (XCP_ON_ETHERNET_PROTOCOL == XCP_ON_ETHERNET_USE_TCP) |
725 | | - s_net = new WiFiTcpAdapter(XCP_ON_ETHERNET_WIFI_SSID, XCP_ON_ETHERNET_WIFI_PASSWORD, XCP_ON_ETHERNET_PORT); |
| 742 | + s_net = new WiFiTcpAdapter(XCP_ON_ETHERNET_WIFI_SSID, XCP_ON_ETHERNET_WIFI_PASSWORD, XCP_ON_ETHERNET_PORT); |
726 | 743 | #else |
727 | | - s_net = new WiFiAdapter(XCP_ON_ETHERNET_WIFI_SSID, XCP_ON_ETHERNET_WIFI_PASSWORD, XCP_ON_ETHERNET_PORT); |
| 744 | + s_net = new WiFiAdapter(XCP_ON_ETHERNET_WIFI_SSID, XCP_ON_ETHERNET_WIFI_PASSWORD, XCP_ON_ETHERNET_PORT); |
728 | 745 | #endif |
729 | 746 | #else |
730 | 747 | #if defined(XCP_ON_ETHERNET_IP_OCTETS) |
|
0 commit comments