#include #include #include #include #include #include #include #include #include #include #include #include #define parallel_port 0x378 /* parallel port address in the Barracuda */ #define bypassbit 3 /* bit 3, pin5 */ #define redbit 5 /* bit 5, pin7 */ #define yellowbit 6 /* bit 6, pin8 */ #define greenbit 7 /* bit 7, pin9 */ /* Version 1.0 */ /* Read a byte from port */ static inline int port_in (unsigned short port) { static FILE *port_access_handle = NULL; if (port_access_handle || (port_access_handle = fopen("/dev/io", "ro")) != NULL) { return inb(port); /* Success */ } else { printf("open port for reading fail\n"); return (-1); /* Failure */ }; return -1; } /* Write a byte 'val' to port */ static inline void port_out (unsigned short port, unsigned char val) { static FILE *port_access_handle = NULL; if (port_access_handle || (port_access_handle = fopen("/dev/io", "rw")) != NULL) { outb(port, val); /* Success */ //printf("Write success!\n"); } else { printf("open port for writing fail\n"); }; } /* Function to set a bit in the parallel port*/ void set_bit(int bit_num){ int bit_val =0; bit_val = port_in(parallel_port); //printf("Old value %x\n", bit_val); bit_val |= 1 << bit_num; //printf("New value %x\n", bit_val); port_out(parallel_port, bit_val); } /* Function to clear a bit in the parallel port*/ void clear_bit(int bit_num){ int bit_val =0; bit_val = port_in(parallel_port); //printf("Old value %x\n", bit_val); bit_val &= ~(1 << bit_num); //printf("New value %x\n", bit_val); port_out(parallel_port, bit_val); } /* Function to check a bit in the parallel port*/ int check_bit(int bit_num){ int old_val =0; int bit =0; old_val = port_in(parallel_port); bit = !!(old_val & (1 << bit_num)); //printf("Value %x\n", old_val); //printf("Bit %d\n", bit); return(bit); } /* Function to display correct program usage */ void usage(void) { printf("BCHW Version 1.0 14/12/2014 stephenw10\n"); printf("This program is intended to control the hardware of some models \n"); printf("of Barracuda web filter. The LEDs and LAN-Bypass are controlled\n"); printf("directly by pins in the parallel port.\n"); printf("BCHW can accept two arguments:\n"); printf(" green, yellow, red or bypass followed by on or off.\n"); printf(" status will report the current setting of the port bits\n"); } /* main function Sets the output pins on the parallel port to control various hardware */ int main(int argc, char *argv[]) { int bit_status; if (argc ==1) /*Check that correct number of arguments have been given */ {usage(); return 0; } if (strcmp(argv[1],"red")==0) { if (argc==2) /* Must provide a value */ { usage(); return 0; } if (strcmp(argv[2],"on")==0) { clear_bit(redbit); /* LED drive is inverted */ } else if (strcmp(argv[2],"off")==0) { set_bit(redbit); } else { usage(); return 0; } } else if (strcmp(argv[1],"yellow")==0) { if (argc==2) /* Must provide a value */ { usage(); return 0; } if (strcmp(argv[2],"on")==0) { clear_bit(yellowbit); /* LED drive is inverted */ } else if (strcmp(argv[2],"off")==0) { set_bit(yellowbit); } else { usage(); return 0; } } else if (strcmp(argv[1],"green")==0) { if (argc==2) /* Must provide a value */ { usage(); return 0; } if (strcmp(argv[2],"on")==0) { clear_bit(greenbit); /* LED drive is inverted */ } else if (strcmp(argv[2],"off")==0) { set_bit(greenbit); } else { usage(); return 0; } } else if (strcmp(argv[1],"bypass")==0) { if (argc==2) /* Must provide a value */ { usage(); return 0; } if (strcmp(argv[2],"on")==0) { clear_bit(bypassbit); /* Powering the relays disables the bypass */ } else if (strcmp(argv[2],"off")==0) { set_bit(bypassbit); } else { usage(); return 0; } } else if (strcmp(argv[1],"status")==0) { bit_status = check_bit(redbit); printf("Red bit is %d \n", bit_status); bit_status = check_bit(yellowbit); printf("Yellow bit is %d \n", bit_status); bit_status = check_bit(greenbit); printf("Green bit is %d \n", bit_status); bit_status = check_bit(bypassbit); printf("Bypass bit is %d \n", bit_status); return 0; } else { usage(); return 0; } return 0; }