Emanuel Kuflik
/
smat_controller
mqtt.h
- Committer:
- MannyK
- Date:
- 2019-12-06
- Revision:
- 4:64c6fc70ddb7
- Parent:
- 2:f10d6fecb345
- Child:
- 5:e0d8e5e922f1
File content as of revision 4:64c6fc70ddb7:
#ifndef _MQTT_H_ #define _MQTT_H_ #include "mbed.h" #include "MQTTNetwork.h" #include "MQTTClient.h" #include "MQTTmbed.h" #include <assert.h> #define MAX_CARS_ON_ROAD 5 enum drive_state_t{ NORMAL_STATE = 0, STOPPED_STATE, CROSSING_STATE }; typedef struct position_msg{ int position; int speed; int car_id; int road_id; int counter; // for debugging and ensuring everyone is synchronized drive_state_t state; // for debugging }position_msg_t; typedef struct road_msg{ int id; }road_msg_t; typedef struct other_road{ bool done; }other_road_t; typedef struct control_msg{ int speed; int car_id; int road_id; int counter; // for debugging synchronization }control_msg_t; class mqtt{ private: // static functions for control message callbacks static void control_message_arrived(MQTT::MessageData& md); // functions for creatin of mqtt and wifi bring up WiFiInterface* setup_wifi(); int send_position_msg(position_msg_t* msg); int send_road_msg(position_msg_t* msg); void manage_network(); void bringup_network(); // data structures needed for creation of network MQTT::Client<MQTTNetwork, Countdown>* client; MQTT::Client<MQTTNetwork, Countdown>* setup_mqtt(MQTTNetwork& network); MQTTNetwork* new_network; // thread that will manage the mqtt network status Thread* thread; // queue for sending position msessages Queue<position_msg_t, 16> position_queue; // queue for sending road msessages Queue<road_msg_t, 1> road_queue; // queue for control message passsing between mqtt and car threads Queue<control_msg_t, 1> control_queue[MAX_CARS_ON_ROAD]; public: // singleton instance for managing network // we can only have one network so this should prohibit accidental // creations of multiple objects static mqtt* mqtt_singleton; // int mqtt_id; // empty constructor mqtt(); // setup and tear down API's void setup_network(); int shutdown_network(); static mqtt *instance() { if (!mqtt_singleton) mqtt_singleton = new mqtt; return mqtt_singleton; } // adding to a queue to be sent on mqtt network inline void add_to_position_queue(position_msg_t* msg) { position_queue.put(msg); } // adding to road queue to be sent on mqtt network inline void add_to_road_queue(road_msg_t* msg) { road_queue.put(msg); } // adding to a queue to be sent on mqtt network inline void add_to_control_queue(int car_id,control_msg_t* msg) { control_queue[car_id].put(msg); } // adding to a queue to be sent on mqtt network inline control_msg_t* get_control_msg(int car_id) { osEvent evt = control_queue[car_id].get(); assert(evt.status == osEventMessage); control_msg_t *message = (control_msg_t*)evt.value.p; return message; } inline bool get_road_msg(int curr_id) { int temp = curr_id; while(temp == curr_id){ osEvent evt = road_queue.get(); assert(evt.status == osEventMessage); road_msg_t *message = (road_msg_t*)evt.value.p; temp = message->id; } return true; } }; #endif