flipperzero-firmware/lib/subghz/protocols/subghz_protocol_common.c
Skorpionm 0a8a944e10
Skorp subghz signal archive (#667)
* SubGhz: Add millis() furi, add subghz history struct
* SubGhz: Fix subghz history
* Gubghz: Fix code repeat  history, add clean history
* SubGhz: reading and adding keys to history
* Gui: Renaming Sub 1-Ghz -> SubGhz
* Archive: Renaming Sub 1-Ghz -> SubGhz
* SubGhz:  Add menu history, modified button for sending a signal, changed output of data about accepted protocol
* Archive: Fix name subghz
* SubGhz: Menu navigation
* Assets: Add assets/SubGHz/icon.png
* Assets: add new icons for subghz
* SubGhz: Fix name Add manually scene
* SubGhz: Fix load icon Read scene. rename encoder struct,  rename protocol function load from file, add load raw data protocol, add info pleasant signals all protocol
* SubGhz: fix memory leak
* SubGhz: change of receiving frequency for read scene
* SubGhz: Add save/load frequency and preset, add automatic configuration of transmit/receive to the desired frequency and modulation, add button "save" config scene
* SubGhz: Fix frequency and preset, fix frequency add manualli scene, fix re-executing the parser
* Furi-hal-subghz: add 2-FSK config, fix ook config 650KHz BW Tx filter
* Fix formatting and release build
* SubGhz: Delete read scene
* SubGhz: Fix frequency add manualli scene,  refactoring code
* SubGhz: 2 profiles for OOK, fix broken build.
* SubGhz: Add passing static codes from read scene, add notification read scene, refactoring code
* SubGhz: fix assert on worker double stop.

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-08-28 16:51:48 +03:00

140 lines
4.4 KiB
C

#include "subghz_protocol_common.h"
#include <stdio.h>
#include <lib/toolbox/hex.h>
SubGhzProtocolCommonEncoder* subghz_protocol_encoder_common_alloc() {
SubGhzProtocolCommonEncoder* instance = furi_alloc(sizeof(SubGhzProtocolCommonEncoder));
instance->upload = furi_alloc(SUBGHZ_ENCODER_UPLOAD_MAX_SIZE * sizeof(LevelDuration));
instance->start = true;
instance->repeat = 10; //default number of repeat
return instance;
}
void subghz_protocol_encoder_common_free(SubGhzProtocolCommonEncoder* instance) {
furi_assert(instance);
free(instance->upload);
free(instance);
}
size_t subghz_encoder_common_get_repeat_left(SubGhzProtocolCommonEncoder* instance) {
furi_assert(instance);
return instance->repeat;
}
LevelDuration subghz_protocol_encoder_common_yield(void* context) {
SubGhzProtocolCommonEncoder* instance = context;
if(instance->repeat == 0){
return level_duration_reset();
}
LevelDuration ret = instance->upload[instance->front];
if(++instance->front == instance->size_upload) {
instance->repeat--;
instance->front = 0;
}
return ret;
}
void subghz_protocol_common_add_bit(SubGhzProtocolCommon *common, uint8_t bit){
common->code_found = common->code_found << 1 | bit;
common->code_count_bit++;
}
bool subghz_protocol_common_check_interval(SubGhzProtocolCommon *common, uint32_t duration, uint16_t duration_check) {
if ((duration_check >= (duration - common->te_delta))&&(duration_check <= (duration + common->te_delta))){
return true;
} else {
return false;
}
}
uint64_t subghz_protocol_common_reverse_key(uint64_t key, uint8_t count_bit){
uint64_t key_reverse=0;
for(uint8_t i=0; i<count_bit; i++) {
key_reverse=key_reverse<<1|bit_read(key,i);
}
return key_reverse;
}
void subghz_protocol_common_set_callback(SubGhzProtocolCommon* common, SubGhzProtocolCommonCallback callback, void* context) {
common->callback = callback;
common->context = context;
}
void subghz_protocol_common_to_str(SubGhzProtocolCommon* instance, string_t output) {
if (instance->to_string) {
instance->to_string(instance, output);
} else {
uint32_t code_found_hi = instance->code_found >> 32;
uint32_t code_found_lo = instance->code_found & 0x00000000ffffffff;
uint64_t code_found_reverse = subghz_protocol_common_reverse_key(instance->code_found, instance->code_count_bit);
uint32_t code_found_reverse_hi = code_found_reverse>>32;
uint32_t code_found_reverse_lo = code_found_reverse&0x00000000ffffffff;
if (code_found_hi>0) {
string_cat_printf(
output,
"Protocol %s, %d Bit\r\n"
" KEY:0x%lX%08lX\r\n"
" YEK:0x%lX%08lX\r\n"
" SN:0x%05lX BTN:%02X\r\n",
instance->name,
instance->code_count_bit,
code_found_hi,
code_found_lo,
code_found_reverse_hi,
code_found_reverse_lo,
instance->serial,
instance->btn
);
} else {
string_cat_printf(
output,
"Protocol %s, %d Bit\r\n"
" KEY:0x%lX%lX\r\n"
" YEK:0x%lX%lX\r\n"
" SN:0x%05lX BTN:%02X\r\n",
instance->name,
instance->code_count_bit,
code_found_hi,
code_found_lo,
code_found_reverse_hi,
code_found_reverse_lo,
instance->serial,
instance->btn
);
}
}
}
bool subghz_protocol_common_read_hex(string_t str, uint8_t* buff, uint16_t len) {
string_strim(str);
uint8_t nibble_high = 0;
uint8_t nibble_low = 0;
bool parsed = true;
for(uint16_t i = 0; i < len; i++) {
if(hex_char_to_hex_nibble(string_get_char(str, 0), &nibble_high) &&
hex_char_to_hex_nibble(string_get_char(str, 1), &nibble_low)) {
buff[i] = (nibble_high << 4) | nibble_low;
if(string_size(str)>2){
string_right(str, 2);
}else if(i<len-1){
parsed = false;
break;
};
} else {
parsed = false;
break;
}
}
return parsed;
}