[FL-2904, FL-2900, FL-2890] WS: add app WeatherStation (#1833)

* WeatherStation: start
* SubGhz: rename protocol magellen -> magellan
* WeatherStation: err Unresolved symbols: {'subghz_protocol_decoder_base_get_string'}
* WeatherStation: fix Unresolved symbols: {'subghz_protocol_decoder_base_get_string'}
* Subghz: add set protocol_items
* WeatherStation: adding your protocols
* WS: add Infactory protocol
* WS: add history
* WS: add setting
* WS: add lock
* WS: add hopper frequency
* WS: fix history
* WS fix string_t -> FuriString*
* WS: add images
* WS: history record update when receiving data from the sensor again
* WS: add receiver info, delete extra code
* WS: add protocol ThermoPRO_TX4
* [FL-2900] SubGhz: Move icons in Sub-GHz
* WS: add Notification
* [FL-2890] SubGhz: Rename *_user files in resources to _user.example
* WS: add about scene
* WS: removing redundant code
* WS: add  protocol Nexus-TH
* WS: add protocol GT_WT03
* WS: fix notification and rename "Weather Station" -> "Read Weather Station"
* SubGhz: partial unit tests fix
* SubGhz: fix unit_test
* SubGhz: remove dead code
* SubGhz: rename SubGhzPresetDefinition into SubGhzRadioPreset, cleanup subghz types.

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Skorpionm
2022-10-19 21:27:26 +04:00
committed by GitHub
parent 79c3040629
commit 9a9abd59e9
143 changed files with 4677 additions and 458 deletions

View File

@@ -0,0 +1,341 @@
#include "gt_wt_03.h"
#define TAG "WSProtocolGT_WT03"
/*
* Help
* https://github.com/merbanan/rtl_433/blob/5f0ff6db624270a4598958ab9dd79bb385ced3ef/src/devices/gt_wt_03.c
*
*
* Globaltronics GT-WT-03 sensor on 433.92MHz.
* The 01-set sensor has 60 ms packet gap with 10 repeats.
* The 02-set sensor has no packet gap with 23 repeats.
* Example:
* {41} 17 cf be fa 6a 80 [ S1 C1 26,1 C 78.9 F 48% Bat-Good Manual-Yes ]
* {41} 17 cf be fa 6a 80 [ S1 C1 26,1 C 78.9 F 48% Bat-Good Manual-Yes Batt-Changed ]
* {41} 17 cf fe fa ea 80 [ S1 C1 26,1 C 78.9 F 48% Bat-Good Manual-No Batt-Changed ]
* {41} 01 cf 6f 11 b2 80 [ S2 C2 23,8 C 74.8 F 48% Bat-LOW Manual-No ]
* {41} 01 c8 d0 2b 76 80 [ S2 C3 -4,4 C 24.1 F 55% Bat-Good Manual-No Batt-Changed ]
* Format string:
* ID:8h HUM:8d B:b M:b C:2d TEMP:12d CHK:8h 1x
* Data layout:
* TYP IIIIIIII HHHHHHHH BMCCTTTT TTTTTTTT XXXXXXXX
* - I: Random Device Code: changes with battery reset
* - H: Humidity: 8 Bit 00-99, Display LL=10%, Display HH=110% (Range 20-95%)
* - B: Battery: 0=OK 1=LOW
* - M: Manual Send Button Pressed: 0=not pressed, 1=pressed
* - C: Channel: 00=CH1, 01=CH2, 10=CH3
* - T: Temperature: 12 Bit 2's complement, scaled by 10, range-50.0 C (-50.1 shown as Lo) to +70.0 C (+70.1 C is shown as Hi)
* - X: Checksum, xor shifting key per byte
* Humidity:
* - the working range is 20-95 %
* - if "LL" in display view it sends 10 %
* - if "HH" in display view it sends 110%
* Checksum:
* Per byte xor the key for each 1-bit, shift per bit. Key list per bit, starting at MSB:
* - 0x00 [07]
* - 0x80 [06]
* - 0x40 [05]
* - 0x20 [04]
* - 0x10 [03]
* - 0x88 [02]
* - 0xc4 [01]
* - 0x62 [00]
* Note: this can also be seen as lower byte of a Galois/Fibonacci LFSR-16, gen 0x00, init 0x3100 (or 0x62 if reversed) resetting at every byte.
* Battery voltages:
* - U=<2,65V +- ~5% Battery indicator
* - U=>2.10C +- 5% plausible readings
* - U=2,00V +- ~5% Temperature offset -5°C Humidity offset unknown
* - U=<1,95V +- ~5% does not initialize anymore
* - U=1,90V +- 5% temperature offset -15°C
* - U=1,80V +- 5% Display is showing refresh pattern
* - U=1.75V +- ~5% TX causes cut out
*
*/
static const SubGhzBlockConst ws_protocol_gt_wt_03_const = {
.te_short = 285,
.te_long = 570,
.te_delta = 120,
.min_count_bit_for_found = 41,
};
struct WSProtocolDecoderGT_WT03 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
WSBlockGeneric generic;
uint16_t header_count;
};
struct WSProtocolEncoderGT_WT03 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
WSBlockGeneric generic;
};
typedef enum {
GT_WT03DecoderStepReset = 0,
GT_WT03DecoderStepCheckPreambule,
GT_WT03DecoderStepSaveDuration,
GT_WT03DecoderStepCheckDuration,
} GT_WT03DecoderStep;
const SubGhzProtocolDecoder ws_protocol_gt_wt_03_decoder = {
.alloc = ws_protocol_decoder_gt_wt_03_alloc,
.free = ws_protocol_decoder_gt_wt_03_free,
.feed = ws_protocol_decoder_gt_wt_03_feed,
.reset = ws_protocol_decoder_gt_wt_03_reset,
.get_hash_data = ws_protocol_decoder_gt_wt_03_get_hash_data,
.serialize = ws_protocol_decoder_gt_wt_03_serialize,
.deserialize = ws_protocol_decoder_gt_wt_03_deserialize,
.get_string = ws_protocol_decoder_gt_wt_03_get_string,
};
const SubGhzProtocolEncoder ws_protocol_gt_wt_03_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol ws_protocol_gt_wt_03 = {
.name = WS_PROTOCOL_GT_WT_03_NAME,
.type = SubGhzProtocolWeatherStation,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
.decoder = &ws_protocol_gt_wt_03_decoder,
.encoder = &ws_protocol_gt_wt_03_encoder,
};
void* ws_protocol_decoder_gt_wt_03_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
WSProtocolDecoderGT_WT03* instance = malloc(sizeof(WSProtocolDecoderGT_WT03));
instance->base.protocol = &ws_protocol_gt_wt_03;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void ws_protocol_decoder_gt_wt_03_free(void* context) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
free(instance);
}
void ws_protocol_decoder_gt_wt_03_reset(void* context) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
instance->decoder.parser_step = GT_WT03DecoderStepReset;
}
static bool ws_protocol_gt_wt_03_check_crc(WSProtocolDecoderGT_WT03* instance) {
uint8_t msg[] = {
instance->decoder.decode_data >> 33,
instance->decoder.decode_data >> 25,
instance->decoder.decode_data >> 17,
instance->decoder.decode_data >> 9};
uint8_t sum = 0;
for(unsigned k = 0; k < sizeof(msg); ++k) {
uint8_t data = msg[k];
uint16_t key = 0x3100;
for(int i = 7; i >= 0; --i) {
// XOR key into sum if data bit is set
if((data >> i) & 1) sum ^= key & 0xff;
// roll the key right
key = (key >> 1);
}
}
return ((sum ^ (uint8_t)((instance->decoder.decode_data >> 1) & 0xFF)) == 0x2D);
}
/**
* Analysis of received data
* @param instance Pointer to a WSBlockGeneric* instance
*/
static void ws_protocol_gt_wt_03_remote_controller(WSBlockGeneric* instance) {
instance->id = instance->data >> 33;
instance->humidity = (instance->data >> 25) & 0xFF;
if(instance->humidity <= 10) { // actually the sensors sends 10 below working range of 20%
instance->humidity = 0;
} else if(instance->humidity > 95) { // actually the sensors sends 110 above working range of 90%
instance->humidity = 100;
}
instance->battery_low = (instance->data >> 24) & 1;
instance->btn = (instance->data >> 23) & 1;
instance->channel = ((instance->data >> 21) & 0x03) + 1;
if(!((instance->data >> 20) & 1)) {
instance->temp = (float)((instance->data >> 9) & 0x07FF) / 10.0f;
} else {
instance->temp = (float)((~(instance->data >> 9) & 0x07FF) + 1) / -10.0f;
}
}
void ws_protocol_decoder_gt_wt_03_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
switch(instance->decoder.parser_step) {
case GT_WT03DecoderStepReset:
if((level) && (DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_short * 3) <
ws_protocol_gt_wt_03_const.te_delta * 2)) {
instance->decoder.parser_step = GT_WT03DecoderStepCheckPreambule;
instance->decoder.te_last = duration;
instance->header_count = 0;
}
break;
case GT_WT03DecoderStepCheckPreambule:
if(level) {
instance->decoder.te_last = duration;
} else {
if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_03_const.te_short * 3) <
ws_protocol_gt_wt_03_const.te_delta * 2) &&
(DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_short * 3) <
ws_protocol_gt_wt_03_const.te_delta * 2)) {
//Found preambule
instance->header_count++;
} else if(instance->header_count == 4) {
if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_03_const.te_short) <
ws_protocol_gt_wt_03_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_long) <
ws_protocol_gt_wt_03_const.te_delta)) {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = GT_WT03DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_03_const.te_long) <
ws_protocol_gt_wt_03_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_short) <
ws_protocol_gt_wt_03_const.te_delta)) {
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = GT_WT03DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = GT_WT03DecoderStepReset;
}
} else {
instance->decoder.parser_step = GT_WT03DecoderStepReset;
}
}
break;
case GT_WT03DecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = GT_WT03DecoderStepCheckDuration;
} else {
instance->decoder.parser_step = GT_WT03DecoderStepReset;
}
break;
case GT_WT03DecoderStepCheckDuration:
if(!level) {
if(((DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_03_const.te_short * 3) <
ws_protocol_gt_wt_03_const.te_delta * 2) &&
(DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_short * 3) <
ws_protocol_gt_wt_03_const.te_delta * 2))) {
if((instance->decoder.decode_count_bit ==
ws_protocol_gt_wt_03_const.min_count_bit_for_found) &&
ws_protocol_gt_wt_03_check_crc(instance)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
ws_protocol_gt_wt_03_remote_controller(&instance->generic);
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->header_count = 1;
instance->decoder.parser_step = GT_WT03DecoderStepCheckPreambule;
break;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_03_const.te_short) <
ws_protocol_gt_wt_03_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_long) <
ws_protocol_gt_wt_03_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = GT_WT03DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_gt_wt_03_const.te_long) <
ws_protocol_gt_wt_03_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_gt_wt_03_const.te_short) <
ws_protocol_gt_wt_03_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = GT_WT03DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = GT_WT03DecoderStepReset;
}
} else {
instance->decoder.parser_step = GT_WT03DecoderStepReset;
}
break;
}
}
uint8_t ws_protocol_decoder_gt_wt_03_get_hash_data(void* context) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
bool ws_protocol_decoder_gt_wt_03_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
}
bool ws_protocol_decoder_gt_wt_03_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
bool ret = false;
do {
if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
break;
}
if(instance->generic.data_count_bit !=
ws_protocol_gt_wt_03_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
ret = true;
} while(false);
return ret;
}
void ws_protocol_decoder_gt_wt_03_get_string(void* context, FuriString* output) {
furi_assert(context);
WSProtocolDecoderGT_WT03* instance = context;
furi_string_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%lX Ch:%d Bat:%d\r\n"
"Temp:%d.%d C Hum:%d%%",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)(instance->generic.data),
instance->generic.id,
instance->generic.channel,
instance->generic.battery_low,
(int16_t)instance->generic.temp,
abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
instance->generic.humidity);
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include <lib/subghz/protocols/base.h>
#include <lib/subghz/blocks/const.h>
#include <lib/subghz/blocks/decoder.h>
#include <lib/subghz/blocks/encoder.h>
#include "ws_generic.h"
#include <lib/subghz/blocks/math.h>
#define WS_PROTOCOL_GT_WT_03_NAME "GT-WT03"
typedef struct WSProtocolDecoderGT_WT03 WSProtocolDecoderGT_WT03;
typedef struct WSProtocolEncoderGT_WT03 WSProtocolEncoderGT_WT03;
extern const SubGhzProtocolDecoder ws_protocol_gt_wt_03_decoder;
extern const SubGhzProtocolEncoder ws_protocol_gt_wt_03_encoder;
extern const SubGhzProtocol ws_protocol_gt_wt_03;
/**
* Allocate WSProtocolDecoderGT_WT03.
* @param environment Pointer to a SubGhzEnvironment instance
* @return WSProtocolDecoderGT_WT03* pointer to a WSProtocolDecoderGT_WT03 instance
*/
void* ws_protocol_decoder_gt_wt_03_alloc(SubGhzEnvironment* environment);
/**
* Free WSProtocolDecoderGT_WT03.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
*/
void ws_protocol_decoder_gt_wt_03_free(void* context);
/**
* Reset decoder WSProtocolDecoderGT_WT03.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
*/
void ws_protocol_decoder_gt_wt_03_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void ws_protocol_decoder_gt_wt_03_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
* @return hash Hash sum
*/
uint8_t ws_protocol_decoder_gt_wt_03_get_hash_data(void* context);
/**
* Serialize data WSProtocolDecoderGT_WT03.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
bool ws_protocol_decoder_gt_wt_03_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data WSProtocolDecoderGT_WT03.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool ws_protocol_decoder_gt_wt_03_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a WSProtocolDecoderGT_WT03 instance
* @param output Resulting text
*/
void ws_protocol_decoder_gt_wt_03_get_string(void* context, FuriString* output);

