Welcome to mirror list, hosted at ThFree Co, Russian Federation.

irda_common_decoder.c « common « encoder_decoder « irda « lib - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e5979256afe7c4a953efca586b07fc3b91ffaed (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "furi/check.h"
#include "furi/common_defines.h"
#include "irda.h"
#include "irda_common_i.h"
#include <stdbool.h>
#include <furi.h>
#include "irda_i.h"
#include <stdint.h>

static void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder);

static inline size_t consume_samples(uint32_t* array, size_t len, size_t shift) {
    furi_assert(len >= shift);
    len -= shift;
    for(int i = 0; i < len; ++i) array[i] = array[i + shift];

    return len;
}

static inline void accumulate_lsb(IrdaCommonDecoder* decoder, bool bit) {
    uint16_t index = decoder->databit_cnt / 8;
    uint8_t shift = decoder->databit_cnt % 8; // LSB first

    if(!shift) decoder->data[index] = 0;

    if(bit) {
        decoder->data[index] |= (0x1 << shift); // add 1
    } else {
        (void)decoder->data[index]; // add 0
    }

    ++decoder->databit_cnt;
}

static bool irda_check_preamble(IrdaCommonDecoder* decoder) {
    furi_assert(decoder);

    bool result = false;
    bool start_level = (decoder->level + decoder->timings_cnt + 1) % 2;

    if(decoder->timings_cnt == 0) return false;

    // align to start at Mark timing
    if(!start_level) {
        decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
    }

    if(decoder->protocol->timings.preamble_mark == 0) {
        return true;
    }

    while((!result) && (decoder->timings_cnt >= 2)) {
        float preamble_tolerance = decoder->protocol->timings.preamble_tolerance;
        uint16_t preamble_mark = decoder->protocol->timings.preamble_mark;
        uint16_t preamble_space = decoder->protocol->timings.preamble_space;

        if((MATCH_TIMING(decoder->timings[0], preamble_mark, preamble_tolerance)) &&
           (MATCH_TIMING(decoder->timings[1], preamble_space, preamble_tolerance))) {
            result = true;
        }

        decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 2);
    }

    return result;
}

/**
 * decoder->protocol->databit_len[0] contains biggest amount of bits, for this protocol.
 * decoder->protocol->databit_len[1...] contains lesser values, but which can be decoded
 * for some protocol modifications.
 */
static IrdaStatus irda_common_decode_bits(IrdaCommonDecoder* decoder) {
    furi_assert(decoder);

    IrdaStatus status = IrdaStatusOk;
    const IrdaTimings* timings = &decoder->protocol->timings;

    while(decoder->timings_cnt && (status == IrdaStatusOk)) {
        bool level = (decoder->level + decoder->timings_cnt + 1) % 2;
        uint32_t timing = decoder->timings[0];

        if(timings->min_split_time && !level) {
            if(timing > timings->min_split_time) {
                /* long low timing - check if we're ready for any of protocol modification */
                for(int i = 0; decoder->protocol->databit_len[i] &&
                               (i < COUNT_OF(decoder->protocol->databit_len));
                    ++i) {
                    if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
                        return IrdaStatusReady;
                    }
                }
            } else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
                /* short low timing for longest protocol - this is signal is longer than we expected */
                return IrdaStatusError;
            }
        }

        status = decoder->protocol->decode(decoder, level, timing);
        furi_check(decoder->databit_cnt <= decoder->protocol->databit_len[0]);
        furi_assert(status == IrdaStatusError || status == IrdaStatusOk);
        if(status == IrdaStatusError) {
            break;
        }
        decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);

        /* check if largest protocol version can be decoded */
        if(level && (decoder->protocol->databit_len[0] == decoder->databit_cnt) &&
           !timings->min_split_time) {
            status = IrdaStatusReady;
            break;
        }
    }

    return status;
}

/* Pulse Distance-Width Modulation */
IrdaStatus irda_common_decode_pdwm(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
    furi_assert(decoder);

    IrdaStatus status = IrdaStatusOk;
    uint32_t bit_tolerance = decoder->protocol->timings.bit_tolerance;
    uint16_t bit1_mark = decoder->protocol->timings.bit1_mark;
    uint16_t bit1_space = decoder->protocol->timings.bit1_space;
    uint16_t bit0_mark = decoder->protocol->timings.bit0_mark;
    uint16_t bit0_space = decoder->protocol->timings.bit0_space;

    bool analyze_timing = level ^ (bit1_mark == bit0_mark);
    uint16_t bit1 = level ? bit1_mark : bit1_space;
    uint16_t bit0 = level ? bit0_mark : bit0_space;
    uint16_t no_info_timing = (bit1_mark == bit0_mark) ? bit1_mark : bit1_space;

    if(analyze_timing) {
        if(MATCH_TIMING(timing, bit1, bit_tolerance)) {
            accumulate_lsb(decoder, 1);
        } else if(MATCH_TIMING(timing, bit0, bit_tolerance)) {
            accumulate_lsb(decoder, 0);
        } else {
            status = IrdaStatusError;
        }
    } else {
        if(!MATCH_TIMING(timing, no_info_timing, bit_tolerance)) {
            status = IrdaStatusError;
        }
    }

    return status;
}

