123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- <?xml version='1.0' encoding='iso-8859-1'?>
- <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
- <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
- <head>
- <title>
- int6kdetect.c
- </title>
- <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
- <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
- <meta name='author' content='cmaier@cmassoc.net'/>
- <meta name='robots' content='noindex,nofollow'/>
- <link href='toolkit.css' rel='stylesheet' type='text/css'/>
- </head>
- <body>
- <div class='headerlink'>
- [<a href='int6k.c.html' title=' int6k.c '>PREV</a>]
- [<a href='toolkit.html' title=' Index '>HOME</a>]
- [<a href='int6keth.c.html' title=' int6keth.c '>NEXT</a>]
- </div>
- <pre>
- /*====================================================================*
- *
- * Copyright (c) 2013 Qualcomm Atheros, Inc.
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or
- * without modification, are permitted (subject to the limitations
- * in the disclaimer below) provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following
- * disclaimer in the documentation and/or other materials
- * provided with the distribution.
- *
- * * Neither the name of Qualcomm Atheros nor the names of
- * its contributors may be used to endorse or promote products
- * derived from this software without specific prior written
- * permission.
- *
- * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
- * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
- * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
- * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- *--------------------------------------------------------------------*/
- /*====================================================================*
- *
- * int6kdetect.c
- *
- *
- * Contributor(s):
- * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
- *
- *--------------------------------------------------------------------*/
- /*====================================================================*
- * system header files;
- *--------------------------------------------------------------------*/
- #include <fcntl.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #ifdef WIN32
- #include <windows.h>
- #else
- #include <termios.h>
- #endif
- /*====================================================================*
- * custom header files;
- *--------------------------------------------------------------------*/
- #include "../tools/error.h"
- #include "../tools/files.h"
- #include "../tools/flags.h"
- #include "../tools/putoptv.h"
- #include "../tools/getoptv.h"
- #include "../serial/serial.h"
- /*====================================================================*
- * custom source files;
- *--------------------------------------------------------------------*/
- #ifndef MAKEFILE
- #include "../tools/getoptv.c"
- #include "../tools/putoptv.c"
- #include "../tools/version.c"
- #include "../tools/error.c"
- #endif
- #ifndef MAKEFILE
- #include "../serial/baudrate.c"
- #endif
- /*====================================================================*
- * program constants;
- *--------------------------------------------------------------------*/
- #define SERIAL_PORT "/dev/ttyS0"
- #define INT6KDETECT_QUIET (1 << 0)
- #define INT6KDETECT_CMD_MODE (1 << 1)
- struct serial
- {
- #ifdef WIN32
- HANDLE h;
- #else
- int fd;
- #endif
- };
- #define UART_NOPARITY 0
- #define UART_EVENPARITY 1
- #define UART_ODDPARITY 2
- struct serial_mode
- {
- int baud_rate;
- int parity;
- int data_bits;
- int stop_bits;
- };
- ssize_t read_serial (struct serial *s, void *buf, size_t nbytes)
- {
- #ifdef WIN32
- DWORD read;
- BOOL r;
- r = ReadFile (s->h, buf, (DWORD) nbytes, &read, NULL);
- if (r) return read;
- else return -1;
- #else
- return (read (s->fd, buf, nbytes));
- #endif
- }
- ssize_t write_serial (struct serial *s, void *buf, size_t nbytes)
- {
- #ifdef WIN32
- DWORD written;
- BOOL r;
- r = WriteFile (s->h, buf, (DWORD) nbytes, &written, NULL);
- if (r) return written;
- else return -1;
- #else
- return (write (s->fd, buf, nbytes));
- #endif
- }
- int open_serial (char const *file, struct serial *s)
- {
- #ifdef WIN32
- s->h = CreateFile (file, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (s->h == INVALID_HANDLE_VALUE)
- {
- return (-1);
- }
- #else
- if ((s->fd = open (file, O_RDWR)) == -1)
- {
- return (-1);
- }
- #endif
- return (0);
- }
- int close_serial (struct serial *s)
- {
- #ifdef WIN32
- if (CloseHandle (s->h)) return 0;
- else return -1;
- #else
- return (close (s->fd));
- #endif
- }
- int set_serial (struct serial *s, struct serial_mode *serial_mode)
- {
- #ifdef WIN32
- COMMTIMEOUTS timeouts;
- DCB dcbSerial;
- memset (&dcbSerial, 0, sizeof (dcbSerial));
- dcbSerial.DCBlength = sizeof (dcbSerial);
- if (!GetCommState (s->h, &dcbSerial))
- {
- return (-1);
- }
- dcbSerial.BaudRate = serial_mode->baud_rate;
- dcbSerial.ByteSize = serial_mode->data_bits;
- switch (serial_mode->stop_bits)
- {
- case 1:
- dcbSerial.StopBits = ONESTOPBIT;
- break;
- case 2:
- dcbSerial.StopBits = TWOSTOPBITS;
- break;
- default:
- error (1, 0, "invalid stop bit setting");
- }
- switch (serial_mode->parity)
- {
- case UART_ODDPARITY:
- dcbSerial.Parity = ODDPARITY;
- dcbSerial.fParity = TRUE;
- break;
- case UART_EVENPARITY:
- dcbSerial.Parity = EVENPARITY;
- dcbSerial.fParity = TRUE;
- break;
- case UART_NOPARITY:
- dcbSerial.Parity = NOPARITY;
- dcbSerial.fParity = FALSE;
- break;
- default:
- error (1, 0, "invalid parity serial_mode");
- }
- if (!SetCommState (s->h, &dcbSerial))
- {
- error (0, 0, "could not set serial port settings");
- return (-1);
- }
- timeouts.ReadIntervalTimeout = 0;
- timeouts.ReadTotalTimeoutConstant = 10;
- timeouts.ReadTotalTimeoutMultiplier = 0;
- timeouts.WriteTotalTimeoutConstant = 10;
- timeouts.WriteTotalTimeoutMultiplier = 10;
- if (!SetCommTimeouts (s->h, &timeouts))
- {
- return (-1);
- }
- #else
- struct termios termios;
- speed_t speed;
- tcgetattr (s->fd, &termios);
- cfmakeraw (&termios);
- termios.c_cflag &= ~CSIZE;
- switch (serial_mode->data_bits)
- {
- case 8:
- termios.c_cflag |= CS8;
- break;
- case 7:
- termios.c_cflag |= CS7;
- break;
- case 6:
- termios.c_cflag |= CS6;
- break;
- case 5:
- termios.c_cflag |= CS5;
- break;
- default:
- error (1, 0, "invalid serial byte size");
- }
- switch (serial_mode->stop_bits)
- {
- case 2:
- termios.c_cflag |= CSTOPB;
- break;
- case 1:
- termios.c_cflag &= ~CSTOPB;
- break;
- default:
- error (1, 0, "invalid number of stop bits");
- }
- switch (serial_mode->parity)
- {
- case UART_ODDPARITY:
- termios.c_cflag |= PARENB;
- termios.c_cflag |= PARODD;
- break;
- case UART_EVENPARITY:
- termios.c_cflag |= PARENB;
- termios.c_cflag &= ~PARODD;
- break;
- case UART_NOPARITY:
- termios.c_cflag &= ~PARENB;
- break;
- default:
- error (1, 0, "invalid parity serial_mode");
- }
- if (baudrate (serial_mode->baud_rate, &speed) == -1)
- {
- error (0, 0, "warning: unsupported baud rate: %d", serial_mode->baud_rate);
- return (-1);
- }
- if (cfsetspeed (&termios, speed) == -1) error (1, 0, "could not set serial baud rate");
- termios.c_cc [VTIME] = 1;
- termios.c_cc [VMIN] = 0;
- if (tcsetattr (s->fd, TCSANOW, &termios) == -1) error (1, 0, "could not set serial attributes");
- #endif
- return (0);
- }
- int at_cmd (struct serial *s)
- {
- char buf [32];
- ssize_t r;
- if (write_serial (s, "AT\r", 3) == -1) error (1, 0, "could not write");
- memset (buf, 0, sizeof (buf));
- r = read_serial (s, buf, sizeof (buf) - 1);
- if (r < 0) return -1;
- else if (r == 0) return -1;
- if (!strcmp (buf, "OK\r")) return 0;
- return (-1);
- }
- void wakeup (struct serial *s)
- {
- sleep (1);
- if (write_serial (s, "+++", 3) == -1) error (1, 0, "could not write");
- sleep (1);
- }
- void dump_serial_mode (struct serial_mode *serial_mode)
- {
- printf ("baud_rate = %d\n", serial_mode->baud_rate);
- printf ("stop_bits = %d\n", serial_mode->stop_bits);
- printf ("data_bits = %d\n", serial_mode->data_bits);
- printf ("parity = %d\n", serial_mode->parity);
- }
- int try_serial_mode (struct serial *s, struct serial_mode *serial_mode, flag_t flags)
- {
- if (set_serial (s, serial_mode) == -1)
- {
- error (0, 0, "could not set serial_mode");
- return (-1);
- }
- if (!_anyset (flags, INT6KDETECT_CMD_MODE)) wakeup (s);
- at_cmd (s);
- return (at_cmd (s));
- }
- int detect (struct serial *s, struct serial_mode *serial_mode, flag_t flags)
- {
- static int rate [] =
- {
- 115200,
- 9600,
- 460800,
- 230400,
- 57600,
- 38400,
- 19200,
- 4800,
- 2400,
- 600,
- 300,
- 50
- };
- static int parity [] =
- {
- UART_NOPARITY,
- UART_EVENPARITY,
- UART_ODDPARITY
- };
- size_t i;
- size_t j;
- unsigned current;
- unsigned total;
- total = 2 * 2 * 3 * (sizeof (rate) / sizeof (int));
- current = 0;
- for (serial_mode->stop_bits = 1; serial_mode->stop_bits <= 2; ++serial_mode->stop_bits)
- {
- for (serial_mode->data_bits = 8; serial_mode->data_bits >= 7; --serial_mode->data_bits)
- {
- for (i = 0; i < sizeof (parity) / sizeof (int); ++i)
- {
- serial_mode->parity = parity [i];
- for (j = 0; j < sizeof (rate) / sizeof (int); ++j)
- {
- serial_mode->baud_rate = rate [j];
- ++current;
- if (!_anyset (flags, INT6KDETECT_QUIET))
- {
- printf ("\rTesting mode: %03d/%03d (%.01f%%)...", current, total, current * 100.0 / total);
- fflush (stdout);
- }
- if (!try_serial_mode (s, serial_mode, flags))
- {
- if (!_anyset (flags, INT6KDETECT_QUIET)) printf ("\n");
- return (0);
- }
- }
- }
- }
- }
- if (!_anyset (flags, INT6KDETECT_QUIET)) printf ("\n");
- return (-1);
- }
- int main (int argc, char const * argv [])
- {
- static char const * optv [] =
- {
- "cl:qv",
- "",
- "Atheros UART Device Detector",
- "c\tassume device is in command mode",
- "l f\tserial port is (f) [" SERIAL_PORT "]",
- "q\tquiet mode",
- "v\tverbose mode",
- (char const *) (0)
- };
- signed c;
- char const *line = SERIAL_PORT;
- struct serial serial;
- struct serial_mode serial_mode;
- flag_t flags = 0;
- optind = 1;
- while ((c = getoptv (argc, argv, optv)) != -1)
- {
- switch ((char) (c))
- {
- case 'c':
- _setbits (flags, INT6KDETECT_CMD_MODE);
- break;
- case 'l':
- line = optarg;
- break;
- case 'q':
- _setbits (flags, INT6KDETECT_QUIET);
- break;
- default:
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (open_serial (line, &serial) == -1) error (1, errno, "could not open %s", line);
- if (detect (&serial, &serial_mode, flags) == -1) error (1, 0, "could not detect device");
- printf ("Detected the following serial mode:\n");
- dump_serial_mode (&serial_mode);
- close_serial (&serial);
- return (0);
- }
- </pre>
- <div class='footerlink'>
- [<a href='int6k.c.html' title=' int6k.c '>PREV</a>]
- [<a href='toolkit.html' title=' Index '>HOME</a>]
- [<a href='int6keth.c.html' title=' int6keth.c '>NEXT</a>]
- </div>
- </body>
- </html>
|