Han Yan / Mbed OS ms2b

Dependencies:   TextLCD MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Communication.h Source File

Communication.h

00001 #ifndef _COMMUNICATION_H_
00002 #define _COMMUNICATION_H_
00003 
00004 #include "mbed.h"
00005 #include "rtos.h"
00006 #include "MQTTNetwork.h"
00007 #include "MQTTClient.h"
00008 #include "MQTTmbed.h"
00009 #include <string.h>
00010 #include <queue>
00011 #include <mutex>
00012 
00013 
00014 typedef struct {
00015     char id;
00016     char speed; // speed for AccCar, -1 for Road
00017     char position; // position for AccCar, update number for Road
00018 } message_t;
00019 
00020 class Communication {
00021 public:
00022     static EventFlags control_flags; // For cars waiting on speed control
00023     static EventFlags sync_flags;
00024     static int speeds[5];
00025     static int sync;
00026     static int flags[5];
00027     
00028     // Singleton
00029     static Communication *getInstance(char* t1, char* t2, char* t3, char* t4);
00030     
00031     // MQTT Setup
00032     void setup_wifi();
00033     void setup_mqtt(MQTTNetwork* network);
00034     static void message_arrived(MQTT::MessageData& md);
00035     static void control_arrived(MQTT::MessageData& md);
00036     static void sync_arrived(MQTT::MessageData& md);
00037     int send_message(char* topic);
00038     int subscribe_to_topic_control(char* topic);
00039     int subscribe_to_topic_sync(char* topic);
00040     void publish_car(int id, int speed, int position);
00041     void publish_road(int num);
00042     void start(); // function for serving requests (single thread)
00043     void reset(); // function to create and start the singleton thread
00044     void stop();
00045     
00046 //private:
00047     static Communication *singleton;
00048     
00049     WiFiInterface *wifi;
00050     MQTTNetwork* network;
00051     MQTT::Client<MQTTNetwork, Countdown> *client;
00052     static Thread* thread;
00053     
00054     Mutex* mutex;
00055     std::queue<message_t>* q;
00056     
00057     char* topic_pub_position;
00058     char* topic_sub_control;
00059     char* topic_pub_receive;
00060     char* topic_sub_send;
00061     
00062     Communication(char* t1, char* t2, char* t3, char* t4);
00063     
00064 };
00065 
00066 #endif