MQTT-SN Gateway Library
Library that allows implementation of MQTT-SN gateway.
Loading...
Searching...
No Matches
Session.h
Go to the documentation of this file.
1//
2// Copyright 2016 - 2024 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
10
11#pragma once
12
13#include <memory>
14#include <functional>
15#include <cstdint>
16#include <vector>
17#include <string>
18#include <utility>
19
20namespace cc_mqttsn_gateway
21{
22
23class SessionImpl;
24
29{
30public:
31
33 using BinaryData = std::vector<std::uint8_t>;
34
38 using AuthInfo = std::pair<std::string, BinaryData>;
39
44 using NextTickProgramReqCb = std::function<void (unsigned value)>;
45
52 using CancelTickWaitReqCb = std::function<unsigned ()>;
53
59 using ClientSendDataReqCb = std::function<void (const std::uint8_t* buf, std::size_t bufSize, unsigned broadcastRadius)>;
60
65 using BrokerSendDataReqCb = std::function<void (const std::uint8_t* buf, std::size_t bufSize)>;
66
71 using TerminationReqCb = std::function<void ()>;
72
76 using BrokerReconnectReqCb = std::function<void ()>;
77
83 using ClientConnectedReportCb = std::function<void (const std::string& clientId)>;
84
89 using AuthInfoReqCb = std::function<AuthInfo (const std::string& clientId)>;
90
93 using ErrorReportCb = std::function<void (const char* msg)>;
94
104 using FwdEncSessionCreatedReportCb = std::function<bool (Session* session)>;
105
110 using FwdEncSessionDeletedReportCb = std::function<void (Session* session)>;
111
114
117
123
130
137
144
151
158
166
174
178
184
189
193 void setGatewayId(std::uint8_t value);
194
196 std::uint8_t getGatewayId() const;
197
205 void setRetryPeriod(unsigned value);
206
208 unsigned getRetryPeriod() const;
209
217 void setRetryCount(unsigned value);
218
220 unsigned getRetryCount() const;
221
230 void setSleepingClientMsgLimit(std::size_t value);
231
233 std::size_t getSleepingClientMsgLimit() const;
234
238 void setDefaultClientId(const std::string& value);
239
241 const std::string& getDefaultClientId() const;
242
253 void setPubOnlyKeepAlive(std::uint16_t value);
254
256 std::uint16_t getPubOnlyKeepAlive() const;
257
263 bool start();
264
266 void stop();
267
270 bool isRunning() const;
271
275 void tick();
276
286 std::size_t dataFromClient(const std::uint8_t* buf, std::size_t len);
287
297 std::size_t dataFromBroker(const std::uint8_t* buf, std::size_t len);
298
305 void setBrokerConnected(bool connected);
306
308 bool getBrokerConnected() const;
309
314 bool addPredefinedTopic(const std::string& topic, std::uint16_t topicId);
315
320 bool setTopicIdAllocationRange(std::uint16_t minVal, std::uint16_t maxVal);
321
322private:
323 std::unique_ptr<SessionImpl> m_pImpl;
324};
325
326} // namespace cc_mqttsn_gateway
327
Interface for Session entity.
Definition Session.h:29
std::size_t getSleepingClientMsgLimit() const
Get currenly configured limit to pending messages being accumulated for the sleeping client.
void setSleepingClientMsgLimit(std::size_t value)
Provide limit to number pending messages being accumulated for the sleeping client.
Session()
Default constructor.
bool start()
Start this object's operation.
std::function< void()> BrokerReconnectReqCb
Type of callback used to request reconnection to the broker.
Definition Session.h:76
std::pair< std::string, BinaryData > AuthInfo
Type of authentication information.
Definition Session.h:38
bool getBrokerConnected() const
Get currently recorded broker connection status.
void setSendDataBrokerReqCb(BrokerSendDataReqCb &&func)
Set the callback to be invoked when new data needs to be sent to the broker.
void setBrokerReconnectReqCb(BrokerReconnectReqCb &&func)
Set the callback to be invoked when the session needs to close existing TCP/IP connection to the brok...
void setDefaultClientId(const std::string &value)
Provide default client ID for clients that report empty one in their attempt to connect.
unsigned getRetryPeriod() const
Get the current configuration of the retry period.
const std::string & getDefaultClientId() const
Get current default client id configuration.
void tick()
Notify the Session object about requested time period expiry.
std::size_t dataFromClient(const std::uint8_t *buf, std::size_t len)
Provide data received from the client for processing.
void setRetryPeriod(unsigned value)
Set retry period to wait between resending unacknowledged message to the client and/or broker.
std::function< void(const char *msg)> ErrorReportCb
Type of the callback used to report detected errors.
Definition Session.h:93
std::function< unsigned()> CancelTickWaitReqCb
Type of callback, used to cancel existing time measurement.
Definition Session.h:52
std::function< void(unsigned value)> NextTickProgramReqCb
Type of callback, used to request new time measurement.
Definition Session.h:44
std::uint16_t getPubOnlyKeepAlive() const
Get current configuration of the default "keep alive" period for "publish only" clients.
bool addPredefinedTopic(const std::string &topic, std::uint16_t topicId)
Add predefined topic string and ID information.
void setCancelTickWaitReqCb(CancelTickWaitReqCb &&func)
Set the callback to be invoked when previously requested time measurement needs to be cancelled.
void setGatewayId(std::uint8_t value)
Set gateway numeric ID to be reported when requested.
std::function< void()> TerminationReqCb
Type of callback, used to request session termination.
Definition Session.h:71
void setErrorReportCb(ErrorReportCb &&func)
Set the callback to be used to report detected errors.
void setRetryCount(unsigned value)
Set number of retry attempts to perform before abandoning attempt to send unacknowledged message.
std::size_t dataFromBroker(const std::uint8_t *buf, std::size_t len)
Provide data received from the broker for processing.
void setTerminationReqCb(TerminationReqCb &&func)
Set the callback to be invoked when the session needs to be terminated and this Session object delete...
void setFwdEncSessionDeletedReportCb(FwdEncSessionDeletedReportCb &&func)
Set the callback to be invoked when the forwarding encapsulation session is about to be deleted.
std::function< bool(Session *session)> FwdEncSessionCreatedReportCb
Type of callback used to notify the application about forwarding encapsulation session being created.
Definition Session.h:104
std::uint8_t getGatewayId() const
Get configured gateway numeric ID to be reported when requested.
std::vector< std::uint8_t > BinaryData
Type for buffer of binary data.
Definition Session.h:33
std::function< void(const std::uint8_t *buf, std::size_t bufSize, unsigned broadcastRadius)> ClientSendDataReqCb
Type of callback, used to request delivery of serialised message to the client.
Definition Session.h:59
void setClientConnectedReportCb(ClientConnectedReportCb &&func)
Set the callback to be invoked when MQTT-SN client is successfully connected to the broker.
void setBrokerConnected(bool connected)
Notify the Session object about broker being connected / disconnected.
void setAuthInfoReqCb(AuthInfoReqCb &&func)
Set the callback to be used to request authentication information for specific client.
void setSendDataClientReqCb(ClientSendDataReqCb &&func)
Set the callback to be invoked when new data needs to be sent to the client.
bool isRunning() const
Check whether the object's operation has been successfull started.
void setFwdEncSessionCreatedReportCb(FwdEncSessionCreatedReportCb &&func)
Set the callback to be invoked when the forwarding encapsulation session is detected and to notify ap...
void setNextTickProgramReqCb(NextTickProgramReqCb &&func)
Set the callback to be invoked when new time measurement is required.
std::function< void(Session *session)> FwdEncSessionDeletedReportCb
Type of callback used to notify the application about forwarding encapsulation session being deleted.
Definition Session.h:110
void setPubOnlyKeepAlive(std::uint16_t value)
Provide default "keep alive" period for "publish only" clients, that do not make an attempt to connec...
std::function< void(const std::uint8_t *buf, std::size_t bufSize)> BrokerSendDataReqCb
Type of callback, used to request delivery of serialised message to the broker.
Definition Session.h:65
bool setTopicIdAllocationRange(std::uint16_t minVal, std::uint16_t maxVal)
Limit range of topic IDs allocated for newly registered topics.
void stop()
Stop the operation of the object.
unsigned getRetryCount() const
Get the current configuration of the retry count.
std::function< AuthInfo(const std::string &clientId)> AuthInfoReqCb
Type of callback used to request authentication information of the client that is trying connect.
Definition Session.h:89
std::function< void(const std::string &clientId)> ClientConnectedReportCb
Type of callback used to report client ID of the newly connected MQTT-SN client.
Definition Session.h:83
Main namespace for all classes / functions of the gateway library.