 4a1695ba1c
			
		
	
	
		4a1695ba1c
		
			
		
	
	
	
	
		
			
			* RPC App control commands * Button release timeout * SubGhz tx fix Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
		
			
				
	
	
		
			294 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			294 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "nfc_i.h"
 | |
| #include "furi_hal_nfc.h"
 | |
| 
 | |
| bool nfc_custom_event_callback(void* context, uint32_t event) {
 | |
|     furi_assert(context);
 | |
|     Nfc* nfc = context;
 | |
|     return scene_manager_handle_custom_event(nfc->scene_manager, event);
 | |
| }
 | |
| 
 | |
| bool nfc_back_event_callback(void* context) {
 | |
|     furi_assert(context);
 | |
|     Nfc* nfc = context;
 | |
|     return scene_manager_handle_back_event(nfc->scene_manager);
 | |
| }
 | |
| 
 | |
| void nfc_tick_event_callback(void* context) {
 | |
|     furi_assert(context);
 | |
|     Nfc* nfc = context;
 | |
|     scene_manager_handle_tick_event(nfc->scene_manager);
 | |
| }
 | |
| 
 | |
| void nfc_rpc_exit_callback(Nfc* nfc) {
 | |
|     if(nfc->rpc_state == NfcRpcStateEmulating) {
 | |
|         // Stop worker
 | |
|         nfc_worker_stop(nfc->worker);
 | |
|     } else if(nfc->rpc_state == NfcRpcStateEmulated) {
 | |
|         // Stop worker
 | |
|         nfc_worker_stop(nfc->worker);
 | |
|         // Save data in shadow file
 | |
|         nfc_device_save_shadow(nfc->dev, nfc->dev->dev_name);
 | |
|     }
 | |
|     if(nfc->rpc_ctx) {
 | |
|         rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
 | |
|         nfc->rpc_ctx = NULL;
 | |
|     }
 | |
| }
 | |
| 
 | |
| static void nfc_rpc_emulate_callback(NfcWorkerEvent event, void* context) {
 | |
|     UNUSED(event);
 | |
|     Nfc* nfc = context;
 | |
| 
 | |
|     nfc->rpc_state = NfcRpcStateEmulated;
 | |
| }
 | |
| 
 | |
| static bool nfc_rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
 | |
|     furi_assert(context);
 | |
|     Nfc* nfc = context;
 | |
| 
 | |
|     if(!nfc->rpc_ctx) {
 | |
|         return false;
 | |
|     }
 | |
| 
 | |
|     bool result = false;
 | |
| 
 | |
