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 | |
---|
52 | extern const char * Direction[]; |
---|
53 | extern const char * SolarSystem[]; |
---|
54 | |
---|
55 | struct ln_date; |
---|
56 | |
---|
57 | /* TTY Error Codes */ |
---|
58 | 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}; |
---|
59 | |
---|
60 | #ifdef __cplusplus |
---|
61 | extern "C" { |
---|
62 | #endif |
---|
63 | |
---|
64 | /** |
---|
65 | * \defgroup ttyFunctions TTY Functions: Functions to perform common terminal access routines. |
---|
66 | */ |
---|
67 | |
---|
68 | /*@{*/ |
---|
69 | |
---|
70 | /** \brief read buffer from terminal |
---|
71 | \param fd file descriptor |
---|
72 | \param buf pointer to store data. Must be initilized and big enough to hold data. |
---|
73 | \param nbytes number of bytes to read. |
---|
74 | \param timeout number of seconds to wait for terminal before a timeout error is issued. |
---|
75 | \param nbytes_read the number of bytes read. |
---|
76 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code. |
---|
77 | */ |
---|
78 | int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read); |
---|
79 | |
---|
80 | /** \brief read buffer from terminal with a delimiter |
---|
81 | \param fd file descriptor |
---|
82 | \param buf pointer to store data. Must be initilized and big enough to hold data. |
---|
83 | \param stop_char if the function encounters \e stop_char then it stops reading and returns the buffer. |
---|
84 | \param timeout number of seconds to wait for terminal before a timeout error is issued. |
---|
85 | \param nbytes_read the number of bytes read. |
---|
86 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code. |
---|
87 | */ |
---|
88 | |
---|
89 | int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read); |
---|
90 | |
---|
91 | |
---|
92 | /** \brief Writes a buffer to fd. |
---|
93 | \param fd file descriptor |
---|
94 | \param buffer a null-terminated buffer to write to fd. |
---|
95 | \param nbytes number of bytes to write from \e buffer |
---|
96 | \param nbytes_written the number of bytes written |
---|
97 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code. |
---|
98 | */ |
---|
99 | int tty_write(int fd, const char * buffer, int nbytes, int *nbytes_written); |
---|
100 | |
---|
101 | /** \brief Writes a null terminated string to fd. |
---|
102 | \param fd file descriptor |
---|
103 | \param buffer the buffer to write to fd. |
---|
104 | \param nbytes_written the number of bytes written |
---|
105 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code. |
---|
106 | */ |
---|
107 | int tty_write_string(int fd, const char * buffer, int *nbytes_written); |
---|
108 | |
---|
109 | |
---|
110 | /** \brief Establishes a tty connection to a terminal device. |
---|
111 | \param device the device node. e.g. /dev/ttyS0 |
---|
112 | \param bit_rate bit rate |
---|
113 | \param word_size number of data bits, 7 or 8, USE 8 DATA BITS with modbus |
---|
114 | \param parity 0=no parity, 1=parity EVEN, 2=parity ODD |
---|
115 | \param stop_bits number of stop bits : 1 or 2 |
---|
116 | \param fd \e fd is set to the file descriptor value on success. |
---|
117 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code. |
---|
118 | \author Wildi Markus |
---|
119 | */ |
---|
120 | |
---|
121 | int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd); |
---|
122 | |
---|
123 | /** \brief Closes a tty connection and flushes the bus. |
---|
124 | \param fd the file descriptor to close. |
---|
125 | \return On success, it returns TTY_OK, otherwise, a TTY_ERROR code. |
---|
126 | */ |
---|
127 | int tty_disconnect(int fd); |
---|
128 | |
---|
129 | /** \brief Retrieve the tty error message |
---|
130 | \param err_code the error code return by any TTY function. |
---|
131 | \param err_msg an initialized buffer to hold the error message. |
---|
132 | \param err_msg_len length in bytes of \e err_msg |
---|
133 | */ |
---|
134 | void tty_error_msg(int err_code, char *err_msg, int err_msg_len); |
---|
135 | |
---|
136 | int tty_timeout(int fd, int timeout); |
---|
137 | /*@}*/ |
---|
138 | |
---|
139 | /** |
---|
140 | * \defgroup convertFunctions Formatting Functions: Functions to perform handy formatting and conversion routines. |
---|
141 | */ |
---|
142 | /*@{*/ |
---|
143 | |
---|
144 | /** \brief Converts a sexagesimal number to a string. |
---|
145 | |
---|
146 | sprint the variable a in sexagesimal format into out[]. |
---|
147 | |
---|
148 | \param out a pointer to store the sexagesimal number. |
---|
149 | \param a the sexagesimal number to convert. |
---|
150 | \param w the number of spaces in the whole part. |
---|
151 | \param fracbase is the number of pieces a whole is to broken into; valid options:\n |
---|
152 | \li 360000: \<w\>:mm:ss.ss |
---|
153 | \li 36000: \<w\>:mm:ss.s |
---|
154 | \li 3600: \<w\>:mm:ss |
---|
155 | \li 600: \<w\>:mm.m |
---|
156 | \li 60: \<w\>:mm |
---|
157 | |
---|
158 | \return number of characters written to out, not counting final null terminator. |
---|
159 | */ |
---|
160 | int fs_sexa (char *out, double a, int w, int fracbase); |
---|
161 | |
---|
162 | /** \brief convert sexagesimal string str AxBxC to double. |
---|
163 | |
---|
164 | x can be anything non-numeric. Any missing A, B or C will be assumed 0. Optional - and + can be anywhere. |
---|
165 | |
---|
166 | \param str0 string containing sexagesimal number. |
---|
167 | \param dp pointer to a double to store the sexagesimal number. |
---|
168 | \return return 0 if ok, -1 if can't find a thing. |
---|
169 | */ |
---|
170 | int f_scansexa (const char *str0, double *dp); |
---|
171 | |
---|
172 | /** \brief Extract ISO 8601 time and store it in a tm struct. |
---|
173 | \param timestr a string containing date and time in ISO 8601 format. |
---|
174 | \param iso_date a pointer to a \e ln_date structure to store the extracted time and date (libnova). |
---|
175 | \return 0 on success, -1 on failure. |
---|
176 | */ |
---|
177 | int extractISOTime(char *timestr, struct ln_date *iso_date); |
---|
178 | |
---|
179 | void getSexComponents(double value, int *d, int *m, int *s); |
---|
180 | |
---|
181 | /** \brief Fill buffer with properly formatted INumber string. |
---|
182 | \param buf to store the formatted string. |
---|
183 | \param format format in sprintf style. |
---|
184 | \param value the number to format. |
---|
185 | \return length of string. |
---|
186 | */ |
---|
187 | int numberFormat (char *buf, const char *format, double value); |
---|
188 | |
---|
189 | /** \brief Create an ISO 8601 formatted time stamp. The format is YYYY-MM-DDTHH:MM:SS |
---|
190 | \return The formatted time stamp. |
---|
191 | */ |
---|
192 | const char *timestamp (void); |
---|
193 | |
---|
194 | /*@}*/ |
---|
195 | |
---|
196 | #ifdef __cplusplus |
---|
197 | } |
---|
198 | #endif |
---|
199 | |
---|
200 | |
---|
201 | #endif |
---|