From 6417b59a30e8445e72aa8616e6403542eee30912 Mon Sep 17 00:00:00 2001 From: Lee Ballard Date: Fri, 3 Nov 2023 16:02:40 -0500 Subject: [PATCH] ballle98/AqualinkD#87: loopover_devices :- can't goto PM_EQUIPTMENT_CONTROL menu --- Makefile | 5 +++-- aq_serial.c | 28 ++++++++++++++++++++++++++++ timespec_subtract.c | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f63173b6..d7a518fb 100755 --- a/Makefile +++ b/Makefile @@ -74,7 +74,7 @@ endif # Main source files SRCS = aqualinkd.c utils.c config.c aq_serial.c aq_panel.c aq_programmer.c net_services.c json_messages.c rs_msg_utils.c\ devices_jandy.c packetLogger.c devices_pentair.c color_lights.c serialadapter.c aq_timer.c aq_scheduler.c web_config.c\ - serial_logger.c mongoose.c + serial_logger.c mongoose.c timespec_subtract.c AQ_FLAGS = @@ -128,7 +128,8 @@ DBG_CFLAGS = $(DBGFLAGS) $(AQ_FLAGS) $(MGFLAGS) # Other sources. DBG_SRC = $(SRCS) debug_timer.c -SL_SRC = serial_logger.c aq_serial.c utils.c packetLogger.c rs_msg_utils.c +SL_SRC = serial_logger.c aq_serial.c utils.c packetLogger.c rs_msg_utils.c \ + timespec_subtract.c # Build durectories OBJ_DIR := ./build diff --git a/aq_serial.c b/aq_serial.c index 66b4f276..5edc42f1 100644 --- a/aq_serial.c +++ b/aq_serial.c @@ -30,6 +30,7 @@ #include "utils.h" #include "config.h" #include "packetLogger.h" +#include "timespec_subtract.h" /* Notes for serial usb speed @@ -52,6 +53,8 @@ int _blocking_fds = -1; static struct termios _oldtio; +static struct timespec last_serial_read_time; + void send_packet(int fd, unsigned char *packet, int length); //unsigned char getProtocolType(unsigned char* packet); @@ -725,6 +728,25 @@ void send_packet(int fd, unsigned char *packet_buffer, int length) void send_packet(int fd, unsigned char *packet, int length) { #endif + struct timespec elapsed_time; + struct timespec now; + struct timespec min_frame_wait_time = {0, 4000000}; // 4 milliseconds + struct timespec frame_wait_time; + struct timespec remainder_time; + + // No spec for aqualink protocol but looking at similar RS-485 protocol + // https://www.tascam.eu/en/docs/MX-8A_RS-485_protocol.pdf there is a + // min frame to frame time of 4 milliseconds and there seems to be issues + // with RPi 4 sending a response too quickly + do { + clock_gettime(CLOCK_REALTIME, &now); + timespec_subtract(&elapsed_time, &now, &last_serial_read_time); + if (timespec_subtract(&frame_wait_time, &min_frame_wait_time, &elapsed_time)) { + break; + } + } while (nanosleep(&frame_wait_time, &remainder_time) != 0); + + clock_gettime(CLOCK_REALTIME, &now); if (_blocking_mode) { //int nwrite = write(fd, packet, length); @@ -783,6 +805,11 @@ void send_packet(int fd, unsigned char *packet, int length) }*/ tcdrain(fd); // Make sure buffer has been sent. + timespec_subtract(&elapsed_time, &now, &last_serial_read_time); + LOG(RSSD_LOG, LOG_DEBUG, "Time from recv to %s send is %ld.%09ld sec\n", + (_blocking_mode?"blocking":"non-blocking"), elapsed_time.tv_sec, + elapsed_time.tv_nsec); + } void _send_ack(int fd, unsigned char ack_type, unsigned char command) @@ -1032,6 +1059,7 @@ int get_packet(int fd, unsigned char* packet) return AQSERR_2SMALL; } + clock_gettime(CLOCK_REALTIME, &last_serial_read_time); LOG(RSSD_LOG,LOG_DEBUG_SERIAL, "Serial read %d bytes\n",index); if (_aqconfig_.log_protocol_packets || getLogLevel(RSSD_LOG) >= LOG_DEBUG_SERIAL) logPacketRead(packet, index); diff --git a/timespec_subtract.c b/timespec_subtract.c index bff19179..361a83bb 100644 --- a/timespec_subtract.c +++ b/timespec_subtract.c @@ -8,7 +8,7 @@ */ -/* Based on https://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html +/* Based on https://www.gnu.org/software/libc/manual/html_node/Calculating-Elapsed-Time.html Subtract the struct timespec values X and Y, storing the result in RESULT. Return 1 if the difference is negative, otherwise 0. */