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.
LMqttsnClient.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 * Contributors: 00014 * Tomoaki Yamaguchi - initial API and implementation and/or initial documentation 00015 **************************************************************************************/ 00016 00017 #include <string.h> 00018 #include <stdio.h> 00019 00020 #include "LGwProxy.h" 00021 #include "LMqttsnClientApp.h" 00022 #include "LMqttsnClient.h" 00023 #include "LScreen.h" 00024 00025 using namespace std; 00026 using namespace linuxAsyncClient; 00027 00028 extern TaskList theTaskList[]; 00029 extern TestList theTestList[]; 00030 extern OnPublishList theOnPublishList[]; 00031 extern MQTTSNCONF; 00032 extern UDPCONF; 00033 extern void setup(void); 00034 00035 /*===================================== 00036 LMqttsnClient 00037 ======================================*/ 00038 LMqttsnClient* theClient = new LMqttsnClient(); 00039 LScreen* theScreen = new LScreen(); 00040 bool theOTAflag = false; 00041 bool theClientMode = true; 00042 00043 00044 /*------------------------------------- 00045 * main 00046 *------------------------------------*/ 00047 00048 int main(int argc, char** argv) 00049 { 00050 #ifndef CLIENT_MODE 00051 char c = 0; 00052 printf("\n%s", PAHO_COPYRIGHT4); 00053 printf("\n%s\n", PAHO_COPYRIGHT0); 00054 printf("%s\n", PAHO_COPYRIGHT1); 00055 printf("%s\n", PAHO_COPYRIGHT2); 00056 printf(" *\n%s\n", PAHO_COPYRIGHT3); 00057 printf("%s\n", TESTER_VERSION); 00058 printf("%s\n", PAHO_COPYRIGHT4); 00059 00060 theClientMode = false; 00061 PROMPT(" Do you like Tomoaki ? ( y/n ) : "); 00062 while (true) 00063 { 00064 if (CHECKKEYIN(&c)) 00065 { 00066 if ( toupper(c) == 'N' ) 00067 { 00068 DISPLAY("\033[0;31m\n**** Sorry ****\033[0;37m\n\n"); 00069 PROMPT(""); 00070 return 0; 00071 } 00072 } 00073 else if ( toupper(c) == 'Y' ) 00074 { 00075 DISPLAY("\033[0m\033[0;32mAttempting to Connect the Broker.....\033[0m\033[0;37m\n"); 00076 PROMPT(""); 00077 break; 00078 } 00079 } 00080 #endif 00081 00082 setup(); 00083 theClient->addTask(theClientMode); 00084 theClient->initialize( theNetcon, theMqcon); 00085 do 00086 { 00087 theClient->run(); 00088 } 00089 while (theClientMode); 00090 00091 delete theScreen; 00092 delete theClient; 00093 return 0; 00094 } 00095 00096 /*===================================== 00097 Class LMqttsnClient 00098 ======================================*/ 00099 LMqttsnClient::LMqttsnClient() 00100 { 00101 00102 } 00103 00104 LMqttsnClient::~LMqttsnClient() 00105 { 00106 00107 } 00108 00109 void LMqttsnClient::initialize(LUdpConfig netconf, LMqttsnConfig mqconf) 00110 { 00111 _gwProxy.initialize(netconf, mqconf); 00112 setSleepDuration(mqconf.sleepDuration); 00113 } 00114 00115 void LMqttsnClient::addTask(bool clientMode) 00116 { 00117 if ( clientMode ) 00118 { 00119 _taskMgr.add(theTaskList); 00120 } 00121 else 00122 { 00123 _taskMgr.add(theTestList); 00124 } 00125 } 00126 00127 00128 LGwProxy* LMqttsnClient::getGwProxy(void) 00129 { 00130 return &_gwProxy; 00131 } 00132 00133 LPublishManager* LMqttsnClient::getPublishManager(void) 00134 { 00135 return &_pubMgr; 00136 } 00137 ; 00138 00139 LSubscribeManager* LMqttsnClient::getSubscribeManager(void) 00140 { 00141 return &_subMgr; 00142 } 00143 ; 00144 00145 LRegisterManager* LMqttsnClient::getRegisterManager(void) 00146 { 00147 return _gwProxy.getRegisterManager(); 00148 } 00149 00150 LTaskManager* LMqttsnClient::getTaskManager(void) 00151 { 00152 return &_taskMgr; 00153 } 00154 ; 00155 00156 LTopicTable* LMqttsnClient::getTopicTable(void) 00157 { 00158 return _gwProxy.getTopicTable(); 00159 } 00160 00161 void LMqttsnClient::publish(const char* topicName, Payload* payload, uint8_t qos, bool retain) 00162 { 00163 _pubMgr.publish(topicName, payload, qos, retain); 00164 } 00165 00166 void LMqttsnClient::publish(const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain) 00167 { 00168 _pubMgr.publish(topicName, payload, len, qos, retain); 00169 } 00170 00171 void LMqttsnClient::publish(uint16_t topicId, Payload* payload, uint8_t qos, bool retain) 00172 { 00173 _pubMgr.publish(topicId, payload, qos, retain); 00174 } 00175 00176 void LMqttsnClient::publish(uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain) 00177 { 00178 _pubMgr.publish(topicId, payload, len, qos, retain); 00179 } 00180 00181 void LMqttsnClient::subscribe(const char* topicName, TopicCallback onPublish, uint8_t qos) 00182 { 00183 _subMgr.subscribe(topicName, onPublish, qos); 00184 } 00185 00186 void LMqttsnClient::subscribe(uint16_t topicId, TopicCallback onPublish, uint8_t qos) 00187 { 00188 _subMgr.subscribe(topicId, onPublish, qos); 00189 } 00190 00191 void LMqttsnClient::unsubscribe(const char* topicName) 00192 { 00193 _subMgr.unsubscribe(topicName); 00194 } 00195 00196 void LMqttsnClient::unsubscribe(const uint16_t topicId) 00197 { 00198 _subMgr.unsubscribe(topicId); 00199 } 00200 00201 void LMqttsnClient::disconnect(uint16_t sleepInSecs) 00202 { 00203 _gwProxy.disconnect(sleepInSecs); 00204 } 00205 00206 void LMqttsnClient::run() 00207 { 00208 _gwProxy.connect(); 00209 _taskMgr.run(); 00210 } 00211 00212 void LMqttsnClient::setSleepMode(uint32_t duration) 00213 { 00214 // ToDo: set WDT and sleep mode 00215 DISPLAY("\033[0m\033[0;32m\n\n Get into SLEEP mode %u [msec].\033[0m\033[0;37m\n\n", duration); 00216 } 00217 00218 void LMqttsnClient::sleep(void) 00219 { 00220 disconnect(_sleepDuration); 00221 } 00222 00223 void LMqttsnClient::setSleepDuration(uint32_t duration) 00224 { 00225 _sleepDuration = duration; 00226 } 00227 00228 void LMqttsnClient::onConnect(void) 00229 { 00230 _subMgr.onConnect(); 00231 } 00232 00233 const char* LMqttsnClient::getClientId(void) 00234 { 00235 return _gwProxy.getClientId(); 00236 } 00237 00238 uint16_t LMqttsnClient::getTopicId(const char* topicName) 00239 { 00240 return _gwProxy.getTopicTable()->getTopicId(topicName); 00241 } 00242
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2