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.
MQTTSNGWAdapter.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 #include "Timer.h" 00017 #include "MQTTSNGWDefines.h" 00018 #include "MQTTSNGateway.h" 00019 #include "MQTTSNGWAdapter.h" 00020 #include "SensorNetwork.h" 00021 #include "MQTTSNGWProcess.h" 00022 #include "MQTTSNGWClient.h" 00023 00024 #include <string.h> 00025 using namespace MQTTSNGW; 00026 00027 00028 /*===================================== 00029 Class Adapter 00030 =====================================*/ 00031 Adapter:: Adapter(Gateway* gw) 00032 { 00033 _gateway = gw; 00034 _proxy = new Proxy(gw); 00035 _proxySecure = new Proxy(gw); 00036 } 00037 00038 Adapter::~Adapter(void) 00039 { 00040 if ( _proxy ) 00041 { 00042 delete _proxy; 00043 } 00044 00045 if ( _proxySecure ) 00046 { 00047 delete _proxySecure; 00048 } 00049 } 00050 00051 00052 void Adapter::setup(const char* adpterName, AdapterType adapterType) 00053 { 00054 _isSecure = false; 00055 if ( _gateway->hasSecureConnection() ) 00056 { 00057 _isSecure = true; 00058 } 00059 00060 MQTTSNString id = MQTTSNString_initializer; 00061 MQTTSNString idSecure = MQTTSNString_initializer; 00062 00063 string name = string(adpterName); 00064 id.cstring = const_cast<char*>(name.c_str()); 00065 string nameSecure = string(adpterName) + "-S"; 00066 idSecure.cstring = const_cast<char*>(nameSecure.c_str()); 00067 00068 Client* client = _gateway->getClientList()->createClient(0, &id, true, false, TRANSPEARENT_TYPE); 00069 setClient(client, false); 00070 client->setAdapterType(adapterType); 00071 00072 client = _gateway->getClientList()->createClient(0, &idSecure, true, true, TRANSPEARENT_TYPE); 00073 setClient(client, true); 00074 client->setAdapterType(adapterType); 00075 } 00076 00077 00078 Client* Adapter::getClient(SensorNetAddress* addr) 00079 { 00080 Client* client = _gateway->getClientList()->getClient(addr); 00081 if ( !client ) 00082 { 00083 return nullptr; 00084 } 00085 else if ( client->isQoSm1() ) 00086 { 00087 return client; 00088 } 00089 else 00090 { 00091 return nullptr; 00092 } 00093 } 00094 00095 const char* Adapter::getClientId(SensorNetAddress* addr) 00096 { 00097 Client* client = getClient(addr); 00098 if ( !client ) 00099 { 00100 return nullptr; 00101 } 00102 else if ( client->isQoSm1() ) 00103 { 00104 return client->getClientId(); 00105 } 00106 else 00107 { 00108 return nullptr; 00109 } 00110 } 00111 00112 bool Adapter::isSecure(SensorNetAddress* addr) 00113 { 00114 Client* client = getClient(addr); 00115 if ( !client ) 00116 { 00117 return false; 00118 } 00119 else if ( client->isSecureNetwork() ) 00120 { 00121 return true; 00122 } 00123 else 00124 { 00125 return false; 00126 } 00127 } 00128 00129 void Adapter::setClient(Client* client, bool secure) 00130 { 00131 if ( secure ) 00132 { 00133 _clientSecure = client; 00134 } 00135 else 00136 { 00137 _client = client; 00138 } 00139 } 00140 00141 Client* Adapter::getClient(void) 00142 { 00143 return _client; 00144 } 00145 00146 Client* Adapter::getSecureClient(void) 00147 { 00148 return _clientSecure; 00149 } 00150 00151 void Adapter::checkConnection(void) 00152 { 00153 _proxy->checkConnection(_client); 00154 00155 if ( _isSecure ) 00156 { 00157 _proxySecure->checkConnection(_clientSecure); 00158 } 00159 } 00160 00161 void Adapter::send(MQTTSNPacket* packet, Client* client) 00162 { 00163 Proxy* proxy = _proxy; 00164 if ( client->isSecureNetwork() && !_isSecure ) 00165 { 00166 if ( _isSecure ) 00167 { 00168 proxy = _proxySecure; 00169 } 00170 else 00171 { 00172 WRITELOG("%s %s No Secure connections %s 's packet is discarded.%s\n", ERRMSG_HEADER, client->getClientId() , ERRMSG_FOOTER); 00173 return; 00174 } 00175 } 00176 00177 proxy->recv(packet, client); 00178 00179 } 00180 00181 void Adapter::resetPingTimer(bool secure) 00182 { 00183 if ( secure ) 00184 { 00185 _proxySecure->resetPingTimer(); 00186 } 00187 else 00188 { 00189 _proxy->resetPingTimer(); 00190 } 00191 } 00192 00193 bool Adapter::isActive(void) 00194 { 00195 return _isActive; 00196 } 00197 00198 void Adapter::savePacket(Client* client, MQTTSNPacket* packet) 00199 { 00200 if ( client->isSecureNetwork()) 00201 { 00202 _proxySecure->savePacket(client, packet); 00203 } 00204 else 00205 { 00206 _proxy->savePacket(client, packet); 00207 } 00208 } 00209 00210 00211 Client* Adapter::getAdapterClient(Client* client) 00212 { 00213 if ( client->isSecureNetwork() ) 00214 { 00215 return _client; 00216 } 00217 else 00218 { 00219 return _client; 00220 } 00221 } 00222 00223 /*===================================== 00224 Class Proxy 00225 =====================================*/ 00226 Proxy::Proxy(Gateway* gw) 00227 { 00228 _gateway = gw; 00229 _suspendedPacketEventQue = new EventQue(); 00230 } 00231 Proxy::~Proxy(void) 00232 { 00233 if ( _suspendedPacketEventQue ) 00234 { 00235 delete _suspendedPacketEventQue; 00236 } 00237 } 00238 00239 void Proxy::checkConnection(Client* client) 00240 { 00241 if ( client->isDisconnect() || ( client->isConnecting() && _responseTimer.isTimeup()) ) 00242 { 00243 client->connectSended(); 00244 _responseTimer.start(QOSM1_PROXY_RESPONSE_DURATION * 1000UL); 00245 MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer; 00246 options.clientID.cstring = client->getClientId(); 00247 options.duration = QOSM1_PROXY_KEEPALIVE_DURATION; 00248 00249 MQTTSNPacket* packet = new MQTTSNPacket(); 00250 packet->setCONNECT(&options); 00251 Event* ev = new Event(); 00252 ev->setClientRecvEvent(client, packet); 00253 _gateway->getPacketEventQue()->post(ev); 00254 } 00255 else if ( (client->isActive() && _keepAliveTimer.isTimeup() ) || (_isWaitingResp && _responseTimer.isTimeup() ) ) 00256 { 00257 MQTTSNPacket* packet = new MQTTSNPacket(); 00258 MQTTSNString clientId = MQTTSNString_initializer; 00259 packet->setPINGREQ(&clientId); 00260 Event* ev = new Event(); 00261 ev->setClientRecvEvent(client, packet); 00262 _gateway->getPacketEventQue()->post(ev); 00263 _responseTimer.start(QOSM1_PROXY_RESPONSE_DURATION * 1000UL); 00264 _isWaitingResp = true; 00265 00266 if ( ++_retryCnt > QOSM1_PROXY_MAX_RETRY_CNT ) 00267 { 00268 client->disconnected(); 00269 } 00270 resetPingTimer(); 00271 } 00272 } 00273 00274 00275 void Proxy::resetPingTimer(void) 00276 { 00277 _keepAliveTimer.start(QOSM1_PROXY_KEEPALIVE_DURATION * 1000UL); 00278 } 00279 00280 void Proxy::recv(MQTTSNPacket* packet, Client* client) 00281 { 00282 if ( packet->getType() == MQTTSN_CONNACK ) 00283 { 00284 if ( packet->isAccepted() ) 00285 { 00286 _responseTimer.stop(); 00287 _retryCnt = 0; 00288 resetPingTimer(); 00289 sendSuspendedPacket(); 00290 } 00291 } 00292 else if ( packet->getType() == MQTTSN_PINGRESP ) 00293 { 00294 _isWaitingResp = false; 00295 _responseTimer.stop(); 00296 _retryCnt = 0; 00297 resetPingTimer(); 00298 } 00299 else if ( packet->getType() == MQTTSN_DISCONNECT ) 00300 { 00301 // blank 00302 } 00303 } 00304 00305 void Proxy::savePacket(Client* client, MQTTSNPacket* packet) 00306 { 00307 MQTTSNPacket* pk = new MQTTSNPacket(*packet); 00308 Event* ev = new Event(); 00309 ev->setClientRecvEvent(client, pk); 00310 _suspendedPacketEventQue->post(ev); 00311 } 00312 00313 void Proxy::sendSuspendedPacket(void) 00314 { 00315 while ( _suspendedPacketEventQue->size() ) 00316 { 00317 Event* ev = _suspendedPacketEventQue->wait(); 00318 _gateway->getPacketEventQue()->post(ev); 00319 } 00320 } 00321
Generated on Wed Jul 13 2022 10:46:02 by
1.7.2