-
Notifications
You must be signed in to change notification settings - Fork 0
/
TAGMremotectrl.cc
604 lines (595 loc) · 20.3 KB
/
TAGMremotectrl.cc
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//
// TAGMremotectrl - daemon application that runs in the background on a
// machine with a NIC on the ethernet segment where the
// TAGM frontend resides. Remote tcp connections are
// accepted on a designated user-space port, and text
// messages are exchanged between the daemon and the
// remote client program that implement the functions
// of the TAGMcontroller base class.
//
// author: richard.t.jones at uconn.edu
// version: january 15, 2016
//
// programmer's notes:
// 1) The public interface of TAGMcontroller is exposed to remote clients
// through a text message interface. The following commands and responses
// are supported. The quotes are not a part of the literal message. All
// requests must be terminated with a newline character.
//
// *) "probe" - responds with a list of all Vbias boards that respond
// to a broadcast query.
// *) "select <address> [<netdev>]" - selects a particular front-end board
// by its hardware address. The string <address> can either be a single
// byte value in hexadecimal notation (eg. 0x9f) or it can be a full
// ethernet address in dot notation (eg. 192.168.1.40).
// *) "get_hostMACaddr [<netdev>]" - reports the ethernet MAC address of
// the host running the TAGMremotectrl daemon on the network facing
// the TAGM frontend.
// *) "get_MACaddr" - reports the ethernet MAC address of the currently
// selected board, or "none" if select has not been issued yet.
// *) "get_Geoaddr" - reports the geographical address of the currently
// selected board, or "none" if select has not been issued yet.
// *) "get_Tchip" - reports the board temperature from T sensor chip (C)
// *) "get_pos5Vpower" - reports the +5V power level (V)
// *) "get_neg5Vpower" - reports the -5V power level (V)
// *) "get_pos3_3Vpower" - reports the +3.3V power level (V)
// *) "get_pos1_2Vpower" - reports the +1.2V power level (V)
// *) "get_Vsumref_1" - reports the SUMREF from preamp 1 (V)
// *) "get_Vsumref_2" - reports the SUMREF from preamp 2 (V)
// *) "get_Vgainmode" - reports the GAINMODE shared by both preamps (V)
// *) "get_gainmode" - reports the =0 (low) or =1 (high) or =-1 (undefined)
// *) "get_Vtherm_1" - reports the thermister voltage on preamp 1 (V)
// *) "get_Vtherm_2" - reports the thermister voltage on preamp 2 (V)
// *) "get_Tpreamp_1" - reports the thermister temperature on preamp 1 (C)
// *) "get_Tpreamp_2" - reports the thermister temperature on preamp 2 (C)
// *) "get_VDAChealth" - reports the DAC channel 31 read-back level (V)
// *) "get_VDACdiode" - reports the DAC temperature diode voltage (V)
// *) "get_TDAC" - reports the DAC internal temperature reading (C)
// *) "latch_status" - capture board status in state variables and return
// captured data in response to get_XXX()
// *) "passthru_status" - reset saved state from last latch_levels() and
// have each get_XXX() request fresh data from board
// *) "latch_voltages" - capture board's demand voltages in state variables
// and return captured data in response to getV()
// *) "passthru_voltages" - reset saved voltages from last latch_voltages()
// and have each getV() request fresh data from board
// *) "getV <chan>" - reports the voltage of channel <chan> on board (V)
// *) "getVnew <chan>" - reports the voltage of channel <chan> to be set
// in next ramp (V)
// *) "setV <chan> <V>" - ); // assign voltage <V> to channel <chan> to be
// set in next ramp (V)
// *) "get_last_packet" - reports the last packet received from the board
// *) "ramp" - push the new voltages to the board, if any
// *) "reset" - send a hard reset to the board, if selected, otherwise
// send the hard reset to all boards in the frontend.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <errno.h>
#include <TAGMcontroller.h>
int listener_port = 5692; // default listener port, you choose!
int listener_socket;
int listener_fd;
char *default_netdev = 0;
TAGMcontroller *Vboard;
std::map<std::string, TAGMcontroller*> Vboards;
std::string process_request(const char* request)
{
char mesg[strlen(request) + 2];
strcpy(mesg, request);
char *req = strtok(mesg, " ");
if (req && strcmp(req, "probe") == 0) {
const char *netdev = strtok(0, " ");
if (netdev == 0 || strlen(netdev) == 0)
netdev = default_netdev;
std::map<unsigned char, std::string> boardlist;
try {
boardlist = TAGMcontroller::probe(netdev);
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
std::map<unsigned char, std::string>::iterator iter;
std::stringstream response;
for (iter = boardlist.begin(); iter != boardlist.end(); ++iter) {
response << std::hex << (unsigned int)iter->first
<< " " << iter->second << std::endl;
}
return response.str();
}
else if (strcmp(req, "get_hostMACaddr") == 0) {
const char *netdev = strtok(0, " ");
if (netdev == 0 || strlen(netdev) == 0)
netdev = default_netdev;
TAGMcontroller *ctrl = Vboard;
if (ctrl == 0) {
try {
ctrl = new TAGMcontroller((unsigned char)0xff, netdev);
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
}
return ctrl->get_hostMACaddr(netdev);
}
else if (strcmp(req, "reset") == 0) {
TAGMcontroller *ctrl = Vboard;
if (ctrl == 0) {
try {
ctrl = new TAGMcontroller((unsigned char)0xff);
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
}
int stat;
try {
stat = ctrl->reset();
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
if (! stat) {
std::stringstream response;
response << "TAGMremotectrl error - "
<< "reset() method failed for board at "
<< std::hex << (unsigned int)ctrl->get_Geoaddr()
<< std::endl;
return response.str();
}
if (Vboard == 0)
delete ctrl;
return std::string("ok\n");
}
else if (strcmp(req, "select") == 0) {
unsigned char geoaddr;
unsigned char macaddr[6];
char *addr = strtok(0, " ");
char *netdev = strtok(0, " ");
if (netdev == 0 || strlen(netdev) == 0)
netdev = default_netdev;
std::string boardId(addr);
if (netdev != 0) {
boardId += "::";
boardId += netdev;
}
if (Vboards.find(boardId) != Vboards.end()) {
Vboard = Vboards[boardId];
}
else if (sscanf(addr, "0x%2hhx", &geoaddr) == 1) {
try {
Vboard = new TAGMcontroller(geoaddr, netdev);
}
catch (const std::runtime_error &err) {
Vboard = 0;
return std::string(err.what()) + "\n";
}
}
else if (sscanf(addr, "%2.2hhx.%2.2hhx.%2.2hhx.%2.2hhx.%2.2hhx.%2.2hhx",
&macaddr[0], &macaddr[1], &macaddr[2],
&macaddr[3], &macaddr[4], &macaddr[5]) == 6)
{
try {
Vboard = new TAGMcontroller(macaddr, netdev);
}
catch (const std::runtime_error &err) {
Vboard = 0;
return std::string(err.what()) + "\n";
}
}
else {
std::stringstream response;
response << "TAGMremotectrl error - "
<< "invalid address " << addr << std::endl;
return response.str();
}
Vboards[boardId] = Vboard;
return std::string("ok\n");
}
else if (Vboard == 0) {
return std::string("TAGMremotectrl error - no board selected\n");
}
else if (strcmp(req, "get_MACaddr") == 0) {
const unsigned char *macaddr = Vboard->get_MACaddr();
std::stringstream response;
for (int i=0; i<6; ++i) {
char hexb[3];
sprintf(hexb, "%2.2x", macaddr[i]);
response << ((i>0)? "." : "") << hexb;
}
response << std::endl;
return response.str();
}
else if (strcmp(req, "get_Geoaddr") == 0) {
const unsigned char geoaddr = Vboard->get_Geoaddr();
char hexb[7];
sprintf(hexb, "0x%2.2x\n", geoaddr);
return std::string(hexb);
}
else if (strcmp(req, "get_Tchip") == 0) {
std::stringstream response;
try {
response << Vboard->get_Tchip() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_pos5Vpower") == 0) {
std::stringstream response;
try {
response << Vboard->get_pos5Vpower() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_neg5Vpower") == 0) {
std::stringstream response;
try {
response << Vboard->get_neg5Vpower() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_pos3_3Vpower") == 0) {
std::stringstream response;
try {
response << Vboard->get_pos3_3Vpower() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_pos1_2Vpower") == 0) {
std::stringstream response;
try {
response << Vboard->get_pos1_2Vpower() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Vsumref_1") == 0) {
std::stringstream response;
try {
response << Vboard->get_Vsumref_1() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Vsumref_2") == 0) {
std::stringstream response;
try {
response << Vboard->get_Vsumref_2() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Vgainmode") == 0) {
std::stringstream response;
try {
response << Vboard->get_Vgainmode() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_gainmode") == 0) {
std::stringstream response;
try {
response << Vboard->get_gainmode() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Vtherm_1") == 0) {
std::stringstream response;
try {
response << Vboard->get_Vtherm_1() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Vtherm_2") == 0) {
std::stringstream response;
try {
response << Vboard->get_Vtherm_2() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Tpreamp_1") == 0) {
std::stringstream response;
try {
response << Vboard->get_Tpreamp_1() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_Tpreamp_2") == 0) {
std::stringstream response;
try {
response << Vboard->get_Tpreamp_2() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_VDAChealth") == 0) {
std::stringstream response;
try {
response << Vboard->get_VDAChealth() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_VDACdiode") == 0) {
std::stringstream response;
try {
response << Vboard->get_VDACdiode() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "get_TDAC") == 0) {
std::stringstream response;
try {
response << Vboard->get_TDAC() << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return response.str();
}
else if (strcmp(req, "latch_status") == 0) {
Vboard->passthru_status();
try {
Vboard->latch_status();
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return std::string("ok\n");
}
else if (strcmp(req, "passthru_status") == 0) {
Vboard->passthru_status();
return std::string("ok\n");
}
else if (strcmp(req, "latch_voltages") == 0) {
Vboard->passthru_voltages();
try {
Vboard->latch_voltages();
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
return std::string("ok\n");
}
else if (strcmp(req, "passthru_voltages") == 0) {
Vboard->passthru_voltages();
return std::string("ok\n");
}
else if (strcmp(req, "getV") == 0) {
std::stringstream response;
unsigned int chan;
const char *arg = strtok(0, " ");
if (arg && sscanf(arg, "%u", &chan) == 1) {
try {
response << Vboard->getV(chan) << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
}
else {
response << "TAGMremotectrl error - "
<< "invalid channel " << arg << std::endl;
}
return response.str();
}
else if (strcmp(req, "getVnew") == 0) {
std::stringstream response;
unsigned int chan;
const char *arg = strtok(0, " ");
if (arg && sscanf(arg, "%u", &chan) == 1) {
try {
response << Vboard->getVnew(chan) << std::endl;
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
}
else {
response << "TAGMremotectrl error - "
<< "invalid channel " << arg << std::endl;
}
return response.str();
}
else if (strcmp(req, "setV") == 0) {
std::stringstream response;
unsigned int chan;
double V;
const char *arg1 = strtok(0, " ");
const char *arg2 = strtok(0, " ");
if (arg1 == 0 || sscanf(arg1, "%u", &chan) != 1) {
response << "TAGMremotectrl error - "
<< "invalid channel " << arg1 << std::endl;
}
else if (arg2 == 0 || sscanf(arg2, "%lf", &V) != 1) {
response << "TAGMremotectrl error - "
<< "invalid voltage " << arg2 << std::endl;
}
else {
try {
Vboard->setV(chan, V);
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
response << "ok\n";
}
return response.str();
}
else if (strcmp(req, "get_last_packet") == 0) {
std::stringstream response;
const unsigned char *pkt = Vboard->get_last_packet();
int pktlen = pkt[13] + 14;
for (int i=0; i < pktlen; ++i) {
char hexb[3];
sprintf(hexb, "%2.2x", pkt[i]);
response << ((i>0)? " " : "") << hexb;
}
response << std::endl;
return response.str();
}
else if (strcmp(req, "ramp") == 0) {
try {
if (Vboard->ramp()) {
return std::string("ok");
}
else {
std::stringstream response;
response << "TAGMremotectrl error - "
<< "error returned by ramp() method for board at "
<< std::hex << (unsigned int)Vboard->get_Geoaddr()
<< std::endl;
return response.str();
}
}
catch (const std::runtime_error &err) {
return std::string(err.what()) + "\n";
}
}
return std::string("unbelievable!\n");
}
int main(int argc, char *argv[])
{
default_netdev = (char*)malloc(strlen(DEFAULT_NETWORK_DEVICE));
strcpy(default_netdev, DEFAULT_NETWORK_DEVICE);
for (int iarg = 1; iarg < argc; ++iarg) {
if (strcmp(argv[iarg], "-p") == 0 &&
sscanf(argv[++iarg], "%d", &listener_port) == 1)
{
++iarg;
}
else if (strcmp(argv[iarg], "-?") == 0 ||
strcmp(argv[iarg], "-h") == 0 ||
strcmp(argv[iarg], "--help") == 0 ||
argv[iarg][0] == '-' || argc - iarg > 2)
{
std::cerr << "Usage: TAGMremotectrl [-p <port>] [<network_device>]"
<< std::endl
<< " where <port> is the listening port"
<< " through which clients will connect to this daemon"
<< std::endl
<< " and <network_device> is the name of the NIC"
<< " connecting to the TAGM frontend, eg. eth0"
<< std::endl;
exit(1);
}
else {
default_netdev = (char *)malloc(strlen(argv[iarg]) + 1);
strcpy(default_netdev, argv[iarg]);
}
}
Vboard = 0;
// open a listening tcp port and wait for incoming connections
if ((listener_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
char errmesg[100];
sprintf(errmesg, "Cannot open listener on port %d", listener_port);
perror(errmesg);
exit(1);
}
struct sockaddr_in myaddr;
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(listener_port);
if (bind(listener_socket,
(const struct sockaddr*)&myaddr,
sizeof(myaddr)) < 0)
{
char errmesg[100];
sprintf(errmesg, "Cannot bind listener on port %d", listener_port);
perror(errmesg);
exit(1);
}
if (listen(listener_socket, 0) < 0) {
char errmesg[100];
sprintf(errmesg, "Cannot listen on port %d", listener_port);
perror(errmesg);
exit(1);
}
for (;;) {
printf("waiting for a client to connect...\n");
socklen_t clientaddr_len;
struct sockaddr_in clientaddr;
while ((listener_fd = accept(listener_socket,
(sockaddr*)&clientaddr,
&clientaddr_len)) < 0)
{
// we may break out of accept if the system call
// was interrupted. In this case, loop back and try again
if ((errno != ECHILD) && (errno != ERESTART) && (errno != EINTR)) {
perror("accept failed");
exit(1);
}
}
printf("just got a new connection, waiting for messages.\n");
for (;;) {
int request_len;
int buffer_size = 999;
char request[buffer_size];
char *buffer = request;
while (int nbytes = read(listener_fd, buffer, buffer_size)) {
buffer_size -= nbytes;
buffer += nbytes;
if (buffer[-1] == '\n') {
buffer[-1] = 0;
break;
}
}
if (buffer == request) {
break;
}
else {
std::string response = process_request(request);
write(listener_fd, response.c_str(), response.size() + 1);
}
}
}
close(listener_fd);
}