Skip to content

Commit

Permalink
Introduce API types and refactor ov module
Browse files Browse the repository at this point in the history
Now, ov doesn't depend on other modules types.
struct ov_device is opaque type, a user is not supposed to work with it directly.
  • Loading branch information
matwey committed Jul 27, 2019
1 parent 98deb88 commit 829406e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
4 changes: 2 additions & 2 deletions include/cha.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct cha {
struct cha_loop {
struct cha* cha;

packet_decoder_callback callback;
ov_packet_decoder_callback callback;
void* user_data;

int count;
Expand All @@ -35,7 +35,7 @@ int cha_write_reg32(struct cha* cha, uint16_t addr, uint32_t val);
int cha_read_reg32(struct cha* cha, uint16_t addr, uint32_t* val);
int cha_start_stream(struct cha* cha);
int cha_stop_stream(struct cha* cha);
int cha_loop_init(struct cha_loop* loop, struct cha* cha, struct packet* packet, size_t packet_size, packet_decoder_callback callback, void* user_data);
int cha_loop_init(struct cha_loop* loop, struct cha* cha, struct ov_packet* packet, size_t packet_size, ov_packet_decoder_callback callback, void* user_data);
int cha_loop_run(struct cha_loop* loop, int count);
void cha_destroy(struct cha* cha);

Expand Down
17 changes: 5 additions & 12 deletions include/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@
#include <memory.h>
#include <stdint.h>

struct packet {
uint16_t flags;
uint16_t size;
uint32_t timestamp;
uint8_t data[];
};

typedef void (*packet_decoder_callback)(struct packet*, void*);
#include <ov.h>

struct packet_decoder {
struct packet* packet;
struct ov_packet* packet;
size_t buf_actual_length;
size_t buf_length;
char* error_str;

packet_decoder_callback callback;
ov_packet_decoder_callback callback;
void* user_data;

enum packet_decoder_state {
Expand All @@ -35,7 +28,7 @@ struct packet_decoder {
} state;
};

int packet_decoder_init(struct packet_decoder* pd, struct packet* p, size_t size, packet_decoder_callback callback, void* data);
int packet_decoder_init(struct packet_decoder* pd, struct ov_packet* p, size_t size, ov_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 @@ -51,7 +44,7 @@ struct frame_decoder {
size_t required_length;
};

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

#endif // _DECODER_H
22 changes: 13 additions & 9 deletions include/ov.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#ifndef _OV_H
#define _OV_H

#include <cha.h>
#include <chb.h>

struct ov_device {
struct cha cha;
struct chb chb;
struct cha_loop loop;
const char* error_str;
#include <stddef.h>
#include <stdint.h>

struct ov_device;

struct ov_packet {
uint16_t flags;
uint16_t size;
uint32_t timestamp;
uint8_t data[];
};

typedef void (*ov_packet_decoder_callback)(struct ov_packet*, void*);

int ov_init(struct ov_device* ov);
int ov_open(struct ov_device* ov);
void ov_destroy(struct ov_device* ov);

int ov_capture_start(struct ov_device* ov, struct packet* packet, size_t packet_size, packet_decoder_callback callback, void* user_data);
int ov_capture_start(struct ov_device* ov, struct ov_packet* packet, size_t packet_size, ov_packet_decoder_callback callback, void* user_data);
int ov_capture_dispatch(struct ov_device* ov, int count);
int ov_capture_stop(struct ov_device* ov);

Expand Down
6 changes: 3 additions & 3 deletions src/cha.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

struct cha_loop_packet_callback_state {
size_t count;
packet_decoder_callback user_callback;
ov_packet_decoder_callback user_callback;
void* user_data;
};

Expand Down Expand Up @@ -317,7 +317,7 @@ int cha_stop_stream(struct cha* cha) {
return 0;
}

static void cha_loop_packet_callback(struct packet* packet, void* data) {
static void cha_loop_packet_callback(struct ov_packet* packet, void* data) {
struct cha_loop* loop = (struct cha_loop*)data;

if (loop->max_count > 0)
Expand Down Expand Up @@ -408,7 +408,7 @@ static int cha_loop_read_from_ftdi(struct cha_loop* loop) {
return -1;
}

int cha_loop_init(struct cha_loop* loop, struct cha* cha, struct packet* packet, size_t packet_size, packet_decoder_callback callback, void* user_data) {
int cha_loop_init(struct cha_loop* loop, struct cha* cha, struct ov_packet* packet, size_t packet_size, ov_packet_decoder_callback callback, void* user_data) {
loop->cha = cha;
loop->callback = callback;
loop->user_data = user_data;
Expand Down
4 changes: 2 additions & 2 deletions src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <assert.h>

int packet_decoder_init(struct packet_decoder* pd, struct packet* p, size_t size, packet_decoder_callback callback, void* data) {
int packet_decoder_init(struct packet_decoder* pd, struct ov_packet* p, size_t size, ov_packet_decoder_callback callback, void* data) {
pd->packet = p;
pd->buf_actual_length = 0;
pd->buf_length = size;
Expand Down Expand Up @@ -85,7 +85,7 @@ 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, struct packet* p, size_t size, packet_decoder_callback callback, void* data) {
int frame_decoder_init(struct frame_decoder* fd, struct ov_packet* p, size_t size, ov_packet_decoder_callback callback, void* data) {
if (packet_decoder_init(&fd->pd, p, size, callback, data) < 0)
return -1;

Expand Down
12 changes: 11 additions & 1 deletion src/ov.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include <ov.h>

#include <cha.h>
#include <chb.h>

struct ov_device {
struct cha cha;
struct chb chb;
struct cha_loop loop;
const char* error_str;
};

int ov_init(struct ov_device* ov) {
int ret = 0;

Expand Down Expand Up @@ -53,7 +63,7 @@ void ov_destroy(struct ov_device* ov) {
cha_destroy(&ov->cha);
}

int ov_capture_start(struct ov_device* ov, struct packet* packet, size_t packet_size, packet_decoder_callback callback, void* user_data) {
int ov_capture_start(struct ov_device* ov, struct ov_packet* packet, size_t packet_size, ov_packet_decoder_callback callback, void* user_data) {

if (cha_switch_fifo_mode(&ov->cha) < 0) {
ov->error_str = cha_get_error_string(&ov->cha);
Expand Down
4 changes: 2 additions & 2 deletions test/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <decoder.h>

union {
struct packet packet;
struct ov_packet packet;
uint8_t data[1024];
} p;
struct packet_decoder pd;
struct frame_decoder fd;

void callback(struct packet* packet, void* data) {
void callback(struct ov_packet* packet, void* data) {
}

void packet_setup() {
Expand Down
4 changes: 2 additions & 2 deletions tools/sample/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define PORTB_DONE_BIT (1 << 2) // GPIOH2
#define PORTB_INIT_BIT (1 << 5) // GPIOH5

static void packet_handler(struct packet* packet, void* data) {
static void packet_handler(struct ov_packet* packet, void* data) {
printf("[%04x] Received %d bytes at %d:", packet->flags, packet->size, packet->timestamp);
for (int i = 0; i < packet->size; ++i)
printf(" %02x", packet->data[i]);
Expand Down Expand Up @@ -104,7 +104,7 @@ int main(int argc, char** argv) {
}

union {
struct packet packet;
struct ov_packet packet;
char buf[1024];
} p;
struct cha_loop cha_loop;
Expand Down

0 comments on commit 829406e

Please sign in to comment.