Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream synchronization fixes #34

Merged
merged 3 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/cha.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define ftdi_tcioflush(x) ftdi_usb_purge_buffers(x)
#endif

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

struct cha_loop_packet_callback_state {
size_t count;
ov_packet_decoder_callback user_callback;
Expand Down Expand Up @@ -517,21 +519,27 @@ static void cha_loop_bus_frame_callback(void* data, uint16_t addr, uint8_t value
static void LIBUSB_CALL cha_loop_transfer_callback(struct libusb_transfer* transfer) {
struct cha_loop* loop = (struct cha_loop*)transfer->user_data;
struct cha* cha = loop->cha;
struct ftdi_context* ftdi = &cha->ftdi;

int ret = 0;
size_t offset = 0;

switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED: {
if (loop->state == RUNNING && transfer->actual_length > 2) {
/* Skip FTDI header */
if (frame_decoder_proc(
&loop->fd,
transfer->buffer + 2,
transfer->actual_length - 2) < 0) {

loop->state = FATAL_ERROR;
cha->error_str = loop->fd.pd.error_str;
};
while (loop->state == RUNNING && offset < transfer->actual_length) {
size_t packet_length = MIN(transfer->actual_length - offset, ftdi->max_packet_size);
if (packet_length > 2) {
/* Skip FTDI header */
if (frame_decoder_proc(
&loop->fd,
transfer->buffer + offset + 2,
packet_length - 2) < 0) {

loop->state = FATAL_ERROR;
cha->error_str = loop->fd.pd.error_str;
};
}
offset += packet_length;
}

while (loop->state == RUNNING
Expand Down
2 changes: 1 addition & 1 deletion src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int packet_decoder_proc(struct packet_decoder* pd, uint8_t* buf, size_t size) {
}
} break;
case NEED_PACKET_DATA: {
const size_t required_length = pd->packet->size - pd->buf_actual_length;
const size_t required_length = ov_packet_captured_size(pd->packet) - pd->buf_actual_length;
const size_t copy = MIN(required_length, end - buf);

memcpy(pd->packet->data + pd->buf_actual_length, buf, copy);
Expand Down
2 changes: 1 addition & 1 deletion tools/sample/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

static void packet_handler(struct ov_packet* packet, void* data) {
printf("[%02x] Received %d bytes at %" PRId64 ":", packet->flags, packet->size, packet->timestamp);
for (int i = 0; i < packet->size; ++i)
for (int i = 0; i < ov_packet_captured_size(packet); ++i)
printf(" %02x", packet->data[i]);
printf("\n");
}
Expand Down
Loading