Skip to content

Commit

Permalink
[FL-2198], [FL-2161] NFC emulation refactoring (flipperdevices#968)
Browse files Browse the repository at this point in the history
* rfal: add state changed callback
* furi_hal_nfc: add NFC-A emulation API
* nfc: add emulation logger, refactor scenes
* elements: fix text_box element
* gui: fix text box module
* nfc: remove unnecessary buffers
* nfc: introduce emulation callback concept
* nfc: format sources
* bt settings: fix incorrect scene switch
* bt settings: format sources
* Debug: fix x2d import for python 3
* Gui: rename method name widget_clear to widget_reset
* nfc: add nfca emulation handler
* nfc: add global custom events enum
* nfc: UID emulation Data -> Log
* furi_hal_nfc: fix incorrect timings
* u2f, badusb: widget_clear() -> widget_reset()

Co-authored-by: Aleksandr Kutuzov <[email protected]>
  • Loading branch information
gornekich and skotopes authored Feb 2, 2022
1 parent 838df4c commit 8cfd0ea
Show file tree
Hide file tree
Showing 46 changed files with 715 additions and 247 deletions.
2 changes: 1 addition & 1 deletion applications/bad_usb/scenes/bad_usb_scene_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ bool bad_usb_scene_error_on_event(void* context, SceneManagerEvent event) {

void bad_usb_scene_error_on_exit(void* context) {
BadUsbApp* app = context;
widget_clear(app->widget);
widget_reset(app->widget);
}
3 changes: 1 addition & 2 deletions applications/gui/elements.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ void elements_text_box(
line[line_num].height = line_height;
line[line_num].descender = line_descender;
if(total_height_min + line_leading_min > height) {
line_num--;
break;
}
total_height_min += line_leading_min;
Expand Down Expand Up @@ -640,7 +639,7 @@ void elements_text_box(
uint8_t free_pixel_num = height - total_height_min;
uint8_t fill_pixel = 0;
uint8_t j = 1;
line[0].y = line[0].height;
line[0].y = y + line[0].height;
while(fill_pixel < free_pixel_num) {
line[j].y = line[j - 1].y + line[j - 1].leading_min + 1;
fill_pixel++;
Expand Down
46 changes: 23 additions & 23 deletions applications/gui/modules/text_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@

struct TextBox {
View* view;
void* context;
TextBoxExitCallback callback;
};

typedef struct {
const char* text;
char* text_pos;
string_t text_formatted;
size_t scroll_pos;
size_t scroll_num;
int32_t scroll_pos;
int32_t scroll_num;
TextBoxFont font;
TextBoxFocus focus;
bool formatted;
} TextBoxModel;

Expand Down Expand Up @@ -52,12 +51,6 @@ static void text_box_process_up(TextBox* text_box) {
});
}

static void text_box_process_back(TextBox* text_box) {
if(text_box->callback) {
text_box->callback(text_box->context);
}
}

static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
size_t i = 0;
size_t line_width = 0;
Expand All @@ -84,8 +77,18 @@ static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) {
line_num++;
model->text = string_get_cstr(model->text_formatted);
model->text_pos = (char*)model->text;
model->scroll_num = MAX(line_num - 4, 0);
model->scroll_pos = 0;
if(model->focus == TextBoxFocusEnd && line_num > 5) {
// Set text position to 5th line from the end
for(uint8_t i = 0; i < line_num - 5; i++) {
while(*model->text_pos++ != '\n') {
};
}
model->scroll_num = line_num - 4;
model->scroll_pos = line_num - 5;
} else {
model->scroll_num = MAX(line_num - 4, 0);
model->scroll_pos = 0;
}
}

static void text_box_view_draw_callback(Canvas* canvas, void* _model) {
Expand Down Expand Up @@ -119,9 +122,6 @@ static bool text_box_view_input_callback(InputEvent* event, void* context) {
} else if(event->key == InputKeyUp) {
text_box_process_up(text_box);
consumed = true;
} else if(event->key == InputKeyBack) {
text_box_process_back(text_box);
consumed = true;
}
}
return consumed;
Expand Down Expand Up @@ -172,10 +172,9 @@ void text_box_reset(TextBox* text_box) {
model->text = NULL;
string_set_str(model->text_formatted, "");
model->font = TextBoxFontText;
model->focus = TextBoxFocusStart;
return true;
});
text_box->context = NULL;
text_box->callback = NULL;
}

