Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSNGWClient.h Source File

MQTTSNGWClient.h

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  *    Tieto Poland Sp. z o.o. - Gateway improvements
00016  **************************************************************************************/
00017 
00018 #ifndef MQTTSNGWCLIENT_H_
00019 #define MQTTSNGWCLIENT_H_
00020 
00021 #include <Timer.h>    // Timer class
00022 #include "MQTTSNGWProcess.h"
00023 #include "MQTTGWPacket.h"
00024 #include "MQTTSNGWPacket.h"
00025 #include "MQTTSNPacket.h"
00026 #include "Network.h"
00027 #include "SensorNetwork.h"
00028 #include "MQTTSNPacket.h"
00029 #include "MQTTSNGWEncapsulatedPacket.h"
00030 #include "MQTTSNGWForwarder.h"
00031 #include "MQTTSNGWTopic.h"
00032 #include "MQTTSNGWClientList.h"
00033 #include "MQTTSNGWAdapter.h"
00034 
00035 namespace MQTTSNGW
00036 {
00037 
00038 #define MQTTSN_TOPIC_MULTI_WILDCARD   '#'
00039 #define MQTTSN_TOPIC_SINGLE_WILDCARD  '+'
00040 
00041 /*=====================================
00042  Class PacketQue
00043  =====================================*/
00044 template<class T> class PacketQue
00045 {
00046 public:
00047     PacketQue()
00048     {
00049         _que = new Que<T>;
00050     }
00051 
00052 
00053     ~PacketQue()
00054     {
00055         clear();
00056         delete _que;
00057     }
00058 
00059     T* getPacket()
00060     {
00061         T* packet;
00062         if (_que->size() > 0)
00063         {
00064             _mutex.lock();
00065             packet = _que->front();
00066             _mutex.unlock();
00067             return packet;
00068         }
00069         else
00070         {
00071             return 0;
00072         }
00073     }
00074 
00075     int
00076     post(T* packet)
00077     {
00078         int rc;
00079         _mutex.lock();
00080         rc = _que->post(packet);
00081         _mutex.unlock();
00082         return rc;
00083     }
00084 
00085     void pop()
00086     {
00087         if (_que->size() > 0)
00088         {
00089             _mutex.lock();
00090             _que->pop();
00091             _mutex.unlock();
00092         }
00093     }
00094 
00095     void clear()
00096     {
00097         _mutex.lock();
00098         while (_que->size() > 0)
00099         {
00100             delete _que->front();
00101             _que->pop();
00102         }
00103         _mutex.unlock();
00104     }
00105 
00106     void setMaxSize(int size)
00107     {
00108         _que->setMaxSize(size);
00109     }
00110 
00111 private:
00112     Que<T>* _que;
00113     Mutex _mutex;
00114 };
00115 
00116 
00117 
00118 /*=====================================
00119  Class WaitREGACKPacket
00120  =====================================*/
00121 class waitREGACKPacket
00122 {
00123     friend class WaitREGACKPacketList;
00124 public:
00125     waitREGACKPacket(MQTTSNPacket* packet, uint16_t REGACKMsgId);
00126     ~waitREGACKPacket();
00127 
00128 private:
00129     uint16_t _msgId;
00130     MQTTSNPacket* _packet;
00131     waitREGACKPacket* _next;
00132     waitREGACKPacket* _prev;
00133 };
00134 
00135 /*=====================================
00136  Class WaitREGACKPacketList
00137  =====================================*/
00138 class WaitREGACKPacketList
00139 {
00140 public:
00141     WaitREGACKPacketList();
00142     ~WaitREGACKPacketList();
00143     int setPacket(MQTTSNPacket* packet, uint16_t REGACKMsgId);
00144     MQTTSNPacket* getPacket(uint16_t REGACKMsgId);
00145     void erase(uint16_t REGACKMsgId);
00146     uint8_t getCount(void);
00147 
00148 private:
00149     uint8_t _cnt;
00150     waitREGACKPacket* _first;
00151     waitREGACKPacket* _end;
00152 };
00153 
00154 
00155 
00156 /*=====================================
00157  Class Client
00158  =====================================*/
00159 typedef enum
00160 {
00161     Cstat_Disconnected = 0, Cstat_TryConnecting, Cstat_Connecting, Cstat_Active, Cstat_Asleep, Cstat_Awake, Cstat_Lost
00162 } ClientStatus;
00163 
00164 typedef enum
00165 {
00166     Ctype_Regular = 0, Ctype_Forwarded, Ctype_QoS_1, Ctype_Aggregated, Ctype_Proxy, Ctype_Aggregater
00167 }ClientType;
00168 
00169 class Forwarder;
00170 
00171 class Client
00172 {
00173     friend class ClientList;
00174 public:
00175     Client(bool secure = false);
00176     Client(uint8_t maxInflightMessages, bool secure);
00177     ~Client();
00178 
00179     Connect* getConnectData(void);
00180     TopicIdMapElement* getWaitedPubTopicId(uint16_t msgId);
00181     TopicIdMapElement* getWaitedSubTopicId(uint16_t msgId);
00182     MQTTGWPacket* getClientSleepPacket(void);
00183     void deleteFirstClientSleepPacket(void);
00184 
00185     MQTTSNPacket* getProxyPacket(void);
00186     void deleteFirstProxyPacket(void);
00187     WaitREGACKPacketList* getWaitREGACKPacketList(void);
00188 
00189     void eraseWaitedPubTopicId(uint16_t msgId);
00190     void eraseWaitedSubTopicId(uint16_t msgId);
00191     void clearWaitedPubTopicId(void);
00192     void clearWaitedSubTopicId(void);
00193 
00194     int  setClientSleepPacket(MQTTGWPacket*);
00195     int setProxyPacket(MQTTSNPacket* packet);
00196     void setWaitedPubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
00197     void setWaitedSubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
00198 
00199     bool checkTimeover(void);
00200     void updateStatus(MQTTSNPacket*);
00201     void updateStatus(ClientStatus);
00202     void connectSended(void);
00203     void connackSended(int rc);
00204     void disconnected(void);
00205     bool isConnectSendable(void);
00206     void tryConnect(void);
00207     ClientStatus getClientStatus(void);
00208 
00209     uint16_t getNextPacketId(void);
00210     uint8_t getNextSnMsgId(void);
00211     Topics* getTopics(void);
00212     void setTopics(Topics* topics);
00213     void setKeepAlive(MQTTSNPacket* packet);
00214 
00215     SensorNetAddress* getSensorNetAddress(void);
00216     Network* getNetwork(void);
00217     void setClientAddress(SensorNetAddress* sensorNetAddr);
00218     void setSensorNetType(bool stable);
00219 
00220     Forwarder* getForwarder(void);
00221     void setForwarder(Forwarder* forwader);
00222 
00223     void setAdapterType(AdapterType type);
00224     void setQoSm1(void);
00225     void setAggregated(void);
00226     bool isQoSm1Proxy(void);
00227     bool isForwarded(void);
00228     bool isAggregated(void);
00229     bool isAggregater(void);
00230     bool isQoSm1(void);
00231     bool isAdapter(void);
00232 
00233     void setClientId(MQTTSNString id);
00234     void setWillTopic(MQTTSNString willTopic);
00235     void setWillMsg(MQTTSNString willmsg);
00236     char* getClientId(void);
00237     char* getWillTopic(void);
00238     char* getWillMsg(void);
00239     const char* getStatus(void);
00240     void setWaitWillMsgFlg(bool);
00241     void setSessionStatus(bool);  // true: clean session
00242     bool erasable(void);
00243 
00244     bool isDisconnect(void);
00245     bool isConnecting(void);
00246     bool isActive(void);
00247     bool isSleep(void);
00248     bool isAwake(void);
00249     bool isSecureNetwork(void);
00250     bool isSensorNetStable(void);
00251     bool isWaitWillMsg(void);
00252 
00253     void holdPingRequest(void);
00254     void resetPingRequest(void);
00255     bool isHoldPringReqest(void);
00256 
00257     Client* getNextClient(void);
00258 
00259 private:
00260     PacketQue<MQTTGWPacket> _clientSleepPacketQue;
00261     PacketQue<MQTTSNPacket> _proxyPacketQue;
00262 
00263     WaitREGACKPacketList    _waitREGACKList;
00264 
00265     Topics* _topics;
00266     TopicIdMap _waitedPubTopicIdMap;
00267     TopicIdMap _waitedSubTopicIdMap;
00268 
00269     Connect _connectData;
00270     MQTTSNPacket* _connAck;
00271 
00272     char* _clientId;
00273     char* _willTopic;
00274     char* _willMsg;
00275 
00276     bool _holdPingRequest;
00277 
00278     Timer _keepAliveTimer;
00279     uint32_t _keepAliveMsec;
00280 
00281     ClientStatus _status;
00282     bool _waitWillMsgFlg;
00283 
00284     uint16_t _packetId;
00285     uint8_t _snMsgId;
00286 
00287     Network* _network;      // Broker
00288     bool  _secureNetwork;    // SSL
00289     bool _sensorNetype;     // false: unstable network like a G3
00290     SensorNetAddress _sensorNetAddr;
00291 
00292     Forwarder* _forwarder;
00293     ClientType _clientType;
00294 
00295     bool _sessionStatus;
00296     bool _hasPredefTopic;
00297 
00298     Client* _nextClient;
00299     Client* _prevClient;
00300 };
00301 
00302 
00303 
00304 }
00305 #endif /* MQTTSNGWCLIENT_H_ */