id
int64 4
16.3M
| file_name
stringlengths 3
68
| file_path
stringlengths 14
181
| content
stringlengths 39
9.06M
| size
int64 39
9.06M
| language
stringclasses 1
value | extension
stringclasses 2
values | total_lines
int64 1
711k
| avg_line_length
float64 3.18
138
| max_line_length
int64 10
140
| alphanum_fraction
float64 0.02
0.93
| repo_name
stringlengths 7
69
| repo_stars
int64 2
61.6k
| repo_forks
int64 12
7.81k
| repo_open_issues
int64 0
1.13k
| repo_license
stringclasses 10
values | repo_extraction_date
stringclasses 657
values | exact_duplicates_stackv2
bool 1
class | exact_duplicates_stackv1
bool 1
class | exact_duplicates_redpajama
bool 1
class | exact_duplicates_githubcode
bool 2
classes | near_duplicates_stackv2
bool 1
class | near_duplicates_stackv1
bool 1
class | near_duplicates_redpajama
bool 1
class | near_duplicates_githubcode
bool 2
classes |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
6,035
|
binary_to_decimal.c
|
TheAlgorithms_C/conversions/binary_to_decimal.c
|
/**
* @brief Converts a number from [Binary to Decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal).
* @details
*
* Binary to decimal conversion is a process to convert a number
* having a binary representation to its equivalent decimal representation.
*
* The base of both number systems is different.
* Binary number system is base 2 number system while decimal number system is base 10 number system.
* The numbers used in binary number system are 0 and 1 while decimal number system has numbers from 0 to 9.
* The conversion of binary number to decimal number is done by multiplying
* each digit of the binary number, starting from the rightmost digit, with the power of 2 and adding the result.
*
* @author [Anup Kumar Pawar](https://github.com/AnupKumarPanwar)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <stdio.h> /// for IO operations
#include <assert.h> /// for assert
#include <math.h> /// for pow
#include <inttypes.h> /// for uint64_t
/**
* @brief Converts the given binary number
* to its equivalent decimal number/value.
* @param number The binary number to be converted
* @returns The decimal equivalent of the binary number
*/
int convert_to_decimal(uint64_t number) {
int decimal_number = 0, i = 0;
while (number > 0) {
decimal_number += (number % 10) * pow(2, i);
number = number / 10;
i++;
}
return decimal_number;
}
/**
* @brief Self-test implementations
* @returns void
*/
static void tests() {
assert(convert_to_decimal(111) == 7);
assert(convert_to_decimal(101) == 5);
assert(convert_to_decimal(1010) == 10);
assert(convert_to_decimal(1101) == 13);
assert(convert_to_decimal(100001) == 33);
assert(convert_to_decimal(10101001) == 169);
assert(convert_to_decimal(111010) == 58);
assert(convert_to_decimal(100000000) == 256);
assert(convert_to_decimal(10000000000) == 1024);
assert(convert_to_decimal(101110111) == 375);
printf("All tests have successfully passed!\n");
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main()
{
tests(); // run self-test implementations
return 0;
}
| 2,181
|
C
|
.c
| 61
| 32.770492
| 113
| 0.700899
|
TheAlgorithms/C
| 18,806
| 4,291
| 23
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,167
|
cc1101.c
|
DarkFlippers_unleashed-firmware/lib/drivers/cc1101.c
|
#include "cc1101.h"
#include <assert.h>
#include <string.h>
#include <furi_hal_cortex.h>
static bool cc1101_spi_trx(FuriHalSpiBusHandle* handle, uint8_t* tx, uint8_t* rx, uint8_t size) {
FuriHalCortexTimer timer = furi_hal_cortex_timer_get(CC1101_TIMEOUT * 1000);
while(furi_hal_gpio_read(handle->miso)) {
if(furi_hal_cortex_timer_is_expired(timer)) {
//timeout
return false;
}
}
if(!furi_hal_spi_bus_trx(handle, tx, rx, size, CC1101_TIMEOUT)) return false;
return true;
}
CC1101Status cc1101_strobe(FuriHalSpiBusHandle* handle, uint8_t strobe) {
uint8_t tx[1] = {strobe};
CC1101Status rx[1] = {0};
rx[0].CHIP_RDYn = 1;
cc1101_spi_trx(handle, tx, (uint8_t*)rx, 1);
assert(rx[0].CHIP_RDYn == 0);
return rx[0];
}
CC1101Status cc1101_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t data) {
uint8_t tx[2] = {reg, data};
CC1101Status rx[2] = {0};
rx[0].CHIP_RDYn = 1;
rx[1].CHIP_RDYn = 1;
cc1101_spi_trx(handle, tx, (uint8_t*)rx, 2);
assert((rx[0].CHIP_RDYn | rx[1].CHIP_RDYn) == 0);
return rx[1];
}
CC1101Status cc1101_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* data) {
assert(sizeof(CC1101Status) == 1);
uint8_t tx[2] = {reg | CC1101_READ, 0};
CC1101Status rx[2] = {0};
rx[0].CHIP_RDYn = 1;
cc1101_spi_trx(handle, tx, (uint8_t*)rx, 2);
assert((rx[0].CHIP_RDYn) == 0);
*data = *(uint8_t*)&rx[1];
return rx[0];
}
uint8_t cc1101_get_partnumber(FuriHalSpiBusHandle* handle) {
uint8_t partnumber = 0;
cc1101_read_reg(handle, CC1101_STATUS_PARTNUM | CC1101_BURST, &partnumber);
return partnumber;
}
uint8_t cc1101_get_version(FuriHalSpiBusHandle* handle) {
uint8_t version = 0;
cc1101_read_reg(handle, CC1101_STATUS_VERSION | CC1101_BURST, &version);
return version;
}
uint8_t cc1101_get_rssi(FuriHalSpiBusHandle* handle) {
uint8_t rssi = 0;
cc1101_read_reg(handle, CC1101_STATUS_RSSI | CC1101_BURST, &rssi);
return rssi;
}
CC1101Status cc1101_reset(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SRES);
}
CC1101Status cc1101_get_status(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SNOP);
}
bool cc1101_wait_status_state(FuriHalSpiBusHandle* handle, CC1101State state, uint32_t timeout_us) {
bool result = false;
CC1101Status status = {0};
FuriHalCortexTimer timer = furi_hal_cortex_timer_get(timeout_us);
while(!furi_hal_cortex_timer_is_expired(timer)) {
status = cc1101_strobe(handle, CC1101_STROBE_SNOP);
if(status.STATE == state) {
result = true;
break;
}
}
return result;
}
CC1101Status cc1101_shutdown(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SPWD);
}
CC1101Status cc1101_calibrate(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SCAL);
}
CC1101Status cc1101_switch_to_idle(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SIDLE);
}
CC1101Status cc1101_switch_to_rx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SRX);
}
CC1101Status cc1101_switch_to_tx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_STX);
}
CC1101Status cc1101_flush_rx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SFRX);
}
CC1101Status cc1101_flush_tx(FuriHalSpiBusHandle* handle) {
return cc1101_strobe(handle, CC1101_STROBE_SFTX);
}
uint32_t cc1101_set_frequency(FuriHalSpiBusHandle* handle, uint32_t value) {
uint64_t real_value = (uint64_t)value * CC1101_FDIV / CC1101_QUARTZ;
// Sanity check
assert((real_value & CC1101_FMASK) == real_value);
cc1101_write_reg(handle, CC1101_FREQ2, (real_value >> 16) & 0xFF);
cc1101_write_reg(handle, CC1101_FREQ1, (real_value >> 8) & 0xFF);
cc1101_write_reg(handle, CC1101_FREQ0, (real_value >> 0) & 0xFF);
uint64_t real_frequency = real_value * CC1101_QUARTZ / CC1101_FDIV;
return (uint32_t)real_frequency;
}
uint32_t cc1101_set_intermediate_frequency(FuriHalSpiBusHandle* handle, uint32_t value) {
uint64_t real_value = value * CC1101_IFDIV / CC1101_QUARTZ;
assert((real_value & 0xFF) == real_value);
cc1101_write_reg(handle, CC1101_FSCTRL0, (real_value >> 0) & 0xFF);
uint64_t real_frequency = real_value * CC1101_QUARTZ / CC1101_IFDIV;
return (uint32_t)real_frequency;
}
void cc1101_set_pa_table(FuriHalSpiBusHandle* handle, const uint8_t value[8]) {
uint8_t tx[9] = {CC1101_PATABLE | CC1101_BURST}; //-V1009
CC1101Status rx[9] = {0};
rx[0].CHIP_RDYn = 1;
rx[8].CHIP_RDYn = 1;
memcpy(&tx[1], &value[0], 8);
cc1101_spi_trx(handle, tx, (uint8_t*)rx, sizeof(rx));
assert((rx[0].CHIP_RDYn | rx[8].CHIP_RDYn) == 0);
}
uint8_t cc1101_write_fifo(FuriHalSpiBusHandle* handle, const uint8_t* data, uint8_t size) {
uint8_t buff_tx[64];
uint8_t buff_rx[64];
buff_tx[0] = CC1101_FIFO | CC1101_BURST;
memcpy(&buff_tx[1], data, size);
cc1101_spi_trx(handle, buff_tx, (uint8_t*)buff_rx, size + 1);
return size;
}
uint8_t cc1101_read_fifo(FuriHalSpiBusHandle* handle, uint8_t* data, uint8_t* size) {
uint8_t buff_trx[2];
buff_trx[0] = CC1101_FIFO | CC1101_READ | CC1101_BURST;
cc1101_spi_trx(handle, buff_trx, buff_trx, 2);
// Check that the packet is placed in the receive buffer
if(buff_trx[1] > 64) {
*size = 64;
} else {
*size = buff_trx[1];
}
furi_hal_spi_bus_trx(handle, NULL, data, *size, CC1101_TIMEOUT);
return *size;
}
| 5,663
|
C
|
.c
| 144
| 34.9375
| 100
| 0.686941
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,169
|
bq25896.c
|
DarkFlippers_unleashed-firmware/lib/drivers/bq25896.c
|
#include "bq25896.h"
#include <stddef.h>
uint8_t bit_reverse(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
typedef struct {
REG00 r00;
REG01 r01;
REG02 r02;
REG03 r03;
REG04 r04;
REG05 r05;
REG06 r06;
REG07 r07;
REG08 r08;
REG09 r09;
REG0A r0A;
REG0B r0B;
REG0C r0C;
REG0D r0D;
REG0E r0E;
REG0F r0F;
REG10 r10;
REG11 r11;
REG12 r12;
REG13 r13;
REG14 r14;
} bq25896_regs_t;
static bq25896_regs_t bq25896_regs;
bool bq25896_init(FuriHalI2cBusHandle* handle) {
bool result = true;
bq25896_regs.r14.REG_RST = 1;
result &= furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x14, *(uint8_t*)&bq25896_regs.r14, BQ25896_I2C_TIMEOUT);
// Readout all registers
result &= furi_hal_i2c_read_mem(
handle,
BQ25896_ADDRESS,
0x00,
(uint8_t*)&bq25896_regs,
sizeof(bq25896_regs),
BQ25896_I2C_TIMEOUT);
// Poll ADC forever
bq25896_regs.r02.CONV_START = 1;
bq25896_regs.r02.CONV_RATE = 1;
result &= furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x02, *(uint8_t*)&bq25896_regs.r02, BQ25896_I2C_TIMEOUT);
bq25896_regs.r07.WATCHDOG = WatchdogDisable;
result &= furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x07, *(uint8_t*)&bq25896_regs.r07, BQ25896_I2C_TIMEOUT);
// OTG power configuration
bq25896_regs.r0A.BOOSTV = 0x8; // BOOST Voltage: 5.062V
bq25896_regs.r0A.BOOST_LIM = BoostLim_1400; // BOOST Current limit: 1.4A
result &= furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x0A, *(uint8_t*)&bq25896_regs.r0A, BQ25896_I2C_TIMEOUT);
result &= furi_hal_i2c_read_mem(
handle,
BQ25896_ADDRESS,
0x00,
(uint8_t*)&bq25896_regs,
sizeof(bq25896_regs),
BQ25896_I2C_TIMEOUT);
return result;
}
void bq25896_set_boost_lim(FuriHalI2cBusHandle* handle, BoostLim boost_lim) {
bq25896_regs.r0A.BOOST_LIM = boost_lim;
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x0A, *(uint8_t*)&bq25896_regs.r0A, BQ25896_I2C_TIMEOUT);
}
void bq25896_poweroff(FuriHalI2cBusHandle* handle) {
bq25896_regs.r09.BATFET_DIS = 1;
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x09, *(uint8_t*)&bq25896_regs.r09, BQ25896_I2C_TIMEOUT);
}
ChrgStat bq25896_get_charge_status(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_mem(
handle,
BQ25896_ADDRESS,
0x00,
(uint8_t*)&bq25896_regs,
sizeof(bq25896_regs),
BQ25896_I2C_TIMEOUT);
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x0B, (uint8_t*)&bq25896_regs.r0B, BQ25896_I2C_TIMEOUT);
return bq25896_regs.r0B.CHRG_STAT;
}
bool bq25896_is_charging(FuriHalI2cBusHandle* handle) {
// Include precharge, fast charging, and charging termination done as "charging"
return bq25896_get_charge_status(handle) != ChrgStatNo;
}
bool bq25896_is_charging_done(FuriHalI2cBusHandle* handle) {
return bq25896_get_charge_status(handle) == ChrgStatDone;
}
void bq25896_enable_charging(FuriHalI2cBusHandle* handle) {
bq25896_regs.r03.CHG_CONFIG = 1;
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x03, *(uint8_t*)&bq25896_regs.r03, BQ25896_I2C_TIMEOUT);
}
void bq25896_disable_charging(FuriHalI2cBusHandle* handle) {
bq25896_regs.r03.CHG_CONFIG = 0;
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x03, *(uint8_t*)&bq25896_regs.r03, BQ25896_I2C_TIMEOUT);
}
void bq25896_enable_otg(FuriHalI2cBusHandle* handle) {
bq25896_regs.r03.OTG_CONFIG = 1;
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x03, *(uint8_t*)&bq25896_regs.r03, BQ25896_I2C_TIMEOUT);
}
void bq25896_disable_otg(FuriHalI2cBusHandle* handle) {
bq25896_regs.r03.OTG_CONFIG = 0;
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x03, *(uint8_t*)&bq25896_regs.r03, BQ25896_I2C_TIMEOUT);
}
bool bq25896_is_otg_enabled(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x03, (uint8_t*)&bq25896_regs.r03, BQ25896_I2C_TIMEOUT);
return bq25896_regs.r03.OTG_CONFIG;
}
uint16_t bq25896_get_vreg_voltage(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x06, (uint8_t*)&bq25896_regs.r06, BQ25896_I2C_TIMEOUT);
return (uint16_t)bq25896_regs.r06.VREG * 16 + 3840;
}
void bq25896_set_vreg_voltage(FuriHalI2cBusHandle* handle, uint16_t vreg_voltage) {
if(vreg_voltage < 3840) {
// Minimum valid value is 3840 mV
vreg_voltage = 3840;
} else if(vreg_voltage > 4208) {
// Maximum safe value is 4208 mV
vreg_voltage = 4208;
}
// Find the nearest voltage value (subtract offset, divide into sections)
// Values are truncated downward as needed (e.g. 4200mV -> 4192 mV)
bq25896_regs.r06.VREG = (uint8_t)((vreg_voltage - 3840) / 16);
// Apply changes
furi_hal_i2c_write_reg_8(
handle, BQ25896_ADDRESS, 0x06, *(uint8_t*)&bq25896_regs.r06, BQ25896_I2C_TIMEOUT);
}
bool bq25896_check_otg_fault(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x0C, (uint8_t*)&bq25896_regs.r0C, BQ25896_I2C_TIMEOUT);
return bq25896_regs.r0C.BOOST_FAULT;
}
uint16_t bq25896_get_vbus_voltage(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x11, (uint8_t*)&bq25896_regs.r11, BQ25896_I2C_TIMEOUT);
if(bq25896_regs.r11.VBUS_GD) {
return (uint16_t)bq25896_regs.r11.VBUSV * 100 + 2600;
} else {
return 0;
}
}
uint16_t bq25896_get_vsys_voltage(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x0F, (uint8_t*)&bq25896_regs.r0F, BQ25896_I2C_TIMEOUT);
return (uint16_t)bq25896_regs.r0F.SYSV * 20 + 2304;
}
uint16_t bq25896_get_vbat_voltage(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x0E, (uint8_t*)&bq25896_regs.r0E, BQ25896_I2C_TIMEOUT);
return (uint16_t)bq25896_regs.r0E.BATV * 20 + 2304;
}
uint16_t bq25896_get_vbat_current(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x12, (uint8_t*)&bq25896_regs.r12, BQ25896_I2C_TIMEOUT);
return (uint16_t)bq25896_regs.r12.ICHGR * 50;
}
uint32_t bq25896_get_ntc_mpct(FuriHalI2cBusHandle* handle) {
furi_hal_i2c_read_reg_8(
handle, BQ25896_ADDRESS, 0x10, (uint8_t*)&bq25896_regs.r10, BQ25896_I2C_TIMEOUT);
return (uint32_t)bq25896_regs.r10.TSPCT * 465 + 21000;
}
| 6,685
|
C
|
.c
| 175
| 33.04
| 90
| 0.67845
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,170
|
st25r3916_reg.c
|
DarkFlippers_unleashed-firmware/lib/drivers/st25r3916_reg.c
|
#include "st25r3916_reg.h"
#include <furi.h>
#define ST25R3916_WRITE_MODE \
(0U << 6) /*!< ST25R3916 Operation Mode: Write */
#define ST25R3916_READ_MODE \
(1U << 6) /*!< ST25R3916 Operation Mode: Read */
#define ST25R3916_CMD_MODE \
(3U << 6) /*!< ST25R3916 Operation Mode: Direct Command */
#define ST25R3916_FIFO_LOAD \
(0x80U) /*!< ST25R3916 Operation Mode: FIFO Load */
#define ST25R3916_FIFO_READ \
(0x9FU) /*!< ST25R3916 Operation Mode: FIFO Read */
#define ST25R3916_PT_A_CONFIG_LOAD \
(0xA0U) /*!< ST25R3916 Operation Mode: Passive Target Memory A-Config Load */
#define ST25R3916_PT_F_CONFIG_LOAD \
(0xA8U) /*!< ST25R3916 Operation Mode: Passive Target Memory F-Config Load */
#define ST25R3916_PT_TSN_DATA_LOAD \
(0xACU) /*!< ST25R3916 Operation Mode: Passive Target Memory TSN Load */
#define ST25R3916_PT_MEM_READ \
(0xBFU) /*!< ST25R3916 Operation Mode: Passive Target Memory Read */
#define ST25R3916_CMD_LEN \
(1U) /*!< ST25R3916 CMD length */
#define ST25R3916_FIFO_DEPTH (512U)
#define ST25R3916_BUF_LEN \
(ST25R3916_CMD_LEN + \
ST25R3916_FIFO_DEPTH) /*!< ST25R3916 communication buffer: CMD + FIFO length */
static void st25r3916_reg_tx_byte(FuriHalSpiBusHandle* handle, uint8_t byte) {
uint8_t val = byte;
furi_hal_spi_bus_tx(handle, &val, 1, 5);
}
void st25r3916_read_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* val) {
furi_check(handle);
st25r3916_read_burst_regs(handle, reg, val, 1);
}
void st25r3916_read_burst_regs(
FuriHalSpiBusHandle* handle,
uint8_t reg_start,
uint8_t* values,
uint8_t length) {
furi_check(handle);
furi_check(values);
furi_check(length);
furi_hal_gpio_write(handle->cs, false);
if(reg_start & ST25R3916_SPACE_B) {
// Send direct command first
st25r3916_reg_tx_byte(handle, ST25R3916_CMD_SPACE_B_ACCESS);
}
st25r3916_reg_tx_byte(handle, (reg_start & ~ST25R3916_SPACE_B) | ST25R3916_READ_MODE);
furi_hal_spi_bus_rx(handle, values, length, 5);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_write_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t val) {
furi_check(handle);
uint8_t reg_val = val;
st25r3916_write_burst_regs(handle, reg, ®_val, 1);
}
void st25r3916_write_burst_regs(
FuriHalSpiBusHandle* handle,
uint8_t reg_start,
const uint8_t* values,
uint8_t length) {
furi_check(handle);
furi_check(values);
furi_check(length);
furi_hal_gpio_write(handle->cs, false);
if(reg_start & ST25R3916_SPACE_B) {
// Send direct command first
st25r3916_reg_tx_byte(handle, ST25R3916_CMD_SPACE_B_ACCESS);
}
st25r3916_reg_tx_byte(handle, (reg_start & ~ST25R3916_SPACE_B) | ST25R3916_WRITE_MODE);
furi_hal_spi_bus_tx(handle, values, length, 5);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_reg_write_fifo(FuriHalSpiBusHandle* handle, const uint8_t* buff, size_t length) {
furi_check(handle);
furi_check(buff);
furi_check(length);
furi_check(length <= ST25R3916_FIFO_DEPTH);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_FIFO_LOAD);
furi_hal_spi_bus_tx(handle, buff, length, 200);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_reg_read_fifo(FuriHalSpiBusHandle* handle, uint8_t* buff, size_t length) {
furi_check(handle);
furi_check(buff);
furi_check(length);
furi_check(length <= ST25R3916_FIFO_DEPTH);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_FIFO_READ);
furi_hal_spi_bus_rx(handle, buff, length, 200);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_write_pta_mem(FuriHalSpiBusHandle* handle, const uint8_t* values, size_t length) {
furi_check(handle);
furi_check(values);
furi_check(length);
furi_check(length <= ST25R3916_PTM_LEN);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_PT_A_CONFIG_LOAD);
furi_hal_spi_bus_tx(handle, values, length, 200);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_read_pta_mem(FuriHalSpiBusHandle* handle, uint8_t* buff, size_t length) {
furi_check(handle);
furi_check(buff);
furi_check(length);
furi_check(length <= ST25R3916_PTM_LEN);
uint8_t tmp_buff[ST25R3916_PTM_LEN + 1];
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_PT_MEM_READ);
furi_hal_spi_bus_rx(handle, tmp_buff, length + 1, 200);
furi_hal_gpio_write(handle->cs, true);
memcpy(buff, tmp_buff + 1, length);
}
void st25r3916_write_ptf_mem(FuriHalSpiBusHandle* handle, const uint8_t* values, size_t length) {
furi_check(handle);
furi_check(values);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_PT_F_CONFIG_LOAD);
furi_hal_spi_bus_tx(handle, values, length, 200);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_write_pttsn_mem(FuriHalSpiBusHandle* handle, uint8_t* buff, size_t length) {
furi_check(handle);
furi_check(buff);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_PT_TSN_DATA_LOAD);
furi_hal_spi_bus_tx(handle, buff, length, 200);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_direct_cmd(FuriHalSpiBusHandle* handle, uint8_t cmd) {
furi_check(handle);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, cmd | ST25R3916_CMD_MODE);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_read_test_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t* val) {
furi_check(handle);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_CMD_TEST_ACCESS);
st25r3916_reg_tx_byte(handle, reg | ST25R3916_READ_MODE);
furi_hal_spi_bus_rx(handle, val, 1, 5);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_write_test_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t val) {
furi_check(handle);
furi_hal_gpio_write(handle->cs, false);
st25r3916_reg_tx_byte(handle, ST25R3916_CMD_TEST_ACCESS);
st25r3916_reg_tx_byte(handle, reg | ST25R3916_WRITE_MODE);
furi_hal_spi_bus_tx(handle, &val, 1, 5);
furi_hal_gpio_write(handle->cs, true);
}
void st25r3916_clear_reg_bits(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t clr_mask) {
furi_check(handle);
uint8_t reg_val = 0;
st25r3916_read_reg(handle, reg, ®_val);
if((reg_val & ~clr_mask) != reg_val) {
reg_val &= ~clr_mask;
st25r3916_write_reg(handle, reg, reg_val);
}
}
void st25r3916_set_reg_bits(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t set_mask) {
furi_check(handle);
uint8_t reg_val = 0;
st25r3916_read_reg(handle, reg, ®_val);
if((reg_val | set_mask) != reg_val) {
reg_val |= set_mask;
st25r3916_write_reg(handle, reg, reg_val);
}
}
void st25r3916_change_reg_bits(
FuriHalSpiBusHandle* handle,
uint8_t reg,
uint8_t mask,
uint8_t value) {
furi_check(handle);
st25r3916_modify_reg(handle, reg, mask, (mask & value));
}
void st25r3916_modify_reg(
FuriHalSpiBusHandle* handle,
uint8_t reg,
uint8_t clr_mask,
uint8_t set_mask) {
furi_check(handle);
uint8_t reg_val = 0;
uint8_t new_val = 0;
st25r3916_read_reg(handle, reg, ®_val);
new_val = (reg_val & ~clr_mask) | set_mask;
if(new_val != reg_val) {
st25r3916_write_reg(handle, reg, new_val);
}
}
void st25r3916_change_test_reg_bits(
FuriHalSpiBusHandle* handle,
uint8_t reg,
uint8_t mask,
uint8_t value) {
furi_check(handle);
uint8_t reg_val = 0;
uint8_t new_val = 0;
st25r3916_read_test_reg(handle, reg, ®_val);
new_val = (reg_val & ~mask) | (mask & value);
if(new_val != reg_val) {
st25r3916_write_test_reg(handle, reg, new_val);
}
}
bool st25r3916_check_reg(FuriHalSpiBusHandle* handle, uint8_t reg, uint8_t mask, uint8_t val) {
furi_check(handle);
uint8_t reg_val = 0;
st25r3916_read_reg(handle, reg, ®_val);
return (reg_val & mask) == val;
}
| 8,367
|
C
|
.c
| 213
| 34.896714
| 97
| 0.668187
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,172
|
st25r3916.c
|
DarkFlippers_unleashed-firmware/lib/drivers/st25r3916.c
|
#include "st25r3916.h"
#include <furi.h>
void st25r3916_mask_irq(FuriHalSpiBusHandle* handle, uint32_t mask) {
furi_assert(handle);
uint8_t irq_mask_regs[4] = {
mask & 0xff,
(mask >> 8) & 0xff,
(mask >> 16) & 0xff,
(mask >> 24) & 0xff,
};
st25r3916_write_burst_regs(handle, ST25R3916_REG_IRQ_MASK_MAIN, irq_mask_regs, 4);
}
uint32_t st25r3916_get_irq(FuriHalSpiBusHandle* handle) {
furi_assert(handle);
uint8_t irq_regs[4] = {};
uint32_t irq = 0;
st25r3916_read_burst_regs(handle, ST25R3916_REG_IRQ_MASK_MAIN, irq_regs, 4);
// FURI_LOG_I(
// "Mask Irq", "%02X %02X %02X %02X", irq_regs[0], irq_regs[1], irq_regs[2], irq_regs[3]);
st25r3916_read_burst_regs(handle, ST25R3916_REG_IRQ_MAIN, irq_regs, 4);
irq = (uint32_t)irq_regs[0];
irq |= (uint32_t)irq_regs[1] << 8;
irq |= (uint32_t)irq_regs[2] << 16;
irq |= (uint32_t)irq_regs[3] << 24;
// FURI_LOG_I("iRQ", "%02X %02X %02X %02X", irq_regs[0], irq_regs[1], irq_regs[2], irq_regs[3]);
return irq;
}
void st25r3916_write_fifo(FuriHalSpiBusHandle* handle, const uint8_t* buff, size_t bits) {
furi_assert(handle);
furi_assert(buff);
size_t bytes = (bits + 7) / 8;
st25r3916_write_reg(handle, ST25R3916_REG_NUM_TX_BYTES2, (uint8_t)(bits & 0xFFU));
st25r3916_write_reg(handle, ST25R3916_REG_NUM_TX_BYTES1, (uint8_t)((bits >> 8) & 0xFFU));
st25r3916_reg_write_fifo(handle, buff, bytes);
}
bool st25r3916_read_fifo(
FuriHalSpiBusHandle* handle,
uint8_t* buff,
size_t buff_size,
size_t* buff_bits) {
furi_assert(handle);
furi_assert(buff);
bool read_success = false;
do {
uint8_t fifo_status[2] = {};
st25r3916_read_burst_regs(handle, ST25R3916_REG_FIFO_STATUS1, fifo_status, 2);
uint16_t fifo_status_b9_b8 =
((fifo_status[1] & ST25R3916_REG_FIFO_STATUS2_fifo_b_mask) >>
ST25R3916_REG_FIFO_STATUS2_fifo_b_shift);
size_t bytes = (fifo_status_b9_b8 << 8) | fifo_status[0];
uint8_t bits =
((fifo_status[1] & ST25R3916_REG_FIFO_STATUS2_fifo_lb_mask) >>
ST25R3916_REG_FIFO_STATUS2_fifo_lb_shift);
if(bytes == 0) break;
if(bytes > buff_size) break;
st25r3916_reg_read_fifo(handle, buff, bytes);
if(bits) {
*buff_bits = (bytes - 1) * 8 + bits;
} else {
*buff_bits = bytes * 8;
}
read_success = true;
} while(false);
return read_success;
}
| 2,531
|
C
|
.c
| 65
| 32.507692
| 100
| 0.606866
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,174
|
lp5562.c
|
DarkFlippers_unleashed-firmware/lib/drivers/lp5562.c
|
#include "lp5562.h"
#include <core/common_defines.h>
#include "lp5562_reg.h"
#include <furi_hal.h>
void lp5562_reset(FuriHalI2cBusHandle* handle) {
Reg0D_Reset reg = {.value = 0xFF};
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x0D, *(uint8_t*)®, LP5562_I2C_TIMEOUT);
}
void lp5562_configure(FuriHalI2cBusHandle* handle) {
Reg08_Config config = {.INT_CLK_EN = true, .PS_EN = true, .PWM_HF = true};
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x08, *(uint8_t*)&config, LP5562_I2C_TIMEOUT);
Reg70_LedMap map = {
.red = EngSelectI2C,
.green = EngSelectI2C,
.blue = EngSelectI2C,
.white = EngSelectI2C,
};
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x70, *(uint8_t*)&map, LP5562_I2C_TIMEOUT);
}
void lp5562_enable(FuriHalI2cBusHandle* handle) {
Reg00_Enable reg = {.CHIP_EN = true, .LOG_EN = true};
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x00, *(uint8_t*)®, LP5562_I2C_TIMEOUT);
//>488μs delay is required after writing to 0x00 register, otherwise program engine will not work
furi_delay_us(500);
}
void lp5562_set_channel_current(FuriHalI2cBusHandle* handle, LP5562Channel channel, uint8_t value) {
uint8_t reg_no;
if(channel == LP5562ChannelRed) {
reg_no = LP5562_CHANNEL_RED_CURRENT_REGISTER;
} else if(channel == LP5562ChannelGreen) {
reg_no = LP5562_CHANNEL_GREEN_CURRENT_REGISTER;
} else if(channel == LP5562ChannelBlue) {
reg_no = LP5562_CHANNEL_BLUE_CURRENT_REGISTER;
} else if(channel == LP5562ChannelWhite) {
reg_no = LP5562_CHANNEL_WHITE_CURRENT_REGISTER;
} else {
return;
}
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, reg_no, value, LP5562_I2C_TIMEOUT);
}
void lp5562_set_channel_value(FuriHalI2cBusHandle* handle, LP5562Channel channel, uint8_t value) {
uint8_t reg_no;
if(channel == LP5562ChannelRed) {
reg_no = LP5562_CHANNEL_RED_VALUE_REGISTER;
} else if(channel == LP5562ChannelGreen) {
reg_no = LP5562_CHANNEL_GREEN_VALUE_REGISTER;
} else if(channel == LP5562ChannelBlue) {
reg_no = LP5562_CHANNEL_BLUE_VALUE_REGISTER;
} else if(channel == LP5562ChannelWhite) {
reg_no = LP5562_CHANNEL_WHITE_VALUE_REGISTER;
} else {
return;
}
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, reg_no, value, LP5562_I2C_TIMEOUT);
}
uint8_t lp5562_get_channel_value(FuriHalI2cBusHandle* handle, LP5562Channel channel) {
uint8_t reg_no;
uint8_t value;
if(channel == LP5562ChannelRed) {
reg_no = LP5562_CHANNEL_RED_VALUE_REGISTER;
} else if(channel == LP5562ChannelGreen) {
reg_no = LP5562_CHANNEL_GREEN_VALUE_REGISTER;
} else if(channel == LP5562ChannelBlue) {
reg_no = LP5562_CHANNEL_BLUE_VALUE_REGISTER;
} else if(channel == LP5562ChannelWhite) {
reg_no = LP5562_CHANNEL_WHITE_VALUE_REGISTER;
} else {
return 0;
}
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, reg_no, &value, LP5562_I2C_TIMEOUT);
return value;
}
void lp5562_set_channel_src(FuriHalI2cBusHandle* handle, LP5562Channel channel, LP5562Engine src) {
uint8_t reg_val = 0;
uint8_t bit_offset = 0;
do {
if(channel & LP5562ChannelRed) {
bit_offset = 4;
channel &= ~LP5562ChannelRed;
} else if(channel & LP5562ChannelGreen) {
bit_offset = 2;
channel &= ~LP5562ChannelGreen;
} else if(channel & LP5562ChannelBlue) {
bit_offset = 0;
channel &= ~LP5562ChannelBlue;
} else if(channel & LP5562ChannelWhite) {
bit_offset = 6;
channel &= ~LP5562ChannelWhite;
} else {
return;
}
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x70, ®_val, LP5562_I2C_TIMEOUT);
reg_val &= ~(0x3 << bit_offset);
reg_val |= ((src & 0x03) << bit_offset);
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x70, reg_val, LP5562_I2C_TIMEOUT);
} while(channel != 0);
}
void lp5562_execute_program(
FuriHalI2cBusHandle* handle,
LP5562Engine eng,
LP5562Channel ch,
uint16_t* program) {
if((eng < LP5562Engine1) || (eng > LP5562Engine3)) return;
uint8_t reg_val = 0;
uint8_t bit_offset = 0;
uint8_t enable_reg = 0;
// Read old value of enable register
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x00, &enable_reg, LP5562_I2C_TIMEOUT);
// Engine configuration
bit_offset = (3 - eng) * 2;
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x01, ®_val, LP5562_I2C_TIMEOUT);
reg_val &= ~(0x3 << bit_offset);
reg_val |= (0x01 << bit_offset); // load
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x01, reg_val, LP5562_I2C_TIMEOUT);
furi_delay_us(100);
// Program load
for(uint8_t i = 0; i < 16; i++) {
// Program words are big-endian, so reverse byte order before loading
program[i] = __REV16(program[i]);
}
furi_hal_i2c_write_mem(
handle,
LP5562_ADDRESS,
0x10 + (0x20 * (eng - 1)),
(uint8_t*)program,
16 * 2,
LP5562_I2C_TIMEOUT);
// Program start
bit_offset = (3 - eng) * 2;
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x01, ®_val, LP5562_I2C_TIMEOUT);
reg_val &= ~(0x3 << bit_offset);
reg_val |= (0x02 << bit_offset); // run
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x01, reg_val, LP5562_I2C_TIMEOUT);
// Switch output to Execution Engine
lp5562_set_channel_src(handle, ch, eng);
enable_reg &= ~(0x3 << bit_offset);
enable_reg |= (0x02 << bit_offset); // run
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x00, enable_reg, LP5562_I2C_TIMEOUT);
}
void lp5562_stop_program(FuriHalI2cBusHandle* handle, LP5562Engine eng) {
if((eng < LP5562Engine1) || (eng > LP5562Engine3)) return;
uint8_t reg_val = 0;
uint8_t bit_offset = 0;
// Engine configuration
bit_offset = (3 - eng) * 2;
furi_hal_i2c_read_reg_8(handle, LP5562_ADDRESS, 0x01, ®_val, LP5562_I2C_TIMEOUT);
reg_val &= ~(0x3 << bit_offset);
// Not setting lowest 2 bits here
furi_hal_i2c_write_reg_8(handle, LP5562_ADDRESS, 0x01, reg_val, LP5562_I2C_TIMEOUT);
}
void lp5562_execute_ramp(
FuriHalI2cBusHandle* handle,
LP5562Engine eng,
LP5562Channel ch,
uint8_t val_start,
uint8_t val_end,
uint16_t time) {
if(val_start == val_end) return;
// Temporary switch to constant value from register
lp5562_set_channel_src(handle, ch, LP5562Direct);
// Prepare command sequence
uint16_t program[16];
uint8_t diff = (val_end > val_start) ? (val_end - val_start) : (val_start - val_end);
uint16_t time_step = time * 2 / diff;
uint8_t prescaller = 0;
if(time_step > 0x3F) {
time_step /= 32;
prescaller = 1;
}
if(time_step == 0) {
time_step = 1;
} else if(time_step > 0x3F)
time_step = 0x3F;
program[0] = 0x4000 | val_start; // Set PWM
if(val_end > val_start) {
program[1] = (prescaller << 14) | (time_step << 8) | ((diff / 2) & 0x7F); // Ramp Up
} else {
program[1] = (prescaller << 14) | (time_step << 8) | 0x80 |
((diff / 2) & 0x7F); // Ramp Down
}
program[2] = 0xA001 | ((2 - 1) << 7); // Loop to step 1, repeat twice to get full 8-bit scale
program[3] = 0xC000; // End
// Execute program
lp5562_execute_program(handle, eng, LP5562ChannelWhite, program);
// Write end value to register
lp5562_set_channel_value(handle, ch, val_end);
}
void lp5562_execute_blink(
FuriHalI2cBusHandle* handle,
LP5562Engine eng,
LP5562Channel ch,
uint16_t on_time,
uint16_t period,
uint8_t brightness) {
// Temporary switch to constant value from register
lp5562_set_channel_src(handle, ch, LP5562Direct);
// Prepare command sequence
uint16_t program[16];
uint16_t time_step = 0;
uint8_t prescaller = 0;
program[0] = 0x4000 | brightness; // Set PWM
time_step = on_time * 2;
if(time_step > 0x3F) {
time_step /= 32;
prescaller = 1;
} else {
prescaller = 0;
}
if(time_step == 0) {
time_step = 1;
} else if(time_step > 0x3F)
time_step = 0x3F;
program[1] = (prescaller << 14) | (time_step << 8); // Delay
program[2] = 0x4000 | 0; // Set PWM
time_step = (period - on_time) * 2;
if(time_step > 0x3F) {
time_step /= 32;
prescaller = 1;
} else {
prescaller = 0;
}
if(time_step == 0) {
time_step = 1;
} else if(time_step > 0x3F)
time_step = 0x3F;
program[3] = (prescaller << 14) | (time_step << 8); // Delay
program[4] = 0x0000; // Go to start
// Execute program
lp5562_execute_program(handle, eng, ch, program);
}
| 8,866
|
C
|
.c
| 230
| 32.543478
| 102
| 0.632062
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,177
|
wrappers.c
|
DarkFlippers_unleashed-firmware/lib/print/wrappers.c
|
#include "wrappers.h"
#include <stdbool.h>
#include <stdarg.h>
#include <furi/core/check.h>
#include <furi/core/thread.h>
#include <furi/core/common_defines.h>
#include "printf_tiny.h"
void _putchar(char character) {
furi_thread_stdout_write(&character, 1);
}
int __wrap_printf(const char* format, ...) {
va_list args;
va_start(args, format);
int ret = vprintf_(format, args);
va_end(args);
return ret;
}
int __wrap_vsnprintf(char* str, size_t size, const char* format, va_list args) {
return vsnprintf_(str, size, format, args);
}
int __wrap_puts(const char* str) {
size_t size = furi_thread_stdout_write(str, strlen(str));
size += furi_thread_stdout_write("\n", 1);
return size;
}
int __wrap_putchar(int ch) {
size_t size = furi_thread_stdout_write((char*)&ch, 1);
return size;
}
int __wrap_putc(int ch, FILE* stream) {
UNUSED(stream);
size_t size = furi_thread_stdout_write((char*)&ch, 1);
return size;
}
int __wrap_snprintf(char* str, size_t size, const char* format, ...) {
va_list args;
va_start(args, format);
int ret = __wrap_vsnprintf(str, size, format, args);
va_end(args);
return ret;
}
int __wrap_fflush(FILE* stream) {
UNUSED(stream);
furi_thread_stdout_flush();
return 0;
}
__attribute__((__noreturn__)) void __wrap___assert(const char* file, int line, const char* e) {
UNUSED(file);
UNUSED(line);
furi_crash(e);
}
__attribute__((__noreturn__)) void
__wrap___assert_func(const char* file, int line, const char* func, const char* e) {
UNUSED(file);
UNUSED(line);
UNUSED(func);
furi_crash(e);
}
| 1,644
|
C
|
.c
| 58
| 24.982759
| 95
| 0.652893
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,180
|
hid_service.c
|
DarkFlippers_unleashed-firmware/lib/ble_profile/extra_services/hid_service.c
|
#include "hid_service.h"
#include "app_common.h" // IWYU pragma: keep
#include <ble/ble.h>
#include <furi_ble/event_dispatcher.h>
#include <furi_ble/gatt.h>
#include <furi.h>
#include <stdint.h>
#define TAG "BleHid"
#define BLE_SVC_HID_REPORT_MAP_MAX_LEN (255)
#define BLE_SVC_HID_REPORT_MAX_LEN (255)
#define BLE_SVC_HID_REPORT_REF_LEN (2)
#define BLE_SVC_HID_INFO_LEN (4)
#define BLE_SVC_HID_CONTROL_POINT_LEN (1)
#define BLE_SVC_HID_INPUT_REPORT_COUNT (3)
#define BLE_SVC_HID_OUTPUT_REPORT_COUNT (0)
#define BLE_SVC_HID_FEATURE_REPORT_COUNT (0)
#define BLE_SVC_HID_REPORT_COUNT \
(BLE_SVC_HID_INPUT_REPORT_COUNT + BLE_SVC_HID_OUTPUT_REPORT_COUNT + \
BLE_SVC_HID_FEATURE_REPORT_COUNT)
typedef enum {
HidSvcGattCharacteristicProtocolMode = 0,
HidSvcGattCharacteristicReportMap,
HidSvcGattCharacteristicInfo,
HidSvcGattCharacteristicCtrlPoint,
HidSvcGattCharacteristicCount,
} HidSvcGattCharacteristicId;
typedef struct {
uint8_t report_idx;
uint8_t report_type;
} HidSvcReportId;
static_assert(sizeof(HidSvcReportId) == sizeof(uint16_t), "HidSvcReportId must be 2 bytes");
static const Service_UUID_t ble_svc_hid_uuid = {
.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
};
static bool ble_svc_hid_char_desc_data_callback(
const void* context,
const uint8_t** data,
uint16_t* data_len) {
const HidSvcReportId* report_id = context;
*data_len = sizeof(HidSvcReportId);
if(data) {
*data = (const uint8_t*)report_id;
}
return false;
}
typedef struct {
const void* data_ptr;
uint16_t data_len;
} HidSvcDataWrapper;
static bool ble_svc_hid_report_data_callback(
const void* context,
const uint8_t** data,
uint16_t* data_len) {
const HidSvcDataWrapper* report_data = context;
if(data) {
*data = report_data->data_ptr;
*data_len = report_data->data_len;
} else {
*data_len = BLE_SVC_HID_REPORT_MAP_MAX_LEN;
}
return false;
}
static const BleGattCharacteristicParams ble_svc_hid_chars[HidSvcGattCharacteristicCount] = {
[HidSvcGattCharacteristicProtocolMode] =
{.name = "Protocol Mode",
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = 1,
.uuid.Char_UUID_16 = PROTOCOL_MODE_CHAR_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[HidSvcGattCharacteristicReportMap] =
{.name = "Report Map",
.data_prop_type = FlipperGattCharacteristicDataCallback,
.data.callback.fn = ble_svc_hid_report_data_callback,
.data.callback.context = NULL,
.uuid.Char_UUID_16 = REPORT_MAP_CHAR_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_VARIABLE},
[HidSvcGattCharacteristicInfo] =
{.name = "HID Information",
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = BLE_SVC_HID_INFO_LEN,
.data.fixed.ptr = NULL,
.uuid.Char_UUID_16 = HID_INFORMATION_CHAR_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ,
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
[HidSvcGattCharacteristicCtrlPoint] =
{.name = "HID Control Point",
.data_prop_type = FlipperGattCharacteristicDataFixed,
.data.fixed.length = BLE_SVC_HID_CONTROL_POINT_LEN,
.uuid.Char_UUID_16 = HID_CONTROL_POINT_CHAR_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_WRITE_WITHOUT_RESP,
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
.is_variable = CHAR_VALUE_LEN_CONSTANT},
};
static const BleGattCharacteristicDescriptorParams ble_svc_hid_char_descr_template = {
.uuid_type = UUID_TYPE_16,
.uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID,
.max_length = BLE_SVC_HID_REPORT_REF_LEN,
.data_callback.fn = ble_svc_hid_char_desc_data_callback,
.security_permissions = ATTR_PERMISSION_NONE,
.access_permissions = ATTR_ACCESS_READ_WRITE,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_CONSTANT,
};
static const BleGattCharacteristicParams ble_svc_hid_report_template = {
.name = "Report",
.data_prop_type = FlipperGattCharacteristicDataCallback,
.data.callback.fn = ble_svc_hid_report_data_callback,
.data.callback.context = NULL,
.uuid.Char_UUID_16 = REPORT_CHAR_UUID,
.uuid_type = UUID_TYPE_16,
.char_properties = CHAR_PROP_READ | CHAR_PROP_NOTIFY,
.security_permissions = ATTR_PERMISSION_NONE,
.gatt_evt_mask = GATT_DONT_NOTIFY_EVENTS,
.is_variable = CHAR_VALUE_LEN_VARIABLE,
};
struct BleServiceHid {
uint16_t svc_handle;
BleGattCharacteristicInstance chars[HidSvcGattCharacteristicCount];
BleGattCharacteristicInstance input_report_chars[BLE_SVC_HID_INPUT_REPORT_COUNT];
BleGattCharacteristicInstance output_report_chars[BLE_SVC_HID_OUTPUT_REPORT_COUNT];
BleGattCharacteristicInstance feature_report_chars[BLE_SVC_HID_FEATURE_REPORT_COUNT];
GapSvcEventHandler* event_handler;
};
static BleEventAckStatus ble_svc_hid_event_handler(void* event, void* context) {
UNUSED(context);
BleEventAckStatus ret = BleEventNotAck;
hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
// aci_gatt_attribute_modified_event_rp0* attribute_modified;
if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
// Process modification events
ret = BleEventAckFlowEnable;
} else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
// Process notification confirmation
ret = BleEventAckFlowEnable;
}
}
return ret;
}
BleServiceHid* ble_svc_hid_start(void) {
BleServiceHid* hid_svc = malloc(sizeof(BleServiceHid));
// Register event handler
hid_svc->event_handler =
ble_event_dispatcher_register_svc_handler(ble_svc_hid_event_handler, hid_svc);
/**
* Add Human Interface Device Service
*/
if(!ble_gatt_service_add(
UUID_TYPE_16,
&ble_svc_hid_uuid,
PRIMARY_SERVICE,
2 + /* protocol mode */
(4 * BLE_SVC_HID_INPUT_REPORT_COUNT) + (3 * BLE_SVC_HID_OUTPUT_REPORT_COUNT) +
(3 * BLE_SVC_HID_FEATURE_REPORT_COUNT) + 1 + 2 + 2 +
2, /* Service + Report Map + HID Information + HID Control Point */
&hid_svc->svc_handle)) {
free(hid_svc);
return NULL;
}
// Maintain previously defined characteristic order
ble_gatt_characteristic_init(
hid_svc->svc_handle,
&ble_svc_hid_chars[HidSvcGattCharacteristicProtocolMode],
&hid_svc->chars[HidSvcGattCharacteristicProtocolMode]);
uint8_t protocol_mode = 1;
ble_gatt_characteristic_update(
hid_svc->svc_handle,
&hid_svc->chars[HidSvcGattCharacteristicProtocolMode],
&protocol_mode);
// reports
BleGattCharacteristicDescriptorParams ble_svc_hid_char_descr;
BleGattCharacteristicParams report_char;
HidSvcReportId report_id;
memcpy(
&ble_svc_hid_char_descr, &ble_svc_hid_char_descr_template, sizeof(ble_svc_hid_char_descr));
memcpy(&report_char, &ble_svc_hid_report_template, sizeof(report_char));
ble_svc_hid_char_descr.data_callback.context = &report_id;
report_char.descriptor_params = &ble_svc_hid_char_descr;
typedef struct {
uint8_t report_type;
uint8_t report_count;
BleGattCharacteristicInstance* chars;
} HidSvcReportCharProps;
HidSvcReportCharProps hid_report_chars[] = {
{0x01, BLE_SVC_HID_INPUT_REPORT_COUNT, hid_svc->input_report_chars},
{0x02, BLE_SVC_HID_OUTPUT_REPORT_COUNT, hid_svc->output_report_chars},
{0x03, BLE_SVC_HID_FEATURE_REPORT_COUNT, hid_svc->feature_report_chars},
};
for(size_t report_type_idx = 0; report_type_idx < COUNT_OF(hid_report_chars);
report_type_idx++) {
report_id.report_type = hid_report_chars[report_type_idx].report_type;
for(size_t report_idx = 0; report_idx < hid_report_chars[report_type_idx].report_count;
report_idx++) {
report_id.report_idx = report_idx + 1;
ble_gatt_characteristic_init(
hid_svc->svc_handle,
&report_char,
&hid_report_chars[report_type_idx].chars[report_idx]);
}
}
// Setup remaining characteristics
for(size_t i = HidSvcGattCharacteristicReportMap; i < HidSvcGattCharacteristicCount; i++) {
ble_gatt_characteristic_init(
hid_svc->svc_handle, &ble_svc_hid_chars[i], &hid_svc->chars[i]);
}
return hid_svc;
}
bool ble_svc_hid_update_report_map(BleServiceHid* hid_svc, const uint8_t* data, uint16_t len) {
furi_assert(data);
furi_assert(hid_svc);
HidSvcDataWrapper report_data = {
.data_ptr = data,
.data_len = len,
};
return ble_gatt_characteristic_update(
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicReportMap], &report_data);
}
bool ble_svc_hid_update_input_report(
BleServiceHid* hid_svc,
uint8_t input_report_num,
uint8_t* data,
uint16_t len) {
furi_assert(data);
furi_assert(hid_svc);
furi_assert(input_report_num < BLE_SVC_HID_INPUT_REPORT_COUNT);
HidSvcDataWrapper report_data = {
.data_ptr = data,
.data_len = len,
};
return ble_gatt_characteristic_update(
hid_svc->svc_handle, &hid_svc->input_report_chars[input_report_num], &report_data);
}
bool ble_svc_hid_update_info(BleServiceHid* hid_svc, uint8_t* data) {
furi_assert(data);
furi_assert(hid_svc);
return ble_gatt_characteristic_update(
hid_svc->svc_handle, &hid_svc->chars[HidSvcGattCharacteristicInfo], &data);
}
void ble_svc_hid_stop(BleServiceHid* hid_svc) {
furi_assert(hid_svc);
ble_event_dispatcher_unregister_svc_handler(hid_svc->event_handler);
// Delete characteristics
for(size_t i = 0; i < HidSvcGattCharacteristicCount; i++) {
ble_gatt_characteristic_delete(hid_svc->svc_handle, &hid_svc->chars[i]);
}
typedef struct {
uint8_t report_count;
BleGattCharacteristicInstance* chars;
} HidSvcReportCharProps;
HidSvcReportCharProps hid_report_chars[] = {
{BLE_SVC_HID_INPUT_REPORT_COUNT, hid_svc->input_report_chars},
{BLE_SVC_HID_OUTPUT_REPORT_COUNT, hid_svc->output_report_chars},
{BLE_SVC_HID_FEATURE_REPORT_COUNT, hid_svc->feature_report_chars},
};
for(size_t report_type_idx = 0; report_type_idx < COUNT_OF(hid_report_chars);
report_type_idx++) {
for(size_t report_idx = 0; report_idx < hid_report_chars[report_type_idx].report_count;
report_idx++) {
ble_gatt_characteristic_delete(
hid_svc->svc_handle, &hid_report_chars[report_type_idx].chars[report_idx]);
}
}
// Delete service
ble_gatt_service_delete(hid_svc->svc_handle);
free(hid_svc);
}
| 11,729
|
C
|
.c
| 280
| 35.457143
| 99
| 0.675695
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,182
|
ibutton_worker.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/ibutton_worker.c
|
#include "ibutton_worker_i.h"
#include "ibutton_protocols.h"
#include <core/check.h>
typedef enum {
iButtonMessageEnd,
iButtonMessageStop,
iButtonMessageRead,
iButtonMessageWriteId,
iButtonMessageWriteCopy,
iButtonMessageEmulate,
iButtonMessageNotifyEmulate,
} iButtonMessageType;
typedef struct {
iButtonMessageType type;
union {
iButtonKey* key;
} data;
} iButtonMessage;
static int32_t ibutton_worker_thread(void* thread_context);
iButtonWorker* ibutton_worker_alloc(iButtonProtocols* protocols) {
furi_check(protocols);
iButtonWorker* worker = malloc(sizeof(iButtonWorker));
worker->protocols = protocols;
worker->messages = furi_message_queue_alloc(1, sizeof(iButtonMessage));
worker->mode_index = iButtonWorkerModeIdle;
worker->thread = furi_thread_alloc_ex("iButtonWorker", 2048, ibutton_worker_thread, worker);
return worker;
}
void ibutton_worker_read_set_callback(
iButtonWorker* worker,
iButtonWorkerReadCallback callback,
void* context) {
furi_check(worker);
furi_check(worker->mode_index == iButtonWorkerModeIdle);
worker->read_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_write_set_callback(
iButtonWorker* worker,
iButtonWorkerWriteCallback callback,
void* context) {
furi_check(worker);
furi_check(worker->mode_index == iButtonWorkerModeIdle);
worker->write_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_emulate_set_callback(
iButtonWorker* worker,
iButtonWorkerEmulateCallback callback,
void* context) {
furi_check(worker);
furi_check(worker->mode_index == iButtonWorkerModeIdle);
worker->emulate_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_read_start(iButtonWorker* worker, iButtonKey* key) {
furi_check(worker);
iButtonMessage message = {.type = iButtonMessageRead, .data.key = key};
furi_check(
furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
}
void ibutton_worker_write_id_start(iButtonWorker* worker, iButtonKey* key) {
furi_check(worker);
furi_check(key);
iButtonMessage message = {.type = iButtonMessageWriteId, .data.key = key};
furi_check(
furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
}
void ibutton_worker_write_copy_start(iButtonWorker* worker, iButtonKey* key) {
furi_check(worker);
furi_check(key);
iButtonMessage message = {.type = iButtonMessageWriteCopy, .data.key = key};
furi_check(
furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
}
void ibutton_worker_emulate_start(iButtonWorker* worker, iButtonKey* key) {
furi_check(worker);
iButtonMessage message = {.type = iButtonMessageEmulate, .data.key = key};
furi_check(
furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
}
void ibutton_worker_stop(iButtonWorker* worker) {
furi_check(worker);
iButtonMessage message = {.type = iButtonMessageStop};
furi_check(
furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
}
void ibutton_worker_free(iButtonWorker* worker) {
furi_check(worker);
furi_message_queue_free(worker->messages);
furi_thread_free(worker->thread);
free(worker);
}
void ibutton_worker_start_thread(iButtonWorker* worker) {
furi_check(worker);
furi_thread_start(worker->thread);
}
void ibutton_worker_stop_thread(iButtonWorker* worker) {
furi_check(worker);
iButtonMessage message = {.type = iButtonMessageEnd};
furi_check(
furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
furi_thread_join(worker->thread);
}
void ibutton_worker_switch_mode(iButtonWorker* worker, iButtonWorkerMode mode) {
ibutton_worker_modes[worker->mode_index].stop(worker);
worker->mode_index = mode;
ibutton_worker_modes[worker->mode_index].start(worker);
}
void ibutton_worker_notify_emulate(iButtonWorker* worker) {
iButtonMessage message = {.type = iButtonMessageNotifyEmulate};
// we're running in an interrupt context, so we can't wait
// and we can drop message if queue is full, that's ok for that message
furi_message_queue_put(worker->messages, &message, 0);
}
void ibutton_worker_set_key_p(iButtonWorker* worker, iButtonKey* key) {
worker->key = key;
}
static int32_t ibutton_worker_thread(void* thread_context) {
iButtonWorker* worker = thread_context;
bool running = true;
iButtonMessage message;
FuriStatus status;
ibutton_worker_modes[worker->mode_index].start(worker);
while(running) {
status = furi_message_queue_get(
worker->messages, &message, ibutton_worker_modes[worker->mode_index].quant);
if(status == FuriStatusOk) {
switch(message.type) {
case iButtonMessageEnd:
ibutton_worker_switch_mode(worker, iButtonWorkerModeIdle);
ibutton_worker_set_key_p(worker, NULL);
running = false;
break;
case iButtonMessageStop:
ibutton_worker_switch_mode(worker, iButtonWorkerModeIdle);
ibutton_worker_set_key_p(worker, NULL);
break;
case iButtonMessageRead:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerModeRead);
break;
case iButtonMessageWriteId:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerModeWriteId);
break;
case iButtonMessageWriteCopy:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerModeWriteCopy);
break;
case iButtonMessageEmulate:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerModeEmulate);
break;
case iButtonMessageNotifyEmulate:
if(worker->emulate_cb) {
worker->emulate_cb(worker->cb_ctx, true);
}
break;
}
} else if(status == FuriStatusErrorTimeout) {
ibutton_worker_modes[worker->mode_index].tick(worker);
} else {
furi_crash("iButton worker error");
}
}
ibutton_worker_modes[worker->mode_index].stop(worker);
return 0;
}
| 6,692
|
C
|
.c
| 169
| 32.757396
| 96
| 0.687925
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,184
|
ibutton_key.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/ibutton_key.c
|
#include "ibutton_key_i.h"
#include <furi.h>
struct iButtonKey {
iButtonProtocolId protocol_id;
iButtonProtocolData* protocol_data;
size_t protocol_data_size;
};
iButtonKey* ibutton_key_alloc(size_t data_size) {
iButtonKey* key = malloc(sizeof(iButtonKey));
key->protocol_id = iButtonProtocolIdInvalid;
key->protocol_data = malloc(data_size);
key->protocol_data_size = data_size;
return key;
}
void ibutton_key_free(iButtonKey* key) {
furi_check(key);
free(key->protocol_data);
free(key);
}
void ibutton_key_reset(iButtonKey* key) {
furi_check(key);
key->protocol_id = iButtonProtocolIdInvalid;
memset(key->protocol_data, 0, key->protocol_data_size);
}
iButtonProtocolId ibutton_key_get_protocol_id(const iButtonKey* key) {
furi_check(key);
return key->protocol_id;
}
void ibutton_key_set_protocol_id(iButtonKey* key, iButtonProtocolId protocol_id) {
furi_check(key);
key->protocol_id = protocol_id;
}
iButtonProtocolData* ibutton_key_get_protocol_data(const iButtonKey* key) {
return key->protocol_data;
}
size_t ibutton_key_get_protocol_data_size(const iButtonKey* key) {
return key->protocol_data_size;
}
| 1,202
|
C
|
.c
| 38
| 28.157895
| 82
| 0.733043
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,185
|
ibutton_protocols.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/ibutton_protocols.c
|
#include "ibutton_protocols.h"
#include <storage/storage.h>
#include "ibutton_key_i.h"
#include "protocols/protocol_group_defs.h"
#define IBUTTON_FILE_TYPE "Flipper iButton key"
#define IBUTTON_PROTOCOL_KEY_V1 "Key type"
#define IBUTTON_PROTOCOL_KEY_V2 "Protocol"
#define IBUTTON_CURRENT_FORMAT_VERSION 2U
#define GET_PROTOCOL_GROUP(id) \
iButtonProtocolGroupInfo info; \
ibutton_protocols_get_group_by_id(protocols, (id), &info);
#define GROUP_BASE (info.base)
#define GROUP_DATA (info.group)
#define PROTOCOL_ID (info.id)
struct iButtonProtocols {
iButtonProtocolGroupData** group_datas;
};
typedef struct {
const iButtonProtocolGroupBase* base;
iButtonProtocolGroupData* group;
iButtonProtocolLocalId id;
} iButtonProtocolGroupInfo;
static void ibutton_protocols_get_group_by_id(
iButtonProtocols* protocols,
iButtonProtocolId id,
iButtonProtocolGroupInfo* info) {
iButtonProtocolLocalId local_id = id;
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
if(local_id < (signed)ibutton_protocol_groups[i]->protocol_count) {
info->base = ibutton_protocol_groups[i];
info->group = protocols->group_datas[i];
info->id = local_id;
return;
} else {
local_id -= ibutton_protocol_groups[i]->protocol_count;
}
}
furi_crash();
}
iButtonProtocols* ibutton_protocols_alloc(void) {
iButtonProtocols* protocols = malloc(sizeof(iButtonProtocols*));
protocols->group_datas = malloc(sizeof(iButtonProtocolGroupData*) * iButtonProtocolGroupMax);
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
protocols->group_datas[i] = ibutton_protocol_groups[i]->alloc();
}
return protocols;
}
void ibutton_protocols_free(iButtonProtocols* protocols) {
furi_check(protocols);
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
ibutton_protocol_groups[i]->free(protocols->group_datas[i]);
}
free(protocols->group_datas);
free(protocols);
}
uint32_t ibutton_protocols_get_protocol_count(void) {
uint32_t count = 0;
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
count += ibutton_protocol_groups[i]->protocol_count;
}
return count;
}
iButtonProtocolId ibutton_protocols_get_id_by_name(iButtonProtocols* protocols, const char* name) {
furi_check(protocols);
furi_check(name);
iButtonProtocolLocalId offset = 0;
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
iButtonProtocolLocalId local_id;
if(ibutton_protocol_groups[i]->get_id_by_name(protocols->group_datas[i], &local_id, name)) {
return local_id + offset;
}
offset += ibutton_protocol_groups[i]->protocol_count;
}
return iButtonProtocolIdInvalid;
}
uint32_t ibutton_protocols_get_features(iButtonProtocols* protocols, iButtonProtocolId id) {
furi_check(protocols);
GET_PROTOCOL_GROUP(id);
return GROUP_BASE->get_features(GROUP_DATA, PROTOCOL_ID);
}
size_t ibutton_protocols_get_max_data_size(iButtonProtocols* protocols) {
furi_check(protocols);
size_t max_size = 0;
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
const size_t current_max_size =
ibutton_protocol_groups[i]->get_max_data_size(protocols->group_datas[i]);
if(current_max_size > max_size) {
max_size = current_max_size;
}
}
return max_size;
}
const char* ibutton_protocols_get_manufacturer(iButtonProtocols* protocols, iButtonProtocolId id) {
furi_check(protocols);
GET_PROTOCOL_GROUP(id);
return GROUP_BASE->get_manufacturer(GROUP_DATA, PROTOCOL_ID);
}
const char* ibutton_protocols_get_name(iButtonProtocols* protocols, iButtonProtocolId id) {
furi_check(protocols);
GET_PROTOCOL_GROUP(id);
return GROUP_BASE->get_name(GROUP_DATA, PROTOCOL_ID);
}
bool ibutton_protocols_read(iButtonProtocols* protocols, iButtonKey* key) {
furi_check(protocols);
furi_check(key);
iButtonProtocolLocalId id = iButtonProtocolIdInvalid;
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
iButtonProtocolLocalId offset = 0;
for(iButtonProtocolGroupId i = 0; i < iButtonProtocolGroupMax; ++i) {
if(ibutton_protocol_groups[i]->read(protocols->group_datas[i], data, &id)) {
id += offset;
break;
}
offset += ibutton_protocol_groups[i]->protocol_count;
}
ibutton_key_set_protocol_id(key, id);
return id != iButtonProtocolIdInvalid;
}
bool ibutton_protocols_write_id(iButtonProtocols* protocols, iButtonKey* key) {
furi_check(protocols);
furi_check(key);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
return GROUP_BASE->write_id(GROUP_DATA, data, PROTOCOL_ID);
}
bool ibutton_protocols_write_copy(iButtonProtocols* protocols, iButtonKey* key) {
furi_check(protocols);
furi_check(key);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
return GROUP_BASE->write_copy(GROUP_DATA, data, PROTOCOL_ID);
}
void ibutton_protocols_emulate_start(iButtonProtocols* protocols, iButtonKey* key) {
furi_check(protocols);
furi_check(key);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->emulate_start(GROUP_DATA, data, PROTOCOL_ID);
}
void ibutton_protocols_emulate_stop(iButtonProtocols* protocols, iButtonKey* key) {
furi_check(protocols);
furi_check(key);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->emulate_stop(GROUP_DATA, data, PROTOCOL_ID);
}
bool ibutton_protocols_save(
iButtonProtocols* protocols,
const iButtonKey* key,
const char* file_name) {
furi_check(protocols);
furi_check(key);
furi_check(file_name);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
const iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
bool success = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
do {
const char* protocol_name = ibutton_protocols_get_name(protocols, id);
if(!flipper_format_buffered_file_open_always(ff, file_name)) break;
if(!flipper_format_write_header_cstr(ff, IBUTTON_FILE_TYPE, IBUTTON_CURRENT_FORMAT_VERSION))
break;
if(!flipper_format_write_string_cstr(ff, IBUTTON_PROTOCOL_KEY_V2, protocol_name)) break;
GET_PROTOCOL_GROUP(id);
if(!GROUP_BASE->save(GROUP_DATA, data, PROTOCOL_ID, ff)) break;
success = true;
} while(false);
flipper_format_free(ff);
furi_record_close(RECORD_STORAGE);
return success;
}
bool ibutton_protocols_load(iButtonProtocols* protocols, iButtonKey* key, const char* file_name) {
furi_check(protocols);
furi_check(key);
furi_check(file_name);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
bool success = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
FuriString* tmp = furi_string_alloc();
do {
if(!flipper_format_buffered_file_open_existing(ff, file_name)) break;
uint32_t version;
if(!flipper_format_read_header(ff, tmp, &version)) break;
if(!furi_string_equal(tmp, IBUTTON_FILE_TYPE)) break;
if(version == 1) {
if(!flipper_format_read_string(ff, IBUTTON_PROTOCOL_KEY_V1, tmp)) break;
} else if(version == 2) {
if(!flipper_format_read_string(ff, IBUTTON_PROTOCOL_KEY_V2, tmp)) break;
} else {
break;
}
const iButtonProtocolId id =
ibutton_protocols_get_id_by_name(protocols, furi_string_get_cstr(tmp));
ibutton_key_set_protocol_id(key, id);
GET_PROTOCOL_GROUP(id);
if(!GROUP_BASE->load(GROUP_DATA, data, PROTOCOL_ID, version, ff)) break;
success = true;
} while(false);
flipper_format_free(ff);
furi_string_free(tmp);
furi_record_close(RECORD_STORAGE);
return success;
}
void ibutton_protocols_render_uid(
iButtonProtocols* protocols,
const iButtonKey* key,
FuriString* result) {
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
const iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->render_uid(GROUP_DATA, data, PROTOCOL_ID, result);
}
void ibutton_protocols_render_data(
iButtonProtocols* protocols,
const iButtonKey* key,
FuriString* result) {
furi_check(protocols);
furi_check(key);
furi_check(result);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
const iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->render_data(GROUP_DATA, data, PROTOCOL_ID, result);
}
void ibutton_protocols_render_brief_data(
iButtonProtocols* protocols,
const iButtonKey* key,
FuriString* result) {
furi_check(protocols);
furi_check(key);
furi_check(result);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
const iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->render_brief_data(GROUP_DATA, data, PROTOCOL_ID, result);
}
void ibutton_protocols_render_error(
iButtonProtocols* protocols,
const iButtonKey* key,
FuriString* result) {
furi_check(protocols);
furi_check(key);
furi_check(result);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
const iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->render_error(GROUP_DATA, data, PROTOCOL_ID, result);
}
bool ibutton_protocols_is_valid(iButtonProtocols* protocols, const iButtonKey* key) {
furi_check(protocols);
furi_check(key);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
const iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
return GROUP_BASE->is_valid(GROUP_DATA, data, PROTOCOL_ID);
}
void ibutton_protocols_get_editable_data(
iButtonProtocols* protocols,
const iButtonKey* key,
iButtonEditableData* editable) {
furi_check(protocols);
furi_check(key);
furi_check(editable);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->get_editable_data(GROUP_DATA, data, PROTOCOL_ID, editable);
}
void ibutton_protocols_apply_edits(iButtonProtocols* protocols, const iButtonKey* key) {
furi_check(protocols);
furi_check(key);
const iButtonProtocolId id = ibutton_key_get_protocol_id(key);
iButtonProtocolData* data = ibutton_key_get_protocol_data(key);
GET_PROTOCOL_GROUP(id);
GROUP_BASE->apply_edits(GROUP_DATA, data, PROTOCOL_ID);
}
| 11,507
|
C
|
.c
| 282
| 35.397163
| 100
| 0.710947
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,186
|
ibutton_worker_modes.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/ibutton_worker_modes.c
|
#include "ibutton_worker_i.h"
#include <core/check.h>
#include <furi_hal_rfid.h>
#include <furi_hal_power.h>
#include "ibutton_protocols.h"
static void ibutton_worker_mode_idle_start(iButtonWorker* worker);
static void ibutton_worker_mode_idle_tick(iButtonWorker* worker);
static void ibutton_worker_mode_idle_stop(iButtonWorker* worker);
static void ibutton_worker_mode_emulate_start(iButtonWorker* worker);
static void ibutton_worker_mode_emulate_tick(iButtonWorker* worker);
static void ibutton_worker_mode_emulate_stop(iButtonWorker* worker);
static void ibutton_worker_mode_read_start(iButtonWorker* worker);
static void ibutton_worker_mode_read_tick(iButtonWorker* worker);
static void ibutton_worker_mode_read_stop(iButtonWorker* worker);
static void ibutton_worker_mode_write_common_start(iButtonWorker* worker);
static void ibutton_worker_mode_write_id_tick(iButtonWorker* worker);
static void ibutton_worker_mode_write_copy_tick(iButtonWorker* worker);
static void ibutton_worker_mode_write_common_stop(iButtonWorker* worker);
const iButtonWorkerModeType ibutton_worker_modes[] = {
{
.quant = FuriWaitForever,
.start = ibutton_worker_mode_idle_start,
.tick = ibutton_worker_mode_idle_tick,
.stop = ibutton_worker_mode_idle_stop,
},
{
.quant = 100,
.start = ibutton_worker_mode_read_start,
.tick = ibutton_worker_mode_read_tick,
.stop = ibutton_worker_mode_read_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_write_common_start,
.tick = ibutton_worker_mode_write_id_tick,
.stop = ibutton_worker_mode_write_common_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_write_common_start,
.tick = ibutton_worker_mode_write_copy_tick,
.stop = ibutton_worker_mode_write_common_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_emulate_start,
.tick = ibutton_worker_mode_emulate_tick,
.stop = ibutton_worker_mode_emulate_stop,
},
};
/*********************** IDLE ***********************/
void ibutton_worker_mode_idle_start(iButtonWorker* worker) {
UNUSED(worker);
}
void ibutton_worker_mode_idle_tick(iButtonWorker* worker) {
UNUSED(worker);
}
void ibutton_worker_mode_idle_stop(iButtonWorker* worker) {
UNUSED(worker);
}
/*********************** READ ***********************/
void ibutton_worker_mode_read_start(iButtonWorker* worker) {
UNUSED(worker);
if(!furi_hal_power_is_otg_enabled()) furi_hal_power_enable_otg();
}
void ibutton_worker_mode_read_tick(iButtonWorker* worker) {
if(ibutton_protocols_read(worker->protocols, worker->key)) {
if(worker->read_cb != NULL) {
worker->read_cb(worker->cb_ctx);
}
ibutton_worker_switch_mode(worker, iButtonWorkerModeIdle);
}
}
void ibutton_worker_mode_read_stop(iButtonWorker* worker) {
UNUSED(worker);
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
}
/*********************** EMULATE ***********************/
void ibutton_worker_mode_emulate_start(iButtonWorker* worker) {
furi_assert(worker->key);
furi_hal_rfid_pins_reset();
furi_hal_rfid_pin_pull_pulldown();
ibutton_protocols_emulate_start(worker->protocols, worker->key);
}
void ibutton_worker_mode_emulate_tick(iButtonWorker* worker) {
UNUSED(worker);
}
void ibutton_worker_mode_emulate_stop(iButtonWorker* worker) {
furi_assert(worker->key);
ibutton_protocols_emulate_stop(worker->protocols, worker->key);
furi_hal_rfid_pins_reset();
}
/*********************** WRITE ***********************/
void ibutton_worker_mode_write_common_start(iButtonWorker* worker) { //-V524
UNUSED(worker);
if(!furi_hal_power_is_otg_enabled()) furi_hal_power_enable_otg();
}
void ibutton_worker_mode_write_id_tick(iButtonWorker* worker) {
furi_assert(worker->key);
const bool success = ibutton_protocols_write_id(worker->protocols, worker->key);
// TODO FL-3527: pass a proper result to the callback
const iButtonWorkerWriteResult result = success ? iButtonWorkerWriteOK :
iButtonWorkerWriteNoDetect;
if(worker->write_cb != NULL) {
worker->write_cb(worker->cb_ctx, result);
}
}
void ibutton_worker_mode_write_copy_tick(iButtonWorker* worker) {
furi_assert(worker->key);
const bool success = ibutton_protocols_write_copy(worker->protocols, worker->key);
// TODO FL-3527: pass a proper result to the callback
const iButtonWorkerWriteResult result = success ? iButtonWorkerWriteOK :
iButtonWorkerWriteNoDetect;
if(worker->write_cb != NULL) {
worker->write_cb(worker->cb_ctx, result);
}
}
void ibutton_worker_mode_write_common_stop(iButtonWorker* worker) { //-V524
UNUSED(worker);
if(furi_hal_power_is_otg_enabled()) furi_hal_power_disable_otg();
}
| 4,987
|
C
|
.c
| 121
| 35.884298
| 86
| 0.675424
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,188
|
protocol_common.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/protocol_common.h
|
#pragma once
#include <stdint.h>
#include <stddef.h>
typedef int32_t iButtonProtocolId;
enum {
iButtonProtocolIdInvalid = -1,
};
typedef enum {
iButtonProtocolFeatureExtData = (1U << 0),
iButtonProtocolFeatureWriteId = (1U << 1),
iButtonProtocolFeatureWriteCopy = (1U << 2),
} iButtonProtocolFeature;
typedef struct {
uint8_t* ptr;
size_t size;
} iButtonEditableData;
| 397
|
C
|
.c
| 16
| 22
| 48
| 0.739362
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,189
|
protocol_common_i.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/protocol_common_i.h
|
#pragma once
#include "protocol_common.h"
typedef void iButtonProtocolData;
typedef int32_t iButtonProtocolLocalId;
| 118
|
C
|
.c
| 4
| 28
| 39
| 0.866071
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,190
|
protocol_group_defs.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/protocol_group_defs.c
|
#include "protocol_group_defs.h"
#include "dallas/protocol_group_dallas.h"
#include "misc/protocol_group_misc.h"
const iButtonProtocolGroupBase* ibutton_protocol_groups[] = {
[iButtonProtocolGroupDallas] = &ibutton_protocol_group_dallas,
[iButtonProtocolGroupMisc] = &ibutton_protocol_group_misc,
};
| 310
|
C
|
.c
| 7
| 41.857143
| 66
| 0.787375
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,191
|
protocol_group_defs.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/protocol_group_defs.h
|
#pragma once
#include "protocol_group_base.h"
typedef enum {
iButtonProtocolGroupDallas,
iButtonProtocolGroupMisc,
iButtonProtocolGroupMax
} iButtonProtocolGroup;
extern const iButtonProtocolGroupBase* ibutton_protocol_groups[];
| 244
|
C
|
.c
| 8
| 27.625
| 65
| 0.83691
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,192
|
protocol_group_base.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/protocol_group_base.h
|
#pragma once
#include <stdbool.h>
#include <flipper_format.h>
#include "protocol_common_i.h"
typedef void iButtonProtocolGroupData;
typedef int32_t iButtonProtocolGroupId;
typedef iButtonProtocolGroupData* (*iButtonProtocolGroupAllocFunc)(void);
typedef void (*iButtonProtocolGroupFreeFunc)(iButtonProtocolGroupData*);
typedef void (*iButtonProtocolGroupRenderFunc)(
iButtonProtocolGroupData*,
const iButtonProtocolData*,
iButtonProtocolLocalId,
FuriString*);
typedef bool (*iButtonProtocolGroupIsValidFunc)(
iButtonProtocolGroupData*,
const iButtonProtocolData*,
iButtonProtocolLocalId);
typedef void (*iButtonProtocolGroupGetDataFunc)(
iButtonProtocolGroupData*,
iButtonProtocolData*,
iButtonProtocolLocalId,
iButtonEditableData*);
typedef void (*iButtonProtocolGroupApplyFunc)(
iButtonProtocolGroupData*,
iButtonProtocolData*,
iButtonProtocolLocalId);
typedef size_t (*iButtonProtocolGropuGetSizeFunc)(iButtonProtocolGroupData*);
typedef uint32_t (
*iButtonProtocolGroupGetFeaturesFunc)(iButtonProtocolGroupData*, iButtonProtocolLocalId);
typedef const char* (
*iButtonProtocolGroupGetStringFunc)(iButtonProtocolGroupData*, iButtonProtocolLocalId);
typedef bool (*iButtonProtocolGroupGetIdFunc)(
iButtonProtocolGroupData*,
iButtonProtocolLocalId*,
const char*);
typedef bool (*iButtonProtocolGroupReadFunc)(
iButtonProtocolGroupData*,
iButtonProtocolData*,
iButtonProtocolLocalId*);
typedef bool (*iButtonProtocolGroupWriteFunc)(
iButtonProtocolGroupData*,
iButtonProtocolData*,
iButtonProtocolLocalId);
typedef bool (*iButtonProtocolGroupSaveFunc)(
iButtonProtocolGroupData*,
const iButtonProtocolData*,
iButtonProtocolLocalId,
FlipperFormat*);
typedef bool (*iButtonProtocolGroupLoadFunc)(
iButtonProtocolGroupData*,
iButtonProtocolData*,
iButtonProtocolLocalId,
uint32_t,
FlipperFormat*);
typedef struct {
const uint32_t protocol_count;
iButtonProtocolGroupAllocFunc alloc;
iButtonProtocolGroupFreeFunc free;
iButtonProtocolGropuGetSizeFunc get_max_data_size;
iButtonProtocolGroupGetIdFunc get_id_by_name;
iButtonProtocolGroupGetFeaturesFunc get_features;
iButtonProtocolGroupGetStringFunc get_manufacturer;
iButtonProtocolGroupGetStringFunc get_name;
iButtonProtocolGroupReadFunc read;
iButtonProtocolGroupWriteFunc write_id;
iButtonProtocolGroupWriteFunc write_copy;
iButtonProtocolGroupApplyFunc emulate_start;
iButtonProtocolGroupApplyFunc emulate_stop;
iButtonProtocolGroupSaveFunc save;
iButtonProtocolGroupLoadFunc load;
iButtonProtocolGroupRenderFunc render_uid;
iButtonProtocolGroupRenderFunc render_data;
iButtonProtocolGroupRenderFunc render_brief_data;
iButtonProtocolGroupRenderFunc render_error;
iButtonProtocolGroupIsValidFunc is_valid;
iButtonProtocolGroupGetDataFunc get_editable_data;
iButtonProtocolGroupApplyFunc apply_edits;
} iButtonProtocolGroupBase;
| 3,031
|
C
|
.c
| 78
| 34.641026
| 93
| 0.828093
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,193
|
rw1990.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/blanks/rw1990.c
|
#include "rw1990.h"
#include <core/kernel.h>
#define RW1990_1_CMD_WRITE_RECORD_FLAG 0xD1
#define RW1990_1_CMD_READ_RECORD_FLAG 0xB5
#define RW1990_1_CMD_WRITE_ROM 0xD5
#define RW1990_2_CMD_WRITE_RECORD_FLAG 0x1D
#define RW1990_2_CMD_READ_RECORD_FLAG 0x1E
#define RW1990_2_CMD_WRITE_ROM 0xD5
#define DS1990_CMD_READ_ROM 0x33
static void rw1990_write_byte(OneWireHost* host, uint8_t value) {
for(uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
onewire_host_write_bit(host, (bool)(bitMask & value));
furi_delay_us(5000);
}
}
static bool rw1990_read_and_compare(OneWireHost* host, const uint8_t* data, size_t data_size) {
bool success = false;
if(onewire_host_reset(host)) {
success = true;
onewire_host_write(host, DS1990_CMD_READ_ROM);
for(size_t i = 0; i < data_size; ++i) {
if(data[i] != onewire_host_read(host)) {
success = false;
break;
}
}
}
return success;
}
bool rw1990_write_v1(OneWireHost* host, const uint8_t* data, size_t data_size) {
// Unlock sequence
onewire_host_reset(host);
onewire_host_write(host, RW1990_1_CMD_WRITE_RECORD_FLAG);
furi_delay_us(10);
onewire_host_write_bit(host, false);
furi_delay_us(5000);
// Write data
onewire_host_reset(host);
onewire_host_write(host, RW1990_1_CMD_WRITE_ROM);
for(size_t i = 0; i < data_size; ++i) {
// inverted key for RW1990.1
rw1990_write_byte(host, ~(data[i]));
furi_delay_us(30000);
}
// Lock sequence
onewire_host_write(host, RW1990_1_CMD_WRITE_RECORD_FLAG);
onewire_host_write_bit(host, true);
furi_delay_us(10000);
// TODO FL-3528: Better error handling
return rw1990_read_and_compare(host, data, data_size);
}
bool rw1990_write_v2(OneWireHost* host, const uint8_t* data, size_t data_size) {
// Unlock sequence
onewire_host_reset(host);
onewire_host_write(host, RW1990_2_CMD_WRITE_RECORD_FLAG);
furi_delay_us(10);
onewire_host_write_bit(host, true);
furi_delay_us(5000);
// Write data
onewire_host_reset(host);
onewire_host_write(host, RW1990_2_CMD_WRITE_ROM);
for(size_t i = 0; i < data_size; ++i) {
rw1990_write_byte(host, data[i]);
furi_delay_us(30000);
}
// Lock sequence
onewire_host_write(host, RW1990_2_CMD_WRITE_RECORD_FLAG);
onewire_host_write_bit(host, false);
furi_delay_us(10000);
// TODO Fl-3528: Better error handling
return rw1990_read_and_compare(host, data, data_size);
}
| 2,598
|
C
|
.c
| 72
| 30.541667
| 95
| 0.652417
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,194
|
tm2004.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/blanks/tm2004.c
|
#include "tm2004.h"
#include <core/kernel.h>
#define TM2004_CMD_READ_STATUS 0xAA
#define TM2004_CMD_READ_MEMORY 0xF0
#define TM2004_CMD_WRITE_ROM 0x3C
#define TM2004_CMD_FINALIZATION 0x35
#define TM2004_ANSWER_READ_MEMORY 0xF5
bool tm2004_write(OneWireHost* host, const uint8_t* data, size_t data_size) {
onewire_host_reset(host);
onewire_host_write(host, TM2004_CMD_WRITE_ROM);
// Starting writing from address 0x0000
onewire_host_write(host, 0x00);
onewire_host_write(host, 0x00);
size_t i;
for(i = 0; i < data_size; ++i) {
uint8_t answer;
onewire_host_write(host, data[i]);
answer = onewire_host_read(host);
// TODO FL-3529: check answer CRC
// pulse indicating that data is correct
furi_delay_us(600);
onewire_host_write_bit(host, true);
furi_delay_us(50000);
// read written key byte
answer = onewire_host_read(host); //-V519
// check that written and read are same
if(data[i] != answer) {
break;
}
}
// TODO FL-3529: Better error handling
return i == data_size;
}
| 1,148
|
C
|
.c
| 33
| 28.787879
| 77
| 0.641953
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,195
|
protocol_dallas_base.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_dallas_base.h
|
#pragma once
#include "../protocol_common_i.h"
#include <one_wire/one_wire_host.h>
#include <one_wire/one_wire_slave.h>
#include <flipper_format/flipper_format.h>
typedef bool (*iButtonProtocolDallasReadWriteFunc)(OneWireHost*, iButtonProtocolData*);
typedef void (*iButtonProtocolDallasEmulateFunc)(OneWireSlave*, iButtonProtocolData*);
typedef bool (*iButtonProtocolDallasSaveFunc)(FlipperFormat*, const iButtonProtocolData*);
typedef bool (*iButtonProtocolDallasLoadFunc)(FlipperFormat*, uint32_t, iButtonProtocolData*);
typedef void (*iButtonProtocolDallasRenderDataFunc)(FuriString*, const iButtonProtocolData*);
typedef bool (*iButtonProtocolDallasIsValidFunc)(const iButtonProtocolData*);
typedef void (
*iButtonProtocolDallasGetEditableDataFunc)(iButtonEditableData*, iButtonProtocolData*);
typedef void (*iButtonProtocolDallasApplyEditsFunc)(iButtonProtocolData*);
typedef struct {
const uint8_t family_code;
const uint32_t features;
const size_t data_size;
const char* manufacturer;
const char* name;
iButtonProtocolDallasReadWriteFunc read;
iButtonProtocolDallasReadWriteFunc write_id;
iButtonProtocolDallasReadWriteFunc write_copy;
iButtonProtocolDallasEmulateFunc emulate;
iButtonProtocolDallasSaveFunc save;
iButtonProtocolDallasLoadFunc load;
iButtonProtocolDallasRenderDataFunc render_uid;
iButtonProtocolDallasRenderDataFunc render_data;
iButtonProtocolDallasRenderDataFunc render_brief_data;
iButtonProtocolDallasRenderDataFunc render_error;
iButtonProtocolDallasIsValidFunc is_valid;
iButtonProtocolDallasGetEditableDataFunc get_editable_data;
iButtonProtocolDallasApplyEditsFunc apply_edits;
} iButtonProtocolDallasBase;
| 1,723
|
C
|
.c
| 34
| 47.264706
| 94
| 0.836601
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,196
|
protocol_ds1992.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_ds1992.c
|
#include "protocol_ds1992.h"
#include <core/core_defines.h>
#include <toolbox/pretty_format.h>
#include "dallas_common.h"
#include "../blanks/tm2004.h"
#define DS1992_FAMILY_CODE 0x08U
#define DS1992_FAMILY_NAME "DS1992"
#define DS1992_SRAM_DATA_SIZE 128U
#define DS1992_SRAM_PAGE_SIZE 4U
#define DS1992_COPY_SCRATCH_TIMEOUT_US 100U
#define DS1992_DATA_BYTE_COUNT 4U
#define DS1992_SRAM_DATA_KEY "Sram Data"
#define DS1992_MEMORY_TYPE "SRAM"
typedef struct {
OneWireSlave* bus;
DallasCommonCommandState command_state;
} DS1992ProtocolState;
typedef struct {
DallasCommonRomData rom_data;
uint8_t sram_data[DS1992_SRAM_DATA_SIZE];
DS1992ProtocolState state;
} DS1992ProtocolData;
static bool dallas_ds1992_read(OneWireHost*, void*);
static bool dallas_ds1992_write_id(OneWireHost*, iButtonProtocolData*);
static bool dallas_ds1992_write_copy(OneWireHost*, iButtonProtocolData*);
static void dallas_ds1992_emulate(OneWireSlave*, iButtonProtocolData*);
static bool dallas_ds1992_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool dallas_ds1992_save(FlipperFormat*, const iButtonProtocolData*);
static void dallas_ds1992_render_uid(FuriString*, const iButtonProtocolData*);
static void dallas_ds1992_render_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1992_render_brief_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1992_render_error(FuriString*, const iButtonProtocolData*);
static bool dallas_ds1992_is_data_valid(const iButtonProtocolData*);
static void dallas_ds1992_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void dallas_ds1992_apply_edits(iButtonProtocolData*);
const iButtonProtocolDallasBase ibutton_protocol_ds1992 = {
.family_code = DS1992_FAMILY_CODE,
.features = iButtonProtocolFeatureExtData | iButtonProtocolFeatureWriteId |
iButtonProtocolFeatureWriteCopy,
.data_size = sizeof(DS1992ProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DS1992_FAMILY_NAME,
.read = dallas_ds1992_read,
.write_id = dallas_ds1992_write_id,
.write_copy = dallas_ds1992_write_copy,
.emulate = dallas_ds1992_emulate,
.save = dallas_ds1992_save,
.load = dallas_ds1992_load,
.render_uid = dallas_ds1992_render_uid,
.render_data = dallas_ds1992_render_data,
.render_brief_data = dallas_ds1992_render_brief_data,
.render_error = dallas_ds1992_render_error,
.is_valid = dallas_ds1992_is_data_valid,
.get_editable_data = dallas_ds1992_get_editable_data,
.apply_edits = dallas_ds1992_apply_edits,
};
bool dallas_ds1992_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data) &&
dallas_common_read_mem(host, 0, data->sram_data, DS1992_SRAM_DATA_SIZE);
}
bool dallas_ds1992_write_id(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
return tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
}
bool dallas_ds1992_write_copy(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
return dallas_common_write_mem(
host,
DS1992_COPY_SCRATCH_TIMEOUT_US,
DS1992_SRAM_PAGE_SIZE,
data->sram_data,
DS1992_SRAM_DATA_SIZE);
}
static bool dallas_ds1992_reset_callback(bool is_short, void* context) {
furi_assert(context);
DS1992ProtocolData* data = context;
if(!is_short) {
data->state.command_state = DallasCommonCommandStateIdle;
onewire_slave_set_overdrive(data->state.bus, is_short);
}
return !is_short;
}
static bool dallas_ds1992_command_callback(uint8_t command, void* context) {
furi_assert(context);
DS1992ProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_search_rom(bus, &data->rom_data);
} else if(data->state.command_state == DallasCommonCommandStateRomCmd) {
data->state.command_state = DallasCommonCommandStateMemCmd;
dallas_common_emulate_read_mem(bus, data->sram_data, DS1992_SRAM_DATA_SIZE);
return false;
} else {
return false;
}
case DALLAS_COMMON_CMD_READ_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_read_rom(bus, &data->rom_data);
} else {
return false;
}
case DALLAS_COMMON_CMD_SKIP_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return true;
} else {
return false;
}
default:
return false;
}
}
void dallas_ds1992_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, dallas_ds1992_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, dallas_ds1992_command_callback, protocol_data);
}
bool dallas_ds1992_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
bool success = false;
do {
if(format_version < 2) break;
if(!dallas_common_load_rom_data(ff, format_version, &data->rom_data)) break;
if(!flipper_format_read_hex(
ff, DS1992_SRAM_DATA_KEY, data->sram_data, DS1992_SRAM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
bool dallas_ds1992_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
bool success = false;
do {
if(!dallas_common_save_rom_data(ff, &data->rom_data)) break;
if(!flipper_format_write_hex(
ff, DS1992_SRAM_DATA_KEY, data->sram_data, DS1992_SRAM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
void dallas_ds1992_render_uid(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
dallas_common_render_uid(result, &data->rom_data);
}
void dallas_ds1992_render_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
furi_string_cat_printf(result, "\e#Memory Data\n--------------------\n");
pretty_format_bytes_hex_canonical(
result,
DS1992_DATA_BYTE_COUNT,
PRETTY_FORMAT_FONT_MONOSPACE,
data->sram_data,
DS1992_SRAM_DATA_SIZE);
}
void dallas_ds1992_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
dallas_common_render_brief_data(
result, &data->rom_data, data->sram_data, DS1992_SRAM_DATA_SIZE, DS1992_MEMORY_TYPE);
}
void dallas_ds1992_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
if(!dallas_common_is_valid_crc(&data->rom_data)) {
dallas_common_render_crc_error(result, &data->rom_data);
}
}
bool dallas_ds1992_is_data_valid(const iButtonProtocolData* protocol_data) {
const DS1992ProtocolData* data = protocol_data;
return dallas_common_is_valid_crc(&data->rom_data);
}
void dallas_ds1992_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void dallas_ds1992_apply_edits(iButtonProtocolData* protocol_data) {
DS1992ProtocolData* data = protocol_data;
dallas_common_apply_edits(&data->rom_data, DS1992_FAMILY_CODE);
}
| 8,330
|
C
|
.c
| 191
| 38.21466
| 100
| 0.721591
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,197
|
protocol_group_dallas_defs.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_group_dallas_defs.c
|
#include "protocol_group_dallas_defs.h"
#include "protocol_ds1990.h"
#include "protocol_ds1992.h"
#include "protocol_ds1996.h"
#include "protocol_ds1971.h"
#include "protocol_ds1420.h"
#include "protocol_ds_generic.h"
const iButtonProtocolDallasBase* ibutton_protocols_dallas[] = {
[iButtonProtocolDS1990] = &ibutton_protocol_ds1990,
[iButtonProtocolDS1992] = &ibutton_protocol_ds1992,
[iButtonProtocolDS1996] = &ibutton_protocol_ds1996,
[iButtonProtocolDS1971] = &ibutton_protocol_ds1971,
[iButtonProtocolDS1420] = &ibutton_protocol_ds1420,
/* Add new 1-Wire protocols here */
/* Default catch-all 1-Wire protocol */
[iButtonProtocolDSGeneric] = &ibutton_protocol_ds_generic,
};
| 715
|
C
|
.c
| 17
| 39
| 63
| 0.759712
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,198
|
protocol_ds_generic.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_ds_generic.c
|
#include "protocol_ds_generic.h"
#include <core/string.h>
#include <core/core_defines.h>
#include "dallas_common.h"
#include "../blanks/tm2004.h"
#define DALLAS_GENERIC_FAMILY_CODE 0x00U
#define DALLAS_GENERIC_FAMILY_NAME "(non-specific)"
typedef struct {
OneWireSlave* bus;
} DallasGenericProtocolState;
typedef struct {
DallasCommonRomData rom_data;
DallasGenericProtocolState state;
} DallasGenericProtocolData;
static bool ds_generic_read(OneWireHost*, iButtonProtocolData*);
static bool ds_generic_write_id(OneWireHost*, iButtonProtocolData*);
static void ds_generic_emulate(OneWireSlave*, iButtonProtocolData*);
static bool ds_generic_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool ds_generic_save(FlipperFormat*, const iButtonProtocolData*);
static void ds_generic_render_uid(FuriString*, const iButtonProtocolData*);
static void ds_generic_render_brief_data(FuriString*, const iButtonProtocolData*);
static void ds_generic_render_error(FuriString*, const iButtonProtocolData*);
static bool ds_generic_is_data_valid(const iButtonProtocolData*);
static void ds_generic_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void ds_generic_apply_edits(iButtonProtocolData*);
const iButtonProtocolDallasBase ibutton_protocol_ds_generic = {
.family_code = DALLAS_GENERIC_FAMILY_CODE,
.features = iButtonProtocolFeatureWriteId,
.data_size = sizeof(DallasGenericProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DALLAS_GENERIC_FAMILY_NAME,
.read = ds_generic_read,
.write_id = ds_generic_write_id,
.write_copy = NULL, /* No data to write a copy */
.emulate = ds_generic_emulate,
.save = ds_generic_save,
.load = ds_generic_load,
.render_data = NULL, /* No data to render */
.render_uid = ds_generic_render_uid,
.render_brief_data = ds_generic_render_brief_data,
.render_error = ds_generic_render_error,
.is_valid = ds_generic_is_data_valid,
.get_editable_data = ds_generic_get_editable_data,
.apply_edits = ds_generic_apply_edits,
};
bool ds_generic_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DallasGenericProtocolData* data = protocol_data;
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data);
}
bool ds_generic_write_id(OneWireHost* host, iButtonProtocolData* protocol_data) {
DallasGenericProtocolData* data = protocol_data;
return tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
}
static bool ds_generic_reset_callback(bool is_short, void* context) {
furi_assert(context);
DallasGenericProtocolData* data = context;
if(!is_short) {
onewire_slave_set_overdrive(data->state.bus, is_short);
}
return !is_short;
}
static bool ds_generic_command_callback(uint8_t command, void* context) {
furi_assert(context);
DallasGenericProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
dallas_common_emulate_search_rom(bus, &data->rom_data);
break;
case DALLAS_COMMON_CMD_READ_ROM:
dallas_common_emulate_read_rom(bus, &data->rom_data);
break;
default:
break;
}
// No support for multiple consecutive commands
return false;
}
void ds_generic_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DallasGenericProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, ds_generic_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, ds_generic_command_callback, protocol_data);
}
bool ds_generic_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DallasGenericProtocolData* data = protocol_data;
return dallas_common_save_rom_data(ff, &data->rom_data);
}
bool ds_generic_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DallasGenericProtocolData* data = protocol_data;
return dallas_common_load_rom_data(ff, format_version, &data->rom_data);
}
void ds_generic_render_uid(FuriString* result, const iButtonProtocolData* protocol_data) {
const DallasGenericProtocolData* data = protocol_data;
dallas_common_render_uid(result, &data->rom_data);
}
void ds_generic_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DallasGenericProtocolData* data = protocol_data;
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < sizeof(DallasCommonRomData); ++i) {
furi_string_cat_printf(result, "%02X ", data->rom_data.bytes[i]);
}
}
void ds_generic_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
UNUSED(result);
UNUSED(protocol_data);
}
bool ds_generic_is_data_valid(const iButtonProtocolData* protocol_data) {
UNUSED(protocol_data);
return true;
}
void ds_generic_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DallasGenericProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void ds_generic_apply_edits(iButtonProtocolData* protocol_data) {
UNUSED(protocol_data);
}
| 5,319
|
C
|
.c
| 124
| 39.072581
| 97
| 0.750629
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,199
|
protocol_ds1996.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_ds1996.c
|
#include "protocol_ds1996.h"
#include <core/core_defines.h>
#include <toolbox/pretty_format.h>
#include "dallas_common.h"
#include "../blanks/tm2004.h"
#define DS1996_FAMILY_CODE 0x0CU
#define DS1996_FAMILY_NAME "DS1996"
#define DS1996_SRAM_DATA_SIZE 8192U
#define DS1996_SRAM_PAGE_SIZE 32U
#define DS1996_COPY_SCRATCH_TIMEOUT_US 100U
#define DS1996_DATA_BYTE_COUNT 4U
#define DS1996_SRAM_DATA_KEY "Sram Data"
#define DS1996_MEMORY_TYPE "SRAM"
typedef struct {
OneWireSlave* bus;
DallasCommonCommandState command_state;
} DS1996ProtocolState;
typedef struct {
DallasCommonRomData rom_data;
uint8_t sram_data[DS1996_SRAM_DATA_SIZE];
DS1996ProtocolState state;
} DS1996ProtocolData;
static bool dallas_ds1996_read(OneWireHost*, void*);
static bool dallas_ds1996_write_id(OneWireHost*, iButtonProtocolData*);
static bool dallas_ds1996_write_copy(OneWireHost*, iButtonProtocolData*);
static void dallas_ds1996_emulate(OneWireSlave*, iButtonProtocolData*);
static bool dallas_ds1996_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool dallas_ds1996_save(FlipperFormat*, const iButtonProtocolData*);
static void dallas_ds1996_render_uid(FuriString*, const iButtonProtocolData*);
static void dallas_ds1996_render_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1996_render_brief_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1996_render_error(FuriString*, const iButtonProtocolData*);
static bool dallas_ds1996_is_data_valid(const iButtonProtocolData*);
static void dallas_ds1996_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void dallas_ds1996_apply_edits(iButtonProtocolData*);
const iButtonProtocolDallasBase ibutton_protocol_ds1996 = {
.family_code = DS1996_FAMILY_CODE,
.features = iButtonProtocolFeatureExtData | iButtonProtocolFeatureWriteId |
iButtonProtocolFeatureWriteCopy,
.data_size = sizeof(DS1996ProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DS1996_FAMILY_NAME,
.read = dallas_ds1996_read,
.write_id = dallas_ds1996_write_id,
.write_copy = dallas_ds1996_write_copy,
.emulate = dallas_ds1996_emulate,
.save = dallas_ds1996_save,
.load = dallas_ds1996_load,
.render_uid = dallas_ds1996_render_uid,
.render_data = dallas_ds1996_render_data,
.render_brief_data = dallas_ds1996_render_brief_data,
.render_error = dallas_ds1996_render_error,
.is_valid = dallas_ds1996_is_data_valid,
.get_editable_data = dallas_ds1996_get_editable_data,
.apply_edits = dallas_ds1996_apply_edits,
};
bool dallas_ds1996_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
bool success = false;
do {
if(!onewire_host_reset(host)) break;
if(!dallas_common_read_rom(host, &data->rom_data)) break;
if(!onewire_host_reset(host)) break;
onewire_host_write(host, DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM);
onewire_host_set_overdrive(host, true);
if(!dallas_common_read_mem(host, 0, data->sram_data, DS1996_SRAM_DATA_SIZE)) break;
success = true;
} while(false);
onewire_host_set_overdrive(host, false);
return success;
}
bool dallas_ds1996_write_id(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
return tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
}
bool dallas_ds1996_write_copy(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
bool success = false;
do {
if(!onewire_host_reset(host)) break;
onewire_host_write(host, DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM);
onewire_host_set_overdrive(host, true);
if(!dallas_common_write_mem(
host,
DS1996_COPY_SCRATCH_TIMEOUT_US,
DS1996_SRAM_PAGE_SIZE,
data->sram_data,
DS1996_SRAM_DATA_SIZE))
break;
success = true;
} while(false);
onewire_host_set_overdrive(host, false);
return success;
}
static bool dallas_ds1996_reset_callback(bool is_short, void* context) {
furi_assert(context);
DS1996ProtocolData* data = context;
data->state.command_state = DallasCommonCommandStateIdle;
onewire_slave_set_overdrive(data->state.bus, is_short);
return true;
}
static bool dallas_ds1996_command_callback(uint8_t command, void* context) {
furi_assert(context);
DS1996ProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_search_rom(bus, &data->rom_data);
} else if(data->state.command_state == DallasCommonCommandStateRomCmd) {
data->state.command_state = DallasCommonCommandStateMemCmd;
return dallas_common_emulate_read_mem(bus, data->sram_data, DS1996_SRAM_DATA_SIZE);
} else {
return false;
}
case DALLAS_COMMON_CMD_READ_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_read_rom(bus, &data->rom_data);
} else {
return false;
}
case DALLAS_COMMON_CMD_SKIP_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return true;
} else {
return false;
}
case DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
onewire_slave_set_overdrive(bus, true);
return true;
} else {
return false;
}
case DALLAS_COMMON_CMD_MATCH_ROM:
case DALLAS_COMMON_CMD_OVERDRIVE_MATCH_ROM:
/* TODO FL-3533: Match ROM command support */
default:
return false;
}
}
void dallas_ds1996_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, dallas_ds1996_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, dallas_ds1996_command_callback, protocol_data);
}
bool dallas_ds1996_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
bool success = false;
do {
if(format_version < 2) break;
if(!dallas_common_load_rom_data(ff, format_version, &data->rom_data)) break;
if(!flipper_format_read_hex(
ff, DS1996_SRAM_DATA_KEY, data->sram_data, DS1996_SRAM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
bool dallas_ds1996_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
bool success = false;
do {
if(!dallas_common_save_rom_data(ff, &data->rom_data)) break;
if(!flipper_format_write_hex(
ff, DS1996_SRAM_DATA_KEY, data->sram_data, DS1996_SRAM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
void dallas_ds1996_render_uid(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
dallas_common_render_uid(result, &data->rom_data);
}
void dallas_ds1996_render_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
furi_string_cat_printf(result, "\e#Memory Data\n--------------------\n");
pretty_format_bytes_hex_canonical(
result,
DS1996_DATA_BYTE_COUNT,
PRETTY_FORMAT_FONT_MONOSPACE,
data->sram_data,
DS1996_SRAM_DATA_SIZE);
}
void dallas_ds1996_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
dallas_common_render_brief_data(
result, &data->rom_data, data->sram_data, DS1996_SRAM_DATA_SIZE, DS1996_MEMORY_TYPE);
}
void dallas_ds1996_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
if(!dallas_common_is_valid_crc(&data->rom_data)) {
dallas_common_render_crc_error(result, &data->rom_data);
}
}
bool dallas_ds1996_is_data_valid(const iButtonProtocolData* protocol_data) {
const DS1996ProtocolData* data = protocol_data;
return dallas_common_is_valid_crc(&data->rom_data);
}
void dallas_ds1996_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void dallas_ds1996_apply_edits(iButtonProtocolData* protocol_data) {
DS1996ProtocolData* data = protocol_data;
dallas_common_apply_edits(&data->rom_data, DS1996_FAMILY_CODE);
}
| 9,446
|
C
|
.c
| 219
| 37.223744
| 100
| 0.710145
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,200
|
protocol_ds1990.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_ds1990.c
|
#include "protocol_ds1990.h"
#include <core/string.h>
#include <core/core_defines.h>
#include "dallas_common.h"
#include "../blanks/rw1990.h"
#include "../blanks/tm2004.h"
#define DS1990_FAMILY_CODE 0x01U
#define DS1990_FAMILY_NAME "DS1990"
#define DS1990_CMD_READ_ROM 0x0FU
typedef struct {
OneWireSlave* bus;
} DS1990ProtocolState;
typedef struct {
DallasCommonRomData rom_data;
DS1990ProtocolState state;
} DS1990ProtocolData;
static bool dallas_ds1990_read(OneWireHost*, iButtonProtocolData*);
static bool dallas_ds1990_write_id(OneWireHost*, iButtonProtocolData*);
static void dallas_ds1990_emulate(OneWireSlave*, iButtonProtocolData*);
static bool dallas_ds1990_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool dallas_ds1990_save(FlipperFormat*, const iButtonProtocolData*);
static void dallas_ds1990_render_uid(FuriString*, const iButtonProtocolData*);
static void dallas_ds1990_render_brief_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1990_render_error(FuriString*, const iButtonProtocolData*);
static bool dallas_ds1990_is_data_valid(const iButtonProtocolData*);
static void dallas_ds1990_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void dallas_ds1990_apply_edits(iButtonProtocolData*);
const iButtonProtocolDallasBase ibutton_protocol_ds1990 = {
.family_code = DS1990_FAMILY_CODE,
.features = iButtonProtocolFeatureWriteId,
.data_size = sizeof(DS1990ProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DS1990_FAMILY_NAME,
.read = dallas_ds1990_read,
.write_id = dallas_ds1990_write_id,
.write_copy = NULL, /* No data to write a copy */
.emulate = dallas_ds1990_emulate,
.save = dallas_ds1990_save,
.load = dallas_ds1990_load,
.render_uid = dallas_ds1990_render_uid,
.render_data = NULL, /* No data to render */
.render_brief_data = dallas_ds1990_render_brief_data,
.render_error = dallas_ds1990_render_error,
.is_valid = dallas_ds1990_is_data_valid,
.get_editable_data = dallas_ds1990_get_editable_data,
.apply_edits = dallas_ds1990_apply_edits,
};
bool dallas_ds1990_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1990ProtocolData* data = protocol_data;
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data);
}
bool dallas_ds1990_write_id(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1990ProtocolData* data = protocol_data;
return rw1990_write_v1(host, data->rom_data.bytes, sizeof(DallasCommonRomData)) ||
rw1990_write_v2(host, data->rom_data.bytes, sizeof(DallasCommonRomData)) ||
tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
}
static bool dallas_ds1990_reset_callback(bool is_short, void* context) {
DS1990ProtocolData* data = context;
if(!is_short) {
onewire_slave_set_overdrive(data->state.bus, is_short);
}
return !is_short;
}
static bool dallas_ds1990_command_callback(uint8_t command, void* context) {
furi_assert(context);
DS1990ProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
dallas_common_emulate_search_rom(bus, &data->rom_data);
break;
case DALLAS_COMMON_CMD_READ_ROM:
case DS1990_CMD_READ_ROM:
dallas_common_emulate_read_rom(bus, &data->rom_data);
break;
default:
break;
}
// No support for multiple consecutive commands
return false;
}
void dallas_ds1990_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DS1990ProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, dallas_ds1990_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, dallas_ds1990_command_callback, protocol_data);
}
bool dallas_ds1990_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DS1990ProtocolData* data = protocol_data;
return dallas_common_save_rom_data(ff, &data->rom_data);
}
bool dallas_ds1990_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DS1990ProtocolData* data = protocol_data;
return dallas_common_load_rom_data(ff, format_version, &data->rom_data);
}
void dallas_ds1990_render_uid(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1990ProtocolData* data = protocol_data;
dallas_common_render_uid(result, &data->rom_data);
}
void dallas_ds1990_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1990ProtocolData* data = protocol_data;
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < sizeof(DallasCommonRomData); ++i) {
furi_string_cat_printf(result, "%02X ", data->rom_data.bytes[i]);
}
furi_string_cat_printf(result, "\nFamily Code: %02X\n", data->rom_data.bytes[0]);
}
void dallas_ds1990_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1990ProtocolData* data = protocol_data;
if(!dallas_common_is_valid_crc(&data->rom_data)) {
dallas_common_render_crc_error(result, &data->rom_data);
}
}
bool dallas_ds1990_is_data_valid(const iButtonProtocolData* protocol_data) {
const DS1990ProtocolData* data = protocol_data;
return dallas_common_is_valid_crc(&data->rom_data);
}
void dallas_ds1990_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DS1990ProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void dallas_ds1990_apply_edits(iButtonProtocolData* protocol_data) {
DS1990ProtocolData* data = protocol_data;
dallas_common_apply_edits(&data->rom_data, DS1990_FAMILY_CODE);
}
| 5,898
|
C
|
.c
| 132
| 40.681818
| 100
| 0.74599
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,201
|
dallas_common.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/dallas_common.c
|
#include "dallas_common.h"
#include <core/common_defines.h>
#include <one_wire/maxim_crc.h>
#define BITS_IN_BYTE 8U
#define DALLAS_COMMON_ROM_DATA_KEY_V1 "Data"
#define DALLAS_COMMON_ROM_DATA_KEY_V2 "Rom Data"
#define DALLAS_COMMON_COPY_SCRATCH_MIN_TIMEOUT_US 5U
#define DALLAS_COMMON_COPY_SCRATCH_POLL_COUNT 20U
#define DALLAS_COMMON_END_ADDRESS_MASK 0x01F
#define DALLAS_COMMON_STATUS_FLAG_PF (1U << 5)
#define DALLAS_COMMON_STATUS_FLAG_OF (1U << 6)
#define DALLAS_COMMON_STATUS_FLAG_AA (1U << 7)
#define DALLAS_COMMON_BRIEF_HEAD_COUNT 4U
#define DALLAS_COMMON_BRIEF_TAIL_COUNT 3U
#define BITS_IN_BYTE 8U
#define BITS_IN_KBIT 1024U
#define BITS_IN_MBIT (BITS_IN_KBIT * 1024U)
bool dallas_common_skip_rom(OneWireHost* host) {
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
return true;
}
bool dallas_common_read_rom(OneWireHost* host, DallasCommonRomData* rom_data) {
onewire_host_write(host, DALLAS_COMMON_CMD_READ_ROM);
onewire_host_read_bytes(host, rom_data->bytes, sizeof(DallasCommonRomData));
return dallas_common_is_valid_crc(rom_data);
}
bool dallas_common_write_scratchpad(
OneWireHost* host,
uint16_t address,
const uint8_t* data,
size_t data_size) {
onewire_host_write(host, DALLAS_COMMON_CMD_WRITE_SCRATCH);
onewire_host_write(host, (uint8_t)address);
onewire_host_write(host, (uint8_t)(address >> BITS_IN_BYTE));
onewire_host_write_bytes(host, data, data_size);
return true;
}
bool dallas_common_read_scratchpad(
OneWireHost* host,
DallasCommonAddressRegs* regs,
uint8_t* data,
size_t data_size) {
onewire_host_write(host, DALLAS_COMMON_CMD_READ_SCRATCH);
onewire_host_read_bytes(host, regs->bytes, sizeof(DallasCommonAddressRegs));
onewire_host_read_bytes(host, data, data_size);
return true;
}
bool dallas_common_copy_scratchpad(
OneWireHost* host,
const DallasCommonAddressRegs* regs,
uint32_t timeout_us) {
onewire_host_write(host, DALLAS_COMMON_CMD_COPY_SCRATCH);
onewire_host_write_bytes(host, regs->bytes, sizeof(DallasCommonAddressRegs));
const uint32_t poll_delay =
MAX(timeout_us / DALLAS_COMMON_COPY_SCRATCH_POLL_COUNT,
DALLAS_COMMON_COPY_SCRATCH_MIN_TIMEOUT_US);
uint32_t time_elapsed;
for(time_elapsed = 0; time_elapsed < timeout_us; time_elapsed += poll_delay) {
if(!onewire_host_read_bit(host)) break;
furi_delay_us(poll_delay);
}
return time_elapsed < timeout_us;
}
bool dallas_common_read_mem(OneWireHost* host, uint16_t address, uint8_t* data, size_t data_size) {
onewire_host_write(host, DALLAS_COMMON_CMD_READ_MEM);
onewire_host_write(host, (uint8_t)address);
onewire_host_write(host, (uint8_t)(address >> BITS_IN_BYTE));
onewire_host_read_bytes(host, data, (uint16_t)data_size);
return true;
}
bool dallas_common_write_mem(
OneWireHost* host,
uint32_t timeout_us,
size_t page_size,
const uint8_t* data,
size_t data_size) {
// Data size must be a multiple of page size
furi_check(data_size % page_size == 0);
DallasCommonAddressRegs regs;
uint8_t* scratch = malloc(page_size);
size_t i;
for(i = 0; i < data_size; i += page_size) {
const uint8_t* data_ptr = data + i;
// Write scratchpad with the next page value
if(!onewire_host_reset(host)) break;
if(!dallas_common_skip_rom(host)) break;
if(!dallas_common_write_scratchpad(host, i, data_ptr, page_size)) break;
// Read back the scratchpad contents and address registers
if(!onewire_host_reset(host)) break;
if(!dallas_common_skip_rom(host)) break;
if(!dallas_common_read_scratchpad(host, ®s, scratch, page_size)) break;
// Verify scratchpad contents
if(memcmp(data_ptr, scratch, page_size) != 0) break;
// Write scratchpad to internal memory
if(!onewire_host_reset(host)) break;
if(!dallas_common_skip_rom(host)) break;
if(!dallas_common_copy_scratchpad(host, ®s, timeout_us)) break;
// Read back the address registers again
if(!onewire_host_reset(host)) break;
if(!dallas_common_skip_rom(host)) break;
if(!dallas_common_read_scratchpad(host, ®s, scratch, 0)) break;
// Check if AA flag is set
if(!(regs.fields.status & DALLAS_COMMON_STATUS_FLAG_AA)) break;
}
free(scratch);
return i == data_size;
}
bool dallas_common_emulate_search_rom(OneWireSlave* bus, const DallasCommonRomData* rom_data) {
for(size_t i = 0; i < sizeof(DallasCommonRomData); i++) {
for(size_t j = 0; j < BITS_IN_BYTE; j++) {
bool bit = (rom_data->bytes[i] >> j) & 0x01;
if(!onewire_slave_send_bit(bus, bit)) return false;
if(!onewire_slave_send_bit(bus, !bit)) return false;
onewire_slave_receive_bit(bus);
// TODO FL-3530: check for errors and return if any
}
}
return true;
}
bool dallas_common_emulate_read_rom(OneWireSlave* bus, const DallasCommonRomData* rom_data) {
return onewire_slave_send(bus, rom_data->bytes, sizeof(DallasCommonRomData));
}
bool dallas_common_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size) {
bool success = false;
union {
uint8_t bytes[sizeof(uint16_t)];
uint16_t word;
} address;
do {
if(!onewire_slave_receive(bus, address.bytes, sizeof(address))) break;
if(address.word >= data_size) break;
if(!onewire_slave_send(bus, data + address.word, data_size - address.word)) break;
success = true;
} while(false);
return success;
}
bool dallas_common_save_rom_data(FlipperFormat* ff, const DallasCommonRomData* rom_data) {
return flipper_format_write_hex(
ff, DALLAS_COMMON_ROM_DATA_KEY_V2, rom_data->bytes, sizeof(DallasCommonRomData));
}
bool dallas_common_load_rom_data(
FlipperFormat* ff,
uint32_t format_version,
DallasCommonRomData* rom_data) {
switch(format_version) {
case 1:
return flipper_format_read_hex(
ff, DALLAS_COMMON_ROM_DATA_KEY_V1, rom_data->bytes, sizeof(DallasCommonRomData));
case 2:
return flipper_format_read_hex(
ff, DALLAS_COMMON_ROM_DATA_KEY_V2, rom_data->bytes, sizeof(DallasCommonRomData));
default:
return false;
}
}
bool dallas_common_is_valid_crc(const DallasCommonRomData* rom_data) {
const uint8_t crc_calculated =
maxim_crc8(rom_data->bytes, sizeof(DallasCommonRomData) - 1, MAXIM_CRC8_INIT);
const uint8_t crc_received = rom_data->fields.checksum;
return crc_calculated == crc_received;
}
void dallas_common_render_uid(FuriString* result, const DallasCommonRomData* rom_data) {
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < sizeof(DallasCommonRomData); ++i) {
furi_string_cat_printf(result, "%02X ", rom_data->bytes[i]);
}
}
void dallas_common_render_brief_data(
FuriString* result,
const DallasCommonRomData* rom_data,
const uint8_t* mem_data,
size_t mem_size,
const char* mem_name) {
UNUSED(mem_data);
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < sizeof(rom_data->bytes); ++i) {
furi_string_cat_printf(result, "%02X ", rom_data->bytes[i]);
}
furi_string_cat_printf(result, "\nFamily Code: %02X\n", rom_data->bytes[0]);
const char* size_prefix = "";
size_t mem_size_bits = mem_size * BITS_IN_BYTE;
if(mem_size_bits >= BITS_IN_MBIT) {
size_prefix = "M";
mem_size_bits /= BITS_IN_MBIT;
} else if(mem_size_bits >= BITS_IN_KBIT) {
size_prefix = "K";
mem_size_bits /= BITS_IN_KBIT;
}
furi_string_cat_printf(result, "%s: %zu %sbit\n", mem_name, mem_size_bits, size_prefix);
}
void dallas_common_render_crc_error(FuriString* result, const DallasCommonRomData* rom_data) {
furi_string_set(result, "\e#CRC Error\e#\n");
const size_t data_size = sizeof(DallasCommonRomData);
for(size_t i = 0; i < data_size; ++i) {
furi_string_cat_printf(
result, (i < data_size - 1) ? "%02X " : "\e!%02X\e!", rom_data->bytes[i]);
}
furi_string_cat_printf(
result,
"\nExpected CRC: \e!%02X\e!",
maxim_crc8(rom_data->bytes, sizeof(DallasCommonRomData) - 1, MAXIM_CRC8_INIT));
}
void dallas_common_apply_edits(DallasCommonRomData* rom_data, uint8_t family_code) {
rom_data->fields.family_code = family_code;
const uint8_t crc =
maxim_crc8(rom_data->bytes, sizeof(DallasCommonRomData) - 1, MAXIM_CRC8_INIT);
rom_data->fields.checksum = crc;
}
| 8,669
|
C
|
.c
| 208
| 36.105769
| 99
| 0.675434
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,202
|
protocol_group_dallas.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_group_dallas.c
|
#include "protocol_group_dallas.h"
#include <furi_hal_resources.h>
#include "protocol_group_dallas_defs.h"
#define IBUTTON_ONEWIRE_ROM_SIZE 8U
typedef struct {
OneWireHost* host;
OneWireSlave* bus;
} iButtonProtocolGroupDallas;
static iButtonProtocolGroupDallas* ibutton_protocol_group_dallas_alloc(void) {
iButtonProtocolGroupDallas* group = malloc(sizeof(iButtonProtocolGroupDallas));
group->host = onewire_host_alloc(&gpio_ibutton);
group->bus = onewire_slave_alloc(&gpio_ibutton);
return group;
}
static void ibutton_protocol_group_dallas_free(iButtonProtocolGroupDallas* group) {
onewire_slave_free(group->bus);
onewire_host_free(group->host);
free(group);
}
static size_t ibutton_protocol_group_dallas_get_max_data_size(iButtonProtocolGroupDallas* group) {
UNUSED(group);
size_t max_data_size = 0;
for(iButtonProtocolLocalId i = 0; i < iButtonProtocolDSMax; ++i) {
const size_t current_rom_size = ibutton_protocols_dallas[i]->data_size;
if(current_rom_size > max_data_size) {
max_data_size = current_rom_size;
}
}
return max_data_size;
}
static bool ibutton_protocol_group_dallas_get_id_by_name(
iButtonProtocolGroupDallas* group,
iButtonProtocolLocalId* id,
const char* name) {
UNUSED(group);
// Handle older key files which refer to DS1990 as just "Dallas"
if(strcmp(name, "Dallas") == 0) {
*id = iButtonProtocolDS1990;
return true;
}
// Handle files that refer to Dallas "Raw Data" as DSGeneric
if(strcmp(name, "DSGeneric") == 0) {
*id = iButtonProtocolDSGeneric;
return true;
}
for(iButtonProtocolLocalId i = 0; i < iButtonProtocolDSMax; ++i) {
if(strcmp(ibutton_protocols_dallas[i]->name, name) == 0) {
*id = i;
return true;
}
}
return false;
}
static uint32_t ibutton_protocol_group_dallas_get_features(
iButtonProtocolGroupDallas* group,
iButtonProtocolLocalId id) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
return ibutton_protocols_dallas[id]->features;
}
static const char* ibutton_protocol_group_dallas_get_manufacturer(
iButtonProtocolGroupDallas* group,
iButtonProtocolLocalId id) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
return ibutton_protocols_dallas[id]->manufacturer;
}
static const char* ibutton_protocol_group_dallas_get_name(
iButtonProtocolGroupDallas* group,
iButtonProtocolLocalId id) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
return ibutton_protocols_dallas[id]->name;
}
static iButtonProtocolLocalId
ibutton_protocol_group_dallas_get_id_by_family_code(uint8_t family_code) {
iButtonProtocolLocalId id;
for(id = 0; id < iButtonProtocolDSGeneric; ++id) {
if(ibutton_protocols_dallas[id]->family_code == family_code) break;
}
return id;
}
static bool ibutton_protocol_group_dallas_read(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId* id) {
bool success = false;
uint8_t rom_data[IBUTTON_ONEWIRE_ROM_SIZE];
OneWireHost* host = group->host;
onewire_host_start(host);
furi_delay_ms(100);
FURI_CRITICAL_ENTER();
if(onewire_host_search(host, rom_data, OneWireHostSearchModeNormal)) {
/* Considering any found 1-Wire device a success.
* It can be checked later with ibutton_key_is_valid(). */
success = true;
/* If a 1-Wire device was found, id is guaranteed to be
* one of the known keys or DSGeneric. */
*id = ibutton_protocol_group_dallas_get_id_by_family_code(rom_data[0]);
ibutton_protocols_dallas[*id]->read(host, data);
}
onewire_host_reset_search(host);
onewire_host_stop(host);
FURI_CRITICAL_EXIT();
return success;
}
static bool ibutton_protocol_group_dallas_write_id(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
furi_assert(id < iButtonProtocolDSMax);
const iButtonProtocolDallasBase* protocol = ibutton_protocols_dallas[id];
furi_assert(protocol->features & iButtonProtocolFeatureWriteId);
OneWireHost* host = group->host;
onewire_host_start(host);
furi_delay_ms(100);
FURI_CRITICAL_ENTER();
const bool success = protocol->write_id(host, data);
onewire_host_stop(host);
FURI_CRITICAL_EXIT();
return success;
}
static bool ibutton_protocol_group_dallas_write_copy(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
furi_assert(id < iButtonProtocolDSMax);
const iButtonProtocolDallasBase* protocol = ibutton_protocols_dallas[id];
furi_assert(protocol->features & iButtonProtocolFeatureWriteCopy);
OneWireHost* host = group->host;
onewire_host_start(host);
furi_delay_ms(100);
FURI_CRITICAL_ENTER();
const bool success = protocol->write_copy(host, data);
onewire_host_stop(host);
FURI_CRITICAL_EXIT();
return success;
}
static void ibutton_protocol_group_dallas_emulate_start(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
furi_assert(id < iButtonProtocolDSMax);
OneWireSlave* bus = group->bus;
ibutton_protocols_dallas[id]->emulate(bus, data);
onewire_slave_start(bus);
}
static void ibutton_protocol_group_dallas_emulate_stop(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
furi_assert(id < iButtonProtocolDSMax);
UNUSED(data);
onewire_slave_stop(group->bus);
}
static bool ibutton_protocol_group_dallas_save(
iButtonProtocolGroupDallas* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FlipperFormat* ff) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
return ibutton_protocols_dallas[id]->save(ff, data);
}
static bool ibutton_protocol_group_dallas_load(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id,
uint32_t version,
FlipperFormat* ff) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
return ibutton_protocols_dallas[id]->load(ff, version, data);
}
static void ibutton_protocol_group_dallas_render_uid(
iButtonProtocolGroupDallas* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
const iButtonProtocolDallasBase* protocol = ibutton_protocols_dallas[id];
furi_assert(protocol->render_uid);
protocol->render_uid(result, data);
}
static void ibutton_protocol_group_dallas_render_data(
iButtonProtocolGroupDallas* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
const iButtonProtocolDallasBase* protocol = ibutton_protocols_dallas[id];
furi_assert(protocol->render_data);
protocol->render_data(result, data);
}
static void ibutton_protocol_group_dallas_render_brief_data(
iButtonProtocolGroupDallas* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
ibutton_protocols_dallas[id]->render_brief_data(result, data);
}
static void ibutton_protocol_group_dallas_render_error(
iButtonProtocolGroupDallas* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
ibutton_protocols_dallas[id]->render_error(result, data);
}
static bool ibutton_protocol_group_dallas_is_valid(
iButtonProtocolGroupDallas* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
return ibutton_protocols_dallas[id]->is_valid(data);
}
static void ibutton_protocol_group_dallas_get_editable_data(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id,
iButtonEditableData* editable) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
ibutton_protocols_dallas[id]->get_editable_data(editable, data);
}
static void ibutton_protocol_group_dallas_apply_edits(
iButtonProtocolGroupDallas* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
UNUSED(group);
furi_assert(id < iButtonProtocolDSMax);
ibutton_protocols_dallas[id]->apply_edits(data);
}
const iButtonProtocolGroupBase ibutton_protocol_group_dallas = {
.protocol_count = iButtonProtocolDSMax,
.alloc = (iButtonProtocolGroupAllocFunc)ibutton_protocol_group_dallas_alloc,
.free = (iButtonProtocolGroupFreeFunc)ibutton_protocol_group_dallas_free,
.get_max_data_size =
(iButtonProtocolGropuGetSizeFunc)ibutton_protocol_group_dallas_get_max_data_size,
.get_id_by_name = (iButtonProtocolGroupGetIdFunc)ibutton_protocol_group_dallas_get_id_by_name,
.get_features =
(iButtonProtocolGroupGetFeaturesFunc)ibutton_protocol_group_dallas_get_features,
.get_manufacturer =
(iButtonProtocolGroupGetStringFunc)ibutton_protocol_group_dallas_get_manufacturer,
.get_name = (iButtonProtocolGroupGetStringFunc)ibutton_protocol_group_dallas_get_name,
.read = (iButtonProtocolGroupReadFunc)ibutton_protocol_group_dallas_read,
.write_id = (iButtonProtocolGroupWriteFunc)ibutton_protocol_group_dallas_write_id,
.write_copy = (iButtonProtocolGroupWriteFunc)ibutton_protocol_group_dallas_write_copy,
.emulate_start = (iButtonProtocolGroupApplyFunc)ibutton_protocol_group_dallas_emulate_start,
.emulate_stop = (iButtonProtocolGroupApplyFunc)ibutton_protocol_group_dallas_emulate_stop,
.save = (iButtonProtocolGroupSaveFunc)ibutton_protocol_group_dallas_save,
.load = (iButtonProtocolGroupLoadFunc)ibutton_protocol_group_dallas_load,
.render_uid = (iButtonProtocolGroupRenderFunc)ibutton_protocol_group_dallas_render_uid,
.render_data = (iButtonProtocolGroupRenderFunc)ibutton_protocol_group_dallas_render_data,
.render_brief_data =
(iButtonProtocolGroupRenderFunc)ibutton_protocol_group_dallas_render_brief_data,
.render_error = (iButtonProtocolGroupRenderFunc)ibutton_protocol_group_dallas_render_error,
.is_valid = (iButtonProtocolGroupIsValidFunc)ibutton_protocol_group_dallas_is_valid,
.get_editable_data =
(iButtonProtocolGroupGetDataFunc)ibutton_protocol_group_dallas_get_editable_data,
.apply_edits = (iButtonProtocolGroupApplyFunc)ibutton_protocol_group_dallas_apply_edits,
};
| 10,784
|
C
|
.c
| 268
| 35.36194
| 98
| 0.74615
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,203
|
protocol_ds1420.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_ds1420.c
|
#include "protocol_ds1420.h"
#include <core/string.h>
#include <core/core_defines.h>
#include "dallas_common.h"
#include "../blanks/rw1990.h"
#include "../blanks/tm2004.h"
#define DS1420_FAMILY_CODE 0x81U
#define DS1420_FAMILY_NAME "DS1420"
#define DS1420_CMD_READ_ROM 0x0FU
typedef struct {
OneWireSlave* bus;
} DS1420ProtocolState;
typedef struct {
DallasCommonRomData rom_data;
DS1420ProtocolState state;
} DS1420ProtocolData;
static bool dallas_ds1420_read(OneWireHost*, iButtonProtocolData*);
static bool dallas_ds1420_write_id(OneWireHost*, iButtonProtocolData*);
static void dallas_ds1420_emulate(OneWireSlave*, iButtonProtocolData*);
static bool dallas_ds1420_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool dallas_ds1420_save(FlipperFormat*, const iButtonProtocolData*);
static void dallas_ds1420_render_uid(FuriString*, const iButtonProtocolData*);
static void dallas_ds1420_render_brief_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1420_render_error(FuriString*, const iButtonProtocolData*);
static bool dallas_ds1420_is_data_valid(const iButtonProtocolData*);
static void dallas_ds1420_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void dallas_ds1420_apply_edits(iButtonProtocolData*);
const iButtonProtocolDallasBase ibutton_protocol_ds1420 = {
.family_code = DS1420_FAMILY_CODE,
.features = iButtonProtocolFeatureWriteId,
.data_size = sizeof(DS1420ProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DS1420_FAMILY_NAME,
.read = dallas_ds1420_read,
.write_id = dallas_ds1420_write_id,
.write_copy = NULL, /* No data to write a copy */
.emulate = dallas_ds1420_emulate,
.save = dallas_ds1420_save,
.load = dallas_ds1420_load,
.render_uid = dallas_ds1420_render_uid,
.render_data = NULL, /* No data to render */
.render_brief_data = dallas_ds1420_render_brief_data,
.render_error = dallas_ds1420_render_error,
.is_valid = dallas_ds1420_is_data_valid,
.get_editable_data = dallas_ds1420_get_editable_data,
.apply_edits = dallas_ds1420_apply_edits,
};
bool dallas_ds1420_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1420ProtocolData* data = protocol_data;
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data);
}
bool dallas_ds1420_write_id(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1420ProtocolData* data = protocol_data;
return rw1990_write_v1(host, data->rom_data.bytes, sizeof(DallasCommonRomData)) ||
rw1990_write_v2(host, data->rom_data.bytes, sizeof(DallasCommonRomData)) ||
tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
}
static bool dallas_ds1420_reset_callback(bool is_short, void* context) {
DS1420ProtocolData* data = context;
if(!is_short) {
onewire_slave_set_overdrive(data->state.bus, is_short);
}
return !is_short;
}
static bool dallas_ds1420_command_callback(uint8_t command, void* context) {
furi_assert(context);
DS1420ProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
dallas_common_emulate_search_rom(bus, &data->rom_data);
break;
case DALLAS_COMMON_CMD_READ_ROM:
case DS1420_CMD_READ_ROM:
dallas_common_emulate_read_rom(bus, &data->rom_data);
break;
default:
break;
}
// No support for multiple consecutive commands
return false;
}
void dallas_ds1420_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DS1420ProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, dallas_ds1420_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, dallas_ds1420_command_callback, protocol_data);
}
bool dallas_ds1420_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DS1420ProtocolData* data = protocol_data;
return dallas_common_save_rom_data(ff, &data->rom_data);
}
bool dallas_ds1420_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DS1420ProtocolData* data = protocol_data;
return dallas_common_load_rom_data(ff, format_version, &data->rom_data);
}
void dallas_ds1420_render_uid(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1420ProtocolData* data = protocol_data;
dallas_common_render_uid(result, &data->rom_data);
}
void dallas_ds1420_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1420ProtocolData* data = protocol_data;
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < sizeof(DallasCommonRomData); ++i) {
furi_string_cat_printf(result, "%02X ", data->rom_data.bytes[i]);
}
furi_string_cat_printf(result, "\nFamily Code: %02X\n", data->rom_data.bytes[0]);
}
void dallas_ds1420_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1420ProtocolData* data = protocol_data;
if(!dallas_common_is_valid_crc(&data->rom_data)) {
dallas_common_render_crc_error(result, &data->rom_data);
}
}
bool dallas_ds1420_is_data_valid(const iButtonProtocolData* protocol_data) {
const DS1420ProtocolData* data = protocol_data;
return dallas_common_is_valid_crc(&data->rom_data);
}
void dallas_ds1420_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DS1420ProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void dallas_ds1420_apply_edits(iButtonProtocolData* protocol_data) {
DS1420ProtocolData* data = protocol_data;
dallas_common_apply_edits(&data->rom_data, DS1420_FAMILY_CODE);
}
| 5,898
|
C
|
.c
| 132
| 40.681818
| 100
| 0.74599
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,204
|
protocol_group_dallas_defs.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_group_dallas_defs.h
|
#pragma once
#include "protocol_dallas_base.h"
typedef enum {
iButtonProtocolDS1990,
iButtonProtocolDS1992,
iButtonProtocolDS1996,
iButtonProtocolDS1971,
iButtonProtocolDS1420,
/* Add new 1-Wire protocols here */
/* Default catch-all 1-Wire protocol */
iButtonProtocolDSGeneric,
iButtonProtocolDSMax,
} iButtonProtocolDallas;
extern const iButtonProtocolDallasBase* ibutton_protocols_dallas[];
| 434
|
C
|
.c
| 14
| 27.142857
| 67
| 0.786058
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,205
|
protocol_ds1971.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/protocol_ds1971.c
|
#include "protocol_ds1971.h"
#include <core/core_defines.h>
#include <toolbox/pretty_format.h>
#include "dallas_common.h"
#include "../blanks/tm2004.h"
#define DS1971_FAMILY_CODE 0x14U
#define DS1971_FAMILY_NAME "DS1971"
#define DS1971_EEPROM_DATA_SIZE 32U
#define DS1971_SRAM_PAGE_SIZE 32U
#define DS1971_COPY_SCRATCH_DELAY_US 250U
#define DS1971_DATA_BYTE_COUNT 4U
#define DS1971_EEPROM_DATA_KEY "Eeprom Data"
#define DS1971_MEMORY_TYPE "EEPROM"
#define DS1971_CMD_FINALIZATION 0xA5
typedef struct {
OneWireSlave* bus;
DallasCommonCommandState command_state;
} DS1971ProtocolState;
typedef struct {
DallasCommonRomData rom_data;
uint8_t eeprom_data[DS1971_EEPROM_DATA_SIZE];
DS1971ProtocolState state;
} DS1971ProtocolData;
static bool dallas_ds1971_read(OneWireHost*, void*);
static bool dallas_ds1971_write_id(OneWireHost*, iButtonProtocolData*);
static bool dallas_ds1971_write_copy(OneWireHost*, iButtonProtocolData*);
static void dallas_ds1971_emulate(OneWireSlave*, iButtonProtocolData*);
static bool dallas_ds1971_load(FlipperFormat*, uint32_t, iButtonProtocolData*);
static bool dallas_ds1971_save(FlipperFormat*, const iButtonProtocolData*);
static void dallas_ds1971_render_uid(FuriString*, const iButtonProtocolData*);
static void dallas_ds1971_render_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1971_render_brief_data(FuriString*, const iButtonProtocolData*);
static void dallas_ds1971_render_error(FuriString*, const iButtonProtocolData*);
static bool dallas_ds1971_is_data_valid(const iButtonProtocolData*);
static void dallas_ds1971_get_editable_data(iButtonEditableData*, iButtonProtocolData*);
static void dallas_ds1971_apply_edits(iButtonProtocolData*);
static bool
dallas_ds1971_read_mem(OneWireHost* host, uint8_t address, uint8_t* data, size_t data_size);
static bool ds1971_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size);
const iButtonProtocolDallasBase ibutton_protocol_ds1971 = {
.family_code = DS1971_FAMILY_CODE,
.features = iButtonProtocolFeatureExtData | iButtonProtocolFeatureWriteId |
iButtonProtocolFeatureWriteCopy,
.data_size = sizeof(DS1971ProtocolData),
.manufacturer = DALLAS_COMMON_MANUFACTURER_NAME,
.name = DS1971_FAMILY_NAME,
.read = dallas_ds1971_read,
.write_id = dallas_ds1971_write_id,
.write_copy = dallas_ds1971_write_copy,
.emulate = dallas_ds1971_emulate,
.save = dallas_ds1971_save,
.load = dallas_ds1971_load,
.render_uid = dallas_ds1971_render_uid,
.render_data = dallas_ds1971_render_data,
.render_brief_data = dallas_ds1971_render_brief_data,
.render_error = dallas_ds1971_render_error,
.is_valid = dallas_ds1971_is_data_valid,
.get_editable_data = dallas_ds1971_get_editable_data,
.apply_edits = dallas_ds1971_apply_edits,
};
bool dallas_ds1971_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data) &&
dallas_ds1971_read_mem(host, 0, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
}
bool dallas_ds1971_write_id(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
return tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
}
bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
onewire_host_reset(host);
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
// Starting writing from address 0x0000
onewire_host_write(host, DALLAS_COMMON_CMD_WRITE_SCRATCH);
onewire_host_write(host, 0x00);
// Write data to scratchpad
onewire_host_write_bytes(host, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
// Read data from scratchpad and verify
bool pad_valid = false;
if(onewire_host_reset(host)) {
pad_valid = true;
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
onewire_host_write(host, DALLAS_COMMON_CMD_READ_SCRATCH);
onewire_host_write(host, 0x00);
for(size_t i = 0; i < DS1971_EEPROM_DATA_SIZE; ++i) {
uint8_t scratch = onewire_host_read(host);
if(data->eeprom_data[i] != scratch) {
pad_valid = false;
break;
}
}
}
// Copy scratchpad to memory and confirm
if(pad_valid) {
if(onewire_host_reset(host)) {
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
onewire_host_write(host, DALLAS_COMMON_CMD_COPY_SCRATCH);
onewire_host_write(host, DS1971_CMD_FINALIZATION);
furi_delay_us(DS1971_COPY_SCRATCH_DELAY_US);
}
}
return pad_valid;
}
static bool dallas_ds1971_reset_callback(bool is_short, void* context) {
furi_assert(context);
DS1971ProtocolData* data = context;
if(!is_short) {
data->state.command_state = DallasCommonCommandStateIdle;
onewire_slave_set_overdrive(data->state.bus, is_short);
}
return !is_short;
}
static bool dallas_ds1971_command_callback(uint8_t command, void* context) {
furi_assert(context);
DS1971ProtocolData* data = context;
OneWireSlave* bus = data->state.bus;
switch(command) {
case DALLAS_COMMON_CMD_SEARCH_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_search_rom(bus, &data->rom_data);
} else if(data->state.command_state == DallasCommonCommandStateRomCmd) {
data->state.command_state = DallasCommonCommandStateMemCmd;
ds1971_emulate_read_mem(bus, data->eeprom_data, DS1971_EEPROM_DATA_SIZE);
return false;
} else {
return false;
}
case DALLAS_COMMON_CMD_READ_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return dallas_common_emulate_read_rom(bus, &data->rom_data);
} else {
return false;
}
case DALLAS_COMMON_CMD_SKIP_ROM:
if(data->state.command_state == DallasCommonCommandStateIdle) {
data->state.command_state = DallasCommonCommandStateRomCmd;
return true;
} else {
return false;
}
default:
return false;
}
}
void dallas_ds1971_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
data->state.bus = bus;
onewire_slave_set_reset_callback(bus, dallas_ds1971_reset_callback, protocol_data);
onewire_slave_set_command_callback(bus, dallas_ds1971_command_callback, protocol_data);
}
bool dallas_ds1971_load(
FlipperFormat* ff,
uint32_t format_version,
iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
bool success = false;
do {
if(format_version < 2) break;
if(!dallas_common_load_rom_data(ff, format_version, &data->rom_data)) break;
if(!flipper_format_read_hex(
ff, DS1971_EEPROM_DATA_KEY, data->eeprom_data, DS1971_EEPROM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
bool dallas_ds1971_save(FlipperFormat* ff, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
bool success = false;
do {
if(!dallas_common_save_rom_data(ff, &data->rom_data)) break;
if(!flipper_format_write_hex(
ff, DS1971_EEPROM_DATA_KEY, data->eeprom_data, DS1971_EEPROM_DATA_SIZE))
break;
success = true;
} while(false);
return success;
}
void dallas_ds1971_render_uid(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
dallas_common_render_uid(result, &data->rom_data);
}
void dallas_ds1971_render_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
furi_string_cat_printf(result, "\e#Memory Data\n--------------------\n");
pretty_format_bytes_hex_canonical(
result,
DS1971_DATA_BYTE_COUNT,
PRETTY_FORMAT_FONT_MONOSPACE,
data->eeprom_data,
DS1971_EEPROM_DATA_SIZE);
}
void dallas_ds1971_render_brief_data(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
dallas_common_render_brief_data(
result, &data->rom_data, data->eeprom_data, DS1971_EEPROM_DATA_SIZE, DS1971_MEMORY_TYPE);
}
void dallas_ds1971_render_error(FuriString* result, const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
if(!dallas_common_is_valid_crc(&data->rom_data)) {
dallas_common_render_crc_error(result, &data->rom_data);
}
}
bool dallas_ds1971_is_data_valid(const iButtonProtocolData* protocol_data) {
const DS1971ProtocolData* data = protocol_data;
return dallas_common_is_valid_crc(&data->rom_data);
}
void dallas_ds1971_get_editable_data(
iButtonEditableData* editable_data,
iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
editable_data->ptr = data->rom_data.bytes;
editable_data->size = sizeof(DallasCommonRomData);
}
void dallas_ds1971_apply_edits(iButtonProtocolData* protocol_data) {
DS1971ProtocolData* data = protocol_data;
dallas_common_apply_edits(&data->rom_data, DS1971_FAMILY_CODE);
}
bool dallas_ds1971_read_mem(OneWireHost* host, uint8_t address, uint8_t* data, size_t data_size) {
onewire_host_write(host, DALLAS_COMMON_CMD_READ_MEM);
onewire_host_write(host, address);
onewire_host_read_bytes(host, data, (uint8_t)data_size);
return true;
}
bool ds1971_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size) {
bool success = false;
do {
uint8_t address;
if(!onewire_slave_receive(bus, &address, sizeof(address))) break;
if(address >= data_size) break;
if(!onewire_slave_send(bus, data + address, data_size - address)) break;
success = true;
} while(false);
return success;
}
| 10,417
|
C
|
.c
| 238
| 38.037815
| 100
| 0.710433
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,206
|
dallas_common.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/dallas/dallas_common.h
|
#pragma once
#include <one_wire/one_wire_host.h>
#include <one_wire/one_wire_slave.h>
#include <flipper_format/flipper_format.h>
#define DALLAS_COMMON_MANUFACTURER_NAME "Dallas"
#define DALLAS_COMMON_CMD_READ_ROM 0x33U
#define DALLAS_COMMON_CMD_MATCH_ROM 0x55U
#define DALLAS_COMMON_CMD_SKIP_ROM 0xCCU
#define DALLAS_COMMON_CMD_COND_SEARCH 0xECU
#define DALLAS_COMMON_CMD_SEARCH_ROM 0xF0U
#define DALLAS_COMMON_CMD_READ_SCRATCH 0xAAU
#define DALLAS_COMMON_CMD_WRITE_SCRATCH 0x0FU
#define DALLAS_COMMON_CMD_COPY_SCRATCH 0x55U
#define DALLAS_COMMON_CMD_READ_MEM 0xF0U
#define DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM 0x3CU
#define DALLAS_COMMON_CMD_OVERDRIVE_MATCH_ROM 0x69U
typedef enum {
DallasCommonCommandStateIdle,
DallasCommonCommandStateRomCmd,
DallasCommonCommandStateMemCmd,
} DallasCommonCommandState;
typedef union {
struct {
uint8_t family_code;
uint8_t serial_number[6];
uint8_t checksum;
} fields;
uint8_t bytes[8];
} DallasCommonRomData;
typedef union {
struct {
uint8_t address_lo;
uint8_t address_hi;
uint8_t status;
} fields;
uint8_t bytes[3];
} DallasCommonAddressRegs;
/* Standard(ish) iButton commands */
bool dallas_common_skip_rom(OneWireHost* host);
bool dallas_common_read_rom(OneWireHost* host, DallasCommonRomData* rom_data);
bool dallas_common_write_scratchpad(
OneWireHost* host,
uint16_t address,
const uint8_t* data,
size_t data_size);
bool dallas_common_read_scratchpad(
OneWireHost* host,
DallasCommonAddressRegs* regs,
uint8_t* data,
size_t data_size);
bool dallas_common_copy_scratchpad(
OneWireHost* host,
const DallasCommonAddressRegs* regs,
uint32_t timeout_us);
bool dallas_common_read_mem(OneWireHost* host, uint16_t address, uint8_t* data, size_t data_size);
/* Combined operations */
bool dallas_common_write_mem(
OneWireHost* host,
uint32_t timeout_us,
size_t page_size,
const uint8_t* data,
size_t data_size);
/* Emulation */
bool dallas_common_emulate_search_rom(OneWireSlave* bus, const DallasCommonRomData* rom_data);
bool dallas_common_emulate_read_rom(OneWireSlave* bus, const DallasCommonRomData* rom_data);
bool dallas_common_emulate_read_mem(OneWireSlave* bus, const uint8_t* data, size_t data_size);
/* Save & Load */
bool dallas_common_save_rom_data(FlipperFormat* ff, const DallasCommonRomData* rom_data);
bool dallas_common_load_rom_data(
FlipperFormat* ff,
uint32_t format_version,
DallasCommonRomData* rom_data);
/* Miscellaneous */
bool dallas_common_is_valid_crc(const DallasCommonRomData* rom_data);
void dallas_common_render_uid(FuriString* result, const DallasCommonRomData* rom_data);
void dallas_common_render_brief_data(
FuriString* result,
const DallasCommonRomData* rom_data,
const uint8_t* mem_data,
size_t mem_size,
const char* mem_name);
void dallas_common_render_crc_error(FuriString* result, const DallasCommonRomData* rom_data);
void dallas_common_apply_edits(DallasCommonRomData* rom_data, uint8_t family_code);
| 3,100
|
C
|
.c
| 83
| 33.855422
| 98
| 0.76087
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,207
|
protocol_group_misc_defs.h
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/misc/protocol_group_misc_defs.h
|
#pragma once
#include <toolbox/protocols/protocol.h>
typedef enum {
iButtonProtocolMiscCyfral,
iButtonProtocolMiscMetakom,
iButtonProtocolMiscMax,
} iButtonProtocolMisc;
extern const ProtocolBase* ibutton_protocols_misc[];
| 238
|
C
|
.c
| 8
| 26.875
| 52
| 0.828194
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,208
|
protocol_group_misc.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/misc/protocol_group_misc.c
|
#include "protocol_group_misc.h"
#include <furi_hal_rfid.h>
#include <furi_hal_ibutton.h>
#include <toolbox/protocols/protocol_dict.h>
#include "protocol_group_misc_defs.h"
#define IBUTTON_MISC_READ_TIMEOUT 100
#define IBUTTON_MISC_DATA_KEY_KEY_COMMON "Data"
typedef struct {
ProtocolDict* dict;
ProtocolId emulate_id;
} iButtonProtocolGroupMisc;
static iButtonProtocolGroupMisc* ibutton_protocol_group_misc_alloc(void) {
iButtonProtocolGroupMisc* group = malloc(sizeof(iButtonProtocolGroupMisc));
group->dict = protocol_dict_alloc(ibutton_protocols_misc, iButtonProtocolMiscMax);
group->emulate_id = PROTOCOL_NO;
return group;
}
static void ibutton_protocol_group_misc_free(iButtonProtocolGroupMisc* group) {
protocol_dict_free(group->dict);
free(group);
}
static size_t ibutton_protocol_group_misc_get_max_data_size(iButtonProtocolGroupMisc* group) {
return protocol_dict_get_max_data_size(group->dict);
}
static bool ibutton_protocol_group_misc_get_id_by_name(
iButtonProtocolGroupMisc* group,
iButtonProtocolLocalId* id,
const char* name) {
const ProtocolId found_id = protocol_dict_get_protocol_by_name(group->dict, name);
if(found_id != PROTOCOL_NO) {
*id = found_id;
return true;
}
return false;
}
static uint32_t ibutton_protocol_group_misc_get_features(
iButtonProtocolGroupMisc* group,
iButtonProtocolLocalId id) {
UNUSED(group);
UNUSED(id);
return 0;
}
static const char* ibutton_protocol_group_misc_get_manufacturer(
iButtonProtocolGroupMisc* group,
iButtonProtocolLocalId id) {
return protocol_dict_get_manufacturer(group->dict, id);
}
static const char* ibutton_protocol_group_misc_get_name(
iButtonProtocolGroupMisc* group,
iButtonProtocolLocalId id) {
return protocol_dict_get_name(group->dict, id);
}
typedef struct {
uint32_t last_dwt_value;
FuriStreamBuffer* stream;
} iButtonReadContext;
static void ibutton_protocols_comparator_callback(bool level, void* context) {
iButtonReadContext* read_context = context;
uint32_t current_dwt_value = DWT->CYCCNT;
LevelDuration data =
level_duration_make(level, current_dwt_value - read_context->last_dwt_value);
furi_stream_buffer_send(read_context->stream, &data, sizeof(LevelDuration), 0);
read_context->last_dwt_value = current_dwt_value;
}
static bool ibutton_protocol_group_misc_read(
iButtonProtocolGroupMisc* group,
iButtonProtocolData* data,
iButtonProtocolLocalId* id) {
bool result = false;
protocol_dict_decoders_start(group->dict);
furi_hal_rfid_pins_reset();
// pulldown pull pin, we sense the signal through the analog part of the RFID schematic
furi_hal_rfid_pin_pull_pulldown();
iButtonReadContext read_context = {
.last_dwt_value = DWT->CYCCNT,
.stream = furi_stream_buffer_alloc(sizeof(LevelDuration) * 512, 1),
};
furi_hal_rfid_comp_set_callback(ibutton_protocols_comparator_callback, &read_context);
furi_hal_rfid_comp_start();
const uint32_t tick_start = furi_get_tick();
for(;;) {
LevelDuration level;
size_t ret = furi_stream_buffer_receive(
read_context.stream, &level, sizeof(LevelDuration), IBUTTON_MISC_READ_TIMEOUT);
if((furi_get_tick() - tick_start) > IBUTTON_MISC_READ_TIMEOUT) {
break;
}
if(ret > 0) {
ProtocolId decoded_index = protocol_dict_decoders_feed(
group->dict, level_duration_get_level(level), level_duration_get_duration(level));
if(decoded_index == PROTOCOL_NO) continue;
*id = decoded_index;
protocol_dict_get_data(
group->dict,
decoded_index,
data,
protocol_dict_get_data_size(group->dict, decoded_index));
result = true;
}
}
furi_hal_rfid_comp_stop();
furi_hal_rfid_comp_set_callback(NULL, NULL);
furi_hal_rfid_pins_reset();
furi_stream_buffer_free(read_context.stream);
return result;
}
static void ibutton_protocol_group_misc_emulate_callback(void* context) {
iButtonProtocolGroupMisc* group = context;
const LevelDuration level_duration =
protocol_dict_encoder_yield(group->dict, group->emulate_id);
furi_hal_ibutton_emulate_set_next(level_duration_get_duration(level_duration));
furi_hal_ibutton_pin_write(level_duration_get_level(level_duration));
}
static void ibutton_protocol_group_misc_emulate_start(
iButtonProtocolGroupMisc* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
group->emulate_id = id;
protocol_dict_set_data(group->dict, id, data, protocol_dict_get_data_size(group->dict, id));
protocol_dict_encoder_start(group->dict, group->emulate_id);
furi_hal_ibutton_pin_configure();
furi_hal_ibutton_emulate_start(0, ibutton_protocol_group_misc_emulate_callback, group);
}
static void ibutton_protocol_group_misc_emulate_stop(
iButtonProtocolGroupMisc* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
UNUSED(group);
UNUSED(data);
UNUSED(id);
furi_hal_ibutton_emulate_stop();
furi_hal_ibutton_pin_reset();
}
static bool ibutton_protocol_group_misc_save(
iButtonProtocolGroupMisc* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FlipperFormat* ff) {
const size_t data_size = protocol_dict_get_data_size(group->dict, id);
return flipper_format_write_hex(ff, IBUTTON_MISC_DATA_KEY_KEY_COMMON, data, data_size);
}
static bool ibutton_protocol_group_misc_load(
iButtonProtocolGroupMisc* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id,
uint32_t version,
FlipperFormat* ff) {
const size_t data_size = protocol_dict_get_data_size(group->dict, id);
switch(version) {
case 1:
case 2:
return flipper_format_read_hex(ff, IBUTTON_MISC_DATA_KEY_KEY_COMMON, data, data_size);
default:
return false;
}
}
static void ibutton_protocol_group_misc_render_uid(
iButtonProtocolGroupMisc* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
const size_t data_size = protocol_dict_get_data_size(group->dict, id);
protocol_dict_set_data(group->dict, id, data, data_size);
protocol_dict_render_uid(group->dict, result, id);
}
static void ibutton_protocol_group_misc_render_data(
iButtonProtocolGroupMisc* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
const size_t data_size = protocol_dict_get_data_size(group->dict, id);
protocol_dict_set_data(group->dict, id, data, data_size);
protocol_dict_render_data(group->dict, result, id);
}
static void ibutton_protocol_group_misc_render_brief_data(
iButtonProtocolGroupMisc* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
const size_t data_size = protocol_dict_get_data_size(group->dict, id);
protocol_dict_set_data(group->dict, id, data, data_size);
protocol_dict_render_brief_data(group->dict, result, id);
}
static void ibutton_protocol_group_misc_render_error(
iButtonProtocolGroupMisc* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id,
FuriString* result) {
UNUSED(group);
UNUSED(data);
UNUSED(id);
UNUSED(result);
}
static bool ibutton_protocol_group_misc_is_valid(
iButtonProtocolGroupMisc* group,
const iButtonProtocolData* data,
iButtonProtocolLocalId id) {
UNUSED(group);
UNUSED(data);
UNUSED(id);
return true;
}
static void ibutton_protocol_group_misc_get_editable_data(
iButtonProtocolGroupMisc* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id,
iButtonEditableData* editable) {
editable->ptr = data;
editable->size = protocol_dict_get_data_size(group->dict, id);
}
static void ibutton_protocol_group_misc_apply_edits(
iButtonProtocolGroupMisc* group,
iButtonProtocolData* data,
iButtonProtocolLocalId id) {
const size_t data_size = protocol_dict_get_data_size(group->dict, id);
protocol_dict_set_data(group->dict, id, data, data_size);
}
const iButtonProtocolGroupBase ibutton_protocol_group_misc = {
.protocol_count = iButtonProtocolMiscMax,
.alloc = (iButtonProtocolGroupAllocFunc)ibutton_protocol_group_misc_alloc,
.free = (iButtonProtocolGroupFreeFunc)ibutton_protocol_group_misc_free,
.get_max_data_size =
(iButtonProtocolGropuGetSizeFunc)ibutton_protocol_group_misc_get_max_data_size,
.get_id_by_name = (iButtonProtocolGroupGetIdFunc)ibutton_protocol_group_misc_get_id_by_name,
.get_features = (iButtonProtocolGroupGetFeaturesFunc)ibutton_protocol_group_misc_get_features,
.get_manufacturer =
(iButtonProtocolGroupGetStringFunc)ibutton_protocol_group_misc_get_manufacturer,
.get_name = (iButtonProtocolGroupGetStringFunc)ibutton_protocol_group_misc_get_name,
.read = (iButtonProtocolGroupReadFunc)ibutton_protocol_group_misc_read,
.write_id = NULL,
.write_copy = NULL,
.emulate_start = (iButtonProtocolGroupApplyFunc)ibutton_protocol_group_misc_emulate_start,
.emulate_stop = (iButtonProtocolGroupApplyFunc)ibutton_protocol_group_misc_emulate_stop,
.save = (iButtonProtocolGroupSaveFunc)ibutton_protocol_group_misc_save,
.load = (iButtonProtocolGroupLoadFunc)ibutton_protocol_group_misc_load,
.render_uid = (iButtonProtocolGroupRenderFunc)ibutton_protocol_group_misc_render_uid,
.render_data = (iButtonProtocolGroupRenderFunc)ibutton_protocol_group_misc_render_data,
.render_brief_data =
(iButtonProtocolGroupRenderFunc)ibutton_protocol_group_misc_render_brief_data,
.render_error = (iButtonProtocolGroupRenderFunc)ibutton_protocol_group_misc_render_error,
.is_valid = (iButtonProtocolGroupIsValidFunc)ibutton_protocol_group_misc_is_valid,
.get_editable_data =
(iButtonProtocolGroupGetDataFunc)ibutton_protocol_group_misc_get_editable_data,
.apply_edits = (iButtonProtocolGroupApplyFunc)ibutton_protocol_group_misc_apply_edits,
};
| 10,207
|
C
|
.c
| 245
| 36.542857
| 98
| 0.735481
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,209
|
protocol_metakom.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/misc/protocol_metakom.c
|
#include <furi.h>
#include <furi_hal.h>
#include "protocol_metakom.h"
#define METAKOM_DATA_SIZE sizeof(uint32_t)
#define METAKOM_PERIOD (125 * furi_hal_cortex_instructions_per_microsecond())
#define METAKOM_0_LOW (METAKOM_PERIOD * 0.33f)
#define METAKOM_0_HI (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_LOW (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_HI (METAKOM_PERIOD * 0.33f)
#define METAKOM_PERIOD_SAMPLE_COUNT 10
typedef enum {
METAKOM_WAIT_PERIOD_SYNC,
METAKOM_WAIT_START_BIT,
METAKOM_WAIT_START_WORD,
METAKOM_READ_WORD,
METAKOM_READ_STOP_WORD,
} MetakomState;
typedef enum {
METAKOM_BIT_WAIT_FRONT_HIGH,
METAKOM_BIT_WAIT_FRONT_LOW,
} MetakomBitState;
typedef struct {
// high + low period time
uint32_t period_time;
uint32_t low_time_storage;
uint8_t period_sample_index;
uint32_t period_sample_data[METAKOM_PERIOD_SAMPLE_COUNT];
uint8_t tmp_data;
uint8_t tmp_counter;
uint8_t key_data_index;
MetakomBitState bit_state;
MetakomState state;
} ProtocolMetakomDecoder;
typedef struct {
uint32_t index;
} ProtocolMetakomEncoder;
typedef struct {
uint32_t data;
ProtocolMetakomDecoder decoder;
ProtocolMetakomEncoder encoder;
} ProtocolMetakom;
static ProtocolMetakom* protocol_metakom_alloc(void) {
ProtocolMetakom* proto = malloc(sizeof(ProtocolMetakom));
return (void*)proto;
}
static void protocol_metakom_free(ProtocolMetakom* proto) {
free(proto);
}
static uint8_t* protocol_metakom_get_data(ProtocolMetakom* proto) {
return (uint8_t*)&proto->data;
}
static void protocol_metakom_decoder_start(ProtocolMetakom* proto) {
ProtocolMetakomDecoder* metakom = &proto->decoder;
metakom->period_sample_index = 0;
metakom->period_time = 0;
metakom->tmp_counter = 0;
metakom->tmp_data = 0;
for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
metakom->period_sample_data[i] = 0;
};
metakom->state = METAKOM_WAIT_PERIOD_SYNC;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
metakom->key_data_index = 0;
metakom->low_time_storage = 0;
proto->data = 0;
}
static bool metakom_parity_check(uint8_t data) {
uint8_t ones_count = 0;
bool result;
for(uint8_t i = 0; i < 8; i++) {
if((data >> i) & 0b00000001) {
ones_count++;
}
}
result = (ones_count % 2 == 0);
return result;
}
static bool metakom_process_bit(
ProtocolMetakomDecoder* metakom,
bool polarity,
uint32_t time,
uint32_t* high_time,
uint32_t* low_time) {
bool result = false;
switch(metakom->bit_state) {
case METAKOM_BIT_WAIT_FRONT_LOW:
if(polarity == false) {
*low_time = metakom->low_time_storage;
*high_time = time;
result = true;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_HIGH;
}
break;
case METAKOM_BIT_WAIT_FRONT_HIGH:
if(polarity == true) {
metakom->low_time_storage = time;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
}
break;
}
return result;
}
static bool protocol_metakom_decoder_feed(ProtocolMetakom* proto, bool level, uint32_t duration) {
ProtocolMetakomDecoder* metakom = &proto->decoder;
bool ready = false;
uint32_t high_time = 0;
uint32_t low_time = 0;
switch(metakom->state) {
case METAKOM_WAIT_PERIOD_SYNC:
if(metakom_process_bit(metakom, level, duration, &high_time, &low_time)) {
metakom->period_sample_data[metakom->period_sample_index] = high_time + low_time;
metakom->period_sample_index++;
if(metakom->period_sample_index == METAKOM_PERIOD_SAMPLE_COUNT) {
for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
metakom->period_time += metakom->period_sample_data[i];
};
metakom->period_time /= METAKOM_PERIOD_SAMPLE_COUNT;
metakom->state = METAKOM_WAIT_START_BIT;
}
}
break;
case METAKOM_WAIT_START_BIT:
if(metakom_process_bit(metakom, level, duration, &high_time, &low_time)) {
metakom->tmp_counter++;
if(high_time > metakom->period_time) {
metakom->tmp_counter = 0;
metakom->state = METAKOM_WAIT_START_WORD;
}
if(metakom->tmp_counter > 40) {
protocol_metakom_decoder_start(proto);
}
}
break;
case METAKOM_WAIT_START_WORD:
if(metakom_process_bit(metakom, level, duration, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 3) {
if(metakom->tmp_data == 0b010) {
metakom->tmp_counter = 0;
metakom->tmp_data = 0;
metakom->state = METAKOM_READ_WORD;
} else {
protocol_metakom_decoder_start(proto);
}
}
}
break;
case METAKOM_READ_WORD:
if(metakom_process_bit(metakom, level, duration, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 8) {
if(metakom_parity_check(metakom->tmp_data)) {
proto->data = (proto->data << 8) | metakom->tmp_data;
metakom->key_data_index++;
metakom->tmp_data = 0;
metakom->tmp_counter = 0;
if(metakom->key_data_index == 4) {
// check for stop bit
if(high_time > metakom->period_time) {
metakom->state = METAKOM_READ_STOP_WORD;
} else {
protocol_metakom_decoder_start(proto);
}
}
} else {
protocol_metakom_decoder_start(proto);
}
}
}
break;
case METAKOM_READ_STOP_WORD:
if(metakom_process_bit(metakom, level, duration, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 3) {
if(metakom->tmp_data == 0b010) {
ready = true;
} else {
protocol_metakom_decoder_start(proto);
}
}
}
break;
}
return ready;
}
static bool protocol_metakom_encoder_start(ProtocolMetakom* proto) {
proto->encoder.index = 0;
return true;
}
static LevelDuration protocol_metakom_encoder_yield(ProtocolMetakom* proto) {
LevelDuration result;
if(proto->encoder.index == 0) {
// sync bit
result = level_duration_make(false, METAKOM_PERIOD);
} else if(proto->encoder.index <= 6) {
// start word (0b010)
switch(proto->encoder.index) {
case 1:
result = level_duration_make(true, METAKOM_0_LOW); //-V1037
break;
case 2:
result = level_duration_make(false, METAKOM_0_HI); //-V1037
break;
case 3:
result = level_duration_make(true, METAKOM_1_LOW);
break;
case 4:
result = level_duration_make(false, METAKOM_1_HI);
break;
case 5:
result = level_duration_make(true, METAKOM_0_LOW);
break;
case 6:
result = level_duration_make(false, METAKOM_0_HI);
break;
}
} else {
// data
uint8_t data_start_index = proto->encoder.index - 7;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = (proto->data >> (32 - 1 - bit_index)) & 1;
if(!clock_polarity) {
if(bit_value) {
result = level_duration_make(true, METAKOM_1_LOW);
} else {
result = level_duration_make(true, METAKOM_0_LOW);
}
} else {
if(bit_value) {
result = level_duration_make(false, METAKOM_1_HI);
} else {
result = level_duration_make(false, METAKOM_0_HI);
}
}
}
proto->encoder.index++;
if(proto->encoder.index >= (1 + 3 * 2 + 32 * 2)) {
proto->encoder.index = 0;
}
return result;
}
static void protocol_metakom_render_uid(ProtocolMetakom* proto, FuriString* result) {
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < METAKOM_DATA_SIZE; ++i) {
furi_string_cat_printf(result, "%02X ", ((uint8_t*)&proto->data)[i]);
}
}
static void protocol_metakom_render_brief_data(ProtocolMetakom* proto, FuriString* result) {
protocol_metakom_render_uid(proto, result);
}
const ProtocolBase ibutton_protocol_misc_metakom = {
.name = "Metakom",
.manufacturer = "Metakom",
.data_size = METAKOM_DATA_SIZE,
.alloc = (ProtocolAlloc)protocol_metakom_alloc,
.free = (ProtocolFree)protocol_metakom_free,
.get_data = (ProtocolGetData)protocol_metakom_get_data,
.decoder =
{
.start = (ProtocolDecoderStart)protocol_metakom_decoder_start,
.feed = (ProtocolDecoderFeed)protocol_metakom_decoder_feed,
},
.encoder =
{
.start = (ProtocolEncoderStart)protocol_metakom_encoder_start,
.yield = (ProtocolEncoderYield)protocol_metakom_encoder_yield,
},
.render_uid = (ProtocolRenderData)protocol_metakom_render_uid,
.render_brief_data = (ProtocolRenderData)protocol_metakom_render_brief_data,
};
| 10,356
|
C
|
.c
| 286
| 27.083916
| 98
| 0.574137
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,210
|
protocol_cyfral.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/misc/protocol_cyfral.c
|
#include <furi.h>
#include <furi_hal.h>
#include "protocol_cyfral.h"
#define CYFRAL_DATA_SIZE sizeof(uint16_t)
#define CYFRAL_PERIOD (125 * furi_hal_cortex_instructions_per_microsecond())
#define CYFRAL_0_LOW (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_0_HI (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_LOW (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_HI (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_MAX_PERIOD_US 230
typedef enum {
CYFRAL_BIT_WAIT_FRONT_HIGH,
CYFRAL_BIT_WAIT_FRONT_LOW,
} CyfralBitState;
typedef enum {
CYFRAL_WAIT_START_NIBBLE,
CYFRAL_READ_NIBBLE,
CYFRAL_READ_STOP_NIBBLE,
} CyfralState;
typedef struct {
CyfralState state;
CyfralBitState bit_state;
// high + low period time
uint32_t period_time;
// temporary nibble storage
uint8_t nibble;
// data valid flag
// MUST be checked only in READ_STOP_NIBBLE state
bool data_valid;
// nibble index, we expect 8 nibbles
uint8_t index;
// bit index in nibble, 4 bit per nibble
uint8_t bit_index;
// max period, 230us x clock per us
uint32_t max_period;
} ProtocolCyfralDecoder;
typedef struct {
uint32_t data;
uint32_t index;
} ProtocolCyfralEncoder;
typedef struct {
uint16_t data;
ProtocolCyfralDecoder decoder;
ProtocolCyfralEncoder encoder;
} ProtocolCyfral;
static void* protocol_cyfral_alloc(void) {
ProtocolCyfral* proto = malloc(sizeof(ProtocolCyfral));
return (void*)proto;
}
static void protocol_cyfral_free(ProtocolCyfral* proto) {
free(proto);
}
static uint8_t* protocol_cyfral_get_data(ProtocolCyfral* proto) {
return (uint8_t*)&proto->data;
}
static void protocol_cyfral_decoder_start(ProtocolCyfral* proto) {
ProtocolCyfralDecoder* cyfral = &proto->decoder;
cyfral->state = CYFRAL_WAIT_START_NIBBLE;
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_LOW;
cyfral->period_time = 0;
cyfral->bit_index = 0;
cyfral->index = 0;
cyfral->nibble = 0;
cyfral->data_valid = true;
cyfral->max_period = CYFRAL_MAX_PERIOD_US * furi_hal_cortex_instructions_per_microsecond();
proto->data = 0;
}
static bool protocol_cyfral_decoder_process_bit(
ProtocolCyfralDecoder* cyfral,
bool polarity,
uint32_t length,
bool* bit_ready,
bool* bit_value) {
bool result = true;
*bit_ready = false;
// bit start from low
switch(cyfral->bit_state) {
case CYFRAL_BIT_WAIT_FRONT_LOW:
if(polarity == true) {
cyfral->period_time += length;
*bit_ready = true;
if(cyfral->period_time <= cyfral->max_period) {
if((cyfral->period_time / 2) > length) {
*bit_value = false;
} else {
*bit_value = true;
}
} else {
result = false;
}
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_HIGH;
} else {
result = false;
}
break;
case CYFRAL_BIT_WAIT_FRONT_HIGH:
if(polarity == false) {
cyfral->period_time = length;
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_LOW;
} else {
result = false;
}
break;
}
return result;
}
static bool protocol_cyfral_decoder_feed(ProtocolCyfral* proto, bool level, uint32_t duration) {
ProtocolCyfralDecoder* cyfral = &proto->decoder;
bool bit_ready;
bool bit_value;
bool decoded = false;
switch(cyfral->state) {
case CYFRAL_WAIT_START_NIBBLE:
// wait for start word
if(protocol_cyfral_decoder_process_bit(cyfral, level, duration, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = ((cyfral->nibble << 1) | bit_value) & 0x0F;
if(cyfral->nibble == 0b0001) {
cyfral->nibble = 0;
cyfral->state = CYFRAL_READ_NIBBLE;
}
}
} else {
protocol_cyfral_decoder_start(proto);
}
break;
case CYFRAL_READ_NIBBLE:
// read nibbles
if(protocol_cyfral_decoder_process_bit(cyfral, level, duration, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = (cyfral->nibble << 1) | bit_value;
cyfral->bit_index++;
//convert every nibble to 2-bit index
if(cyfral->bit_index == 4) {
switch(cyfral->nibble) {
case 0b1110:
proto->data = (proto->data << 2) | 0b11;
break;
case 0b1101:
proto->data = (proto->data << 2) | 0b10;
break;
case 0b1011:
proto->data = (proto->data << 2) | 0b01;
break;
case 0b0111:
proto->data = (proto->data << 2) | 0b00;
break;
default:
cyfral->data_valid = false;
break;
}
cyfral->nibble = 0;
cyfral->bit_index = 0;
cyfral->index++;
}
// successfully read 8 nibbles
if(cyfral->index == 8) {
cyfral->state = CYFRAL_READ_STOP_NIBBLE;
}
}
} else {
protocol_cyfral_decoder_start(proto);
}
break;
case CYFRAL_READ_STOP_NIBBLE:
// read stop nibble
if(protocol_cyfral_decoder_process_bit(cyfral, level, duration, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = ((cyfral->nibble << 1) | bit_value) & 0x0F;
cyfral->bit_index++;
switch(cyfral->bit_index) {
case 0:
case 1:
case 2:
case 3:
break;
case 4:
if(cyfral->nibble == 0b0001) {
// validate data
if(cyfral->data_valid) {
decoded = true;
} else {
protocol_cyfral_decoder_start(proto);
}
} else {
protocol_cyfral_decoder_start(proto);
}
break;
default:
protocol_cyfral_decoder_start(proto);
break;
}
}
} else {
protocol_cyfral_decoder_start(proto);
}
break;
}
return decoded;
}
static uint32_t protocol_cyfral_encoder_encode(const uint16_t data) {
uint32_t value = 0;
for(int8_t i = 0; i <= 7; i++) {
switch((data >> (i * 2)) & 0b00000011) {
case 0b11:
value = value << 4;
value += 0b00000111;
break;
case 0b10:
value = value << 4;
value += 0b00001011;
break;
case 0b01:
value = value << 4;
value += 0b00001101;
break;
case 0b00:
value = value << 4;
value += 0b00001110;
break;
default:
break;
}
}
return value;
}
static bool protocol_cyfral_encoder_start(ProtocolCyfral* proto) {
proto->encoder.index = 0;
proto->encoder.data = protocol_cyfral_encoder_encode(proto->data);
return true;
}
static LevelDuration protocol_cyfral_encoder_yield(ProtocolCyfral* proto) {
LevelDuration result;
if(proto->encoder.index < 8) {
// start word (0b0001)
switch(proto->encoder.index) {
case 0:
result = level_duration_make(false, CYFRAL_0_LOW); //-V1037
break;
case 1:
result = level_duration_make(true, CYFRAL_0_HI); //-V1037
break;
case 2:
result = level_duration_make(false, CYFRAL_0_LOW);
break;
case 3:
result = level_duration_make(true, CYFRAL_0_HI);
break;
case 4:
result = level_duration_make(false, CYFRAL_0_LOW);
break;
case 5:
result = level_duration_make(true, CYFRAL_0_HI);
break;
case 6:
result = level_duration_make(false, CYFRAL_1_LOW);
break;
case 7:
result = level_duration_make(true, CYFRAL_1_HI);
break;
}
} else {
// data
uint8_t data_start_index = proto->encoder.index - 8;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = ((proto->encoder.data >> bit_index) & 1);
if(!clock_polarity) {
if(bit_value) {
result = level_duration_make(false, CYFRAL_1_LOW);
} else {
result = level_duration_make(false, CYFRAL_0_LOW);
}
} else {
if(bit_value) {
result = level_duration_make(true, CYFRAL_1_HI);
} else {
result = level_duration_make(true, CYFRAL_0_HI);
}
}
}
proto->encoder.index++;
if(proto->encoder.index >= (9 * 4 * 2)) {
proto->encoder.index = 0;
}
return result;
}
static void protocol_cyfral_render_uid(ProtocolCyfral* proto, FuriString* result) {
furi_string_cat_printf(result, "ID: ");
for(size_t i = 0; i < CYFRAL_DATA_SIZE; ++i) {
furi_string_cat_printf(result, "%02X ", ((uint8_t*)&proto->data)[i]);
}
}
static void protocol_cyfral_render_brief_data(ProtocolCyfral* proto, FuriString* result) {
protocol_cyfral_render_uid(proto, result);
}
const ProtocolBase ibutton_protocol_misc_cyfral = {
.name = "Cyfral",
.manufacturer = "Cyfral",
.data_size = CYFRAL_DATA_SIZE,
.alloc = (ProtocolAlloc)protocol_cyfral_alloc,
.free = (ProtocolFree)protocol_cyfral_free,
.get_data = (ProtocolGetData)protocol_cyfral_get_data,
.decoder =
{
.start = (ProtocolDecoderStart)protocol_cyfral_decoder_start,
.feed = (ProtocolDecoderFeed)protocol_cyfral_decoder_feed,
},
.encoder =
{
.start = (ProtocolEncoderStart)protocol_cyfral_encoder_start,
.yield = (ProtocolEncoderYield)protocol_cyfral_encoder_yield,
},
.render_uid = (ProtocolRenderData)protocol_cyfral_render_uid,
.render_brief_data = (ProtocolRenderData)protocol_cyfral_render_brief_data,
};
| 10,674
|
C
|
.c
| 316
| 23.721519
| 98
| 0.541586
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,211
|
protocol_group_misc_defs.c
|
DarkFlippers_unleashed-firmware/lib/ibutton/protocols/misc/protocol_group_misc_defs.c
|
#include "protocol_group_misc_defs.h"
#include "protocol_cyfral.h"
#include "protocol_metakom.h"
const ProtocolBase* ibutton_protocols_misc[] = {
[iButtonProtocolMiscCyfral] = &ibutton_protocol_misc_cyfral,
[iButtonProtocolMiscMetakom] = &ibutton_protocol_misc_metakom,
/* Add new misc protocols here */
};
| 321
|
C
|
.c
| 8
| 37.375
| 66
| 0.758842
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,213
|
digital_signal.c
|
DarkFlippers_unleashed-firmware/lib/digital_signal/digital_signal.c
|
#include "digital_signal.h"
#include "digital_signal_i.h"
#include <furi.h>
#define TAG "DigitalSignal"
DigitalSignal* digital_signal_alloc(uint32_t max_size) {
DigitalSignal* signal = malloc(sizeof(DigitalSignal) + (max_size * sizeof(uint32_t)));
signal->max_size = max_size;
return signal;
}
void digital_signal_free(DigitalSignal* signal) {
furi_check(signal);
free(signal);
}
bool digital_signal_get_start_level(const DigitalSignal* signal) {
furi_check(signal);
return signal->start_level;
}
void digital_signal_set_start_level(DigitalSignal* signal, bool level) {
furi_check(signal);
signal->start_level = level;
}
uint32_t digital_signal_get_size(const DigitalSignal* signal) {
furi_check(signal);
return signal->size;
}
void digital_signal_add_period(DigitalSignal* signal, uint32_t ticks) {
furi_check(signal);
furi_check(signal->size < signal->max_size);
const uint32_t duration = ticks + signal->remainder;
uint32_t reload_value = duration / DIGITAL_SIGNAL_T_TIM;
int32_t remainder = duration - reload_value * DIGITAL_SIGNAL_T_TIM;
if(remainder >= DIGITAL_SIGNAL_T_TIM_DIV2) {
reload_value += 1;
remainder -= DIGITAL_SIGNAL_T_TIM;
}
furi_check(reload_value > 1);
signal->data[signal->size++] = reload_value - 1;
signal->remainder = remainder;
}
static void digital_signal_extend_last_period(DigitalSignal* signal, uint32_t ticks) {
furi_assert(signal->size <= signal->max_size);
const uint32_t reload_value_old = signal->data[signal->size - 1] + 1;
const uint32_t duration = ticks + signal->remainder + reload_value_old * DIGITAL_SIGNAL_T_TIM;
uint32_t reload_value = duration / DIGITAL_SIGNAL_T_TIM;
int32_t remainder = duration - reload_value * DIGITAL_SIGNAL_T_TIM;
if(remainder >= DIGITAL_SIGNAL_T_TIM_DIV2) {
reload_value += 1;
remainder -= DIGITAL_SIGNAL_T_TIM;
}
furi_check(reload_value > 1);
signal->data[signal->size - 1] = reload_value - 1;
signal->remainder = remainder;
}
void digital_signal_add_period_with_level(DigitalSignal* signal, uint32_t ticks, bool level) {
furi_check(signal);
if(signal->size == 0) {
signal->start_level = level;
digital_signal_add_period(signal, ticks);
} else {
const bool end_level = signal->start_level ^ !(signal->size % 2);
if(level != end_level) {
digital_signal_add_period(signal, ticks);
} else {
digital_signal_extend_last_period(signal, ticks);
}
}
}
| 2,584
|
C
|
.c
| 67
| 33.507463
| 98
| 0.681398
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,216
|
iso15693_signal.c
|
DarkFlippers_unleashed-firmware/lib/digital_signal/presets/nfc/iso15693_signal.c
|
#include "iso15693_signal.h"
#include <digital_signal/digital_sequence.h>
#define BITS_IN_BYTE (8U)
#define ISO15693_SIGNAL_COEFF_HI (1U)
#define ISO15693_SIGNAL_COEFF_LO (4U)
#define ISO15693_SIGNAL_ZERO_EDGES (16U)
#define ISO15693_SIGNAL_ONE_EDGES (ISO15693_SIGNAL_ZERO_EDGES + 1U)
#define ISO15693_SIGNAL_EOF_EDGES (64U)
#define ISO15693_SIGNAL_SOF_EDGES (ISO15693_SIGNAL_EOF_EDGES + 1U)
#define ISO15693_SIGNAL_EDGES (1350U)
#define ISO15693_SIGNAL_FC (13.56e6)
#define ISO15693_SIGNAL_FC_16 (16.0e11 / ISO15693_SIGNAL_FC)
#define ISO15693_SIGNAL_FC_256 (256.0e11 / ISO15693_SIGNAL_FC)
#define ISO15693_SIGNAL_FC_768 (768.0e11 / ISO15693_SIGNAL_FC)
typedef enum {
Iso15693SignalIndexSof,
Iso15693SignalIndexEof,
Iso15693SignalIndexOne,
Iso15693SignalIndexZero,
Iso15693SignalIndexNum,
} Iso15693SignalIndex;
typedef DigitalSignal* Iso15693SignalBank[Iso15693SignalIndexNum];
struct Iso15693Signal {
DigitalSequence* tx_sequence;
Iso15693SignalBank banks[Iso15693SignalDataRateNum];
};
// Add an unmodulated signal for the length of Fc / 256 * k (where k = 1 or 4)
static void iso15693_add_silence(DigitalSignal* signal, Iso15693SignalDataRate data_rate) {
const uint32_t k = data_rate == Iso15693SignalDataRateHi ? ISO15693_SIGNAL_COEFF_HI :
ISO15693_SIGNAL_COEFF_LO;
digital_signal_add_period_with_level(signal, ISO15693_SIGNAL_FC_256 * k, false);
}
// Add 8 * k subcarrier pulses of Fc / 16 (where k = 1 or 4)
static void iso15693_add_subcarrier(DigitalSignal* signal, Iso15693SignalDataRate data_rate) {
const uint32_t k = data_rate == Iso15693SignalDataRateHi ? ISO15693_SIGNAL_COEFF_HI :
ISO15693_SIGNAL_COEFF_LO;
for(uint32_t i = 0; i < ISO15693_SIGNAL_ZERO_EDGES * k; ++i) {
digital_signal_add_period_with_level(signal, ISO15693_SIGNAL_FC_16, !(i % 2));
}
}
static void iso15693_add_bit(DigitalSignal* signal, Iso15693SignalDataRate data_rate, bool bit) {
if(bit) {
iso15693_add_silence(signal, data_rate);
iso15693_add_subcarrier(signal, data_rate);
} else {
iso15693_add_subcarrier(signal, data_rate);
iso15693_add_silence(signal, data_rate);
}
}
static inline void iso15693_add_sof(DigitalSignal* signal, Iso15693SignalDataRate data_rate) {
// Not adding silence since it only increases response time
for(uint32_t i = 0; i < ISO15693_SIGNAL_FC_768 / ISO15693_SIGNAL_FC_256; ++i) {
iso15693_add_subcarrier(signal, data_rate);
}
iso15693_add_bit(signal, data_rate, true);
}
static inline void iso15693_add_eof(DigitalSignal* signal, Iso15693SignalDataRate data_rate) {
iso15693_add_bit(signal, data_rate, false);
for(uint32_t i = 0; i < ISO15693_SIGNAL_FC_768 / ISO15693_SIGNAL_FC_256; ++i) {
iso15693_add_subcarrier(signal, data_rate);
}
// Not adding silence since it does nothing here
}
static inline uint32_t
iso15693_get_sequence_index(Iso15693SignalIndex index, Iso15693SignalDataRate data_rate) {
return index + data_rate * Iso15693SignalIndexNum;
}
static inline void
iso15693_add_byte(Iso15693Signal* instance, Iso15693SignalDataRate data_rate, uint8_t byte) {
for(size_t i = 0; i < BITS_IN_BYTE; i++) {
const uint8_t bit = byte & (1U << i);
digital_sequence_add_signal(
instance->tx_sequence,
iso15693_get_sequence_index(
bit ? Iso15693SignalIndexOne : Iso15693SignalIndexZero, data_rate));
}
}
static inline void iso15693_signal_encode(
Iso15693Signal* instance,
Iso15693SignalDataRate data_rate,
const uint8_t* tx_data,
size_t tx_data_size) {
digital_sequence_add_signal(
instance->tx_sequence, iso15693_get_sequence_index(Iso15693SignalIndexSof, data_rate));
for(size_t i = 0; i < tx_data_size; i++) {
iso15693_add_byte(instance, data_rate, tx_data[i]);
}
digital_sequence_add_signal(
instance->tx_sequence, iso15693_get_sequence_index(Iso15693SignalIndexEof, data_rate));
}
static void iso15693_signal_bank_fill(Iso15693Signal* instance, Iso15693SignalDataRate data_rate) {
const uint32_t k = data_rate == Iso15693SignalDataRateHi ? ISO15693_SIGNAL_COEFF_HI :
ISO15693_SIGNAL_COEFF_LO;
DigitalSignal** bank = instance->banks[data_rate];
bank[Iso15693SignalIndexSof] = digital_signal_alloc(ISO15693_SIGNAL_SOF_EDGES * k);
bank[Iso15693SignalIndexEof] = digital_signal_alloc(ISO15693_SIGNAL_EOF_EDGES * k);
bank[Iso15693SignalIndexOne] = digital_signal_alloc(ISO15693_SIGNAL_ONE_EDGES * k);
bank[Iso15693SignalIndexZero] = digital_signal_alloc(ISO15693_SIGNAL_ZERO_EDGES * k);
iso15693_add_sof(bank[Iso15693SignalIndexSof], data_rate);
iso15693_add_eof(bank[Iso15693SignalIndexEof], data_rate);
iso15693_add_bit(bank[Iso15693SignalIndexOne], data_rate, true);
iso15693_add_bit(bank[Iso15693SignalIndexZero], data_rate, false);
}
static void
iso15693_signal_bank_clear(Iso15693Signal* instance, Iso15693SignalDataRate data_rate) {
DigitalSignal** bank = instance->banks[data_rate];
for(uint32_t i = 0; i < Iso15693SignalIndexNum; ++i) {
digital_signal_free(bank[i]);
}
}
static void
iso15693_signal_bank_register(Iso15693Signal* instance, Iso15693SignalDataRate data_rate) {
for(uint32_t i = 0; i < Iso15693SignalIndexNum; ++i) {
digital_sequence_register_signal(
instance->tx_sequence,
iso15693_get_sequence_index(i, data_rate),
instance->banks[data_rate][i]);
}
}
Iso15693Signal* iso15693_signal_alloc(const GpioPin* pin) {
furi_assert(pin);
Iso15693Signal* instance = malloc(sizeof(Iso15693Signal));
instance->tx_sequence = digital_sequence_alloc(BITS_IN_BYTE * 255 + 2, pin);
for(uint32_t i = 0; i < Iso15693SignalDataRateNum; ++i) {
iso15693_signal_bank_fill(instance, i);
iso15693_signal_bank_register(instance, i);
}
return instance;
}
void iso15693_signal_free(Iso15693Signal* instance) {
furi_assert(instance);
digital_sequence_free(instance->tx_sequence);
for(uint32_t i = 0; i < Iso15693SignalDataRateNum; ++i) {
iso15693_signal_bank_clear(instance, i);
}
free(instance);
}
void iso15693_signal_tx(
Iso15693Signal* instance,
Iso15693SignalDataRate data_rate,
const uint8_t* tx_data,
size_t tx_data_size) {
furi_assert(instance);
furi_assert(data_rate < Iso15693SignalDataRateNum);
furi_assert(tx_data);
FURI_CRITICAL_ENTER();
digital_sequence_clear(instance->tx_sequence);
iso15693_signal_encode(instance, data_rate, tx_data, tx_data_size);
digital_sequence_transmit(instance->tx_sequence);
FURI_CRITICAL_EXIT();
}
void iso15693_signal_tx_sof(Iso15693Signal* instance, Iso15693SignalDataRate data_rate) {
furi_assert(instance);
furi_assert(data_rate < Iso15693SignalDataRateNum);
FURI_CRITICAL_ENTER();
digital_sequence_clear(instance->tx_sequence);
digital_sequence_add_signal(
instance->tx_sequence, iso15693_get_sequence_index(Iso15693SignalIndexSof, data_rate));
digital_sequence_transmit(instance->tx_sequence);
FURI_CRITICAL_EXIT();
}
| 7,373
|
C
|
.c
| 161
| 39.925466
| 99
| 0.709862
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,217
|
iso14443_3a_signal.c
|
DarkFlippers_unleashed-firmware/lib/digital_signal/presets/nfc/iso14443_3a_signal.c
|
#include "iso14443_3a_signal.h"
#include <digital_signal/digital_sequence.h>
#define BITS_IN_BYTE (8)
#define ISO14443_3A_SIGNAL_BIT_MAX_EDGES (10)
#define ISO14443_3A_SIGNAL_MAX_EDGES (1350)
#define ISO14443_3A_SIGNAL_SEQUENCE_SIZE \
(ISO14443_3A_SIGNAL_MAX_EDGES / (ISO14443_3A_SIGNAL_BIT_MAX_EDGES - 2))
#define ISO14443_3A_SIGNAL_F_SIG (13560000.0)
#define ISO14443_3A_SIGNAL_T_SIG 7374 //73.746ns*100
#define ISO14443_3A_SIGNAL_T_SIG_X8 58992 //T_SIG*8
#define ISO14443_3A_SIGNAL_T_SIG_X8_X8 471936 //T_SIG*8*8
#define ISO14443_3A_SIGNAL_T_SIG_X8_X9 530928 //T_SIG*8*9
typedef enum {
Iso14443_3aSignalIndexZero,
Iso14443_3aSignalIndexOne,
Iso14443_3aSignalIndexCount,
} Iso14443_3aSignalIndex;
typedef DigitalSignal* Iso14443_3aSignalBank[Iso14443_3aSignalIndexCount];
struct Iso14443_3aSignal {
DigitalSequence* tx_sequence;
Iso14443_3aSignalBank signals;
};
static void iso14443_3a_signal_add_byte(Iso14443_3aSignal* instance, uint8_t byte, bool parity) {
for(size_t i = 0; i < BITS_IN_BYTE; i++) {
digital_sequence_add_signal(
instance->tx_sequence,
FURI_BIT(byte, i) ? Iso14443_3aSignalIndexOne : Iso14443_3aSignalIndexZero);
}
digital_sequence_add_signal(
instance->tx_sequence, parity ? Iso14443_3aSignalIndexOne : Iso14443_3aSignalIndexZero);
}
static void iso14443_3a_signal_encode(
Iso14443_3aSignal* instance,
const uint8_t* tx_data,
const uint8_t* tx_parity,
size_t tx_bits) {
furi_assert(instance);
furi_assert(tx_data);
furi_assert(tx_parity);
// Start of frame
digital_sequence_add_signal(instance->tx_sequence, Iso14443_3aSignalIndexOne);
if(tx_bits < BITS_IN_BYTE) {
for(size_t i = 0; i < tx_bits; i++) {
digital_sequence_add_signal(
instance->tx_sequence,
FURI_BIT(tx_data[0], i) ? Iso14443_3aSignalIndexOne : Iso14443_3aSignalIndexZero);
}
} else {
for(size_t i = 0; i < tx_bits / BITS_IN_BYTE; i++) {
bool parity = FURI_BIT(tx_parity[i / BITS_IN_BYTE], i % BITS_IN_BYTE);
iso14443_3a_signal_add_byte(instance, tx_data[i], parity);
}
}
}
static inline void iso14443_3a_signal_set_bit(DigitalSignal* signal, bool bit) {
digital_signal_set_start_level(signal, bit);
if(bit) {
for(uint32_t i = 0; i < 7; ++i) {
digital_signal_add_period(signal, ISO14443_3A_SIGNAL_T_SIG_X8);
}
digital_signal_add_period(signal, ISO14443_3A_SIGNAL_T_SIG_X8_X9);
} else {
digital_signal_add_period(signal, ISO14443_3A_SIGNAL_T_SIG_X8_X8);
for(uint32_t i = 0; i < 8; ++i) {
digital_signal_add_period(signal, ISO14443_3A_SIGNAL_T_SIG_X8);
}
}
}
static inline void iso14443_3a_signal_bank_fill(Iso14443_3aSignalBank bank) {
for(uint32_t i = 0; i < Iso14443_3aSignalIndexCount; ++i) {
bank[i] = digital_signal_alloc(ISO14443_3A_SIGNAL_BIT_MAX_EDGES);
iso14443_3a_signal_set_bit(bank[i], i % Iso14443_3aSignalIndexCount != 0);
}
}
static inline void iso14443_3a_signal_bank_clear(Iso14443_3aSignalBank bank) {
for(uint32_t i = 0; i < Iso14443_3aSignalIndexCount; ++i) {
digital_signal_free(bank[i]);
}
}
static inline void
iso14443_3a_signal_bank_register(Iso14443_3aSignalBank bank, DigitalSequence* sequence) {
for(uint32_t i = 0; i < Iso14443_3aSignalIndexCount; ++i) {
digital_sequence_register_signal(sequence, i, bank[i]);
}
}
Iso14443_3aSignal* iso14443_3a_signal_alloc(const GpioPin* pin) {
furi_assert(pin);
Iso14443_3aSignal* instance = malloc(sizeof(Iso14443_3aSignal));
instance->tx_sequence = digital_sequence_alloc(ISO14443_3A_SIGNAL_SEQUENCE_SIZE, pin);
iso14443_3a_signal_bank_fill(instance->signals);
iso14443_3a_signal_bank_register(instance->signals, instance->tx_sequence);
return instance;
}
void iso14443_3a_signal_free(Iso14443_3aSignal* instance) {
furi_assert(instance);
furi_assert(instance->tx_sequence);
iso14443_3a_signal_bank_clear(instance->signals);
digital_sequence_free(instance->tx_sequence);
free(instance);
}
void iso14443_3a_signal_tx(
Iso14443_3aSignal* instance,
const uint8_t* tx_data,
const uint8_t* tx_parity,
size_t tx_bits) {
furi_assert(instance);
furi_assert(tx_data);
furi_assert(tx_parity);
FURI_CRITICAL_ENTER();
digital_sequence_clear(instance->tx_sequence);
iso14443_3a_signal_encode(instance, tx_data, tx_parity, tx_bits);
digital_sequence_transmit(instance->tx_sequence);
FURI_CRITICAL_EXIT();
}
| 4,654
|
C
|
.c
| 114
| 35.535088
| 98
| 0.689922
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,219
|
subghz_tx_rx_worker.c
|
DarkFlippers_unleashed-firmware/lib/subghz/subghz_tx_rx_worker.c
|
#include "subghz_tx_rx_worker.h"
#include <furi.h>
#define TAG "SubGhzTxRxWorker"
#define SUBGHZ_TXRX_WORKER_BUF_SIZE 2048
//you can not set more than 62 because it will not fit into the FIFO CC1101
#define SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE 60
#define SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF 40
struct SubGhzTxRxWorker {
FuriThread* thread;
FuriStreamBuffer* stream_tx;
FuriStreamBuffer* stream_rx;
volatile bool worker_running;
volatile bool worker_stopping;
SubGhzTxRxWorkerStatus status;
uint32_t frequency;
const SubGhzDevice* device;
const GpioPin* device_data_gpio;
SubGhzTxRxWorkerCallbackHaveRead callback_have_read;
void* context_have_read;
};
bool subghz_tx_rx_worker_write(SubGhzTxRxWorker* instance, uint8_t* data, size_t size) {
furi_check(instance);
bool ret = false;
size_t stream_tx_free_byte = furi_stream_buffer_spaces_available(instance->stream_tx);
if(size && (stream_tx_free_byte >= size)) {
if(furi_stream_buffer_send(
instance->stream_tx, data, size, SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF) ==
size) {
ret = true;
}
}
return ret;
}
size_t subghz_tx_rx_worker_available(SubGhzTxRxWorker* instance) {
furi_check(instance);
return furi_stream_buffer_bytes_available(instance->stream_rx);
}
size_t subghz_tx_rx_worker_read(SubGhzTxRxWorker* instance, uint8_t* data, size_t size) {
furi_check(instance);
return furi_stream_buffer_receive(instance->stream_rx, data, size, 0);
}
void subghz_tx_rx_worker_set_callback_have_read(
SubGhzTxRxWorker* instance,
SubGhzTxRxWorkerCallbackHaveRead callback,
void* context) {
furi_check(instance);
furi_check(callback);
furi_check(context);
instance->callback_have_read = callback;
instance->context_have_read = context;
}
bool subghz_tx_rx_worker_rx(SubGhzTxRxWorker* instance, uint8_t* data, uint8_t* size) {
uint8_t timeout = 100;
bool ret = false;
if(instance->status != SubGhzTxRxWorkerStatusRx) {
subghz_devices_set_rx(instance->device);
instance->status = SubGhzTxRxWorkerStatusRx;
furi_delay_tick(1);
}
//waiting for reception to complete
while(furi_hal_gpio_read(instance->device_data_gpio)) {
furi_delay_tick(1);
if(!--timeout) {
FURI_LOG_W(TAG, "RX cc1101_g0 timeout");
subghz_devices_flush_rx(instance->device);
subghz_devices_set_rx(instance->device);
break;
}
}
if(subghz_devices_rx_pipe_not_empty(instance->device)) {
FURI_LOG_I(
TAG,
"RSSI: %03.1fdbm LQI: %d",
(double)subghz_devices_get_rssi(instance->device),
subghz_devices_get_lqi(instance->device));
if(subghz_devices_is_rx_data_crc_valid(instance->device)) {
subghz_devices_read_packet(instance->device, data, size);
ret = true;
}
subghz_devices_flush_rx(instance->device);
subghz_devices_set_rx(instance->device);
}
return ret;
}
void subghz_tx_rx_worker_tx(SubGhzTxRxWorker* instance, uint8_t* data, size_t size) {
uint8_t timeout = 200;
if(instance->status != SubGhzTxRxWorkerStatusIDLE) {
subghz_devices_idle(instance->device);
}
subghz_devices_write_packet(instance->device, data, size);
subghz_devices_set_tx(instance->device); //start send
instance->status = SubGhzTxRxWorkerStatusTx;
while(!furi_hal_gpio_read(
instance->device_data_gpio)) { // Wait for GDO0 to be set -> sync transmitted
furi_delay_tick(1);
if(!--timeout) {
FURI_LOG_W(TAG, "TX !cc1101_g0 timeout");
break;
}
}
while(furi_hal_gpio_read(
instance->device_data_gpio)) { // Wait for GDO0 to be cleared -> end of packet
furi_delay_tick(1);
if(!--timeout) {
FURI_LOG_W(TAG, "TX cc1101_g0 timeout");
break;
}
}
subghz_devices_idle(instance->device);
instance->status = SubGhzTxRxWorkerStatusIDLE;
}
/** Worker thread
*
* @param context
* @return exit code
*/
static int32_t subghz_tx_rx_worker_thread(void* context) {
SubGhzTxRxWorker* instance = context;
furi_check(instance->device);
FURI_LOG_I(TAG, "Worker start");
subghz_devices_begin(instance->device);
instance->device_data_gpio = subghz_devices_get_data_gpio(instance->device);
subghz_devices_reset(instance->device);
subghz_devices_idle(instance->device);
subghz_devices_load_preset(instance->device, FuriHalSubGhzPresetGFSK9_99KbAsync, NULL);
furi_hal_gpio_init(instance->device_data_gpio, GpioModeInput, GpioPullNo, GpioSpeedLow);
subghz_devices_set_frequency(instance->device, instance->frequency);
subghz_devices_flush_rx(instance->device);
uint8_t data[SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE + 1] = {0};
size_t size_tx = 0;
uint8_t size_rx[1] = {0};
uint8_t timeout_tx = 0;
bool callback_rx = false;
while(instance->worker_running) {
//transmit
size_tx = furi_stream_buffer_bytes_available(instance->stream_tx);
if(size_tx > 0 && !timeout_tx) {
timeout_tx = 10; //20ms
if(size_tx > SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE) {
furi_stream_buffer_receive(
instance->stream_tx,
&data,
SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE,
SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF);
subghz_tx_rx_worker_tx(instance, data, SUBGHZ_TXRX_WORKER_MAX_TXRX_SIZE);
} else {
//TODO FL-3554: checking that it managed to write all the data to the TX buffer
furi_stream_buffer_receive(
instance->stream_tx, &data, size_tx, SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF);
subghz_tx_rx_worker_tx(instance, data, size_tx);
}
} else {
//receive
if(subghz_tx_rx_worker_rx(instance, data, size_rx)) {
if(furi_stream_buffer_spaces_available(instance->stream_rx) >= size_rx[0]) {
if(instance->callback_have_read &&
furi_stream_buffer_bytes_available(instance->stream_rx) == 0) {
callback_rx = true;
}
//TODO FL-3554: checking that it managed to write all the data to the RX buffer
furi_stream_buffer_send(
instance->stream_rx,
&data,
size_rx[0],
SUBGHZ_TXRX_WORKER_TIMEOUT_READ_WRITE_BUF);
if(callback_rx) {
instance->callback_have_read(instance->context_have_read);
callback_rx = false;
}
} else {
//TODO FL-3555: RX buffer overflow
}
}
}
if(timeout_tx) timeout_tx--;
furi_delay_tick(1);
}
subghz_devices_sleep(instance->device);
subghz_devices_end(instance->device);
FURI_LOG_I(TAG, "Worker stop");
return 0;
}
SubGhzTxRxWorker* subghz_tx_rx_worker_alloc(void) {
SubGhzTxRxWorker* instance = malloc(sizeof(SubGhzTxRxWorker));
instance->thread =
furi_thread_alloc_ex("SubGhzTxRxWorker", 2048, subghz_tx_rx_worker_thread, instance);
instance->stream_tx =
furi_stream_buffer_alloc(sizeof(uint8_t) * SUBGHZ_TXRX_WORKER_BUF_SIZE, sizeof(uint8_t));
instance->stream_rx =
furi_stream_buffer_alloc(sizeof(uint8_t) * SUBGHZ_TXRX_WORKER_BUF_SIZE, sizeof(uint8_t));
instance->status = SubGhzTxRxWorkerStatusIDLE;
instance->worker_stopping = true;
return instance;
}
void subghz_tx_rx_worker_free(SubGhzTxRxWorker* instance) {
furi_check(instance);
furi_check(!instance->worker_running);
furi_stream_buffer_free(instance->stream_tx);
furi_stream_buffer_free(instance->stream_rx);
furi_thread_free(instance->thread);
free(instance);
}
bool subghz_tx_rx_worker_start(
SubGhzTxRxWorker* instance,
const SubGhzDevice* device,
uint32_t frequency) {
furi_check(instance);
furi_check(!instance->worker_running);
bool res = false;
furi_stream_buffer_reset(instance->stream_tx);
furi_stream_buffer_reset(instance->stream_rx);
instance->worker_running = true;
if(furi_hal_subghz_is_tx_allowed(frequency)) {
instance->frequency = frequency;
instance->device = device;
res = true;
}
furi_thread_start(instance->thread);
return res;
}
void subghz_tx_rx_worker_stop(SubGhzTxRxWorker* instance) {
furi_check(instance);
furi_check(instance->worker_running);
instance->worker_running = false;
furi_thread_join(instance->thread);
}
bool subghz_tx_rx_worker_is_running(SubGhzTxRxWorker* instance) {
furi_check(instance);
return instance->worker_running;
}
| 9,032
|
C
|
.c
| 230
| 31.334783
| 100
| 0.639621
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,220
|
subghz_worker.c
|
DarkFlippers_unleashed-firmware/lib/subghz/subghz_worker.c
|
#include "subghz_worker.h"
#include <furi.h>
#define TAG "SubGhzWorker"
struct SubGhzWorker {
FuriThread* thread;
FuriStreamBuffer* stream;
volatile bool running;
volatile bool overrun;
LevelDuration filter_level_duration;
uint16_t filter_duration;
SubGhzWorkerOverrunCallback overrun_callback;
SubGhzWorkerPairCallback pair_callback;
void* context;
};
/** Rx callback timer
*
* @param level received signal level
* @param duration received signal duration
* @param context
*/
void subghz_worker_rx_callback(bool level, uint32_t duration, void* context) {
SubGhzWorker* instance = context;
LevelDuration level_duration = level_duration_make(level, duration);
if(instance->overrun) {
instance->overrun = false;
level_duration = level_duration_reset();
}
size_t ret =
furi_stream_buffer_send(instance->stream, &level_duration, sizeof(LevelDuration), 0);
if(sizeof(LevelDuration) != ret) instance->overrun = true;
}
/** Worker callback thread
*
* @param context
* @return exit code
*/
static int32_t subghz_worker_thread_callback(void* context) {
SubGhzWorker* instance = context;
LevelDuration level_duration;
while(instance->running) {
int ret = furi_stream_buffer_receive(
instance->stream, &level_duration, sizeof(LevelDuration), 10);
if(ret == sizeof(LevelDuration)) {
if(level_duration_is_reset(level_duration)) {
FURI_LOG_E(TAG, "Overrun buffer");
if(instance->overrun_callback) instance->overrun_callback(instance->context);
} else {
bool level = level_duration_get_level(level_duration);
uint32_t duration = level_duration_get_duration(level_duration);
if((duration < instance->filter_duration) ||
(instance->filter_level_duration.level == level)) {
instance->filter_level_duration.duration += duration;
} else if(instance->filter_level_duration.level != level) {
if(instance->pair_callback)
instance->pair_callback(
instance->context,
instance->filter_level_duration.level,
instance->filter_level_duration.duration);
instance->filter_level_duration.duration = duration;
instance->filter_level_duration.level = level;
}
}
}
}
return 0;
}
SubGhzWorker* subghz_worker_alloc(void) {
SubGhzWorker* instance = malloc(sizeof(SubGhzWorker));
instance->thread =
furi_thread_alloc_ex("SubGhzWorker", 2048, subghz_worker_thread_callback, instance);
instance->stream =
furi_stream_buffer_alloc(sizeof(LevelDuration) * 4096, sizeof(LevelDuration));
//setting default filter in us
instance->filter_duration = 30;
return instance;
}
void subghz_worker_free(SubGhzWorker* instance) {
furi_check(instance);
furi_stream_buffer_free(instance->stream);
furi_thread_free(instance->thread);
free(instance);
}
void subghz_worker_set_overrun_callback(
SubGhzWorker* instance,
SubGhzWorkerOverrunCallback callback) {
furi_check(instance);
instance->overrun_callback = callback;
}
void subghz_worker_set_pair_callback(SubGhzWorker* instance, SubGhzWorkerPairCallback callback) {
furi_check(instance);
instance->pair_callback = callback;
}
void subghz_worker_set_context(SubGhzWorker* instance, void* context) {
furi_check(instance);
instance->context = context;
}
void subghz_worker_start(SubGhzWorker* instance) {
furi_check(instance);
furi_check(!instance->running);
instance->running = true;
furi_thread_start(instance->thread);
}
void subghz_worker_stop(SubGhzWorker* instance) {
furi_check(instance);
furi_check(instance->running);
instance->running = false;
furi_thread_join(instance->thread);
}
bool subghz_worker_is_running(SubGhzWorker* instance) {
furi_check(instance);
return instance->running;
}
void subghz_worker_set_filter(SubGhzWorker* instance, uint16_t timeout) {
furi_check(instance);
instance->filter_duration = timeout;
}
| 4,302
|
C
|
.c
| 116
| 30.241379
| 97
| 0.674693
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,221
|
registry.c
|
DarkFlippers_unleashed-firmware/lib/subghz/registry.c
|
#include "registry.h"
const SubGhzProtocol* subghz_protocol_registry_get_by_name(
const SubGhzProtocolRegistry* protocol_registry,
const char* name) {
furi_check(protocol_registry);
for(size_t i = 0; i < subghz_protocol_registry_count(protocol_registry); i++) {
if(strcmp(name, protocol_registry->items[i]->name) == 0) {
return protocol_registry->items[i];
}
}
return NULL;
}
const SubGhzProtocol* subghz_protocol_registry_get_by_index(
const SubGhzProtocolRegistry* protocol_registry,
size_t index) {
furi_check(protocol_registry);
if(index < subghz_protocol_registry_count(protocol_registry)) {
return protocol_registry->items[index];
} else {
return NULL;
}
}
size_t subghz_protocol_registry_count(const SubGhzProtocolRegistry* protocol_registry) {
furi_check(protocol_registry);
return protocol_registry->size;
}
| 921
|
C
|
.c
| 26
| 30.423077
| 88
| 0.707071
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,223
|
subghz_file_encoder_worker.c
|
DarkFlippers_unleashed-firmware/lib/subghz/subghz_file_encoder_worker.c
|
#include "subghz_file_encoder_worker.h"
#include <toolbox/stream/stream.h>
#include <flipper_format/flipper_format.h>
#include <flipper_format/flipper_format_i.h>
#include <lib/subghz/devices/devices.h>
#include <lib/toolbox/strint.h>
#define TAG "SubGhzFileEncoderWorker"
#define SUBGHZ_FILE_ENCODER_LOAD 512
struct SubGhzFileEncoderWorker {
FuriThread* thread;
FuriStreamBuffer* stream;
Storage* storage;
FlipperFormat* flipper_format;
volatile bool worker_running;
volatile bool worker_stopping;
bool is_storage_slow;
FuriString* str_data;
FuriString* file_path;
const SubGhzDevice* device;
SubGhzFileEncoderWorkerCallbackEnd callback_end;
void* context_end;
};
void subghz_file_encoder_worker_callback_end(
SubGhzFileEncoderWorker* instance,
SubGhzFileEncoderWorkerCallbackEnd callback_end,
void* context_end) {
furi_assert(instance);
furi_assert(callback_end);
instance->callback_end = callback_end;
instance->context_end = context_end;
}
void subghz_file_encoder_worker_add_level_duration(
SubGhzFileEncoderWorker* instance,
int32_t duration) {
size_t ret = furi_stream_buffer_send(instance->stream, &duration, sizeof(int32_t), 100);
if(sizeof(int32_t) != ret) FURI_LOG_E(TAG, "Invalid add duration in the stream");
}
bool subghz_file_encoder_worker_data_parse(SubGhzFileEncoderWorker* instance, const char* strStart) {
// Line sample: "RAW_Data: -1, 2, -2..."
// Look for the key in the line
char* str = strstr(strStart, "RAW_Data: ");
bool res = false;
if(str) {
// Skip key
str = strchr(str, ' ');
// Parse next element
int32_t duration;
while(strint_to_int32(str, &str, &duration, 10) == StrintParseNoError) {
if((duration < -1000000) || (duration > 1000000)) {
if(duration > 0) {
subghz_file_encoder_worker_add_level_duration(instance, (int32_t)100);
} else {
subghz_file_encoder_worker_add_level_duration(instance, (int32_t)-100);
}
//FURI_LOG_I("PARSE", "Number overflow - %d", duration);
} else {
subghz_file_encoder_worker_add_level_duration(instance, duration);
}
if(*str == ',') str++; // could also be `\0`
}
res = true;
}
return res;
}
void subghz_file_encoder_worker_get_text_progress(
SubGhzFileEncoderWorker* instance,
FuriString* output) {
UNUSED(output);
Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
size_t total_size = stream_size(stream);
size_t current_offset = stream_tell(stream);
size_t buffer_avail = furi_stream_buffer_bytes_available(instance->stream);
furi_string_printf(output, "%03u%%", 100 * (current_offset - buffer_avail) / total_size);
}
LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
furi_assert(context);
SubGhzFileEncoderWorker* instance = context;
int32_t duration;
int ret = furi_stream_buffer_receive(instance->stream, &duration, sizeof(int32_t), 0);
if(ret == sizeof(int32_t)) {
LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
if(duration < 0) {
level_duration = level_duration_make(false, -duration);
} else if(duration > 0) {
level_duration = level_duration_make(true, duration);
} else if(duration == 0) { //-V547
level_duration = level_duration_reset();
FURI_LOG_I(TAG, "Stop transmission");
instance->worker_stopping = true;
}
return level_duration;
} else {
instance->is_storage_slow = true;
return level_duration_wait();
}
}
/** Worker thread
*
* @param context
* @return exit code
*/
static int32_t subghz_file_encoder_worker_thread(void* context) {
SubGhzFileEncoderWorker* instance = context;
FURI_LOG_I(TAG, "Worker start");
bool res = false;
instance->is_storage_slow = false;
Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
do {
if(!flipper_format_file_open_existing(
instance->flipper_format, furi_string_get_cstr(instance->file_path))) {
FURI_LOG_E(
TAG,
"Unable to open file for read: %s",
furi_string_get_cstr(instance->file_path));
break;
}
if(!flipper_format_read_string(instance->flipper_format, "Protocol", instance->str_data)) {
FURI_LOG_E(TAG, "Missing Protocol");
break;
}
//skip the end of the previous line "\n"
stream_seek(stream, 1, StreamOffsetFromCurrent);
res = true;
instance->worker_stopping = false;
FURI_LOG_I(TAG, "Start transmission");
} while(0);
while(res && instance->worker_running) {
size_t stream_free_byte = furi_stream_buffer_spaces_available(instance->stream);
if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
if(stream_read_line(stream, instance->str_data)) {
furi_string_trim(instance->str_data);
if(!subghz_file_encoder_worker_data_parse(
instance, furi_string_get_cstr(instance->str_data))) {
subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
break;
}
} else {
subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
break;
}
} else {
furi_delay_ms(1);
}
}
//waiting for the end of the transfer
if(instance->is_storage_slow) {
FURI_LOG_E(TAG, "Storage is slow");
}
FURI_LOG_I(TAG, "End read file");
while(instance->device && !subghz_devices_is_async_complete_tx(instance->device) &&
instance->worker_running) {
furi_delay_ms(5);
}
FURI_LOG_I(TAG, "End transmission");
while(instance->worker_running) {
if(instance->worker_stopping) {
if(instance->callback_end) instance->callback_end(instance->context_end);
}
furi_delay_ms(50);
}
flipper_format_file_close(instance->flipper_format);
FURI_LOG_I(TAG, "Worker stop");
return 0;
}
SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc(void) {
SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
instance->thread =
furi_thread_alloc_ex("SubGhzFEWorker", 2048, subghz_file_encoder_worker_thread, instance);
instance->stream = furi_stream_buffer_alloc(sizeof(int32_t) * 2048, sizeof(int32_t));
instance->storage = furi_record_open(RECORD_STORAGE);
instance->flipper_format = flipper_format_file_alloc(instance->storage);
instance->str_data = furi_string_alloc();
instance->file_path = furi_string_alloc();
instance->worker_stopping = true;
return instance;
}
void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
furi_assert(instance);
furi_stream_buffer_free(instance->stream);
furi_thread_free(instance->thread);
furi_string_free(instance->str_data);
furi_string_free(instance->file_path);
flipper_format_free(instance->flipper_format);
furi_record_close(RECORD_STORAGE);
free(instance);
}
bool subghz_file_encoder_worker_start(
SubGhzFileEncoderWorker* instance,
const char* file_path,
const char* radio_device_name) {
furi_assert(instance);
furi_assert(!instance->worker_running);
furi_stream_buffer_reset(instance->stream);
furi_string_set(instance->file_path, file_path);
if(radio_device_name) {
instance->device = subghz_devices_get_by_name(radio_device_name);
}
instance->worker_running = true;
furi_thread_start(instance->thread);
return true;
}
void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
furi_assert(instance);
furi_assert(instance->worker_running);
instance->worker_running = false;
furi_thread_join(instance->thread);
}
bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
furi_assert(instance);
return instance->worker_running;
}
| 8,349
|
C
|
.c
| 211
| 32.488152
| 101
| 0.654977
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,227
|
transmitter.c
|
DarkFlippers_unleashed-firmware/lib/subghz/transmitter.c
|
#include "transmitter.h"
#include "protocols/base.h"
#include "registry.h"
struct SubGhzTransmitter {
const SubGhzProtocol* protocol;
SubGhzProtocolEncoderBase* protocol_instance;
};
SubGhzTransmitter*
subghz_transmitter_alloc_init(SubGhzEnvironment* environment, const char* protocol_name) {
SubGhzTransmitter* instance = NULL;
const SubGhzProtocolRegistry* protocol_registry_items =
subghz_environment_get_protocol_registry(environment);
const SubGhzProtocol* protocol =
subghz_protocol_registry_get_by_name(protocol_registry_items, protocol_name);
if(protocol && protocol->encoder && protocol->encoder->alloc) {
instance = malloc(sizeof(SubGhzTransmitter));
instance->protocol = protocol;
instance->protocol_instance = instance->protocol->encoder->alloc(environment);
}
return instance;
}
void subghz_transmitter_free(SubGhzTransmitter* instance) {
furi_check(instance);
instance->protocol->encoder->free(instance->protocol_instance);
free(instance);
}
SubGhzProtocolEncoderBase* subghz_transmitter_get_protocol_instance(SubGhzTransmitter* instance) {
furi_check(instance);
return instance->protocol_instance;
}
bool subghz_transmitter_stop(SubGhzTransmitter* instance) {
furi_check(instance);
bool ret = false;
if(instance->protocol && instance->protocol->encoder && instance->protocol->encoder->stop) {
instance->protocol->encoder->stop(instance->protocol_instance);
ret = true;
}
return ret;
}
SubGhzProtocolStatus
subghz_transmitter_deserialize(SubGhzTransmitter* instance, FlipperFormat* flipper_format) {
furi_check(instance);
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
if(instance->protocol && instance->protocol->encoder &&
instance->protocol->encoder->deserialize) {
ret =
instance->protocol->encoder->deserialize(instance->protocol_instance, flipper_format);
}
return ret;
}
LevelDuration subghz_transmitter_yield(void* context) {
SubGhzTransmitter* instance = context;
return instance->protocol->encoder->yield(instance->protocol_instance);
}
| 2,169
|
C
|
.c
| 54
| 35.444444
| 98
| 0.746793
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,230
|
environment.c
|
DarkFlippers_unleashed-firmware/lib/subghz/environment.c
|
#include "environment.h"
#include "registry.h"
struct SubGhzEnvironment {
SubGhzKeystore* keystore;
const SubGhzProtocolRegistry* protocol_registry;
const char* nice_flor_s_rainbow_table_file_name;
const char* alutech_at_4n_rainbow_table_file_name;
const char* mfname;
uint8_t kl_type;
};
SubGhzEnvironment* subghz_environment_alloc(void) {
SubGhzEnvironment* instance = malloc(sizeof(SubGhzEnvironment));
instance->keystore = subghz_keystore_alloc();
instance->protocol_registry = NULL;
instance->nice_flor_s_rainbow_table_file_name = NULL;
instance->alutech_at_4n_rainbow_table_file_name = NULL;
instance->mfname = "";
instance->kl_type = 0;
return instance;
}
void subghz_environment_free(SubGhzEnvironment* instance) {
furi_check(instance);
instance->protocol_registry = NULL;
instance->nice_flor_s_rainbow_table_file_name = NULL;
instance->alutech_at_4n_rainbow_table_file_name = NULL;
subghz_keystore_free(instance->keystore);
free(instance);
}
bool subghz_environment_load_keystore(SubGhzEnvironment* instance, const char* filename) {
furi_check(instance);
return subghz_keystore_load(instance->keystore, filename);
}
SubGhzKeystore* subghz_environment_get_keystore(SubGhzEnvironment* instance) {
furi_check(instance);
return instance->keystore;
}
void subghz_environment_set_came_atomo_rainbow_table_file_name(
SubGhzEnvironment* instance,
const char* filename) {
UNUSED(instance);
UNUSED(filename);
// Do nothing :)
return;
}
const char*
subghz_environment_get_came_atomo_rainbow_table_file_name(SubGhzEnvironment* instance) {
UNUSED(instance);
// No table, sorry
return "";
}
void subghz_environment_set_alutech_at_4n_rainbow_table_file_name(
SubGhzEnvironment* instance,
const char* filename) {
furi_check(instance);
instance->alutech_at_4n_rainbow_table_file_name = filename;
}
const char*
subghz_environment_get_alutech_at_4n_rainbow_table_file_name(SubGhzEnvironment* instance) {
furi_check(instance);
return instance->alutech_at_4n_rainbow_table_file_name;
}
void subghz_environment_set_nice_flor_s_rainbow_table_file_name(
SubGhzEnvironment* instance,
const char* filename) {
furi_check(instance);
instance->nice_flor_s_rainbow_table_file_name = filename;
}
const char*
subghz_environment_get_nice_flor_s_rainbow_table_file_name(SubGhzEnvironment* instance) {
furi_check(instance);
return instance->nice_flor_s_rainbow_table_file_name;
}
void subghz_environment_set_protocol_registry(
SubGhzEnvironment* instance,
const SubGhzProtocolRegistry* protocol_registry_items) {
furi_check(instance);
const SubGhzProtocolRegistry* protocol_registry = protocol_registry_items;
instance->protocol_registry = protocol_registry;
}
const SubGhzProtocolRegistry*
subghz_environment_get_protocol_registry(SubGhzEnvironment* instance) {
furi_check(instance);
furi_check(instance->protocol_registry);
return instance->protocol_registry;
}
const char*
subghz_environment_get_protocol_name_registry(SubGhzEnvironment* instance, size_t idx) {
furi_check(instance);
furi_check(instance->protocol_registry);
const SubGhzProtocol* protocol =
subghz_protocol_registry_get_by_index(instance->protocol_registry, idx);
if(protocol != NULL) {
return protocol->name;
} else {
return NULL;
}
}
void subghz_environment_reset_keeloq(SubGhzEnvironment* instance) {
furi_assert(instance);
subghz_keystore_reset_kl(instance->keystore);
}
| 3,633
|
C
|
.c
| 101
| 31.861386
| 95
| 0.748716
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,232
|
marantec24.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/marantec24.c
|
#include "marantec24.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolMarantec24"
static const SubGhzBlockConst subghz_protocol_marantec24_const = {
.te_short = 800,
.te_long = 1600,
.te_delta = 200,
.min_count_bit_for_found = 24,
};
struct SubGhzProtocolDecoderMarantec24 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderMarantec24 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
Marantec24DecoderStepReset = 0,
Marantec24DecoderStepSaveDuration,
Marantec24DecoderStepCheckDuration,
} Marantec24DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_marantec24_decoder = {
.alloc = subghz_protocol_decoder_marantec24_alloc,
.free = subghz_protocol_decoder_marantec24_free,
.feed = subghz_protocol_decoder_marantec24_feed,
.reset = subghz_protocol_decoder_marantec24_reset,
.get_hash_data = subghz_protocol_decoder_marantec24_get_hash_data,
.serialize = subghz_protocol_decoder_marantec24_serialize,
.deserialize = subghz_protocol_decoder_marantec24_deserialize,
.get_string = subghz_protocol_decoder_marantec24_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_marantec24_encoder = {
.alloc = subghz_protocol_encoder_marantec24_alloc,
.free = subghz_protocol_encoder_marantec24_free,
.deserialize = subghz_protocol_encoder_marantec24_deserialize,
.stop = subghz_protocol_encoder_marantec24_stop,
.yield = subghz_protocol_encoder_marantec24_yield,
};
const SubGhzProtocol subghz_protocol_marantec24 = {
.name = SUBGHZ_PROTOCOL_MARANTEC24_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_marantec24_decoder,
.encoder = &subghz_protocol_marantec24_encoder,
};
void* subghz_protocol_encoder_marantec24_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderMarantec24* instance = malloc(sizeof(SubGhzProtocolEncoderMarantec24));
instance->base.protocol = &subghz_protocol_marantec24;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_marantec24_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderMarantec24* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderMarantec24 instance
*/
static void
subghz_protocol_encoder_marantec24_get_upload(SubGhzProtocolEncoderMarantec24* instance) {
furi_assert(instance);
size_t index = 0;
// Send key and GAP
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
// Send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_marantec24_const.te_short);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_marantec24_const.te_long * 9 +
subghz_protocol_marantec24_const.te_short);
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_marantec24_const.te_long * 2);
}
} else {
// Send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_marantec24_const.te_long);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_marantec24_const.te_long * 9 +
subghz_protocol_marantec24_const.te_short);
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_marantec24_const.te_short * 3);
}
}
}
instance->encoder.size_upload = index;
return;
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_marantec24_check_remote_controller(SubGhzBlockGeneric* instance) {
instance->serial = instance->data >> 4;
instance->btn = instance->data & 0xF;
}
SubGhzProtocolStatus
subghz_protocol_encoder_marantec24_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderMarantec24* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_marantec24_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_marantec24_check_remote_controller(&instance->generic);
subghz_protocol_encoder_marantec24_get_upload(instance);
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_marantec24_stop(void* context) {
SubGhzProtocolEncoderMarantec24* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_marantec24_yield(void* context) {
SubGhzProtocolEncoderMarantec24* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_marantec24_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderMarantec24* instance = malloc(sizeof(SubGhzProtocolDecoderMarantec24));
instance->base.protocol = &subghz_protocol_marantec24;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_marantec24_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
free(instance);
}
void subghz_protocol_decoder_marantec24_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
instance->decoder.parser_step = Marantec24DecoderStepReset;
}
void subghz_protocol_decoder_marantec24_feed(void* context, bool level, volatile uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
// Key samples
// 101011000000010111001000 = AC05C8
// 101011000000010111000100 = AC05C4
// 101011000000010111001100 = AC05CC
// 101011000000010111000000 = AC05C0
switch(instance->decoder.parser_step) {
case Marantec24DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_marantec24_const.te_long * 9) <
subghz_protocol_marantec24_const.te_delta * 4)) {
//Found GAP
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = Marantec24DecoderStepSaveDuration;
}
break;
case Marantec24DecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = Marantec24DecoderStepCheckDuration;
} else {
instance->decoder.parser_step = Marantec24DecoderStepReset;
}
break;
case Marantec24DecoderStepCheckDuration:
if(!level) {
// Bit 0 is long and short x2 timing = 1600us HIGH (te_last) and 2400us LOW
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_marantec24_const.te_long) <
subghz_protocol_marantec24_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_marantec24_const.te_short * 3) <
subghz_protocol_marantec24_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = Marantec24DecoderStepSaveDuration;
// Bit 1 is short and long x2 timing = 800us HIGH (te_last) and 3200us LOW
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_marantec24_const.te_short) <
subghz_protocol_marantec24_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_marantec24_const.te_long * 2) <
subghz_protocol_marantec24_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = Marantec24DecoderStepSaveDuration;
} else if(
// End of the key
DURATION_DIFF(duration, subghz_protocol_marantec24_const.te_long * 9) <
subghz_protocol_marantec24_const.te_delta * 4) {
//Found next GAP and add bit 0 or 1 (only bit 0 was found on the remotes)
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_marantec24_const.te_long) <
subghz_protocol_marantec24_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_marantec24_const.te_long * 9) <
subghz_protocol_marantec24_const.te_delta * 4)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
}
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_marantec24_const.te_short) <
subghz_protocol_marantec24_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_marantec24_const.te_long * 9) <
subghz_protocol_marantec24_const.te_delta * 4)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
}
// If got 24 bits key reading is finished
if(instance->decoder.decode_count_bit ==
subghz_protocol_marantec24_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = Marantec24DecoderStepReset;
} else {
instance->decoder.parser_step = Marantec24DecoderStepReset;
}
} else {
instance->decoder.parser_step = Marantec24DecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_marantec24_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_marantec24_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_marantec24_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_marantec24_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_marantec24_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderMarantec24* instance = context;
subghz_protocol_marantec24_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key: 0x%06lX\r\n"
"Serial: 0x%05lX\r\n"
"Btn: %01X",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFF),
instance->generic.serial,
instance->generic.btn);
}
| 13,573
|
C
|
.c
| 299
| 36.859532
| 101
| 0.671302
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,233
|
hay21.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/hay21.c
|
#include "hay21.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
#define TAG "SubGhzProtocolHay21"
static const SubGhzBlockConst subghz_protocol_hay21_const = {
.te_short = 300,
.te_long = 700,
.te_delta = 150,
.min_count_bit_for_found = 21,
};
struct SubGhzProtocolDecoderHay21 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderHay21 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
Hay21DecoderStepReset = 0,
Hay21DecoderStepSaveDuration,
Hay21DecoderStepCheckDuration,
} Hay21DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_hay21_decoder = {
.alloc = subghz_protocol_decoder_hay21_alloc,
.free = subghz_protocol_decoder_hay21_free,
.feed = subghz_protocol_decoder_hay21_feed,
.reset = subghz_protocol_decoder_hay21_reset,
.get_hash_data = subghz_protocol_decoder_hay21_get_hash_data,
.serialize = subghz_protocol_decoder_hay21_serialize,
.deserialize = subghz_protocol_decoder_hay21_deserialize,
.get_string = subghz_protocol_decoder_hay21_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_hay21_encoder = {
.alloc = subghz_protocol_encoder_hay21_alloc,
.free = subghz_protocol_encoder_hay21_free,
.deserialize = subghz_protocol_encoder_hay21_deserialize,
.stop = subghz_protocol_encoder_hay21_stop,
.yield = subghz_protocol_encoder_hay21_yield,
};
const SubGhzProtocol subghz_protocol_hay21 = {
.name = SUBGHZ_PROTOCOL_HAY21_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_hay21_decoder,
.encoder = &subghz_protocol_hay21_encoder,
};
void* subghz_protocol_encoder_hay21_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderHay21* instance = malloc(sizeof(SubGhzProtocolEncoderHay21));
instance->base.protocol = &subghz_protocol_hay21;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_hay21_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderHay21* instance = context;
free(instance->encoder.upload);
free(instance);
}
// Get custom button code
static uint8_t subghz_protocol_hay21_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x5A:
btn = 0xC3;
break;
case 0xC3:
btn = 0x5A;
break;
case 0x88:
btn = 0x5A;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x5A:
btn = 0x88;
break;
case 0xC3:
btn = 0x88;
break;
case 0x88:
btn = 0xC3;
break;
default:
break;
}
}
return btn;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderHay21 instance
*/
static void subghz_protocol_encoder_hay21_get_upload(SubGhzProtocolEncoderHay21* instance) {
furi_assert(instance);
// Generate new key using custom or default button
instance->generic.btn = subghz_protocol_hay21_get_btn_code();
// Counter increment
if(instance->generic.cnt < 0xF) {
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xF) {
instance->generic.cnt = 0;
} else {
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
}
} else if(instance->generic.cnt >= 0xF) {
instance->generic.cnt = 0;
}
// Reconstruction of the data
instance->generic.data =
((uint64_t)instance->generic.btn << 13 | (uint64_t)instance->generic.serial << 5 |
instance->generic.cnt << 1) |
0b1;
size_t index = 0;
// Send key and GAP between parcels
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
// Send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hay21_const.te_long);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hay21_const.te_long * 6);
} else {
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hay21_const.te_short);
}
} else {
// Send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hay21_const.te_short);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hay21_const.te_long * 6);
} else {
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hay21_const.te_long);
}
}
}
instance->encoder.size_upload = index;
return;
}
/**
* Analysis of received data and parsing serial number
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_hay21_remote_controller(SubGhzBlockGeneric* instance) {
instance->btn = (instance->data >> 13) & 0xFF;
instance->serial = (instance->data >> 5) & 0xFF;
instance->cnt = (instance->data >> 1) & 0xF;
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(2);
// Hay21 Decoder
// 09.2024 - @xMasterX (MMX)
// Key samples (inverted)
// button serial CNT (goes lower since 0/1 are inverted)
//14A84A = 000 10100101 01000010 0101 0 (cnt 5)
//14A848 = 000 10100101 01000010 0100 0 (cnt 4)
//14A846 = 000 10100101 01000010 0011 0 (cnt 3)
//14A844 = 000 10100101 01000010 0010 0 (cnt 2)
//14A842 = 000 10100101 01000010 0001 0 (cnt 1)
//14A840 = 000 10100101 01000010 0000 0 (cnt 0)
//14A85E = 000 10100101 01000010 1111 0 (cnt F)
//14A85C = 000 10100101 01000010 1110 0 (cnt E)
//14A85A = 000 10100101 01000010 1101 0 (cnt D)
//14A858 = 000 10100101 01000010 1100 0 (cnt C)
//14A856 = 000 10100101 01000010 1011 0 (cnt B)
// 0xA5 (Labeled as On/Off on the remote board)
// 0x3C (Labeled as Mode on the remote board)
// 0x42 (Serial)
// BTN Serial CNT
//078854 = 000 00111100 01000010 1010 0 (cnt A)
//078852 = 000 00111100 01000010 1001 0 (cnt 9)
//078850 = 000 00111100 01000010 1000 0 (cnt 8)
//07884E = 000 00111100 01000010 0111 0 (cnt 7)
// Inverted back
//1877B9 = 000 11000011 10111101 1100 1
//1877BB = 000 11000011 10111101 1101 1
//1877BD = 000 11000011 10111101 1110 1
//0B57BF = 000 01011010 10111101 1111 1
}
SubGhzProtocolStatus
subghz_protocol_encoder_hay21_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderHay21* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_hay21_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_hay21_remote_controller(&instance->generic);
subghz_protocol_encoder_hay21_get_upload(instance);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_hay21_stop(void* context) {
SubGhzProtocolEncoderHay21* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_hay21_yield(void* context) {
SubGhzProtocolEncoderHay21* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_hay21_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderHay21* instance = malloc(sizeof(SubGhzProtocolDecoderHay21));
instance->base.protocol = &subghz_protocol_hay21;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_hay21_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
free(instance);
}
void subghz_protocol_decoder_hay21_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
instance->decoder.parser_step = Hay21DecoderStepReset;
}
void subghz_protocol_decoder_hay21_feed(void* context, bool level, volatile uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
switch(instance->decoder.parser_step) {
case Hay21DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_hay21_const.te_long * 6) <
subghz_protocol_hay21_const.te_delta * 3)) {
//Found GAP
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = Hay21DecoderStepSaveDuration;
}
break;
case Hay21DecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = Hay21DecoderStepCheckDuration;
} else {
instance->decoder.parser_step = Hay21DecoderStepReset;
}
break;
case Hay21DecoderStepCheckDuration:
if(!level) {
// Bit 1 is long + short timing
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hay21_const.te_long) <
subghz_protocol_hay21_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_hay21_const.te_short) <
subghz_protocol_hay21_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = Hay21DecoderStepSaveDuration;
// Bit 0 is short + long timing
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hay21_const.te_short) <
subghz_protocol_hay21_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_hay21_const.te_long) <
subghz_protocol_hay21_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = Hay21DecoderStepSaveDuration;
} else if(
// End of the key
DURATION_DIFF(duration, subghz_protocol_hay21_const.te_long * 6) <
subghz_protocol_hay21_const.te_delta * 2) {
//Found next GAP and add bit 0 or 1
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hay21_const.te_long) <
subghz_protocol_hay21_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
}
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hay21_const.te_short) <
subghz_protocol_hay21_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
}
// If got 21 bits key reading is finished
if(instance->decoder.decode_count_bit ==
subghz_protocol_hay21_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = Hay21DecoderStepReset;
} else {
instance->decoder.parser_step = Hay21DecoderStepReset;
}
} else {
instance->decoder.parser_step = Hay21DecoderStepReset;
}
break;
}
}
/**
* Get button name.
* @param btn Button number, 4 bit
*/
static const char* subghz_protocol_hay21_get_button_name(uint8_t btn) {
const char* btn_name;
switch(btn) {
case 0x5A:
btn_name = "On/Off";
break;
case 0xC3:
btn_name = "Mode";
break;
case 0x88:
btn_name = "Hold";
break;
default:
btn_name = "Unknown";
break;
}
return btn_name;
}
uint8_t subghz_protocol_decoder_hay21_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_hay21_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_hay21_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_hay21_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_hay21_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderHay21* instance = context;
// Parse serial, button, counter
subghz_protocol_hay21_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s - %dbit\r\n"
"Key: 0x%06lX\r\n"
"Serial: 0x%02X\r\n"
"Btn: 0x%01X - %s\r\n"
"Cnt: 0x%01X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
(uint8_t)(instance->generic.serial & 0xFF),
instance->generic.btn,
subghz_protocol_hay21_get_button_name(instance->generic.btn),
(uint8_t)(instance->generic.cnt & 0xF));
}
| 16,762
|
C
|
.c
| 410
| 33.039024
| 100
| 0.642875
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,234
|
ansonic.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/ansonic.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_ANSONIC_NAME "Ansonic"
typedef struct SubGhzProtocolDecoderAnsonic SubGhzProtocolDecoderAnsonic;
typedef struct SubGhzProtocolEncoderAnsonic SubGhzProtocolEncoderAnsonic;
extern const SubGhzProtocolDecoder subghz_protocol_ansonic_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_ansonic_encoder;
extern const SubGhzProtocol subghz_protocol_ansonic;
/**
* Allocate SubGhzProtocolEncoderAnsonic.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderAnsonic* pointer to a SubGhzProtocolEncoderAnsonic instance
*/
void* subghz_protocol_encoder_ansonic_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderAnsonic.
* @param context Pointer to a SubGhzProtocolEncoderAnsonic instance
*/
void subghz_protocol_encoder_ansonic_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderAnsonic instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
SubGhzProtocolStatus
subghz_protocol_encoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderAnsonic instance
*/
void subghz_protocol_encoder_ansonic_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderAnsonic instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_ansonic_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderAnsonic.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderAnsonic* pointer to a SubGhzProtocolDecoderAnsonic instance
*/
void* subghz_protocol_decoder_ansonic_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderAnsonic.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
*/
void subghz_protocol_decoder_ansonic_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderAnsonic.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
*/
void subghz_protocol_decoder_ansonic_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_ansonic_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_ansonic_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderAnsonic.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
SubGhzProtocolStatus subghz_protocol_decoder_ansonic_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderAnsonic.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
SubGhzProtocolStatus
subghz_protocol_decoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderAnsonic instance
* @param output Resulting text
*/
void subghz_protocol_decoder_ansonic_get_string(void* context, FuriString* output);
| 3,857
|
C
|
.c
| 92
| 39.934783
| 94
| 0.819637
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,235
|
marantec24.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/marantec24.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_MARANTEC24_NAME "Marantec24"
typedef struct SubGhzProtocolDecoderMarantec24 SubGhzProtocolDecoderMarantec24;
typedef struct SubGhzProtocolEncoderMarantec24 SubGhzProtocolEncoderMarantec24;
extern const SubGhzProtocolDecoder subghz_protocol_marantec24_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_marantec24_encoder;
extern const SubGhzProtocol subghz_protocol_marantec24;
/**
* Allocate SubGhzProtocolEncoderMarantec24.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderMarantec24* pointer to a SubGhzProtocolEncoderMarantec24 instance
*/
void* subghz_protocol_encoder_marantec24_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderMarantec24.
* @param context Pointer to a SubGhzProtocolEncoderMarantec24 instance
*/
void subghz_protocol_encoder_marantec24_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderMarantec24 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_marantec24_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderMarantec24 instance
*/
void subghz_protocol_encoder_marantec24_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderMarantec24 instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_marantec24_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderMarantec24.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderMarantec24* pointer to a SubGhzProtocolDecoderMarantec24 instance
*/
void* subghz_protocol_decoder_marantec24_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderMarantec24.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
*/
void subghz_protocol_decoder_marantec24_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderMarantec24.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
*/
void subghz_protocol_decoder_marantec24_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_marantec24_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_marantec24_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderMarantec24.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_marantec24_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderMarantec24.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_marantec24_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderMarantec24 instance
* @param output Resulting text
*/
void subghz_protocol_decoder_marantec24_get_string(void* context, FuriString* output);
| 3,962
|
C
|
.c
| 92
| 41.076087
| 97
| 0.82611
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,236
|
holtek_ht12x.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/holtek_ht12x.c
|
#include "holtek_ht12x.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
/*
* Help
* https://www.holtek.com/documents/10179/116711/HT12A_Ev130.pdf
*
*/
#define TAG "SubGhzProtocolHoltekHt12x"
#define DIP_PATTERN "%c%c%c%c%c%c%c%c"
#define CNT_TO_DIP(dip) \
(dip & 0x0080 ? '0' : '1'), (dip & 0x0040 ? '0' : '1'), (dip & 0x0020 ? '0' : '1'), \
(dip & 0x0010 ? '0' : '1'), (dip & 0x0008 ? '0' : '1'), (dip & 0x0004 ? '0' : '1'), \
(dip & 0x0002 ? '0' : '1'), (dip & 0x0001 ? '0' : '1')
static const SubGhzBlockConst subghz_protocol_holtek_th12x_const = {
.te_short = 320,
.te_long = 640,
.te_delta = 200,
.min_count_bit_for_found = 12,
};
struct SubGhzProtocolDecoderHoltek_HT12X {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint32_t te;
uint32_t last_data;
};
struct SubGhzProtocolEncoderHoltek_HT12X {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
uint32_t te;
};
typedef enum {
Holtek_HT12XDecoderStepReset = 0,
Holtek_HT12XDecoderStepFoundStartBit,
Holtek_HT12XDecoderStepSaveDuration,
Holtek_HT12XDecoderStepCheckDuration,
} Holtek_HT12XDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_holtek_th12x_decoder = {
.alloc = subghz_protocol_decoder_holtek_th12x_alloc,
.free = subghz_protocol_decoder_holtek_th12x_free,
.feed = subghz_protocol_decoder_holtek_th12x_feed,
.reset = subghz_protocol_decoder_holtek_th12x_reset,
.get_hash_data = subghz_protocol_decoder_holtek_th12x_get_hash_data,
.serialize = subghz_protocol_decoder_holtek_th12x_serialize,
.deserialize = subghz_protocol_decoder_holtek_th12x_deserialize,
.get_string = subghz_protocol_decoder_holtek_th12x_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_holtek_th12x_encoder = {
.alloc = subghz_protocol_encoder_holtek_th12x_alloc,
.free = subghz_protocol_encoder_holtek_th12x_free,
.deserialize = subghz_protocol_encoder_holtek_th12x_deserialize,
.stop = subghz_protocol_encoder_holtek_th12x_stop,
.yield = subghz_protocol_encoder_holtek_th12x_yield,
};
const SubGhzProtocol subghz_protocol_holtek_th12x = {
.name = SUBGHZ_PROTOCOL_HOLTEK_HT12X_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_holtek_th12x_decoder,
.encoder = &subghz_protocol_holtek_th12x_encoder,
};
void* subghz_protocol_encoder_holtek_th12x_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderHoltek_HT12X* instance =
malloc(sizeof(SubGhzProtocolEncoderHoltek_HT12X));
instance->base.protocol = &subghz_protocol_holtek_th12x;
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_running = false;
return instance;
}
void subghz_protocol_encoder_holtek_th12x_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderHoltek_HT12X* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderHoltek_HT12X instance
* @return true On success
*/
static bool
subghz_protocol_encoder_holtek_th12x_get_upload(SubGhzProtocolEncoderHoltek_HT12X* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te * 36);
//Send start bit
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)instance->te * 2);
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
} else {
//send bit 0
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)instance->te * 2);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_holtek_th12x_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderHoltek_HT12X* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_holtek_th12x_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
FURI_LOG_E(TAG, "Missing TE");
ret = SubGhzProtocolStatusErrorParserTe;
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_holtek_th12x_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_holtek_th12x_stop(void* context) {
SubGhzProtocolEncoderHoltek_HT12X* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_holtek_th12x_yield(void* context) {
SubGhzProtocolEncoderHoltek_HT12X* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_holtek_th12x_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderHoltek_HT12X* instance =
malloc(sizeof(SubGhzProtocolDecoderHoltek_HT12X));
instance->base.protocol = &subghz_protocol_holtek_th12x;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_holtek_th12x_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
free(instance);
}
void subghz_protocol_decoder_holtek_th12x_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
}
void subghz_protocol_decoder_holtek_th12x_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
switch(instance->decoder.parser_step) {
case Holtek_HT12XDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_short * 36) <
subghz_protocol_holtek_th12x_const.te_delta * 36)) {
//Found Preambula
instance->decoder.parser_step = Holtek_HT12XDecoderStepFoundStartBit;
}
break;
case Holtek_HT12XDecoderStepFoundStartBit:
if((level) && (DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_short) <
subghz_protocol_holtek_th12x_const.te_delta)) {
//Found StartBit
instance->decoder.parser_step = Holtek_HT12XDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->te = duration;
} else {
instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
}
break;
case Holtek_HT12XDecoderStepSaveDuration:
//save duration
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_holtek_th12x_const.te_short * 10 +
subghz_protocol_holtek_th12x_const.te_delta)) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_holtek_th12x_const.min_count_bit_for_found) {
if((instance->last_data == instance->decoder.decode_data) &&
instance->last_data) {
instance->te /= (instance->decoder.decode_count_bit * 3 + 1);
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->last_data = instance->decoder.decode_data;
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->te = 0;
instance->decoder.parser_step = Holtek_HT12XDecoderStepFoundStartBit;
break;
} else {
instance->decoder.te_last = duration;
instance->te += duration;
instance->decoder.parser_step = Holtek_HT12XDecoderStepCheckDuration;
}
} else {
instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
}
break;
case Holtek_HT12XDecoderStepCheckDuration:
if(level) {
instance->te += duration;
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_holtek_th12x_const.te_long) <
subghz_protocol_holtek_th12x_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_short) <
subghz_protocol_holtek_th12x_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = Holtek_HT12XDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_holtek_th12x_const.te_short) <
subghz_protocol_holtek_th12x_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_holtek_th12x_const.te_long) <
subghz_protocol_holtek_th12x_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = Holtek_HT12XDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
}
} else {
instance->decoder.parser_step = Holtek_HT12XDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_holtek_th12x_check_remote_controller(SubGhzBlockGeneric* instance) {
instance->btn = instance->data & 0x0F;
instance->cnt = (instance->data >> 4) & 0xFF;
}
uint8_t subghz_protocol_decoder_holtek_th12x_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_holtek_th12x_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
SubGhzProtocolStatus ret =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_write_uint32(flipper_format, "TE", &instance->te, 1)) {
FURI_LOG_E(TAG, "Unable to add TE");
ret = SubGhzProtocolStatusErrorParserTe;
}
return ret;
}
SubGhzProtocolStatus
subghz_protocol_decoder_holtek_th12x_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_holtek_th12x_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
FURI_LOG_E(TAG, "Missing TE");
ret = SubGhzProtocolStatusErrorParserTe;
break;
}
} while(false);
return ret;
}
static void subghz_protocol_holtek_th12x_event_serialize(uint8_t event, FuriString* output) {
furi_string_cat_printf(
output,
"%s%s%s%s\r\n",
(((event >> 3) & 0x1) == 0x0 ? "B1 " : ""),
(((event >> 2) & 0x1) == 0x0 ? "B2 " : ""),
(((event >> 1) & 0x1) == 0x0 ? "B3 " : ""),
(((event >> 0) & 0x1) == 0x0 ? "B4 " : ""));
}
void subghz_protocol_decoder_holtek_th12x_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderHoltek_HT12X* instance = context;
subghz_protocol_holtek_th12x_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key:0x%03lX\r\n"
"Btn: ",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFF));
subghz_protocol_holtek_th12x_event_serialize(instance->generic.btn, output);
furi_string_cat_printf(
output,
"DIP:" DIP_PATTERN "\r\n"
"Te:%luus\r\n",
CNT_TO_DIP(instance->generic.cnt),
instance->te);
}
| 15,442
|
C
|
.c
| 356
| 35.469101
| 100
| 0.653854
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,237
|
marantec.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/marantec.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_MARANTEC_NAME "Marantec"
typedef struct SubGhzProtocolDecoderMarantec SubGhzProtocolDecoderMarantec;
typedef struct SubGhzProtocolEncoderMarantec SubGhzProtocolEncoderMarantec;
extern const SubGhzProtocolDecoder subghz_protocol_marantec_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_marantec_encoder;
extern const SubGhzProtocol subghz_protocol_marantec;
/**
* Allocate SubGhzProtocolEncoderMarantec.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderMarantec* pointer to a SubGhzProtocolEncoderMarantec instance
*/
void* subghz_protocol_encoder_marantec_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderMarantec.
* @param context Pointer to a SubGhzProtocolEncoderMarantec instance
*/
void subghz_protocol_encoder_marantec_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderMarantec instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_marantec_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderMarantec instance
*/
void subghz_protocol_encoder_marantec_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderMarantec instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_marantec_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderMarantec.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderMarantec* pointer to a SubGhzProtocolDecoderMarantec instance
*/
void* subghz_protocol_decoder_marantec_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderMarantec.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
*/
void subghz_protocol_decoder_marantec_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderMarantec.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
*/
void subghz_protocol_decoder_marantec_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_marantec_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_marantec_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderMarantec.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_marantec_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderMarantec.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_marantec_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderMarantec instance
* @param output Resulting text
*/
void subghz_protocol_decoder_marantec_get_string(void* context, FuriString* output);
| 3,874
|
C
|
.c
| 92
| 40.119565
| 95
| 0.822045
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,238
|
secplus_v1.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/secplus_v1.h
|
#pragma once
#include "base.h"
#include "public_api.h"
#define SUBGHZ_PROTOCOL_SECPLUS_V1_NAME "Security+ 1.0"
typedef struct SubGhzProtocolDecoderSecPlus_v1 SubGhzProtocolDecoderSecPlus_v1;
typedef struct SubGhzProtocolEncoderSecPlus_v1 SubGhzProtocolEncoderSecPlus_v1;
extern const SubGhzProtocolDecoder subghz_protocol_secplus_v1_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_secplus_v1_encoder;
extern const SubGhzProtocol subghz_protocol_secplus_v1;
/**
* Allocate SubGhzProtocolEncoderSecPlus_v1.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderSecPlus_v1* pointer to a SubGhzProtocolEncoderSecPlus_v1 instance
*/
void* subghz_protocol_encoder_secplus_v1_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderSecPlus_v1.
* @param context Pointer to a SubGhzProtocolEncoderSecPlus_v1 instance
*/
void subghz_protocol_encoder_secplus_v1_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderSecPlus_v1 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_secplus_v1_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderSecPlus_v1 instance
*/
void subghz_protocol_encoder_secplus_v1_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderSecPlus_v1 instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_secplus_v1_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderSecPlus_v1.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderSecPlus_v1* pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
*/
void* subghz_protocol_decoder_secplus_v1_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderSecPlus_v1.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
*/
void subghz_protocol_decoder_secplus_v1_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderSecPlus_v1.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
*/
void subghz_protocol_decoder_secplus_v1_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_secplus_v1_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_secplus_v1_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderSecPlus_v1.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_secplus_v1_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderSecPlus_v1.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_secplus_v1_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderSecPlus_v1 instance
* @param output Resulting text
*/
void subghz_protocol_decoder_secplus_v1_get_string(void* context, FuriString* output);
| 3,989
|
C
|
.c
| 93
| 40.913978
| 97
| 0.81387
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,239
|
nice_flo.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/nice_flo.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_NICE_FLO_NAME "Nice FLO"
typedef struct SubGhzProtocolDecoderNiceFlo SubGhzProtocolDecoderNiceFlo;
typedef struct SubGhzProtocolEncoderNiceFlo SubGhzProtocolEncoderNiceFlo;
extern const SubGhzProtocolDecoder subghz_protocol_nice_flo_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_nice_flo_encoder;
extern const SubGhzProtocol subghz_protocol_nice_flo;
/**
* Allocate SubGhzProtocolEncoderNiceFlo.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderNiceFlo* pointer to a SubGhzProtocolEncoderNiceFlo instance
*/
void* subghz_protocol_encoder_nice_flo_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderNiceFlo.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlo instance
*/
void subghz_protocol_encoder_nice_flo_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlo instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_nice_flo_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlo instance
*/
void subghz_protocol_encoder_nice_flo_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlo instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_nice_flo_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderNiceFlo.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderNiceFlo* pointer to a SubGhzProtocolDecoderNiceFlo instance
*/
void* subghz_protocol_decoder_nice_flo_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderNiceFlo.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
*/
void subghz_protocol_decoder_nice_flo_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderNiceFlo.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
*/
void subghz_protocol_decoder_nice_flo_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_nice_flo_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_nice_flo_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderNiceFlo.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_nice_flo_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderNiceFlo.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_nice_flo_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlo instance
* @param output Resulting text
*/
void subghz_protocol_decoder_nice_flo_get_string(void* context, FuriString* output);
| 3,848
|
C
|
.c
| 92
| 39.836957
| 95
| 0.815994
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,241
|
ansonic.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/ansonic.c
|
#include "ansonic.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolAnsonic"
#define DIP_PATTERN "%c%c%c%c%c%c%c%c%c%c"
#define CNT_TO_DIP(dip) \
(dip & 0x0800 ? '1' : '0'), (dip & 0x0400 ? '1' : '0'), (dip & 0x0200 ? '1' : '0'), \
(dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), (dip & 0x0040 ? '1' : '0'), \
(dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), (dip & 0x0001 ? '1' : '0'), \
(dip & 0x0008 ? '1' : '0')
static const SubGhzBlockConst subghz_protocol_ansonic_const = {
.te_short = 555,
.te_long = 1111,
.te_delta = 120,
.min_count_bit_for_found = 12,
};
struct SubGhzProtocolDecoderAnsonic {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderAnsonic {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
AnsonicDecoderStepReset = 0,
AnsonicDecoderStepFoundStartBit,
AnsonicDecoderStepSaveDuration,
AnsonicDecoderStepCheckDuration,
} AnsonicDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_ansonic_decoder = {
.alloc = subghz_protocol_decoder_ansonic_alloc,
.free = subghz_protocol_decoder_ansonic_free,
.feed = subghz_protocol_decoder_ansonic_feed,
.reset = subghz_protocol_decoder_ansonic_reset,
.get_hash_data = subghz_protocol_decoder_ansonic_get_hash_data,
.serialize = subghz_protocol_decoder_ansonic_serialize,
.deserialize = subghz_protocol_decoder_ansonic_deserialize,
.get_string = subghz_protocol_decoder_ansonic_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_ansonic_encoder = {
.alloc = subghz_protocol_encoder_ansonic_alloc,
.free = subghz_protocol_encoder_ansonic_free,
.deserialize = subghz_protocol_encoder_ansonic_deserialize,
.stop = subghz_protocol_encoder_ansonic_stop,
.yield = subghz_protocol_encoder_ansonic_yield,
};
const SubGhzProtocol subghz_protocol_ansonic = {
.name = SUBGHZ_PROTOCOL_ANSONIC_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_FM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_ansonic_decoder,
.encoder = &subghz_protocol_ansonic_encoder,
};
void* subghz_protocol_encoder_ansonic_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderAnsonic* instance = malloc(sizeof(SubGhzProtocolEncoderAnsonic));
instance->base.protocol = &subghz_protocol_ansonic;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_ansonic_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderAnsonic* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderAnsonic instance
* @return true On success
*/
static bool subghz_protocol_encoder_ansonic_get_upload(SubGhzProtocolEncoderAnsonic* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_ansonic_const.te_short * 35);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_ansonic_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_ansonic_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_ansonic_const.te_long);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_ansonic_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_ansonic_const.te_short);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderAnsonic* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
res = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_ansonic_const.min_count_bit_for_found);
if(res != SubGhzProtocolStatusOk) {
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_encoder_ansonic_get_upload(instance)) {
res = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return res;
}
void subghz_protocol_encoder_ansonic_stop(void* context) {
SubGhzProtocolEncoderAnsonic* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_ansonic_yield(void* context) {
SubGhzProtocolEncoderAnsonic* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_ansonic_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderAnsonic* instance = malloc(sizeof(SubGhzProtocolDecoderAnsonic));
instance->base.protocol = &subghz_protocol_ansonic;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_ansonic_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
free(instance);
}
void subghz_protocol_decoder_ansonic_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
instance->decoder.parser_step = AnsonicDecoderStepReset;
}
void subghz_protocol_decoder_ansonic_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
switch(instance->decoder.parser_step) {
case AnsonicDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short * 35) <
subghz_protocol_ansonic_const.te_delta * 35)) {
//Found header Ansonic
instance->decoder.parser_step = AnsonicDecoderStepFoundStartBit;
}
break;
case AnsonicDecoderStepFoundStartBit:
if(!level) {
break;
} else if(
DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short) <
subghz_protocol_ansonic_const.te_delta) {
//Found start bit Ansonic
instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = AnsonicDecoderStepReset;
}
break;
case AnsonicDecoderStepSaveDuration:
if(!level) { //save interval
if(duration >= (subghz_protocol_ansonic_const.te_short * 4)) {
instance->decoder.parser_step = AnsonicDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit >=
subghz_protocol_ansonic_const.min_count_bit_for_found) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
instance->decoder.te_last = duration;
instance->decoder.parser_step = AnsonicDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = AnsonicDecoderStepReset;
}
break;
case AnsonicDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ansonic_const.te_short) <
subghz_protocol_ansonic_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_long) <
subghz_protocol_ansonic_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ansonic_const.te_long) <
subghz_protocol_ansonic_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_ansonic_const.te_short) <
subghz_protocol_ansonic_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = AnsonicDecoderStepSaveDuration;
} else
instance->decoder.parser_step = AnsonicDecoderStepReset;
} else {
instance->decoder.parser_step = AnsonicDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_ansonic_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* 12345678(10) k 9
* AAA => 10101010 1 01 0
*
* 1...10 - DIP
* k- KEY
*/
instance->cnt = instance->data & 0xFFF;
instance->btn = ((instance->data >> 1) & 0x3);
}
uint8_t subghz_protocol_decoder_ansonic_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_ansonic_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_ansonic_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_ansonic_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_ansonic_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderAnsonic* instance = context;
subghz_protocol_ansonic_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%03lX\r\n"
"Btn:%X\r\n"
"DIP:" DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
instance->generic.btn,
CNT_TO_DIP(instance->generic.cnt));
}
| 12,778
|
C
|
.c
| 297
| 35.794613
| 99
| 0.671329
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,242
|
mastercode.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/mastercode.c
|
#include "mastercode.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
// protocol MASTERCODE Clemsa MV1/MV12
#define TAG "SubGhzProtocolMastercode"
#define DIP_P 0b11 //(+)
#define DIP_O 0b10 //(0)
#define DIP_N 0b00 //(-)
#define DIP_PATTERN "%c%c%c%c%c%c%c%c"
#define SHOW_DIP_P(dip, check_dip) \
((((dip >> 0x0) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x2) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x4) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x6) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x8) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xA) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xC) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xE) & 0x3) == check_dip) ? '*' : '_')
static const SubGhzBlockConst subghz_protocol_mastercode_const = {
.te_short = 1072,
.te_long = 2145,
.te_delta = 150,
.min_count_bit_for_found = 36,
};
struct SubGhzProtocolDecoderMastercode {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderMastercode {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
MastercodeDecoderStepReset = 0,
MastercodeDecoderStepSaveDuration,
MastercodeDecoderStepCheckDuration,
} MastercodeDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_mastercode_decoder = {
.alloc = subghz_protocol_decoder_mastercode_alloc,
.free = subghz_protocol_decoder_mastercode_free,
.feed = subghz_protocol_decoder_mastercode_feed,
.reset = subghz_protocol_decoder_mastercode_reset,
.get_hash_data = subghz_protocol_decoder_mastercode_get_hash_data,
.serialize = subghz_protocol_decoder_mastercode_serialize,
.deserialize = subghz_protocol_decoder_mastercode_deserialize,
.get_string = subghz_protocol_decoder_mastercode_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_mastercode_encoder = {
.alloc = subghz_protocol_encoder_mastercode_alloc,
.free = subghz_protocol_encoder_mastercode_free,
.deserialize = subghz_protocol_encoder_mastercode_deserialize,
.stop = subghz_protocol_encoder_mastercode_stop,
.yield = subghz_protocol_encoder_mastercode_yield,
};
const SubGhzProtocol subghz_protocol_mastercode = {
.name = SUBGHZ_PROTOCOL_MASTERCODE_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_mastercode_decoder,
.encoder = &subghz_protocol_mastercode_encoder,
};
void* subghz_protocol_encoder_mastercode_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderMastercode* instance = malloc(sizeof(SubGhzProtocolEncoderMastercode));
instance->base.protocol = &subghz_protocol_mastercode;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 72;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_mastercode_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderMastercode* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderMastercode instance
* @return true On success
*/
static bool
subghz_protocol_encoder_mastercode_get_upload(SubGhzProtocolEncoderMastercode* 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, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_mastercode_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_mastercode_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_mastercode_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_mastercode_const.te_long);
}
}
if(bit_read(instance->generic.data, 0)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_mastercode_const.te_long);
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_mastercode_const.te_short +
subghz_protocol_mastercode_const.te_short * 13);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_mastercode_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_mastercode_const.te_long +
subghz_protocol_mastercode_const.te_short * 13);
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_mastercode_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderMastercode* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_mastercode_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_mastercode_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_mastercode_stop(void* context) {
SubGhzProtocolEncoderMastercode* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_mastercode_yield(void* context) {
SubGhzProtocolEncoderMastercode* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_mastercode_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderMastercode* instance = malloc(sizeof(SubGhzProtocolDecoderMastercode));
instance->base.protocol = &subghz_protocol_mastercode;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_mastercode_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
free(instance);
}
void subghz_protocol_decoder_mastercode_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
instance->decoder.parser_step = MastercodeDecoderStepReset;
}
void subghz_protocol_decoder_mastercode_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
switch(instance->decoder.parser_step) {
case MastercodeDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_mastercode_const.te_short * 15) <
subghz_protocol_mastercode_const.te_delta * 15)) {
instance->decoder.parser_step = MastercodeDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
break;
case MastercodeDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = MastercodeDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = MastercodeDecoderStepReset;
}
break;
case MastercodeDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_mastercode_const.te_short) <
subghz_protocol_mastercode_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_mastercode_const.te_long) <
subghz_protocol_mastercode_const.te_delta * 8)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = MastercodeDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_mastercode_const.te_long) <
subghz_protocol_mastercode_const.te_delta * 8) &&
(DURATION_DIFF(duration, subghz_protocol_mastercode_const.te_short) <
subghz_protocol_mastercode_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = MastercodeDecoderStepSaveDuration;
} else if(
DURATION_DIFF(duration, subghz_protocol_mastercode_const.te_short * 15) <
subghz_protocol_mastercode_const.te_delta * 15) {
if(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_mastercode_const.te_short) <
subghz_protocol_mastercode_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
} else if(
DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_mastercode_const.te_long) <
subghz_protocol_mastercode_const.te_delta * 8) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else {
instance->decoder.parser_step = MastercodeDecoderStepReset;
}
if(instance->decoder.decode_count_bit ==
subghz_protocol_mastercode_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.parser_step = MastercodeDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = MastercodeDecoderStepReset;
}
} else {
instance->decoder.parser_step = MastercodeDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_mastercode_check_remote_controller(SubGhzBlockGeneric* instance) {
instance->serial = (instance->data >> 4) & 0xFFFF;
instance->btn = (instance->data >> 2 & 0x03);
}
uint8_t subghz_protocol_decoder_mastercode_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_mastercode_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_mastercode_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_mastercode_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_mastercode_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderMastercode* instance = context;
subghz_protocol_mastercode_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%llX Btn %X\r\n"
" +: " DIP_PATTERN "\r\n"
" o: " DIP_PATTERN "\r\n"
" -: " DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint64_t)(instance->generic.data),
instance->generic.btn,
SHOW_DIP_P(instance->generic.serial, DIP_P),
SHOW_DIP_P(instance->generic.serial, DIP_O),
SHOW_DIP_P(instance->generic.serial, DIP_N));
}
| 14,074
|
C
|
.c
| 313
| 37.083067
| 98
| 0.663847
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,243
|
nice_flor_s.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/nice_flor_s.c
|
#include "nice_flor_s.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
/*
* https://phreakerclub.com/1615
* https://phreakerclub.com/forum/showthread.php?t=2360
* https://vrtp.ru/index.php?showtopic=27867
*/
#define TAG "SubGhzProtocolNiceFlorS"
#define NICE_ONE_COUNT_BIT 72
#define NICE_ONE_NAME "Nice One"
static const SubGhzBlockConst subghz_protocol_nice_flor_s_const = {
.te_short = 500,
.te_long = 1000,
.te_delta = 300,
.min_count_bit_for_found = 52,
};
struct SubGhzProtocolDecoderNiceFlorS {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
const char* nice_flor_s_rainbow_table_file_name;
uint64_t data;
};
struct SubGhzProtocolEncoderNiceFlorS {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
const char* nice_flor_s_rainbow_table_file_name;
};
typedef enum {
NiceFlorSDecoderStepReset = 0,
NiceFlorSDecoderStepCheckHeader,
NiceFlorSDecoderStepFoundHeader,
NiceFlorSDecoderStepSaveDuration,
NiceFlorSDecoderStepCheckDuration,
} NiceFlorSDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_nice_flor_s_decoder = {
.alloc = subghz_protocol_decoder_nice_flor_s_alloc,
.free = subghz_protocol_decoder_nice_flor_s_free,
.feed = subghz_protocol_decoder_nice_flor_s_feed,
.reset = subghz_protocol_decoder_nice_flor_s_reset,
.get_hash_data = subghz_protocol_decoder_nice_flor_s_get_hash_data,
.serialize = subghz_protocol_decoder_nice_flor_s_serialize,
.deserialize = subghz_protocol_decoder_nice_flor_s_deserialize,
.get_string = subghz_protocol_decoder_nice_flor_s_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_nice_flor_s_encoder = {
.alloc = subghz_protocol_encoder_nice_flor_s_alloc,
.free = subghz_protocol_encoder_nice_flor_s_free,
.deserialize = subghz_protocol_encoder_nice_flor_s_deserialize,
.stop = subghz_protocol_encoder_nice_flor_s_stop,
.yield = subghz_protocol_encoder_nice_flor_s_yield,
};
const SubGhzProtocol subghz_protocol_nice_flor_s = {
.name = SUBGHZ_PROTOCOL_NICE_FLOR_S_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
SubGhzProtocolFlag_Send | SubGhzProtocolFlag_NiceFlorS,
.decoder = &subghz_protocol_nice_flor_s_decoder,
.encoder = &subghz_protocol_nice_flor_s_encoder,
};
static void subghz_protocol_nice_flor_s_remote_controller(
SubGhzBlockGeneric* instance,
const char* file_name);
void* subghz_protocol_encoder_nice_flor_s_alloc(SubGhzEnvironment* environment) {
SubGhzProtocolEncoderNiceFlorS* instance = malloc(sizeof(SubGhzProtocolEncoderNiceFlorS));
instance->base.protocol = &subghz_protocol_nice_flor_s;
instance->generic.protocol_name = instance->base.protocol->name;
instance->nice_flor_s_rainbow_table_file_name =
subghz_environment_get_nice_flor_s_rainbow_table_file_name(environment);
if(instance->nice_flor_s_rainbow_table_file_name) {
FURI_LOG_D(
TAG, "Loading rainbow table from %s", instance->nice_flor_s_rainbow_table_file_name);
}
instance->encoder.repeat = 10;
instance->encoder.size_upload = 2400; //wrong!! upload 186*16 = 2976 - actual size about 1728
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_nice_flor_s_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderNiceFlorS* instance = context;
free(instance->encoder.upload);
free(instance);
}
static void subghz_protocol_nice_one_get_data(uint8_t* p, uint8_t num_parcel, uint8_t hold_bit);
/**
* Defines the button value for the current btn_id
* Basic set | 0x1 | 0x2 | 0x4 | 0x8 |
* @return Button code
*/
static uint8_t subghz_protocol_nice_flor_s_get_btn_code(void);
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderNiceFlorS instance
* @return true On success
*/
static void subghz_protocol_encoder_nice_flor_s_get_upload(
SubGhzProtocolEncoderNiceFlorS* instance,
uint8_t btn,
const char* file_name) {
furi_assert(instance);
size_t index = 0;
btn = instance->generic.btn;
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(btn);
}
btn = subghz_protocol_nice_flor_s_get_btn_code();
size_t size_upload = ((instance->generic.data_count_bit * 2) + ((37 + 2 + 2) * 2) * 16);
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
} else {
instance->encoder.size_upload = size_upload;
}
if(instance->generic.cnt < 0xFFFF) {
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xFFFF) {
instance->generic.cnt = 0;
} else {
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
}
} else if(instance->generic.cnt >= 0xFFFF) {
instance->generic.cnt = 0;
}
uint64_t decrypt = ((uint64_t)instance->generic.serial << 16) | instance->generic.cnt;
uint64_t enc_part = subghz_protocol_nice_flor_s_encrypt(decrypt, file_name);
for(int i = 0; i < 16; i++) {
static const uint64_t loops[16] = {
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
uint8_t byte;
byte = btn << 4 | (0xF ^ btn ^ loops[i]);
instance->generic.data = (uint64_t)byte << 44 | enc_part;
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nice_flor_s_const.te_short * 37);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nice_flor_s_const.te_short * 3);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nice_flor_s_const.te_short * 3);
//Send key data
for(uint8_t j = 52; j > 0; j--) {
if(bit_read(instance->generic.data, j - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nice_flor_s_const.te_long);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_nice_flor_s_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_nice_flor_s_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_nice_flor_s_const.te_long);
}
}
if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
uint8_t add_data[10] = {0};
for(size_t i = 0; i < 7; i++) {
add_data[i] = (instance->generic.data >> (48 - i * 8)) & 0xFF;
}
subghz_protocol_nice_one_get_data(add_data, loops[i], loops[i]);
instance->generic.data_2 = 0;
for(size_t j = 7; j < 10; j++) {
instance->generic.data_2 <<= 8;
instance->generic.data_2 += add_data[j];
}
//Send key data
for(uint8_t j = 24; j > 4; j--) {
if(bit_read(instance->generic.data_2, j - 1)) {
//send bit 1
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_nice_flor_s_const.te_long);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_nice_flor_s_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_nice_flor_s_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_nice_flor_s_const.te_long);
}
}
}
//Send stop bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nice_flor_s_const.te_short * 3);
//instance->encoder.upload[index++] =
//level_duration_make(false, (uint32_t)subghz_protocol_nice_flor_s_const.te_short * 3);
}
instance->encoder.size_upload = index;
}
SubGhzProtocolStatus
subghz_protocol_encoder_nice_flor_s_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderNiceFlorS* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
if(SubGhzProtocolStatusOk !=
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);
// flipper_format_read_uint32(
// flipper_format, "Data", (uint32_t*)&instance->generic.data_2, 1);
subghz_protocol_nice_flor_s_remote_controller(
&instance->generic, instance->nice_flor_s_rainbow_table_file_name);
subghz_protocol_encoder_nice_flor_s_get_upload(
instance, instance->generic.btn, instance->nice_flor_s_rainbow_table_file_name);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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 update Key");
break;
}
if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
uint32_t temp = (instance->generic.data_2 >> 4) & 0xFFFFF;
if(!flipper_format_update_uint32(flipper_format, "Data", &temp, 1)) {
FURI_LOG_E(TAG, "Unable to update Data");
}
}
instance->encoder.is_running = true;
res = SubGhzProtocolStatusOk;
} while(false);
return res;
}
void subghz_protocol_encoder_nice_flor_s_stop(void* context) {
SubGhzProtocolEncoderNiceFlorS* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_nice_flor_s_yield(void* context) {
SubGhzProtocolEncoderNiceFlorS* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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;
}
/**
* Read bytes from rainbow table
* @param p array[10] P0-P1|P2-P3-P4-P5-P6-P7-P8-P9-P10
* @return crc
*/
static uint32_t subghz_protocol_nice_one_crc(uint8_t* p) {
uint8_t crc = 0;
uint8_t crc_data = 0xff;
for(uint8_t i = 4; i < 68; i++) {
if(subghz_protocol_blocks_get_bit_array(p, i)) {
crc = crc_data ^ 1;
} else {
crc = crc_data;
}
crc_data >>= 1;
if((crc & 0x01)) {
crc_data ^= 0x97;
}
}
crc = 0;
for(uint8_t i = 0; i < 8; i++) {
crc <<= 1;
if((crc_data >> i) & 0x01) crc = crc | 1;
}
return crc;
}
/**
* Read bytes from rainbow table
* @param p array[10] P0-P1|P2-P3-P4-P5-P6-P7-XX-XX-XX
* @param num_parcel parcel number 0..15
* @param hold_bit 0 - the button was only pressed, 1 - the button was held down
*/
static void subghz_protocol_nice_one_get_data(uint8_t* p, uint8_t num_parcel, uint8_t hold_bit) {
uint8_t k = 0;
uint8_t crc = 0;
p[1] = (p[1] & 0x0f) | ((0x0f ^ (p[0] & 0x0F) ^ num_parcel) << 4);
if(num_parcel < 4) {
k = 0x8f;
} else {
k = 0x80;
}
if(!hold_bit) {
hold_bit = 0;
} else {
hold_bit = 0x10;
}
k = num_parcel ^ k;
p[7] = k;
p[8] = hold_bit ^ (k << 4);
crc = subghz_protocol_nice_one_crc(p);
p[8] |= crc >> 4;
p[9] = crc << 4;
}
/**
* Read bytes from rainbow table
* @param file_name Full path to rainbow table the file
* @param address Byte address in file
* @return data
*/
static uint8_t
subghz_protocol_nice_flor_s_get_byte_in_file(const char* file_name, uint32_t address) {
if(!file_name) return 0;
uint8_t buffer[1] = {0};
if(subghz_keystore_raw_get_data(file_name, address, buffer, sizeof(uint8_t))) {
return buffer[0];
} else {
return 0;
}
}
static inline void subghz_protocol_decoder_nice_flor_s_magic_xor(uint8_t* p, uint8_t k) {
for(uint8_t i = 1; i < 6; i++) {
p[i] ^= k;
}
}
uint64_t subghz_protocol_nice_flor_s_encrypt(uint64_t data, const char* file_name) {
uint8_t* p = (uint8_t*)&data;
uint8_t k = 0;
for(uint8_t y = 0; y < 2; y++) {
k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] & 0x1f);
subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
p[5] &= 0x0f;
p[0] ^= k & 0xe0;
k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] >> 3) + 0x25;
subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
p[5] &= 0x0f;
p[0] ^= k & 0x7;
if(y == 0) {
k = p[0];
p[0] = p[1];
p[1] = k;
}
}
p[5] = ~p[5] & 0x0f;
k = ~p[4];
p[4] = ~p[0];
p[0] = ~p[2];
p[2] = k;
k = ~p[3];
p[3] = ~p[1];
p[1] = k;
return data;
}
static uint64_t
subghz_protocol_nice_flor_s_decrypt(SubGhzBlockGeneric* instance, const char* file_name) {
furi_assert(instance);
uint64_t data = instance->data;
uint8_t* p = (uint8_t*)&data;
uint8_t k = 0;
k = ~p[4];
p[5] = ~p[5];
p[4] = ~p[2];
p[2] = ~p[0];
p[0] = k;
k = ~p[3];
p[3] = ~p[1];
p[1] = k;
for(uint8_t y = 0; y < 2; y++) {
k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] >> 3) + 0x25;
subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
p[5] &= 0x0f;
p[0] ^= k & 0x7;
k = subghz_protocol_nice_flor_s_get_byte_in_file(file_name, p[0] & 0x1f);
subghz_protocol_decoder_nice_flor_s_magic_xor(p, k);
p[5] &= 0x0f;
p[0] ^= k & 0xe0;
if(y == 0) {
k = p[0];
p[0] = p[1];
p[1] = k;
}
}
return data;
}
bool subghz_protocol_nice_flor_s_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint16_t cnt,
SubGhzRadioPreset* preset,
bool nice_one) {
furi_assert(context);
SubGhzProtocolEncoderNiceFlorS* instance = context;
instance->generic.serial = serial;
instance->generic.cnt = cnt;
if(nice_one) {
instance->generic.data_count_bit = NICE_ONE_COUNT_BIT;
} else {
instance->generic.data_count_bit = 52;
}
uint64_t decrypt = ((uint64_t)instance->generic.serial << 16) | instance->generic.cnt;
uint64_t enc_part = subghz_protocol_nice_flor_s_encrypt(
decrypt, instance->nice_flor_s_rainbow_table_file_name);
uint8_t byte = btn << 4 | (0xF ^ btn ^ 0x3);
instance->generic.data = (uint64_t)byte << 44 | enc_part;
if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
uint8_t add_data[10] = {0};
for(size_t i = 0; i < 7; i++) {
add_data[i] = (instance->generic.data >> (48 - i * 8)) & 0xFF;
}
subghz_protocol_nice_one_get_data(add_data, 0, 0);
instance->generic.data_2 = 0;
for(size_t j = 7; j < 10; j++) {
instance->generic.data_2 <<= 8;
instance->generic.data_2 += add_data[j];
}
}
SubGhzProtocolStatus res =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
return res == SubGhzProtocolStatusOk;
}
void* subghz_protocol_decoder_nice_flor_s_alloc(SubGhzEnvironment* environment) {
SubGhzProtocolDecoderNiceFlorS* instance = malloc(sizeof(SubGhzProtocolDecoderNiceFlorS));
instance->base.protocol = &subghz_protocol_nice_flor_s;
instance->generic.protocol_name = instance->base.protocol->name;
instance->nice_flor_s_rainbow_table_file_name =
subghz_environment_get_nice_flor_s_rainbow_table_file_name(environment);
if(instance->nice_flor_s_rainbow_table_file_name) {
FURI_LOG_D(
TAG, "Loading rainbow table from %s", instance->nice_flor_s_rainbow_table_file_name);
}
return instance;
}
void subghz_protocol_decoder_nice_flor_s_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
instance->nice_flor_s_rainbow_table_file_name = NULL;
free(instance);
}
void subghz_protocol_decoder_nice_flor_s_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
instance->decoder.parser_step = NiceFlorSDecoderStepReset;
}
void subghz_protocol_decoder_nice_flor_s_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
switch(instance->decoder.parser_step) {
case NiceFlorSDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 38) <
subghz_protocol_nice_flor_s_const.te_delta * 38)) {
//Found start header Nice Flor-S
instance->decoder.parser_step = NiceFlorSDecoderStepCheckHeader;
}
break;
case NiceFlorSDecoderStepCheckHeader:
if((level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
subghz_protocol_nice_flor_s_const.te_delta * 3)) {
//Found next header Nice Flor-S
instance->decoder.parser_step = NiceFlorSDecoderStepFoundHeader;
} else {
instance->decoder.parser_step = NiceFlorSDecoderStepReset;
}
break;
case NiceFlorSDecoderStepFoundHeader:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
subghz_protocol_nice_flor_s_const.te_delta * 3)) {
//Found header Nice Flor-S
instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = NiceFlorSDecoderStepReset;
}
break;
case NiceFlorSDecoderStepSaveDuration:
if(level) {
if(DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short * 3) <
subghz_protocol_nice_flor_s_const.te_delta) {
//Found STOP bit
instance->decoder.parser_step = NiceFlorSDecoderStepReset;
if((instance->decoder.decode_count_bit ==
subghz_protocol_nice_flor_s_const.min_count_bit_for_found) ||
(instance->decoder.decode_count_bit == NICE_ONE_COUNT_BIT)) {
instance->generic.data = instance->data;
instance->data = instance->decoder.decode_data;
instance->decoder.decode_data = instance->generic.data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
} else {
//save interval
instance->decoder.te_last = duration;
instance->decoder.parser_step = NiceFlorSDecoderStepCheckDuration;
}
}
break;
case NiceFlorSDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nice_flor_s_const.te_short) <
subghz_protocol_nice_flor_s_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_long) <
subghz_protocol_nice_flor_s_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nice_flor_s_const.te_long) <
subghz_protocol_nice_flor_s_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_nice_flor_s_const.te_short) <
subghz_protocol_nice_flor_s_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = NiceFlorSDecoderStepSaveDuration;
} else
instance->decoder.parser_step = NiceFlorSDecoderStepReset;
} else {
instance->decoder.parser_step = NiceFlorSDecoderStepReset;
}
if(instance->decoder.decode_count_bit ==
subghz_protocol_nice_flor_s_const.min_count_bit_for_found) {
instance->data = instance->decoder.decode_data;
instance->decoder.decode_data = 0;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
* @param file_name Full path to rainbow table the file
*/
static void subghz_protocol_nice_flor_s_remote_controller(
SubGhzBlockGeneric* instance,
const char* file_name) {
/*
* Protocol Nice Flor-S
* Packet format Nice Flor-s: START-P0-P1-P2-P3-P4-P5-P6-P7-STOP
* P0 (4-bit) - button positional code - 1:0x1, 2:0x2, 3:0x4, 4:0x8;
* P1 (4-bit) - batch repetition number, calculated by the formula:
* P1 = 0xF ^ P0 ^ n; where n changes from 1 to 15, then 0, and then in a circle
* key 1: {0xE,0xF,0xC,0xD,0xA,0xB,0x8,0x9,0x6,0x7,0x4,0x5,0x2,0x3,0x0,0x1};
* key 2: {0xD,0xC,0xF,0xE,0x9,0x8,0xB,0xA,0x5,0x4,0x7,0x6,0x1,0x0,0x3,0x2};
* key 3: {0xB,0xA,0x9,0x8,0xF,0xE,0xD,0xC,0x3,0x2,0x1,0x0,0x7,0x6,0x5,0x4};
* key 4: {0x7,0x6,0x5,0x4,0x3,0x2,0x1,0x0,0xF,0xE,0xD,0xC,0xB,0xA,0x9,0x8};
* P2 (4-bit) - part of the serial number, P2 = (K ^ S3) & 0xF;
* P3 (byte) - the major part of the encrypted index
* P4 (byte) - the low-order part of the encrypted index
* P5 (byte) - part of the serial number, P5 = K ^ S2;
* P6 (byte) - part of the serial number, P6 = K ^ S1;
* P7 (byte) - part of the serial number, P7 = K ^ S0;
* K (byte) - depends on P3 and P4, K = Fk(P3, P4);
* S3,S2,S1,S0 - serial number of the console 28 bit.
*
* data => 0x1c5783607f7b3 key serial cnt
* decrypt => 0x10436c6820444 => 0x1 0436c682 0444
*
* Protocol Nice One
* Generally repeats the Nice Flor-S protocol, but there are a few changes
* Packet format first 52 bytes repeat Nice Flor-S protocol
* The additional 20 bytes contain the code of the pressed button,
* the button hold bit and the CRC of the entire message.
* START-P0-P1-P2-P3-P4-P5-P6-P7-P8-P9-P10-STOP
* P7 (byte) - if (n<4) k=0x8f : k=0x80; P7= k^n;
* P8 (byte) - if (hold bit) b=0x00 : b=0x10; P8= b^(k<<4) | 4 hi bit crc
* P10 (4-bit) - 4 lo bit crc
* key+b crc
* data => 0x1724A7D9A522F 899 D6 hold bit = 0 - just pressed the button
* data => 0x1424A7D9A522F 8AB 03 hold bit = 1 - button hold
*
* A small button hold counter (0..15) is stored between each press,
* i.e. if 1 press of the button stops counter 6, then the next press
* of the button will start from the value 7 (hold bit = 0), 8 (hold bit = 1)...
* further up to 15 with overflow
*
*/
if(!file_name) {
instance->cnt = 0;
instance->serial = 0;
instance->btn = 0;
} else {
uint64_t decrypt = subghz_protocol_nice_flor_s_decrypt(instance, file_name);
instance->cnt = decrypt & 0xFFFF;
instance->serial = (decrypt >> 16) & 0xFFFFFFF;
instance->btn = (decrypt >> 48) & 0xF;
}
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(4);
}
uint8_t subghz_protocol_decoder_nice_flor_s_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_nice_flor_s_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
SubGhzProtocolStatus ret =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
}
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_insert_or_update_uint32(
flipper_format, "Data", (uint32_t*)&instance->data, 1)) {
FURI_LOG_E(TAG, "Unable to add Data");
ret = SubGhzProtocolStatusErrorParserOthers;
}
}
return ret;
}
SubGhzProtocolStatus
subghz_protocol_decoder_nice_flor_s_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if((instance->generic.data_count_bit !=
subghz_protocol_nice_flor_s_const.min_count_bit_for_found) &&
(instance->generic.data_count_bit != NICE_ONE_COUNT_BIT)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
uint32_t temp = 0;
if(!flipper_format_read_uint32(flipper_format, "Data", (uint32_t*)&temp, 1)) {
FURI_LOG_E(TAG, "Missing Data");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
instance->data = (uint64_t)temp;
}
} while(false);
return ret;
}
static uint8_t subghz_protocol_nice_flor_s_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x1:
btn = 0x2;
break;
case 0x2:
btn = 0x1;
break;
case 0x4:
btn = 0x1;
break;
case 0x8:
btn = 0x1;
break;
case 0x3:
btn = 0x1;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x1:
btn = 0x4;
break;
case 0x2:
btn = 0x4;
break;
case 0x4:
btn = 0x2;
break;
case 0x8:
btn = 0x4;
break;
case 0x3:
btn = 0x4;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0x1:
btn = 0x8;
break;
case 0x2:
btn = 0x8;
break;
case 0x4:
btn = 0x8;
break;
case 0x8:
btn = 0x2;
break;
case 0x3:
btn = 0x8;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_RIGHT) {
switch(original_btn_code) {
case 0x1:
btn = 0x3;
break;
case 0x2:
btn = 0x3;
break;
case 0x4:
btn = 0x3;
break;
case 0x8:
btn = 0x3;
break;
case 0x3:
btn = 0x2;
break;
default:
break;
}
}
return btn;
}
void subghz_protocol_decoder_nice_flor_s_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlorS* instance = context;
subghz_protocol_nice_flor_s_remote_controller(
&instance->generic, instance->nice_flor_s_rainbow_table_file_name);
if(instance->generic.data_count_bit == NICE_ONE_COUNT_BIT) {
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%013llX%llX\r\n"
"Sn:%05lX\r\n"
"Cnt:%04lX Btn:%02X\r\n",
NICE_ONE_NAME,
instance->generic.data_count_bit,
instance->generic.data,
instance->data,
instance->generic.serial,
instance->generic.cnt,
instance->generic.btn);
} else {
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%013llX\r\n"
"Sn:%05lX\r\n"
"Cnt:%04lX Btn:%02X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
instance->generic.data,
instance->generic.serial,
instance->generic.cnt,
instance->generic.btn);
}
}
| 31,656
|
C
|
.c
| 794
| 31.562972
| 99
| 0.603595
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,244
|
megacode.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/megacode.c
|
#include "megacode.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
/*
* Help
* https://wiki.cuvoodoo.info/doku.php?id=megacode
* https://wiki.cuvoodoo.info/lib/exe/fetch.php?media=megacode:megacode_1.pdf
* https://fccid.io/EF4ACP00872/Test-Report/Megacode-2-112615.pdf
* https://github.com/aaronsp777/megadecoder
* https://github.com/rjmendez/Linear_keyfob
* https://github.com/j07rdi/Linear_MegaCode_Garage_Remote
*
*/
#define TAG "SubGhzProtocolMegaCode"
static const SubGhzBlockConst subghz_protocol_megacode_const = {
.te_short = 1000,
.te_long = 1000,
.te_delta = 200,
.min_count_bit_for_found = 24,
};
struct SubGhzProtocolDecoderMegaCode {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint8_t last_bit;
};
struct SubGhzProtocolEncoderMegaCode {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
MegaCodeDecoderStepReset = 0,
MegaCodeDecoderStepFoundStartBit,
MegaCodeDecoderStepSaveDuration,
MegaCodeDecoderStepCheckDuration,
} MegaCodeDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_megacode_decoder = {
.alloc = subghz_protocol_decoder_megacode_alloc,
.free = subghz_protocol_decoder_megacode_free,
.feed = subghz_protocol_decoder_megacode_feed,
.reset = subghz_protocol_decoder_megacode_reset,
.get_hash_data = subghz_protocol_decoder_megacode_get_hash_data,
.serialize = subghz_protocol_decoder_megacode_serialize,
.deserialize = subghz_protocol_decoder_megacode_deserialize,
.get_string = subghz_protocol_decoder_megacode_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_megacode_encoder = {
.alloc = subghz_protocol_encoder_megacode_alloc,
.free = subghz_protocol_encoder_megacode_free,
.deserialize = subghz_protocol_encoder_megacode_deserialize,
.stop = subghz_protocol_encoder_megacode_stop,
.yield = subghz_protocol_encoder_megacode_yield,
};
const SubGhzProtocol subghz_protocol_megacode = {
.name = SUBGHZ_PROTOCOL_MEGACODE_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_megacode_decoder,
.encoder = &subghz_protocol_megacode_encoder,
};
void* subghz_protocol_encoder_megacode_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderMegaCode* instance = malloc(sizeof(SubGhzProtocolEncoderMegaCode));
instance->base.protocol = &subghz_protocol_megacode;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_megacode_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderMegaCode* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderMegaCode instance
* @return true On success
*/
static bool subghz_protocol_encoder_megacode_get_upload(SubGhzProtocolEncoderMegaCode* instance) {
furi_assert(instance);
uint8_t last_bit = 0;
size_t size_upload = (instance->generic.data_count_bit * 2);
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
/*
* Due to the nature of the protocol
*
* 00000 1
* _____|-| = 1 becomes
*
* 00 1 000
* __|-|___ = 0 becomes
*
* it's easier for us to generate an upload backwards
*/
size_t index = size_upload - 1;
// Send end level
instance->encoder.upload[index--] =
level_duration_make(true, (uint32_t)subghz_protocol_megacode_const.te_short);
if(bit_read(instance->generic.data, 0)) {
last_bit = 1;
} else {
last_bit = 0;
}
//Send key data
for(uint8_t i = 1; i < instance->generic.data_count_bit; i++) {
if(bit_read(instance->generic.data, i)) {
//if bit 1
instance->encoder.upload[index--] = level_duration_make(
false,
last_bit ? (uint32_t)subghz_protocol_megacode_const.te_short * 5 :
(uint32_t)subghz_protocol_megacode_const.te_short * 2);
last_bit = 1;
} else {
//if bit 0
instance->encoder.upload[index--] = level_duration_make(
false,
last_bit ? (uint32_t)subghz_protocol_megacode_const.te_short * 8 :
(uint32_t)subghz_protocol_megacode_const.te_short * 5);
last_bit = 0;
}
instance->encoder.upload[index--] =
level_duration_make(true, (uint32_t)subghz_protocol_megacode_const.te_short);
}
//Send PT_GUARD
if(bit_read(instance->generic.data, 0)) {
//if end bit 1
instance->encoder.upload[index] =
level_duration_make(false, (uint32_t)subghz_protocol_megacode_const.te_short * 11);
} else {
//if end bit 1
instance->encoder.upload[index] =
level_duration_make(false, (uint32_t)subghz_protocol_megacode_const.te_short * 14);
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_megacode_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderMegaCode* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_megacode_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_megacode_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_megacode_stop(void* context) {
SubGhzProtocolEncoderMegaCode* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_megacode_yield(void* context) {
SubGhzProtocolEncoderMegaCode* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_megacode_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderMegaCode* instance = malloc(sizeof(SubGhzProtocolDecoderMegaCode));
instance->base.protocol = &subghz_protocol_megacode;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_megacode_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
free(instance);
}
void subghz_protocol_decoder_megacode_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
instance->decoder.parser_step = MegaCodeDecoderStepReset;
}
void subghz_protocol_decoder_megacode_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
switch(instance->decoder.parser_step) {
case MegaCodeDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short * 13) <
subghz_protocol_megacode_const.te_delta * 17)) { //10..16ms
//Found header MegaCode
instance->decoder.parser_step = MegaCodeDecoderStepFoundStartBit;
}
break;
case MegaCodeDecoderStepFoundStartBit:
if(level && (DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
subghz_protocol_megacode_const.te_delta)) {
//Found start bit MegaCode
instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->last_bit = 1;
} else {
instance->decoder.parser_step = MegaCodeDecoderStepReset;
}
break;
case MegaCodeDecoderStepSaveDuration:
if(!level) { //save interval
if(duration >= (subghz_protocol_megacode_const.te_short * 10)) {
instance->decoder.parser_step = MegaCodeDecoderStepReset;
if(instance->decoder.decode_count_bit ==
subghz_protocol_megacode_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
if(!instance->last_bit) {
instance->decoder.te_last = duration - subghz_protocol_megacode_const.te_short * 3;
} else {
instance->decoder.te_last = duration;
}
instance->decoder.parser_step = MegaCodeDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = MegaCodeDecoderStepReset;
}
break;
case MegaCodeDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_megacode_const.te_short * 5) <
subghz_protocol_megacode_const.te_delta * 5) &&
(DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
subghz_protocol_megacode_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->last_bit = 1;
instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_megacode_const.te_short * 2) <
subghz_protocol_megacode_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_megacode_const.te_short) <
subghz_protocol_megacode_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->last_bit = 0;
instance->decoder.parser_step = MegaCodeDecoderStepSaveDuration;
} else
instance->decoder.parser_step = MegaCodeDecoderStepReset;
} else {
instance->decoder.parser_step = MegaCodeDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_megacode_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* Short: 1000 µs
* Long: 1000 µs
* Gap: 11000 .. 14000 µs
* A Linear Megacode transmission consists of 24 bit frames starting with
* the most significant bit and ending with the least. Each of the 24 bit
* frames is 6 milliseconds wide and always contains a single 1 millisecond
* pulse. A frame with more than 1 pulse or a frame with no pulse is invalid
* and a receiver should reset and begin watching for another start bit.
* Start bit is always 1.
*
*
* Example (I created with my own remote):
* Remote “A” has the code “17316”, a Facility Code of “3”, and a single button.
* Start bit (S) = 1
* Facility Code 3 (F) = 0011
* Remote Code (Key) 17316 = 43A4 = 0100001110100100
* Button (Btn) 1 = 001
* S F Key Btn
* Result = 1|0011|0100001110100100|001
*
* 00000 1
* _____|-| = 1 becomes
*
* 00 1 000
* __|-|___ = 0 becomes
*
* The device needs to transmit with a 9000 µs gap between retransmissions:
* 000001 001000 001000 000001 000001 001000 000001 001000 001000 001000 001000 000001
* 000001 000001 001000 000001 001000 001000 000001 001000 001000 001000 001000 000001
* wait 9000 µs
* 000001 001000 001000 000001 000001 001000 000001 001000 001000 001000 001000 000001
* 000001 000001 001000 000001 001000 001000 000001 001000 001000 001000 001000 000001
*
*/
if((instance->data >> 23) == 1) {
instance->serial = (instance->data >> 3) & 0xFFFF;
instance->btn = instance->data & 0b111;
instance->cnt = (instance->data >> 19) & 0b1111;
} else {
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
}
}
uint8_t subghz_protocol_decoder_megacode_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_megacode_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_megacode_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_megacode_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_megacode_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderMegaCode* instance = context;
subghz_protocol_megacode_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%06lX\r\n"
"Sn:0x%04lX - %lu\r\n"
"Facility:%lX Btn:%X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)instance->generic.data,
instance->generic.serial,
instance->generic.serial,
instance->generic.cnt,
instance->generic.btn);
}
| 15,394
|
C
|
.c
| 372
| 34.091398
| 99
| 0.670032
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,245
|
dickert_mahs.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/dickert_mahs.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME "Dickert_MAHS"
typedef struct SubGhzProtocolDecoderDickertMAHS SubGhzProtocolDecoderDickertMAHS;
typedef struct SubGhzProtocolEncoderDickertMAHS SubGhzProtocolEncoderDickertMAHS;
extern const SubGhzProtocolDecoder subghz_protocol_dickert_mahs_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_dickert_mahs_encoder;
extern const SubGhzProtocol subghz_protocol_dickert_mahs;
/** Allocate SubGhzProtocolEncoderDickertMAHS.
*
* @param environment Pointer to a SubGhzEnvironment instance
*
* @return pointer to a SubGhzProtocolEncoderDickertMAHS instance
*/
void* subghz_protocol_encoder_dickert_mahs_alloc(SubGhzEnvironment* environment);
/** Free SubGhzProtocolEncoderDickertMAHS.
*
* @param context Pointer to a SubGhzProtocolEncoderDickertMAHS instance
*/
void subghz_protocol_encoder_dickert_mahs_free(void* context);
/** Deserialize and generating an upload to send.
*
* @param context Pointer to a SubGhzProtocolEncoderDickertMAHS
* instance
* @param flipper_format Pointer to a FlipperFormat instance
*
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format);
/** Forced transmission stop.
*
* @param context Pointer to a SubGhzProtocolEncoderDickertMAHS instance
*/
void subghz_protocol_encoder_dickert_mahs_stop(void* context);
/** Getting the level and duration of the upload to be loaded into DMA.
*
* @param context Pointer to a SubGhzProtocolEncoderDickertMAHS instance
*
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_dickert_mahs_yield(void* context);
/** Allocate SubGhzProtocolDecoderDickertMAHS.
*
* @param environment Pointer to a SubGhzEnvironment instance
*
* @return pointer to a SubGhzProtocolDecoderDickertMAHS instance
*/
void* subghz_protocol_decoder_dickert_mahs_alloc(SubGhzEnvironment* environment);
/** Free SubGhzProtocolDecoderDickertMAHS.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance
*/
void subghz_protocol_decoder_dickert_mahs_free(void* context);
/** Reset decoder SubGhzProtocolDecoderDickertMAHS.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance
*/
void subghz_protocol_decoder_dickert_mahs_reset(void* context);
/** Parse a raw sequence of levels and durations received from the air.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_dickert_mahs_feed(void* context, bool level, uint32_t duration);
/** Getting the hash sum of the last randomly received parcel.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance
*
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_dickert_mahs_get_hash_data(void* context);
/** Serialize data SubGhzProtocolDecoderDickertMAHS.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS
* instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received,
* SubGhzRadioPreset
*
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_dickert_mahs_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/** Deserialize data SubGhzProtocolDecoderDickertMAHS.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS
* instance
* @param flipper_format Pointer to a FlipperFormat instance
*
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format);
/** Getting a textual representation of the received data.
*
* @param context Pointer to a SubGhzProtocolDecoderDickertMAHS instance
* @param output Resulting text
*/
void subghz_protocol_decoder_dickert_mahs_get_string(void* context, FuriString* output);
| 4,282
|
C
|
.c
| 103
| 39.592233
| 99
| 0.759971
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,246
|
alutech_at_4n.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/alutech_at_4n.c
|
#include "alutech_at_4n.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
#define TAG "SubGhzProtocoAlutech_at_4n"
#define SUBGHZ_NO_ALUTECH_AT_4N_RAINBOW_TABLE 0xFFFFFFFF
static const SubGhzBlockConst subghz_protocol_alutech_at_4n_const = {
.te_short = 400,
.te_long = 800,
.te_delta = 140,
.min_count_bit_for_found = 72,
};
struct SubGhzProtocolDecoderAlutech_at_4n {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint64_t data;
uint32_t crc;
uint16_t header_count;
const char* alutech_at_4n_rainbow_table_file_name;
};
struct SubGhzProtocolEncoderAlutech_at_4n {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
const char* alutech_at_4n_rainbow_table_file_name;
uint32_t crc;
};
typedef enum {
Alutech_at_4nDecoderStepReset = 0,
Alutech_at_4nDecoderStepCheckPreambula,
Alutech_at_4nDecoderStepSaveDuration,
Alutech_at_4nDecoderStepCheckDuration,
} Alutech_at_4nDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_alutech_at_4n_decoder = {
.alloc = subghz_protocol_decoder_alutech_at_4n_alloc,
.free = subghz_protocol_decoder_alutech_at_4n_free,
.feed = subghz_protocol_decoder_alutech_at_4n_feed,
.reset = subghz_protocol_decoder_alutech_at_4n_reset,
.get_hash_data = subghz_protocol_decoder_alutech_at_4n_get_hash_data,
.serialize = subghz_protocol_decoder_alutech_at_4n_serialize,
.deserialize = subghz_protocol_decoder_alutech_at_4n_deserialize,
.get_string = subghz_protocol_decoder_alutech_at_4n_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_alutech_at_4n_encoder = {
.alloc = subghz_protocol_encoder_alutech_at_4n_alloc,
.free = subghz_protocol_encoder_alutech_at_4n_free,
.deserialize = subghz_protocol_encoder_alutech_at_4n_deserialize,
.stop = subghz_protocol_encoder_alutech_at_4n_stop,
.yield = subghz_protocol_encoder_alutech_at_4n_yield,
};
const SubGhzProtocol subghz_protocol_alutech_at_4n = {
.name = SUBGHZ_PROTOCOL_ALUTECH_AT_4N_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_alutech_at_4n_decoder,
.encoder = &subghz_protocol_alutech_at_4n_encoder,
};
static void subghz_protocol_alutech_at_4n_remote_controller(
SubGhzBlockGeneric* instance,
uint8_t crc,
const char* file_name);
void* subghz_protocol_encoder_alutech_at_4n_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderAlutech_at_4n* instance =
malloc(sizeof(SubGhzProtocolEncoderAlutech_at_4n));
instance->base.protocol = &subghz_protocol_alutech_at_4n;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 512;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
instance->alutech_at_4n_rainbow_table_file_name =
subghz_environment_get_alutech_at_4n_rainbow_table_file_name(environment);
if(instance->alutech_at_4n_rainbow_table_file_name) {
FURI_LOG_I(
TAG, "Loading rainbow table from %s", instance->alutech_at_4n_rainbow_table_file_name);
}
return instance;
}
void subghz_protocol_encoder_alutech_at_4n_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderAlutech_at_4n* instance = context;
free(instance->encoder.upload);
free(instance);
}
void subghz_protocol_encoder_alutech_at_4n_stop(void* context) {
SubGhzProtocolEncoderAlutech_at_4n* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_alutech_at_4n_yield(void* context) {
SubGhzProtocolEncoderAlutech_at_4n* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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;
}
/**
* Read bytes from rainbow table
* @param file_name Full path to rainbow table the file
* @param number_alutech_at_4n_magic_data number in the array
* @return alutech_at_4n_magic_data
*/
static uint32_t subghz_protocol_alutech_at_4n_get_magic_data_in_file(
const char* file_name,
uint8_t number_alutech_at_4n_magic_data) {
if(!strcmp(file_name, "")) return SUBGHZ_NO_ALUTECH_AT_4N_RAINBOW_TABLE;
uint8_t buffer[sizeof(uint32_t)] = {0};
uint32_t address = number_alutech_at_4n_magic_data * sizeof(uint32_t);
uint32_t alutech_at_4n_magic_data = 0;
if(subghz_keystore_raw_get_data(file_name, address, buffer, sizeof(uint32_t))) {
for(size_t i = 0; i < sizeof(uint32_t); i++) {
alutech_at_4n_magic_data = (alutech_at_4n_magic_data << 8) | buffer[i];
}
} else {
alutech_at_4n_magic_data = SUBGHZ_NO_ALUTECH_AT_4N_RAINBOW_TABLE;
}
return alutech_at_4n_magic_data;
}
static uint8_t subghz_protocol_alutech_at_4n_crc(uint64_t data) {
uint8_t* p = (uint8_t*)&data;
uint8_t crc = 0xff;
for(uint8_t y = 0; y < 8; y++) {
crc = crc ^ p[y];
for(uint8_t i = 0; i < 8; i++) {
if((crc & 0x80) != 0) {
crc <<= 1;
crc ^= 0x31;
} else {
crc <<= 1;
}
}
}
return crc;
}
static uint8_t subghz_protocol_alutech_at_4n_decrypt_data_crc(uint8_t data) {
uint8_t crc = data;
for(uint8_t i = 0; i < 8; i++) {
if((crc & 0x80) != 0) {
crc <<= 1;
crc ^= 0x31;
} else {
crc <<= 1;
}
}
return ~crc;
}
static uint64_t subghz_protocol_alutech_at_4n_decrypt(uint64_t data, const char* file_name) {
uint8_t* p = (uint8_t*)&data;
uint32_t data1 = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
uint32_t data2 = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
uint32_t data3 = 0;
uint32_t magic_data[] = {
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 0),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 1),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 2),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 3),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 4),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 5)};
uint32_t i = magic_data[0];
do {
data2 = data2 -
((magic_data[1] + (data1 << 4)) ^ ((magic_data[2] + (data1 >> 5)) ^ (data1 + i)));
data3 = data2 + i;
i += magic_data[3];
data1 =
data1 - ((magic_data[4] + (data2 << 4)) ^ ((magic_data[5] + (data2 >> 5)) ^ data3));
} while(i != 0);
p[0] = (uint8_t)(data1 >> 24);
p[1] = (uint8_t)(data1 >> 16);
p[3] = (uint8_t)data1;
p[4] = (uint8_t)(data2 >> 24);
p[5] = (uint8_t)(data2 >> 16);
p[2] = (uint8_t)(data1 >> 8);
p[6] = (uint8_t)(data2 >> 8);
p[7] = (uint8_t)data2;
return data;
}
static uint64_t subghz_protocol_alutech_at_4n_encrypt(uint64_t data, const char* file_name) {
uint8_t* p = (uint8_t*)&data;
uint32_t data1 = 0;
uint32_t data2 = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
uint32_t data3 = p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
uint32_t magic_data[] = {
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 6),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 4),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 5),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 1),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 2),
subghz_protocol_alutech_at_4n_get_magic_data_in_file(file_name, 0)};
do {
data1 = data1 + magic_data[0];
data2 = data2 + ((magic_data[1] + (data3 << 4)) ^
((magic_data[2] + (data3 >> 5)) ^ (data1 + data3)));
data3 = data3 + ((magic_data[3] + (data2 << 4)) ^
((magic_data[4] + (data2 >> 5)) ^ (data1 + data2)));
} while(data1 != magic_data[5]);
p[0] = (uint8_t)(data2 >> 24);
p[1] = (uint8_t)(data2 >> 16);
p[3] = (uint8_t)data2;
p[4] = (uint8_t)(data3 >> 24);
p[5] = (uint8_t)(data3 >> 16);
p[2] = (uint8_t)(data2 >> 8);
p[6] = (uint8_t)(data3 >> 8);
p[7] = (uint8_t)data3;
return data;
}
static bool subghz_protocol_alutech_at_4n_gen_data(
SubGhzProtocolEncoderAlutech_at_4n* instance,
uint8_t btn) {
uint64_t data = subghz_protocol_blocks_reverse_key(instance->generic.data, 64);
data = subghz_protocol_alutech_at_4n_decrypt(
data, instance->alutech_at_4n_rainbow_table_file_name);
uint8_t crc = data >> 56;
if(crc == subghz_protocol_alutech_at_4n_decrypt_data_crc((uint8_t)((data >> 8) & 0xFF))) {
instance->generic.btn = (uint8_t)data & 0xFF;
instance->generic.cnt = (uint16_t)(data >> 8) & 0xFFFF;
instance->generic.serial = (uint32_t)(data >> 24) & 0xFFFFFFFF;
}
if(instance->generic.cnt < 0xFFFF) {
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xFFFF) {
instance->generic.cnt = 0;
} else {
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
}
} else if(instance->generic.cnt >= 0xFFFF) {
instance->generic.cnt = 0;
}
crc = subghz_protocol_alutech_at_4n_decrypt_data_crc((uint8_t)(instance->generic.cnt & 0xFF));
data = (uint64_t)crc << 56 | (uint64_t)instance->generic.serial << 24 |
(uint32_t)instance->generic.cnt << 8 | btn;
data = subghz_protocol_alutech_at_4n_encrypt(
data, instance->alutech_at_4n_rainbow_table_file_name);
crc = subghz_protocol_alutech_at_4n_crc(data);
instance->generic.data = subghz_protocol_blocks_reverse_key(data, 64);
instance->crc = subghz_protocol_blocks_reverse_key(crc, 8);
return true;
}
bool subghz_protocol_alutech_at_4n_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint16_t cnt,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolEncoderAlutech_at_4n* instance = context;
instance->generic.serial = serial;
instance->generic.cnt = cnt;
instance->generic.data_count_bit = 72;
if(subghz_protocol_alutech_at_4n_gen_data(instance, btn)) {
if((subghz_block_generic_serialize(&instance->generic, flipper_format, preset) !=
SubGhzProtocolStatusOk)) {
FURI_LOG_E(TAG, "Serialize error");
return false;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
return false;
}
if(!flipper_format_insert_or_update_uint32(flipper_format, "CRC", &instance->crc, 1)) {
FURI_LOG_E(TAG, "Unable to add CRC");
return false;
}
}
return true;
}
/**
* Defines the button value for the current btn_id
* Basic set | 0x11 | 0x22 | 0xFF | 0x44 | 0x33 |
* @return Button code
*/
static uint8_t subghz_protocol_alutech_at_4n_get_btn_code(void);
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderAlutech instance
* @return true On success
*/
static bool subghz_protocol_encoder_alutech_at_4n_get_upload(
SubGhzProtocolEncoderAlutech_at_4n* instance,
uint8_t btn) {
furi_assert(instance);
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(btn);
}
btn = subghz_protocol_alutech_at_4n_get_btn_code();
// Gen new key
if(!subghz_protocol_alutech_at_4n_gen_data(instance, btn)) {
return false;
}
size_t index = 0;
// Send preambula
for(uint8_t i = 0; i < 12; ++i) {
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_alutech_at_4n_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_alutech_at_4n_const.te_short); // 0
}
instance->encoder.upload[index - 1].duration +=
(uint32_t)subghz_protocol_alutech_at_4n_const.te_short * 9;
// Send key data
for(uint8_t i = 64; i > 0; --i) {
if(bit_read(instance->generic.data, i - 1)) {
//1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_alutech_at_4n_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_alutech_at_4n_const.te_long);
} else {
//0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_alutech_at_4n_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_alutech_at_4n_const.te_short);
}
}
// Send crc
for(uint8_t i = 8; i > 0; --i) {
if(bit_read(instance->crc, i - 1)) {
//1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_alutech_at_4n_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_alutech_at_4n_const.te_long);
} else {
//0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_alutech_at_4n_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_alutech_at_4n_const.te_short);
}
}
// Inter-frame silence
instance->encoder.upload[index - 1].duration +=
(uint32_t)subghz_protocol_alutech_at_4n_const.te_long * 20;
size_t size_upload = index;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
return true;
}
SubGhzProtocolStatus subghz_protocol_encoder_alutech_at_4n_deserialize(
void* context,
FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderAlutech_at_4n* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
if(SubGhzProtocolStatusOk !=
subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
FURI_LOG_E(TAG, "Deserialize error");
break;
}
if(!flipper_format_read_uint32(flipper_format, "CRC", (uint32_t*)&instance->crc, 1)) {
FURI_LOG_E(TAG, "Missing CRC");
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_alutech_at_4n_remote_controller(
&instance->generic, instance->crc, instance->alutech_at_4n_rainbow_table_file_name);
subghz_protocol_encoder_alutech_at_4n_get_upload(instance, instance->generic.btn);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
if(!flipper_format_update_uint32(flipper_format, "CRC", &instance->crc, 1)) {
FURI_LOG_E(TAG, "Unable to add CRC");
break;
}
instance->encoder.is_running = true;
res = SubGhzProtocolStatusOk;
} while(false);
return res;
}
void* subghz_protocol_decoder_alutech_at_4n_alloc(SubGhzEnvironment* environment) {
SubGhzProtocolDecoderAlutech_at_4n* instance =
malloc(sizeof(SubGhzProtocolDecoderAlutech_at_4n));
instance->base.protocol = &subghz_protocol_alutech_at_4n;
instance->generic.protocol_name = instance->base.protocol->name;
instance->alutech_at_4n_rainbow_table_file_name =
subghz_environment_get_alutech_at_4n_rainbow_table_file_name(environment);
if(instance->alutech_at_4n_rainbow_table_file_name) {
FURI_LOG_I(
TAG, "Loading rainbow table from %s", instance->alutech_at_4n_rainbow_table_file_name);
}
return instance;
}
void subghz_protocol_decoder_alutech_at_4n_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
instance->alutech_at_4n_rainbow_table_file_name = NULL;
free(instance);
}
void subghz_protocol_decoder_alutech_at_4n_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
}
void subghz_protocol_decoder_alutech_at_4n_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
switch(instance->decoder.parser_step) {
case Alutech_at_4nDecoderStepReset:
if((level) && DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short) <
subghz_protocol_alutech_at_4n_const.te_delta) {
instance->decoder.parser_step = Alutech_at_4nDecoderStepCheckPreambula;
instance->header_count++;
}
break;
case Alutech_at_4nDecoderStepCheckPreambula:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short) <
subghz_protocol_alutech_at_4n_const.te_delta)) {
instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
break;
}
if((instance->header_count > 2) &&
(DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short * 10) <
subghz_protocol_alutech_at_4n_const.te_delta * 10)) {
// Found header
instance->decoder.parser_step = Alutech_at_4nDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
instance->header_count = 0;
}
break;
case Alutech_at_4nDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = Alutech_at_4nDecoderStepCheckDuration;
}
break;
case Alutech_at_4nDecoderStepCheckDuration:
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_alutech_at_4n_const.te_short * 2 +
subghz_protocol_alutech_at_4n_const.te_delta)) {
//add last bit
if(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_short) <
subghz_protocol_alutech_at_4n_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else if(
DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_long) <
subghz_protocol_alutech_at_4n_const.te_delta * 2) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
}
// Found end TX
instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
if(instance->decoder.decode_count_bit ==
subghz_protocol_alutech_at_4n_const.min_count_bit_for_found) {
if(instance->generic.data != instance->data) {
instance->generic.data = instance->data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
instance->crc = instance->decoder.decode_data;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->data = 0;
instance->decoder.decode_count_bit = 0;
instance->header_count = 0;
}
break;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_short) <
subghz_protocol_alutech_at_4n_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_long) <
subghz_protocol_alutech_at_4n_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
if(instance->decoder.decode_count_bit == 64) {
instance->data = instance->decoder.decode_data;
instance->decoder.decode_data = 0;
}
instance->decoder.parser_step = Alutech_at_4nDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_alutech_at_4n_const.te_long) <
subghz_protocol_alutech_at_4n_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_alutech_at_4n_const.te_short) <
subghz_protocol_alutech_at_4n_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
if(instance->decoder.decode_count_bit == 64) {
instance->data = instance->decoder.decode_data;
instance->decoder.decode_data = 0;
}
instance->decoder.parser_step = Alutech_at_4nDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
instance->header_count = 0;
}
} else {
instance->decoder.parser_step = Alutech_at_4nDecoderStepReset;
instance->header_count = 0;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
* @param file_name Full path to rainbow table the file
*/
static void subghz_protocol_alutech_at_4n_remote_controller(
SubGhzBlockGeneric* instance,
uint8_t crc,
const char* file_name) {
/**
* Message format 72bit LSB first
* data crc
* XXXXXXXXXXXXXXXX CC
*
* For analysis, you need to turn the package MSB
* in decoded messages format
*
* crc1 serial cnt key
* cc SSSSSSSS XXxx BB
*
* crc1 is calculated from the lower part of cnt
* key 1=0xff, 2=0x11, 3=0x22, 4=0x33, 5=0x44
*
*/
bool status = false;
uint64_t data = subghz_protocol_blocks_reverse_key(instance->data, 64);
crc = subghz_protocol_blocks_reverse_key(crc, 8);
if(crc == subghz_protocol_alutech_at_4n_crc(data)) {
data = subghz_protocol_alutech_at_4n_decrypt(data, file_name);
status = true;
}
if(status && ((uint8_t)(data >> 56) ==
subghz_protocol_alutech_at_4n_decrypt_data_crc((uint8_t)((data >> 8) & 0xFF)))) {
instance->btn = (uint8_t)data & 0xFF;
instance->cnt = (uint16_t)(data >> 8) & 0xFFFF;
instance->serial = (uint32_t)(data >> 24) & 0xFFFFFFFF;
}
if(!status) {
instance->btn = 0;
instance->cnt = 0;
instance->serial = 0;
}
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(4);
}
uint8_t subghz_protocol_decoder_alutech_at_4n_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
return (uint8_t)instance->crc;
}
SubGhzProtocolStatus subghz_protocol_decoder_alutech_at_4n_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
SubGhzProtocolStatus res =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
res = SubGhzProtocolStatusErrorParserOthers;
}
if((res == SubGhzProtocolStatusOk) &&
!flipper_format_insert_or_update_uint32(flipper_format, "CRC", &instance->crc, 1)) {
FURI_LOG_E(TAG, "Unable to add CRC");
res = SubGhzProtocolStatusErrorParserOthers;
}
return res;
}
SubGhzProtocolStatus subghz_protocol_decoder_alutech_at_4n_deserialize(
void* context,
FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_alutech_at_4n_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_uint32(flipper_format, "CRC", (uint32_t*)&instance->crc, 1)) {
FURI_LOG_E(TAG, "Missing CRC");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
} while(false);
return ret;
}
static uint8_t subghz_protocol_alutech_at_4n_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x11:
btn = 0x22;
break;
case 0x22:
btn = 0x11;
break;
case 0xFF:
btn = 0x11;
break;
case 0x44:
btn = 0x11;
break;
case 0x33:
btn = 0x11;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x11:
btn = 0x44;
break;
case 0x22:
btn = 0x44;
break;
case 0xFF:
btn = 0x44;
break;
case 0x44:
btn = 0xFF;
break;
case 0x33:
btn = 0x44;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0x11:
btn = 0x33;
break;
case 0x22:
btn = 0x33;
break;
case 0xFF:
btn = 0x33;
break;
case 0x44:
btn = 0x33;
break;
case 0x33:
btn = 0x22;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_RIGHT) {
switch(original_btn_code) {
case 0x11:
btn = 0xFF;
break;
case 0x22:
btn = 0xFF;
break;
case 0xFF:
btn = 0x22;
break;
case 0x44:
btn = 0x22;
break;
case 0x33:
btn = 0xFF;
break;
default:
break;
}
}
return btn;
}
void subghz_protocol_decoder_alutech_at_4n_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderAlutech_at_4n* instance = context;
subghz_protocol_alutech_at_4n_remote_controller(
&instance->generic, instance->crc, instance->alutech_at_4n_rainbow_table_file_name);
uint32_t code_found_hi = instance->generic.data >> 32;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s\r\n"
"Key:0x%08lX%08lX\nCRC:%02X %dbit\r\n"
"Sn:0x%08lX Btn:0x%01X\r\n"
"Cnt:0x%04lX\r\n",
instance->generic.protocol_name,
code_found_hi,
code_found_lo,
(uint8_t)instance->crc,
instance->generic.data_count_bit,
instance->generic.serial,
instance->generic.btn,
instance->generic.cnt);
}
| 29,985
|
C
|
.c
| 741
| 32.167341
| 99
| 0.614902
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,247
|
bett.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/bett.c
|
#include "bett.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
// protocol BERNER / ELKA / TEDSEN / TELETASTER
#define TAG "SubGhzProtocolBett"
#define DIP_P 0b11 //(+)
#define DIP_O 0b10 //(0)
#define DIP_N 0b00 //(-)
#define DIP_PATTERN "%c%c%c%c%c%c%c%c%c"
#define SHOW_DIP_P(dip, check_dip) \
((((dip >> 0x8) >> 0x8) == check_dip) ? '*' : '_'), \
((((dip >> 0xE) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xC) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xA) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x8) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x6) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x4) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x2) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x0) & 0x3) == check_dip) ? '*' : '_')
static const SubGhzBlockConst subghz_protocol_bett_const = {
.te_short = 340,
.te_long = 2000,
.te_delta = 150,
.min_count_bit_for_found = 18,
};
struct SubGhzProtocolDecoderBETT {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderBETT {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
BETTDecoderStepReset = 0,
BETTDecoderStepSaveDuration,
BETTDecoderStepCheckDuration,
} BETTDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_bett_decoder = {
.alloc = subghz_protocol_decoder_bett_alloc,
.free = subghz_protocol_decoder_bett_free,
.feed = subghz_protocol_decoder_bett_feed,
.reset = subghz_protocol_decoder_bett_reset,
.get_hash_data = subghz_protocol_decoder_bett_get_hash_data,
.serialize = subghz_protocol_decoder_bett_serialize,
.deserialize = subghz_protocol_decoder_bett_deserialize,
.get_string = subghz_protocol_decoder_bett_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_bett_encoder = {
.alloc = subghz_protocol_encoder_bett_alloc,
.free = subghz_protocol_encoder_bett_free,
.deserialize = subghz_protocol_encoder_bett_deserialize,
.stop = subghz_protocol_encoder_bett_stop,
.yield = subghz_protocol_encoder_bett_yield,
};
const SubGhzProtocol subghz_protocol_bett = {
.name = SUBGHZ_PROTOCOL_BETT_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_bett_decoder,
.encoder = &subghz_protocol_bett_encoder,
};
void* subghz_protocol_encoder_bett_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderBETT* instance = malloc(sizeof(SubGhzProtocolEncoderBETT));
instance->base.protocol = &subghz_protocol_bett;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_bett_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderBETT* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderBETT instance
* @return true On success
*/
static bool subghz_protocol_encoder_bett_get_upload(SubGhzProtocolEncoderBETT* 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, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_bett_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_bett_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_bett_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_bett_const.te_long);
}
}
if(bit_read(instance->generic.data, 0)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_bett_const.te_long);
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_bett_const.te_short +
subghz_protocol_bett_const.te_long * 7);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_bett_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_bett_const.te_long + subghz_protocol_bett_const.te_long * 7);
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_bett_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderBETT* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_bett_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
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_encoder_bett_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_bett_stop(void* context) {
SubGhzProtocolEncoderBETT* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_bett_yield(void* context) {
SubGhzProtocolEncoderBETT* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_bett_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderBETT* instance = malloc(sizeof(SubGhzProtocolDecoderBETT));
instance->base.protocol = &subghz_protocol_bett;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_bett_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
free(instance);
}
void subghz_protocol_decoder_bett_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
instance->decoder.parser_step = BETTDecoderStepReset;
}
void subghz_protocol_decoder_bett_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
switch(instance->decoder.parser_step) {
case BETTDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_bett_const.te_short * 44) <
(subghz_protocol_bett_const.te_delta * 15))) {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = BETTDecoderStepCheckDuration;
}
break;
case BETTDecoderStepSaveDuration:
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_bett_const.te_short * 44) <
(subghz_protocol_bett_const.te_delta * 15)) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_bett_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
} else {
instance->decoder.parser_step = BETTDecoderStepReset;
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
if((DURATION_DIFF(duration, subghz_protocol_bett_const.te_short) <
subghz_protocol_bett_const.te_delta) ||
(DURATION_DIFF(duration, subghz_protocol_bett_const.te_long) <
subghz_protocol_bett_const.te_delta * 3)) {
instance->decoder.parser_step = BETTDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = BETTDecoderStepReset;
}
}
}
break;
case BETTDecoderStepCheckDuration:
if(level) {
if(DURATION_DIFF(duration, subghz_protocol_bett_const.te_long) <
subghz_protocol_bett_const.te_delta * 3) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = BETTDecoderStepSaveDuration;
} else if(
DURATION_DIFF(duration, subghz_protocol_bett_const.te_short) <
subghz_protocol_bett_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = BETTDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = BETTDecoderStepReset;
}
} else {
instance->decoder.parser_step = BETTDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_bett_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_bett_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_bett_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_bett_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_bett_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderBETT* instance = context;
uint32_t data = (uint32_t)(instance->generic.data & 0x3FFFF);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%05lX\r\n"
" +: " DIP_PATTERN "\r\n"
" o: " DIP_PATTERN "\r\n"
" -: " DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
data,
SHOW_DIP_P(data, DIP_P),
SHOW_DIP_P(data, DIP_O),
SHOW_DIP_P(data, DIP_N));
}
| 12,407
|
C
|
.c
| 291
| 35.030928
| 99
| 0.647921
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,248
|
somfy_telis.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/somfy_telis.c
|
#include "somfy_telis.h"
#include <lib/toolbox/manchester_decoder.h>
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
#define TAG "SubGhzProtocolSomfyTelis"
static const SubGhzBlockConst subghz_protocol_somfy_telis_const = {
.te_short = 640,
.te_long = 1280,
.te_delta = 250,
.min_count_bit_for_found = 56,
};
struct SubGhzProtocolDecoderSomfyTelis {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint16_t header_count;
ManchesterState manchester_saved_state;
};
struct SubGhzProtocolEncoderSomfyTelis {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
SomfyTelisDecoderStepReset = 0,
SomfyTelisDecoderStepCheckPreambula,
SomfyTelisDecoderStepFoundPreambula,
SomfyTelisDecoderStepStartDecode,
SomfyTelisDecoderStepDecoderData,
} SomfyTelisDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_somfy_telis_decoder = {
.alloc = subghz_protocol_decoder_somfy_telis_alloc,
.free = subghz_protocol_decoder_somfy_telis_free,
.feed = subghz_protocol_decoder_somfy_telis_feed,
.reset = subghz_protocol_decoder_somfy_telis_reset,
.get_hash_data = subghz_protocol_decoder_somfy_telis_get_hash_data,
.serialize = subghz_protocol_decoder_somfy_telis_serialize,
.deserialize = subghz_protocol_decoder_somfy_telis_deserialize,
.get_string = subghz_protocol_decoder_somfy_telis_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_somfy_telis_encoder = {
.alloc = subghz_protocol_encoder_somfy_telis_alloc,
.free = subghz_protocol_encoder_somfy_telis_free,
.deserialize = subghz_protocol_encoder_somfy_telis_deserialize,
.stop = subghz_protocol_encoder_somfy_telis_stop,
.yield = subghz_protocol_encoder_somfy_telis_yield,
};
const SubGhzProtocol subghz_protocol_somfy_telis = {
.name = SUBGHZ_PROTOCOL_SOMFY_TELIS_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_somfy_telis_decoder,
.encoder = &subghz_protocol_somfy_telis_encoder,
};
void* subghz_protocol_encoder_somfy_telis_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderSomfyTelis* instance = malloc(sizeof(SubGhzProtocolEncoderSomfyTelis));
instance->base.protocol = &subghz_protocol_somfy_telis;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 512;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_somfy_telis_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderSomfyTelis* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Defines the button value for the current btn_id
* Basic set | 0x1 | 0x2 | 0x4 | 0x8 |
* @return Button code
*/
static uint8_t subghz_protocol_somfy_telis_get_btn_code(void);
static bool subghz_protocol_somfy_telis_gen_data(
SubGhzProtocolEncoderSomfyTelis* instance,
uint8_t btn,
bool new_remote) {
// If we doing a clone we will use its data
uint64_t data = instance->generic.data ^ (instance->generic.data >> 8);
if(!new_remote) {
instance->generic.btn = (data >> 44) & 0xF; // ctrl
btn = instance->generic.btn;
instance->generic.cnt = (data >> 24) & 0xFFFF; // rolling code
instance->generic.serial = data & 0xFFFFFF; // address
}
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(btn);
}
btn = subghz_protocol_somfy_telis_get_btn_code();
if(instance->generic.cnt < 0xFFFF) {
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xFFFF) {
instance->generic.cnt = 0;
} else {
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
}
} else if(instance->generic.cnt >= 0xFFFF) {
instance->generic.cnt = 0;
}
uint8_t frame[7];
if(!new_remote) {
frame[0] = data >> 48;
} else {
frame[0] = 0xA7;
}
frame[1] = btn << 4;
frame[2] = instance->generic.cnt >> 8;
frame[3] = instance->generic.cnt;
frame[4] = instance->generic.serial >> 16;
frame[5] = instance->generic.serial >> 8;
frame[6] = instance->generic.serial;
uint8_t checksum = 0;
for(uint8_t i = 0; i < 7; i++) {
checksum = checksum ^ frame[i] ^ (frame[i] >> 4);
}
checksum &= 0xF;
frame[1] |= checksum;
for(uint8_t i = 1; i < 7; i++) {
frame[i] ^= frame[i - 1];
}
data = 0;
for(uint8_t i = 0; i < 7; ++i) {
data <<= 8;
data |= frame[i];
}
instance->generic.data = data;
return true;
}
bool subghz_protocol_somfy_telis_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint16_t cnt,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolEncoderSomfyTelis* instance = context;
instance->generic.serial = serial;
instance->generic.cnt = cnt;
instance->generic.data_count_bit = 56;
bool res = subghz_protocol_somfy_telis_gen_data(instance, btn, true);
if(res) {
return SubGhzProtocolStatusOk ==
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
return res;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderSomfyTelis instance
* @return true On success
*/
static bool subghz_protocol_encoder_somfy_telis_get_upload(
SubGhzProtocolEncoderSomfyTelis* instance,
uint8_t btn) {
furi_assert(instance);
// Gen new key
if(!subghz_protocol_somfy_telis_gen_data(instance, btn, false)) {
return false;
}
size_t index = 0;
//Send header
//Wake up
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)9415); // 1
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)89565); // 0
//Hardware sync
for(uint8_t i = 0; i < 2; ++i) {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short * 4); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short * 4); // 0
}
//Software sync
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)4550); // 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
//Send key data MSB manchester
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration *= 2; // 00
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 1
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 1
}
} else {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_HIGH) {
instance->encoder.upload[index - 1].duration *= 2; // 11
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
} else {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
}
}
}
//Inter-frame silence
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration += (uint32_t)30415;
} else {
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)30415);
}
//Retransmission
for(uint8_t i = 0; i < 2; i++) {
//Hardware sync
for(uint8_t i = 0; i < 7; ++i) {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short * 4); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short * 4); // 0
}
//Software sync
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)4550); // 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
//Send key data MSB manchester
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration *= 2; // 00
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 1
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 1
}
} else {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_HIGH) {
instance->encoder.upload[index - 1].duration *= 2; // 11
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
} else {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_telis_const.te_short); // 0
}
}
}
//Inter-frame silence
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration += (uint32_t)30415;
} else {
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)30415);
}
}
size_t size_upload = index;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_somfy_telis_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderSomfyTelis* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
if(SubGhzProtocolStatusOk !=
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);
subghz_protocol_encoder_somfy_telis_get_upload(instance, instance->generic.btn);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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_running = true;
res = SubGhzProtocolStatusOk;
} while(false);
return res;
}
void subghz_protocol_encoder_somfy_telis_stop(void* context) {
SubGhzProtocolEncoderSomfyTelis* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_somfy_telis_yield(void* context) {
SubGhzProtocolEncoderSomfyTelis* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_somfy_telis_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderSomfyTelis* instance = malloc(sizeof(SubGhzProtocolDecoderSomfyTelis));
instance->base.protocol = &subghz_protocol_somfy_telis;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_somfy_telis_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
free(instance);
}
void subghz_protocol_decoder_somfy_telis_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
instance->decoder.parser_step = SomfyTelisDecoderStepReset;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
}
/**
* Сhecksum calculation.
* @param data Вata for checksum calculation
* @return CRC
*/
static uint8_t subghz_protocol_somfy_telis_crc(uint64_t data) {
uint8_t crc = 0;
data &= 0xFFF0FFFFFFFFFF;
for(uint8_t i = 0; i < 56; i += 8) {
crc = crc ^ data >> i ^ (data >> (i + 4));
}
return crc & 0xf;
}
void subghz_protocol_decoder_somfy_telis_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
ManchesterEvent event = ManchesterEventReset;
switch(instance->decoder.parser_step) {
case SomfyTelisDecoderStepReset:
if((level) && DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
subghz_protocol_somfy_telis_const.te_delta * 4) {
instance->decoder.parser_step = SomfyTelisDecoderStepFoundPreambula;
instance->header_count++;
}
break;
case SomfyTelisDecoderStepFoundPreambula:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
subghz_protocol_somfy_telis_const.te_delta * 4)) {
instance->decoder.parser_step = SomfyTelisDecoderStepCheckPreambula;
} else {
instance->header_count = 0;
instance->decoder.parser_step = SomfyTelisDecoderStepReset;
}
break;
case SomfyTelisDecoderStepCheckPreambula:
if(level) {
if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 4) <
subghz_protocol_somfy_telis_const.te_delta * 4) {
instance->decoder.parser_step = SomfyTelisDecoderStepFoundPreambula;
instance->header_count++;
} else if(
(instance->header_count > 1) &&
(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short * 7) <
subghz_protocol_somfy_telis_const.te_delta * 4)) {
instance->decoder.parser_step = SomfyTelisDecoderStepDecoderData;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->header_count = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventLongHigh,
&instance->manchester_saved_state,
NULL);
}
}
break;
case SomfyTelisDecoderStepDecoderData:
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short) <
subghz_protocol_somfy_telis_const.te_delta) {
event = ManchesterEventShortLow;
} else if(
DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_long) <
subghz_protocol_somfy_telis_const.te_delta) {
event = ManchesterEventLongLow;
} else if(
duration >= (subghz_protocol_somfy_telis_const.te_long +
subghz_protocol_somfy_telis_const.te_delta)) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_somfy_telis_const.min_count_bit_for_found) {
//check crc
uint64_t data_tmp = instance->decoder.decode_data ^
(instance->decoder.decode_data >> 8);
if(((data_tmp >> 40) & 0xF) == subghz_protocol_somfy_telis_crc(data_tmp)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventLongHigh,
&instance->manchester_saved_state,
NULL);
instance->decoder.parser_step = SomfyTelisDecoderStepReset;
} else {
instance->decoder.parser_step = SomfyTelisDecoderStepReset;
}
} else {
if(DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_short) <
subghz_protocol_somfy_telis_const.te_delta) {
event = ManchesterEventShortHigh;
} else if(
DURATION_DIFF(duration, subghz_protocol_somfy_telis_const.te_long) <
subghz_protocol_somfy_telis_const.te_delta) {
event = ManchesterEventLongHigh;
} else {
instance->decoder.parser_step = SomfyTelisDecoderStepReset;
}
}
if(event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(
instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
if(data_ok) {
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
instance->decoder.decode_count_bit++;
}
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_somfy_telis_check_remote_controller(SubGhzBlockGeneric* instance) {
//https://pushstack.wordpress.com/somfy-rts-protocol/
/*
* 604 us
* /
* | 2416us | 2416us | 2416us | 2416us | 4550 us | | 67648 us | 30415 us |
*
* +--------+ +--------+ +---...---+
* + +--------+ +--------+ +--+XXXX...XXX+-----...-----
*
* | hw. sync. | soft. | | Inter-frame
* | | sync. | data | gap
*
*
* encrypt | decrypt
*
* package 56 bit cnt key btn|crc cnt serial
* 0xA7232323312222 - 0 => A7 8 0 | 00 00 | 12 13 00
* 0xA7222223312222 - 1 => A7 8 5 | 00 01 | 12 13 00
* 0xA7212123312222 - 2 => A7 8 6 | 00 02 | 12 13 00
*
* Key: “Encryption Key”, Most significant 4-bit are always 0xA, Least Significant bits is
* a linear counter. In the Smoove Origin this counter is increased together with the
* rolling code. But leaving this on a constant value also works. Gerardwr notes that
* for some other types of remotes the MSB is not constant.
* Btn: 4-bit Control codes, this indicates the button that is pressed
* CRC: 4-bit Checksum.
* Ctn: 16-bit rolling code (big-endian) increased with every button press.
* Serial: 24-bit identifier of sending device (little-endian)
*
*
* Decrypt
*
* uint8_t frame[7];
* for (i=1; i < 7; i++) {
* frame[i] = frame[i] ^ frame[i-1];
* }
* or
* uint64 Decrypt = frame ^ (frame>>8);
*
* Btn
*
* Value Button(s) Description
* 0x1 My Stop or move to favourite position
* 0x2 Up Move up
* 0x3 My + Up Set upper motor limit in initial programming mode
* 0x4 Down Move down
* 0x5 My + Down Set lower motor limit in initial programming mode
* 0x6 Up + Down Change motor limit and initial programming mode
* 0x8 Prog Used for (de-)registering remotes, see below
* 0x9 Sun + Flag Enable sun and wind detector (SUN and FLAG symbol on the Telis Soliris RC)
* 0xA Flag Disable sun detector (FLAG symbol on the Telis Soliris RC)
*
* CRC
*
* uint8_t frame[7];
* for (i=0; i < 7; i++) {
* cksum = cksum ^ frame[i] ^ (frame[i] >> 4);
* }
* cksum = cksum & 0xf;
*
*/
uint64_t data = instance->data ^ (instance->data >> 8);
instance->btn = (data >> 44) & 0xF; // ctrl
instance->cnt = (data >> 24) & 0xFFFF; // rolling code
instance->serial = data & 0xFFFFFF; // address
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(3);
}
/**
* Get button name.
* @param btn Button number, 4 bit
*/
static const char* subghz_protocol_somfy_telis_get_name_button(uint8_t btn) {
const char* name_btn[16] = {
"Unknown",
"My",
"Up",
"My+Up",
"Down",
"My+Down",
"Up+Down",
"0x07",
"Prog",
"Sun+Flag",
"Flag",
"0x0B",
"0x0C",
"0x0D",
"0x0E",
"0x0F"};
return btn <= 0xf ? name_btn[btn] : name_btn[0];
}
uint8_t subghz_protocol_decoder_somfy_telis_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_somfy_telis_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_somfy_telis_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_somfy_telis_const.min_count_bit_for_found);
}
static uint8_t subghz_protocol_somfy_telis_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x1:
btn = 0x2;
break;
case 0x2:
btn = 0x1;
break;
case 0x4:
btn = 0x1;
break;
case 0x8:
btn = 0x1;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x1:
btn = 0x4;
break;
case 0x2:
btn = 0x4;
break;
case 0x4:
btn = 0x2;
break;
case 0x8:
btn = 0x4;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0x1:
btn = 0x8;
break;
case 0x2:
btn = 0x8;
break;
case 0x4:
btn = 0x8;
break;
case 0x8:
btn = 0x2;
break;
default:
break;
}
}
return btn;
}
void subghz_protocol_decoder_somfy_telis_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderSomfyTelis* instance = context;
subghz_protocol_somfy_telis_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%06lX \r\n"
"Cnt:0x%04lX\r\n"
"Btn:%s\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->generic.serial,
instance->generic.cnt,
subghz_protocol_somfy_telis_get_name_button(instance->generic.btn));
}
| 27,600
|
C
|
.c
| 672
| 32.721726
| 102
| 0.607341
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,249
|
keeloq_common.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/keeloq_common.c
|
#include "keeloq_common.h"
#include <furi.h>
#include <m-array.h>
#define bit(x, n) (((x) >> (n)) & 1)
#define g5(x, a, b, c, d, e) \
(bit(x, a) + bit(x, b) * 2 + bit(x, c) * 4 + bit(x, d) * 8 + bit(x, e) * 16)
/** Simple Learning Encrypt
* @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
* @param key - manufacture (64bit)
* @return keeloq encrypt data
*/
inline uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key) {
uint32_t x = data, r;
for(r = 0; r < 528; r++)
x = (x >> 1) ^ ((bit(x, 0) ^ bit(x, 16) ^ (uint32_t)bit(key, r & 63) ^
bit(KEELOQ_NLF, g5(x, 1, 9, 20, 26, 31)))
<< 31);
return x;
}
/** Simple Learning Decrypt
* @param data - keeloq encrypt data
* @param key - manufacture (64bit)
* @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
*/
inline uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key) {
uint32_t x = data, r;
for(r = 0; r < 528; r++)
x = (x << 1) ^ bit(x, 31) ^ bit(x, 15) ^ (uint32_t)bit(key, (15 - r) & 63) ^
bit(KEELOQ_NLF, g5(x, 0, 8, 19, 25, 30));
return x;
}
/** Normal Learning
* @param data - serial number (28bit)
* @param key - manufacture (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key) {
uint32_t k1, k2;
data &= 0x0FFFFFFF;
data |= 0x20000000;
k1 = subghz_protocol_keeloq_common_decrypt(data, key);
data &= 0x0FFFFFFF;
data |= 0x60000000;
k2 = subghz_protocol_keeloq_common_decrypt(data, key);
return ((uint64_t)k2 << 32) | k1; // key - shifrovanoya
}
/** Secure Learning
* @param data - serial number (28bit)
* @param seed - seed number (32bit)
* @param key - manufacture (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t subghz_protocol_keeloq_common_secure_learning(
uint32_t data,
uint32_t seed,
const uint64_t key) {
uint32_t k1, k2;
data &= 0x0FFFFFFF;
k1 = subghz_protocol_keeloq_common_decrypt(data, key);
k2 = subghz_protocol_keeloq_common_decrypt(seed, key);
return ((uint64_t)k1 << 32) | k2;
}
/** Magic_xor_type1 Learning
* @param data - serial number (28bit)
* @param xor - magic xor (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t
subghz_protocol_keeloq_common_magic_xor_type1_learning(uint32_t data, uint64_t xor) {
data &= 0x0FFFFFFF;
return (((uint64_t)data << 32) | data) ^ xor;
}
/** Faac SLH (Spa) Learning
* @param seed - seed number (32bit)
* @param key - mfkey (64bit)
* @return man_learning for this seed number (64bit)
*/
inline uint64_t
subghz_protocol_keeloq_common_faac_learning(const uint32_t seed, const uint64_t key) {
uint16_t hs = seed >> 16;
const uint16_t ending = 0x544D;
uint32_t lsb = (uint32_t)hs << 16 | ending;
uint64_t man = (uint64_t)subghz_protocol_keeloq_common_encrypt(seed, key) << 32 |
subghz_protocol_keeloq_common_encrypt(lsb, key);
return man;
}
/** Magic_serial_type1 Learning
* @param data - serial number (28bit)
* @param man - magic man (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t
subghz_protocol_keeloq_common_magic_serial_type1_learning(uint32_t data, uint64_t man) {
return (man & 0xFFFFFFFF) | ((uint64_t)data << 40) |
((uint64_t)(((data & 0xff) + ((data >> 8) & 0xFF)) & 0xFF) << 32);
}
/** Magic_serial_type2 Learning
* @param data - btn+serial number (32bit)
* @param man - magic man (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t
subghz_protocol_keeloq_common_magic_serial_type2_learning(uint32_t data, uint64_t man) {
uint8_t* p = (uint8_t*)&data;
uint8_t* m = (uint8_t*)&man;
m[7] = p[0];
m[6] = p[1];
m[5] = p[2];
m[4] = p[3];
return man;
}
/** Magic_serial_type3 Learning
* @param data - serial number (24bit)
* @param man - magic man (64bit)
* @return manufacture for this serial number (64bit)
*/
inline uint64_t
subghz_protocol_keeloq_common_magic_serial_type3_learning(uint32_t data, uint64_t man) {
return (man & 0xFFFFFFFFFF000000) | (data & 0xFFFFFF);
}
| 4,379
|
C
|
.c
| 120
| 32.641667
| 98
| 0.644088
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,250
|
intertechno_v3.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/intertechno_v3.c
|
#include "intertechno_v3.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolIntertechnoV3"
#define CH_PATTERN "%c%c%c%c"
#define CNT_TO_CH(ch) \
(ch & 0x8 ? '1' : '0'), (ch & 0x4 ? '1' : '0'), (ch & 0x2 ? '1' : '0'), (ch & 0x1 ? '1' : '0')
#define INTERTECHNO_V3_DIMMING_COUNT_BIT 36
static const SubGhzBlockConst subghz_protocol_intertechno_v3_const = {
.te_short = 275,
.te_long = 1375,
.te_delta = 150,
.min_count_bit_for_found = 32,
};
struct SubGhzProtocolDecoderIntertechno_V3 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderIntertechno_V3 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
IntertechnoV3DecoderStepReset = 0,
IntertechnoV3DecoderStepStartSync,
IntertechnoV3DecoderStepFoundSync,
IntertechnoV3DecoderStepStartDuration,
IntertechnoV3DecoderStepSaveDuration,
IntertechnoV3DecoderStepCheckDuration,
IntertechnoV3DecoderStepEndDuration,
} IntertechnoV3DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_intertechno_v3_decoder = {
.alloc = subghz_protocol_decoder_intertechno_v3_alloc,
.free = subghz_protocol_decoder_intertechno_v3_free,
.feed = subghz_protocol_decoder_intertechno_v3_feed,
.reset = subghz_protocol_decoder_intertechno_v3_reset,
.get_hash_data = subghz_protocol_decoder_intertechno_v3_get_hash_data,
.serialize = subghz_protocol_decoder_intertechno_v3_serialize,
.deserialize = subghz_protocol_decoder_intertechno_v3_deserialize,
.get_string = subghz_protocol_decoder_intertechno_v3_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_intertechno_v3_encoder = {
.alloc = subghz_protocol_encoder_intertechno_v3_alloc,
.free = subghz_protocol_encoder_intertechno_v3_free,
.deserialize = subghz_protocol_encoder_intertechno_v3_deserialize,
.stop = subghz_protocol_encoder_intertechno_v3_stop,
.yield = subghz_protocol_encoder_intertechno_v3_yield,
};
const SubGhzProtocol subghz_protocol_intertechno_v3 = {
.name = SUBGHZ_PROTOCOL_INTERTECHNO_V3_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_intertechno_v3_decoder,
.encoder = &subghz_protocol_intertechno_v3_encoder,
};
void* subghz_protocol_encoder_intertechno_v3_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderIntertechno_V3* instance =
malloc(sizeof(SubGhzProtocolEncoderIntertechno_V3));
instance->base.protocol = &subghz_protocol_intertechno_v3;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_intertechno_v3_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderIntertechno_V3* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
* @return true On success
*/
static bool subghz_protocol_encoder_intertechno_v3_get_upload(
SubGhzProtocolEncoderIntertechno_V3* instance) {
furi_assert(instance);
size_t index = 0;
//Send header
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short * 38);
//Send sync
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short * 10);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if((instance->generic.data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) && (i == 9)) {
//send bit dimm
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
} else if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_intertechno_v3_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_intertechno_v3_const.te_long);
}
}
instance->encoder.size_upload = index;
return true;
}
SubGhzProtocolStatus subghz_protocol_encoder_intertechno_v3_deserialize(
void* context,
FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderIntertechno_V3* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if((instance->generic.data_count_bit !=
subghz_protocol_intertechno_v3_const.min_count_bit_for_found) &&
(instance->generic.data_count_bit != INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_intertechno_v3_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_intertechno_v3_stop(void* context) {
SubGhzProtocolEncoderIntertechno_V3* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_intertechno_v3_yield(void* context) {
SubGhzProtocolEncoderIntertechno_V3* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_intertechno_v3_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderIntertechno_V3* instance =
malloc(sizeof(SubGhzProtocolDecoderIntertechno_V3));
instance->base.protocol = &subghz_protocol_intertechno_v3;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_intertechno_v3_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
free(instance);
}
void subghz_protocol_decoder_intertechno_v3_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
void subghz_protocol_decoder_intertechno_v3_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
switch(instance->decoder.parser_step) {
case IntertechnoV3DecoderStepReset:
if((!level) &&
(DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short * 37) <
subghz_protocol_intertechno_v3_const.te_delta * 15)) {
instance->decoder.parser_step = IntertechnoV3DecoderStepStartSync;
}
break;
case IntertechnoV3DecoderStepStartSync:
if(level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta)) {
instance->decoder.parser_step = IntertechnoV3DecoderStepFoundSync;
} else {
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
break;
case IntertechnoV3DecoderStepFoundSync:
if(!level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short * 10) <
subghz_protocol_intertechno_v3_const.te_delta * 3)) {
instance->decoder.parser_step = IntertechnoV3DecoderStepStartDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
break;
case IntertechnoV3DecoderStepStartDuration:
if(level && (DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta)) {
instance->decoder.parser_step = IntertechnoV3DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
break;
case IntertechnoV3DecoderStepSaveDuration:
if(!level) { //save interval
if(duration >= (subghz_protocol_intertechno_v3_const.te_short * 11)) {
instance->decoder.parser_step = IntertechnoV3DecoderStepStartSync;
if((instance->decoder.decode_count_bit ==
subghz_protocol_intertechno_v3_const.min_count_bit_for_found) ||
(instance->decoder.decode_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
instance->decoder.te_last = duration;
instance->decoder.parser_step = IntertechnoV3DecoderStepCheckDuration;
} else {
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
break;
case IntertechnoV3DecoderStepCheckDuration:
if(level) {
//Add 0 bit
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
} else if(
//Add 1 bit
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_long) <
subghz_protocol_intertechno_v3_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
} else if(
//Add dimm_state
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta) &&
(instance->decoder.decode_count_bit == 27)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = IntertechnoV3DecoderStepEndDuration;
} else
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
} else {
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
break;
case IntertechnoV3DecoderStepEndDuration:
if(!level && ((DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_short) <
subghz_protocol_intertechno_v3_const.te_delta) ||
(DURATION_DIFF(duration, subghz_protocol_intertechno_v3_const.te_long) <
subghz_protocol_intertechno_v3_const.te_delta * 2))) {
instance->decoder.parser_step = IntertechnoV3DecoderStepStartDuration;
} else {
instance->decoder.parser_step = IntertechnoV3DecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_intertechno_v3_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* A frame is either 32 or 36 bits:
*
* _
* start bit: | |__________ (T,10T)
* _ _
* '0': | |_| |_____ (T,T,T,5T)
* _ _
* '1': | |_____| |_ (T,5T,T,T)
* _ _
* dimm: | |_| |_ (T,T,T,T)
*
* _
* stop bit: | |____...____ (T,38T)
*
* if frame 32 bits
* SSSSSSSSSSSSSSSSSSSSSSSSSS all_ch on/off ~ch
* Key:0x3F86C59F => 00111111100001101100010110 0 1 1111
*
* if frame 36 bits
* SSSSSSSSSSSSSSSSSSSSSSSSSS all_ch dimm ~ch dimm_level
* Key:0x42D2E8856 => 01000010110100101110100010 0 X 0101 0110
*
*/
if(instance->data_count_bit == subghz_protocol_intertechno_v3_const.min_count_bit_for_found) {
instance->serial = (instance->data >> 6) & 0x3FFFFFF;
if((instance->data >> 5) & 0x1) {
instance->cnt = 1 << 5;
} else {
instance->cnt = (~instance->data & 0xF);
}
instance->btn = (instance->data >> 4) & 0x1;
} else if(instance->data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) {
instance->serial = (instance->data >> 10) & 0x3FFFFFF;
if((instance->data >> 9) & 0x1) {
instance->cnt = 1 << 5;
} else {
instance->cnt = (~(instance->data >> 4) & 0xF);
}
instance->btn = (instance->data) & 0xF;
} else {
instance->serial = 0;
instance->cnt = 0;
instance->btn = 0;
}
}
uint8_t subghz_protocol_decoder_intertechno_v3_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_intertechno_v3_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus subghz_protocol_decoder_intertechno_v3_deserialize(
void* context,
FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if((instance->generic.data_count_bit !=
subghz_protocol_intertechno_v3_const.min_count_bit_for_found) &&
(instance->generic.data_count_bit != INTERTECHNO_V3_DIMMING_COUNT_BIT)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
} while(false);
return ret;
}
void subghz_protocol_decoder_intertechno_v3_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderIntertechno_V3* instance = context;
subghz_protocol_intertechno_v3_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%.11s %db\r\n"
"Key:0x%08llX\r\n"
"Sn:%07lX\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
instance->generic.data,
instance->generic.serial);
if(instance->generic.data_count_bit ==
subghz_protocol_intertechno_v3_const.min_count_bit_for_found) {
if(instance->generic.cnt >> 5) {
furi_string_cat_printf(
output, "Ch: All Btn:%s\r\n", (instance->generic.btn ? "On" : "Off"));
} else {
furi_string_cat_printf(
output,
"Ch:" CH_PATTERN " Btn:%s\r\n",
CNT_TO_CH(instance->generic.cnt),
(instance->generic.btn ? "On" : "Off"));
}
} else if(instance->generic.data_count_bit == INTERTECHNO_V3_DIMMING_COUNT_BIT) {
furi_string_cat_printf(
output,
"Ch:" CH_PATTERN " Dimm:%d%%\r\n",
CNT_TO_CH(instance->generic.cnt),
(int)(6.67f * (float)instance->generic.btn));
}
}
| 19,428
|
C
|
.c
| 423
| 37.695035
| 99
| 0.656994
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,251
|
keeloq_common.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/keeloq_common.h
|
#pragma once
#include "base.h"
#include <furi.h>
/*
* Keeloq
* https://ru.wikipedia.org/wiki/KeeLoq
* https://phreakerclub.com/forum/showthread.php?t=1094
*
*/
#define KEELOQ_NLF 0x3A5C742E
/*
* KeeLoq learning types
* https://phreakerclub.com/forum/showthread.php?t=67
*/
#define KEELOQ_LEARNING_UNKNOWN 0u
#define KEELOQ_LEARNING_SIMPLE 1u
#define KEELOQ_LEARNING_NORMAL 2u
#define KEELOQ_LEARNING_SECURE 3u
#define KEELOQ_LEARNING_MAGIC_XOR_TYPE_1 4u
#define KEELOQ_LEARNING_FAAC 5u
#define KEELOQ_LEARNING_MAGIC_SERIAL_TYPE_1 6u
#define KEELOQ_LEARNING_MAGIC_SERIAL_TYPE_2 7u
#define KEELOQ_LEARNING_MAGIC_SERIAL_TYPE_3 8u
/**
* Simple Learning Encrypt
* @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
* @param key - manufacture (64bit)
* @return keeloq encrypt data
*/
uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key);
/**
* Simple Learning Decrypt
* @param data - keeloq encrypt data
* @param key - manufacture (64bit)
* @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter
*/
uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key);
/**
* Normal Learning
* @param data - serial number (28bit)
* @param key - manufacture (64bit)
* @return manufacture for this serial number (64bit)
*/
uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key);
/**
* Secure Learning
* @param data - serial number (28bit)
* @param seed - seed number (32bit)
* @param key - manufacture (64bit)
* @return manufacture for this serial number (64bit)
*/
uint64_t
subghz_protocol_keeloq_common_secure_learning(uint32_t data, uint32_t seed, const uint64_t key);
/**
* Magic_xor_type1 Learning
* @param data - serial number (28bit)
* @param xor - magic xor (64bit)
* @return manufacture for this serial number (64bit)
*/
uint64_t subghz_protocol_keeloq_common_magic_xor_type1_learning(uint32_t data, uint64_t xor);
/** Faac SLH (Spa) Learning
* @param seed - seed number (32bit)
* @param key - mfkey (64bit)
* @return man_learning for this fix number (64bit)
*/
uint64_t subghz_protocol_keeloq_common_faac_learning(const uint32_t seed, const uint64_t key);
/** Magic_serial_type1 Learning
* @param data - serial number (28bit)
* @param man - magic man (64bit)
* @return manufacture for this serial number (64bit)
*/
uint64_t subghz_protocol_keeloq_common_magic_serial_type1_learning(uint32_t data, uint64_t man);
/** Magic_serial_type2 Learning
* @param data - btn+serial number (32bit)
* @param man - magic man (64bit)
* @return manufacture for this serial number (64bit)
*/
uint64_t subghz_protocol_keeloq_common_magic_serial_type2_learning(uint32_t data, uint64_t man);
/** Magic_serial_type3 Learning
* @param data - btn+serial number (32bit)
* @param man - magic man (64bit)
* @return manufacture for this serial number (64bit)
*/
uint64_t subghz_protocol_keeloq_common_magic_serial_type3_learning(uint32_t data, uint64_t man);
| 3,111
|
C
|
.c
| 84
| 35.154762
| 100
| 0.731318
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,252
|
intertechno_v3.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/intertechno_v3.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_INTERTECHNO_V3_NAME "Intertechno_V3"
typedef struct SubGhzProtocolDecoderIntertechno_V3 SubGhzProtocolDecoderIntertechno_V3;
typedef struct SubGhzProtocolEncoderIntertechno_V3 SubGhzProtocolEncoderIntertechno_V3;
extern const SubGhzProtocolDecoder subghz_protocol_intertechno_v3_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_intertechno_v3_encoder;
extern const SubGhzProtocol subghz_protocol_intertechno_v3;
/**
* Allocate SubGhzProtocolEncoderIntertechno_V3.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderIntertechno_V3* pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
*/
void* subghz_protocol_encoder_intertechno_v3_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderIntertechno_V3.
* @param context Pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
*/
void subghz_protocol_encoder_intertechno_v3_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return Starus error
*/
SubGhzProtocolStatus subghz_protocol_encoder_intertechno_v3_deserialize(
void* context,
FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
*/
void subghz_protocol_encoder_intertechno_v3_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderIntertechno_V3 instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_intertechno_v3_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderIntertechno_V3.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderIntertechno_V3* pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
*/
void* subghz_protocol_decoder_intertechno_v3_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderIntertechno_V3.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
*/
void subghz_protocol_decoder_intertechno_v3_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderIntertechno_V3.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
*/
void subghz_protocol_decoder_intertechno_v3_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_intertechno_v3_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_intertechno_v3_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderIntertechno_V3.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return Starus error
*/
SubGhzProtocolStatus subghz_protocol_decoder_intertechno_v3_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderIntertechno_V3.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return Starus error
*/
SubGhzProtocolStatus subghz_protocol_decoder_intertechno_v3_deserialize(
void* context,
FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderIntertechno_V3 instance
* @param output Resulting text
*/
void subghz_protocol_decoder_intertechno_v3_get_string(void* context, FuriString* output);
| 4,166
|
C
|
.c
| 94
| 42.265957
| 105
| 0.821208
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,253
|
secplus_v1.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/secplus_v1.c
|
#include "secplus_v1.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
/*
* Help
* https://github.com/argilo/secplus
* https://github.com/merbanan/rtl_433/blob/master/src/devices/secplus_v1.c
*/
#define TAG "SubGhzProtocoSecPlusV1"
#define SECPLUS_V1_BIT_ERR -1 //0b0000
#define SECPLUS_V1_BIT_0 0 //0b0001
#define SECPLUS_V1_BIT_1 1 //0b0011
#define SECPLUS_V1_BIT_2 2 //0b0111
#define SECPLUS_V1_PACKET_1_HEADER 0x00
#define SECPLUS_V1_PACKET_2_HEADER 0x02
#define SECPLUS_V1_PACKET_1_INDEX_BASE 0
#define SECPLUS_V1_PACKET_2_INDEX_BASE 21
#define SECPLUS_V1_PACKET_1_ACCEPTED (1 << 0)
#define SECPLUS_V1_PACKET_2_ACCEPTED (1 << 1)
static const SubGhzBlockConst subghz_protocol_secplus_v1_const = {
.te_short = 500,
.te_long = 1500,
.te_delta = 100,
.min_count_bit_for_found = 21,
};
struct SubGhzProtocolDecoderSecPlus_v1 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint8_t packet_accepted;
uint8_t base_packet_index;
uint8_t data_array[44];
};
struct SubGhzProtocolEncoderSecPlus_v1 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
uint8_t data_array[44];
};
typedef enum {
SecPlus_v1DecoderStepReset = 0,
SecPlus_v1DecoderStepSearchStartBit,
SecPlus_v1DecoderStepSaveDuration,
SecPlus_v1DecoderStepDecoderData,
} SecPlus_v1DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_secplus_v1_decoder = {
.alloc = subghz_protocol_decoder_secplus_v1_alloc,
.free = subghz_protocol_decoder_secplus_v1_free,
.feed = subghz_protocol_decoder_secplus_v1_feed,
.reset = subghz_protocol_decoder_secplus_v1_reset,
.get_hash_data = subghz_protocol_decoder_secplus_v1_get_hash_data,
.serialize = subghz_protocol_decoder_secplus_v1_serialize,
.deserialize = subghz_protocol_decoder_secplus_v1_deserialize,
.get_string = subghz_protocol_decoder_secplus_v1_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_secplus_v1_encoder = {
.alloc = subghz_protocol_encoder_secplus_v1_alloc,
.free = subghz_protocol_encoder_secplus_v1_free,
.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 |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Send | SubGhzProtocolFlag_Save,
.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_running = 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;
}
SubGhzProtocolStatus
subghz_protocol_encoder_secplus_v1_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderSecPlus_v1* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
2 * subghz_protocol_secplus_v1_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_secplus_v1_encode(instance)) {
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!subghz_protocol_encoder_secplus_v1_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
;
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");
ret = SubGhzProtocolStatusErrorParserKey;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_secplus_v1_stop(void* context) {
SubGhzProtocolEncoderSecPlus_v1* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_secplus_v1_yield(void* context) {
SubGhzProtocolEncoderSecPlus_v1* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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));
instance->base.protocol = &subghz_protocol_secplus_v1;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_secplus_v1_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v1* instance = context;
free(instance);
}
void subghz_protocol_decoder_secplus_v1_reset(void* context) {
furi_assert(context);
// SubGhzProtocolDecoderSecPlus_v1* instance = context;
// does not reset the decoder because you need to get 2 parts of the package
}
/**
* Security+ 1.0 message decoding
* @param instance SubGhzProtocolDecoderSecPlus_v1*
*/
static void subghz_protocol_secplus_v1_decode(SubGhzProtocolDecoderSecPlus_v1* instance) {
uint32_t rolling = 0;
uint32_t fixed = 0;
uint32_t acc = 0;
uint8_t digit = 0;
//decode packet 1
for(uint8_t i = 1; i < 21; i += 2) {
digit = instance->data_array[i];
rolling = (rolling * 3) + digit;
acc += digit;
digit = (60 + instance->data_array[i + 1] - acc) % 3;
fixed = (fixed * 3) + digit;
acc += digit;
}
acc = 0;
//decode packet 2
for(uint8_t i = 22; i < 42; i += 2) {
digit = instance->data_array[i];
rolling = (rolling * 3) + digit;
acc += digit;
digit = (60 + instance->data_array[i + 1] - acc) % 3;
fixed = (fixed * 3) + digit;
acc += digit;
}
rolling = subghz_protocol_blocks_reverse_key(rolling, 32);
instance->generic.data = (uint64_t)fixed << 32 | rolling;
instance->generic.data_count_bit =
subghz_protocol_secplus_v1_const.min_count_bit_for_found * 2;
}
void subghz_protocol_decoder_secplus_v1_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v1* instance = context;
switch(instance->decoder.parser_step) {
case SecPlus_v1DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 120) <
subghz_protocol_secplus_v1_const.te_delta * 120)) {
//Found header Security+ 1.0
instance->decoder.parser_step = SecPlus_v1DecoderStepSearchStartBit;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->packet_accepted = 0;
memset(instance->data_array, 0, sizeof(instance->data_array));
}
break;
case SecPlus_v1DecoderStepSearchStartBit:
if(level) {
if(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short) <
subghz_protocol_secplus_v1_const.te_delta) {
instance->base_packet_index = SECPLUS_V1_PACKET_1_INDEX_BASE;
instance
->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
SECPLUS_V1_BIT_0;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
} else if(
DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_long) <
subghz_protocol_secplus_v1_const.te_delta) {
instance->base_packet_index = SECPLUS_V1_PACKET_2_INDEX_BASE;
instance
->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
SECPLUS_V1_BIT_2;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
}
} else {
instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
}
break;
case SecPlus_v1DecoderStepSaveDuration:
if(!level) { //save interval
if(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 120) <
subghz_protocol_secplus_v1_const.te_delta * 120) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_secplus_v1_const.min_count_bit_for_found) {
if(instance->base_packet_index == SECPLUS_V1_PACKET_1_INDEX_BASE)
instance->packet_accepted |= SECPLUS_V1_PACKET_1_ACCEPTED;
if(instance->base_packet_index == SECPLUS_V1_PACKET_2_INDEX_BASE)
instance->packet_accepted |= SECPLUS_V1_PACKET_2_ACCEPTED;
if(instance->packet_accepted ==
(SECPLUS_V1_PACKET_1_ACCEPTED | SECPLUS_V1_PACKET_2_ACCEPTED)) {
subghz_protocol_secplus_v1_decode(instance);
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
}
}
instance->decoder.parser_step = SecPlus_v1DecoderStepSearchStartBit;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = SecPlus_v1DecoderStepDecoderData;
}
} else {
instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
}
break;
case SecPlus_v1DecoderStepDecoderData:
if(level && (instance->decoder.decode_count_bit <=
subghz_protocol_secplus_v1_const.min_count_bit_for_found)) {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_secplus_v1_const.te_short * 3) <
subghz_protocol_secplus_v1_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short) <
subghz_protocol_secplus_v1_const.te_delta)) {
instance
->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
SECPLUS_V1_BIT_0;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_secplus_v1_const.te_short * 2) <
subghz_protocol_secplus_v1_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 2) <
subghz_protocol_secplus_v1_const.te_delta * 2)) {
instance
->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
SECPLUS_V1_BIT_1;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_secplus_v1_const.te_short) <
subghz_protocol_secplus_v1_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_secplus_v1_const.te_short * 3) <
subghz_protocol_secplus_v1_const.te_delta * 3)) {
instance
->data_array[instance->decoder.decode_count_bit + instance->base_packet_index] =
SECPLUS_V1_BIT_2;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = SecPlus_v1DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
}
} else {
instance->decoder.parser_step = SecPlus_v1DecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_secplus_v1_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v1* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_secplus_v1_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v1* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_secplus_v1_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v1* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
2 * subghz_protocol_secplus_v1_const.min_count_bit_for_found);
}
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; //-V560
} while(false);
return true;
}
void subghz_protocol_decoder_secplus_v1_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v1* instance = context;
uint32_t fixed = (instance->generic.data >> 32) & 0xFFFFFFFF;
instance->generic.cnt = instance->generic.data & 0xFFFFFFFF;
instance->generic.btn = fixed % 3;
uint8_t id0 = (fixed / 3) % 3;
uint8_t id1 = (fixed / 9) % 3;
uint16_t pin = 0;
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key:%lX%08lX\r\n"
"id1:%d id0:%d",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
id1,
id0);
if(id1 == 0) {
// (fixed // 3**3) % (3**7) 3^3=27 3^73=72187
instance->generic.serial = (fixed / 27) % 2187;
// pin = (fixed // 3**10) % (3**9) 3^10=59049 3^9=19683
pin = (fixed / 59049) % 19683;
if(pin <= 9999) {
furi_string_cat_printf(output, " pin:%d", pin);
} else if(pin <= 11029) {
furi_string_cat_printf(output, " pin:enter");
}
int pin_suffix = 0;
// pin_suffix = (fixed // 3**19) % 3 3^19=1162261467
pin_suffix = (fixed / 1162261467) % 3;
if(pin_suffix == 1) {
furi_string_cat_printf(output, " #\r\n");
} else if(pin_suffix == 2) {
furi_string_cat_printf(output, " *\r\n");
} else {
furi_string_cat_printf(output, "\r\n");
}
furi_string_cat_printf(
output,
"Sn:0x%08lX\r\n"
"Cnt:0x%03lX "
"SwID:0x%X\r\n",
instance->generic.serial,
instance->generic.cnt,
instance->generic.btn);
} else {
//id = fixed / 27;
instance->generic.serial = fixed / 27;
if(instance->generic.btn == 1) {
furi_string_cat_printf(output, " Btn:left\r\n");
} else if(instance->generic.btn == 0) {
furi_string_cat_printf(output, " Btn:middle\r\n");
} else if(instance->generic.btn == 2) { //-V547
furi_string_cat_printf(output, " Btn:right\r\n");
}
furi_string_cat_printf(
output,
"Sn:0x%08lX\r\n"
"Cnt:0x%03lX "
"SwID:0x%X\r\n",
instance->generic.serial,
instance->generic.cnt,
instance->generic.btn);
}
}
| 23,435
|
C
|
.c
| 542
| 34.523985
| 100
| 0.626622
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,254
|
princeton.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/princeton.c
|
#include "princeton.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
/*
* Help
* https://phreakerclub.com/447
*
*/
#define TAG "SubGhzProtocolPrinceton"
#define PRINCETON_GUARD_TIME_DEFALUT 30 //GUARD_TIME = PRINCETON_GUARD_TIME_DEFALUT * TE
// Guard Time value should be between 15 -> 72 otherwise default value will be used
static const SubGhzBlockConst subghz_protocol_princeton_const = {
.te_short = 390,
.te_long = 1170,
.te_delta = 300,
.min_count_bit_for_found = 24,
};
struct SubGhzProtocolDecoderPrinceton {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint32_t te;
uint32_t last_data;
uint32_t guard_time;
};
struct SubGhzProtocolEncoderPrinceton {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
uint32_t te;
uint32_t guard_time;
};
typedef enum {
PrincetonDecoderStepReset = 0,
PrincetonDecoderStepSaveDuration,
PrincetonDecoderStepCheckDuration,
} PrincetonDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_princeton_decoder = {
.alloc = subghz_protocol_decoder_princeton_alloc,
.free = subghz_protocol_decoder_princeton_free,
.feed = subghz_protocol_decoder_princeton_feed,
.reset = subghz_protocol_decoder_princeton_reset,
.get_hash_data = subghz_protocol_decoder_princeton_get_hash_data,
.serialize = subghz_protocol_decoder_princeton_serialize,
.deserialize = subghz_protocol_decoder_princeton_deserialize,
.get_string = subghz_protocol_decoder_princeton_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_princeton_encoder = {
.alloc = subghz_protocol_encoder_princeton_alloc,
.free = subghz_protocol_encoder_princeton_free,
.deserialize = subghz_protocol_encoder_princeton_deserialize,
.stop = subghz_protocol_encoder_princeton_stop,
.yield = subghz_protocol_encoder_princeton_yield,
};
const SubGhzProtocol subghz_protocol_princeton = {
.name = SUBGHZ_PROTOCOL_PRINCETON_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send | SubGhzProtocolFlag_Princeton,
.decoder = &subghz_protocol_princeton_decoder,
.encoder = &subghz_protocol_princeton_encoder,
};
void* subghz_protocol_encoder_princeton_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderPrinceton* instance = malloc(sizeof(SubGhzProtocolEncoderPrinceton));
instance->base.protocol = &subghz_protocol_princeton;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52; //max 24bit*2 + 2 (start, stop)
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_princeton_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderPrinceton* instance = context;
free(instance->encoder.upload);
free(instance);
}
// Get custom button code
static uint8_t subghz_protocol_princeton_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x1:
btn = 0x2;
break;
case 0x2:
btn = 0x1;
break;
case 0x4:
btn = 0x2;
break;
case 0x8:
btn = 0x2;
break;
case 0xF:
btn = 0x2;
break;
// Second encoding type
case 0x30:
btn = 0xC0;
break;
case 0xC0:
btn = 0x30;
break;
case 0xF3:
btn = 0xC0;
break;
case 0xFC:
btn = 0xC0;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x1:
btn = 0x4;
break;
case 0x2:
btn = 0x4;
break;
case 0x4:
btn = 0x1;
break;
case 0x8:
btn = 0x1;
break;
case 0xF:
btn = 0x1;
break;
// Second encoding type
case 0x30:
btn = 0xF3;
break;
case 0xC0:
btn = 0xF3;
break;
case 0xF3:
btn = 0x30;
break;
case 0xFC:
btn = 0xF3;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0x1:
btn = 0x8;
break;
case 0x2:
btn = 0x8;
break;
case 0x4:
btn = 0x8;
break;
case 0x8:
btn = 0x4;
break;
case 0xF:
btn = 0x4;
break;
// Second encoding type
case 0x30:
btn = 0xFC;
break;
case 0xC0:
btn = 0xFC;
break;
case 0xF3:
btn = 0xFC;
break;
case 0xFC:
btn = 0x30;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_RIGHT) {
switch(original_btn_code) {
case 0x1:
btn = 0xF;
break;
case 0x2:
btn = 0xF;
break;
case 0x4:
btn = 0xF;
break;
case 0x8:
btn = 0xF;
break;
case 0xF:
btn = 0x8;
break;
default:
break;
}
}
return btn;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderPrinceton instance
* @return true On success
*/
static bool
subghz_protocol_encoder_princeton_get_upload(SubGhzProtocolEncoderPrinceton* instance) {
furi_assert(instance);
// Generate new key using custom or default button
instance->generic.btn = subghz_protocol_princeton_get_btn_code();
// Reconstruction of the data
// If we have 8bit button code move serial to left by 8 bits (and 4 if 4 bits)
if(instance->generic.btn == 0x30 || instance->generic.btn == 0xC0) {
instance->generic.data =
((uint64_t)instance->generic.serial << 8 | (uint64_t)instance->generic.btn);
} else if(instance->generic.btn == 0xF3 || instance->generic.btn == 0xFC) {
instance->generic.data =
((uint64_t)instance->generic.serial << 8 | (uint64_t)(instance->generic.btn & 0xF));
} else {
instance->generic.data =
((uint64_t)instance->generic.serial << 4 | (uint64_t)instance->generic.btn);
}
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)instance->te * 3);
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te);
} else {
//send bit 0
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)instance->te * 3);
}
}
//Send Stop bit
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
//Send PT_GUARD_TIME
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)instance->te * instance->guard_time);
return true;
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_princeton_check_remote_controller(SubGhzBlockGeneric* instance) {
// Parse button modes for second encoding type (and serial is smaller)
// Button code is 8bit and has fixed values of one of these
// Exclude button code for each type from serial number before parsing
if((instance->data & 0xFF) == 0x30 || (instance->data & 0xFF) == 0xC0) {
// Save serial and button code
instance->serial = instance->data >> 8;
instance->btn = instance->data & 0xFF;
} else if((instance->data & 0xFF) == 0x03 || (instance->data & 0xFF) == 0x0C) {
// Fix for button code 0x03 and 0x0C having zero at the beggining
instance->serial = instance->data >> 8;
instance->btn = (instance->data & 0xFF) | 0xF0;
} else {
instance->serial = instance->data >> 4;
instance->btn = instance->data & 0xF;
}
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(4);
}
SubGhzProtocolStatus
subghz_protocol_encoder_princeton_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderPrinceton* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_princeton_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
FURI_LOG_E(TAG, "Missing TE");
ret = SubGhzProtocolStatusErrorParserTe;
break;
}
//optional parameter parameter
if(!flipper_format_read_uint32(
flipper_format, "Guard_time", (uint32_t*)&instance->guard_time, 1)) {
instance->guard_time = PRINCETON_GUARD_TIME_DEFALUT;
} else {
// Guard Time value should be between 15 -> 72 otherwise default value will be used
if((instance->guard_time < 15) || (instance->guard_time > 72)) {
instance->guard_time = PRINCETON_GUARD_TIME_DEFALUT;
}
}
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
// Get button and serial before calling get_upload
subghz_protocol_princeton_check_remote_controller(&instance->generic);
if(!subghz_protocol_encoder_princeton_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_princeton_stop(void* context) {
SubGhzProtocolEncoderPrinceton* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_princeton_yield(void* context) {
SubGhzProtocolEncoderPrinceton* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_princeton_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderPrinceton* instance = malloc(sizeof(SubGhzProtocolDecoderPrinceton));
instance->base.protocol = &subghz_protocol_princeton;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_princeton_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
free(instance);
}
void subghz_protocol_decoder_princeton_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
instance->decoder.parser_step = PrincetonDecoderStepReset;
instance->last_data = 0;
}
void subghz_protocol_decoder_princeton_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
switch(instance->decoder.parser_step) {
case PrincetonDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_princeton_const.te_short * 36) <
subghz_protocol_princeton_const.te_delta * 36)) {
//Found Preambula
instance->decoder.parser_step = PrincetonDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->te = 0;
instance->guard_time = PRINCETON_GUARD_TIME_DEFALUT;
}
break;
case PrincetonDecoderStepSaveDuration:
//save duration
if(level) {
instance->decoder.te_last = duration;
instance->te += duration;
instance->decoder.parser_step = PrincetonDecoderStepCheckDuration;
}
break;
case PrincetonDecoderStepCheckDuration:
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_princeton_const.te_long * 2)) {
instance->decoder.parser_step = PrincetonDecoderStepSaveDuration;
if(instance->decoder.decode_count_bit ==
subghz_protocol_princeton_const.min_count_bit_for_found) {
if((instance->last_data == instance->decoder.decode_data) &&
instance->last_data) {
instance->te /= (instance->decoder.decode_count_bit * 4 + 1);
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
instance->guard_time = roundf((float)duration / instance->te);
// Guard Time value should be between 15 -> 72 otherwise default value will be used
if((instance->guard_time < 15) || (instance->guard_time > 72)) {
instance->guard_time = PRINCETON_GUARD_TIME_DEFALUT;
}
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->last_data = instance->decoder.decode_data;
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->te = 0;
break;
}
instance->te += duration;
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_princeton_const.te_short) <
subghz_protocol_princeton_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_princeton_const.te_long) <
subghz_protocol_princeton_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = PrincetonDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_princeton_const.te_long) <
subghz_protocol_princeton_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_princeton_const.te_short) <
subghz_protocol_princeton_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = PrincetonDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = PrincetonDecoderStepReset;
}
} else {
instance->decoder.parser_step = PrincetonDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_princeton_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_princeton_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
SubGhzProtocolStatus ret =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_write_uint32(flipper_format, "TE", &instance->te, 1)) {
FURI_LOG_E(TAG, "Unable to add TE");
ret = SubGhzProtocolStatusErrorParserTe;
}
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_write_uint32(flipper_format, "Guard_time", &instance->guard_time, 1)) {
FURI_LOG_E(TAG, "Unable to add Guard_time");
ret = SubGhzProtocolStatusErrorParserOthers;
}
return ret;
}
SubGhzProtocolStatus
subghz_protocol_decoder_princeton_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_princeton_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
FURI_LOG_E(TAG, "Missing TE");
ret = SubGhzProtocolStatusErrorParserTe;
break;
}
if(!flipper_format_read_uint32(
flipper_format, "Guard_time", (uint32_t*)&instance->guard_time, 1)) {
instance->guard_time = PRINCETON_GUARD_TIME_DEFALUT;
} else {
// Guard Time value should be between 15 -> 72 otherwise default value will be used
if((instance->guard_time < 15) || (instance->guard_time > 72)) {
instance->guard_time = PRINCETON_GUARD_TIME_DEFALUT;
}
}
} while(false);
return ret;
}
void subghz_protocol_decoder_princeton_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderPrinceton* instance = context;
subghz_protocol_princeton_check_remote_controller(&instance->generic);
uint32_t data_rev = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
if(instance->generic.btn == 0x30 || instance->generic.btn == 0xC0 ||
instance->generic.btn == 0xF3 || instance->generic.btn == 0xFC) {
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%08lX\r\n"
"Yek:0x%08lX\r\n"
"Sn:0x%05lX Btn:%02X (8b)\r\n"
"Te:%luus GT:Te*%lu\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFF),
data_rev,
instance->generic.serial,
(instance->generic.btn == 0xF3 || instance->generic.btn == 0xFC) ?
instance->generic.btn & 0xF :
instance->generic.btn,
instance->te,
instance->guard_time);
} else {
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%08lX\r\n"
"Yek:0x%08lX\r\n"
"Sn:0x%05lX Btn:%01X (4b)\r\n"
"Te:%luus GT:Te*%lu\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFF),
data_rev,
instance->generic.serial,
instance->generic.btn,
instance->te,
instance->guard_time);
}
}
| 22,011
|
C
|
.c
| 560
| 30.335714
| 107
| 0.615845
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,255
|
holtek.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/holtek.c
|
#include "holtek.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
/*
* Help
* https://pdf1.alldatasheet.com/datasheet-pdf/view/82103/HOLTEK/HT640.html
* https://fccid.io/OJM-CMD-HHLR-XXXA
*
*/
#define TAG "SubGhzProtocolHoltek"
#define HOLTEK_HEADER_MASK 0xF000000000
#define HOLTEK_HEADER 0x5000000000
static const SubGhzBlockConst subghz_protocol_holtek_const = {
.te_short = 430,
.te_long = 870,
.te_delta = 100,
.min_count_bit_for_found = 40,
};
struct SubGhzProtocolDecoderHoltek {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderHoltek {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
HoltekDecoderStepReset = 0,
HoltekDecoderStepFoundStartBit,
HoltekDecoderStepSaveDuration,
HoltekDecoderStepCheckDuration,
} HoltekDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_holtek_decoder = {
.alloc = subghz_protocol_decoder_holtek_alloc,
.free = subghz_protocol_decoder_holtek_free,
.feed = subghz_protocol_decoder_holtek_feed,
.reset = subghz_protocol_decoder_holtek_reset,
.get_hash_data = subghz_protocol_decoder_holtek_get_hash_data,
.serialize = subghz_protocol_decoder_holtek_serialize,
.deserialize = subghz_protocol_decoder_holtek_deserialize,
.get_string = subghz_protocol_decoder_holtek_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_holtek_encoder = {
.alloc = subghz_protocol_encoder_holtek_alloc,
.free = subghz_protocol_encoder_holtek_free,
.deserialize = subghz_protocol_encoder_holtek_deserialize,
.stop = subghz_protocol_encoder_holtek_stop,
.yield = subghz_protocol_encoder_holtek_yield,
};
const SubGhzProtocol subghz_protocol_holtek = {
.name = SUBGHZ_PROTOCOL_HOLTEK_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_holtek_decoder,
.encoder = &subghz_protocol_holtek_encoder,
};
void* subghz_protocol_encoder_holtek_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderHoltek* instance = malloc(sizeof(SubGhzProtocolEncoderHoltek));
instance->base.protocol = &subghz_protocol_holtek;
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_running = false;
return instance;
}
void subghz_protocol_encoder_holtek_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderHoltek* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderHoltek instance
* @return true On success
*/
static bool subghz_protocol_encoder_holtek_get_upload(SubGhzProtocolEncoderHoltek* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_holtek_const.te_short * 36);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_holtek_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_holtek_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_holtek_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_holtek_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_holtek_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_holtek_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderHoltek* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_holtek_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_holtek_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_holtek_stop(void* context) {
SubGhzProtocolEncoderHoltek* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_holtek_yield(void* context) {
SubGhzProtocolEncoderHoltek* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_holtek_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderHoltek* instance = malloc(sizeof(SubGhzProtocolDecoderHoltek));
instance->base.protocol = &subghz_protocol_holtek;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_holtek_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
free(instance);
}
void subghz_protocol_decoder_holtek_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
instance->decoder.parser_step = HoltekDecoderStepReset;
}
void subghz_protocol_decoder_holtek_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
switch(instance->decoder.parser_step) {
case HoltekDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_holtek_const.te_short * 36) <
subghz_protocol_holtek_const.te_delta * 36)) {
//Found Preambula
instance->decoder.parser_step = HoltekDecoderStepFoundStartBit;
}
break;
case HoltekDecoderStepFoundStartBit:
if((level) && (DURATION_DIFF(duration, subghz_protocol_holtek_const.te_short) <
subghz_protocol_holtek_const.te_delta)) {
//Found StartBit
instance->decoder.parser_step = HoltekDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = HoltekDecoderStepReset;
}
break;
case HoltekDecoderStepSaveDuration:
//save duration
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_holtek_const.te_short * 10 +
subghz_protocol_holtek_const.te_delta)) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_holtek_const.min_count_bit_for_found) {
if((instance->decoder.decode_data & HOLTEK_HEADER_MASK) == HOLTEK_HEADER) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = HoltekDecoderStepFoundStartBit;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = HoltekDecoderStepCheckDuration;
}
} else {
instance->decoder.parser_step = HoltekDecoderStepReset;
}
break;
case HoltekDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_holtek_const.te_short) <
subghz_protocol_holtek_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_holtek_const.te_long) <
subghz_protocol_holtek_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = HoltekDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_holtek_const.te_long) <
subghz_protocol_holtek_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_holtek_const.te_short) <
subghz_protocol_holtek_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = HoltekDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = HoltekDecoderStepReset;
}
} else {
instance->decoder.parser_step = HoltekDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_holtek_check_remote_controller(SubGhzBlockGeneric* instance) {
if((instance->data & HOLTEK_HEADER_MASK) == HOLTEK_HEADER) {
instance->serial =
subghz_protocol_blocks_reverse_key((instance->data >> 16) & 0xFFFFF, 20);
uint16_t btn = instance->data & 0xFFFF;
if((btn & 0xf) != 0xA) {
instance->btn = 0x1 << 4 | (btn & 0xF);
} else if(((btn >> 4) & 0xF) != 0xA) {
instance->btn = 0x2 << 4 | ((btn >> 4) & 0xF);
} else if(((btn >> 8) & 0xF) != 0xA) {
instance->btn = 0x3 << 4 | ((btn >> 8) & 0xF);
} else if(((btn >> 12) & 0xF) != 0xA) {
instance->btn = 0x4 << 4 | ((btn >> 12) & 0xF);
} else {
instance->btn = 0;
}
} else {
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
}
}
uint8_t subghz_protocol_decoder_holtek_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_holtek_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_holtek_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_holtek_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_holtek_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderHoltek* instance = context;
subghz_protocol_holtek_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%05lX Btn:%X ",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)((instance->generic.data >> 32) & 0xFFFFFFFF),
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
instance->generic.serial,
instance->generic.btn >> 4);
if((instance->generic.btn & 0xF) == 0xE) {
furi_string_cat_printf(output, "ON\r\n");
} else if((instance->generic.btn & 0xF) == 0xB) {
furi_string_cat_printf(output, "OFF\r\n");
}
}
| 13,473
|
C
|
.c
| 317
| 34.943218
| 98
| 0.666031
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,256
|
clemsa.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/clemsa.c
|
#include "clemsa.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
// protocol BERNER / ELKA / TEDSEN / TELETASTER
#define TAG "SubGhzProtocolClemsa"
#define DIP_P 0b11 //(+)
#define DIP_O 0b10 //(0)
#define DIP_N 0b00 //(-)
#define DIP_PATTERN "%c%c%c%c%c%c%c%c"
#define SHOW_DIP_P(dip, check_dip) \
((((dip >> 0xE) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xC) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xA) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x8) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x6) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x4) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x2) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x0) & 0x3) == check_dip) ? '*' : '_')
static const SubGhzBlockConst subghz_protocol_clemsa_const = {
.te_short = 385,
.te_long = 2695,
.te_delta = 150,
.min_count_bit_for_found = 18,
};
struct SubGhzProtocolDecoderClemsa {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderClemsa {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
ClemsaDecoderStepReset = 0,
ClemsaDecoderStepSaveDuration,
ClemsaDecoderStepCheckDuration,
} ClemsaDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_clemsa_decoder = {
.alloc = subghz_protocol_decoder_clemsa_alloc,
.free = subghz_protocol_decoder_clemsa_free,
.feed = subghz_protocol_decoder_clemsa_feed,
.reset = subghz_protocol_decoder_clemsa_reset,
.get_hash_data = subghz_protocol_decoder_clemsa_get_hash_data,
.serialize = subghz_protocol_decoder_clemsa_serialize,
.deserialize = subghz_protocol_decoder_clemsa_deserialize,
.get_string = subghz_protocol_decoder_clemsa_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_clemsa_encoder = {
.alloc = subghz_protocol_encoder_clemsa_alloc,
.free = subghz_protocol_encoder_clemsa_free,
.deserialize = subghz_protocol_encoder_clemsa_deserialize,
.stop = subghz_protocol_encoder_clemsa_stop,
.yield = subghz_protocol_encoder_clemsa_yield,
};
const SubGhzProtocol subghz_protocol_clemsa = {
.name = SUBGHZ_PROTOCOL_CLEMSA_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_clemsa_decoder,
.encoder = &subghz_protocol_clemsa_encoder,
};
void* subghz_protocol_encoder_clemsa_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderClemsa* instance = malloc(sizeof(SubGhzProtocolEncoderClemsa));
instance->base.protocol = &subghz_protocol_clemsa;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_clemsa_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderClemsa* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderClemsa instance
* @return true On success
*/
static bool subghz_protocol_encoder_clemsa_get_upload(SubGhzProtocolEncoderClemsa* 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, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_clemsa_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_clemsa_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_clemsa_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_clemsa_const.te_long);
}
}
if(bit_read(instance->generic.data, 0)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_clemsa_const.te_long);
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_clemsa_const.te_short +
subghz_protocol_clemsa_const.te_long * 7);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_clemsa_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_clemsa_const.te_long +
subghz_protocol_clemsa_const.te_long * 7);
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_clemsa_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderClemsa* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_clemsa_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_clemsa_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_clemsa_stop(void* context) {
SubGhzProtocolEncoderClemsa* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_clemsa_yield(void* context) {
SubGhzProtocolEncoderClemsa* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_clemsa_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderClemsa* instance = malloc(sizeof(SubGhzProtocolDecoderClemsa));
instance->base.protocol = &subghz_protocol_clemsa;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_clemsa_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
free(instance);
}
void subghz_protocol_decoder_clemsa_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
instance->decoder.parser_step = ClemsaDecoderStepReset;
}
void subghz_protocol_decoder_clemsa_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
switch(instance->decoder.parser_step) {
case ClemsaDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_clemsa_const.te_short * 51) <
subghz_protocol_clemsa_const.te_delta * 25)) {
instance->decoder.parser_step = ClemsaDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
break;
case ClemsaDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = ClemsaDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = ClemsaDecoderStepReset;
}
break;
case ClemsaDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_clemsa_const.te_short) <
subghz_protocol_clemsa_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_clemsa_const.te_long) <
subghz_protocol_clemsa_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = ClemsaDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_clemsa_const.te_long) <
subghz_protocol_clemsa_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_clemsa_const.te_short) <
subghz_protocol_clemsa_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = ClemsaDecoderStepSaveDuration;
} else if(
DURATION_DIFF(duration, subghz_protocol_clemsa_const.te_short * 51) <
subghz_protocol_clemsa_const.te_delta * 25) {
if(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_clemsa_const.te_short) <
subghz_protocol_clemsa_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
} else if(
DURATION_DIFF(instance->decoder.te_last, subghz_protocol_clemsa_const.te_long) <
subghz_protocol_clemsa_const.te_delta * 3) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else {
instance->decoder.parser_step = ClemsaDecoderStepReset;
}
if(instance->decoder.decode_count_bit ==
subghz_protocol_clemsa_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.parser_step = ClemsaDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = ClemsaDecoderStepReset;
}
} else {
instance->decoder.parser_step = ClemsaDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_clemsa_check_remote_controller(SubGhzBlockGeneric* instance) {
instance->serial = (instance->data >> 2) & 0xFFFF;
instance->btn = (instance->data & 0x03);
}
uint8_t subghz_protocol_decoder_clemsa_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_clemsa_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_clemsa_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_clemsa_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_clemsa_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderClemsa* instance = context;
subghz_protocol_clemsa_check_remote_controller(&instance->generic);
//uint32_t data = (uint32_t)(instance->generic.data & 0xFFFFFF);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%05lX Btn %X\r\n"
" +: " DIP_PATTERN "\r\n"
" o: " DIP_PATTERN "\r\n"
" -: " DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0x3FFFF),
instance->generic.btn,
SHOW_DIP_P(instance->generic.serial, DIP_P),
SHOW_DIP_P(instance->generic.serial, DIP_O),
SHOW_DIP_P(instance->generic.serial, DIP_N));
}
| 13,618
|
C
|
.c
| 307
| 36.677524
| 100
| 0.658222
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,258
|
raw.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/raw.c
|
#include "raw.h"
#include <lib/flipper_format/flipper_format.h>
#include "../subghz_file_encoder_worker.h"
#include "../blocks/const.h"
#include "../blocks/generic.h"
#include <flipper_format/flipper_format_i.h>
#include <lib/toolbox/stream/stream.h>
#define TAG "SubGhzProtocolRaw"
#define SUBGHZ_DOWNLOAD_MAX_SIZE 512
static const SubGhzBlockConst subghz_protocol_raw_const = {
.te_short = 50,
.te_long = 32700,
.te_delta = 0,
.min_count_bit_for_found = 0,
};
struct SubGhzProtocolDecoderRAW {
SubGhzProtocolDecoderBase base;
int32_t* upload_raw;
uint16_t ind_write;
Storage* storage;
FlipperFormat* flipper_file;
uint32_t file_is_open;
FuriString* file_name;
size_t sample_write;
bool last_level;
bool pause;
};
struct SubGhzProtocolEncoderRAW {
SubGhzProtocolEncoderBase base;
bool is_running;
FuriString* file_name;
FuriString* radio_device_name;
SubGhzFileEncoderWorker* file_worker_encoder;
};
typedef enum {
RAWFileIsOpenClose = 0,
RAWFileIsOpenWrite,
RAWFileIsOpenRead,
} RAWFilIsOpen;
const SubGhzProtocolDecoder subghz_protocol_raw_decoder = {
.alloc = subghz_protocol_decoder_raw_alloc,
.free = subghz_protocol_decoder_raw_free,
.feed = subghz_protocol_decoder_raw_feed,
.reset = subghz_protocol_decoder_raw_reset,
.get_hash_data = NULL,
.serialize = NULL,
.deserialize = subghz_protocol_decoder_raw_deserialize,
.get_string = subghz_protocol_decoder_raw_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_raw_encoder = {
.alloc = subghz_protocol_encoder_raw_alloc,
.free = subghz_protocol_encoder_raw_free,
.deserialize = subghz_protocol_encoder_raw_deserialize,
.stop = subghz_protocol_encoder_raw_stop,
.yield = subghz_protocol_encoder_raw_yield,
};
const SubGhzProtocol subghz_protocol_raw = {
.name = SUBGHZ_PROTOCOL_RAW_NAME,
.type = SubGhzProtocolTypeRAW,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_RAW |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_raw_decoder,
.encoder = &subghz_protocol_raw_encoder,
};
bool subghz_protocol_raw_save_to_file_init(
SubGhzProtocolDecoderRAW* instance,
const char* dev_name,
SubGhzRadioPreset* preset) {
furi_check(instance);
instance->storage = furi_record_open(RECORD_STORAGE);
instance->flipper_file = flipper_format_file_alloc(instance->storage);
FuriString* temp_str;
temp_str = furi_string_alloc();
bool init = false;
do {
// Create subghz folder directory if necessary
if(!storage_simply_mkdir(instance->storage, SUBGHZ_RAW_FOLDER)) {
break;
}
// Create saved directory if necessary
if(!storage_simply_mkdir(instance->storage, SUBGHZ_RAW_FOLDER)) {
break;
}
furi_string_set(instance->file_name, dev_name);
// First remove subghz device file if it was saved
furi_string_printf(
temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, dev_name, SUBGHZ_APP_FILENAME_EXTENSION);
if(!storage_simply_remove(instance->storage, furi_string_get_cstr(temp_str))) {
break;
}
// Open file
if(!flipper_format_file_open_always(
instance->flipper_file, furi_string_get_cstr(temp_str))) {
FURI_LOG_E(TAG, "Unable to open file for write: %s", furi_string_get_cstr(temp_str));
break;
}
if(!flipper_format_write_header_cstr(
instance->flipper_file, SUBGHZ_RAW_FILE_TYPE, SUBGHZ_RAW_FILE_VERSION)) {
FURI_LOG_E(TAG, "Unable to add header");
break;
}
if(!flipper_format_write_uint32(
instance->flipper_file, "Frequency", &preset->frequency, 1)) {
FURI_LOG_E(TAG, "Unable to add Frequency");
break;
}
subghz_block_generic_get_preset_name(furi_string_get_cstr(preset->name), temp_str);
if(!flipper_format_write_string_cstr(
instance->flipper_file, "Preset", furi_string_get_cstr(temp_str))) {
FURI_LOG_E(TAG, "Unable to add Preset");
break;
}
if(!strcmp(furi_string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
if(!flipper_format_write_string_cstr(
instance->flipper_file, "Custom_preset_module", "CC1101")) {
FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
break;
}
if(!flipper_format_write_hex(
instance->flipper_file, "Custom_preset_data", preset->data, preset->data_size)) {
FURI_LOG_E(TAG, "Unable to add Custom_preset_data");
break;
}
}
if(!flipper_format_write_string_cstr(
instance->flipper_file, "Protocol", instance->base.protocol->name)) {
FURI_LOG_E(TAG, "Unable to add Protocol");
break;
}
instance->upload_raw = malloc(SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof(int32_t));
instance->file_is_open = RAWFileIsOpenWrite;
instance->sample_write = 0;
instance->last_level = false;
instance->pause = false;
init = true;
} while(0);
furi_string_free(temp_str);
return init;
}
static bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolDecoderRAW* instance) {
furi_assert(instance);
bool is_write = false;
if(instance->file_is_open == RAWFileIsOpenWrite) {
if(!flipper_format_write_int32(
instance->flipper_file, "RAW_Data", instance->upload_raw, instance->ind_write)) {
FURI_LOG_E(TAG, "Unable to add RAW_Data");
} else {
instance->sample_write += instance->ind_write;
instance->ind_write = 0;
is_write = true;
}
}
return is_write;
}
void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolDecoderRAW* instance) {
furi_check(instance);
if(instance->file_is_open == RAWFileIsOpenWrite && instance->ind_write)
subghz_protocol_raw_save_to_file_write(instance);
if(instance->file_is_open != RAWFileIsOpenClose) {
free(instance->upload_raw);
instance->upload_raw = NULL;
flipper_format_file_close(instance->flipper_file);
flipper_format_free(instance->flipper_file);
furi_record_close(RECORD_STORAGE);
}
instance->file_is_open = RAWFileIsOpenClose;
}
void subghz_protocol_raw_save_to_file_pause(SubGhzProtocolDecoderRAW* instance, bool pause) {
furi_check(instance);
if(instance->pause != pause) {
instance->pause = pause;
}
}
size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolDecoderRAW* instance) {
furi_check(instance);
return instance->sample_write + instance->ind_write;
}
void* subghz_protocol_decoder_raw_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderRAW* instance = malloc(sizeof(SubGhzProtocolDecoderRAW));
instance->base.protocol = &subghz_protocol_raw;
instance->upload_raw = NULL;
instance->ind_write = 0;
instance->last_level = false;
instance->file_is_open = RAWFileIsOpenClose;
instance->file_name = furi_string_alloc();
return instance;
}
void subghz_protocol_decoder_raw_free(void* context) {
furi_check(context);
SubGhzProtocolDecoderRAW* instance = context;
furi_string_free(instance->file_name);
free(instance);
}
void subghz_protocol_decoder_raw_reset(void* context) {
furi_check(context);
SubGhzProtocolDecoderRAW* instance = context;
instance->ind_write = 0;
instance->last_level = false;
}
void subghz_protocol_decoder_raw_feed(void* context, bool level, uint32_t duration) {
furi_check(context);
SubGhzProtocolDecoderRAW* instance = context;
// Add check if we got duration higher than 1 second, we skipping it, temp fix
if((!instance->pause && (instance->upload_raw != NULL)) && (duration < ((uint32_t)1000000))) {
if(duration > subghz_protocol_raw_const.te_short) {
if(instance->last_level != level) {
instance->last_level = (level ? true : false);
instance->upload_raw[instance->ind_write++] = (level ? duration : -duration);
}
}
if(instance->ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE) {
subghz_protocol_raw_save_to_file_write(instance);
}
}
}
SubGhzProtocolStatus
subghz_protocol_decoder_raw_deserialize(void* context, FlipperFormat* flipper_format) {
furi_check(context);
UNUSED(context);
UNUSED(flipper_format);
// stub, for backwards compatibility
return SubGhzProtocolStatusOk;
}
void subghz_protocol_decoder_raw_get_string(void* context, FuriString* output) {
furi_check(context);
//SubGhzProtocolDecoderRAW* instance = context;
UNUSED(context);
furi_string_cat_printf(output, "RAW Data");
}
void* subghz_protocol_encoder_raw_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderRAW* instance = malloc(sizeof(SubGhzProtocolEncoderRAW));
instance->base.protocol = &subghz_protocol_raw;
instance->file_name = furi_string_alloc();
instance->radio_device_name = furi_string_alloc();
instance->is_running = false;
return instance;
}
void subghz_protocol_encoder_raw_stop(void* context) {
furi_check(context);
SubGhzProtocolEncoderRAW* instance = context;
instance->is_running = false;
if(subghz_file_encoder_worker_is_running(instance->file_worker_encoder)) {
subghz_file_encoder_worker_stop(instance->file_worker_encoder);
subghz_file_encoder_worker_free(instance->file_worker_encoder);
}
}
void subghz_protocol_encoder_raw_free(void* context) {
furi_check(context);
SubGhzProtocolEncoderRAW* instance = context;
subghz_protocol_encoder_raw_stop(instance);
furi_string_free(instance->file_name);
furi_string_free(instance->radio_device_name);
free(instance);
}
void subghz_protocol_raw_file_encoder_worker_set_callback_end(
SubGhzProtocolEncoderRAW* instance,
SubGhzProtocolEncoderRAWCallbackEnd callback_end,
void* context_end) {
furi_check(instance);
furi_check(callback_end);
subghz_file_encoder_worker_callback_end(
instance->file_worker_encoder, callback_end, context_end);
}
static bool subghz_protocol_encoder_raw_worker_init(SubGhzProtocolEncoderRAW* instance) {
furi_assert(instance);
instance->file_worker_encoder = subghz_file_encoder_worker_alloc();
if(subghz_file_encoder_worker_start(
instance->file_worker_encoder,
furi_string_get_cstr(instance->file_name),
furi_string_get_cstr(instance->radio_device_name))) {
//the worker needs a file in order to open and read part of the file
furi_delay_ms(100);
instance->is_running = true;
} else {
subghz_protocol_encoder_raw_stop(instance);
}
return instance->is_running;
}
void subghz_protocol_raw_gen_fff_data(
FlipperFormat* flipper_format,
const char* file_path,
const char* radio_device_name) {
furi_check(flipper_format);
do {
stream_clean(flipper_format_get_raw_stream(flipper_format));
if(!flipper_format_write_string_cstr(flipper_format, "Protocol", "RAW")) {
FURI_LOG_E(TAG, "Unable to add Protocol");
break;
}
if(!flipper_format_write_string_cstr(flipper_format, "File_name", file_path)) {
FURI_LOG_E(TAG, "Unable to add File_name");
break;
}
if(!flipper_format_write_string_cstr(
flipper_format, "Radio_device_name", radio_device_name)) {
FURI_LOG_E(TAG, "Unable to add Radio_device_name");
break;
}
} while(false);
}
SubGhzProtocolStatus
subghz_protocol_encoder_raw_deserialize(void* context, FlipperFormat* flipper_format) {
furi_check(context);
SubGhzProtocolEncoderRAW* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
FuriString* temp_str;
temp_str = furi_string_alloc();
do {
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
res = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_string(flipper_format, "File_name", temp_str)) {
FURI_LOG_E(TAG, "Missing File_name");
res = SubGhzProtocolStatusErrorParserOthers;
break;
}
furi_string_set(instance->file_name, temp_str);
if(!flipper_format_read_string(flipper_format, "Radio_device_name", temp_str)) {
FURI_LOG_E(TAG, "Missing Radio_device_name");
res = SubGhzProtocolStatusErrorParserOthers;
break;
}
furi_string_set(instance->radio_device_name, temp_str);
if(!subghz_protocol_encoder_raw_worker_init(instance)) {
res = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
res = SubGhzProtocolStatusOk;
} while(false);
furi_string_free(temp_str);
return res;
}
LevelDuration subghz_protocol_encoder_raw_yield(void* context) {
furi_check(context);
SubGhzProtocolEncoderRAW* instance = context;
if(!instance->is_running) return level_duration_reset();
return subghz_file_encoder_worker_get_level_duration(instance->file_worker_encoder);
}
| 13,669
|
C
|
.c
| 343
| 32.953353
| 100
| 0.670738
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,259
|
gangqi.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/gangqi.c
|
#include "gangqi.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
#define TAG "SubGhzProtocolGangQi"
static const SubGhzBlockConst subghz_protocol_gangqi_const = {
.te_short = 500,
.te_long = 1200,
.te_delta = 200,
.min_count_bit_for_found = 34,
};
struct SubGhzProtocolDecoderGangQi {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderGangQi {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
GangQiDecoderStepReset = 0,
GangQiDecoderStepSaveDuration,
GangQiDecoderStepCheckDuration,
} GangQiDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_gangqi_decoder = {
.alloc = subghz_protocol_decoder_gangqi_alloc,
.free = subghz_protocol_decoder_gangqi_free,
.feed = subghz_protocol_decoder_gangqi_feed,
.reset = subghz_protocol_decoder_gangqi_reset,
.get_hash_data = subghz_protocol_decoder_gangqi_get_hash_data,
.serialize = subghz_protocol_decoder_gangqi_serialize,
.deserialize = subghz_protocol_decoder_gangqi_deserialize,
.get_string = subghz_protocol_decoder_gangqi_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_gangqi_encoder = {
.alloc = subghz_protocol_encoder_gangqi_alloc,
.free = subghz_protocol_encoder_gangqi_free,
.deserialize = subghz_protocol_encoder_gangqi_deserialize,
.stop = subghz_protocol_encoder_gangqi_stop,
.yield = subghz_protocol_encoder_gangqi_yield,
};
const SubGhzProtocol subghz_protocol_gangqi = {
.name = SUBGHZ_PROTOCOL_GANGQI_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_gangqi_decoder,
.encoder = &subghz_protocol_gangqi_encoder,
};
void* subghz_protocol_encoder_gangqi_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderGangQi* instance = malloc(sizeof(SubGhzProtocolEncoderGangQi));
instance->base.protocol = &subghz_protocol_gangqi;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_gangqi_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderGangQi* instance = context;
free(instance->encoder.upload);
free(instance);
}
// Get custom button code
static uint8_t subghz_protocol_gangqi_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0xD:
btn = 0xE;
break;
case 0xE:
btn = 0xD;
break;
case 0xB:
btn = 0xD;
break;
case 0x7:
btn = 0xD;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0xD:
btn = 0xB;
break;
case 0xE:
btn = 0xB;
break;
case 0xB:
btn = 0xE;
break;
case 0x7:
btn = 0xE;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0xD:
btn = 0x7;
break;
case 0xE:
btn = 0x7;
break;
case 0xB:
btn = 0x7;
break;
case 0x7:
btn = 0xB;
break;
default:
break;
}
}
return btn;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderGangQi instance
*/
static void subghz_protocol_encoder_gangqi_get_upload(SubGhzProtocolEncoderGangQi* instance) {
furi_assert(instance);
// Generate new key using custom or default button
instance->generic.btn = subghz_protocol_gangqi_get_btn_code();
uint64_t new_key = (instance->generic.data >> 14) << 14 | (instance->generic.btn << 10) |
(0b01 << 8);
uint8_t crc = -0xD7 - ((new_key >> 32) & 0xFF) - ((new_key >> 24) & 0xFF) -
((new_key >> 16) & 0xFF) - ((new_key >> 8) & 0xFF);
instance->generic.data = (new_key | crc);
size_t index = 0;
// Send key and GAP between parcels
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
// Send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_gangqi_const.te_long);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_gangqi_const.te_short * 4 +
subghz_protocol_gangqi_const.te_delta);
} else {
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_gangqi_const.te_short);
}
} else {
// Send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_gangqi_const.te_short);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_gangqi_const.te_short * 4 +
subghz_protocol_gangqi_const.te_delta);
} else {
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_gangqi_const.te_long);
}
}
}
instance->encoder.size_upload = index;
return;
}
/**
* Analysis of received data and parsing serial number
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_gangqi_remote_controller(SubGhzBlockGeneric* instance) {
instance->btn = (instance->data >> 10) & 0xF;
instance->serial = (instance->data & 0xFFFFF0000) >> 16;
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(3);
// GangQi Decoder
// 09.2024 - @xMasterX (MMX)
// Thanks @Skorpionm for support!
//// 4D=F8=171=229 byte sum should be always the same
// Button
// Serial || BBBB || CRC (byte sum) with overflow and starting point 0xD7
//034AAB75BC = 00110100101010101011 01 1101 01 101111 00 // A (0xD)
//034AAB79B8 = 00110100101010101011 01 1110 01 101110 00 // B (0xE)
//034AAB6DC4 = 00110100101010101011 01 1011 01 110001 00 // C (0xB)
//034AAB5DD4 = 00110100101010101011 01 0111 01 110101 00 // D (0x7)
//034AAB55DC = 00110100101010101011 01 0101 01 110111 00 // Settings (0x5)
//034AAB51E0 = 00110100101010101011 01 0100 01 111000 00 // A (0x4)
//034AAB49E8 = 00110100101010101011 01 0010 01 111010 00 // C (0x2)
//034AAB59D8 = 00110100101010101011 01 0110 01 110110 00 // D (0x6)
//034AAB45EC = 00110100101010101011 01 0001 01 111011 00 // Settings exit (0x1)
//
// Serial 3 bytes should meet requirements see validation example at subghz_protocol_decoder_gangqi_get_string
//
// Code for finding start byte for crc sum
//
//uint64_t test = 0x034AAB79B8; //B8
//for(size_t byte = 0; byte < 0xFF; ++byte) {
// uint8_t crc_res = -byte - ((test >> 32) & 0xFF) - ((test >> 24) & 0xFF) -
// ((test >> 16) & 0xFF) - ((test >> 8) & 0xFF);
// if(crc_res == 0xB8) {
// uint64_t test2 = 0x034AAB6DC4; //C4
// uint8_t crc_res2 = -byte - ((test2 >> 32) & 0xFF) - ((test2 >> 24) & 0xFF) -
// ((test2 >> 16) & 0xFF) - ((test2 >> 8) & 0xFF);
// if(crc_res2 == 0xC4) {
// printf("Start byte for CRC = %02lX / CRC = %02X \n", byte, crc_res);
//
// printf("Testing second parcel CRC = %02X", crc_res2);
// }
// }
// }
}
SubGhzProtocolStatus
subghz_protocol_encoder_gangqi_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderGangQi* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_gangqi_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_gangqi_remote_controller(&instance->generic);
subghz_protocol_encoder_gangqi_get_upload(instance);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_gangqi_stop(void* context) {
SubGhzProtocolEncoderGangQi* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_gangqi_yield(void* context) {
SubGhzProtocolEncoderGangQi* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_gangqi_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderGangQi* instance = malloc(sizeof(SubGhzProtocolDecoderGangQi));
instance->base.protocol = &subghz_protocol_gangqi;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_gangqi_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
free(instance);
}
void subghz_protocol_decoder_gangqi_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
instance->decoder.parser_step = GangQiDecoderStepReset;
}
void subghz_protocol_decoder_gangqi_feed(void* context, bool level, volatile uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
switch(instance->decoder.parser_step) {
case GangQiDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_gangqi_const.te_long * 2) <
subghz_protocol_gangqi_const.te_delta * 3)) {
//Found GAP
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = GangQiDecoderStepSaveDuration;
}
break;
case GangQiDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = GangQiDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = GangQiDecoderStepReset;
}
break;
case GangQiDecoderStepCheckDuration:
if(!level) {
// Bit 0 is short and long timing
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_gangqi_const.te_short) <
subghz_protocol_gangqi_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_gangqi_const.te_long) <
subghz_protocol_gangqi_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = GangQiDecoderStepSaveDuration;
// Bit 1 is long and short timing
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_gangqi_const.te_long) <
subghz_protocol_gangqi_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_gangqi_const.te_short) <
subghz_protocol_gangqi_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = GangQiDecoderStepSaveDuration;
} else if(
// End of the key
DURATION_DIFF(duration, subghz_protocol_gangqi_const.te_short * 4) <
subghz_protocol_gangqi_const.te_delta) {
//Found next GAP and add bit 0 or 1 (only bit 0 was found on the remotes)
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_gangqi_const.te_short) <
subghz_protocol_gangqi_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_gangqi_const.te_short * 4) <
subghz_protocol_gangqi_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
}
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_gangqi_const.te_long) <
subghz_protocol_gangqi_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_gangqi_const.te_short * 4) <
subghz_protocol_gangqi_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
}
// If got 34 bits key reading is finished
if(instance->decoder.decode_count_bit ==
subghz_protocol_gangqi_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = GangQiDecoderStepReset;
} else {
instance->decoder.parser_step = GangQiDecoderStepReset;
}
} else {
instance->decoder.parser_step = GangQiDecoderStepReset;
}
break;
}
}
/**
* Get button name.
* @param btn Button number, 4 bit
*/
static const char* subghz_protocol_gangqi_get_button_name(uint8_t btn) {
const char* name_btn[16] = {
"Unknown",
"Exit settings",
"Volume setting",
"0x3",
"Vibro sens. setting",
"Settings mode",
"Ringtone setting",
"Ring", // D
"0x8",
"0x9",
"0xA",
"Alarm", // C
"0xC",
"Arm", // A
"Disarm", // B
"0xF"};
return btn <= 0xf ? name_btn[btn] : name_btn[0];
}
uint8_t subghz_protocol_decoder_gangqi_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_gangqi_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_gangqi_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_gangqi_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_gangqi_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderGangQi* instance = context;
// Parse serial
subghz_protocol_gangqi_remote_controller(&instance->generic);
// Get CRC
uint8_t crc = -0xD7 - ((instance->generic.data >> 32) & 0xFF) -
((instance->generic.data >> 24) & 0xFF) -
((instance->generic.data >> 16) & 0xFF) - ((instance->generic.data >> 8) & 0xFF);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key: 0x%X%08lX\r\n"
"Serial: 0x%05lX CRC: 0x%02X\r\n"
"Btn: 0x%01X - %s\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint8_t)(instance->generic.data >> 32),
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
instance->generic.serial,
crc,
instance->generic.btn,
subghz_protocol_gangqi_get_button_name(instance->generic.btn));
}
| 18,468
|
C
|
.c
| 444
| 33.191441
| 114
| 0.620665
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,260
|
nice_flor_s.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/nice_flor_s.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_NICE_FLOR_S_NAME "Nice FloR-S"
typedef struct SubGhzProtocolDecoderNiceFlorS SubGhzProtocolDecoderNiceFlorS;
typedef struct SubGhzProtocolEncoderNiceFlorS SubGhzProtocolEncoderNiceFlorS;
extern const SubGhzProtocolDecoder subghz_protocol_nice_flor_s_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_nice_flor_s_encoder;
extern const SubGhzProtocol subghz_protocol_nice_flor_s;
/**
* Allocate SubGhzProtocolEncoderNiceFlorS.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderNiceFlorS* pointer to a SubGhzProtocolEncoderNiceFlorS instance
*/
void* subghz_protocol_encoder_nice_flor_s_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderNiceFlorS.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlorS instance
*/
void subghz_protocol_encoder_nice_flor_s_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlorS instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
SubGhzProtocolStatus
subghz_protocol_encoder_nice_flor_s_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlorS instance
*/
void subghz_protocol_encoder_nice_flor_s_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderNiceFlorS instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_nice_flor_s_yield(void* context);
uint64_t subghz_protocol_nice_flor_s_encrypt(uint64_t data, const char* file_name);
/**
* Allocate SubGhzProtocolDecoderNiceFlorS.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderNiceFlorS* pointer to a SubGhzProtocolDecoderNiceFlorS instance
*/
void* subghz_protocol_decoder_nice_flor_s_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderNiceFlorS.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
*/
void subghz_protocol_decoder_nice_flor_s_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderNiceFlorS.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
*/
void subghz_protocol_decoder_nice_flor_s_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_nice_flor_s_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_nice_flor_s_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderNiceFlorS.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_nice_flor_s_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderNiceFlorS.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_nice_flor_s_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderNiceFlorS instance
* @param output Resulting text
*/
void subghz_protocol_decoder_nice_flor_s_get_string(void* context, FuriString* output);
| 4,048
|
C
|
.c
| 93
| 41.537634
| 98
| 0.815596
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,261
|
ido.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/ido.c
|
#include "ido.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolIdo117/111"
static const SubGhzBlockConst subghz_protocol_ido_const = {
.te_short = 450,
.te_long = 1450,
.te_delta = 150,
.min_count_bit_for_found = 48,
};
struct SubGhzProtocolDecoderIDo {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderIDo {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
IDoDecoderStepReset = 0,
IDoDecoderStepFoundPreambula,
IDoDecoderStepSaveDuration,
IDoDecoderStepCheckDuration,
} IDoDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_ido_decoder = {
.alloc = subghz_protocol_decoder_ido_alloc,
.free = subghz_protocol_decoder_ido_free,
.feed = subghz_protocol_decoder_ido_feed,
.reset = subghz_protocol_decoder_ido_reset,
.get_hash_data = subghz_protocol_decoder_ido_get_hash_data,
.deserialize = subghz_protocol_decoder_ido_deserialize,
.serialize = subghz_protocol_decoder_ido_serialize,
.get_string = subghz_protocol_decoder_ido_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_ido_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol subghz_protocol_ido = {
.name = SUBGHZ_PROTOCOL_IDO_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Save,
.decoder = &subghz_protocol_ido_decoder,
.encoder = &subghz_protocol_ido_encoder,
};
void* subghz_protocol_decoder_ido_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderIDo* instance = malloc(sizeof(SubGhzProtocolDecoderIDo));
instance->base.protocol = &subghz_protocol_ido;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_ido_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
free(instance);
}
void subghz_protocol_decoder_ido_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
instance->decoder.parser_step = IDoDecoderStepReset;
}
void subghz_protocol_decoder_ido_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
switch(instance->decoder.parser_step) {
case IDoDecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_ido_const.te_short * 10) <
subghz_protocol_ido_const.te_delta * 5)) {
instance->decoder.parser_step = IDoDecoderStepFoundPreambula;
}
break;
case IDoDecoderStepFoundPreambula:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_ido_const.te_short * 10) <
subghz_protocol_ido_const.te_delta * 5)) {
//Found Preambula
instance->decoder.parser_step = IDoDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = IDoDecoderStepReset;
}
break;
case IDoDecoderStepSaveDuration:
if(level) {
if(duration >= ((uint32_t)subghz_protocol_ido_const.te_short * 5 +
subghz_protocol_ido_const.te_delta)) {
instance->decoder.parser_step = IDoDecoderStepFoundPreambula;
if(instance->decoder.decode_count_bit >=
subghz_protocol_ido_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = IDoDecoderStepCheckDuration;
}
} else {
instance->decoder.parser_step = IDoDecoderStepReset;
}
break;
case IDoDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ido_const.te_short) <
subghz_protocol_ido_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_ido_const.te_long) <
subghz_protocol_ido_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = IDoDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_ido_const.te_short) <
subghz_protocol_ido_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_ido_const.te_short) <
subghz_protocol_ido_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = IDoDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = IDoDecoderStepReset;
}
} else {
instance->decoder.parser_step = IDoDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_ido_check_remote_controller(SubGhzBlockGeneric* instance) {
uint64_t code_found_reverse =
subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
uint32_t code_fix = code_found_reverse & 0xFFFFFF;
instance->serial = code_fix & 0xFFFFF;
instance->btn = (code_fix >> 20) & 0x0F;
}
uint8_t subghz_protocol_decoder_ido_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_ido_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_ido_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_ido_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_ido_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderIDo* instance = context;
subghz_protocol_ido_check_remote_controller(&instance->generic);
uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
uint32_t code_fix = code_found_reverse & 0xFFFFFF;
uint32_t code_hop = (code_found_reverse >> 24) & 0xFFFFFF;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Fix:%06lX \r\n"
"Hop:%06lX \r\n"
"Sn:%05lX Btn:%X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
code_fix,
code_hop,
instance->generic.serial,
instance->generic.btn);
}
| 8,127
|
C
|
.c
| 195
| 34.066667
| 95
| 0.670463
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,262
|
power_smart.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/power_smart.c
|
#include "power_smart.h"
#include <lib/toolbox/manchester_decoder.h>
#include <lib/toolbox/manchester_encoder.h>
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolPowerSmart"
#define POWER_SMART_PACKET_HEADER 0xFD000000AA000000
#define POWER_SMART_PACKET_HEADER_MASK 0xFF000000FF000000
#define CHANNEL_PATTERN "%c%c%c%c%c%c"
#define CNT_TO_CHANNEL(dip) \
(dip & 0x0001 ? '*' : '-'), (dip & 0x0002 ? '*' : '-'), (dip & 0x0004 ? '*' : '-'), \
(dip & 0x0008 ? '*' : '-'), (dip & 0x0010 ? '*' : '-'), (dip & 0x0020 ? '*' : '-')
static const SubGhzBlockConst subghz_protocol_power_smart_const = {
.te_short = 225,
.te_long = 450,
.te_delta = 100,
.min_count_bit_for_found = 64,
};
struct SubGhzProtocolDecoderPowerSmart {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
ManchesterState manchester_saved_state;
uint16_t header_count;
};
struct SubGhzProtocolEncoderPowerSmart {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
PowerSmartDecoderStepReset = 0,
PowerSmartDecoderFoundHeader,
PowerSmartDecoderStepDecoderData,
} PowerSmartDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_power_smart_decoder = {
.alloc = subghz_protocol_decoder_power_smart_alloc,
.free = subghz_protocol_decoder_power_smart_free,
.feed = subghz_protocol_decoder_power_smart_feed,
.reset = subghz_protocol_decoder_power_smart_reset,
.get_hash_data = subghz_protocol_decoder_power_smart_get_hash_data,
.serialize = subghz_protocol_decoder_power_smart_serialize,
.deserialize = subghz_protocol_decoder_power_smart_deserialize,
.get_string = subghz_protocol_decoder_power_smart_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_power_smart_encoder = {
.alloc = subghz_protocol_encoder_power_smart_alloc,
.free = subghz_protocol_encoder_power_smart_free,
.deserialize = subghz_protocol_encoder_power_smart_deserialize,
.stop = subghz_protocol_encoder_power_smart_stop,
.yield = subghz_protocol_encoder_power_smart_yield,
};
const SubGhzProtocol subghz_protocol_power_smart = {
.name = SUBGHZ_PROTOCOL_POWER_SMART_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_power_smart_decoder,
.encoder = &subghz_protocol_power_smart_encoder,
};
void* subghz_protocol_encoder_power_smart_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderPowerSmart* instance = malloc(sizeof(SubGhzProtocolEncoderPowerSmart));
instance->base.protocol = &subghz_protocol_power_smart;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 1024;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_power_smart_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderPowerSmart* instance = context;
free(instance->encoder.upload);
free(instance);
}
static LevelDuration
subghz_protocol_encoder_power_smart_add_duration_to_upload(ManchesterEncoderResult result) {
LevelDuration data = {.duration = 0, .level = 0};
switch(result) {
case ManchesterEncoderResultShortLow:
data.duration = subghz_protocol_power_smart_const.te_short;
data.level = false;
break;
case ManchesterEncoderResultLongLow:
data.duration = subghz_protocol_power_smart_const.te_long;
data.level = false;
break;
case ManchesterEncoderResultLongHigh:
data.duration = subghz_protocol_power_smart_const.te_long;
data.level = true;
break;
case ManchesterEncoderResultShortHigh:
data.duration = subghz_protocol_power_smart_const.te_short;
data.level = true;
break;
default:
furi_crash("SubGhz: ManchesterEncoderResult is incorrect.");
break;
}
return level_duration_make(data.level, data.duration);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderPowerSmart instance
*/
static void
subghz_protocol_encoder_power_smart_get_upload(SubGhzProtocolEncoderPowerSmart* instance) {
furi_assert(instance);
size_t index = 0;
ManchesterEncoderState enc_state;
manchester_encoder_reset(&enc_state);
ManchesterEncoderResult result;
for(int i = 8; i > 0; i--) {
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(!manchester_encoder_advance(
&enc_state, !bit_read(instance->generic.data, i - 1), &result)) {
instance->encoder.upload[index++] =
subghz_protocol_encoder_power_smart_add_duration_to_upload(result);
manchester_encoder_advance(
&enc_state, !bit_read(instance->generic.data, i - 1), &result);
}
instance->encoder.upload[index++] =
subghz_protocol_encoder_power_smart_add_duration_to_upload(result);
}
}
instance->encoder.upload[index] = subghz_protocol_encoder_power_smart_add_duration_to_upload(
manchester_encoder_finish(&enc_state));
if(level_duration_get_level(instance->encoder.upload[index])) {
index++;
}
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_power_smart_const.te_long * 1111);
instance->encoder.size_upload = index;
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_power_smart_remote_controller(SubGhzBlockGeneric* instance) {
/*
* Protocol: Manchester encoding, symbol rate ~2222.
* Packet Format:
* 0xFDXXXXYYAAZZZZWW where 0xFD and 0xAA sync word
* XXXX = ~ZZZZ, YY=(~WW)-1
* Example:
* SYNC1 K1 CHANNEL DATA1 K2 DATA2 SYNC2 ~K1 ~CHANNEL ~DATA2 ~K2 (~DATA2)-1
* 0xFD2137ACAADEC852 => 11111101 0 010000 10011011 1 10101100 10101010 1 1011110 1100100 0 01010010
* 0xFDA137ACAA5EC852 => 11111101 1 010000 10011011 1 10101100 10101010 0 1011110 1100100 0 01010010
* 0xFDA136ACAA5EC952 => 11111101 1 010000 10011011 0 10101100 10101010 0 1011110 1100100 1 01010010
*
* Key:
* K1K2
* 0 0 - key_unknown
* 0 1 - key_down
* 1 0 - key_up
* 1 1 - key_stop
*
*/
instance->btn = ((instance->data >> 54) & 0x02) | ((instance->data >> 40) & 0x1);
instance->serial = ((instance->data >> 33) & 0x3FFF00) | ((instance->data >> 32) & 0xFF);
instance->cnt = ((instance->data >> 49) & 0x3F);
}
SubGhzProtocolStatus
subghz_protocol_encoder_power_smart_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderPowerSmart* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_power_smart_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_power_smart_remote_controller(&instance->generic);
subghz_protocol_encoder_power_smart_get_upload(instance);
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_power_smart_stop(void* context) {
SubGhzProtocolEncoderPowerSmart* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_power_smart_yield(void* context) {
SubGhzProtocolEncoderPowerSmart* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_power_smart_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderPowerSmart* instance = malloc(sizeof(SubGhzProtocolDecoderPowerSmart));
instance->base.protocol = &subghz_protocol_power_smart;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_power_smart_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
free(instance);
}
void subghz_protocol_decoder_power_smart_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
}
bool subghz_protocol_power_smart_chek_valid(uint64_t packet) {
uint32_t data_1 = (uint32_t)((packet >> 40) & 0xFFFF);
uint32_t data_2 = (uint32_t)((~packet >> 8) & 0xFFFF);
uint8_t data_3 = (uint8_t)(packet >> 32) & 0xFF;
uint8_t data_4 = (uint8_t)(((~packet) & 0xFF) - 1);
return (data_1 == data_2) && (data_3 == data_4);
}
void subghz_protocol_decoder_power_smart_feed(
void* context,
bool level,
volatile uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
ManchesterEvent event = ManchesterEventReset;
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_short) <
subghz_protocol_power_smart_const.te_delta) {
event = ManchesterEventShortLow;
} else if(
DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_long) <
subghz_protocol_power_smart_const.te_delta * 2) {
event = ManchesterEventLongLow;
}
} else {
if(DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_short) <
subghz_protocol_power_smart_const.te_delta) {
event = ManchesterEventShortHigh;
} else if(
DURATION_DIFF(duration, subghz_protocol_power_smart_const.te_long) <
subghz_protocol_power_smart_const.te_delta * 2) {
event = ManchesterEventLongHigh;
}
}
if(event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(
instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
if(data_ok) {
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | !data;
}
if((instance->decoder.decode_data & POWER_SMART_PACKET_HEADER_MASK) ==
POWER_SMART_PACKET_HEADER) {
if(subghz_protocol_power_smart_chek_valid(instance->decoder.decode_data)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit =
subghz_protocol_power_smart_const.min_count_bit_for_found;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
}
} else {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
}
}
static const char* subghz_protocol_power_smart_get_name_button(uint8_t btn) {
btn &= 0x3;
const char* name_btn[0x4] = {"Unknown", "Down", "Up", "Stop"};
return name_btn[btn];
}
uint8_t subghz_protocol_decoder_power_smart_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_power_smart_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_power_smart_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_power_smart_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_power_smart_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderPowerSmart* instance = context;
subghz_protocol_power_smart_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%07lX \r\n"
"Btn:%s\r\n"
"Channel:" CHANNEL_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
instance->generic.serial,
subghz_protocol_power_smart_get_name_button(instance->generic.btn),
CNT_TO_CHANNEL(instance->generic.cnt));
}
| 14,400
|
C
|
.c
| 336
| 36.517857
| 115
| 0.679295
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,263
|
base.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/base.c
|
#include "base.h"
#include "registry.h"
void subghz_protocol_decoder_base_set_decoder_callback(
SubGhzProtocolDecoderBase* decoder_base,
SubGhzProtocolDecoderBaseRxCallback callback,
void* context) {
furi_check(decoder_base);
decoder_base->callback = callback;
decoder_base->context = context;
}
bool subghz_protocol_decoder_base_get_string(
SubGhzProtocolDecoderBase* decoder_base,
FuriString* output) {
furi_check(decoder_base);
furi_check(output);
bool status = false;
if(decoder_base->protocol && decoder_base->protocol->decoder &&
decoder_base->protocol->decoder->get_string) {
decoder_base->protocol->decoder->get_string(decoder_base, output);
status = true;
}
return status;
}
SubGhzProtocolStatus subghz_protocol_decoder_base_serialize(
SubGhzProtocolDecoderBase* decoder_base,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_check(decoder_base);
furi_check(flipper_format);
SubGhzProtocolStatus status = SubGhzProtocolStatusError;
if(decoder_base->protocol && decoder_base->protocol->decoder &&
decoder_base->protocol->decoder->serialize) {
status = decoder_base->protocol->decoder->serialize(decoder_base, flipper_format, preset);
}
return status;
}
SubGhzProtocolStatus subghz_protocol_decoder_base_deserialize(
SubGhzProtocolDecoderBase* decoder_base,
FlipperFormat* flipper_format) {
furi_check(decoder_base);
SubGhzProtocolStatus status = SubGhzProtocolStatusError;
if(decoder_base->protocol && decoder_base->protocol->decoder &&
decoder_base->protocol->decoder->deserialize) {
status = decoder_base->protocol->decoder->deserialize(decoder_base, flipper_format);
}
return status;
}
uint8_t subghz_protocol_decoder_base_get_hash_data(SubGhzProtocolDecoderBase* decoder_base) {
furi_check(decoder_base);
uint8_t hash = 0;
if(decoder_base->protocol && decoder_base->protocol->decoder &&
decoder_base->protocol->decoder->get_hash_data) {
hash = decoder_base->protocol->decoder->get_hash_data(decoder_base);
}
return hash;
}
| 2,177
|
C
|
.c
| 56
| 33.839286
| 98
| 0.730861
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,264
|
phoenix_v2.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/phoenix_v2.c
|
#include "phoenix_v2.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolPhoenixV2"
//transmission only static mode
static const SubGhzBlockConst subghz_protocol_phoenix_v2_const = {
.te_short = 427,
.te_long = 853,
.te_delta = 100,
.min_count_bit_for_found = 52,
};
struct SubGhzProtocolDecoderPhoenix_V2 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderPhoenix_V2 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
Phoenix_V2DecoderStepReset = 0,
Phoenix_V2DecoderStepFoundStartBit,
Phoenix_V2DecoderStepSaveDuration,
Phoenix_V2DecoderStepCheckDuration,
} Phoenix_V2DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_phoenix_v2_decoder = {
.alloc = subghz_protocol_decoder_phoenix_v2_alloc,
.free = subghz_protocol_decoder_phoenix_v2_free,
.feed = subghz_protocol_decoder_phoenix_v2_feed,
.reset = subghz_protocol_decoder_phoenix_v2_reset,
.get_hash_data = subghz_protocol_decoder_phoenix_v2_get_hash_data,
.serialize = subghz_protocol_decoder_phoenix_v2_serialize,
.deserialize = subghz_protocol_decoder_phoenix_v2_deserialize,
.get_string = subghz_protocol_decoder_phoenix_v2_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_phoenix_v2_encoder = {
.alloc = subghz_protocol_encoder_phoenix_v2_alloc,
.free = subghz_protocol_encoder_phoenix_v2_free,
.deserialize = subghz_protocol_encoder_phoenix_v2_deserialize,
.stop = subghz_protocol_encoder_phoenix_v2_stop,
.yield = subghz_protocol_encoder_phoenix_v2_yield,
};
const SubGhzProtocol subghz_protocol_phoenix_v2 = {
.name = SUBGHZ_PROTOCOL_PHOENIX_V2_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_phoenix_v2_decoder,
.encoder = &subghz_protocol_phoenix_v2_encoder,
};
void* subghz_protocol_encoder_phoenix_v2_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderPhoenix_V2* instance = malloc(sizeof(SubGhzProtocolEncoderPhoenix_V2));
instance->base.protocol = &subghz_protocol_phoenix_v2;
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_running = false;
return instance;
}
void subghz_protocol_encoder_phoenix_v2_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderPhoenix_V2* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderPhoenix_V2 instance
* @return true On success
*/
static bool
subghz_protocol_encoder_phoenix_v2_get_upload(SubGhzProtocolEncoderPhoenix_V2* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_phoenix_v2_const.te_short * 60);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_phoenix_v2_const.te_short * 6);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(!bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_phoenix_v2_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_phoenix_v2_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_phoenix_v2_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_phoenix_v2_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_phoenix_v2_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderPhoenix_V2* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_phoenix_v2_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_phoenix_v2_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_phoenix_v2_stop(void* context) {
SubGhzProtocolEncoderPhoenix_V2* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_phoenix_v2_yield(void* context) {
SubGhzProtocolEncoderPhoenix_V2* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_phoenix_v2_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderPhoenix_V2* instance = malloc(sizeof(SubGhzProtocolDecoderPhoenix_V2));
instance->base.protocol = &subghz_protocol_phoenix_v2;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_phoenix_v2_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
free(instance);
}
void subghz_protocol_decoder_phoenix_v2_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
}
void subghz_protocol_decoder_phoenix_v2_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
switch(instance->decoder.parser_step) {
case Phoenix_V2DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_short * 60) <
subghz_protocol_phoenix_v2_const.te_delta * 30)) {
//Found Preambula
instance->decoder.parser_step = Phoenix_V2DecoderStepFoundStartBit;
}
break;
case Phoenix_V2DecoderStepFoundStartBit:
if(level && (DURATION_DIFF(duration, (subghz_protocol_phoenix_v2_const.te_short * 6)) <
subghz_protocol_phoenix_v2_const.te_delta * 4)) {
//Found start bit
instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
}
break;
case Phoenix_V2DecoderStepSaveDuration:
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_phoenix_v2_const.te_short * 10 +
subghz_protocol_phoenix_v2_const.te_delta)) {
instance->decoder.parser_step = Phoenix_V2DecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit ==
subghz_protocol_phoenix_v2_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = Phoenix_V2DecoderStepCheckDuration;
}
}
break;
case Phoenix_V2DecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_phoenix_v2_const.te_short) <
subghz_protocol_phoenix_v2_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_long) <
subghz_protocol_phoenix_v2_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_phoenix_v2_const.te_long) <
subghz_protocol_phoenix_v2_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_phoenix_v2_const.te_short) <
subghz_protocol_phoenix_v2_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = Phoenix_V2DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
}
} else {
instance->decoder.parser_step = Phoenix_V2DecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_phoenix_v2_check_remote_controller(SubGhzBlockGeneric* instance) {
uint64_t data_rev =
subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit + 4);
instance->serial = data_rev & 0xFFFFFFFF;
instance->cnt = (data_rev >> 40) & 0xFFFF;
instance->btn = (data_rev >> 32) & 0xF;
}
uint8_t subghz_protocol_decoder_phoenix_v2_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_phoenix_v2_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_phoenix_v2_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_phoenix_v2_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_phoenix_v2_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderPhoenix_V2* instance = context;
subghz_protocol_phoenix_v2_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%02lX%08lX\r\n"
"Sn:0x%07lX \r\n"
"Btn:%X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32) & 0xFFFFFFFF,
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
instance->generic.serial,
instance->generic.btn);
}
| 12,726
|
C
|
.c
| 290
| 36.458621
| 98
| 0.681969
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,265
|
nice_flo.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/nice_flo.c
|
#include "nice_flo.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolNiceFlo"
static const SubGhzBlockConst subghz_protocol_nice_flo_const = {
.te_short = 700,
.te_long = 1400,
.te_delta = 200,
.min_count_bit_for_found = 12,
};
struct SubGhzProtocolDecoderNiceFlo {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderNiceFlo {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
NiceFloDecoderStepReset = 0,
NiceFloDecoderStepFoundStartBit,
NiceFloDecoderStepSaveDuration,
NiceFloDecoderStepCheckDuration,
} NiceFloDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_nice_flo_decoder = {
.alloc = subghz_protocol_decoder_nice_flo_alloc,
.free = subghz_protocol_decoder_nice_flo_free,
.feed = subghz_protocol_decoder_nice_flo_feed,
.reset = subghz_protocol_decoder_nice_flo_reset,
.get_hash_data = subghz_protocol_decoder_nice_flo_get_hash_data,
.serialize = subghz_protocol_decoder_nice_flo_serialize,
.deserialize = subghz_protocol_decoder_nice_flo_deserialize,
.get_string = subghz_protocol_decoder_nice_flo_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_nice_flo_encoder = {
.alloc = subghz_protocol_encoder_nice_flo_alloc,
.free = subghz_protocol_encoder_nice_flo_free,
.deserialize = subghz_protocol_encoder_nice_flo_deserialize,
.stop = subghz_protocol_encoder_nice_flo_stop,
.yield = subghz_protocol_encoder_nice_flo_yield,
};
const SubGhzProtocol subghz_protocol_nice_flo = {
.name = SUBGHZ_PROTOCOL_NICE_FLO_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_nice_flo_decoder,
.encoder = &subghz_protocol_nice_flo_encoder,
};
void* subghz_protocol_encoder_nice_flo_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderNiceFlo* instance = malloc(sizeof(SubGhzProtocolEncoderNiceFlo));
instance->base.protocol = &subghz_protocol_nice_flo;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52; //max 24bit*2 + 2 (start, stop)
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_nice_flo_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderNiceFlo* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderNiceFlo instance
* @return true On success
*/
static bool subghz_protocol_encoder_nice_flo_get_upload(SubGhzProtocolEncoderNiceFlo* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nice_flo_const.te_short * 36);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nice_flo_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nice_flo_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nice_flo_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nice_flo_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nice_flo_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_nice_flo_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderNiceFlo* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if((instance->generic.data_count_bit <
subghz_protocol_nice_flo_const.min_count_bit_for_found) ||
(instance->generic.data_count_bit >
2 * subghz_protocol_nice_flo_const.min_count_bit_for_found)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_nice_flo_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_nice_flo_stop(void* context) {
SubGhzProtocolEncoderNiceFlo* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_nice_flo_yield(void* context) {
SubGhzProtocolEncoderNiceFlo* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_nice_flo_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderNiceFlo* instance = malloc(sizeof(SubGhzProtocolDecoderNiceFlo));
instance->base.protocol = &subghz_protocol_nice_flo;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_nice_flo_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
free(instance);
}
void subghz_protocol_decoder_nice_flo_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
instance->decoder.parser_step = NiceFloDecoderStepReset;
}
void subghz_protocol_decoder_nice_flo_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
switch(instance->decoder.parser_step) {
case NiceFloDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_nice_flo_const.te_short * 36) <
subghz_protocol_nice_flo_const.te_delta * 36)) {
//Found header Nice Flo
instance->decoder.parser_step = NiceFloDecoderStepFoundStartBit;
}
break;
case NiceFloDecoderStepFoundStartBit:
if(!level) {
break;
} else if(
DURATION_DIFF(duration, subghz_protocol_nice_flo_const.te_short) <
subghz_protocol_nice_flo_const.te_delta) {
//Found start bit Nice Flo
instance->decoder.parser_step = NiceFloDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = NiceFloDecoderStepReset;
}
break;
case NiceFloDecoderStepSaveDuration:
if(!level) { //save interval
if(duration >= (subghz_protocol_nice_flo_const.te_short * 4)) {
instance->decoder.parser_step = NiceFloDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit >=
subghz_protocol_nice_flo_const.min_count_bit_for_found) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
instance->decoder.te_last = duration;
instance->decoder.parser_step = NiceFloDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = NiceFloDecoderStepReset;
}
break;
case NiceFloDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_nice_flo_const.te_short) <
subghz_protocol_nice_flo_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_nice_flo_const.te_long) <
subghz_protocol_nice_flo_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = NiceFloDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_nice_flo_const.te_long) <
subghz_protocol_nice_flo_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_nice_flo_const.te_short) <
subghz_protocol_nice_flo_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = NiceFloDecoderStepSaveDuration;
} else
instance->decoder.parser_step = NiceFloDecoderStepReset;
} else {
instance->decoder.parser_step = NiceFloDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_nice_flo_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_nice_flo_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_nice_flo_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if((instance->generic.data_count_bit <
subghz_protocol_nice_flo_const.min_count_bit_for_found) ||
(instance->generic.data_count_bit >
2 * subghz_protocol_nice_flo_const.min_count_bit_for_found)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
} while(false);
return ret;
}
void subghz_protocol_decoder_nice_flo_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderNiceFlo* instance = context;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%08lX\r\n"
"Yek:0x%08lX\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
code_found_lo,
code_found_reverse_lo);
}
| 12,837
|
C
|
.c
| 295
| 35.918644
| 99
| 0.678371
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,267
|
scher_khan.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/scher_khan.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_SCHER_KHAN_NAME "Scher-Khan"
typedef struct SubGhzProtocolDecoderScherKhan SubGhzProtocolDecoderScherKhan;
typedef struct SubGhzProtocolEncoderScherKhan SubGhzProtocolEncoderScherKhan;
extern const SubGhzProtocolDecoder subghz_protocol_scher_khan_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_scher_khan_encoder;
extern const SubGhzProtocol subghz_protocol_scher_khan;
/**
* Allocate SubGhzProtocolDecoderScherKhan.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderScherKhan* pointer to a SubGhzProtocolDecoderScherKhan instance
*/
void* subghz_protocol_decoder_scher_khan_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderScherKhan.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
*/
void subghz_protocol_decoder_scher_khan_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderScherKhan.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
*/
void subghz_protocol_decoder_scher_khan_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_scher_khan_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_scher_khan_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderScherKhan.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_scher_khan_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderScherKhan.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_scher_khan_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderScherKhan instance
* @param output Resulting text
*/
void subghz_protocol_decoder_scher_khan_get_string(void* context, FuriString* output);
| 2,719
|
C
|
.c
| 62
| 41.854839
| 97
| 0.822684
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,270
|
hollarm.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/hollarm.c
|
#include "hollarm.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
#define TAG "SubGhzProtocolHollarm"
static const SubGhzBlockConst subghz_protocol_hollarm_const = {
.te_short = 200,
.te_long = 1000,
.te_delta = 200,
.min_count_bit_for_found = 42,
};
struct SubGhzProtocolDecoderHollarm {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderHollarm {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
HollarmDecoderStepReset = 0,
HollarmDecoderStepSaveDuration,
HollarmDecoderStepCheckDuration,
} HollarmDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_hollarm_decoder = {
.alloc = subghz_protocol_decoder_hollarm_alloc,
.free = subghz_protocol_decoder_hollarm_free,
.feed = subghz_protocol_decoder_hollarm_feed,
.reset = subghz_protocol_decoder_hollarm_reset,
.get_hash_data = subghz_protocol_decoder_hollarm_get_hash_data,
.serialize = subghz_protocol_decoder_hollarm_serialize,
.deserialize = subghz_protocol_decoder_hollarm_deserialize,
.get_string = subghz_protocol_decoder_hollarm_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_hollarm_encoder = {
.alloc = subghz_protocol_encoder_hollarm_alloc,
.free = subghz_protocol_encoder_hollarm_free,
.deserialize = subghz_protocol_encoder_hollarm_deserialize,
.stop = subghz_protocol_encoder_hollarm_stop,
.yield = subghz_protocol_encoder_hollarm_yield,
};
const SubGhzProtocol subghz_protocol_hollarm = {
.name = SUBGHZ_PROTOCOL_HOLLARM_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_hollarm_decoder,
.encoder = &subghz_protocol_hollarm_encoder,
};
void* subghz_protocol_encoder_hollarm_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderHollarm* instance = malloc(sizeof(SubGhzProtocolEncoderHollarm));
instance->base.protocol = &subghz_protocol_hollarm;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_hollarm_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderHollarm* instance = context;
free(instance->encoder.upload);
free(instance);
}
// Get custom button code
static uint8_t subghz_protocol_hollarm_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x1:
btn = 0x2;
break;
case 0x2:
btn = 0x1;
break;
case 0x4:
btn = 0x1;
break;
case 0x8:
btn = 0x1;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x1:
btn = 0x4;
break;
case 0x2:
btn = 0x4;
break;
case 0x4:
btn = 0x2;
break;
case 0x8:
btn = 0x4;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0x1:
btn = 0x8;
break;
case 0x2:
btn = 0x8;
break;
case 0x4:
btn = 0x8;
break;
case 0x8:
btn = 0x2;
break;
default:
break;
}
}
return btn;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderHollarm instance
*/
static void subghz_protocol_encoder_hollarm_get_upload(SubGhzProtocolEncoderHollarm* instance) {
furi_assert(instance);
// Generate new key using custom or default button
instance->generic.btn = subghz_protocol_hollarm_get_btn_code();
uint64_t new_key = (instance->generic.data >> 12) << 12 | (instance->generic.btn << 8);
uint8_t crc = ((new_key >> 32) & 0xFF) + ((new_key >> 24) & 0xFF) + ((new_key >> 16) & 0xFF) +
((new_key >> 8) & 0xFF);
instance->generic.data = (new_key | crc);
size_t index = 0;
// Send key and GAP between parcels
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
// Read and prepare levels with 2 bit (was saved for better parsing) to the left offset to fit with the original remote transmission
if(bit_read((instance->generic.data << 2), i - 1)) {
// Send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hollarm_const.te_short);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_hollarm_const.te_short * 12);
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_hollarm_const.te_short * 8);
}
} else {
// Send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hollarm_const.te_short);
if(i == 1) {
//Send gap if bit was last
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_hollarm_const.te_short * 12);
} else {
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hollarm_const.te_long);
}
}
}
instance->encoder.size_upload = index;
return;
}
/**
* Analysis of received data and parsing serial number
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_hollarm_remote_controller(SubGhzBlockGeneric* instance) {
instance->btn = (instance->data >> 8) & 0xF;
instance->serial = (instance->data & 0xFFFFFFF0000) >> 16;
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(3);
// Hollarm Decoder
// 09.2024 - @xMasterX (MMX)
// Thanks @Skorpionm for support!
// F0B93422FF = FF 8bit Sum
// F0B93421FE = FE 8bit Sum
// F0B9342401 = 01 8bit Sum
// F0B9342805 = 05 8bit Sum
// Serial (moved 2bit to right) | Btn | 8b CRC (previous 4 bytes sum)
// 00001111000010111001001101000010 0010 11111111 btn = (0x2)
// 00001111000010111001001101000010 0001 11111110 btn = (0x1)
// 00001111000010111001001101000010 0100 00000001 btn = (0x4)
// 00001111000010111001001101000010 1000 00000101 btn = (0x8)
}
SubGhzProtocolStatus
subghz_protocol_encoder_hollarm_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderHollarm* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_hollarm_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_hollarm_remote_controller(&instance->generic);
subghz_protocol_encoder_hollarm_get_upload(instance);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_hollarm_stop(void* context) {
SubGhzProtocolEncoderHollarm* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_hollarm_yield(void* context) {
SubGhzProtocolEncoderHollarm* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_hollarm_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderHollarm* instance = malloc(sizeof(SubGhzProtocolDecoderHollarm));
instance->base.protocol = &subghz_protocol_hollarm;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_hollarm_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
free(instance);
}
void subghz_protocol_decoder_hollarm_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
instance->decoder.parser_step = HollarmDecoderStepReset;
}
void subghz_protocol_decoder_hollarm_feed(void* context, bool level, volatile uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
switch(instance->decoder.parser_step) {
case HollarmDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_hollarm_const.te_short * 12) <
subghz_protocol_hollarm_const.te_delta * 2)) {
//Found GAP between parcels
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = HollarmDecoderStepSaveDuration;
}
break;
case HollarmDecoderStepSaveDuration:
// Save HIGH level timing for next step
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = HollarmDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = HollarmDecoderStepReset;
}
break;
case HollarmDecoderStepCheckDuration:
if(!level) {
// Bit 0 is short 200us HIGH + long 1000us LOW timing
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hollarm_const.te_short) <
subghz_protocol_hollarm_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_hollarm_const.te_long) <
subghz_protocol_hollarm_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = HollarmDecoderStepSaveDuration;
// Bit 1 is short 200us HIGH + short x8 = 1600us LOW timing
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hollarm_const.te_short) <
subghz_protocol_hollarm_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_hollarm_const.te_short * 8) <
subghz_protocol_hollarm_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = HollarmDecoderStepSaveDuration;
} else if(
// End of the key
DURATION_DIFF(duration, subghz_protocol_hollarm_const.te_short * 12) <
subghz_protocol_hollarm_const.te_delta) {
// When next GAP is found add bit 0 and do check for read finish
// (we have 42 high level pulses, last or first one may be a stop/start bit but we will parse it as zero)
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
// If got 42 bits key reading is finished
if(instance->decoder.decode_count_bit ==
subghz_protocol_hollarm_const.min_count_bit_for_found) {
// Saving with 2bit to the right offset for proper parsing
instance->generic.data = (instance->decoder.decode_data >> 2);
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = HollarmDecoderStepReset;
} else {
instance->decoder.parser_step = HollarmDecoderStepReset;
}
} else {
instance->decoder.parser_step = HollarmDecoderStepReset;
}
break;
}
}
/**
* Get button name.
* @param btn Button number, 4 bit
*/
static const char* subghz_protocol_hollarm_get_button_name(uint8_t btn) {
const char* name_btn[16] = {
"Unknown",
"Disarm", // B (2)
"Arm", // A (1)
"0x3",
"Ringtone/Alarm", // C (3)
"0x5",
"0x6",
"0x7",
"Ring", // D (4)
"Settings mode",
"Exit settings",
"Vibro sens. setting",
"Not used\n(in settings)",
"Volume setting",
"0xE",
"0xF"};
return btn <= 0xf ? name_btn[btn] : name_btn[0];
}
uint8_t subghz_protocol_decoder_hollarm_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_hollarm_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_hollarm_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_hollarm_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_hollarm_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderHollarm* instance = context;
// Parse serial
subghz_protocol_hollarm_remote_controller(&instance->generic);
// Get CRC
uint8_t crc = ((instance->generic.data >> 32) & 0xFF) +
((instance->generic.data >> 24) & 0xFF) +
((instance->generic.data >> 16) & 0xFF) + ((instance->generic.data >> 8) & 0xFF);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key: 0x%02lX%08lX\r\n"
"Serial: 0x%06lX CRC: %02X\r\n"
"Btn: 0x%01X - %s\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->generic.serial,
crc,
instance->generic.btn,
subghz_protocol_hollarm_get_button_name(instance->generic.btn));
}
| 16,732
|
C
|
.c
| 407
| 32.997543
| 140
| 0.640595
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,271
|
linear.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/linear.c
|
#include "linear.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolLinear"
#define DIP_PATTERN "%c%c%c%c%c%c%c%c%c%c"
#define DATA_TO_DIP(dip) \
(dip & 0x0200 ? '1' : '0'), (dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), \
(dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), \
(dip & 0x0008 ? '1' : '0'), (dip & 0x0004 ? '1' : '0'), (dip & 0x0002 ? '1' : '0'), \
(dip & 0x0001 ? '1' : '0')
static const SubGhzBlockConst subghz_protocol_linear_const = {
.te_short = 500,
.te_long = 1500,
.te_delta = 150,
.min_count_bit_for_found = 10,
};
struct SubGhzProtocolDecoderLinear {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderLinear {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
LinearDecoderStepReset = 0,
LinearDecoderStepSaveDuration,
LinearDecoderStepCheckDuration,
} LinearDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_linear_decoder = {
.alloc = subghz_protocol_decoder_linear_alloc,
.free = subghz_protocol_decoder_linear_free,
.feed = subghz_protocol_decoder_linear_feed,
.reset = subghz_protocol_decoder_linear_reset,
.get_hash_data = subghz_protocol_decoder_linear_get_hash_data,
.serialize = subghz_protocol_decoder_linear_serialize,
.deserialize = subghz_protocol_decoder_linear_deserialize,
.get_string = subghz_protocol_decoder_linear_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_linear_encoder = {
.alloc = subghz_protocol_encoder_linear_alloc,
.free = subghz_protocol_encoder_linear_free,
.deserialize = subghz_protocol_encoder_linear_deserialize,
.stop = subghz_protocol_encoder_linear_stop,
.yield = subghz_protocol_encoder_linear_yield,
};
const SubGhzProtocol subghz_protocol_linear = {
.name = SUBGHZ_PROTOCOL_LINEAR_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_linear_decoder,
.encoder = &subghz_protocol_linear_encoder,
};
void* subghz_protocol_encoder_linear_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderLinear* instance = malloc(sizeof(SubGhzProtocolEncoderLinear));
instance->base.protocol = &subghz_protocol_linear;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 28; //max 10bit*2 + 2 (start, stop)
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_linear_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderLinear* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderLinear instance
* @return true On success
*/
static bool subghz_protocol_encoder_linear_get_upload(SubGhzProtocolEncoderLinear* 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, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_const.te_short * 3);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_linear_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_linear_const.te_short * 3);
}
}
//Send end bit
if(bit_read(instance->generic.data, 0)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_const.te_short * 3);
//Send PT_GUARD
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_linear_const.te_short * 42);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_const.te_short);
//Send PT_GUARD
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_linear_const.te_short * 44);
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_linear_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderLinear* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_linear_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_linear_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_linear_stop(void* context) {
SubGhzProtocolEncoderLinear* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_linear_yield(void* context) {
SubGhzProtocolEncoderLinear* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_linear_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderLinear* instance = malloc(sizeof(SubGhzProtocolDecoderLinear));
instance->base.protocol = &subghz_protocol_linear;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_linear_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
free(instance);
}
void subghz_protocol_decoder_linear_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
instance->decoder.parser_step = LinearDecoderStepReset;
}
void subghz_protocol_decoder_linear_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
switch(instance->decoder.parser_step) {
case LinearDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_linear_const.te_short * 42) <
subghz_protocol_linear_const.te_delta * 20)) {
//Found header Linear
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
}
break;
case LinearDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = LinearDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = LinearDecoderStepReset;
}
break;
case LinearDecoderStepCheckDuration:
if(!level) { //save interval
if(duration >= (subghz_protocol_linear_const.te_short * 5)) {
instance->decoder.parser_step = LinearDecoderStepReset;
//checking that the duration matches the guardtime
if(DURATION_DIFF(duration, subghz_protocol_linear_const.te_short * 42) >
subghz_protocol_linear_const.te_delta * 20) {
break;
}
if(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_linear_const.te_short) <
subghz_protocol_linear_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
} else if(
DURATION_DIFF(instance->decoder.te_last, subghz_protocol_linear_const.te_long) <
subghz_protocol_linear_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
}
if(instance->decoder.decode_count_bit ==
subghz_protocol_linear_const.min_count_bit_for_found) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_linear_const.te_short) <
subghz_protocol_linear_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_linear_const.te_long) <
subghz_protocol_linear_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_linear_const.te_long) <
subghz_protocol_linear_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_linear_const.te_short) <
subghz_protocol_linear_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = LinearDecoderStepReset;
}
} else {
instance->decoder.parser_step = LinearDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_linear_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_linear_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_linear_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_linear_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_linear_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderLinear* instance = context;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%08lX\r\n"
"Yek:0x%08lX\r\n"
"DIP:" DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
code_found_lo,
code_found_reverse_lo,
DATA_TO_DIP(code_found_lo));
}
| 13,139
|
C
|
.c
| 294
| 36.785714
| 100
| 0.661796
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,272
|
gate_tx.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/gate_tx.c
|
#include "gate_tx.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolGateTx"
static const SubGhzBlockConst subghz_protocol_gate_tx_const = {
.te_short = 350,
.te_long = 700,
.te_delta = 100,
.min_count_bit_for_found = 24,
};
struct SubGhzProtocolDecoderGateTx {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderGateTx {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
GateTXDecoderStepReset = 0,
GateTXDecoderStepFoundStartBit,
GateTXDecoderStepSaveDuration,
GateTXDecoderStepCheckDuration,
} GateTXDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_gate_tx_decoder = {
.alloc = subghz_protocol_decoder_gate_tx_alloc,
.free = subghz_protocol_decoder_gate_tx_free,
.feed = subghz_protocol_decoder_gate_tx_feed,
.reset = subghz_protocol_decoder_gate_tx_reset,
.get_hash_data = subghz_protocol_decoder_gate_tx_get_hash_data,
.serialize = subghz_protocol_decoder_gate_tx_serialize,
.deserialize = subghz_protocol_decoder_gate_tx_deserialize,
.get_string = subghz_protocol_decoder_gate_tx_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_gate_tx_encoder = {
.alloc = subghz_protocol_encoder_gate_tx_alloc,
.free = subghz_protocol_encoder_gate_tx_free,
.deserialize = subghz_protocol_encoder_gate_tx_deserialize,
.stop = subghz_protocol_encoder_gate_tx_stop,
.yield = subghz_protocol_encoder_gate_tx_yield,
};
const SubGhzProtocol subghz_protocol_gate_tx = {
.name = SUBGHZ_PROTOCOL_GATE_TX_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_gate_tx_decoder,
.encoder = &subghz_protocol_gate_tx_encoder,
};
void* subghz_protocol_encoder_gate_tx_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderGateTx* instance = malloc(sizeof(SubGhzProtocolEncoderGateTx));
instance->base.protocol = &subghz_protocol_gate_tx;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 52; //max 24bit*2 + 2 (start, stop)
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_gate_tx_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderGateTx* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderGateTx instance
* @return true On success
*/
static bool subghz_protocol_encoder_gate_tx_get_upload(SubGhzProtocolEncoderGateTx* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_gate_tx_const.te_short * 49);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_gate_tx_const.te_long);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_gate_tx_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_gate_tx_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_gate_tx_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_gate_tx_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_gate_tx_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderGateTx* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_gate_tx_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_gate_tx_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_gate_tx_stop(void* context) {
SubGhzProtocolEncoderGateTx* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_gate_tx_yield(void* context) {
SubGhzProtocolEncoderGateTx* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_gate_tx_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderGateTx* instance = malloc(sizeof(SubGhzProtocolDecoderGateTx));
instance->base.protocol = &subghz_protocol_gate_tx;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_gate_tx_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
free(instance);
}
void subghz_protocol_decoder_gate_tx_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
instance->decoder.parser_step = GateTXDecoderStepReset;
}
void subghz_protocol_decoder_gate_tx_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
switch(instance->decoder.parser_step) {
case GateTXDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_gate_tx_const.te_short * 47) <
subghz_protocol_gate_tx_const.te_delta * 47)) {
//Found Preambula
instance->decoder.parser_step = GateTXDecoderStepFoundStartBit;
}
break;
case GateTXDecoderStepFoundStartBit:
if(level && (DURATION_DIFF(duration, subghz_protocol_gate_tx_const.te_long) <
subghz_protocol_gate_tx_const.te_delta * 3)) {
//Found start bit
instance->decoder.parser_step = GateTXDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = GateTXDecoderStepReset;
}
break;
case GateTXDecoderStepSaveDuration:
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_gate_tx_const.te_short * 10 +
subghz_protocol_gate_tx_const.te_delta)) {
instance->decoder.parser_step = GateTXDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit ==
subghz_protocol_gate_tx_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = GateTXDecoderStepCheckDuration;
}
}
break;
case GateTXDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_gate_tx_const.te_short) <
subghz_protocol_gate_tx_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_gate_tx_const.te_long) <
subghz_protocol_gate_tx_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = GateTXDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_gate_tx_const.te_long) <
subghz_protocol_gate_tx_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_gate_tx_const.te_short) <
subghz_protocol_gate_tx_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = GateTXDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = GateTXDecoderStepReset;
}
} else {
instance->decoder.parser_step = GateTXDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_gate_tx_check_remote_controller(SubGhzBlockGeneric* instance) {
uint32_t code_found_reverse =
subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit);
instance->serial = (code_found_reverse & 0xFF) << 12 |
((code_found_reverse >> 8) & 0xFF) << 4 |
((code_found_reverse >> 20) & 0x0F);
instance->btn = ((code_found_reverse >> 16) & 0x0F);
}
uint8_t subghz_protocol_decoder_gate_tx_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_gate_tx_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_gate_tx_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_gate_tx_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_gate_tx_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderGateTx* instance = context;
subghz_protocol_gate_tx_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%06lX\r\n"
"Sn:%05lX Btn:%X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFF),
instance->generic.serial,
instance->generic.btn);
}
| 12,329
|
C
|
.c
| 283
| 36.106007
| 99
| 0.677051
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,274
|
megacode.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/megacode.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_MEGACODE_NAME "MegaCode"
typedef struct SubGhzProtocolDecoderMegaCode SubGhzProtocolDecoderMegaCode;
typedef struct SubGhzProtocolEncoderMegaCode SubGhzProtocolEncoderMegaCode;
extern const SubGhzProtocolDecoder subghz_protocol_megacode_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_megacode_encoder;
extern const SubGhzProtocol subghz_protocol_megacode;
/**
* Allocate SubGhzProtocolEncoderMegaCode.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderMegaCode* pointer to a SubGhzProtocolEncoderMegaCode instance
*/
void* subghz_protocol_encoder_megacode_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderMegaCode.
* @param context Pointer to a SubGhzProtocolEncoderMegaCode instance
*/
void subghz_protocol_encoder_megacode_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderMegaCode instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_megacode_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderMegaCode instance
*/
void subghz_protocol_encoder_megacode_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderMegaCode instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_megacode_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderMegaCode.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderMegaCode* pointer to a SubGhzProtocolDecoderMegaCode instance
*/
void* subghz_protocol_decoder_megacode_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderMegaCode.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
*/
void subghz_protocol_decoder_megacode_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderMegaCode.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
*/
void subghz_protocol_decoder_megacode_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_megacode_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_megacode_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderMegaCode.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_megacode_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderMegaCode.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_megacode_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderMegaCode instance
* @param output Resulting text
*/
void subghz_protocol_decoder_megacode_get_string(void* context, FuriString* output);
| 3,874
|
C
|
.c
| 92
| 40.119565
| 95
| 0.822045
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,275
|
somfy_keytis.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/somfy_keytis.c
|
#include "somfy_keytis.h"
#include <lib/toolbox/manchester_decoder.h>
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolSomfyKeytis"
static const SubGhzBlockConst subghz_protocol_somfy_keytis_const = {
.te_short = 640,
.te_long = 1280,
.te_delta = 250,
.min_count_bit_for_found = 80,
};
struct SubGhzProtocolDecoderSomfyKeytis {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint16_t header_count;
ManchesterState manchester_saved_state;
uint32_t press_duration_counter;
};
struct SubGhzProtocolEncoderSomfyKeytis {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
SomfyKeytisDecoderStepReset = 0,
SomfyKeytisDecoderStepCheckPreambula,
SomfyKeytisDecoderStepFoundPreambula,
SomfyKeytisDecoderStepStartDecode,
SomfyKeytisDecoderStepDecoderData,
} SomfyKeytisDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_somfy_keytis_decoder = {
.alloc = subghz_protocol_decoder_somfy_keytis_alloc,
.free = subghz_protocol_decoder_somfy_keytis_free,
.feed = subghz_protocol_decoder_somfy_keytis_feed,
.reset = subghz_protocol_decoder_somfy_keytis_reset,
.get_hash_data = subghz_protocol_decoder_somfy_keytis_get_hash_data,
.serialize = subghz_protocol_decoder_somfy_keytis_serialize,
.deserialize = subghz_protocol_decoder_somfy_keytis_deserialize,
.get_string = subghz_protocol_decoder_somfy_keytis_get_string,
};
const SubGhzProtocol subghz_protocol_somfy_keytis = {
.name = SUBGHZ_PROTOCOL_SOMFY_KEYTIS_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_somfy_keytis_decoder,
.encoder = &subghz_protocol_somfy_keytis_encoder,
};
const SubGhzProtocolEncoder subghz_protocol_somfy_keytis_encoder = {
.alloc = subghz_protocol_encoder_somfy_keytis_alloc,
.free = subghz_protocol_encoder_somfy_keytis_free,
.deserialize = subghz_protocol_encoder_somfy_keytis_deserialize,
.stop = subghz_protocol_encoder_somfy_keytis_stop,
.yield = subghz_protocol_encoder_somfy_keytis_yield,
};
void* subghz_protocol_encoder_somfy_keytis_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderSomfyKeytis* instance = malloc(sizeof(SubGhzProtocolEncoderSomfyKeytis));
instance->base.protocol = &subghz_protocol_somfy_keytis;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 512;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void* subghz_protocol_decoder_somfy_keytis_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderSomfyKeytis* instance = malloc(sizeof(SubGhzProtocolDecoderSomfyKeytis));
instance->base.protocol = &subghz_protocol_somfy_keytis;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_encoder_somfy_keytis_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderSomfyKeytis* instance = context;
free(instance->encoder.upload);
free(instance);
}
void subghz_protocol_decoder_somfy_keytis_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
free(instance);
}
void subghz_protocol_decoder_somfy_keytis_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
}
static bool
subghz_protocol_somfy_keytis_gen_data(SubGhzProtocolEncoderSomfyKeytis* instance, uint8_t btn) {
UNUSED(btn);
uint64_t data = instance->generic.data ^ (instance->generic.data >> 8);
instance->generic.btn = (data >> 48) & 0xF;
instance->generic.cnt = (data >> 24) & 0xFFFF;
instance->generic.serial = data & 0xFFFFFF;
if(instance->generic.cnt < 0xFFFF) {
if((instance->generic.cnt + furi_hal_subghz_get_rolling_counter_mult()) > 0xFFFF) {
instance->generic.cnt = 0;
} else {
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
}
} else if(instance->generic.cnt >= 0xFFFF) {
instance->generic.cnt = 0;
}
uint8_t frame[10];
frame[0] = (0xA << 4) | instance->generic.btn;
frame[1] = 0xF << 4;
frame[2] = instance->generic.cnt >> 8;
frame[3] = instance->generic.cnt;
frame[4] = instance->generic.serial >> 16;
frame[5] = instance->generic.serial >> 8;
frame[6] = instance->generic.serial;
frame[7] = 0xC4;
frame[8] = 0x00;
frame[9] = 0x19;
uint8_t checksum = 0;
for(uint8_t i = 0; i < 7; i++) {
checksum = checksum ^ frame[i] ^ (frame[i] >> 4);
}
checksum &= 0xF;
frame[1] |= checksum;
for(uint8_t i = 1; i < 7; i++) {
frame[i] ^= frame[i - 1];
}
data = 0;
for(uint8_t i = 0; i < 7; ++i) {
data <<= 8;
data |= frame[i];
}
instance->generic.data = data;
data = 0;
for(uint8_t i = 7; i < 10; ++i) {
data <<= 8;
data |= frame[i];
}
instance->generic.data_2 = data;
return true;
}
bool subghz_protocol_somfy_keytis_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint16_t cnt,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolEncoderSomfyKeytis* instance = context;
instance->generic.serial = serial;
instance->generic.cnt = cnt;
instance->generic.data_count_bit = 80;
bool res = subghz_protocol_somfy_keytis_gen_data(instance, btn);
if(res) {
return SubGhzProtocolStatusOk ==
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
return res;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderSomfyKeytis instance
* @return true On success
*/
static bool subghz_protocol_encoder_somfy_keytis_get_upload(
SubGhzProtocolEncoderSomfyKeytis* instance,
uint8_t btn) {
furi_assert(instance);
// Gen new key
if(!subghz_protocol_somfy_keytis_gen_data(instance, btn)) {
return false;
}
size_t index = 0;
//Send header
//Wake up
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)9415); // 1
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)89565); // 0
//Hardware sync
for(uint8_t i = 0; i < 12; ++i) {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 4); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 4); // 0
}
//Software sync
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)4550); // 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
//Send key data MSB manchester
for(uint8_t i = instance->generic.data_count_bit - 24; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration *= 2; // 00
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
}
} else {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_HIGH) {
instance->encoder.upload[index - 1].duration *= 2; // 11
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
} else {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
}
}
}
for(uint8_t i = 24; i > 0; i--) {
if(bit_read(instance->generic.data_2, i - 1)) {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration *= 2; // 00
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
}
} else {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_HIGH) {
instance->encoder.upload[index - 1].duration *= 2; // 11
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
} else {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
}
}
}
//Inter-frame silence
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration +=
(uint32_t)subghz_protocol_somfy_keytis_const.te_short * 3;
} else {
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 3);
}
for(uint8_t i = 0; i < 2; ++i) {
//Hardware sync
for(uint8_t i = 0; i < 6; ++i) {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 4); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 4); // 0
}
//Software sync
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)4550); // 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
//Send key data MSB manchester
for(uint8_t i = instance->generic.data_count_bit - 24; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration *= 2; // 00
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
}
} else {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_HIGH) {
instance->encoder.upload[index - 1].duration *= 2; // 11
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
} else {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
}
}
}
for(uint8_t i = 24; i > 0; i--) {
if(bit_read(instance->generic.data_2, i - 1)) {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration *= 2; // 00
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
}
} else {
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_HIGH) {
instance->encoder.upload[index - 1].duration *= 2; // 11
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
} else {
instance->encoder.upload[index++] = level_duration_make(
true, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 1
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short); // 0
}
}
}
//Inter-frame silence
if(instance->encoder.upload[index - 1].level == LEVEL_DURATION_LEVEL_LOW) {
instance->encoder.upload[index - 1].duration +=
(uint32_t)subghz_protocol_somfy_keytis_const.te_short * 3;
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 3);
}
}
//Inter-frame silence
instance->encoder.upload[index - 1].duration +=
(uint32_t)30415 - (uint32_t)subghz_protocol_somfy_keytis_const.te_short * 3;
size_t size_upload = index;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_somfy_keytis_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderSomfyKeytis* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
if(SubGhzProtocolStatusOk !=
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);
subghz_protocol_encoder_somfy_keytis_get_upload(instance, instance->generic.btn);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
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_running = true;
res = SubGhzProtocolStatusOk;
} while(false);
return res;
}
void subghz_protocol_encoder_somfy_keytis_stop(void* context) {
SubGhzProtocolEncoderSomfyKeytis* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_somfy_keytis_yield(void* context) {
SubGhzProtocolEncoderSomfyKeytis* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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;
}
/**
* Сhecksum calculation.
* @param data Вata for checksum calculation
* @return CRC
*/
static uint8_t subghz_protocol_somfy_keytis_crc(uint64_t data) {
uint8_t crc = 0;
data &= 0xFFF0FFFFFFFFFF;
for(uint8_t i = 0; i < 56; i += 8) {
crc = crc ^ data >> i ^ (data >> (i + 4));
}
return crc & 0xf;
}
void subghz_protocol_decoder_somfy_keytis_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
ManchesterEvent event = ManchesterEventReset;
switch(instance->decoder.parser_step) {
case SomfyKeytisDecoderStepReset:
if((level) && DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 4) <
subghz_protocol_somfy_keytis_const.te_delta * 4) {
instance->decoder.parser_step = SomfyKeytisDecoderStepFoundPreambula;
instance->header_count++;
}
break;
case SomfyKeytisDecoderStepFoundPreambula:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 4) <
subghz_protocol_somfy_keytis_const.te_delta * 4)) {
instance->decoder.parser_step = SomfyKeytisDecoderStepCheckPreambula;
} else {
instance->header_count = 0;
instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
}
break;
case SomfyKeytisDecoderStepCheckPreambula:
if(level) {
if(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 4) <
subghz_protocol_somfy_keytis_const.te_delta * 4) {
instance->decoder.parser_step = SomfyKeytisDecoderStepFoundPreambula;
instance->header_count++;
} else if(
(instance->header_count > 1) &&
(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short * 7) <
subghz_protocol_somfy_keytis_const.te_delta * 4)) {
instance->decoder.parser_step = SomfyKeytisDecoderStepDecoderData;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->press_duration_counter = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventLongHigh,
&instance->manchester_saved_state,
NULL);
}
}
break;
case SomfyKeytisDecoderStepDecoderData:
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short) <
subghz_protocol_somfy_keytis_const.te_delta) {
event = ManchesterEventShortLow;
} else if(
DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_long) <
subghz_protocol_somfy_keytis_const.te_delta) {
event = ManchesterEventLongLow;
} else if(
duration >= (subghz_protocol_somfy_keytis_const.te_long +
subghz_protocol_somfy_keytis_const.te_delta)) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_somfy_keytis_const.min_count_bit_for_found) {
//check crc
uint64_t data_tmp = instance->generic.data ^ (instance->generic.data >> 8);
if(((data_tmp >> 40) & 0xF) == subghz_protocol_somfy_keytis_crc(data_tmp)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventLongHigh,
&instance->manchester_saved_state,
NULL);
instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
} else {
instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
}
} else {
if(DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_short) <
subghz_protocol_somfy_keytis_const.te_delta) {
event = ManchesterEventShortHigh;
} else if(
DURATION_DIFF(duration, subghz_protocol_somfy_keytis_const.te_long) <
subghz_protocol_somfy_keytis_const.te_delta) {
event = ManchesterEventLongHigh;
} else {
instance->decoder.parser_step = SomfyKeytisDecoderStepReset;
}
}
if(event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(
instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
if(data_ok) {
if(instance->decoder.decode_count_bit < 56) {
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
} else {
instance->press_duration_counter = (instance->press_duration_counter << 1) |
data;
}
instance->decoder.decode_count_bit++;
}
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_somfy_keytis_check_remote_controller(SubGhzBlockGeneric* instance) {
//https://pushstack.wordpress.com/somfy-rts-protocol/
/*
* 604 us
* /
* | 2416us | 2416us | 2416us | 2416us | 4550 us | |
*
* +--------+ +--------+ +---...---+
* + +--------+ +--------+ +--+XXXX...XXX+
*
* | hw. sync. | soft. | |
* | | sync. | data |
*
*
* encrypt | decrypt
*
* package 80 bit pdc key btn crc cnt serial
*
* 0xA453537C4B9855 C40019 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 C80026 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 CC0033 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 D00049 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 D4005C => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 D80063 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 DC0076 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 E00086 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 E40093 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 E800AC => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 EC00B9 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 F000C3 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 F400D6 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 F800E9 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 FC00FC => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 FC0102 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 FC0113 => 0xA 4 F 7 002F 37D3CD
* 0xA453537C4B9855 FC0120 => 0xA 4 F 7 002F 37D3CD
* ..........
* 0xA453537C4B9855 FC048F => 0xA 4 F 7 002F 37D3CD
*
* Pdc: "Press Duration Counter" the total delay of the button is sent 72 parcels,
* pdc cnt4b cnt8b pdc_crc
* C40019 => 11 0001 00 0000 00000001 1001
* C80026 => 11 0010 00 0000 00000010 0110
* CC0033 => 11 0011 00 0000 00000011 0011
* D00049 => 11 0100 00 0000 00000100 1001
* D4005C => 11 0101 00 0000 00000101 1100
* D80063 => 11 0110 00 0000 00000110 0011
* DC0076 => 11 0111 00 0000 00000111 0110
* E00086 => 11 1000 00 0000 00001000 0110
* E40093 => 11 1001 00 0000 00001001 0011
* E800AC => 11 1010 00 0000 00001010 1100
* EC00B9 => 11 1011 00 0000 00001011 1001
* F000C3 => 11 1100 00 0000 00001100 0011
* F400D6 => 11 1101 00 0000 00001101 0110
* F800E9 => 11 1110 00 0000 00001110 1001
* FC00FC => 11 1111 00 0000 00001111 1100
* FC0102 => 11 1111 00 0000 00010000 0010
* FC0113 => 11 1111 00 0000 00010001 0011
* FC0120 => 11 1111 00 0000 00010010 0000
*
* Cnt4b: 4-bit counter changes from 1 to 15 then always equals 15
* Cnt8b: 8-bit counter changes from 1 to 72 (0x48)
* Ppdc_crc:
* uint8_t crc=0;
* for(i=4; i<24; i+=4){
* crc ^=(pdc>>i);
* }
* return crc;
* example: crc = 1^0^0^4^C = 9
* 11, 00, 0000: const
*
* Key: “Encryption Key”, Most significant 4-bit are always 0xA, Least Significant bits is
* a linear counter. In the Smoove Origin this counter is increased together with the
* rolling code. But leaving this on a constant value also works. Gerardwr notes that
* for some other types of remotes the MSB is not constant.
* Btn: 4-bit Control codes, this indicates the button that is pressed
* CRC: 4-bit Checksum.
* Ctn: 16-bit rolling code (big-endian) increased with every button press.
* Serial: 24-bit identifier of sending device (little-endian)
*
*
* Decrypt
*
* uint8_t frame[7];
* for (i=1; i < 7; i++) {
* frame[i] = frame[i] ^ frame[i-1];
* }
* or
* uint64 Decrypt = frame ^ (frame>>8);
*
* CRC
*
* uint8_t frame[7];
* for (i=0; i < 7; i++) {
* crc = crc ^ frame[i] ^ (frame[i] >> 4);
* }
* crc = crc & 0xf;
*
*/
uint64_t data = instance->data ^ (instance->data >> 8);
instance->btn = (data >> 48) & 0xF;
instance->cnt = (data >> 24) & 0xFFFF;
instance->serial = data & 0xFFFFFF;
}
/**
* Get button name.
* @param btn Button number, 4 bit
*/
static const char* subghz_protocol_somfy_keytis_get_name_button(uint8_t btn) {
const char* name_btn[0x10] = {
"Unknown",
"0x01",
"0x02",
"Prog",
"Key_1",
"0x05",
"0x06",
"0x07",
"0x08",
"0x09",
"0x0A",
"0x0B",
"0x0C",
"0x0D",
"0x0E",
"0x0F"};
return btn <= 0xf ? name_btn[btn] : name_btn[0];
}
uint8_t subghz_protocol_decoder_somfy_keytis_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_somfy_keytis_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
SubGhzProtocolStatus ret =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_write_uint32(
flipper_format, "Duration_Counter", &instance->press_duration_counter, 1)) {
FURI_LOG_E(TAG, "Unable to add Duration_Counter");
ret = SubGhzProtocolStatusErrorParserOthers;
}
return ret;
}
SubGhzProtocolStatus
subghz_protocol_decoder_somfy_keytis_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_somfy_keytis_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
if(!flipper_format_read_uint32(
flipper_format,
"Duration_Counter",
(uint32_t*)&instance->press_duration_counter,
1)) {
FURI_LOG_E(TAG, "Missing Duration_Counter");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
} while(false);
return ret;
}
void subghz_protocol_decoder_somfy_keytis_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderSomfyKeytis* instance = context;
subghz_protocol_somfy_keytis_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %db\r\n"
"%lX%08lX%06lX\r\n"
"Sn:0x%06lX \r\n"
"Cnt:0x%04lX\r\n"
"Btn:%s\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->press_duration_counter,
instance->generic.serial,
instance->generic.cnt,
subghz_protocol_somfy_keytis_get_name_button(instance->generic.btn));
}
| 31,964
|
C
|
.c
| 721
| 35.542302
| 100
| 0.604577
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,276
|
dooya.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/dooya.c
|
#include "dooya.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolDooya"
#define DOYA_SINGLE_CHANNEL 0xFF
static const SubGhzBlockConst subghz_protocol_dooya_const = {
.te_short = 366,
.te_long = 733,
.te_delta = 120,
.min_count_bit_for_found = 40,
};
struct SubGhzProtocolDecoderDooya {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderDooya {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
DooyaDecoderStepReset = 0,
DooyaDecoderStepFoundStartBit,
DooyaDecoderStepSaveDuration,
DooyaDecoderStepCheckDuration,
} DooyaDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_dooya_decoder = {
.alloc = subghz_protocol_decoder_dooya_alloc,
.free = subghz_protocol_decoder_dooya_free,
.feed = subghz_protocol_decoder_dooya_feed,
.reset = subghz_protocol_decoder_dooya_reset,
.get_hash_data = subghz_protocol_decoder_dooya_get_hash_data,
.serialize = subghz_protocol_decoder_dooya_serialize,
.deserialize = subghz_protocol_decoder_dooya_deserialize,
.get_string = subghz_protocol_decoder_dooya_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_dooya_encoder = {
.alloc = subghz_protocol_encoder_dooya_alloc,
.free = subghz_protocol_encoder_dooya_free,
.deserialize = subghz_protocol_encoder_dooya_deserialize,
.stop = subghz_protocol_encoder_dooya_stop,
.yield = subghz_protocol_encoder_dooya_yield,
};
const SubGhzProtocol subghz_protocol_dooya = {
.name = SUBGHZ_PROTOCOL_DOOYA_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_dooya_decoder,
.encoder = &subghz_protocol_dooya_encoder,
};
void* subghz_protocol_encoder_dooya_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderDooya* instance = malloc(sizeof(SubGhzProtocolEncoderDooya));
instance->base.protocol = &subghz_protocol_dooya;
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_running = false;
return instance;
}
void subghz_protocol_encoder_dooya_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderDooya* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderDooya instance
* @return true On success
*/
static bool subghz_protocol_encoder_dooya_get_upload(SubGhzProtocolEncoderDooya* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
if(bit_read(instance->generic.data, 0)) {
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_dooya_const.te_long * 12 +
subghz_protocol_dooya_const.te_long);
} else {
instance->encoder.upload[index++] = level_duration_make(
false,
(uint32_t)subghz_protocol_dooya_const.te_long * 12 +
subghz_protocol_dooya_const.te_short);
}
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_dooya_const.te_short * 13);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_dooya_const.te_long * 2);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_dooya_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_dooya_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_dooya_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_dooya_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_dooya_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderDooya* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_dooya_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_dooya_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_dooya_stop(void* context) {
SubGhzProtocolEncoderDooya* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_dooya_yield(void* context) {
SubGhzProtocolEncoderDooya* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_dooya_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderDooya* instance = malloc(sizeof(SubGhzProtocolDecoderDooya));
instance->base.protocol = &subghz_protocol_dooya;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_dooya_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
free(instance);
}
void subghz_protocol_decoder_dooya_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
instance->decoder.parser_step = DooyaDecoderStepReset;
}
void subghz_protocol_decoder_dooya_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
switch(instance->decoder.parser_step) {
case DooyaDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_dooya_const.te_long * 12) <
subghz_protocol_dooya_const.te_delta * 20)) {
instance->decoder.parser_step = DooyaDecoderStepFoundStartBit;
}
break;
case DooyaDecoderStepFoundStartBit:
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_dooya_const.te_long * 2) <
subghz_protocol_dooya_const.te_delta * 3) {
instance->decoder.parser_step = DooyaDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = DooyaDecoderStepReset;
}
} else if(
DURATION_DIFF(duration, subghz_protocol_dooya_const.te_short * 13) <
subghz_protocol_dooya_const.te_delta * 5) {
break;
} else {
instance->decoder.parser_step = DooyaDecoderStepReset;
}
break;
case DooyaDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = DooyaDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = DooyaDecoderStepReset;
}
break;
case DooyaDecoderStepCheckDuration:
if(!level) {
if(duration >= (subghz_protocol_dooya_const.te_long * 4)) {
//add last bit
if(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_short) <
subghz_protocol_dooya_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
} else if(
DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_long) <
subghz_protocol_dooya_const.te_delta * 2) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else {
instance->decoder.parser_step = DooyaDecoderStepReset;
break;
}
instance->decoder.parser_step = DooyaDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit ==
subghz_protocol_dooya_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_short) <
subghz_protocol_dooya_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_dooya_const.te_long) <
subghz_protocol_dooya_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = DooyaDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_dooya_const.te_long) <
subghz_protocol_dooya_const.te_delta * 2) &&
(DURATION_DIFF(duration, subghz_protocol_dooya_const.te_short) <
subghz_protocol_dooya_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = DooyaDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = DooyaDecoderStepReset;
}
} else {
instance->decoder.parser_step = DooyaDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_dooya_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* serial s/m ch key
* long press down X * E1DC030533, 40b 111000011101110000000011 0000 0101 0011 0011
*
* short press down 3 * E1DC030533, 40b 111000011101110000000011 0000 0101 0011 0011
* 3 * E1DC03053C, 40b 111000011101110000000011 0000 0101 0011 1100
*
* press stop X * E1DC030555, 40b 111000011101110000000011 0000 0101 0101 0101
*
* long press up X * E1DC030511, 40b 111000011101110000000011 0000 0101 0001 0001
*
* short press up 3 * E1DC030511, 40b 111000011101110000000011 0000 0101 0001 0001
* 3 * E1DC03051E, 40b 111000011101110000000011 0000 0101 0001 1110
*
* serial: 3 byte serial number
* s/m: single (b0000) / multi (b0001) channel console
* ch: channel if single (always b0101) or multi
* key: 0b00010001 - long press up
* 0b00011110 - short press up
* 0b00110011 - long press down
* 0b00111100 - short press down
* 0b01010101 - press stop
* 0b01111001 - press up + down
* 0b10000000 - press up + stop
* 0b10000001 - press down + stop
* 0b11001100 - press P2
*
*/
instance->serial = (instance->data >> 16);
if((instance->data >> 12) & 0x0F) {
instance->cnt = (instance->data >> 8) & 0x0F;
} else {
instance->cnt = DOYA_SINGLE_CHANNEL;
}
instance->btn = instance->data & 0xFF;
}
uint8_t subghz_protocol_decoder_dooya_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_dooya_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_dooya_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_dooya_const.min_count_bit_for_found);
}
/**
* Get button name.
* @param btn Button number, 8 bit
*/
static const char* subghz_protocol_dooya_get_name_button(uint8_t btn) {
const char* btn_name;
switch(btn) {
case 0b00010001:
btn_name = "Up_Long";
break;
case 0b00011110:
btn_name = "Up_Short";
break;
case 0b00110011:
btn_name = "Down_Long";
break;
case 0b00111100:
btn_name = "Down_Short";
break;
case 0b01010101:
btn_name = "Stop";
break;
case 0b01111001:
btn_name = "Up+Down";
break;
case 0b10000000:
btn_name = "Up+Stop";
break;
case 0b10000001:
btn_name = "Down+Stop";
break;
case 0b11001100:
btn_name = "P2";
break;
default:
btn_name = "Unknown";
break;
}
return btn_name;
}
void subghz_protocol_decoder_dooya_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderDooya* instance = context;
subghz_protocol_dooya_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%010llX\r\n"
"Sn:0x%08lX\r\n"
"Btn:%s\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
instance->generic.data,
instance->generic.serial,
subghz_protocol_dooya_get_name_button(instance->generic.btn));
if(instance->generic.cnt == DOYA_SINGLE_CHANNEL) {
furi_string_cat_printf(output, "Ch:Single\r\n");
} else {
furi_string_cat_printf(output, "Ch:%lu\r\n", instance->generic.cnt);
}
}
| 15,660
|
C
|
.c
| 387
| 33.069767
| 99
| 0.658215
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,277
|
secplus_v2.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/secplus_v2.c
|
#include "secplus_v2.h"
#include <lib/toolbox/manchester_decoder.h>
#include <lib/toolbox/manchester_encoder.h>
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include "../blocks/custom_btn_i.h"
/*
* Help
* https://github.com/argilo/secplus
* https://github.com/merbanan/rtl_433/blob/master/src/devices/secplus_v2.c
*/
#define TAG "SubGhzProtocoSecPlusV2"
#define SECPLUS_V2_HEADER 0x3C0000000000
#define SECPLUS_V2_HEADER_MASK 0xFFFF3C0000000000
#define SECPLUS_V2_PACKET_1 0x000000000000
#define SECPLUS_V2_PACKET_2 0x010000000000
#define SECPLUS_V2_PACKET_MASK 0x30000000000
static const SubGhzBlockConst subghz_protocol_secplus_v2_const = {
.te_short = 250,
.te_long = 500,
.te_delta = 110,
.min_count_bit_for_found = 62,
};
struct SubGhzProtocolDecoderSecPlus_v2 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
ManchesterState manchester_saved_state;
uint64_t secplus_packet_1;
};
struct SubGhzProtocolEncoderSecPlus_v2 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
uint64_t secplus_packet_1;
};
typedef enum {
SecPlus_v2DecoderStepReset = 0,
SecPlus_v2DecoderStepDecoderData,
} SecPlus_v2DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_secplus_v2_decoder = {
.alloc = subghz_protocol_decoder_secplus_v2_alloc,
.free = subghz_protocol_decoder_secplus_v2_free,
.feed = subghz_protocol_decoder_secplus_v2_feed,
.reset = subghz_protocol_decoder_secplus_v2_reset,
.get_hash_data = subghz_protocol_decoder_secplus_v2_get_hash_data,
.serialize = subghz_protocol_decoder_secplus_v2_serialize,
.deserialize = subghz_protocol_decoder_secplus_v2_deserialize,
.get_string = subghz_protocol_decoder_secplus_v2_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_secplus_v2_encoder = {
.alloc = subghz_protocol_encoder_secplus_v2_alloc,
.free = subghz_protocol_encoder_secplus_v2_free,
.deserialize = subghz_protocol_encoder_secplus_v2_deserialize,
.stop = subghz_protocol_encoder_secplus_v2_stop,
.yield = subghz_protocol_encoder_secplus_v2_yield,
};
const SubGhzProtocol subghz_protocol_secplus_v2 = {
.name = SUBGHZ_PROTOCOL_SECPLUS_V2_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_secplus_v2_decoder,
.encoder = &subghz_protocol_secplus_v2_encoder,
};
void* subghz_protocol_encoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolEncoderSecPlus_v2));
instance->base.protocol = &subghz_protocol_secplus_v2;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_secplus_v2_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderSecPlus_v2* instance = context;
free(instance->encoder.upload);
free(instance);
}
static bool subghz_protocol_secplus_v2_mix_invet(uint8_t invert, uint16_t p[]) {
// selectively invert buffers
switch(invert) {
case 0x00: // 0b0000 (True, True, False),
p[0] = ~p[0] & 0x03FF;
p[1] = ~p[1] & 0x03FF;
break;
case 0x01: // 0b0001 (False, True, False),
p[1] = ~p[1] & 0x03FF;
break;
case 0x02: // 0b0010 (False, False, True),
p[2] = ~p[2] & 0x03FF;
break;
case 0x04: // 0b0100 (True, True, True),
p[0] = ~p[0] & 0x03FF;
p[1] = ~p[1] & 0x03FF;
p[2] = ~p[2] & 0x03FF;
break;
case 0x05: // 0b0101 (True, False, True),
case 0x0a: // 0b1010 (True, False, True),
p[0] = ~p[0] & 0x03FF;
p[2] = ~p[2] & 0x03FF;
break;
case 0x06: // 0b0110 (False, True, True),
p[1] = ~p[1] & 0x03FF;
p[2] = ~p[2] & 0x03FF;
break;
case 0x08: // 0b1000 (True, False, False),
p[0] = ~p[0] & 0x03FF;
break;
case 0x09: // 0b1001 (False, False, False),
break;
default:
FURI_LOG_E(TAG, "Invert FAIL");
return false;
}
return true;
}
static bool subghz_protocol_secplus_v2_mix_order_decode(uint8_t order, uint16_t p[]) {
uint16_t a = p[0], b = p[1], c = p[2];
// selectively reorder buffers
switch(order) {
case 0x06: // 0b0110 2, 1, 0],
case 0x09: // 0b1001 2, 1, 0],
p[2] = a;
// p[1]: no change
p[0] = c;
break;
case 0x08: // 0b1000 1, 2, 0],
case 0x04: // 0b0100 1, 2, 0],
p[1] = a;
p[2] = b;
p[0] = c;
break;
case 0x01: // 0b0001 2, 0, 1],
p[2] = a;
p[0] = b;
p[1] = c;
break;
case 0x00: // 0b0000 0, 2, 1],
// p[0]: no change
p[2] = b;
p[1] = c;
break;
case 0x05: // 0b0101 1, 0, 2],
p[1] = a;
p[0] = b;
// p[2]: no change
break;
case 0x02: // 0b0010 0, 1, 2],
case 0x0A: // 0b1010 0, 1, 2],
// no reordering
break;
default:
FURI_LOG_E(TAG, "Order FAIL");
return false;
}
return true;
}
static bool subghz_protocol_secplus_v2_mix_order_encode(uint8_t order, uint16_t p[]) {
uint16_t a, b, c;
// selectively reorder buffers
switch(order) {
case 0x06: // 0b0110 2, 1, 0],
case 0x09: // 0b1001 2, 1, 0],
a = p[2];
b = p[1];
c = p[0];
break;
case 0x08: // 0b1000 1, 2, 0],
case 0x04: // 0b0100 1, 2, 0],
a = p[1];
b = p[2];
c = p[0];
break;
case 0x01: // 0b0001 2, 0, 1],
a = p[2];
b = p[0];
c = p[1];
break;
case 0x00: // 0b0000 0, 2, 1],
a = p[0];
b = p[2];
c = p[1];
break;
case 0x05: // 0b0101 1, 0, 2],
a = p[1];
b = p[0];
c = p[2];
break;
case 0x02: // 0b0010 0, 1, 2],
case 0x0A: // 0b1010 0, 1, 2],
a = p[0];
b = p[1];
c = p[2];
break;
default:
FURI_LOG_E(TAG, "Order FAIL");
return false;
}
p[0] = a;
p[1] = b;
p[2] = c;
return true;
}
/**
* Security+ 2.0 half-message decoding
* @param data data
* @param roll_array[] return roll_array part
* @param fixed[] return fixed part
* @return true On success
*/
static bool
subghz_protocol_secplus_v2_decode_half(uint64_t data, uint8_t roll_array[], uint32_t* fixed) {
uint8_t order = (data >> 34) & 0x0f;
uint8_t invert = (data >> 30) & 0x0f;
uint16_t p[3] = {0};
for(int i = 29; i >= 0; i -= 3) {
p[0] = p[0] << 1 | bit_read(data, i);
p[1] = p[1] << 1 | bit_read(data, i - 1);
p[2] = p[2] << 1 | bit_read(data, i - 2);
}
if(!subghz_protocol_secplus_v2_mix_invet(invert, p)) return false;
if(!subghz_protocol_secplus_v2_mix_order_decode(order, p)) return false;
data = order << 4 | invert;
int k = 0;
for(int i = 6; i >= 0; i -= 2) {
roll_array[k] = (data >> i) & 0x03;
if(roll_array[k++] == 3) {
FURI_LOG_E(TAG, "Roll_Array FAIL");
return false;
}
}
for(int i = 8; i >= 0; i -= 2) {
roll_array[k] = (p[2] >> i) & 0x03;
if(roll_array[k++] == 3) {
FURI_LOG_E(TAG, "Roll_Array FAIL");
return false;
}
}
fixed[0] = p[0] << 10 | p[1];
return true;
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
* @param packet_1 first part of the message
*/
static void
subghz_protocol_secplus_v2_remote_controller(SubGhzBlockGeneric* instance, uint64_t packet_1) {
uint32_t fixed_1[1];
uint8_t roll_1[9] = {0};
uint32_t fixed_2[1];
uint8_t roll_2[9] = {0};
uint8_t rolling_digits[18] = {0};
if(subghz_protocol_secplus_v2_decode_half(packet_1, roll_1, fixed_1) &&
subghz_protocol_secplus_v2_decode_half(instance->data, roll_2, fixed_2)) {
rolling_digits[0] = roll_2[8];
rolling_digits[1] = roll_1[8];
rolling_digits[2] = roll_2[4];
rolling_digits[3] = roll_2[5];
rolling_digits[4] = roll_2[6];
rolling_digits[5] = roll_2[7];
rolling_digits[6] = roll_1[4];
rolling_digits[7] = roll_1[5];
rolling_digits[8] = roll_1[6];
rolling_digits[9] = roll_1[7];
rolling_digits[10] = roll_2[0];
rolling_digits[11] = roll_2[1];
rolling_digits[12] = roll_2[2];
rolling_digits[13] = roll_2[3];
rolling_digits[14] = roll_1[0];
rolling_digits[15] = roll_1[1];
rolling_digits[16] = roll_1[2];
rolling_digits[17] = roll_1[3];
uint32_t rolling = 0;
for(int i = 0; i < 18; i++) {
rolling = (rolling * 3) + rolling_digits[i];
}
// Max value = 2^28 (268435456)
if(rolling >= 0x10000000) {
FURI_LOG_E(TAG, "Rolling FAIL");
instance->cnt = 0;
instance->btn = 0;
instance->serial = 0;
} else {
instance->cnt = subghz_protocol_blocks_reverse_key(rolling, 28);
instance->btn = fixed_1[0] >> 12;
instance->serial = fixed_1[0] << 20 | fixed_2[0];
}
} else {
instance->cnt = 0;
instance->btn = 0;
instance->serial = 0;
}
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->btn);
}
subghz_custom_btn_set_max(4);
}
/**
* Security+ 2.0 half-message encoding
* @param roll_array[] roll_array part
* @param fixed[] fixed part
* @return return data
*/
static uint64_t subghz_protocol_secplus_v2_encode_half(uint8_t roll_array[], uint32_t fixed) {
uint64_t data = 0;
uint16_t p[3] = {(fixed >> 10) & 0x3FF, fixed & 0x3FF, 0};
uint8_t order = roll_array[0] << 2 | roll_array[1];
uint8_t invert = roll_array[2] << 2 | roll_array[3];
p[2] = (uint16_t)roll_array[4] << 8 | roll_array[5] << 6 | roll_array[6] << 4 |
roll_array[7] << 2 | roll_array[8];
if(!subghz_protocol_secplus_v2_mix_order_encode(order, p)) return 0;
if(!subghz_protocol_secplus_v2_mix_invet(invert, p)) return 0;
for(int i = 0; i < 10; i++) {
data <<= 3;
data |= bit_read(p[0], 9 - i) << 2 | bit_read(p[1], 9 - i) << 1 | bit_read(p[2], 9 - i);
}
data |= ((uint64_t)order) << 34 | ((uint64_t)invert) << 30;
return data;
}
/**
* Defines the button value for the current btn_id
* Basic set | 0x68 | 0x80 | 0x81 | 0xE2 | 0x78
* @return Button code
*/
static uint8_t subghz_protocol_secplus_v2_get_btn_code(void);
/**
* Security+ 2.0 message encoding
* @param instance SubGhzProtocolEncoderSecPlus_v2*
*/
static void subghz_protocol_secplus_v2_encode(SubGhzProtocolEncoderSecPlus_v2* instance) {
// Save original button for later use
if(subghz_custom_btn_get_original() == 0) {
subghz_custom_btn_set_original(instance->generic.btn);
}
instance->generic.btn = subghz_protocol_secplus_v2_get_btn_code();
uint32_t fixed_1[1] = {instance->generic.btn << 12 | instance->generic.serial >> 20};
uint32_t fixed_2[1] = {instance->generic.serial & 0xFFFFF};
uint8_t rolling_digits[18] = {0};
uint8_t roll_1[9] = {0};
uint8_t roll_2[9] = {0};
instance->generic.cnt += furi_hal_subghz_get_rolling_counter_mult();
//ToDo it is not known what value the counter starts
if(instance->generic.cnt > 0xFFFFFFF) instance->generic.cnt = 0xE500000;
uint32_t rolling = subghz_protocol_blocks_reverse_key(instance->generic.cnt, 28);
for(int8_t i = 17; i > -1; i--) {
rolling_digits[i] = rolling % 3;
rolling /= 3;
}
roll_2[8] = rolling_digits[0];
roll_1[8] = rolling_digits[1];
roll_2[4] = rolling_digits[2];
roll_2[5] = rolling_digits[3];
roll_2[6] = rolling_digits[4];
roll_2[7] = rolling_digits[5];
roll_1[4] = rolling_digits[6];
roll_1[5] = rolling_digits[7];
roll_1[6] = rolling_digits[8];
roll_1[7] = rolling_digits[9];
roll_2[0] = rolling_digits[10];
roll_2[1] = rolling_digits[11];
roll_2[2] = rolling_digits[12];
roll_2[3] = rolling_digits[13];
roll_1[0] = rolling_digits[14];
roll_1[1] = rolling_digits[15];
roll_1[2] = rolling_digits[16];
roll_1[3] = rolling_digits[17];
instance->secplus_packet_1 = SECPLUS_V2_HEADER | SECPLUS_V2_PACKET_1 |
subghz_protocol_secplus_v2_encode_half(roll_1, fixed_1[0]);
instance->generic.data = SECPLUS_V2_HEADER | SECPLUS_V2_PACKET_2 |
subghz_protocol_secplus_v2_encode_half(roll_2, fixed_2[0]);
}
static LevelDuration
subghz_protocol_encoder_secplus_v2_add_duration_to_upload(ManchesterEncoderResult result) {
LevelDuration data = {.duration = 0, .level = 0};
switch(result) {
case ManchesterEncoderResultShortLow:
data.duration = subghz_protocol_secplus_v2_const.te_short;
data.level = false;
break;
case ManchesterEncoderResultLongLow:
data.duration = subghz_protocol_secplus_v2_const.te_long;
data.level = false;
break;
case ManchesterEncoderResultLongHigh:
data.duration = subghz_protocol_secplus_v2_const.te_long;
data.level = true;
break;
case ManchesterEncoderResultShortHigh:
data.duration = subghz_protocol_secplus_v2_const.te_short;
data.level = true;
break;
default:
furi_crash("SubGhz: ManchesterEncoderResult is incorrect.");
break;
}
return level_duration_make(data.level, data.duration);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderSecPlus_v2 instance
*/
static void
subghz_protocol_encoder_secplus_v2_get_upload(SubGhzProtocolEncoderSecPlus_v2* instance) {
furi_assert(instance);
size_t index = 0;
ManchesterEncoderState enc_state;
manchester_encoder_reset(&enc_state);
ManchesterEncoderResult result;
//Send data packet 1
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(!manchester_encoder_advance(
&enc_state, bit_read(instance->secplus_packet_1, i - 1), &result)) {
instance->encoder.upload[index++] =
subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
manchester_encoder_advance(
&enc_state, bit_read(instance->secplus_packet_1, i - 1), &result);
}
instance->encoder.upload[index++] =
subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
}
instance->encoder.upload[index] = subghz_protocol_encoder_secplus_v2_add_duration_to_upload(
manchester_encoder_finish(&enc_state));
if(level_duration_get_level(instance->encoder.upload[index])) {
index++;
}
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_secplus_v2_const.te_long * 136);
//Send data packet 2
manchester_encoder_reset(&enc_state);
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(!manchester_encoder_advance(
&enc_state, bit_read(instance->generic.data, i - 1), &result)) {
instance->encoder.upload[index++] =
subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
manchester_encoder_advance(
&enc_state, bit_read(instance->generic.data, i - 1), &result);
}
instance->encoder.upload[index++] =
subghz_protocol_encoder_secplus_v2_add_duration_to_upload(result);
}
instance->encoder.upload[index] = subghz_protocol_encoder_secplus_v2_add_duration_to_upload(
manchester_encoder_finish(&enc_state));
if(level_duration_get_level(instance->encoder.upload[index])) {
index++;
}
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_secplus_v2_const.te_long * 136);
instance->encoder.size_upload = index;
}
SubGhzProtocolStatus
subghz_protocol_encoder_secplus_v2_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderSecPlus_v2* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_secplus_v2_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
uint8_t key_data[sizeof(uint64_t)] = {0};
if(!flipper_format_read_hex(
flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Secplus_packet_1");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
}
subghz_protocol_secplus_v2_remote_controller(
&instance->generic, instance->secplus_packet_1);
subghz_protocol_secplus_v2_encode(instance);
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
subghz_protocol_encoder_secplus_v2_get_upload(instance);
//update data
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");
ret = SubGhzProtocolStatusErrorParserKey;
break;
}
for(size_t i = 0; i < sizeof(uint64_t); i++) {
key_data[sizeof(uint64_t) - i - 1] = (instance->secplus_packet_1 >> (i * 8)) & 0xFF;
}
if(!flipper_format_update_hex(
flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_secplus_v2_stop(void* context) {
SubGhzProtocolEncoderSecPlus_v2* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_secplus_v2_yield(void* context) {
SubGhzProtocolEncoderSecPlus_v2* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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;
}
bool subghz_protocol_secplus_v2_create_data(
void* context,
FlipperFormat* flipper_format,
uint32_t serial,
uint8_t btn,
uint32_t cnt,
SubGhzRadioPreset* preset) {
furi_check(context);
SubGhzProtocolEncoderSecPlus_v2* instance = context;
instance->generic.serial = serial;
instance->generic.cnt = cnt;
instance->generic.btn = btn;
instance->generic.data_count_bit =
(uint8_t)subghz_protocol_secplus_v2_const.min_count_bit_for_found;
subghz_protocol_secplus_v2_encode(instance);
SubGhzProtocolStatus res =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
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->secplus_packet_1 >> (i * 8)) & 0xFF;
}
if((res == SubGhzProtocolStatusOk) &&
!flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
res = SubGhzProtocolStatusErrorParserOthers;
}
return res == SubGhzProtocolStatusOk;
}
void* subghz_protocol_decoder_secplus_v2_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderSecPlus_v2* instance = malloc(sizeof(SubGhzProtocolDecoderSecPlus_v2));
instance->base.protocol = &subghz_protocol_secplus_v2;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_secplus_v2_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v2* instance = context;
free(instance);
}
void subghz_protocol_decoder_secplus_v2_reset(void* context) {
furi_assert(context);
// SubGhzProtocolDecoderSecPlus_v2* instance = context;
// does not reset the decoder because you need to get 2 parts of the package
}
static bool subghz_protocol_secplus_v2_check_packet(SubGhzProtocolDecoderSecPlus_v2* instance) {
if((instance->decoder.decode_data & SECPLUS_V2_HEADER_MASK) == SECPLUS_V2_HEADER) {
if((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_1) {
instance->secplus_packet_1 = instance->decoder.decode_data;
} else if(
((instance->decoder.decode_data & SECPLUS_V2_PACKET_MASK) == SECPLUS_V2_PACKET_2) &&
(instance->secplus_packet_1)) {
return true;
}
}
return false;
}
void subghz_protocol_decoder_secplus_v2_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v2* instance = context;
ManchesterEvent event = ManchesterEventReset;
switch(instance->decoder.parser_step) {
case SecPlus_v2DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long * 130) <
subghz_protocol_secplus_v2_const.te_delta * 100)) {
//Found header Security+ 2.0
instance->decoder.parser_step = SecPlus_v2DecoderStepDecoderData;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->secplus_packet_1 = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventLongHigh,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventShortLow,
&instance->manchester_saved_state,
NULL);
}
break;
case SecPlus_v2DecoderStepDecoderData:
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
subghz_protocol_secplus_v2_const.te_delta) {
event = ManchesterEventShortLow;
} else if(
DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
subghz_protocol_secplus_v2_const.te_delta) {
event = ManchesterEventLongLow;
} else if(
duration >= (subghz_protocol_secplus_v2_const.te_long * 2UL +
subghz_protocol_secplus_v2_const.te_delta)) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_secplus_v2_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(subghz_protocol_secplus_v2_check_packet(instance)) {
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
}
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
manchester_advance(
instance->manchester_saved_state,
ManchesterEventReset,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventLongHigh,
&instance->manchester_saved_state,
NULL);
manchester_advance(
instance->manchester_saved_state,
ManchesterEventShortLow,
&instance->manchester_saved_state,
NULL);
} else {
instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
}
} else {
if(DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_short) <
subghz_protocol_secplus_v2_const.te_delta) {
event = ManchesterEventShortHigh;
} else if(
DURATION_DIFF(duration, subghz_protocol_secplus_v2_const.te_long) <
subghz_protocol_secplus_v2_const.te_delta) {
event = ManchesterEventLongHigh;
} else {
instance->decoder.parser_step = SecPlus_v2DecoderStepReset;
}
}
if(event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(
instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
if(data_ok) {
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
instance->decoder.decode_count_bit++;
}
}
break;
}
}
uint8_t subghz_protocol_decoder_secplus_v2_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v2* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_secplus_v2_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v2* instance = context;
SubGhzProtocolStatus ret =
subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
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->secplus_packet_1 >> (i * 8)) & 0xFF;
}
if((ret == SubGhzProtocolStatusOk) &&
!flipper_format_write_hex(flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Secplus_packet_1");
ret = SubGhzProtocolStatusErrorParserOthers;
}
return ret;
}
SubGhzProtocolStatus
subghz_protocol_decoder_secplus_v2_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v2* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_secplus_v2_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
uint8_t key_data[sizeof(uint64_t)] = {0};
if(!flipper_format_read_hex(
flipper_format, "Secplus_packet_1", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Missing Secplus_packet_1");
ret = SubGhzProtocolStatusErrorParserOthers;
break;
}
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
instance->secplus_packet_1 = instance->secplus_packet_1 << 8 | key_data[i];
}
} while(false);
return ret;
}
static uint8_t subghz_protocol_secplus_v2_get_btn_code(void) {
uint8_t custom_btn_id = subghz_custom_btn_get();
uint8_t original_btn_code = subghz_custom_btn_get_original();
uint8_t btn = original_btn_code;
// Set custom button
if((custom_btn_id == SUBGHZ_CUSTOM_BTN_OK) && (original_btn_code != 0)) {
// Restore original button code
btn = original_btn_code;
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_UP) {
switch(original_btn_code) {
case 0x68:
btn = 0x80;
break;
case 0x80:
btn = 0x68;
break;
case 0x81:
btn = 0x80;
break;
case 0xE2:
btn = 0x80;
break;
case 0x78:
btn = 0x80;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_DOWN) {
switch(original_btn_code) {
case 0x68:
btn = 0x81;
break;
case 0x80:
btn = 0x81;
break;
case 0x81:
btn = 0x68;
break;
case 0xE2:
btn = 0x81;
break;
case 0x78:
btn = 0x81;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_LEFT) {
switch(original_btn_code) {
case 0x68:
btn = 0xE2;
break;
case 0x80:
btn = 0xE2;
break;
case 0x81:
btn = 0xE2;
break;
case 0xE2:
btn = 0x68;
break;
case 0x78:
btn = 0xE2;
break;
default:
break;
}
} else if(custom_btn_id == SUBGHZ_CUSTOM_BTN_RIGHT) {
switch(original_btn_code) {
case 0x68:
btn = 0x78;
break;
case 0x80:
btn = 0x78;
break;
case 0x81:
btn = 0x78;
break;
case 0xE2:
btn = 0x78;
break;
case 0x78:
btn = 0x68;
break;
default:
break;
}
}
return btn;
}
void subghz_protocol_decoder_secplus_v2_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderSecPlus_v2* instance = context;
subghz_protocol_secplus_v2_remote_controller(&instance->generic, instance->secplus_packet_1);
furi_string_cat_printf(
output,
"%s %db\r\n"
"Pk1:0x%lX%08lX\r\n"
"Pk2:0x%lX%08lX\r\n"
"Sn:0x%08lX Btn:0x%01X\r\n"
"Cnt:0x%03lX\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->secplus_packet_1 >> 32),
(uint32_t)instance->secplus_packet_1,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->generic.serial,
instance->generic.btn,
instance->generic.cnt);
}
| 32,074
|
C
|
.c
| 848
| 29.78066
| 99
| 0.604589
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,278
|
linear_delta3.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/linear_delta3.c
|
#include "linear_delta3.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolLinearDelta3"
#define DIP_PATTERN "%c%c%c%c%c%c%c%c"
#define DATA_TO_DIP(dip) \
(dip & 0x0080 ? '0' : '1'), (dip & 0x0040 ? '0' : '1'), (dip & 0x0020 ? '0' : '1'), \
(dip & 0x0010 ? '0' : '1'), (dip & 0x0008 ? '0' : '1'), (dip & 0x0004 ? '0' : '1'), \
(dip & 0x0002 ? '0' : '1'), (dip & 0x0001 ? '0' : '1')
static const SubGhzBlockConst subghz_protocol_linear_delta3_const = {
.te_short = 500,
.te_long = 2000,
.te_delta = 150,
.min_count_bit_for_found = 8,
};
struct SubGhzProtocolDecoderLinearDelta3 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint32_t last_data;
};
struct SubGhzProtocolEncoderLinearDelta3 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
LinearDecoderStepReset = 0,
LinearDecoderStepSaveDuration,
LinearDecoderStepCheckDuration,
} LinearDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_linear_delta3_decoder = {
.alloc = subghz_protocol_decoder_linear_delta3_alloc,
.free = subghz_protocol_decoder_linear_delta3_free,
.feed = subghz_protocol_decoder_linear_delta3_feed,
.reset = subghz_protocol_decoder_linear_delta3_reset,
.get_hash_data = subghz_protocol_decoder_linear_delta3_get_hash_data,
.serialize = subghz_protocol_decoder_linear_delta3_serialize,
.deserialize = subghz_protocol_decoder_linear_delta3_deserialize,
.get_string = subghz_protocol_decoder_linear_delta3_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_linear_delta3_encoder = {
.alloc = subghz_protocol_encoder_linear_delta3_alloc,
.free = subghz_protocol_encoder_linear_delta3_free,
.deserialize = subghz_protocol_encoder_linear_delta3_deserialize,
.stop = subghz_protocol_encoder_linear_delta3_stop,
.yield = subghz_protocol_encoder_linear_delta3_yield,
};
const SubGhzProtocol subghz_protocol_linear_delta3 = {
.name = SUBGHZ_PROTOCOL_LINEAR_DELTA3_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_linear_delta3_decoder,
.encoder = &subghz_protocol_linear_delta3_encoder,
};
void* subghz_protocol_encoder_linear_delta3_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderLinearDelta3* instance =
malloc(sizeof(SubGhzProtocolEncoderLinearDelta3));
instance->base.protocol = &subghz_protocol_linear_delta3;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 16;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_linear_delta3_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderLinearDelta3* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderLinearDelta3 instance
* @return true On success
*/
static bool
subghz_protocol_encoder_linear_delta3_get_upload(SubGhzProtocolEncoderLinearDelta3* 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, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_delta3_const.te_short);
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_linear_delta3_const.te_short * 7);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_delta3_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_linear_delta3_const.te_long);
}
}
//Send end bit
if(bit_read(instance->generic.data, 0)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_delta3_const.te_short);
//Send PT_GUARD
instance->encoder.upload[index] = level_duration_make(
false, (uint32_t)subghz_protocol_linear_delta3_const.te_short * 73);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_linear_delta3_const.te_long);
//Send PT_GUARD
instance->encoder.upload[index] = level_duration_make(
false, (uint32_t)subghz_protocol_linear_delta3_const.te_short * 70);
}
return true;
}
SubGhzProtocolStatus subghz_protocol_encoder_linear_delta3_deserialize(
void* context,
FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderLinearDelta3* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_linear_delta3_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_linear_delta3_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_linear_delta3_stop(void* context) {
SubGhzProtocolEncoderLinearDelta3* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_linear_delta3_yield(void* context) {
SubGhzProtocolEncoderLinearDelta3* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_linear_delta3_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderLinearDelta3* instance =
malloc(sizeof(SubGhzProtocolDecoderLinearDelta3));
instance->base.protocol = &subghz_protocol_linear_delta3;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_linear_delta3_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
free(instance);
}
void subghz_protocol_decoder_linear_delta3_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
instance->decoder.parser_step = LinearDecoderStepReset;
instance->last_data = 0;
}
void subghz_protocol_decoder_linear_delta3_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
switch(instance->decoder.parser_step) {
case LinearDecoderStepReset:
if((!level) &&
(DURATION_DIFF(duration, subghz_protocol_linear_delta3_const.te_short * 70) <
subghz_protocol_linear_delta3_const.te_delta * 24)) {
//Found header Linear
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
}
break;
case LinearDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = LinearDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = LinearDecoderStepReset;
}
break;
case LinearDecoderStepCheckDuration:
if(!level) {
if(duration >= (subghz_protocol_linear_delta3_const.te_short * 10)) {
instance->decoder.parser_step = LinearDecoderStepReset;
if(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_linear_delta3_const.te_short) <
subghz_protocol_linear_delta3_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else if(
DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_linear_delta3_const.te_long) <
subghz_protocol_linear_delta3_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
}
if(instance->decoder.decode_count_bit ==
subghz_protocol_linear_delta3_const.min_count_bit_for_found) {
if((instance->last_data == instance->decoder.decode_data) &&
instance->last_data) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
instance->last_data = instance->decoder.decode_data;
}
break;
}
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_linear_delta3_const.te_short) <
subghz_protocol_linear_delta3_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_linear_delta3_const.te_short * 7) <
subghz_protocol_linear_delta3_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_linear_delta3_const.te_long) <
subghz_protocol_linear_delta3_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_linear_delta3_const.te_long) <
subghz_protocol_linear_delta3_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = LinearDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = LinearDecoderStepReset;
}
} else {
instance->decoder.parser_step = LinearDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_linear_delta3_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8));
}
SubGhzProtocolStatus subghz_protocol_decoder_linear_delta3_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus subghz_protocol_decoder_linear_delta3_deserialize(
void* context,
FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_linear_delta3_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_linear_delta3_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderLinearDelta3* instance = context;
uint32_t data = instance->generic.data & 0xFF;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX\r\n"
"DIP:" DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
data,
DATA_TO_DIP(data));
}
| 13,514
|
C
|
.c
| 302
| 36.569536
| 99
| 0.66373
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,279
|
magellan.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/magellan.c
|
#include "magellan.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolMagellan"
static const SubGhzBlockConst subghz_protocol_magellan_const = {
.te_short = 200,
.te_long = 400,
.te_delta = 100,
.min_count_bit_for_found = 32,
};
struct SubGhzProtocolDecoderMagellan {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint16_t header_count;
};
struct SubGhzProtocolEncoderMagellan {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
MagellanDecoderStepReset = 0,
MagellanDecoderStepCheckPreambula,
MagellanDecoderStepFoundPreambula,
MagellanDecoderStepSaveDuration,
MagellanDecoderStepCheckDuration,
} MagellanDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_magellan_decoder = {
.alloc = subghz_protocol_decoder_magellan_alloc,
.free = subghz_protocol_decoder_magellan_free,
.feed = subghz_protocol_decoder_magellan_feed,
.reset = subghz_protocol_decoder_magellan_reset,
.get_hash_data = subghz_protocol_decoder_magellan_get_hash_data,
.serialize = subghz_protocol_decoder_magellan_serialize,
.deserialize = subghz_protocol_decoder_magellan_deserialize,
.get_string = subghz_protocol_decoder_magellan_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_magellan_encoder = {
.alloc = subghz_protocol_encoder_magellan_alloc,
.free = subghz_protocol_encoder_magellan_free,
.deserialize = subghz_protocol_encoder_magellan_deserialize,
.stop = subghz_protocol_encoder_magellan_stop,
.yield = subghz_protocol_encoder_magellan_yield,
};
const SubGhzProtocol subghz_protocol_magellan = {
.name = SUBGHZ_PROTOCOL_MAGELLAN_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send |
SubGhzProtocolFlag_Magellan,
.decoder = &subghz_protocol_magellan_decoder,
.encoder = &subghz_protocol_magellan_encoder,
};
void* subghz_protocol_encoder_magellan_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderMagellan* instance = malloc(sizeof(SubGhzProtocolEncoderMagellan));
instance->base.protocol = &subghz_protocol_magellan;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_magellan_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderMagellan* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderMagellan instance
* @return true On success
*/
static bool subghz_protocol_encoder_magellan_get_upload(SubGhzProtocolEncoderMagellan* instance) {
furi_assert(instance);
size_t index = 0;
//Send header
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short * 4);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_short);
for(uint8_t i = 0; i < 12; i++) {
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_short);
}
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_long * 3);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_short);
}
}
//Send stop bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_magellan_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_magellan_const.te_long * 100);
instance->encoder.size_upload = index;
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_magellan_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderMagellan* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_magellan_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_magellan_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_magellan_stop(void* context) {
SubGhzProtocolEncoderMagellan* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_magellan_yield(void* context) {
SubGhzProtocolEncoderMagellan* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_magellan_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderMagellan* instance = malloc(sizeof(SubGhzProtocolDecoderMagellan));
instance->base.protocol = &subghz_protocol_magellan;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_magellan_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
free(instance);
}
void subghz_protocol_decoder_magellan_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
instance->decoder.parser_step = MagellanDecoderStepReset;
}
uint8_t subghz_protocol_magellan_crc8(uint8_t* data, size_t len) {
uint8_t crc = 0x00;
size_t i, j;
for(i = 0; i < len; i++) {
crc ^= data[i];
for(j = 0; j < 8; j++) {
if((crc & 0x80) != 0)
crc = (uint8_t)((crc << 1) ^ 0x31);
else
crc <<= 1;
}
}
return crc;
}
static bool subghz_protocol_magellan_check_crc(SubGhzProtocolDecoderMagellan* instance) {
uint8_t data[3] = {
instance->decoder.decode_data >> 24,
instance->decoder.decode_data >> 16,
instance->decoder.decode_data >> 8};
return (instance->decoder.decode_data & 0xFF) ==
subghz_protocol_magellan_crc8(data, sizeof(data));
}
void subghz_protocol_decoder_magellan_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
switch(instance->decoder.parser_step) {
case MagellanDecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
subghz_protocol_magellan_const.te_delta)) {
instance->decoder.parser_step = MagellanDecoderStepCheckPreambula;
instance->decoder.te_last = duration;
instance->header_count = 0;
}
break;
case MagellanDecoderStepCheckPreambula:
if(level) {
instance->decoder.te_last = duration;
} else {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
subghz_protocol_magellan_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
subghz_protocol_magellan_const.te_delta)) {
// Found header
instance->header_count++;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
subghz_protocol_magellan_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
subghz_protocol_magellan_const.te_delta * 2) &&
(instance->header_count > 10)) {
instance->decoder.parser_step = MagellanDecoderStepFoundPreambula;
} else {
instance->decoder.parser_step = MagellanDecoderStepReset;
}
}
break;
case MagellanDecoderStepFoundPreambula:
if(level) {
instance->decoder.te_last = duration;
} else {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_magellan_const.te_short * 6) <
subghz_protocol_magellan_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
subghz_protocol_magellan_const.te_delta * 2)) {
instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = MagellanDecoderStepReset;
}
}
break;
case MagellanDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = MagellanDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = MagellanDecoderStepReset;
}
break;
case MagellanDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_short) <
subghz_protocol_magellan_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_magellan_const.te_long) <
subghz_protocol_magellan_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_magellan_const.te_long) <
subghz_protocol_magellan_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_magellan_const.te_short) <
subghz_protocol_magellan_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = MagellanDecoderStepSaveDuration;
} else if(duration >= (subghz_protocol_magellan_const.te_long * 3)) {
//Found stop bit
if((instance->decoder.decode_count_bit ==
subghz_protocol_magellan_const.min_count_bit_for_found) &&
subghz_protocol_magellan_check_crc(instance)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = MagellanDecoderStepReset;
} else {
instance->decoder.parser_step = MagellanDecoderStepReset;
}
} else {
instance->decoder.parser_step = MagellanDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_magellan_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* package 32b data 24b CRC8
* 0x037AE4828 => 001101111010111001001000 00101000
*
* 0x037AE48 (flipped in reverse bit sequence) => 0x1275EC
*
* 0x1275EC => 0x12-event codes, 0x75EC-serial (dec 117236)
*
* Event codes consist of two parts:
* - The upper nibble (bits 7-4) represents the event type:
* - 0x00: Nothing
* - 0x01: Door
* - 0x02: Motion
* - 0x03: Smoke Alarm
* - 0x04: REM1
* - 0x05: REM1 with subtype Off1
* - 0x06: REM2
* - 0x07: REM2 with subtype Off1
* - Others: Unknown
* - The lower nibble (bits 3-0) represents the event subtype, which varies based on the model type:
* - If the model type is greater than 0x03 (e.g., REM1 or REM2):
* - 0x00: Arm1
* - 0x01: Btn1
* - 0x02: Btn2
* - 0x03: Btn3
* - 0x08: Reset
* - 0x09: LowBatt
* - 0x0A: BattOk
* - 0x0B: Learn
* - Others: Unknown
* - Otherwise:
* - 0x00: Sealed
* - 0x01: Alarm
* - 0x02: Tamper
* - 0x03: Alarm + Tamper
* - 0x08: Reset
* - 0x09: LowBatt
* - 0x0A: BattOk
* - 0x0B: Learn
* - Others: Unknown
*
*/
uint64_t data_rev = subghz_protocol_blocks_reverse_key(instance->data >> 8, 24);
instance->serial = data_rev & 0xFFFF;
instance->btn = (data_rev >> 16) & 0xFF;
}
static void subghz_protocol_magellan_get_event_serialize(uint8_t event, FuriString* output) {
const char* event_type;
const char* event_subtype;
switch((event >> 4) & 0x0F) {
case 0x00:
event_type = "Nothing";
break;
case 0x01:
event_type = "Door";
break;
case 0x02:
event_type = "Motion";
break;
case 0x03:
event_type = "Smoke Alarm";
break;
case 0x04:
event_type = "REM1";
break;
case 0x05:
event_type = "REM1";
event_subtype = "Off1";
furi_string_cat_printf(output, "%s - %s", event_type, event_subtype);
return;
case 0x06:
event_type = "REM2";
event_subtype = "Off1";
furi_string_cat_printf(output, "%s - %s", event_type, event_subtype);
return;
default:
event_type = "Unknown";
break;
}
switch(event & 0x0F) {
case 0x00:
event_subtype = (((event >> 4) & 0x0F) > 0x03) ? "Arm1" : "Sealed";
break;
case 0x01:
event_subtype = (((event >> 4) & 0x0F) > 0x03) ? "Btn1" : "Alarm";
break;
case 0x02:
event_subtype = (((event >> 4) & 0x0F) > 0x03) ? "Btn2" : "Tamper";
break;
case 0x03:
event_subtype = (((event >> 4) & 0x0F) > 0x03) ? "Btn3" : "Alarm + Tamper";
break;
case 0x08:
event_subtype = "Reset";
break;
case 0x09:
event_subtype = "LowBatt";
break;
case 0x0A:
event_subtype = "BattOk";
break;
case 0x0B:
event_subtype = "Learn";
break;
default:
event_subtype = "Unknown";
break;
}
furi_string_cat_printf(output, "%s - %s", event_type, event_subtype);
}
uint8_t subghz_protocol_decoder_magellan_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_magellan_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_magellan_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_magellan_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_magellan_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderMagellan* instance = context;
subghz_protocol_magellan_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%08lX\r\n"
"Sn:%03ld%03ld, Event:0x%02X\r\n"
"Stat:",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
(instance->generic.serial >> 8) & 0xFF,
instance->generic.serial & 0xFF,
instance->generic.btn);
subghz_protocol_magellan_get_event_serialize(instance->generic.btn, output);
}
| 18,558
|
C
|
.c
| 458
| 33.272926
| 100
| 0.651629
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,280
|
doitrand.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/doitrand.c
|
#include "doitrand.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolDoitrand"
#define DIP_PATTERN "%c%c%c%c%c%c%c%c%c%c"
#define CNT_TO_DIP(dip) \
(dip & 0x0001 ? '1' : '0'), (dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), \
(dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), (dip & 0x1000 ? '1' : '0'), \
(dip & 0x0800 ? '1' : '0'), (dip & 0x0400 ? '1' : '0'), (dip & 0x0200 ? '1' : '0'), \
(dip & 0x0002 ? '1' : '0')
static const SubGhzBlockConst subghz_protocol_doitrand_const = {
.te_short = 400,
.te_long = 1100,
.te_delta = 150,
.min_count_bit_for_found = 37,
};
struct SubGhzProtocolDecoderDoitrand {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderDoitrand {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
DoitrandDecoderStepReset = 0,
DoitrandDecoderStepFoundStartBit,
DoitrandDecoderStepSaveDuration,
DoitrandDecoderStepCheckDuration,
} DoitrandDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_doitrand_decoder = {
.alloc = subghz_protocol_decoder_doitrand_alloc,
.free = subghz_protocol_decoder_doitrand_free,
.feed = subghz_protocol_decoder_doitrand_feed,
.reset = subghz_protocol_decoder_doitrand_reset,
.get_hash_data = subghz_protocol_decoder_doitrand_get_hash_data,
.serialize = subghz_protocol_decoder_doitrand_serialize,
.deserialize = subghz_protocol_decoder_doitrand_deserialize,
.get_string = subghz_protocol_decoder_doitrand_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_doitrand_encoder = {
.alloc = subghz_protocol_encoder_doitrand_alloc,
.free = subghz_protocol_encoder_doitrand_free,
.deserialize = subghz_protocol_encoder_doitrand_deserialize,
.stop = subghz_protocol_encoder_doitrand_stop,
.yield = subghz_protocol_encoder_doitrand_yield,
};
const SubGhzProtocol subghz_protocol_doitrand = {
.name = SUBGHZ_PROTOCOL_DOITRAND_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_doitrand_decoder,
.encoder = &subghz_protocol_doitrand_encoder,
};
void* subghz_protocol_encoder_doitrand_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderDoitrand* instance = malloc(sizeof(SubGhzProtocolEncoderDoitrand));
instance->base.protocol = &subghz_protocol_doitrand;
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_running = false;
return instance;
}
void subghz_protocol_encoder_doitrand_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderDoitrand* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderDoitrand instance
* @return true On success
*/
static bool subghz_protocol_encoder_doitrand_get_upload(SubGhzProtocolEncoderDoitrand* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_doitrand_const.te_short * 62);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_doitrand_const.te_short * 2 - 100);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_doitrand_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_doitrand_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_doitrand_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_doitrand_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_doitrand_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderDoitrand* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_doitrand_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_doitrand_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_doitrand_stop(void* context) {
SubGhzProtocolEncoderDoitrand* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_doitrand_yield(void* context) {
SubGhzProtocolEncoderDoitrand* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_doitrand_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderDoitrand* instance = malloc(sizeof(SubGhzProtocolDecoderDoitrand));
instance->base.protocol = &subghz_protocol_doitrand;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_doitrand_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
free(instance);
}
void subghz_protocol_decoder_doitrand_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
instance->decoder.parser_step = DoitrandDecoderStepReset;
}
void subghz_protocol_decoder_doitrand_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
switch(instance->decoder.parser_step) {
case DoitrandDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_doitrand_const.te_short * 62) <
subghz_protocol_doitrand_const.te_delta * 30)) {
//Found Preambula
instance->decoder.parser_step = DoitrandDecoderStepFoundStartBit;
}
break;
case DoitrandDecoderStepFoundStartBit:
if(level && (DURATION_DIFF(duration, (subghz_protocol_doitrand_const.te_short * 2)) <
subghz_protocol_doitrand_const.te_delta * 3)) {
//Found start bit
instance->decoder.parser_step = DoitrandDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = DoitrandDecoderStepReset;
}
break;
case DoitrandDecoderStepSaveDuration:
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_doitrand_const.te_short * 10 +
subghz_protocol_doitrand_const.te_delta)) {
instance->decoder.parser_step = DoitrandDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit ==
subghz_protocol_doitrand_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = DoitrandDecoderStepCheckDuration;
}
}
break;
case DoitrandDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_doitrand_const.te_short) <
subghz_protocol_doitrand_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_doitrand_const.te_long) <
subghz_protocol_doitrand_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = DoitrandDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_doitrand_const.te_long) <
subghz_protocol_doitrand_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_doitrand_const.te_short) <
subghz_protocol_doitrand_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = DoitrandDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = DoitrandDecoderStepReset;
}
} else {
instance->decoder.parser_step = DoitrandDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_doitrand_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* 67892345 0 k 1
* 0000082F5F => 00000000000000000 10 000010111101011111
* 0002082F5F => 00000000000100000 10 000010111101011111
* 0200082F5F => 00010000000000000 10 000010111101011111
* 0400082F5F => 00100000000000000 10 000010111101011111
* 0800082F5F => 01000000000000000 10 000010111101011111
* 1000082F5F => 10000000000000000 10 000010111101011111
* 0020082F5F => 00000001000000000 10 000010111101011111
* 0040082F5F => 00000010000000000 10 000010111101011111
* 0080082F5F => 00000100000000000 10 000010111101011111
* 0100082F5F => 00001000000000000 10 000010111101011111
* 000008AF5F => 00000000000000000 10 001010111101011111
* 1FE208AF5F => 11111111000100000 10 001010111101011111
*
* 0...9 - DIP
* k- KEY
*/
instance->cnt = (instance->data >> 24) | ((instance->data >> 15) & 0x1);
instance->btn = ((instance->data >> 18) & 0x3);
}
uint8_t subghz_protocol_decoder_doitrand_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_doitrand_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_doitrand_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_doitrand_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_doitrand_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderDoitrand* instance = context;
subghz_protocol_doitrand_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%02lX%08lX\r\n"
"Btn:%X\r\n"
"DIP:" DIP_PATTERN "\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32) & 0xFFFFFFFF,
(uint32_t)(instance->generic.data & 0xFFFFFFFF),
instance->generic.btn,
CNT_TO_DIP(instance->generic.cnt));
}
| 13,568
|
C
|
.c
| 307
| 37.153094
| 99
| 0.679955
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,281
|
nero_sketch.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/nero_sketch.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_NERO_SKETCH_NAME "Nero Sketch"
typedef struct SubGhzProtocolDecoderNeroSketch SubGhzProtocolDecoderNeroSketch;
typedef struct SubGhzProtocolEncoderNeroSketch SubGhzProtocolEncoderNeroSketch;
extern const SubGhzProtocolDecoder subghz_protocol_nero_sketch_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_nero_sketch_encoder;
extern const SubGhzProtocol subghz_protocol_nero_sketch;
/**
* Allocate SubGhzProtocolEncoderNeroSketch.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderNeroSketch* pointer to a SubGhzProtocolEncoderNeroSketch instance
*/
void* subghz_protocol_encoder_nero_sketch_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderNeroSketch.
* @param context Pointer to a SubGhzProtocolEncoderNeroSketch instance
*/
void subghz_protocol_encoder_nero_sketch_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderNeroSketch instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_encoder_nero_sketch_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderNeroSketch instance
*/
void subghz_protocol_encoder_nero_sketch_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderNeroSketch instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_nero_sketch_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderNeroSketch.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderNeroSketch* pointer to a SubGhzProtocolDecoderNeroSketch instance
*/
void* subghz_protocol_decoder_nero_sketch_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderNeroSketch.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
*/
void subghz_protocol_decoder_nero_sketch_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderNeroSketch.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
*/
void subghz_protocol_decoder_nero_sketch_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_nero_sketch_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_nero_sketch_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderNeroSketch.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_nero_sketch_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderNeroSketch.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_nero_sketch_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderNeroSketch instance
* @param output Resulting text
*/
void subghz_protocol_decoder_nero_sketch_get_string(void* context, FuriString* output);
| 3,980
|
C
|
.c
| 92
| 41.271739
| 98
| 0.822268
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,282
|
dickert_mahs.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/dickert_mahs.c
|
#include "dickert_mahs.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_rtc.h>
#define TAG "SubGhzProtocolDicketMAHS"
static const SubGhzBlockConst subghz_protocol_dickert_mahs_const = {
.te_short = 400,
.te_long = 800,
.te_delta = 100,
.min_count_bit_for_found = 36,
};
struct SubGhzProtocolDecoderDickertMAHS {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint32_t tmp[2];
uint8_t tmp_cnt;
};
struct SubGhzProtocolEncoderDickertMAHS {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
DickertMAHSDecoderStepReset = 0,
DickertMAHSDecoderStepInitial,
DickertMAHSDecoderStepRecording,
} DickertMAHSDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_dickert_mahs_decoder = {
.alloc = subghz_protocol_decoder_dickert_mahs_alloc,
.free = subghz_protocol_decoder_dickert_mahs_free,
.feed = subghz_protocol_decoder_dickert_mahs_feed,
.reset = subghz_protocol_decoder_dickert_mahs_reset,
.get_hash_data = subghz_protocol_decoder_dickert_mahs_get_hash_data,
.serialize = subghz_protocol_decoder_dickert_mahs_serialize,
.deserialize = subghz_protocol_decoder_dickert_mahs_deserialize,
.get_string = subghz_protocol_decoder_dickert_mahs_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_dickert_mahs_encoder = {
.alloc = subghz_protocol_encoder_dickert_mahs_alloc,
.free = subghz_protocol_encoder_dickert_mahs_free,
.deserialize = subghz_protocol_encoder_dickert_mahs_deserialize,
.stop = subghz_protocol_encoder_dickert_mahs_stop,
.yield = subghz_protocol_encoder_dickert_mahs_yield,
};
const SubGhzProtocol subghz_protocol_dickert_mahs = {
.name = SUBGHZ_PROTOCOL_DICKERT_MAHS_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_dickert_mahs_decoder,
.encoder = &subghz_protocol_dickert_mahs_encoder,
};
static void subghz_protocol_encoder_dickert_mahs_parse_buffer(
SubGhzProtocolDecoderDickertMAHS* instance,
FuriString* output) {
// We assume we have only decodes < 64 bit!
uint64_t data = instance->generic.data;
uint8_t bits[36] = {};
// Convert uint64_t into bit array
for(int i = 35; i >= 0; i--) {
if(data & 1) {
bits[i] = 1;
}
data >>= 1;
}
// Decode symbols
FuriString* code = furi_string_alloc();
for(size_t i = 0; i < 35; i += 2) {
uint8_t dip = (bits[i] << 1) + bits[i + 1];
// PLUS = 3, // 0b11
// ZERO = 1, // 0b01
// MINUS = 0, // 0x00
if(dip == 0x01) {
furi_string_cat(code, "0");
} else if(dip == 0x00) {
furi_string_cat(code, "-");
} else if(dip == 0x03) {
furi_string_cat(code, "+");
} else {
furi_string_cat(code, "?");
}
}
FuriString* user_dips = furi_string_alloc();
FuriString* fact_dips = furi_string_alloc();
furi_string_set_n(user_dips, code, 0, 10);
furi_string_set_n(fact_dips, code, 10, 8);
furi_string_cat_printf(
output,
"%s\r\n"
"User-Dips:\t%s\r\n"
"Fac-Code:\t%s\r\n",
instance->generic.protocol_name,
furi_string_get_cstr(user_dips),
furi_string_get_cstr(fact_dips));
furi_string_free(user_dips);
furi_string_free(fact_dips);
furi_string_free(code);
}
void* subghz_protocol_encoder_dickert_mahs_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderDickertMAHS* instance = malloc(sizeof(SubGhzProtocolEncoderDickertMAHS));
instance->base.protocol = &subghz_protocol_dickert_mahs;
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_running = false;
return instance;
}
void subghz_protocol_encoder_dickert_mahs_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderDickertMAHS* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderDickertMAHS instance
* @return true On success
*/
static bool
subghz_protocol_encoder_dickert_mahs_get_upload(SubGhzProtocolEncoderDickertMAHS* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_dickert_mahs_const.te_short * 112);
// Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_dickert_mahs_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_dickert_mahs_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_dickert_mahs_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_dickert_mahs_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_dickert_mahs_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderDickertMAHS* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
// Allow for longer keys (<) instead of !=
if((instance->generic.data_count_bit <
subghz_protocol_dickert_mahs_const.min_count_bit_for_found)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_dickert_mahs_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_dickert_mahs_stop(void* context) {
SubGhzProtocolEncoderDickertMAHS* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_dickert_mahs_yield(void* context) {
SubGhzProtocolEncoderDickertMAHS* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_dickert_mahs_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderDickertMAHS* instance = malloc(sizeof(SubGhzProtocolDecoderDickertMAHS));
instance->base.protocol = &subghz_protocol_dickert_mahs;
instance->generic.protocol_name = instance->base.protocol->name;
instance->tmp_cnt = 0;
return instance;
}
void subghz_protocol_decoder_dickert_mahs_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDickertMAHS* instance = context;
free(instance);
}
void subghz_protocol_decoder_dickert_mahs_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDickertMAHS* instance = context;
instance->decoder.parser_step = DickertMAHSDecoderStepReset;
}
void subghz_protocol_decoder_dickert_mahs_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderDickertMAHS* instance = context;
switch(instance->decoder.parser_step) {
case DickertMAHSDecoderStepReset:
// Check if done
if(instance->decoder.decode_count_bit >=
subghz_protocol_dickert_mahs_const.min_count_bit_for_found) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
if((!level) && (duration > 10 * subghz_protocol_dickert_mahs_const.te_short)) {
//Found header DICKERT_MAHS
instance->decoder.parser_step = DickertMAHSDecoderStepInitial;
}
break;
case DickertMAHSDecoderStepInitial:
if(!level) {
break;
} else if(
DURATION_DIFF(duration, subghz_protocol_dickert_mahs_const.te_short) <
subghz_protocol_dickert_mahs_const.te_delta) {
//Found start bit DICKERT_MAHS
instance->decoder.parser_step = DickertMAHSDecoderStepRecording;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = DickertMAHSDecoderStepReset;
}
break;
case DickertMAHSDecoderStepRecording:
if((!level && instance->tmp_cnt == 0) || (level && instance->tmp_cnt == 1)) {
instance->tmp[instance->tmp_cnt] = duration;
instance->tmp_cnt++;
if(instance->tmp_cnt == 2) {
if(DURATION_DIFF(instance->tmp[0] + instance->tmp[1], 1200) <
subghz_protocol_dickert_mahs_const.te_delta) {
if(DURATION_DIFF(instance->tmp[0], subghz_protocol_dickert_mahs_const.te_long) <
subghz_protocol_dickert_mahs_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else if(
DURATION_DIFF(
instance->tmp[0], subghz_protocol_dickert_mahs_const.te_short) <
subghz_protocol_dickert_mahs_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
}
instance->tmp_cnt = 0;
} else {
instance->tmp_cnt = 0;
instance->decoder.parser_step = DickertMAHSDecoderStepReset;
}
}
} else {
instance->tmp_cnt = 0;
instance->decoder.parser_step = DickertMAHSDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_dickert_mahs_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderDickertMAHS* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_dickert_mahs_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderDickertMAHS* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_dickert_mahs_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderDickertMAHS* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
// Allow for longer keys (<) instead of !=
if((instance->generic.data_count_bit <
subghz_protocol_dickert_mahs_const.min_count_bit_for_found)) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
} while(false);
return ret;
}
void subghz_protocol_decoder_dickert_mahs_get_string(void* context, FuriString* output) {
furi_assert(context);
subghz_protocol_encoder_dickert_mahs_parse_buffer(context, output);
}
| 13,603
|
C
|
.c
| 326
| 34.260736
| 100
| 0.664775
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,283
|
faac_slh.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/faac_slh.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_FAAC_SLH_NAME "Faac SLH"
typedef struct SubGhzProtocolDecoderFaacSLH SubGhzProtocolDecoderFaacSLH;
typedef struct SubGhzProtocolEncoderFaacSLH SubGhzProtocolEncoderFaacSLH;
extern const SubGhzProtocolDecoder subghz_protocol_faac_slh_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_faac_slh_encoder;
extern const SubGhzProtocol subghz_protocol_faac_slh;
/**
* Allocate SubGhzProtocolEncoderFaacSLH.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderFaacSLH* pointer to a SubGhzProtocolEncoderFaacSLH instance
*/
void* subghz_protocol_encoder_faac_slh_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderFaacSLH.
* @param context Pointer to a SubGhzProtocolEncoderFaacSLH instance
*/
void subghz_protocol_encoder_faac_slh_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderFaacSLH instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
SubGhzProtocolStatus
subghz_protocol_encoder_faac_slh_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderFaacSLH instance
*/
void subghz_protocol_encoder_faac_slh_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderFaacSLH instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_faac_slh_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderFaacSLH.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderFaacSLH* pointer to a SubGhzProtocolDecoderFaacSLH instance
*/
void* subghz_protocol_decoder_faac_slh_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderFaacSLH.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
*/
void subghz_protocol_decoder_faac_slh_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderFaacSLH.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
*/
void subghz_protocol_decoder_faac_slh_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_faac_slh_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_faac_slh_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderFaacSLH.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_faac_slh_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderFaacSLH.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_faac_slh_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderFaacSLH instance
* @param output Resulting text
*/
void subghz_protocol_decoder_faac_slh_get_string(void* context, FuriString* output);
// Reset prog mode vars
// TODO: Remake in proper way
void faac_slh_reset_prog_mode(void);
| 3,949
|
C
|
.c
| 95
| 39.6
| 95
| 0.81439
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,284
|
came.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/came.c
|
#include "came.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
/*
* Help
* https://phreakerclub.com/447
*
*/
#define TAG "SubGhzProtocolCame"
#define CAME_12_COUNT_BIT 12
#define CAME_24_COUNT_BIT 24
#define PRASTEL_COUNT_BIT 25
#define PRASTEL_NAME "Prastel"
#define AIRFORCE_COUNT_BIT 18
#define AIRFORCE_NAME "Airforce"
static const SubGhzBlockConst subghz_protocol_came_const = {
.te_short = 320,
.te_long = 640,
.te_delta = 150,
.min_count_bit_for_found = 12,
};
struct SubGhzProtocolDecoderCame {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderCame {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
CameDecoderStepReset = 0,
CameDecoderStepFoundStartBit,
CameDecoderStepSaveDuration,
CameDecoderStepCheckDuration,
} CameDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_came_decoder = {
.alloc = subghz_protocol_decoder_came_alloc,
.free = subghz_protocol_decoder_came_free,
.feed = subghz_protocol_decoder_came_feed,
.reset = subghz_protocol_decoder_came_reset,
.get_hash_data = subghz_protocol_decoder_came_get_hash_data,
.serialize = subghz_protocol_decoder_came_serialize,
.deserialize = subghz_protocol_decoder_came_deserialize,
.get_string = subghz_protocol_decoder_came_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_came_encoder = {
.alloc = subghz_protocol_encoder_came_alloc,
.free = subghz_protocol_encoder_came_free,
.deserialize = subghz_protocol_encoder_came_deserialize,
.stop = subghz_protocol_encoder_came_stop,
.yield = subghz_protocol_encoder_came_yield,
};
const SubGhzProtocol subghz_protocol_came = {
.name = SUBGHZ_PROTOCOL_CAME_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_came_decoder,
.encoder = &subghz_protocol_came_encoder,
};
void* subghz_protocol_encoder_came_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderCame* instance = malloc(sizeof(SubGhzProtocolEncoderCame));
instance->base.protocol = &subghz_protocol_came;
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_running = false;
return instance;
}
void subghz_protocol_encoder_came_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderCame* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderCame instance
* @return true On success
*/
static bool subghz_protocol_encoder_came_get_upload(SubGhzProtocolEncoderCame* instance) {
furi_assert(instance);
uint32_t header_te = 0;
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
switch(instance->generic.data_count_bit) {
case CAME_24_COUNT_BIT:
// CAME 24 Bit = 24320 us
header_te = 76;
break;
case CAME_12_COUNT_BIT:
case AIRFORCE_COUNT_BIT:
// CAME 12 Bit Original only! and Airforce protocol = 15040 us
header_te = 47;
break;
case PRASTEL_COUNT_BIT:
// PRASTEL = 11520 us
header_te = 36;
break;
default:
// Some wrong detected protocols, 5120 us
header_te = 16;
break;
}
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_came_const.te_short * header_te);
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_came_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_came_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_came_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_came_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_came_const.te_long);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_came_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderCame* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(instance->generic.data_count_bit > PRASTEL_COUNT_BIT) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_came_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_came_stop(void* context) {
SubGhzProtocolEncoderCame* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_came_yield(void* context) {
SubGhzProtocolEncoderCame* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_came_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderCame* instance = malloc(sizeof(SubGhzProtocolDecoderCame));
instance->base.protocol = &subghz_protocol_came;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_came_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
free(instance);
}
void subghz_protocol_decoder_came_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
instance->decoder.parser_step = CameDecoderStepReset;
}
void subghz_protocol_decoder_came_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
switch(instance->decoder.parser_step) {
case CameDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_came_const.te_short * 56) <
subghz_protocol_came_const.te_delta * 47)) {
//Found header CAME
instance->decoder.parser_step = CameDecoderStepFoundStartBit;
}
break;
case CameDecoderStepFoundStartBit:
if(!level) {
break;
} else if(
DURATION_DIFF(duration, subghz_protocol_came_const.te_short) <
subghz_protocol_came_const.te_delta) {
//Found start bit CAME
instance->decoder.parser_step = CameDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = CameDecoderStepReset;
}
break;
case CameDecoderStepSaveDuration:
if(!level) { //save interval
if(duration >= (subghz_protocol_came_const.te_short * 4)) {
instance->decoder.parser_step = CameDecoderStepFoundStartBit;
if((instance->decoder.decode_count_bit ==
subghz_protocol_came_const.min_count_bit_for_found) ||
(instance->decoder.decode_count_bit == AIRFORCE_COUNT_BIT) ||
(instance->decoder.decode_count_bit == PRASTEL_COUNT_BIT) ||
(instance->decoder.decode_count_bit == CAME_24_COUNT_BIT)) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
instance->decoder.te_last = duration;
instance->decoder.parser_step = CameDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = CameDecoderStepReset;
}
break;
case CameDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_came_const.te_short) <
subghz_protocol_came_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_came_const.te_long) <
subghz_protocol_came_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = CameDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_came_const.te_long) <
subghz_protocol_came_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_came_const.te_short) <
subghz_protocol_came_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = CameDecoderStepSaveDuration;
} else
instance->decoder.parser_step = CameDecoderStepReset;
} else {
instance->decoder.parser_step = CameDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_came_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_came_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_came_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(instance->generic.data_count_bit > PRASTEL_COUNT_BIT) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
} while(false);
return ret;
}
void subghz_protocol_decoder_came_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderCame* instance = context;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%08lX\r\n"
"Yek:0x%08lX\r\n",
(instance->generic.data_count_bit == PRASTEL_COUNT_BIT ?
PRASTEL_NAME :
(instance->generic.data_count_bit == AIRFORCE_COUNT_BIT ?
AIRFORCE_NAME :
instance->generic.protocol_name)),
instance->generic.data_count_bit,
code_found_lo,
code_found_reverse_lo);
}
| 13,359
|
C
|
.c
| 327
| 33.327217
| 95
| 0.667642
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,285
|
alutech_at_4n.h
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/alutech_at_4n.h
|
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_ALUTECH_AT_4N_NAME "Alutech AT-4N"
typedef struct SubGhzProtocolDecoderAlutech_at_4n SubGhzProtocolDecoderAlutech_at_4n;
typedef struct SubGhzProtocolEncoderAlutech_at_4n SubGhzProtocolEncoderAlutech_at_4n;
extern const SubGhzProtocolDecoder subghz_protocol_alutech_at_4n_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_alutech_at_4n_encoder;
extern const SubGhzProtocol subghz_protocol_alutech_at_4n;
/**
* Allocate SubGhzProtocolEncoderAlutech_at_4n.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderAlutech_at_4n* pointer to a SubGhzProtocolEncoderAlutech_at_4n instance
*/
void* subghz_protocol_encoder_alutech_at_4n_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderAlutech_at_4n.
* @param context Pointer to a SubGhzProtocolEncoderAlutech_at_4n instance
*/
void subghz_protocol_encoder_alutech_at_4n_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderAlutech_at_4n instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
SubGhzProtocolStatus
subghz_protocol_encoder_alutech_at_4n_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderAlutech_at_4n instance
*/
void subghz_protocol_encoder_alutech_at_4n_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderAlutech_at_4n instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_alutech_at_4n_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderAlutech_at_4n.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderAlutech_at_4n* pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
*/
void* subghz_protocol_decoder_alutech_at_4n_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderAlutech_at_4n.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
*/
void subghz_protocol_decoder_alutech_at_4n_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderAlutech_at_4n.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
*/
void subghz_protocol_decoder_alutech_at_4n_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_alutech_at_4n_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_alutech_at_4n_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderAlutech_at_4n.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return status
*/
SubGhzProtocolStatus subghz_protocol_decoder_alutech_at_4n_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderAlutech_at_4n.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return status
*/
SubGhzProtocolStatus
subghz_protocol_decoder_alutech_at_4n_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderAlutech_at_4n instance
* @param output Resulting text
*/
void subghz_protocol_decoder_alutech_at_4n_get_string(void* context, FuriString* output);
| 4,102
|
C
|
.c
| 92
| 42.608696
| 103
| 0.809715
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,286
|
kia.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/kia.c
|
#include "kia.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocoKia"
static const SubGhzBlockConst subghz_protocol_kia_const = {
.te_short = 250,
.te_long = 500,
.te_delta = 100,
.min_count_bit_for_found = 61,
};
struct SubGhzProtocolDecoderKIA {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint16_t header_count;
};
struct SubGhzProtocolEncoderKIA {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
KIADecoderStepReset = 0,
KIADecoderStepCheckPreambula,
KIADecoderStepSaveDuration,
KIADecoderStepCheckDuration,
} KIADecoderStep;
const SubGhzProtocolDecoder subghz_protocol_kia_decoder = {
.alloc = subghz_protocol_decoder_kia_alloc,
.free = subghz_protocol_decoder_kia_free,
.feed = subghz_protocol_decoder_kia_feed,
.reset = subghz_protocol_decoder_kia_reset,
.get_hash_data = subghz_protocol_decoder_kia_get_hash_data,
.serialize = subghz_protocol_decoder_kia_serialize,
.deserialize = subghz_protocol_decoder_kia_deserialize,
.get_string = subghz_protocol_decoder_kia_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_kia_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol subghz_protocol_kia = {
.name = SUBGHZ_PROTOCOL_KIA_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_AutoAlarms,
.decoder = &subghz_protocol_kia_decoder,
.encoder = &subghz_protocol_kia_encoder,
};
void* subghz_protocol_decoder_kia_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderKIA* instance = malloc(sizeof(SubGhzProtocolDecoderKIA));
instance->base.protocol = &subghz_protocol_kia;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_kia_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
free(instance);
}
void subghz_protocol_decoder_kia_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
instance->decoder.parser_step = KIADecoderStepReset;
}
void subghz_protocol_decoder_kia_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
switch(instance->decoder.parser_step) {
case KIADecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
subghz_protocol_kia_const.te_delta)) {
instance->decoder.parser_step = KIADecoderStepCheckPreambula;
instance->decoder.te_last = duration;
instance->header_count = 0;
}
break;
case KIADecoderStepCheckPreambula:
if(level) {
if((DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
subghz_protocol_kia_const.te_delta) ||
(DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
subghz_protocol_kia_const.te_delta)) {
instance->decoder.te_last = duration;
} else {
instance->decoder.parser_step = KIADecoderStepReset;
}
} else if(
(DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
subghz_protocol_kia_const.te_delta) &&
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
subghz_protocol_kia_const.te_delta)) {
// Found header
instance->header_count++;
break;
} else if(
(DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
subghz_protocol_kia_const.te_delta) &&
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
subghz_protocol_kia_const.te_delta)) {
// Found start bit
if(instance->header_count > 15) {
instance->decoder.parser_step = KIADecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 1;
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
} else {
instance->decoder.parser_step = KIADecoderStepReset;
}
} else {
instance->decoder.parser_step = KIADecoderStepReset;
}
break;
case KIADecoderStepSaveDuration:
if(level) {
if(duration >=
(subghz_protocol_kia_const.te_long + subghz_protocol_kia_const.te_delta * 2UL)) {
//Found stop bit
instance->decoder.parser_step = KIADecoderStepReset;
if(instance->decoder.decode_count_bit ==
subghz_protocol_kia_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = KIADecoderStepCheckDuration;
}
} else {
instance->decoder.parser_step = KIADecoderStepReset;
}
break;
case KIADecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_short) <
subghz_protocol_kia_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_kia_const.te_short) <
subghz_protocol_kia_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = KIADecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_kia_const.te_long) <
subghz_protocol_kia_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_kia_const.te_long) <
subghz_protocol_kia_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = KIADecoderStepSaveDuration;
} else {
instance->decoder.parser_step = KIADecoderStepReset;
}
} else {
instance->decoder.parser_step = KIADecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_kia_crc8(uint8_t* data, size_t len) {
uint8_t crc = 0x08;
size_t i, j;
for(i = 0; i < len; i++) {
crc ^= data[i];
for(j = 0; j < 8; j++) {
if((crc & 0x80) != 0)
crc = (uint8_t)((crc << 1) ^ 0x7F);
else
crc <<= 1;
}
}
return crc;
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_kia_check_remote_controller(SubGhzBlockGeneric* instance) {
/*
* 0x0F 0112 43B04EC 1 7D
* 0x0F 0113 43B04EC 1 DF
* 0x0F 0114 43B04EC 1 30
* 0x0F 0115 43B04EC 2 13
* 0x0F 0116 43B04EC 3 F5
* CNT Serial K CRC8 Kia (CRC8, poly 0x7f, start_crc 0x08)
*/
instance->serial = (uint32_t)((instance->data >> 12) & 0x0FFFFFFF);
instance->btn = (instance->data >> 8) & 0x0F;
instance->cnt = (instance->data >> 40) & 0xFFFF;
}
uint8_t subghz_protocol_decoder_kia_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_kia_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_kia_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_kia_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_kia_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderKIA* instance = context;
subghz_protocol_kia_check_remote_controller(&instance->generic);
uint32_t code_found_hi = instance->generic.data >> 32;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%08lX%08lX\r\n"
"Sn:%07lX Btn:%X Cnt:%04lX\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
code_found_hi,
code_found_lo,
instance->generic.serial,
instance->generic.btn,
instance->generic.cnt);
}
| 9,703
|
C
|
.c
| 239
| 32.364017
| 96
| 0.644084
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,287
|
honeywell.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/honeywell.c
|
#include "honeywell.h"
#include <lib/toolbox/manchester_decoder.h>
//Created by HTotoo 2023-10-30
//Got a lot of help from LiQuiDz.
//Protocol decoding help from: https://github.com/merbanan/rtl_433/blob/master/src/devices/honeywell.c
/*
64 bit packets, repeated multiple times per open/close event.
Protocol whitepaper: "DEFCON 22: Home Insecurity" by Logan Lamb.
Data layout:
PP PP C IIIII EE SS SS
- P: 16bit Preamble and sync bit (always ff fe)
- C: 4bit Channel
- I: 20bit Device serial number / or counter value
- E: 8bit Event, where 0x80 = Open/Close, 0x04 = Heartbeat / or id
- S: 16bit CRC
*/
#define TAG "SubGhzProtocolHoneywell"
uint16_t subghz_protocol_honeywell_crc16(
uint8_t const message[],
unsigned nBytes,
uint16_t polynomial,
uint16_t init) {
uint16_t remainder = init;
unsigned byte, bit;
for(byte = 0; byte < nBytes; ++byte) {
remainder ^= message[byte] << 8;
for(bit = 0; bit < 8; ++bit) {
if(remainder & 0x8000) {
remainder = (remainder << 1) ^ polynomial;
} else {
remainder = (remainder << 1);
}
}
}
return remainder;
}
void subghz_protocol_decoder_honeywell_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
free(instance);
}
void subghz_protocol_decoder_honeywell_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
void subghz_protocol_decoder_honeywell_addbit(void* context, bool data) {
SubGhzProtocolDecoderHoneywell* instance = context;
instance->decoder.decode_data = (instance->decoder.decode_data << 1) | data;
instance->decoder.decode_count_bit++;
if(instance->decoder.decode_count_bit < 62) return;
uint16_t preamble = (instance->decoder.decode_data >> 48) & 0xFFFF;
//can be multiple, since flipper can't read it well..
if(preamble == 0b0011111111111110 || preamble == 0b0111111111111110 ||
preamble == 0b1111111111111110) {
uint8_t datatocrc[4];
datatocrc[0] = (instance->decoder.decode_data >> 40) & 0xFFFF;
datatocrc[1] = (instance->decoder.decode_data >> 32) & 0xFFFF;
datatocrc[2] = (instance->decoder.decode_data >> 24) & 0xFFFF;
datatocrc[3] = (instance->decoder.decode_data >> 16) & 0xFFFF;
uint8_t channel = (instance->decoder.decode_data >> 44) & 0xF;
uint16_t crc_calc = 0;
if(channel == 0x2 || channel == 0x4 || channel == 0xA) {
// 2GIG brand
crc_calc = subghz_protocol_honeywell_crc16(datatocrc, 4, 0x8050, 0);
} else if(channel == 0x8) {
crc_calc = subghz_protocol_honeywell_crc16(datatocrc, 4, 0x8005, 0);
} else {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
return;
}
uint16_t crc = instance->decoder.decode_data & 0xFFFF;
if(crc == crc_calc) {
//the data is good. process it.
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit =
instance->decoder
.decode_count_bit; //maybe set it to 64, and hack the first 2 bits to 1! will see if replay needs it
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
return;
}
} else if(instance->decoder.decode_count_bit >= 64) {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
return;
}
}
void subghz_protocol_decoder_honeywell_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
ManchesterEvent event = ManchesterEventReset;
if(!level) {
if(DURATION_DIFF(duration, subghz_protocol_honeywell_const.te_short) <
subghz_protocol_honeywell_const.te_delta) {
event = ManchesterEventShortLow;
} else if(
DURATION_DIFF(duration, subghz_protocol_honeywell_const.te_long) <
subghz_protocol_honeywell_const.te_delta * 2) {
event = ManchesterEventLongLow;
}
} else {
if(DURATION_DIFF(duration, subghz_protocol_honeywell_const.te_short) <
subghz_protocol_honeywell_const.te_delta) {
event = ManchesterEventShortHigh;
} else if(
DURATION_DIFF(duration, subghz_protocol_honeywell_const.te_long) <
subghz_protocol_honeywell_const.te_delta * 2) {
event = ManchesterEventLongHigh;
}
}
if(event != ManchesterEventReset) {
bool data;
bool data_ok = manchester_advance(
instance->manchester_saved_state, event, &instance->manchester_saved_state, &data);
if(data_ok) {
subghz_protocol_decoder_honeywell_addbit(instance, data);
}
} else {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
}
uint8_t subghz_protocol_decoder_honeywell_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_honeywell_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_honeywell_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_honeywell_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_honeywell_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderHoneywell* instance = context;
// Parse here and not in decode to avoid visual glitches when loading from file
instance->generic.serial = (instance->generic.data >> 24) & 0xFFFFF;
instance->generic.btn = (instance->generic.data >> 16) &
0xFF; //not exactly button, but can contain btn data too.
uint8_t channel = (instance->generic.data >> 44) & 0xF;
uint8_t contact = (instance->generic.btn & 0x80) >> 7;
uint8_t tamper = (instance->generic.btn & 0x40) >> 6;
uint8_t reed = (instance->generic.btn & 0x20) >> 5;
uint8_t alarm = (instance->generic.btn & 0x10) >> 4;
uint8_t battery_low = (instance->generic.btn & 0x08) >> 3;
uint8_t heartbeat = (instance->generic.btn & 0x04) >> 2;
furi_string_cat_printf(
output,
"%s\r\n%dbit "
"Sn:%07lu\r\nCh:%u Bat:%d Hb: %d\r\n"
"L1: %u, L2: %u, L3: %u, L4: %u\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
instance->generic.serial,
channel,
battery_low,
heartbeat,
contact,
reed,
alarm,
tamper);
}
void* subghz_protocol_decoder_honeywell_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderHoneywell* instance = malloc(sizeof(SubGhzProtocolDecoderHoneywell));
instance->base.protocol = &subghz_protocol_honeywell;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void* subghz_protocol_encoder_honeywell_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderHoneywell* instance = malloc(sizeof(SubGhzProtocolEncoderHoneywell));
instance->base.protocol = &subghz_protocol_honeywell;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 3;
instance->encoder.size_upload = 64 * 2 + 10;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_honeywell_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderHoneywell* instance = context;
free(instance->encoder.upload);
free(instance);
}
static LevelDuration
subghz_protocol_encoder_honeywell_add_duration_to_upload(ManchesterEncoderResult result) {
LevelDuration data = {.duration = 0, .level = 0};
switch(result) {
case ManchesterEncoderResultShortLow:
data.duration = subghz_protocol_honeywell_const.te_short;
data.level = false;
break;
case ManchesterEncoderResultLongLow:
data.duration = subghz_protocol_honeywell_const.te_long;
data.level = false;
break;
case ManchesterEncoderResultLongHigh:
data.duration = subghz_protocol_honeywell_const.te_long;
data.level = true;
break;
case ManchesterEncoderResultShortHigh:
data.duration = subghz_protocol_honeywell_const.te_short;
data.level = true;
break;
default:
furi_crash("SubGhz: ManchesterEncoderResult is incorrect.");
break;
}
return level_duration_make(data.level, data.duration);
}
static void
subghz_protocol_encoder_honeywell_get_upload(SubGhzProtocolEncoderHoneywell* instance) {
furi_assert(instance);
size_t index = 0;
ManchesterEncoderState enc_state;
manchester_encoder_reset(&enc_state);
ManchesterEncoderResult result;
for(uint8_t i = 63; i > 0; i--) {
if(!manchester_encoder_advance(
&enc_state, bit_read(instance->generic.data, i - 1), &result)) {
instance->encoder.upload[index++] =
subghz_protocol_encoder_honeywell_add_duration_to_upload(result);
manchester_encoder_advance(
&enc_state, bit_read(instance->generic.data, i - 1), &result);
}
instance->encoder.upload[index++] =
subghz_protocol_encoder_honeywell_add_duration_to_upload(result);
}
instance->encoder.upload[index] = subghz_protocol_encoder_honeywell_add_duration_to_upload(
manchester_encoder_finish(&enc_state));
if(level_duration_get_level(instance->encoder.upload[index])) {
index++;
}
instance->encoder.size_upload = index;
}
SubGhzProtocolStatus
subghz_protocol_encoder_honeywell_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderHoneywell* instance = context;
SubGhzProtocolStatus res = SubGhzProtocolStatusError;
do {
if(SubGhzProtocolStatusOk !=
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);
subghz_protocol_encoder_honeywell_get_upload(instance);
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
instance->encoder.is_running = true;
res = SubGhzProtocolStatusOk;
} while(false);
return res;
}
void subghz_protocol_encoder_honeywell_stop(void* context) {
SubGhzProtocolEncoderHoneywell* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_honeywell_yield(void* context) {
SubGhzProtocolEncoderHoneywell* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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;
}
const SubGhzProtocolDecoder subghz_protocol_honeywell_decoder = {
.alloc = subghz_protocol_decoder_honeywell_alloc,
.free = subghz_protocol_decoder_honeywell_free,
.feed = subghz_protocol_decoder_honeywell_feed,
.reset = subghz_protocol_decoder_honeywell_reset,
.get_hash_data = subghz_protocol_decoder_honeywell_get_hash_data,
.serialize = subghz_protocol_decoder_honeywell_serialize,
.deserialize = subghz_protocol_decoder_honeywell_deserialize,
.get_string = subghz_protocol_decoder_honeywell_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_honeywell_encoder = {
.alloc = subghz_protocol_encoder_honeywell_alloc,
.free = subghz_protocol_encoder_honeywell_free,
.deserialize = subghz_protocol_encoder_honeywell_deserialize,
.stop = subghz_protocol_encoder_honeywell_stop,
.yield = subghz_protocol_encoder_honeywell_yield,
};
const SubGhzProtocol subghz_protocol_honeywell = {
.name = SUBGHZ_PROTOCOL_HONEYWELL_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.encoder = &subghz_protocol_honeywell_encoder,
.decoder = &subghz_protocol_honeywell_decoder,
};
| 13,870
|
C
|
.c
| 325
| 35.815385
| 120
| 0.678791
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,288
|
scher_khan.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/scher_khan.c
|
#include "scher_khan.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
//https://phreakerclub.com/72
//https://phreakerclub.com/forum/showthread.php?t=7&page=2
//https://phreakerclub.com/forum/showthread.php?t=274&highlight=magicar
//!!! https://phreakerclub.com/forum/showthread.php?t=489&highlight=magicar&page=5
#define TAG "SubGhzProtocolScherKhan"
static const SubGhzBlockConst subghz_protocol_scher_khan_const = {
.te_short = 750,
.te_long = 1100,
.te_delta = 150,
.min_count_bit_for_found = 35,
};
struct SubGhzProtocolDecoderScherKhan {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint16_t header_count;
const char* protocol_name;
};
struct SubGhzProtocolEncoderScherKhan {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
ScherKhanDecoderStepReset = 0,
ScherKhanDecoderStepCheckPreambula,
ScherKhanDecoderStepSaveDuration,
ScherKhanDecoderStepCheckDuration,
} ScherKhanDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_scher_khan_decoder = {
.alloc = subghz_protocol_decoder_scher_khan_alloc,
.free = subghz_protocol_decoder_scher_khan_free,
.feed = subghz_protocol_decoder_scher_khan_feed,
.reset = subghz_protocol_decoder_scher_khan_reset,
.get_hash_data = subghz_protocol_decoder_scher_khan_get_hash_data,
.serialize = subghz_protocol_decoder_scher_khan_serialize,
.deserialize = subghz_protocol_decoder_scher_khan_deserialize,
.get_string = subghz_protocol_decoder_scher_khan_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_scher_khan_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol subghz_protocol_scher_khan = {
.name = SUBGHZ_PROTOCOL_SCHER_KHAN_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_AutoAlarms,
.decoder = &subghz_protocol_scher_khan_decoder,
.encoder = &subghz_protocol_scher_khan_encoder,
};
void* subghz_protocol_decoder_scher_khan_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderScherKhan* instance = malloc(sizeof(SubGhzProtocolDecoderScherKhan));
instance->base.protocol = &subghz_protocol_scher_khan;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_scher_khan_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
free(instance);
}
void subghz_protocol_decoder_scher_khan_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
void subghz_protocol_decoder_scher_khan_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
switch(instance->decoder.parser_step) {
case ScherKhanDecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
subghz_protocol_scher_khan_const.te_delta)) {
instance->decoder.parser_step = ScherKhanDecoderStepCheckPreambula;
instance->decoder.te_last = duration;
instance->header_count = 0;
}
break;
case ScherKhanDecoderStepCheckPreambula:
if(level) {
if((DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
subghz_protocol_scher_khan_const.te_delta) ||
(DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
subghz_protocol_scher_khan_const.te_delta)) {
instance->decoder.te_last = duration;
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
} else if(
(DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short * 2) <
subghz_protocol_scher_khan_const.te_delta) ||
(DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
subghz_protocol_scher_khan_const.te_delta)) {
if(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short * 2) <
subghz_protocol_scher_khan_const.te_delta) {
// Found header
instance->header_count++;
break;
} else if(
DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short) <
subghz_protocol_scher_khan_const.te_delta) {
// Found start bit
if(instance->header_count >= 2) {
instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 1;
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
break;
case ScherKhanDecoderStepSaveDuration:
if(level) {
if(duration >= (subghz_protocol_scher_khan_const.te_delta * 2UL +
subghz_protocol_scher_khan_const.te_long)) {
//Found stop bit
instance->decoder.parser_step = ScherKhanDecoderStepReset;
if(instance->decoder.decode_count_bit >=
subghz_protocol_scher_khan_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = ScherKhanDecoderStepCheckDuration;
}
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
break;
case ScherKhanDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_scher_khan_const.te_short) <
subghz_protocol_scher_khan_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_short) <
subghz_protocol_scher_khan_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_scher_khan_const.te_long) <
subghz_protocol_scher_khan_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_scher_khan_const.te_long) <
subghz_protocol_scher_khan_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = ScherKhanDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
} else {
instance->decoder.parser_step = ScherKhanDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
* @param protocol_name
*/
static void subghz_protocol_scher_khan_check_remote_controller(
SubGhzBlockGeneric* instance,
const char** protocol_name) {
/*
* MAGICAR 51 bit 00000001A99121DE83C3 MAGIC CODE, Dynamic
* 0E8C1619E830C -> 000011101000110000010110 0001 1001 1110 1000001100001100
* 0E8C1629D830D -> 000011101000110000010110 0010 1001 1101 1000001100001101
* 0E8C1649B830E -> 000011101000110000010110 0100 1001 1011 1000001100001110
* 0E8C16897830F -> 000011101000110000010110 1000 1001 0111 1000001100001111
* Serial Key Ser ~Key CNT
*/
switch(instance->data_count_bit) {
case 35: //MAGIC CODE, Static
*protocol_name = "MAGIC CODE, Static";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
case 51: //MAGIC CODE, Dynamic
*protocol_name = "MAGIC CODE, Dynamic";
instance->serial = ((instance->data >> 24) & 0xFFFFFF0) | ((instance->data >> 20) & 0x0F);
instance->btn = (instance->data >> 24) & 0x0F;
instance->cnt = instance->data & 0xFFFF;
break;
case 57: //MAGIC CODE PRO / PRO2
*protocol_name = "MAGIC CODE PRO/PRO2";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
case 63: //MAGIC CODE, Dynamic Response
*protocol_name = "MAGIC CODE, Response";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
case 64: //MAGICAR, Response ???
*protocol_name = "MAGICAR, Response";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
case 81: //MAGIC CODE PRO / PRO2 Response ???
*protocol_name = "MAGIC CODE PRO,\n Response";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
case 82: //MAGIC CODE PRO / PRO2 Response ???
*protocol_name = "MAGIC CODE PRO,\n Response";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
default:
*protocol_name = "Unknown";
instance->serial = 0;
instance->btn = 0;
instance->cnt = 0;
break;
}
}
uint8_t subghz_protocol_decoder_scher_khan_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_scher_khan_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_scher_khan_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
return subghz_block_generic_deserialize(&instance->generic, flipper_format);
}
void subghz_protocol_decoder_scher_khan_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderScherKhan* instance = context;
subghz_protocol_scher_khan_check_remote_controller(
&instance->generic, &instance->protocol_name);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:%07lX Btn:%X Cnt:%04lX\r\n"
"Pt: %s\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->generic.serial,
instance->generic.btn,
instance->generic.cnt,
instance->protocol_name);
}
| 12,111
|
C
|
.c
| 286
| 33.741259
| 98
| 0.64671
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,290
|
hormann.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/hormann.c
|
#include "hormann.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolHormannHsm"
#define HORMANN_HSM_PATTERN 0xFF000000003
static const SubGhzBlockConst subghz_protocol_hormann_const = {
.te_short = 500,
.te_long = 1000,
.te_delta = 200,
.min_count_bit_for_found = 44,
};
struct SubGhzProtocolDecoderHormann {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderHormann {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
HormannDecoderStepReset = 0,
HormannDecoderStepFoundStartHeader,
HormannDecoderStepFoundHeader,
HormannDecoderStepFoundStartBit,
HormannDecoderStepSaveDuration,
HormannDecoderStepCheckDuration,
} HormannDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_hormann_decoder = {
.alloc = subghz_protocol_decoder_hormann_alloc,
.free = subghz_protocol_decoder_hormann_free,
.feed = subghz_protocol_decoder_hormann_feed,
.reset = subghz_protocol_decoder_hormann_reset,
.get_hash_data = subghz_protocol_decoder_hormann_get_hash_data,
.serialize = subghz_protocol_decoder_hormann_serialize,
.deserialize = subghz_protocol_decoder_hormann_deserialize,
.get_string = subghz_protocol_decoder_hormann_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_hormann_encoder = {
.alloc = subghz_protocol_encoder_hormann_alloc,
.free = subghz_protocol_encoder_hormann_free,
.deserialize = subghz_protocol_encoder_hormann_deserialize,
.stop = subghz_protocol_encoder_hormann_stop,
.yield = subghz_protocol_encoder_hormann_yield,
};
const SubGhzProtocol subghz_protocol_hormann = {
.name = SUBGHZ_PROTOCOL_HORMANN_HSM_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save |
SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_hormann_decoder,
.encoder = &subghz_protocol_hormann_encoder,
};
void* subghz_protocol_encoder_hormann_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderHormann* instance = malloc(sizeof(SubGhzProtocolEncoderHormann));
instance->base.protocol = &subghz_protocol_hormann;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 2048;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_hormann_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderHormann* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderHormann instance
* @return true On success
*/
static bool subghz_protocol_encoder_hormann_get_upload(SubGhzProtocolEncoderHormann* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2 + 2) * 20 + 1;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
instance->encoder.repeat = 10; //original remote does 10 repeats
for(size_t repeat = 0; repeat < 20; repeat++) {
//Send start bit
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 24);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_long);
}
}
}
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 24);
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderHormann* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_hormann_const.min_count_bit_for_found);
if(ret != SubGhzProtocolStatusOk) {
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_hormann_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_hormann_stop(void* context) {
SubGhzProtocolEncoderHormann* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_hormann_yield(void* context) {
SubGhzProtocolEncoderHormann* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_hormann_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderHormann* instance = malloc(sizeof(SubGhzProtocolDecoderHormann));
instance->base.protocol = &subghz_protocol_hormann;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_hormann_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
free(instance);
}
static bool subghz_protocol_decoder_hormann_check_pattern(SubGhzProtocolDecoderHormann* instance) {
return (instance->decoder.decode_data & HORMANN_HSM_PATTERN) == HORMANN_HSM_PATTERN;
}
void subghz_protocol_decoder_hormann_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
instance->decoder.parser_step = HormannDecoderStepReset;
}
void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
switch(instance->decoder.parser_step) {
case HormannDecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 24) <
subghz_protocol_hormann_const.te_delta * 24)) {
instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
}
break;
case HormannDecoderStepFoundStartBit:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
subghz_protocol_hormann_const.te_delta)) {
instance->decoder.parser_step = HormannDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = HormannDecoderStepReset;
}
break;
case HormannDecoderStepSaveDuration:
if(level) { //save interval
if(duration >= (subghz_protocol_hormann_const.te_short * 5) &&
subghz_protocol_decoder_hormann_check_pattern(instance)) {
instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit >=
subghz_protocol_hormann_const.min_count_bit_for_found) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
break;
}
instance->decoder.te_last = duration;
instance->decoder.parser_step = HormannDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = HormannDecoderStepReset;
}
break;
case HormannDecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_short) <
subghz_protocol_hormann_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_hormann_const.te_long) <
subghz_protocol_hormann_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = HormannDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_hormann_const.te_long) <
subghz_protocol_hormann_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short) <
subghz_protocol_hormann_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = HormannDecoderStepSaveDuration;
} else
instance->decoder.parser_step = HormannDecoderStepReset;
} else {
instance->decoder.parser_step = HormannDecoderStepReset;
}
break;
}
}
/**
* Analysis of received data
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_hormann_check_remote_controller(SubGhzBlockGeneric* instance) {
instance->btn = (instance->data >> 8) & 0xF;
}
uint8_t subghz_protocol_decoder_hormann_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_hormann_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_hormann_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
return subghz_block_generic_deserialize_check_count_bit(
&instance->generic, flipper_format, subghz_protocol_hormann_const.min_count_bit_for_found);
}
void subghz_protocol_decoder_hormann_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
subghz_protocol_hormann_check_remote_controller(&instance->generic);
furi_string_cat_printf(
output,
"%s\r\n"
"%dbit\r\n"
"Key:0x%03lX%08lX\r\n"
"Btn:0x%01X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->generic.btn);
}
| 12,611
|
C
|
.c
| 286
| 36.706294
| 99
| 0.689658
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,292
|
nero_radio.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/nero_radio.c
|
#include "nero_radio.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolNeroRadio"
static const SubGhzBlockConst subghz_protocol_nero_radio_const = {
.te_short = 200,
.te_long = 400,
.te_delta = 80,
.min_count_bit_for_found = 56,
};
struct SubGhzProtocolDecoderNeroRadio {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint16_t header_count;
};
struct SubGhzProtocolEncoderNeroRadio {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
NeroRadioDecoderStepReset = 0,
NeroRadioDecoderStepCheckPreambula,
NeroRadioDecoderStepSaveDuration,
NeroRadioDecoderStepCheckDuration,
} NeroRadioDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_nero_radio_decoder = {
.alloc = subghz_protocol_decoder_nero_radio_alloc,
.free = subghz_protocol_decoder_nero_radio_free,
.feed = subghz_protocol_decoder_nero_radio_feed,
.reset = subghz_protocol_decoder_nero_radio_reset,
.get_hash_data = subghz_protocol_decoder_nero_radio_get_hash_data,
.serialize = subghz_protocol_decoder_nero_radio_serialize,
.deserialize = subghz_protocol_decoder_nero_radio_deserialize,
.get_string = subghz_protocol_decoder_nero_radio_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_nero_radio_encoder = {
.alloc = subghz_protocol_encoder_nero_radio_alloc,
.free = subghz_protocol_encoder_nero_radio_free,
.deserialize = subghz_protocol_encoder_nero_radio_deserialize,
.stop = subghz_protocol_encoder_nero_radio_stop,
.yield = subghz_protocol_encoder_nero_radio_yield,
};
const SubGhzProtocol subghz_protocol_nero_radio = {
.name = SUBGHZ_PROTOCOL_NERO_RADIO_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_nero_radio_decoder,
.encoder = &subghz_protocol_nero_radio_encoder,
};
void* subghz_protocol_encoder_nero_radio_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderNeroRadio* instance = malloc(sizeof(SubGhzProtocolEncoderNeroRadio));
instance->base.protocol = &subghz_protocol_nero_radio;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 256;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_nero_radio_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderNeroRadio* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderNeroRadio instance
* @return true On success
*/
static bool
subghz_protocol_encoder_nero_radio_get_upload(SubGhzProtocolEncoderNeroRadio* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = 49 * 2 + 2 + (instance->generic.data_count_bit * 2);
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
for(uint8_t i = 0; i < 49; i++) {
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nero_radio_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nero_radio_const.te_short);
}
//Send start bit
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)830);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nero_radio_const.te_short);
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 1; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nero_radio_const.te_long);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nero_radio_const.te_short);
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nero_radio_const.te_short);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_nero_radio_const.te_long);
}
}
if(bit_read(instance->generic.data, 0)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nero_radio_const.te_long);
if(instance->generic.data_count_bit == 57) {
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)1300);
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_nero_radio_const.te_short * 23);
}
} else {
//send bit 0
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_nero_radio_const.te_short);
if(instance->generic.data_count_bit == 57) {
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)1300);
} else {
instance->encoder.upload[index++] = level_duration_make(
false, (uint32_t)subghz_protocol_nero_radio_const.te_short * 23);
}
}
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_nero_radio_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderNeroRadio* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_nero_radio_const.min_count_bit_for_found);
if((ret == SubGhzProtocolStatusErrorValueBitCount) &&
(instance->generic.data_count_bit == 57)) {
ret = SubGhzProtocolStatusOk;
} else {
if(ret != SubGhzProtocolStatusOk) {
break;
}
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_nero_radio_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_nero_radio_stop(void* context) {
SubGhzProtocolEncoderNeroRadio* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_nero_radio_yield(void* context) {
SubGhzProtocolEncoderNeroRadio* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_nero_radio_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderNeroRadio* instance = malloc(sizeof(SubGhzProtocolDecoderNeroRadio));
instance->base.protocol = &subghz_protocol_nero_radio;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_nero_radio_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
free(instance);
}
void subghz_protocol_decoder_nero_radio_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
void subghz_protocol_decoder_nero_radio_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
switch(instance->decoder.parser_step) {
case NeroRadioDecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta)) {
instance->decoder.parser_step = NeroRadioDecoderStepCheckPreambula;
instance->decoder.te_last = duration;
instance->header_count = 0;
}
break;
case NeroRadioDecoderStepCheckPreambula:
if(level) {
if((DURATION_DIFF(duration, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta) ||
(DURATION_DIFF(duration, subghz_protocol_nero_radio_const.te_short * 4) <
subghz_protocol_nero_radio_const.te_delta)) {
instance->decoder.te_last = duration;
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
} else if(
DURATION_DIFF(duration, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta) {
if(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta) {
// Found header
instance->header_count++;
break;
} else if(
DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nero_radio_const.te_short * 4) <
subghz_protocol_nero_radio_const.te_delta) {
// Found start bit
if(instance->header_count > 40) {
instance->decoder.parser_step = NeroRadioDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
break;
case NeroRadioDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = NeroRadioDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
break;
case NeroRadioDecoderStepCheckDuration:
if(!level) {
if(duration >= ((uint32_t)1250)) {
//Found stop bit
if(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
} else if(
DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nero_radio_const.te_long) <
subghz_protocol_nero_radio_const.te_delta) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
}
instance->decoder.parser_step = NeroRadioDecoderStepReset;
if((instance->decoder.decode_count_bit ==
subghz_protocol_nero_radio_const.min_count_bit_for_found) ||
(instance->decoder.decode_count_bit ==
subghz_protocol_nero_radio_const.min_count_bit_for_found + 1)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = NeroRadioDecoderStepReset; //-V1048
break;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_nero_radio_const.te_long) <
subghz_protocol_nero_radio_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = NeroRadioDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_nero_radio_const.te_long) <
subghz_protocol_nero_radio_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_nero_radio_const.te_short) <
subghz_protocol_nero_radio_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = NeroRadioDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
} else {
instance->decoder.parser_step = NeroRadioDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_nero_radio_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_nero_radio_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_nero_radio_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
SubGhzProtocolStatus stat;
stat = subghz_block_generic_deserialize_check_count_bit(
&instance->generic,
flipper_format,
subghz_protocol_nero_radio_const.min_count_bit_for_found);
if((stat == SubGhzProtocolStatusErrorValueBitCount) &&
(instance->generic.data_count_bit == 57)) {
return SubGhzProtocolStatusOk;
} else {
return stat;
}
}
void subghz_protocol_decoder_nero_radio_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderNeroRadio* instance = context;
uint32_t code_found_hi = instance->generic.data >> 32;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
uint32_t code_found_reverse_hi = code_found_reverse >> 32;
uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Yek:0x%lX%08lX\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
code_found_hi,
code_found_lo,
code_found_reverse_hi,
code_found_reverse_lo);
}
| 16,288
|
C
|
.c
| 362
| 36.104972
| 100
| 0.655959
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,293
|
protocol_items.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/protocol_items.c
|
#include "protocol_items.h" // IWYU pragma: keep
const SubGhzProtocol* subghz_protocol_registry_items[] = {
&subghz_protocol_gate_tx,
&subghz_protocol_keeloq,
&subghz_protocol_star_line,
&subghz_protocol_nice_flo,
&subghz_protocol_came,
&subghz_protocol_faac_slh,
&subghz_protocol_nice_flor_s,
&subghz_protocol_came_twee,
&subghz_protocol_came_atomo,
&subghz_protocol_nero_sketch,
&subghz_protocol_ido,
&subghz_protocol_kia,
&subghz_protocol_hormann,
&subghz_protocol_nero_radio,
&subghz_protocol_somfy_telis,
&subghz_protocol_somfy_keytis,
&subghz_protocol_scher_khan,
&subghz_protocol_princeton,
&subghz_protocol_raw,
&subghz_protocol_linear,
&subghz_protocol_secplus_v2,
&subghz_protocol_secplus_v1,
&subghz_protocol_megacode,
&subghz_protocol_holtek,
&subghz_protocol_chamb_code,
&subghz_protocol_power_smart,
&subghz_protocol_marantec,
&subghz_protocol_bett,
&subghz_protocol_doitrand,
&subghz_protocol_phoenix_v2,
&subghz_protocol_honeywell_wdb,
&subghz_protocol_magellan,
&subghz_protocol_intertechno_v3,
&subghz_protocol_clemsa,
&subghz_protocol_ansonic,
&subghz_protocol_smc5326,
&subghz_protocol_holtek_th12x,
&subghz_protocol_linear_delta3,
&subghz_protocol_dooya,
&subghz_protocol_alutech_at_4n,
&subghz_protocol_kinggates_stylo_4k,
&subghz_protocol_bin_raw,
&subghz_protocol_mastercode,
&subghz_protocol_honeywell,
&subghz_protocol_legrand,
&subghz_protocol_dickert_mahs,
&subghz_protocol_gangqi,
&subghz_protocol_marantec24,
&subghz_protocol_hollarm,
&subghz_protocol_hay21,
};
const SubGhzProtocolRegistry subghz_protocol_registry = {
.items = subghz_protocol_registry_items,
.size = COUNT_OF(subghz_protocol_registry_items)};
| 1,852
|
C
|
.c
| 56
| 28.321429
| 58
| 0.72631
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
6,294
|
chamberlain_code.c
|
DarkFlippers_unleashed-firmware/lib/subghz/protocols/chamberlain_code.c
|
#include "chamberlain_code.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolChambCode"
#define CHAMBERLAIN_CODE_BIT_STOP 0b0001
#define CHAMBERLAIN_CODE_BIT_1 0b0011
#define CHAMBERLAIN_CODE_BIT_0 0b0111
#define CHAMBERLAIN_7_CODE_MASK 0xF000000FF0F
#define CHAMBERLAIN_8_CODE_MASK 0xF00000F00F
#define CHAMBERLAIN_9_CODE_MASK 0xF000000000F
#define CHAMBERLAIN_7_CODE_MASK_CHECK 0x10000001101
#define CHAMBERLAIN_8_CODE_MASK_CHECK 0x1000001001
#define CHAMBERLAIN_9_CODE_MASK_CHECK 0x10000000001
#define CHAMBERLAIN_7_CODE_DIP_PATTERN "%c%c%c%c%c%c%c"
#define CHAMBERLAIN_7_CODE_DATA_TO_DIP(dip) \
(dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), \
(dip & 0x0008 ? '1' : '0'), (dip & 0x0004 ? '1' : '0'), (dip & 0x0002 ? '1' : '0'), \
(dip & 0x0001 ? '1' : '0')
#define CHAMBERLAIN_8_CODE_DIP_PATTERN "%c%c%c%c%cx%c%c"
#define CHAMBERLAIN_8_CODE_DATA_TO_DIP(dip) \
(dip & 0x0080 ? '1' : '0'), (dip & 0x0040 ? '1' : '0'), (dip & 0x0020 ? '1' : '0'), \
(dip & 0x0010 ? '1' : '0'), (dip & 0x0008 ? '1' : '0'), (dip & 0x0001 ? '1' : '0'), \
(dip & 0x0002 ? '1' : '0')
#define CHAMBERLAIN_9_CODE_DIP_PATTERN "%c%c%c%c%c%c%c%c%c"
#define CHAMBERLAIN_9_CODE_DATA_TO_DIP(dip) \
(dip & 0x0100 ? '1' : '0'), (dip & 0x0080 ? '1' : '0'), (dip & 0x0040 ? '1' : '0'), \
(dip & 0x0020 ? '1' : '0'), (dip & 0x0010 ? '1' : '0'), (dip & 0x0008 ? '1' : '0'), \
(dip & 0x0001 ? '1' : '0'), (dip & 0x0002 ? '1' : '0'), (dip & 0x0004 ? '1' : '0')
static const SubGhzBlockConst subghz_protocol_chamb_code_const = {
.te_short = 1000,
.te_long = 3000,
.te_delta = 200,
.min_count_bit_for_found = 10,
};
struct SubGhzProtocolDecoderChamb_Code {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderChamb_Code {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
Chamb_CodeDecoderStepReset = 0,
Chamb_CodeDecoderStepFoundStartBit,
Chamb_CodeDecoderStepSaveDuration,
Chamb_CodeDecoderStepCheckDuration,
} Chamb_CodeDecoderStep;
const SubGhzProtocolDecoder subghz_protocol_chamb_code_decoder = {
.alloc = subghz_protocol_decoder_chamb_code_alloc,
.free = subghz_protocol_decoder_chamb_code_free,
.feed = subghz_protocol_decoder_chamb_code_feed,
.reset = subghz_protocol_decoder_chamb_code_reset,
.get_hash_data = subghz_protocol_decoder_chamb_code_get_hash_data,
.serialize = subghz_protocol_decoder_chamb_code_serialize,
.deserialize = subghz_protocol_decoder_chamb_code_deserialize,
.get_string = subghz_protocol_decoder_chamb_code_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_chamb_code_encoder = {
.alloc = subghz_protocol_encoder_chamb_code_alloc,
.free = subghz_protocol_encoder_chamb_code_free,
.deserialize = subghz_protocol_encoder_chamb_code_deserialize,
.stop = subghz_protocol_encoder_chamb_code_stop,
.yield = subghz_protocol_encoder_chamb_code_yield,
};
const SubGhzProtocol subghz_protocol_chamb_code = {
.name = SUBGHZ_PROTOCOL_CHAMB_CODE_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_315 | SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable |
SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_chamb_code_decoder,
.encoder = &subghz_protocol_chamb_code_encoder,
};
void* subghz_protocol_encoder_chamb_code_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderChamb_Code* instance = malloc(sizeof(SubGhzProtocolEncoderChamb_Code));
instance->base.protocol = &subghz_protocol_chamb_code;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 24;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_chamb_code_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderChamb_Code* instance = context;
free(instance->encoder.upload);
free(instance);
}
static uint64_t subghz_protocol_chamb_bit_to_code(uint64_t data, uint8_t size) {
uint64_t data_res = 0;
for(uint8_t i = 0; i < size; i++) {
if(!(bit_read(data, size - i - 1))) {
data_res = data_res << 4 | CHAMBERLAIN_CODE_BIT_0;
} else {
data_res = data_res << 4 | CHAMBERLAIN_CODE_BIT_1;
}
}
return data_res;
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderChamb_Code instance
* @return true On success
*/
static bool
subghz_protocol_encoder_chamb_code_get_upload(SubGhzProtocolEncoderChamb_Code* instance) {
furi_assert(instance);
uint64_t data = subghz_protocol_chamb_bit_to_code(
instance->generic.data, instance->generic.data_count_bit);
switch(instance->generic.data_count_bit) {
case 7:
data = ((data >> 4) << 16) | (data & 0xF) << 4 | CHAMBERLAIN_7_CODE_MASK_CHECK;
break;
case 8:
data = ((data >> 12) << 16) | (data & 0xFF) << 4 | CHAMBERLAIN_8_CODE_MASK_CHECK;
break;
case 9:
data = (data << 4) | CHAMBERLAIN_9_CODE_MASK_CHECK;
break;
default:
FURI_LOG_E(TAG, "Invalid bits count");
return false;
break;
}
#define UPLOAD_HEX_DATA_SIZE 10
uint8_t upload_hex_data[UPLOAD_HEX_DATA_SIZE] = {0};
size_t upload_hex_count_bit = 0;
//insert guard time
for(uint8_t i = 0; i < 36; i++) {
subghz_protocol_blocks_set_bit_array(
0, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
}
//insert data
switch(instance->generic.data_count_bit) {
case 7:
case 9:
for(uint8_t i = 44; i > 0; i--) {
if(!bit_read(data, i - 1)) {
subghz_protocol_blocks_set_bit_array(
0, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
} else {
subghz_protocol_blocks_set_bit_array(
1, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
}
}
break;
case 8:
for(uint8_t i = 40; i > 0; i--) {
if(!bit_read(data, i - 1)) {
subghz_protocol_blocks_set_bit_array(
0, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
} else {
subghz_protocol_blocks_set_bit_array(
1, upload_hex_data, upload_hex_count_bit++, UPLOAD_HEX_DATA_SIZE);
}
}
break;
}
instance->encoder.size_upload = subghz_protocol_blocks_get_upload_from_bit_array(
upload_hex_data,
upload_hex_count_bit,
instance->encoder.upload,
instance->encoder.size_upload,
subghz_protocol_chamb_code_const.te_short,
SubGhzProtocolBlockAlignBitLeft);
return true;
}
SubGhzProtocolStatus
subghz_protocol_encoder_chamb_code_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderChamb_Code* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(instance->generic.data_count_bit >
subghz_protocol_chamb_code_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_chamb_code_get_upload(instance)) {
ret = SubGhzProtocolStatusErrorEncoderGetUpload;
break;
}
instance->encoder.is_running = true;
} while(false);
return ret;
}
void subghz_protocol_encoder_chamb_code_stop(void* context) {
SubGhzProtocolEncoderChamb_Code* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_chamb_code_yield(void* context) {
SubGhzProtocolEncoderChamb_Code* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = 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_chamb_code_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderChamb_Code* instance = malloc(sizeof(SubGhzProtocolDecoderChamb_Code));
instance->base.protocol = &subghz_protocol_chamb_code;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_chamb_code_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
free(instance);
}
void subghz_protocol_decoder_chamb_code_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
}
static bool subghz_protocol_chamb_code_to_bit(uint64_t* data, uint8_t size) {
uint64_t data_tmp = data[0];
uint64_t data_res = 0;
for(uint8_t i = 0; i < size; i++) {
if((data_tmp & 0xFll) == CHAMBERLAIN_CODE_BIT_0) {
bit_write(data_res, i, 0);
} else if((data_tmp & 0xFll) == CHAMBERLAIN_CODE_BIT_1) {
bit_write(data_res, i, 1);
} else {
return false;
}
data_tmp >>= 4;
}
data[0] = data_res;
return true;
}
static bool subghz_protocol_decoder_chamb_code_check_mask_and_parse(
SubGhzProtocolDecoderChamb_Code* instance) {
furi_assert(instance);
if(instance->decoder.decode_count_bit >
subghz_protocol_chamb_code_const.min_count_bit_for_found + 1)
return false;
if((instance->decoder.decode_data & CHAMBERLAIN_7_CODE_MASK) ==
CHAMBERLAIN_7_CODE_MASK_CHECK) {
instance->decoder.decode_count_bit = 7;
instance->decoder.decode_data &= ~CHAMBERLAIN_7_CODE_MASK;
instance->decoder.decode_data = (instance->decoder.decode_data >> 12) |
((instance->decoder.decode_data >> 4) & 0xF);
} else if(
(instance->decoder.decode_data & CHAMBERLAIN_8_CODE_MASK) ==
CHAMBERLAIN_8_CODE_MASK_CHECK) {
instance->decoder.decode_count_bit = 8;
instance->decoder.decode_data &= ~CHAMBERLAIN_8_CODE_MASK;
instance->decoder.decode_data = instance->decoder.decode_data >> 4 |
CHAMBERLAIN_CODE_BIT_0 << 8; //DIP 6 no use
} else if(
(instance->decoder.decode_data & CHAMBERLAIN_9_CODE_MASK) ==
CHAMBERLAIN_9_CODE_MASK_CHECK) {
instance->decoder.decode_count_bit = 9;
instance->decoder.decode_data &= ~CHAMBERLAIN_9_CODE_MASK;
instance->decoder.decode_data >>= 4;
} else {
return false;
}
return subghz_protocol_chamb_code_to_bit(
&instance->decoder.decode_data, instance->decoder.decode_count_bit);
}
void subghz_protocol_decoder_chamb_code_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
switch(instance->decoder.parser_step) {
case Chamb_CodeDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 39) <
subghz_protocol_chamb_code_const.te_delta * 20)) {
//Found header Chamb_Code
instance->decoder.parser_step = Chamb_CodeDecoderStepFoundStartBit;
}
break;
case Chamb_CodeDecoderStepFoundStartBit:
if((level) && (DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short) <
subghz_protocol_chamb_code_const.te_delta)) {
//Found start bit Chamb_Code
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.decode_data = instance->decoder.decode_data << 4 |
CHAMBERLAIN_CODE_BIT_STOP;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
}
break;
case Chamb_CodeDecoderStepSaveDuration:
if(!level) { //save interval
if(duration > subghz_protocol_chamb_code_const.te_short * 5) {
if(instance->decoder.decode_count_bit >=
subghz_protocol_chamb_code_const.min_count_bit_for_found) {
instance->generic.serial = 0x0;
instance->generic.btn = 0x0;
if(subghz_protocol_decoder_chamb_code_check_mask_and_parse(instance)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
}
instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = Chamb_CodeDecoderStepCheckDuration;
}
} else {
instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
}
break;
case Chamb_CodeDecoderStepCheckDuration:
if(level) {
if((DURATION_DIFF( //Found stop bit Chamb_Code
instance->decoder.te_last,
subghz_protocol_chamb_code_const.te_short * 3) <
subghz_protocol_chamb_code_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short) <
subghz_protocol_chamb_code_const.te_delta)) {
instance->decoder.decode_data = instance->decoder.decode_data << 4 |
CHAMBERLAIN_CODE_BIT_STOP;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_chamb_code_const.te_short * 2) <
subghz_protocol_chamb_code_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 2) <
subghz_protocol_chamb_code_const.te_delta)) {
instance->decoder.decode_data = instance->decoder.decode_data << 4 |
CHAMBERLAIN_CODE_BIT_1;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, subghz_protocol_chamb_code_const.te_short) <
subghz_protocol_chamb_code_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_chamb_code_const.te_short * 3) <
subghz_protocol_chamb_code_const.te_delta)) {
instance->decoder.decode_data = instance->decoder.decode_data << 4 |
CHAMBERLAIN_CODE_BIT_0;
instance->decoder.decode_count_bit++;
instance->decoder.parser_step = Chamb_CodeDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
}
} else {
instance->decoder.parser_step = Chamb_CodeDecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_chamb_code_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_chamb_code_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_chamb_code_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
SubGhzProtocolStatus ret = SubGhzProtocolStatusError;
do {
ret = subghz_block_generic_deserialize(&instance->generic, flipper_format);
if(ret != SubGhzProtocolStatusOk) {
break;
}
if(instance->generic.data_count_bit >
subghz_protocol_chamb_code_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
ret = SubGhzProtocolStatusErrorValueBitCount;
break;
}
} while(false);
return ret;
}
void subghz_protocol_decoder_chamb_code_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderChamb_Code* instance = context;
uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff;
uint64_t code_found_reverse = subghz_protocol_blocks_reverse_key(
instance->generic.data, instance->generic.data_count_bit);
uint32_t code_found_reverse_lo = code_found_reverse & 0x00000000ffffffff;
furi_string_cat_printf(
output,
"%s %db\r\n"
"Key:0x%03lX\r\n"
"Yek:0x%03lX\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
code_found_lo,
code_found_reverse_lo);
switch(instance->generic.data_count_bit) {
case 7:
furi_string_cat_printf(
output,
"DIP:" CHAMBERLAIN_7_CODE_DIP_PATTERN "\r\n",
CHAMBERLAIN_7_CODE_DATA_TO_DIP(code_found_lo));
break;
case 8:
furi_string_cat_printf(
output,
"DIP:" CHAMBERLAIN_8_CODE_DIP_PATTERN "\r\n",
CHAMBERLAIN_8_CODE_DATA_TO_DIP(code_found_lo));
break;
case 9:
furi_string_cat_printf(
output,
"DIP:" CHAMBERLAIN_9_CODE_DIP_PATTERN "\r\n",
CHAMBERLAIN_9_CODE_DATA_TO_DIP(code_found_lo));
break;
default:
break;
}
}
| 19,575
|
C
|
.c
| 443
| 35.681716
| 98
| 0.631044
|
DarkFlippers/unleashed-firmware
| 16,875
| 1,409
| 86
|
GPL-3.0
|
9/7/2024, 9:40:14 AM (Europe/Amsterdam)
| false
| false
| false
| false
| false
| false
| false
| false
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.