Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lfrfid_app.cpp « lfrfid « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 329f052bac029adfb727579d1e3d1f19abcd40a8 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "lfrfid_app.h"
#include "assets_icons.h"
#include <core/common_defines.h>
#include "m-string.h"
#include "scene/lfrfid_app_scene_start.h"
#include "scene/lfrfid_app_scene_read.h"
#include "scene/lfrfid_app_scene_read_success.h"
#include "scene/lfrfid_app_scene_retry_confirm.h"
#include "scene/lfrfid_app_scene_exit_confirm.h"
#include "scene/lfrfid_app_scene_read_menu.h"
#include "scene/lfrfid_app_scene_write.h"
#include "scene/lfrfid_app_scene_write_success.h"
#include "scene/lfrfid_app_scene_emulate.h"
#include "scene/lfrfid_app_scene_save_name.h"
#include "scene/lfrfid_app_scene_save_success.h"
#include "scene/lfrfid_app_scene_select_key.h"
#include "scene/lfrfid_app_scene_saved_key_menu.h"
#include "scene/lfrfid_app_scene_save_data.h"
#include "scene/lfrfid_app_scene_save_type.h"
#include "scene/lfrfid_app_scene_saved_info.h"
#include "scene/lfrfid_app_scene_delete_confirm.h"
#include "scene/lfrfid_app_scene_delete_success.h"
#include "scene/lfrfid_app_scene_rpc.h"

#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>

#include "rpc/rpc_app.h"

const char* LfRfidApp::app_folder = ANY_PATH("lfrfid");
const char* LfRfidApp::app_extension = ".rfid";
const char* LfRfidApp::app_filetype = "Flipper RFID key";

LfRfidApp::LfRfidApp()
    : scene_controller{this}
    , notification{"notification"}
    , storage{"storage"}
    , dialogs{"dialogs"}
    , text_store(40) {
    string_init_set_str(file_path, app_folder);
}

LfRfidApp::~LfRfidApp() {
    string_clear(file_path);
    if(rpc_ctx) {
        rpc_system_app_set_callback(rpc_ctx, NULL, NULL);
        rpc_system_app_send_exited(rpc_ctx);
    }
}

static bool rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
    furi_assert(context);
    LfRfidApp* app = static_cast<LfRfidApp*>(context);

    bool result = false;

    if(event == RpcAppEventSessionClose) {
        rpc_system_app_set_callback(app->rpc_ctx, NULL, NULL);
        app->rpc_ctx = NULL;
        LfRfidApp::Event event;
        event.type = LfRfidApp::EventType::Exit;
        app->view_controller.send_event(&event);
        result = true;
    } else if(event == RpcAppEventAppExit) {
        LfRfidApp::Event event;
        event.type = LfRfidApp::EventType::Exit;
        app->view_controller.send_event(&event);
        result = true;
    } else if(event == RpcAppEventLoadFile) {
        if(arg) {
            string_set_str(app->file_path, arg);
            if(app->load_key_data(app->file_path, &(app->worker.key), false)) {
                LfRfidApp::Event event;
                event.type = LfRfidApp::EventType::EmulateStart;
                app->view_controller.send_event(&event);
                app->worker.start_emulate();
                result = true;
            }
        }
    }

    return result;
}

void LfRfidApp::run(void* _args) {
    const char* args = reinterpret_cast<const char*>(_args);

    make_app_folder();

    if(strlen(args)) {
        uint32_t rpc_ctx_ptr = 0;
        if(sscanf(args, "RPC %lX", &rpc_ctx_ptr) == 1) {
            rpc_ctx = (RpcAppSystem*)rpc_ctx_ptr;
            rpc_system_app_set_callback(rpc_ctx, rpc_command_callback, this);
            rpc_system_app_send_started(rpc_ctx);
            scene_controller.add_scene(SceneType::Rpc, new LfRfidAppSceneRpc());
            scene_controller.process(100, SceneType::Rpc);
        } else {
            string_set_str(file_path, args);
            load_key_data(file_path, &worker.key, true);
            scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate());
            scene_controller.process(100, SceneType::Emulate);
        }

    } else {
        scene_controller.add_scene(SceneType::Start, new LfRfidAppSceneStart());
        scene_controller.add_scene(SceneType::Read, new LfRfidAppSceneRead());
        scene_controller.add_scene(SceneType::RetryConfirm, new LfRfidAppSceneRetryConfirm());
        scene_controller.add_scene(SceneType::ExitConfirm, new LfRfidAppSceneExitConfirm());
        scene_controller.add_scene(SceneType::ReadSuccess, new LfRfidAppSceneReadSuccess());
        scene_controller.add_scene(SceneType::ReadKeyMenu, new LfRfidAppSceneReadKeyMenu());
        scene_controller.add_scene(SceneType::Write, new LfRfidAppSceneWrite());
        scene_controller.add_scene(SceneType::WriteSuccess, new LfRfidAppSceneWriteSuccess());
        scene_controller.add_scene(SceneType::Emulate, new LfRfidAppSceneEmulate());
        scene_controller.add_scene(SceneType::SaveName, new LfRfidAppSceneSaveName());
        scene_controller.add_scene(SceneType::SaveSuccess, new LfRfidAppSceneSaveSuccess());
        scene_controller.add_scene(SceneType::SelectKey, new LfRfidAppSceneSelectKey());
        scene_controller.add_scene(SceneType::SavedKeyMenu, new LfRfidAppSceneSavedKeyMenu());
        scene_controller.add_scene(SceneType::SaveData, new LfRfidAppSceneSaveData());
        scene_controller.add_scene(SceneType::SaveType, new LfRfidAppSceneSaveType());
        scene_controller.add_scene(SceneType::SavedInfo, new LfRfidAppSceneSavedInfo());
        scene_controller.add_scene(SceneType::DeleteConfirm, new LfRfidAppSceneDeleteConfirm());
        scene_controller.add_scene(SceneType::DeleteSuccess, new LfRfidAppSceneDeleteSuccess());
        scene_controller.process(100);
    }
}

