 53435579b3
			
		
	
	
		53435579b3
		
			
		
	
	
	
	
		
			
			* fbt, faploader: minimal app module implementation * faploader, libs: moved API hashtable core to flipper_application * example: compound api * lib: flipper_application: naming fixes, doxygen comments * fbt: changed `requires` manifest field behavior for app extensions * examples: refactored plugin apps; faploader: changed new API naming; fbt: changed PLUGIN app type meaning * loader: dropped support for debug apps & plugin menus * moved applications/plugins -> applications/external * Restored x bit on chiplist_convert.py * git: fixed free-dap submodule path * pvs: updated submodule paths * examples: example_advanced_plugins.c: removed potential memory leak on errors * examples: example_plugins: refined requires * fbt: not deploying app modules for debug/sample apps; extra validation for .PLUGIN-type apps * apps: removed cdefines for external apps * fbt: moved ext app path definition * fbt: reworked fap_dist handling; f18: synced api_symbols.csv * fbt: removed resources_paths for extapps * scripts: reworked storage * scripts: reworked runfap.py & selfupdate.py to use new api * wip: fal runner * fbt: moved file packaging into separate module * scripts: storage: fixes * scripts: storage: minor fixes for new api * fbt: changed internal artifact storage details for external apps * scripts: storage: additional fixes and better error reporting; examples: using APP_DATA_PATH() * fbt, scripts: reworked launch_app to deploy plugins; moved old runfap.py to distfap.py * fbt: extra check for plugins descriptors * fbt: additional checks in emitter * fbt: better info message on SDK rebuild * scripts: removed requirements.txt * loader: removed remnants of plugins & debug menus * post-review fixes
		
			
				
	
	
		
			171 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			171 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "nfc_magic_worker_i.h"
 | |
| 
 | |
| #include "lib/magic/magic.h"
 | |
| 
 | |
| #define TAG "NfcMagicWorker"
 | |
| 
 | |
| static void
 | |
|     nfc_magic_worker_change_state(NfcMagicWorker* nfc_magic_worker, NfcMagicWorkerState state) {
 | |
|     furi_assert(nfc_magic_worker);
 | |
| 
 | |
|     nfc_magic_worker->state = state;
 | |
| }
 | |
| 
 | |
| NfcMagicWorker* nfc_magic_worker_alloc() {
 | |
|     NfcMagicWorker* nfc_magic_worker = malloc(sizeof(NfcMagicWorker));
 | |
| 
 | |
|     // Worker thread attributes
 | |
|     nfc_magic_worker->thread =
 | |
|         furi_thread_alloc_ex("NfcMagicWorker", 8192, nfc_magic_worker_task, nfc_magic_worker);
 | |
| 
 | |
|     nfc_magic_worker->callback = NULL;
 | |
|     nfc_magic_worker->context = NULL;
 | |
| 
 | |
|     nfc_magic_worker_change_state(nfc_magic_worker, NfcMagicWorkerStateReady);
 | |
| 
 | |
|     return nfc_magic_worker;
 | |
| }
 | |
| 
 | |
| void nfc_magic_worker_free(NfcMagicWorker* nfc_magic_worker) {
 | |
|     furi_assert(nfc_magic_worker);
 | |
| 
 | |
|     furi_thread_free(nfc_magic_worker->thread);
 | |
|     free(nfc_magic_worker);
 | |
| }
 | |
| 
 | |
| void nfc_magic_worker_stop(NfcMagicWorker* nfc_magic_worker) {
 | |
|     furi_assert(nfc_magic_worker);
 | |
| 
 | |
|     nfc_magic_worker_change_state(nfc_magic_worker, NfcMagicWorkerStateStop);
 | |
|     furi_thread_join(nfc_magic_worker->thread);
 | |
| }
 | |
| 
 | |
| void nfc_magic_worker_start(
 | |
|     NfcMagicWorker* nfc_magic_worker,
 | |
|     NfcMagicWorkerState state,
 | |
|     NfcDeviceData* dev_data,
 | |
|     NfcMagicWorkerCallback callback,
 | |
|     void* context) {
 | |
|     furi_assert(nfc_magic_worker);
 | |
|     furi_assert(dev_data);
 | |
| 
 | |
|     nfc_magic_worker->callback = callback;
 | |
|     nfc_magic_worker->context = context;
 | |
|     nfc_magic_worker->dev_data = dev_data;
 | |
|     nfc_magic_worker_change_state(nfc_magic_worker, state);
 | |
|     furi_thread_start(nfc_magic_worker->thread);
 | |
| }
 | |
| 
 | |
| int32_t nfc_magic_worker_task(void* context) {
 | |
|     NfcMagicWorker* nfc_magic_worker = context;
 | |
| 
 | |
|     if(nfc_magic_worker->state == NfcMagicWorkerStateCheck) {
 | |
|         nfc_magic_worker_check(nfc_magic_worker);
 | |
|     } else if(nfc_magic_worker->state == NfcMagicWorkerStateWrite) {
 | |
|         nfc_magic_worker_write(nfc_magic_worker);
 | |
|     } else if(nfc_magic_worker->state == NfcMagicWorkerStateWipe) {
 | |
|         nfc_magic_worker_wipe(nfc_magic_worker);
 | |
|     }
 | |
| 
 | |
|     nfc_magic_worker_change_state(nfc_magic_worker, NfcMagicWorkerStateReady);
 | |
| 
 | |
|     return 0;
 | |
| }
 | |