|     if(event == RpcAppEventSessionClose) {
 | |
|         rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
 | |
|         nfc->rpc_ctx = NULL;
 | |
|         view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
 | |
|         result = true;
 | |
|     } else if(event == RpcAppEventAppExit) {
 | |
|         view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
 | |
|         result = true;
 | |
|     } else if(event == RpcAppEventLoadFile) {
 | |
|         if((arg) && (nfc->rpc_state == NfcRpcStateIdle)) {
 | |
|             if(nfc_device_load(nfc->dev, arg, false)) {
 | |
|                 if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
 | |
|                     nfc_worker_start(
 | |
|                         nfc->worker,
 | |
|                         NfcWorkerStateEmulateMifareUltralight,
 | |
|                         &nfc->dev->dev_data,
 | |
|                         nfc_rpc_emulate_callback,
 | |
|                         nfc);
 | |
|                 } else if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {
 | |
|                     nfc_worker_start(
 | |
|                         nfc->worker,
 | |
|                         NfcWorkerStateEmulateMifareClassic,
 | |
|                         &nfc->dev->dev_data,
 | |
|                         nfc_rpc_emulate_callback,
 | |
|                         nfc);
 | |
|                 } else {
 | |
|                     nfc_worker_start(
 | |
|                         nfc->worker, NfcWorkerStateEmulate, &nfc->dev->dev_data, NULL, nfc);
 | |
|                 }
 | |
|                 nfc->rpc_state = NfcRpcStateEmulating;
 | |
|                 result = true;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     return result;
 | |
| }
 | |
| 
 | |
| Nfc* nfc_alloc() {
 | |
|     Nfc* nfc = malloc(sizeof(Nfc));
 | |
| 
 | |
|     nfc->worker = nfc_worker_alloc();
 | |
|     nfc->view_dispatcher = view_dispatcher_alloc();
 | |
|     nfc->scene_manager = scene_manager_alloc(&nfc_scene_handlers, nfc);
 | |
|     view_dispatcher_enable_queue(nfc->view_dispatcher);
 | |
|     view_dispatcher_set_event_callback_context(nfc->view_dispatcher, nfc);
 | |
|     view_dispatcher_set_custom_event_callback(nfc->view_dispatcher, nfc_custom_event_callback);
 | |
|     view_dispatcher_set_navigation_event_callback(nfc->view_dispatcher, nfc_back_event_callback);
 | |
|     view_dispatcher_set_tick_event_callback(nfc->view_dispatcher, nfc_tick_event_callback, 100);
 | |
| 
 | |
|     // Nfc device
 | |
|     nfc->dev = nfc_device_alloc();
 | |
| 
 | |
|     // Open GUI record
 | |
|     nfc->gui = furi_record_open("gui");
 | |
|     view_dispatcher_attach_to_gui(nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);
 | |
| 
 | |
|     // Open Notification record
 | |
|     nfc->notifications = furi_record_open("notification");
 | |
| 
 | |
|     // Submenu
 | |
|     nfc->submenu = submenu_alloc();
 | |
|     view_dispatcher_add_view(nfc->view_dispatcher, NfcViewMenu, submenu_get_view(nfc->submenu));
 | |
| 
 | |
|     // Dialog
 | |
|     nfc->dialog_ex = dialog_ex_alloc();
 | |
|     view_dispatcher_add_view(
 | |
|         nfc->view_dispatcher, NfcViewDialogEx, dialog_ex_get_view(nfc->dialog_ex));
 | |
| 
 | |
|     // Popup
 | |
|     nfc->popup = popup_alloc();
 | |
|     view_dispatcher_add_view(nfc->view_dispatcher, NfcViewPopup, popup_get_view(nfc->popup));
 | |
| 
 | |
|     // Text Input
 | |
|     nfc->text_input = text_input_alloc();
 | |
|     view_dispatcher_add_view(
 | |
|         nfc->view_dispatcher, NfcViewTextInput, text_input_get_view(nfc->text_input));
 | |
| 
 | |
|     // Byte Input
 | |
|     nfc->byte_input = byte_input_alloc();
 | |
|     view_dispatcher_add_view(
 | |
|         nfc->view_dispatcher, NfcViewByteInput, byte_input_get_view(nfc->byte_input));
 | |
| 
 | |
|     // TextBox
 | |
|     nfc->text_box = text_box_alloc();
 | |
|     view_dispatcher_add_view(
 | |
|         nfc->view_dispatcher, NfcViewTextBox, text_box_get_view(nfc->text_box));
 | |
|     string_init(nfc->text_box_store);
 | |
| 
 | |
|     // Custom Widget
 | |
|     nfc->widget = widget_alloc();
 | |
|     view_dispatcher_add_view(nfc->view_dispatcher, NfcViewWidget, widget_get_view(nfc->widget));
 | |
| 
 | |
|     // Bank Card
 | |
|     nfc->bank_card = bank_card_alloc();
 | |
|     view_dispatcher_add_view(
 | |
|         nfc->view_dispatcher, NfcViewBankCard, bank_card_get_view(nfc->bank_card));
 | |
| 
 | |
|     // Dict Attack
 | |
|     nfc->dict_attack = dict_attack_alloc();
 | |
|     view_dispatcher_add_view(
 | |
|         nfc->view_dispatcher, NfcViewDictAttack, dict_attack_get_view(nfc->dict_attack));
 | |
| 
 | |
|     // Generator
 | |
|     nfc->generator = NULL;
 | |
| 
 | |
|     return nfc;
 | |
| }
 | |
| 
 | |
| void nfc_free(Nfc* nfc) {
 | |
|     furi_assert(nfc);
 | |
| 
 | |
|     // Nfc device
 | |
|     nfc_device_free(nfc->dev);
 | |
| 
 | |
|     // Submenu
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewMenu);
 | |
|     submenu_free(nfc->submenu);
 | |
| 
 | |
|     // DialogEx
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDialogEx);
 | |
|     dialog_ex_free(nfc->dialog_ex);
 | |
| 
 | |
|     // Popup
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewPopup);
 | |
|     popup_free(nfc->popup);
 | |
| 
 | |
|     // TextInput
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextInput);
 | |