View File

@@ -0,0 +1,296 @@
#include "infactory.h"
#define TAG "WSProtocolInfactory"
/*
* Help
* https://github.com/merbanan/rtl_433/blob/master/src/devices/infactory.c
*
* Analysis using Genuino (see http://gitlab.com/hp-uno, e.g. uno_log_433):
* Observed On-Off-Key (OOK) data pattern:
* preamble syncPrefix data...(40 bit) syncPostfix
* HHLL HHLL HHLL HHLL HLLLLLLLLLLLLLLLL (HLLLL HLLLLLLLL HLLLL HLLLLLLLL ....) HLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
* Breakdown:
* - four preamble pairs '1'/'0' each with a length of ca. 1000us
* - syncPre, syncPost, data0, data1 have a '1' start pulse of ca. 500us
* - syncPre pulse before dataPtr has a '0' pulse length of ca. 8000us
* - data0 (0-bits) have then a '0' pulse length of ca. 2000us
* - data1 (1-bits) have then a '0' pulse length of ca. 4000us
* - syncPost after dataPtr has a '0' pulse length of ca. 16000us
* This analysis is the reason for the new r_device definitions below.
* NB: pulse_slicer_ppm does not use .gap_limit if .tolerance is set.
*
* Outdoor sensor, transmits temperature and humidity data
* - inFactory NC-3982-913/NX-5817-902, Pearl (for FWS-686 station)
* - nor-tec 73383 (weather station + sensor), Schou Company AS, Denmark
* - DAY 73365 (weather station + sensor), Schou Company AS, Denmark
* Known brand names: inFactory, nor-tec, GreenBlue, DAY. Manufacturer in China.
* Transmissions includes an id. Every 60 seconds the sensor transmits 6 packets:
* 0000 1111 | 0011 0000 | 0101 1100 | 1110 0111 | 0110 0001
* iiii iiii | cccc ub?? | tttt tttt | tttt hhhh | hhhh ??nn
* - i: identification; changes on battery switch
* - c: CRC-4; CCITT checksum, see below for computation specifics
* - u: unknown; (sometimes set at power-on, but not always)
* - b: battery low; flag to indicate low battery voltage
* - h: Humidity; BCD-encoded, each nibble is one digit, 'A0' means 100%rH
* - t: Temperature; in °F as binary number with one decimal place + 90 °F offset
* - n: Channel; Channel number 1 - 3
*
*/
static const SubGhzBlockConst ws_protocol_infactory_const = {
.te_short = 500,
.te_long = 2000,
.te_delta = 150,
.min_count_bit_for_found = 40,
};
struct WSProtocolDecoderInfactory {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
WSBlockGeneric generic;
uint16_t header_count;
};
struct WSProtocolEncoderInfactory {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
WSBlockGeneric generic;
};
typedef enum {
InfactoryDecoderStepReset = 0,
InfactoryDecoderStepCheckPreambule,
InfactoryDecoderStepSaveDuration,
InfactoryDecoderStepCheckDuration,
} InfactoryDecoderStep;
const SubGhzProtocolDecoder ws_protocol_infactory_decoder = {
.alloc = ws_protocol_decoder_infactory_alloc,
.free = ws_protocol_decoder_infactory_free,
.feed = ws_protocol_decoder_infactory_feed,
.reset = ws_protocol_decoder_infactory_reset,
.get_hash_data = ws_protocol_decoder_infactory_get_hash_data,
.serialize = ws_protocol_decoder_infactory_serialize,
.deserialize = ws_protocol_decoder_infactory_deserialize,
.get_string = ws_protocol_decoder_infactory_get_string,
};
const SubGhzProtocolEncoder ws_protocol_infactory_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol ws_protocol_infactory = {
.name = WS_PROTOCOL_INFACTORY_NAME,
.type = SubGhzProtocolWeatherStation,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
.decoder = &ws_protocol_infactory_decoder,
.encoder = &ws_protocol_infactory_encoder,
};
void* ws_protocol_decoder_infactory_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
WSProtocolDecoderInfactory* instance = malloc(sizeof(WSProtocolDecoderInfactory));
instance->base.protocol = &ws_protocol_infactory;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void ws_protocol_decoder_infactory_free(void* context) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
free(instance);
}
void ws_protocol_decoder_infactory_reset(void* context) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
instance->decoder.parser_step = InfactoryDecoderStepReset;
}
static bool ws_protocol_infactory_check_crc(WSProtocolDecoderInfactory* instance) {
uint8_t msg[] = {
instance->decoder.decode_data >> 32,
(((instance->decoder.decode_data >> 24) & 0x0F) | (instance->decoder.decode_data & 0x0F)
<< 4),
instance->decoder.decode_data >> 16,
instance->decoder.decode_data >> 8,
instance->decoder.decode_data};
uint8_t crc =
subghz_protocol_blocks_crc4(msg, 4, 0x13, 0); // Koopmann 0x9, CCITT-4; FP-4; ITU-T G.704
crc ^= msg[4] >> 4; // last nibble is only XORed
return (crc == ((instance->decoder.decode_data >> 28) & 0x0F));
}
/**
* Analysis of received data
* @param instance Pointer to a WSBlockGeneric* instance
*/
static void ws_protocol_infactory_remote_controller(WSBlockGeneric* instance) {
instance->id = instance->data >> 32;
instance->battery_low = (instance->data >> 26) & 1;
instance->temp = ws_block_generic_fahrenheit_to_celsius(
((float)((instance->data >> 12) & 0x0FFF) - 900.0f) / 10.0f);
instance->humidity =
(((instance->data >> 8) & 0x0F) * 10) + ((instance->data >> 4) & 0x0F); // BCD, 'A0'=100%rH
instance->channel = instance->data & 0x03;
}
void ws_protocol_decoder_infactory_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
switch(instance->decoder.parser_step) {
case InfactoryDecoderStepReset:
if((level) && (DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 2) <
ws_protocol_infactory_const.te_delta * 2)) {
instance->decoder.parser_step = InfactoryDecoderStepCheckPreambule;
instance->decoder.te_last = duration;
instance->header_count = 0;
}
break;
case InfactoryDecoderStepCheckPreambule:
if(level) {
instance->decoder.te_last = duration;
} else {
if((DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short * 2) <
ws_protocol_infactory_const.te_delta * 2) &&
(DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 2) <
ws_protocol_infactory_const.te_delta * 2)) {
//Found preambule
instance->header_count++;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
ws_protocol_infactory_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_infactory_const.te_short * 16) <
ws_protocol_infactory_const.te_delta * 8)) {
//Found syncPrefix
if(instance->header_count > 3) {
instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
} else {
instance->decoder.parser_step = InfactoryDecoderStepReset;
}
}
break;
case InfactoryDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = InfactoryDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = InfactoryDecoderStepReset;
}
break;
case InfactoryDecoderStepCheckDuration:
if(!level) {
if(duration >= ((uint32_t)ws_protocol_infactory_const.te_short * 30)) {
//Found syncPostfix
if((instance->decoder.decode_count_bit ==
ws_protocol_infactory_const.min_count_bit_for_found) &&
ws_protocol_infactory_check_crc(instance)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
ws_protocol_infactory_remote_controller(&instance->generic);
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 = InfactoryDecoderStepReset;
break;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
ws_protocol_infactory_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_infactory_const.te_long) <
ws_protocol_infactory_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_infactory_const.te_short) <
ws_protocol_infactory_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_infactory_const.te_long * 2) <
ws_protocol_infactory_const.te_delta * 4)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = InfactoryDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = InfactoryDecoderStepReset;
}
} else {
instance->decoder.parser_step = InfactoryDecoderStepReset;
}
break;
}
}
uint8_t ws_protocol_decoder_infactory_get_hash_data(void* context) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
bool ws_protocol_decoder_infactory_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
}
bool ws_protocol_decoder_infactory_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
bool ret = false;
do {
if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
break;
}
if(instance->generic.data_count_bit !=
ws_protocol_infactory_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
ret = true;
} while(false);
return ret;
}
void ws_protocol_decoder_infactory_get_string(void* context, FuriString* output) {
furi_assert(context);
WSProtocolDecoderInfactory* instance = context;
furi_string_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%lX Ch:%d Bat:%d\r\n"
"Temp:%d.%d C Hum:%d%%",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)(instance->generic.data),
instance->generic.id,
instance->generic.channel,
instance->generic.battery_low,
(int16_t)instance->generic.temp,
abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
instance->generic.humidity);
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include <lib/subghz/protocols/base.h>
#include <lib/subghz/blocks/const.h>
#include <lib/subghz/blocks/decoder.h>
#include <lib/subghz/blocks/encoder.h>
#include "ws_generic.h"
#include <lib/subghz/blocks/math.h>
#define WS_PROTOCOL_INFACTORY_NAME "inFactory-TH"
typedef struct WSProtocolDecoderInfactory WSProtocolDecoderInfactory;
typedef struct WSProtocolEncoderInfactory WSProtocolEncoderInfactory;
extern const SubGhzProtocolDecoder ws_protocol_infactory_decoder;
extern const SubGhzProtocolEncoder ws_protocol_infactory_encoder;
extern const SubGhzProtocol ws_protocol_infactory;
/**
* Allocate WSProtocolDecoderInfactory.
* @param environment Pointer to a SubGhzEnvironment instance
* @return WSProtocolDecoderInfactory* pointer to a WSProtocolDecoderInfactory instance
*/
void* ws_protocol_decoder_infactory_alloc(SubGhzEnvironment* environment);
/**
* Free WSProtocolDecoderInfactory.
* @param context Pointer to a WSProtocolDecoderInfactory instance
*/
void ws_protocol_decoder_infactory_free(void* context);
/**
* Reset decoder WSProtocolDecoderInfactory.
* @param context Pointer to a WSProtocolDecoderInfactory instance
*/
void ws_protocol_decoder_infactory_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a WSProtocolDecoderInfactory instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void ws_protocol_decoder_infactory_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a WSProtocolDecoderInfactory instance
* @return hash Hash sum
*/
uint8_t ws_protocol_decoder_infactory_get_hash_data(void* context);
/**
* Serialize data WSProtocolDecoderInfactory.
* @param context Pointer to a WSProtocolDecoderInfactory instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
bool ws_protocol_decoder_infactory_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data WSProtocolDecoderInfactory.
* @param context Pointer to a WSProtocolDecoderInfactory instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool ws_protocol_decoder_infactory_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a WSProtocolDecoderInfactory instance
* @param output Resulting text
*/
void ws_protocol_decoder_infactory_get_string(void* context, FuriString* output);

