/
ota_os_freertos.c
360 lines (294 loc) · 10.1 KB
/
ota_os_freertos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* AWS IoT Over-the-air Update v3.4.0
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file ota_os_freertos.c
* @brief Example implementation of the OTA OS Functional Interface for
* FreeRTOS.
*/
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "timers.h"
#include "queue.h"
/* OTA OS POSIX Interface Includes.*/
#include "ota_os_freertos.h"
/* OTA Library include. */
#include "ota.h"
#include "ota_private.h"
/* OTA Event queue attributes.*/
#define MAX_MESSAGES 20
#define MAX_MSG_SIZE sizeof( OtaEventMsg_t )
/* Array containing pointer to the OTA event structures used to send events to the OTA task. */
static OtaEventMsg_t queueData[ MAX_MESSAGES * MAX_MSG_SIZE ];
/* The queue control structure. .*/
static StaticQueue_t staticQueue;
/* The queue control handle. .*/
static QueueHandle_t otaEventQueue;
/* OTA App Timer callback.*/
static OtaTimerCallback_t otaTimerCallbackPtr;
/* OTA Timer handles.*/
static TimerHandle_t otaTimer[ OtaNumOfTimers ];
/* OTA Timer callbacks.*/
static void requestTimerCallback( TimerHandle_t T );
static void selfTestTimerCallback( TimerHandle_t T );
void ( * timerCallback[ OtaNumOfTimers ] )( TimerHandle_t T ) = { requestTimerCallback, selfTestTimerCallback };
OtaOsStatus_t OtaInitEvent_FreeRTOS( OtaEventContext_t * pEventCtx )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
( void ) pEventCtx;
otaEventQueue = xQueueCreateStatic( ( UBaseType_t ) MAX_MESSAGES,
( UBaseType_t ) MAX_MSG_SIZE,
( uint8_t * ) queueData,
&staticQueue );
if( otaEventQueue == NULL )
{
otaOsStatus = OtaOsEventQueueCreateFailed;
LogError( ( "Failed to create OTA Event Queue: "
"xQueueCreateStatic returned error: "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
}
else
{
LogDebug( ( "OTA Event Queue created." ) );
}
return otaOsStatus;
}
OtaOsStatus_t OtaSendEvent_FreeRTOS( OtaEventContext_t * pEventCtx,
const void * pEventMsg,
unsigned int timeout )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
BaseType_t retVal = pdFALSE;
( void ) pEventCtx;
( void ) timeout;
/* Send the event to OTA event queue.*/
retVal = xQueueSendToBack( otaEventQueue, pEventMsg, ( TickType_t ) 0 );
if( retVal == pdTRUE )
{
LogDebug( ( "OTA Event Sent." ) );
}
else
{
otaOsStatus = OtaOsEventQueueSendFailed;
LogError( ( "Failed to send event to OTA Event Queue: "
"xQueueSendToBack returned error: "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
}
return otaOsStatus;
}
OtaOsStatus_t OtaReceiveEvent_FreeRTOS( OtaEventContext_t * pEventCtx,
void * pEventMsg,
uint32_t timeout )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
BaseType_t retVal = pdFALSE;
/* Temp buffer.*/
uint8_t buff[ sizeof( OtaEventMsg_t ) ];
( void ) pEventCtx;
retVal = xQueueReceive( otaEventQueue, &buff, pdMS_TO_TICKS( timeout ) );
if( retVal == pdTRUE )
{
/* copy the data from local buffer.*/
memcpy( pEventMsg, buff, MAX_MSG_SIZE );
LogDebug( ( "OTA Event received" ) );
}
else
{
otaOsStatus = OtaOsEventQueueReceiveFailed;
LogDebug( ( "Failed to receive event or timeout from OTA Event Queue: "
"xQueueReceive returned error: "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
}
return otaOsStatus;
}
OtaOsStatus_t OtaDeinitEvent_FreeRTOS( OtaEventContext_t * pEventCtx )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
( void ) pEventCtx;
/* Remove the event queue.*/
if( otaEventQueue != NULL )
{
vQueueDelete( otaEventQueue );
LogDebug( ( "OTA Event Queue Deleted." ) );
}
return otaOsStatus;
}
static void selfTestTimerCallback( TimerHandle_t T )
{
( void ) T;
LogDebug( ( "Self-test expired within %ums",
( unsigned ) otaconfigSELF_TEST_RESPONSE_WAIT_MS ) );
if( otaTimerCallbackPtr != NULL )
{
otaTimerCallbackPtr( OtaSelfTestTimer );
}
else
{
LogWarn( ( "Self-test timer event unhandled." ) );
}
}
static void requestTimerCallback( TimerHandle_t T )
{
( void ) T;
LogDebug( ( "Request timer expired in %ums",
( unsigned ) otaconfigFILE_REQUEST_WAIT_MS ) );
if( otaTimerCallbackPtr != NULL )
{
otaTimerCallbackPtr( OtaRequestTimer );
}
else
{
LogWarn( ( "Request timer event unhandled." ) );
}
}
OtaOsStatus_t OtaStartTimer_FreeRTOS( OtaTimerId_t otaTimerId,
const char * const pTimerName,
const uint32_t timeout,
OtaTimerCallback_t callback )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
BaseType_t retVal = pdFALSE;
configASSERT( callback != NULL );
configASSERT( pTimerName != NULL );
/* Set OTA lib callback. */
otaTimerCallbackPtr = callback;
/* If timer is not created.*/
if( otaTimer[ otaTimerId ] == NULL )
{
/* Create the timer. */
otaTimer[ otaTimerId ] = xTimerCreate( pTimerName,
pdMS_TO_TICKS( timeout ),
pdFALSE,
NULL,
timerCallback[ otaTimerId ] );
if( otaTimer[ otaTimerId ] == NULL )
{
otaOsStatus = OtaOsTimerCreateFailed;
LogError( ( "Failed to create OTA timer: "
"timerCreate returned NULL "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
}
else
{
LogDebug( ( "OTA Timer created." ) );
/* Start the timer. */
retVal = xTimerStart( otaTimer[ otaTimerId ], portMAX_DELAY );
if( retVal == pdTRUE )
{
LogDebug( ( "OTA Timer started." ) );
}
else
{
otaOsStatus = OtaOsTimerStartFailed;
LogError( ( "Failed to start OTA timer: "
"timerStart returned error." ) );
}
}
}
else
{
/* Reset the timer. */
retVal = xTimerReset( otaTimer[ otaTimerId ], portMAX_DELAY );
if( retVal == pdTRUE )
{
LogDebug( ( "OTA Timer restarted." ) );
}
else
{
otaOsStatus = OtaOsTimerRestartFailed;
LogError( ( "Failed to set OTA timer timeout: "
"timer_settime returned error: "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
}
}
return otaOsStatus;
}
OtaOsStatus_t OtaStopTimer_FreeRTOS( OtaTimerId_t otaTimerId )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
BaseType_t retVal = pdFALSE;
if( otaTimer[ otaTimerId ] != NULL )
{
/* Stop the timer. */
retVal = xTimerStop( otaTimer[ otaTimerId ], portMAX_DELAY );
if( retVal == pdTRUE )
{
LogDebug( ( "OTA Timer Stopped for Timerid=%d.", ( int ) otaTimerId ) );
}
else
{
LogError( ( "Failed to stop OTA timer: "
"timer_settime returned error: "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
otaOsStatus = OtaOsTimerStopFailed;
}
}
else
{
LogWarn( ( "OTA Timer handle NULL for Timerid=%d, can't stop.", ( int ) otaTimerId ) );
}
return otaOsStatus;
}
OtaOsStatus_t OtaDeleteTimer_FreeRTOS( OtaTimerId_t otaTimerId )
{
OtaOsStatus_t otaOsStatus = OtaOsSuccess;
BaseType_t retVal = pdFALSE;
if( otaTimer[ otaTimerId ] != NULL )
{
/* Delete the timer. */
retVal = xTimerDelete( otaTimer[ otaTimerId ], portMAX_DELAY );
if( retVal == pdTRUE )
{
otaTimer[ otaTimerId ] = NULL;
LogDebug( ( "OTA Timer deleted." ) );
}
else
{
otaOsStatus = OtaOsTimerDeleteFailed;
LogError( ( "Failed to delete OTA timer: "
"timer_delete returned error: "
"OtaOsStatus_t=%d ",
( int ) otaOsStatus ) );
}
}
else
{
otaOsStatus = OtaOsTimerDeleteFailed;
LogWarn( ( "OTA Timer handle NULL for Timerid=%d, can't delete.", ( int ) otaTimerId ) );
}
return otaOsStatus;
}
void * Malloc_FreeRTOS( size_t size )
{
return pvPortMalloc( size );
}
void Free_FreeRTOS( void * ptr )
{
vPortFree( ptr );
}