2022-06-21 15:45:50 +03:00
|
|
|
#include "infrared_brute_force.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <m-dict.h>
|
|
|
|
#include <flipper_format/flipper_format.h>
|
|
|
|
|
|
|
|
#include "infrared_signal.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t index;
|
|
|
|
uint32_t count;
|
|
|
|
} InfraredBruteForceRecord;
|
|
|
|
|
|
|
|
DICT_DEF2(
|
|
|
|
InfraredBruteForceRecordDict,
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString*,
|
|
|
|
FURI_STRING_OPLIST,
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredBruteForceRecord,
|
|
|
|
M_POD_OPLIST);
|
|
|
|
|
|
|
|
struct InfraredBruteForce {
|
|
|
|
FlipperFormat* ff;
|
|
|
|
const char* db_filename;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* current_record_name;
|
2022-09-27 20:11:28 +03:00
|
|
|
InfraredSignal* current_signal;
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredBruteForceRecordDict_t records;
|
2022-09-27 20:11:28 +03:00
|
|
|
bool is_started;
|
2022-06-21 15:45:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
InfraredBruteForce* infrared_brute_force_alloc() {
|
|
|
|
InfraredBruteForce* brute_force = malloc(sizeof(InfraredBruteForce));
|
|
|
|
brute_force->ff = NULL;
|
|
|
|
brute_force->db_filename = NULL;
|
2022-09-27 20:11:28 +03:00
|
|
|
brute_force->current_signal = NULL;
|
|
|
|
brute_force->is_started = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
brute_force->current_record_name = furi_string_alloc();
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredBruteForceRecordDict_init(brute_force->records);
|
|
|
|
return brute_force;
|
|
|
|
}
|
|
|
|
|
2022-08-27 02:39:06 +03:00
|
|
|
void infrared_brute_force_clear_records(InfraredBruteForce* brute_force) {
|
2022-09-28 04:04:12 +03:00
|
|
|
furi_assert(!brute_force->is_started);
|
2022-08-27 02:39:06 +03:00
|
|
|
InfraredBruteForceRecordDict_reset(brute_force->records);
|
|
|
|
}
|
|
|
|
|
2022-06-21 15:45:50 +03:00
|
|
|
void infrared_brute_force_free(InfraredBruteForce* brute_force) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(!brute_force->is_started);
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredBruteForceRecordDict_clear(brute_force->records);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(brute_force->current_record_name);
|
2022-06-21 15:45:50 +03:00
|
|
|
free(brute_force);
|
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_brute_force_set_db_filename(InfraredBruteForce* brute_force, const char* db_filename) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(!brute_force->is_started);
|
2022-06-21 15:45:50 +03:00
|
|
|
brute_force->db_filename = db_filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(!brute_force->is_started);
|
2022-06-21 15:45:50 +03:00
|
|
|
furi_assert(brute_force->db_filename);
|
|
|
|
bool success = false;
|
|
|
|
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-07-22 19:00:25 +03:00
|
|
|
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
|
2022-06-21 15:45:50 +03:00
|
|
|
|
2022-07-22 19:00:25 +03:00
|
|
|
success = flipper_format_buffered_file_open_existing(ff, brute_force->db_filename);
|
2022-06-21 15:45:50 +03:00
|
|
|
if(success) {
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* signal_name;
|
|
|
|
signal_name = furi_string_alloc();
|
2022-06-21 15:45:50 +03:00
|
|
|
while(flipper_format_read_string(ff, "name", signal_name)) {
|
|
|
|
InfraredBruteForceRecord* record =
|
|
|
|
InfraredBruteForceRecordDict_get(brute_force->records, signal_name);
|
|
|
|
if(record) {
|
|
|
|
++(record->count);
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(signal_name);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
flipper_format_free(ff);
|
2022-07-26 15:21:51 +03:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-06-21 15:45:50 +03:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool infrared_brute_force_start(
|
|
|
|
InfraredBruteForce* brute_force,
|
|
|
|
uint32_t index,
|
|
|
|
uint32_t* record_count) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(!brute_force->is_started);
|
2022-06-21 15:45:50 +03:00
|
|
|
bool success = false;
|
|
|
|
*record_count = 0;
|
|
|
|
|
|
|
|
InfraredBruteForceRecordDict_it_t it;
|
|
|
|
for(InfraredBruteForceRecordDict_it(it, brute_force->records);
|
|
|
|
!InfraredBruteForceRecordDict_end_p(it);
|
|
|
|
InfraredBruteForceRecordDict_next(it)) {
|
|
|
|
const InfraredBruteForceRecordDict_itref_t* record = InfraredBruteForceRecordDict_cref(it);
|
|
|
|
if(record->value.index == index) {
|
|
|
|
*record_count = record->value.count;
|
|
|
|
if(*record_count) {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(brute_force->current_record_name, record->key);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(*record_count) {
|
2022-07-26 15:21:51 +03:00
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
2022-07-22 19:00:25 +03:00
|
|
|
brute_force->ff = flipper_format_buffered_file_alloc(storage);
|
2022-09-27 20:11:28 +03:00
|
|
|
brute_force->current_signal = infrared_signal_alloc();
|
|
|
|
brute_force->is_started = true;
|
2022-07-22 19:00:25 +03:00
|
|
|
success =
|
|
|
|
flipper_format_buffered_file_open_existing(brute_force->ff, brute_force->db_filename);
|
2022-09-27 20:11:28 +03:00
|
|
|
if(!success) infrared_brute_force_stop(brute_force);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool infrared_brute_force_is_started(InfraredBruteForce* brute_force) {
|
2022-09-27 20:11:28 +03:00
|
|
|
return brute_force->is_started;
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_brute_force_stop(InfraredBruteForce* brute_force) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(brute_force->is_started);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_reset(brute_force->current_record_name);
|
2022-09-27 20:11:28 +03:00
|
|
|
infrared_signal_free(brute_force->current_signal);
|
2022-06-21 15:45:50 +03:00
|
|
|
flipper_format_free(brute_force->ff);
|
2022-09-27 20:11:28 +03:00
|
|
|
brute_force->current_signal = NULL;
|
2022-06-21 15:45:50 +03:00
|
|
|
brute_force->ff = NULL;
|
2022-09-27 20:11:28 +03:00
|
|
|
brute_force->is_started = false;
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool infrared_brute_force_send_next(InfraredBruteForce* brute_force) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(brute_force->is_started);
|
|
|
|
const bool success = infrared_signal_search_and_read(
|
|
|
|
brute_force->current_signal, brute_force->ff, brute_force->current_record_name);
|
2022-06-21 15:45:50 +03:00
|
|
|
if(success) {
|
2022-09-27 20:11:28 +03:00
|
|
|
infrared_signal_transmit(brute_force->current_signal);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void infrared_brute_force_add_record(
|
|
|
|
InfraredBruteForce* brute_force,
|
|
|
|
uint32_t index,
|
|
|
|
const char* name) {
|
|
|
|
InfraredBruteForceRecord value = {.index = index, .count = 0};
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* key;
|
|
|
|
key = furi_string_alloc_set(name);
|
2022-06-21 15:45:50 +03:00
|
|
|
InfraredBruteForceRecordDict_set_at(brute_force->records, key, value);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(key);
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|