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