| 1 |
/* |
| 2 |
INDI LIB |
| 3 |
Common routines used by all drivers |
| 4 |
Copyright (C) 2003 by Jason Harris ([email protected]) |
| 5 |
Elwood C. Downey |
| 6 |
|
| 7 |
This is the C version of the astronomical library in KStars |
| 8 |
modified by Jasem Mutlaq ([email protected]) |
| 9 |
|
| 10 |
This library is free software; you can redistribute it and/or |
| 11 |
modify it under the terms of the GNU Lesser General Public |
| 12 |
License as published by the Free Software Foundation; either |
| 13 |
version 2.1 of the License, or (at your option) any later version. |
| 14 |
|
| 15 |
This library is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
Lesser General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU Lesser General Public |
| 21 |
License along with this library; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 |
|
| 24 |
*/ |
| 25 |
|
| 26 |
#define _GNU_SOURCE 1 |
| 27 |
|
| 28 |
#include "indicom.h" |
| 29 |
|
| 30 |
#include "indidevapi.h" |
| 31 |
#include "locale_compat.h" |
| 32 |
#include "base64.h" |
| 33 |
|
| 34 |
#include "config.h" |
| 35 |
|
| 36 |
#if defined(HAVE_LIBNOVA) |
| 37 |
#include <libnova/julian_day.h> |
| 38 |
#include <libnova/sidereal_time.h> |
| 39 |
#include <libnova/ln_types.h> |
| 40 |
#include <libnova/transform.h> |
| 41 |
#endif // HAVE_LIBNOVA |
| 42 |
|
| 43 |
#include <errno.h> |
| 44 |
#include <fcntl.h> |
| 45 |
#include <math.h> |
| 46 |
#include <stdarg.h> |
| 47 |
#include <stdint.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
#include <time.h> |
| 52 |
|
| 53 |
#ifdef __linux__ |
| 54 |
#include <sys/ioctl.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifdef __APPLE__ |
| 58 |
#include <sys/param.h> |
| 59 |
#include <mach/clock.h> |
| 60 |
#include <mach/mach.h> |
| 61 |
#endif |
| 62 |
|
| 63 |
#ifdef __FreeBSD__ |
| 64 |
#include <sys/param.h> |
| 65 |
#endif |
| 66 |
|
| 67 |
#if defined(BSD) && !defined(__GNU__) |
| 68 |
#ifdef __APPLE__ |
| 69 |
#include <IOKit/serial/ioss.h> |
| 70 |
#endif |
| 71 |
#include <sys/ioctl.h> |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef _WIN32 |
| 75 |
#undef CX |
| 76 |
#undef CY |
| 77 |
#endif |
| 78 |
|
| 79 |
#ifndef _WIN32 |
| 80 |
#include <unistd.h> |
| 81 |
#include <termios.h> |
| 82 |
#include <sys/param.h> |
| 83 |
#define PARITY_NONE 0 |
| 84 |
#define PARITY_EVEN 1 |
| 85 |
#define PARITY_ODD 2 |
| 86 |
#endif |
| 87 |
|
| 88 |
#include "userio.h" |
| 89 |
#include "indiuserio.h" |
| 90 |
|
| 91 |
#if defined(_MSC_VER) |
| 92 |
#define snprintf _snprintf |
| 93 |
#pragma warning(push) |
| 94 |
///@todo Introduce plattform indipendent safe functions as macros to fix this |
| 95 |
#pragma warning(disable : 4996) |
| 96 |
#endif |
| 97 |
|
| 98 |
#define MAXRBUF 2048 |
| 99 |
|
| 100 |
static int tty_debug = 0; |
| 101 |
static int tty_gemini_udp_format = 0; |
| 102 |
static int tty_generic_udp_format = 0; |
| 103 |
static int tty_sequence_number = 1; |
| 104 |
static int tty_clear_trailing_lf = 0; |
| 105 |
|
| 106 |
#if defined(HAVE_LIBNOVA) |
| 107 |
int extractISOTime(const char *timestr, struct ln_date *iso_date) |
| 108 |
{ |
| 109 |
struct tm utm; |
| 110 |
|
| 111 |
if (strptime(timestr, "%Y/%m/%dT%H:%M:%S", &utm)) |
| 112 |
{ |
| 113 |
ln_get_date_from_tm(&utm, iso_date); |
| 114 |
return (0); |
| 115 |
} |
| 116 |
|
| 117 |
if (strptime(timestr, "%Y-%m-%dT%H:%M:%S", &utm)) |
| 118 |
{ |
| 119 |
ln_get_date_from_tm(&utm, iso_date); |
| 120 |
return (0); |
| 121 |
} |
| 122 |
|
| 123 |
return (-1); |
| 124 |
} |
| 125 |
#endif |
| 126 |
|
| 127 |
/* sprint the variable a in sexagesimal format into out[]. |
| 128 |
* w is the number of spaces for the whole part. |
| 129 |
* fracbase is the number of pieces a whole is to broken into; valid options: |
| 130 |
* 360000: <w>:mm:ss.ss |
| 131 |
* 36000: <w>:mm:ss.s |
| 132 |
* 3600: <w>:mm:ss |
| 133 |
* 600: <w>:mm.m |
| 134 |
* 60: <w>:mm |
| 135 |
* return number of characters written to out, not counting final '\0'. |
| 136 |
*/ |
| 137 |
int fs_sexa(char *out, double a, int w, int fracbase) |
| 138 |
{ |
| 139 |
char *out0 = out; |
| 140 |
unsigned long n; |
| 141 |
int d; |
| 142 |
int f; |
| 143 |
int m; |
| 144 |
int s; |
| 145 |
int isneg; |
| 146 |
|
| 147 |
/* save whether it's negative but do all the rest with a positive */ |
| 148 |
isneg = (a < 0); |
| 149 |
if (isneg) |
| 150 |
a = -a; |
| 151 |
|
| 152 |
/* convert to an integral number of whole portions */ |
| 153 |
n = (unsigned long)(a * fracbase + 0.5); |
| 154 |
d = n / fracbase; |
| 155 |
f = n % fracbase; |
| 156 |
|
| 157 |
/* form the whole part; "negative 0" is a special case */ |
| 158 |
if (isneg && d == 0) |
| 159 |
out += snprintf(out, MAXINDIFORMAT, "%*s-0", w - 2, ""); |
| 160 |
else |
| 161 |
out += snprintf(out, MAXINDIFORMAT, "%*d", w, isneg ? -d : d); |
| 162 |
|
| 163 |
/* do the rest */ |
| 164 |
switch (fracbase) |
| 165 |
{ |
| 166 |
case 60: /* dd:mm */ |
| 167 |
m = f / (fracbase / 60); |
| 168 |
out += snprintf(out, MAXINDIFORMAT, ":%02d", m); |
| 169 |
break; |
| 170 |
case 600: /* dd:mm.m */ |
| 171 |
out += snprintf(out, MAXINDIFORMAT, ":%02d.%1d", f / 10, f % 10); |
| 172 |
break; |
| 173 |
case 3600: /* dd:mm:ss */ |
| 174 |
m = f / (fracbase / 60); |
| 175 |
s = f % (fracbase / 60); |
| 176 |
out += snprintf(out, MAXINDIFORMAT, ":%02d:%02d", m, s); |
| 177 |
break; |
| 178 |
case 36000: /* dd:mm:ss.s*/ |
| 179 |
m = f / (fracbase / 60); |
| 180 |
s = f % (fracbase / 60); |
| 181 |
out += snprintf(out, MAXINDIFORMAT, ":%02d:%02d.%1d", m, s / 10, s % 10); |
| 182 |
break; |
| 183 |
case 360000: /* dd:mm:ss.ss */ |
| 184 |
m = f / (fracbase / 60); |
| 185 |
s = f % (fracbase / 60); |
| 186 |
out += snprintf(out, MAXINDIFORMAT, ":%02d:%02d.%02d", m, s / 100, s % 100); |
| 187 |
break; |
| 188 |
default: |
| 189 |
printf("fs_sexa: unknown fracbase: %d\n", fracbase); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
|
| 193 |
return (out - out0); |
| 194 |
} |
| 195 |
|
| 196 |
/* convert sexagesimal string str AxBxC to double. |
| 197 |
* x can be anything non-numeric. Any missing A, B or C will be assumed 0. |
| 198 |
* optional - and + can be anywhere. |
| 199 |
* return 0 if ok, -1 if can't find a thing. |
| 200 |
*/ |
| 201 |
int f_scansexa(const char *str0, /* input string */ |
| 202 |
double *dp) /* cracked value, if return 0 */ |
| 203 |
{ |
| 204 |
locale_char_t *orig = indi_locale_C_numeric_push(); |
| 205 |
|
| 206 |
double a = 0, b = 0, c = 0; |
| 207 |
char str[128]; |
| 208 |
//char *neg; |
| 209 |
uint8_t isNegative=0; |
| 210 |
int r= 0; |
| 211 |
|
| 212 |
/* copy str0 so we can play with it */ |
| 213 |
strncpy(str, str0, sizeof(str) - 1); |
| 214 |
str[sizeof(str) - 1] = '\0'; |
| 215 |
|
| 216 |
/* remove any spaces */ |
| 217 |
char* i = str; |
| 218 |
char* j = str; |
| 219 |
while(*j != 0) |
| 220 |
{ |
| 221 |
*i = *j++; |
| 222 |
if(*i != ' ') |
| 223 |
i++; |
| 224 |
} |
| 225 |
*i = 0; |
| 226 |
|
| 227 |
// This has problem process numbers in scientific notations e.g. 1e-06 |
| 228 |
/*neg = strchr(str, '-'); |
| 229 |
if (neg) |
| 230 |
*neg = ' '; |
| 231 |
*/ |
| 232 |
if (str[0] == '-') |
| 233 |
{ |
| 234 |
isNegative = 1; |
| 235 |
str[0] = ' '; |
| 236 |
} |
| 237 |
|
| 238 |
r = sscanf(str, "%lf%*[^0-9]%lf%*[^0-9]%lf", &a, &b, &c); |
| 239 |
|
| 240 |
indi_locale_C_numeric_pop(orig); |
| 241 |
|
| 242 |
if (r < 1) |
| 243 |
return (-1); |
| 244 |
*dp = a + b / 60 + c / 3600; |
| 245 |
if (isNegative) |
| 246 |
*dp *= -1; |
| 247 |
return (0); |
| 248 |
} |
| 249 |
|
| 250 |
void getSexComponents(double value, int *d, int *m, int *s) |
| 251 |
{ |
| 252 |
*d = (int32_t)fabs(value); |
| 253 |
*m = (int32_t)((fabs(value) - *d) * 60.0); |
| 254 |
*s = (int32_t)rint(((fabs(value) - *d) * 60.0 - *m) * 60.0); |
| 255 |
|
| 256 |
// Special case if seconds are >= 59.5 so it will be rounded by rint above |
| 257 |
// to 60 |
| 258 |
if (*s == 60) |
| 259 |
{ |
| 260 |
*s = 0; |
| 261 |
*m += 1; |
| 262 |
} |
| 263 |
if (*m == 60) |
| 264 |
{ |
| 265 |
*m = 0; |
| 266 |
*d += 1; |
| 267 |
} |
| 268 |
|
| 269 |
if (value < 0) |
| 270 |
*d *= -1; |
| 271 |
} |
| 272 |
|
| 273 |
void getSexComponentsIID(double value, int *d, int *m, double *s) |
| 274 |
{ |
| 275 |
*d = (int32_t)fabs(value); |
| 276 |
*m = (int32_t)((fabs(value) - *d) * 60.0); |
| 277 |
*s = (double)(((fabs(value) - *d) * 60.0 - *m) * 60.0); |
| 278 |
|
| 279 |
if (value < 0) |
| 280 |
*d *= -1; |
| 281 |
} |
| 282 |
|
| 283 |
/* fill buf with properly formatted INumber string. return length */ |
| 284 |
int numberFormat(char *buf, const char *format, double value) |
| 285 |
{ |
| 286 |
int w, f, s; |
| 287 |
char m; |
| 288 |
|
| 289 |
if (sscanf(format, "%%%d.%d%c", &w, &f, &m) == 3 && m == 'm') |
| 290 |
{ |
| 291 |
/* INDI sexi format */ |
| 292 |
switch (f) |
| 293 |
{ |
| 294 |
case 9: s = 360000; break; |
| 295 |
case 8: s = 36000; break; |
| 296 |
case 6: s = 3600; break; |
| 297 |
case 5: s = 600; break; |
| 298 |
default: s = 60; break; |
| 299 |
} |
| 300 |
return (fs_sexa(buf, value, w - f, s)); |
| 301 |
} |
| 302 |
else |
| 303 |
{ |
| 304 |
/* normal printf format */ |
| 305 |
return (snprintf(buf, MAXINDIFORMAT, format, value)); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/* log message locally. |
| 310 |
* this has nothing to do with XML or any Clients. |
| 311 |
*/ |
| 312 |
void IDLog(const char *fmt, ...) |
| 313 |
{ |
| 314 |
va_list ap; |
| 315 |
/* JM: Since all INDI's stderr are timestampped now, we don't need to time stamp ID Log */ |
| 316 |
/*fprintf (stderr, "%s ", timestamp());*/ |
| 317 |
va_start(ap, fmt); |
| 318 |
vfprintf(stderr, fmt, ap); |
| 319 |
va_end(ap); |
| 320 |
} |
| 321 |
|
| 322 |
double time_ns() |
| 323 |
{ |
| 324 |
struct timespec ts; |
| 325 |
#ifdef __APPLE__ // OS X does not have clock_gettime, use clock_get_time |
| 326 |
clock_serv_t cclock; |
| 327 |
mach_timespec_t mts; |
| 328 |
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); |
| 329 |
clock_get_time(cclock, &mts); |
| 330 |
mach_port_deallocate(mach_task_self(), cclock); |
| 331 |
ts.tv_sec = mts.tv_sec; |
| 332 |
ts.tv_nsec = mts.tv_nsec; |
| 333 |
#else |
| 334 |
timespec_get(&ts, TIME_UTC); |
| 335 |
#endif |
| 336 |
return (double)ts.tv_sec+(double)(ts.tv_nsec%1000000000)/1000000000.0; |
| 337 |
} |
| 338 |
|
| 339 |
/* return current system time in message format */ |
| 340 |
const char *timestamp() |
| 341 |
{ |
| 342 |
static char ts[32]; |
| 343 |
struct tm *tp; |
| 344 |
time_t t; |
| 345 |
|
| 346 |
time(&t); |
| 347 |
tp = gmtime(&t); |
| 348 |
strftime(ts, sizeof(ts), "%Y-%m-%dT%H:%M:%S", tp); |
| 349 |
return (ts); |
| 350 |
} |
| 351 |
|
| 352 |
void tty_set_debug(int debug) |
| 353 |
{ |
| 354 |
tty_debug = debug; |
| 355 |
} |
| 356 |
|
| 357 |
void tty_set_gemini_udp_format(int enabled) |
| 358 |
{ |
| 359 |
tty_gemini_udp_format = enabled; |
| 360 |
} |
| 361 |
|
| 362 |
void tty_set_generic_udp_format(int enabled) |
| 363 |
{ |
| 364 |
tty_generic_udp_format = enabled; |
| 365 |
} |
| 366 |
|
| 367 |
void tty_clr_trailing_read_lf(int enabled) |
| 368 |
{ |
| 369 |
tty_clear_trailing_lf = enabled; |
| 370 |
} |
| 371 |
|
| 372 |
int tty_timeout(int fd, int timeout) |
| 373 |
{ |
| 374 |
return tty_timeout_microseconds(fd, timeout, 0); |
| 375 |
} |
| 376 |
|
| 377 |
int tty_timeout_microseconds(int fd, long timeout_seconds, long timeout_microseconds) |
| 378 |
{ |
| 379 |
#if defined(_WIN32) || defined(ANDROID) |
| 380 |
INDI_UNUSED(fd); |
| 381 |
INDI_UNUSED(timeout_seconds); |
| 382 |
INDI_UNUSED(timeout_microseconds); |
| 383 |
return TTY_ERRNO; |
| 384 |
#else |
| 385 |
|
| 386 |
if (fd == -1) |
| 387 |
return TTY_ERRNO; |
| 388 |
|
| 389 |
struct timeval tv; |
| 390 |
fd_set readout; |
| 391 |
int retval; |
| 392 |
|
| 393 |
FD_ZERO(&readout); |
| 394 |
FD_SET(fd, &readout); |
| 395 |
|
| 396 |
/* wait for 'timeout' seconds + microseconds */ |
| 397 |
tv.tv_sec = timeout_seconds; |
| 398 |
tv.tv_usec = timeout_microseconds; |
| 399 |
|
| 400 |
/* Wait till we have a change in the fd status */ |
| 401 |
retval = select(fd + 1, &readout, NULL, NULL, &tv); |
| 402 |
|
| 403 |
/* Return 0 on successful fd change */ |
| 404 |
if (retval > 0) |
| 405 |
return TTY_OK; |
| 406 |
/* Return -1 due to an error */ |
| 407 |
else if (retval == -1) |
| 408 |
return TTY_SELECT_ERROR; |
| 409 |
/* Return -2 if time expires before anything interesting happens */ |
| 410 |
else |
| 411 |
return TTY_TIME_OUT; |
| 412 |
|
| 413 |
#endif |
| 414 |
} |
| 415 |
|
| 416 |
int tty_write(int fd, const char *buf, int nbytes, int *nbytes_written) |
| 417 |
{ |
| 418 |
#ifdef _WIN32 |
| 419 |
return TTY_ERRNO; |
| 420 |
#else |
| 421 |
int geminiBuffer[66]={0}; |
| 422 |
char *buffer = (char *)buf; |
| 423 |
|
| 424 |
if (tty_gemini_udp_format) |
| 425 |
{ |
| 426 |
buffer = (char*)geminiBuffer; |
| 427 |
geminiBuffer[0] = ++tty_sequence_number; |
| 428 |
geminiBuffer[1] = 0; |
| 429 |
memcpy((char *)&geminiBuffer[2], buf, nbytes); |
| 430 |
// Add on the 8 bytes for the header and 1 byte for the null terminator |
| 431 |
nbytes += 9; |
| 432 |
} |
| 433 |
|
| 434 |
if (fd == -1) |
| 435 |
return TTY_ERRNO; |
| 436 |
|
| 437 |
int bytes_w = 0; |
| 438 |
*nbytes_written = 0; |
| 439 |
|
| 440 |
if (tty_debug) |
| 441 |
{ |
| 442 |
int i = 0; |
| 443 |
for (i = 0; i < nbytes; i++) |
| 444 |
IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, i, (unsigned char)buf[i], buf[i]); |
| 445 |
} |
| 446 |
|
| 447 |
while (nbytes > 0) |
| 448 |
{ |
| 449 |
bytes_w = write(fd, buffer + (*nbytes_written), nbytes); |
| 450 |
|
| 451 |
if (bytes_w < 0) |
| 452 |
return TTY_WRITE_ERROR; |
| 453 |
|
| 454 |
*nbytes_written += bytes_w; |
| 455 |
nbytes -= bytes_w; |
| 456 |
} |
| 457 |
|
| 458 |
if (tty_gemini_udp_format) |
| 459 |
*nbytes_written -= 9; |
| 460 |
|
| 461 |
return TTY_OK; |
| 462 |
|
| 463 |
#endif |
| 464 |
} |
| 465 |
|
| 466 |
int tty_write_string(int fd, const char *buf, int *nbytes_written) |
| 467 |
{ |
| 468 |
unsigned int nbytes; |
| 469 |
nbytes = strlen(buf); |
| 470 |
|
| 471 |
return tty_write(fd, buf, nbytes, nbytes_written); |
| 472 |
} |
| 473 |
|
| 474 |
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read) |
| 475 |
{ |
| 476 |
return tty_read_expanded(fd, buf, nbytes, timeout, 0, nbytes_read); |
| 477 |
} |
| 478 |
|
| 479 |
int tty_read_expanded(int fd, char *buf, int nbytes, long timeout_seconds, long timeout_microseconds, int *nbytes_read) |
| 480 |
{ |
| 481 |
#ifdef _WIN32 |
| 482 |
return TTY_ERRNO; |
| 483 |
#else |
| 484 |
|
| 485 |
if (fd == -1) |
| 486 |
return TTY_ERRNO; |
| 487 |
|
| 488 |
int numBytesToRead = nbytes; |
| 489 |
int bytesRead = 0; |
| 490 |
int err = 0; |
| 491 |
*nbytes_read = 0; |
| 492 |
|
| 493 |
if (nbytes <= 0) |
| 494 |
return TTY_PARAM_ERROR; |
| 495 |
|
| 496 |
if (tty_debug) |
| 497 |
IDLog("%s: Request to read %d bytes with %ld s, %ld us timeout for fd %d\n", __FUNCTION__, nbytes, timeout_seconds, timeout_microseconds, fd); |
| 498 |
|
| 499 |
char geminiBuffer[257]={0}; |
| 500 |
char* buffer = buf; |
| 501 |
|
| 502 |
if (tty_gemini_udp_format) |
| 503 |
{ |
| 504 |
numBytesToRead = nbytes + 8; |
| 505 |
buffer = geminiBuffer; |
| 506 |
} |
| 507 |
|
| 508 |
while (numBytesToRead > 0) |
| 509 |
{ |
| 510 |
if ((err = tty_timeout_microseconds(fd, timeout_seconds, timeout_microseconds))) |
| 511 |
return err; |
| 512 |
|
| 513 |
bytesRead = read(fd, buffer + (*nbytes_read), ((uint32_t)numBytesToRead)); |
| 514 |
|
| 515 |
if (bytesRead < 0) |
| 516 |
return TTY_READ_ERROR; |
| 517 |
|
| 518 |
if (tty_debug) |
| 519 |
{ |
| 520 |
IDLog("%d bytes read and %d bytes remaining...\n", bytesRead, numBytesToRead - bytesRead); |
| 521 |
int i = 0; |
| 522 |
for (i = *nbytes_read; i < (*nbytes_read + bytesRead); i++) |
| 523 |
IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, i, (unsigned char)buf[i], buf[i]); |
| 524 |
} |
| 525 |
|
| 526 |
if (*nbytes_read == 0 && tty_clear_trailing_lf && *buffer == 0x0A) |
| 527 |
{ |
| 528 |
if (tty_debug) |
| 529 |
IDLog("%s: Cleared LF char left in buf\n", __FUNCTION__); |
| 530 |
|
| 531 |
memcpy(buffer, buffer+1,bytesRead); |
| 532 |
--bytesRead; |
| 533 |
} |
| 534 |
|
| 535 |
*nbytes_read += bytesRead; |
| 536 |
numBytesToRead -= bytesRead; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
if (tty_gemini_udp_format) |
| 541 |
{ |
| 542 |
int *intSizedBuffer = (int *)geminiBuffer; |
| 543 |
if (intSizedBuffer[0] != tty_sequence_number) |
| 544 |
{ |
| 545 |
// Not the right reply just do the read again. |
| 546 |
return tty_read_expanded(fd, buf, nbytes, timeout_seconds, timeout_microseconds, nbytes_read); |
| 547 |
} |
| 548 |
|
| 549 |
*nbytes_read -= 8; |
| 550 |
memcpy(buf, geminiBuffer+8, *nbytes_read); |
| 551 |
} |
| 552 |
|
| 553 |
return TTY_OK; |
| 554 |
|
| 555 |
#endif |
| 556 |
} |
| 557 |
|
| 558 |
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read) |
| 559 |
{ |
| 560 |
return tty_read_section_expanded(fd, buf, stop_char, (long) timeout, (long) 0, nbytes_read); |
| 561 |
} |
| 562 |
|
| 563 |
int tty_read_section_expanded(int fd, char *buf, char stop_char, long timeout_seconds, long timeout_microseconds, int *nbytes_read) |
| 564 |
{ |
| 565 |
#ifdef _WIN32 |
| 566 |
return TTY_ERRNO; |
| 567 |
#else |
| 568 |
|
| 569 |
char readBuffer[257]={0}; |
| 570 |
|
| 571 |
if (fd == -1) |
| 572 |
return TTY_ERRNO; |
| 573 |
|
| 574 |
int bytesRead = 0; |
| 575 |
int err = TTY_OK; |
| 576 |
*nbytes_read = 0; |
| 577 |
|
| 578 |
uint8_t *read_char = 0; |
| 579 |
|
| 580 |
if (tty_debug) |
| 581 |
IDLog("%s: Request to read until stop char '%#02X' with %ld s %ld us timeout for fd %d\n", __FUNCTION__, stop_char, timeout_seconds, timeout_microseconds, fd); |
| 582 |
|
| 583 |
if (tty_gemini_udp_format) |
| 584 |
{ |
| 585 |
bytesRead = read(fd, readBuffer, 255); |
| 586 |
|
| 587 |
if (bytesRead < 0) |
| 588 |
return TTY_READ_ERROR; |
| 589 |
|
| 590 |
int *intSizedBuffer = (int *)readBuffer; |
| 591 |
if (intSizedBuffer[0] != tty_sequence_number) |
| 592 |
{ |
| 593 |
// Not the right reply just do the read again. |
| 594 |
return tty_read_section_expanded(fd, buf, stop_char, timeout_seconds, timeout_microseconds, nbytes_read); |
| 595 |
} |
| 596 |
|
| 597 |
for (int index = 8; index < bytesRead; index++) |
| 598 |
{ |
| 599 |
(*nbytes_read)++; |
| 600 |
|
| 601 |
if (*(readBuffer+index) == stop_char) |
| 602 |
{ |
| 603 |
strncpy(buf, readBuffer+8, *nbytes_read); |
| 604 |
return TTY_OK; |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
else if (tty_generic_udp_format) |
| 609 |
{ |
| 610 |
bytesRead = read(fd, readBuffer, 255); |
| 611 |
if (bytesRead < 0) |
| 612 |
return TTY_READ_ERROR; |
| 613 |
for (int index = 0; index < bytesRead; index++) |
| 614 |
{ |
| 615 |
(*nbytes_read)++; |
| 616 |
|
| 617 |
if (*(readBuffer+index) == stop_char) |
| 618 |
{ |
| 619 |
strncpy(buf, readBuffer, *nbytes_read); |
| 620 |
return TTY_OK; |
| 621 |
} |
| 622 |
} |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
for (;;) |
| 627 |
{ |
| 628 |
if ((err = tty_timeout_microseconds(fd, timeout_seconds, timeout_microseconds))) |
| 629 |
return err; |
| 630 |
|
| 631 |
read_char = (uint8_t*)(buf + *nbytes_read); |
| 632 |
bytesRead = read(fd, read_char, 1); |
| 633 |
|
| 634 |
if (bytesRead < 0) |
| 635 |
return TTY_READ_ERROR; |
| 636 |
|
| 637 |
if (tty_debug) |
| 638 |
IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, (*nbytes_read), *read_char, *read_char); |
| 639 |
|
| 640 |
if (!(tty_clear_trailing_lf && *read_char == 0X0A && *nbytes_read == 0)) |
| 641 |
(*nbytes_read)++; |
| 642 |
else { |
| 643 |
if (tty_debug) |
| 644 |
IDLog("%s: Cleared LF char left in buf\n", __FUNCTION__); |
| 645 |
} |
| 646 |
|
| 647 |
if (*read_char == stop_char) { |
| 648 |
return TTY_OK; |
| 649 |
} |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
return TTY_TIME_OUT; |
| 654 |
|
| 655 |
#endif |
| 656 |
} |
| 657 |
|
| 658 |
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read) |
| 659 |
{ |
| 660 |
#ifdef _WIN32 |
| 661 |
return TTY_ERRNO; |
| 662 |
#else |
| 663 |
|
| 664 |
if (fd == -1) |
| 665 |
return TTY_ERRNO; |
| 666 |
|
| 667 |
// For Gemini |
| 668 |
if (tty_gemini_udp_format || tty_generic_udp_format) |
| 669 |
return tty_read_section(fd, buf, stop_char, timeout, nbytes_read); |
| 670 |
|
| 671 |
int bytesRead = 0; |
| 672 |
int err = TTY_OK; |
| 673 |
*nbytes_read = 0; |
| 674 |
uint8_t *read_char = 0; |
| 675 |
memset(buf, 0, nsize); |
| 676 |
|
| 677 |
if (tty_debug) |
| 678 |
IDLog("%s: Request to read until stop char '%#02X' with %d timeout for fd %d\n", __FUNCTION__, stop_char, timeout, fd); |
| 679 |
|
| 680 |
for (;;) |
| 681 |
{ |
| 682 |
if ((err = tty_timeout(fd, timeout))) |
| 683 |
return err; |
| 684 |
|
| 685 |
read_char = (uint8_t*)(buf + *nbytes_read); |
| 686 |
bytesRead = read(fd, read_char, 1); |
| 687 |
|
| 688 |
if (bytesRead < 0) |
| 689 |
return TTY_READ_ERROR; |
| 690 |
|
| 691 |
if (tty_debug) |
| 692 |
IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, (*nbytes_read), *read_char, *read_char); |
| 693 |
|
| 694 |
if (!(tty_clear_trailing_lf && *read_char == 0X0A && *nbytes_read == 0)) |
| 695 |
(*nbytes_read)++; |
| 696 |
else { |
| 697 |
if (tty_debug) |
| 698 |
IDLog("%s: Cleared LF char left in buf\n", __FUNCTION__); |
| 699 |
} |
| 700 |
|
| 701 |
if (*read_char == stop_char) |
| 702 |
return TTY_OK; |
| 703 |
else if (*nbytes_read >= nsize) |
| 704 |
return TTY_OVERFLOW; |
| 705 |
} |
| 706 |
|
| 707 |
#endif |
| 708 |
} |
| 709 |
|
| 710 |
#if defined(BSD) && !defined(__GNU__) |
| 711 |
// BSD - OSX version |
| 712 |
int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd) |
| 713 |
{ |
| 714 |
int t_fd = -1; |
| 715 |
int bps; |
| 716 |
char msg[80]; |
| 717 |
int handshake; |
| 718 |
struct termios tty_setting; |
| 719 |
|
| 720 |
// Open the serial port read/write, with no controlling terminal, and don't wait for a connection. |
| 721 |
// The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking. |
| 722 |
// See open(2) ("man 2 open") for details. |
| 723 |
|
| 724 |
t_fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 725 |
if (t_fd == -1) |
| 726 |
{ |
| 727 |
IDLog("Error opening serial port (%s) - %s(%d).\n", device, strerror(errno), errno); |
| 728 |
goto error; |
| 729 |
} |
| 730 |
|
| 731 |
// Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed |
| 732 |
// unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned |
| 733 |
// processes. |
| 734 |
// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details. |
| 735 |
|
| 736 |
if (ioctl(t_fd, TIOCEXCL) == -1) |
| 737 |
{ |
| 738 |
IDLog("Error setting TIOCEXCL on %s - %s(%d).\n", device, strerror(errno), errno); |
| 739 |
goto error; |
| 740 |
} |
| 741 |
|
| 742 |
// Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block. |
| 743 |
// See fcntl(2) ("man 2 fcntl") for details. |
| 744 |
|
| 745 |
if (fcntl(t_fd, F_SETFL, 0) == -1) |
| 746 |
{ |
| 747 |
IDLog("Error clearing O_NONBLOCK %s - %s(%d).\n", device, strerror(errno), errno); |
| 748 |
goto error; |
| 749 |
} |
| 750 |
|
| 751 |
// Get the current options and save them so we can restore the default settings later. |
| 752 |
if (tcgetattr(t_fd, &tty_setting) == -1) |
| 753 |
{ |
| 754 |
IDLog("Error getting tty attributes %s - %s(%d).\n", device, strerror(errno), errno); |
| 755 |
goto error; |
| 756 |
} |
| 757 |
|
| 758 |
// Set raw input (non-canonical) mode, with reads blocking until either a single character |
| 759 |
// has been received or a one second timeout expires. |
| 760 |
// See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details. |
| 761 |
|
| 762 |
cfmakeraw(&tty_setting); |
| 763 |
tty_setting.c_cc[VMIN] = 1; |
| 764 |
tty_setting.c_cc[VTIME] = 10; |
| 765 |
|
| 766 |
// The baud rate, word length, and handshake options can be set as follows: |
| 767 |
switch (bit_rate) |
| 768 |
{ |
| 769 |
case 0: bps = B0; break; |
| 770 |
case 50: bps = B50; break; |
| 771 |
case 75: bps = B75; break; |
| 772 |
case 110: bps = B110; break; |
| 773 |
case 134: bps = B134; break; |
| 774 |
case 150: bps = B150; break; |
| 775 |
case 200: bps = B200; break; |
| 776 |
case 300: bps = B300; break; |
| 777 |
case 600: bps = B600; break; |
| 778 |
case 1200: bps = B1200; break; |
| 779 |
case 1800: bps = B1800; break; |
| 780 |
case 2400: bps = B2400; break; |
| 781 |
case 4800: bps = B4800; break; |
| 782 |
case 9600: bps = B9600; break; |
| 783 |
case 19200: bps = B19200; break; |
| 784 |
case 38400: bps = B38400; break; |
| 785 |
case 57600: bps = B57600; break; |
| 786 |
case 115200: bps = B115200; break; |
| 787 |
case 230400: bps = B230400; break; |
| 788 |
#if !defined(__APPLE__) && !defined(__FreeBSD__) |
| 789 |
case 460800: bps = B460800; break; |
| 790 |
case 576000: bps = B576000; break; |
| 791 |
case 921600: bps = B921600; break; |
| 792 |
#endif |
| 793 |
default: |
| 794 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid bit rate.", bit_rate) < 0) |
| 795 |
perror(NULL); |
| 796 |
else |
| 797 |
perror(msg); |
| 798 |
return TTY_PARAM_ERROR; |
| 799 |
} |
| 800 |
|
| 801 |
cfsetspeed(&tty_setting, bps); // Set baud rate |
| 802 |
/* word size */ |
| 803 |
switch (word_size) |
| 804 |
{ |
| 805 |
case 5: tty_setting.c_cflag |= CS5; break; |
| 806 |
case 6: tty_setting.c_cflag |= CS6; break; |
| 807 |
case 7: tty_setting.c_cflag |= CS7; break; |
| 808 |
case 8: tty_setting.c_cflag |= CS8; break; |
| 809 |
default: |
| 810 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid data bit count.", word_size) < 0) |
| 811 |
perror(NULL); |
| 812 |
else |
| 813 |
perror(msg); |
| 814 |
|
| 815 |
return TTY_PARAM_ERROR; |
| 816 |
} |
| 817 |
|
| 818 |
/* parity */ |
| 819 |
switch (parity) |
| 820 |
{ |
| 821 |
case PARITY_NONE: |
| 822 |
break; |
| 823 |
case PARITY_EVEN: |
| 824 |
tty_setting.c_cflag |= PARENB; |
| 825 |
break; |
| 826 |
case PARITY_ODD: |
| 827 |
tty_setting.c_cflag |= PARENB | PARODD; |
| 828 |
break; |
| 829 |
default: |
| 830 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid parity selection value.", parity) < 0) |
| 831 |
perror(NULL); |
| 832 |
else |
| 833 |
perror(msg); |
| 834 |
|
| 835 |
return TTY_PARAM_ERROR; |
| 836 |
} |
| 837 |
|
| 838 |
/* stop_bits */ |
| 839 |
switch (stop_bits) |
| 840 |
{ |
| 841 |
case 1: |
| 842 |
break; |
| 843 |
case 2: |
| 844 |
tty_setting.c_cflag |= CSTOPB; |
| 845 |
break; |
| 846 |
default: |
| 847 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid stop bit count.", stop_bits) < 0) |
| 848 |
perror(NULL); |
| 849 |
else |
| 850 |
perror(msg); |
| 851 |
|
| 852 |
return TTY_PARAM_ERROR; |
| 853 |
} |
| 854 |
|
| 855 |
#if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4) |
| 856 |
// Starting with Tiger, the IOSSIOSPEED ioctl can be used to set arbitrary baud rates |
| 857 |
// other than those specified by POSIX. The driver for the underlying serial hardware |
| 858 |
// ultimately determines which baud rates can be used. This ioctl sets both the input |
| 859 |
// and output speed. |
| 860 |
|
| 861 |
speed_t speed = 14400; // Set 14400 baud |
| 862 |
if (ioctl(t_fd, IOSSIOSPEED, &speed) == -1) |
| 863 |
{ |
| 864 |
IDLog("Error calling ioctl(..., IOSSIOSPEED, ...) - %s(%d).\n", strerror(errno), errno); |
| 865 |
} |
| 866 |
#endif |
| 867 |
|
| 868 |
// Cause the new options to take effect immediately. |
| 869 |
if (tcsetattr(t_fd, TCSANOW, &tty_setting) == -1) |
| 870 |
{ |
| 871 |
IDLog("Error setting tty attributes %s - %s(%d).\n", device, strerror(errno), errno); |
| 872 |
goto error; |
| 873 |
} |
| 874 |
|
| 875 |
// To set the modem handshake lines, use the following ioctls. |
| 876 |
// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details. |
| 877 |
|
| 878 |
if (ioctl(t_fd, TIOCSDTR) == -1) // Assert Data Terminal Ready (DTR) |
| 879 |
{ |
| 880 |
IDLog("Error asserting DTR %s - %s(%d).\n", device, strerror(errno), errno); |
| 881 |
} |
| 882 |
|
| 883 |
if (ioctl(t_fd, TIOCCDTR) == -1) // Clear Data Terminal Ready (DTR) |
| 884 |
{ |
| 885 |
IDLog("Error clearing DTR %s - %s(%d).\n", device, strerror(errno), errno); |
| 886 |
} |
| 887 |
|
| 888 |
handshake = TIOCM_DTR | TIOCM_RTS | TIOCM_CTS | TIOCM_DSR; |
| 889 |
if (ioctl(t_fd, TIOCMSET, &handshake) == -1) |
| 890 |
// Set the modem lines depending on the bits set in handshake |
| 891 |
{ |
| 892 |
IDLog("Error setting handshake lines %s - %s(%d).\n", device, strerror(errno), errno); |
| 893 |
} |
| 894 |
|
| 895 |
// To read the state of the modem lines, use the following ioctl. |
| 896 |
// See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details. |
| 897 |
|
| 898 |
if (ioctl(t_fd, TIOCMGET, &handshake) == -1) |
| 899 |
// Store the state of the modem lines in handshake |
| 900 |
{ |
| 901 |
IDLog("Error getting handshake lines %s - %s(%d).\n", device, strerror(errno), errno); |
| 902 |
} |
| 903 |
|
| 904 |
IDLog("Handshake lines currently set to %d\n", handshake); |
| 905 |
|
| 906 |
#if defined(MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3) |
| 907 |
unsigned long mics = 1UL; |
| 908 |
|
| 909 |
// Set the receive latency in microseconds. Serial drivers use this value to determine how often to |
| 910 |
// dequeue characters received by the hardware. Most applications don't need to set this value: if an |
| 911 |
// app reads lines of characters, the app can't do anything until the line termination character has been |
| 912 |
// received anyway. The most common applications which are sensitive to read latency are MIDI and IrDA |
| 913 |
// applications. |
| 914 |
|
| 915 |
if (ioctl(t_fd, IOSSDATALAT, &mics) == -1) |
| 916 |
{ |
| 917 |
// set latency to 1 microsecond |
| 918 |
IDLog("Error setting read latency %s - %s(%d).\n", device, strerror(errno), errno); |
| 919 |
goto error; |
| 920 |
} |
| 921 |
#endif |
| 922 |
|
| 923 |
*fd = t_fd; |
| 924 |
/* return success */ |
| 925 |
return TTY_OK; |
| 926 |
|
| 927 |
// Failure path |
| 928 |
error: |
| 929 |
if (t_fd != -1) |
| 930 |
{ |
| 931 |
close(t_fd); |
| 932 |
*fd = -1; |
| 933 |
} |
| 934 |
|
| 935 |
return TTY_PORT_FAILURE; |
| 936 |
} |
| 937 |
#else |
| 938 |
int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd) |
| 939 |
{ |
| 940 |
#ifdef _WIN32 |
| 941 |
return TTY_PORT_FAILURE; |
| 942 |
|
| 943 |
#else |
| 944 |
int t_fd = -1; |
| 945 |
int i = 0; |
| 946 |
char msg[128]={0}; |
| 947 |
int bps; |
| 948 |
struct termios tty_setting; |
| 949 |
// Check for bluetooth & virtualcom which can be shared |
| 950 |
int ignore_exclusive_close = strstr(device, "rfcomm") || strstr(device, "Bluetooth") || strstr(device, "virtualcom"); |
| 951 |
|
| 952 |
|
| 953 |
// Open as Read/Write, no fnctl, and close on exclusive |
| 954 |
for (i = 0 ; i < 3 ; i++) |
| 955 |
{ |
| 956 |
// Do not use O_CLOEXEC when ignored |
| 957 |
t_fd = open(device, O_RDWR | O_NOCTTY | (ignore_exclusive_close ? 0 : O_CLOEXEC)); |
| 958 |
if (t_fd > 0) |
| 959 |
break; |
| 960 |
else |
| 961 |
{ |
| 962 |
*fd = -1; |
| 963 |
if (errno == EBUSY) |
| 964 |
{ |
| 965 |
usleep(1e6); |
| 966 |
continue; |
| 967 |
} |
| 968 |
else |
| 969 |
return TTY_PORT_FAILURE; |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
if (t_fd == -1) |
| 974 |
return TTY_PORT_BUSY; |
| 975 |
|
| 976 |
// Set port in exclusive mode to prevent other non-root processes from opening it. |
| 977 |
// JM 2019-08-12: Do not set it when ignored |
| 978 |
if (ignore_exclusive_close == 0 && ioctl(t_fd, TIOCEXCL) == -1) |
| 979 |
{ |
| 980 |
perror("tty_connect: Error setting TIOCEXC."); |
| 981 |
close(t_fd); |
| 982 |
return TTY_PORT_FAILURE; |
| 983 |
} |
| 984 |
|
| 985 |
// Get the current options and save them so we can restore the default settings later. |
| 986 |
if (tcgetattr(t_fd, &tty_setting) == -1) |
| 987 |
{ |
| 988 |
perror("tty_connect: failed getting tty attributes."); |
| 989 |
close(t_fd); |
| 990 |
return TTY_PORT_FAILURE; |
| 991 |
} |
| 992 |
|
| 993 |
/* Control Modes |
| 994 |
Set bps rate */ |
| 995 |
switch (bit_rate) |
| 996 |
{ |
| 997 |
case 0: bps = B0; break; |
| 998 |
case 50: bps = B50; break; |
| 999 |
case 75: bps = B75; break; |
| 1000 |
case 110: bps = B110; break; |
| 1001 |
case 134: bps = B134; break; |
| 1002 |
case 150: bps = B150; break; |
| 1003 |
case 200: bps = B200; break; |
| 1004 |
case 300: bps = B300; break; |
| 1005 |
case 600: bps = B600; break; |
| 1006 |
case 1200: bps = B1200; break; |
| 1007 |
case 1800: bps = B1800; break; |
| 1008 |
case 2400: bps = B2400; break; |
| 1009 |
case 4800: bps = B4800; break; |
| 1010 |
case 9600: bps = B9600; break; |
| 1011 |
case 19200: bps = B19200; break; |
| 1012 |
case 38400: bps = B38400; break; |
| 1013 |
case 57600: bps = B57600; break; |
| 1014 |
case 115200: bps = B115200; break; |
| 1015 |
case 230400: bps = B230400; break; |
| 1016 |
case 460800: bps = B460800; break; |
| 1017 |
case 576000: bps = B576000; break; |
| 1018 |
case 921600: bps = B921600; break; |
| 1019 |
default: |
| 1020 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid bit rate.", bit_rate) < 0) |
| 1021 |
perror(NULL); |
| 1022 |
else |
| 1023 |
perror(msg); |
| 1024 |
close(t_fd); |
| 1025 |
return TTY_PARAM_ERROR; |
| 1026 |
} |
| 1027 |
if ((cfsetispeed(&tty_setting, bps) < 0) || (cfsetospeed(&tty_setting, bps) < 0)) |
| 1028 |
{ |
| 1029 |
perror("tty_connect: failed setting bit rate."); |
| 1030 |
close(t_fd); |
| 1031 |
return TTY_PORT_FAILURE; |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* Control Modes |
| 1035 |
set no flow control word size, parity and stop bits. |
| 1036 |
Also don't hangup automatically and ignore modem status. |
| 1037 |
Finally enable receiving characters. */ |
| 1038 |
tty_setting.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | HUPCL | CRTSCTS); |
| 1039 |
tty_setting.c_cflag |= (CLOCAL | CREAD); |
| 1040 |
|
| 1041 |
/* word size */ |
| 1042 |
switch (word_size) |
| 1043 |
{ |
| 1044 |
case 5: tty_setting.c_cflag |= CS5; break; |
| 1045 |
case 6: tty_setting.c_cflag |= CS6; break; |
| 1046 |
case 7: tty_setting.c_cflag |= CS7; break; |
| 1047 |
case 8: tty_setting.c_cflag |= CS8; break; |
| 1048 |
default: |
| 1049 |
|
| 1050 |
fprintf(stderr, "Default\n"); |
| 1051 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid data bit count.", word_size) < 0) |
| 1052 |
perror(NULL); |
| 1053 |
else |
| 1054 |
perror(msg); |
| 1055 |
|
| 1056 |
close(t_fd); |
| 1057 |
return TTY_PARAM_ERROR; |
| 1058 |
} |
| 1059 |
|
| 1060 |
/* parity */ |
| 1061 |
switch (parity) |
| 1062 |
{ |
| 1063 |
case PARITY_NONE: |
| 1064 |
break; |
| 1065 |
case PARITY_EVEN: |
| 1066 |
tty_setting.c_cflag |= PARENB; |
| 1067 |
break; |
| 1068 |
case PARITY_ODD: |
| 1069 |
tty_setting.c_cflag |= PARENB | PARODD; |
| 1070 |
break; |
| 1071 |
default: |
| 1072 |
|
| 1073 |
fprintf(stderr, "Default1\n"); |
| 1074 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid parity selection value.", parity) < 0) |
| 1075 |
perror(NULL); |
| 1076 |
else |
| 1077 |
perror(msg); |
| 1078 |
|
| 1079 |
close(t_fd); |
| 1080 |
return TTY_PARAM_ERROR; |
| 1081 |
} |
| 1082 |
|
| 1083 |
/* stop_bits */ |
| 1084 |
switch (stop_bits) |
| 1085 |
{ |
| 1086 |
case 1: |
| 1087 |
break; |
| 1088 |
case 2: |
| 1089 |
tty_setting.c_cflag |= CSTOPB; |
| 1090 |
break; |
| 1091 |
default: |
| 1092 |
fprintf(stderr, "Default2\n"); |
| 1093 |
if (snprintf(msg, sizeof(msg), "tty_connect: %d is not a valid stop bit count.", stop_bits) < 0) |
| 1094 |
perror(NULL); |
| 1095 |
else |
| 1096 |
perror(msg); |
| 1097 |
|
| 1098 |
close(t_fd); |
| 1099 |
return TTY_PARAM_ERROR; |
| 1100 |
} |
| 1101 |
/* Control Modes complete */ |
| 1102 |
|
| 1103 |
/* Ignore bytes with parity errors and make terminal raw and dumb.*/ |
| 1104 |
tty_setting.c_iflag &= ~(PARMRK | ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON | IXANY); |
| 1105 |
tty_setting.c_iflag |= INPCK | IGNPAR | IGNBRK; |
| 1106 |
|
| 1107 |
/* Raw output.*/ |
| 1108 |
tty_setting.c_oflag &= ~(OPOST | ONLCR); |
| 1109 |
|
| 1110 |
/* Local Modes |
| 1111 |
Don't echo characters. Don't generate signals. |
| 1112 |
Don't process any characters.*/ |
| 1113 |
tty_setting.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN | NOFLSH | TOSTOP); |
| 1114 |
tty_setting.c_lflag |= NOFLSH; |
| 1115 |
|
| 1116 |
/* blocking read until 1 char arrives */ |
| 1117 |
tty_setting.c_cc[VMIN] = 1; |
| 1118 |
tty_setting.c_cc[VTIME] = 0; |
| 1119 |
|
| 1120 |
/* now clear input and output buffers and activate the new terminal settings */ |
| 1121 |
tcflush(t_fd, TCIOFLUSH); |
| 1122 |
if (tcsetattr(t_fd, TCSANOW, &tty_setting)) |
| 1123 |
{ |
| 1124 |
perror("tty_connect: failed setting attributes on serial port."); |
| 1125 |
tty_disconnect(t_fd); |
| 1126 |
return TTY_PORT_FAILURE; |
| 1127 |
} |
| 1128 |
|
| 1129 |
*fd = t_fd; |
| 1130 |
/* return success */ |
| 1131 |
return TTY_OK; |
| 1132 |
#endif |
| 1133 |
} |
| 1134 |
// Unix - Linux version |
| 1135 |
|
| 1136 |
#endif |
| 1137 |
|
| 1138 |
int tty_disconnect(int fd) |
| 1139 |
{ |
| 1140 |
if (fd == -1) |
| 1141 |
return TTY_ERRNO; |
| 1142 |
|
| 1143 |
#ifdef _WIN32 |
| 1144 |
return TTY_ERRNO; |
| 1145 |
#else |
| 1146 |
int err; |
| 1147 |
tcflush(fd, TCIOFLUSH); |
| 1148 |
err = close(fd); |
| 1149 |
|
| 1150 |
if (err != 0) |
| 1151 |
return TTY_ERRNO; |
| 1152 |
|
| 1153 |
return TTY_OK; |
| 1154 |
#endif |
| 1155 |
} |
| 1156 |
|
| 1157 |
void tty_error_msg(int err_code, char *err_msg, int err_msg_len) |
| 1158 |
{ |
| 1159 |
switch (err_code) |
| 1160 |
{ |
| 1161 |
case TTY_OK: |
| 1162 |
snprintf(err_msg, err_msg_len, "No Error"); |
| 1163 |
break; |
| 1164 |
|
| 1165 |
case TTY_READ_ERROR: |
| 1166 |
snprintf(err_msg, err_msg_len, "Read Error: %s", strerror(errno)); |
| 1167 |
break; |
| 1168 |
|
| 1169 |
case TTY_WRITE_ERROR: |
| 1170 |
snprintf(err_msg, err_msg_len, "Write Error: %s", strerror(errno)); |
| 1171 |
break; |
| 1172 |
|
| 1173 |
case TTY_SELECT_ERROR: |
| 1174 |
snprintf(err_msg, err_msg_len, "Select Error: %s", strerror(errno)); |
| 1175 |
break; |
| 1176 |
|
| 1177 |
case TTY_TIME_OUT: |
| 1178 |
snprintf(err_msg, err_msg_len, "Timeout error"); |
| 1179 |
break; |
| 1180 |
|
| 1181 |
case TTY_PORT_FAILURE: |
| 1182 |
if (errno == EACCES) |
| 1183 |
snprintf(err_msg, err_msg_len, |
| 1184 |
"Port failure Error: %s. Try adding your user to the dialout group and restart (sudo adduser " |
| 1185 |
"$USER dialout)", |
| 1186 |
strerror(errno)); |
| 1187 |
else |
| 1188 |
snprintf(err_msg, err_msg_len, "Port failure Error: %s. Check if device is connected to this port.", |
| 1189 |
strerror(errno)); |
| 1190 |
|
| 1191 |
break; |
| 1192 |
|
| 1193 |
case TTY_PARAM_ERROR: |
| 1194 |
snprintf(err_msg, err_msg_len, "Parameter error"); |
| 1195 |
break; |
| 1196 |
|
| 1197 |
case TTY_ERRNO: |
| 1198 |
snprintf(err_msg, err_msg_len, "%s", strerror(errno)); |
| 1199 |
break; |
| 1200 |
|
| 1201 |
case TTY_OVERFLOW: |
| 1202 |
snprintf(err_msg, err_msg_len, "Read overflow"); |
| 1203 |
break; |
| 1204 |
|
| 1205 |
case TTY_PORT_BUSY: |
| 1206 |
snprintf(err_msg, err_msg_len, "Port is busy"); |
| 1207 |
break; |
| 1208 |
|
| 1209 |
default: |
| 1210 |
snprintf(err_msg, err_msg_len, "Error: unrecognized error code"); |
| 1211 |
break; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
double rangeHA(double r) |
| 1216 |
{ |
| 1217 |
double res = r; |
| 1218 |
while (res < -12.0) |
| 1219 |
res += 24.0; |
| 1220 |
while (res >= 12.0) |
| 1221 |
res -= 24.0; |
| 1222 |
return res; |
| 1223 |
} |
| 1224 |
|
| 1225 |
double range24(double r) |
| 1226 |
{ |
| 1227 |
double res = r; |
| 1228 |
while (res < 0.0) |
| 1229 |
res += 24.0; |
| 1230 |
while (res > 24.0) |
| 1231 |
res -= 24.0; |
| 1232 |
return res; |
| 1233 |
} |
| 1234 |
|
| 1235 |
double range360(double r) |
| 1236 |
{ |
| 1237 |
double res = r; |
| 1238 |
while (res < 0.0) |
| 1239 |
res += 360.0; |
| 1240 |
while (res > 360.0) |
| 1241 |
res -= 360.0; |
| 1242 |
return res; |
| 1243 |
} |
| 1244 |
|
| 1245 |
double rangeDec(double decdegrees) |
| 1246 |
{ |
| 1247 |
if ((decdegrees >= 270.0) && (decdegrees <= 360.0)) |
| 1248 |
return (decdegrees - 360.0); |
| 1249 |
if ((decdegrees >= 180.0) && (decdegrees < 270.0)) |
| 1250 |
return (180.0 - decdegrees); |
| 1251 |
if ((decdegrees >= 90.0) && (decdegrees < 180.0)) |
| 1252 |
return (180.0 - decdegrees); |
| 1253 |
return decdegrees; |
| 1254 |
} |
| 1255 |
|
| 1256 |
#if defined(HAVE_LIBNOVA) |
| 1257 |
double get_local_sidereal_time(double longitude) |
| 1258 |
{ |
| 1259 |
return range24(ln_get_apparent_sidereal_time(ln_get_julian_from_sys()) + longitude / 15.0); |
| 1260 |
} |
| 1261 |
|
| 1262 |
void get_hrz_from_equ(struct ln_equ_posn *object, struct ln_lnlat_posn *observer, double JD, struct ln_hrz_posn *position) |
| 1263 |
{ |
| 1264 |
ln_get_hrz_from_equ(object, observer, JD, position); |
| 1265 |
position->az -= 180; |
| 1266 |
if (position->az < 0) |
| 1267 |
position->az += 360; |
| 1268 |
} |
| 1269 |
|
| 1270 |
void get_equ_from_hrz(struct ln_hrz_posn *object, struct ln_lnlat_posn *observer, double JD, struct ln_equ_posn *position) |
| 1271 |
{ |
| 1272 |
struct ln_hrz_posn libnova_object; |
| 1273 |
libnova_object.az = object->az + 180; |
| 1274 |
if (libnova_object.az > 360) |
| 1275 |
libnova_object.az -= 360; |
| 1276 |
libnova_object.alt = object->alt; |
| 1277 |
|
| 1278 |
ln_get_equ_from_hrz(&libnova_object, observer, JD, position); |
| 1279 |
} |
| 1280 |
|
| 1281 |
#endif // HAVE_LIBNOVA |
| 1282 |
|
| 1283 |
double get_local_hour_angle(double sideral_time, double ra) |
| 1284 |
{ |
| 1285 |
double HA = sideral_time - ra; |
| 1286 |
return rangeHA(HA); |
| 1287 |
} |
| 1288 |
|
| 1289 |
void get_alt_az_coordinates(double Ha, double Dec, double Lat, double* Alt, double *Az) |
| 1290 |
{ |
| 1291 |
double alt, az; |
| 1292 |
Ha *= M_PI / 180.0; |
| 1293 |
Dec *= M_PI / 180.0; |
| 1294 |
Lat *= M_PI / 180.0; |
| 1295 |
alt = asin(sin(Dec) * sin(Lat) + cos(Dec) * cos(Lat) * cos(Ha)); |
| 1296 |
az = acos((sin(Dec) - sin(alt)*sin(Lat)) / (cos(alt) * cos(Lat))); |
| 1297 |
alt *= 180.0 / M_PI; |
| 1298 |
az *= 180.0 / M_PI; |
| 1299 |
if (sin(Ha) >= 0.0) |
| 1300 |
az = 360 - az; |
| 1301 |
*Alt = alt; |
| 1302 |
*Az = az; |
| 1303 |
} |
| 1304 |
|
| 1305 |
double estimate_geocentric_elevation(double Lat, double El) |
| 1306 |
{ |
| 1307 |
Lat *= M_PI / 180.0; |
| 1308 |
Lat = sin(Lat); |
| 1309 |
El += Lat * (EARTHRADIUSPOLAR - EARTHRADIUSEQUATORIAL); |
| 1310 |
return El; |
| 1311 |
} |
| 1312 |
|
| 1313 |
double estimate_field_rotation_rate(double Alt, double Az, double Lat) |
| 1314 |
{ |
| 1315 |
Alt *= M_PI / 180.0; |
| 1316 |
Az *= M_PI / 180.0; |
| 1317 |
Lat *= M_PI / 180.0; |
| 1318 |
double ret = cos(Lat) * cos(Az) / cos(Alt); |
| 1319 |
ret *= 180.0 / M_PI; |
| 1320 |
return ret; |
| 1321 |
} |
| 1322 |
|
| 1323 |
double estimate_field_rotation(double HA, double rate) |
| 1324 |
{ |
| 1325 |
HA *= rate; |
| 1326 |
while(HA >= 360.0) |
| 1327 |
HA -= 360.0; |
| 1328 |
while(HA < 0) |
| 1329 |
HA += 360.0; |
| 1330 |
return HA; |
| 1331 |
} |
| 1332 |
|
| 1333 |
double as2rad(double as) |
| 1334 |
{ |
| 1335 |
return as * M_PI / (60.0*60.0*12.0); |
| 1336 |
} |
| 1337 |
|
| 1338 |
double rad2as(double rad) |
| 1339 |
{ |
| 1340 |
return rad * (60.0*60.0*12.0) / M_PI; |
| 1341 |
} |
| 1342 |
|
| 1343 |
double estimate_distance(double parsecs, double parallax_radius) |
| 1344 |
{ |
| 1345 |
return parallax_radius / sin(as2rad(parsecs)); |
| 1346 |
} |
| 1347 |
|
| 1348 |
double m2au(double m) |
| 1349 |
{ |
| 1350 |
return m / ASTRONOMICALUNIT; |
| 1351 |
} |
| 1352 |
|
| 1353 |
double calc_delta_magnitude(double mag_ratio, double *spectrum, double *ref_spectrum, int spectrum_size) |
| 1354 |
{ |
| 1355 |
double delta_mag = 0; |
| 1356 |
for(int l = 0; l < spectrum_size; l++) { |
| 1357 |
delta_mag += spectrum[l] * mag_ratio * ref_spectrum[l] / spectrum[l]; |
| 1358 |
} |
| 1359 |
delta_mag /= spectrum_size; |
| 1360 |
return delta_mag; |
| 1361 |
} |
| 1362 |
|
| 1363 |
double calc_star_mass(double delta_mag, double ref_size) |
| 1364 |
{ |
| 1365 |
return delta_mag * ref_size; |
| 1366 |
} |
| 1367 |
|
| 1368 |
double estimate_orbit_radius(double obs_lambda, double ref_lambda, double period) |
| 1369 |
{ |
| 1370 |
return M_PI*2*DOPPLER(REDSHIFT(obs_lambda, ref_lambda), LIGHTSPEED)/period; |
| 1371 |
} |
| 1372 |
|
| 1373 |
double estimate_secondary_mass(double star_mass, double star_drift, double orbit_radius) |
| 1374 |
{ |
| 1375 |
return orbit_radius*pow(star_drift*orbit_radius, 3)*3*star_mass; |
| 1376 |
} |
| 1377 |
|
| 1378 |
double estimate_secondary_size(double star_size, double dropoff_ratio) |
| 1379 |
{ |
| 1380 |
return pow(dropoff_ratio*pow(star_size, 2), 0.5); |
| 1381 |
} |
| 1382 |
|
| 1383 |
double calc_photon_flux(double rel_magnitude, double filter_bandwidth, double wavelength, double steradian) |
| 1384 |
{ |
| 1385 |
return pow(10, rel_magnitude*-0.4)*(LUMEN(wavelength)*steradian*filter_bandwidth); |
| 1386 |
} |
| 1387 |
|
| 1388 |
double calc_rel_magnitude(double photon_flux, double filter_bandwidth, double wavelength, double steradian) |
| 1389 |
{ |
| 1390 |
return pow(10, 1.0/(photon_flux/(LUMEN(wavelength)*steradian*filter_bandwidth)))/-0.4; |
| 1391 |
} |
| 1392 |
|
| 1393 |
double estimate_absolute_magnitude(double delta_dist, double delta_mag) |
| 1394 |
{ |
| 1395 |
return sqrt(delta_dist) * delta_mag; |
| 1396 |
} |
| 1397 |
|
| 1398 |
void baseline_2d_projection(double alt, double az, double baseline[3], double wavelength, double uvresult[2]) |
| 1399 |
{ |
| 1400 |
az *= M_PI / 180.0; |
| 1401 |
alt *= M_PI / 180.0; |
| 1402 |
uvresult[0] = (baseline[0] * sin(az) + baseline[1] * cos(az)); |
| 1403 |
uvresult[1] = (baseline[1] * sin(alt) * sin(az) - baseline[0] * sin(alt) * cos(az) + baseline[2] * cos(alt)); |
| 1404 |
uvresult[0] *= AIRY / wavelength; |
| 1405 |
uvresult[1] *= AIRY / wavelength; |
| 1406 |
} |
| 1407 |
|
| 1408 |
double baseline_delay(double alt, double az, double baseline[3]) |
| 1409 |
{ |
| 1410 |
az *= M_PI / 180.0; |
| 1411 |
alt *= M_PI / 180.0; |
| 1412 |
return cos(az) * baseline[1] * cos(alt) - baseline[0] * sin(az) * cos(alt) + sin(alt) * baseline[2]; |
| 1413 |
} |
| 1414 |
|
| 1415 |
#if defined(_MSC_VER) |
| 1416 |
#undef snprintf |
| 1417 |
#pragma warning(pop) |
| 1418 |
#endif |