diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h index cdb5b19e20d077..c051d9c78ddce0 100644 --- a/src/include/platform/PlatformManager.h +++ b/src/include/platform/PlatformManager.h @@ -90,6 +90,7 @@ class PlatformManager void LockChipStack(void); bool TryLockChipStack(void); void UnlockChipStack(void); + CHIP_ERROR Shutdown(void); private: // ===== Members for internal use by the following friends. @@ -234,6 +235,11 @@ inline CHIP_ERROR PlatformManager::StartChipTimer(uint32_t durationMS) return static_cast(this)->_StartChipTimer(durationMS); } +inline CHIP_ERROR PlatformManager::Shutdown(void) +{ + return static_cast(this)->_Shutdown(); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.h b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.h index 23844757a7aea1..dddff8d6ba2939 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.h +++ b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.h @@ -72,6 +72,7 @@ class GenericPlatformManagerImpl_FreeRTOS : public GenericPlatformManagerImpl #include +#include #include // Include the non-inline definitions for the GenericPlatformManagerImpl<> template, // from which the GenericPlatformManagerImpl_FreeRTOS<> template inherits. #include - namespace chip { namespace DeviceLayer { namespace Internal { @@ -41,15 +40,15 @@ namespace Internal { // Fully instantiate the generic implementation class in whatever compilation unit includes this file. template class GenericPlatformManagerImpl_FreeRTOS; -template +template CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS::_InitChipStack(void) { CHIP_ERROR err = CHIP_NO_ERROR; vTaskSetTimeOutState(&mNextTimerBaseTime); mNextTimerDurationTicks = 0; - mEventLoopTask = NULL; - mChipTimerActive = false; + mEventLoopTask = NULL; + mChipTimerActive = false; mChipStackLock = xSemaphoreCreateMutex(); if (mChipStackLock == NULL) @@ -73,25 +72,25 @@ exit: return err; } -template +template void GenericPlatformManagerImpl_FreeRTOS::_LockChipStack(void) { xSemaphoreTake(mChipStackLock, portMAX_DELAY); } -template +template bool GenericPlatformManagerImpl_FreeRTOS::_TryLockChipStack(void) { return xSemaphoreTake(mChipStackLock, 0) == pdTRUE; } -template +template void GenericPlatformManagerImpl_FreeRTOS::_UnlockChipStack(void) { xSemaphoreGive(mChipStackLock); } -template +template void GenericPlatformManagerImpl_FreeRTOS::_PostEvent(const ChipDeviceEvent * event) { if (mChipEventQueue != NULL) @@ -103,7 +102,7 @@ void GenericPlatformManagerImpl_FreeRTOS::_PostEvent(const ChipDevice } } -template +template void GenericPlatformManagerImpl_FreeRTOS::_RunEventLoop(void) { CHIP_ERROR err; @@ -180,29 +179,26 @@ void GenericPlatformManagerImpl_FreeRTOS::_RunEventLoop(void) } } -template +template CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS::_StartEventLoopTask(void) { BaseType_t res; - res = xTaskCreate(EventLoopTaskMain, - CHIP_DEVICE_CONFIG_CHIP_TASK_NAME, - CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE / sizeof(StackType_t), - this, - CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY, - NULL); + res = xTaskCreate(EventLoopTaskMain, CHIP_DEVICE_CONFIG_CHIP_TASK_NAME, + CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE / sizeof(StackType_t), this, CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY, + NULL); return (res == pdPASS) ? CHIP_NO_ERROR : CHIP_ERROR_NO_MEMORY; } -template +template void GenericPlatformManagerImpl_FreeRTOS::EventLoopTaskMain(void * arg) { ChipLogDetail(DeviceLayer, "CHIP task running"); - static_cast*>(arg)->Impl()->RunEventLoop(); + static_cast *>(arg)->Impl()->RunEventLoop(); } -template +template CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS::_StartChipTimer(uint32_t aMilliseconds) { mChipTimerActive = true; @@ -222,7 +218,7 @@ CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS::_StartChipTimer(uint3 return CHIP_NO_ERROR; } -template +template void GenericPlatformManagerImpl_FreeRTOS::PostEventFromISR(const ChipDeviceEvent * event, BaseType_t & yieldRequired) { yieldRequired = pdFALSE; @@ -236,6 +232,12 @@ void GenericPlatformManagerImpl_FreeRTOS::PostEventFromISR(const Chip } } +template +CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS::_Shutdown(void) +{ + return CHIP_ERROR_NOT_IMPLEMENTED; +} + } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_POSIX.h b/src/include/platform/internal/GenericPlatformManagerImpl_POSIX.h index 78391e359af0a2..63535d486222ca 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl_POSIX.h +++ b/src/include/platform/internal/GenericPlatformManagerImpl_POSIX.h @@ -78,6 +78,7 @@ class GenericPlatformManagerImpl_POSIX : public GenericPlatformManagerImpl::_InitChipStack(void) err = GenericPlatformManagerImpl::_InitChipStack(); SuccessOrExit(err); + mShouldRunEventLoop = true; + exit: return err; } @@ -190,20 +192,22 @@ void GenericPlatformManagerImpl_POSIX::_RunEventLoop(void) { Impl()->LockChipStack(); - // TODO(#742): add exit condition - while (true) + do { SysUpdate(); SysProcess(); - } + } while (mShouldRunEventLoop); Impl()->UnlockChipStack(); + + pthread_exit(0); } template void * GenericPlatformManagerImpl_POSIX::EventLoopTaskMain(void * arg) { ChipLogDetail(DeviceLayer, "CHIP task running"); + static_cast *>(arg)->Impl()->RunEventLoop(); return NULL; } @@ -224,6 +228,18 @@ exit: return System::MapErrorPOSIX(err); } +template +CHIP_ERROR GenericPlatformManagerImpl_POSIX::_Shutdown(void) +{ + int err; + mShouldRunEventLoop = false; + err = pthread_join(mChipTask, NULL); + SuccessOrExit(err); + +exit: + return System::MapErrorPOSIX(err); +} + // Fully instantiate the generic implementation class in whatever compilation unit includes this file. template class GenericPlatformManagerImpl_POSIX;