]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/control.c
Cleanup and test messages and events
[~andy/csm213a-hw] / hw2 / control.c
index fa54a9716f8bf3223c3722a8f60cf7953f00fb7a..ad97c75b887cdc47c265abede86b0b0024197793 100644 (file)
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
-
-#include <arpa/inet.h>
+#include <string.h>
 
 #include <time.h>
 
@@ -24,35 +23,57 @@ void dump(const char *label, uint8_t *data, int len)
 
 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;