2021-06-09 16:04:49 +03:00
|
|
|
#include "irda.h"
|
2021-07-08 21:20:13 +03:00
|
|
|
#include "furi/check.h"
|
|
|
|
#include "irda_common_i.h"
|
|
|
|
#include "irda_protocol_defs_i.h"
|
2021-05-18 13:51:00 +03:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include "irda_i.h"
|
2021-07-08 21:20:13 +03:00
|
|
|
#include <api-hal-irda.h>
|
2021-05-18 13:51:00 +03:00
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
struct IrdaDecoderHandler {
|
2021-05-18 13:51:00 +03:00
|
|
|
void** ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
IrdaAlloc alloc;
|
|
|
|
IrdaDecode decode;
|
2021-06-02 18:16:05 +03:00
|
|
|
IrdaReset reset;
|
2021-05-18 13:51:00 +03:00
|
|
|
IrdaFree free;
|
|
|
|
} IrdaDecoders;
|
|
|
|
|
|
|
|
typedef struct {
|
2021-07-08 21:20:13 +03:00
|
|
|
IrdaEncoderReset reset;
|
|
|
|
IrdaAlloc alloc;
|
2021-05-18 13:51:00 +03:00
|
|
|
IrdaEncode encode;
|
2021-07-08 21:20:13 +03:00
|
|
|
IrdaFree free;
|
2021-05-18 13:51:00 +03:00
|
|
|
} IrdaEncoders;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
IrdaProtocol protocol;
|
|
|
|
const char* name;
|
|
|
|
IrdaDecoders decoder;
|
|
|
|
IrdaEncoders encoder;
|
2021-06-02 18:16:05 +03:00
|
|
|
uint8_t address_length;
|
|
|
|
uint8_t command_length;
|
2021-05-18 13:51:00 +03:00
|
|
|
} IrdaProtocolImplementation;
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
struct IrdaEncoderHandler {
|
|
|
|
void* encoder;
|
|
|
|
IrdaProtocol protocol;
|
|
|
|
};
|
2021-05-18 13:51:00 +03:00
|
|
|
|
|
|
|
// TODO: replace with key-value, Now we refer by enum index, which is dangerous.
|
|
|
|
static const IrdaProtocolImplementation irda_protocols[] = {
|
|
|
|
// #0
|
|
|
|
{ .protocol = IrdaProtocolNEC,
|
|
|
|
.name = "NEC",
|
|
|
|
.decoder = {
|
|
|
|
.alloc = irda_decoder_nec_alloc,
|
|
|
|
.decode = irda_decoder_nec_decode,
|
2021-06-02 18:16:05 +03:00
|
|
|
.reset = irda_decoder_nec_reset,
|
2021-05-18 13:51:00 +03:00
|
|
|
.free = irda_decoder_nec_free},
|
|
|
|
.encoder = {
|
2021-07-08 21:20:13 +03:00
|
|
|
.alloc = irda_encoder_nec_alloc,
|
|
|
|
.encode = irda_encoder_nec_encode,
|
|
|
|
.reset = irda_encoder_nec_reset,
|
|
|
|
.free = irda_encoder_nec_free},
|
2021-06-02 18:16:05 +03:00
|
|
|
.address_length = 2,
|
|
|
|
.command_length = 2,
|
|
|
|
},
|
2021-07-08 21:20:13 +03:00
|
|
|
// #1 - have to be after NEC
|
2021-06-02 18:16:05 +03:00
|
|
|
{ .protocol = IrdaProtocolNECext,
|
|
|
|
.name = "NECext",
|
|
|
|
.decoder = {
|
|
|
|
.alloc = irda_decoder_necext_alloc,
|
|
|
|
.decode = irda_decoder_nec_decode,
|
|
|
|
.reset = irda_decoder_nec_reset,
|
|
|
|
.free = irda_decoder_nec_free},
|
|
|
|
.encoder = {
|
2021-07-08 21:20:13 +03:00
|
|
|
.alloc = irda_encoder_necext_alloc,
|
|
|
|
.encode = irda_encoder_nec_encode,
|
|
|
|
.reset = irda_encoder_necext_reset,
|
|
|
|
.free = irda_encoder_nec_free},
|
2021-06-02 18:16:05 +03:00
|
|
|
.address_length = 4,
|
|
|
|
.command_length = 2,
|
2021-05-18 13:51:00 +03:00
|
|
|
},
|
2021-07-08 21:20:13 +03:00
|
|
|
// #2
|
|
|
|
{ .protocol = IrdaProtocolSamsung32,
|
|
|
|
.name ="Samsung32",
|
|
|
|
.decoder = {
|
|
|
|
.alloc = irda_decoder_samsung32_alloc,
|
|
|
|
.decode = irda_decoder_samsung32_decode,
|
|
|
|
.reset = irda_decoder_samsung32_reset,
|
|
|
|
.free = irda_decoder_samsung32_free},
|
|
|
|
.encoder = {
|
|
|
|
.alloc = irda_encoder_samsung32_alloc,
|
|
|
|
.encode = irda_encoder_samsung32_encode,
|
|
|
|
.reset = irda_encoder_samsung32_reset,
|
|
|
|
.free = irda_encoder_samsung32_free},
|
|
|
|
.address_length = 2,
|
|
|
|
.command_length = 2,
|
|
|
|
},
|
|
|
|
// #3
|
|
|
|
{ .protocol = IrdaProtocolRC6,
|
|
|
|
.name = "RC6",
|
|
|
|
.decoder = {
|
|
|
|
.alloc = irda_decoder_rc6_alloc,
|
|
|
|
.decode = irda_decoder_rc6_decode,
|
|
|
|
.reset = irda_decoder_rc6_reset,
|
|
|
|
.free = irda_decoder_rc6_free},
|
|
|
|
.encoder = {
|
|
|
|
.alloc = irda_encoder_rc6_alloc,
|
|
|
|
.encode = irda_encoder_rc6_encode,
|
|
|
|
.reset = irda_encoder_rc6_reset,
|
|
|
|
.free = irda_encoder_rc6_free},
|
|
|
|
.address_length = 2,
|
|
|
|
.command_length = 2,
|
|
|
|
},
|
2021-05-18 13:51:00 +03:00
|
|
|
};
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
|
2021-05-18 13:51:00 +03:00
|
|
|
furi_assert(handler);
|
|
|
|
|
|
|
|
IrdaMessage* message = NULL;
|
|
|
|
IrdaMessage* result = NULL;
|
|
|
|
|
|
|
|
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
2021-06-09 16:04:49 +03:00
|
|
|
if (irda_protocols[i].decoder.decode) {
|
|
|
|
message = irda_protocols[i].decoder.decode(handler->ctx[i], level, duration);
|
|
|
|
if (!result && message) {
|
|
|
|
message->protocol = irda_protocols[i].protocol;
|
|
|
|
result = message;
|
|
|
|
}
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
IrdaDecoderHandler* irda_alloc_decoder(void) {
|
|
|
|
IrdaDecoderHandler* handler = furi_alloc(sizeof(IrdaDecoderHandler));
|
2021-05-18 13:51:00 +03:00
|
|
|
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_protocols));
|
|
|
|
|
|
|
|
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
2021-06-09 16:04:49 +03:00
|
|
|
handler->ctx[i] = 0;
|
|
|
|
if (irda_protocols[i].decoder.alloc)
|
|
|
|
handler->ctx[i] = irda_protocols[i].decoder.alloc();
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
void irda_free_decoder(IrdaDecoderHandler* handler) {
|
2021-05-18 13:51:00 +03:00
|
|
|
furi_assert(handler);
|
|
|
|
furi_assert(handler->ctx);
|
|
|
|
|
|
|
|
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
2021-06-09 16:04:49 +03:00
|
|
|
if (irda_protocols[i].decoder.free)
|
|
|
|
irda_protocols[i].decoder.free(handler->ctx[i]);
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
free(handler->ctx);
|
|
|
|
free(handler);
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
void irda_reset_decoder(IrdaDecoderHandler* handler) {
|
2021-06-02 18:16:05 +03:00
|
|
|
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
2021-06-09 16:04:49 +03:00
|
|
|
if (irda_protocols[i].decoder.reset)
|
|
|
|
irda_protocols[i].decoder.reset(handler->ctx[i]);
|
2021-06-02 18:16:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
IrdaEncoderHandler* irda_alloc_encoder(void) {
|
|
|
|
IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
|
|
|
|
handler->encoder = NULL;
|
|
|
|
handler->protocol = IrdaProtocolUnknown;
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irda_free_encoder(IrdaEncoderHandler* handler) {
|
|
|
|
furi_assert(handler);
|
|
|
|
|
|
|
|
if (handler->encoder) {
|
|
|
|
furi_assert(irda_is_protocol_valid(handler->protocol));
|
|
|
|
furi_assert(irda_protocols[handler->protocol].encoder.free);
|
|
|
|
irda_protocols[handler->protocol].encoder.free(handler->encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message) {
|
|
|
|
furi_assert(handler);
|
2021-05-18 13:51:00 +03:00
|
|
|
furi_assert(message);
|
2021-06-09 16:04:49 +03:00
|
|
|
furi_assert(irda_is_protocol_valid(message->protocol));
|
2021-07-08 21:20:13 +03:00
|
|
|
furi_assert(irda_protocols[message->protocol].encoder.reset);
|
|
|
|
furi_assert(irda_protocols[message->protocol].encoder.alloc);
|
|
|
|
|
|
|
|
/* Realloc encoder if different protocol set */
|
|
|
|
if (message->protocol != handler->protocol) {
|
|
|
|
if (handler->encoder != NULL) {
|
|
|
|
furi_assert(handler->protocol != IrdaProtocolUnknown);
|
|
|
|
irda_protocols[handler->protocol].encoder.free(handler->encoder);
|
2021-06-09 16:04:49 +03:00
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
handler->encoder = irda_protocols[message->protocol].encoder.alloc();
|
|
|
|
handler->protocol = message->protocol;
|
2021-06-09 16:04:49 +03:00
|
|
|
}
|
2021-07-08 21:20:13 +03:00
|
|
|
|
|
|
|
irda_protocols[handler->protocol].encoder.reset(handler->encoder, message);
|
2021-06-09 16:04:49 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 21:20:13 +03:00
|
|
|
|
|
|
|
IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level) {
|
|
|
|
furi_assert(handler);
|
|
|
|
furi_assert(irda_is_protocol_valid(handler->protocol));
|
|
|
|
furi_assert(irda_protocols[handler->protocol].encoder.encode);
|
|
|
|
|
|
|
|
IrdaStatus status = irda_protocols[handler->protocol].encoder.encode(handler->encoder, duration, level);
|
|
|
|
furi_assert(status != IrdaStatusError);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-09 16:04:49 +03:00
|
|
|
bool irda_is_protocol_valid(IrdaProtocol protocol) {
|
|
|
|
return (protocol >= 0) && (protocol < COUNT_OF(irda_protocols));
|
|
|
|
}
|
|
|
|
|
|
|
|
IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) {
|
|
|
|
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
|
|
|
if (!strcmp(irda_protocols[i].name, protocol_name))
|
|
|
|
return i;
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
2021-06-09 16:04:49 +03:00
|
|
|
return IrdaProtocolUnknown;
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* irda_get_protocol_name(IrdaProtocol protocol) {
|
2021-06-09 16:04:49 +03:00
|
|
|
if (irda_is_protocol_valid(protocol))
|
|
|
|
return irda_protocols[protocol].name;
|
|
|
|
else
|
|
|
|
return "Invalid";
|
2021-05-18 13:51:00 +03:00
|
|
|
}
|
|
|
|
|
2021-06-02 18:16:05 +03:00
|
|
|
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
|
2021-06-09 16:04:49 +03:00
|
|
|
if (irda_is_protocol_valid(protocol))
|
|
|
|
return irda_protocols[protocol].address_length;
|
|
|
|
else
|
|
|
|
return 0;
|
2021-06-02 18:16:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
|
2021-06-09 16:04:49 +03:00
|
|
|
if (irda_is_protocol_valid(protocol))
|
|
|
|
return irda_protocols[protocol].command_length;
|
|
|
|
else
|
|
|
|
return 0;
|
2021-06-02 18:16:05 +03:00
|
|
|
}
|
|
|
|
|