/
ota_demo.c
528 lines (455 loc) · 16.6 KB
/
ota_demo.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright Amazon.com, Inc. and its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT
*
* Licensed under the MIT License. See the LICENSE accompanying this file
* for the specific language governing permissions and limitations under
* the License.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "MQTTFileDownloader.h"
#include "jobs.h"
#include "mqtt_wrapper.h"
#include "ota_demo.h"
#include "ota_job_processor.h"
#include "os/ota_os_freertos.h"
#include "FreeRTOS.h"
#include "semphr.h"
#define CONFIG_MAX_FILE_SIZE 65536U
#define NUM_OF_BLOCKS_REQUESTED 1U
#define START_JOB_MSG_LENGTH 147U
#define MAX_THING_NAME_SIZE 128U
#define MAX_JOB_ID_LENGTH 64U
#define UPDATE_JOB_MSG_LENGTH 48U
#define MAX_NUM_OF_OTA_DATA_BUFFERS 5U
MqttFileDownloaderContext_t mqttFileDownloaderContext = { 0 };
static uint32_t numOfBlocksRemaining = 0;
static uint32_t currentBlockOffset = 0;
static uint8_t currentFileId = 0;
static uint32_t totalBytesReceived = 0;
static uint8_t downloadedData[ CONFIG_MAX_FILE_SIZE ] = { 0 };
char globalJobId[ MAX_JOB_ID_LENGTH ] = { 0 };
static OtaDataEvent_t dataBuffers[MAX_NUM_OF_OTA_DATA_BUFFERS] = { 0 };
static OtaJobEventData_t jobDocBuffer = { 0 };
static SemaphoreHandle_t bufferSemaphore;
static OtaState_t otaAgentState = OtaAgentStateInit;
static void finishDownload( void );
static void processOTAEvents( void );
static void requestJobDocumentHandler( void );
static bool receivedJobDocumentHandler( OtaJobEventData_t * jobDoc );
static bool jobDocumentParser( char * message, size_t messageLength, AfrOtaJobDocumentFields_t *jobFields );
static void initMqttDownloader( AfrOtaJobDocumentFields_t *jobFields );
static OtaDataEvent_t * getOtaDataEventBuffer( void );
static void freeOtaDataEventBuffer( OtaDataEvent_t * const buffer );
static void handleMqttStreamsBlockArrived( uint8_t *data, size_t dataLength );
static void requestDataBlock( void );
static void freeOtaDataEventBuffer( OtaDataEvent_t * const pxBuffer )
{
if( xSemaphoreTake( bufferSemaphore, portMAX_DELAY ) == pdTRUE )
{
pxBuffer->bufferUsed = false;
( void ) xSemaphoreGive( bufferSemaphore );
}
else
{
printf( "Failed to get buffer semaphore.\n" );
}
}
static OtaDataEvent_t * getOtaDataEventBuffer( void )
{
uint32_t ulIndex = 0;
OtaDataEvent_t * freeBuffer = NULL;
if( xSemaphoreTake( bufferSemaphore, portMAX_DELAY ) == pdTRUE )
{
for( ulIndex = 0; ulIndex < MAX_NUM_OF_OTA_DATA_BUFFERS; ulIndex++ )
{
if( dataBuffers[ ulIndex ].bufferUsed == false )
{
dataBuffers[ ulIndex ].bufferUsed = true;
freeBuffer = &dataBuffers[ ulIndex ];
break;
}
}
( void ) xSemaphoreGive( bufferSemaphore );
}
else
{
printf("Failed to get buffer semaphore. \n" );
}
return freeBuffer;
}
void otaDemo_start( void )
{
OtaEventMsg_t initEvent = { 0 };
if( !mqttWrapper_isConnected() )
{
return;
}
bufferSemaphore = xSemaphoreCreateMutex();
if( bufferSemaphore != NULL )
{
memset( dataBuffers, 0x00, sizeof( dataBuffers ) );
}
OtaInitEvent_FreeRTOS();
initEvent.eventId = OtaAgentEventRequestJobDocument;
OtaSendEvent_FreeRTOS( &initEvent );
while (otaAgentState != OtaAgentStateStopped ) {
processOTAEvents();
}
}
OtaState_t getOtaAgentState()
{
return otaAgentState;
}
static void requestJobDocumentHandler()
{
char thingName[ MAX_THING_NAME_SIZE + 1 ] = { 0 };
size_t thingNameLength = 0U;
char topicBuffer[ TOPIC_BUFFER_SIZE + 1 ] = { 0 };
char messageBuffer[ START_JOB_MSG_LENGTH ] = { 0 };
size_t topicLength = 0U;
mqttWrapper_getThingName( thingName, &thingNameLength );
/*
* AWS IoT Jobs library:
* Creates the topic string for a StartNextPendingJobExecution request.
* It used to check if any pending jobs are available.
*/
Jobs_StartNext(topicBuffer,
TOPIC_BUFFER_SIZE,
thingName,
thingNameLength,
&topicLength);
/*
* AWS IoT Jobs library:
* Creates the message string for a StartNextPendingJobExecution request.
* It will be sent on the topic created in the previous step.
*/
size_t messageLength = Jobs_StartNextMsg("test",
4U,
messageBuffer,
START_JOB_MSG_LENGTH );
mqttWrapper_publish(topicBuffer,
topicLength,
( uint8_t * ) messageBuffer,
messageLength);
}
static void initMqttDownloader( AfrOtaJobDocumentFields_t *jobFields )
{
char thingName[ MAX_THING_NAME_SIZE + 1 ] = { 0 };
size_t thingNameLength = 0U;
numOfBlocksRemaining = jobFields->fileSize /
mqttFileDownloader_CONFIG_BLOCK_SIZE;
numOfBlocksRemaining += ( jobFields->fileSize %
mqttFileDownloader_CONFIG_BLOCK_SIZE > 0 ) ? 1 : 0;
currentFileId = jobFields->fileId;
currentBlockOffset = 0;
totalBytesReceived = 0;
mqttWrapper_getThingName( thingName, &thingNameLength );
/*
* MQTT streams Library:
* Initializing the MQTT streams downloader. Passing the
* parameters extracted from the AWS IoT OTA jobs document
* using OTA jobs parser.
*/
mqttDownloader_init( &mqttFileDownloaderContext,
jobFields->imageRef,
jobFields->imageRefLen,
thingName,
thingNameLength,
DATA_TYPE_JSON );
}
static bool receivedJobDocumentHandler( OtaJobEventData_t * jobDoc )
{
bool parseJobDocument = false;
bool handled = false;
char * jobId;
size_t jobIdLength = 0U;
AfrOtaJobDocumentFields_t jobFields = { 0 };
/*
* AWS IoT Jobs library:
* Extracting the job ID from the received OTA job document.
*/
jobIdLength = Jobs_GetJobId( (char *)jobDoc->jobData, jobDoc->jobDataLength, &jobId );
if ( jobIdLength )
{
if ( strncmp( globalJobId, jobId, jobIdLength ) )
{
parseJobDocument = true;
strncpy( globalJobId, jobId, jobIdLength );
}
else
{
handled = true;
}
}
if ( parseJobDocument )
{
handled = jobDocumentParser( (char * )jobDoc->jobData, jobDoc->jobDataLength, &jobFields );
if (handled)
{
initMqttDownloader( &jobFields );
}
}
return handled;
}
static void requestDataBlock( void )
{
char getStreamRequest[ GET_STREAM_REQUEST_BUFFER_SIZE ];
size_t getStreamRequestLength = 0U;
/*
* MQTT streams Library:
* Creating the Get data block request. MQTT streams library only
* creates the get block request. To publish the request, MQTT libraries
* like coreMQTT are required.
*/
getStreamRequestLength = mqttDownloader_createGetDataBlockRequest( mqttFileDownloaderContext.dataType,
currentFileId,
mqttFileDownloader_CONFIG_BLOCK_SIZE,
currentBlockOffset,
NUM_OF_BLOCKS_REQUESTED,
getStreamRequest,
GET_STREAM_REQUEST_BUFFER_SIZE );
mqttWrapper_publish( mqttFileDownloaderContext.topicGetStream,
mqttFileDownloaderContext.topicGetStreamLength,
( uint8_t * ) getStreamRequest,
getStreamRequestLength );
}
static void processOTAEvents() {
OtaEventMsg_t recvEvent = { 0 };
OtaEvent_t recvEventId = 0;
OtaEventMsg_t nextEvent = { 0 };
OtaReceiveEvent_FreeRTOS(&recvEvent);
recvEventId = recvEvent.eventId;
printf("Received Event is %d \n", recvEventId);
switch (recvEventId)
{
case OtaAgentEventRequestJobDocument:
printf("Request Job Document event Received \n");
printf("-------------------------------------\n");
requestJobDocumentHandler();
otaAgentState = OtaAgentStateRequestingJob;
break;
case OtaAgentEventReceivedJobDocument:
printf("Received Job Document event Received \n");
printf("-------------------------------------\n");
if (otaAgentState == OtaAgentStateSuspended)
{
printf("OTA-Agent is in Suspend State. Hence dropping Job Document. \n");
break;
}
if ( receivedJobDocumentHandler(recvEvent.jobEvent) )
{
printf( "Received OTA Job. \n" );
nextEvent.eventId = OtaAgentEventRequestFileBlock;
OtaSendEvent_FreeRTOS( &nextEvent );
}
else
{
printf("This is not an OTA job \n");
}
otaAgentState = OtaAgentStateCreatingFile;
break;
case OtaAgentEventRequestFileBlock:
otaAgentState = OtaAgentStateRequestingFileBlock;
printf("Request File Block event Received \n");
printf("-----------------------------------\n");
if (currentBlockOffset == 0)
{
printf( "Starting The Download. \n" );
}
requestDataBlock();
break;
case OtaAgentEventReceivedFileBlock:
printf("Received File Block event Received \n");
printf("---------------------------------------\n");
if (otaAgentState == OtaAgentStateSuspended)
{
printf("OTA-Agent is in Suspend State. Hence dropping File Block. \n");
freeOtaDataEventBuffer(recvEvent.dataEvent);
break;
}
uint8_t decodedData[ mqttFileDownloader_CONFIG_BLOCK_SIZE ];
size_t decodedDataLength = 0;
/*
* MQTT streams Library:
* Extracting and decoding the received data block from the incoming MQTT message.
*/
mqttDownloader_processReceivedDataBlock(
&mqttFileDownloaderContext,
recvEvent.dataEvent->data,
recvEvent.dataEvent->dataLength,
decodedData,
&decodedDataLength );
handleMqttStreamsBlockArrived(decodedData, decodedDataLength);
freeOtaDataEventBuffer(recvEvent.dataEvent);
numOfBlocksRemaining--;
currentBlockOffset++;
if( numOfBlocksRemaining == 0 )
{
nextEvent.eventId = OtaAgentEventCloseFile;
OtaSendEvent_FreeRTOS( &nextEvent );
}
else
{
nextEvent.eventId = OtaAgentEventRequestFileBlock;
OtaSendEvent_FreeRTOS( &nextEvent );
}
break;
case OtaAgentEventCloseFile:
printf("Close file event Received \n");
printf("-----------------------\n");
printf( "Downloaded Data %s \n", ( char * ) downloadedData );
finishDownload();
otaAgentState = OtaAgentStateStopped;
break;
case OtaAgentEventSuspend:
printf("Suspend Event Received \n");
printf("-----------------------\n");
otaAgentState = OtaAgentStateSuspended;
break;
case OtaAgentEventResume:
printf("Resume Event Received \n");
printf("---------------------\n");
otaAgentState = OtaAgentStateRequestingJob;
nextEvent.eventId = OtaAgentEventRequestJobDocument;
OtaSendEvent_FreeRTOS( &nextEvent );
default:
break;
}
}
/* Implemented for use by the MQTT library */
bool otaDemo_handleIncomingMQTTMessage( char * topic,
size_t topicLength,
uint8_t * message,
size_t messageLength )
{
OtaEventMsg_t nextEvent = { 0 };
char thingName[ MAX_THING_NAME_SIZE + 1 ] = { 0 };
size_t thingNameLength = 0U;
mqttWrapper_getThingName(thingName, &thingNameLength);
/*
* AWS IoT Jobs library:
* Checks if a message comes from the start-next/accepted reserved topic.
*/
bool handled = Jobs_IsStartNextAccepted( topic,
topicLength,
thingName,
thingNameLength );
if( handled )
{
memcpy(jobDocBuffer.jobData, message, messageLength);
nextEvent.jobEvent = &jobDocBuffer;
jobDocBuffer.jobDataLength = messageLength;
nextEvent.eventId = OtaAgentEventReceivedJobDocument;
OtaSendEvent_FreeRTOS( &nextEvent );
}
else
{
/*
* MQTT streams Library:
* Checks if the incoming message contains the requested data block. It is performed by
* comparing the incoming MQTT message topic with MQTT streams topics.
*/
handled = mqttDownloader_isDataBlockReceived(&mqttFileDownloaderContext, topic, topicLength);
if (handled)
{
nextEvent.eventId = OtaAgentEventReceivedFileBlock;
OtaDataEvent_t * dataBuf = getOtaDataEventBuffer();
memcpy(dataBuf->data, message, messageLength);
nextEvent.dataEvent = dataBuf;
dataBuf->dataLength = messageLength;
OtaSendEvent_FreeRTOS( &nextEvent );
}
}
if( !handled )
{
printf( "Unrecognized incoming MQTT message received on topic: "
"%.*s\nMessage: %.*s\n",
( unsigned int ) topicLength,
topic,
( unsigned int ) messageLength,
( char * ) message );
}
return handled;
}
static bool jobDocumentParser( char * message, size_t messageLength, AfrOtaJobDocumentFields_t *jobFields )
{
char * jobDoc;
size_t jobDocLength = 0U;
int8_t fileIndex = 0;
/*
* AWS IoT Jobs library:
* Extracting the OTA job document from the jobs message recevied from AWS IoT core.
*/
jobDocLength = Jobs_GetJobDocument( message, messageLength, &jobDoc );
if( jobDocLength != 0U )
{
do
{
/*
* AWS IoT Jobs library:
* Parsing the OTA job document to extract all of the parameters needed to download
* the new firmware.
*/
fileIndex = otaParser_parseJobDocFile( jobDoc,
jobDocLength,
fileIndex,
jobFields );
} while( fileIndex > 0 );
}
// File index will be -1 if an error occured, and 0 if all files were
// processed
return fileIndex == 0;
}
/* Stores the received data blocks in the flash partition reserved for OTA */
static void handleMqttStreamsBlockArrived(
uint8_t *data, size_t dataLength )
{
assert( ( totalBytesReceived + dataLength ) <
CONFIG_MAX_FILE_SIZE );
printf( "Downloaded block %u of %u. \n", currentBlockOffset, (currentBlockOffset + numOfBlocksRemaining) );
memcpy( downloadedData + totalBytesReceived,
data,
dataLength );
totalBytesReceived += dataLength;
}
static void finishDownload()
{
/* TODO: Do something with the completed download */
/* Start the bootloader */
char thingName[ MAX_THING_NAME_SIZE + 1 ] = { 0 };
size_t thingNameLength = 0U;
char topicBuffer[ TOPIC_BUFFER_SIZE + 1 ] = { 0 };
size_t topicBufferLength = 0U;
char messageBuffer[ UPDATE_JOB_MSG_LENGTH ] = { 0 };
mqttWrapper_getThingName( thingName, &thingNameLength );
/*
* AWS IoT Jobs library:
* Creating the MQTT topic to update the status of OTA job.
*/
Jobs_Update(topicBuffer,
TOPIC_BUFFER_SIZE,
thingName,
thingNameLength,
globalJobId,
strnlen( globalJobId, 1000U ),
&topicBufferLength);
/*
* AWS IoT Jobs library:
* Creating the message which contains the status of OTA job.
* It will be published on the topic created in the previous step.
*/
size_t messageBufferLength = Jobs_UpdateMsg(Succeeded,
"2",
1U,
messageBuffer,
UPDATE_JOB_MSG_LENGTH );
mqttWrapper_publish(topicBuffer,
topicBufferLength,
( uint8_t * ) messageBuffer,
messageBufferLength);
printf( "\033[1;32mOTA Completed successfully!\033[0m\n" );
globalJobId[ 0 ] = 0U;
}