]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/main_comm.c
Split start and period set functions
[~andy/csm213a-hw] / hw2 / main_comm.c
index 5e261b40e07bfe2cc8a9ad2106fb8dedacdd1b31..54cdffcf23d18b9025359e8836f36d6a1d6d56c1 100644 (file)
@@ -44,7 +44,8 @@
  * Communication functions *
  ***************************/
 
-static uint32_t comm_device_id   = 0;
+static int      comm_device_id   = 0;
+static int      comm_relay_mode  = 0;
 
 const  uint64_t comm_sync_delay  = NSEC_PER_SEC / 100;
 static uint64_t comm_sync_due    = 0;
@@ -181,14 +182,12 @@ void comm_send_event(uint16_t event, uint64_t local)
        body.local  = ltime;
 
        // Transmit message to BBB
-       if (comm_device_id == 1) {
-               sirq_write(comm_sirq_bbb,  &head, sizeof(head));
-               sirq_write(comm_sirq_bbb,  &body, sizeof(body));
-       } else if (comm_device_id > 1) {
+       if (comm_relay_mode) {
                sirq_write(comm_sirq_mbed, &head, sizeof(head));
                sirq_write(comm_sirq_mbed, &body, sizeof(body));
        } else {
-               sirq_printf("no device id, skipping event\r\n");
+               sirq_write(comm_sirq_bbb,  &head, sizeof(head));
+               sirq_write(comm_sirq_bbb,  &body, sizeof(body));
        }
 }
 
@@ -214,34 +213,35 @@ void comm_handle_init(header_t *head, init_msg_t *body)
 
        // Debug output
        sirq_printf("initialize: %s %s %s %s %s\r\n",
-               body->valid & MSG_VALID_DEVICE ? "DEV"    : "dev",
-               body->valid & MSG_VALID_START  ? "START"  : "start",
-               body->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
-               body->valid & MSG_VALID_WORLD  ? "WORLD"  : "world",
-               body->valid & MSG_VALID_SYNC   ? "SYNC"   : "sync");
+               body->control & MSG_CTL_VALID_DEVICE ? "DEV"    : "dev",
+               body->control & MSG_CTL_VALID_START  ? "START"  : "start",
+               body->control & MSG_CTL_VALID_PERIOD ? "PERIOD" : "period",
+               body->control & MSG_CTL_VALID_WORLD  ? "WORLD"  : "world",
+               body->control & MSG_CTL_VALID_SYNC   ? "SYNC"   : "sync");
        sirq_printf("  dev    -- %d\r\n", body->device);
        time_printf("  start ", comm_read_time(body->start));
        time_printf("  period", comm_read_time(body->period));
        time_printf("  world ", comm_read_time(body->world));
 
        // Validate message parts and initialize
-       if (body->valid & MSG_VALID_DEVICE)
+       if (body->control & MSG_CTL_VALID_DEVICE)
                comm_device_id = body->device;
 
-       if (body->valid & MSG_VALID_START ||
-           body->valid & MSG_VALID_PERIOD) {
-               uint64_t start  = comm_read_time(body->start);
-               uint64_t period = comm_read_time(body->period);
-               emit_enable(start, period);
-       }
+       if (body->control & MSG_CTL_VALID_START)
+               emit_set_start(comm_read_time(body->start));
 
-       if (body->valid & MSG_VALID_WORLD) {
-               uint64_t world = comm_read_time(body->world);
-               uint64_t local = tdma_time();
-               time_ext_init(local, world);
-       }
+       if (body->control & MSG_CTL_VALID_PERIOD)
+               emit_set_period(comm_read_time(body->period));
+
+       if (body->control & MSG_CTL_VALID_WORLD)
+               time_ext_init(tdma_time(), comm_read_time(body->world));
 
-       if (body->valid & MSG_VALID_SYNC)
+       if (body->control & MSG_CTL_RELAY_MODE)
+               comm_relay_mode = 1;
+       else
+               comm_relay_mode = 0;
+
+       if (body->control & MSG_CTL_VALID_SYNC)
                comm_sync_due = tdma_time() + comm_sync_delay;
 }
 
@@ -277,9 +277,7 @@ void comm_handle_sync(header_t *head, sync_msg_t *body)
  */
 void comm_handle_event(header_t *head, event_msg_t *body)
 {
-       // Relay event from mbed to bbb
-       if (comm_device_id == 1) {
-               sirq_write(comm_sirq_bbb, head, sizeof(*head));
-               sirq_write(comm_sirq_bbb, body, sizeof(*body));
-       }
+       // Relay events from other mbeds
+       sirq_write(comm_sirq_bbb, head, sizeof(*head));
+       sirq_write(comm_sirq_bbb, body, sizeof(*body));
 }