Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mainPub.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 "ClientPUB", // ClientId 00056 {225,1,1,1}, // Multicast group IP 00057 1883, // Multicast group Port 00058 20010, // Local PortNo 00059 }; 00060 00061 /*------------------------------------------------------ 00062 * Client Configuration (theMqcon) 00063 *------------------------------------------------------*/ 00064 MQTTSNCONF = { 00065 300, //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* topic57 = "ty4tw/topic5/7"; 00081 00082 /*------------------------------------------------------ 00083 * Callback routines for Subscribed Topics 00084 *------------------------------------------------------*/ 00085 00086 /*------------------------------------------------------ 00087 * A Link list of Callback routines and Topics 00088 *------------------------------------------------------*/ 00089 00090 SUBSCRIBE_LIST = {// e.g. SUB(topic, callback, QoS), 00091 END_OF_SUBSCRIBE_LIST 00092 }; 00093 00094 00095 /*------------------------------------------------------ 00096 * Test functions 00097 *------------------------------------------------------*/ 00098 00099 void publishTopic1(void) 00100 { 00101 char payload[300]; 00102 sprintf(payload, "publish \"ty4tw/Topic1\" \n"); 00103 uint8_t qos = 0; 00104 PUBLISH(topic1,(uint8_t*)payload, strlen(payload), qos); 00105 } 00106 00107 void publishTopic2(void) 00108 { 00109 char payload[300]; 00110 sprintf(payload, "publish \"ty4tw/topic2\" \n"); 00111 uint8_t qos = 0; 00112 PUBLISH(topic2,(uint8_t*)payload, strlen(payload), qos); 00113 } 00114 00115 void publishTopic57(void) 00116 { 00117 char payload[300]; 00118 sprintf(payload, "publish \"ty4tw/topic57\" \n"); 00119 uint8_t qos = 0; 00120 PUBLISH(topic2,(uint8_t*)payload, strlen(payload), qos); 00121 } 00122 00123 00124 void disconnect(void) 00125 { 00126 DISCONNECT(0); 00127 } 00128 00129 00130 /*------------------------------------------------------ 00131 * A List of Test functions is valid in case of 00132 * line 23 of LMqttsnClientApp.h is commented out. 00133 * //#define CLIENT_MODE 00134 *------------------------------------------------------*/ 00135 00136 TEST_LIST = {// e.g. TEST( Label, Test), 00137 TEST("Step1:Publish topic1", publishTopic1), 00138 TEST("Step2:Publish topic57", publishTopic57), 00139 TEST("Step3:Publish topic2", publishTopic2), 00140 TEST("Step4:Disconnect", disconnect), 00141 END_OF_TEST_LIST 00142 }; 00143 00144 00145 /*------------------------------------------------------ 00146 * List of tasks is valid in case of line23 of 00147 * LMqttsnClientApp.h is uncommented. 00148 * #define CLIENT_MODE 00149 *------------------------------------------------------*/ 00150 TASK_LIST = {// e.g. TASK( task, executing duration in second), 00151 TASK(publishTopic1, 4), // publishTopic1() is executed every 4 seconds 00152 TASK(publishTopic2, 7), // publishTopic2() is executed every 7 seconds 00153 END_OF_TASK_LIST 00154 }; 00155 00156 00157 /*------------------------------------------------------ 00158 * Initialize function 00159 *------------------------------------------------------*/ 00160 void setup(void) 00161 { 00162 SetForwarderMode(false); 00163 } 00164 00165 00166 /***************** END OF PROGRAM ********************/
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2