Skip to content

Commit

Permalink
ESP32 - Upgrade CHIP to ESP-IDF release v4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrishi committed Sep 10, 2020
1 parent 3b0505b commit 683ec7e
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 142 deletions.
4 changes: 2 additions & 2 deletions examples/wifi-echo/server/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ Development Framework and the xtensa-esp32-elf toolchain.
The VSCode devcontainer has these components pre-installed, so you can skip this
step. To install these components manually, follow these steps:

- Clone the Expressif ESP-IDF and checkout version 4.0
- Clone the Espressif ESP-IDF and checkout release tag v4.1

$ mkdir ${HOME}/tools
$ cd ${HOME}/tools
$ git clone https://github.com/espressif/esp-idf.git
$ cd esp-idf
$ git checkout release/v4.0
$ git checkout tags/v4.1
$ git submodule update --init
$ export IDF_PATH=${HOME}/tools/esp-idf
$ ./install.sh
Expand Down
1 change: 0 additions & 1 deletion examples/wifi-echo/server/esp32/main/EchoClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
#include <string.h>
#include <sys/param.h>

Expand Down
7 changes: 4 additions & 3 deletions examples/wifi-echo/server/esp32/main/EchoDeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "RendezvousSession.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_netif.h"
#include <platform/CHIPDeviceLayer.h>
#include <support/CodeUtils.h>

