-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfbf.c
More file actions
200 lines (174 loc) · 6.98 KB
/
lfbf.c
File metadata and controls
200 lines (174 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <furi.h>
#include <furi_hal.h>
#include <gui/gui.h>
#include <input/input.h>
#include <gui/elements.h>
#include <toolbox/protocols/protocol_dict.h>
#include <lfrfid/protocols/lfrfid_protocols.h>
#include <lib/lfrfid/lfrfid_worker.h>
#define TAG "lfbf"
#define HEXCHAR(i) ( (i) < 10 ? '0' + (i) : 'A' + (i) - 10 )
typedef struct {
bool active;
uint8_t data[16]; // but we can only print 10 :(
uint8_t data_length; // length in use for the current protocol
int8_t data_current_byte; // which byte (little endian) we're currently editing, -1 == protocol
ProtocolDict* dict;
ProtocolId protocol_id;
LFRFIDWorker* worker;
} LFBFState;
static LFBFState state = {
.active = false,
.data = {0},
.data_length = 16,
.data_current_byte = -1,
.dict = NULL,
.protocol_id = 0,
.worker = NULL,
};
// Screen is 128x64 px
static void app_draw_callback(Canvas* canvas, void* ctx) {
UNUSED(ctx);
const char *protocol_name;
canvas_clear(canvas);
canvas_set_font(canvas, FontKeyboard);
protocol_name = protocol_dict_get_name(state.dict, state.protocol_id);
canvas_draw_str(canvas, 0, 7, protocol_name);
if (state.data_current_byte == -1) {
for (uint8_t i = 0; i < strlen(protocol_name); i++) {
canvas_draw_str(canvas, 6 * i, 7, "_");
}
}
if (state.active) {
canvas_draw_str(canvas, 90, 7, "ACTIVE");
}
for (uint8_t i = 0; i < state.data_length; i++) {
char hex_byte[3];
hex_byte[0] = HEXCHAR(state.data[i] >> 4);
hex_byte[1] = HEXCHAR(state.data[i] & 0xf);
hex_byte[2] = '\0';
canvas_draw_str(canvas, 12*i, 18, hex_byte);
}
if (state.data_current_byte != -1) {
canvas_draw_str(canvas, 12 * state.data_current_byte, 18, "__");
}
canvas_set_font(canvas, FontSecondary);
FuriString* render_data;
render_data = furi_string_alloc();
protocol_dict_render_data(state.dict, render_data, state.protocol_id);
elements_multiline_text(canvas, 0, 29, furi_string_get_cstr(render_data));
furi_string_free(render_data);
}
static void app_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);
FuriMessageQueue* event_queue = ctx;
furi_message_queue_put(event_queue, input_event, FuriWaitForever);
}
void lfbf_emulation_start() {
state.worker = lfrfid_worker_alloc(state.dict);
lfrfid_worker_start_thread(state.worker);
lfrfid_worker_emulate_start(state.worker, state.protocol_id);
state.active = true;
}
void lfbf_emulation_stop() {
if (state.active) {
lfrfid_worker_stop(state.worker);
lfrfid_worker_stop_thread(state.worker);
lfrfid_worker_free(state.worker);
state.worker = NULL;
}
state.active = false;
}
int32_t lfbf_main(void* p) {
UNUSED(p);
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
// Configure view port
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, app_draw_callback, view_port);
view_port_input_callback_set(view_port, app_input_callback, event_queue);
// Register view port in GUI
Gui* gui = furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
// Set up LFBF
state.dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
state.protocol_id = 0;
state.data_length = protocol_dict_get_data_size(state.dict, state.protocol_id);
protocol_dict_get_data(state.dict, state.protocol_id, state.data, state.data_length);
InputEvent event;
bool running = true;
while(running) {
if(furi_message_queue_get(event_queue, &event, 100) == FuriStatusOk) {
if((event.type == InputTypePress) || (event.type == InputTypeRepeat)) {
bool initial_state;
switch(event.key) {
case InputKeyLeft:
state.data_current_byte -= 1;
if (state.data_current_byte == -2) {
state.data_current_byte = state.data_length - 1;
}
break;
case InputKeyRight:
state.data_current_byte += 1;
if (state.data_current_byte == state.data_length) {
state.data_current_byte = -1;
}
break;
case InputKeyUp:
initial_state = state.active;
lfbf_emulation_stop();
if (state.data_current_byte >= 0) {
state.data[state.data_current_byte] += 1;
protocol_dict_set_data(state.dict, state.protocol_id, state.data, state.data_length);
if (initial_state) {
lfbf_emulation_start();
}
FURI_LOG_I(TAG, "set data length %u", state.data_length);
} else {
lfbf_emulation_stop();
state.protocol_id = (state.protocol_id + LFRFIDProtocolMax - 1) % LFRFIDProtocolMax;
state.data_length = protocol_dict_get_data_size(state.dict, state.protocol_id);
protocol_dict_get_data(state.dict, state.protocol_id, state.data, state.data_length);
}
break;
case InputKeyDown:
initial_state = state.active;
lfbf_emulation_stop();
if (state.data_current_byte >= 0) {
state.data[state.data_current_byte] -= 1;
protocol_dict_set_data(state.dict, state.protocol_id, state.data, state.data_length);
if (initial_state) {
lfbf_emulation_start();
}
FURI_LOG_I(TAG, "set data length %u", state.data_length);
} else {
lfbf_emulation_stop();
state.protocol_id = (state.protocol_id + 1) % LFRFIDProtocolMax;
state.data_length = protocol_dict_get_data_size(state.dict, state.protocol_id);
protocol_dict_get_data(state.dict, state.protocol_id, state.data, state.data_length);
}
break;
case InputKeyOk:
if (state.active) {
lfbf_emulation_stop();
} else {
lfbf_emulation_start();
}
break;
default:
running = false;
break;
}
}
}
view_port_update(view_port);
}
// tear down LFBF
lfbf_emulation_stop();
protocol_dict_free(state.dict);
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_message_queue_free(event_queue);
furi_record_close(RECORD_GUI);
return 0;
}