#include #include #include #include #include #include #include #include "aq_serial.h" #include "utils.h" #define SLOG_MAX 80 #define PACKET_MAX 600 /* typedef enum used { yes, no, unknown } used; */ typedef struct serial_id_log { unsigned char ID; bool inuse; } serial_id_log; bool _keepRunning = true; unsigned char _goodID[] = {0x0a, 0x0b, 0x08, 0x09}; unsigned char _filter = 0x00; void intHandler(int dummy) { _keepRunning = false; logMessage(LOG_NOTICE, "Stopping!"); } void advance_cursor() { static int pos=0; char cursor[4]={'/','-','\\','|'}; printf("%c\b", cursor[pos]); fflush(stdout); pos = (pos+1) % 4; } bool canUse(unsigned char ID) { int i; for (i = 0; i < strlen((char *)_goodID); i++) { if (ID == _goodID[i]) return true; } return false; } void printHex(char *pk, int length) { int i=0; for (i=0;i (log # packets) & -i ie:=\n\t%s /dev/ttyUSB0 -d -p 1000 -i 0x08\n\n", argv[0]); return 1; } 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]); } 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. } } setLoggingPrms(logLevel, false, false); rs_fd = init_serial_port(argv[1]); signal(SIGINT, intHandler); signal(SIGTERM, intHandler); logMessage(LOG_NOTICE, "Logging serial information!\n"); if (logLevel < LOG_DEBUG) printf("Please wait."); while (_keepRunning == true) { if (rs_fd < 0) { logMessage(LOG_ERR, "ERROR, serial port disconnect\n"); } packet_length = get_packet(rs_fd, packet_buffer); if (packet_length == -1) { // Unrecoverable read error. Force an attempt to reconnect. logMessage(LOG_ERR, "ERROR, on serial port\n"); _keepRunning = false; } else if (packet_length == 0) { // Nothing read } else if (packet_length > 0) { //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)); if (logLevel > LOG_NOTICE) 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++; } } 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; } } } lastID = packet_buffer[PKT_DEST]; received_packets++; } if (logPackets != 0 && received_packets >= logPackets) { _keepRunning = false; } if (logLevel < LOG_DEBUG) advance_cursor(); //sleep(1); } logMessage(LOG_DEBUG, "\n"); if (sindex >= SLOG_MAX) 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"); for (i = 0; i <= sindex; i++) { 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" : ""); } return 0; }