minor: Refactor create_device to decoder_create with user_data_size
This commit is contained in:
parent
f2098fd66a
commit
f48fc95d7b
6 changed files with 48 additions and 46 deletions
|
@ -29,7 +29,14 @@
|
|||
#endif
|
||||
|
||||
/// Create a new r_device, copy from dev_template if not NULL.
|
||||
r_device *create_device(r_device const *dev_template);
|
||||
///
|
||||
/// A user data memory of `user_data_size` bytes will be allocated if not `0`.
|
||||
r_device *decoder_create(r_device const *dev_template, unsigned user_data_size);
|
||||
|
||||
/// Get the user data pointer, otherwise NULL.
|
||||
///
|
||||
/// The memory can be freely used by a decoder and is of the size given to `decoder_create()`.
|
||||
void *decoder_user_data(r_device *decoder);
|
||||
|
||||
/// Output data.
|
||||
void decoder_output_data(r_device *decoder, data_t *data);
|
||||
|
|
|
@ -16,19 +16,33 @@
|
|||
|
||||
// create decoder functions
|
||||
|
||||
r_device *create_device(r_device const *dev_template)
|
||||
r_device *decoder_create(r_device const *dev_template, unsigned user_data_size)
|
||||
{
|
||||
r_device *r_dev = malloc(sizeof (*r_dev));
|
||||
r_device *r_dev = calloc(1, sizeof (*r_dev));
|
||||
if (!r_dev) {
|
||||
WARN_MALLOC("create_device()");
|
||||
WARN_MALLOC("decoder_create()");
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
if (dev_template)
|
||||
*r_dev = *dev_template; // copy
|
||||
|
||||
if (user_data_size) {
|
||||
r_dev->decode_ctx = calloc(1, user_data_size);
|
||||
if (!r_dev->decode_ctx) {
|
||||
WARN_MALLOC("decoder_create()");
|
||||
free(r_dev);
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
}
|
||||
|
||||
return r_dev;
|
||||
}
|
||||
|
||||
void *decoder_user_data(r_device *decoder)
|
||||
{
|
||||
return decoder->decode_ctx;
|
||||
}
|
||||
|
||||
// output functions
|
||||
|
||||
void decoder_output_log(r_device *decoder, int level, data_t *data)
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "fatal.h"
|
||||
#include "decoder.h"
|
||||
|
||||
/**
|
||||
|
@ -189,7 +188,7 @@ static uint8_t rev_crc8(uint8_t const message[], unsigned nBytes, uint8_t polyno
|
|||
|
||||
static uint16_t guess_blueline_id(r_device *decoder, const uint8_t *current_row)
|
||||
{
|
||||
struct blueline_stateful_context *const context = decoder->decode_ctx;
|
||||
struct blueline_stateful_context *const context = decoder_user_data(decoder);
|
||||
const uint16_t start_value = ((current_row[2] << 8) | current_row[1]);
|
||||
const uint8_t recv_crc = current_row[3];
|
||||
const uint8_t rcv_msg_type = (current_row[1] & 0x03);
|
||||
|
@ -239,7 +238,7 @@ static uint16_t guess_blueline_id(r_device *decoder, const uint8_t *current_row)
|
|||
|
||||
static int blueline_decode(r_device *decoder, bitbuffer_t *bitbuffer)
|
||||
{
|
||||
struct blueline_stateful_context *const context = decoder->decode_ctx;
|
||||
struct blueline_stateful_context *const context = decoder_user_data(decoder);
|
||||
data_t *data;
|
||||
int row_index;
|
||||
uint8_t *current_row;
|
||||
|
@ -400,20 +399,12 @@ r_device const blueline;
|
|||
|
||||
static r_device *blueline_create(char *arg)
|
||||
{
|
||||
r_device *r_dev = create_device(&blueline);
|
||||
r_device *r_dev = decoder_create(&blueline, sizeof(struct blueline_stateful_context));
|
||||
if (!r_dev) {
|
||||
WARN_MALLOC("blueline_create()");
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
|
||||
struct blueline_stateful_context *context = malloc(sizeof(*context));
|
||||
if (!context) {
|
||||
WARN_MALLOC("blueline_create()");
|
||||
free(r_dev);
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
memset(context, 0, sizeof(*context));
|
||||
r_dev->decode_ctx = context;
|
||||
struct blueline_stateful_context *context = decoder_user_data(r_dev);
|
||||
|
||||
if (arg != NULL) {
|
||||
if (strcmp(arg, "auto") == 0) {
|
||||
|
|
|
@ -12,31 +12,24 @@
|
|||
*/
|
||||
|
||||
#include "decoder.h"
|
||||
#include "fatal.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
r_device const fineoffset_WH2;
|
||||
|
||||
static r_device *fineoffset_WH2_create(char *arg)
|
||||
{
|
||||
r_device *r_dev = create_device(&fineoffset_WH2);
|
||||
if (!r_dev) {
|
||||
WARN_MALLOC("fineoffset_WH2_create()");
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
|
||||
if (arg && !strcmp(arg, "no-wh5")) {
|
||||
int *quirk = malloc(sizeof (*quirk));
|
||||
if (!quirk) {
|
||||
WARN_MALLOC("fineoffset_WH2_create()");
|
||||
free(r_dev);
|
||||
r_device *r_dev = decoder_create(&fineoffset_WH2, sizeof(int));
|
||||
if (!r_dev) {
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
*quirk = 1;
|
||||
r_dev->decode_ctx = quirk;
|
||||
}
|
||||
|
||||
return r_dev;
|
||||
int *quirk = decoder_user_data(r_dev);
|
||||
*quirk = 1;
|
||||
return r_dev;
|
||||
}
|
||||
else {
|
||||
return decoder_create(&fineoffset_WH2, 0); // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,6 +62,7 @@ http://lucsmall.com/2012/04/29/weather-station-hacking-part-2/
|
|||
#define MODEL_TP 7
|
||||
static int fineoffset_WH2_callback(r_device *decoder, bitbuffer_t *bitbuffer)
|
||||
{
|
||||
void *user_data = decoder_user_data(decoder);
|
||||
bitrow_t *bb = bitbuffer->bb;
|
||||
uint8_t b[6] = {0};
|
||||
data_t *data;
|
||||
|
@ -94,7 +88,7 @@ static int fineoffset_WH2_callback(r_device *decoder, bitbuffer_t *bitbuffer)
|
|||
bb[0][0] == 0xFE) { // WH5
|
||||
bitbuffer_extract_bytes(bitbuffer, 0, 7, b, 40);
|
||||
model_num = MODEL_WH5;
|
||||
if (decoder->decode_ctx) // don't care for the actual value
|
||||
if (user_data) // don't care for the actual value
|
||||
model_num = MODEL_RB;
|
||||
|
||||
} else if (bitbuffer->bits_per_row[0] == 49 &&
|
||||
|
@ -121,7 +115,7 @@ static int fineoffset_WH2_callback(r_device *decoder, bitbuffer_t *bitbuffer)
|
|||
|
||||
// Nibble 5,6,7 contains 12 bits of temperature
|
||||
temp = ((b[1] & 0x0F) << 8) | b[2];
|
||||
if (bitbuffer->bits_per_row[0] != 47 || decoder->decode_ctx) { // WH2, Telldus, WH2A
|
||||
if (bitbuffer->bits_per_row[0] != 47 || user_data) { // WH2, Telldus, WH2A
|
||||
// The temperature is signed magnitude and scaled by 10
|
||||
if (temp & 0x800) {
|
||||
temp &= 0x7FF; // remove sign bit
|
||||
|
|
|
@ -157,7 +157,7 @@ static int flex_callback(r_device *decoder, bitbuffer_t *bitbuffer)
|
|||
char *row_codes[BITBUF_ROWS];
|
||||
char row_bytes[BITBUF_ROWS * BITBUF_COLS * 2 + 1]; // TODO: this is a lot of stack
|
||||
|
||||
struct flex_params *params = decoder->decode_ctx;
|
||||
struct flex_params *params = decoder_user_data(decoder);
|
||||
|
||||
// discard short / unwanted bitbuffers
|
||||
if ((bitbuffer->num_rows < params->min_rows)
|
||||
|
@ -586,18 +586,11 @@ r_device *flex_create_device(char *spec)
|
|||
help();
|
||||
}
|
||||
|
||||
struct flex_params *params = calloc(1, sizeof(*params));
|
||||
if (!params) {
|
||||
WARN_CALLOC("flex_create_device()");
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
r_device *dev = calloc(1, sizeof(*dev));
|
||||
r_device *dev = decoder_create(NULL, sizeof(struct flex_params));
|
||||
if (!dev) {
|
||||
WARN_CALLOC("flex_create_device()");
|
||||
free(params);
|
||||
return NULL; // NOTE: returns NULL on alloc failure.
|
||||
}
|
||||
dev->decode_ctx = params;
|
||||
struct flex_params *params = decoder_user_data(dev);
|
||||
int get_count = 0;
|
||||
|
||||
spec = strdup(spec);
|
||||
|
|
|
@ -187,10 +187,12 @@ static int parse_insteon_pkt(r_device *decoder, bitbuffer_t *bits, unsigned int
|
|||
decoder_logf(decoder, 1, __func__, "%s %-5s %s %s %s",
|
||||
"pkt_i", "pkt_d", "next", "length", "count");
|
||||
|
||||
{
|
||||
uint8_t buffy[4];
|
||||
bitbuffer_extract_bytes(bits, row, start_pos - 2, buffy, 30);
|
||||
decoder_logf_bitrow(decoder, 1, __func__, buffy, 30, "%2d %02X %03u %u %2d",
|
||||
pkt_i, pkt_d, next_pos, (next_pos - start_pos), 0);
|
||||
}
|
||||
|
||||
/* Is this overkill ??
|
||||
unsigned int l;
|
||||
|
@ -234,12 +236,13 @@ static int parse_insteon_pkt(r_device *decoder, bitbuffer_t *bits, unsigned int
|
|||
|
||||
results[results_len++] = pkt_d;
|
||||
|
||||
{
|
||||
uint8_t buffy[4];
|
||||
bitbuffer_extract_bytes(bits, row, start_pos - 2, buffy, 30);
|
||||
// decoder_logf_bitrow(decoder, 1, __func__, buffy, 30, "%s: %2d %02X %3u %d %d",
|
||||
decoder_logf_bitrow(decoder, 1, __func__, buffy, 30, "%2d %02X %03u %u %2d",
|
||||
pkt_i, pkt_d, next_pos, (next_pos - start_pos), j);
|
||||
// parse_insteon_pkt: curr packet (3f) { 1} d6 : 1
|
||||
}
|
||||
|
||||
// packet index should decrement
|
||||
if (pkt_i < prev_i) {
|
||||
|
|
Loading…
Add table
Reference in a new issue