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

working on adding furi thread to send read cmd thru uart

frux-c 2 лет назад
Родитель
Сommit
dd80e62dcf
10 измененных файлов с 144 добавлено и 32 удалено
  1. 1 1
      scenes/uhf_scene_config.h
  2. 16 21
      scenes/uhf_scene_read_tag.c
  3. 61 0
      scenes/uhf_scene_read_tag_success.c
  4. 1 1
      scenes/uhf_scene_start.c
  5. 23 0
      uhf_app.c
  6. 3 0
      uhf_app_i.h
  7. 2 0
      uhf_cmd.h
  8. 8 4
      uhf_data.c
  9. 4 1
      uhf_data.h
  10. 25 4
      uhf_worker.c

+ 1 - 1
scenes/uhf_scene_config.h

@@ -1,6 +1,6 @@
 ADD_SCENE(uhf, start, Start)
 ADD_SCENE(uhf, read_tag, ReadTag)
-// ADD_SCENE(uhf, read_card_success, ReadCardSuccess)
+ADD_SCENE(uhf, read_tag_success, ReadTagSuccess)
 // ADD_SCENE(uhf, card_menu, CardMenu)
 // ADD_SCENE(uhf, save_name, SaveName)
 // ADD_SCENE(uhf, save_success, SaveSuccess)

+ 16 - 21
scenes/uhf_scene_read_tag.c

