| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- /*
- * Copyright (c) 2011 Petr Stetiar <[email protected]>, Gaben Ltd.
- *
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "librs232/rs232.h"
- static const char *
- rs232_baud[] = {
- "300",
- "2400",
- "4800",
- "9600",
- "19200",
- "38400",
- "57600",
- "115200",
- "460800",
- };
- static const char *
- rs232_data[] = {
- "5",
- "6",
- "7",
- "8",
- };
- static const char *
- rs232_parity[] = {
- "none",
- "odd",
- "even",
- };
- static const char *
- rs232_stop[] = {
- "1",
- "2",
- };
- static const char *
- rs232_flow[] = {
- "off",
- "hardware",
- "xon/xoff",
- };
- static const char *
- rs232_dtr[] = {
- "off",
- "on",
- };
- static const char *
- rs232_rts[] = {
- "off",
- "on",
- };
- static const char *
- rs232_error[] = {
- "no error",
- "unknown error",
- "open error",
- "close error",
- "flush error",
- "get/set settings error",
- "read error",
- "write error",
- "select error",
- "timeout error",
- "ioctl error",
- "port closed error",
- };
- #ifdef RS232_DEBUG
- const char *
- rs232_hex_dump(const void *data, unsigned int len)
- {
- static char string[1024] = {0};
- unsigned char *d = (unsigned char *) data;
- unsigned int i;
- for (i = 0; len--; i += 3) {
- if (i >= sizeof(string) - 4)
- break;
- snprintf(string+i, 4, "%02x ", *d++);
- }
- string[i] = '\0';
- return string;
- }
- const char *
- rs232_ascii_dump(const void *data, unsigned int len)
- {
- static char string[1024] = {0};
- unsigned char *d = (unsigned char *) data;
- unsigned int i;
- unsigned char c;
- for (i = 0; len--; i++) {
- if (i >= sizeof(string) - 2)
- break;
- c = *d++;
- snprintf(string+i, 2, "%c", isprint(c) ? c : '.');
- }
- string[i] = '\0';
- return string;
- }
- #endif
- RS232_LIB const char *
- rs232_strerror(unsigned int error)
- {
- DBG("error=%d\n", error);
- if (error >= RS232_ERR_MAX)
- return NULL;
- return rs232_error[error];
- }
- RS232_LIB const char *
- rs232_strbaud(unsigned int baud)
- {
- DBG("baud=%d\n", baud);
- if (baud >= RS232_BAUD_MAX)
- return NULL;
- return rs232_baud[baud];
- }
- RS232_LIB const char *
- rs232_strdata(unsigned int data)
- {
- DBG("data=%d\n", data);
- if (data >= RS232_DATA_MAX)
- return NULL;
- return rs232_data[data];
- }
- RS232_LIB const char *
- rs232_strparity(unsigned int parity)
- {
- DBG("parity=%d\n", parity);
- if (parity >= RS232_PARITY_MAX)
- return NULL;
- return rs232_parity[parity];
- }
- RS232_LIB const char *
- rs232_strstop(unsigned int stop)
- {
- DBG("stop=%d\n", stop);
- if (stop >= RS232_STOP_MAX)
- return NULL;
- return rs232_stop[stop];
- }
- RS232_LIB const char *
- rs232_strflow(unsigned int flow)
- {
- DBG("flow=%d\n", flow);
- if (flow >= RS232_FLOW_MAX)
- return NULL;
- return rs232_flow[flow];
- }
- RS232_LIB const char *
- rs232_strdtr(unsigned int dtr)
- {
- DBG("dtr=%d\n", dtr);
- if (dtr >= RS232_DTR_MAX)
- return NULL;
- return rs232_dtr[dtr];
- }
- RS232_LIB const char *
- rs232_strrts(unsigned int rts)
- {
- DBG("rts=%d\n", rts);
- if (rts >= RS232_RTS_MAX)
- return NULL;
- return rs232_rts[rts];
- }
- RS232_LIB const char *
- rs232_to_string(struct rs232_port_t *p)
- {
- static char str[RS232_STRLEN+1];
- DBG("p=%p\n", (void *)p);
- if (p == NULL)
- return NULL;
- snprintf(str, RS232_STRLEN, "device: %s, baud: %s, data bits: %s,"
- " parity: %s, stop bits: %s,"
- " flow control: %s",
- p->dev,
- rs232_strbaud(p->baud),
- rs232_strdata(p->data),
- rs232_strparity(p->parity),
- rs232_strstop(p->stop),
- rs232_strflow(p->flow));
- return str;
- }
- RS232_LIB const char *
- rs232_get_device(struct rs232_port_t *p)
- {
- DBG("p=%p device: %s\n", (void *)p, p->dev);
- return p->dev;
- }
- RS232_LIB unsigned int
- rs232_get_baud(struct rs232_port_t *p)
- {
- DBG("p=%p baud: %d\n", (void *)p, p->baud);
- return p->baud;
- }
- RS232_LIB unsigned int
- rs232_get_stop(struct rs232_port_t *p)
- {
- DBG("p=%p baud: %d\n", (void *)p, p->stop);
- return p->stop;
- }
- RS232_LIB unsigned int
- rs232_get_data(struct rs232_port_t *p)
- {
- DBG("p=%p data: %d\n", (void *)p, p->data);
- return p->data;
- }
- RS232_LIB unsigned int
- rs232_get_parity(struct rs232_port_t *p)
- {
- DBG("p=%p parity: %d\n", (void *)p, p->parity);
- return p->parity;
- }
- RS232_LIB unsigned int
- rs232_get_flow(struct rs232_port_t *p)
- {
- DBG("p=%p flow: %d\n", (void *)p, p->flow);
- return p->flow;
- }
- RS232_LIB unsigned int
- rs232_port_open(struct rs232_port_t *p)
- {
- DBG("p=%p p->status=%d\n", (void *)p, p->status);
- return p->status;
- }
- RS232_LIB unsigned int
- rs232_get_dtr(struct rs232_port_t *p)
- {
- DBG("p=%p dtr: %d\n", (void *)p, p->dtr);
- return p->dtr;
- }
- RS232_LIB unsigned int
- rs232_get_rts(struct rs232_port_t *p)
- {
- DBG("p=%p rts: %d\n", (void *)p, p->rts);
- return p->rts;
- }
|