/* level switch detection goes in middle of time-quant */
IrdaStatus irda_common_decode_manchester(IrdaCommonDecoder* decoder, bool level, uint32_t timing) {
    furi_assert(decoder);
    uint16_t bit = decoder->protocol->timings.bit1_mark;
    uint16_t tolerance = decoder->protocol->timings.bit_tolerance;

    bool* switch_detect = &decoder->switch_detect;
    furi_assert((*switch_detect == true) || (*switch_detect == false));

    bool single_timing = MATCH_TIMING(timing, bit, tolerance);
    bool double_timing = MATCH_TIMING(timing, 2 * bit, tolerance);

    if(!single_timing && !double_timing) {
        return IrdaStatusError;
    }

    if(decoder->protocol->manchester_start_from_space && (decoder->databit_cnt == 0)) {
        *switch_detect = 1; /* fake as we were previously in the middle of time-quant */
        accumulate_lsb(decoder, 0);
    }

    if(*switch_detect == 0) {
        if(double_timing) {
            return IrdaStatusError;
        }
        /* only single timing - level switch required in the middle of time-quant */
        *switch_detect = 1;
    } else {
        /* double timing means we're in the middle of time-quant again */
        if(single_timing) *switch_detect = 0;
    }

    if(*switch_detect) {
        if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
            return IrdaStatusError;
        }
        accumulate_lsb(decoder, level);
    }

    return IrdaStatusOk;
}

IrdaMessage* irda_common_decoder_check_ready(IrdaCommonDecoder* decoder) {
    IrdaMessage* message = NULL;
    bool found_length = false;

    for(int i = 0;
        decoder->protocol->databit_len[i] && (i < COUNT_OF(decoder->protocol->databit_len));
        ++i) {
        if(decoder->protocol->databit_len[i] == decoder->databit_cnt) {
            found_length = true;
            break;
        }
    }

    if(found_length && decoder->protocol->interpret(decoder)) {
        decoder->databit_cnt = 0;
        message = &decoder->message;
        if(decoder->protocol->decode_repeat) {
            decoder->state = IrdaCommonDecoderStateProcessRepeat;
        } else {
            decoder->state = IrdaCommonDecoderStateWaitPreamble;
        }
    }

    return message;
}

IrdaMessage* irda_common_decode(IrdaCommonDecoder* decoder, bool level, uint32_t duration) {
    furi_assert(decoder);

    IrdaMessage* message = 0;
    IrdaStatus status = IrdaStatusError;

    if(decoder->level == level) {
        irda_common_decoder_reset(decoder);
    }
    decoder->level = level; // start with low level (Space timing)

    decoder->timings[decoder->timings_cnt] = duration;
    decoder->timings_cnt++;
    furi_check(decoder->timings_cnt <= sizeof(decoder->timings));

    while(1) {
        switch(decoder->state) {
        case IrdaCommonDecoderStateWaitPreamble:
            if(irda_check_preamble(decoder)) {
                decoder->state = IrdaCommonDecoderStateDecode;
                decoder->databit_cnt = 0;
                decoder->switch_detect = false;
                continue;
            }
            break;
        case IrdaCommonDecoderStateDecode:
            status = irda_common_decode_bits(decoder);
            if(status == IrdaStatusReady) {
                message = irda_common_decoder_check_ready(decoder);
                if(message) {
                    continue;
                } else if(decoder->protocol->databit_len[0] == decoder->databit_cnt) {
                    /* error: can't decode largest protocol - begin decoding from start */
                    decoder->state = IrdaCommonDecoderStateWaitPreamble;
                }
            } else if(status == IrdaStatusError) {
                irda_common_decoder_reset_state(decoder);
                continue;
            }
            break;
        case IrdaCommonDecoderStateProcessRepeat:
            status = decoder->protocol->decode_repeat(decoder);
            if(status == IrdaStatusError) {
                irda_common_decoder_reset_state(decoder);
                continue;
            } else if(status == IrdaStatusReady) {
                decoder->message.repeat = true;
                message = &decoder->message;
            }
            break;
        }
        break;
    }

    return message;
}

void* irda_common_decoder_alloc(const IrdaCommonProtocolSpec* protocol) {
    furi_assert(protocol);

    /* protocol->databit_len[0] has to contain biggest value of bits that can be decoded */
    for(int i = 1; i < COUNT_OF(protocol->databit_len); ++i) {
        furi_assert(protocol->databit_len[i] <= protocol->databit_len[0]);
    }

    uint32_t alloc_size = sizeof(IrdaCommonDecoder) + protocol->databit_len[0] / 8 +
                          !!(protocol->databit_len[0] % 8);
    IrdaCommonDecoder* decoder = malloc(alloc_size);
    decoder->protocol = protocol;
    decoder->level = true;
    return decoder;
}

void irda_common_decoder_free(IrdaCommonDecoder* decoder) {
    furi_assert(decoder);
    free(decoder);
}

void irda_common_decoder_reset_state(IrdaCommonDecoder* decoder) {
    decoder->state = IrdaCommonDecoderStateWaitPreamble;
    decoder->databit_cnt = 0;
    decoder->switch_detect = false;
    decoder->message.protocol = IrdaProtocolUnknown;
    if(decoder->protocol->timings.preamble_mark == 0) {
        if(decoder->timings_cnt > 0) {
            decoder->timings_cnt = consume_samples(decoder->timings, decoder->timings_cnt, 1);
        }
    }
}

void irda_common_decoder_reset(IrdaCommonDecoder* decoder) {
    furi_assert(decoder);

    irda_common_decoder_reset_state(decoder);
    decoder->timings_cnt = 0;
}