View File

@@ -0,0 +1,261 @@
#include "nexus_th.h"
#define TAG "WSProtocolNexus_TH"
/*
* Help
* https://github.com/merbanan/rtl_433/blob/ef2d37cf51e3264d11cde9149ef87de2f0a4d37a/src/devices/nexus.c
*
* Nexus sensor protocol with ID, temperature and optional humidity
* also FreeTec (Pearl) NC-7345 sensors for FreeTec Weatherstation NC-7344,
* also infactory/FreeTec (Pearl) NX-3980 sensors for infactory/FreeTec NX-3974 station,
* also Solight TE82S sensors for Solight TE76/TE82/TE83/TE84 stations,
* also TFA 30.3209.02 temperature/humidity sensor.
* The sensor sends 36 bits 12 times,
* the packets are ppm modulated (distance coding) with a pulse of ~500 us
* followed by a short gap of ~1000 us for a 0 bit or a long ~2000 us gap for a
* 1 bit, the sync gap is ~4000 us.
* The data is grouped in 9 nibbles:
* [id0] [id1] [flags] [temp0] [temp1] [temp2] [const] [humi0] [humi1]
* - The 8-bit id changes when the battery is changed in the sensor.
* - flags are 4 bits B 0 C C, where B is the battery status: 1=OK, 0=LOW
* - and CC is the channel: 0=CH1, 1=CH2, 2=CH3
* - temp is 12 bit signed scaled by 10
* - const is always 1111 (0x0F)
* - humidity is 8 bits
* The sensors can be bought at Clas Ohlsen (Nexus) and Pearl (infactory/FreeTec).
*
*/
#define NEXUS_TH_CONST_DATA 0b1111
static const SubGhzBlockConst ws_protocol_nexus_th_const = {
.te_short = 500,
.te_long = 2000,
.te_delta = 150,
.min_count_bit_for_found = 36,
};
struct WSProtocolDecoderNexus_TH {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
WSBlockGeneric generic;
};
struct WSProtocolEncoderNexus_TH {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
WSBlockGeneric generic;
};
typedef enum {
Nexus_THDecoderStepReset = 0,
Nexus_THDecoderStepSaveDuration,
Nexus_THDecoderStepCheckDuration,
} Nexus_THDecoderStep;
const SubGhzProtocolDecoder ws_protocol_nexus_th_decoder = {
.alloc = ws_protocol_decoder_nexus_th_alloc,
.free = ws_protocol_decoder_nexus_th_free,
.feed = ws_protocol_decoder_nexus_th_feed,
.reset = ws_protocol_decoder_nexus_th_reset,
.get_hash_data = ws_protocol_decoder_nexus_th_get_hash_data,
.serialize = ws_protocol_decoder_nexus_th_serialize,
.deserialize = ws_protocol_decoder_nexus_th_deserialize,
.get_string = ws_protocol_decoder_nexus_th_get_string,
};
const SubGhzProtocolEncoder ws_protocol_nexus_th_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol ws_protocol_nexus_th = {
.name = WS_PROTOCOL_NEXUS_TH_NAME,
.type = SubGhzProtocolWeatherStation,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
.decoder = &ws_protocol_nexus_th_decoder,
.encoder = &ws_protocol_nexus_th_encoder,
};
void* ws_protocol_decoder_nexus_th_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
WSProtocolDecoderNexus_TH* instance = malloc(sizeof(WSProtocolDecoderNexus_TH));
instance->base.protocol = &ws_protocol_nexus_th;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void ws_protocol_decoder_nexus_th_free(void* context) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
free(instance);
}
void ws_protocol_decoder_nexus_th_reset(void* context) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
instance->decoder.parser_step = Nexus_THDecoderStepReset;
}
static bool ws_protocol_nexus_th_check(WSProtocolDecoderNexus_TH* instance) {
uint8_t type = (instance->decoder.decode_data >> 8) & 0x0F;
if((type == NEXUS_TH_CONST_DATA) && ((instance->decoder.decode_data >> 4) != 0xffffffff)) {
return true;
} else {
return false;
}
return true;
}
/**
* Analysis of received data
* @param instance Pointer to a WSBlockGeneric* instance
*/
static void ws_protocol_nexus_th_remote_controller(WSBlockGeneric* instance) {
instance->id = (instance->data >> 28) & 0xFF;
instance->battery_low = !((instance->data >> 27) & 1);
instance->channel = ((instance->data >> 24) & 0x03) + 1;
if(!((instance->data >> 23) & 1)) {
instance->temp = (float)((instance->data >> 12) & 0x07FF) / 10.0f;
} else {
instance->temp = (float)((~(instance->data >> 12) & 0x07FF) + 1) / -10.0f;
}
instance->humidity = instance->data & 0xFF;
}
void ws_protocol_decoder_nexus_th_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
switch(instance->decoder.parser_step) {
case Nexus_THDecoderStepReset:
if((!level) && (DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
ws_protocol_nexus_th_const.te_delta * 4)) {
//Found sync
instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
break;
case Nexus_THDecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
} else {
instance->decoder.parser_step = Nexus_THDecoderStepReset;
}
break;
case Nexus_THDecoderStepCheckDuration:
if(!level) {
if(DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 8) <
ws_protocol_nexus_th_const.te_delta * 4) {
//Found sync
instance->decoder.parser_step = Nexus_THDecoderStepReset;
if((instance->decoder.decode_count_bit ==
ws_protocol_nexus_th_const.min_count_bit_for_found) &&
ws_protocol_nexus_th_check(instance)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
ws_protocol_nexus_th_remote_controller(&instance->generic);
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.parser_step = Nexus_THDecoderStepCheckDuration;
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
ws_protocol_nexus_th_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 2) <
ws_protocol_nexus_th_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, ws_protocol_nexus_th_const.te_short) <
ws_protocol_nexus_th_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_nexus_th_const.te_short * 4) <
ws_protocol_nexus_th_const.te_delta * 4)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = Nexus_THDecoderStepSaveDuration;
} else {
instance->decoder.parser_step = Nexus_THDecoderStepReset;
}
} else {
instance->decoder.parser_step = Nexus_THDecoderStepReset;
}
break;
}
}
uint8_t ws_protocol_decoder_nexus_th_get_hash_data(void* context) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
bool ws_protocol_decoder_nexus_th_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
}
bool ws_protocol_decoder_nexus_th_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
bool ret = false;
do {
if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
break;
}
if(instance->generic.data_count_bit !=
ws_protocol_nexus_th_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
ret = true;
} while(false);
return ret;
}
void ws_protocol_decoder_nexus_th_get_string(void* context, FuriString* output) {
furi_assert(context);
WSProtocolDecoderNexus_TH* instance = context;
furi_string_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%lX Ch:%d Bat:%d\r\n"
"Temp:%d.%d C Hum:%d%%",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)(instance->generic.data),
instance->generic.id,
instance->generic.channel,
instance->generic.battery_low,
(int16_t)instance->generic.temp,
abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
instance->generic.humidity);
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include <lib/subghz/protocols/base.h>
#include <lib/subghz/blocks/const.h>
#include <lib/subghz/blocks/decoder.h>
#include <lib/subghz/blocks/encoder.h>
#include "ws_generic.h"
#include <lib/subghz/blocks/math.h>
#define WS_PROTOCOL_NEXUS_TH_NAME "Nexus-TH"
typedef struct WSProtocolDecoderNexus_TH WSProtocolDecoderNexus_TH;
typedef struct WSProtocolEncoderNexus_TH WSProtocolEncoderNexus_TH;
extern const SubGhzProtocolDecoder ws_protocol_nexus_th_decoder;
extern const SubGhzProtocolEncoder ws_protocol_nexus_th_encoder;
extern const SubGhzProtocol ws_protocol_nexus_th;
/**
* Allocate WSProtocolDecoderNexus_TH.
* @param environment Pointer to a SubGhzEnvironment instance
* @return WSProtocolDecoderNexus_TH* pointer to a WSProtocolDecoderNexus_TH instance
*/
void* ws_protocol_decoder_nexus_th_alloc(SubGhzEnvironment* environment);
/**
* Free WSProtocolDecoderNexus_TH.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
*/
void ws_protocol_decoder_nexus_th_free(void* context);
/**
* Reset decoder WSProtocolDecoderNexus_TH.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
*/
void ws_protocol_decoder_nexus_th_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void ws_protocol_decoder_nexus_th_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
* @return hash Hash sum
*/
uint8_t ws_protocol_decoder_nexus_th_get_hash_data(void* context);
/**
* Serialize data WSProtocolDecoderNexus_TH.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
bool ws_protocol_decoder_nexus_th_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data WSProtocolDecoderNexus_TH.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool ws_protocol_decoder_nexus_th_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a WSProtocolDecoderNexus_TH instance
* @param output Resulting text
*/
void ws_protocol_decoder_nexus_th_get_string(void* context, FuriString* output);

