@@ -42,34 +42,26 @@ namespace Internal {
42
42
template <class ImplClass >
43
43
CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_InitChipStack(void )
44
44
{
45
- CHIP_ERROR err = CHIP_NO_ERROR;
46
-
47
45
mNextTimerBaseTime = osKernelGetTickCount ();
48
46
mNextTimerDurationTicks = 0 ;
49
47
mChipTimerActive = false ;
50
48
51
49
// 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
54
52
// already exist here before trying to create them.
55
53
if (mChipStackLock == nullptr )
56
54
{
57
55
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" ));
63
58
}
64
59
65
60
if (mChipEventQueue == nullptr )
66
61
{
67
62
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" ));
73
65
}
74
66
else
75
67
{
@@ -85,11 +77,8 @@ CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_InitChipStack(void)
85
77
{
86
78
mBackgroundEventQueue =
87
79
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" ));
93
82
}
94
83
else
95
84
{
@@ -100,11 +89,7 @@ CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_InitChipStack(void)
100
89
#endif
101
90
102
91
// 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 ();
108
93
}
109
94
110
95
template <class ImplClass >
@@ -141,7 +126,7 @@ bool GenericPlatformManagerImpl_CMSISOS<ImplClass>::_IsChipStackLockedByCurrentT
141
126
template <class ImplClass >
142
127
CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_PostEvent(const ChipDeviceEvent * event)
143
128
{
144
- VerifyOrReturnError (mChipEventQueue != nullptr , CHIP_ERROR_INTERNAL );
129
+ VerifyOrReturnError (mChipEventQueue != nullptr , CHIP_ERROR_UNINITIALIZED );
145
130
146
131
osStatus_t status = osMessageQueuePut (mChipEventQueue , event, osPriorityNormal, 1 );
147
132
if (status != osOK)
@@ -155,9 +140,6 @@ CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_PostEvent(const ChipD
155
140
template <class ImplClass >
156
141
void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void )
157
142
{
158
- CHIP_ERROR err;
159
- ChipDeviceEvent event;
160
-
161
143
// Lock the CHIP stack.
162
144
StackLock lock;
163
145
@@ -177,7 +159,7 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
177
159
{
178
160
// Adjust the base time and remaining duration for the next scheduled timer based on the
179
161
// 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
181
163
// else wait for a queue event for timer remaining time.
182
164
waitTimeInTicks = SyncNextChipTimerHandling ();
183
165
if (waitTimeInTicks == 0 )
@@ -188,9 +170,9 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
188
170
189
171
// Call into the system layer to dispatch the callback functions for all timers
190
172
// 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
192
174
// it.
193
- err = static_cast <System::LayerImplFreeRTOS &>(DeviceLayer::SystemLayer ()).HandlePlatformTimer ();
175
+ CHIP_ERROR err = static_cast <System::LayerImplFreeRTOS &>(DeviceLayer::SystemLayer ()).HandlePlatformTimer ();
194
176
if (err != CHIP_NO_ERROR)
195
177
{
196
178
ChipLogError (DeviceLayer, " Error handling CHIP timers: %" CHIP_ERROR_FORMAT, err.Format ());
@@ -199,14 +181,15 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunEventLoop(void)
199
181
}
200
182
else
201
183
{
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
203
185
// queue.
204
186
waitTimeInTicks = osWaitForever;
205
187
}
206
188
207
189
// Unlock the CHIP stack, allowing other threads to enter CHIP while
208
190
// the event loop thread is sleeping.
209
191
StackUnlock unlock;
192
+ ChipDeviceEvent event;
210
193
osStatus_t eventReceived = osMessageQueueGet (mChipEventQueue , &event, nullptr , waitTimeInTicks);
211
194
212
195
// 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
236
219
237
220
/* *
238
221
* @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
240
223
*
241
224
* @return The next Timer remaining time in ticks
242
225
*/
243
226
template <class ImplClass >
244
227
uint32_t GenericPlatformManagerImpl_CMSISOS<ImplClass>::SyncNextChipTimerHandling()
245
228
{
246
- uint32_t elapsedTime;
229
+ uint32_t elapsedTime = 0 ;
247
230
uint32_t timerBaseTime = mNextTimerBaseTime ;
248
231
uint32_t currentTime = osKernelGetTickCount ();
249
232
if (currentTime < timerBaseTime)
@@ -273,21 +256,15 @@ template <class ImplClass>
273
256
CHIP_ERROR GenericPlatformManagerImpl_CMSISOS<ImplClass>::_PostBackgroundEvent(const ChipDeviceEvent * event)
274
257
{
275
258
#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);
280
260
if (!(event->Type == DeviceEventType::kCallWorkFunct || event->Type == DeviceEventType::kNoOp ))
281
261
{
282
262
return CHIP_ERROR_INVALID_ARGUMENT;
283
263
}
284
264
285
265
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" ));
291
268
return CHIP_NO_ERROR;
292
269
#else
293
270
// Use foreground event loop for background events
@@ -300,11 +277,9 @@ void GenericPlatformManagerImpl_CMSISOS<ImplClass>::_RunBackgroundEventLoop(void
300
277
{
301
278
#if defined(CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING) && CHIP_DEVICE_CONFIG_ENABLE_BG_EVENT_PROCESSING
302
279
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" ));
308
283
309
284
while (mShouldRunBackgroundEventLoop .load ())
310
285
{
0 commit comments