Просмотр исходного кода

unstable changes to verify module on launch

frux-c 2 лет назад
Родитель
Сommit
4bf8dc830b

+ 2 - 2
README.md

@@ -8,8 +8,8 @@ This repository contains a UHF RFID application developed for FlipperZero, a ver
 
 ## Features
 
-- [ ] Read UHF RFID tags.
-- [x] Write UHF RFID tags.
+- [x] Read UHF RFID tags.
+- [ ] Write UHF RFID tags.
 - [ ] Communicate with the YRM100 module to perform UHF RFID operations.
 - [ ] Easy-to-use interface on FlipperZero's display.
 

+ 1 - 0
scenes/uhf_scene_config.h

@@ -1,3 +1,4 @@
+ADD_SCENE(uhf, verify, Verify)
 ADD_SCENE(uhf, start, Start)
 ADD_SCENE(uhf, read_tag, ReadTag)
 ADD_SCENE(uhf, read_tag_success, ReadTagSuccess)

+ 2 - 1
scenes/uhf_scene_read_tag.c

@@ -23,7 +23,8 @@ void uhf_scene_read_tag_on_enter(void* ctx) {
 
     // Start worker
     view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewPopup);
-    uhf_worker_start(uhf_app->worker, UHFWorkerStateDetect, uhf_read_tag_worker_callback, uhf_app);
+    uhf_worker_start(
+        uhf_app->worker, UHFWorkerStateDetectSingle, uhf_read_tag_worker_callback, uhf_app);
 
     uhf_blink_start(uhf_app);
 }

+ 1 - 2
scenes/uhf_scene_read_tag_success.c

