Bladeren bron

Restructure CHIP_ERROR numeric values (#8179)

* 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
Kevin Schoedel 4 jaren geleden
bovenliggende
commit
6ed99f4234
68 gewijzigde bestanden met toevoegingen van 458 en 675 verwijderingen
  1. 1 1
      examples/chip-tool/commands/tests/TestCommand.cpp
  2. 10 2
      examples/lighting-app/efr32/include/AppTask.h
  3. 1 1
      examples/lighting-app/efr32/include/Rpc.h
  4. 8 14
      examples/lighting-app/efr32/src/AppTask.cpp
  5. 3 3
      examples/lighting-app/efr32/src/LightingManager.cpp
  6. 1 3
      examples/lighting-app/efr32/src/Rpc.cpp
  7. 1 3
      examples/lighting-app/efr32/src/main.cpp
  8. 3 2
      examples/lighting-app/k32w/main/AppTask.cpp
  9. 8 0
      examples/lighting-app/k32w/main/include/AppTask.h
  10. 1 3
      examples/lighting-app/k32w/main/main.cpp
  11. 10 2
      examples/lock-app/efr32/include/AppTask.h
  12. 7 14
      examples/lock-app/efr32/src/AppTask.cpp
  13. 3 3
      examples/lock-app/efr32/src/BoltLockManager.cpp
  14. 1 3
      examples/lock-app/efr32/src/main.cpp
  15. 7 16
      examples/lock-app/esp32/main/AppTask.cpp
  16. 2 2
      examples/lock-app/esp32/main/BoltLockManager.cpp
  17. 10 2
      examples/lock-app/esp32/main/include/AppTask.h
  18. 3 1
      examples/lock-app/esp32/main/include/BoltLockManager.h
  19. 3 3
      examples/lock-app/qpg/src/BoltLockManager.cpp
  20. 9 9
      examples/platform/esp32/shell_extension/heap_trace.cpp
  21. 2 6
      examples/platform/qpg/app/main.cpp
  22. 1 2
      examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp
  23. 1 3
      examples/pump-app/cc13x2x7_26x2x7/main/main.cpp
  24. 1 2
      examples/pump-controller-app/cc13x2x7_26x2x7/main/AppTask.cpp
  25. 1 3
      examples/pump-controller-app/cc13x2x7_26x2x7/main/main.cpp
  26. 1 3
      examples/shell/efr32/src/main.cpp
  27. 1 3
      examples/shell/k32w/main/main.cpp
  28. 9 1
      examples/window-app/efr32/include/AppTask.h
  29. 3 9
      examples/window-app/efr32/src/AppTask.cpp
  30. 3 3
      examples/window-app/efr32/src/AppTimer.cpp
  31. 1 3
      examples/window-app/efr32/src/main.cpp
  32. 1 1
      src/ble/BleError.cpp
  33. 33 48
      src/ble/BleError.h
  34. 1 1
      src/darwin/Framework/CHIP/CHIPError.h
  35. 7 7
      src/include/platform/CHIPDeviceError.h
  36. 1 1
      src/inet/InetError.cpp
  37. 30 45
      src/inet/InetError.h
  38. 1 1
      src/inet/tests/TestInetEndPoint.cpp
  39. 1 1
      src/lib/asn1/ASN1Error.cpp
  40. 13 48
      src/lib/asn1/ASN1Error.h
  41. 0 49
      src/lib/core/CHIPConfig.h
  42. 1 1
      src/lib/core/CHIPError.cpp
  43. 223 122
      src/lib/core/CHIPError.h
  44. 0 4
      src/lib/support/tests/TestErrorStr.cpp
  45. 0 3
      src/platform/Darwin/CHIPPlatformConfig.h
  46. 1 1
      src/platform/EFR32/BLEManagerImpl.cpp
  47. 2 2
      src/platform/EFR32/CHIPDevicePlatformConfig.h
  48. 0 8
      src/platform/EFR32/CHIPPlatformConfig.h
  49. 1 1
      src/platform/EFR32/EFR32Config.cpp
  50. 0 3
      src/platform/EFR32/SystemPlatformConfig.h
  51. 1 1
      src/platform/ESP32/CHIPDevicePlatformConfig.h
  52. 0 8
      src/platform/ESP32/CHIPPlatformConfig.h
  53. 0 3
      src/platform/ESP32/SystemPlatformConfig.h
  54. 1 1
      src/platform/ESP32/bluedroid/BLEManagerImpl.cpp
  55. 1 1
      src/platform/ESP32/nimble/BLEManagerImpl.cpp
  56. 1 1
      src/platform/GeneralUtils.cpp
  57. 0 8
      src/platform/K32W/CHIPPlatformConfig.h
  58. 2 2
      src/platform/K32W/K32WConfig.cpp
  59. 0 3
      src/platform/K32W/SystemPlatformConfig.h
  60. 4 3
      src/platform/OpenThread/OpenThreadUtils.cpp
  61. 0 3
      src/platform/cc13x2_26x2/CHIPPlatformConfig.h
  62. 0 6
      src/platform/mbed/CHIPPlatformConfig.h
  63. 0 8
      src/platform/mbed/SystemPlatformConfig.h
  64. 0 8
      src/platform/qpg/CHIPPlatformConfig.h
  65. 0 3
      src/platform/qpg/SystemPlatformConfig.h
  66. 0 26
      src/system/SystemConfig.h
  67. 13 113
      src/system/SystemError.cpp
  68. 3 5
      src/system/SystemError.h

+ 1 - 1
examples/chip-tool/commands/tests/TestCommand.cpp

@@ -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);

+ 10 - 2
examples/lighting-app/efr32/include/AppTask.h

@@ -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);
@@ -45,7 +53,7 @@ public:
 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);

+ 1 - 1
examples/lighting-app/efr32/include/Rpc.h

@@ -23,7 +23,7 @@ namespace rpc {
 
 class LightingService;
 
-int Init();
+void Init();
 void RunRpcService(void *);
 
 } // namespace rpc

+ 8 - 14
examples/lighting-app/efr32/src/AppTask.cpp

@@ -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;
 
@@ -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)
     {
@@ -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)
@@ -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;
@@ -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;

+ 3 - 3
examples/lighting-app/efr32/src/LightingManager.cpp

@@ -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;
@@ -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);
     }
 }
 
@@ -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);
     }
 }
 

+ 1 - 3
examples/lighting-app/efr32/src/Rpc.cpp

@@ -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

+ 1 - 3
examples/lighting-app/efr32/src/main.cpp

@@ -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);
 
@@ -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");

+ 3 - 2
examples/lighting-app/k32w/main/AppTask.cpp

@@ -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);
     }
@@ -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);
     }
@@ -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)

+ 8 - 0
examples/lighting-app/k32w/main/include/AppTask.h

@@ -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:

+ 1 - 3
examples/lighting-app/k32w/main/main.cpp

@@ -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)
@@ -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()");

+ 10 - 2
examples/lock-app/efr32/include/AppTask.h

@@ -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);
@@ -45,7 +53,7 @@ public:
 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);

+ 7 - 14
examples/lock-app/efr32/src/AppTask.cpp

@@ -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;
 
@@ -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)
@@ -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;
@@ -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;

+ 3 - 3
examples/lock-app/efr32/src/BoltLockManager.cpp

@@ -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;
@@ -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);
     }
 }
 
@@ -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);
     }
 }
 

+ 1 - 3
examples/lock-app/efr32/src/main.cpp

@@ -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);
 
@@ -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");

+ 7 - 16
examples/lock-app/esp32/main/AppTask.cpp

@@ -63,31 +63,22 @@ 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)
     {
         ESP_LOGE(TAG, "Failed to allocate app event queue");
-        return 0;
+        return APP_ERROR_EVENT_QUEUE_FAILED;
     }
 
     // Start App task.
     sAppTaskHandle = xTaskCreate(AppTaskMain, APP_TASK_NAME, ArraySize(appStack), NULL, 1, NULL);
-    if (sAppTaskHandle)
-    {
-        err = CHIP_NO_ERROR;
-    }
-
-    return err;
+    return sAppTaskHandle ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED;
 }
 
-int AppTask::Init()
+CHIP_ERROR AppTask::Init()
 {
-    CHIP_ERROR err = CHIP_NO_ERROR;
-
     // Create FreeRTOS sw timer for Function Selection.
     sFunctionTimer = xTimerCreate("FnTmr",          // Just a text name, not used by the RTOS kernel
                                   1,                // == default timer period (mS)
@@ -95,7 +86,7 @@ int AppTask::Init()
                                   (void *) this,    // init timer id = app task obj context
                                   TimerEventHandler // timer callback handler
     );
-    err            = BoltLockMgr().Init();
+    CHIP_ERROR err = BoltLockMgr().Init();
     if (err != CHIP_NO_ERROR)
     {
         ESP_LOGI(TAG, "BoltLockMgr().Init() failed");
@@ -216,7 +207,7 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent)
     bool initiated = false;
     BoltLockManager::Action_t action;
     int32_t actor;
-    int err = CHIP_NO_ERROR;
+    CHIP_ERROR err = CHIP_NO_ERROR;
 
     if (aEvent->Type == AppEvent::kEventType_Lock)
     {
@@ -237,7 +228,7 @@ void AppTask::LockActionEventHandler(AppEvent * aEvent)
     }
     else
     {
-        err = CHIP_CONFIG_CORE_ERROR_MAX;
+        err = APP_ERROR_UNHANDLED_EVENT;
     }
 
     if (err == CHIP_NO_ERROR)

+ 2 - 2
examples/lock-app/esp32/main/BoltLockManager.cpp

@@ -26,7 +26,7 @@ BoltLockManager BoltLockManager::sLock;
 
 TimerHandle_t sLockTimer;
 
-int BoltLockManager::Init()
+CHIP_ERROR BoltLockManager::Init()
 {
     // Create FreeRTOS sw timer for lock timer.
     sLockTimer = xTimerCreate("lockTmr",        // Just a text name, not used by the RTOS kernel
@@ -39,7 +39,7 @@ int BoltLockManager::Init()
     if (sLockTimer == NULL)
     {
         ESP_LOGE(TAG, "sLockTimer timer create failed");
-        return CHIP_CONFIG_CORE_ERROR_MAX;
+        return APP_ERROR_CREATE_TIMER_FAILED;
     }
 
     mState              = kState_LockingCompleted;

+ 10 - 2
examples/lock-app/esp32/main/include/AppTask.h

@@ -28,11 +28,19 @@
 #include <lib/support/CodeUtils.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);
@@ -43,7 +51,7 @@ public:
 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);

+ 3 - 1
examples/lock-app/esp32/main/include/BoltLockManager.h

@@ -23,6 +23,8 @@
 
 #include "AppEvent.h"
 
+#include <core/CHIPError.h>
+
 #include "freertos/FreeRTOS.h"
 #include "freertos/timers.h" // provides FreeRTOS timer support
 
@@ -45,7 +47,7 @@ public:
         kState_UnlockingCompleted,
     } State;
 
-    int Init();
+    CHIP_ERROR Init();
     bool IsUnlocked();
     void EnableAutoRelock(bool aOn);
     void SetAutoLockDuration(uint32_t aDurationInSecs);

+ 3 - 3
examples/lock-app/qpg/src/BoltLockManager.cpp

@@ -41,7 +41,7 @@ int BoltLockManager::Init()
     {
         ChipLogProgress(NotSpecified, "sLockTimer timer create failed");
         // TODO:
-        // appError(CHIP_CONFIG_CORE_ERROR_MAX);
+        // appError(APP_ERROR_CREATE_TIMER_FAILED);
     }
 
     mState              = kState_LockingCompleted;
@@ -136,7 +136,7 @@ void BoltLockManager::StartTimer(uint32_t aTimeoutMs)
     if (xTimerChangePeriod(sLockTimer, (aTimeoutMs / portTICK_PERIOD_MS), 100) != pdPASS)
     {
         ChipLogError(NotSpecified, "sLockTimer timer start() failed");
-        // appError(CHIP_CONFIG_CORE_ERROR_MAX);
+        // appError(APP_ERROR_START_TIMER_FAILED);
     }
 }
 
