Skip to content

Commit

Permalink
Restructure CHIP_ERROR numeric values (#8179)
Browse files Browse the repository at this point in the history
* Restructure CHIP_ERROR numeric values

#### Problem

Large constants hurt code density on small processors, and `CHIP_ERROR`
constants are very common.

#### Summary of Changes

The internal details of `CHIP_ERROR` numbers change so that commonly
used constants are small. The new representation uses bit fields managed
by a helper class, `::chip::ChipError`.

Several examples have been using `CHIP_CONFIG_CORE_ERROR_MAX` as a
single application-level error code. Since this no longer exists, and
the new representation makes it straightforward, a portion of the
`CHIP_ERROR` space is now explicitly available for SDK users.

#### Testing

No changes to functionality. Manual sanity test with chip-tool and
all-clusters-app.

* fix examples/platform/esp32/shell_extension/heap_trace.cpp

* Darwin typedef

* review

* zap regen
  • Loading branch information
kpschoedel authored and pull[bot] committed Jul 15, 2021
1 parent 280c819 commit 1319193
Show file tree
Hide file tree
Showing 68 changed files with 576 additions and 784 deletions.
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/tests/TestCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void TestCommand::OnDeviceConnectedFn(void * context, chip::Controller::Device *

void TestCommand::OnDeviceConnectionFailureFn(void * context, NodeId deviceId, CHIP_ERROR error)
{
ChipLogError(chipTool, "Failed in connecting to the device %" PRIu64 ". Error %d", deviceId, error);
ChipLogError(chipTool, "Failed in connecting to the device %" PRIu64 ". Error %" CHIP_ERROR_FORMAT, deviceId, error);
auto * command = static_cast<TestCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "Test command context is null"));
command->SetCommandExitStatus(error);
Expand Down
12 changes: 10 additions & 2 deletions examples/lighting-app/efr32/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@
#include <ble/BLEEndPoint.h>
#include <platform/CHIPDeviceLayer.h>

// Application-defined error codes in the CHIP_ERROR space.
#define APP_ERROR_EVENT_QUEUE_FAILED CHIP_APPLICATION_ERROR(0x01)
#define APP_ERROR_CREATE_TASK_FAILED CHIP_APPLICATION_ERROR(0x02)
#define APP_ERROR_UNHANDLED_EVENT CHIP_APPLICATION_ERROR(0x03)
#define APP_ERROR_CREATE_TIMER_FAILED CHIP_APPLICATION_ERROR(0x04)
#define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05)
#define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06)

class AppTask
{

public:
int StartAppTask();
CHIP_ERROR StartAppTask();
static void AppTaskMain(void * pvParameter);

void PostLightActionRequest(int32_t aActor, LightingManager::Action_t aAction);
Expand All @@ -45,7 +53,7 @@ class AppTask
private:
friend AppTask & GetAppTask(void);

int Init();
CHIP_ERROR Init();

static void ActionInitiated(LightingManager::Action_t aAction, int32_t aActor);
static void ActionCompleted(LightingManager::Action_t aAction);
Expand Down
2 changes: 1 addition & 1 deletion examples/lighting-app/efr32/include/Rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace rpc {

class LightingService;

int Init();
void Init();
void RunRpcService(void *);

} // namespace rpc
Expand Down
22 changes: 8 additions & 14 deletions examples/lighting-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,21 @@ using namespace ::chip::DeviceLayer;

AppTask AppTask::sAppTask;

int AppTask::StartAppTask()
CHIP_ERROR AppTask::StartAppTask()
{
int err = CHIP_CONFIG_CORE_ERROR_MAX;

sAppEventQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
if (sAppEventQueue == NULL)
{
EFR32_LOG("Failed to allocate app event queue");
appError(err);
appError(APP_ERROR_EVENT_QUEUE_FAILED);
}

// Start App task.
sAppTaskHandle = xTaskCreateStatic(AppTaskMain, APP_TASK_NAME, ArraySize(appStack), NULL, 1, appStack, &appTaskStruct);
if (sAppTaskHandle != NULL)
{
err = CHIP_NO_ERROR;
}
return err;
return (sAppTaskHandle == nullptr) ? APP_ERROR_CREATE_TASK_FAILED : CHIP_NO_ERROR;
}

int AppTask::Init()
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -254,7 +248,7 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)
bool initiated = false;
LightingManager::Action_t action;
int32_t actor;
int err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;

if (aEvent->Type == AppEvent::kEventType_Light)
{
Expand All @@ -275,7 +269,7 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)
}
else
{
err = CHIP_CONFIG_CORE_ERROR_MAX;
err = APP_ERROR_UNHANDLED_EVENT;
}

