Instrument Neutral Distributed Interface INDI  2.0.2
magellandriver.c
Go to the documentation of this file.
1 #if 0
2  MAGELLAN Driver
3  Copyright (C) 2011 Onno Hommes (ohommes@alumni.cmu.edu)
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 
19 #endif
20 
21 #include "magellandriver.h"
22 
23 #include "indicom.h"
24 #include "indidevapi.h"
25 
26 #ifndef _WIN32
27 #include <termios.h>
28 #endif
29 
30 /**************************************************************************
31  Connection Diagnostics
32  **************************************************************************/
33 char ACK(int fd);
35 
36 /**************************************************************************
37  Get Commands: store data in the supplied buffer.
38  Return 0 on success or -1 on failure
39  **************************************************************************/
40 
41 /* Get Double from Sexagisemal */
42 int getCommandSexa(int fd, double *value, const char *cmd);
43 
44 /* Get String */
45 static int getCommandString(int fd, char *data, const char *cmd);
46 
47 /* Get Calender data */
48 int getCalendarDate(int fd, char *date);
49 
50 /**************************************************************************
51  Driver Implementations
52  **************************************************************************/
53 
54 /*
55  Check the Magellan Connection using any set command
56  Magellan 1 does not support any sey but still sends
57  'OK' for it
58 */
60 {
61  int i = 0;
62 
63  /* Magellan I ALways Response OK for :S?# */
64  char ack[4] = ":S?#";
65 
66  char Response[64];
67  int nbytes_read = 0;
68 
69 #ifdef INDI_DEBUG
70  IDLog("Testing telescope's connection...\n");
71 #endif
72 
73  if (fd <= 0)
74  return MAGELLAN_ERROR;
75 
76  for (i = 0; i < CONNECTION_RETRIES; i++)
77  {
78  if (write(fd, ack, 4) < 0)
79  return MAGELLAN_ERROR;
80 
81  tty_read(fd, Response, 2, MAGELLAN_TIMEOUT, &nbytes_read);
82 
83  if (nbytes_read == 2)
84  return MAGELLAN_OK;
85 
86  usleep(50000);
87  }
88 
89  tcflush(fd, TCIFLUSH);
90  return MAGELLAN_ERROR;
91 }
92 
93 /**********************************************************************
94 * GET FUNCTIONS
95 **********************************************************************/
96 
97 char ACK(int fd)
98 {
99  /* Magellan I ALways Response OK for :S?# (any set will do)*/
100  char ack[4] = ":S?#";
101  char Response[64];
102  int nbytes_read = 0;
103  int i = 0;
104  int result = MAGELLAN_ERROR;
105 
106  /* Check for Comm handle */
107  if (fd > 0)
108  {
109  for (i = 0; i < CONNECTION_RETRIES; i++)
110  {
111  /* Send ACK string to Magellan */
112  if (write(fd, ack, 4) >= 0)
113  {
114  /* Read Response */
115  tty_read(fd, Response, 2, MAGELLAN_TIMEOUT, &nbytes_read);
116 
117  /* If the two byte OK is returned we have an Ack */
118  if (nbytes_read == 2)
119  {
120  result = MAGELLAN_ACK;
121  break; /* Force quick success return */
122  }
123  else
124  usleep(50000);
125  }
126  }
127  }
128 
129  tcflush(fd, TCIFLUSH);
130  return result;
131 }
132 
133 int getCommandSexa(int fd, double *value, const char *cmd)
134 {
135  char temp_string[16];
136  int result = MAGELLAN_ERROR;
137  int nbytes_write = 0, nbytes_read = 0;
138 
139  if ((tty_write_string(fd, cmd, &nbytes_write)) == TTY_OK)
140  {
141  if ((tty_read_section(fd, temp_string, '#', MAGELLAN_TIMEOUT, &nbytes_read)) == TTY_OK)
142  {
143  temp_string[nbytes_read - 1] = '\0';
144 
145  if (f_scansexa(temp_string, value))
146  {
147 #ifdef INDI_DEBUG
148  IDLog("unable to process [%s]\n", temp_string);
149 #endif
150  }
151  else
152  {
153  result = MAGELLAN_OK;
154  }
155  }
156  }
157 
158  tcflush(fd, TCIFLUSH);
159  return result;
160 }
161 
162 static int getCommandString(int fd, char *data, const char *cmd)
163 {
164  int nbytes_write = 0, nbytes_read = 0;
165  int result = MAGELLAN_ERROR;
166 
167  if ((tty_write_string(fd, cmd, &nbytes_write)) == TTY_OK)
168  {
169  if ((tty_read_section(fd, data, '#', MAGELLAN_TIMEOUT, &nbytes_read)) == TTY_OK)
170  {
171  data[nbytes_read - 1] = '\0';
172  result = MAGELLAN_OK;
173  }
174  }
175 
176  tcflush(fd, TCIFLUSH);
177  return result;
178 }
179 
180 int getCalendarDate(int fd, char *date)
181 {
182  int dd, mm, yy;
183  char century[3];
184  int result;
185 
186  if ((result = getCommandString(fd, date, "#:GC#")))
187  {
188  /* Magellan format is MM/DD/YY */
189  if ((sscanf(date, "%d%*c%d%*c%d", &mm, &dd, &yy)) > 2)
190  {
191  /* Consider years 92 or more to be in the last century, anything less
192  in the 21st century.*/
193  if (yy > CENTURY_THRESHOLD)
194  strncpy(century, "19", 3);
195  else
196  strncpy(century, "20", 3);
197 
198  /* Format must be YYYY/MM/DD format */
199  snprintf(date, 16, "%s%02d/%02d/%02d", century, yy, mm, dd);
200  }
201  }
202 
203  return result;
204 }
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read)
read buffer from terminal with a delimiter
Definition: indicom.c:566
int f_scansexa(const char *str0, double *dp)
convert sexagesimal string str AxBxC to double. x can be anything non-numeric. Any missing A,...
Definition: indicom.c:205
void IDLog(const char *fmt,...)
Definition: indicom.c:316
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
read buffer from terminal
Definition: indicom.c:482
int tty_write_string(int fd, const char *buf, int *nbytes_written)
Writes a null terminated string to fd.
Definition: indicom.c:474
Implementations for common driver routines.
@ TTY_OK
Definition: indicom.h:150
Interface to the reference INDI C API device implementation on the Device Driver side.
int fd
Definition: intelliscope.c:43
int getCommandString(int fd, char *data, const char *cmd)
int check_magellan_connection(int fd)
int getCommandSexa(int fd, double *value, const char *cmd)
char ACK(int fd)
int getCalendarDate(int fd, char *date)
#define CONNECTION_RETRIES
#define MAGELLAN_ERROR
#define CENTURY_THRESHOLD
#define MAGELLAN_OK
#define MAGELLAN_ACK
#define MAGELLAN_TIMEOUT
__u8 cmd[4]
Definition: pwc-ioctl.h:2