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.
mainPubQoS-1.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 "QoS-1_Client01", // ClientId 00056 {225,1,1,1}, // Multicast group IP 00057 1883, // Multicast group Port 00058 20001, // 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 00078 00079 /*------------------------------------------------------ 00080 * Callback routines for Subscribed Topics 00081 *------------------------------------------------------*/ 00082 00083 /*------------------------------------------------------ 00084 * A Link list of Callback routines and Topics 00085 *------------------------------------------------------*/ 00086 SUBSCRIBE_LIST = {// e.g. SUB(TopicType, topicName, TopicId, callback, QoSx), 00087 00088 END_OF_SUBSCRIBE_LIST 00089 }; 00090 00091 /*------------------------------------------------------ 00092 * Test functions 00093 *------------------------------------------------------*/ 00094 00095 void publishTopic1(void) 00096 { 00097 char payload[300]; 00098 sprintf(payload, "publish \"ty4tw/Topic1\" \n"); 00099 uint8_t qos = 3; 00100 PUBLISH(1,(uint8_t*)payload, strlen(payload), qos); 00101 } 00102 00103 void publishTopic2(void) 00104 { 00105 char payload[300]; 00106 sprintf(payload, "publish \"ty4tw/topic2\" \n"); 00107 uint8_t qos = 3; 00108 PUBLISH(2,(uint8_t*)payload, strlen(payload), qos); 00109 } 00110 00111 void publishTopic57(void) 00112 { 00113 char payload[300]; 00114 sprintf(payload, "publish \"ty4tw/topic57\" \n"); 00115 uint8_t qos = 3; 00116 PUBLISH(5,(uint8_t*)payload, strlen(payload), qos); 00117 } 00118 00119 00120 void disconnect(void) 00121 { 00122 DISCONNECT(0); 00123 } 00124 00125 00126 /*------------------------------------------------------ 00127 * A List of Test functions is valid in case of 00128 * line 23 of LMqttsnClientApp.h is commented out. 00129 * //#define CLIENT_MODE 00130 *------------------------------------------------------*/ 00131 00132 TEST_LIST = {// e.g. TEST( Label, Test), 00133 TEST("Step1:Publish topic1", publishTopic1), 00134 TEST("Step2:Publish topic57", publishTopic57), 00135 TEST("Step3:Publish topic2", publishTopic2), 00136 END_OF_TEST_LIST 00137 }; 00138 00139 00140 /*------------------------------------------------------ 00141 * List of tasks is invalid in case of line23 of 00142 * LMqttsnClientApp.h is commented out. 00143 * #define CLIENT_MODE 00144 *------------------------------------------------------*/ 00145 TASK_LIST = {// e.g. TASK( task, executing duration in second), 00146 TASK(publishTopic1, 4), // publishTopic1() is executed every 4 seconds 00147 TASK(publishTopic2, 7), // publishTopic2() is executed every 7 seconds 00148 END_OF_TASK_LIST 00149 }; 00150 00151 00152 /*------------------------------------------------------ 00153 * Initialize function 00154 *------------------------------------------------------*/ 00155 void setup(void) 00156 { 00157 SetQoSMinus1Mode(true); 00158 } 00159 00160 00161 /***************** END OF PROGRAM ********************/
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2