34 #include <sys/param.h>
59 void getSexComponents(
double value,
int *d,
int *m,
int *s);
67 if (strptime(timestr,
"%Y/%m/%dT%H:%M:%S", &utm))
69 ln_get_date_from_tm(&utm, iso_date);
73 if (strptime(timestr,
"%Y-%m-%dT%H:%M:%S", &utm))
75 ln_get_date_from_tm(&utm, iso_date);
97 fs_sexa (
char *out,
double a,
int w,
int fracbase)
113 n = (
unsigned long)(a * fracbase + 0.5);
119 out += sprintf (out,
"%*s-0", w-2,
"");
121 out += sprintf (out,
"%*d", w, isneg ? -d : d);
127 out += sprintf (out,
":%02d", m);
130 out += sprintf (out,
":%02d.%1d", f/10, f%10);
135 out += sprintf (out,
":%02d:%02d", m, s);
140 out += sprintf (out,
":%02d:%02d.%1d", m, s/10, s%10);
145 out += sprintf (out,
":%02d:%02d.%02d", m, s/100, s%100);
148 printf (
"fs_sexa: unknown fracbase: %d\n", fracbase);
165 double a = 0, b = 0, c = 0;
171 strncpy (str, str0,
sizeof(str)-1);
172 str[
sizeof(str)-1] =
'\0';
174 neg = strchr(str,
'-');
177 r = sscanf (str,
"%lf%*[^0-9]%lf%*[^0-9]%lf", &a, &b, &c);
180 *dp = a + b/60 + c/3600;
186 void getSexComponents(
double value,
int *d,
int *m,
int *s)
189 *d = (int) fabs(value);
190 *m = (int) ((fabs(value) - *d) * 60.0);
191 *s = (int) rint(((fabs(value) - *d) * 60.0 - *m) *60.0);
204 if (sscanf (format,
"%%%d.%d%c", &w, &f, &m) == 3 && m ==
'm') {
207 case 9: s = 360000;
break;
208 case 8: s = 36000;
break;
209 case 6: s = 3600;
break;
210 case 5: s = 600;
break;
211 default: s = 60;
break;
213 return (
fs_sexa (buf, value, w-f, s));
216 return (sprintf (buf, format, value));
230 vfprintf (stderr, fmt, ap);
244 strftime (ts,
sizeof(ts),
"%Y-%m-%dT%H:%M:%S", tp);
248 int tty_timeout(
int fd,
int timeout)
258 FD_SET(fd, &readout);
265 retval = select (fd+1, &readout, NULL, NULL, &tv);
271 else if (retval == -1)
272 return TTY_SELECT_ERROR;
279 int tty_write(
int fd,
const char * buf,
int nbytes,
int *nbytes_written)
290 bytes_w = write(fd, buf, nbytes);
293 return TTY_WRITE_ERROR;
295 *nbytes_written += bytes_w;
312 nbytes = strlen(buf);
317 bytes_w = write(fd, buf, nbytes);
320 return TTY_WRITE_ERROR;
322 *nbytes_written += bytes_w;
330 int tty_read(
int fd,
char *buf,
int nbytes,
int timeout,
int *nbytes_read)
340 return TTY_PARAM_ERROR;
344 if ( (err = tty_timeout(fd, timeout)) )
347 bytesRead = read(fd, buf, ((
unsigned) nbytes));
350 return TTY_READ_ERROR;
353 *nbytes_read += bytesRead;
371 if ( (err = tty_timeout(fd, timeout)) )
374 bytesRead = read(fd, buf, 1);
377 return TTY_READ_ERROR;
382 if (*buf == stop_char)
391 #if defined(BSD) && !defined(__GNU__)
393 int tty_connect(
const char *device,
int bit_rate,
int word_size,
int parity,
int stop_bits,
int *fd)
399 struct termios tty_setting;
405 t_fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
408 printf(
"Error opening serial port %s - %s(%d).\n",
409 device, strerror(errno), errno);
418 if (ioctl(t_fd, TIOCEXCL) == -1)
420 printf(
"Error setting TIOCEXCL on %s - %s(%d).\n",
421 device, strerror(errno), errno);
428 if (fcntl(t_fd, F_SETFL, 0) == -1)
430 printf(
"Error clearing O_NONBLOCK %s - %s(%d).\n",
431 device, strerror(errno), errno);
436 if (tcgetattr(t_fd, &tty_setting) == -1)
438 printf(
"Error getting tty attributes %s - %s(%d).\n",
439 device, strerror(errno), errno);
447 cfmakeraw(&tty_setting);
448 tty_setting.c_cc[VMIN] = 1;
449 tty_setting.c_cc[VTIME] = 10;
511 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid bit rate.", bit_rate) < 0)
515 return TTY_PARAM_ERROR;
518 cfsetspeed(&tty_setting, bps);
522 tty_setting.c_cflag |= CS5;
525 tty_setting.c_cflag |= CS6;
528 tty_setting.c_cflag |= CS7;
531 tty_setting.c_cflag |= CS8;
535 fprintf( stderr,
"Default\n") ;
536 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid data bit count.", word_size) < 0)
541 return TTY_PARAM_ERROR;
549 tty_setting.c_cflag |= PARENB;
552 tty_setting.c_cflag |= PARENB | PARODD;
556 fprintf( stderr,
"Default1\n") ;
557 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid parity selection value.", parity) < 0)
562 return TTY_PARAM_ERROR;
570 tty_setting.c_cflag |= CSTOPB;
573 fprintf( stderr,
"Default2\n") ;
574 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid stop bit count.", stop_bits) < 0)
579 return TTY_PARAM_ERROR;
582 #if defined(MAC_OS_X_VERSION_10_4) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
588 speed_t speed = 14400;
589 if (ioctl(fileDescriptor, IOSSIOSPEED, &speed) == -1)
591 printf(
"Error calling ioctl(..., IOSSIOSPEED, ...) %s - %s(%d).\n",
592 bsdPath, strerror(errno), errno);
597 if (tcsetattr(t_fd, TCSANOW, &tty_setting) == -1)
599 printf(
"Error setting tty attributes %s - %s(%d).\n",
600 device, strerror(errno), errno);
607 if (ioctl(t_fd, TIOCSDTR) == -1)
609 printf(
"Error asserting DTR %s - %s(%d).\n",
610 device, strerror(errno), errno);
613 if (ioctl(t_fd, TIOCCDTR) == -1)
615 printf(
"Error clearing DTR %s - %s(%d).\n",
616 device, strerror(errno), errno);
619 handshake = TIOCM_DTR | TIOCM_RTS | TIOCM_CTS | TIOCM_DSR;
620 if (ioctl(t_fd, TIOCMSET, &handshake) == -1)
623 printf(
"Error setting handshake lines %s - %s(%d).\n",
624 device, strerror(errno), errno);
630 if (ioctl(t_fd, TIOCMGET, &handshake) == -1)
633 printf(
"Error getting handshake lines %s - %s(%d).\n",
634 device, strerror(errno), errno);
637 printf(
"Handshake lines currently set to %d\n", handshake);
639 #if defined(MAC_OS_X_VERSION_10_3) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
640 unsigned long mics = 1UL;
648 if (ioctl(t_fd, IOSSDATALAT, &mics) == -1)
651 printf(
"Error setting read latency %s - %s(%d).\n",
652 device, strerror(errno), errno);
669 return TTY_PORT_FAILURE;
672 int tty_connect(
const char *device,
int bit_rate,
int word_size,
int parity,
int stop_bits,
int *fd)
675 return TTY_PORT_FAILURE;
681 struct termios tty_setting;
683 if ( (t_fd = open(device, O_RDWR | O_NOCTTY )) == -1)
687 return TTY_PORT_FAILURE;
751 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid bit rate.", bit_rate) < 0)
755 return TTY_PARAM_ERROR;
757 if ((cfsetispeed(&tty_setting, bps) < 0) ||
758 (cfsetospeed(&tty_setting, bps) < 0))
760 perror(
"tty_connect: failed setting bit rate.");
761 return TTY_PORT_FAILURE;
768 tty_setting.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | HUPCL | CRTSCTS);
769 tty_setting.c_cflag |= (CLOCAL | CREAD);
774 tty_setting.c_cflag |= CS5;
777 tty_setting.c_cflag |= CS6;
780 tty_setting.c_cflag |= CS7;
783 tty_setting.c_cflag |= CS8;
787 fprintf( stderr,
"Default\n") ;
788 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid data bit count.", word_size) < 0)
793 return TTY_PARAM_ERROR;
801 tty_setting.c_cflag |= PARENB;
804 tty_setting.c_cflag |= PARENB | PARODD;
808 fprintf( stderr,
"Default1\n") ;
809 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid parity selection value.", parity) < 0)
814 return TTY_PARAM_ERROR;
822 tty_setting.c_cflag |= CSTOPB;
825 fprintf( stderr,
"Default2\n") ;
826 if (snprintf(msg,
sizeof(msg),
"tty_connect: %d is not a valid stop bit count.", stop_bits) < 0)
831 return TTY_PARAM_ERROR;
836 tty_setting.c_iflag &= ~(PARMRK | ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON | IXANY);
837 tty_setting.c_iflag |= INPCK | IGNPAR | IGNBRK;
840 tty_setting.c_oflag &= ~(OPOST | ONLCR);
845 tty_setting.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN | NOFLSH | TOSTOP);
846 tty_setting.c_lflag |= NOFLSH;
849 tty_setting.c_cc[VMIN] = 1;
850 tty_setting.c_cc[VTIME] = 0;
853 tcflush(t_fd, TCIOFLUSH);
854 if (tcsetattr(t_fd, TCSANOW, &tty_setting))
856 perror(
"tty_connect: failed setting attributes on serial port.");
858 return TTY_PORT_FAILURE;
880 tcflush(fd, TCIOFLUSH);
892 char error_string[512];
897 strncpy(err_msg,
"No Error", err_msg_len);
901 snprintf(error_string, 512,
"Read Error: %s", strerror(errno));
902 strncpy(err_msg, error_string, err_msg_len);
905 case TTY_WRITE_ERROR:
906 snprintf(error_string, 512,
"Write Error: %s", strerror(errno));
907 strncpy(err_msg, error_string, err_msg_len);
910 case TTY_SELECT_ERROR:
911 snprintf(error_string, 512,
"Select Error: %s", strerror(errno));
912 strncpy(err_msg, error_string, err_msg_len);
916 strncpy(err_msg,
"Timeout error", err_msg_len);
919 case TTY_PORT_FAILURE:
921 snprintf(error_string, 512,
"Port failure Error: %s. Try adding your user to the dialout group and restart (sudo adduser $username dialout)", strerror(errno));
923 snprintf(error_string, 512,
"Port failure Error: %s. Check if device is connected to this port.", strerror(errno));
925 strncpy(err_msg, error_string, err_msg_len);
928 case TTY_PARAM_ERROR:
929 strncpy(err_msg,
"Parameter error", err_msg_len);
933 snprintf(error_string, 512,
"%s", strerror(errno));
934 strncpy(err_msg, error_string, err_msg_len);
938 strncpy(err_msg,
"Error: unrecognized error code", err_msg_len);
951 case IPS_OK:
return (
"Ok");
955 fprintf (stderr,
"Impossible IPState %d\n", s);
966 if (!strcmp (str,
"Idle")) *ip =
IPS_IDLE;
967 else if (!strcmp (str,
"Ok")) *ip =
IPS_OK;
968 else if (!strcmp (str,
"Busy")) *ip =
IPS_BUSY;
969 else if (!strcmp (str,
"Alert")) *ip =
IPS_ALERT;
980 if (!strcmp (str,
"On")) *ip =
ISS_ON;
981 else if (!strcmp (str,
"Off")) *ip =
ISS_OFF;
989 if (!strcmp (str,
"rw")) *ip =
IP_RW;
990 else if (!strcmp (str,
"ro")) *ip =
IP_RO;
991 else if (!strcmp (str,
"wo")) *ip =
IP_WO;
999 else if (!strcmp (str,
"AtMostOne")) *ip =
ISR_ATMOST1;
1000 else if (!strcmp (str,
"AnyOfMany")) *ip =
ISR_NOFMANY;
1010 case ISS_ON:
return (
"On");
1013 fprintf (stderr,
"Impossible ISState %d\n", s);
1027 fprintf (stderr,
"Impossible ISRule %d\n", r);
1037 case IP_RO:
return (
"ro");
1038 case IP_WO:
return (
"wo");
1039 case IP_RW:
return (
"rw");
1041 fprintf (stderr,
"Impossible IPerm %d\n", p);
1050 printf (
"<?xml version='1.0'?>\n");
1057 crackDN (XMLEle *root,
char **dev,
char **name,
char msg[])
1063 sprintf (msg,
"%s requires 'device' attribute",
tagXMLEle(root));
1070 sprintf (msg,
"%s requires 'name' attribute",
tagXMLEle(root));
1084 for (i = 0; i < tvp->
ntp; i++)
1085 if (strcmp (tvp->
tp[i].
name, name) == 0)
1086 return (&tvp->
tp[i]);
1087 fprintf (stderr,
"No IText '%s' in %s.%s\n",name,tvp->
device,tvp->
name);
1097 for (i = 0; i < nvp->
nnp; i++)
1098 if (strcmp (nvp->
np[i].name, name) == 0)
1099 return (&nvp->
np[i]);
1100 fprintf(stderr,
"No INumber '%s' in %s.%s\n",name,nvp->
device,nvp->
name);
1110 for (i = 0; i < svp->
nsp; i++)
1111 if (strcmp (svp->
sp[i].name, name) == 0)
1112 return (&svp->
sp[i]);
1113 fprintf(stderr,
"No ISwitch '%s' in %s.%s\n",name,svp->
device,svp->
name);
1123 for (i = 0; i < lvp->
nlp; i++)
1124 if (strcmp (lvp->
lp[i].name, name) == 0)
1125 return (&lvp->
lp[i]);
1126 fprintf(stderr,
"No ILight '%s' in %s.%s\n",name,lvp->
device,lvp->
name);
1136 for (i = 0; i < bvp->
nbp; i++)
1137 if (strcmp (bvp->
bp[i].
name, name) == 0)
1138 return (&bvp->
bp[i]);
1139 fprintf(stderr,
"No IBLOB '%s' in %s.%s\n",name,bvp->
device,bvp->
name);
1151 for (i = 0; i < svp->
nsp; i++)
1153 return (&svp->
sp[i]);
1163 for (i = 0; i < svp->
nsp; i++)
1175 for (i = 0; i < svp->
nsp; i++)
1184 if (tp->
text == NULL)
1185 tp->
text = malloc (1);
1188 tp->
text = strcpy (realloc (tp->
text, strlen(newtext)+1), newtext);