Expand Down Expand Up @@ -56,11 +57,11 @@ void EchoDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, int
ESP_LOGI(TAG, "Current free heap: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT));
if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established)
{
tcpip_adapter_ip_info_t ipInfo;
if (tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo) == ESP_OK)
esp_netif_ip_info_t ipInfo;
if (esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ipInfo) == ESP_OK)
{
char ipAddrStr[INET_ADDRSTRLEN];
IPAddress::FromIPv4(ipInfo.ip).ToString(ipAddrStr, sizeof(ipAddrStr));
esp_ip4addr_ntoa(&ipInfo.ip, ipAddrStr, sizeof(ipAddrStr));
ESP_LOGI(TAG, "Server ready at: %s:%d", ipAddrStr, CHIP_PORT);

// Since the commissioner device does not yet have a mechanism to discover the IP address
Expand Down
10 changes: 3 additions & 7 deletions examples/wifi-echo/server/esp32/main/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"

#include <algorithm>
#include <stdint.h>
Expand Down Expand Up @@ -247,12 +246,9 @@ void PairingComplete(Optional<NodeId> peerNodeId, uint16_t peerKeyId, uint16_t l
// The echo server assumes the platform's networking has been setup already
void startServer()
{
CHIP_ERROR err = CHIP_NO_ERROR;
struct netif * ipV6NetIf = NULL;
tcpip_adapter_get_netif(TCPIP_ADAPTER_IF_AP, (void **) &ipV6NetIf);

err = sessions.Init(kLocalNodeId, &DeviceLayer::SystemLayer,
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6).SetInterfaceId(ipV6NetIf),
CHIP_ERROR err = CHIP_NO_ERROR;
err = sessions.Init(kLocalNodeId, &DeviceLayer::SystemLayer,
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv6).SetInterfaceId(NULL),
UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(kIPAddressType_IPv4));
SuccessOrExit(err);

Expand Down
12 changes: 5 additions & 7 deletions examples/wifi-echo/server/esp32/main/wifi-echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,16 @@
#include "RendezvousSession.h"
#include "ScreenManager.h"
#include "WiFiWidget.h"
#include "esp_event_loop.h"
#include "esp_heap_caps_init.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_spi_flash.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"

#include "tcpip_adapter.h"

#include <cmath>
#include <cstdio>
#include <string>
Expand Down Expand Up @@ -348,10 +346,10 @@ void SetupPretendDevices()

void GetGatewayIP(char * ip_buf, size_t ip_len)
{
tcpip_adapter_ip_info_t ip;
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip);
IPAddress::FromIPv4(ip.ip).ToString(ip_buf, ip_len);
ESP_LOGE(TAG, "Got gateway ip %s", ip_buf);
esp_netif_ip_info_t ipInfo;
esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"), &ipInfo);
esp_ip4addr_ntoa(&ipInfo.ip, ip_buf, ip_len);
ESP_LOGI(TAG, "Got gateway ip %s", ip_buf);
}

bool isRendezvousBLE()
Expand Down
2 changes: 1 addition & 1 deletion integrations/docker/images/chip-build-esp32/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN set -x \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y python \
&& cd /var \
&& git clone --progress -b release/v4.0 https://github.com/espressif/esp-idf.git \
&& git clone --progress -b v4.1 https://github.com/espressif/esp-idf.git \
&& cd esp-idf \
&& git submodule update --init --progress \
&& IDF_TOOLS_PATH=/var/.espressif ./install.sh \
Expand Down
8 changes: 4 additions & 4 deletions src/lib/support/CHIPArgParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,10 @@ bool ParseSubnetId(const char * str, uint16_t & subnetId)
*/
bool ParseHexString(const char * hexStr, uint32_t strLen, uint8_t * outBuf, uint32_t outBufSize, uint32_t & outDataLen)
{
bool isFirstNibble = true;
uint8_t firstNibbleVal;
const char * p = hexStr;
uint32_t dataLen = 0;
bool isFirstNibble = true;
uint8_t firstNibbleVal = 0;
const char * p = hexStr;
uint32_t dataLen = 0;

outDataLen = 0;

Expand Down
21 changes: 20 additions & 1 deletion src/platform/ESP32/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,26 @@ struct ChipDevicePlatformEvent final
{
union
{
system_event_t ESPSystemEvent;
struct
{
esp_event_base_t Base;
int32_t Id;
union
{
ip_event_got_ip_t IpGotIp;
ip_event_got_ip6_t IpGotIp6;
ip_event_ap_staipassigned_t IpApStaIpAssigned;
wifi_event_sta_scan_done_t WifiStaScanDone;
wifi_event_sta_connected_t WifiStaConnected;
wifi_event_sta_disconnected_t WifiStaDisconnected;
wifi_event_sta_authmode_change_t WifiStaAuthModeChange;
wifi_event_sta_wps_er_pin_t WifiStaWpsErPin;
wifi_event_sta_wps_fail_reason_t WifiStaWpsErFailed;
wifi_event_ap_staconnected_t WifiApStaConnected;
wifi_event_ap_stadisconnected_t WifiApStaDisconnected;
wifi_event_ap_probe_req_rx_t WifiApProbeReqRecved;
} Data;
} ESPSystemEvent;
};
};

Expand Down
143 changes: 75 additions & 68 deletions src/platform/ESP32/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <support/logging/CHIPLogging.h>

#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"

#include <lwip/dns.h>
Expand Down Expand Up @@ -454,60 +455,72 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
// Handle ESP system events...
if (event->Type == DeviceEventType::kESPSystemEvent)
{
switch (event->Platform.ESPSystemEvent.event_id)
if (event->Platform.ESPSystemEvent.Base == WIFI_EVENT)
{
case SYSTEM_EVENT_STA_START:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_STA_START");
DriveStationState();
break;
case SYSTEM_EVENT_STA_CONNECTED:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_STA_CONNECTED");
if (mWiFiStationState == kWiFiStationState_Connecting)
switch (event->Platform.ESPSystemEvent.Id)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded);
case WIFI_EVENT_STA_START:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_START");
DriveStationState();
break;
case WIFI_EVENT_STA_CONNECTED:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_CONNECTED");
if (mWiFiStationState == kWiFiStationState_Connecting)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded);
}
DriveStationState();
break;
case WIFI_EVENT_STA_DISCONNECTED:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_DISCONNECTED");
if (mWiFiStationState == kWiFiStationState_Connecting)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
}
DriveStationState();
break;
case WIFI_EVENT_STA_STOP:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_STOP");
DriveStationState();
break;
case WIFI_EVENT_AP_START:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_START");
ChangeWiFiAPState(kWiFiAPState_Active);
DriveAPState();
break;
case WIFI_EVENT_AP_STOP:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_STOP");
ChangeWiFiAPState(kWiFiAPState_NotActive);
DriveAPState();
break;
case WIFI_EVENT_AP_STACONNECTED:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_STACONNECTED");
MaintainOnDemandWiFiAP();
break;
default:
break;
}
DriveStationState();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_STA_DISCONNECTED");
if (mWiFiStationState == kWiFiStationState_Connecting)
}