@@ -145,7 +145,7 @@ void BoltLockManager::CancelTimer(void)
     if (xTimerStop(sLockTimer, 0) == pdFAIL)
     {
         ChipLogError(NotSpecified, "Lock timer timer stop() failed");
-        // appError(CHIP_CONFIG_CORE_ERROR_MAX);
+        // appError(APP_ERROR_STOP_TIMER_FAILED);
     }
 }
 

+ 9 - 9
examples/platform/esp32/shell_extension/heap_trace.cpp

@@ -46,31 +46,31 @@ heap_trace_record_t sTraceRecords[kNumHeapTraceRecords];
 
 Engine sShellHeapSubCommands;
 
-int HeapTraceHelpHandler(int argc, char ** argv)
+CHIP_ERROR HeapTraceHelpHandler(int argc, char ** argv)
 {
     sShellHeapSubCommands.ForEachCommand(PrintCommandHelp, nullptr);
-    return 0;
+    return CHIP_NO_ERROR;
 }
 
 #if CONFIG_HEAP_TRACING_STANDALONE
-int HeapTraceResetHandler(int argc, char ** argv)
+CHIP_ERROR HeapTraceResetHandler(int argc, char ** argv)
 {
     ESP_ERROR_CHECK(heap_trace_stop());
     ESP_ERROR_CHECK(heap_trace_start(HEAP_TRACE_LEAKS));
-    return 0;
+    return CHIP_NO_ERROR;
 }
 
-int HeapTraceDumpHandler(int argc, char ** argv)
+CHIP_ERROR HeapTraceDumpHandler(int argc, char ** argv)
 {
     heap_trace_dump();
     streamer_printf(streamer_get(), "Free heap %d/%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT),
                     heap_caps_get_total_size(MALLOC_CAP_8BIT));
-    return 0;
+    return CHIP_NO_ERROR;
 }
 #endif // CONFIG_HEAP_TRACING_STANDALONE
 
 #if CONFIG_HEAP_TASK_TRACKING
-int HeapTraceTaskHandler(int argc, char ** argv)
+CHIP_ERROR HeapTraceTaskHandler(int argc, char ** argv)
 {
     // static storage is required for task memory info;
     static size_t numTotals = 0;
@@ -103,11 +103,11 @@ int HeapTraceTaskHandler(int argc, char ** argv)
     streamer_printf(streamer_get(), "Free heap %d/%d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT),
                     heap_caps_get_total_size(MALLOC_CAP_8BIT));
 
-    return 0;
+    return CHIP_NO_ERROR;
 }
 #endif
 