bool LfRfidApp::save_key(RfidKey* key) {
    bool result = false;

    make_app_folder();

    if(string_end_with_str_p(file_path, app_extension)) {
        size_t filename_start = string_search_rchar(file_path, '/');
        string_left(file_path, filename_start);
    }

    string_cat_printf(file_path, "/%s%s", key->get_name(), app_extension);

    result = save_key_data(file_path, key);
    return result;
}

bool LfRfidApp::load_key_from_file_select(bool need_restore) {
    if(!need_restore) {
        string_set_str(file_path, app_folder);
    }

    bool result = dialog_file_browser_show(
        dialogs, file_path, file_path, app_extension, true, &I_125_10px, true);

    if(result) {
        result = load_key_data(file_path, &worker.key, true);
    }

    return result;
}

bool LfRfidApp::delete_key(RfidKey* key) {
    UNUSED(key);
    return storage_simply_remove(storage, string_get_cstr(file_path));
}

bool LfRfidApp::load_key_data(string_t path, RfidKey* key, bool show_dialog) {
    FlipperFormat* file = flipper_format_file_alloc(storage);
    bool result = false;
    string_t str_result;
    string_init(str_result);

    do {
        if(!flipper_format_file_open_existing(file, string_get_cstr(path))) break;

        // header
        uint32_t version;
        if(!flipper_format_read_header(file, str_result, &version)) break;
        if(string_cmp_str(str_result, app_filetype) != 0) break;
        if(version != 1) break;

        // key type
        LfrfidKeyType type;
        RfidKey loaded_key;

        if(!flipper_format_read_string(file, "Key type", str_result)) break;
        if(!lfrfid_key_get_string_type(string_get_cstr(str_result), &type)) break;
        loaded_key.set_type(type);

        // key data
        uint8_t key_data[loaded_key.get_type_data_count()] = {};
        if(!flipper_format_read_hex(file, "Data", key_data, loaded_key.get_type_data_count()))
            break;
        loaded_key.set_data(key_data, loaded_key.get_type_data_count());

        path_extract_filename(path, str_result, true);
        loaded_key.set_name(string_get_cstr(str_result));

        *key = loaded_key;
        result = true;
    } while(0);

    flipper_format_free(file);
    string_clear(str_result);

    if((!result) && (show_dialog)) {
        dialog_message_show_storage_error(dialogs, "Cannot load\nkey file");
    }

    return result;
}

bool LfRfidApp::save_key_data(string_t path, RfidKey* key) {
    FlipperFormat* file = flipper_format_file_alloc(storage);
    bool result = false;

    do {
        if(!flipper_format_file_open_always(file, string_get_cstr(path))) break;
        if(!flipper_format_write_header_cstr(file, app_filetype, 1)) break;
        if(!flipper_format_write_comment_cstr(file, "Key type can be EM4100, H10301 or I40134"))
            break;
        if(!flipper_format_write_string_cstr(
               file, "Key type", lfrfid_key_get_type_string(key->get_type())))
            break;
        if(!flipper_format_write_comment_cstr(
               file, "Data size for EM4100 is 5, for H10301 is 3, for I40134 is 3"))
            break;
        if(!flipper_format_write_hex(file, "Data", key->get_data(), key->get_type_data_count()))
            break;
        result = true;
    } while(0);

    flipper_format_free(file);

    if(!result) {
        dialog_message_show_storage_error(dialogs, "Cannot save\nkey file");
    }

    return result;
}

void LfRfidApp::make_app_folder() {
    if(!storage_simply_mkdir(storage, app_folder)) {
        dialog_message_show_storage_error(dialogs, "Cannot create\napp folder");
    }
}