void text_box_set_text(TextBox* text_box, const char* text) {
Expand All @@ -185,6 +184,7 @@ void text_box_set_text(TextBox* text_box, const char* text) {
with_view_model(
text_box->view, (TextBoxModel * model) {
model->text = text;
string_reset(model->text_formatted);
string_reserve(model->text_formatted, strlen(text));
model->formatted = false;
return true;
Expand All @@ -201,12 +201,12 @@ void text_box_set_font(TextBox* text_box, TextBoxFont font) {
});
}

void text_box_set_context(TextBox* text_box, void* context) {
void text_box_set_focus(TextBox* text_box, TextBoxFocus focus) {
furi_assert(text_box);
text_box->context = context;
}

void text_box_set_exit_callback(TextBox* text_box, TextBoxExitCallback callback) {
furi_assert(text_box);
text_box->callback = callback;
with_view_model(
text_box->view, (TextBoxModel * model) {
model->focus = focus;
return true;
});
}
20 changes: 9 additions & 11 deletions applications/gui/modules/text_box.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ extern "C" {

/** TextBox anonymous structure */
typedef struct TextBox TextBox;
typedef void (*TextBoxExitCallback)(void* context);

typedef enum {
TextBoxFontText,
TextBoxFontHex,
} TextBoxFont;

typedef enum {
TextBoxFocusStart,
TextBoxFocusEnd,
} TextBoxFocus;

/** Allocate and initialize text_box
*
* @return TextBox instance
Expand Down Expand Up @@ -60,19 +64,13 @@ void text_box_set_text(TextBox* text_box, const char* text);
*/
void text_box_set_font(TextBox* text_box, TextBoxFont font);

/** Set text_box context
*
* @param text_box TextBox instance
* @param context context pointer
*/
void text_box_set_context(TextBox* text_box, void* context);

/** Set exit callback
/** Set TextBox focus
* @note Use to display from start or from end
*
* @param text_box TextBox instance
* @param callback TextBoxExitCallback callback pointer
* @param focus TextBoxFocus instance
*/
void text_box_set_exit_callback(TextBox* text_box, TextBoxExitCallback callback);
void text_box_set_focus(TextBox* text_box, TextBoxFocus focus);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions applications/gui/modules/widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Widget* widget_alloc() {
return widget;
}

void widget_clear(Widget* widget) {
void widget_reset(Widget* widget) {
furi_assert(widget);

with_view_model(
Expand All @@ -89,7 +89,7 @@ void widget_clear(Widget* widget) {
void widget_free(Widget* widget) {
furi_assert(widget);
// Free all elements
widget_clear(widget);
widget_reset(widget);
// Free elements container
with_view_model(
widget->view, (GuiWidgetModel * model) {
Expand Down
4 changes: 2 additions & 2 deletions applications/gui/modules/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ Widget* widget_alloc();
*/
void widget_free(Widget* widget);

/** Clear Widget
/** Reset Widget
*
* @param widget Widget instance
*/
void widget_clear(Widget* widget);
void widget_reset(Widget* widget);

/** Get Widget view
*
Expand Down
4 changes: 2 additions & 2 deletions applications/ibutton/scene/ibutton_scene_delete_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void iButtonSceneDeleteConfirm::on_exit(iButtonApp* app) {

app->set_text_store("");

widget_clear(widget);
widget_reset(widget);
}

void iButtonSceneDeleteConfirm::widget_callback(
Expand All @@ -91,4 +91,4 @@ void iButtonSceneDeleteConfirm::widget_callback(
}

app->get_view_manager()->send_event(&event);
}
}
2 changes: 1 addition & 1 deletion applications/ibutton/scene/ibutton_scene_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void iButtonSceneInfo::on_exit(iButtonApp* app) {

app->set_text_store("");

widget_clear(widget);
widget_reset(widget);
}

void iButtonSceneInfo::widget_callback(GuiButtonType result, InputType type, void* context) {
Expand Down
11 changes: 11 additions & 0 deletions applications/nfc/helpers/nfc_custom_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

enum NfcCustomEvent {
// Reserve first 100 events for button types and indexes, starting from 0
NfcCustomEventReserved = 100,

NfcCustomEventViewExit,
NfcCustomEventWorkerExit,
NfcCustomEventByteInputDone,
NfcCustomEventTextInputDone,
};
7 changes: 7 additions & 0 deletions applications/nfc/nfc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define NFC_DEV_NAME_MAX_LEN 22
#define NFC_FILE_NAME_MAX_LEN 120
#define NFC_READER_DATA_MAX_SIZE 64

#define NFC_APP_FOLDER "/any/nfc"
#define NFC_APP_EXTENSION ".nfc"
Expand Down Expand Up @@ -54,11 +55,17 @@ typedef struct {
uint16_t currency_code;
} NfcEmvData;

typedef struct {
uint8_t data[NFC_READER_DATA_MAX_SIZE];
uint16_t size;
} NfcReaderRequestData;

typedef struct {
NfcDeviceCommonData nfc_data;
union {
NfcEmvData emv_data;
MifareUlData mf_ul_data;
NfcReaderRequestData reader_data;
};
} NfcDeviceData;

Expand Down
1 change: 1 addition & 0 deletions applications/nfc/nfc_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "views/bank_card.h"

#include <nfc/scenes/nfc_scene.h>
#include <nfc/helpers/nfc_custom_event.h>

#define NFC_SEND_NOTIFICATION_FALSE (0UL)
#define NFC_SEND_NOTIFICATION_TRUE (1UL)
Expand Down
Loading

0 comments on commit 8cfd0ea

Please sign in to comment.