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

lfrfid-cli.cpp « lfrfid « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e47dd99b4da49d175f9a6b9c949cbe3315e421b (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
#include <furi.h>
#include <furi-hal.h>
#include <stdarg.h>
#include <cli/cli.h>
#include <lib/toolbox/args.h>

#include "helpers/rfid-reader.h"
#include "helpers/rfid-timer-emulator.h"

void lfrfid_cli(Cli* cli, string_t args, void* context);

// app cli function
extern "C" void lfrfid_on_system_start() {
#ifdef SRV_CLI
    Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
    cli_add_command(cli, "rfid", CliCommandFlagDefault, lfrfid_cli, NULL);
    furi_record_close("cli");
#endif
}

void lfrfid_cli_print_usage() {
    printf("Usage:\r\n");
    printf("rfid read <optional: normal | indala>\r\n");
    printf("rfid <write | emulate> <key_type> <key_data>\r\n");
    printf("\t<key_type> choose from:\r\n");
    printf("\tEM4100, EM-Marin (5 bytes key_data)\r\n");
    printf("\tH10301, HID26 (3 bytes key_data)\r\n");
    printf("\tI40134, Indala (3 bytes key_data)\r\n");
    printf("\t<key_data> are hex-formatted\r\n");
};

bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
    bool result = false;

    if(string_cmp_str(data, "EM4100") == 0 || string_cmp_str(data, "EM-Marin") == 0) {
        result = true;
        *type = LfrfidKeyType::KeyEM4100;
    } else if(string_cmp_str(data, "H10301") == 0 || string_cmp_str(data, "HID26") == 0) {
        result = true;
        *type = LfrfidKeyType::KeyH10301;
    } else if(string_cmp_str(data, "I40134") == 0 || string_cmp_str(data, "Indala") == 0) {
        result = true;
        *type = LfrfidKeyType::KeyI40134;
    }

    return result;
}

void lfrfid_cli_read(Cli* cli, string_t args) {
    RfidReader reader;
    string_t type_string;
    string_init(type_string);
    bool simple_mode = true;
    LfrfidKeyType type;
    RfidReader::Type reader_type = RfidReader::Type::Normal;
    static const uint8_t data_size = LFRFID_KEY_SIZE;
    uint8_t data[data_size] = {0};

    if(args_read_string_and_trim(args, type_string)) {
        simple_mode = false;

        if(string_cmp_str(type_string, "normal") == 0) {
            reader_type = RfidReader::Type::Normal;
        } else if(string_cmp_str(type_string, "indala") == 0) {
            reader_type = RfidReader::Type::Indala;
        } else {
            lfrfid_cli_print_usage();
            string_clear(type_string);
            return;
        }
    }

    if(simple_mode) {
        reader.start();
    } else {
        reader.start_forced(reader_type);
    }

    printf("Reading RFID...\r\nPress Ctrl+C to abort\r\n");
    while(!cli_cmd_interrupt_received(cli)) {
        if(reader.read(&type, data, data_size, simple_mode)) {
            printf("%s", lfrfid_key_get_type_string(type));
            printf(" ");

            for(uint8_t i = 0; i < lfrfid_key_get_type_data_count(type); i++) {
                printf("%02X", data[i]);
            }
            printf("\r\n");
            break;
        }
        delay(100);
    }

    printf("Reading stopped\r\n");
    reader.stop();

    string_clear(type_string);
}

void lfrfid_cli_write(Cli* cli, string_t args) {
    // TODO implement rfid write
    printf("Not implemented :(\r\n");
}

void lfrfid_cli_emulate(Cli* cli, string_t args) {
    string_t data;
    string_init(data);
    RfidTimerEmulator emulator;

    static const uint8_t data_size = LFRFID_KEY_SIZE;
    uint8_t key_data[data_size] = {0};
    uint8_t key_data_size = 0;
    LfrfidKeyType type;

    if(!args_read_string_and_trim(args, data)) {
        lfrfid_cli_print_usage();
        string_clear(data);
        return;
    }

    if(!lfrfid_cli_get_key_type(data, &type)) {
        lfrfid_cli_print_usage();
        string_clear(data);
        return;
    }

    key_data_size = lfrfid_key_get_type_data_count(type);

    if(!args_read_hex_bytes(args, key_data, key_data_size)) {
        lfrfid_cli_print_usage();
        string_clear(data);
        return;
    }

    emulator.start(type, key_data, key_data_size);

    printf("Emulating RFID...\r\nPress Ctrl+C to abort\r\n");
    while(!cli_cmd_interrupt_received(cli)) {
        delay(100);
    }
    printf("Emulation stopped\r\n");
    emulator.stop();

    string_clear(data);
}

void lfrfid_cli(Cli* cli, string_t args, void* context) {
    string_t cmd;
    string_init(cmd);

    if(!args_read_string_and_trim(args, cmd)) {
        string_clear(cmd);
        lfrfid_cli_print_usage();
        return;
    }

    if(string_cmp_str(cmd, "read") == 0) {
        lfrfid_cli_read(cli, args);
    } else if(string_cmp_str(cmd, "write") == 0) {
        lfrfid_cli_write(cli, args);
    } else if(string_cmp_str(cmd, "emulate") == 0) {
        lfrfid_cli_emulate(cli, args);
    } else {
        lfrfid_cli_print_usage();
    }

    string_clear(cmd);
}