-int HeapTraceDispatch(int argc, char ** argv)
+CHIP_ERROR HeapTraceDispatch(int argc, char ** argv)
 {
     if (argc == 0)
     {

+ 2 - 6
examples/platform/qpg/app/main.cpp

@@ -60,14 +60,12 @@ using namespace ::chip::DeviceLayer::Internal;
 
 int Application_Init(void)
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
-
     /* Launch application task */
     ChipLogProgress(NotSpecified, "============================");
     ChipLogProgress(NotSpecified, "Qorvo " APP_NAME " Launching");
     ChipLogProgress(NotSpecified, "============================");
 
-    ret = GetAppTask().StartAppTask();
+    CHIP_ERROR ret = GetAppTask().StartAppTask();
     if (ret != CHIP_NO_ERROR)
     {
         ChipLogError(NotSpecified, "GetAppTask().Init() failed");
@@ -79,9 +77,7 @@ int Application_Init(void)
 
 int CHIP_Init(void)
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
-
-    ret = chip::Platform::MemoryInit();
+    CHIP_ERROR ret = chip::Platform::MemoryInit();
     if (ret != CHIP_NO_ERROR)
     {
         ChipLogError(NotSpecified, "Platform::MemoryInit() failed");

+ 1 - 2
examples/pump-app/cc13x2x7_26x2x7/main/AppTask.cpp

@@ -79,7 +79,6 @@ int AppTask::StartAppTask()
 
 int AppTask::Init()
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
     LED_Params ledParams;
     Button_Params buttionParams;
     ConnectivityManager::ThreadPollingConfig pollingConfig;
@@ -89,7 +88,7 @@ int AppTask::Init()
     // Init Chip memory management before the stack
     chip::Platform::MemoryInit();
 
-    ret = PlatformMgr().InitChipStack();
+    CHIP_ERROR ret = PlatformMgr().InitChipStack();
     if (ret != CHIP_NO_ERROR)
     {
         PLAT_LOG("PlatformMgr().InitChipStack() failed");

+ 1 - 3
examples/pump-app/cc13x2x7_26x2x7/main/main.cpp

@@ -62,8 +62,6 @@ extern "C" void vApplicationStackOverflowHook(void)
 // ================================================================================
 int main(void)
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
-
     Board_init();
     bpool((void *) GlobalHeapZoneBuffer, TOTAL_ICALL_HEAP_SIZE);
 
@@ -79,7 +77,7 @@ int main(void)
 
     SHA2_init();
 
-    ret = GetAppTask().StartAppTask();
+    CHIP_ERROR ret = GetAppTask().StartAppTask();
     if (ret != CHIP_NO_ERROR)
     {
         // can't log until the kernel is started

+ 1 - 2
examples/pump-controller-app/cc13x2x7_26x2x7/main/AppTask.cpp

@@ -79,7 +79,6 @@ int AppTask::StartAppTask()
 
 int AppTask::Init()
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
     LED_Params ledParams;
     Button_Params buttionParams;
     ConnectivityManager::ThreadPollingConfig pollingConfig;
@@ -89,7 +88,7 @@ int AppTask::Init()
     // Init Chip memory management before the stack
     chip::Platform::MemoryInit();
 
-    ret = PlatformMgr().InitChipStack();
+    CHIP_ERROR ret = PlatformMgr().InitChipStack();
     if (ret != CHIP_NO_ERROR)
     {
         PLAT_LOG("PlatformMgr().InitChipStack() failed");

+ 1 - 3
examples/pump-controller-app/cc13x2x7_26x2x7/main/main.cpp

@@ -62,8 +62,6 @@ extern "C" void vApplicationStackOverflowHook(void)
 // ================================================================================
 int main(void)
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
-
     Board_init();
     bpool((void *) GlobalHeapZoneBuffer, TOTAL_ICALL_HEAP_SIZE);
 
@@ -79,7 +77,7 @@ int main(void)
 
     SHA2_init();
 
-    ret = GetAppTask().StartAppTask();
+    CHIP_ERROR ret = GetAppTask().StartAppTask();
     if (ret != CHIP_NO_ERROR)
     {
         // can't log until the kernel is started

+ 1 - 3
examples/shell/efr32/src/main.cpp

@@ -107,8 +107,6 @@ static void shell_task(void * args)
 
 int main(void)
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
-
     init_efrPlatform();
     mbedtls_platform_set_calloc_free(CHIPPlatformMemoryCalloc, CHIPPlatformMemoryFree);
 
@@ -129,7 +127,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");

+ 1 - 3
examples/shell/k32w/main/main.cpp

@@ -64,8 +64,6 @@ unsigned int sleep(unsigned int seconds)
 
 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)
@@ -87,7 +85,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()");

+ 9 - 1
examples/window-app/efr32/include/AppTask.h

@@ -34,6 +34,14 @@
 
 using namespace ::chip::DeviceLayer;
 
+// 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:
@@ -47,7 +55,7 @@ public:
 
     static AppTask & Instance();
 
-    int Start(void);
+    CHIP_ERROR Start(void);
     WindowCover & Cover();
     void PostEvent(const AppEvent & event);
 

+ 3 - 9
examples/window-app/efr32/src/AppTask.cpp

@@ -76,24 +76,18 @@ WindowCover & AppTask::Cover()
     return mCover;
 }
 
-int AppTask::Start()
+CHIP_ERROR AppTask::Start()
 {
-    int err = CHIP_CONFIG_CORE_ERROR_MAX;
-
     mQueue = xQueueCreateStatic(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent), sAppEventQueueBuffer, &sAppEventQueueStruct);
     if (mQueue == NULL)
     {
         EFR32_LOG("Failed to allocate app event queue");
-        appError(err);
+        appError(APP_ERROR_EVENT_QUEUE_FAILED);
     }
 
     // Start App task.
     mHandle = xTaskCreateStatic(Main, APP_TASK_NAME, ArraySize(sAppStack), NULL, 1, sAppStack, &sAppTaskStruct);
-    if (mHandle != NULL)
-    {
-        err = CHIP_NO_ERROR;
-    }
-    return err;
+    return mHandle ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED;
 }
 
 void AppTask::Main(void * pvParameter)

+ 3 - 3
examples/window-app/efr32/src/AppTimer.cpp

@@ -59,7 +59,7 @@ void AppTimer::Start()
     if (xTimerStart(mHandler, 100) != pdPASS)
     {
         EFR32_LOG("AppTimer start() failed");
-        appError(CHIP_CONFIG_CORE_ERROR_MAX);
+        appError(APP_ERROR_START_TIMER_FAILED);
     }
 
     mIsActive = true;
@@ -79,7 +79,7 @@ void AppTimer::Start(uint32_t timeoutInMs)
     if (xTimerChangePeriod(mHandler, timeoutInMs / portTICK_PERIOD_MS, 100) != pdPASS)
     {
         EFR32_LOG("AppTimer start() failed");
-        appError(CHIP_CONFIG_CORE_ERROR_MAX);
+        appError(APP_ERROR_START_TIMER_FAILED);
     }
 
     mIsActive = true;
@@ -104,7 +104,7 @@ void AppTimer::Stop()
     if (xTimerStop(mHandler, 0) == pdFAIL)
     {
         EFR32_LOG("AppTimer stop() failed");
-        appError(CHIP_CONFIG_CORE_ERROR_MAX);
+        appError(APP_ERROR_STOP_TIMER_FAILED);
     }
 }
 

+ 1 - 3
examples/window-app/efr32/src/main.cpp

@@ -95,8 +95,6 @@ extern "C" void vApplicationIdleHook(void)
 // ================================================================================
 int main(void)
 {
-    int ret = CHIP_CONFIG_CORE_ERROR_MAX;
-
     init_efrPlatform();
 
 #if PW_RPC_ENABLED
@@ -118,7 +116,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");

+ 1 - 1
src/ble/BleError.cpp

@@ -49,7 +49,7 @@ bool FormatLayerError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
     const char * desc = nullptr;
 
-    if (err < BLE_CONFIG_ERROR_MIN || err > BLE_CONFIG_ERROR_MAX)
+    if (!ChipError::IsPart(ChipError::SdkPart::kBLE, err))
     {
         return false;
     }

+ 33 - 48
src/ble/BleError.h

@@ -34,23 +34,9 @@
 
 #include <core/CHIPError.h>
 
-// clang-format off
+#define CHIP_BLE_ERROR(e) CHIP_SDK_ERROR(::chip::ChipError::SdkPart::kBLE, (e))
 
-/**
- *  @def CHIP_BLE_ERROR(e)
- *
- *  @brief
- *    This defines a mapping function for BleLayer errors that allows
- *    mapping such errors into a platform- or system-specific
- *    range. This function may be configured via
- *    #BLE_CONFIG_ERROR.
- *
- *  @param[in]  e  The BleLayer error to map.
- *
- *  @return The mapped BleLayer error.
- *
- */
-#define CHIP_BLE_ERROR(e)                                  BLE_CONFIG_ERROR(e)
+// clang-format off
 
 /**
  *  @name Error Definitions
@@ -58,9 +44,8 @@
  *  @{
  */
 
-// unused                                                  CHIP_BLE_ERROR(0)
-// unused                                                  CHIP_BLE_ERROR(1)
-// unused                                                  CHIP_BLE_ERROR(2)
+// unused                                                  CHIP_BLE_ERROR(0x01)
+// unused                                                  CHIP_BLE_ERROR(0x02)
 
 /**
  *  @def BLE_ERROR_NO_CONNECTION_RECEIVED_CALLBACK
@@ -70,7 +55,7 @@
  *    connection.
  *
  */
-#define BLE_ERROR_NO_CONNECTION_RECEIVED_CALLBACK          CHIP_BLE_ERROR(3)
+#define BLE_ERROR_NO_CONNECTION_RECEIVED_CALLBACK          CHIP_BLE_ERROR(0x03)
 
 /**
  *  @def BLE_ERROR_CENTRAL_UNSUBSCRIBED
@@ -80,7 +65,7 @@
  *    Transport Protocol (BTP) transmit characteristic.
  *
  */
-#define BLE_ERROR_CENTRAL_UNSUBSCRIBED                     CHIP_BLE_ERROR(4)
+#define BLE_ERROR_CENTRAL_UNSUBSCRIBED                     CHIP_BLE_ERROR(0x04)
 
 /**
  *  @def BLE_ERROR_GATT_SUBSCRIBE_FAILED
@@ -90,7 +75,7 @@
  *    Transport Protocol (BTP) transmit characteristic.
  *
  */
-#define BLE_ERROR_GATT_SUBSCRIBE_FAILED                    CHIP_BLE_ERROR(5)
+#define BLE_ERROR_GATT_SUBSCRIBE_FAILED                    CHIP_BLE_ERROR(0x05)
 
 /**
  *  @def BLE_ERROR_GATT_UNSUBSCRIBE_FAILED
@@ -100,7 +85,7 @@
  *    BLE Transport Protocol (BTP) transmit characteristic.
  *
  */
-#define BLE_ERROR_GATT_UNSUBSCRIBE_FAILED                  CHIP_BLE_ERROR(6)
+#define BLE_ERROR_GATT_UNSUBSCRIBE_FAILED                  CHIP_BLE_ERROR(0x06)
 
 /**
  *  @def BLE_ERROR_GATT_WRITE_FAILED
@@ -109,7 +94,7 @@
  *    A General Attribute Profile (GATT) write operation failed.
  *
  */
-#define BLE_ERROR_GATT_WRITE_FAILED                        CHIP_BLE_ERROR(7)
+#define BLE_ERROR_GATT_WRITE_FAILED                        CHIP_BLE_ERROR(0x07)
 
 /**
  *  @def BLE_ERROR_GATT_INDICATE_FAILED
@@ -118,10 +103,10 @@
  *    A General Attribute Profile (GATT) indicate operation failed.
  *
  */
-#define BLE_ERROR_GATT_INDICATE_FAILED                     CHIP_BLE_ERROR(8)
+#define BLE_ERROR_GATT_INDICATE_FAILED                     CHIP_BLE_ERROR(0x08)
 
-// unused                                                  CHIP_BLE_ERROR(9)
-// unused                                                  CHIP_BLE_ERROR(10)
+// unused                                                  CHIP_BLE_ERROR(0x09)
+// unused                                                  CHIP_BLE_ERROR(0x0a)
 
 /**
  *  @def BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT
@@ -130,7 +115,7 @@
  *    A BLE Transport Protocol (BTP) error was encountered.
  *
  */
-#define BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT                  CHIP_BLE_ERROR(11)
+#define BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT                  CHIP_BLE_ERROR(0x0b)
 
 /**
  *  @def BLE_ERROR_REMOTE_DEVICE_DISCONNECTED
@@ -140,7 +125,7 @@
  *    expiration of a BLE connection supervision timeout.
  *
  */
-#define BLE_ERROR_REMOTE_DEVICE_DISCONNECTED               CHIP_BLE_ERROR(12)
+#define BLE_ERROR_REMOTE_DEVICE_DISCONNECTED               CHIP_BLE_ERROR(0x0c)
 
 /**
  *  @def BLE_ERROR_APP_CLOSED_CONNECTION
@@ -149,9 +134,9 @@
  *    The local application closed a BLE connection, and has informed BleLayer.
  *
  */
-#define BLE_ERROR_APP_CLOSED_CONNECTION                    CHIP_BLE_ERROR(13)
+#define BLE_ERROR_APP_CLOSED_CONNECTION                    CHIP_BLE_ERROR(0x0d)
 
-// unused                                                  CHIP_BLE_ERROR(14)
+// unused                                                  CHIP_BLE_ERROR(0x0e)
 
 /**
  *  @def BLE_ERROR_NOT_CHIP_DEVICE
@@ -161,7 +146,7 @@
  *    (GATT) service required by the Bluetooth Transport Protocol (BTP).
  *
  */
-#define BLE_ERROR_NOT_CHIP_DEVICE                         CHIP_BLE_ERROR(15)
+#define BLE_ERROR_NOT_CHIP_DEVICE                         CHIP_BLE_ERROR(0x0f)
 
 /**
  *  @def BLE_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS
@@ -171,10 +156,10 @@
  *    Transport Protocol (BTP).
  *
  */
-#define BLE_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS           CHIP_BLE_ERROR(16)
+#define BLE_ERROR_INCOMPATIBLE_PROTOCOL_VERSIONS           CHIP_BLE_ERROR(0x10)
 
-// unused                                                  CHIP_BLE_ERROR(17)
-// unused                                                  CHIP_BLE_ERROR(18)
+// unused                                                  CHIP_BLE_ERROR(0x11)
+// unused                                                  CHIP_BLE_ERROR(0x12)
 
 /**
  *  @def BLE_ERROR_INVALID_FRAGMENT_SIZE
@@ -184,7 +169,7 @@
  *    fragment size.
  *
  */
-#define BLE_ERROR_INVALID_FRAGMENT_SIZE                    CHIP_BLE_ERROR(19)
+#define BLE_ERROR_INVALID_FRAGMENT_SIZE                    CHIP_BLE_ERROR(0x13)
 
 /**
  *  @def BLE_ERROR_START_TIMER_FAILED
@@ -193,7 +178,7 @@
  *    A timer failed to start within BleLayer.
  *
  */
-#define BLE_ERROR_START_TIMER_FAILED                       CHIP_BLE_ERROR(20)
+#define BLE_ERROR_START_TIMER_FAILED                       CHIP_BLE_ERROR(0x14)
 
 /**
  *  @def BLE_ERROR_CONNECT_TIMED_OUT
@@ -203,7 +188,7 @@
  *    connect handshake response timed out.
  *
  */
-#define BLE_ERROR_CONNECT_TIMED_OUT                        CHIP_BLE_ERROR(21)
+#define BLE_ERROR_CONNECT_TIMED_OUT                        CHIP_BLE_ERROR(0x15)
 
 /**
  *  @def BLE_ERROR_RECEIVE_TIMED_OUT
@@ -213,7 +198,7 @@
  *    handshake timed out.
  *
  */
-#define BLE_ERROR_RECEIVE_TIMED_OUT                        CHIP_BLE_ERROR(22)
+#define BLE_ERROR_RECEIVE_TIMED_OUT                        CHIP_BLE_ERROR(0x16)
 
 /**
  *  @def BLE_ERROR_INVALID_MESSAGE
@@ -222,7 +207,7 @@
  *    An invalid Bluetooth Transport Protocol (BTP) message was received.
  *
  */
-#define BLE_ERROR_INVALID_MESSAGE                          CHIP_BLE_ERROR(23)
+#define BLE_ERROR_INVALID_MESSAGE                          CHIP_BLE_ERROR(0x17)
 
 /**
  *  @def BLE_ERROR_FRAGMENT_ACK_TIMED_OUT
@@ -232,7 +217,7 @@
  *    acknowledgement timed out.
  *
  */
-#define BLE_ERROR_FRAGMENT_ACK_TIMED_OUT                   CHIP_BLE_ERROR(24)
+#define BLE_ERROR_FRAGMENT_ACK_TIMED_OUT                   CHIP_BLE_ERROR(0x18)
 
 /**
  *  @def BLE_ERROR_KEEP_ALIVE_TIMED_OUT
@@ -242,7 +227,7 @@
  *    fragment timed out.
  *
  */
-#define BLE_ERROR_KEEP_ALIVE_TIMED_OUT                     CHIP_BLE_ERROR(25)
+#define BLE_ERROR_KEEP_ALIVE_TIMED_OUT                     CHIP_BLE_ERROR(0x19)
 
 /**
  *  @def BLE_ERROR_NO_CONNECT_COMPLETE_CALLBACK
@@ -252,7 +237,7 @@
  *    connect completion.
  *
  */
-#define BLE_ERROR_NO_CONNECT_COMPLETE_CALLBACK            CHIP_BLE_ERROR(26)
+#define BLE_ERROR_NO_CONNECT_COMPLETE_CALLBACK            CHIP_BLE_ERROR(0x1a)
 
 /**
  *  @def BLE_ERROR_INVALID_ACK
@@ -261,7 +246,7 @@
  *    A Bluetooth Transport Protcol (BTP) fragment acknowledgement was invalid.
  *
  */
-#define BLE_ERROR_INVALID_ACK                              CHIP_BLE_ERROR(27)
+#define BLE_ERROR_INVALID_ACK                              CHIP_BLE_ERROR(0x1b)
 
 /**
  *  @def BLE_ERROR_REASSEMBLER_MISSING_DATA
@@ -272,7 +257,7 @@
  *    the indicated size of the original fragmented message.
  *
  */
-#define BLE_ERROR_REASSEMBLER_MISSING_DATA                 CHIP_BLE_ERROR(28)
+#define BLE_ERROR_REASSEMBLER_MISSING_DATA                 CHIP_BLE_ERROR(0x1c)
 
 /**
  *  @def BLE_ERROR_INVALID_BTP_HEADER_FLAGS
@@ -281,7 +266,7 @@
  *    A set of Bluetooth Transport Protocol (BTP) header flags is invalid.
  *
  */
-#define BLE_ERROR_INVALID_BTP_HEADER_FLAGS                 CHIP_BLE_ERROR(29)
+#define BLE_ERROR_INVALID_BTP_HEADER_FLAGS                 CHIP_BLE_ERROR(0x1d)
 
 /**
  *  @def BLE_ERROR_INVALID_BTP_SEQUENCE_NUMBER
@@ -290,7 +275,7 @@
  *    A Bluetooth Transport Protocol (BTP) fragment sequence number is invalid.
  *
  */
-#define BLE_ERROR_INVALID_BTP_SEQUENCE_NUMBER              CHIP_BLE_ERROR(30)
+#define BLE_ERROR_INVALID_BTP_SEQUENCE_NUMBER              CHIP_BLE_ERROR(0x1e)
 
 /**
  *  @def BLE_ERROR_REASSEMBLER_INCORRECT_STATE
@@ -300,7 +285,7 @@
  *    encountered an unexpected state.
  *
  */
-#define BLE_ERROR_REASSEMBLER_INCORRECT_STATE              CHIP_BLE_ERROR(31)
+#define BLE_ERROR_REASSEMBLER_INCORRECT_STATE              CHIP_BLE_ERROR(0x1f)
 
 // !!!!! IMPORTANT !!!!!  If you add new Ble errors, please update the translation
 // of error codes to strings in BleError.cpp, and add them to unittest

+ 1 - 1
src/darwin/Framework/CHIP/CHIPError.h

@@ -20,7 +20,7 @@
 NS_ASSUME_NONNULL_BEGIN
 FOUNDATION_EXPORT NSErrorDomain const CHIPErrorDomain;
 
-typedef int32_t CHIP_ERROR;
+typedef uint32_t CHIP_ERROR;
 
 typedef NS_ERROR_ENUM(CHIPErrorDomain, CHIPErrorCode) {
     CHIPSuccess = 0,

+ 7 - 7
src/include/platform/CHIPDeviceError.h

@@ -18,9 +18,9 @@
 
 #pragma once
 
-#define CHIP_DEVICE_ERROR_MIN 11000000
-#define CHIP_DEVICE_ERROR_MAX 11000999
-#define CHIP_DEVICE_ERROR(e) (CHIP_DEVICE_ERROR_MIN + (e))
+#include <core/CHIPError.h>
+
+#define CHIP_DEVICE_ERROR(e) CHIP_SDK_ERROR(::chip::ChipError::SdkPart::kDevice, (e))
 
 /**
  *  @def CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND
@@ -29,7 +29,7 @@
  *    The requested configuration value was not found.
  *
  */
-#define CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND CHIP_DEVICE_ERROR(1)
+#define CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND CHIP_DEVICE_ERROR(0x01)
 
 /**
  *  @def CHIP_DEVICE_ERROR_NOT_SERVICE_PROVISIONED
@@ -38,7 +38,7 @@
  *    The device has not been service provisioned.
  *
  */
-#define CHIP_DEVICE_ERROR_NOT_SERVICE_PROVISIONED CHIP_DEVICE_ERROR(2)
+#define CHIP_DEVICE_ERROR_NOT_SERVICE_PROVISIONED CHIP_DEVICE_ERROR(0x02)
 
 /**
  *  @def CHIP_DEVICE_ERROR_SOFTWARE_UPDATE_ABORTED
@@ -47,7 +47,7 @@
  *    The software update was aborted by application
  *
  */
-#define CHIP_DEVICE_ERROR_SOFTWARE_UPDATE_ABORTED CHIP_DEVICE_ERROR(3)
+#define CHIP_DEVICE_ERROR_SOFTWARE_UPDATE_ABORTED CHIP_DEVICE_ERROR(0x03)
 
 /**
  *  @def CHIP_DEVICE_ERROR_SOFTWARE_UPDATE_IGNORED
@@ -56,4 +56,4 @@
  *    The software update was ignored by application.
  *
  */
-#define CHIP_DEVICE_ERROR_SOFTWARE_UPDATE_IGNORED CHIP_DEVICE_ERROR(4)
+#define CHIP_DEVICE_ERROR_SOFTWARE_UPDATE_IGNORED CHIP_DEVICE_ERROR(0x04)

+ 1 - 1
src/inet/InetError.cpp

@@ -59,7 +59,7 @@ bool FormatLayerError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
     const char * desc = nullptr;
 
-    if (err < INET_CONFIG_ERROR_MIN || err > INET_CONFIG_ERROR_MAX)
+    if (!ChipError::IsPart(ChipError::SdkPart::kInet, err))
     {
         return false;
     }

+ 30 - 45
src/inet/InetError.h

@@ -32,25 +32,12 @@
 
 #include <inet/InetConfig.h>
 
+#include <core/CHIPError.h>
 #include <system/SystemError.h>
 
-// clang-format off
+#define CHIP_INET_ERROR(e) CHIP_SDK_ERROR(::chip::ChipError::SdkPart::kInet, (e))
 
-/**
- *  @def CHIP_INET_ERROR(e)
- *
- *  @brief
- *    This defines a mapping function for InetLayer errors that allows
- *    mapping such errors into a platform- or system-specific
- *    range. This function may be configured via
- *    #INET_CONFIG_ERROR(e).
- *
- *  @param[in]  e  The InetLayer error to map.
- *
- *  @return The mapped InetLayer error.
- *
- */
-#define CHIP_INET_ERROR(e)                                  INET_CONFIG_ERROR(e)
+// clang-format off
 
 /**
  *  @name Error Definitions
@@ -66,9 +53,7 @@
  *    the expected type or scope.
  *
  */
-#define INET_ERROR_WRONG_ADDRESS_TYPE                       CHIP_INET_ERROR(0)
-
-// unused                                                   CHIP_INET_ERROR(1)
+#define INET_ERROR_WRONG_ADDRESS_TYPE                       CHIP_INET_ERROR(0x01)
 
 /**
  *  @def INET_ERROR_PEER_DISCONNECTED
@@ -77,14 +62,14 @@
  *    A remote connection peer disconnected.
  *
  */
-#define INET_ERROR_PEER_DISCONNECTED                        CHIP_INET_ERROR(2)
+#define INET_ERROR_PEER_DISCONNECTED                        CHIP_INET_ERROR(0x02)
 
-// unused                                                   CHIP_INET_ERROR(3)
-// unused                                                   CHIP_INET_ERROR(4)
-// unused                                                   CHIP_INET_ERROR(5)
-// unused                                                   CHIP_INET_ERROR(6)
-// unused                                                   CHIP_INET_ERROR(7)
-// unused                                                   CHIP_INET_ERROR(8)
+// unused                                                   CHIP_INET_ERROR(0x03)
+// unused                                                   CHIP_INET_ERROR(0x04)
+// unused                                                   CHIP_INET_ERROR(0x05)
+// unused                                                   CHIP_INET_ERROR(0x06)
+// unused                                                   CHIP_INET_ERROR(0x07)
+// unused                                                   CHIP_INET_ERROR(0x08)
 
 /**
  *  @def INET_ERROR_HOST_NOT_FOUND
@@ -93,7 +78,7 @@
  *    A requested host name could not be resolved to an address.
  *
  */
-#define INET_ERROR_HOST_NOT_FOUND                           CHIP_INET_ERROR(9)
+#define INET_ERROR_HOST_NOT_FOUND                           CHIP_INET_ERROR(0x09)
 
 /**
  *  @def INET_ERROR_DNS_TRY_AGAIN
@@ -103,7 +88,7 @@
  *    again later.
  *
  */
-#define INET_ERROR_DNS_TRY_AGAIN                            CHIP_INET_ERROR(10)
+#define INET_ERROR_DNS_TRY_AGAIN                            CHIP_INET_ERROR(0x0a)
 
 /**
  *  @def INET_ERROR_DNS_NO_RECOVERY
@@ -112,9 +97,9 @@
  *    A name server returned an unrecoverable error.
  *
  */
-#define INET_ERROR_DNS_NO_RECOVERY                          CHIP_INET_ERROR(11)
+#define INET_ERROR_DNS_NO_RECOVERY                          CHIP_INET_ERROR(0x0b)
 
-// unused                                                   CHIP_INET_ERROR(12)
+// unused                                                   CHIP_INET_ERROR(0x0c)
 
 /**
  *  @def INET_ERROR_WRONG_PROTOCOL_TYPE
@@ -123,7 +108,7 @@
  *    An incorrect or unexpected protocol type was encountered.
  *
  */
-#define INET_ERROR_WRONG_PROTOCOL_TYPE                      CHIP_INET_ERROR(13)
+#define INET_ERROR_WRONG_PROTOCOL_TYPE                      CHIP_INET_ERROR(0x0d)
 
 /**
  *  @def INET_ERROR_UNKNOWN_INTERFACE
@@ -132,9 +117,9 @@
  *    An unknown interface identifier was encountered.
  *
  */
-#define INET_ERROR_UNKNOWN_INTERFACE                        CHIP_INET_ERROR(14)
+#define INET_ERROR_UNKNOWN_INTERFACE                        CHIP_INET_ERROR(0x0e)
 
-// unused                                                   CHIP_INET_ERROR(15)
+// unused                                                   CHIP_INET_ERROR(0x0f)
 
 /**
  *  @def INET_ERROR_ADDRESS_NOT_FOUND
@@ -143,7 +128,7 @@
  *    A requested address type, class, or scope cannot be found.
  *
  */
-#define INET_ERROR_ADDRESS_NOT_FOUND                        CHIP_INET_ERROR(16)
+#define INET_ERROR_ADDRESS_NOT_FOUND                        CHIP_INET_ERROR(0x10)
 
 /**
  *  @def INET_ERROR_HOST_NAME_TOO_LONG
@@ -152,7 +137,7 @@
  *    A requested host name is too long.
  *
  */
-#define INET_ERROR_HOST_NAME_TOO_LONG                       CHIP_INET_ERROR(17)
+#define INET_ERROR_HOST_NAME_TOO_LONG                       CHIP_INET_ERROR(0x11)
 
 /**
  *  @def INET_ERROR_INVALID_HOST_NAME
@@ -161,10 +146,10 @@
  *    A requested host name and port is invalid.
  *
  */
-#define INET_ERROR_INVALID_HOST_NAME                        CHIP_INET_ERROR(18)
+#define INET_ERROR_INVALID_HOST_NAME                        CHIP_INET_ERROR(0x12)
 
-// unused                                                   CHIP_INET_ERROR(19)
-// unused                                                   CHIP_INET_ERROR(20)
+// unused                                                   CHIP_INET_ERROR(0x13)
+// unused                                                   CHIP_INET_ERROR(0x14)
 
 /**
  *  @def INET_ERROR_IDLE_TIMEOUT
@@ -173,9 +158,9 @@
  *    A TCP connection timed out due to inactivity.
  *
  */
-#define INET_ERROR_IDLE_TIMEOUT                             CHIP_INET_ERROR(21)
+#define INET_ERROR_IDLE_TIMEOUT                             CHIP_INET_ERROR(0x15)
 
-// unused                                                   CHIP_INET_ERROR(22)
+// unused                                                   CHIP_INET_ERROR(0x16)
 
 /**
  *  @def INET_ERROR_INVALID_IPV6_PKT
@@ -184,7 +169,7 @@
  *    An IPv6 packet is invalid.
  *
  */
-#define INET_ERROR_INVALID_IPV6_PKT                         CHIP_INET_ERROR(23)
+#define INET_ERROR_INVALID_IPV6_PKT                         CHIP_INET_ERROR(0x17)
 
 /**
  *  @def INET_ERROR_INTERFACE_INIT_FAILURE
@@ -193,7 +178,7 @@
  *    Failure to initialize an interface.
  *
  */
-#define INET_ERROR_INTERFACE_INIT_FAILURE                   CHIP_INET_ERROR(24)
+#define INET_ERROR_INTERFACE_INIT_FAILURE                   CHIP_INET_ERROR(0x18)
 
 /**
  *  @def INET_ERROR_TCP_USER_TIMEOUT
@@ -203,7 +188,7 @@
  *    acknowledgment for transmitted packet.
  *
  */
-#define INET_ERROR_TCP_USER_TIMEOUT                         CHIP_INET_ERROR(25)
+#define INET_ERROR_TCP_USER_TIMEOUT                         CHIP_INET_ERROR(0x19)
 
 /**
  *  @def INET_ERROR_TCP_CONNECT_TIMEOUT
@@ -214,7 +199,7 @@
  *    of an error.
  *
  */
-#define INET_ERROR_TCP_CONNECT_TIMEOUT                      CHIP_INET_ERROR(26)
+#define INET_ERROR_TCP_CONNECT_TIMEOUT                      CHIP_INET_ERROR(0x1a)
 
 /**
  *  @def INET_ERROR_INCOMPATIBLE_IP_ADDRESS_TYPE
@@ -224,7 +209,7 @@
  *    IP address type.
  *
  */
-#define INET_ERROR_INCOMPATIBLE_IP_ADDRESS_TYPE             CHIP_INET_ERROR(27)
+#define INET_ERROR_INCOMPATIBLE_IP_ADDRESS_TYPE             CHIP_INET_ERROR(0x1b)
 
 //                        !!!!! IMPORTANT !!!!!
 //

+ 1 - 1
src/inet/tests/TestInetEndPoint.cpp

@@ -217,7 +217,7 @@ static void TestInetError(nlTestSuite * inSuite, void * inContext)
 
     err = MapErrorPOSIX(EPERM);
     NL_TEST_ASSERT(inSuite, DescribeErrorPOSIX(err));
-    NL_TEST_ASSERT(inSuite, IsErrorPOSIX(err));
+    NL_TEST_ASSERT(inSuite, ChipError::IsRange(ChipError::Range::kPOSIX, err));
 }
 
 static void TestInetInterface(nlTestSuite * inSuite, void * inContext)

+ 1 - 1
src/lib/asn1/ASN1Error.cpp

@@ -46,7 +46,7 @@ bool FormatASN1Error(char * buf, uint16_t bufSize, int32_t err)
 {
     const char * desc = nullptr;
 
-    if (err < ASN1_ERROR_MIN || err > ASN1_ERROR_MAX)
+    if (!ChipError::IsPart(ChipError::SdkPart::kASN1, err))
     {
         return false;
     }

+ 13 - 48
src/lib/asn1/ASN1Error.h

@@ -33,48 +33,13 @@
 
 #include <core/CHIPError.h>
 
+#define CHIP_ASN1_ERROR(e) CHIP_SDK_ERROR(::chip::ChipError::SdkPart::kASN1, (e))
+
 namespace chip {
 namespace ASN1 {
 
 // clang-format off
 
-/**
- *  @def ASN1_ERROR_MIN
- *
- *  @brief
- *    This defines the base or minimum ASN1 error number range.
- *    This value may be configured via #ASN1_CONFIG_ERROR_MIN.
- *
- */
-#define ASN1_ERROR_MIN                  ASN1_CONFIG_ERROR_MIN
-
-/**
- *  @def ASN1_ERROR_MAX
- *
- *  @brief
- *    This defines the top or maximum ASN1 error number range.
- *    This value may be configured via #ASN1_CONFIG_ERROR_MAX.
- *
- */
-#define ASN1_ERROR_MAX                  ASN1_CONFIG_ERROR_MAX
-
-/**
- *  @def CHIP_ASN1_ERROR(e)
- *
- *  @brief
- *    This defines a mapping function for ASN1 errors that allows
- *    mapping such errors into a platform- or system-specific
- *    range. This function may be configured via
- *    #ASN1_CONFIG_ERROR(e).
- *
- *  @param[in]  e  The ASN1 error to map.
- *
- *  @return The mapped ASN1 error.
- *
- */
-#define CHIP_ASN1_ERROR(e)              ASN1_CONFIG_ERROR(e)
-
-
 /**
  *  @name Error Definitions
  *
@@ -88,7 +53,7 @@ namespace ASN1 {
  *    An end of ASN1 container or stream condition occurred.
  *
  */
-#define ASN1_END                        CHIP_ASN1_ERROR(0)
+#define ASN1_END                        CHIP_ASN1_ERROR(0x00)
 
 /**
  *  @def ASN1_ERROR_UNDERRUN
@@ -97,7 +62,7 @@ namespace ASN1 {
  *    The ASN.1 encoding ended prematurely.
  *
  */
-#define ASN1_ERROR_UNDERRUN             CHIP_ASN1_ERROR(1)
+#define ASN1_ERROR_UNDERRUN             CHIP_ASN1_ERROR(0x01)
 
 /**
  *  @def ASN1_ERROR_OVERFLOW
@@ -106,7 +71,7 @@ namespace ASN1 {
  *    The encoding exceeds the available space required to write it.
  *
  */
-#define ASN1_ERROR_OVERFLOW             CHIP_ASN1_ERROR(2)
+#define ASN1_ERROR_OVERFLOW             CHIP_ASN1_ERROR(0x02)
 
 /**
  *  @def ASN1_ERROR_INVALID_STATE
@@ -115,7 +80,7 @@ namespace ASN1 {
  *    An unexpected or invalid state was encountered.
  *
  */
-#define ASN1_ERROR_INVALID_STATE        CHIP_ASN1_ERROR(3)
+#define ASN1_ERROR_INVALID_STATE        CHIP_ASN1_ERROR(0x03)
 
 /**
  *  @def ASN1_ERROR_MAX_DEPTH_EXCEEDED
@@ -124,7 +89,7 @@ namespace ASN1 {
  *    The maximum number of container reading contexts was exceeded.
  *
  */
-#define ASN1_ERROR_MAX_DEPTH_EXCEEDED   CHIP_ASN1_ERROR(4)
+#define ASN1_ERROR_MAX_DEPTH_EXCEEDED   CHIP_ASN1_ERROR(0x04)
 
 /**
  *  @def ASN1_ERROR_INVALID_ENCODING
@@ -133,7 +98,7 @@ namespace ASN1 {
  *    The ASN.1 encoding is invalid.
  *
  */
-#define ASN1_ERROR_INVALID_ENCODING     CHIP_ASN1_ERROR(5)
+#define ASN1_ERROR_INVALID_ENCODING     CHIP_ASN1_ERROR(0x05)
 
 /**
  *  @def ASN1_ERROR_UNSUPPORTED_ENCODING
@@ -142,7 +107,7 @@ namespace ASN1 {
  *    An unsupported encoding was requested or encountered.
  *
  */
-#define ASN1_ERROR_UNSUPPORTED_ENCODING CHIP_ASN1_ERROR(6)
+#define ASN1_ERROR_UNSUPPORTED_ENCODING CHIP_ASN1_ERROR(0x06)
 
 /**
  *  @def ASN1_ERROR_TAG_OVERFLOW
@@ -152,7 +117,7 @@ namespace ASN1 {
  *    for it.
  *
  */
-#define ASN1_ERROR_TAG_OVERFLOW         CHIP_ASN1_ERROR(7)
+#define ASN1_ERROR_TAG_OVERFLOW         CHIP_ASN1_ERROR(0x07)
 
 /**
  *  @def ASN1_ERROR_LENGTH_OVERFLOW
@@ -162,7 +127,7 @@ namespace ASN1 {
  *    required for it.
  *
  */
-#define ASN1_ERROR_LENGTH_OVERFLOW      CHIP_ASN1_ERROR(8)
+#define ASN1_ERROR_LENGTH_OVERFLOW      CHIP_ASN1_ERROR(0x08)
 
 /**
  *  @def ASN1_ERROR_VALUE_OVERFLOW
@@ -172,7 +137,7 @@ namespace ASN1 {
  *    required for it.
  *
  */
-#define ASN1_ERROR_VALUE_OVERFLOW       CHIP_ASN1_ERROR(9)
+#define ASN1_ERROR_VALUE_OVERFLOW       CHIP_ASN1_ERROR(0x09)
 
 /**
  *  @def ASN1_ERROR_UNKNOWN_OBJECT_ID
@@ -182,7 +147,7 @@ namespace ASN1 {
  *    supported object identifiers.
  *
  */
-#define ASN1_ERROR_UNKNOWN_OBJECT_ID    CHIP_ASN1_ERROR(10)
+#define ASN1_ERROR_UNKNOWN_OBJECT_ID    CHIP_ASN1_ERROR(0x0a)
 
 //                        !!!!! IMPORTANT !!!!!
 //

+ 0 - 49
src/lib/core/CHIPConfig.h

@@ -74,55 +74,6 @@
 
 // Profile-specific Configuration Headers
 
-/**
- *  @def CHIP_CONFIG_ERROR_TYPE
- *
- *  @brief
- *    This defines the data type used to represent errors for chip.
- *
- */
-#ifndef CHIP_CONFIG_ERROR_TYPE
-#include <inttypes.h>
-#include <stdint.h>
-
-#define CHIP_CONFIG_ERROR_TYPE int32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-#endif // CHIP_CONFIG_ERROR_TYPE
-
-/**
- *  @def CHIP_CONFIG_CORE_ERROR_MIN
- *
- *  @brief
- *    This defines the base or minimum chip error number range.
- *
- */
-#ifndef CHIP_CONFIG_CORE_ERROR_MIN
-#define CHIP_CONFIG_CORE_ERROR_MIN 4000
-#endif // CHIP_CONFIG_CORE_ERROR_MIN
-
-/**
- *  @def CHIP_CONFIG_CORE_ERROR_MAX
- *
- *  @brief
- *    This defines the top or maximum chip error number range.
- *
- */
-#ifndef CHIP_CONFIG_CORE_ERROR_MAX
-#define CHIP_CONFIG_CORE_ERROR_MAX 4999
-#endif // CHIP_CONFIG_CORE_ERROR_MAX
-
-/**
- *  @def CHIP_CONFIG_CORE_ERROR
- *
- *  @brief
- *    This defines a mapping function for chip errors that allows
- *    mapping such errors into a platform- or system-specific manner.
- *
- */
-#ifndef CHIP_CONFIG_CORE_ERROR
-#define CHIP_CONFIG_CORE_ERROR(e) (CHIP_CONFIG_CORE_ERROR_MIN + (e))
-#endif // CHIP_CONFIG_CORE_ERROR
-
 /**
  *  @def CHIP_CONFIG_USE_OPENSSL_ECC
  *

+ 1 - 1
src/lib/core/CHIPError.cpp

@@ -54,7 +54,7 @@ bool FormatCHIPError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
     const char * desc = nullptr;
 
-    if (err < CHIP_CONFIG_CORE_ERROR_MIN || err > CHIP_CONFIG_CORE_ERROR_MAX)
+    if (!ChipError::IsPart(ChipError::SdkPart::kCore, err))
     {
         return false;
     }

File diff suppressed because it is too large
+ 223 - 122
src/lib/core/CHIPError.h


+ 0 - 4
src/lib/support/tests/TestErrorStr.cpp

@@ -152,10 +152,6 @@ static bool testFormatErr()
     // skip both
     FormatError(buf, kBufSize, nullptr, 1, nullptr);
     ret &= CHECK_EQ_STR(buf, "Error 1 (0x00000001)");
-
-    // negative
-    FormatError(buf, kBufSize, nullptr, static_cast<CHIP_ERROR>(-1), nullptr);
-    ret &= CHECK_EQ_STR(buf, "Error -1 (0xFFFFFFFF)");
 #endif
 
     return ret;

+ 0 - 3
src/platform/Darwin/CHIPPlatformConfig.h

@@ -25,9 +25,6 @@
 
 // ==================== General Platform Adaptations ====================
 
-#define CHIP_CONFIG_ERROR_TYPE int32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-
 #define ChipDie() abort()
 
 // TODO:(#756) Add FabricState support

+ 1 - 1
src/platform/EFR32/BLEManagerImpl.cpp

@@ -550,7 +550,7 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
     case SL_STATUS_INVALID_PARAMETER:
         return CHIP_ERROR_INVALID_ARGUMENT;
     default:
-        return static_cast<CHIP_ERROR>(bleErr + CHIP_DEVICE_CONFIG_EFR32_BLE_ERROR_MIN);
+        return ChipError::Encapsulate(ChipError::Range::kPlatform, bleErr + CHIP_DEVICE_CONFIG_EFR32_BLE_ERROR_MIN);
     }
 }
 

+ 2 - 2
src/platform/EFR32/CHIPDevicePlatformConfig.h

@@ -26,8 +26,8 @@
 
 // ==================== Platform Adaptations ====================
 
-#define CHIP_DEVICE_CONFIG_EFR32_NVM3_ERROR_MIN 12000000
-#define CHIP_DEVICE_CONFIG_EFR32_BLE_ERROR_MIN 13000000
+#define CHIP_DEVICE_CONFIG_EFR32_NVM3_ERROR_MIN 0xB00000
+#define CHIP_DEVICE_CONFIG_EFR32_BLE_ERROR_MIN 0xC00000
 
 #define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0
 #define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0

+ 0 - 8
src/platform/EFR32/CHIPPlatformConfig.h

@@ -28,14 +28,6 @@
 
 // ==================== General Platform Adaptations ====================
 
-#define CHIP_CONFIG_ERROR_TYPE int32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-#define CHIP_CONFIG_CORE_ERROR_MIN 4000000
-#define CHIP_CONFIG_CORE_ERROR_MAX 4000999
-
-#define ASN1_CONFIG_ERROR_MIN 5000000
-#define ASN1_CONFIG_ERROR_MAX 5000999
-
 #define ChipDie() abort()
 
 #define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE uint8_t

+ 1 - 1
src/platform/EFR32/EFR32Config.cpp

@@ -566,7 +566,7 @@ CHIP_ERROR EFR32Config::MapNvm3Error(Ecode_t nvm3Res)
         err = CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
         break;
     default:
-        err = static_cast<CHIP_ERROR>((nvm3Res & 0xFF) + CHIP_DEVICE_CONFIG_EFR32_NVM3_ERROR_MIN);
+        err = ChipError::Encapsulate(ChipError::Range::kPlatform, (nvm3Res & 0xFF) + CHIP_DEVICE_CONFIG_EFR32_NVM3_ERROR_MIN);
         break;
     }
 

+ 0 - 3
src/platform/EFR32/SystemPlatformConfig.h

@@ -39,9 +39,6 @@ struct ChipDeviceEvent;
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
 
-#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000000
-#define CHIP_SYSTEM_LWIP_ERROR_MAX 3000128
-
 // ========== Platform-specific Configuration Overrides =========
 
 #ifndef CHIP_SYSTEM_CONFIG_NUM_TIMERS

+ 1 - 1
src/platform/ESP32/CHIPDevicePlatformConfig.h

@@ -29,7 +29,7 @@
 
 #define CHIP_DEVICE_CONFIG_LWIP_WIFI_STATION_IF_NAME "st"
 
-#define CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN 11000000
+#define CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN 0xB00000
 // ==================== Kconfig Overrides ====================
 
 // The following values are configured via the ESP-IDF Kconfig mechanism.

+ 0 - 8
src/platform/ESP32/CHIPPlatformConfig.h

@@ -41,14 +41,6 @@
 // The ESP NVS implementation limits key names to 15 characters.
 #define CHIP_CONFIG_PERSISTED_STORAGE_MAX_KEY_LENGTH 15
 
-#define CHIP_CONFIG_ERROR_TYPE esp_err_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-#define CHIP_CONFIG_CORE_ERROR_MIN 4000000
-#define CHIP_CONFIG_CORE_ERROR_MAX 4000999
-
-#define ASN1_CONFIG_ERROR_MIN 5000000
-#define ASN1_CONFIG_ERROR_MAX 5000999
-
 #define CHIP_LOG_FILTERING 0
 #define CHIP_CONFIG_TIME_ENABLE_CLIENT 1
 #define CHIP_CONFIG_TIME_ENABLE_SERVER 0

+ 0 - 3
src/platform/ESP32/SystemPlatformConfig.h

@@ -42,9 +42,6 @@ struct ChipDeviceEvent;
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
 
-#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000000
-#define CHIP_SYSTEM_LWIP_ERROR_MAX 3000128
-
 // ==================== General Configuration Overrides ====================
 
 // NOTE: Values that are mapped to CONFIG_ #defines are settable via the ESP-IDF Kconfig mechanism.

+ 1 - 1
src/platform/ESP32/bluedroid/BLEManagerImpl.cpp

@@ -443,7 +443,7 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
     case ESP_ERR_NO_MEM:
         return CHIP_ERROR_NO_MEMORY;
     default:
-        return CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + static_cast<CHIP_ERROR>(bleErr);
+        return ChipError::Encapsulate(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
     }
 }
 

+ 1 - 1
src/platform/ESP32/nimble/BLEManagerImpl.cpp

@@ -452,7 +452,7 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
     case ESP_ERR_INVALID_ARG:
         return CHIP_ERROR_INVALID_ARGUMENT;
     default:
-        return CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + static_cast<CHIP_ERROR>(bleErr);
+        return ChipError::Encapsulate(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
     }
 }
 void BLEManagerImpl::DriveBLEState(void)

+ 1 - 1
src/platform/GeneralUtils.cpp

@@ -115,7 +115,7 @@ bool FormatDeviceLayerError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
     const char * desc = nullptr;
 
-    if (err < CHIP_DEVICE_ERROR_MIN || err > CHIP_DEVICE_ERROR_MAX)
+    if (!ChipError::IsPart(ChipError::SdkPart::kDevice, err))
     {
         return false;
     }

+ 0 - 8
src/platform/K32W/CHIPPlatformConfig.h

@@ -29,14 +29,6 @@
 
 // ==================== General Platform Adaptations ====================
 
-#define CHIP_CONFIG_ERROR_TYPE int32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-#define CHIP_CONFIG_CORE_ERROR_MIN 4000000
-#define CHIP_CONFIG_CORE_ERROR_MAX 4000999
-
-#define ASN1_CONFIG_ERROR_MIN 5000000
-#define ASN1_CONFIG_ERROR_MAX 5000999
-
 #define ChipDie() abort()
 
 #define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE uint16_t

+ 2 - 2
src/platform/K32W/K32WConfig.cpp

@@ -411,7 +411,7 @@ CHIP_ERROR K32WConfig::MapPdmStatus(PDM_teStatus pdmStatus)
         err = CHIP_NO_ERROR;
         break;
     default:
-        err = CHIP_CONFIG_CORE_ERROR_MIN + pdmStatus;
+        err = ChipError::Encapsulate(ChipError::Range::kPlatform, pdmStatus);
         break;
     }
 
@@ -420,7 +420,7 @@ CHIP_ERROR K32WConfig::MapPdmStatus(PDM_teStatus pdmStatus)
 
 CHIP_ERROR K32WConfig::MapPdmInitStatus(int pdmStatus)
 {
-    return (pdmStatus == 0) ? CHIP_NO_ERROR : CHIP_CONFIG_CORE_ERROR_MIN + pdmStatus;
+    return (pdmStatus == 0) ? CHIP_NO_ERROR : ChipError::Encapsulate(ChipError::Range::kPlatform, pdmStatus);
 }
 
 bool K32WConfig::ValidConfigKey(Key key)

+ 0 - 3
src/platform/K32W/SystemPlatformConfig.h

@@ -40,9 +40,6 @@ struct ChipDeviceEvent;
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
 
-#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000000
-#define CHIP_SYSTEM_LWIP_ERROR_MAX 3000128
-
 // ========== Platform-specific Configuration Overrides =========
 
 #ifndef CHIP_SYSTEM_CONFIG_NUM_TIMERS

+ 4 - 3
src/platform/OpenThread/OpenThreadUtils.cpp

@@ -42,7 +42,8 @@ namespace Internal {
  */
 CHIP_ERROR MapOpenThreadError(otError otErr)
 {
-    return (otErr == OT_ERROR_NONE) ? CHIP_NO_ERROR : CHIP_CONFIG_OPENTHREAD_ERROR_MIN + static_cast<CHIP_ERROR>(otErr);
+    return (otErr == OT_ERROR_NONE) ? CHIP_NO_ERROR
+                                    : ChipError::Encapsulate(ChipError::Range::kOpenThread, static_cast<unsigned int>(otErr));
 }
 
 /**
@@ -59,7 +60,7 @@ CHIP_ERROR MapOpenThreadError(otError otErr)
  */
 bool FormatOpenThreadError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
-    if (err < CHIP_CONFIG_OPENTHREAD_ERROR_MIN || err > CHIP_CONFIG_OPENTHREAD_ERROR_MAX)
+    if (!ChipError::IsRange(ChipError::Range::kOpenThread, err))
     {
         return false;
     }
@@ -67,7 +68,7 @@ bool FormatOpenThreadError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 #if CHIP_CONFIG_SHORT_ERROR_STR
     const char * desc = NULL;
 #else  // CHIP_CONFIG_SHORT_ERROR_STR
-    otError otErr     = (otError)(err - CHIP_CONFIG_OPENTHREAD_ERROR_MIN);
+    otError otErr     = (otError) ChipError::GetValue(err);
     const char * desc = otThreadErrorToString(otErr);
 #endif // CHIP_CONFIG_SHORT_ERROR_STR
 

+ 0 - 3
src/platform/cc13x2_26x2/CHIPPlatformConfig.h

@@ -30,9 +30,6 @@
 
 // ==================== General Platform Adaptations ====================
 
-#define CHIP_CONFIG_ERROR_TYPE uint32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRIu32
-
 #define ChipDie() assert()
 
 #define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE uint16_t

+ 0 - 6
src/platform/mbed/CHIPPlatformConfig.h

@@ -28,12 +28,6 @@
 
 // ==================== General Platform Adaptations ====================
 
-#define CHIP_CONFIG_ERROR_TYPE int32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-
-#define CHIP_CONFIG_ERROR_MIN 4000000
-#define CHIP_CONFIG_ERROR_MAX 4000999
-
 #define ChipDie() abort()
 
 #define CHIP_CONFIG_PERSISTED_STORAGE_KEY_TYPE const char *

+ 0 - 8
src/platform/mbed/SystemPlatformConfig.h

@@ -44,14 +44,6 @@ struct ChipDeviceEvent;
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
 
-#define CHIP_SYSTEM_CONFIG_ERROR_TYPE int32_t
-#define CHIP_SYSTEM_CONFIG_NO_ERROR 0
-#define CHIP_SYSTEM_CONFIG_ERROR_MIN 7000000
-#define CHIP_SYSTEM_CONFIG_ERROR_MAX 7000999
-#define _CHIP_SYSTEM_CONFIG_ERROR(e) (CHIP_SYSTEM_CONFIG_ERROR_MIN + (e))
-#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000000
-#define CHIP_SYSTEM_LWIP_ERROR_MAX 3000128
-
 // ========== Platform-specific Configuration Overrides =========
 
 #ifndef CHIP_SYSTEM_CONFIG_NUM_TIMERS

+ 0 - 8
src/platform/qpg/CHIPPlatformConfig.h

@@ -25,14 +25,6 @@
 
 // ==================== General Platform Adaptations ====================
 
-#define CHIP_CONFIG_ERROR_TYPE int32_t
-#define CHIP_CONFIG_ERROR_FORMAT PRId32
-#define CHIP_CONFIG_CORE_ERROR_MIN 4000000
-#define CHIP_CONFIG_CORE_ERROR_MAX 4000999
-
-#define ASN1_CONFIG_ERROR_MIN 5000000
-#define ASN1_CONFIG_ERROR_MAX 5000999
-
 #define ChipDie() abort()
 
 #define CHIP_CONFIG_ENABLE_TUNNELING 0

+ 0 - 3
src/platform/qpg/SystemPlatformConfig.h

@@ -38,9 +38,6 @@ struct ChipDeviceEvent;
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_TYPE int
 #define CHIP_SYSTEM_CONFIG_LWIP_EVENT_OBJECT_TYPE const struct ::chip::DeviceLayer::ChipDeviceEvent *
 
-#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000000
-#define CHIP_SYSTEM_LWIP_ERROR_MAX 3000128
-
 // ========== Platform-specific Configuration Overrides =========
 
 #ifndef CHIP_SYSTEM_CONFIG_NUM_TIMERS

+ 0 - 26
src/system/SystemConfig.h

@@ -418,32 +418,6 @@ struct LwIPEvent;
 
 #endif /* CHIP_SYSTEM_CONFIG_USE_LWIP */
 
-/**
- *  @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
- *
- *  @brief
- *      This defines whether (1) or not (0) your platform will provide the following platform- and system-specific functions:
- *      - chip::System::MapErrorPOSIX
- *      - chip::System::DescribeErrorPOSIX
- *      - chip::System::IsErrorPOSIX
- */
-#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
-#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS 0
-#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS */
-
-/**
- *  @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS
- *
- *  @brief
- *      This defines whether (1) or not (0) your platform will provide the following system-specific functions:
- *      - chip::System::MapErrorLwIP
- *      - chip::System::DescribeErrorLwIP
- *      - chip::System::IsErrorLwIP
- */
-#ifndef CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS
-#define CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS 0
-#endif /* CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS */
-
 /**
  *  @def CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_XTOR_FUNCTIONS
  *

+ 13 - 113
src/system/SystemError.cpp

@@ -40,68 +40,13 @@
 #include <lwip/err.h>
 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
 
-#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
-#include <string.h>
-#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
-
-#include <stddef.h>
-
 #include <limits>
-
-#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
-
-/**
- *  @def CHIP_SYSTEM_POSIX_ERROR_MIN
- *
- *  @brief
- *      This defines the base or minimum CHIP System Layer error number range, when passing through errors from an underlying
- *      POSIX layer.
- */
-#define CHIP_SYSTEM_POSIX_ERROR_MIN 2000
-
-/**
- *  @def CHIP_SYSTEM_POSIX_ERROR_MAX
- *
- *  @brief
- *      This defines the base or maximum CHIP System Layer error number range, when passing through errors from an underlying
- *      POSIX layer.
- */
-#define CHIP_SYSTEM_POSIX_ERROR_MAX 2999
-
-#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
-
-#if CHIP_SYSTEM_CONFIG_USE_LWIP
-#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS
-
-/**
- *  @def CHIP_SYSTEM_LWIP_ERROR_MIN
- *
- *  @brief
- *      This defines the base or minimum CHIP System Layer error number range, when passing through errors from an underlying LWIP
- *      stack.
- */
-#ifndef CHIP_SYSTEM_LWIP_ERROR_MIN
-#define CHIP_SYSTEM_LWIP_ERROR_MIN 3000
-#endif // CHIP_SYSTEM_LWIP_ERROR_MIN
-
-/**
- *  @def CHIP_SYSTEM_LWIP_ERROR_MAX
- *
- *  @brief
- *      This defines the base or maximum CHIP System Layer error number range, when passing through errors from an underlying LWIP
- *      layer.
- */
-#ifndef CHIP_SYSTEM_LWIP_ERROR_MAX
-#define CHIP_SYSTEM_LWIP_ERROR_MAX 3128
-#endif // CHIP_SYSTEM_LWIP_ERROR_MAX
-
-#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS
-#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
+#include <stddef.h>
+#include <string.h>
 
 namespace chip {
 namespace System {
 
-#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
 /**
  * This implements a mapping function for CHIP System Layer errors that allows mapping integers in the number space of the
  * underlying POSIX network and OS stack errors into a platform- or system-specific range. Error codes beyond those currently
@@ -113,7 +58,8 @@ namespace System {
  */
 DLL_EXPORT CHIP_ERROR MapErrorPOSIX(int aError)
 {
-    return (aError == 0 ? CHIP_NO_ERROR : static_cast<CHIP_ERROR>(CHIP_SYSTEM_POSIX_ERROR_MIN + aError));
+    return (aError == 0 ? CHIP_NO_ERROR
+                        : ChipError::Encapsulate(ChipError::Range::kPOSIX, static_cast<ChipError::BaseType>(aError)));
 }
 
 /**
@@ -126,25 +72,10 @@ DLL_EXPORT CHIP_ERROR MapErrorPOSIX(int aError)
  */
 DLL_EXPORT const char * DescribeErrorPOSIX(CHIP_ERROR aError)
 {
-    const int lError = (static_cast<int>(aError) - CHIP_SYSTEM_POSIX_ERROR_MIN);
+    const int lError = static_cast<int>(ChipError::GetValue(aError));
     return strerror(lError);
 }
 
-/**
- * This implements an introspection function for CHIP System Layer errors that allows the caller to determine whether the
- * specified error is an internal, underlying OS error.
- *
- *  @param[in] aError  The mapped error to determine whether it is an OS error.
- *
- *  @return True if the specified error is an OS error; otherwise, false.
- */
-DLL_EXPORT bool IsErrorPOSIX(CHIP_ERROR aError)
-{
-    return (aError >= CHIP_SYSTEM_POSIX_ERROR_MIN && aError <= CHIP_SYSTEM_POSIX_ERROR_MAX);
-}
-
-#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_POSIX_ERROR_FUNCTIONS
-
 /**
  * Register a text error formatter for POSIX errors.
  */
@@ -169,15 +100,13 @@ void RegisterPOSIXErrorFormatter()
  */
 bool FormatPOSIXError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
-    const CHIP_ERROR sysErr = static_cast<CHIP_ERROR>(err);
-
-    if (IsErrorPOSIX(sysErr))
+    if (ChipError::IsRange(ChipError::Range::kPOSIX, err))
     {
         const char * desc =
 #if CHIP_CONFIG_SHORT_ERROR_STR
             NULL;
 #else
-            DescribeErrorPOSIX(sysErr);
+            DescribeErrorPOSIX(err);
 #endif
         FormatError(buf, bufSize, "OS", err, desc);
         return true;
@@ -200,7 +129,6 @@ DLL_EXPORT CHIP_ERROR MapErrorZephyr(int aError)
 }
 
 #if CHIP_SYSTEM_CONFIG_USE_LWIP
-#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS
 
 /**
  * This implements a mapping function for CHIP System Layer errors that allows mapping underlying LwIP network stack errors into a
@@ -213,9 +141,8 @@ DLL_EXPORT CHIP_ERROR MapErrorZephyr(int aError)
  */
 DLL_EXPORT CHIP_ERROR MapErrorLwIP(err_t aError)
 {
-    static_assert(std::numeric_limits<err_t>::min() == CHIP_SYSTEM_LWIP_ERROR_MIN - CHIP_SYSTEM_LWIP_ERROR_MAX,
-                  "Can't represent all LWIP errors");
-    return (aError == ERR_OK ? CHIP_NO_ERROR : CHIP_SYSTEM_LWIP_ERROR_MIN - aError);
+    static_assert(ChipError::CanEncapsulate(-std::numeric_limits<err_t>::min()), "Can't represent all LWIP errors");
+    return (aError == ERR_OK ? CHIP_NO_ERROR : ChipError::Encapsulate(ChipError::Range::kLwIP, static_cast<unsigned int>(-aError)));
 }
 
 /**
@@ -229,20 +156,12 @@ DLL_EXPORT CHIP_ERROR MapErrorLwIP(err_t aError)
  */
 DLL_EXPORT const char * DescribeErrorLwIP(CHIP_ERROR aError)
 {
-    if (!IsErrorLwIP(aError))
+    if (!ChipError::IsRange(ChipError::Range::kLwIP, aError))
     {
         return nullptr;
     }
 
-    // Error might be a signed or unsigned type.  But we know the value is no
-    // larger than CHIP_SYSTEM_LWIP_ERROR_MAX and that this means it's safe to
-    // store in int.
-    static_assert(INT_MAX > CHIP_SYSTEM_LWIP_ERROR_MAX, "Our subtraction will fail");
-    const int lErrorWithoutOffset = aError - CHIP_SYSTEM_LWIP_ERROR_MIN;
-    // Cast is safe because the range from CHIP_SYSTEM_LWIP_ERROR_MIN to
-    // CHIP_SYSTEM_LWIP_ERROR_MAX all fits inside err_t.  See static_assert in
-    // MapErrorLwIP.
-    const err_t lError = static_cast<err_t>(-lErrorWithoutOffset);
+    const err_t lError = static_cast<err_t>(-static_cast<err_t>(ChipError::GetValue(aError)));
 
     // If we are not compiling with LWIP_DEBUG asserted, the unmapped
     // local value may go unused.
@@ -252,23 +171,6 @@ DLL_EXPORT const char * DescribeErrorLwIP(CHIP_ERROR aError)
     return lwip_strerr(lError);
 }
 
-/**
- * This implements an introspection function for CHIP System Layer errors that
- * allows the caller to determine whether the specified error is an
- * internal, underlying LwIP error.
- *
- *  @param[in] aError  The mapped error to determine whether it is a LwIP error.
- *
- *  @return True if the specified error is a LwIP error; otherwise, false.
- *
- */
-DLL_EXPORT bool IsErrorLwIP(CHIP_ERROR aError)
-{
-    return (aError >= CHIP_SYSTEM_LWIP_ERROR_MIN && aError <= CHIP_SYSTEM_LWIP_ERROR_MAX);
-}
-
-#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_LWIP_ERROR_FUNCTIONS
-
 /**
  * Register a text error formatter for LwIP errors.
  */
@@ -293,15 +195,13 @@ void RegisterLwIPErrorFormatter(void)
  */
 bool FormatLwIPError(char * buf, uint16_t bufSize, CHIP_ERROR err)
 {
-    const CHIP_ERROR sysErr = static_cast<CHIP_ERROR>(err);
-
-    if (IsErrorLwIP(sysErr))
+    if (ChipError::IsRange(ChipError::Range::kLwIP, err))
     {
         const char * desc =
 #if CHIP_CONFIG_SHORT_ERROR_STR
             NULL;
 #else
-            DescribeErrorLwIP(sysErr);
+            DescribeErrorLwIP(err);
 #endif
         chip::FormatError(buf, bufSize, "LwIP", err, desc);
         return true;

+ 3 - 5
src/system/SystemError.h

@@ -44,21 +44,19 @@
 namespace chip {
 namespace System {
 
-extern void RegisterLayerErrorFormatter();
-extern bool FormatLayerError(char * buf, uint16_t bufSize, CHIP_ERROR err);
-
 extern CHIP_ERROR MapErrorPOSIX(int code);
 extern const char * DescribeErrorPOSIX(CHIP_ERROR code);
-extern bool IsErrorPOSIX(CHIP_ERROR code);
 extern void RegisterPOSIXErrorFormatter();
 extern bool FormatPOSIXError(char * buf, uint16_t bufSize, CHIP_ERROR err);
+
+#if __ZEPHYR__
 extern CHIP_ERROR MapErrorZephyr(int code);
+#endif // __ZEPHYR__
 
 #if CHIP_SYSTEM_CONFIG_USE_LWIP
 
 extern CHIP_ERROR MapErrorLwIP(err_t code);
 extern const char * DescribeErrorLwIP(CHIP_ERROR code);
-extern bool IsErrorLwIP(CHIP_ERROR code);
 extern void RegisterLwIPErrorFormatter(void);
 extern bool FormatLwIPError(char * buf, uint16_t bufSize, CHIP_ERROR err);
 

Some files were not shown because too many files changed in this diff