]> Pileus Git - ~andy/csm213a-hw/blob - hw2/messages.h
Make slave mode configurable
[~andy/csm213a-hw] / hw2 / messages.h
1 #ifndef MESSAGES_H
2 #define MESSAGES_H
3
4 #include <stdint.h>
5
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9
10 /***********************
11  * Message Definitions *
12  ***********************/
13
14 #define MSG_HEADER           0x1234
15
16 #define MSG_CTL_VALID_DEVICE 0x0001  // device id is valid
17 #define MSG_CTL_VALID_WORLD  0x0002  // world time is valid
18 #define MSG_CTL_VALID_START  0x0004  // start time is valid
19 #define MSG_CTL_VALID_PERIOD 0x0008  // period is valid
20 #define MSG_CTL_RELAY_MODE   0x4000  // relay output messages
21 #define MSG_CTL_VALID_SYNC   0x8000  // begin time sync
22
23 #pragma pack(1)
24
25 typedef enum {
26         MSG_ID_INIT,       // Device initialization
27         MSG_ID_SYNC,       // Time synchronization
28         MSG_ID_EVENT,      // Event occurred
29         MSG_MAX_ID,        // Maximum message ID
30 } msgid_t;
31
32 typedef struct {
33         uint32_t seconds;  // Seconds since 1970
34         uint32_t nanosec;  // Nanoseconds since 'seconds'
35 } ntime_t;
36
37 typedef struct {
38         uint16_t header;   // Message Header
39         uint16_t msgid;    // Message ID
40         uint16_t length;   // Body length
41         uint16_t cksum;    // Body checksum
42 } header_t;
43
44 typedef struct {
45         uint16_t control;  // Message control bits
46         uint16_t device;   // Device ID to use
47         ntime_t  world;    // World time (since 1970)
48         ntime_t  start;    // Transmit start time 
49         ntime_t  period;   // Transmit period
50 } init_msg_t;
51
52 typedef struct {
53         ntime_t  time;     // Time of previous message
54 } sync_msg_t;
55
56 typedef struct {
57         uint16_t device;   // Device ID
58         uint16_t event;    // Event ID
59         ntime_t  world;    // UTC Time of event
60         ntime_t  local;    // Time since turn-on
61 } event_msg_t;
62
63 #pragma pack()
64
65 /******************
66  * Message Parser *
67  ******************/
68
69 typedef void (*handler_t)(header_t *head, void *body);
70
71 typedef struct {
72         int       index;
73         int       state;
74         uint8_t   buffer[256];
75         handler_t handler[MSG_MAX_ID];
76 } parser_t;
77
78 void msg_register(parser_t *parser, int msgid, handler_t handler);
79
80 void msg_receive(parser_t *parser, int byte);
81
82 #ifdef __cplusplus
83 }
84 #endif
85
86 #endif