-
Notifications
You must be signed in to change notification settings - Fork 0
/
adcs-util.c
243 lines (205 loc) · 7.98 KB
/
adcs-util.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
/**
* Test code for process command handling
* @author
*
*/
#include <polysat/polysat.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include "adcs-telemetry.h"
#define WAIT_MS (2 * 1000)
struct MulticallInfo;
static int adcs_status(int, char**, struct MulticallInfo *);
static int adcs_telemetry(int, char**, struct MulticallInfo *);
// struct holding all possible function calls
// running the executable with the - flags will call that function
// running without flags will print out this struct
struct MulticallInfo {
int (*func)(int argc, char **argv, struct MulticallInfo *);
const char *name;
const char *opt;
const char *help;
} multicall[] = {
{ &adcs_status, "adcs-status", "-S",
"Display the current status of the adcs process -S" },
{ &adcs_telemetry, "adcs-telemetry", "-T",
"Display the current KVP telemetry of the adcs process -T" },
{ NULL, NULL, NULL, NULL }
};
static int adcs_status(int argc, char **argv, struct MulticallInfo * self)
{
struct {
uint8_t cmd;
struct ADCSReaderStatus status;
} __attribute__((packed)) resp;
struct {
uint8_t cmd;
} __attribute__((packed)) send;
send.cmd = 1;
const char *ip = "127.0.0.1";
int len, opt;
while ((opt = getopt(argc, argv, "h:")) != -1) {
switch(opt) {
case 'h':
ip = optarg;
break;
}
}
// send packet and wait for response
if ((len = socket_send_packet_and_read_response(ip, "adcs", &send,
sizeof(send), &resp, sizeof(resp), WAIT_MS)) <= 0) {
return len;
}
if (resp.cmd != CMD_STATUS_RESPONSE) {
printf("response code incorrect, Got 0x%02X expected 0x%02X\n",
resp.cmd, CMD_STATUS_RESPONSE);
return 5;
}
// print out returned status values
printf("Accel X=%f [G]\n", ((int32_t)ntohl(resp.status.accel.x))
/ (1024.0*1024.0*16.0));
printf("Accel Y=%f [G]\n", ((int32_t)ntohl(resp.status.accel.y))
/ (1024.0*1024.0*16.0));
printf("Accel Z=%f [G]\n", ((int32_t)ntohl(resp.status.accel.z))
/ (1024.0*1024.0*16.0));
printf("Gyro X=%f [d/s]\n", ((int32_t)ntohl(resp.status.gyro.x))
/ (1024.0*1024.0));
printf("Gyro Y=%f [d/s]\n", ((int32_t)ntohl(resp.status.gyro.y))
/ (1024.0*1024.0));
printf("Gyro Z=%f [d/s]\n", ((int32_t)ntohl(resp.status.gyro.z))
/ (1024.0*1024.0));
printf("MB Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_mb.x));
printf("MB Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_mb.y));
printf("MB Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_mb.z));
printf("-X Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_nx.x));
printf("-X Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_nx.y));
printf("-X Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_nx.z));
printf("+X Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_px.x));
printf("+X Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_px.y));
printf("+X Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_px.z));
printf("-Y Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_ny.x));
printf("-Y Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_ny.y));
printf("-Y Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_ny.z));
printf("+Y Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_py.x));
printf("+Y Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_py.y));
printf("+Y Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_py.z));
printf("-Z Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_nz.x));
printf("-Z Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_nz.y));
printf("-Z Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_nz.z));
printf("+Z Mag X=%d [nT]\n", (int32_t)ntohl(resp.status.mag_pz.x));
printf("+Z Mag Y=%d [nT]\n", (int32_t)ntohl(resp.status.mag_pz.y));
printf("+Z Mag Z=%d [nT]\n", (int32_t)ntohl(resp.status.mag_pz.z));
return 0;
}
/* get telemetry from ADCS process
* @param argc number of command line arguments
* @param argv char array of command line arguments
* @return 0 on succes, failure otherwise
*/
static int adcs_telemetry(int argc, char **argv, struct MulticallInfo * self)
{
struct {
uint8_t cmd;
struct ADCSReaderStatus status;
} __attribute__((packed)) resp;
struct {
uint8_t cmd;
} __attribute__((packed)) send;
send.cmd = 1;
const char *ip = "127.0.0.1";
int len, opt;
while ((opt = getopt(argc, argv, "h:")) != -1) {
switch(opt) {
case 'h':
ip = optarg;
break;
}
}
// send packet and wait for response
if ((len = socket_send_packet_and_read_response(ip, "adcs", &send,
sizeof(send), &resp, sizeof(resp), WAIT_MS)) <= 0) {
return len;
}
if (resp.cmd != CMD_STATUS_RESPONSE) {
printf("response code incorrect, Got 0x%02X expected 0x%02X\n",
resp.cmd, CMD_STATUS_RESPONSE);
return 5;
}
// print out returned status values
printf("accel_x=%d\n", (int32_t)ntohl(resp.status.accel.x));
printf("accel_y=%d\n", (int32_t)ntohl(resp.status.accel.y));
printf("accel_z=%d\n", (int32_t)ntohl(resp.status.accel.z));
printf("gyro_x=%d\n", (int32_t)ntohl(resp.status.gyro.x));
printf("gyro_y=%d\n", (int32_t)ntohl(resp.status.gyro.y));
printf("gyro_z=%d\n", (int32_t)ntohl(resp.status.gyro.z));
printf("mb_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_mb.x));
printf("mb_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_mb.y));
printf("mb_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_mb.z));
printf("nz_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_nz.x));
printf("nz_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_nz.y));
printf("nz_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_nz.z));
printf("pz_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_pz.x));
printf("pz_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_pz.y));
printf("pz_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_pz.z));
printf("ny_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_ny.x));
printf("ny_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_ny.y));
printf("ny_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_ny.z));
printf("py_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_py.x));
printf("py_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_py.y));
printf("py_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_py.z));
printf("nx_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_nx.x));
printf("nx_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_nx.y));
printf("nx_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_nx.z));
printf("px_mag_x=%d\n", (int32_t)ntohl(resp.status.mag_px.x));
printf("px_mag_y=%d\n", (int32_t)ntohl(resp.status.mag_px.y));
printf("px_mag_z=%d\n", (int32_t)ntohl(resp.status.mag_px.z));
return 0;
}
// prints out available commands for this util
static int print_usage(const char *name)
{
struct MulticallInfo *curr;
printf("adcs-util multicall binary, use the following names instead:\n");
for (curr = multicall; curr->func; curr++) {
printf(" %-16s %s\n", curr->name, curr->help);
}
return 0;
}
int main(int argc, char **argv)
{
struct MulticallInfo *curr;
char *exec_name;
exec_name = rindex(argv[0], '/');
if (!exec_name) {
exec_name = argv[0];
}
else {
exec_name++;
}
for (curr = multicall; curr->func; curr++) {
if (!strcmp(curr->name, exec_name)) {
return curr->func(argc, argv, curr);
}
}
if (argc > 1) {
for (curr = multicall; curr->func; curr++) {
if (!strcmp(curr->opt, argv[1])) {
return curr->func(argc - 1, argv + 1, curr);
}
}
}
else {
return print_usage(argv[0]);
}
return 0;
}