if (err == CHIP_NO_ERROR)
Expand Down Expand Up @@ -419,7 +413,7 @@ void AppTask::CancelTimer()
if (xTimerStop(sFunctionTimer, 0) == pdFAIL)
{
EFR32_LOG("app timer stop() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_STOP_TIMER_FAILED);
}

mFunctionTimerActive = false;
Expand All @@ -439,7 +433,7 @@ void AppTask::StartTimer(uint32_t aTimeoutInMs)
if (xTimerChangePeriod(sFunctionTimer, aTimeoutInMs / portTICK_PERIOD_MS, 100) != pdPASS)
{
EFR32_LOG("app timer start() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_START_TIMER_FAILED);
}

mFunctionTimerActive = true;
Expand Down
6 changes: 3 additions & 3 deletions examples/lighting-app/efr32/src/LightingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int LightingManager::Init()
if (sLightTimer == NULL)
{
EFR32_LOG("sLightTimer timer create failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_CREATE_TIMER_FAILED);
}

mState = kState_OffCompleted;
Expand Down Expand Up @@ -135,7 +135,7 @@ void LightingManager::StartTimer(uint32_t aTimeoutMs)
if (xTimerChangePeriod(sLightTimer, (aTimeoutMs / portTICK_PERIOD_MS), 100) != pdPASS)
{
EFR32_LOG("sLightTimer timer start() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_START_TIMER_FAILED);
}
}

Expand All @@ -144,7 +144,7 @@ void LightingManager::CancelTimer(void)
if (xTimerStop(sLightTimer, 0) == pdFAIL)
{
EFR32_LOG("sLightTimer stop() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_STOP_TIMER_FAILED);
}
}

Expand Down
4 changes: 1 addition & 3 deletions examples/lighting-app/efr32/src/Rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,13 @@ void RunRpcService(void *)
Start(RegisterServices, &logger_mutex);
}

int Init()
void Init()
{
int err = CHIP_CONFIG_CORE_ERROR_MAX;
pw_sys_io_Init();

// Start App task.
sRpcTaskHandle = xTaskCreateStatic(RunRpcService, "RPC_TASK", ArraySize(sRpcTaskStack), nullptr, RPC_TASK_PRIORITY,
sRpcTaskStack, &sRpcTaskBuffer);
return err;
}

} // namespace rpc
Expand Down
4 changes: 1 addition & 3 deletions examples/lighting-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ extern "C" void vApplicationIdleHook(void)
// ================================================================================
int main(void)
{
int ret = CHIP_CONFIG_CORE_ERROR_MAX;

init_efrPlatform();
mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);

Expand All @@ -125,7 +123,7 @@ int main(void)
chip::Platform::MemoryInit();
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();

ret = PlatformMgr().InitChipStack();
CHIP_ERROR ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
{
EFR32_LOG("PlatformMgr().InitChipStack() failed");
Expand Down
5 changes: 3 additions & 2 deletions examples/lighting-app/k32w/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ int AppTask::StartAppTask()
sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent));
if (sAppEventQueue == NULL)
{
err = CHIP_CONFIG_CORE_ERROR_MAX;
err = APP_ERROR_EVENT_QUEUE_FAILED;
K32W_LOG("Failed to allocate app event queue");
assert(err == CHIP_NO_ERROR);
}
Expand Down Expand Up @@ -113,6 +113,7 @@ int AppTask::Init()
);
if (sFunctionTimer == NULL)
{
err = APP_ERROR_CREATE_TIMER_FAILED;
K32W_LOG("app_timer_create() failed");
assert(err == CHIP_NO_ERROR);
}
Expand Down Expand Up @@ -408,7 +409,7 @@ void AppTask::LightActionEventHandler(AppEvent * aEvent)
}
else
{
err = CHIP_CONFIG_CORE_ERROR_MAX;
err = APP_ERROR_UNHANDLED_EVENT;
}

if (err == CHIP_NO_ERROR)
Expand Down
8 changes: 8 additions & 0 deletions examples/lighting-app/k32w/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
#include "FreeRTOS.h"
#include "timers.h"

// Application-defined error codes in the CHIP_ERROR space.
#define APP_ERROR_EVENT_QUEUE_FAILED CHIP_APPLICATION_ERROR(0x01)
#define APP_ERROR_CREATE_TASK_FAILED CHIP_APPLICATION_ERROR(0x02)
#define APP_ERROR_UNHANDLED_EVENT CHIP_APPLICATION_ERROR(0x03)
#define APP_ERROR_CREATE_TIMER_FAILED CHIP_APPLICATION_ERROR(0x04)
#define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05)
#define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06)

