#include #include #include #include #include "messages.h" void error(char *msg) { printf("Error: %s\n", msg); exit(0); } void dump(const char *label, uint8_t *data, int len) { int i; printf("%s: ", label); for (i = 0; i < len; i++) printf("%02hhx ", data[i]); printf("\n"); } int main(int argc, char **argv) { if (argc < 3) error("usage: control /dev/ttyACM0 0 sync"); // Parse args char *opt_tty = argv[1]; int opt_device = atoi(argv[2]); int opt_sync = argv[3] && !strcmp(argv[3], "sync") ? MSG_CTL_VALID_SYNC : 0; int opt_relay = opt_device == 2; // Lookup current wall-clock time struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); // Message buffers header_t head = {}; init_msg_t body = {}; // Set message header head.header = MSG_HEADER; head.msgid = MSG_ID_INIT; head.length = sizeof(init_msg_t); head.cksum = 0; // todo // Set valid flags body.control = MSG_CTL_VALID_DEVICE | MSG_CTL_VALID_START | MSG_CTL_VALID_PERIOD | MSG_CTL_VALID_WORLD | opt_relay | opt_sync; // Set message body body.device = opt_device; body.world.seconds = ts.tv_sec; body.world.nanosec = ts.tv_nsec; body.start.seconds = ts.tv_sec; body.start.nanosec = 0; //ts.tv_nsec; body.period.seconds = 0; body.period.nanosec = 100000000; // [s][m][u][n] // Transmit message FILE *fd = fopen(opt_tty, "a+"); if (!fd) error("opening device"); int len = 0; len += fwrite(&head, 1, sizeof(head), fd); len += fwrite(&body, 1, sizeof(body), fd); fclose(fd); // Debug output dump("head", (uint8_t*)&head, sizeof(head)); dump("body", (uint8_t*)&body, sizeof(body)); printf("wrote %d bytes\n", len); return 0; }