if (event->Platform.ESPSystemEvent.Base == IP_EVENT)
{
switch (event->Platform.ESPSystemEvent.Id)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
case IP_EVENT_STA_GOT_IP:
ChipLogProgress(DeviceLayer, "IP_EVENT_STA_GOT_IP");
OnStationIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp);
break;
case IP_EVENT_STA_LOST_IP:
ChipLogProgress(DeviceLayer, "IP_EVENT_STA_LOST_IP");
OnStationIPv4AddressLost();
break;
case IP_EVENT_GOT_IP6:
ChipLogProgress(DeviceLayer, "IP_EVENT_GOT_IP6");
OnIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6);
break;
default:
break;
}
DriveStationState();
break;
case SYSTEM_EVENT_STA_STOP:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_STA_STOP");
DriveStationState();
break;
case SYSTEM_EVENT_STA_GOT_IP:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_STA_GOT_IP");
OnStationIPv4AddressAvailable(event->Platform.ESPSystemEvent.event_info.got_ip);
break;
case SYSTEM_EVENT_STA_LOST_IP:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_STA_LOST_IP");
OnStationIPv4AddressLost();
break;
case SYSTEM_EVENT_GOT_IP6:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_GOT_IP6");
OnIPv6AddressAvailable(event->Platform.ESPSystemEvent.event_info.got_ip6);
break;
case SYSTEM_EVENT_AP_START:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_AP_START");
ChangeWiFiAPState(kWiFiAPState_Active);
DriveAPState();
break;
case SYSTEM_EVENT_AP_STOP:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_AP_STOP");
ChangeWiFiAPState(kWiFiAPState_NotActive);
DriveAPState();
break;
case SYSTEM_EVENT_AP_STACONNECTED:
ChipLogProgress(DeviceLayer, "SYSTEM_EVENT_AP_STACONNECTED");
MaintainOnDemandWiFiAP();
break;
default:
break;
}
}
}
Expand Down Expand Up @@ -653,10 +666,10 @@ void ConnectivityManagerImpl::OnStationConnected()
CHIP_ERROR err;

// Assign an IPv6 link local address to the station interface.
err = tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
err = esp_netif_create_ip6_linklocal(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
if (err != ESP_OK)
{
ChipLogError(DeviceLayer, "tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA) failed: %s", chip::ErrorStr(err));
ChipLogError(DeviceLayer, "esp_netif_create_ip6_linklocal() failed for WIFI_STA_DEF interface: %s", chip::ErrorStr(err));
}

// TODO Invoke WARM to perform actions that occur when the WiFi station interface comes up.
Expand Down Expand Up @@ -818,13 +831,13 @@ void ConnectivityManagerImpl::DriveAPState()

// If AP is active, but the interface doesn't have an IPv6 link-local
// address, assign one now.
if (mWiFiAPState == kWiFiAPState_Active && Internal::ESP32Utils::IsInterfaceUp(TCPIP_ADAPTER_IF_AP) &&
!Internal::ESP32Utils::HasIPv6LinkLocalAddress(TCPIP_ADAPTER_IF_AP))
if (mWiFiAPState == kWiFiAPState_Active && Internal::ESP32Utils::IsInterfaceUp("WIFI_AP_DEF") &&
!Internal::ESP32Utils::HasIPv6LinkLocalAddress("WIFI_AP_DEF"))
{
err = tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP);
err = esp_netif_create_ip6_linklocal(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
if (err != ESP_OK)
{
ChipLogError(DeviceLayer, "tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_AP) failed: %s", chip::ErrorStr(err));
ChipLogError(DeviceLayer, "esp_netif_create_ip6_linklocal() failed for WIFI_AP_DEF interface: %s", chip::ErrorStr(err));
}
SuccessOrExit(err);
}
Expand Down Expand Up @@ -953,16 +966,13 @@ void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
}
}

