#include #include #include #include #include #include #define NO_ERROR_EXIT_CODE 0 #define PARAMETER_ERROR_EXIT_CODE 1 #define PORT_OPEN_ERROR_EXIT_CODE 2 #define WRITE_ERROR_EXIT_CODE 4 #define READ_ERROR_EXIT_CODE 5 int set_interface_attribs (int fUSBPort, int speed) { /* Set up the control structure */ struct termios toptions; /* Get currently set options for the tty */ tcgetattr(fUSBPort, &toptions); /* Set custom options */ /* 9600 baud */ cfsetispeed(&toptions, speed); cfsetospeed(&toptions, speed); /* 8 bits, no parity, no stop bits */ toptions.c_cflag &= ~PARENB; toptions.c_cflag &= ~CSTOPB; toptions.c_cflag &= ~CSIZE; toptions.c_cflag |= CS8; /* no hardware flow control */ toptions.c_cflag &= ~CRTSCTS; /* enable receiver, ignore status lines */ toptions.c_cflag |= CREAD | CLOCAL; /* disable input/output flow control, disable restart chars */ toptions.c_iflag &= ~(IXON | IXOFF | IXANY); /* disable canonical input, disable echo, disable visually erase chars, disable terminal-generated signals */ toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); toptions.c_lflag |= ICANON; /* disable output processing */ toptions.c_oflag &= ~OPOST; /* wait for 1 character to come in before read returns */ toptions.c_cc[VMIN] = 1; // 20 = 2 sec toptions.c_cc[VTIME] = 20; /* commit the options */ tcsetattr(fUSBPort, TCSANOW, &toptions); } int main(int argc, char *argv[]) { if (argc<2) { printf("Proper usage: upsmon [full_port_name]\n"); printf("Exit codes:\n"); printf(" %d=success\n", NO_ERROR_EXIT_CODE); printf(" %d=parameter error\n", PARAMETER_ERROR_EXIT_CODE); printf(" %d=port open fail\n", PORT_OPEN_ERROR_EXIT_CODE); printf(" %d=write error\n", WRITE_ERROR_EXIT_CODE); printf(" %d=read error\n", READ_ERROR_EXIT_CODE); exit(PARAMETER_ERROR_EXIT_CODE); } //FILE* f=fopen("/share/Installers/QNAPUPSMon/UPSMonLog.txt", "a"); //// use \r\n so it'll be windows-friendly //fprintf(f, "starting PID=%d\r\n", getpid()); //fclose(f); char* sUSBPortName = (char*) malloc(50); strcpy(sUSBPortName, argv[1]); printf("MONITORING %s\n", sUSBPortName); int fUSBPort = open(sUSBPortName, O_RDWR | O_NOCTTY); if (fUSBPort < 0) { printf("Error %d opening %s: %s\n", errno, sUSBPortName, strerror(errno)); free(sUSBPortName); exit(PORT_OPEN_ERROR_EXIT_CODE); } free(sUSBPortName); // internally handled: 8n1 (no parity) set_interface_attribs(fUSBPort, B9600); /* Flush anything already in the serial buffer */ tcflush(fUSBPort, TCIFLUSH); int n = write(fUSBPort, "HowAreThings?\n", 14); if (n < 0) { printf("Write error %d.\n", n); exit(WRITE_ERROR_EXIT_CODE); } const int icBufferLen = 100; char* sReadBuffer = (char*) malloc(icBufferLen); n = read(fUSBPort, sReadBuffer, icBufferLen); if (n < 0) { printf("Read error %d.\n", n); exit(READ_ERROR_EXIT_CODE); } printf("Received %d bytes buffer contains [%s]\n", n, sReadBuffer); int bOK = (strstr(sReadBuffer, "OK") != 0); int bShutdown = (strstr(sReadBuffer, "NASShutdown") != 0); free(sReadBuffer); if (bOK) printf("Power is on!\n"); if (bShutdown) { printf("Starting power-off now!\n"); system("/sbin/poweroff"); } close(fUSBPort); exit(NO_ERROR_EXIT_CODE); }