[FL-1696, FL-1685] IRDA: Add RC5, decoder refactoring (#663)
* [FL-1696] IRDA: Split decoders and protocols * IRDA: Restruct directories. * IRDA: fix long timings * [FL-1685] IRDA: Add RC5 Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "irda.h"
|
||||
#include "furi/check.h"
|
||||
#include "irda_common_i.h"
|
||||
#include "common/irda_common_i.h"
|
||||
#include "irda_protocol_defs_i.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -9,10 +9,6 @@
|
||||
#include "irda_i.h"
|
||||
#include <furi-hal-irda.h>
|
||||
|
||||
struct IrdaDecoderHandler {
|
||||
void** ctx;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
IrdaAlloc alloc;
|
||||
IrdaDecode decode;
|
||||
@@ -27,27 +23,36 @@ typedef struct {
|
||||
IrdaFree free;
|
||||
} IrdaEncoders;
|
||||
|
||||
typedef struct {
|
||||
IrdaProtocol protocol;
|
||||
const char* name;
|
||||
IrdaDecoders decoder;
|
||||
IrdaEncoders encoder;
|
||||
uint8_t address_length;
|
||||
uint8_t command_length;
|
||||
uint32_t frequency;
|
||||
float duty_cycle;
|
||||
} IrdaProtocolImplementation;
|
||||
|
||||
struct IrdaEncoderHandler {
|
||||
void* encoder;
|
||||
IrdaProtocol protocol;
|
||||
struct IrdaDecoderHandler {
|
||||
void** ctx;
|
||||
};
|
||||
|
||||
// TODO: replace with key-value, Now we refer by enum index, which is dangerous.
|
||||
static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
// #0
|
||||
{ .protocol = IrdaProtocolNEC,
|
||||
.name = "NEC",
|
||||
struct IrdaEncoderHandler {
|
||||
void* handler;
|
||||
const IrdaEncoders* encoder;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
IrdaEncoders encoder;
|
||||
IrdaDecoders decoder;
|
||||
IrdaGetProtocolSpec get_protocol_spec;
|
||||
} IrdaEncoderDecoder;
|
||||
|
||||
static const IrdaEncoderDecoder irda_encoder_decoder[] = {
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_rc5_alloc,
|
||||
.decode = irda_decoder_rc5_decode,
|
||||
.reset = irda_decoder_rc5_reset,
|
||||
.free = irda_decoder_rc5_free},
|
||||
.encoder = {
|
||||
.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,
|
||||
},
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_nec_alloc,
|
||||
.decode = irda_decoder_nec_decode,
|
||||
@@ -58,32 +63,9 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.encode = irda_encoder_nec_encode,
|
||||
.reset = irda_encoder_nec_reset,
|
||||
.free = irda_encoder_nec_free},
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
.get_protocol_spec = irda_nec_get_spec,
|
||||
},
|
||||
// #1 - have to be after NEC
|
||||
{ .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 = {
|
||||
.alloc = irda_encoder_necext_alloc,
|
||||
.encode = irda_encoder_nec_encode,
|
||||
.reset = irda_encoder_necext_reset,
|
||||
.free = irda_encoder_nec_free},
|
||||
.address_length = 4,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
},
|
||||
// #2
|
||||
{ .protocol = IrdaProtocolSamsung32,
|
||||
.name ="Samsung32",
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_samsung32_alloc,
|
||||
.decode = irda_decoder_samsung32_decode,
|
||||
@@ -94,14 +76,9 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.encode = irda_encoder_samsung32_encode,
|
||||
.reset = irda_encoder_samsung32_reset,
|
||||
.free = irda_encoder_samsung32_free},
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
.get_protocol_spec = irda_samsung32_get_spec,
|
||||
},
|
||||
// #3
|
||||
{ .protocol = IrdaProtocolRC6,
|
||||
.name = "RC6",
|
||||
{
|
||||
.decoder = {
|
||||
.alloc = irda_decoder_rc6_alloc,
|
||||
.decode = irda_decoder_rc6_decode,
|
||||
@@ -112,10 +89,7 @@ static const IrdaProtocolImplementation irda_protocols[] = {
|
||||
.encode = irda_encoder_rc6_encode,
|
||||
.reset = irda_encoder_rc6_reset,
|
||||
.free = irda_encoder_rc6_free},
|
||||
.address_length = 2,
|
||||
.command_length = 2,
|
||||
.frequency = IRDA_COMMON_CARRIER_FREQUENCY,
|
||||
.duty_cycle = IRDA_COMMON_DUTY_CYCLE,
|
||||
.get_protocol_spec = irda_rc6_get_spec,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -125,11 +99,10 @@ const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t
|
||||
IrdaMessage* message = NULL;
|
||||
IrdaMessage* result = NULL;
|
||||
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
if (irda_protocols[i].decoder.decode) {
|
||||
message = irda_protocols[i].decoder.decode(handler->ctx[i], level, duration);
|
||||
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);
|
||||
if (!result && message) {
|
||||
message->protocol = irda_protocols[i].protocol;
|
||||
result = message;
|
||||
}
|
||||
}
|
||||
@@ -140,12 +113,12 @@ const IrdaMessage* irda_decode(IrdaDecoderHandler* handler, bool level, uint32_t
|
||||
|
||||
IrdaDecoderHandler* irda_alloc_decoder(void) {
|
||||
IrdaDecoderHandler* handler = furi_alloc(sizeof(IrdaDecoderHandler));
|
||||
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_protocols));
|
||||
handler->ctx = furi_alloc(sizeof(void*) * COUNT_OF(irda_encoder_decoder));
|
||||
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
for (int i = 0; i < COUNT_OF(irda_encoder_decoder); ++i) {
|
||||
handler->ctx[i] = 0;
|
||||
if (irda_protocols[i].decoder.alloc)
|
||||
handler->ctx[i] = irda_protocols[i].decoder.alloc();
|
||||
if (irda_encoder_decoder[i].decoder.alloc)
|
||||
handler->ctx[i] = irda_encoder_decoder[i].decoder.alloc();
|
||||
}
|
||||
|
||||
return handler;
|
||||
@@ -155,9 +128,9 @@ void irda_free_decoder(IrdaDecoderHandler* handler) {
|
||||
furi_assert(handler);
|
||||
furi_assert(handler->ctx);
|
||||
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
if (irda_protocols[i].decoder.free)
|
||||
irda_protocols[i].decoder.free(handler->ctx[i]);
|
||||
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]);
|
||||
}
|
||||
|
||||
free(handler->ctx);
|
||||
@@ -165,114 +138,119 @@ void irda_free_decoder(IrdaDecoderHandler* handler) {
|
||||
}
|
||||
|
||||
void irda_reset_decoder(IrdaDecoderHandler* handler) {
|
||||
for (int i = 0; i < COUNT_OF(irda_protocols); ++i) {
|
||||
if (irda_protocols[i].decoder.reset)
|
||||
irda_protocols[i].decoder.reset(handler->ctx[i]);
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
IrdaEncoderHandler* irda_alloc_encoder(void) {
|
||||
IrdaEncoderHandler* handler = furi_alloc(sizeof(IrdaEncoderHandler));
|
||||
handler->handler = NULL;
|
||||
handler->encoder = NULL;
|
||||
handler->protocol = IrdaProtocolUnknown;
|
||||
return handler;
|
||||
}
|
||||
|
||||
void irda_free_encoder(IrdaEncoderHandler* handler) {
|
||||
furi_assert(handler);
|
||||
const IrdaEncoders* encoder = handler->encoder;
|
||||
|
||||
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);
|
||||
if (encoder || handler->handler) {
|
||||
furi_assert(encoder);
|
||||
furi_assert(handler->handler);
|
||||
furi_assert(encoder->free);
|
||||
encoder->free(handler->handler);
|
||||
}
|
||||
|
||||
free(handler);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void irda_reset_encoder(IrdaEncoderHandler* handler, const IrdaMessage* message) {
|
||||
furi_assert(handler);
|
||||
furi_assert(message);
|
||||
furi_assert(irda_is_protocol_valid(message->protocol));
|
||||
furi_assert(irda_protocols[message->protocol].encoder.reset);
|
||||
furi_assert(irda_protocols[message->protocol].encoder.alloc);
|
||||
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);
|
||||
|
||||
/* 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);
|
||||
if (required_encoder != handler->encoder) {
|
||||
if (handler->handler != NULL) {
|
||||
furi_assert(handler->encoder->free);
|
||||
handler->encoder->free(handler->handler);
|
||||
}
|
||||
handler->encoder = irda_protocols[message->protocol].encoder.alloc();
|
||||
handler->protocol = message->protocol;
|
||||
handler->encoder = required_encoder;
|
||||
handler->handler = handler->encoder->alloc();
|
||||
}
|
||||
|
||||
irda_protocols[handler->protocol].encoder.reset(handler->encoder, message);
|
||||
handler->encoder->reset(handler->handler, message);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
furi_assert(duration);
|
||||
furi_assert(level);
|
||||
const IrdaEncoders* encoder = handler->encoder;
|
||||
furi_assert(encoder);
|
||||
furi_assert(encoder->encode);
|
||||
|
||||
IrdaStatus status = irda_protocols[handler->protocol].encoder.encode(handler->encoder, duration, level);
|
||||
IrdaStatus status = encoder->encode(handler->handler, duration, level);
|
||||
furi_assert(status != IrdaStatusError);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
bool irda_is_protocol_valid(IrdaProtocol protocol) {
|
||||
return (protocol >= 0) && (protocol < COUNT_OF(irda_protocols));
|
||||
return irda_find_index_by_protocol(protocol) >= 0;
|
||||
}
|
||||
|
||||
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;
|
||||
for (IrdaProtocol protocol = 0; protocol < IrdaProtocolMAX; ++protocol) {
|
||||
const char* name = irda_get_protocol_name(protocol);
|
||||
if (!strcmp(name, protocol_name))
|
||||
return protocol;
|
||||
}
|
||||
furi_assert(0);
|
||||
return IrdaProtocolUnknown;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const char* irda_get_protocol_name(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].name;
|
||||
else
|
||||
return "Invalid";
|
||||
return irda_get_spec_by_protocol(protocol)->name;
|
||||
}
|
||||
|
||||
uint8_t irda_get_protocol_address_length(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].address_length;
|
||||
else
|
||||
return 0;
|
||||
return irda_get_spec_by_protocol(protocol)->address_length;
|
||||
}
|
||||
|
||||
uint8_t irda_get_protocol_command_length(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].command_length;
|
||||
else
|
||||
return 0;
|
||||
return irda_get_spec_by_protocol(protocol)->command_length;
|
||||
}
|
||||
|
||||
uint32_t irda_get_protocol_frequency(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].frequency;
|
||||
else
|
||||
return 0;
|
||||
return irda_get_spec_by_protocol(protocol)->frequency;
|
||||
}
|
||||
|
||||
float irda_get_protocol_duty_cycle(IrdaProtocol protocol) {
|
||||
furi_assert(irda_is_protocol_valid(protocol));
|
||||
if (irda_is_protocol_valid(protocol))
|
||||
return irda_protocols[protocol].duty_cycle;
|
||||
else
|
||||
return 0;
|
||||
return irda_get_spec_by_protocol(protocol)->duty_cycle;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user