-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinfrared_signal.c
300 lines (243 loc) · 9.36 KB
/
infrared_signal.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
289
290
291
292
293
294
295
296
297
298
299
300
#include "infrared_signal.h"
#include <stdlib.h>
#include <string.h>
#include <core/check.h>
#include <lib/infrared/worker/infrared_transmit.h>
#include <lib/infrared/worker/infrared_worker.h>
#define TAG "InfraredSignal"
struct InfraredSignal {
bool is_raw;
union {
InfraredMessage message;
InfraredRawSignal raw;
} payload;
};
static void infrared_signal_clear_timings(InfraredSignal* signal) {
if(signal->is_raw) {
free(signal->payload.raw.timings);
signal->payload.raw.timings_size = 0;
signal->payload.raw.timings = NULL;
}
}
static bool infrared_signal_is_message_valid(InfraredMessage* message) {
if(!infrared_is_protocol_valid(message->protocol)) {
FURI_LOG_E(TAG, "Unknown protocol");
return false;
}
uint32_t address_length = infrared_get_protocol_address_length(message->protocol);
uint32_t address_mask = (1UL << address_length) - 1;
if(message->address != (message->address & address_mask)) {
FURI_LOG_E(
TAG,
"Address is out of range (mask 0x%08lX): 0x%lX\r\n",
address_mask,
message->address);
return false;
}
uint32_t command_length = infrared_get_protocol_command_length(message->protocol);
uint32_t command_mask = (1UL << command_length) - 1;
if(message->command != (message->command & command_mask)) {
FURI_LOG_E(
TAG,
"Command is out of range (mask 0x%08lX): 0x%lX\r\n",
command_mask,
message->command);
return false;
}
return true;
}
static bool infrared_signal_is_raw_valid(InfraredRawSignal* raw) {
if((raw->frequency > INFRARED_MAX_FREQUENCY) || (raw->frequency < INFRARED_MIN_FREQUENCY)) {
FURI_LOG_E(
TAG,
"Frequency is out of range (%X - %X): %lX",
INFRARED_MIN_FREQUENCY,
INFRARED_MAX_FREQUENCY,
raw->frequency);
return false;
} else if((raw->duty_cycle <= 0) || (raw->duty_cycle > 1)) {
FURI_LOG_E(TAG, "Duty cycle is out of range (0 - 1): %f", (double)raw->duty_cycle);
return false;
} else if((raw->timings_size <= 0) || (raw->timings_size > MAX_TIMINGS_AMOUNT)) {
FURI_LOG_E(
TAG,
"Timings amount is out of range (0 - %X): %X",
MAX_TIMINGS_AMOUNT,
raw->timings_size);
return false;
}
return true;
}
static inline bool infrared_signal_save_message(InfraredMessage* message, FlipperFormat* ff) {
const char* protocol_name = infrared_get_protocol_name(message->protocol);
return flipper_format_write_string_cstr(ff, "type", "parsed") &&
flipper_format_write_string_cstr(ff, "protocol", protocol_name) &&
flipper_format_write_hex(ff, "address", (uint8_t*)&message->address, 4) &&
flipper_format_write_hex(ff, "command", (uint8_t*)&message->command, 4);
}
static inline bool infrared_signal_save_raw(InfraredRawSignal* raw, FlipperFormat* ff) {
furi_assert(raw->timings_size <= MAX_TIMINGS_AMOUNT);
return flipper_format_write_string_cstr(ff, "type", "raw") &&
flipper_format_write_uint32(ff, "frequency", &raw->frequency, 1) &&
flipper_format_write_float(ff, "duty_cycle", &raw->duty_cycle, 1) &&
flipper_format_write_uint32(ff, "data", raw->timings, raw->timings_size);
}
static inline bool infrared_signal_read_message(InfraredSignal* signal, FlipperFormat* ff) {
FuriString* buf;
buf = furi_string_alloc();
bool success = false;
do {
if(!flipper_format_read_string(ff, "protocol", buf)) break;
InfraredMessage message;
message.protocol = infrared_get_protocol_by_name(furi_string_get_cstr(buf));
success = flipper_format_read_hex(ff, "address", (uint8_t*)&message.address, 4) &&
flipper_format_read_hex(ff, "command", (uint8_t*)&message.command, 4) &&
infrared_signal_is_message_valid(&message);
if(!success) break;
infrared_signal_set_message(signal, &message);
} while(0);
furi_string_free(buf);
return success;
}
static inline bool infrared_signal_read_raw(InfraredSignal* signal, FlipperFormat* ff) {
uint32_t timings_size, frequency;
float duty_cycle;
bool success = flipper_format_read_uint32(ff, "frequency", &frequency, 1) &&
flipper_format_read_float(ff, "duty_cycle", &duty_cycle, 1) &&
flipper_format_get_value_count(ff, "data", &timings_size);
if(!success || timings_size > MAX_TIMINGS_AMOUNT) {
return false;
}
uint32_t* timings = malloc(sizeof(uint32_t) * timings_size);
success = flipper_format_read_uint32(ff, "data", timings, timings_size);
if(success) {
infrared_signal_set_raw_signal(signal, timings, timings_size, frequency, duty_cycle);
}
free(timings);
return success;
}
static bool infrared_signal_read_body(InfraredSignal* signal, FlipperFormat* ff) {
FuriString* tmp = furi_string_alloc();
bool success = false;
do {
if(!flipper_format_read_string(ff, "type", tmp)) break;
if(furi_string_equal(tmp, "raw")) {
success = infrared_signal_read_raw(signal, ff);
} else if(furi_string_equal(tmp, "parsed")) {
success = infrared_signal_read_message(signal, ff);
} else {
FURI_LOG_E(TAG, "Unknown signal type");
}
} while(false);
furi_string_free(tmp);
return success;
}
InfraredSignal* infrared_signal_alloc() {
InfraredSignal* signal = malloc(sizeof(InfraredSignal));
signal->is_raw = false;
signal->payload.message.protocol = InfraredProtocolUnknown;
return signal;
}
void infrared_signal_free(InfraredSignal* signal) {
infrared_signal_clear_timings(signal);
free(signal);
}
bool infrared_signal_is_raw(InfraredSignal* signal) {
return signal->is_raw;
}
bool infrared_signal_is_valid(InfraredSignal* signal) {
return signal->is_raw ? infrared_signal_is_raw_valid(&signal->payload.raw) :
infrared_signal_is_message_valid(&signal->payload.message);
}
void infrared_signal_set_signal(InfraredSignal* signal, const InfraredSignal* other) {
if(other->is_raw) {
const InfraredRawSignal* raw = &other->payload.raw;
infrared_signal_set_raw_signal(
signal, raw->timings, raw->timings_size, raw->frequency, raw->duty_cycle);
} else {
const InfraredMessage* message = &other->payload.message;
infrared_signal_set_message(signal, message);
}
}
void infrared_signal_set_raw_signal(
InfraredSignal* signal,
const uint32_t* timings,
size_t timings_size,
uint32_t frequency,
float duty_cycle) {
infrared_signal_clear_timings(signal);
signal->is_raw = true;
signal->payload.raw.timings_size = timings_size;
signal->payload.raw.frequency = frequency;
signal->payload.raw.duty_cycle = duty_cycle;
signal->payload.raw.timings = malloc(timings_size * sizeof(uint32_t));
memcpy(signal->payload.raw.timings, timings, timings_size * sizeof(uint32_t));
}
InfraredRawSignal* infrared_signal_get_raw_signal(InfraredSignal* signal) {
furi_assert(signal->is_raw);
return &signal->payload.raw;
}
void infrared_signal_set_message(InfraredSignal* signal, const InfraredMessage* message) {
infrared_signal_clear_timings(signal);
signal->is_raw = false;
signal->payload.message = *message;
}
InfraredMessage* infrared_signal_get_message(InfraredSignal* signal) {
furi_assert(!signal->is_raw);
return &signal->payload.message;
}
bool infrared_signal_save(InfraredSignal* signal, FlipperFormat* ff, const char* name) {
if(!flipper_format_write_comment_cstr(ff, "") ||
!flipper_format_write_string_cstr(ff, "name", name)) {
return false;
} else if(signal->is_raw) {
return infrared_signal_save_raw(&signal->payload.raw, ff);
} else {
return infrared_signal_save_message(&signal->payload.message, ff);
}
}
bool infrared_signal_read(InfraredSignal* signal, FlipperFormat* ff, FuriString* name) {
FuriString* tmp = furi_string_alloc();
bool success = false;
do {
if(!flipper_format_read_string(ff, "name", tmp)) break;
furi_string_set(name, tmp);
if(!infrared_signal_read_body(signal, ff)) break;
success = true;
} while(0);
furi_string_free(tmp);
return success;
}
bool infrared_signal_search_and_read(
InfraredSignal* signal,
FlipperFormat* ff,
const FuriString* name) {
bool success = false;
FuriString* tmp = furi_string_alloc();
do {
bool is_name_found = false;
while(flipper_format_read_string(ff, "name", tmp)) {
is_name_found = furi_string_equal(name, tmp);
if(is_name_found) break;
}
if(!is_name_found) break;
if(!infrared_signal_read_body(signal, ff)) break;
success = true;
} while(false);
furi_string_free(tmp);
return success;
}
void infrared_signal_transmit(InfraredSignal* signal) {
if(signal->is_raw) {
InfraredRawSignal* raw_signal = &signal->payload.raw;
infrared_send_raw_ext(
raw_signal->timings,
raw_signal->timings_size,
true,
raw_signal->frequency,
raw_signal->duty_cycle);
} else {
InfraredMessage* message = &signal->payload.message;
infrared_send(message, 2);
}
}