@@ -3,17 +3,12 @@
 
 void uhf_read_tag_worker_callback(UHFWorkerEvent event, void* ctx) {
     UNUSED(event);
-    UNUSED(ctx);
-    // UHFApp* uhf_app = ctx;
-    // testing
-    FURI_LOG_E("THREAD", "uhf_read_tag_worker_callback %d", event);
-    // furi_delay_ms(3000);
-    // scene_manager_set_scene_state(uhf_app->scene_manager, UHFSceneReadTag, UHFSceneStart);
-    // view_dispatcher_send_custom_event(uhf_app->view_dispatcher, UHFCustomEventWorkerExit);
+    UHFApp* uhf_app = ctx;
+    view_dispatcher_send_custom_event(uhf_app->view_dispatcher, UHFCustomEventWorkerExit);
 }
 
 void uhf_scene_read_tag_on_enter(void* ctx) {
-    FURI_LOG_E("33", "uhf_scene_read_tag_on_enter was called!");
+    // FURI_LOG_E("33", "uhf_scene_read_tag_on_enter was called!");
     UHFApp* uhf_app = ctx;
     dolphin_deed(DolphinDeedNfcRead);
 
@@ -33,21 +28,22 @@ void uhf_scene_read_tag_on_enter(void* ctx) {
 }
 
 bool uhf_scene_read_tag_on_event(void* ctx, SceneManagerEvent event) {
-    UNUSED(ctx);
-    // UHFApp* uhf_app = ctx;
+    UHFApp* uhf_app = ctx;
     bool consumed = false;
-    FURI_LOG_E("33", "uhf_scene_read_tag_on_event was called! event.event: %lu", event.event);
+    uint8_t cmd[] = {0xBB, 0x00, 0x22, 0x00, 0x00, 0x22, 0x7E};
+    // while(uhf_worker->state != UHFWorkerStateStop || !first_data->data_full) {
+    FURI_LOG_E("uhf_scene_read_tag_on_event", "sending single polling command");
+    furi_hal_uart_tx(FuriHalUartIdUSART1, cmd, 7);
+    if(uhf_app->worker->data->data->data_full) {
+        scene_manager_next_scene(uhf_app->scene_manager, UHFSceneReadTagSuccess);
+        consumed = true;
+    }
+    // FURI_LOG_E("33", "uhf_scene_read_tag_on_event was called! event.event: %lu", event.event);
     if(event.type == SceneManagerEventTypeCustom) {
-        FURI_LOG_E("36", "SceneManagerEventTypeCustom");
+        // FURI_LOG_E("36", "SceneManagerEventTypeCustom");
         if(event.event == UHFCustomEventWorkerExit) {
-            FURI_LOG_E("38", "UHFCustomEventWorkerExit");
-            // if(memcmp(uhf_app->dev->dev_data.pacs.key, uhf_factory_debit_key, PICOPASS_BLOCK_LEN) ==
-            //    0) {
-            //     scene_manager_next_scene(uhf_app->scene_manager, PicopassSceneReadFactorySuccess);
-            // } else {
-            //     scene_manager_next_scene(uhf_app->scene_manager, PicopassSceneReadCardSuccess);
-            // }
-            // scene_manager_next_scene(uhf_app->scene_manager, UHFSceneStart);
+            uhf_app->worker->state = UHFWorkerStateStop;
+            // scene_manager_next_scene(uhf_app->scene_manager, UHFSceneReadTagSuccess);
             consumed = true;
         }
     }
@@ -56,7 +52,6 @@ bool uhf_scene_read_tag_on_event(void* ctx, SceneManagerEvent event) {
 
 void uhf_scene_read_tag_on_exit(void* ctx) {
     UHFApp* uhf_app = ctx;
-
     // Stop worker
     uhf_worker_stop(uhf_app->worker);
     // Clear view

+ 61 - 0
scenes/uhf_scene_read_tag_success.c

@@ -0,0 +1,61 @@
+#include "../uhf_app_i.h"
+
+void uhf_read_tag_success_worker_callback(UHFWorkerEvent event, void* ctx) {
+    UNUSED(event);
+    UNUSED(ctx);
+}
+
+void uhf_scene_read_tag_success_on_enter(void* ctx) {
+    // UNUSED(ctx);
+    UHFApp* uhf_app = ctx;
+
+    widget_add_string_element(
+        uhf_app->widget,
+        20,
+        20,
+        AlignCenter,
+        AlignCenter,
+        FontPrimary,
+        convertToHexString(uhf_app->worker->data->data->data, uhf_app->worker->data->data->length));
+
+    view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewWidget);
+}
+
+bool uhf_scene_read_tag_success_on_event(void* ctx, SceneManagerEvent event) {
+    UHFApp* uhf_app = ctx;
+    bool consumed = false;
+    // FURI_LOG_E("33", "uhf_scene_read_tag_on_event was called! event.event: %d", event.type);
+    // FURI_LOG_E("33", "uhf_scene_read_tag_on_event was called! event.event: %lu", event.event);
+    if(event.event == SceneManagerEventTypeBack) {
+        // FURI_LOG_E("36", "Back button was pressed");
+        uhf_app->worker->state = UHFWorkerStateStop;
+    }
+    if(event.type == SceneManagerEventTypeCustom) {
+        // FURI_LOG_E("36", "SceneManagerEventTypeCustom");
+        if(event.event == UHFCustomEventWorkerExit) {
+            // FURI_LOG_E("38", "UHFCustomEventWorkerExit");
+            // if(memcmp(uhf_app->dev->dev_data.pacs.key, uhf_factory_debit_key, PICOPASS_BLOCK_LEN) ==
+            //    0) {
+            //     scene_manager_next_scene(uhf_app->scene_manager, PicopassSceneReadFactorySuccess);
+            // } else {
+            //     scene_manager_next_scene(uhf_app->scene_manager, PicopassSceneReadCardSuccess);
+            // }
+            // scene_manager_next_scene(uhf_app->scene_manager, UHFSceneStart);
+            consumed = true;
+        }
+    }
+    return consumed;
+}
+
+void uhf_scene_read_tag_success_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);
+
+    // uhf_blink_stop(uhf_app);
+}

+ 1 - 1
scenes/uhf_scene_start.c

@@ -26,8 +26,8 @@ void uhf_scene_start_on_enter(void* ctx) {
 bool uhf_scene_start_on_event(void* ctx, SceneManagerEvent event) {
     UHFApp* uhf_app = ctx;
     bool consumed = false;
-
     if(event.type == SceneManagerEventTypeCustom) {
+        // FURI_LOG_E("scene_start_on_event", "%lu", event.event);
         if(event.event == SubmenuIndexRead) {
             scene_manager_set_scene_state(uhf_app->scene_manager, UHFSceneStart, SubmenuIndexRead);
             scene_manager_next_scene(uhf_app->scene_manager, UHFSceneReadTag);

+ 23 - 0
uhf_app.c

@@ -1,5 +1,28 @@
 #include "uhf_app_i.h"
 
+char* convertToHexString(const uint8_t* array, size_t length) {
+    if(array == NULL || length == 0) {
+        return NULL;
+    }
+
+    // Each byte takes 3 characters in the hex representation (2 characters + space), plus 1 for the null terminator
+    size_t hexLength = (length * 3) + 1;
+
+    char* hexArray = (char*)malloc(hexLength * sizeof(char));
+    if(hexArray == NULL) {
+        return NULL;
+    }
+
+    size_t index = 0;
+    for(size_t i = 0; i < length; i++) {
+        index += snprintf(&hexArray[index], hexLength - index, "%02x ", array[i]);
+    }
+
+    hexArray[hexLength - 1] = '\0';
+
+    return hexArray;
+}
+
 bool uhf_custom_event_callback(void* ctx, uint32_t event) {
     furi_assert(ctx);
     UHFApp* uhf_app = ctx;

+ 3 - 0
uhf_app_i.h

@@ -4,6 +4,7 @@
 #include <gui/gui.h>
 #include <gui/view_dispatcher.h>
 #include <gui/scene_manager.h>
+#include <gui/canvas_i.h>
 #include <notification/notification_messages.h>
 
 #include <gui/modules/submenu.h>
@@ -90,3 +91,5 @@ void uhf_show_loading_popup(void* context, bool show);
  * @return     True if memory is set to pattern, false otherwise
  */
 bool uhf_is_memset(const uint8_t* data, const uint8_t pattern, size_t size);
+
+char* convertToHexString(const uint8_t* array, size_t length);

+ 2 - 0
uhf_cmd.h

@@ -1,4 +1,5 @@
 #pragma once
+
 #include <stdint.h>
 #include <stdlib.h>
 
@@ -21,6 +22,7 @@ typedef enum{
     KOREA = 6
 }UHFWorkArea;
 
+#define DEFAULT_BAUD_RATE 115200
 #define FRAME_START 0xBB
 #define FRAME_END 0x7E
 #define ERROR 0x15

+ 8 - 4
uhf_data.c

@@ -1,23 +1,27 @@
-#include "uhf_data.h"
 #include <furi.h>
+#include "uhf_data.h"
+// #include "uhf_cmd.h"
+
 UHFData* uhf_data_alloc() {
     UHFData* uhf_data = (UHFData*)malloc(sizeof(UHFData));
     uhf_data->length = 0;
+    uhf_data->data_full = false;
     uhf_data->next = NULL;
     return uhf_data;
 }
 
 int uhf_data_append(UHFData* uhf_data, uint8_t data) {
-    if(uhf_data->length >= 32) return 0;
+    if(uhf_data->length >= MAX_DATA_SIZE || uhf_data->data_full) return 0;
     uhf_data->data[uhf_data->length++] = data;
+    if(data == 0x7E) {
+        uhf_data->data_full = true;
+    }
     return 1;
 }
 
 void uhf_data_free(UHFData* uhf_data) {
     if(uhf_data != NULL) {
-        FURI_LOG_E("LL", "Freeing linked list");
         while(uhf_data != NULL) {
-            FURI_LOG_E("LL", "Freeing node in linked list");
             UHFData* next = uhf_data->next;
             free(uhf_data);
             uhf_data = next;

+ 4 - 1
uhf_data.h

@@ -2,9 +2,12 @@
 #include <stdint.h>
 #include <stdlib.h>
 
+#define MAX_DATA_SIZE 32
+
 typedef struct UHFData {
-    uint8_t data[32];
+    uint8_t data[MAX_DATA_SIZE];
     size_t length;
+    bool data_full;
     struct UHFData* next;
 } UHFData;
 

+ 25 - 4
uhf_worker.c

@@ -1,11 +1,32 @@
 #include "uhf_worker.h"
-#include "uhf_data.h"
+// #include "uhf_cmd.h"
+
+void single_poll_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);
+}
+
+UHFWorkerEvent read_single_card(UHFWorker* uhf_worker) {
+    // UNUSED(uhf_worker);
+    // furi_hal_uart_set_br(FuriHalUartIdUSART1, DEFAULT_BAUD_RATE);
+    furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
+    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, single_poll_rx_callback, uhf_worker->data);
+    // UHFData* first_data = uhf_worker->data->data;
+
+    // furi_delay_ms(1000);
+    // FURI_LOG_E("read_single_card", "waiting on read");
+    // }
+    return UHFWorkerEventSuccess;
+}
 
 int32_t uhf_worker_task(void* ctx) {
     UHFWorker* uhf_worker = ctx;
-    FURI_LOG_E("uhf_worker", "worker callback has been called");
-    if(uhf_worker->callback) {
-        uhf_worker->callback((UHFWorkerEvent)NULL, uhf_worker->ctx);
+    // // FURI_LOG_E("uhf_worker", "worker callback has been called");
+    if(uhf_worker->state == UHFWorkerStateDetect) {
+        UHFWorkerEvent event = read_single_card(uhf_worker);
+        uhf_worker->callback(event, uhf_worker->ctx);
     }
     return 0;
 }