AqualinkD/serial_logger.c

236 lines
6.2 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
2018-08-16 19:54:58 +00:00
#define PACKET_MAX 600
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-09-03 13:31:58 +00:00
unsigned char _goodPDAID[] = {0x60, 0x61, 0x62, 0x63};
2018-07-31 23:21:03 +00:00
unsigned char _filter = 0x00;
2018-03-15 20:03:57 +00:00
void intHandler(int dummy) {
_keepRunning = false;
logMessage(LOG_NOTICE, "Stopping!");
}
2018-08-16 19:54:58 +00:00
void advance_cursor() {
static int pos=0;
char cursor[4]={'/','-','\\','|'};
printf("%c\b", cursor[pos]);
fflush(stdout);
pos = (pos+1) % 4;
}
2018-04-18 00:33:17 +00:00
bool canUse(unsigned char ID) {
2018-03-16 15:26:47 +00:00
int i;
2018-09-03 13:31:58 +00:00
for (i = 0; i < 4; 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
}
2018-09-03 13:31:58 +00:00
char* canUseExtended(unsigned char ID) {
int i;
for (i = 0; i < 4; i++) {
if (ID == _goodID[i])
return " <-- can use for Aqualinkd";
}
for (i = 0; i < 4; i++) {
if (ID == _goodPDAID[i])
return " <-- can use for Aqualinkd (PDA mode only)";
}
return "";
}
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)
{
2018-07-31 23:21:03 +00:00
//if (_filter != 0x00 && ID != _filter && packet_buffer[PKT_DEST] != _filter )
// return;
if (_filter != 0x00) {
if ( packet_buffer[PKT_DEST]==0x00 && ID != _filter )
return;
if ( packet_buffer[PKT_DEST]!=0x00 && packet_buffer[PKT_DEST] != _filter )
return;
}
2018-04-18 00:33:17 +00:00
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;
2018-07-15 19:36:19 +00:00
int logPackets = PACKET_MAX;
int logLevel = LOG_NOTICE;
//int logLevel;
2018-04-18 00:33:17 +00:00
//char buffer[256];
2018-07-15 19:36:19 +00:00
//bool idMode = true;
2018-03-15 20:03:57 +00:00
2018-09-03 13:31:58 +00:00
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-07-15 19:36:19 +00:00
if (argc < 2 || access( argv[1], F_OK ) == -1 ) {
fprintf(stderr, "ERROR, first param must be valid serial port, ie:-\n\t%s /dev/ttyUSB0\n\n", argv[0]);
2018-07-31 23:21:03 +00:00
fprintf(stderr, "Optional parameters are -d (debug) & -p <number> (log # packets) & -i <ID> ie:=\n\t%s /dev/ttyUSB0 -d -p 1000 -i 0x08\n\n", argv[0]);
2018-03-15 20:03:57 +00:00
return 1;
}
2018-07-15 19:36:19 +00:00
for (i = 2; i < argc; i++) {
if (strcmp(argv[i], "-d") == 0) {
logLevel = LOG_DEBUG;
} else if (strcmp(argv[i], "-p") == 0 && i+1 < argc) {
logPackets = atoi(argv[i+1]);
2018-07-31 23:21:03 +00:00
} else if (strcmp(argv[i], "-i") == 0 && i+1 < argc) {
unsigned int n;
sscanf(argv[i+1], "0x%2x", &n);
_filter = n;
logLevel = LOG_DEBUG; // no point in filtering on ID if we're not going to print it.
2018-07-15 19:36:19 +00:00
}
}
setLoggingPrms(logLevel, false, false);
2018-03-15 20:03:57 +00:00
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);
2018-08-16 19:54:58 +00:00
logMessage(LOG_NOTICE, "Logging serial information!\n");
if (logLevel < LOG_DEBUG)
printf("Please wait.");
2018-07-15 19:36:19 +00:00
2018-03-15 20:03:57 +00:00
while (_keepRunning == true) {
2018-04-18 00:33:17 +00:00
if (rs_fd < 0) {
2018-07-15 19:36:19 +00:00
logMessage(LOG_ERR, "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-07-15 19:36:19 +00:00
logMessage(LOG_ERR, "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));
2018-07-15 19:36:19 +00:00
if (logLevel > LOG_NOTICE)
printPacket(lastID, packet_buffer, packet_length);
2018-04-18 00:33:17 +00:00
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
2018-08-31 12:49:41 +00:00
if (packet_buffer[PKT_DEST] == DEV_MASTER /*&& packet_buffer[PKT_CMD] == CMD_ACK*/) {
2018-04-18 00:33:17 +00:00
//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++;
}
2018-07-31 23:21:03 +00:00
if (logPackets != 0 && received_packets >= logPackets) {
2018-03-16 15:26:47 +00:00
_keepRunning = false;
2018-03-15 20:03:57 +00:00
}
2018-08-16 19:54:58 +00:00
if (logLevel < LOG_DEBUG)
advance_cursor();
2018-08-25 20:13:33 +00:00
//sleep(1);
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-07-15 19:36:19 +00:00
logMessage(LOG_ERR, "Ran out of storage, some ID's were not captured, please increase SLOG_MAX and recompile\n");
logMessage(LOG_NOTICE, "ID's found\n");
2018-04-18 00:33:17 +00:00
for (i = 0; i <= sindex; i++) {
2018-09-03 13:31:58 +00:00
//logMessage(LOG_NOTICE, "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-07-15 19:36:19 +00:00
logMessage(LOG_NOTICE, "ID 0x%02hhx is %s %s\n", slog[i].ID, (slog[i].inuse == true) ? "in use" : "not used",
2018-09-03 13:31:58 +00:00
(slog[i].inuse == false)?canUseExtended(slog[i].ID):"");
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
}