Nfc: replace cmsis thread with furi, fix condition race in thread stop routine. (#1003)
* Nfc: replace cmsis thread with furi, fix condition race on thread stop routine. * Nfc: fix memory leak * FuriCore, CMSIS: properly handle thread names before scheduler start
This commit is contained in:
		
							parent
							
								
									ddd909faa0
								
							
						
					
					
						commit
						3c77ae2eb8
					
				| @ -9,11 +9,17 @@ | |||||||
| 
 | 
 | ||||||
| NfcWorker* nfc_worker_alloc() { | NfcWorker* nfc_worker_alloc() { | ||||||
|     NfcWorker* nfc_worker = malloc(sizeof(NfcWorker)); |     NfcWorker* nfc_worker = malloc(sizeof(NfcWorker)); | ||||||
|  | 
 | ||||||
|     // Worker thread attributes
 |     // Worker thread attributes
 | ||||||
|     nfc_worker->thread_attr.name = "NfcWorker"; |     nfc_worker->thread = furi_thread_alloc(); | ||||||
|     nfc_worker->thread_attr.stack_size = 8192; |     furi_thread_set_name(nfc_worker->thread, "NfcWorker"); | ||||||
|  |     furi_thread_set_stack_size(nfc_worker->thread, 8192); | ||||||
|  |     furi_thread_set_callback(nfc_worker->thread, nfc_worker_task); | ||||||
|  |     furi_thread_set_context(nfc_worker->thread, nfc_worker); | ||||||
|  | 
 | ||||||
|     nfc_worker->callback = NULL; |     nfc_worker->callback = NULL; | ||||||
|     nfc_worker->context = NULL; |     nfc_worker->context = NULL; | ||||||
|  | 
 | ||||||
|     // Initialize rfal
 |     // Initialize rfal
 | ||||||
|     while(furi_hal_nfc_is_busy()) { |     while(furi_hal_nfc_is_busy()) { | ||||||
|         osDelay(10); |         osDelay(10); | ||||||
| @ -25,6 +31,7 @@ NfcWorker* nfc_worker_alloc() { | |||||||
| 
 | 
 | ||||||
| void nfc_worker_free(NfcWorker* nfc_worker) { | void nfc_worker_free(NfcWorker* nfc_worker) { | ||||||
|     furi_assert(nfc_worker); |     furi_assert(nfc_worker); | ||||||
|  |     furi_thread_free(nfc_worker->thread); | ||||||
|     free(nfc_worker); |     free(nfc_worker); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -48,7 +55,7 @@ void nfc_worker_start( | |||||||
|     nfc_worker->context = context; |     nfc_worker->context = context; | ||||||
|     nfc_worker->dev_data = dev_data; |     nfc_worker->dev_data = dev_data; | ||||||
|     nfc_worker_change_state(nfc_worker, state); |     nfc_worker_change_state(nfc_worker, state); | ||||||
|     nfc_worker->thread = osThreadNew(nfc_worker_task, nfc_worker, &nfc_worker->thread_attr); |     furi_thread_start(nfc_worker->thread); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void nfc_worker_stop(NfcWorker* nfc_worker) { | void nfc_worker_stop(NfcWorker* nfc_worker) { | ||||||
| @ -58,6 +65,7 @@ void nfc_worker_stop(NfcWorker* nfc_worker) { | |||||||
|     } |     } | ||||||
|     furi_hal_nfc_stop(); |     furi_hal_nfc_stop(); | ||||||
|     nfc_worker_change_state(nfc_worker, NfcWorkerStateStop); |     nfc_worker_change_state(nfc_worker, NfcWorkerStateStop); | ||||||
|  |     furi_thread_join(nfc_worker->thread); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) { | void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) { | ||||||
| @ -66,7 +74,7 @@ void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state) { | |||||||
| 
 | 
 | ||||||
| /***************************** NFC Worker Thread *******************************/ | /***************************** NFC Worker Thread *******************************/ | ||||||
| 
 | 
 | ||||||
| void nfc_worker_task(void* context) { | int32_t nfc_worker_task(void* context) { | ||||||
|     NfcWorker* nfc_worker = context; |     NfcWorker* nfc_worker = context; | ||||||
| 
 | 
 | ||||||
|     furi_hal_power_insomnia_enter(); |     furi_hal_power_insomnia_enter(); | ||||||
| @ -92,7 +100,8 @@ void nfc_worker_task(void* context) { | |||||||
|     furi_hal_nfc_deactivate(); |     furi_hal_nfc_deactivate(); | ||||||
|     nfc_worker_change_state(nfc_worker, NfcWorkerStateReady); |     nfc_worker_change_state(nfc_worker, NfcWorkerStateReady); | ||||||
|     furi_hal_power_insomnia_exit(); |     furi_hal_power_insomnia_exit(); | ||||||
|     osThreadExit(); | 
 | ||||||
|  |     return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void nfc_worker_detect(NfcWorker* nfc_worker) { | void nfc_worker_detect(NfcWorker* nfc_worker) { | ||||||
|  | |||||||
| @ -17,8 +17,7 @@ | |||||||
| #include <st25r3916_irq.h> | #include <st25r3916_irq.h> | ||||||
| 
 | 
 | ||||||
| struct NfcWorker { | struct NfcWorker { | ||||||
|     osThreadAttr_t thread_attr; |     FuriThread* thread; | ||||||
|     osThreadId_t thread; |  | ||||||
| 
 | 
 | ||||||
|     NfcDeviceData* dev_data; |     NfcDeviceData* dev_data; | ||||||
| 
 | 
 | ||||||
| @ -30,7 +29,7 @@ struct NfcWorker { | |||||||
| 
 | 
 | ||||||
| void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state); | void nfc_worker_change_state(NfcWorker* nfc_worker, NfcWorkerState state); | ||||||
| 
 | 
 | ||||||
| void nfc_worker_task(void* context); | int32_t nfc_worker_task(void* context); | ||||||
| 
 | 
 | ||||||
| void nfc_worker_read_emv_app(NfcWorker* nfc_worker); | void nfc_worker_read_emv_app(NfcWorker* nfc_worker); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -288,6 +288,9 @@ static void print_heap_init() { | |||||||
| static void print_heap_malloc(void* ptr, size_t size) { | static void print_heap_malloc(void* ptr, size_t size) { | ||||||
|     char tmp_str[33]; |     char tmp_str[33]; | ||||||
|     const char* name = osThreadGetName(osThreadGetId()); |     const char* name = osThreadGetName(osThreadGetId()); | ||||||
|  |     if(!name) { | ||||||
|  |         name = ""; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     // {thread name|m|address|size}
 |     // {thread name|m|address|size}
 | ||||||
|     FURI_CRITICAL_ENTER(); |     FURI_CRITICAL_ENTER(); | ||||||
| @ -306,6 +309,9 @@ static void print_heap_malloc(void* ptr, size_t size) { | |||||||
| static void print_heap_free(void* ptr) { | static void print_heap_free(void* ptr) { | ||||||
|     char tmp_str[33]; |     char tmp_str[33]; | ||||||
|     const char* name = osThreadGetName(osThreadGetId()); |     const char* name = osThreadGetName(osThreadGetId()); | ||||||
|  |     if(!name) { | ||||||
|  |         name = ""; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     // {thread name|f|address}
 |     // {thread name|f|address}
 | ||||||
|     FURI_CRITICAL_ENTER(); |     FURI_CRITICAL_ENTER(); | ||||||
|  | |||||||
| @ -578,8 +578,10 @@ const char *osThreadGetName (osThreadId_t thread_id) { | |||||||
| 
 | 
 | ||||||
|   if ((IRQ_Context() != 0U) || (hTask == NULL)) { |   if ((IRQ_Context() != 0U) || (hTask == NULL)) { | ||||||
|     name = NULL; |     name = NULL; | ||||||
|   } else { |   } else if(osKernelGetState() == osKernelRunning) { | ||||||
|     name = pcTaskGetName (hTask); |     name = pcTaskGetName (hTask); | ||||||
|  |   } else { | ||||||
|  |     name = NULL; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* Return name as null-terminated string */ |   /* Return name as null-terminated string */ | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 あく
						あく