From 3b23168977e238ab86ec88803271ac455c2aa78c Mon Sep 17 00:00:00 2001 From: marschap Date: Sat, 8 Apr 2006 13:20:59 +0000 Subject: [PATCH] harmonize coding style and messages; add more checks --- server/drivers/bayrad.c | 270 ++++++++++++++++++---------------------- 1 file changed, 122 insertions(+), 148 deletions(-) diff --git a/server/drivers/bayrad.c b/server/drivers/bayrad.c index 08844ab..63566eb 100644 --- a/server/drivers/bayrad.c +++ b/server/drivers/bayrad.c @@ -47,7 +47,7 @@ ////////////////////// Base "class" to derive from /////////////////////// ////////////////////////////////////////////////////////////////////////// -static int fd; +static int fd = -1; static int width = 0; static int height = 0; static int cellwidth = 5; @@ -349,13 +349,11 @@ bayrad_init(Driver *drvthis) height = 2; framebuf = malloc(width * height); - - if(!framebuf) - { - bayrad_close(drvthis); - report(RPT_ERR, "bayrad_init: Error: unable to create BayRAD framebuffer."); - return -1; - } + if (framebuf == NULL) { + bayrad_close(drvthis); + report(RPT_ERR, "bayrad_init: Error: unable to create BayRAD framebuffer."); + return -1; + } memset(framebuf, ' ', width * height); @@ -368,6 +366,7 @@ bayrad_init(Driver *drvthis) strncpy(device, drvthis->config_get_string(drvthis->name, "Device", 0, BAYRAD_DEFAULT_DEVICE), sizeof(device)); device[sizeof(device)-1] = '\0'; + report(RPT_INFO, "%s: using Device %s", drvthis->name, device); /* What speed to use */ speed = drvthis->config_get_int(drvthis->name, "Speed", 0, 9600); @@ -377,54 +376,55 @@ bayrad_init(Driver *drvthis) else if (speed == 9600) speed = B9600; else if (speed == 19200) speed = B19200; else { - report(RPT_WARNING, "bayrad_init: Illegal speed: %d. Must be one of 1200, 2400, 9600 or 19200. Using default.\n", speed); + report(RPT_WARNING, "%s: illegal Speed %d; must be one of 1200, 2400, 9600 or 19200; using default %d", + drvthis->name, speed, 9600); speed = B9600; } - // Set up io port correctly, and open it... - fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); - if (fd == -1) - { - report(RPT_ERR, "bayrad_init: failed (%s)", strerror(errno)); - return -1; - } + // Set up io port correctly, and open it... + fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); + if (fd == -1) { + report(RPT_ERR, "%s: open(%s) failed (%s)", drvthis->name, device, strerror(errno)); + return -1; + } - //else debug(RPT_DEBUG, "bayrad_init: opened device %s\n", device); + //else debug(RPT_DEBUG, "bayrad_init: opened device %s", device); - tcflush(fd, TCIOFLUSH); + tcflush(fd, TCIOFLUSH); - // We use RAW mode + // We use RAW mode #ifdef HAVE_CFMAKERAW - // The easy way - cfmakeraw( &portset ); + // The easy way + cfmakeraw(&portset); #else - // The hard way - portset.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP - | INLCR | IGNCR | ICRNL | IXON ); - portset.c_oflag &= ~OPOST; - portset.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN ); - portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS ); - portset.c_cflag |= CS8 | CREAD | CLOCAL ; + // The hard way + portset.c_iflag &= ~( IGNBRK | BRKINT | PARMRK | ISTRIP + | INLCR | IGNCR | ICRNL | IXON ); + portset.c_oflag &= ~OPOST; + portset.c_lflag &= ~( ECHO | ECHONL | ICANON | ISIG | IEXTEN ); + portset.c_cflag &= ~( CSIZE | PARENB | CRTSCTS ); + portset.c_cflag |= CS8 | CREAD | CLOCAL ; #endif - portset.c_cc[VTIME] = 0; // Don't use the timer, no workee - portset.c_cc[VMIN] = 1; // Need at least 1 char + portset.c_cc[VTIME] = 0; // Don't use the timer, no workee + portset.c_cc[VMIN] = 1; // Need at least 1 char - // Set port speed - cfsetospeed(&portset, B9600); - cfsetispeed(&portset, B0); + // Set port speed + cfsetospeed(&portset, B9600); + cfsetispeed(&portset, B0); - // Do it... - tcsetattr(fd, TCSANOW, &portset); - tcflush(fd, TCIOFLUSH); + // Do it... + tcsetattr(fd, TCSANOW, &portset); + tcflush(fd, TCIOFLUSH); - /*------------------------------------*/ + /*------------------------------------*/ - /*** Open the port write-only, then fork off a process that reads chars ?!!? ***/ + /*** Open the port write-only, then fork off a process that reads chars ?!!? ***/ + /* Reset and clear the BayRAD */ + write(fd, "\x80\x86\x00\x1a\x1e", 5); // sync,reset to type 0, clear screen, home - /* Reset and clear the BayRAD */ - write(fd, "\x80\x86\x00\x1a\x1e", 5); // sync,reset to type 0, clear screen, home + report(RPT_DEBUG, "%s: init() done", drvthis->name); return 0; } @@ -437,13 +437,15 @@ bayrad_init(Driver *drvthis) MODULE_EXPORT void bayrad_close(Driver * drvthis) { - //debug(RPT_DEBUG, "\nClosing BayRAD.\n"); - write(fd, "\x8e\x00", 2); // Backlight OFF + //debug(RPT_DEBUG, "Closing BayRAD"); + if (fd >= 0) { + write(fd, "\x8e\x00", 2); // Backlight OFF + close(fd); + } - if(framebuf) free(framebuf); + if (framebuf != NULL) + free(framebuf); framebuf = NULL; - - close(fd); } @@ -475,7 +477,6 @@ bayrad_clear(Driver * drvthis) { memset(framebuf, ' ', width * height); ccmode = CCMODE_STANDARD; - } @@ -485,15 +486,11 @@ bayrad_clear(Driver * drvthis) MODULE_EXPORT void bayrad_flush(Driver * drvthis) { - - //debug(RPT_DEBUG, "\nBayRAD flush"); - + //debug(RPT_DEBUG, "BayRAD flush"); write(fd, "\x80\x1e", 2); //sync, home write(fd, framebuf, 20); write(fd, "\x1e\x0a", 2); //home, LF write(fd, framebuf+20, 20); - - return; } ///////////////////////////////////////////////////////////////// @@ -504,34 +501,30 @@ MODULE_EXPORT void bayrad_string(Driver * drvthis, int x, int y, char string[]) { int i; - unsigned char c; - //debug(RPT_DEBUG, "\nPutting string %s at %i, %i", string, x, y); + //debug(RPT_DEBUG, "Putting string %s at %i, %i", string, x, y); - x -= 1; // Convert 1-based coords to 0-based... - y -= 1; + x--; // Convert 1-based coords to 0-based... + y--; - for(i=0; string[i]; i++) - { - // Check for buffer overflows... - if((y*width) + x + i > (width*height)) - break; + for (i = 0; string[i] != '\0'; i++) { + unsigned char c = (unsigned char) string[i]; - c = (unsigned char) string[i]; + // Check for buffer overflows... + if ((y * width) + x + i > (width * height)) + break; - if(c> 0x7F && c < 0x98) - { - //c &= 0x7F; - report(RPT_WARNING, "bayrad_strign: Illegal char %#x requested in bayrad_string()!", c); - c = ' '; + if ((c > 0x7F) && (c < 0x98)) { + //c &= 0x7F; + report(RPT_WARNING, "%s: illegal char 0x%02X requested in bayrad_string()", + drvthis->name, c); + c = ' '; + } - } + if (c < 8) /* The custom characters are mapped at 0x98 - 0x9F, */ + c += 0x98; /* as 0x07 makes a beep instead of printing a character */ - if(c < 8) /* The custom characters are mapped at 0x98 - 0x9F, */ - c += 0x98; /* as 0x07 makes a beep instead of printing a character */ - - - framebuf[(y*width) + x + i] = c; + framebuf[(y * width) + x + i] = c; } } @@ -542,23 +535,22 @@ bayrad_string(Driver * drvthis, int x, int y, char string[]) MODULE_EXPORT void bayrad_chr(Driver * drvthis, int x, int y, char c) { - unsigned char ch; + unsigned char ch = (unsigned char) c; - //debug(RPT_DEBUG, "\nPutting char %c (%#x) at %i, %i", c, c, x, y); + //debug(RPT_DEBUG, "Putting char %c (%#x) at %i, %i", c, c, x, y); y--; x--; - ch = (unsigned char) c; - if(ch > 0x7F && ch < 0x98) - { - report(RPT_WARNING, "Illegal char %#x requested in bayrad_chr()!", ch); - ch = ' '; - } + if ((ch > 0x7F) && (ch < 0x98)) { + report(RPT_WARNING, "%s: illegal char 0x%02X requested in bayrad_chr()", + drvthis->name, c); + ch = ' '; + } /* No shifting the custom chars here, so bayrad_chr() can beep */ - framebuf[(y*width) + x] = ch; + framebuf[(y * width) + x] = ch; } ////////////////////////////////////////////////////////////////////// @@ -567,23 +559,21 @@ bayrad_chr(Driver * drvthis, int x, int y, char c) MODULE_EXPORT void bayrad_backlight(Driver * drvthis, int on) { - /* This violates the LCDd driver model, but it does leave the * backlight control entirely in the hands of the user via * BayRAd buttons, which is nice, since the backlights have * a finite lifespan... */ - if(on) - {; + if (on) { + ; //write(fd, "\x8e\x0f", 2); - //debug(RPT_DEBUG, "Backlight ON\n"); + //debug(RPT_DEBUG, "Backlight ON"); } - else - {; + else { + ; //write(fd, "\x8e\x00", 2); - //debug(RPT_DEBUG, "Backlight OFF\n"); + //debug(RPT_DEBUG, "Backlight OFF"); } - } ////////////////////////////////////////////////////////////////////// @@ -592,16 +582,17 @@ bayrad_backlight(Driver * drvthis, int on) void bayrad_init_vbar(Driver * drvthis) { - //debug(RPT_DEBUG,"Init Vertical bars.\n"); + //debug(RPT_DEBUG,"Init Vertical bars."); - if( ccmode == CCMODE_VBAR ) { + if (ccmode == CCMODE_VBAR) { /* Work already done */ return; } - if( ccmode != CCMODE_STANDARD ) { + if (ccmode != CCMODE_STANDARD) { /* Not supported (yet) */ - report( RPT_WARNING, "bayrad_init_vbar: Cannot combine two modes using user defined characters" ); + report(RPT_WARNING, "%s: cannot combine two modes using user defined characters", + drvthis->name); return; } ccmode = CCMODE_VBAR; @@ -613,9 +604,6 @@ bayrad_init_vbar(Driver * drvthis) bayrad_set_char(drvthis, 5, bar_up[4]); bayrad_set_char(drvthis, 6, bar_up[5]); bayrad_set_char(drvthis, 7, bar_up[6]); - - - return; } ////////////////////////////////////////////////////////////////////// @@ -624,16 +612,17 @@ bayrad_init_vbar(Driver * drvthis) void bayrad_init_hbar(Driver * drvthis) { - //debug(RPT_DEBUG,"Init Horizontal bars.\n"); + //debug(RPT_DEBUG,"Init Horizontal bars."); - if( ccmode == CCMODE_HBAR ) { + if (ccmode == CCMODE_HBAR) { /* Work already done */ return; } - if( ccmode != CCMODE_STANDARD ) { + if (ccmode != CCMODE_STANDARD) { /* Not supported (yet) */ - report( RPT_WARNING, "bayrad_init_hbar: Cannot combine two modes using user defined characters" ); + report(RPT_WARNING, "%s: cannot combine two modes using user defined characters", + drvthis->name); return; } ccmode = CCMODE_HBAR; @@ -643,8 +632,6 @@ bayrad_init_hbar(Driver * drvthis) bayrad_set_char(drvthis, 3, bar_right[2]); bayrad_set_char(drvthis, 4, bar_right[3]); bayrad_set_char(drvthis, 5, bar_right[4]); - -return; } ////////////////////////////////////////////////////////////////////// @@ -653,7 +640,7 @@ return; void bayrad_init_num(Driver * drvthis) { -// debug(RPT_DEBUG,"Big Numbers.\n"); +// debug(RPT_DEBUG,"Big Numbers."); } ////////////////////////////////////////////////////////////////////// @@ -662,7 +649,7 @@ bayrad_init_num(Driver * drvthis) MODULE_EXPORT void bayrad_num(Driver * drvthis, int x, int num) { -// debug(RPT_DEBUG,"BigNum(%i, %i)\n", x, num); +// debug(RPT_DEBUG,"BigNum(%i, %i)", x, num); } ////////////////////////////////////////////////////////////////////// @@ -673,28 +660,23 @@ bayrad_set_char(Driver * drvthis, int n, char *dat) { char out[4]; int row, col; - char letter; - //debug(RPT_DEBUG, "\nSet char %i", n); + //debug(RPT_DEBUG, "Set char %i", n); - if(n < 0 || n > 7) /* Do we want to the aliased indexes as well (0x98 - 0x9F?) */ + if ((n < 0) || (n > 7)) /* Do we want to the aliased indexes as well (0x98 - 0x9F?) */ return; - - if(!dat) + if (!dat) return; - n = 0x40 + (n * 8); /* Set n to the proper location in CG RAM */ - /* Set the LCD to accept data for rewrite-able char n */ - snprintf(out, sizeof(out), "\x88%c", n); + snprintf(out, sizeof(out), "\x88%c", 0x40 + (n * 8)); write(fd, out, 2); - for(row=0; row 0); } @@ -703,8 +685,6 @@ bayrad_set_char(Driver * drvthis, int n, char *dat) /* return the LCD to normal operation */ write(fd, "\x80", 1); - -return; } ///////////////////////////////////////////////////////////////// @@ -713,7 +693,7 @@ return; MODULE_EXPORT void bayrad_vbar(Driver * drvthis, int x, int y, int len, int promille, int options) { - //debug(RPT_DEBUG, "\nVbar at %i, length %i", x, len); + //debug(RPT_DEBUG, "Vbar at %i, length %i", x, len); /* x and y are the start position of the bar. * The bar by default grows in the 'up' direction @@ -733,7 +713,7 @@ bayrad_vbar(Driver * drvthis, int x, int y, int len, int promille, int options) MODULE_EXPORT void bayrad_hbar(Driver * drvthis, int x, int y, int len, int promille, int options) { - //debug(RPT_DEBUG, "\nHbar at %i,%i; length %i", x, y, len); + //debug(RPT_DEBUG, "Hbar at %i,%i; length %i", x, y, len); /* x and y are the start position of the bar. * The bar by default grows in the 'right' direction @@ -754,7 +734,7 @@ bayrad_hbar(Driver * drvthis, int x, int y, int len, int promille, int options) MODULE_EXPORT int bayrad_icon(Driver * drvthis, int x, int y, int icon) { - switch( icon ) { + switch (icon) { case ICON_BLOCK_FILLED: bayrad_chr( drvthis, x, y, 0xFF ); break; @@ -777,9 +757,9 @@ bayrad_get_key(Driver * drvthis) struct timeval twait; char readchar; int retval; - static char ret_val[2] = {0,0}; + static char ret_val[2] = { 0, 0 }; - //debug(RPT_DEBUG, "\nBayRAD get_key..."); + //debug(RPT_DEBUG, "BayRAD get_key..."); /* Check for incoming data. Turn backlight ON/OFF as needed */ @@ -791,32 +771,26 @@ bayrad_get_key(Driver * drvthis) twait.tv_sec = 0; twait.tv_usec = 0; - if(select(fd+1, &brfdset, NULL, NULL, &twait)) - { - retval = read(fd, &readchar, 1); - if(retval > 0) - { - debug(RPT_INFO, "bayrad_get_key: Got key: %c", readchar); + if (select(fd+1, &brfdset, NULL, NULL, &twait)) { + retval = read(fd, &readchar, 1); + if (retval > 0) { + debug(RPT_INFO, "bayrad_get_key: Got key: %c", readchar); - if(readchar == 'Y') - { - write(fd, "\x8e\x0f", 2); - } - else if(readchar == 'N') - { - write(fd, "\x8e\x00", 2); - } - - } /* if read returned data */ - else - { /* Read error */ - report(RPT_ERR, "bayrad_get_key: Read error in BayRAD getchar."); - } - } /* if select */ - else - { - ;//debug(RPT_DEBUG, "No BayRAD data present."); + if (readchar == 'Y') { + write(fd, "\x8e\x0f", 2); + } + else if (readchar == 'N') { + write(fd, "\x8e\x00", 2); + } + } /* if read returned data */ + else { + /* Read error */ + report(RPT_ERR, "%s: Read error in BayRAD getchar.", drvthis->name); } + } /* if select */ + else { + ;//debug(RPT_DEBUG, "No BayRAD data present."); + } ret_val[0] = readchar; return ret_val;