flipperzero-firmware/applications/subghz/subghz_history.c
SG 274c12fc56
[FL-2274] Inventing streams and moving FFF to them (#981)
* Streams: string stream
* String stream: updated insert/delete api
* Streams: generic stream interface and string stream implementation
* Streams: helpers for insert and delete_and_insert
* FFF: now compatible with streams
* MinUnit: introduced tests with arguments
* FFF: stream access violation
* Streams: copy data between streams
* Streams: file stream
* FFF: documentation
* FFStream: documentation
* FFF: alloc as file
* MinUnit: support for nested tests
* Streams: changed delete_and_insert, now it returns success flag. Added ability dump stream inner parameters and data to cout.
* FFF: simplified file open function
* Streams: unit tests
* FFF: tests
* Streams: declare cache_size constant as define, to allow variable modified arrays
* FFF: lib moved to a separate folder
* iButton: new FFF
* RFID: new FFF
* Animations: new FFF
* IR: new FFF
* NFC: new FFF
* Flipper file format: delete lib
* U2F: new FFF
* Subghz: new FFF and streams
* Streams: read line
* Streams: split
* FuriCore: implement memset with extra asserts
* FuriCore: implement extra heap asserts without inventing memset
* Scene manager: protected access to the scene id stack with a size check
* NFC worker: dirty fix for issue where hal_nfc was busy on app start
* Furi: update allocator to erase memory on allocation. Replace furi_alloc with malloc.
* FuriCore: cleanup memmgr code.
* Furi HAL: furi_hal_init is split into critical and non-critical parts. The critical part is currently clock and console.
* Memmgr: added ability to track allocations and deallocations through console.
* FFStream: some speedup
* Streams, FF: minor fixes
* Tests: restore
* File stream: a slightly more thread-safe version of file_stream_delete_and_insert

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2022-02-18 22:53:46 +03:00

177 lines
6.2 KiB
C

#include "subghz_history.h"
#include <lib/subghz/protocols/subghz_protocol_keeloq.h>
#include <lib/subghz/protocols/subghz_protocol_star_line.h>
#include <lib/subghz/protocols/subghz_protocol_princeton.h>
#include <lib/subghz/protocols/subghz_protocol_somfy_keytis.h>
#include <furi.h>
#include <m-string.h>
#define SUBGHZ_HISTORY_MAX 50
typedef struct SubGhzHistoryStruct SubGhzHistoryStruct;
struct SubGhzHistoryStruct {
const char* name;
const char* manufacture_name;
uint8_t type_protocol;
uint8_t code_count_bit;
uint64_t code_found;
uint32_t data1;
FuriHalSubGhzPreset preset;
uint32_t real_frequency;
};
struct SubGhzHistory {
uint32_t last_update_timestamp;
uint16_t last_index_write;
uint64_t code_last_found;
SubGhzHistoryStruct history[SUBGHZ_HISTORY_MAX];
SubGhzProtocolCommonLoad data;
};
SubGhzHistory* subghz_history_alloc(void) {
SubGhzHistory* instance = malloc(sizeof(SubGhzHistory));
return instance;
}
void subghz_history_free(SubGhzHistory* instance) {
furi_assert(instance);
free(instance);
}
void subghz_history_set_frequency_preset(
SubGhzHistory* instance,
uint16_t idx,
uint32_t frequency,
FuriHalSubGhzPreset preset) {
furi_assert(instance);
if(instance->last_index_write >= SUBGHZ_HISTORY_MAX) return;
instance->history[idx].preset = preset;
instance->history[idx].real_frequency = frequency;
}
uint32_t subghz_history_get_frequency(SubGhzHistory* instance, uint16_t idx) {
furi_assert(instance);
return instance->history[idx].real_frequency;
}
FuriHalSubGhzPreset subghz_history_get_preset(SubGhzHistory* instance, uint16_t idx) {
furi_assert(instance);
return instance->history[idx].preset;
}
void subghz_history_reset(SubGhzHistory* instance) {
furi_assert(instance);
instance->last_index_write = 0;
instance->code_last_found = 0;
}
uint16_t subghz_history_get_item(SubGhzHistory* instance) {
furi_assert(instance);
return instance->last_index_write;
}
uint8_t subghz_history_get_type_protocol(SubGhzHistory* instance, uint16_t idx) {
furi_assert(instance);
return instance->history[idx].type_protocol;
}
const char* subghz_history_get_name(SubGhzHistory* instance, uint16_t idx) {
furi_assert(instance);
return instance->history[idx].name;
}
SubGhzProtocolCommonLoad* subghz_history_get_raw_data(SubGhzHistory* instance, uint16_t idx) {
furi_assert(instance);
instance->data.code_found = instance->history[idx].code_found;
instance->data.code_count_bit = instance->history[idx].code_count_bit;
instance->data.param1 = instance->history[idx].data1;
return &instance->data;
}
bool subghz_history_get_text_space_left(SubGhzHistory* instance, string_t output) {
furi_assert(instance);
if(instance->last_index_write == SUBGHZ_HISTORY_MAX) {
if(output != NULL) string_printf(output, "Memory is FULL");
return true;
}
if(output != NULL)
string_printf(output, "%02u/%02u", instance->last_index_write, SUBGHZ_HISTORY_MAX);
return false;
}
void subghz_history_get_text_item_menu(SubGhzHistory* instance, string_t output, uint16_t idx) {
if(instance->history[idx].code_count_bit < 33) {
string_printf(
output,
"%s %lX",
instance->history[idx].name,
(uint32_t)(instance->history[idx].code_found & 0xFFFFFFFF));
} else {
string_t str_buff;
string_init(str_buff);
if(strcmp(instance->history[idx].name, "KeeLoq") == 0) {
string_set(str_buff, "KL ");
string_cat(str_buff, instance->history[idx].manufacture_name);
} else if(strcmp(instance->history[idx].name, "Star Line") == 0) {
string_set(str_buff, "SL ");
string_cat(str_buff, instance->history[idx].manufacture_name);
} else {
string_set(str_buff, instance->history[idx].name);
}
string_printf(
output,
"%s %lX%08lX",
string_get_cstr(str_buff),
(uint32_t)(instance->history[idx].code_found >> 32),
(uint32_t)(instance->history[idx].code_found & 0xFFFFFFFF));
string_clear(str_buff);
}
}
bool subghz_history_add_to_history(
SubGhzHistory* instance,
void* context,
uint32_t frequency,
FuriHalSubGhzPreset preset) {
furi_assert(instance);
furi_assert(context);
SubGhzProtocolCommon* protocol = context;
if(instance->last_index_write >= SUBGHZ_HISTORY_MAX) return false;
if((instance->code_last_found == (protocol->code_last_found & 0xFFFF0FFFFFFFFFFF)) &&
((millis() - instance->last_update_timestamp) < 500)) {
instance->last_update_timestamp = millis();
return false;
}
instance->code_last_found = protocol->code_last_found & 0xFFFF0FFFFFFFFFFF;
instance->last_update_timestamp = millis();
instance->history[instance->last_index_write].real_frequency = frequency;
instance->history[instance->last_index_write].preset = preset;
instance->history[instance->last_index_write].data1 = 0;
instance->history[instance->last_index_write].manufacture_name = NULL;
instance->history[instance->last_index_write].name = protocol->name;
instance->history[instance->last_index_write].code_count_bit = protocol->code_last_count_bit;
instance->history[instance->last_index_write].code_found = protocol->code_last_found;
if(strcmp(protocol->name, "KeeLoq") == 0) {
instance->history[instance->last_index_write].manufacture_name =
subghz_protocol_keeloq_find_and_get_manufacture_name(protocol);
} else if(strcmp(protocol->name, "Star Line") == 0) {
instance->history[instance->last_index_write].manufacture_name =
subghz_protocol_star_line_find_and_get_manufacture_name(protocol);
} else if(strcmp(protocol->name, "Princeton") == 0) {
instance->history[instance->last_index_write].data1 =
subghz_protocol_princeton_get_te(protocol);
} else if(strcmp(protocol->name, "Somfy Keytis") == 0) {
instance->history[instance->last_index_write].data1 =
subghz_protocol_somfy_keytis_get_press_duration(protocol);
}
instance->history[instance->last_index_write].type_protocol = protocol->type_protocol;
instance->last_index_write++;
return true;
}