View File

@@ -0,0 +1,12 @@
#include "protocol_items.h"
const SubGhzProtocol* weather_station_protocol_registry_items[] = {
&ws_protocol_infactory,
&ws_protocol_thermopro_tx4,
&ws_protocol_nexus_th,
&ws_protocol_gt_wt_03,
};
const SubGhzProtocolRegistry weather_station_protocol_registry = {
.items = weather_station_protocol_registry_items,
.size = COUNT_OF(weather_station_protocol_registry_items)};

View File

@@ -0,0 +1,9 @@
#pragma once
#include "../weather_station_app_i.h"
#include "infactory.h"
#include "thermopro_tx4.h"
#include "nexus_th.h"
#include "gt_wt_03.h"
extern const SubGhzProtocolRegistry weather_station_protocol_registry;

View File

@@ -0,0 +1,260 @@
#include "thermopro_tx4.h"
#define TAG "WSProtocolThermoPRO_TX4"
/*
* Help
* https://github.com/merbanan/rtl_433/blob/master/src/devices/thermopro_tx2.c
*
* The sensor sends 37 bits 6 times, before the first packet there is a sync pulse.
* The packets are ppm modulated (distance coding) with a pulse of ~500 us
* followed by a short gap of ~2000 us for a 0 bit or a long ~4000 us gap for a
* 1 bit, the sync gap is ~9000 us.
* The data is grouped in 9 nibbles
* [type] [id0] [id1] [flags] [temp0] [temp1] [temp2] [humi0] [humi1]
* - type: 4 bit fixed 1001 (9) or 0110 (5)
* - id: 8 bit a random id that is generated when the sensor starts, could include battery status
* the same batteries often generate the same id
* - flags(3): is 1 when the battery is low, otherwise 0 (ok)
* - flags(2): is 1 when the sensor sends a reading when pressing the button on the sensor
* - flags(1,0): the channel number that can be set by the sensor (1, 2, 3, X)
* - temp: 12 bit signed scaled by 10
* - humi: 8 bit always 11001100 (0xCC) if no humidity sensor is available
*
*/
#define THERMO_PRO_TX4_TYPE_1 0b1001
#define THERMO_PRO_TX4_TYPE_2 0b0110
static const SubGhzBlockConst ws_protocol_thermopro_tx4_const = {
.te_short = 500,
.te_long = 2000,
.te_delta = 150,
.min_count_bit_for_found = 37,
};
struct WSProtocolDecoderThermoPRO_TX4 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
WSBlockGeneric generic;
};
struct WSProtocolEncoderThermoPRO_TX4 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
WSBlockGeneric generic;
};
typedef enum {
ThermoPRO_TX4DecoderStepReset = 0,
ThermoPRO_TX4DecoderStepSaveDuration,
ThermoPRO_TX4DecoderStepCheckDuration,
} ThermoPRO_TX4DecoderStep;
const SubGhzProtocolDecoder ws_protocol_thermopro_tx4_decoder = {
.alloc = ws_protocol_decoder_thermopro_tx4_alloc,
.free = ws_protocol_decoder_thermopro_tx4_free,
.feed = ws_protocol_decoder_thermopro_tx4_feed,
.reset = ws_protocol_decoder_thermopro_tx4_reset,
.get_hash_data = ws_protocol_decoder_thermopro_tx4_get_hash_data,
.serialize = ws_protocol_decoder_thermopro_tx4_serialize,
.deserialize = ws_protocol_decoder_thermopro_tx4_deserialize,
.get_string = ws_protocol_decoder_thermopro_tx4_get_string,
};
const SubGhzProtocolEncoder ws_protocol_thermopro_tx4_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol ws_protocol_thermopro_tx4 = {
.name = WS_PROTOCOL_THERMOPRO_TX4_NAME,
.type = SubGhzProtocolWeatherStation,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_315 | SubGhzProtocolFlag_868 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable,
.decoder = &ws_protocol_thermopro_tx4_decoder,
.encoder = &ws_protocol_thermopro_tx4_encoder,
};
void* ws_protocol_decoder_thermopro_tx4_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
WSProtocolDecoderThermoPRO_TX4* instance = malloc(sizeof(WSProtocolDecoderThermoPRO_TX4));
instance->base.protocol = &ws_protocol_thermopro_tx4;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void ws_protocol_decoder_thermopro_tx4_free(void* context) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
free(instance);
}
void ws_protocol_decoder_thermopro_tx4_reset(void* context) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
}
static bool ws_protocol_thermopro_tx4_check(WSProtocolDecoderThermoPRO_TX4* instance) {
uint8_t type = instance->decoder.decode_data >> 33;
if((type == THERMO_PRO_TX4_TYPE_1) || (type == THERMO_PRO_TX4_TYPE_2)) {
return true;
} else {
return false;
}
}
/**
* Analysis of received data
* @param instance Pointer to a WSBlockGeneric* instance
*/
static void ws_protocol_thermopro_tx4_remote_controller(WSBlockGeneric* instance) {
instance->id = (instance->data >> 25) & 0xFF;
instance->battery_low = (instance->data >> 24) & 1;
instance->btn = (instance->data >> 23) & 1;
instance->channel = ((instance->data >> 21) & 0x03) + 1;
if(!((instance->data >> 20) & 1)) {
instance->temp = (float)((instance->data >> 9) & 0x07FF) / 10.0f;
} else {
instance->temp = (float)((~(instance->data >> 9) & 0x07FF) + 1) / -10.0f;
}
instance->humidity = (instance->data >> 1) & 0xFF;
}
void ws_protocol_decoder_thermopro_tx4_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
switch(instance->decoder.parser_step) {
case ThermoPRO_TX4DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_short * 18) <
ws_protocol_thermopro_tx4_const.te_delta * 10)) {
//Found sync
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
}
break;
case ThermoPRO_TX4DecoderStepSaveDuration:
if(level) {
instance->decoder.te_last = duration;
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepCheckDuration;
} else {
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
}
break;
case ThermoPRO_TX4DecoderStepCheckDuration:
if(!level) {
if(DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_short * 18) <
ws_protocol_thermopro_tx4_const.te_delta * 10) {
//Found sync
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
if((instance->decoder.decode_count_bit ==
ws_protocol_thermopro_tx4_const.min_count_bit_for_found) &&
ws_protocol_thermopro_tx4_check(instance)) {
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
ws_protocol_thermopro_tx4_remote_controller(&instance->generic);
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepCheckDuration;
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
break;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, ws_protocol_thermopro_tx4_const.te_short) <
ws_protocol_thermopro_tx4_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_long) <
ws_protocol_thermopro_tx4_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(
instance->decoder.te_last, ws_protocol_thermopro_tx4_const.te_short) <
ws_protocol_thermopro_tx4_const.te_delta) &&
(DURATION_DIFF(duration, ws_protocol_thermopro_tx4_const.te_long * 2) <
ws_protocol_thermopro_tx4_const.te_delta * 4)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
}
} else {
instance->decoder.parser_step = ThermoPRO_TX4DecoderStepReset;
}
break;
}
}
uint8_t ws_protocol_decoder_thermopro_tx4_get_hash_data(void* context) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
bool ws_protocol_decoder_thermopro_tx4_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
return ws_block_generic_serialize(&instance->generic, flipper_format, preset);
}
bool ws_protocol_decoder_thermopro_tx4_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
bool ret = false;
do {
if(!ws_block_generic_deserialize(&instance->generic, flipper_format)) {
break;
}
if(instance->generic.data_count_bit !=
ws_protocol_thermopro_tx4_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
ret = true;
} while(false);
return ret;
}
void ws_protocol_decoder_thermopro_tx4_get_string(void* context, FuriString* output) {
furi_assert(context);
WSProtocolDecoderThermoPRO_TX4* instance = context;
furi_string_printf(
output,
"%s %dbit\r\n"
"Key:0x%lX%08lX\r\n"
"Sn:0x%lX Ch:%d Bat:%d\r\n"
"Temp:%d.%d C Hum:%d%%",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)(instance->generic.data),
instance->generic.id,
instance->generic.channel,
instance->generic.battery_low,
(int16_t)instance->generic.temp,
abs(((int16_t)(instance->generic.temp * 10) - (((int16_t)instance->generic.temp) * 10))),
instance->generic.humidity);
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include <lib/subghz/protocols/base.h>
#include <lib/subghz/blocks/const.h>
#include <lib/subghz/blocks/decoder.h>
#include <lib/subghz/blocks/encoder.h>
#include "ws_generic.h"
#include <lib/subghz/blocks/math.h>
#define WS_PROTOCOL_THERMOPRO_TX4_NAME "ThermoPRO-TX4"
typedef struct WSProtocolDecoderThermoPRO_TX4 WSProtocolDecoderThermoPRO_TX4;
typedef struct WSProtocolEncoderThermoPRO_TX4 WSProtocolEncoderThermoPRO_TX4;
extern const SubGhzProtocolDecoder ws_protocol_thermopro_tx4_decoder;
extern const SubGhzProtocolEncoder ws_protocol_thermopro_tx4_encoder;
extern const SubGhzProtocol ws_protocol_thermopro_tx4;
/**
* Allocate WSProtocolDecoderThermoPRO_TX4.
* @param environment Pointer to a SubGhzEnvironment instance
* @return WSProtocolDecoderThermoPRO_TX4* pointer to a WSProtocolDecoderThermoPRO_TX4 instance
*/
void* ws_protocol_decoder_thermopro_tx4_alloc(SubGhzEnvironment* environment);
/**
* Free WSProtocolDecoderThermoPRO_TX4.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
*/
void ws_protocol_decoder_thermopro_tx4_free(void* context);
/**
* Reset decoder WSProtocolDecoderThermoPRO_TX4.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
*/
void ws_protocol_decoder_thermopro_tx4_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void ws_protocol_decoder_thermopro_tx4_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
* @return hash Hash sum
*/
uint8_t ws_protocol_decoder_thermopro_tx4_get_hash_data(void* context);
/**
* Serialize data WSProtocolDecoderThermoPRO_TX4.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
bool ws_protocol_decoder_thermopro_tx4_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data WSProtocolDecoderThermoPRO_TX4.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool ws_protocol_decoder_thermopro_tx4_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a WSProtocolDecoderThermoPRO_TX4 instance
* @param output Resulting text
*/
void ws_protocol_decoder_thermopro_tx4_get_string(void* context, FuriString* output);

