SubGhz: Сreating and delivering Security+ 1.0 (#1268)
* SubGhz: fix frequency reset on incomplete exit from the transmission menu * SubGhz: fix incorrect shutdown of CC1101 if it was not turned on * SubGhz: consciousness and transmission support Security+ 1.0 * Unit_test: add test encoder Security+ 1/0 * SubGhz: fix start counter Security+ 1.0 Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -48,6 +48,8 @@ struct SubGhzProtocolEncoderSecPlus_v1 {
|
||||
|
||||
SubGhzProtocolBlockEncoder encoder;
|
||||
SubGhzBlockGeneric generic;
|
||||
|
||||
uint8_t data_array[44];
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
@@ -71,23 +73,258 @@ const SubGhzProtocolDecoder subghz_protocol_secplus_v1_decoder = {
|
||||
};
|
||||
|
||||
const SubGhzProtocolEncoder subghz_protocol_secplus_v1_encoder = {
|
||||
.alloc = NULL,
|
||||
.free = NULL,
|
||||
.alloc = subghz_protocol_encoder_secplus_v1_alloc,
|
||||
.free = subghz_protocol_encoder_secplus_v1_free,
|
||||
|
||||
.deserialize = NULL,
|
||||
.stop = NULL,
|
||||
.yield = NULL,
|
||||
.deserialize = subghz_protocol_encoder_secplus_v1_deserialize,
|
||||
.stop = subghz_protocol_encoder_secplus_v1_stop,
|
||||
.yield = subghz_protocol_encoder_secplus_v1_yield,
|
||||
};
|
||||
|
||||
const SubGhzProtocol subghz_protocol_secplus_v1 = {
|
||||
.name = SUBGHZ_PROTOCOL_SECPLUS_V1_NAME,
|
||||
.type = SubGhzProtocolTypeDynamic,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
|
||||
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
|
||||
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Send,
|
||||
|
||||
.decoder = &subghz_protocol_secplus_v1_decoder,
|
||||
.encoder = &subghz_protocol_secplus_v1_encoder,
|
||||
};
|
||||
|
||||
void* subghz_protocol_encoder_secplus_v1_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolEncoderSecPlus_v1* instance = malloc(sizeof(SubGhzProtocolEncoderSecPlus_v1));
|
||||
|
||||
instance->base.protocol = &subghz_protocol_secplus_v1;
|
||||
instance->generic.protocol_name = instance->base.protocol->name;
|
||||
|
||||
instance->encoder.repeat = 10;
|
||||
instance->encoder.size_upload = 128;
|
||||
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
|
||||
instance->encoder.is_runing = false;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_secplus_v1_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSecPlus_v1* instance = context;
|
||||
free(instance->encoder.upload);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generating an upload from data.
|
||||
* @param instance Pointer to a SubGhzProtocolEncoderSecPlus_v1 instance
|
||||
* @return true On success
|
||||
*/
|
||||
static bool
|
||||
subghz_protocol_encoder_secplus_v1_get_upload(SubGhzProtocolEncoderSecPlus_v1* instance) {
|
||||
furi_assert(instance);
|
||||
size_t index = 0;
|
||||
size_t size_upload = (instance->generic.data_count_bit * 2);
|
||||
if(size_upload > instance->encoder.size_upload) {
|
||||
FURI_LOG_E(TAG, "Encoder size upload exceeds allocated encoder buffer.");
|
||||
return false;
|
||||
} else {
|
||||
instance->encoder.size_upload = size_upload;
|
||||
}
|
||||
|
||||
//Send header packet 1
|
||||
instance->encoder.upload[index++] = level_duration_make(
|
||||
false, (uint32_t)subghz_protocol_secplus_v1_const.te_short * (116 + 3));
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short);
|
||||
|
||||
//Send data packet 1
|
||||
for(uint8_t i = SECPLUS_V1_PACKET_1_INDEX_BASE + 1; i < SECPLUS_V1_PACKET_1_INDEX_BASE + 21;
|
||||
i++) {
|
||||
switch(instance->data_array[i]) {
|
||||
case SECPLUS_V1_BIT_0:
|
||||
instance->encoder.upload[index++] = level_duration_make(
|
||||
false, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 3);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short);
|
||||
break;
|
||||
case SECPLUS_V1_BIT_1:
|
||||
instance->encoder.upload[index++] = level_duration_make(
|
||||
false, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 2);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 2);
|
||||
break;
|
||||
case SECPLUS_V1_BIT_2:
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_secplus_v1_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 3);
|
||||
break;
|
||||
|
||||
default:
|
||||
FURI_LOG_E(TAG, "Encoder error, wrong bit type");
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Send header packet 2
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_secplus_v1_const.te_short * (116));
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 3);
|
||||
|
||||
//Send data packet 2
|
||||
for(uint8_t i = SECPLUS_V1_PACKET_2_INDEX_BASE + 1; i < SECPLUS_V1_PACKET_2_INDEX_BASE + 21;
|
||||
i++) {
|
||||
switch(instance->data_array[i]) {
|
||||
case SECPLUS_V1_BIT_0:
|
||||
instance->encoder.upload[index++] = level_duration_make(
|
||||
false, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 3);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short);
|
||||
break;
|
||||
case SECPLUS_V1_BIT_1:
|
||||
instance->encoder.upload[index++] = level_duration_make(
|
||||
false, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 2);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 2);
|
||||
break;
|
||||
case SECPLUS_V1_BIT_2:
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(false, (uint32_t)subghz_protocol_secplus_v1_const.te_short);
|
||||
instance->encoder.upload[index++] =
|
||||
level_duration_make(true, (uint32_t)subghz_protocol_secplus_v1_const.te_short * 3);
|
||||
break;
|
||||
|
||||
default:
|
||||
FURI_LOG_E(TAG, "Encoder error, wrong bit type.");
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Security+ 1.0 message encoding
|
||||
* @param instance SubGhzProtocolEncoderSecPlus_v1*
|
||||
*/
|
||||
|
||||
static bool subghz_protocol_secplus_v1_encode(SubGhzProtocolEncoderSecPlus_v1* instance) {
|
||||
uint32_t fixed = (instance->generic.data >> 32) & 0xFFFFFFFF;
|
||||
uint32_t rolling = instance->generic.data & 0xFFFFFFFF;
|
||||
|
||||
uint8_t rolling_array[20] = {0};
|
||||
uint8_t fixed_array[20] = {0};
|
||||
uint32_t acc = 0;
|
||||
|
||||
//increment the counter
|
||||
rolling += 2;
|
||||
|
||||
//update data
|
||||
instance->generic.data &= 0xFFFFFFFF00000000;
|
||||
instance->generic.data |= rolling;
|
||||
|
||||
if(rolling > 0xFFFFFFFF) {
|
||||
rolling = 0xE6000000;
|
||||
}
|
||||
if(fixed > 0xCFD41B90) {
|
||||
FURI_LOG_E("TAG", "Encode wrong fixed data");
|
||||
return false;
|
||||
}
|
||||
|
||||
rolling = subghz_protocol_blocks_reverse_key(rolling, 32);
|
||||
|
||||
for(int i = 19; i > -1; i--) {
|
||||
rolling_array[i] = rolling % 3;
|
||||
rolling /= 3;
|
||||
fixed_array[i] = fixed % 3;
|
||||
fixed /= 3;
|
||||
}
|
||||
|
||||
instance->data_array[SECPLUS_V1_PACKET_1_INDEX_BASE] = SECPLUS_V1_PACKET_1_HEADER;
|
||||
instance->data_array[SECPLUS_V1_PACKET_2_INDEX_BASE] = SECPLUS_V1_PACKET_2_HEADER;
|
||||
|
||||
//encode packet 1
|
||||
for(uint8_t i = 1; i < 11; i++) {
|
||||
acc += rolling_array[i - 1];
|
||||
instance->data_array[i * 2 - 1] = rolling_array[i - 1];
|
||||
acc += fixed_array[i - 1];
|
||||
instance->data_array[i * 2] = acc % 3;
|
||||
}
|
||||
|
||||
acc = 0;
|
||||
//encode packet 2
|
||||
for(uint8_t i = 11; i < 21; i++) {
|
||||
acc += rolling_array[i - 1];
|
||||
instance->data_array[i * 2] = rolling_array[i - 1];
|
||||
acc += fixed_array[i - 1];
|
||||
instance->data_array[i * 2 + 1] = acc % 3;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool subghz_protocol_encoder_secplus_v1_deserialize(void* context, FlipperFormat* flipper_format) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolEncoderSecPlus_v1* instance = context;
|
||||
bool res = false;
|
||||
do {
|
||||
if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
|
||||
FURI_LOG_E(TAG, "Deserialize error");
|
||||
break;
|
||||
}
|
||||
|
||||
//optional parameter parameter
|
||||
flipper_format_read_uint32(
|
||||
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
|
||||
|
||||
if(!subghz_protocol_secplus_v1_encode(instance)) {
|
||||
break;
|
||||
}
|
||||
if(!subghz_protocol_encoder_secplus_v1_get_upload(instance)) {
|
||||
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->generic.data >> i * 8) & 0xFF;
|
||||
}
|
||||
if(!flipper_format_update_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
||||
FURI_LOG_E(TAG, "Unable to add Key");
|
||||
break;
|
||||
}
|
||||
|
||||
instance->encoder.is_runing = true;
|
||||
|
||||
res = true;
|
||||
} while(false);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void subghz_protocol_encoder_secplus_v1_stop(void* context) {
|
||||
SubGhzProtocolEncoderSecPlus_v1* instance = context;
|
||||
instance->encoder.is_runing = false;
|
||||
}
|
||||
|
||||
LevelDuration subghz_protocol_encoder_secplus_v1_yield(void* context) {
|
||||
SubGhzProtocolEncoderSecPlus_v1* instance = context;
|
||||
|
||||
if(instance->encoder.repeat == 0 || !instance->encoder.is_runing) {
|
||||
instance->encoder.is_runing = false;
|
||||
return level_duration_reset();
|
||||
}
|
||||
|
||||
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
|
||||
|
||||
if(++instance->encoder.front == instance->encoder.size_upload) {
|
||||
instance->encoder.repeat--;
|
||||
instance->encoder.front = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* subghz_protocol_decoder_secplus_v1_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
SubGhzProtocolDecoderSecPlus_v1* instance = malloc(sizeof(SubGhzProtocolDecoderSecPlus_v1));
|
||||
@@ -110,7 +347,7 @@ void subghz_protocol_decoder_secplus_v1_reset(void* context) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Security+ 1.0 half-message decoding
|
||||
* Security+ 1.0 message decoding
|
||||
* @param instance SubGhzProtocolDecoderSecPlus_v1*
|
||||
*/
|
||||
|
||||
@@ -291,6 +528,18 @@ bool subghz_protocol_decoder_secplus_v1_deserialize(void* context, FlipperFormat
|
||||
return subghz_block_generic_deserialize(&instance->generic, flipper_format);
|
||||
}
|
||||
|
||||
bool subghz_protocol_secplus_v1_check_fixed(uint32_t fixed) {
|
||||
//uint8_t id0 = (fixed / 3) % 3;
|
||||
uint8_t id1 = (fixed / 9) % 3;
|
||||
uint8_t btn = fixed % 3;
|
||||
|
||||
do {
|
||||
if(id1 == 0) return false;
|
||||
if(!(btn == 0 || btn == 1 || btn == 2)) return false;
|
||||
} while(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_secplus_v1_get_string(void* context, string_t output) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderSecPlus_v1* instance = context;
|
||||
|
Reference in New Issue
Block a user