Skip to content

Commit a9e5e38

Browse files
committed
Add initial working version... derived heavily from https://github.com/NorthernMan54/rtl_433_ESP
0 parents  commit a9e5e38

File tree

258 files changed

+50234
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

258 files changed

+50234
-0
lines changed

.clang-format

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
BasedOnStyle: Google
2+
Language: Cpp
3+
ColumnLimit: 0
4+
IndentWidth: 2
5+
TabWidth: 2
6+
UseTab: Never
7+
IndentPPDirectives: AfterHash
8+
ReflowComments: false
9+
SpacesBeforeTrailingComments: 1
10+
AlignConsecutiveMacros: true
11+
AlignTrailingComments: false
12+
AccessModifierOffset: -2
13+
DerivePointerAlignment: false
14+
PointerAlignment: Left

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = false
8+
insert_final_newline = true

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
compile_commands.json
2+
3+
.pio
4+
.vscode
5+
/rtl_433
6+
7+
tools/copy.list
8+
tools/devices.list
9+
tools/rtl_433_ESP.fragment
10+
example/OOK_Receiver/compile_commands.json

LICENSE.txt

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

include/abuf.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/** @file
2+
array buffer (string builder).
3+
4+
Copyright (C) 2018 Christian Zuckschwerdt
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
*/
11+
12+
#ifndef INCLUDE_ABUF_H_
13+
#define INCLUDE_ABUF_H_
14+
15+
#if defined _MSC_VER || defined ESP32 // Microsoft Visual Studio or ESP32
16+
// MSC and ESP32 have something like C99 restrict as __restrict
17+
#ifndef restrict
18+
#define restrict __restrict
19+
#endif
20+
#endif
21+
// Defined in newer <sal.h> for MSVC.
22+
#ifndef _Printf_format_string_
23+
#define _Printf_format_string_
24+
#endif
25+
26+
#include <stddef.h>
27+
28+
typedef struct abuf {
29+
char *head;
30+
char *tail;
31+
size_t left;
32+
} abuf_t;
33+
34+
void abuf_init(abuf_t *buf, char *dst, size_t len);
35+
36+
void abuf_setnull(abuf_t *buf);
37+
38+
char *abuf_push(abuf_t *buf);
39+
40+
void abuf_pop(abuf_t *buf, char *end);
41+
42+
void abuf_cat(abuf_t *buf, const char *str);
43+
44+
int abuf_printf(abuf_t *buf, _Printf_format_string_ char const *restrict format, ...)
45+
#if defined(__GNUC__) || defined(__clang__)
46+
__attribute__((format(printf, 2, 3)))
47+
#endif
48+
;
49+
50+
#endif /* INCLUDE_ABUF_H_ */

