Eric Micallef / Mbed OS smat_controller

Dependencies:   MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mqtt.h Source File

mqtt.h

00001 #ifndef _MQTT_H_
00002 #define _MQTT_H_
00003 
00004 #include "mbed.h"
00005 #include "MQTTNetwork.h"
00006 #include "MQTTClient.h"
00007 #include "MQTTmbed.h"
00008 #include <assert.h>
00009 
00010 #define MAX_CARS_ON_ROAD 5
00011 
00012 enum drive_state_t{
00013  NORMAL_STATE = 0,
00014  STOPPED_STATE,
00015  CROSSING_STATE
00016 };
00017 
00018 typedef struct position_msg{
00019     int position;
00020     int speed;
00021     int car_id;
00022     int road_id; 
00023     int counter; // for debugging and ensuring everyone is synchronized
00024     drive_state_t state; // for debugging
00025 }position_msg_t;
00026 
00027 
00028 typedef struct control_msg{
00029     int speed;
00030     int car_id;
00031     int road_id;
00032     int counter; // for debugging synchronization
00033 }control_msg_t;
00034 
00035 
00036 class mqtt{
00037 
00038 private:
00039     // static functions for control message callbacks
00040     static void control_message_arrived(MQTT::MessageData& md);
00041     
00042     // functions for creatin of mqtt and wifi bring up
00043     WiFiInterface* setup_wifi();
00044     int send_position_msg(position_msg_t* msg);
00045     void manage_network();
00046     void bringup_network();
00047     
00048     // data structures needed for creation of network
00049     MQTT::Client<MQTTNetwork, Countdown>* client;
00050     MQTT::Client<MQTTNetwork, Countdown>* setup_mqtt(MQTTNetwork& network); 
00051     MQTTNetwork* new_network;
00052     
00053     // thread that will manage the mqtt network status
00054     Thread* thread;
00055     
00056     // queue for sending position msessages
00057     Queue<position_msg_t, 16> position_queue;
00058     
00059     // queue for control message passsing between mqtt and car threads
00060     Queue<control_msg_t, 1> control_queue[MAX_CARS_ON_ROAD];
00061 
00062     
00063 public:
00064     // singleton instance for managing network
00065     // we can only have one network so this should prohibit accidental
00066     // creations of multiple objects
00067     static mqtt* mqtt_singleton;
00068     
00069     //
00070     int mqtt_id;
00071     
00072     // empty constructor
00073     mqtt();
00074     
00075     // setup and tear down API's
00076     void setup_network();
00077     int shutdown_network();
00078 
00079     
00080     static mqtt *instance()
00081     {
00082         if (!mqtt_singleton)
00083           mqtt_singleton = new mqtt;
00084         return mqtt_singleton;
00085     }
00086     
00087     // adding to a queue to be sent on mqtt network
00088     inline void add_to_position_queue(position_msg_t* msg)
00089     {
00090         position_queue.put(msg);
00091     }
00092     
00093     // adding to a queue to be sent on mqtt network
00094     inline void add_to_control_queue(int car_id,control_msg_t* msg)
00095     {
00096         control_queue[car_id].put(msg);
00097     }
00098     
00099     // adding to a queue to be sent on mqtt network
00100     inline control_msg_t* get_control_msg(int car_id)
00101     {
00102         osEvent evt = control_queue[car_id].get();
00103         assert(evt.status == osEventMessage);
00104         control_msg_t *message = (control_msg_t*)evt.value.p;
00105         return message;
00106     }
00107     
00108 };
00109 
00110 
00111 #endif