From 9160c46066aeb5f7e2c14c4720b9039e776e139f Mon Sep 17 00:00:00 2001 From: empathicqubit Date: Sat, 7 Jan 2023 21:12:19 +0100 Subject: [PATCH] Get more bytes at a time --- src/tibridge.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/tibridge.c b/src/tibridge.c index 090f13b..e03b43a 100644 --- a/src/tibridge.c +++ b/src/tibridge.c @@ -73,6 +73,16 @@ void retry_write_calc(uint8_t* send, int sendCount) { } } +void retry_read_calc(uint8_t* recv, int getCount) { + int err; + do { + if((err = ticables_cable_recv(cable_handle, recv, getCount))) { + log(LEVEL_ERROR, "error receiving: %d\n", err); + } + } while(err); + log(LEVEL_TRACE, "%.*s", getCount, recv); +} + void ack() { retry_write_calc((uint8_t*)"+", 1); } @@ -245,27 +255,25 @@ int main(int argc, char *argv[]) { while(true) { uint8_t recv[1023]; - unsigned char current = 0; int recvCount = 0; log(LEVEL_INFO, "<"); log(LEVEL_DEBUG, "RECEIVE PHASE\n"); while(true) { - do { - if((err = ticables_cable_recv(cable_handle, &recv[recvCount], 1))) { - log(LEVEL_ERROR, "error receiving: %d\n", err); + int getCount = 1; + unsigned char *current = NULL; + if(recvCount > 0 && memchr(recv, '$', recvCount)) { + getCount = 3; + } + + retry_read_calc(&recv[recvCount], getCount); + recvCount += getCount; + if((current = memchr(&recv[recvCount-getCount], '#', getCount))) { + getCount = 2 - ((&recv[recvCount]) - current - 1); + if(getCount > 0) { + retry_read_calc(&recv[recvCount], getCount); + recvCount += getCount; } - } while(err); - current = recv[recvCount]; - log(LEVEL_TRACE, "%c", current); - recvCount++; - if(current == '#') { - do { - if((err = ticables_cable_recv(cable_handle, &recv[recvCount], 2))) { - log(LEVEL_ERROR, "error receiving: %d\n", err); - } - } while(err); - recvCount += 2; recv[recvCount] = '\0'; @@ -302,7 +310,8 @@ int main(int argc, char *argv[]) { break; } else if(recvCount == 1) { - if(current == '-') { + current = &recv[recvCount - 1]; + if(*current == '-') { if(!handle_acks) { retry_write_host(recv, recvCount); } @@ -312,7 +321,7 @@ int main(int argc, char *argv[]) { recvCount = 0; break; } - else if(current == '+') { + else if(*current == '+') { continue; } } @@ -325,6 +334,7 @@ int main(int argc, char *argv[]) { log(LEVEL_INFO, ">"); log(LEVEL_DEBUG, "SEND PHASE\n"); while(true) { + uint8_t current; uint8_t send[255]; int sendCount = 0;