Rover API Documentation
rover_pahomqtt.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 FH Dortmund and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Description:
9  * Rover PahoMQTT API - Interfaces for Rover PahoMQTT cloud application development - Header file
10  *
11  * Contributors:
12  * M.Ozcelikors <mozcelikors@gmail.com>, created PahoMQTT API 07.12.2017
13  * improved PahoMQTT API 10.12.2017
14  *
15  */
16 
17 #ifndef ROVERAPI_ROVER_PAHOMQTT_HPP_
18 #define ROVERAPI_ROVER_PAHOMQTT_HPP_
19 
20 #include <roverapi/rover_cloud.hpp>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 
25 #include "MQTTClient.h"
26 #include "MQTTAsync.h"
27 
28 using namespace std;
29 
30 namespace rover
31 {
32 
36  typedef struct
37  {
38  char * clientID;
39  char * topic;
40  char * payload;
41  char * username;
42  char * password;
43  int qos; // 1
44  long int timeout; // Such as 10000L usec
46 
50  typedef struct
51  {
53  char * data;
55 
59  typedef struct
60  {
67 
71  typedef struct
72  {
77 
83  class RoverPahoMQTT : public RoverCloud
84  {
85  protected:
89  char * HOST_NAME;
90 
94  int PORT;
95 
100 
105 
110 
115 
119  MQTTAsync client = NULL;
120 
124  MQTTClient client2;
125 
129  MQTTAsync_connectOptions conn_opts;
130 
134  MQTTAsync_disconnectOptions disc_opts;
135 
139  volatile MQTTAsync_token deliveredtoken;
140 
144  void createClient(void);
145 
149  void destroyClient(void);
150 
151 
152  public:
156  ~RoverPahoMQTT ();
157 
161  RoverPahoMQTT();
162 
163 
167  RoverPahoMQTT (char * host_name, int port, RoverMQTT_Configure_t MQTT_Configure);
168 
173  void setHostName (char * host_name);
174 
179  void setPort (const int port);
180 
186  void setPayload (const char * payload, int payloadLen);
187 
192  void setTopic (char * topic);
193 
199  int read (char * data);
200 
204  int isDataReady (void);
205 
217  int publish (void);
218 
230  int connect (void);
231 
243  int subscribe (void);
244 
256  int unsubscribe (void);
257 
262  int getConnected(void);
263 
264  protected:
269  void flushFlags (void);
270 
275  void constructAddress (void);
276 
277  /* Internal callbacks */
278  /* Related to publisher */
285  static void onConnect_Redirect (void* context, MQTTAsync_successData* response);
286 
293  static void onPublisherSend_Redirect (void* context, MQTTAsync_successData* response);
294 
300  void onConnect (MQTTAsync_successData* response);
301 
302 
308  void onPublisherSend (MQTTAsync_successData* response);
309 
310 
311  /* Related to subscriber */
320  static int onSubscriberMessageArrived_Redirect (void *context, char *topicName, int topicLen, MQTTAsync_message *message);
321 
328  static void onSubscribe_Redirect (void* context, MQTTAsync_successData* response);
329 
336  static void onSubscribeFailure_Redirect (void* context, MQTTAsync_failureData* response);
337 
338 
346  int onSubscriberMessageArrived (char *topicName, int topicLen, MQTTAsync_message *message);
347 
353  void onSubscribe (MQTTAsync_successData* response);
354 
360  void onSubscribeFailure (MQTTAsync_failureData* response);
361 
362  /* Related to both */
369  static void onConnectFailure_Redirect (void* context, MQTTAsync_failureData* response);
370 
377  static void onConnectionLost_Redirect (void* context, char *cause);
378 
384  void onConnectFailure (MQTTAsync_failureData* response);
385 
392  static void onDisconnect_Redirect (void* context, MQTTAsync_successData* response);
393 
399  void onConnectionLost (char *cause);
400 
406  void onDisconnect (MQTTAsync_successData* response);
407 
408  private:
412  char * payload = {};
413 
417  char my_address[100] = {};
418 
419  int connected = 0;
420 
421  };
422 }
423 
424 
425 
426 #endif /* ROVERAPI_ROVER_PAHOMQTT_HPP_ */
char * clientID
Definition: rover_pahomqtt.hpp:38
int f_mqtt_subscribed
Definition: rover_pahomqtt.hpp:63
char * payload
Definition: rover_pahomqtt.hpp:40
char * topic
Definition: rover_pahomqtt.hpp:39
char * HOST_NAME
Host name used for connecting to the Eclipse Paho MQTT server.
Definition: rover_pahomqtt.hpp:89
RoverMQTT_StatusFlags_t defaultRoverMQTTFlags
Member attribute for flags.
Definition: rover_pahomqtt.hpp:109
char * password
Definition: rover_pahomqtt.hpp:42
int qos
Definition: rover_pahomqtt.hpp:43
int f_mqtt_publish_successful
Definition: rover_pahomqtt.hpp:64
Data type to store internal flags for MQTT communication.
Definition: rover_pahomqtt.hpp:59
RoverPahoMQTT contains member functions to use rover as a client and to publish / subscribe to Eclips...
Definition: rover_pahomqtt.hpp:83
RoverMQTT_ReturnCodes_t defaultReturnCodes
Member attribute for return codes.
Definition: rover_pahomqtt.hpp:114
int data_ready
Definition: rover_pahomqtt.hpp:52
long int timeout
Definition: rover_pahomqtt.hpp:44
RoverMQTT_SubscribeData_t defaultRoverSubscribeData
Member attribute for subscribed data.
Definition: rover_pahomqtt.hpp:99
int rc_publish
Definition: rover_pahomqtt.hpp:73
rover Namespace contains classes to manage Rover sensors, gpio, buzzer, buttons, driving, utilities, and cloud.
Definition: rover_api.hpp:293
int f_mqtt_connected
Definition: rover_pahomqtt.hpp:65
MQTTAsync_connectOptions conn_opts
Connect options.
Definition: rover_pahomqtt.hpp:129
Data type to store initial configuration information for MQTT communication.
Definition: rover_pahomqtt.hpp:36
RoverCloud defines the interfaces that can be used for classes that connect to a remote cloud server...
Definition: rover_cloud.hpp:25
MQTTAsync_disconnectOptions disc_opts
Disconnect options.
Definition: rover_pahomqtt.hpp:134
int rc_unsubscribe
Definition: rover_pahomqtt.hpp:75
int rc_subscribe
Definition: rover_pahomqtt.hpp:74
char * data
Definition: rover_pahomqtt.hpp:53
int f_mqtt_finished
Definition: rover_pahomqtt.hpp:62
volatile MQTTAsync_token deliveredtoken
Delivered token.
Definition: rover_pahomqtt.hpp:139
Data type to store return codes that belong to member functions in RoverPahoMQTT class.
Definition: rover_pahomqtt.hpp:71
RoverMQTT_Configure_t defaultRoverMQTTConfigure
Member attribute for configuration data.
Definition: rover_pahomqtt.hpp:104
int f_mqtt_disconnect_finished
Definition: rover_pahomqtt.hpp:61
MQTTClient client2
Client object from MQTTClient.
Definition: rover_pahomqtt.hpp:124
char * username
Definition: rover_pahomqtt.hpp:41
Data type to store subscribed data.
Definition: rover_pahomqtt.hpp:50
int PORT
Port used for connecting to the Eclipse Paho MQTT server.
Definition: rover_pahomqtt.hpp:94