24
24
/*******************************************************************************
25
25
* Variables
26
26
******************************************************************************/
27
- static bootloader_cmd_t bootloader_cmd ;
28
27
29
28
#ifdef BOOTLOADER
30
29
// variables use to save binary data in flash
@@ -38,7 +37,6 @@ uint16_t residual_space = (uint16_t)BUFFER_SIZE;
38
37
uint32_t nb_bytes = 0 ;
39
38
uint8_t crc = 0 ;
40
39
bool load_flag = false;
41
- uint16_t source_id = 0 ; // used to save source_id, ie gate_id
42
40
uint32_t tickstart = 0 ;
43
41
44
42
#ifndef BOOTLOADER_UPDATER
@@ -57,8 +55,9 @@ static inline uint8_t LuosBootloader_IsEnoughSpace(uint32_t);
57
55
static inline void LuosBootloader_EraseMemory (void );
58
56
static inline void LuosBootloader_ProcessData (void );
59
57
static inline void LuosBootloader_SaveLastData (void );
60
- static void LuosBootloader_SendResponse (bootloader_cmd_t );
61
- static void LuosBootloader_SendCrc (bootloader_cmd_t , uint8_t );
58
+ static void LuosBootloader_SendResponse (service_t * service , uint16_t source_id , bootloader_cmd_t response );
59
+ static void LuosBootloader_SendCrc (service_t * service , uint16_t source_id , bootloader_cmd_t , uint8_t );
60
+ static void LuosBootloader_MsgHandler (service_t * service , const msg_t * input );
62
61
#endif
63
62
64
63
/******************************************************************************
@@ -91,9 +90,9 @@ void LuosBootloader_Init(void)
91
90
{
92
91
revision_t version = {.major = 2 , .minor = 0 , .build = 0 };
93
92
#ifdef BOOTLOADER_UPDATER
94
- Luos_CreateService (0 , VOID_TYPE , "boot_updater" , version );
93
+ Luos_CreateService (LuosBootloader_MsgHandler , VOID_TYPE , "boot_updater" , version );
95
94
#else
96
- Luos_CreateService (0 , VOID_TYPE , "boot_service" , version );
95
+ Luos_CreateService (LuosBootloader_MsgHandler , VOID_TYPE , "boot_service" , version );
97
96
#endif
98
97
99
98
// set ID node saved in flash
@@ -315,7 +314,7 @@ uint8_t compute_crc(void)
315
314
* @param data : The crc value
316
315
* @return None
317
316
******************************************************************************/
318
- void LuosBootloader_SendCrc (bootloader_cmd_t response , uint8_t data )
317
+ void LuosBootloader_SendCrc (service_t * service , uint16_t source_id , bootloader_cmd_t response , uint8_t data )
319
318
{
320
319
msg_t ready_msg ;
321
320
ready_msg .header .cmd = BOOTLOADER_RESP ;
@@ -328,15 +327,15 @@ void LuosBootloader_SendCrc(bootloader_cmd_t response, uint8_t data)
328
327
uint32_t tick = LuosHAL_GetSystick ();
329
328
while (LuosHAL_GetSystick () - tick < node -> node_id )
330
329
;
331
- Luos_SendMsg (0 , & ready_msg );
330
+ Luos_SendMsg (service , & ready_msg );
332
331
}
333
332
334
333
/******************************************************************************
335
334
* @brief Send response to the gate
336
335
* @param response : The type of crc message
337
336
* @return None
338
337
******************************************************************************/
339
- void LuosBootloader_SendResponse (bootloader_cmd_t response )
338
+ void LuosBootloader_SendResponse (service_t * service , uint16_t source_id , bootloader_cmd_t response )
340
339
{
341
340
msg_t ready_msg ;
342
341
ready_msg .header .cmd = BOOTLOADER_RESP ;
@@ -348,7 +347,7 @@ void LuosBootloader_SendResponse(bootloader_cmd_t response)
348
347
uint32_t tick = LuosHAL_GetSystick ();
349
348
while (LuosHAL_GetSystick () - tick < node -> node_id )
350
349
;
351
- Luos_SendMsg (0 , & ready_msg );
350
+ Luos_SendMsg (service , & ready_msg );
352
351
}
353
352
354
353
/******************************************************************************
@@ -363,115 +362,115 @@ void LuosBootloader_Loop(void)
363
362
364
363
/******************************************************************************
365
364
* @brief Message handler called from luos library
365
+ * @param service : Pointer to the service which received the message
366
366
* @param input : Pointer to message received from luos network
367
367
* @return None
368
368
******************************************************************************/
369
- void LuosBootloader_MsgHandler (const msg_t * input )
369
+ void LuosBootloader_MsgHandler (service_t * service , const msg_t * input )
370
370
{
371
- bootloader_cmd = input -> data [0 ];
372
-
373
- switch (bootloader_cmd )
371
+ if (input -> header .cmd == BOOTLOADER_CMD )
374
372
{
373
+ switch (input -> data [0 ])
374
+ {
375
375
#ifdef WITH_BOOTLOADER
376
- case BOOTLOADER_START :
377
- // We're in the app,
378
- // set bootloader mode, save node ID and reboot
379
- LuosBootloader_JumpToBootloader ();
380
- break ;
376
+ case BOOTLOADER_START :
377
+ // We're in the app,
378
+ // set bootloader mode, save node ID and reboot
379
+ LuosBootloader_JumpToBootloader ();
380
+ break ;
381
381
#endif
382
382
#ifdef BOOTLOADER
383
- // we're in the bootloader,
384
- // process cmd and data
385
- case BOOTLOADER_READY :
386
- source_id = input -> header .source ;
387
- bootloader_data_size = input -> header .size - 2 * sizeof (char );
388
- Luos_Subscribe (0 , (uint16_t )input -> data [1 ]);
389
- memcpy (bootloader_data , & (input -> data [2 ]), bootloader_data_size );
390
-
391
- LuosHAL_SetMode ((uint8_t )BOOT_MODE );
392
-
393
- // save binary length
394
- memcpy (& nb_bytes , & bootloader_data [0 ], sizeof (uint32_t ));
395
- // check free space in flash
396
- if (LuosBootloader_IsEnoughSpace (nb_bytes ) == SUCCEED )
397
- {
398
- // send READY response
399
- LuosBootloader_SendResponse (BOOTLOADER_READY_RESP );
400
- }
401
- else
402
- {
403
- // send ERROR response
404
- LuosBootloader_SendResponse (BOOTLOADER_ERROR_SIZE );
405
- }
406
- break ;
407
-
408
- case BOOTLOADER_ERASE :
409
- // erase flash memory
410
- LuosBootloader_EraseMemory ();
411
- // reset load flag
412
- load_flag = false;
413
- // send ERASE response
414
- LuosBootloader_SendResponse (BOOTLOADER_ERASE_RESP );
415
- break ;
416
-
417
- case BOOTLOADER_BIN_CHUNK :
418
- source_id = input -> header .source ;
419
- bootloader_data_size = input -> header .size - sizeof (char );
420
- memcpy (bootloader_data , & (input -> data [1 ]), bootloader_data_size );
421
-
422
- // handle binary data
423
- LuosBootloader_ProcessData ();
424
- // send ack to the Host
425
- LuosBootloader_SendResponse (BOOTLOADER_BIN_CHUNK_RESP );
426
- break ;
427
-
428
- case BOOTLOADER_BIN_END :
429
- source_id = input -> header .source ;
430
- bootloader_data_size = input -> header .size - sizeof (char );
431
- memcpy (bootloader_data , & (input -> data [1 ]), bootloader_data_size );
432
-
433
- // save the current page in flash memory
434
- LuosBootloader_SaveLastData ();
435
- // send ack to the Host
436
- LuosBootloader_SendResponse (BOOTLOADER_BIN_END_RESP );
437
- break ;
438
-
439
- case BOOTLOADER_CRC_TEST :
440
- crc = compute_crc ();
441
- // send ack to the Host
442
- LuosBootloader_SendCrc (BOOTLOADER_CRC_RESP , crc );
443
- break ;
444
-
445
- case BOOTLOADER_APP_SAVED :
446
- // set load flag
447
- load_flag = true;
448
- Luos_Unsubscribe (0 , input -> header .target );
449
- break ;
450
-
451
- case BOOTLOADER_STOP :
452
- // wait for the command to be send to all nodes
453
- tickstart = LuosHAL_GetSystick ();
454
- while ((LuosHAL_GetSystick () - tickstart ) < 1000 )
455
- ;
456
- // save bootloader mode in flash
457
- if (load_flag || (LuosBootloader_GetMode () == APP_RELOAD_MODE ))
458
- {
459
- // boot the application programmed in dedicated flash partition
460
- LuosBootloader_DeInit ();
461
- LuosBootloader_JumpToApp ();
462
- }
463
- else
464
- {
465
- // reboot the node
466
- LuosHAL_Reboot ();
467
- }
468
- break ;
383
+ // we're in the bootloader,
384
+ // process cmd and data
385
+ case BOOTLOADER_READY :
386
+ bootloader_data_size = input -> header .size - 2 * sizeof (char );
387
+ Luos_Subscribe (service , (uint16_t )input -> data [1 ]);
388
+ memcpy (bootloader_data , & (input -> data [2 ]), bootloader_data_size ); // why?
389
+
390
+ LuosHAL_SetMode ((uint8_t )BOOT_MODE );
391
+
392
+ // save binary length
393
+ memcpy (& nb_bytes , & bootloader_data [0 ], sizeof (uint32_t ));
394
+ // check free space in flash
395
+ if (LuosBootloader_IsEnoughSpace (nb_bytes ) == SUCCEED )
396
+ {
397
+ // send READY response
398
+ LuosBootloader_SendResponse (service , input -> header .source , BOOTLOADER_READY_RESP );
399
+ }
400
+ else
401
+ {
402
+ // send ERROR response
403
+ LuosBootloader_SendResponse (service , input -> header .source , BOOTLOADER_ERROR_SIZE );
404
+ }
405
+ break ;
406
+
407
+ case BOOTLOADER_ERASE :
408
+ // erase flash memory
409
+ LuosBootloader_EraseMemory ();
410
+ // reset load flag
411
+ load_flag = false;
412
+ // send ERASE response
413
+ LuosBootloader_SendResponse (service , input -> header .source , BOOTLOADER_ERASE_RESP );
414
+ break ;
415
+
416
+ case BOOTLOADER_BIN_CHUNK :
417
+ bootloader_data_size = input -> header .size - sizeof (char );
418
+ memcpy (bootloader_data , & (input -> data [1 ]), bootloader_data_size );
419
+
420
+ // handle binary data
421
+ LuosBootloader_ProcessData ();
422
+
423
+ // send ack to the Host
424
+ LuosBootloader_SendResponse (service , input -> header .source , BOOTLOADER_BIN_CHUNK_RESP );
425
+ break ;
426
+
427
+ case BOOTLOADER_BIN_END :
428
+ bootloader_data_size = input -> header .size - sizeof (char );
429
+ memcpy (bootloader_data , & (input -> data [1 ]), bootloader_data_size );
430
+
431
+ // save the current page in flash memory
432
+ LuosBootloader_SaveLastData ();
433
+ // send ack to the Host
434
+ LuosBootloader_SendResponse (service , input -> header .source , BOOTLOADER_BIN_END_RESP );
435
+ break ;
436
+
437
+ case BOOTLOADER_CRC_TEST :
438
+ crc = compute_crc ();
439
+ // send ack to the Host
440
+ LuosBootloader_SendCrc (service , input -> header .source , BOOTLOADER_CRC_RESP , crc );
441
+ break ;
442
+
443
+ case BOOTLOADER_APP_SAVED :
444
+ // set load flag
445
+ load_flag = true;
446
+ Luos_Unsubscribe (service , input -> header .target );
447
+ break ;
448
+
449
+ case BOOTLOADER_STOP :
450
+ // wait for the command to be send to all nodes
451
+ tickstart = LuosHAL_GetSystick ();
452
+ while ((LuosHAL_GetSystick () - tickstart ) < 1000 )
453
+ ;
454
+ // save bootloader mode in flash
455
+ if (load_flag || (LuosBootloader_GetMode () == APP_RELOAD_MODE ))
456
+ {
457
+ // boot the application programmed in dedicated flash partition
458
+ LuosBootloader_DeInit ();
459
+ LuosBootloader_JumpToApp ();
460
+ }
461
+ else
462
+ {
463
+ // reboot the node
464
+ LuosHAL_Reboot ();
465
+ }
466
+ break ;
469
467
#endif
470
- case BOOTLOADER_RESET :
471
- LuosHAL_SetMode ((uint8_t )BOOT_MODE );
472
- LuosHAL_Reboot ();
473
- break ;
474
- default :
475
- break ;
468
+ case BOOTLOADER_RESET :
469
+ LuosHAL_SetMode ((uint8_t )BOOT_MODE );
470
+ LuosHAL_Reboot ();
471
+ break ;
472
+ default :
473
+ break ;
474
+ }
476
475
}
477
476
}
0 commit comments