Ramka: odrzucaj nieprawidlowe ID i usun zgadywany lowbat

This commit is contained in:
czcionka committed 2026-10-27 17:49:04 +00:00
1 parent 3dad173002
commit e2ffb8d8cf
2 files changed
+63 -115

No files matched your search

+48 -63
View File
@@ -1,97 +1,82 @@
/*
* tpms_frame.cpp - generator ramek TPMS Schrader EG53MA4 (bez zaleznosci
* od Arduino - kompilowalne takze natywnie w testach).
*
* Zastrzezenie: symulator generuje sztuczne dane cisnienia/temperatury
* i nie zastepuje prawdziwych czujnikow TPMS.
*/
/* Generator ramek EG53MA4. Sztuczne dane wylacznie do diagnostyki. */
#include <string.h>
#include "config.h"
#include "settings.h"
#include "tpms_frame.h"
static void put_bit(uint8_t *buf, unsigned *nbits, unsigned v)
static void put_bit(uint8_t *buf, unsigned *nbits, unsigned value)
{
if (v) {
if (value != 0u) {
buf[*nbits >> 3] |= (uint8_t)(0x80u >> (*nbits & 7u));
}
(*nbits)++;
++(*nbits);
}
/* G.E.T. Manchester: 0 -> "01", 1 -> "10" */
/* Polaryzacja potwierdzona nagraniami i rtl_433 OOK_MC_ZEROBIT: 0=01, 1=10. */
static void put_cell(uint8_t *buf, unsigned *nbits, unsigned bit)
{
if (bit) {
put_bit(buf, nbits, 1);
put_bit(buf, nbits, 0);
}
else {
put_bit(buf, nbits, 0);
put_bit(buf, nbits, 1);
}
put_bit(buf, nbits, bit != 0u ? 1u : 0u);
put_bit(buf, nbits, bit != 0u ? 0u : 1u);
}
uint8_t tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
uint16_t press_kpa10, int8_t temp_c,
uint32_t flags, bool low_batt)
bool tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
uint16_t press_kpa10, int8_t temp_c, uint32_t flags)
{
if (low_batt) {
flags |= TPMS_FLAGS_LOWBAT_MASK;
if (out == NULL || !settings_valid_id(id)) {
return false;
}
/* cisnienie: 25 mbar/bit = 2.5 kPa/bit, zaokraglenie w gore do polowy bitu */
int32_t press = ((int32_t)press_kpa10 + 12) / 25;
if (press < 0) press = 0;
if (press > 255) press = 255;
int32_t pressure = ((int32_t)press_kpa10 + 12) / 25;
if (pressure > 255) {
pressure = 255;
}
/* temperatura: protokol podaje stopnie Fahrenheita */
int32_t num = 9 * (int32_t)temp_c + 160;
int32_t tf = (num >= 0) ? (num + 2) / 5 : -((-num + 2) / 5);
if (tf < 0) tf = 0;
if (tf > 255) tf = 255;
int32_t numerator = 9 * (int32_t)temp_c + 160;
int32_t temperature_f = numerator >= 0
? (numerator + 2) / 5 : -((-numerator + 2) / 5);
if (temperature_f < 0) {
temperature_f = 0;
} else if (temperature_f > 255) {
temperature_f = 255;
}
out[0] = (uint8_t)(flags >> 24);
out[1] = (uint8_t)(flags >> 16);
out[2] = (uint8_t)(flags >> 8);
out[3] = (uint8_t)(flags);
out[3] = (uint8_t)flags;
out[4] = (uint8_t)(id >> 16);
out[5] = (uint8_t)(id >> 8);
out[6] = (uint8_t)(id);
out[7] = (uint8_t)press;
out[8] = (uint8_t)tf;
uint8_t crc = 0;
for (unsigned i = 0; i < 9; i++) {
crc = (uint8_t)(crc + out[i]); // suma mod 256 (add_bytes w rtl_433)
out[6] = (uint8_t)id;
out[7] = (uint8_t)pressure;
out[8] = (uint8_t)temperature_f;
uint8_t checksum = 0;
for (uint8_t i = 0; i < 9u; ++i) {
checksum = (uint8_t)(checksum + out[i]);
}
out[9] = crc;
return crc;
out[9] = checksum;
return true;
}
void tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
uint16_t press_kpa10, int8_t temp_c,
uint32_t flags, bool low_batt)
bool tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
uint16_t press_kpa10, int8_t temp_c, uint32_t flags)
{
uint8_t payload[TPMS_PAYLOAD_LEN];
tpms_build_payload(payload, id, press_kpa10, temp_c, flags, low_batt);
if (out == NULL || !tpms_build_payload(payload, id, press_kpa10, temp_c, flags)) {
return false;
}
memset(out, 0, TPMS_TX_BYTES);
unsigned nbits = 0;
put_bit(out, &nbits, 1); // chip rozbiegowy (carrier on)
/* 39 bitow sync - pierwszy (zerowy) bit dopisuje odbiornik (MC_ZEROBIT) */
for (unsigned i = 0; i < TPMS_SYNC_BITS - 1; i++) {
put_cell(out, &nbits, 0);
put_bit(out, &nbits, 1u); /* chip startowy */
for (unsigned i = 0; i < TPMS_SYNC_BITS - 1u; ++i) {
put_cell(out, &nbits, 0u);
}
/* 80 bitow payloadu */
for (unsigned b = 0; b < 8u * TPMS_PAYLOAD_LEN; b++) {
unsigned bit = (payload[b >> 3] >> (7 - (b & 7u))) & 1u;
put_cell(out, &nbits, bit);
for (unsigned bit = 0; bit < 8u * TPMS_PAYLOAD_LEN; ++bit) {
unsigned value = (payload[bit >> 3] >> (7u - (bit & 7u))) & 1u;
put_cell(out, &nbits, value);
}
/* padding do granicy bajtu - dla OOK sa to chipy "off" = cisza w eterze */
while (nbits % 8u) {
put_bit(out, &nbits, 0);
while ((nbits & 7u) != 0u) {
put_bit(out, &nbits, 0u);
}
return nbits == TPMS_TX_CHIPS;
}
+15 -52
View File
@@ -1,70 +1,33 @@
/*
* tpms_frame.h - generator ramek TPMS Schrader EG53MA4
*
* Zastrzezenie: dane sa sztuczne (diagnostyka/testy), nie zastepuja
* prawdziwych czujnikow TPMS.
*
* Ramka na zrzuconej warstwie bitowej (120 bitow, po dekodowaniu
* Manchester przez rtl_433 OOK_MC_ZEROBIT):
*
* 40 bit sync (zera) | flags(4B) | id(3B) | pressure(1B) | temp(1B) | crc(1B)
*
* - flags: 32 bity, wg dekodera "moze zawierac preamble/status/battery"
* - pressure: 25 mbar/bit (cisnienie w kPa = bajt * 2.5)
* - temperature: stopnie Fahrenheita
* - crc: suma 9 poprzednich bajtow mod 256 (add_bytes w rtl_433)
*
* Kodowanie na zrzuconej warstwie RF (G.E.T. Manchester):
* bit 0 -> chipy "01", bit 1 -> chipy "10", chip = 120 us
* Odbiornik rtl_433 (MC_ZEROBIT) dopisuje pierwszy bit ramki samodzielnie,
* wiec nadajnik wysyla ramke BEZ pierwszego bitu, poprzedzona jednym
* chipem rozbiegowym '1'. Bufor wyjsciowy jest dopełniany zerami do
* granicy bajtu (dla OOK sa to chipy "wylaczone" = cisza).
*/
/* Schrader EG53MA4: 40 bitow sync + 80 bitow danych, OOK Manchester. */
#ifndef TPMS_FRAME_H
#define TPMS_FRAME_H
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define TPMS_ROW_BITS 120 // dlugosc dekodowanej ramki (wymagana przez rtl_433)
#define TPMS_SYNC_BITS 40
#define TPMS_PAYLOAD_LEN 10 // flags(4)+id(3)+press+temp+crc
#define TPMS_TX_BYTES 30 // 240 chipow = 1 lead + 238 danych + 1 padding
#define TPMS_TX_CHIPS (TPMS_TX_BYTES * 8)
#define TPMS_ROW_BITS 120
#define TPMS_SYNC_BITS 40
#define TPMS_PAYLOAD_LEN 10
#define TPMS_TX_BYTES 30
#define TPMS_TX_CHIPS (TPMS_TX_BYTES * 8)
#ifdef __cplusplus
/* Zaleznosc bufora nadawczego od dlugosci ramki: 1 chip rozbiegowy +
* 2 chipy na kazdy z (TPMS_ROW_BITS-1) bitow (pierwszy bit dopisuje
* odbiornik) + padding do granicy bajtu (< 8 chipow). */
static_assert(TPMS_TX_CHIPS >= 1 + 2 * (TPMS_ROW_BITS - 1)
&& TPMS_TX_CHIPS < 1 + 2 * (TPMS_ROW_BITS - 1) + 8,
"TPMS_TX_BYTES musi miescic 1 + 2*(TPMS_ROW_BITS-1) chipow");
static_assert(TPMS_TX_CHIPS == 1 + 2 * (TPMS_ROW_BITS - 1) + 1,
"Nieprawidlowa dlugosc bufora Manchester");
#endif
typedef struct {
uint32_t id; // 24-bit identyfikator czujnika
uint16_t press_kpa10; // cisnienie w kPa * 10 (np. 2600 = 260.0 kPa)
int8_t temp_c; // temperatura w st. C
const char *name; // "FL", "FR", "RR", "RL"
} tpms_sensor_t;
/* Zbuduj 10-bajtowy payload (bez sync). Zwraca crc (tez wpisane do out[9]). */
uint8_t tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
uint16_t press_kpa10, int8_t temp_c,
uint32_t flags, bool low_batt);
/* Zbuduj kompletny bufor nadawczy (TPMS_TX_BYTES) gotowy do CC1101 FIFO. */
void tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
uint16_t press_kpa10, int8_t temp_c,
uint32_t flags, bool low_batt);
/* Zwraca false dla ID 0, 0xFFFFFF i wartosci spoza 24 bitow. */
bool tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
uint16_t press_kpa10, int8_t temp_c, uint32_t flags);
bool tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
uint16_t press_kpa10, int8_t temp_c, uint32_t flags);
#ifdef __cplusplus
}
#endif
#endif /* TPMS_FRAME_H */
#endif