Skip to content

Commit a39d6b3

Browse files
Address comments
1 parent 408f30c commit a39d6b3

File tree

2 files changed

+30
-55
lines changed

2 files changed

+30
-55
lines changed

src/include/platform/internal/GenericPlatformManagerImpl_CMSISOS.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ class GenericPlatformManagerImpl_CMSISOS : public GenericPlatformManagerImpl<Imp
4747
{
4848

4949
protected:
50-
osMutexId_t mChipStackLock = NULL;
51-
osMessageQueueId_t mChipEventQueue = NULL;
52-
osThreadId_t mEventLoopTask = NULL;
50+
osMutexId_t mChipStackLock = nullptr;
51+
osMessageQueueId_t mChipEventQueue = nullptr;
52+
osThreadId_t mEventLoopTask = nullptr;
5353
bool mChipTimerActive;
5454

5555
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
56-
osMessageQueueId_t mBackgroundEventQueue = NULL;
57-
osThreadId_t mBackgroundEventLoopTask = NULL;
56+
osMessageQueueId_t mBackgroundEventQueue = nullptr;
57+
osThreadId_t mBackgroundEventLoopTask = nullptr;
5858
#endif
5959

6060
// ===== Methods that implement the PlatformManager abstract interface.
@@ -90,8 +90,8 @@ class GenericPlatformManagerImpl_CMSISOS : public GenericPlatformManagerImpl<Imp
9090

9191
static void EventLoopTaskMain(void * pvParameter);
9292
uint32_t SyncNextChipTimerHandling();
93-
uint32_t mNextTimerBaseTime;
94-
uint32_t mNextTimerDurationTicks;
93+
uint32_t mNextTimerBaseTime = 0;
94+
uint32_t mNextTimerDurationTicks = 0;
9595
std::atomic<bool> mShouldRunEventLoop;
9696

9797
#if defined(CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE) && CHIP_CONFIG_CMSISOS_USE_STATIC_QUEUE

src/include/platform/internal/GenericPlatformManagerImpl_CMSISOS.ipp

+23-48
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,26 @@ namespace Internal {
4242
template <class ImplClass>
4343
CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_InitChipStack(void)
4444
{
45-
CHIP_ERROR err = CHIP_NO_ERROR;
46-
4745
mNextTimerBaseTime = osKernelGetTickCount();
4846
mNextTimerDurationTicks = 0;
4947
mChipTimerActive = false;
5048

5149
// We support calling Shutdown followed by InitChipStack, because some tests
52-
// do that. To keep things simple for existing consumers, we keep not
53-
// destroying our lock and queue in shutdown, but rather check whether they
50+
// do that. To keep things simple for existing consumers, we do not
51+
// destroy our lock and queue at shutdown, but rather check whether they
5452
// already exist here before trying to create them.
5553
if (mChipStackLock == nullptr)
5654
{
5755
mChipStackLock = osMutexNew(&mChipStackMutexAttr);
58-
if (mChipStackLock == nullptr)
59-
{
60-
ChipLogError(DeviceLayer, "Failed to create CHIP stack lock");
61-
ExitNow(err = CHIP_ERROR_NO_MEMORY);
62-
}
56+
VerifyOrReturnError(mChipStackLock != nullptr, CHIP_ERROR_NO_MEMORY,
57+
ChipLogError(DeviceLayer, "Failed to create CHIP stack lock"));
6358
}
6459

6560
if (mChipEventQueue == nullptr)
6661
{
6762
mChipEventQueue = osMessageQueueNew(CHIP_DEVICE_CONFIG_MAX_EVENT_QUEUE_SIZE, sizeof(ChipDeviceEvent), mEventQueueAttrPtr);
68-
if (mChipEventQueue == nullptr)
69-
{
70-
ChipLogError(DeviceLayer, "Failed to allocate CHIP main event queue");
71-
ExitNow(err = CHIP_ERROR_NO_MEMORY);
72-
}
63+
VerifyOrReturnError(mChipEventQueue != nullptr, CHIP_ERROR_NO_MEMORY,
64+
ChipLogError(DeviceLayer, "Failed to allocate CHIP main event queue"));
7365
}
7466
else
7567
{
@@ -85,11 +77,8 @@ CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_InitChipStack(void)
8577
{
8678
mBackgroundEventQueue =
8779
osMessageQueueNew(CHIP_DEVICE_CONFIG_BG_MAX_EVENT_QUEUE_SIZE, sizeof(ChipDeviceEvent), mBgQueueAttrPtr);
88-
if (mBackgroundEventQueue == nullptr)
89-
{
90-
ChipLogError(DeviceLayer, "Failed to allocate CHIP background event queue");
91-
ExitNow(err = CHIP_ERROR_NO_MEMORY);
92-
}
80+
VerifyOrReturnError(mBackgroundEventQueue != nullptr, CHIP_ERROR_NO_MEMORY,
81+
ChipLogError(DeviceLayer, "Failed to allocate CHIP background event queue"));
9382
}
9483
else
9584
{
@@ -100,11 +89,7 @@ CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_InitChipStack(void)
10089
#endif
10190

10291
// Call up to the base class _InitChipStack() to perform the bulk of the initialization.
103-
err = GenericPlatformManagerImpl<ImplClass>::_InitChipStack();
104-
SuccessOrExit(err);
105-
106-
exit:
107-
return err;
92+
return GenericPlatformManagerImpl<ImplClass>::_InitChipStack();
10893
}
10994

11095
template <class ImplClass>
@@ -141,7 +126,7 @@ bool GenericPlatformManagerImpl_CMSISOS<ImplClass>::_IsChipStackLockedByCurrentT
141126
template <class ImplClass>
142127
CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_PostEvent(const ChipDeviceEvent * event)
143128
{
144-
VerifyOrReturnError(mChipEventQueue != nullptr, CHIP_ERROR_INTERNAL);
129+
VerifyOrReturnError(mChipEventQueue != nullptr, CHIP_ERROR_UNINITIALIZED);
145130

146131
osStatus_t status = osMessageQueuePut(mChipEventQueue, event, osPriorityNormal, 1);
147132
if (status != osOK)
@@ -155,9 +140,6 @@ CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_PostEvent(const ChipD
155140
template <class ImplClass>
156141
void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
157142
{
158-
CHIP_ERROR err;
159-
ChipDeviceEvent event;
160-
161143
// Lock the CHIP stack.
162144
StackLock lock;
163145

@@ -177,7 +159,7 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
177159
{
178160
// Adjust the base time and remaining duration for the next scheduled timer based on the
179161
// amount of time that has elapsed since it was started.
180-
// When the timer's expiration time elapsed, Handle the platform Timer
162+
// When the timer's expiration time elapses, Handle the platform Timer
181163
// else wait for a queue event for timer remaining time.
182164
waitTimeInTicks = SyncNextChipTimerHandling();
183165
if (waitTimeInTicks == 0)
@@ -188,9 +170,9 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
188170

189171
// Call into the system layer to dispatch the callback functions for all timers
190172
// that have expired.
191-
// TODO We use the same SystemLayer implementation than FreeRTOS, Nothing in it is freeRTOS specific. Maybe rename
173+
// TODO We use the same SystemLayer implementation as FreeRTOS, Nothing in it is freeRTOS specific. We should
192174
// it.
193-
err = static_cast<System::LayerImplFreeRTOS &>(DeviceLayer::SystemLayer()).HandlePlatformTimer();
175+
CHIP_ERROR err = static_cast<System::LayerImplFreeRTOS &>(DeviceLayer::SystemLayer()).HandlePlatformTimer();
194176
if (err != CHIP_NO_ERROR)
195177
{
196178
ChipLogError(DeviceLayer, "Error handling CHIP timers: %" CHIP_ERROR_FORMAT, err.Format());
@@ -199,14 +181,15 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
199181
}
200182
else
201183
{
202-
// No CHIP timers are active, so wait indefinitely for an event to arrive on the event
184+
// No CHIP timers are active, so we wait indefinitely for an event to arrive on the event
203185
// queue.
204186
waitTimeInTicks = osWaitForever;
205187
}
206188

207189
// Unlock the CHIP stack, allowing other threads to enter CHIP while
208190
// the event loop thread is sleeping.
209191
StackUnlock unlock;
192+
ChipDeviceEvent event;
210193
osStatus_t eventReceived = osMessageQueueGet(mChipEventQueue, &event, nullptr, waitTimeInTicks);
211194

212195
// If an event was received, dispatch it and continue until the queue is empty.
@@ -236,14 +219,14 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::EventLoopTaskMain(void * pvP
236219

237220
/**
238221
* @brief Calculate the elapsed time of the active chip platform timer since it has been started,
239-
* as set in mNextTimerBaseTime, and adjust its remaining time in mNextTimerDurationTicks member
222+
* as set in mNextTimerBaseTime, and adjust its remaining time with the mNextTimerDurationTicks member
240223
*
241224
* @return The next Timer remaining time in ticks
242225
*/
243226
template <class ImplClass>
244227
uint32_t GenericPlatformManagerImpl_CMSISOS<ImplClass>::SyncNextChipTimerHandling()
245228
{
246-
uint32_t elapsedTime;
229+
uint32_t elapsedTime = 0;
247230
uint32_t timerBaseTime = mNextTimerBaseTime;
248231
uint32_t currentTime = osKernelGetTickCount();
249232
if (currentTime < timerBaseTime)
@@ -273,21 +256,15 @@ template <class ImplClass>
273256
CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_PostBackgroundEvent(const ChipDeviceEvent * event)
274257
{
275258
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
276-
if (mBackgroundEventQueue == NULL)
277-
{
278-
return CHIP_ERROR_INTERNAL;
279-
}
259+
VerifyOrReturnError(mBackgroundEventQueue != nullptr, CHIP_ERROR_UNINITIALIZED);
280260
if (!(event->Type == DeviceEventType::kCallWorkFunct || event->Type == DeviceEventType::kNoOp))
281261
{
282262
return CHIP_ERROR_INVALID_ARGUMENT;
283263
}
284264

285265
osStatus_t status = osMessageQueuePut(mBackgroundEventQueue, event, osPriorityNormal, 1);
286-
if (status != osOK)
287-
{
288-
ChipLogError(DeviceLayer, "Failed to post event to CHIP background event queue");
289-
return CHIP_ERROR_NO_MEMORY;
290-
}
266+
VerifyOrReturnError(status == osOk, CHIP_ERROR_NO_MEMORY,
267+
ChipLogError(DeviceLayer, "Failed to post event to CHIP background event queue"));
291268
return CHIP_NO_ERROR;
292269
#else
293270
// Use foreground event loop for background events
@@ -300,11 +277,9 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunBackgroundEventLoop(void
300277
{
301278
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
302279
bool oldShouldRunBackgroundEventLoop = false;
303-
if (!mShouldRunBackgroundEventLoop.compare_exchange_strong(oldShouldRunBackgroundEventLoop /* expected */, true /* desired */))
304-
{
305-
ChipLogError(DeviceLayer, "Error trying to run the background event loop while it is already running");
306-
return;
307-
}
280+
VerifyOrReturn(
281+
mShouldRunBackgroundEventLoop.compare_exchange_strong(oldShouldRunBackgroundEventLoop /* expected */, true /* desired */),
282+
ChipLogError(DeviceLayer, "Error trying to run the background event loop while it is already running"));
308283

309284
while (mShouldRunBackgroundEventLoop.load())
310285
{

0 commit comments

Comments
 (0)