diff --git a/src/coap.h b/src/coap.h index c2fcd40..d2ed7cb 100644 --- a/src/coap.h +++ b/src/coap.h @@ -31,6 +31,7 @@ namespace CoAPMessageType { DESCRIBE, FUNCTION_CALL, VARIABLE_REQUEST, + SAVE_BEGIN, UPDATE_BEGIN, UPDATE_DONE, CHUNK, diff --git a/src/spark_protocol.cpp b/src/spark_protocol.cpp index 08ae3fc..48e7a87 100644 --- a/src/spark_protocol.cpp +++ b/src/spark_protocol.cpp @@ -71,6 +71,7 @@ void SparkProtocol::init(const char *id, callback_send = callbacks.send; callback_receive = callbacks.receive; + callback_prepare_to_save_file = callbacks.prepare_to_save_file; callback_prepare_for_firmware_update = callbacks.prepare_for_firmware_update; callback_finish_firmware_update = callbacks.finish_firmware_update; callback_calculate_crc = callbacks.calculate_crc; @@ -290,6 +291,7 @@ CoAPMessageType::Enum return CoAPMessageType::EVENT; case 'h': return CoAPMessageType::HELLO; case 'f': return CoAPMessageType::FUNCTION_CALL; + case 's': return CoAPMessageType::SAVE_BEGIN; case 'u': return CoAPMessageType::UPDATE_BEGIN; case 'c': return CoAPMessageType::CHUNK; default: break; @@ -1002,6 +1004,8 @@ bool SparkProtocol::handle_received_message(void) } break; } + case CoAPMessageType::SAVE_BEGIN: + // fall through case CoAPMessageType::UPDATE_BEGIN: // send ACK *msg_to_send = 0; @@ -1013,7 +1017,19 @@ bool SparkProtocol::handle_received_message(void) return false; } - callback_prepare_for_firmware_update(); + if(message_type == CoAPMessageType::SAVE_BEGIN) + { + // save file to the specified location in external flash + unsigned int sflash_address = queue[8] << 24 | queue[9] << 16 | queue[10] << 8 | queue[11]; + // file size information is required to calculate the sectors that need to be erased + unsigned int file_size = queue[12] << 24 | queue[13] << 16 | queue[14] << 8 | queue[15]; + callback_prepare_to_save_file(sflash_address, file_size); + } + else + { + callback_prepare_for_firmware_update(); + } + last_chunk_millis = callback_millis(); chunk_index = 0; updating = true; diff --git a/src/spark_protocol.h b/src/spark_protocol.h index 39abce0..3179622 100644 --- a/src/spark_protocol.h +++ b/src/spark_protocol.h @@ -62,6 +62,7 @@ struct SparkCallbacks { int (*send)(const unsigned char *buf, int buflen); int (*receive)(unsigned char *buf, int buflen); + void (*prepare_to_save_file)(unsigned int sflash_address, unsigned int file_size); void (*prepare_for_firmware_update)(void); void (*finish_firmware_update)(void); long unsigned int (*calculate_crc)(unsigned char *buf, long unsigned int buflen); @@ -146,6 +147,7 @@ class SparkProtocol int (*callback_send)(const unsigned char *buf, int buflen); int (*callback_receive)(unsigned char *buf, int buflen); + void (*callback_prepare_to_save_file)(unsigned int sflash_address, unsigned int file_size); void (*callback_prepare_for_firmware_update)(void); void (*callback_finish_firmware_update)(void); long unsigned int (*callback_calculate_crc)(unsigned char *buf, long unsigned int buflen);