forked from jamisonderek/flipper-zero-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx10.c
288 lines (259 loc) · 10.5 KB
/
x10.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "x10.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
#define TAG "SubGhzProtocolX10"
// @CodeAllNight - X10 Packet decoder...
//
// Do a Sub-GHz read at 310MHz, with 650KHz AM modulation.
//
// Pulses are as follows...
// + 9600 [16*te_short] ~ [te_delta*3] | 9025 [te_delta*7]
// - 4875 [8*te_short] ~ [te_delta*3] | 4488 [te_delta*5]
//
// 32 bits of data (see below)...
// + 600 [te_short] | 550
// - 600 [te_short] (for 0) or 1800 [te_long] (for 1) | 550 (for 0) or 1700 (for 1) [te_delta*2]
//
// + 600 [te_short]
// -43200 [72*te_short] ~ [te_delta*2]
//
// Data simplification of 32 bits can the thought of as:
// first 8 bits are device id (technically first 4 bits sent are channel #).
// second 8 bits are inverted from previous 8 bits.
// next 8 bits are command.
// last 8 bits are inverted from previous 8 bits.
//
// Format: SSSSXBXX ssssxbxx DBOQBXXX dboqbxxx
// S - The serial number (Channel) is encoded in the first four bits that were sent.
// x - Unused bits
// B - Bit 6 is set if the button should be button 9-16, instead of buttons 1-8.
// DQ - The 1st bit of byte 3 is 1 if DIMMER. (bit 4=0 for BRIGHT, bit 4=1 for DIM)
// B - The 2nd bit of byte 3 is the button number.
// Q - 3rd bit of byte 3 are 1 for OFF and 0 for ON (unless DIMMER).
// B - 4th and 5th bit of byte 3 is the rest of the button number.
//
// Actual protocol can be found at http://kbase.x10.com/wiki/CM17A_Protocol
static const SubGhzBlockConst subghz_protocol_x10_const = {
.te_short = 600,
.te_long = 1800,
.te_delta = 100,
.min_count_bit_for_found = 32,
};
struct SubGhzProtocolDecoderX10 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
};
struct SubGhzProtocolEncoderX10 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
};
typedef enum {
X10DecoderStepReset = 0,
X10DecoderStepFoundPreambula,
X10DecoderStepSaveDuration,
X10DecoderStepCheckDuration,
} X10DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_x10_decoder = {
.alloc = subghz_protocol_decoder_x10_alloc,
.free = subghz_protocol_decoder_x10_free,
.feed = subghz_protocol_decoder_x10_feed,
.reset = subghz_protocol_decoder_x10_reset,
.get_hash_data = subghz_protocol_decoder_x10_get_hash_data,
.serialize = subghz_protocol_decoder_x10_serialize,
.deserialize = subghz_protocol_decoder_x10_deserialize,
.get_string = subghz_protocol_decoder_x10_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_x10_encoder = {
.alloc = NULL,
.free = NULL,
.deserialize = NULL,
.stop = NULL,
.yield = NULL,
};
const SubGhzProtocol subghz_protocol_x10 = {
.name = SUBGHZ_PROTOCOL_X10_NAME,
.type = SubGhzProtocolTypeDynamic,
.flag = SubGhzProtocolFlag_315 /* Technically it is 310MHz only */ | SubGhzProtocolFlag_AM |
SubGhzProtocolFlag_Decodable,
.decoder = &subghz_protocol_x10_decoder,
.encoder = &subghz_protocol_x10_encoder,
};
void* subghz_protocol_decoder_x10_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderX10* instance = malloc(sizeof(SubGhzProtocolDecoderX10));
instance->base.protocol = &subghz_protocol_x10;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_x10_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
free(instance);
}
void subghz_protocol_decoder_x10_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->decoder.parser_step = X10DecoderStepReset;
}
bool subghz_protocol_x10_validate(void* context) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
SubGhzBlockDecoder* decoder = &instance->decoder;
uint64_t data = decoder->decode_data;
return decoder->decode_count_bit >= subghz_protocol_x10_const.min_count_bit_for_found &&
((((data >> 24) ^ (data >> 16)) & 0xFF) == 0xFF) &&
((((data >> 8) ^ (data )) & 0xFF) == 0xFF);
}
void subghz_protocol_decoder_x10_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
switch(instance->decoder.parser_step) {
case X10DecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_x10_const.te_short * 16) <
subghz_protocol_x10_const.te_delta * 7)) {
instance->decoder.parser_step = X10DecoderStepFoundPreambula;
}
break;
case X10DecoderStepFoundPreambula:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_x10_const.te_short * 8) <
subghz_protocol_x10_const.te_delta * 5)) {
instance->decoder.parser_step = X10DecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
} else {
subghz_protocol_decoder_x10_reset(context);
}
break;
case X10DecoderStepSaveDuration:
if(level) {
if(DURATION_DIFF(duration, subghz_protocol_x10_const.te_short) <
subghz_protocol_x10_const.te_delta) {
if(instance->decoder.decode_count_bit ==
subghz_protocol_x10_const.min_count_bit_for_found) {
instance->decoder.parser_step = X10DecoderStepReset;
if (subghz_protocol_x10_validate(context)) {
FURI_LOG_E(TAG, "Decoded a signal!");
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);
}
subghz_protocol_decoder_x10_reset(context);
} else {
instance->decoder.te_last = duration;
instance->decoder.parser_step = X10DecoderStepCheckDuration;
}
} else {
subghz_protocol_decoder_x10_reset(context);
}
} else {
subghz_protocol_decoder_x10_reset(context);
}
break;
case X10DecoderStepCheckDuration:
if(!level) {
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_x10_const.te_short) <
subghz_protocol_x10_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_x10_const.te_short) <
subghz_protocol_x10_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = X10DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_x10_const.te_short) <
subghz_protocol_x10_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_x10_const.te_long) <
subghz_protocol_x10_const.te_delta * 2)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = X10DecoderStepSaveDuration;
} else {
subghz_protocol_decoder_x10_reset(context);
}
} else {
subghz_protocol_decoder_x10_reset(context);
}
break;
}
}
/**
* Set the serial and btn values based on the data and data_count_bit.
* @param instance Pointer to a SubGhzBlockGeneric* instance
*/
static void subghz_protocol_x10_check_remote_controller(SubGhzBlockGeneric* instance) {
instance->serial = (instance->data & 0xF0000000) >> (24+4);
instance->btn = (((instance->data & 0x07000000) >> 24) | ((instance->data & 0xF800) >> 8));
}
uint8_t subghz_protocol_decoder_x10_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
SubGhzProtocolStatus subghz_protocol_decoder_x10_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
return subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
}
SubGhzProtocolStatus
subghz_protocol_decoder_x10_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
bool ret = false;
do {
if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
break;
}
if(instance->generic.data_count_bit !=
subghz_protocol_x10_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
ret = true;
} while(false);
return ret;
}
const char* CHANNEL_LETTERS = "MNOPCDABEFGHKLIJ";
void subghz_protocol_decoder_x10_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderX10* instance = context;
subghz_protocol_x10_check_remote_controller(&instance->generic);
char code_channel = CHANNEL_LETTERS[(instance->generic.serial & 0x0F)];
uint32_t code_button = 1 + (
((instance->generic.btn&0x10) >> 4) |
((instance->generic.btn&0x8) >> 2) |
((instance->generic.btn&0x40)>>4) |
((instance->generic.btn&4)<<1));
char* code_action = (instance->generic.btn & 0x20) == 0x20 ? "Off" : "On";
if (instance->generic.btn == 0x98) {
code_button = 0;
code_action = "Dim";
} else if (instance->generic.btn == 0x88) {
code_button = 0;
code_action = "Bright";
}
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Channel:%c \r\n"
"Button:%ld %s\r\n\r\n"
"Key:%lX%08lX\r\n"
"Sn:%07lX Btn:%X\r\n",
instance->generic.protocol_name,
instance->generic.data_count_bit,
code_channel,
code_button,
code_action,
(uint32_t)(instance->generic.data >> 32),
(uint32_t)instance->generic.data,
instance->generic.serial,
instance->generic.btn);
}