class AppTask
{
public:
Expand Down
4 changes: 1 addition & 3 deletions examples/lighting-app/k32w/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ uint8_t __attribute__((section(".heap"))) ucHeap[0xF000];

extern "C" void main_task(void const * argument)
{
CHIP_ERROR ret = CHIP_CONFIG_CORE_ERROR_MAX;

/* Call C++ constructors */
InitFunc * pFunc = &__init_array_start;
for (; pFunc < &__init_array_end; ++pFunc)
Expand All @@ -78,7 +76,7 @@ extern "C" void main_task(void const * argument)
// Init Chip memory management before the stack
chip::Platform::MemoryInit();

ret = PlatformMgr().InitChipStack();
CHIP_ERROR ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
{
K32W_LOG("Error during PlatformMgr().InitWeaveStack()");
Expand Down
12 changes: 10 additions & 2 deletions examples/lock-app/efr32/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@
#include <ble/BLEEndPoint.h>
#include <platform/CHIPDeviceLayer.h>

// Application-defined error codes in the CHIP_ERROR space.
#define APP_ERROR_EVENT_QUEUE_FAILED CHIP_APPLICATION_ERROR(0x01)
#define APP_ERROR_CREATE_TASK_FAILED CHIP_APPLICATION_ERROR(0x02)
#define APP_ERROR_UNHANDLED_EVENT CHIP_APPLICATION_ERROR(0x03)
#define APP_ERROR_CREATE_TIMER_FAILED CHIP_APPLICATION_ERROR(0x04)
#define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05)
#define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06)

class AppTask
{

public:
int StartAppTask();
CHIP_ERROR StartAppTask();
static void AppTaskMain(void * pvParameter);

void PostLockActionRequest(int32_t aActor, BoltLockManager::Action_t aAction);
Expand All @@ -45,7 +53,7 @@ class AppTask
private:
friend AppTask & GetAppTask(void);

int Init();
CHIP_ERROR Init();

static void ActionInitiated(BoltLockManager::Action_t aAction, int32_t aActor);
static void ActionCompleted(BoltLockManager::Action_t aAction);
Expand Down
21 changes: 7 additions & 14 deletions examples/lock-app/efr32/src/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,21 @@ using namespace ::chip::DeviceLayer;

AppTask AppTask::sAppTask;

int AppTask::StartAppTask()
CHIP_ERROR AppTask::StartAppTask()
{
int err = CHIP_CONFIG_CORE_ERROR_MAX;

sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent));
if (sAppEventQueue == NULL)
{
EFR32_LOG("Failed to allocate app event queue");
appError(err);
appError(APP_ERROR_EVENT_QUEUE_FAILED);
}

// Start App task.
sAppTaskHandle = xTaskCreateStatic(AppTaskMain, APP_TASK_NAME, ArraySize(appStack), NULL, 1, appStack, &appTaskStruct);
if (sAppTaskHandle != NULL)
{
err = CHIP_NO_ERROR;
}

return err;
return (sAppTaskHandle == nullptr) ? APP_ERROR_CREATE_TASK_FAILED : CHIP_NO_ERROR;
}

int AppTask::Init()
CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;

Expand Down Expand Up @@ -271,7 +264,7 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent)
}
else
{
err = CHIP_CONFIG_CORE_ERROR_MAX;
err = APP_ERROR_UNHANDLED_EVENT;
}

if (err == CHIP_NO_ERROR)
Expand Down Expand Up @@ -415,7 +408,7 @@ void AppTask::CancelTimer()
if (xTimerStop(sFunctionTimer, 0) == pdFAIL)
{
EFR32_LOG("app timer stop() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_STOP_TIMER_FAILED);
}

mFunctionTimerActive = false;
Expand All @@ -435,7 +428,7 @@ void AppTask::StartTimer(uint32_t aTimeoutInMs)
if (xTimerChangePeriod(sFunctionTimer, aTimeoutInMs / portTICK_PERIOD_MS, 100) != pdPASS)
{
EFR32_LOG("app timer start() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_START_TIMER_FAILED);
}

mFunctionTimerActive = true;
Expand Down
6 changes: 3 additions & 3 deletions examples/lock-app/efr32/src/BoltLockManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int BoltLockManager::Init()
if (sLockTimer == NULL)
{
EFR32_LOG("sLockTimer timer create failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_CREATE_TIMER_FAILED);
}

mState = kState_LockingCompleted;
Expand Down Expand Up @@ -135,7 +135,7 @@ void BoltLockManager::StartTimer(uint32_t aTimeoutMs)
if (xTimerChangePeriod(sLockTimer, (aTimeoutMs / portTICK_PERIOD_MS), 100) != pdPASS)
{
EFR32_LOG("sLockTimer timer start() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_START_TIMER_FAILED);
}
}

Expand All @@ -144,7 +144,7 @@ void BoltLockManager::CancelTimer(void)
if (xTimerStop(sLockTimer, 0) == pdFAIL)
{
EFR32_LOG("Lock timer timer stop() failed");
appError(CHIP_CONFIG_CORE_ERROR_MAX);
appError(APP_ERROR_STOP_TIMER_FAILED);
}
}

Expand Down
4 changes: 1 addition & 3 deletions examples/lock-app/efr32/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ extern "C" void vApplicationIdleHook(void)
// ================================================================================
int main(void)
{
int ret = CHIP_CONFIG_CORE_ERROR_MAX;

init_efrPlatform();
mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);

Expand All @@ -109,7 +107,7 @@ int main(void)
chip::Platform::MemoryInit();
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().Init();

ret = PlatformMgr().InitChipStack();
CHIP_ERROR ret = PlatformMgr().InitChipStack();
if (ret != CHIP_NO_ERROR)
{
EFR32_LOG("PlatformMgr().InitChipStack() failed");
Expand Down
Loading

0 comments on commit 1319193

Please sign in to comment.