| 1 | /*
|
|---|
| 2 | INDI LIB
|
|---|
| 3 | Common routines used by all drivers
|
|---|
| 4 | Copyright (C) 2003 by Jason Harris (jharris@30doradus.org)
|
|---|
| 5 | Elwood C. Downey
|
|---|
| 6 | Jasem Mutlaq
|
|---|
| 7 |
|
|---|
| 8 | This library is free software; you can redistribute it and/or
|
|---|
| 9 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 10 | License as published by the Free Software Foundation; either
|
|---|
| 11 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This library is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | Lesser General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 19 | License along with this library; if not, write to the Free Software
|
|---|
| 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 21 |
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | /** \file indicom.h
|
|---|
| 25 | \brief Implementations for common driver routines.
|
|---|
| 26 |
|
|---|
| 27 | The INDI Common Routine Library provides formatting and serial routines employed by many INDI drivers. Currently, the library is composed of the following sections:
|
|---|
| 28 |
|
|---|
| 29 | <ul>
|
|---|
| 30 | <li>Formatting Functions</li>
|
|---|
| 31 | <li>Conversion Functions</li>
|
|---|
| 32 | <li>TTY Functions</li>
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | </ul>
|
|---|
| 36 |
|
|---|
| 37 | \author Jason Harris
|
|---|
| 38 | \author Elwood C. Downey
|
|---|
| 39 | \author Jasem Mutlaq
|
|---|
| 40 | */
|
|---|
| 41 |
|
|---|
| 42 | #ifndef INDICOM_H
|
|---|
| 43 | #define INDICOM_H
|
|---|
| 44 |
|
|---|
| 45 | #include <time.h>
|
|---|
| 46 |
|
|---|
| 47 | #define J2000 2451545.0
|
|---|
| 48 | #define ERRMSG_SIZE 1024
|
|---|
| 49 | #define INDI_DEBUG
|
|---|
| 50 |
|
|---|
| 51 | extern const char * Direction[];
|
|---|
| 52 | extern const char * SolarSystem[];
|
|---|
| 53 |
|
|---|
| 54 | struct ln_date;
|
|---|
| 55 |
|
|---|
| 56 | /* TTY Error Codes */
|
|---|
| 57 | enum TTY_ERROR { TTY_OK=0, TTY_READ_ERROR=-1, TTY_WRITE_ERROR=-2, TTY_SELECT_ERROR=-3, TTY_TIME_OUT=-4, TTY_PORT_FAILURE=-5, TTY_PARAM_ERROR=-6, TTY_ERRNO = -7};
|
|---|
| 58 |
|
|---|
| 59 | #ifdef __cplusplus
|
|---|
| 60 | extern "C" {
|
|---|
| 61 | #endif
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * \defgroup ttyFunctions TTY Functions: Functions to perform common terminal access routines.
|
|---|
| 65 | */
|
|---|
| 66 |
|
|---|
| 67 | /*@{*/
|
|---|
| 68 |
|
|---|
| 69 | /** \brief read buffer from terminal
|
|---|
| 70 | \param fd file descriptor
|
|---|
| 71 | \param buf pointer to store data. Must be initilized and big enough to hold data.
|
|---|
| 72 | \param nbytes number of bytes to read.
|
|---|
| 73 | \param timeout number of seconds to wait for terminal before a timeout error is issued.
|
|---|
| 74 | \param nbytes_read the number of bytes read.
|
|---|
| 75 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code.
|
|---|
| 76 | */
|
|---|
| 77 | int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read);
|
|---|
| 78 |
|
|---|
| 79 | /** \brief read buffer from terminal with a delimiter
|
|---|
| 80 | \param fd file descriptor
|
|---|
| 81 | \param buf pointer to store data. Must be initilized and big enough to hold data.
|
|---|
| 82 | \param stop_char if the function encounters \e stop_char then it stops reading and returns the buffer.
|
|---|
| 83 | \param timeout number of seconds to wait for terminal before a timeout error is issued.
|
|---|
| 84 | \param nbytes_read the number of bytes read.
|
|---|
| 85 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code.
|
|---|
| 86 | */
|
|---|
| 87 |
|
|---|
| 88 | int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read);
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | /** \brief Writes a buffer to fd.
|
|---|
| 92 | \param fd file descriptor
|
|---|
| 93 | \param buffer a null-terminated buffer to write to fd.
|
|---|
| 94 | \param nbytes number of bytes to write from \e buffer
|
|---|
| 95 | \param nbytes_written the number of bytes written
|
|---|
| 96 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code.
|
|---|
| 97 | */
|
|---|
| 98 | int tty_write(int fd, const char * buffer, int nbytes, int *nbytes_written);
|
|---|
| 99 |
|
|---|
| 100 | /** \brief Writes a null terminated string to fd.
|
|---|
| 101 | \param fd file descriptor
|
|---|
| 102 | \param buffer the buffer to write to fd.
|
|---|
| 103 | \param nbytes_written the number of bytes written
|
|---|
| 104 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code.
|
|---|
| 105 | */
|
|---|
| 106 | int tty_write_string(int fd, const char * buffer, int *nbytes_written);
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | /** \brief Establishes a tty connection to a terminal device.
|
|---|
| 110 | \param device the device node. e.g. /dev/ttyS0
|
|---|
| 111 | \param bit_rate bit rate
|
|---|
| 112 | \param word_size number of data bits, 7 or 8, USE 8 DATA BITS with modbus
|
|---|
| 113 | \param parity 0=no parity, 1=parity EVEN, 2=parity ODD
|
|---|
| 114 | \param stop_bits number of stop bits : 1 or 2
|
|---|
| 115 | \param fd \e fd is set to the file descriptor value on success.
|
|---|
| 116 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code.
|
|---|
| 117 | \author Wildi Markus
|
|---|
| 118 | */
|
|---|
| 119 |
|
|---|
| 120 | int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd);
|
|---|
| 121 |
|
|---|
| 122 | /** \brief Closes a tty connection and flushes the bus.
|
|---|
| 123 | \param fd the file descriptor to close.
|
|---|
| 124 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code.
|
|---|
| 125 | */
|
|---|
| 126 | int tty_disconnect(int fd);
|
|---|
| 127 |
|
|---|
| 128 | /** \brief Retrieve the tty error message
|
|---|
| 129 | \param err_code the error code return by any TTY function.
|
|---|
| 130 | \param err_msg an initialized buffer to hold the error message.
|
|---|
| 131 | \param err_msg_len length in bytes of \e err_msg
|
|---|
| 132 | */
|
|---|
| 133 | void tty_error_msg(int err_code, char *err_msg, int err_msg_len);
|
|---|
| 134 |
|
|---|
| 135 | int tty_timeout(int fd, int timeout);
|
|---|
| 136 | /*@}*/
|
|---|
| 137 |
|
|---|
| 138 | /**
|
|---|
| 139 | * \defgroup convertFunctions Formatting Functions: Functions to perform handy formatting and conversion routines.
|
|---|
| 140 | */
|
|---|
| 141 | /*@{*/
|
|---|
| 142 |
|
|---|
| 143 | /** \brief Converts a sexagesimal number to a string.
|
|---|
| 144 |
|
|---|
| 145 | sprint the variable a in sexagesimal format into out[].
|
|---|
| 146 |
|
|---|
| 147 | \param out a pointer to store the sexagesimal number.
|
|---|
| 148 | \param a the sexagesimal number to convert.
|
|---|
| 149 | \param w the number of spaces in the whole part.
|
|---|
| 150 | \param fracbase is the number of pieces a whole is to broken into; valid options:\n
|
|---|
| 151 | \li 360000: \<w\>:mm:ss.ss
|
|---|
| 152 | \li 36000: \<w\>:mm:ss.s
|
|---|
| 153 | \li 3600: \<w\>:mm:ss
|
|---|
| 154 | \li 600: \<w\>:mm.m
|
|---|
| 155 | \li 60: \<w\>:mm
|
|---|
| 156 |
|
|---|
| 157 | \return number of characters written to out, not counting final null terminator.
|
|---|
| 158 | */
|
|---|
| 159 | int fs_sexa (char *out, double a, int w, int fracbase);
|
|---|
| 160 |
|
|---|
| 161 | /** \brief convert sexagesimal string str AxBxC to double.
|
|---|
| 162 |
|
|---|
| 163 | x can be anything non-numeric. Any missing A, B or C will be assumed 0. Optional - and + can be anywhere.
|
|---|
| 164 |
|
|---|
| 165 | \param str0 string containing sexagesimal number.
|
|---|
| 166 | \param dp pointer to a double to store the sexagesimal number.
|
|---|
| 167 | \return return 0 if ok, -1 if can't find a thing.
|
|---|
| 168 | */
|
|---|
| 169 | int f_scansexa (const char *str0, double *dp);
|
|---|
| 170 |
|
|---|
| 171 | /** \brief Extract ISO 8601 time and store it in a tm struct.
|
|---|
| 172 | \param timestr a string containing date and time in ISO 8601 format.
|
|---|
| 173 | \param iso_date a pointer to a \e ln_date structure to store the extracted time and date (libnova).
|
|---|
| 174 | \return 0 on success, -1 on failure.
|
|---|
| 175 | */
|
|---|
| 176 | int extractISOTime(char *timestr, struct ln_date *iso_date);
|
|---|
| 177 |
|
|---|
| 178 | void getSexComponents(double value, int *d, int *m, int *s);
|
|---|
| 179 |
|
|---|
| 180 | /** \brief Fill buffer with properly formatted INumber string.
|
|---|
| 181 | \param buf to store the formatted string.
|
|---|
| 182 | \param format format in sprintf style.
|
|---|
| 183 | \param value the number to format.
|
|---|
| 184 | \return length of string.
|
|---|
| 185 | */
|
|---|
| 186 | int numberFormat (char *buf, const char *format, double value);
|
|---|
| 187 |
|
|---|
| 188 | /** \brief Create an ISO 8601 formatted time stamp. The format is YYYY-MM-DDTHH:MM:SS
|
|---|
| 189 | \return The formatted time stamp.
|
|---|
| 190 | */
|
|---|
| 191 | const char *timestamp (void);
|
|---|
| 192 |
|
|---|
| 193 | /*@}*/
|
|---|
| 194 |
|
|---|
| 195 | #ifdef __cplusplus
|
|---|
| 196 | }
|
|---|
| 197 | #endif
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | #endif
|
|---|