123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- #ifndef SERIAL_SOURCE
- #define SERIAL_SOURCE
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <memory.h>
- #include <errno.h>
- #if defined (WIN32)
- #include <Windows.h>
- #else
- #include <sys/select.h>
- #endif
- #include "../serial/serial.h"
- #include "../tools/number.h"
- #include "../tools/types.h"
- #include "../tools/flags.h"
- #include "../tools/error.h"
- struct command command;
- void clearcommand ()
- {
- extern struct command command;
- memset (&command, 0, sizeof (command));
- return;
- }
- void sendcommand (struct _file_ * port, flag_t flags)
- {
- extern struct command command;
- if (_anyset (flags, UART_VERBOSE))
- {
- write (STDERR_FILENO, command.buffer, command.length);
- write (STDERR_FILENO, "\n", sizeof (char));
- }
- if (write (port->file, command.buffer, command.length) != (signed)(command.length))
- {
- error (1, errno, "Can't write to %s", port->name);
- }
- clearcommand ();
- return;
- }
- void readcommand (struct _file_ * port, flag_t flags)
- {
- extern struct command command;
- ssize_t tmp;
- #if defined (WIN32)
- PAUSE (250);
- memset (&command, 0, sizeof (command));
- tmp = read (port->file, command.buffer, sizeof (command.buffer));
- if (tmp < 0)
- {
- error (1, errno, "Bad response from %s", port->name);
- }
- if (tmp == 0)
- {
- error (1, errno, "No response from %s", port->name);
- }
- command.length = tmp;
- #else
- struct timeval tv;
- fd_set rfd;
- memset (&command, 0, sizeof (command));
- while (!strchr (command.buffer, '\r'))
- {
- tv.tv_sec = 1;
- tv.tv_usec = 0;
- FD_ZERO (&rfd);
- FD_SET (port->file, &rfd);
- if (select (port->file + 1, &rfd, NULL, NULL, &tv) != 1)
- {
- error (1, errno, "Read timeout");
- }
- tmp = read (port->file, command.buffer + command.length, sizeof (command.buffer) - command.length - 1);
- if (tmp < 0)
- {
- error (1, errno, "Could not read %s", port->name);
- }
- command.length += tmp;
- command.buffer [command.length] = '\0';
- }
- #endif
- if (_anyset (flags, UART_VERBOSE))
- {
- write (STDERR_FILENO, command.buffer, command.length);
- write (STDERR_FILENO, "\n", sizeof (char));
- }
- if (!memcmp (command.buffer, "ERROR", 5))
- {
- error (1, ECANCELED, "Device refused request");
- }
- return;
- }
- void insert (char c)
- {
- extern struct command command;
- if (command.length < sizeof (command.buffer))
- {
- command.buffer [command.length++] = c;
- }
- return;
- }
- static signed fdgetc (signed fd)
- {
- char c;
- return ((read (fd, &c, sizeof (c)) == sizeof (c))? c: EOF);
- }
- size_t readframe (signed fd, void * memory, size_t extent)
- {
- unsigned digits = 0;
- uint8_t * origin = (uint8_t *)(memory);
- uint8_t * offset = (uint8_t *)(memory);
- signed c = EOF;
- while ((extent) && ((c = fdgetc (fd)) != EOF) && (c != ';'))
- {
- if (isspace (c))
- {
- continue;
- }
- if (c == '#')
- {
- do
- {
- c = fdgetc (fd);
- }
- while ((c != '\n') && (c != EOF));
- continue;
- }
- if (c == '/')
- {
- c = fdgetc (fd);
- if (c == '/')
- {
- do
- {
- c = fdgetc (fd);
- }
- while ((c != '\n') && (c != EOF));
- continue;
- }
- if (c == '*')
- {
- while ((c != '/') && (c != EOF))
- {
- while ((c != '*') && (c != EOF))
- {
- c = fdgetc (fd);
- }
- c = fdgetc (fd);
- }
- continue;
- }
- continue;
- }
- if (isxdigit (c))
- {
- *offset = c;
- offset++;
- digits++;
- extent--;
- continue;
- }
- error (1, ENOTSUP, "Illegal hex digit '%c' (0x%02X) in source", c, c);
- }
- if (digits & 1)
- {
- error (1, ENOTSUP, "Odd number of hex digits (%d) in source", digits);
- }
- return (offset - origin);
- }
- void decode (void const * memory, size_t extent)
- {
- extern struct command command;
- register byte * binary = (byte *)(memory);
- while ((command.length < sizeof (command.buffer)) && (extent--))
- {
- insert (DIGITS_HEX [(*binary >> 4) & 0x0F]);
- insert (DIGITS_HEX [(*binary >> 0) & 0x0F]);
- binary++;
- }
- return;
- }
- void encode (void * memory, size_t extent)
- {
- extern struct command command;
- register byte * binary = (byte *)(memory);
- unsigned digit;
- while ((command.offset < command.length) && (extent--))
- {
- *binary = 0;
- if ((digit = todigit (command.buffer [command.offset++])) > 0x0F)
- {
- command.buffer [command.offset] = (char)(0);
- error (1, EINVAL, "[%s]1", command.buffer);
- }
- *binary |= digit << 4;
- if ((digit = todigit (command.buffer [command.offset++])) > 0x0F)
- {
- command.buffer [command.offset] = (char)(0);
- error (1, EINVAL, "[%s]2", command.buffer);
- }
- *binary |= digit;
- binary++;
- }
- return;
- }
- void string (char * string)
- {
- extern struct command command;
- while ((command.offset < command.length) && (command.buffer [command.offset] != '\"'))
- {
- *string++ = command.buffer [command.offset++];
- }
- *string = (char)(0);
- return;
- }
- uint64_t hextoint (unsigned bytes)
- {
- extern struct command command;
- uint64_t limit = -1;
- uint64_t value = 0;
- unsigned radix = 16;
- unsigned digit = 0;
- if (bytes < sizeof (limit))
- {
- limit <<= (bytes << 3);
- limit = ~limit;
- }
- while ((digit = todigit (command.buffer [command.offset])) < radix)
- {
- value *= radix;
- value += digit;
- command.offset++;
- if (value > limit)
- {
- command.buffer [command.offset] = (char)(0);
- error (1, EINVAL, "[%s] exceeds %d bits", command.buffer, (bytes << 3));
- }
- }
- return (value);
- }
- void mustbe (char c)
- {
- extern struct command command;
- if (command.offset >= command.length)
- {
- command.buffer [command.offset] = (char)(0);
- error (1, EINVAL, "[%s]: overflow", command.buffer);
- }
- if (command.buffer [command.offset++] != (c))
- {
- command.buffer [command.offset] = (char)(0);
- error (1, EINVAL, "[%s]: expecting 0x%02X", command.buffer, c);
- }
- return;
- }
- #endif
|