]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/messages.h
Make control script more configurable
[~andy/csm213a-hw] / hw2 / messages.h
index e18166bd66b987dc37d588ae700f9355550b5852..cd9439832c85b534025f196e2350ce79176163f0 100644 (file)
@@ -1,15 +1,24 @@
+#ifndef MESSAGES_H
+#define MESSAGES_H
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /***********************
  * Message Definitions *
  ***********************/
 
-#include <stdint.h>
-
-#define MSG_HEADER       0x1234
+#define MSG_HEADER           0x1234
 
-#define MSG_VALID_DEVICE 0x0001
-#define MSG_VALID_START  0x0002
-#define MSG_VALID_PERIOD 0x0004
-#define MSG_VALID_WORLD  0x0008
+#define MSG_CTL_VALID_DEVICE 0x0001  // device id is valid
+#define MSG_CTL_VALID_WORLD  0x0002  // world time is valid
+#define MSG_CTL_VALID_START  0x0004  // start time is valid
+#define MSG_CTL_VALID_PERIOD 0x0008  // period is valid
+#define MSG_CTL_RELAY_MODE   0x4000  // relay output messages
+#define MSG_CTL_BEGIN_SYNC   0x8000  // begin time sync
 
 #pragma pack(1)
 
@@ -21,7 +30,7 @@ typedef enum {
 } msgid_t;
 
 typedef struct {
-       uint32_t seconds;  // Seconds since 1970 (without leap seconds)
+       uint32_t seconds;  // Seconds since 1970
        uint32_t nanosec;  // Nanoseconds since 'seconds'
 } ntime_t;
 
@@ -33,22 +42,45 @@ typedef struct {
 } header_t;
 
 typedef struct {
-       uint16_t valid;    // Message valid bits
+       uint16_t control;  // Message control bits
        uint16_t device;   // Device ID to use
+       ntime_t  world;    // World time (since 1970)
        ntime_t  start;    // Transmit start time 
        ntime_t  period;   // Transmit period
-       ntime_t  world;    // World time (since 1970)
 } init_msg_t;
 
 typedef struct {
-       uint32_t seq;      // Current sequence counter
        ntime_t  time;     // Time of previous message
 } sync_msg_t;
 
 typedef struct {
        uint16_t device;   // Device ID
        uint16_t event;    // Event ID
-       ntime_t  time;     // Timestamp
+       ntime_t  world;    // UTC Time of event
+       ntime_t  local;    // Time since turn-on
 } event_msg_t;
 
 #pragma pack()
+
+/******************
+ * Message Parser *
+ ******************/
+
+typedef void (*handler_t)(header_t *head, void *body);
+
+typedef struct {
+       int       index;
+       int       state;
+       uint8_t   buffer[256];
+       handler_t handler[MSG_MAX_ID];
+} parser_t;
+
+void msg_register(parser_t *parser, int msgid, handler_t handler);
+
+void msg_receive(parser_t *parser, int byte);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif