Skip to content

Commit

Permalink
Reimplement cha_loop using decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
matwey committed Jun 25, 2017
1 parent b45e741 commit e298af6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
9 changes: 7 additions & 2 deletions include/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
#include <memory.h>
#include <stdint.h>

typedef void (*packet_decoder_callback)(uint8_t*, size_t, void*);

struct packet_decoder {
uint8_t* buf;
size_t buf_actual_length;
size_t buf_length;
char* error_str;

packet_decoder_callback callback;
void* user_data;

enum packet_decoder_state {
NEED_PACKET_MAGIC,
NEED_PACKET_LENGTH,
Expand All @@ -18,7 +23,7 @@ struct packet_decoder {
size_t required_length;
};

int packet_decoder_init(struct packet_decoder* pd, uint8_t* buf, size_t size);
int packet_decoder_init(struct packet_decoder* pd, uint8_t* buf, size_t size, packet_decoder_callback callback, void* data);
int packet_decoder_proc(struct packet_decoder* pd, uint8_t* buf, size_t size);

struct frame_decoder {
Expand All @@ -34,7 +39,7 @@ struct frame_decoder {
size_t required_length;
};

int frame_decoder_init(struct frame_decoder* fd, uint8_t* buf, size_t size);
int frame_decoder_init(struct frame_decoder* fd, uint8_t* buf, size_t size, packet_decoder_callback callback, void* data);
int frame_decoder_proc(struct frame_decoder* fd, uint8_t* buf, size_t size);

#endif // _DECODER_H
13 changes: 12 additions & 1 deletion src/cha.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ int cha_stop_stream(struct cha* cha) {
return 0;
}

static void cha_loop_packet_callback(uint8_t* buf, size_t size, void* data) {
printf("Received %d :", size);
for (int i = 0; i < size; ++i)
printf(" %02x", buf[i]);
printf("\n");
}

static void cha_loop_transfer_callback(struct libusb_transfer* transfer) {
struct frame_decoder* fd = (struct frame_decoder*)transfer->user_data;

Expand All @@ -329,7 +336,7 @@ int cha_loop(struct cha* cha, int cnt) {

struct frame_decoder fd;

if (frame_decoder_init(&fd, packet_buf, sizeof(packet_buf)) == -1)
if (frame_decoder_init(&fd, packet_buf, sizeof(packet_buf), &cha_loop_packet_callback, NULL) == -1)
return -1;

usb_transfer = libusb_alloc_transfer(0);
Expand All @@ -350,6 +357,10 @@ int cha_loop(struct cha* cha, int cnt) {
cha->error_str = libusb_error_name(ret);
goto fail_libusb_handle_events;
}
if (fd.error_str) {
cha->error_str = fd.error_str;
goto fail_libusb_handle_events;
}
}

libusb_free_transfer(usb_transfer);
Expand Down
13 changes: 9 additions & 4 deletions src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

#include <assert.h>

int packet_decoder_init(struct packet_decoder* pd, uint8_t* buf, size_t size) {
int packet_decoder_init(struct packet_decoder* pd, uint8_t* buf, size_t size, packet_decoder_callback callback, void* data) {
pd->buf = buf;
pd->buf_actual_length = 0;
pd->buf_length = size;
pd->callback = callback;
pd->user_data = data;
pd->error_str = NULL;
pd->state = NEED_PACKET_MAGIC;
pd->required_length = 0;
Expand Down Expand Up @@ -56,6 +58,7 @@ int packet_decoder_proc(struct packet_decoder* pd, uint8_t* buf, size_t size) {

if (pd->required_length == 0) {
/* Finalize packet here*/
pd->callback(pd->buf, pd->buf_actual_length, pd->user_data);

pd->buf_actual_length = 0;
pd->state = NEED_PACKET_MAGIC;
Expand All @@ -70,8 +73,8 @@ int packet_decoder_proc(struct packet_decoder* pd, uint8_t* buf, size_t size) {
return size - (end - buf);
}

int frame_decoder_init(struct frame_decoder* fd, uint8_t* buf, size_t size) {
if (packet_decoder_init(&fd->pd, buf, size) != 0)
int frame_decoder_init(struct frame_decoder* fd, uint8_t* buf, size_t size, packet_decoder_callback callback, void* data) {
if (packet_decoder_init(&fd->pd, buf, size, callback, data) != 0)
return -1;

fd->error_str = NULL;
Expand Down Expand Up @@ -103,8 +106,10 @@ int frame_decoder_proc(struct frame_decoder* fd, uint8_t* buf, size_t size) {
int ret = 0;

ret = packet_decoder_proc(&fd->pd, buf, size);
if (ret == -1)
if (ret == -1) {
fd->error_str = fd->pd.error_str;
return -1;
}

buf += ret;
fd->required_length -= ret;
Expand Down
7 changes: 5 additions & 2 deletions test/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ char buf[1024];
struct packet_decoder pd;
struct frame_decoder fd;

void callback(uint8_t* buf, size_t size, void* data) {
}

void packet_setup() {
ck_assert_int_eq(packet_decoder_init(&pd, buf, sizeof(buf)), 0);
ck_assert_int_eq(packet_decoder_init(&pd, buf, sizeof(buf), &callback, NULL), 0);
}

void packet_teardown() {

}

void frame_setup() {
ck_assert_int_eq(frame_decoder_init(&fd, buf, sizeof(buf)), 0);
ck_assert_int_eq(frame_decoder_init(&fd, buf, sizeof(buf), &callback, NULL), 0);
}

void frame_teardown() {
Expand Down

0 comments on commit e298af6

Please sign in to comment.