Skip to content

Commit

Permalink
Make CHIP_ERROR a class type on ESP32.
Browse files Browse the repository at this point in the history
#### Problem

Having CHIP_ERROR be a class type would provide (a) type safety,
and (b) the ability to trace the source of errors (issue project-chip#8340).

#### Change overview

- Enable `CHIP_CONFIG_ERROR_CLASS` on ESP32.

#### Testing

Passes esp32_qemu_tests.sh
  • Loading branch information
kpschoedel committed Jul 19, 2021
1 parent ff2a1e5 commit 240f527
Show file tree
Hide file tree
Showing 23 changed files with 408 additions and 605 deletions.
13 changes: 5 additions & 8 deletions examples/all-clusters-app/esp32/main/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ extern const char * TAG;

esp_err_t Button::Init(gpio_num_t gpioNum, uint16_t debouncePeriod)
{
esp_err_t err;

mGPIONum = gpioNum;
mDebouncePeriod = debouncePeriod / portTICK_PERIOD_MS;
mState = false;
mLastPolledState = false;

err = gpio_set_direction(gpioNum, GPIO_MODE_INPUT);
SuccessOrExit(err);

Poll();

exit:
esp_err_t err = gpio_set_direction(gpioNum, GPIO_MODE_INPUT);
if (err == ESP_OK)
{
Poll();
}
return err;
}

Expand Down
31 changes: 12 additions & 19 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ std::string createSetupPayload()

if (err != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Couldn't get payload string %d", err);
ESP_LOGE(TAG, "Couldn't get payload string %" CHIP_ERROR_FORMAT, ChipError::FormatError(err));
}
return result;
};
Expand Down Expand Up @@ -586,18 +586,11 @@ extern "C" void app_main()
ESP_LOGI(TAG, "%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

CHIP_ERROR err; // A quick note about errors: CHIP adopts the error type and numbering
// convention of the environment into which it is ported. Thus esp_err_t
// and CHIP_ERROR are in fact the same type, and both ESP-IDF errors
// and CHIO-specific errors can be stored in the same value without
// ambiguity. For convenience, ESP_OK and CHIP_NO_ERROR are mapped
// to the same value.

// Initialize the ESP NVS layer.
err = nvs_flash_init();
if (err != CHIP_NO_ERROR)
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}

Expand All @@ -607,10 +600,10 @@ extern "C" void app_main()

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();

err = deviceMgr.Init(&EchoCallbacks);
if (err != CHIP_NO_ERROR)
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
if (error != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error));
return;
}

Expand All @@ -635,8 +628,8 @@ extern "C" void app_main()

{
std::vector<char> qrCode(3 * qrCodeText.size() + 1);
err = EncodeQRCodeToUrl(qrCodeText.c_str(), qrCodeText.size(), qrCode.data(), qrCode.max_size());
if (err == CHIP_NO_ERROR)
error = EncodeQRCodeToUrl(qrCodeText.c_str(), qrCodeText.size(), qrCode.data(), qrCode.max_size());
if (error == CHIP_NO_ERROR)
{
ESP_LOGI(TAG, "Copy/paste the below URL in a browser to see the QR CODE:\n\t%s?data=%s", QRCODE_BASE_URL,
qrCode.data());
Expand All @@ -648,7 +641,7 @@ extern "C" void app_main()
err = InitDisplay();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "InitDisplay() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "InitDisplay() failed: %s", esp_err_to_name(err));
return;
}

Expand All @@ -660,9 +653,9 @@ extern "C" void app_main()
for (int i = 0; i < buttons.size(); ++i)
{
err = buttons[i].Init(button_gpios[i], 50);
if (err != CHIP_NO_ERROR)
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Button.Init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "Button.Init() failed: %s", esp_err_to_name(err));
return;
}
}
Expand Down
5 changes: 2 additions & 3 deletions examples/lock-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,13 @@ CHIP_ERROR AppTask::Init()

