[FL-2230] SubGhz: protocol API refactoring (#969)
* SubGhz: protocols library refactoring * SubGhz: new architecture and refactoring * SubGhz: simplify protocol structure, remove unused types * SubGhz: rename Subghz to SubGhz * SubGhz: add environment concept Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com> Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
1
lib/subghz/blocks/const.c
Normal file
1
lib/subghz/blocks/const.c
Normal file
@@ -0,0 +1 @@
|
||||
#include "const.h"
|
12
lib/subghz/blocks/const.h
Normal file
12
lib/subghz/blocks/const.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
const uint16_t te_long;
|
||||
const uint16_t te_short;
|
||||
const uint16_t te_delta;
|
||||
const uint8_t min_count_bit_for_found;
|
||||
} SubGhzBlockConst;
|
17
lib/subghz/blocks/decoder.c
Normal file
17
lib/subghz/blocks/decoder.c
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "decoder.h"
|
||||
|
||||
#define TAG "SubGhzBlockDecoder"
|
||||
|
||||
void subghz_protocol_blocks_add_bit(SubGhzBlockDecoder* decoder, uint8_t bit) {
|
||||
decoder->decode_data = decoder->decode_data << 1 | bit;
|
||||
decoder->decode_count_bit++;
|
||||
}
|
||||
|
||||
uint8_t subghz_protocol_blocks_get_hash_data(SubGhzBlockDecoder* decoder, size_t len) {
|
||||
uint8_t hash = 0;
|
||||
uint8_t* p = (uint8_t*)&decoder->decode_data;
|
||||
for(size_t i = 0; i < len; i++) {
|
||||
hash ^= p[i];
|
||||
}
|
||||
return hash;
|
||||
}
|
17
lib/subghz/blocks/decoder.h
Normal file
17
lib/subghz/blocks/decoder.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct SubGhzBlockDecoder SubGhzBlockDecoder;
|
||||
|
||||
struct SubGhzBlockDecoder {
|
||||
uint32_t parser_step;
|
||||
uint32_t te_last;
|
||||
uint64_t decode_data;
|
||||
uint8_t decode_count_bit;
|
||||
};
|
||||
|
||||
void subghz_protocol_blocks_add_bit(SubGhzBlockDecoder* decoder, uint8_t bit);
|
||||
uint8_t subghz_protocol_blocks_get_hash_data(SubGhzBlockDecoder* decoder, size_t len);
|
3
lib/subghz/blocks/encoder.c
Normal file
3
lib/subghz/blocks/encoder.c
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "encoder.h"
|
||||
|
||||
#define TAG "SubGhzBlockEncoder"
|
16
lib/subghz/blocks/encoder.h
Normal file
16
lib/subghz/blocks/encoder.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <lib/toolbox/level_duration.h>
|
||||
|
||||
typedef struct {
|
||||
bool is_runing;
|
||||
size_t repeat;
|
||||
size_t front;
|
||||
size_t size_upload;
|
||||
LevelDuration* upload;
|
||||
|
||||
} SubGhzProtocolBlockEncoder;
|
118
lib/subghz/blocks/generic.c
Normal file
118
lib/subghz/blocks/generic.c
Normal file
@@ -0,0 +1,118 @@
|
||||
#include "generic.h"
|
||||
#include "../types.h"
|
||||
#include <lib/toolbox/stream/stream.h>
|
||||
#include <lib/flipper_format/flipper_format_i.h>
|
||||
|
||||
#define TAG "SubGhzBlockGeneric"
|
||||
|
||||
bool subghz_block_generic_get_preset_name(FuriHalSubGhzPreset preset, string_t preset_str) {
|
||||
const char* preset_name;
|
||||
switch(preset) {
|
||||
case FuriHalSubGhzPresetOok270Async:
|
||||
preset_name = "FuriHalSubGhzPresetOok270Async";
|
||||
break;
|
||||
case FuriHalSubGhzPresetOok650Async:
|
||||
preset_name = "FuriHalSubGhzPresetOok650Async";
|
||||
break;
|
||||
case FuriHalSubGhzPreset2FSKDev238Async:
|
||||
preset_name = "FuriHalSubGhzPreset2FSKDev238Async";
|
||||
break;
|
||||
case FuriHalSubGhzPreset2FSKDev476Async:
|
||||
preset_name = "FuriHalSubGhzPreset2FSKDev476Async";
|
||||
break;
|
||||
default:
|
||||
FURI_LOG_E(TAG, "Unknown preset");
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
string_set(preset_str, preset_name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool subghz_block_generic_serialize(
|
||||
SubGhzBlockGeneric* instance,
|
||||
FlipperFormat* flipper_format,
|
||||
uint32_t frequency,
|
||||
FuriHalSubGhzPreset preset) {
|
||||
furi_assert(instance);
|
||||
bool res = false;
|
||||
string_t temp_str;
|
||||
string_init(temp_str);
|
||||
do {
|
||||
stream_clean(flipper_format_get_raw_stream(flipper_format));
|
||||
if(!flipper_format_write_header_cstr(
|
||||
flipper_format, SUBGHZ_KEY_FILE_TYPE, SUBGHZ_KEY_FILE_VERSION)) {
|
||||
FURI_LOG_E(TAG, "Unable to add header");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_format_write_uint32(flipper_format, "Frequency", &frequency, 1)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Frequency");
|
||||
break;
|
||||
}
|
||||
if(!subghz_block_generic_get_preset_name(preset, temp_str)) {
|
||||
break;
|
||||
}
|
||||
if(!flipper_format_write_string_cstr(flipper_format, "Preset", string_get_cstr(temp_str))) {
|
||||
FURI_LOG_E(TAG, "Unable to add Preset");
|
||||
break;
|
||||
}
|
||||
if(!flipper_format_write_string_cstr(flipper_format, "Protocol", instance->protocol_name)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Protocol");
|
||||
break;
|
||||
}
|
||||
uint32_t temp = instance->data_count_bit;
|
||||
if(!flipper_format_write_uint32(flipper_format, "Bit", &temp, 1)) {
|
||||
FURI_LOG_E(TAG, "Unable to add Bit");
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
for(size_t i = 0; i < sizeof(uint64_t); i++) {
|
||||
key_data[sizeof(uint64_t) - i - 1] = (instance->data >> i * 8) & 0xFF;
|
||||
}
|
||||
|
||||
if(!flipper_format_write_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(TAG, "Unable to add Key");
|
||||
break;
|
||||
}
|
||||
res = true;
|
||||
} while(false);
|
||||
string_clear(temp_str);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool subghz_block_generic_deserialize(SubGhzBlockGeneric* instance, FlipperFormat* flipper_format) {
|
||||
furi_assert(instance);
|
||||
bool res = false;
|
||||
string_t temp_str;
|
||||
string_init(temp_str);
|
||||
uint32_t temp_data = 0;
|
||||
|
||||
do {
|
||||
if(!flipper_format_rewind(flipper_format)) {
|
||||
FURI_LOG_E(TAG, "Rewind error");
|
||||
break;
|
||||
}
|
||||
if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
|
||||
FURI_LOG_E(TAG, "Missing Bit");
|
||||
break;
|
||||
}
|
||||
instance->data_count_bit = (uint8_t)temp_data;
|
||||
|
||||
uint8_t key_data[sizeof(uint64_t)] = {0};
|
||||
if(!flipper_format_read_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(TAG, "Missing Key");
|
||||
break;
|
||||
}
|
||||
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
|
||||
instance->data = instance->data << 8 | key_data[i];
|
||||
}
|
||||
|
||||
res = true;
|
||||
} while(0);
|
||||
|
||||
string_clear(temp_str);
|
||||
|
||||
return res;
|
||||
}
|
30
lib/subghz/blocks/generic.h
Normal file
30
lib/subghz/blocks/generic.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <lib/flipper_format/flipper_format.h>
|
||||
#include "furi.h"
|
||||
#include "furi_hal.h"
|
||||
|
||||
typedef struct SubGhzBlockGeneric SubGhzBlockGeneric;
|
||||
|
||||
struct SubGhzBlockGeneric {
|
||||
const char* protocol_name;
|
||||
uint64_t data;
|
||||
uint32_t serial;
|
||||
uint8_t data_count_bit;
|
||||
uint8_t btn;
|
||||
uint16_t cnt;
|
||||
};
|
||||
|
||||
bool subghz_block_generic_get_preset_name(FuriHalSubGhzPreset preset, string_t preset_str);
|
||||
|
||||
bool subghz_block_generic_serialize(
|
||||
SubGhzBlockGeneric* instance,
|
||||
FlipperFormat* flipper_format,
|
||||
uint32_t frequency,
|
||||
FuriHalSubGhzPreset preset);
|
||||
|
||||
bool subghz_block_generic_deserialize(SubGhzBlockGeneric* instance, FlipperFormat* flipper_format);
|
9
lib/subghz/blocks/math.c
Normal file
9
lib/subghz/blocks/math.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "math.h"
|
||||
|
||||
uint64_t subghz_protocol_blocks_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;
|
||||
}
|
13
lib/subghz/blocks/math.h
Normal file
13
lib/subghz/blocks/math.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define bit_read(value, bit) (((value) >> (bit)) & 0x01)
|
||||
#define bit_set(value, bit) ((value) |= (1UL << (bit)))
|
||||
#define bit_clear(value, bit) ((value) &= ~(1UL << (bit)))
|
||||
#define bit_write(value, bit, bitvalue) (bitvalue ? bit_set(value, bit) : bit_clear(value, bit))
|
||||
#define DURATION_DIFF(x, y) ((x < y) ? (y - x) : (x - y))
|
||||
|
||||
uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t count_bit);
|
Reference in New Issue
Block a user