Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace CoAPMessageType {
DESCRIBE,
FUNCTION_CALL,
VARIABLE_REQUEST,
SAVE_BEGIN,
UPDATE_BEGIN,
UPDATE_DONE,
CHUNK,
Expand Down
18 changes: 17 additions & 1 deletion src/spark_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/spark_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down