From 54cb98634e77693d6219cd3cbb6a90caba0af656 Mon Sep 17 00:00:00 2001 From: Gerolf Ziegenhain Date: Sat, 24 Sep 2016 16:55:36 +0200 Subject: [PATCH] sort functions --- src/Makefile | 2 +- src/cwprotocol.c | 36 ----------------- src/cwprotocol.cpp | 84 ++++++++++++++++++++++++++++++++++++++++ src/cwprotocol.h | 17 ++++++++ src/{irmc.c => irmc.cpp} | 44 +-------------------- 5 files changed, 103 insertions(+), 80 deletions(-) delete mode 100644 src/cwprotocol.c create mode 100644 src/cwprotocol.cpp rename src/{irmc.c => irmc.cpp} (89%) diff --git a/src/Makefile b/src/Makefile index f8bd6b4..d674a34 100644 --- a/src/Makefile +++ b/src/Makefile @@ -13,7 +13,7 @@ options: @echo "CC = ${CC}" @echo "INSTALLDIR = ${INSTALLDIR}" -.c.o: +.cpp.o: @echo CC $< @${CC} -c ${CFLAGS} $< diff --git a/src/cwprotocol.c b/src/cwprotocol.c deleted file mode 100644 index d6daf96..0000000 --- a/src/cwprotocol.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include "cwprotocol.h" - -int prepare_id (struct data_packet_format *id_packet, char *id) -{ - id_packet->command = DAT; - id_packet->length = SIZE_DATA_PACKET_PAYLOAD; - snprintf(id_packet->id, SIZE_ID, id, "%s"); - id_packet->sequence = 0; - id_packet->n = 0; - snprintf(id_packet->status, SIZE_ID, INTERFACE_VERSION); - id_packet->a21 = 1; /* These magic numbers was provided by Les Kerr */ - id_packet->a22 = 755; - id_packet->a23 = 65535; - - return 0; -} - - -int prepare_tx (struct data_packet_format *tx_packet, char *id) -{ - int i; - - tx_packet->command = DAT; - tx_packet->length = SIZE_DATA_PACKET_PAYLOAD; - snprintf(tx_packet->id, SIZE_ID, id, "%s"); - tx_packet->sequence = 0; - tx_packet->n = 0; - for(i = 1; i < 51; i++)tx_packet->code[i] = 0; - tx_packet->a21 = 0; /* These magic numbers was provided by Les Kerr */ - tx_packet->a22 = 755; - tx_packet->a23 = 16777215; - snprintf(tx_packet->status, SIZE_STATUS, "?"); - - return 0; -} diff --git a/src/cwprotocol.cpp b/src/cwprotocol.cpp new file mode 100644 index 0000000..ffc8e27 --- /dev/null +++ b/src/cwprotocol.cpp @@ -0,0 +1,84 @@ +#include +#include +#include "cwprotocol.h" + +/* Global variables */ +struct command_packet_format connect_packet = {CON, DEFAULT_CHANNEL}; +struct command_packet_format disconnect_packet = {DIS, 0}; +struct data_packet_format id_packet; +struct data_packet_format rx_data_packet; +struct data_packet_format tx_data_packet; + +int tx_sequence = 0, rx_sequence; +int fd_socket; + +int prepare_id (struct data_packet_format *id_packet, char *id) +{ + id_packet->command = DAT; + id_packet->length = SIZE_DATA_PACKET_PAYLOAD; + snprintf(id_packet->id, SIZE_ID, id, "%s"); + id_packet->sequence = 0; + id_packet->n = 0; + snprintf(id_packet->status, SIZE_ID, INTERFACE_VERSION); + id_packet->a21 = 1; /* These magic numbers was provided by Les Kerr */ + id_packet->a22 = 755; + id_packet->a23 = 65535; + + return 0; +} + + +int prepare_tx (struct data_packet_format *tx_packet, char *id) +{ + int i; + + tx_packet->command = DAT; + tx_packet->length = SIZE_DATA_PACKET_PAYLOAD; + snprintf(tx_packet->id, SIZE_ID, id, "%s"); + tx_packet->sequence = 0; + tx_packet->n = 0; + for(i = 1; i < 51; i++)tx_packet->code[i] = 0; + tx_packet->a21 = 0; /* These magic numbers was provided by Les Kerr */ + tx_packet->a22 = 755; + tx_packet->a23 = 16777215; + snprintf(tx_packet->status, SIZE_STATUS, "?"); + + return 0; +} + + +// connect to server and send my id. +void identifyclient(void) +{ + tx_sequence++; + id_packet.sequence = tx_sequence; + send(fd_socket, &connect_packet, SIZE_COMMAND_PACKET, 0); + send(fd_socket, &id_packet, SIZE_DATA_PACKET, 0); +} + +int send_latch (void) +{ + int i; + tx_sequence++; + tx_data_packet.sequence = tx_sequence; + tx_data_packet.code[0] = -1; + tx_data_packet.code[1] = 1; + tx_data_packet.n = 2; + for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0); + tx_data_packet.n = 0; + return 0; +} + +int send_unlatch (void) +{ + int i; + tx_sequence++; + tx_data_packet.sequence = tx_sequence; + tx_data_packet.code[0] = -1; + tx_data_packet.code[1] = 2; + tx_data_packet.n = 2; + for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0); + tx_data_packet.n = 0; + return 0; +} + diff --git a/src/cwprotocol.h b/src/cwprotocol.h index e1f76b1..977a2f4 100644 --- a/src/cwprotocol.h +++ b/src/cwprotocol.h @@ -40,6 +40,23 @@ struct data_packet_format{ // Define the packets used #define DEFAULT_CHANNEL 103 +/* Define functions provided by cwprotocol */ int prepare_id (struct data_packet_format *id_packet, char *id); int prepare_tx (struct data_packet_format *tx_packet, char *id); +void identifyclient (void); +int send_latch (void); +int send_unlatch (void); + +/* Define external struct for global variables */ +extern struct command_packet_format connect_packet; +extern struct command_packet_format disconnect_packet; +extern struct data_packet_format id_packet; +extern struct data_packet_format rx_data_packet; +extern struct data_packet_format tx_data_packet; + +extern int tx_sequence, rx_sequence; + +extern int fd_socket; + + diff --git a/src/irmc.c b/src/irmc.cpp similarity index 89% rename from src/irmc.c rename to src/irmc.cpp index bbfd710..24877cc 100644 --- a/src/irmc.c +++ b/src/irmc.cpp @@ -35,21 +35,13 @@ #include #endif - //#define DEBUG 1 #define MAXDATASIZE 1024 // max number of bytes we can get at once #include "cwprotocol.h" -struct command_packet_format connect_packet = {CON, DEFAULT_CHANNEL}; -struct command_packet_format disconnect_packet = {DIS, 0}; -struct data_packet_format id_packet; -struct data_packet_format rx_data_packet; -struct data_packet_format tx_data_packet; - -int serial_status = 0, fd_serial, fd_socket, numbytes; -int tx_sequence = 0, rx_sequence; +int serial_status = 0, fd_serial, numbytes; double tx_timeout = 0; long tx_timer = 0; @@ -84,7 +76,6 @@ void current_utc_time(struct timespec *ts) { #else clock_gettime(CLOCK_REALTIME, ts); #endif - } /* a better clock() in milliseconds */ @@ -125,14 +116,6 @@ void *get_in_addr(struct sockaddr *sa) return &(((struct sockaddr_in6*)sa)->sin6_addr); } -// connect to server and send my id. -void identifyclient(void) -{ - tx_sequence++; - id_packet.sequence = tx_sequence; - send(fd_socket, &connect_packet, SIZE_COMMAND_PACKET, 0); - send(fd_socket, &id_packet, SIZE_DATA_PACKET, 0); -} // disconnect from the server void inthandler(int sig) @@ -176,31 +159,6 @@ void txloop (void) } } -int send_latch (void) -{ - int i; - tx_sequence++; - tx_data_packet.sequence = tx_sequence; - tx_data_packet.code[0] = -1; - tx_data_packet.code[1] = 1; - tx_data_packet.n = 2; - for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0); - tx_data_packet.n = 0; - return 0; -} - -int send_unlatch (void) -{ - int i; - tx_sequence++; - tx_data_packet.sequence = tx_sequence; - tx_data_packet.code[0] = -1; - tx_data_packet.code[1] = 2; - tx_data_packet.n = 2; - for(i = 0; i < 5; i++) send(fd_socket, &tx_data_packet, SIZE_DATA_PACKET, 0); - tx_data_packet.n = 0; - return 0; -} void message(int msg) {