/
funkbus.c
184 lines (153 loc) · 5.75 KB
/
funkbus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @file
Funkbus / Instafunk.
Copyright (C) 2021 Markus Sattler
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
*/
#include "decoder.h"
/** @fn int funkbus_decode(r_device *decoder, bitbuffer_t *bitbuffer)
Funkbus / Instafunk.
Used by Berker, Gira, Jung and may more
developed by Insta GmbH.
- Frequency: 433.42MHz
- Preamble: 4000us
- Short: 500us
- Long: 1000us
- Encoding: Differential Manchester Biphase-Mark (BP-M)
__ __ __ __ __ __
| | | | | | | |
_| |__ __| |__| |__| |__.....
| 0 | 0 | 1 | 0 | 1 |
- Mic: parity + lfsr with 8bit mask 0x8C shifted left by 2 bit
- Bits: 48
- Endian: LSB
Data layout:
TS II II IF FA AX
- T: 4 bit type, there are multiple types
- S: 4 bit subtype
- I: 20 bit serial number
- F: 2 bit r1, unknown
- F: 1 bit bat, 1 == battery low
- F: 2 bit r2, // unknown
- F: 3 bit command, button on the remote
- A: 2 bit group, remote channel group 0-2 (A-C) are switches, 3 == light scene
- A: 1 bit r3, unknown
- A: 2 bit action, STOP, OFF, ON, SCENE
- A: 1 bit repeat, 1 == not first send of packet
- A: 1 bit longpress, longpress of button for (dim up/down, scene learning)
- A: 1 bit parity, parity over all bits before
- X: 4 bit check, LFSR with 8 bit mask 0x8C shifted left by 2 each bit
Some details can be found by searching "instafunk RX/TX-Modul pdf".
*/
static uint32_t get_bits_reflect(uint8_t const *bitrow, unsigned start, unsigned len)
{
unsigned end = start + len - 1;
uint32_t result = 0;
uint32_t mask = 1;
result = 0;
for (; start <= end; mask <<= 1)
if (bitrow_get_bit(bitrow, start++) != 0)
result |= mask;
return result;
}
static uint8_t calc_checksum(uint8_t const *bitrow, unsigned len)
{
const uint8_t full_bytes = len / 8;
const uint8_t bits_left = len % 8;
uint8_t xor_byte = xor_bytes(bitrow, full_bytes);
// Mask out all unused lower bits from the last (partial) byte
uint8_t mask = 0xff << (8 - bits_left);
xor_byte ^= bitrow[full_bytes] & mask;
const uint8_t xor_nibble = ((xor_byte & 0xF0) >> 4) ^ (xor_byte & 0x0F);
uint8_t result = 0;
if (xor_nibble & 0x8) {
result ^= 0x8C;
}
if (xor_nibble & 0x4) {
result ^= 0x32;
}
if (xor_nibble & 0x2) {
result ^= 0xC8;
}
if (xor_nibble & 0x01) {
result ^= 0x23;
}
result = result & 0xF;
result |= (parity8(xor_byte) << 4);
return result;
}
static int funkbus_decode(r_device *decoder, bitbuffer_t *bitbuffer)
{
int events = 0;
for (int row = 0; row < bitbuffer->num_rows; row++) {
if (bitbuffer->bits_per_row[row] < 48) {
return DECODE_ABORT_LENGTH;
}
uint8_t *b = bitbuffer->bb[row];
int typ = get_bits_reflect(b, 0, 4);
int subtyp = get_bits_reflect(b, 4, 4);
// only handle packet typ for remotes
if (typ != 0x4 || subtyp != 0x3) {
return DECODE_ABORT_EARLY;
}
int sn = get_bits_reflect(b, 8, 20);
// int r1 = get_bits_reflect(b, 28, 2); // unknown
int bat = get_bits_reflect(b, 30, 1); // 1 == battery low
// int r2 = get_bits_reflect(b, 31, 2); // unknown
int command = get_bits_reflect(b, 33, 3); // button on the remote
int group = get_bits_reflect(b, 36, 2); // remote channel group 0-2 (A-C) are switches, 3 == light scene
// int r3 = get_bits_reflect(b, 38, 1); // unknown
int action = get_bits_reflect(b, 39, 2); // STOP, OFF, ON, SCENE
int repeat = get_bits_reflect(b, 41, 1); // 1 == not first send of packet
int longpress = get_bits_reflect(b, 42, 1); // longpress of button for (dim up/down, scene learning)
int parity = get_bits_reflect(b, 43, 1); // parity over all bits before
int check = get_bits_reflect(b, 44, 4); // lfsr with 8bit mask 0x8C shifted left by 2 each bit
uint8_t checksum = calc_checksum(b, 43);
if (check != reflect4(checksum & 0xF) ||
parity != (checksum >> 4)) {
return DECODE_FAIL_MIC;
}
/* clang-format off */
data_t *data = data_make(
"model", "", DATA_STRING, "Funkbus-Remote",
"id", "Serial number", DATA_INT, sn,
"battery_ok", "Battery", DATA_INT, bat ? 0 : 1,
"command", "Switch", DATA_INT, command,
"group", "Group", DATA_INT, group,
"action", "Action", DATA_INT, action,
"repeat", "Repeat", DATA_INT, repeat,
"longpress", "Longpress", DATA_INT, longpress,
"mic", "Integrity", DATA_STRING, "CHECKSUM",
NULL);
/* clang-format on */
decoder_output_data(decoder, data);
events++;
}
return events;
}
static char const *const output_fields[] = {
"model",
"id",
"battery_ok",
"command",
"group",
"action",
"repeat",
"longpress",
"mic",
NULL,
};
r_device const funkbus_remote = {
.name = "Funkbus / Instafunk (Berker, Gira, Jung)",
.modulation = OOK_PULSE_DMC,
.short_width = 500,
.long_width = 1000,
.reset_limit = 2000,
.gap_limit = 1500,
.sync_width = 4000,
.tolerance = 300, // us
.decode_fn = &funkbus_decode,
.fields = output_fields,
};