Fix handling buffers with text

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2023-09-07 22:52:32 +02:00
parent 06911014f8
commit 1a307252da
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
2 changed files with 28 additions and 4 deletions

View file

@ -239,7 +239,7 @@ device3_type* device3_open(device3_event_callback callback) {
uint32_t calibration_len = 0;
if (recv_payload_msg(device, DEVICE3_MSG_GET_CAL_DATA_LENGTH, 4, (uint8_t*) &calibration_len)) {
char* calibration_data = malloc(calibration_len);
char* calibration_data = malloc(calibration_len + 1);
uint32_t position = 0;
while (position < calibration_len) {
@ -257,6 +257,8 @@ device3_type* device3_open(device3_event_callback callback) {
position += next;
}
calibration_data[calibration_len] = '\0';
struct json_tokener* tokener = json_tokener_new();
struct json_object* root = json_tokener_parse_ex(tokener, calibration_data, calibration_len);

View file

@ -111,16 +111,19 @@ static bool recv_payload_msg(device4_type* device, uint16_t msgid, uint8_t len,
}
if (packet.head != PACKET_HEAD) {
perror("ERROR: invalid payload received\n");
return false;
}
if (packet.msgid != msgid) {
perror("ERROR: unexpected payload received\n");
return false;
}
const uint8_t status = packet.data[0];
const uint8_t status = packet.data[0];
if (status != 0) {
perror("ERROR: payload status failed\n");
return false;
}
@ -153,6 +156,7 @@ static bool do_payload_action(device4_type* device, uint16_t msgid, uint8_t len,
attempts--;
}
perror("ERROR: payload status failed\n");
return false;
}
@ -483,6 +487,22 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) {
goto cleanup;
}
if (!send_payload_action(device, DEVICE4_MSG_R_ACTIVATION_TIME, 0, NULL)) {
perror("Requesting activation time failed!\n");
return device;
}
uint8_t activated;
if (!recv_payload_msg(device, DEVICE4_MSG_R_ACTIVATION_TIME, 1, &activated)) {
perror("Receiving activation time failed!\n");
return device;
}
if (!activated) {
perror("Device is not activated!\n");
goto jump_to_app;
}
size_t offset = 0;
while (offset < firmware_len) {
@ -500,10 +520,12 @@ bool device4_update_mcu_firmware(device4_type* device, const char* path) {
msgid = DEVICE4_MSG_W_UPDATE_MCU_APP_FW_TRANSMIT;
}
if (!do_payload_action(device, msgid, len, firmware)) {
if (!do_payload_action(device, msgid, len, firmware + offset)) {
perror("Failed sending firmware upload!\n");
goto jump_to_app;
}
offset += len;
}
printf("Finish upload!\n");