From 80e98ad2c5e3f967f99aac4f47ceae281f3e0f61 Mon Sep 17 00:00:00 2001 From: Maksymilian Wojczuk Date: Tue, 13 Nov 2018 11:59:12 +0100 Subject: [PATCH] Fix multiple targets Added support for multiple targets and fixed race condition for JTAG and USB FAT FS. Changed file names convention for different targets. --- .../wakaama_client/objects/object_target.h | 3 +++ Src/communication/binary_download.c | 4 +++- .../wakaama_client/objects/object_target.c | 15 +++++++++++---- Src/target.c | 5 +++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Inc/communication/wakaama_client/objects/object_target.h b/Inc/communication/wakaama_client/objects/object_target.h index 1118eb0..9f1719d 100644 --- a/Inc/communication/wakaama_client/objects/object_target.h +++ b/Inc/communication/wakaama_client/objects/object_target.h @@ -4,6 +4,9 @@ #include "target.h" + +extern int JTAG_BUSY; +extern int USB_BUSY; /* * Multiple instance objects can use userdata to store data that will be shared between the different instances. * The lwm2m_object_t object structure - which represent every object of the liblwm2m as seen in the single instance diff --git a/Src/communication/binary_download.c b/Src/communication/binary_download.c index 95221ce..3bcc63e 100644 --- a/Src/communication/binary_download.c +++ b/Src/communication/binary_download.c @@ -30,6 +30,7 @@ static void download_error(target_instance_t *targetP, int err, int socket, char lwip_close(socket); } lwm2m_free(url_str); + USB_BUSY = 0; vTaskDelete(NULL); } @@ -155,12 +156,13 @@ void startDownload(void *object_target) { targetP->download_error = NO_ERROR; lwip_close(socket); lwm2m_free(url_str); + USB_BUSY = 0; vTaskDelete(NULL); } static uint8_t createFile(target_instance_t *targetP, char *url_str, int socket, FIL *file) { if (get_usb_ready()) { - int result = usb_open_file(targetP->binary_filename, file, FA_WRITE | FA_CREATE_NEW); + int result = usb_open_file(targetP->binary_filename, file, FA_WRITE | FA_CREATE_ALWAYS); if (result != 0) { download_error(targetP, USB_ERROR, socket, url_str); } diff --git a/Src/communication/wakaama_client/objects/object_target.c b/Src/communication/wakaama_client/objects/object_target.c index 0ef82d1..897c793 100644 --- a/Src/communication/wakaama_client/objects/object_target.c +++ b/Src/communication/wakaama_client/objects/object_target.c @@ -33,6 +33,9 @@ #include "object_target.h" #include "binary_download.h" +int USB_BUSY = 0; +int JTAG_BUSY = 0; + static void prv_output_buffer(uint8_t * buffer, int length) { @@ -210,7 +213,7 @@ static uint8_t target_write(uint16_t instanceId, return COAP_405_METHOD_NOT_ALLOWED; case 2: { - if (targetP->download_state == DOWNLOAD_IN_PROGRESS || targetP->flash_state == FLASH_IN_PROGRESS) { + if (targetP->download_state == DOWNLOAD_IN_PROGRESS || targetP->flash_state == FLASH_IN_PROGRESS || USB_BUSY) { return COAP_412_PRECONDITION_FAILED; } if (!(dataArray[i].type == LWM2M_TYPE_STRING) && !(dataArray[i].type == LWM2M_TYPE_OPAQUE)) { @@ -223,9 +226,10 @@ static uint8_t target_write(uint16_t instanceId, targetP->firmware_url = lwm2m_strdup((char*)dataArray[i].value.asBuffer.buffer); targetP->firmware_version = lwm2m_gettime(); targetP->download_state = DOWNLOAD_IN_PROGRESS; - sprintf(targetP->binary_filename, "%ld", targetP->firmware_version); + sprintf(targetP->binary_filename, "%x.%d", targetP->firmware_version, targetP->shortID); targetP->download_progress = 0; + USB_BUSY = 1; xTaskCreate(startDownload, NULL, 2000, (void*) targetP, 2, NULL); } break; @@ -298,7 +302,7 @@ static uint8_t target_exec(uint16_t instanceId, case 4: return COAP_405_METHOD_NOT_ALLOWED; case 5: - if (targetP->download_state != DOWNLOAD_COMPLETED || targetP->flash_state == FLASH_IN_PROGRESS) { + if (targetP->download_state != DOWNLOAD_COMPLETED || targetP->flash_state == FLASH_IN_PROGRESS || JTAG_BUSY || USB_BUSY) { return COAP_412_PRECONDITION_FAILED; } fprintf(stdout, "\r\n-----------------\r\n" @@ -310,12 +314,14 @@ static uint8_t target_exec(uint16_t instanceId, fprintf(stdout, "-----------------\r\n\r\n"); targetP->flash_state=FLASH_IN_PROGRESS; targetP->flash_progress=0; + JTAG_BUSY = 1; + USB_BUSY = 1; xTaskCreate(flash_target_task, "Flash_Target", 2000, targetP, 1, NULL); return COAP_204_CHANGED; case 6: return COAP_405_METHOD_NOT_ALLOWED; case 7: - if (targetP->flash_state == FLASH_IN_PROGRESS) { + if (targetP->flash_state == FLASH_IN_PROGRESS || JTAG_BUSY) { return COAP_412_PRECONDITION_FAILED; } fprintf(stdout, "\r\n-----------------\r\n" @@ -325,6 +331,7 @@ static uint8_t target_exec(uint16_t instanceId, objectP->objID, instanceId, resourceId, targetP->target_type, length); prv_output_buffer((uint8_t*)buffer, length); fprintf(stdout, "-----------------\r\n\r\n"); + JTAG_BUSY = 1; xTaskCreate(reset_target_task, "ResetTarget", 300, targetP, 1, NULL); return COAP_204_CHANGED; case 8: diff --git a/Src/target.c b/Src/target.c index 4d9ef30..06fd3aa 100644 --- a/Src/target.c +++ b/Src/target.c @@ -27,6 +27,8 @@ void flash_target_task(void *object) { if (result != 0) { target_object->flash_error = USB_FS_ERROR; target_object->flash_state = FLASH_ERROR; + JTAG_BUSY = 0; + USB_BUSY = 0; vTaskDelete(NULL); } @@ -40,6 +42,8 @@ void flash_target_task(void *object) { } result = usb_close_file(&file); + JTAG_BUSY = 0; + USB_BUSY = 0; if (result != 0) { target_object->flash_error = USB_FS_ERROR; vTaskDelete(NULL); @@ -50,5 +54,6 @@ void flash_target_task(void *object) { void reset_target_task(void *object) { target_instance_t * target_object = (target_instance_t *) object; target_object->target->ops->reset_target(target_object->target->priv); + JTAG_BUSY = 0; vTaskDelete(NULL); } \ No newline at end of file