#include <stdio.h>
#include <stdlib.h>
-
-#include <arpa/inet.h>
+#include <string.h>
#include <time.h>
int main(int argc, char **argv)
{
- char *device = argv[1];
- if (!device)
- error("usage: host /dev/ttyACM0");
+ if (argc < 3)
+ error("usage: host /dev/ttyACM0 0 sync");
- header_t head = {};
- sync_msg_t body = {};
+ // Parse args
+ char *opt_tty = argv[1];
+ int opt_device = atoi(argv[2]);
+ int opt_sync = argv[3] && !strcmp(argv[3], "sync") ?
+ MSG_VALID_SYNC : 0;
+ // 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_SYNC;
- head.length = sizeof(sync_msg_t);
+ head.msgid = MSG_ID_INIT;
+ head.length = sizeof(init_msg_t);
head.cksum = 0; // todo
- body.seq = 0;
- body.time.seconds = ts.tv_sec;
- body.time.nanosec = ts.tv_nsec;
+ // Set valid flags
+ body.valid = MSG_VALID_DEVICE
+ | MSG_VALID_START
+ | MSG_VALID_PERIOD
+ | MSG_VALID_WORLD
+ | opt_sync;
- dump("head", (uint8_t*)&head, sizeof(head));
- dump("body", (uint8_t*)&body, sizeof(body));
+ // 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 = ts.tv_nsec;
+ body.period.seconds = 1;
+ body.period.nanosec = 0;
- FILE *fd = fopen(device, "a+");
+ // 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;