2021-06-09 13:04:49 +00:00
|
|
|
#include "irda.h"
|
2021-07-08 18:20:13 +00:00
|
|
|
#include "furi/check.h"
|
2021-08-20 20:51:15 +00:00
|
|
|
#include "common/irda_common_i.h"
|
2021-07-08 18:20:13 +00:00
|
|
|
#include "irda_protocol_defs_i.h"
|
2021-05-18 10:51:00 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <furi.h>
|
|
|
|
#include "irda_i.h"
|
2021-08-08 18:03:25 +00:00
|
|
|
#include <furi-hal-irda.h>
|
2021-05-18 10:51:00 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
IrdaAlloc alloc;
|
|
|
|
IrdaDecode decode;
|
2021-06-02 15:16:05 +00:00
|
|
|
IrdaReset reset;
|
2021-05-18 10:51:00 +00:00
|
|
|
IrdaFree free;
|
|
|
|
} IrdaDecoders;
|
|
|
|
|
|
|
|
typedef struct {
|
2021-07-08 18:20:13 +00:00
|
|
|
IrdaEncoderReset reset;
|
|
|
|
IrdaAlloc alloc;
|
2021-05-18 10:51:00 +00:00
|
|
|
IrdaEncode encode;
|
2021-07-08 18:20:13 +00:00
|
|
|
IrdaFree free;
|
2021-05-18 10:51:00 +00:00
|
|
|
} IrdaEncoders;
|
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
struct IrdaDecoderHandler {
|
|
|
|
void** ctx;
|
|
|
|
};
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
struct IrdaEncoderHandler {
|
2021-08-20 20:51:15 +00:00
|
|
|
void* handler;
|
|
|
|
const IrdaEncoders* encoder;
|
2021-07-08 18:20:13 +00:00
|
|
|
};
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
typedef struct {
|
|
|
|
IrdaEncoders encoder;
|
|
|
|
IrdaDecoders decoder;
|
|
|
|
IrdaGetProtocolSpec get_protocol_spec;
|
|
|
|
} IrdaEncoderDecoder;
|
|
|
|
|
|
|
|
static const IrdaEncoderDecoder irda_encoder_decoder[] = {
|
|
|
|
{
|
2021-05-18 10:51:00 +00:00
|
|
|
.decoder = {
|
2021-08-20 20:51:15 +00:00
|
|
|
.alloc = irda_decoder_rc5_alloc,
|
|
|
|
.decode = irda_decoder_rc5_decode,
|
|
|
|
.reset = irda_decoder_rc5_reset,
|
|
|
|
.free = irda_decoder_rc5_free},
|
2021-05-18 10:51:00 +00:00
|
|
|
.encoder = {
|
2021-08-20 20:51:15 +00:00
|
|
|
.alloc = irda_encoder_rc5_alloc,
|
|
|
|
.encode = irda_encoder_rc5_encode,
|
|
|
|
.reset = irda_encoder_rc5_reset,
|
|
|
|
.free = irda_encoder_rc5_free},
|
|
|
|
.get_protocol_spec = irda_rc5_get_spec,
|
2021-06-02 15:16:05 +00:00
|
|
|
},
|
2021-08-20 20:51:15 +00:00
|
|
|
{
|
2021-06-02 15:16:05 +00:00
|
|
|
.decoder = {
|
2021-08-20 20:51:15 +00:00
|
|
|
.alloc = irda_decoder_nec_alloc,
|
2021-06-02 15:16:05 +00:00
|
|
|
.decode = irda_decoder_nec_decode,
|
|
|
|
.reset = irda_decoder_nec_reset,
|
|
|
|
.free = irda_decoder_nec_free},
|
|
|
|
.encoder = {
|
2021-08-20 20:51:15 +00:00
|
|
|
.alloc = irda_encoder_nec_alloc,
|
2021-07-08 18:20:13 +00:00
|
|
|
.encode = irda_encoder_nec_encode,
|
2021-08-20 20:51:15 +00:00
|
|
|
.reset = irda_encoder_nec_reset,
|
2021-07-08 18:20:13 +00:00
|
|
|
.free = irda_encoder_nec_free},
|
2021-08-20 20:51:15 +00:00
|
|
|
.get_protocol_spec = irda_nec_get_spec,
|
2021-05-18 10:51:00 +00:00
|
|
|
},
|
2021-08-20 20:51:15 +00:00
|
|
|
{
|
2021-07-08 18:20:13 +00:00
|
|
|
.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},
|
2021-08-20 20:51:15 +00:00
|
|
|
.get_protocol_spec = irda_samsung32_get_spec,
|
2021-07-08 18:20:13 +00:00
|
|
|
},
|
2021-08-20 20:51:15 +00:00
|
|
|
{
|
2021-07-08 18:20:13 +00:00
|
|
|
.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},
|
2021-08-20 20:51:15 +00:00
|
|
|
.get_protocol_spec = irda_rc6_get_spec,
|
2021-07-08 18:20:13 +00:00
|
|
|
},
|
2021-05-18 10:51:00 +00:00
|
|
|
};
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t duration) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(handler);
|
|
|
|
|
|
|
|
IrdaMessage* message = NULL;
|
|
|
|
IrdaMessage* result = NULL;
|
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
|
|
|
if (irda_encoder_decoder[i].decoder.decode) {
|
|
|
|
message = irda_encoder_decoder[i].decoder.decode(handler->ctx[i], level, duration);
|
2021-06-09 13:04:49 +00:00
|
|
|
if (!result && message) {
|
|
|
|
result = message;
|
|
|
|
}
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
IrdaDecoderHandler* irda_alloc_decoder(void) {
|
|
|
|
IrdaDecoderHandler* handler = furi_alloc(sizeof(IrdaDecoderHandler));
|
2021-08-20 20:51:15 +00:00
|
|
|
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_encoder_decoder));
|
2021-05-18 10:51:00 +00:00
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
2021-06-09 13:04:49 +00:00
|
|
|
handler->ctx[i] = 0;
|
2021-08-20 20:51:15 +00:00
|
|
|
if (irda_encoder_decoder[i].decoder.alloc)
|
|
|
|
handler->ctx[i] = irda_encoder_decoder[i].decoder.alloc();
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
void irda_free_decoder(IrdaDecoderHandler* handler) {
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(handler);
|
|
|
|
furi_assert(handler->ctx);
|
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
|
|
|
if (irda_encoder_decoder[i].decoder.free)
|
|
|
|
irda_encoder_decoder[i].decoder.free(handler->ctx[i]);
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(handler->ctx);
|
|
|
|
free(handler);
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
void irda_reset_decoder(IrdaDecoderHandler* handler) {
|
2021-08-20 20:51:15 +00:00
|
|
|
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
|
|
|
if (irda_encoder_decoder[i].decoder.reset)
|
|
|
|
irda_encoder_decoder[i].decoder.reset(handler->ctx[i]);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
IrdaEncoderHandler* irda_alloc_encoder(void) {
|
|
|
|
IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
|
2021-08-20 20:51:15 +00:00
|
|
|
handler->handler = NULL;
|
2021-07-08 18:20:13 +00:00
|
|
|
handler->encoder = NULL;
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irda_free_encoder(IrdaEncoderHandler* handler) {
|
|
|
|
furi_assert(handler);
|
2021-08-20 20:51:15 +00:00
|
|
|
const IrdaEncoders* encoder = handler->encoder;
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
if (encoder || handler->handler) {
|
|
|
|
furi_assert(encoder);
|
|
|
|
furi_assert(handler->handler);
|
|
|
|
furi_assert(encoder->free);
|
|
|
|
encoder->free(handler->handler);
|
2021-07-08 18:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(handler);
|
|
|
|
}
|
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
static int irda_find_index_by_protocol(IrdaProtocol protocol) {
|
|
|
|
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
|
|
|
if (irda_encoder_decoder[i].get_protocol_spec(protocol)) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message) {
|
|
|
|
furi_assert(handler);
|
2021-05-18 10:51:00 +00:00
|
|
|
furi_assert(message);
|
2021-08-20 20:51:15 +00:00
|
|
|
int index = irda_find_index_by_protocol(message->protocol);
|
|
|
|
furi_check(index >= 0);
|
|
|
|
|
|
|
|
const IrdaEncoders* required_encoder = &irda_encoder_decoder[index].encoder;
|
|
|
|
furi_assert(required_encoder);
|
|
|
|
furi_assert(required_encoder->reset);
|
|
|
|
furi_assert(required_encoder->alloc);
|
2021-07-08 18:20:13 +00:00
|
|
|
|
|
|
|
/* Realloc encoder if different protocol set */
|
2021-08-20 20:51:15 +00:00
|
|
|
if (required_encoder != handler->encoder) {
|
|
|
|
if (handler->handler != NULL) {
|
|
|
|
furi_assert(handler->encoder->free);
|
|
|
|
handler->encoder->free(handler->handler);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
2021-08-20 20:51:15 +00:00
|
|
|
handler->encoder = required_encoder;
|
|
|
|
handler->handler = handler->encoder->alloc();
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
handler->encoder->reset(handler->handler, message);
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 18:20:13 +00:00
|
|
|
IrdaStatus irda_encode(IrdaEncoderHandler* handler, uint32_t* duration, bool* level) {
|
|
|
|
furi_assert(handler);
|
2021-08-20 20:51:15 +00:00
|
|
|
furi_assert(duration);
|
|
|
|
furi_assert(level);
|
|
|
|
const IrdaEncoders* encoder = handler->encoder;
|
|
|
|
furi_assert(encoder);
|
|
|
|
furi_assert(encoder->encode);
|
2021-07-08 18:20:13 +00:00
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
IrdaStatus status = encoder->encode(handler->handler, duration, level);
|
2021-07-08 18:20:13 +00:00
|
|
|
furi_assert(status != IrdaStatusError);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-06-09 13:04:49 +00:00
|
|
|
bool irda_is_protocol_valid(IrdaProtocol protocol) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return irda_find_index_by_protocol(protocol) >= 0;
|
2021-06-09 13:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IrdaProtocol irda_get_protocol_by_name(const char* protocol_name) {
|
2021-08-20 20:51:15 +00:00
|
|
|
for (IrdaProtocol protocol = 0; protocol < IrdaProtocolMAX; ++protocol) {
|
|
|
|
const char* name = irda_get_protocol_name(protocol);
|
|
|
|
if (!strcmp(name, protocol_name))
|
|
|
|
return protocol;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
2021-06-09 13:04:49 +00:00
|
|
|
return IrdaProtocolUnknown;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 20:51:15 +00:00
|
|
|
static const IrdaProtocolSpecification* irda_get_spec_by_protocol(IrdaProtocol protocol) {
|
|
|
|
int index = irda_find_index_by_protocol(protocol);
|
|
|
|
furi_check(index >= 0);
|
|
|
|
const IrdaProtocolSpecification* spec = irda_encoder_decoder[index].get_protocol_spec(protocol);
|
|
|
|
furi_assert(spec);
|
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:51:00 +00:00
|
|
|
const char* irda_get_protocol_name(IrdaProtocol protocol) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return irda_get_spec_by_protocol(protocol)->name;
|
2021-05-18 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 15:16:05 +00:00
|
|
|
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return irda_get_spec_by_protocol(protocol)->address_length;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return irda_get_spec_by_protocol(protocol)->command_length;
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:51:06 +00:00
|
|
|
uint32_t irda_get_protocol_frequency(IrdaProtocol protocol) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return irda_get_spec_by_protocol(protocol)->frequency;
|
2021-08-11 17:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float irda_get_protocol_duty_cycle(IrdaProtocol protocol) {
|
2021-08-20 20:51:15 +00:00
|
|
|
return irda_get_spec_by_protocol(protocol)->duty_cycle;
|
2021-08-11 17:51:06 +00:00
|
|
|
}
|
|
|
|
|