|     text_input_free(nfc->text_input);
 | |
| 
 | |
|     // ByteInput
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewByteInput);
 | |
|     byte_input_free(nfc->byte_input);
 | |
| 
 | |
|     // TextBox
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewTextBox);
 | |
|     text_box_free(nfc->text_box);
 | |
|     string_clear(nfc->text_box_store);
 | |
| 
 | |
|     // Custom Widget
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewWidget);
 | |
|     widget_free(nfc->widget);
 | |
| 
 | |
|     // Bank Card
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewBankCard);
 | |
|     bank_card_free(nfc->bank_card);
 | |
| 
 | |
|     // Dict Attack
 | |
|     view_dispatcher_remove_view(nfc->view_dispatcher, NfcViewDictAttack);
 | |
|     dict_attack_free(nfc->dict_attack);
 | |
| 
 | |
|     // Worker
 | |
|     nfc_worker_stop(nfc->worker);
 | |
|     nfc_worker_free(nfc->worker);
 | |
| 
 | |
|     // View Dispatcher
 | |
|     view_dispatcher_free(nfc->view_dispatcher);
 | |
| 
 | |
|     // Scene Manager
 | |
|     scene_manager_free(nfc->scene_manager);
 | |
| 
 | |
|     // GUI
 | |
|     furi_record_close("gui");
 | |
|     nfc->gui = NULL;
 | |
| 
 | |
|     // Notifications
 | |
|     furi_record_close("notification");
 | |
|     nfc->notifications = NULL;
 | |
| 
 | |
|     free(nfc);
 | |
| }
 | |
| 
 | |
| void nfc_text_store_set(Nfc* nfc, const char* text, ...) {
 | |
|     va_list args;
 | |
|     va_start(args, text);
 | |
| 
 | |
|     vsnprintf(nfc->text_store, sizeof(nfc->text_store), text, args);
 | |
| 
 | |
|     va_end(args);
 | |
| }
 | |
| 
 | |
| void nfc_text_store_clear(Nfc* nfc) {
 | |
|     memset(nfc->text_store, 0, sizeof(nfc->text_store));
 | |
| }
 | |
| 
 | |
| static const NotificationSequence sequence_blink_start_blue = {
 | |
|     &message_blink_start_10,
 | |
|     &message_blink_set_color_blue,
 | |
|     &message_do_not_reset,
 | |
|     NULL,
 | |
| };
 | |
| 
 | |
| static const NotificationSequence sequence_blink_stop = {
 | |
|     &message_blink_stop,
 | |
|     NULL,
 | |
| };
 | |
| 
 | |
| void nfc_blink_start(Nfc* nfc) {
 | |
|     notification_message(nfc->notifications, &sequence_blink_start_blue);
 | |
| }
 | |
| 
 | |
| void nfc_blink_stop(Nfc* nfc) {
 | |
|     notification_message(nfc->notifications, &sequence_blink_stop);
 | |
| }
 | |
| 
 | |
| int32_t nfc_app(void* p) {
 | |
|     Nfc* nfc = nfc_alloc();
 | |
|     char* args = p;
 | |
| 
 | |
|     // Check argument and run corresponding scene
 | |
|     if((*args != '\0')) {
 | |
|         uint32_t rpc_ctx = 0;
 | |
|         if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
 | |
|             nfc->rpc_ctx = (void*)rpc_ctx;
 | |
|             rpc_system_app_set_callback(nfc->rpc_ctx, nfc_rpc_command_callback, nfc);
 | |
|             scene_manager_next_scene(nfc->scene_manager, NfcSceneRpc);
 | |
|         } else if(nfc_device_load(nfc->dev, p, true)) {
 | |
|             if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
 | |
|                 scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateMifareUl);
 | |
|             } else if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {
 | |
|                 scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateMifareClassic);
 | |
|             } else {
 | |
|                 scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateUid);
 | |
|             }
 | |
|         } else {
 | |
|             // Exit app
 | |
|             view_dispatcher_stop(nfc->view_dispatcher);
 | |
|         }
 | |
|     } else {
 | |
|         scene_manager_next_scene(nfc->scene_manager, NfcSceneStart);
 | |
|     }
 | |
| 
 | |
|     view_dispatcher_run(nfc->view_dispatcher);
 | |
| 
 | |
|     nfc_free(nfc);
 | |
| 
 | |
|     return 0;
 | |
| }
 |