View File

@@ -0,0 +1,198 @@
#include "ws_generic.h"
#include <lib/toolbox/stream/stream.h>
#include <lib/flipper_format/flipper_format_i.h>
#include "../helpers/weather_station_types.h"
#define TAG "WSBlockGeneric"
void ws_block_generic_get_preset_name(const char* preset_name, FuriString* preset_str) {
const char* preset_name_temp;
if(!strcmp(preset_name, "AM270")) {
preset_name_temp = "FuriHalSubGhzPresetOok270Async";
} else if(!strcmp(preset_name, "AM650")) {
preset_name_temp = "FuriHalSubGhzPresetOok650Async";
} else if(!strcmp(preset_name, "FM238")) {
preset_name_temp = "FuriHalSubGhzPreset2FSKDev238Async";
} else if(!strcmp(preset_name, "FM476")) {
preset_name_temp = "FuriHalSubGhzPreset2FSKDev476Async";
} else {
preset_name_temp = "FuriHalSubGhzPresetCustom";
}
furi_string_set(preset_str, preset_name_temp);
}
bool ws_block_generic_serialize(
WSBlockGeneric* instance,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(instance);
bool res = false;
FuriString* temp_str;
temp_str = furi_string_alloc();
do {
stream_clean(flipper_format_get_raw_stream(flipper_format));
if(!flipper_format_write_header_cstr(
flipper_format, WS_KEY_FILE_TYPE, WS_KEY_FILE_VERSION)) {
FURI_LOG_E(TAG, "Unable to add header");
break;
}
if(!flipper_format_write_uint32(flipper_format, "Frequency", &preset->frequency, 1)) {
FURI_LOG_E(TAG, "Unable to add Frequency");
break;
}
ws_block_generic_get_preset_name(furi_string_get_cstr(preset->name), temp_str);
if(!flipper_format_write_string_cstr(
flipper_format, "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(
flipper_format, "Custom_preset_module", "CC1101")) {
FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
break;
}
if(!flipper_format_write_hex(
flipper_format, "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(flipper_format, "Protocol", instance->protocol_name)) {
FURI_LOG_E(TAG, "Unable to add Protocol");
break;
}
uint32_t temp_data = instance->id;
if(!flipper_format_write_uint32(flipper_format, "Id", &temp_data, 1)) {
FURI_LOG_E(TAG, "Unable to add Id");
break;
}
temp_data = instance->data_count_bit;
if(!flipper_format_write_uint32(flipper_format, "Bit", &temp_data, 1)) {
FURI_LOG_E(TAG, "Unable to add Bit");
break;
}
uint8_t key_data[sizeof(uint64_t)] = {0};
for(size_t i = 0; i < sizeof(uint64_t); i++) {
key_data[sizeof(uint64_t) - i - 1] = (instance->data >> i * 8) & 0xFF;
}
if(!flipper_format_write_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Unable to add Data");
break;
}
temp_data = instance->battery_low;
if(!flipper_format_write_uint32(flipper_format, "Batt", &temp_data, 1)) {
FURI_LOG_E(TAG, "Unable to add Battery_low");
break;
}
temp_data = instance->humidity;
if(!flipper_format_write_uint32(flipper_format, "Hum", &temp_data, 1)) {
FURI_LOG_E(TAG, "Unable to add Humidity");
break;
}
temp_data = instance->channel;
if(!flipper_format_write_uint32(flipper_format, "Ch", &temp_data, 1)) {
FURI_LOG_E(TAG, "Unable to add Channel");
break;
}
// temp_data = instance->btn;
// if(!flipper_format_write_uint32(flipper_format, "Btn", &temp_data, 1)) {
// FURI_LOG_E(TAG, "Unable to add Btn");
// break;
// }
float temp = instance->temp;
if(!flipper_format_write_float(flipper_format, "Temp", &temp, 1)) {
FURI_LOG_E(TAG, "Unable to add Temperature");
break;
}
res = true;
} while(false);
furi_string_free(temp_str);
return res;
}
bool ws_block_generic_deserialize(WSBlockGeneric* instance, FlipperFormat* flipper_format) {
furi_assert(instance);
bool res = false;
uint32_t temp_data = 0;
do {
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
if(!flipper_format_read_uint32(flipper_format, "Id", (uint32_t*)&temp_data, 1)) {
FURI_LOG_E(TAG, "Missing Id");
break;
}
instance->id = (uint32_t)temp_data;
if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
FURI_LOG_E(TAG, "Missing Bit");
break;
}
instance->data_count_bit = (uint8_t)temp_data;
uint8_t key_data[sizeof(uint64_t)] = {0};
if(!flipper_format_read_hex(flipper_format, "Data", key_data, sizeof(uint64_t))) {
FURI_LOG_E(TAG, "Missing Data");
break;
}
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
instance->data = instance->data << 8 | key_data[i];
}
if(!flipper_format_read_uint32(flipper_format, "Batt", (uint32_t*)&temp_data, 1)) {
FURI_LOG_E(TAG, "Missing Battery_low");
break;
}
instance->battery_low = (uint8_t)temp_data;
if(!flipper_format_read_uint32(flipper_format, "Hum", (uint32_t*)&temp_data, 1)) {
FURI_LOG_E(TAG, "Missing Humidity");
break;
}
instance->humidity = (uint8_t)temp_data;
if(!flipper_format_read_uint32(flipper_format, "Ch", (uint32_t*)&temp_data, 1)) {
FURI_LOG_E(TAG, "Missing Channel");
break;
}
instance->channel = (uint8_t)temp_data;
// if(!flipper_format_read_uint32(flipper_format, "Btn", (uint32_t*)&temp_data, 1)) {
// FURI_LOG_E(TAG, "Missing Btn");
// break;
// }
// instance->btn = (uint8_t)temp_data;
float temp;
if(!flipper_format_read_float(flipper_format, "Temp", (float*)&temp, 1)) {
FURI_LOG_E(TAG, "Missing Temperature");
break;
}
instance->temp = temp;
res = true;
} while(0);
return res;
}
float ws_block_generic_fahrenheit_to_celsius(float fahrenheit) {
return (fahrenheit - 32.0f) / 1.8f;
}

View File

@@ -0,0 +1,61 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <lib/flipper_format/flipper_format.h>
#include "furi.h"
#include "furi_hal.h"
#include <lib/subghz/types.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct WSBlockGeneric WSBlockGeneric;
struct WSBlockGeneric {
const char* protocol_name;
uint64_t data;
uint32_t id;
uint8_t data_count_bit;
uint8_t battery_low;
uint8_t humidity;
uint8_t channel;
uint8_t btn;
float temp;
};
/**
* Get name preset.
* @param preset_name name preset
* @param preset_str Output name preset
*/
void ws_block_generic_get_preset_name(const char* preset_name, FuriString* preset_str);
/**
* Serialize data WSBlockGeneric.
* @param instance Pointer to a WSBlockGeneric instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
bool ws_block_generic_serialize(
WSBlockGeneric* instance,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data WSBlockGeneric.
* @param instance Pointer to a WSBlockGeneric instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool ws_block_generic_deserialize(WSBlockGeneric* instance, FlipperFormat* flipper_format);
float ws_block_generic_fahrenheit_to_celsius(float fahrenheit);
#ifdef __cplusplus
}
#endif