unleashed-firmware/lib/fl_subghz/protocols/subghz_protocol.c
あく e8211226f3
[FL-1496] SubGhz: Library, Cli, Application (#543)
* ApiHal: set frequency and path in one go. Drivers: proper frequency registers calculation for CC1101. Update subghz cli to match new api.
* SubGhz: preparation for parsers porting, tim2 sharing
* ApiHal: add interrupts API, move all TIM2 related things there.
* SubGhz: refactor protocol lib and add keeloq.
* SubGhz: proper init_set for keeloq manafacture key
* SubGhz: port more protocols to lib
* SubGhz: load keeloq manufacture keys from sd card (if any).
* SubGhz: format output from protocols.
* SubGhz: use default frequency in subghz_rx cli command.
* SubGhz: keeloq key types
* Fix compillation error when internal storage disabled
* SubGhz: minor cleanup
* SubGhz: properly handle timeout and reset signal in subghz_rx
* SubGhz: Worker, Capture View. Furi: emulate thread join.
* SubGhz: free strings on keeloq key load end
* SubGhz: update protocols reporting API, app refactoring and capture view, update API HAL usage.
* SubGhz: update dump formatting
* ApiHal: backport subghz preset to F5
* ApiHal: backport subghz frequency range to F5
2021-06-30 00:19:20 +03:00

131 lines
4.8 KiB
C

#include "subghz_protocol.h"
#include "subghz_protocol_came.h"
#include "subghz_protocol_cfm.h"
#include "subghz_protocol_keeloq.h"
#include "subghz_protocol_nice_flo.h"
#include "subghz_protocol_nice_flor_s.h"
#include "subghz_protocol_princeton.h"
#include <furi.h>
#include <m-string.h>
#include <filesystem-api.h>
#define FILE_BUFFER_SIZE 64
struct SubGhzProtocol {
SubGhzProtocolCame* came;
SubGhzProtocolKeeloq* keeloq;
SubGhzProtocolNiceFlo* nice_flo;
SubGhzProtocolNiceFlorS* nice_flor_s;
SubGhzProtocolPrinceton* princeton;
SubGhzProtocolTextCallback text_callback;
void* text_callback_context;
};
static void subghz_protocol_came_rx_callback(SubGhzProtocolCommon* parser, void* context) {
SubGhzProtocol* instance = context;
string_t output;
string_init(output);
subghz_protocol_common_to_str((SubGhzProtocolCommon*)parser, output);
if (instance->text_callback) {
instance->text_callback(output, instance->text_callback_context);
} else {
printf(string_get_cstr(output));
}
string_clear(output);
}
SubGhzProtocol* subghz_protocol_alloc() {
SubGhzProtocol* instance = furi_alloc(sizeof(SubGhzProtocol));
instance->came = subghz_protocol_came_alloc();
instance->keeloq = subghz_protocol_keeloq_alloc();
instance->princeton = subghz_protocol_princeton_alloc();
instance->nice_flo = subghz_protocol_nice_flo_alloc();
instance->nice_flor_s = subghz_protocol_nice_flor_s_alloc();
return instance;
}
void subghz_protocol_free(SubGhzProtocol* instance) {
furi_assert(instance);
subghz_protocol_came_free(instance->came);
subghz_protocol_keeloq_free(instance->keeloq);
subghz_protocol_princeton_free(instance->princeton);
subghz_protocol_nice_flo_free(instance->nice_flo);
subghz_protocol_nice_flor_s_free(instance->nice_flor_s);
free(instance);
}
void subghz_protocol_enable_dump(SubGhzProtocol* instance, SubGhzProtocolTextCallback callback, void* context) {
furi_assert(instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->came, subghz_protocol_came_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->keeloq, subghz_protocol_came_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->princeton, subghz_protocol_came_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flo, subghz_protocol_came_rx_callback, instance);
subghz_protocol_common_set_callback((SubGhzProtocolCommon*)instance->nice_flor_s, subghz_protocol_came_rx_callback, instance);
instance->text_callback = callback;
instance->text_callback_context = context;
}
static void subghz_protocol_load_keeloq_file_process_line(SubGhzProtocol* instance, string_t line) {
uint64_t key = 0;
uint16_t type = 0;
char skey[17] = {0};
char name[65] = {0};
int ret = sscanf(string_get_cstr(line), "%16s:%hu:%64s", skey, &type, name);
key = strtoull(skey, NULL, 16);
if (ret == 3) {
subghz_protocol_keeloq_add_manafacture_key(instance->keeloq, name, key, type);
} else {
printf("Failed to load line: %s\r\n", string_get_cstr(line));
}
}
void subghz_protocol_load_keeloq_file(SubGhzProtocol* instance, const char* file_name) {
File manufacture_keys_file;
FS_Api* fs_api = furi_record_open("sdcard");
fs_api->file.open(&manufacture_keys_file, file_name, FSAM_READ, FSOM_OPEN_EXISTING);
string_t line;
string_init(line);
if(manufacture_keys_file.error_id == FSE_OK) {
printf("Loading manufacture keys file %s\r\n", file_name);
char buffer[FILE_BUFFER_SIZE];
uint16_t ret;
do {
ret = fs_api->file.read(&manufacture_keys_file, buffer, FILE_BUFFER_SIZE);
for (uint16_t i=0; i < ret; i++) {
if (buffer[i] == '\n' && string_size(line) > 0) {
subghz_protocol_load_keeloq_file_process_line(instance, line);
string_clean(line);
} else {
string_push_back(line, buffer[i]);
}
}
} while(ret > 0);
} else {
printf("Manufacture keys file is not found: %s\r\n", file_name);
}
string_clear(line);
fs_api->file.close(&manufacture_keys_file);
furi_record_close("sdcard");
}
void subghz_protocol_reset(SubGhzProtocol* instance) {
}
void subghz_protocol_parse(SubGhzProtocol* instance, LevelPair data) {
subghz_protocol_came_parse(instance->came, data);
subghz_protocol_keeloq_parse(instance->keeloq, data);
subghz_protocol_princeton_parse(instance->princeton, data);
subghz_protocol_nice_flo_parse(instance->nice_flo, data);
subghz_protocol_nice_flor_s_parse(instance->nice_flor_s, data);
}