Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mainTest.cpp Source File

mainTest.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     "GatewayTestClient", // ClientId
00056     {225,1,1,1},         // Multicast group IP
00057     1883,                // Multicast group Port
00058     20020,               // 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 = "ty4tw/topic4";
00081 const char* topic51 = "ty4tw/topic5/1";
00082 const char* topic52 = "ty4tw/topic5/2";
00083 const char* topic53 = "ty4tw/topic5/3";
00084 const char* topic50 = "ty4tw/topic5/+";
00085 
00086 
00087 /*------------------------------------------------------
00088  *       Callback routines for Subscribed Topics
00089  *------------------------------------------------------*/
00090 int on_Topic01(uint8_t* pload, uint16_t ploadlen)
00091 {
00092     DISPLAY("\n\nTopic1 recv.\n");
00093     char c = pload[ploadlen-1];
00094     pload[ploadlen-1]= 0;   // set null terminator
00095     DISPLAY("Payload -->%s%c<--\n\n",pload, c);
00096     return 0;
00097 }
00098 
00099 int on_Topic02(uint8_t* pload, uint16_t ploadlen)
00100 {
00101     DISPLAY("\n\nTopic2 recv.\n");
00102     pload[ploadlen-1]= 0;   // set null terminator
00103     DISPLAY("Payload -->%s <--\n\n",pload);
00104     return 0;
00105 }
00106 
00107 int on_Topic03(uint8_t* pload, uint16_t ploadlen)
00108 {
00109     DISPLAY("\n\nNew callback recv Topic3\n");
00110     pload[ploadlen-1]= 0;   // set null terminator
00111     DISPLAY("Payload -->%s <--\n\n",pload);
00112     return 0;
00113 }
00114 
00115 /*------------------------------------------------------
00116  *      A Link list of Callback routines and Topics
00117  *------------------------------------------------------*/
00118 
00119 SUBSCRIBE_LIST = {// e.g. SUB(TopicType, topicName, TopicId, callback, QoSx),
00120                   SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic1, 0, on_Topic01, QoS1),
00121                   SUB(MQTTSN_TOPIC_TYPE_NORMAL, topic2, 0, on_Topic02, QoS1),
00122                   END_OF_SUBSCRIBE_LIST
00123                  };
00124 
00125 
00126 /*------------------------------------------------------
00127  *    Test functions
00128  *------------------------------------------------------*/
00129 void subscribePredefTopic1(void)
00130 {
00131     SUBSCRIBE(1, on_Topic03, QoS1);
00132 }
00133 
00134 void publishTopic1(void)
00135 {
00136     char payload[300];
00137     sprintf(payload, "publish \"ty4tw/Topic1\" \n");
00138     PUBLISH(topic1,(uint8_t*)payload, strlen(payload), QoS0);
00139 }
00140 
00141 void subscribeTopic2(void)
00142 {
00143     SUBSCRIBE(10, on_Topic02, QoS1);
00144 }
00145 
00146 void publishTopic2(void)
00147 {
00148     char payload[300];
00149     sprintf(payload, "publish \"ty4tw/topic2\" \n");
00150     PUBLISH(topic2,(uint8_t*)payload, strlen(payload), QoS1);
00151 }
00152 
00153 
00154 
00155 void unsubscribe(void)
00156 {
00157     UNSUBSCRIBE(topic2);
00158 }
00159 
00160 void subscribechangeCallback(void)
00161 {
00162     SUBSCRIBE(topic2, on_Topic02, QoS1);
00163 }
00164 
00165 void test3(void)
00166 {
00167     char payload[300];
00168     sprintf(payload, "TEST3 ");
00169     uint8_t qos = 0;
00170     PUBLISH(topic2,(uint8_t*)payload, strlen(payload), qos);
00171 }
00172 
00173 void disconnect(void)
00174 {
00175     DISCONNECT(0);
00176 }
00177 
00178 void asleep(void)
00179 {
00180     DISCONNECT(theMqcon.sleepDuration);
00181 }
00182 
00183 /*------------------------------------------------------
00184  *    A List of Test functions is valid in case of
00185  *    line 23 of LMqttsnClientApp.h is commented out.
00186  *    //#define CLIENT_MODE
00187  *------------------------------------------------------*/
00188 
00189 TEST_LIST = {// e.g. TEST( Label, Test),
00190             TEST("Step0:Subscribe predef topic1",     subscribePredefTopic1),
00191              TEST("Step1:Publish topic1",     publishTopic1),
00192              TEST("Step2:Publish topic2",     publishTopic2),
00193              TEST("Step3:Subscribe topic2",   subscribeTopic2),
00194              TEST("Step4:Publish topic2",     publishTopic2),
00195              TEST("Step5:Unsubscribe topic2", unsubscribe),
00196              TEST("Step6:Publish topic2",     publishTopic2),
00197              TEST("Step7:subscribe again",    subscribechangeCallback),
00198              TEST("Step8:Publish topic2",     publishTopic2),
00199              TEST("Step9:Sleep     ",         asleep),
00200              TEST("Step10:Publish topic1",    publishTopic1),
00201              TEST("Step11:Disconnect",        disconnect),
00202              END_OF_TEST_LIST
00203             };
00204 
00205 
00206 /*------------------------------------------------------
00207  *    List of tasks is valid in case of line23 of
00208  *    LMqttsnClientApp.h is uncommented.
00209  *    #define CLIENT_MODE
00210  *------------------------------------------------------*/
00211 TASK_LIST = {// e.g. TASK( task, executing duration in second),
00212             TASK(publishTopic1, 4),  // publishTopic1() is executed every 4 seconds
00213             TASK(publishTopic2, 7),  // publishTopic2() is executed every 7 seconds
00214              END_OF_TASK_LIST
00215             };
00216 
00217 
00218 /*------------------------------------------------------
00219  *    Initialize function
00220  *------------------------------------------------------*/
00221 void setup(void)
00222 {
00223     SetForwarderMode(false);
00224 }
00225 
00226 
00227 /*****************  END OF  PROGRAM ********************/