Dependencies:   MQTT

Committer:
MannyK
Date:
Fri Dec 06 20:34:52 2019 +0000
Revision:
4:64c6fc70ddb7
Parent:
2:f10d6fecb345
Child:
5:e0d8e5e922f1
Road sync

Who changed what in which revision?

UserRevisionLine numberNew contents of line
micallef25 2:f10d6fecb345 1 #ifndef _MQTT_H_
micallef25 2:f10d6fecb345 2 #define _MQTT_H_
micallef25 2:f10d6fecb345 3
micallef25 2:f10d6fecb345 4 #include "mbed.h"
micallef25 2:f10d6fecb345 5 #include "MQTTNetwork.h"
micallef25 2:f10d6fecb345 6 #include "MQTTClient.h"
micallef25 2:f10d6fecb345 7 #include "MQTTmbed.h"
micallef25 2:f10d6fecb345 8 #include <assert.h>
micallef25 2:f10d6fecb345 9
micallef25 2:f10d6fecb345 10 #define MAX_CARS_ON_ROAD 5
micallef25 2:f10d6fecb345 11
micallef25 2:f10d6fecb345 12 enum drive_state_t{
micallef25 2:f10d6fecb345 13 NORMAL_STATE = 0,
micallef25 2:f10d6fecb345 14 STOPPED_STATE,
micallef25 2:f10d6fecb345 15 CROSSING_STATE
micallef25 2:f10d6fecb345 16 };
micallef25 2:f10d6fecb345 17
micallef25 2:f10d6fecb345 18 typedef struct position_msg{
micallef25 2:f10d6fecb345 19 int position;
micallef25 2:f10d6fecb345 20 int speed;
micallef25 2:f10d6fecb345 21 int car_id;
micallef25 2:f10d6fecb345 22 int road_id;
micallef25 2:f10d6fecb345 23 int counter; // for debugging and ensuring everyone is synchronized
micallef25 2:f10d6fecb345 24 drive_state_t state; // for debugging
micallef25 2:f10d6fecb345 25 }position_msg_t;
micallef25 2:f10d6fecb345 26
MannyK 4:64c6fc70ddb7 27 typedef struct road_msg{
MannyK 4:64c6fc70ddb7 28 int id;
MannyK 4:64c6fc70ddb7 29 }road_msg_t;
MannyK 4:64c6fc70ddb7 30
MannyK 4:64c6fc70ddb7 31 typedef struct other_road{
MannyK 4:64c6fc70ddb7 32 bool done;
MannyK 4:64c6fc70ddb7 33 }other_road_t;
micallef25 2:f10d6fecb345 34
micallef25 2:f10d6fecb345 35 typedef struct control_msg{
micallef25 2:f10d6fecb345 36 int speed;
micallef25 2:f10d6fecb345 37 int car_id;
micallef25 2:f10d6fecb345 38 int road_id;
micallef25 2:f10d6fecb345 39 int counter; // for debugging synchronization
micallef25 2:f10d6fecb345 40 }control_msg_t;
micallef25 2:f10d6fecb345 41
micallef25 2:f10d6fecb345 42
micallef25 2:f10d6fecb345 43 class mqtt{
micallef25 2:f10d6fecb345 44
micallef25 2:f10d6fecb345 45 private:
micallef25 2:f10d6fecb345 46 // static functions for control message callbacks
micallef25 2:f10d6fecb345 47 static void control_message_arrived(MQTT::MessageData& md);
micallef25 2:f10d6fecb345 48
micallef25 2:f10d6fecb345 49 // functions for creatin of mqtt and wifi bring up
micallef25 2:f10d6fecb345 50 WiFiInterface* setup_wifi();
micallef25 2:f10d6fecb345 51 int send_position_msg(position_msg_t* msg);
MannyK 4:64c6fc70ddb7 52 int send_road_msg(position_msg_t* msg);
micallef25 2:f10d6fecb345 53 void manage_network();
micallef25 2:f10d6fecb345 54 void bringup_network();
micallef25 2:f10d6fecb345 55
micallef25 2:f10d6fecb345 56 // data structures needed for creation of network
micallef25 2:f10d6fecb345 57 MQTT::Client<MQTTNetwork, Countdown>* client;
micallef25 2:f10d6fecb345 58 MQTT::Client<MQTTNetwork, Countdown>* setup_mqtt(MQTTNetwork& network);
micallef25 2:f10d6fecb345 59 MQTTNetwork* new_network;
micallef25 2:f10d6fecb345 60
micallef25 2:f10d6fecb345 61 // thread that will manage the mqtt network status
micallef25 2:f10d6fecb345 62 Thread* thread;
micallef25 2:f10d6fecb345 63
micallef25 2:f10d6fecb345 64 // queue for sending position msessages
micallef25 2:f10d6fecb345 65 Queue<position_msg_t, 16> position_queue;
micallef25 2:f10d6fecb345 66
MannyK 4:64c6fc70ddb7 67 // queue for sending road msessages
MannyK 4:64c6fc70ddb7 68 Queue<road_msg_t, 1> road_queue;
MannyK 4:64c6fc70ddb7 69
micallef25 2:f10d6fecb345 70 // queue for control message passsing between mqtt and car threads
micallef25 2:f10d6fecb345 71 Queue<control_msg_t, 1> control_queue[MAX_CARS_ON_ROAD];
micallef25 2:f10d6fecb345 72
micallef25 2:f10d6fecb345 73
micallef25 2:f10d6fecb345 74 public:
micallef25 2:f10d6fecb345 75 // singleton instance for managing network
micallef25 2:f10d6fecb345 76 // we can only have one network so this should prohibit accidental
micallef25 2:f10d6fecb345 77 // creations of multiple objects
micallef25 2:f10d6fecb345 78 static mqtt* mqtt_singleton;
micallef25 2:f10d6fecb345 79
micallef25 2:f10d6fecb345 80 //
micallef25 2:f10d6fecb345 81 int mqtt_id;
micallef25 2:f10d6fecb345 82
micallef25 2:f10d6fecb345 83 // empty constructor
micallef25 2:f10d6fecb345 84 mqtt();
micallef25 2:f10d6fecb345 85
micallef25 2:f10d6fecb345 86 // setup and tear down API's
micallef25 2:f10d6fecb345 87 void setup_network();
micallef25 2:f10d6fecb345 88 int shutdown_network();
micallef25 2:f10d6fecb345 89
micallef25 2:f10d6fecb345 90
micallef25 2:f10d6fecb345 91 static mqtt *instance()
micallef25 2:f10d6fecb345 92 {
micallef25 2:f10d6fecb345 93 if (!mqtt_singleton)
micallef25 2:f10d6fecb345 94 mqtt_singleton = new mqtt;
micallef25 2:f10d6fecb345 95 return mqtt_singleton;
micallef25 2:f10d6fecb345 96 }
micallef25 2:f10d6fecb345 97
micallef25 2:f10d6fecb345 98 // adding to a queue to be sent on mqtt network
micallef25 2:f10d6fecb345 99 inline void add_to_position_queue(position_msg_t* msg)
micallef25 2:f10d6fecb345 100 {
micallef25 2:f10d6fecb345 101 position_queue.put(msg);
micallef25 2:f10d6fecb345 102 }
micallef25 2:f10d6fecb345 103
MannyK 4:64c6fc70ddb7 104 // adding to road queue to be sent on mqtt network
MannyK 4:64c6fc70ddb7 105 inline void add_to_road_queue(road_msg_t* msg)
MannyK 4:64c6fc70ddb7 106 {
MannyK 4:64c6fc70ddb7 107 road_queue.put(msg);
MannyK 4:64c6fc70ddb7 108 }
MannyK 4:64c6fc70ddb7 109
micallef25 2:f10d6fecb345 110 // adding to a queue to be sent on mqtt network
micallef25 2:f10d6fecb345 111 inline void add_to_control_queue(int car_id,control_msg_t* msg)
micallef25 2:f10d6fecb345 112 {
micallef25 2:f10d6fecb345 113 control_queue[car_id].put(msg);
micallef25 2:f10d6fecb345 114 }
micallef25 2:f10d6fecb345 115
micallef25 2:f10d6fecb345 116 // adding to a queue to be sent on mqtt network
micallef25 2:f10d6fecb345 117 inline control_msg_t* get_control_msg(int car_id)
micallef25 2:f10d6fecb345 118 {
micallef25 2:f10d6fecb345 119 osEvent evt = control_queue[car_id].get();
micallef25 2:f10d6fecb345 120 assert(evt.status == osEventMessage);
micallef25 2:f10d6fecb345 121 control_msg_t *message = (control_msg_t*)evt.value.p;
micallef25 2:f10d6fecb345 122 return message;
micallef25 2:f10d6fecb345 123 }
micallef25 2:f10d6fecb345 124
MannyK 4:64c6fc70ddb7 125 inline bool get_road_msg(int curr_id)
MannyK 4:64c6fc70ddb7 126 {
MannyK 4:64c6fc70ddb7 127 int temp = curr_id;
MannyK 4:64c6fc70ddb7 128 while(temp == curr_id){
MannyK 4:64c6fc70ddb7 129 osEvent evt = road_queue.get();
MannyK 4:64c6fc70ddb7 130 assert(evt.status == osEventMessage);
MannyK 4:64c6fc70ddb7 131 road_msg_t *message = (road_msg_t*)evt.value.p;
MannyK 4:64c6fc70ddb7 132 temp = message->id;
MannyK 4:64c6fc70ddb7 133 }
MannyK 4:64c6fc70ddb7 134 return true;
MannyK 4:64c6fc70ddb7 135 }
MannyK 4:64c6fc70ddb7 136
micallef25 2:f10d6fecb345 137 };
micallef25 2:f10d6fecb345 138
micallef25 2:f10d6fecb345 139
micallef25 2:f10d6fecb345 140 #endif