void AppTask::AppTaskMain(void * pvParameter)
{
int err;
AppEvent event;
uint64_t mLastChangeTimeUS = 0;

err = sAppTask.Init();
CHIP_ERROR err = sAppTask.Init();
if (err != CHIP_NO_ERROR)
{
ESP_LOGI(TAG, "AppTask.Init() failed due to %d", err);
ESP_LOGI(TAG, "AppTask.Init() failed due to %" CHIP_ERROR_FORMAT, chip::ChipError::FormatError(err));
return;
}

Expand Down
8 changes: 1 addition & 7 deletions examples/lock-app/esp32/main/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,12 @@

esp_err_t Button::Init(gpio_num_t gpioNum, uint16_t debouncePeriod)
{
esp_err_t err;

mGPIONum = gpioNum;
mDebouncePeriod = debouncePeriod / portTICK_PERIOD_MS;
mState = false;
mLastPolledState = false;

err = gpio_set_direction(gpioNum, GPIO_MODE_INPUT);
SuccessOrExit(err);

exit:
return err;
return gpio_set_direction(gpioNum, GPIO_MODE_INPUT);
}

bool Button::Poll()
Expand Down
19 changes: 9 additions & 10 deletions examples/lock-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ static DeviceCallbacks EchoCallbacks;

extern "C" void app_main()
{
int err = 0;
// Initialize the ESP NVS layer.
err = nvs_flash_init();
if (err != CHIP_NO_ERROR)
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}

Expand All @@ -75,19 +74,19 @@ extern "C" void app_main()

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();

err = deviceMgr.Init(&EchoCallbacks);
if (err != CHIP_NO_ERROR)
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
if (error != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error));
return;
}

InitServer();

ESP_LOGI(TAG, "------------------------Starting App Task---------------------------");
err = GetAppTask().StartAppTask();
if (err != CHIP_NO_ERROR)
error = GetAppTask().StartAppTask();
if (error != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "GetAppTask().Init() failed");
ESP_LOGE(TAG, "GetAppTask().Init() failed: %s", ErrorStr(error));
}
}
6 changes: 3 additions & 3 deletions examples/persistent-storage/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const char * TAG = "persistent-storage";

extern "C" void app_main()
{
auto err = nvs_flash_init();
if (err != CHIP_NO_ERROR)
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", ::chip::ErrorStr(err));
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}
ESP_LOGI(TAG, "=============================================");
Expand Down
19 changes: 6 additions & 13 deletions examples/temperature-measurement-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,20 @@ extern "C" void app_main()
ESP_LOGI(TAG, "%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

CHIP_ERROR err; // A quick note about errors: CHIP adopts the error type and numbering
// convention of the environment into which it is ported. Thus esp_err_t
// and CHIP_ERROR are in fact the same type, and both ESP-IDF errors
// and CHIO-specific errors can be stored in the same value without
// ambiguity. For convenience, ESP_OK and CHIP_NO_ERROR are mapped
// to the same value.

// Initialize the ESP NVS layer.
err = nvs_flash_init();
if (err != CHIP_NO_ERROR)
esp_err_t err = nvs_flash_init();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "nvs_flash_init() failed: %s", esp_err_to_name(err));
return;
}

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();

err = deviceMgr.Init(&EchoCallbacks);
if (err != CHIP_NO_ERROR)
CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
if (error != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(err));
ESP_LOGE(TAG, "device.Init() failed: %s", ErrorStr(error));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/shell/MainLoopESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ProcessShellLine(intptr_t context)
esp_console_run(shellArgs->GetLine(), &ret);
if (ret)
{
printf("Error: %s\r\n", chip::ErrorStr(ret));
printf("Error: %d\r\n", ret);
}
else
{
Expand Down
6 changes: 4 additions & 2 deletions src/lib/shell/streamer_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ namespace Shell {

static int chip_command_handler(int argc, char ** argv)
{
CHIP_ERROR err;
if (argc > 0)
{
return Engine::Root().ExecCommand(argc - 1, argv + 1);
err = Engine::Root().ExecCommand(argc - 1, argv + 1);
}
else
{
return CHIP_ERROR_INVALID_ARGUMENT;
err = CHIP_ERROR_INVALID_ARGUMENT;
}
return static_cast<int>(chip::ChipError::AsInteger(err));
}

int streamer_esp32_init(streamer_t * streamer)
Expand Down
2 changes: 2 additions & 0 deletions src/platform/ESP32/CHIPPlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

#define ChipDie() abort()

#define CHIP_CONFIG_ERROR_CLASS 1

// ==================== Security Adaptations ====================

#define CHIP_CONFIG_USE_OPENSSL_ECC 0
Expand Down
6 changes: 3 additions & 3 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg)
}

// Restore WiFi persistent settings to default values.
err = esp_wifi_restore();
if (err != ESP_OK)
esp_err_t error = esp_wifi_restore();
if (error != ESP_OK)
{
ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", chip::ErrorStr(err));
ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", esp_err_to_name(error));
}

// Restart the system.
Expand Down
Loading

0 comments on commit 240f527

Please sign in to comment.