Dependencies:   MQTT

Revision:
7:fd8e0604faaa
Parent:
6:6cb13ac483e0
--- a/mqtt.cpp	Wed Dec 11 20:12:21 2019 +0000
+++ b/mqtt.cpp	Thu Dec 12 17:25:27 2019 +0000
@@ -146,7 +146,7 @@
     if( msg->road_id != mqtt::instance()->mqtt_id )
     {
 #ifdef DEBUG_MQTT  
-    mqtt_pc.printf(MQTT_TAG"rcvd road %d "DELIM,msg->road_id);
+    mqtt_pc.printf(MQTT_TAG"rcvd road %d "DELIM,msg->road_clock);
 #endif
         // add our message to the queue no fucking clue what happens internally to
         // the message memory thanks mbed os 5 documentation
@@ -169,19 +169,17 @@
 {
     assert(msg != NULL && client != NULL);
     
-//    MQTT::Message message;
-    
 #ifdef DEBUG_MQTT
     mqtt_pc.printf(MQTT_TAG"sending position msg %d "DELIM,msg->road_id);
 #endif
+    
+    MQTT::Message tmessage;
 
-    // might be safest to memcopy this? but well if shit breaks then well fix
-//    memcpy(message.payload,msg,sizeof(position_msg_t));
-    message.payload = (void*)msg;
-    message.payloadlen = sizeof(position_msg_t); 
-    message.qos = MQTT::QOS1;
+    tmessage.payload = (void*)msg;
+    tmessage.payloadlen = sizeof(position_msg_t); 
+    tmessage.qos = MQTT::QOS1;
 
-    int rc = client->publish(POSITION_TOPIC,message);
+    int rc = client->publish(POSITION_TOPIC,tmessage);
     assert(rc == 0);  
 
     return 0;
@@ -191,19 +189,16 @@
 {
     assert(msg != NULL && client != NULL);
     
-//    MQTT::Message message;
-    
 #ifdef DEBUG_MQTT
-    mqtt_pc.printf(MQTT_TAG"sending road msg %d "DELIM,msg->road_id);
+    mqtt_pc.printf(MQTT_TAG"sending road msg %d "DELIM,msg->road_clock);
 #endif
+    MQTT::Message tmessage;
 
-    // might be safest to memcopy thius seems to work
-    //memcpy(message.payload,msg,sizeof(position_msg_t));
-    message.payload = (void*)msg;
-    message.payloadlen = sizeof(road_msg_t); 
-    message.qos = MQTT::QOS1;
+    tmessage.payload = (void*)msg;
+    tmessage.payloadlen = sizeof(road_msg_t); 
+    tmessage.qos = MQTT::QOS1;
 
-    int rc = client->publish(ROAD_TOPIC,message);
+    int rc = client->publish(ROAD_TOPIC,tmessage);
     assert(rc == 0);  
     
     return 0;
@@ -234,7 +229,7 @@
     
     // make a road based of mqtt id
     mqtt_id = 0;
-    if(strcmp(wifi->get_mac_address(),"2c:3a:e8:0b:75:06") == 0){
+    if(strcmp(wifi->get_mac_address(),"2c:3a:e8:0b:8e:77") == 0){
         mqtt_id = 0;
     }
     else{
@@ -280,6 +275,7 @@
             road_msg_t *road_msg = (road_msg_t*)revt.value.p;
             assert(road_msg != NULL);
             send_road_msg(mqtt_message,road_msg);
+            delete road_msg;
         }
 
       client->yield(10);
@@ -317,4 +313,48 @@
     assert(thread != NULL);
     thread->start( callback(this,&mqtt::manage_network) );
     
+}
+
+// before the next run make sure
+// that everything is cleaned up properly
+// the control queue may have one entry due to 
+// the last iteration ending
+void mqtt::clear_queues()
+{
+    while(!position_queue.empty())
+    {
+        osEvent revt = position_queue.get();
+        assert(revt.status == osEventMessage);
+        //printf("cleaned up queue\n");
+            //
+        position_msg_t *msg = (position_msg_t*)revt.value.p;
+        delete msg;     
+    }
+    for(int i = 0; i < 5; i++)
+    {
+        while(!control_queue[i].empty())
+        {
+            osEvent revt = control_queue[i].get();
+            assert(revt.status == osEventMessage);
+            //printf("cleaned up control queue\n");
+            //
+            control_msg_t *msg = (control_msg_t*)revt.value.p;
+            delete msg;     
+        }  
+    }
+       
+    while(!network_to_road_queue.empty())
+    {
+        osEvent evt = network_to_road_queue.get();
+        assert(evt.status == osEventMessage);
+        road_msg_t *message = (road_msg_t*)evt.value.p;
+        delete message;     
+    }  
+    while(!road_to_network_queue.empty())
+    {
+        osEvent evt = road_to_network_queue.get();
+        assert(evt.status == osEventMessage);
+        road_msg_t *message = (road_msg_t*)evt.value.p;
+        delete message;     
+    }  
 }
\ No newline at end of file