AqualinkD/serial_logger.c

176 lines
4.4 KiB
C
Raw Normal View History

2018-03-15 20:03:57 +00:00
2018-04-18 00:33:17 +00:00
#include <signal.h>
2018-03-15 20:03:57 +00:00
#include <stdarg.h>
#include <stdbool.h>
2018-04-18 00:33:17 +00:00
#include <stdio.h>
#include <stdlib.h>
2018-03-16 15:26:47 +00:00
#include <string.h>
2018-04-18 00:33:17 +00:00
#include <unistd.h>
2018-03-15 20:03:57 +00:00
#include "aq_serial.h"
#include "utils.h"
2018-03-16 17:18:15 +00:00
#define SLOG_MAX 80
#define PACKET_MAX 10000
2018-03-15 20:03:57 +00:00
2018-04-18 00:33:17 +00:00
/*
typedef enum used {
yes,
no,
unknown
} used;
*/
2018-03-15 20:03:57 +00:00
typedef struct serial_id_log {
unsigned char ID;
bool inuse;
} serial_id_log;
2018-04-18 00:33:17 +00:00
bool _keepRunning = true;
2018-03-15 20:03:57 +00:00
2018-04-18 00:33:17 +00:00
unsigned char _goodID[] = {0x0a, 0x0b, 0x08, 0x09};
2018-03-15 20:03:57 +00:00
void intHandler(int dummy) {
_keepRunning = false;
logMessage(LOG_NOTICE, "Stopping!");
}
2018-04-18 00:33:17 +00:00
bool canUse(unsigned char ID) {
2018-03-16 15:26:47 +00:00
int i;
2018-04-18 00:33:17 +00:00
for (i = 0; i < strlen((char *)_goodID); i++) {
2018-03-16 15:26:47 +00:00
if (ID == _goodID[i])
return true;
}
return false;
2018-04-18 00:33:17 +00:00
}
void printHex(char *pk, int length)
{
int i=0;
for (i=0;i<length;i++)
{
printf("0x%02hhx|",pk[i]);
}
}
void printPacket(unsigned char ID, unsigned char *packet_buffer, int packet_length)
{
if (packet_buffer[PKT_DEST] != 0x00)
printf("\n");
printf("%4.4s 0x%02hhx of type %8.8s", (packet_buffer[PKT_DEST]==0x00?"From":"To"), (packet_buffer[PKT_DEST]==0x00?ID:packet_buffer[PKT_DEST]), get_packet_type(packet_buffer, packet_length));
printf(" | HEX: ");
printHex((char *)packet_buffer, packet_length);
if (packet_buffer[PKT_CMD] == CMD_MSG || packet_buffer[PKT_CMD] == CMD_MSG_LONG) {
printf(" Message : ");
//fwrite(packet_buffer + 4, 1, AQ_MSGLEN+1, stdout);
fwrite(packet_buffer + 4, 1, packet_length-7, stdout);
}
//if (packet_buffer[PKT_DEST]==0x00)
// printf("\n\n");
//else
printf("\n");
}
2018-03-15 20:03:57 +00:00
int main(int argc, char *argv[]) {
int rs_fd;
int packet_length;
2018-04-18 00:33:17 +00:00
unsigned char packet_buffer[AQ_MAXPKTLEN];
2018-03-15 20:03:57 +00:00
unsigned char lastID;
2018-04-18 00:33:17 +00:00
int i = 0;
2018-03-15 20:03:57 +00:00
bool found;
serial_id_log slog[SLOG_MAX];
int sindex = 0;
2018-04-18 00:33:17 +00:00
int received_packets = 0;
//char buffer[256];
bool idMode = true;
2018-03-15 20:03:57 +00:00
if (getuid() != 0) {
fprintf(stderr, "ERROR %s Can only be run as root\n", argv[0]);
return EXIT_FAILURE;
}
2018-04-18 00:33:17 +00:00
//if (idMode)
setLoggingPrms(LOG_DEBUG, false, false);
//else
// setLoggingPrms(LOG_DEBUG_SERIAL, false, false);
2018-03-15 20:03:57 +00:00
if (argc < 2) {
2018-04-18 00:33:17 +00:00
logMessage(LOG_DEBUG, "ERROR, first param must be serial port, ie:-\n %s /dev/ttyUSB0\n\n", argv[0]);
2018-03-15 20:03:57 +00:00
return 1;
}
rs_fd = init_serial_port(argv[1]);
2018-04-18 00:33:17 +00:00
2018-03-15 20:03:57 +00:00
signal(SIGINT, intHandler);
signal(SIGTERM, intHandler);
while (_keepRunning == true) {
2018-04-18 00:33:17 +00:00
if (rs_fd < 0) {
logMessage(LOG_DEBUG, "ERROR, serial port disconnect\n");
2018-03-15 20:03:57 +00:00
}
packet_length = get_packet(rs_fd, packet_buffer);
2018-04-18 00:33:17 +00:00
2018-03-15 20:03:57 +00:00
if (packet_length == -1) {
// Unrecoverable read error. Force an attempt to reconnect.
2018-04-18 00:33:17 +00:00
logMessage(LOG_DEBUG, "ERROR, on serial port\n");
2018-03-15 20:03:57 +00:00
_keepRunning = false;
} else if (packet_length == 0) {
// Nothing read
} else if (packet_length > 0) {
2018-04-18 00:33:17 +00:00
//logMessage(LOG_DEBUG_SERIAL, "Received Packet for ID 0x%02hhx of type %s\n", packet_buffer[PKT_DEST], get_packet_type(packet_buffer, packet_length));
printPacket(lastID, packet_buffer, packet_length);
if (packet_buffer[PKT_DEST] != DEV_MASTER) {
found = false;
for (i = 0; i <= sindex; i++) {
if (slog[i].ID == packet_buffer[PKT_DEST]) {
found = true;
break;
}
}
if (found != true && sindex < SLOG_MAX) {
slog[sindex].ID = packet_buffer[PKT_DEST];
slog[sindex].inuse = false;
sindex++;
2018-03-15 20:03:57 +00:00
}
}
2018-04-18 00:33:17 +00:00
if (packet_buffer[PKT_DEST] == DEV_MASTER && packet_buffer[PKT_CMD] == CMD_ACK) {
//logMessage(LOG_DEBUG_SERIAL, "ID is in use 0x%02hhx %x\n", lastID, lastID);
for (i = 0; i <= sindex; i++) {
if (slog[i].ID == lastID) {
slog[i].inuse = true;
break;
}
2018-03-15 20:03:57 +00:00
}
}
2018-04-18 00:33:17 +00:00
2018-03-15 20:03:57 +00:00
lastID = packet_buffer[PKT_DEST];
2018-03-16 15:26:47 +00:00
received_packets++;
}
if (received_packets >= PACKET_MAX) {
_keepRunning = false;
2018-03-15 20:03:57 +00:00
}
}
2018-04-18 00:33:17 +00:00
logMessage(LOG_DEBUG, "\n");
2018-03-16 14:32:54 +00:00
if (sindex >= SLOG_MAX)
2018-04-18 00:33:17 +00:00
logMessage(LOG_DEBUG, "Ran out of storage, some ID's were not captured, please increase SLOG_MAX and recompile\n");
logMessage(LOG_DEBUG, "ID's found\n");
for (i = 0; i <= sindex; i++) {
logMessage(LOG_DEBUG, "ID 0x%02hhx is %s %s\n", slog[i].ID, slog[i].inuse == true ? "in use" : "not used",
slog[i].inuse == false && canUse(slog[i].ID) == true ? " <-- can use for Aqualinkd" : "");
2018-03-15 20:03:57 +00:00
}
2018-03-16 15:26:47 +00:00
2018-03-15 20:03:57 +00:00
return 0;
2018-04-18 00:33:17 +00:00
}