unleashed-firmware/applications/external/flipfrid/flipfrid.c

279 lines
10 KiB
C
Raw Normal View History

2022-08-24 19:14:03 +03:00
#include "flipfrid.h"
2022-09-01 18:03:18 +03:00
#include "scene/flipfrid_scene_entrypoint.h"
#include "scene/flipfrid_scene_load_file.h"
#include "scene/flipfrid_scene_select_field.h"
#include "scene/flipfrid_scene_run_attack.h"
2022-09-05 00:32:58 +03:00
#include "scene/flipfrid_scene_load_custom_uids.h"
2022-08-24 19:14:03 +03:00
2022-09-05 14:05:43 +03:00
#define RFIDFUZZER_APP_FOLDER "/ext/rfidfuzzer"
2022-08-24 19:14:03 +03:00
static void flipfrid_draw_callback(Canvas* const canvas, void* ctx) {
furi_assert(ctx);
FlipFridState* flipfrid_state = ctx;
furi_mutex_acquire(flipfrid_state->mutex, FuriWaitForever);
2022-08-24 19:14:03 +03:00
2022-09-01 18:03:18 +03:00
// Draw correct Canvas
switch(flipfrid_state->current_scene) {
case NoneScene:
case SceneEntryPoint:
flipfrid_scene_entrypoint_on_draw(canvas, flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-01 18:03:18 +03:00
case SceneSelectFile:
flipfrid_scene_load_file_on_draw(canvas, flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-01 18:03:18 +03:00
case SceneSelectField:
flipfrid_scene_select_field_on_draw(canvas, flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-01 18:03:18 +03:00
case SceneAttack:
flipfrid_scene_run_attack_on_draw(canvas, flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-05 00:32:58 +03:00
case SceneLoadCustomUids:
flipfrid_scene_load_custom_uids_on_draw(canvas, flipfrid_state);
break;
2022-08-24 19:14:03 +03:00
}
furi_mutex_release(flipfrid_state->mutex);
2022-08-24 19:14:03 +03:00
}
void flipfrid_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
FlipFridEvent event = {
.evt_type = EventTypeKey, .key = input_event->key, .input_type = input_event->type};
furi_message_queue_put(event_queue, &event, 25);
}
static void flipfrid_timer_callback(FuriMessageQueue* event_queue) {
furi_assert(event_queue);
FlipFridEvent event = {
.evt_type = EventTypeTick, .key = InputKeyUp, .input_type = InputTypeRelease};
furi_message_queue_put(event_queue, &event, 25);
}
2022-09-01 18:03:18 +03:00
FlipFridState* flipfrid_alloc() {
FlipFridState* flipfrid = malloc(sizeof(FlipFridState));
2022-10-05 21:27:13 +03:00
flipfrid->notification_msg = furi_string_alloc();
flipfrid->attack_name = furi_string_alloc();
2022-10-06 02:30:40 +03:00
flipfrid->proto_name = furi_string_alloc();
flipfrid->data_str = furi_string_alloc();
2022-09-01 18:03:18 +03:00
flipfrid->main_menu_items[0] = furi_string_alloc_set("Default Values");
flipfrid->main_menu_items[1] = furi_string_alloc_set("BF Customer ID");
flipfrid->main_menu_items[2] = furi_string_alloc_set("Load File");
flipfrid->main_menu_items[3] = furi_string_alloc_set("Load UIDs from file");
flipfrid->main_menu_proto_items[0] = furi_string_alloc_set("EM4100");
flipfrid->main_menu_proto_items[1] = furi_string_alloc_set("HIDProx");
flipfrid->main_menu_proto_items[2] = furi_string_alloc_set("PAC/Stanley");
flipfrid->main_menu_proto_items[3] = furi_string_alloc_set("H10301");
2022-09-01 18:03:18 +03:00
flipfrid->previous_scene = NoneScene;
flipfrid->current_scene = SceneEntryPoint;
flipfrid->is_running = true;
flipfrid->is_attacking = false;
flipfrid->key_index = 0;
flipfrid->menu_index = 0;
flipfrid->menu_proto_index = 0;
2022-09-01 18:03:18 +03:00
flipfrid->attack = FlipFridAttackDefaultValues;
flipfrid->notify = furi_record_open(RECORD_NOTIFICATION);
flipfrid->data[0] = 0x00;
flipfrid->data[1] = 0x00;
flipfrid->data[2] = 0x00;
flipfrid->data[3] = 0x00;
flipfrid->data[4] = 0x00;
flipfrid->data[5] = 0x00;
2022-09-01 18:03:18 +03:00
flipfrid->payload[0] = 0x00;
flipfrid->payload[1] = 0x00;
flipfrid->payload[2] = 0x00;
flipfrid->payload[3] = 0x00;
flipfrid->payload[4] = 0x00;
flipfrid->payload[5] = 0x00;
2022-09-01 18:03:18 +03:00
//Dialog
flipfrid->dialogs = furi_record_open(RECORD_DIALOGS);
return flipfrid;
}
void flipfrid_free(FlipFridState* flipfrid) {
//Dialog
furi_record_close(RECORD_DIALOGS);
notification_message(flipfrid->notify, &sequence_blink_stop);
2022-09-05 14:05:43 +03:00
// Strings
2022-10-05 21:27:13 +03:00
furi_string_free(flipfrid->notification_msg);
furi_string_free(flipfrid->attack_name);
2022-10-06 02:30:40 +03:00
furi_string_free(flipfrid->proto_name);
furi_string_free(flipfrid->data_str);
2022-09-01 18:03:18 +03:00
for(uint32_t i = 0; i < 4; i++) {
furi_string_free(flipfrid->main_menu_items[i]);
}
for(uint32_t i = 0; i < 4; i++) {
furi_string_free(flipfrid->main_menu_proto_items[i]);
}
2022-09-01 18:03:18 +03:00
free(flipfrid->data);
free(flipfrid->payload);
// The rest
free(flipfrid);
}
2022-08-24 19:14:03 +03:00
// ENTRYPOINT
int32_t flipfrid_start(void* p) {
2022-09-01 18:46:08 +03:00
UNUSED(p);
2022-08-24 19:14:03 +03:00
// Input
2022-09-01 18:03:18 +03:00
FURI_LOG_I(TAG, "Initializing input");
2022-08-24 19:14:03 +03:00
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(FlipFridEvent));
2022-09-01 18:03:18 +03:00
FlipFridState* flipfrid_state = flipfrid_alloc();
2022-08-24 19:14:03 +03:00
flipfrid_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
if(!flipfrid_state->mutex) {
2022-09-01 18:03:18 +03:00
FURI_LOG_E(TAG, "cannot create mutex\r\n");
2022-08-24 19:14:03 +03:00
furi_message_queue_free(event_queue);
furi_record_close(RECORD_NOTIFICATION);
2022-10-06 02:30:40 +03:00
flipfrid_free(flipfrid_state);
return 255;
2022-08-24 19:14:03 +03:00
}
2022-09-05 14:05:43 +03:00
Storage* storage = furi_record_open(RECORD_STORAGE);
if(!storage_simply_mkdir(storage, RFIDFUZZER_APP_FOLDER)) {
FURI_LOG_E(TAG, "Could not create folder %s", RFIDFUZZER_APP_FOLDER);
}
furi_record_close(RECORD_STORAGE);
2022-08-24 19:14:03 +03:00
// Configure view port
2022-09-01 18:03:18 +03:00
FURI_LOG_I(TAG, "Initializing viewport");
2022-08-24 19:14:03 +03:00
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, flipfrid_draw_callback, flipfrid_state);
2022-08-24 19:14:03 +03:00
view_port_input_callback_set(view_port, flipfrid_input_callback, event_queue);
// Configure timer
2022-09-01 18:03:18 +03:00
FURI_LOG_I(TAG, "Initializing timer");
2022-08-24 19:14:03 +03:00
FuriTimer* timer =
furi_timer_alloc(flipfrid_timer_callback, FuriTimerTypePeriodic, event_queue);
2022-09-01 18:03:18 +03:00
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 10); // 10 times per second
2022-08-24 19:14:03 +03:00
// Register view port in GUI
2022-09-01 18:03:18 +03:00
FURI_LOG_I(TAG, "Initializing gui");
2022-08-24 19:14:03 +03:00
Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
// Init values
FlipFridEvent event;
2022-09-01 18:03:18 +03:00
while(flipfrid_state->is_running) {
2022-08-24 19:14:03 +03:00
// Get next event
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 25);
if(event_status == FuriStatusOk) {
if(event.evt_type == EventTypeKey) {
2022-09-01 18:03:18 +03:00
//Handle event key
switch(flipfrid_state->current_scene) {
case NoneScene:
case SceneEntryPoint:
flipfrid_scene_entrypoint_on_event(event, flipfrid_state);
break;
case SceneSelectFile:
flipfrid_scene_load_file_on_event(event, flipfrid_state);
break;
case SceneSelectField:
flipfrid_scene_select_field_on_event(event, flipfrid_state);
break;
case SceneAttack:
flipfrid_scene_run_attack_on_event(event, flipfrid_state);
break;
2022-09-05 00:32:58 +03:00
case SceneLoadCustomUids:
flipfrid_scene_load_custom_uids_on_event(event, flipfrid_state);
break;
2022-09-01 18:03:18 +03:00
}
} else if(event.evt_type == EventTypeTick) {
//Handle event tick
if(flipfrid_state->current_scene != flipfrid_state->previous_scene) {
// Trigger Exit Scene
switch(flipfrid_state->previous_scene) {
case SceneEntryPoint:
flipfrid_scene_entrypoint_on_exit(flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-01 18:03:18 +03:00
case SceneSelectFile:
flipfrid_scene_load_file_on_exit(flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-01 18:03:18 +03:00
case SceneSelectField:
flipfrid_scene_select_field_on_exit(flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-01 18:03:18 +03:00
case SceneAttack:
flipfrid_scene_run_attack_on_exit(flipfrid_state);
2022-08-24 19:14:03 +03:00
break;
2022-09-05 00:32:58 +03:00
case SceneLoadCustomUids:
flipfrid_scene_load_custom_uids_on_exit(flipfrid_state);
break;
2022-09-01 18:03:18 +03:00
case NoneScene:
2022-08-24 19:14:03 +03:00
break;
}
2022-09-01 18:03:18 +03:00
// Trigger Entry Scene
switch(flipfrid_state->current_scene) {
case NoneScene:
case SceneEntryPoint:
flipfrid_scene_entrypoint_on_enter(flipfrid_state);
break;
case SceneSelectFile:
flipfrid_scene_load_file_on_enter(flipfrid_state);
break;
case SceneSelectField:
flipfrid_scene_select_field_on_enter(flipfrid_state);
break;
case SceneAttack:
flipfrid_scene_run_attack_on_enter(flipfrid_state);
break;
2022-09-05 00:32:58 +03:00
case SceneLoadCustomUids:
flipfrid_scene_load_custom_uids_on_enter(flipfrid_state);
break;
2022-08-24 19:14:03 +03:00
}
2022-09-01 18:03:18 +03:00
flipfrid_state->previous_scene = flipfrid_state->current_scene;
}
// Trigger Tick Scene
switch(flipfrid_state->current_scene) {
case NoneScene:
case SceneEntryPoint:
flipfrid_scene_entrypoint_on_tick(flipfrid_state);
break;
case SceneSelectFile:
flipfrid_scene_load_file_on_tick(flipfrid_state);
break;
case SceneSelectField:
flipfrid_scene_select_field_on_tick(flipfrid_state);
break;
case SceneAttack:
flipfrid_scene_run_attack_on_tick(flipfrid_state);
break;
2022-09-05 00:32:58 +03:00
case SceneLoadCustomUids:
flipfrid_scene_load_custom_uids_on_tick(flipfrid_state);
break;
2022-08-24 19:14:03 +03:00
}
view_port_update(view_port);
}
}
}
// Cleanup
furi_timer_stop(timer);
furi_timer_free(timer);
2022-09-01 18:03:18 +03:00
FURI_LOG_I(TAG, "Cleaning up");
2022-08-24 19:14:03 +03:00
gui_remove_view_port(gui, view_port);
view_port_free(view_port);
furi_message_queue_free(event_queue);
furi_record_close(RECORD_GUI);
furi_record_close(RECORD_NOTIFICATION);
furi_mutex_free(flipfrid_state->mutex);
2022-09-01 18:03:18 +03:00
flipfrid_free(flipfrid_state);
2022-08-24 19:14:03 +03:00
return 0;
}