From 7ba8d716ce6689fc402d4774625aff71bc04253c Mon Sep 17 00:00:00 2001 From: Beluzsar Peter Date: Thu, 8 Oct 2026 14:58:20 +0200 Subject: [PATCH] make some fixes, refactor some variables, finish most of the packet code --- src/anou.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++---------- src/anou.h | 22 +++++++++++------ 2 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/anou.cpp b/src/anou.cpp index dc3be6f..dd3a8a2 100644 --- a/src/anou.cpp +++ b/src/anou.cpp @@ -1,5 +1,5 @@ #include "anou.h" - +#include #define TYPE_DATA 0b0 #define TYPE_START 0b1 @@ -8,45 +8,90 @@ #define ADDR_BROADCAST 0b111111 #define ADDR_HOST 0b0 + +#define MODE_SETVAR6 0x01 +#define MODE_SETVAR12 0x02 +#define MODE_RESPONSE 0x3d +#define ACK 0x03 +using namespace std; + class ANOUServer { - char* portname; + string portname; char buffer[1024]; - int* addresses; + char* addresses; public: - int ConfigureSerialPort() + char ConfigureSerialPort() { } - int StartListener() + char StartListener() { } - int WriteSerialPort(char* message) + char WriteSerialPort(char* message) { } - char* AssemblePacket(int src_addr = ADDR_HOST, int dst_addr, char** message) + char* AssemblePacket(char* message, char dst_addr, char src_addr = ADDR_HOST) { - + char message_size = sizeof(message)/sizeof(char); + char full_message[message_size + 2]; + + full_message[0] = TYPE_START + dst_addr << 2; + for (char i = 0; i < message_size; i++) full_message[i+1] = message[i]; + full_message[message_size+1] = TYPE_STOP + dst_addr << 2; } - char* AssembleMessage(int type, char* mode, char* value) + char* Response(char address) { + char message[2] { + TYPE_DATA + MODE_RESPONSE<<2, + TYPE_DATA + address<<2 + }; + return message; } - int SHAW(bool force_readdress) + char* SetVar6(char var_index, int var_value) + { + char message[3] = { + TYPE_DATA + MODE_SETVAR6<<2, + TYPE_DATA + var_index<<2, + TYPE_DATA + var_value<<2 + }; + + return message; + } + + char* SetVar12(char var_index, int var_value) + { + char message[4] = { + TYPE_DATA + MODE_SETVAR6<<2, + TYPE_DATA + var_index<<2, + TYPE_DATA + (var_value>>6)<<2, + TYPE_DATA + var_value<<2<<6 + }; + + return message; + } + + char SHAW(char address, bool force_readdress = false) { //Stateful Host Addressing Waterfall protocol //sends out an addressing message with the address it wants to //assign. This will be assigned to the first client without an address + char message[2] = { + TYPE_SHAW + address << 2, + 0b0 + }; + if (force_readdress) { //force readdressing ignores, whether the clients have @@ -55,11 +100,10 @@ public: //a force readdress SHAW packet downstream with addr 2 and force_readdress //enabled - WriteSerialPort(AssemblePacket()) - + message[1] = 0b1; } - + WriteSerialPort(AssemblePacket(message, ADDR_BROADCAST)); } }; \ No newline at end of file diff --git a/src/anou.h b/src/anou.h index c3dbb9f..90ef5dd 100644 --- a/src/anou.h +++ b/src/anou.h @@ -1,19 +1,25 @@ #ifndef ANOU_H #define ANOU_H +#include +using namespace std; + class ANOUServer { - char* portname; //name of the serial port (eg.: /dev/ttyUSB1) + string portname; //name of the serial port eg.: /dev/ttyUSB1) char buffer[1024]; //message buffer - int* addresses; //known client addresses + char* addresses; //known client addresses public: - int ConfigureSerialPort(); //configuring the serial port - int StartListener(); //start listening on the serial port - int WriteSerialPort(char* message); //write the message to the serial port - char* AssemblePacket(int src_addr, int dst_addr, char** message) - char* AssembleMessage(int type, char* mode, char* value); //append the value and the message type to the message mode - int SHAW(bool force_readdress); //Stateful Host Addressing Waterfall protocol + char ConfigureSerialPort(); //configuring the serial port + char StartListener(); //start listening on the serial port + char WriteSerialPort(char* message); //write the message to the serial port + char* AssemblePacket(char src_addr, char dst_addr, char* message); //assemble the full packet, with the source and destination address too + char SHAW(bool force_readdress); //Stateful Host Addressing Waterfall protocol + + char* SetVar6(char var_index, int var_value); //used to create a 6 bit long variable altering message + char* SetVar12(char var_index, int var_value); //used to create a 12 bit long variable altering message + char* Response(char address); //specifies an address the client should make a response to }; #endif \ No newline at end of file