bootloader: process callback in incoming messages

pull/25/head
Pavol Rusnak 7 years ago
parent 46c26b8c09
commit aa6c96046e
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -126,11 +126,6 @@ void check_and_jump(void)
if (image_check_signature((const uint8_t *)BOOTLOADER_START, &hdr, NULL)) {
display_printf("valid bootloader signature\n");
// TODO: remove debug wait
display_printf("waiting 1 second\n");
HAL_Delay(1000);
// end
display_printf("JUMP!\n");
jump_to(BOOTLOADER_START + HEADER_SIZE);

@ -182,7 +182,7 @@ static bool _recv_msg(uint8_t iface_num, uint32_t msg_size, uint8_t *buf, const
.errmsg = NULL
};
if (!pb_decode(&stream, fields, msg)) {
if (!pb_decode_noinit(&stream, fields, msg)) {
return false;
}
@ -191,7 +191,21 @@ static bool _recv_msg(uint8_t iface_num, uint32_t msg_size, uint8_t *buf, const
return true;
}
#define MSG_RECV_INIT(TYPE) TYPE msg_recv = TYPE##_init_zero
static bool _decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
pb_byte_t *buf = *arg;
memset(buf, 0, 1024);
if (stream->bytes_left > 1024 - 1) {
return false;
}
if (!pb_read(stream, buf, stream->bytes_left)) {
return false;
}
return true;
}
#define MSG_RECV_INIT(TYPE) TYPE msg_recv = TYPE##_init_default
#define MSG_RECV_ASSIGN_STRING(FIELD, VALUE) do { msg_recv.FIELD.funcs.decode = &_decode_string; msg_recv.FIELD.arg = VALUE; } while (0)
#define MSG_RECV(TYPE) do { _recv_msg(iface_num, msg_size, buf, TYPE##_fields, &msg_recv); } while(0)
void process_msg_Initialize(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
@ -210,15 +224,16 @@ void process_msg_Initialize(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
MSG_SEND(Features);
}
void process_msg_Ping(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
{
char m[1024];
MSG_RECV_INIT(Ping);
MSG_RECV_ASSIGN_STRING(message, m);
MSG_RECV(Ping);
MSG_SEND_INIT(Success);
// TODO: read message from Ping
MSG_SEND_ASSIGN_STRING(message, "PONG!");
MSG_SEND_ASSIGN_STRING(message, m);
MSG_SEND(Success);
}
@ -227,23 +242,24 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu
MSG_RECV_INIT(FirmwareErase);
MSG_RECV(FirmwareErase);
// TODO: implement
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_FirmwareError);
MSG_SEND_ASSIGN_STRING(message, "Unsupported message");
MSG_SEND(Failure);
MSG_SEND_INIT(FirmwareRequest);
MSG_SEND_ASSIGN_VALUE(offset, 0);
MSG_SEND_ASSIGN_VALUE(length, FIRMWARE_CHUNK_SIZE);
MSG_SEND(FirmwareRequest);
}
void process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)
{
MSG_RECV_INIT(FirmwareUpload);
MSG_RECV(FirmwareUpload);
// TODO: implement
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_FirmwareError);
MSG_SEND_ASSIGN_STRING(message, "Unsupported message");
MSG_SEND(Failure);
/*
MSG_SEND_INIT(FirmwareRequest);
MSG_SEND_ASSIGN_VALUE(offset, FIRMWARE_CHUNK_SIZE);
MSG_SEND_ASSIGN_VALUE(length, FIRMWARE_CHUNK_SIZE);
MSG_SEND(FirmwareRequest);
*/
MSG_SEND_INIT(Success);
MSG_SEND(Success);
}
void process_msg_unknown(uint8_t iface_num, uint32_t msg_size, uint8_t *buf)

@ -12,7 +12,7 @@
#define USB_PACKET_SIZE 64
#define USB_IFACE_NUM 0
#define UPLOAD_CHUNK_SIZE (128*1024)
#define FIRMWARE_CHUNK_SIZE (128*1024)
bool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size);

Loading…
Cancel
Save