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
|
|
|
};
|
|
|
|
|
2024-03-19 17:43:52 +03:00
|
|
|
InfraredBruteForce* infrared_brute_force_alloc(void) {
|
2022-06-21 15:45:50 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode 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);
|
2024-09-06 12:52:00 +03:00
|
|
|
InfraredErrorCode error = InfraredErrorCodeNone;
|
2022-06-21 15:45:50 +03:00
|
|
|
|
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);
|
2024-02-12 05:30:10 +03:00
|
|
|
FuriString* signal_name = furi_string_alloc();
|
|
|
|
InfraredSignal* signal = infrared_signal_alloc();
|
|
|
|
|
|
|
|
do {
|
2024-09-06 12:52:00 +03:00
|
|
|
if(!flipper_format_buffered_file_open_existing(ff, brute_force->db_filename)) {
|
|
|
|
error = InfraredErrorCodeFileOperationFailed;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-12 05:30:10 +03:00
|
|
|
|
|
|
|
bool signals_valid = false;
|
2024-09-06 12:52:00 +03:00
|
|
|
while(infrared_signal_read_name(ff, signal_name) == InfraredErrorCodeNone) {
|
|
|
|
error = infrared_signal_read_body(signal, ff);
|
|
|
|
signals_valid = (!INFRARED_ERROR_PRESENT(error)) && infrared_signal_is_valid(signal);
|
2024-02-12 05:30:10 +03:00
|
|
|
if(!signals_valid) break;
|
2022-06-21 15:45:50 +03:00
|
|
|
|
|
|
|
InfraredBruteForceRecord* record =
|
|
|
|
InfraredBruteForceRecordDict_get(brute_force->records, signal_name);
|
2022-12-26 15:13:30 +03:00
|
|
|
if(record) { //-V547
|
2022-06-21 15:45:50 +03:00
|
|
|
++(record->count);
|
|
|
|
}
|
|
|
|
}
|
2024-02-12 05:30:10 +03:00
|
|
|
if(!signals_valid) break;
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
infrared_signal_free(signal);
|
|
|
|
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);
|
2024-09-06 12:52:00 +03:00
|
|
|
return error;
|
2022-06-21 15:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-30 19:20:35 +03:00
|
|
|
bool infrared_brute_force_is_started(const 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);
|
2024-09-06 12:52:00 +03:00
|
|
|
|
2023-11-10 10:22:34 +03:00
|
|
|
const bool success = infrared_signal_search_by_name_and_read(
|
2024-09-06 12:52:00 +03:00
|
|
|
brute_force->current_signal,
|
|
|
|
brute_force->ff,
|
|
|
|
furi_string_get_cstr(brute_force->current_record_name)) ==
|
|
|
|
InfraredErrorCodeNone;
|
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
|
|
|
}
|
2022-09-22 19:13:00 +03:00
|
|
|
|
|
|
|
void infrared_brute_force_reset(InfraredBruteForce* brute_force) {
|
2022-09-27 20:11:28 +03:00
|
|
|
furi_assert(!brute_force->is_started);
|
2022-09-22 19:13:00 +03:00
|
|
|
InfraredBruteForceRecordDict_reset(brute_force->records);
|
|
|
|
}
|