diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-05-29 17:01:03 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-05-29 17:01:03 +0200 |
commit | 59802547c336d61aa7e950414a3f44180e211974 (patch) | |
tree | cf7107b9085ce3c93e98ec874d25e04ad2362a4c | |
parent | 3b2c2cf6b2af9e76b343a5a8fc8e9245f240690d (diff) |
client ping working
-rw-r--r-- | client/commands.c | 40 | ||||
-rw-r--r-- | client/commands.h | 8 | ||||
-rw-r--r-- | client/main.c | 2 | ||||
-rw-r--r-- | client/main.h | 1 | ||||
-rw-r--r-- | client/serial.c | 7 | ||||
-rw-r--r-- | client/setup.c | 4 | ||||
-rw-r--r-- | client/time.c | 3 | ||||
-rw-r--r-- | client/time.h | 11 | ||||
-rw-r--r-- | client/time_linux.c | 21 |
9 files changed, 95 insertions, 2 deletions
diff --git a/client/commands.c b/client/commands.c new file mode 100644 index 0000000..67025f4 --- /dev/null +++ b/client/commands.c @@ -0,0 +1,40 @@ +#include <stdlib.h> + +#include "commands.h" +#include "time.h" +#include "main.h" +#include "../shared/bin.h" +#include "../shared/protocol.h" + +void w2_send_bin(w2_s_bin *data) { + w2_serial_write("\xff", 1); + for (uint8_t i = 0; i < data->bytes; i++) { + uint8_t byte = data->data[i]; + byte == 0xff ? w2_serial_write("\xff\xff", 2) : w2_serial_write((char *)&byte, 1); + } +} + +w2_s_bin* w2_send_info() { + size_t msg_size = sizeof(w2_s_cmd_info_rx); + w2_s_cmd_info_rx *msg = malloc(msg_size); + msg->opcode = W2_CMD_INFO | W2_CMDDIR_RX; + w2_s_bin *msg_bin = w2_bin_s_alloc(msg_size, (uint8_t *)msg); + w2_send_bin(msg_bin); + free(msg); + free(msg_bin); +} + +w2_s_bin* w2_send_ping() { + g_w2_state.ping_id = (uint8_t) rand(); + size_t msg_size = sizeof(w2_s_cmd_ping_rx); + w2_s_cmd_ping_rx *msg = malloc(msg_size); + msg->opcode = W2_CMD_PING | W2_CMDDIR_RX; + msg->id = g_w2_state.ping_id; + w2_s_bin *msg_bin = w2_bin_s_alloc(msg_size, (uint8_t *)msg); + w2_send_bin(msg_bin); + free(msg); + free(msg_bin); + + w2_timer_start(W2_TIMER_PING); +} + diff --git a/client/commands.h b/client/commands.h new file mode 100644 index 0000000..aa4b964 --- /dev/null +++ b/client/commands.h @@ -0,0 +1,8 @@ +#pragma once + +#include "serial.h" +#include "../shared/bin.h" + +w2_s_bin* w2_send_info(); +w2_s_bin* w2_send_ping(); + diff --git a/client/main.c b/client/main.c index 1060f7d..530af2a 100644 --- a/client/main.c +++ b/client/main.c @@ -4,6 +4,8 @@ #include "serial.h" #include "ui.h" +w2_s_client_state g_w2_state; + int main(int argc, char **argv) { w2_client_setup(argc, argv); diff --git a/client/main.h b/client/main.h index 7bb2173..f81e8ce 100644 --- a/client/main.h +++ b/client/main.h @@ -4,6 +4,7 @@ typedef struct { unsigned int ping; + uint8_t ping_id; w2_s_cmd_info_tx info; w2_s_cmd_sens_tx io; } w2_s_client_state; diff --git a/client/serial.c b/client/serial.c index 6f667a8..ec523cc 100644 --- a/client/serial.c +++ b/client/serial.c @@ -1,15 +1,18 @@ #include "serial.h" +#include "time.h" +#include "main.h" #include "../shared/protocol.h" #include "../shared/serial_parse.h" void w2_serial_main() { int temp; while ((temp = w2_serial_read()) != -1) w2_serial_parse(temp); - w2_serial_write("\xff\x14", 2); } void w2_cmd_ping_tx(w2_s_bin *data) { - printf("w2_cmd_ping_tx()\n"); + // TODO: check ping id + g_w2_state.ping = w2_timer_end(W2_TIMER_PING); + printf("ping measured, %ims\n", g_w2_state.ping); } void w2_cmd_expt_tx(w2_s_bin *data) { printf("w2_cmd_expt_tx()\n"); diff --git a/client/setup.c b/client/setup.c index d7314e8..35b761c 100644 --- a/client/setup.c +++ b/client/setup.c @@ -3,6 +3,7 @@ #include "serial.h" #include "setup.h" +#include "commands.h" #include "../shared/bin.h" #include "../shared/protocol.h" @@ -24,6 +25,9 @@ void w2_client_setup(int argc, char** argv) { w2_cmd_setup_handlers(); + w2_send_info(); + w2_send_ping(); + // check endianness g_w2_endianness = *_ptest; } diff --git a/client/time.c b/client/time.c new file mode 100644 index 0000000..d48be64 --- /dev/null +++ b/client/time.c @@ -0,0 +1,3 @@ +#include "time.h" + +unsigned long g_w2_client_timers[W2_CLIENT_TIMER_COUNT] = {0}; diff --git a/client/time.h b/client/time.h new file mode 100644 index 0000000..75b1f70 --- /dev/null +++ b/client/time.h @@ -0,0 +1,11 @@ +#pragma once + +/** amount of parallel timers */ +#define W2_CLIENT_TIMER_COUNT (4) +extern unsigned long g_w2_client_timers[W2_CLIENT_TIMER_COUNT]; +typedef enum { + W2_TIMER_PING = 0 +} w2_e_client_timers; + +void w2_timer_start(w2_e_client_timers label); +unsigned long w2_timer_end(w2_e_client_timers label); diff --git a/client/time_linux.c b/client/time_linux.c new file mode 100644 index 0000000..5b72838 --- /dev/null +++ b/client/time_linux.c @@ -0,0 +1,21 @@ +#ifdef W2_HOST_LINUX + +#include <time.h> + +#include "time.h" + +unsigned long w2_get_time() { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return ((now.tv_sec * 1000) + (now.tv_nsec / 1000000)); +} + +void w2_timer_start(w2_e_client_timers label) { + g_w2_client_timers[label] = w2_get_time(); +} + +unsigned long w2_timer_end(w2_e_client_timers label) { + return w2_get_time() - g_w2_client_timers[label]; +} + +#endif |