|
| 1 | +/* |
| 2 | + * Copyright PolySat, California Polytechnic State University, San Luis Obispo. [email protected] |
| 3 | + * This file is part of libproc, a PolySat library. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program 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 |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +#include <string.h> |
| 19 | +#include <limits.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <ctype.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <sys/types.h> |
| 24 | +#include <dirent.h> |
| 25 | +#include <sys/stat.h> |
| 26 | +#include <unistd.h> |
| 27 | +#include <sys/time.h> |
| 28 | + |
| 29 | +#include "telm_dict.h" |
| 30 | +#include "ipc.h" |
| 31 | +#include "priorityQueue.h" |
| 32 | +#include "debug.h" |
| 33 | + |
| 34 | +int TELM_print_datalogger_info(struct TELMTelemetryInfo *points, |
| 35 | + const char *dfl_name, const char *dfl_path, int argc, char **argv) |
| 36 | +{ |
| 37 | + int opt; |
| 38 | + int header = 0; |
| 39 | + int mode = 0; |
| 40 | + int start_time = 5; |
| 41 | + int period = 300; |
| 42 | + struct TELMTelemetryInfo *curr; |
| 43 | + const char *name = dfl_name; |
| 44 | + const char *path = dfl_path; |
| 45 | + struct StringList { |
| 46 | + const char *name; |
| 47 | + struct StringList *next; |
| 48 | + } *itr, *head = NULL; |
| 49 | + |
| 50 | +#ifdef __APPLE__ |
| 51 | + optind = 1; |
| 52 | +#else |
| 53 | + optind = 0; |
| 54 | +#endif |
| 55 | + |
| 56 | + while ((opt = getopt(argc, argv, "s:n:x:m:t:p:H")) != -1) { |
| 57 | + switch(opt) { |
| 58 | + case 'p': |
| 59 | + period = atol(optarg); |
| 60 | + break; |
| 61 | + case 's': |
| 62 | + start_time = atol(optarg); |
| 63 | + break; |
| 64 | + case 't': |
| 65 | + path = optarg; |
| 66 | + break; |
| 67 | + case 'H': |
| 68 | + header = 1; |
| 69 | + break; |
| 70 | + case 'n': |
| 71 | + name = optarg; |
| 72 | + break; |
| 73 | + case 'x': |
| 74 | + itr = malloc(sizeof(*itr)); |
| 75 | + itr->next = head; |
| 76 | + itr->name = optarg; |
| 77 | + head = itr; |
| 78 | + break; |
| 79 | + case 'm': |
| 80 | + mode = atol(optarg); |
| 81 | + break; |
| 82 | + default: |
| 83 | + goto usage; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (optind < (argc)) |
| 88 | + goto usage; |
| 89 | + |
| 90 | + if (mode == 2) { |
| 91 | + printf("%s\n", name); |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + if (header) |
| 96 | + printf("<EVENT>\n" |
| 97 | + " NAME=%s\n" |
| 98 | + " PROC_NAME=%s\n" |
| 99 | + " NUM=0\n" |
| 100 | + " START_TIME=P+%d\n" |
| 101 | + " SCHED_TIME=%d\n" |
| 102 | + " DURATION=0\n" |
| 103 | + "</EVENT>\n\n" |
| 104 | + "<SUBPROCESS>\n" |
| 105 | + " NAME=%s\n" |
| 106 | + " PROC_PATH=%s\n" |
| 107 | + " POWER=1\n" |
| 108 | + " POWER_PROC=./none\n" |
| 109 | + " PARAM=\n\n", |
| 110 | + name, name, start_time, period, name, path); |
| 111 | + |
| 112 | + for (curr = points; curr->id; curr++) { |
| 113 | + int ignore = 0; |
| 114 | + for (itr = head; itr; itr = itr->next) { |
| 115 | + if (0 == strcasecmp(itr->name, curr->id)) { |
| 116 | + ignore = 1; |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (mode == 1) { |
| 122 | + if (!ignore) |
| 123 | + printf("%s\n", curr->id); |
| 124 | + } |
| 125 | + else { |
| 126 | + if (!ignore) { |
| 127 | + printf(" <SENSOR>\n"); |
| 128 | + printf(" NAME=%s\n", name); |
| 129 | + printf(" SENSOR_KEY=%s\n", curr->id); |
| 130 | + printf(" LOCATION=%s\n", curr->location); |
| 131 | + printf(" GROUPS=%s\n", curr->group); |
| 132 | + printf(" </SENSOR>\n"); |
| 133 | + } |
| 134 | + else { |
| 135 | + printf(" <IGNORE>\n"); |
| 136 | + printf(" SENSOR_KEY=%s\n", curr->id); |
| 137 | + printf(" </IGNORE>\n"); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (header) |
| 143 | + printf("</SUBPROCESS>\n"); |
| 144 | + |
| 145 | + return 0; |
| 146 | + |
| 147 | +usage: |
| 148 | + printf("%s [-n <subprocess name>] [-H] [-p <period>] [-s <start time>] [-t <telemetry path] [-x <exclude tlm id>] [-x <>] ...\n", |
| 149 | + argv[0]); |
| 150 | + return 0; |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | +int TELM_print_sensor_metadata(struct TELMTelemetryInfo *points, |
| 155 | + struct TELMEventInfo *events) |
| 156 | +{ |
| 157 | + struct TELMTelemetryInfo *curr; |
| 158 | + struct TELMBitfieldInfo *bitr; |
| 159 | + struct TELMEventInfo *eitr; |
| 160 | + const char *type; |
| 161 | + |
| 162 | + for (curr = points; curr->id; curr++) { |
| 163 | + type = "SENSOR"; |
| 164 | + if (curr->computed_by) |
| 165 | + type = "VSENSOR"; |
| 166 | + |
| 167 | + printf("<%s>\n", type); |
| 168 | + printf(" KEY=%s\n", curr->id); |
| 169 | + printf(" LOCATION=%s\n", curr->location); |
| 170 | + printf(" SUBSYSTEM=%s\n", curr->group); |
| 171 | + printf(" UNITS=%s\n", curr->units); |
| 172 | + printf(" DIVISOR=%lf\n", curr->divisor); |
| 173 | + printf(" OFFSET=%lf\n", curr->offset); |
| 174 | + printf(" NAME=%s\n", curr->name); |
| 175 | + printf(" DESCRIPTION=%s\n", curr->desc); |
| 176 | + if (curr->computed_by) |
| 177 | + printf(" FUNCTION=%s\n", curr->computed_by); |
| 178 | + |
| 179 | + if (curr->bitfields) { |
| 180 | + for (bitr = curr->bitfields; bitr->set_label; bitr++) { |
| 181 | + printf(" <ENUM>\n"); |
| 182 | + printf(" VALUE=%u\n", bitr->value); |
| 183 | + printf(" LABEL=%s\n", bitr->set_label); |
| 184 | + if (bitr->clear_label) |
| 185 | + printf(" UNSET=%s\n", bitr->clear_label); |
| 186 | + printf(" </ENUM>\n"); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + printf("</%s>\n", type); |
| 191 | + } |
| 192 | + |
| 193 | + for (eitr = events; eitr->port_name; ++eitr) { |
| 194 | + printf("<EVENT>\n"); |
| 195 | + printf(" PORT_NAME=%s\n", eitr->port_name); |
| 196 | + printf(" PORT=%u\n", socket_get_addr_by_name(eitr->port_name)); |
| 197 | + printf(" ID=%u\n", eitr->id); |
| 198 | + printf(" NAME=%s\n", eitr->name); |
| 199 | + printf(" DESCRIPTION=%s\n", eitr->desc); |
| 200 | + printf("</EVENT>\n"); |
| 201 | + } |
| 202 | + |
| 203 | + return 0; |
| 204 | +} |
| 205 | + |
| 206 | +int TELM_print_json_telem_dict(struct TELMTelemetryInfo *points, |
| 207 | + struct TELMEventInfo *events, int argc, char **argv) |
| 208 | +{ |
| 209 | + int opt; |
| 210 | + int first = 1; |
| 211 | + int mode = 0; |
| 212 | + struct TELMTelemetryInfo *curr; |
| 213 | + struct StringList { |
| 214 | + const char *name; |
| 215 | + struct StringList *next; |
| 216 | + } *itr, *head = NULL; |
| 217 | + struct TELMEventInfo *eitr; |
| 218 | + struct TELMBitfieldInfo *bitr; |
| 219 | + const char *type; |
| 220 | + |
| 221 | +#ifdef __APPLE__ |
| 222 | + optind = 1; |
| 223 | +#else |
| 224 | + optind = 0; |
| 225 | +#endif |
| 226 | + |
| 227 | + while ((opt = getopt(argc, argv, "x:m:")) != -1) { |
| 228 | + switch(opt) { |
| 229 | + case 'x': |
| 230 | + itr = malloc(sizeof(*itr)); |
| 231 | + itr->next = head; |
| 232 | + itr->name = optarg; |
| 233 | + head = itr; |
| 234 | + break; |
| 235 | + case 'm': |
| 236 | + mode = atol(optarg); |
| 237 | + break; |
| 238 | + default: |
| 239 | + goto usage; |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + if (optind < (argc)) |
| 244 | + goto usage; |
| 245 | + |
| 246 | + first = 1; |
| 247 | + for (curr = points; curr->id; curr++) { |
| 248 | + int ignore = 0; |
| 249 | + |
| 250 | + for (itr = head; itr; itr = itr->next) { |
| 251 | + if (0 == strcasecmp(itr->name, curr->id)) { |
| 252 | + ignore = 1; |
| 253 | + break; |
| 254 | + } |
| 255 | + } |
| 256 | + if (ignore) |
| 257 | + continue; |
| 258 | + |
| 259 | + if (mode == 1 && !curr->computed_by) |
| 260 | + type = "sensor"; |
| 261 | + else if (mode == 3 && curr->computed_by) |
| 262 | + type = "virtual_sensor"; |
| 263 | + else |
| 264 | + continue; |
| 265 | + |
| 266 | + if (!first) |
| 267 | + printf(",\n"); |
| 268 | + printf("{\n"); |
| 269 | + printf(" \"type\": \"%s\",\n", type); |
| 270 | + printf(" \"key\": \"%s\",\n", curr->id); |
| 271 | + printf(" \"location\": \"%s\",\n", curr->location); |
| 272 | + printf(" \"subsystem\": \"%s\",\n", curr->group); |
| 273 | + printf(" \"units\": \"%s\",\n", curr->units); |
| 274 | + printf(" \"divisor\": %lf,\n", curr->divisor); |
| 275 | + printf(" \"offset\": %lf,\n", curr->offset); |
| 276 | + printf(" \"name\": \"%s\",\n", curr->name); |
| 277 | + if (curr->computed_by) |
| 278 | + printf(" \"function\": \"%s\",\n", curr->computed_by); |
| 279 | + |
| 280 | + if (curr->bitfields) { |
| 281 | + int efirst = 1; |
| 282 | + printf(" \"enums\": [\n"); |
| 283 | + for (bitr = curr->bitfields; bitr->set_label; bitr++) { |
| 284 | + if (!efirst) |
| 285 | + printf(",\n"); |
| 286 | + |
| 287 | + printf(" {\n"); |
| 288 | + printf(" \"value\": %u,\n", bitr->value); |
| 289 | + if (bitr->clear_label) |
| 290 | + printf(" \"unset\": \"%s\",\n", bitr->clear_label); |
| 291 | + printf(" \"label\": \"%s\"\n", bitr->set_label); |
| 292 | + printf(" }"); |
| 293 | + |
| 294 | + efirst = 0; |
| 295 | + } |
| 296 | + printf(" ],\n"); |
| 297 | + } |
| 298 | + |
| 299 | + printf(" \"description\": \"%s\"\n", curr->desc); |
| 300 | + |
| 301 | + printf("}"); |
| 302 | + first = 0; |
| 303 | + } |
| 304 | + if (!first) |
| 305 | + printf("\n"); |
| 306 | + |
| 307 | + first = 1; |
| 308 | + for (eitr = events; mode == 2 && eitr->id; eitr++) { |
| 309 | + if (!first) |
| 310 | + printf(",\n"); |
| 311 | + printf("{\n"); |
| 312 | + |
| 313 | + printf(" \"port_name\" : \"%s\",\n", eitr->port_name); |
| 314 | + printf(" \"port\" : %u,\n", socket_get_addr_by_name(eitr->port_name)); |
| 315 | + printf(" \"id\" : =%u,\n", eitr->id); |
| 316 | + printf(" \"name\" : \"%s\",\n", eitr->name); |
| 317 | + printf(" \"description\" : \"%s\"\n", eitr->desc); |
| 318 | + |
| 319 | + printf("}"); |
| 320 | + first = 0; |
| 321 | + } |
| 322 | + if (!first) |
| 323 | + printf("\n"); |
| 324 | + |
| 325 | + return 0; |
| 326 | + |
| 327 | +usage: |
| 328 | + printf("%s [-m <mode>] [-x <exclude tlm id>] [-x <>] ...\n", |
| 329 | + argv[0]); |
| 330 | + return 0; |
| 331 | +} |
0 commit comments