Skip to content

Commit

Permalink
ESP32: Address -Wundef build errors and enable it for platform (#29728)
Browse files Browse the repository at this point in the history
* [ESP32] Fix build errors when -Wundef compiler flag is enabled

* Enable -Wundef for ESP32

* Defining CHIP_SYSTEM_CONFIG_LWIP_PBUF_FROM_CUSTOM_POOL

Some platforms do not use the lwip from third_party/lwip and may not
have LWIP_PBUF_FROM_CUSTOM_POOLS defined in lwip/opt.h. When building
apps with -Wundef cflag enabled, it may fail to find LWIP_PBUF_FROM_CUSTOM_POOLS
hence this option. This defaults to LWIP_PBUF_FROM_CUSTOM_POOLS if
defined, otherwise 0.

* remove includes of <lwip/opt.h> and use <system/SystemConfig.h>
  • Loading branch information
shubhamdp authored May 16, 2024
1 parent 5665be8 commit f980f2c
Show file tree
Hide file tree
Showing 24 changed files with 230 additions and 157 deletions.
16 changes: 10 additions & 6 deletions src/lib/shell/streamer_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
#include <lib/shell/Engine.h>
#include <lib/shell/streamer.h>

#include "sdkconfig.h"

#include "esp_console.h"
#include "esp_vfs_dev.h"
#include "linenoise/linenoise.h"
#include <fcntl.h>
#include <lib/core/CHIPError.h>
#include <stdio.h>
#include <string.h>
#if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM

#if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
#include "driver/uart.h"
#endif
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG

#ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
#include "driver/usb_serial_jtag.h"
#include "esp_vfs_usb_serial_jtag.h"
#endif
Expand All @@ -55,7 +59,7 @@ int streamer_esp32_init(streamer_t * streamer)
fflush(stdout);
fsync(fileno(stdout));
setvbuf(stdin, NULL, _IONBF, 0);
#if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
#if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
esp_vfs_dev_uart_port_set_rx_line_endings((uart_port_t) CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
esp_vfs_dev_uart_port_set_tx_line_endings((uart_port_t) CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
if (!uart_is_driver_installed((uart_port_t) CONFIG_ESP_CONSOLE_UART_NUM))
Expand All @@ -79,7 +83,7 @@ int streamer_esp32_init(streamer_t * streamer)
esp_vfs_dev_uart_use_driver(0);
#endif // CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM

#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
#ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
esp_vfs_dev_usb_serial_jtag_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
esp_vfs_dev_usb_serial_jtag_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);

Expand Down Expand Up @@ -121,10 +125,10 @@ ssize_t streamer_esp32_read(streamer_t * streamer, char * buf, size_t len)

ssize_t streamer_esp32_write(streamer_t * streamer, const char * buf, size_t len)
{
#if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM
#if defined(CONFIG_ESP_CONSOLE_UART_DEFAULT) || defined(CONFIG_ESP_CONSOLE_UART_CUSTOM)
return uart_write_bytes((uart_port_t) CONFIG_ESP_CONSOLE_UART_NUM, buf, len);
#endif
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
#ifdef CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
return usb_serial_jtag_write_bytes(buf, len, 0);
#endif
}
Expand Down
8 changes: 2 additions & 6 deletions src/messaging/ReliableMessageProtocolConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
#include <system/SystemClock.h>
#include <system/SystemConfig.h>

#if CHIP_SYSTEM_CONFIG_USE_LWIP
#include <lwip/opt.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

namespace chip {

/**
Expand Down Expand Up @@ -129,14 +125,14 @@ namespace chip {
#ifndef CHIP_CONFIG_RMP_RETRANS_TABLE_SIZE
#if CHIP_SYSTEM_CONFIG_USE_LWIP

#if !LWIP_PBUF_FROM_CUSTOM_POOLS && PBUF_POOL_SIZE != 0
#if !CHIP_SYSTEM_CONFIG_LWIP_PBUF_FROM_CUSTOM_POOL && PBUF_POOL_SIZE != 0
// Configure the table size to be less than the number of packet buffers to make sure
// that not all buffers are held by the retransmission entries, in which case the device
// is unable to receive an ACK and hence becomes unavailable until a message times out.
#define CHIP_CONFIG_RMP_RETRANS_TABLE_SIZE std::min(PBUF_POOL_SIZE - 1, CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS)
#else
#define CHIP_CONFIG_RMP_RETRANS_TABLE_SIZE CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS
#endif // !LWIP_PBUF_FROM_CUSTOM_POOLS && PBUF_POOL_SIZE != 0
#endif // !CHIP_SYSTEM_CONFIG_LWIP_PBUF_FROM_CUSTOM_POOL && PBUF_POOL_SIZE != 0

#else // CHIP_SYSTEM_CONFIG_USE_LWIP

Expand Down
81 changes: 40 additions & 41 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@

#include <lib/core/Optional.h>

#if CONFIG_BT_BLUEDROID_ENABLED
#ifdef CONFIG_BT_BLUEDROID_ENABLED

#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include "esp_gattc_api.h"
#endif
#include "esp_gatts_api.h"
#include <lib/core/CHIPCallback.h>
#elif CONFIG_BT_NIMBLE_ENABLED

#elif defined(CONFIG_BT_NIMBLE_ENABLED)

/* min max macros in NimBLE can cause build issues with generic min max
* functions defined in CHIP.*/
Expand All @@ -60,25 +61,23 @@ struct ble_gatt_char_context
void * arg;
};

#endif

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER && CONFIG_BT_NIMBLE_ENABLED
#include "nimble/blecent.h"
#endif
#endif // CONFIG_BT_BLUEDROID_ENABLED

#include "ble/Ble.h"
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include <ble/Ble.h>
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#include <platform/ESP32/ChipDeviceScanner.h>
#endif
#ifdef CONFIG_BT_NIMBLE_ENABLED
#include "nimble/blecent.h"
#endif // CONFIG_BT_NIMBLE_ENABLED
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER

#define MAX_SCAN_RSP_DATA_LEN 31

namespace chip {
namespace DeviceLayer {
namespace Internal {

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
enum class BleScanState : uint8_t
{
kNotScanning,
Expand Down Expand Up @@ -117,30 +116,30 @@ struct BLEScanConfig
void * mAppState = nullptr;
};

#endif
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
/**
* Concrete implementation of the BLEManager singleton object for the ESP32 platform.
*/
class BLEManagerImpl final : public BLEManager,
private Ble::BleLayer,
private Ble::BlePlatformDelegate,
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
private Ble::BleApplicationDelegate,
private Ble::BleConnectionDelegate,
private ChipDeviceScannerDelegate
#else
private Ble::BleApplicationDelegate
#endif
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
{
public:
uint8_t scanResponseBuffer[MAX_SCAN_RSP_DATA_LEN];
BLEManagerImpl() {}
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
CHIP_ERROR ConfigureBle(uint32_t aAdapterId, bool aIsCentral);
#if CONFIG_BT_BLUEDROID_ENABLED
#ifdef CONFIG_BT_BLUEDROID_ENABLED
static void gattc_profile_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t * param);
#endif
#endif
#endif // CONFIG_BT_BLUEDROID_ENABLED
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER

CHIP_ERROR ConfigureScanResponseData(ByteSpan data);
void ClearScanResponseData(void);
Expand All @@ -164,10 +163,10 @@ class BLEManagerImpl final : public BLEManager,
CHIP_ERROR _SetDeviceName(const char * deviceName);
uint16_t _NumConnections(void);
void _OnPlatformEvent(const ChipDeviceEvent * event);
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
void HandlePlatformSpecificBLEEvent(const ChipDeviceEvent * event);
CHIP_ERROR _SetCHIPoBLEServiceMode(CHIPoBLEServiceMode val);
#endif
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
::chip::Ble::BleLayer * _GetBleLayer(void);

// ===== Members that implement virtual methods on BlePlatformDelegate.
Expand All @@ -191,23 +190,23 @@ class BLEManagerImpl final : public BLEManager,

void NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) override;
// ===== Members that implement virtual methods on BleConnectionDelegate.
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER

void NewConnection(chip::Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & connDiscriminator) override;
void NewConnection(chip::Ble::BleLayer * bleLayer, void * appState, BLE_CONNECTION_OBJECT connObj) override{};
CHIP_ERROR CancelConnection() override;

// ===== Members that implement virtual methods on ChipDeviceScannerDelegate
#if CONFIG_BT_NIMBLE_ENABLED
#ifdef CONFIG_BT_NIMBLE_ENABLED
virtual void OnDeviceScanned(const struct ble_hs_adv_fields & fields, const ble_addr_t & addr,
const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#elif CONFIG_BT_BLUEDROID_ENABLED
#elif defined(CONFIG_BT_BLUEDROID_ENABLED)
virtual void OnDeviceScanned(esp_ble_addr_type_t & addr_type, esp_bd_addr_t & addr,
const chip::Ble::ChipBLEDeviceIdentificationInfo & info) override;
#endif
#endif // CONFIG_BT_NIMBLE_ENABLED

void OnScanComplete() override;
#endif
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
// ===== Members for internal use by the following friends.

friend BLEManager & BLEMgr(void);
Expand Down Expand Up @@ -240,12 +239,12 @@ class BLEManagerImpl final : public BLEManager,
kMaxDeviceNameLength = 16
};

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
BLEAdvConfig mBLEAdvConfig;
#endif
#if CONFIG_BT_NIMBLE_ENABLED
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_BT_NIMBLE_ENABLED
uint16_t mSubscribedConIds[kMaxConnections];
#endif
#endif // CONFIG_BT_NIMBLE_ENABLED

struct CHIPoBLEConState
{
Expand Down Expand Up @@ -278,11 +277,11 @@ class BLEManagerImpl final : public BLEManager,

CHIPoBLEConState mCons[kMaxConnections];
CHIPoBLEServiceMode mServiceMode;
#if CONFIG_BT_BLUEDROID_ENABLED
#ifdef CONFIG_BT_BLUEDROID_ENABLED
esp_gatt_if_t mAppIf;
#elif CONFIG_BT_NIMBLE_ENABLED
#elif defined(CONFIG_BT_NIMBLE_ENABLED)
uint16_t mNumGAPCons;
#endif
#endif // CONFIG_BT_BLUEDROID_ENABLED
uint16_t mServiceAttrHandle;
uint16_t mRXCharAttrHandle;
uint16_t mTXCharAttrHandle;
Expand All @@ -303,7 +302,7 @@ class BLEManagerImpl final : public BLEManager,
void CancelBleAdvTimeoutTimer(void);
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);

#if CONFIG_BT_BLUEDROID_ENABLED
#ifdef CONFIG_BT_BLUEDROID_ENABLED
void HandleGATTControlEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
void HandleGATTCommEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
void HandleRXCharWrite(esp_ble_gatts_cb_param_t * param);
Expand All @@ -314,7 +313,7 @@ class BLEManagerImpl final : public BLEManager,
void HandleDisconnect(esp_ble_gatts_cb_param_t * param);
CHIPoBLEConState * GetConnectionState(uint16_t conId, bool allocate = false);
bool ReleaseConnectionState(uint16_t conId);
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
CHIP_ERROR HandleGAPConnect(esp_ble_gattc_cb_param_t p_data);
CHIP_ERROR HandleGAPCentralConnect(esp_ble_gattc_cb_param_t p_data);

Expand All @@ -326,7 +325,7 @@ class BLEManagerImpl final : public BLEManager,
static void HandleGATTEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
static void HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t * param);

#elif CONFIG_BT_NIMBLE_ENABLED
#elif defined(CONFIG_BT_NIMBLE_ENABLED)
CHIP_ERROR DeinitBLE();
static void ClaimBLEMemory(System::Layer *, void *);

Expand Down Expand Up @@ -361,7 +360,7 @@ class BLEManagerImpl final : public BLEManager,
void HandleC3CharRead(struct ble_gatt_char_context * param);
#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */

#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static int btshell_on_mtu(uint16_t conn_handle, const struct ble_gatt_error * error, uint16_t mtu, void * arg);

bool SubOrUnsubChar(BLE_CONNECTION_OBJECT conId, const Ble::ChipBleUUID * svcId, const Ble::ChipBleUUID * charId,
Expand All @@ -370,9 +369,9 @@ class BLEManagerImpl final : public BLEManager,
static void OnGattDiscComplete(const struct peer * peer, int status, void * arg);
static void HandleConnectFailed(CHIP_ERROR error);
CHIP_ERROR HandleRXNotify(struct ble_gap_event * event);
#endif
#endif
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER
#endif // CONFIG_BT_NIMBLE_ENABLED
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static void CancelConnect(void);
static void HandleConnectTimeout(chip::System::Layer *, void * context);
void InitiateScan(BleScanState scanType);
Expand All @@ -382,7 +381,7 @@ class BLEManagerImpl final : public BLEManager,
void CleanScanConfig();
BLEScanConfig mBLEScanConfig;
bool mIsCentral;
#endif
#endif // CONFIG_ENABLE_ESP32_BLE_CONTROLLER

static void DriveBLEState(intptr_t arg);
};
Expand Down
5 changes: 4 additions & 1 deletion src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,8 @@ static_library("ESP32") {
]
}

cflags = [ "-Wconversion" ]
cflags = [
"-Wconversion",
"-Wundef",
]
}
Loading

0 comments on commit f980f2c

Please sign in to comment.