@@ -15,7 +15,6 @@ void uhf_scene_read_card_success_widget_callback(GuiButtonType result, InputType
 }
 
 void uhf_scene_read_tag_success_on_enter(void* ctx) {
-    // UNUSED(ctx);
     UHFApp* uhf_app = ctx;
 
     const uint8_t* read_data = uhf_app->worker->data->data->data;
@@ -101,7 +100,7 @@ void uhf_scene_read_tag_success_on_exit(void* ctx) {
     UHFApp* uhf_app = ctx;
 
     // // Stop worker
-    // uhf_worker_stop(uhf_app->worker);
+    uhf_worker_stop(uhf_app->worker);
     // Clear view
     popup_reset(uhf_app->popup);
     // clear widget

+ 1 - 1
scenes/uhf_scene_save_name.c

@@ -49,7 +49,7 @@ bool uhf_scene_save_name_on_event(void* context, SceneManagerEvent event) {
     bool consumed = false;
     if(event.type == SceneManagerEventTypeCustom) {
         if(event.event == UHFCustomEventTextInputDone) {
-            if(uhf_save_data(uhf_data_save, uhf_app->storage, uhf_app->text_store)) {
+            if(uhf_save_read_data(uhf_data_save, uhf_app->storage, uhf_app->text_store)) {
                 scene_manager_next_scene(uhf_app->scene_manager, UHFSceneSaveSuccess);
                 consumed = true;
             } else {

+ 94 - 0
scenes/uhf_scene_verify.c

@@ -0,0 +1,94 @@
+#include "../uhf_app_i.h"
+
+bool verify_success = false;
+
+void uhf_scene_verify_callback_event(UHFWorkerEvent event, void* ctx) {
+    UNUSED(ctx);
+    UHFApp* uhf_app = ctx;
+    if(event == UHFWorkerEventSuccess) {
+        verify_success = true;
+        // FURI_LOG_E("verify", "UHFWorkerEventSuccess");
+        // FURI_LOG_E("verify", "%d", verify_success);
+    } else {
+        // FURI_LOG_E("verify", "UHFWorkerEventFail");
+    }
+    view_dispatcher_send_custom_event(uhf_app->view_dispatcher, UHFCustomEventVerifyDone);
+}
+
+void uhf_scene_verify_widget_callback(GuiButtonType result, InputType type, void* ctx) {
+    furi_assert(ctx);
+    UHFApp* uhf_app = ctx;
+
+    if(type == InputTypeShort) {
+        view_dispatcher_send_custom_event(uhf_app->view_dispatcher, result);
+    }
+}
+
+void uhf_scene_verify_on_enter(void* ctx) {
+    UHFApp* uhf_app = ctx;
+    uhf_worker_start(
+        uhf_app->worker, UHFWorkerStateVerify, uhf_scene_verify_callback_event, uhf_app);
+}
+
+bool uhf_scene_verify_on_event(void* ctx, SceneManagerEvent event) {
+    UHFApp* uhf_app = ctx;
+    bool consumed = false;
+    if(event.event == SceneManagerEventTypeBack) {
+        uhf_app->worker->state = UHFWorkerStateStop;
+    } else if(event.type == SceneManagerEventTypeCustom) {
+        if(event.event == GuiButtonTypeRight) {
+            scene_manager_next_scene(uhf_app->scene_manager, UHFSceneStart);
+            consumed = true;
+        } else if(event.event == UHFCustomEventVerifyDone) {
+            if(verify_success) {
+                // FuriString* temp_str = furi_string_alloc();
+                // UHFResponseData* response_data = uhf_app->worker->data;
+                // UHFData* software_version = uhf_response_data_get_uhf_data(response_data, 0);
+
+                widget_add_string_element(
+                    uhf_app->widget, 64, 5, AlignCenter, AlignCenter, FontPrimary, "Module Info");
+                // furi_string_cat_str(temp_str, "Software Version: ");
+                // for(int i = 0; i < 10; i++) {
+                //     furi_string_cat_printf(temp_str, "%c ", software_version->data[6 + i]);
+                // }
+                // widget_add_string_element(
+                //     uhf_app->widget,
+                //     3,
+                //     10,
+                //     AlignLeft,
+                //     AlignBottom,
+                //     FontSecondary,
+                //     furi_string_get_cstr(temp_str));
+                widget_add_string_element(
+                    uhf_app->widget, 64, 5, AlignCenter, AlignCenter, FontPrimary, "Module Info");
+                widget_add_button_element(
+                    uhf_app->widget,
+                    GuiButtonTypeRight,
+                    "Continue",
+                    uhf_scene_verify_widget_callback,
+                    uhf_app);
+                // furi_string_free(temp_str);
+            } else {
+                widget_add_string_element(
+                    uhf_app->widget,
+                    64,
+                    5,
+                    AlignCenter,
+                    AlignCenter,
+                    FontPrimary,
+                    "No UHF Module found");
+            }
+        }
+    }
+    return consumed;
+}
+
+void uhf_scene_verify_on_exit(void* ctx) {
+    UHFApp* uhf_app = ctx;
+    // // Stop worker
+    uhf_worker_stop(uhf_app->worker);
+    // // Clear view
+    // popup_reset(uhf_app->popup);
+    // clear widget
+    widget_reset(uhf_app->widget);
+}

+ 8 - 16
uhf_app.c

@@ -26,7 +26,7 @@ char* convertToHexString(const uint8_t* array, size_t length) {
     return hexArray;
 }
 
-bool uhf_save_data(UHFResponseData* uhf_response_data, Storage* storage, const char* filename) {
+bool uhf_save_read_data(UHFResponseData* uhf_response_data, Storage* storage, const char* filename) {
     if(!storage_dir_exists(storage, UHF_APPS_DATA_FOLDER)) {
         storage_simply_mkdir(storage, UHF_APPS_DATA_FOLDER);
     }
@@ -170,20 +170,6 @@ void uhf_free(UHFApp* uhf_app) {
     free(uhf_app);
 }
 
-// void uhf_text_store_set(UHFApp* uhf_app, const char* text, ...) {
-//     va_list args;
-//     va_start(args, text);
-
-//     vsnprintf(uhf_app->text_store, sizeof(uhf_app->text_store), text, args);
-
-//     va_end(args);
-// }
-
-// void uhf_text_store_clear(UHFApp* uhf_app) {
-//     memset(uhf_app->text_store, 0, sizeof(uhf_app->text_store));
-// }
-
-//  ==================
 static const NotificationSequence uhf_sequence_blink_start_cyan = {
     &message_blink_start_10,
     &message_blink_set_color_cyan,
@@ -222,9 +208,15 @@ int32_t uhf_app_main(void* ctx) {
     UNUSED(ctx);
     UHFApp* uhf_app = uhf_alloc();
 
-    scene_manager_next_scene(uhf_app->scene_manager, UHFSceneStart);
+    // enable 5v pin
+    furi_hal_power_enable_otg();
+
+    scene_manager_next_scene(uhf_app->scene_manager, UHFSceneVerify);
     view_dispatcher_run(uhf_app->view_dispatcher);
 
+    // disable 5v pin
+    furi_hal_power_disable_otg();
+
     uhf_free(uhf_app);
     return 0;
 }

+ 2 - 1
uhf_app_i.h

@@ -40,6 +40,7 @@ enum UHFCustomEvent {
     // Reserve first 100 events for button types and indexes, starting from 0
     UHFCustomEventReserved = 100,
 
+    UHFCustomEventVerifyDone,
     UHFCustomEventViewExit,
     UHFCustomEventWorkerExit,
     UHFCustomEventByteInputDone,
@@ -104,4 +105,4 @@ bool uhf_is_memset(const uint8_t* data, const uint8_t pattern, size_t size);
 
 char* convertToHexString(const uint8_t* array, size_t length);
 
-bool uhf_save_data(UHFResponseData* uhf_response_data, Storage* storage, const char* filename);
+bool uhf_save_read_data(UHFResponseData* uhf_response_data, Storage* storage, const char* filename);

+ 12 - 1
uhf_data.c

@@ -51,7 +51,7 @@ UHFResponseData* uhf_response_data_alloc() {
     return uhf_response_data;
 }
 
-UHFData* add_uhf_data_to_uhf_response_data(UHFResponseData* uhf_response_data) {
+UHFData* uhf_response_data_add_new_uhf_data(UHFResponseData* uhf_response_data) {
     UHFData* next = uhf_response_data->data;
     while(next->next != NULL) {
         next = next->next;
@@ -61,6 +61,17 @@ UHFData* add_uhf_data_to_uhf_response_data(UHFResponseData* uhf_response_data) {
     return next->next;
 }
 
+UHFData* uhf_response_data_get_uhf_data(UHFResponseData* uhf_response_data, uint index) {
+    if(uhf_response_data == NULL || uhf_response_data->size <= index) return NULL;
+    UHFData* uhf_data = uhf_response_data->data;
+    if(index == 0) return uhf_data;
+    while(uhf_data != NULL && index >= 1) {
+        uhf_data = uhf_data->next;
+        index--;
+    }
+    return uhf_data;
+}
+
 void uhf_response_data_free(UHFResponseData* uhf_response_data) {
     uhf_data_free(uhf_response_data->data);
     free(uhf_response_data);

+ 2 - 1
uhf_data.h

@@ -25,5 +25,6 @@ void uhf_data_reset(UHFData* uhf_data);
 void uhf_data_free(UHFData* uhf_data);
 
 UHFResponseData* uhf_response_data_alloc();
-UHFData* add_uhf_data_to_uhf_response_data(UHFResponseData* uhf_response_data);
+UHFData* uhf_response_data_add_new_uhf_data(UHFResponseData* uhf_response_data);
+UHFData* uhf_response_data_get_uhf_data(UHFResponseData* uhf_response_data, uint index);
 void uhf_response_data_free(UHFResponseData* uhf_response_data);

+ 48 - 8
uhf_worker.c

@@ -1,32 +1,72 @@
 #include "uhf_worker.h"
 #include "uhf_cmd.h"
 
-void single_poll_rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
+// uart callback functions
+void module_rx_callback(UartIrqEvent event, uint8_t data, void* ctx) {
     UNUSED(event);
-    UHFResponseData* response_data = ctx;
-    UHFData* first_data = response_data->data;
-    uhf_data_append(first_data, data);
+    UHFData* uhf_data = ctx;
+    uhf_data_append(uhf_data, data);
+    // FURI_LOG_E("module_rx_callback", "%02x", data);
 }
 
-UHFWorkerEvent read_single_card(UHFWorker* uhf_worker) {
+// yrm100 module commands
+UHFWorkerEvent verify_module_connected(UHFWorker* uhf_worker) {
     UHFResponseData* uhf_response_data = uhf_worker->data;
+    UHFData* hardware_version = uhf_response_data->data;
+    uhf_data_reset(hardware_version);
+    // UHFData* software_version = uhf_response_data_add_new_uhf_data(uhf_response_data);
+    // UHFData* manufacturer = uhf_response_data_add_new_uhf_data(uhf_response_data);
     furi_hal_uart_set_br(FuriHalUartIdUSART1, DEFAULT_BAUD_RATE);
-    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, single_poll_rx_callback, uhf_response_data);
+    // read hardware version
+    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, hardware_version);
+    furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_HARDWARE_VERSION.cmd, CMD_HARDWARE_VERSION.length);
+    furi_delay_ms(200);
+    // // read software version
+    // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, software_version);
+    // furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_SOFTWARE_VERSION.cmd, CMD_SOFTWARE_VERSION.length);
+    // furi_delay_ms(200);
+    // // read manufacturer
+    // furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, manufacturer);
+    // furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_MANUFACTURERS.cmd, CMD_MANUFACTURERS.length);
+    // furi_delay_ms(200);
+
+    if(!hardware_version->end) { //|| !software_version->end || !manufacturer->end) {
+        return UHFWorkerEventFail;
+    }
+    return UHFWorkerEventSuccess;
+}
+
+UHFWorkerEvent read_single_card(UHFWorker* uhf_worker) {
+    UHFResponseData* uhf_response_data = uhf_worker->data;
     UHFData* uhf_data = uhf_response_data->data;
+    furi_hal_uart_set_br(FuriHalUartIdUSART1, DEFAULT_BAUD_RATE);
+    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, module_rx_callback, uhf_data);
     uhf_data_reset(uhf_data);
-    while(!uhf_data->end) {
+    while(true) {
         furi_hal_uart_tx(FuriHalUartIdUSART1, CMD_SINGLE_POLLING.cmd, CMD_SINGLE_POLLING.length);
         furi_delay_ms(100);
         if(uhf_worker->state == UHFWorkerStateStop) {
             return UHFWorkerEventAborted;
         }
+        if(uhf_data->end) {
+            // before breaking, check if the response is not an error
+            // index 1 = response type, index 5 = parameter
+            if(uhf_data->data[1] == 0x01 && uhf_data->data[5] == 0x15) {
+                continue;
+            } else if(uhf_data->data[1] == 0x02)
+                break; // success read
+        }
     }
     return UHFWorkerEventSuccess;
 }
 
 int32_t uhf_worker_task(void* ctx) {
     UHFWorker* uhf_worker = ctx;
-    if(uhf_worker->state == UHFWorkerStateDetect) {
+    if(uhf_worker->state == UHFWorkerStateVerify) {
+        UHFWorkerEvent event = verify_module_connected(uhf_worker);
+        uhf_worker->callback(event, uhf_worker->ctx);
+    }
+    if(uhf_worker->state == UHFWorkerStateDetectSingle) {
         UHFWorkerEvent event = read_single_card(uhf_worker);
         uhf_worker->callback(event, uhf_worker->ctx);
     }

+ 3 - 2
uhf_worker.h

@@ -9,9 +9,10 @@ typedef enum {
     UHFWorkerStateNone,
     UHFWorkerStateBroken,
     UHFWorkerStateReady,
+    UHFWorkerStateVerify,
     // Main worker states
-    UHFWorkerStateDetect,
-    UHFWorkerStateWrite,
+    UHFWorkerStateDetectSingle,
+    UHFWorkerStateWriteSingle,
     UHFWorkerStateWriteKey,
     // Transition
     UHFWorkerStateStop,