void ConnectivityManagerImpl::OnStationIPv4AddressAvailable(const system_event_sta_got_ip_t & got_ip)
void ConnectivityManagerImpl::OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip)
{
#if CHIP_PROGRESS_LOGGING
{
char ipAddrStr[INET_ADDRSTRLEN], netMaskStr[INET_ADDRSTRLEN], gatewayStr[INET_ADDRSTRLEN];
IPAddress::FromIPv4(got_ip.ip_info.ip).ToString(ipAddrStr, sizeof(ipAddrStr));
IPAddress::FromIPv4(got_ip.ip_info.netmask).ToString(netMaskStr, sizeof(netMaskStr));
IPAddress::FromIPv4(got_ip.ip_info.gw).ToString(gatewayStr, sizeof(gatewayStr));
ChipLogProgress(DeviceLayer, "IPv4 address %s on WiFi station interface: %s/%s gateway %s",
(got_ip.ip_changed) ? "changed" : "ready", ipAddrStr, netMaskStr, gatewayStr);
ChipLogProgress(DeviceLayer, "IPv4 address %s on WiFi station interface: " IPSTR "/" IPSTR " gateway " IPSTR,
(got_ip.ip_changed) ? "changed" : "ready", IP2STR(&got_ip.ip_info.ip), IP2STR(&got_ip.ip_info.netmask),
IP2STR(&got_ip.ip_info.gw));
}
#endif // CHIP_PROGRESS_LOGGING

Expand All @@ -980,15 +990,12 @@ void ConnectivityManagerImpl::OnStationIPv4AddressLost(void)
UpdateInternetConnectivityState();
}

void ConnectivityManagerImpl::OnIPv6AddressAvailable(const system_event_got_ip6_t & got_ip)
void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip)
{
#if CHIP_PROGRESS_LOGGING
{
IPAddress ipAddr = IPAddress::FromIPv6(got_ip.ip6_info.ip);
char ipAddrStr[INET6_ADDRSTRLEN];
ipAddr.ToString(ipAddrStr, sizeof(ipAddrStr));
ChipLogProgress(DeviceLayer, "IPv6 addr available. Ready on %s interface: %s",
Internal::ESP32Utils::InterfaceIdToName(got_ip.if_index), ipAddrStr);
ChipLogProgress(DeviceLayer, "IPv6 addr available. Ready on %s interface: " IPV6STR, esp_netif_get_ifkey(got_ip.esp_netif),
IPV62STR(got_ip.ip6_info.ip));
}
#endif // CHIP_PROGRESS_LOGGING

Expand Down
4 changes: 2 additions & 2 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
static void DriveAPState(::chip::System::Layer * aLayer, void * aAppState, ::chip::System::Error aError);

void UpdateInternetConnectivityState(void);
void OnStationIPv4AddressAvailable(const system_event_sta_got_ip_t & got_ip);
void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip);
void OnStationIPv4AddressLost(void);
void OnIPv6AddressAvailable(const system_event_got_ip6_t & got_ip);
void OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip);

static const char * WiFiStationStateToStr(WiFiStationState state);
static const char * WiFiAPStateToStr(WiFiAPState state);
Expand Down
Loading

0 comments on commit 683ec7e

Please sign in to comment.