-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngclient.c
251 lines (215 loc) · 6.41 KB
/
ngclient.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//extern "C"
//{
/*############################################################################
# Title: ngclient.c
# Author: C. Johnson
# Date: 12/2/12
# Description: Contains a set of commands specific for communicating with
# a TCS-NG server
#
#############################################################################*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close */
#include "ngindi.h"
int initialize(char *name, int port);
#define MAX_MSG 1500
int indiNetErr(char *in);
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
int sd, rc, initialized=0;
#define mydev "TCS-NG-INDI"
#define TELID "VATT"
#define SYSID "TCS"
/*############################################################################
# Title: ng_send()
# Author: C. Johnson
# Date: 12/2/12
# Args:type="COMMAND" or "REQUEST"
# input= string to be sent
# output=return string
# Description: sends and receives data using the TCS-NG protocol
#
#############################################################################*/
int ng_send(char *type, char *input, char *output)
{
char data[MAX_MSG];
char term = '\n';
char preamble[100];
initialize((char *)"10.0.1.10", 5750);
/* if(!initialized)
{
initialize(NET_ADDR, PORT);
if(!initialized)
return 0;
}*/
strcpy(output,"\0");
strcpy(preamble,"\0");
sprintf(preamble,"%s %s 123 ", TELID, SYSID);
strcpy(data,"\0");
strcpy(data,preamble);
strncat(data, type, strlen(type));
strncat(data, " ", 1);
strncat(data, input, strlen(input));
strncat(data, &term, 1);
rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
if(rc<0) {
indiNetErr( (char *) "cannot connect ");
return(1);
}
rc = send(sd, data, strlen(data) + 1, 0);
if(rc<0) {
indiNetErr( (char *)"cannot send data ");
close(sd);
return(1);
}
if (recv(sd,&data,sizeof(data),0) == -1)
{
indiNetErr( (char *)"recv error");
return(1);
}
//printf("got: %s\n", data);
close(sd);
strcpy(output, &data[strlen(preamble)]);
return 0;
}
/*############################################################################
# Title: initialize()
# Author: C. Johnson
# Date: 12/2/12
# Args: name=name or IP address of TCS-NG server
# port=port on server to talk to
# Description: Initializes the network stack to talk to a TCS-NG
# server
#
#############################################################################*/
int initialize(char *name, int port)
{
char out[50];
h = gethostbyname(name);
if(h==NULL) {
sprintf(out, "unknown host '%s'\n",name);
indiNetErr(out);
return 1;
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = htons(port);
/* create socket */
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
indiNetErr( (char *)"cannot open socket ");
return 1;
}
/* bind any port number */
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0) {
printf("cannot bind port TCP %u\n",port);
indiNetErr( (char *)"error ");
return 1;
}
initialized=1;
return 0;
}
/*############################################################################
# Title: ng_request()
# Author: C. Johnson
# Date: 12/2/12
# Args: input=request string to be sent
# output=return string
# Description: sends a request to the TCS and populates a string with the
# return
#
#############################################################################*/
int ng_request(char *input, char *output)
{
char type[] = "REQUEST";
int err;
err = ng_send(type, input, output);
//if(output!=NULL);
// return(strlen(output));
return(err);
//return 0;
}
/*############################################################################
# Title: ng_command()
# Author: C. Johnson
# Date: 12/2/12
# Args: input=command string to be sent
# output=return string
# Description: sends a command to the TCS and populates a string with the
# return
#
#############################################################################*/
int ng_command(char *input, char *output)
{
char type[] = "COMMAND";
int err;
err = ng_send(type, input, output);
//if(output!=NULL);
return(err);
//return 0;
}
/*############################################################################
# Title: ngatof()
# Author: C. Johnson
# Date: 12/2/12
# Args: in=char ptr to input string containing ascii degrees in "D:M:S.s"
# out=float describing degrees.
# Description: converts an ascii "D:M:S.s" string to a float "D.d"
#
#############################################################################*/
int ngatof(char *in, double *out)
{
double H, M, S;
sscanf(in, "%lf:%lf:%lf", &H, &M, &S);
*out = H + (M/60) + (S/3600);
return 0;
}
/*############################################################################
# Title: ngdegtoa()
# Author: C. Johnson
# Date: 12/2/12
# Args: in=char ptr to input string containing ascii degrees in NG format
# out=char ptr to ouput string containing ascii degrees in DD:MM:SS.ss
# Returns: always returns 0
# Description: converts the NG style DMS string to a ':' delimited string
#
#############################################################################*/
int ngdegtoa(char *in, char *out)
{
char temp[20];
int iH, iM, iS, is;
double M, S, s;
sscanf(in, "%i.%i", &iH, &is);
sprintf(temp, "%06i", iH);
sscanf(temp, "%2i%2i%2i", &iH, &iM, &iS);
sprintf(out, "%02i:%02i:%02i.%2i", iH, iM, iS, is);
return 0;
}
/*############################################################################
# Title: indiNetErr()
# Author: C. Johnson
# Date: 9/2/15
# Args: in=char ptr to input string containing eror
#
# Returns: always returns 0
# Description: converts the NG style DMS string to a ':' delimited string
#
#############################################################################*/
int indiNetErr(char *in)
{
char buff[50];
sprintf(buff, "NET:%s", in);
return 0;
}
//}