]> Pileus Git - ~andy/csm213a-hw/blob - hw2/control.c
Work on second mbed
[~andy/csm213a-hw] / hw2 / control.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <arpa/inet.h>
5
6 #include <time.h>
7
8 #include "messages.h"
9
10 void error(char *msg)
11 {
12         printf("Error: %s\n", msg);
13         exit(0);
14 }
15
16 void dump(const char *label, uint8_t *data, int len)
17 {
18         int i;
19         printf("%s: ", label);
20         for (i = 0; i < len; i++)
21                 printf("%02hhx ", data[i]);
22         printf("\n");
23 }
24
25 int main(int argc, char **argv)
26 {
27         char *device = argv[1];
28         if (!device)
29                 error("usage: host /dev/ttyACM0");
30
31         header_t   head = {};
32         sync_msg_t body = {};
33
34         struct timespec ts;
35         clock_gettime(CLOCK_REALTIME, &ts);
36
37         head.header = MSG_HEADER;
38         head.msgid  = MSG_ID_SYNC;
39         head.length = sizeof(sync_msg_t);
40         head.cksum  = 0; // todo
41
42         body.seq    = 0;
43         body.time.seconds = ts.tv_sec;
44         body.time.nanosec = ts.tv_nsec;
45
46         dump("head", (uint8_t*)&head, sizeof(head));
47         dump("body", (uint8_t*)&body, sizeof(body));
48
49         FILE *fd = fopen(device, "a+");
50         if (!fd) error("opening device");
51         int len = 0;
52         len += fwrite(&head, 1, sizeof(head), fd);
53         len += fwrite(&body, 1, sizeof(body), fd);
54         fclose(fd);
55
56         printf("wrote %d bytes\n", len);
57
58         return 0;
59 }