Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mainSub.cpp Source File

mainSub.cpp

00001 /****************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  *---------------------------------------------------------------------------
00014  *
00015  *   MQTT-SN GATEWAY TEST CLIENT
00016  *
00017  *   Supported functions.
00018  *
00019  *   void PUBLISH  ( const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
00020  *
00021  *   void PUBLISH  ( uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
00022  *
00023  *  void SUBSCRIBE ( const char* topicName, TopicCallback onPublish, uint8_t qos );
00024  *
00025  *  void SUBSCRIBE ( uint16_t topicId, TopicCallback onPublish, uint8_t qos );
00026  *
00027  *  void UNSUBSCRIBE ( const char* topicName );
00028  *
00029  *  void UNSUBSCRIBE ( uint16_t topicId );
00030  *
00031  *   void DISCONNECT ( uint16_t sleepInSecs );
00032  *
00033  *   void CONNECT ( void );
00034  *
00035  *   void DISPLAY( format, .....);    <== instead of printf()
00036  *
00037  *
00038  * Contributors:
00039  *    Tomoaki Yamaguchi - initial API and implementation
00040  ***************************************************************************/
00041 
00042 #include "LMqttsnClientApp.h"
00043 #include "LMqttsnClient.h"
00044 #include "LScreen.h"
00045 
00046 using namespace std;
00047 using namespace linuxAsyncClient;
00048 extern LMqttsnClient* theClient;
00049 extern LScreen* theScreen;
00050 
00051 /*------------------------------------------------------
00052  *    UDP Configuration    (theNetcon)
00053  *------------------------------------------------------*/
00054 UDPCONF  = {
00055     "ClientSUB", // ClientId
00056     {225,1,1,1},         // Multicast group IP
00057     1883,                // Multicast group Port
00058     20011,               // Local PortNo
00059 };
00060 
00061 /*------------------------------------------------------
00062  *    Client Configuration  (theMqcon)
00063  *------------------------------------------------------*/
00064 MQTTSNCONF = {
00065      60,            //KeepAlive [seconds]
00066     true,          //Clean session
00067     300,           //Sleep duration [seconds]
00068     "",            //WillTopic
00069     "",            //WillMessage
00070     0,             //WillQos
00071     false          //WillRetain
00072 };
00073 
00074 /*------------------------------------------------------
00075  *     Define Topics
00076  *------------------------------------------------------*/
00077 const char* topic1 = "ty4tw/topic1";
00078 const char* topic2 = "ty4tw/topic2";
00079 const char* topic3 = "ty4tw/topic3";
00080 const char* topic4 = "a";
00081 const char* topic5 = "ty4tw/#";
00082 
00083 
00084 /*------------------------------------------------------
00085  *       Callback routines for Subscribed Topics
00086  *------------------------------------------------------*/
00087 int on_Topic01(uint8_t* pload, uint16_t ploadlen)
00088 {
00089     DISPLAY("\n\nTopic1 recv.\n");
00090     char c = pload[ploadlen-1];
00091     pload[ploadlen-1]= 0;   // set null terminator
00092     DISPLAY("Payload -->%s%c<--\n\n",pload, c);
00093     return 0;
00094 }
00095 
00096 int on_Topic02(uint8_t* pload, uint16_t ploadlen)
00097 {
00098     DISPLAY("\n\nTopic2 recv.\n");
00099     pload[ploadlen-1]= 0;   // set null terminator
00100     DISPLAY("Payload -->%s <--\n\n",pload);
00101     return 0;
00102 }
00103 
00104 int on_Topic03(uint8_t* pload, uint16_t ploadlen)
00105 {
00106     DISPLAY("\n\nNew callback recv TopicA\n");
00107     pload[ploadlen-1]= 0;   // set null terminator
00108     DISPLAY("Payload -->%s <--\n\n",pload);
00109     return 0;
00110 }
00111 
00112 int on_Topic05(uint8_t* pload, uint16_t ploadlen)
00113 {
00114     DISPLAY("\n\nNew callback recv TopicA\n");
00115     pload[ploadlen-1]= 0;   // set null terminator
00116     DISPLAY("Payload -->%s <--\n\n",pload);
00117     return 0;
00118 }
00119 
00120 /*------------------------------------------------------
00121  *      A Link list of Callback routines and Topics
00122  *------------------------------------------------------*/
00123 
00124 SUBSCRIBE_LIST = {// e.g. SUB(TopicType, topicName, TopicId, callback, QoSx),
00125 
00126                   // SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic5, 0, on_Topic05, QoS1),
00127                   //SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic2, 0, on_Topic02, QoS1),
00128                   END_OF_SUBSCRIBE_LIST
00129                  };
00130 
00131 
00132 
00133 /*------------------------------------------------------
00134  *    Test functions
00135  *------------------------------------------------------*/
00136 
00137 
00138 void subscribeTopic1(void)
00139 {
00140     uint8_t qos = 1;
00141     SUBSCRIBE(topic1, on_Topic01, qos);
00142 }
00143 
00144 void subscribeTopic2(void)
00145 {
00146     uint8_t qos = 1;
00147     SUBSCRIBE(topic2, on_Topic02, qos);
00148 }
00149 
00150 void subscribeTopic5(void)
00151 {
00152     uint8_t qos = 1;
00153     SUBSCRIBE(topic5, on_Topic05, qos);
00154 }
00155 
00156 void disconnect(void)
00157 {
00158     DISCONNECT(0);
00159 }
00160 
00161 void connect(void)
00162 {
00163     CONNECT();
00164 }
00165 
00166 void asleep(void)
00167 {
00168     DISCONNECT(theMqcon.sleepDuration);
00169 }
00170 
00171 /*------------------------------------------------------
00172  *    A List of Test functions is valid in case of
00173  *    line 23 of LMqttsnClientApp.h is commented out.
00174  *    //#define CLIENT_MODE
00175  *------------------------------------------------------*/
00176 
00177 TEST_LIST = {// e.g. TEST( Label, Test),
00178              TEST("Step1:Subscribe topic5",     subscribeTopic5),
00179              //TEST("Step2:Subscribe topic2",     subscribeTopic2),
00180              TEST("Step2:Disconnect",     asleep),
00181              TEST("Step3:Cconnect",     connect),
00182              TEST("Step4:Disconnect",     asleep),
00183              END_OF_TEST_LIST
00184             };
00185 
00186 
00187 /*------------------------------------------------------
00188  *    List of tasks is valid in case of line23 of
00189  *    LMqttsnClientApp.h is uncommented.
00190  *    #define CLIENT_MODE
00191  *------------------------------------------------------*/
00192 TASK_LIST = {// e.g. TASK( task, executing duration in second),
00193              END_OF_TASK_LIST
00194             };
00195 
00196 
00197 /*------------------------------------------------------
00198  *    Initialize function
00199  *------------------------------------------------------*/
00200 void setup(void)
00201 {
00202     SetForwarderMode(false);
00203 }
00204 
00205 
00206 /*****************  END OF  PROGRAM ********************/
00207