| 
 | |
| void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker) {
 | |
|     bool card_found_notified = false;
 | |
|     FuriHalNfcDevData nfc_data = {};
 | |
|     MfClassicData* src_data = &nfc_magic_worker->dev_data->mf_classic_data;
 | |
| 
 | |
|     while(nfc_magic_worker->state == NfcMagicWorkerStateWrite) {
 | |
|         if(furi_hal_nfc_detect(&nfc_data, 200)) {
 | |
|             if(!card_found_notified) {
 | |
|                 nfc_magic_worker->callback(
 | |
|                     NfcMagicWorkerEventCardDetected, nfc_magic_worker->context);
 | |
|                 card_found_notified = true;
 | |
|             }
 | |
|             furi_hal_nfc_sleep();
 | |
| 
 | |
|             if(!magic_wupa()) {
 | |
|                 FURI_LOG_E(TAG, "Not Magic card");
 | |
|                 nfc_magic_worker->callback(
 | |
|                     NfcMagicWorkerEventWrongCard, nfc_magic_worker->context);
 | |
|                 break;
 | |
|             }
 | |
|             if(!magic_data_access_cmd()) {
 | |
|                 FURI_LOG_E(TAG, "Not Magic card");
 | |
|                 nfc_magic_worker->callback(
 | |
|                     NfcMagicWorkerEventWrongCard, nfc_magic_worker->context);
 | |
|                 break;
 | |
|             }
 | |
|             for(size_t i = 0; i < 64; i++) {
 | |
|                 FURI_LOG_D(TAG, "Writing block %d", i);
 | |
|                 if(!magic_write_blk(i, &src_data->block[i])) {
 | |
|                     FURI_LOG_E(TAG, "Failed to write %d block", i);
 | |
|                     nfc_magic_worker->callback(NfcMagicWorkerEventFail, nfc_magic_worker->context);
 | |
|                     break;
 | |
|                 }
 | |
|             }
 | |
|             nfc_magic_worker->callback(NfcMagicWorkerEventSuccess, nfc_magic_worker->context);
 | |
|             break;
 | |
|         } else {
 | |
|             if(card_found_notified) {
 | |
|                 nfc_magic_worker->callback(
 | |
|                     NfcMagicWorkerEventNoCardDetected, nfc_magic_worker->context);
 | |
|                 card_found_notified = false;
 | |
|             }
 | |
|         }
 | |
|         furi_delay_ms(300);
 | |
|     }
 | |
|     magic_deactivate();
 | |
| }
 | |
| 
 | |
| void nfc_magic_worker_check(NfcMagicWorker* nfc_magic_worker) {
 | |
|     bool card_found_notified = false;
 | |
| 
 | |
|     while(nfc_magic_worker->state == NfcMagicWorkerStateCheck) {
 | |
|         if(magic_wupa()) {
 | |
|             if(!card_found_notified) {
 | |
|                 nfc_magic_worker->callback(
 | |
|                     NfcMagicWorkerEventCardDetected, nfc_magic_worker->context);
 | |
|                 card_found_notified = true;
 | |
|             }
 | |
| 
 | |
|             nfc_magic_worker->callback(NfcMagicWorkerEventSuccess, nfc_magic_worker->context);
 | |
|             break;
 | |
|         } else {
 | |
|             if(card_found_notified) {
 | |
|                 nfc_magic_worker->callback(
 | |
|                     NfcMagicWorkerEventNoCardDetected, nfc_magic_worker->context);
 | |
|                 card_found_notified = false;
 | |
|             }
 | |
|         }
 | |
|         furi_delay_ms(300);
 | |
|     }
 | |
|     magic_deactivate();
 | |
| }
 | |
| 
 | |
| void nfc_magic_worker_wipe(NfcMagicWorker* nfc_magic_worker) {
 | |
|     MfClassicBlock block;
 | |
|     memset(&block, 0, sizeof(MfClassicBlock));
 | |
|     block.value[0] = 0x01;
 | |
|     block.value[1] = 0x02;
 | |
|     block.value[2] = 0x03;
 | |
|     block.value[3] = 0x04;
 | |
|     block.value[4] = 0x04;
 | |
|     block.value[5] = 0x08;
 | |
|     block.value[6] = 0x04;
 | |
| 
 | |
|     while(nfc_magic_worker->state == NfcMagicWorkerStateWipe) {
 | |
|         magic_deactivate();
 | |
|         furi_delay_ms(300);
 | |
|         if(!magic_wupa()) continue;
 | |
|         if(!magic_wipe()) continue;
 | |
|         if(!magic_data_access_cmd()) continue;
 | |
|         if(!magic_write_blk(0, &block)) continue;
 | |
|         nfc_magic_worker->callback(NfcMagicWorkerEventSuccess, nfc_magic_worker->context);
 | |
|         break;
 | |
|     }
 | |
|     magic_deactivate();
 | |
| }
 |