include/bitbuffer.h

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/** @file
2+
A two-dimensional bit buffer consisting of bytes.
3+
4+
Copyright (C) 2015 Tommy Vestermark
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
*/
11+
12+
#ifndef INCLUDE_BITBUFFER_H_
13+
#define INCLUDE_BITBUFFER_H_
14+
15+
#include <stdint.h>
16+
17+
// NOTE: Wireless mbus protocol needs at least ((256+16*2+3)*12)/8 => 437 bytes
18+
// which fits even if RTL_433_REDUCE_STACK_USE is defined because of row spilling
19+
#ifdef RTL_433_REDUCE_STACK_USE
20+
#define BITBUF_COLS 40 // Number of bytes in a column
21+
#define BITBUF_ROWS 25
22+
#else
23+
#define BITBUF_COLS 128 // Number of bytes in a column
24+
#define BITBUF_ROWS 50
25+
#endif
26+
#define BITBUF_MAX_ROW_BITS (BITBUF_ROWS * BITBUF_COLS * 8) // Maximum number of bits per row, max UINT16_MAX
27+
#define BITBUF_MAX_PRINT_BITS 50 // Maximum number of bits to print (in addition to hex values)
28+
29+
typedef uint8_t bitrow_t[BITBUF_COLS];
30+
typedef bitrow_t bitarray_t[BITBUF_ROWS];
31+
32+
/// Bit buffer.
33+
typedef struct bitbuffer {
34+
uint16_t num_rows; ///< Number of active rows
35+
uint16_t free_row; ///< Index of next free row
36+
uint16_t bits_per_row[BITBUF_ROWS]; ///< Number of active bits per row
37+
uint16_t syncs_before_row[BITBUF_ROWS]; ///< Number of sync pulses before row
38+
bitarray_t bb; ///< The actual bits buffer
39+
} bitbuffer_t;
40+
41+
/// Clear the content of the bitbuffer.
42+
void bitbuffer_clear(bitbuffer_t *bits);
43+
44+
/// Add a single bit at the end of the bitbuffer (MSB first).
45+
void bitbuffer_add_bit(bitbuffer_t *bits, int bit);
46+
47+
/// Add a new row to the bitbuffer.
48+
void bitbuffer_add_row(bitbuffer_t *bits);
49+
50+
/// Increment sync counter, add new row if not empty.
51+
void bitbuffer_add_sync(bitbuffer_t *bits);
52+
53+
/// Extract (potentially unaligned) bytes from the bit buffer. Len is bits.
54+
void bitbuffer_extract_bytes(bitbuffer_t *bitbuffer, unsigned row,
55+
unsigned pos, uint8_t *out, unsigned len);
56+
57+
/// Invert all bits in the bitbuffer (do not invert the empty bits).
58+
void bitbuffer_invert(bitbuffer_t *bits);
59+
60+
/// Non-Return-to-Zero Space (NRZI) decode the bitbuffer.
61+
/// "One" is represented by no change in level, "Zero" is represented by change in level.
62+
void bitbuffer_nrzs_decode(bitbuffer_t *bits);
63+
64+
/// Non-Return-to-Zero Mark (NRZI) decode the bitbuffer.
65+
/// "One" is represented by change in level, "Zero" is represented by no change in level.
66+
void bitbuffer_nrzm_decode(bitbuffer_t *bits);
67+
68+
/// Print the content of the bitbuffer.
69+
/// @deprecated For debug only, use decoder_log_bitbuffer otherwise
70+
void bitbuffer_print(const bitbuffer_t *bits);
71+
72+
/// Debug the content of the bitbuffer.
73+
/// @deprecated For debug only, use decoder_log_bitbuffer otherwise
74+
void bitbuffer_debug(const bitbuffer_t *bits);
75+
76+
/// Print the content of a bit row (byte buffer).
77+
/// @deprecated For debug only, use decoder_log_bitrow otherwise
78+
void bitrow_print(uint8_t const *bitrow, unsigned bit_len);
79+
80+
/// Debug the content of a bit row (byte buffer).
81+
/// @deprecated For debug only, use decoder_log_bitrow otherwise
82+
void bitrow_debug(uint8_t const *bitrow, unsigned bit_len);
83+
84+
/// Print the content of a bit row (byte buffer) to a string buffer.
85+
///
86+
/// Write at most @p size - 1 characters,
87+
/// the output is always null-terminated, unless size is 0.
88+
///
89+
/// @param bitrow the row of bytes to print
90+
/// @param bit_len the number of bits in @p bitrow to print
91+
/// @param str an output string buffer of sufficient size
92+
/// @param size the size of @p str
93+
///
94+
/// @return the number of characters printed (not including the trailing `\0`).
95+
int bitrow_snprint(uint8_t const *bitrow, unsigned bit_len, char *str, unsigned size);
96+
97+
/// Parse a string into a bitbuffer.
98+
///
99+
/// The (optionally "0x" prefixed) hex code is processed into a bitbuffer_t.
100+
/// Each row is optionally prefixed with a length enclosed in braces "{}" or
101+
/// separated with a slash "/" character. Whitespace is ignored.
102+
void bitbuffer_parse(bitbuffer_t *bits, const char *code);
103+
104+
/// Search the specified row of the bitbuffer, starting from bit 'start', for
105+
/// the pattern provided. Return the location of the first match, or the end
106+
/// of the row if no match is found.
107+
/// The pattern starts in the high bit. For example if searching for 011011
108+
/// the byte pointed to by 'pattern' would be 0xAC. (011011xx).
109+
unsigned bitbuffer_search(bitbuffer_t *bitbuffer, unsigned row, unsigned start,
110+
const uint8_t *pattern, unsigned pattern_bits_len);
111+
112+
/// Manchester decoding from one bitbuffer into another, starting at the
113+
/// specified row and start bit. Decode at most 'max' data bits (i.e. 2*max)
114+
/// bits from the input buffer). Return the bit position in the input row
115+
/// (i.e. returns start + 2*outbuf->bits_per_row[0]).
116+
/// per IEEE 802.3 conventions, i.e. high-low is a 0 bit, low-high is a 1 bit.
117+
unsigned bitbuffer_manchester_decode(bitbuffer_t *inbuf, unsigned row, unsigned start,
118+
bitbuffer_t *outbuf, unsigned max);
119+
120+
/// Differential Manchester decoding from one bitbuffer into another, starting at the
121+
/// specified row and start bit. Decode at most 'max' data bits (i.e. 2*max)
122+
/// bits from the input buffer). Return the bit position in the input row
123+
/// (i.e. returns start + 2*outbuf->bits_per_row[0]).
124+
unsigned bitbuffer_differential_manchester_decode(bitbuffer_t *inbuf, unsigned row, unsigned start,
125+
bitbuffer_t *outbuf, unsigned max);
126+
127+
/// Compares two given rows of a bitbuffer.
128+
///
129+
/// If @p max_bits is greater than 0 then only up that many bits are compared.
130+
int bitbuffer_compare_rows(bitbuffer_t *bits, unsigned row_a, unsigned row_b, unsigned max_bits);
131+
132+
/// Count the number of repeats of row at index @p row.
133+
///
134+
/// If @p max_bits is greater than 0 then only up that many bits are compared.
135+
/// The returned count will include the given row and will be at least 1.
136+
unsigned bitbuffer_count_repeats(bitbuffer_t *bits, unsigned row, unsigned max_bits);
137+
138+
/// Find a row repeated at least @p min_repeats times and with at least @p min_bits bits length,
139+
/// all bits in the repeats need to match.
140+
/// @return the row index or -1.
141+
int bitbuffer_find_repeated_row(bitbuffer_t *bits, unsigned min_repeats, unsigned min_bits);
142+
143+
/// Find a row repeated at least @p min_repeats times and with at least @p min_bits bits length,
144+
/// a prefix of at most @p min_bits bits will be compared.
145+
/// @return the row index or -1.
146+
int bitbuffer_find_repeated_prefix(bitbuffer_t *bits, unsigned min_repeats, unsigned min_bits);
147+
148+
/// Return a single bit from a bitrow at bit_idx position.
149+
static inline uint8_t bitrow_get_bit(uint8_t const *bitrow, unsigned bit_idx)
150+
{
151+
return bitrow[bit_idx >> 3] >> (7 - (bit_idx & 7)) & 1;
152+
}
153+
154+
/// Return a single byte from a bitrow at bit_idx position (which may be unaligned).
155+
static inline uint8_t bitrow_get_byte(uint8_t const *bitrow, unsigned bit_idx)
156+
{
157+
return (uint8_t)((bitrow[(bit_idx >> 3)] << (bit_idx & 7)) |
158+
(bitrow[(bit_idx >> 3) + 1] >> (8 - (bit_idx & 7))));
159+
}
160+
161+
#endif /* INCLUDE_BITBUFFER_H_ */

include/compat_time.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** @file
2+
compat_time addresses compatibility time functions.
3+
4+
topic: high-resolution timestamps
5+
issue: <sys/time.h> is not available on Windows systems
6+
solution: provide a compatible version for Windows systems
7+
*/
8+
9+
#ifndef INCLUDE_COMPAT_TIME_H_
10+
#define INCLUDE_COMPAT_TIME_H_
11+
12+
// ensure struct timeval is known
13+
#ifdef _WIN32
14+
#include <winsock2.h>
15+
#else
16+
#include <sys/time.h>
17+
#endif
18+
19+
/** Subtract `struct timeval` values.
20+
21+
@param[out] result time difference result
22+
@param x first time value
23+
@param y second time value
24+
@return 1 if the difference is negative, otherwise 0.
25+
*/
26+
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y);
27+
28+
// platform-specific functions
29+
30+
#ifdef _WIN32
31+
int gettimeofday(struct timeval *tv, void *tz);
32+
#endif
33+
34+
#endif /* INCLUDE_COMPAT_TIME_H_ */

0 commit comments

Comments
 (0)