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 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include <memory>
16#include <functional>
17#include <cstdint>
18#include <vector>
19#include <string>
20#include <utility>
21
22namespace cc_mqttsn_gateway
23{
24
25class SessionImpl;
26
31{
32public:
33
35 using BinaryData = std::vector<std::uint8_t>;
36
40 using AuthInfo = std::pair<std::string, BinaryData>;
41
46 using NextTickProgramReqCb = std::function<void (unsigned value)>;
47
54 using CancelTickWaitReqCb = std::function<unsigned ()>;
55
61 using ClientSendDataReqCb = std::function<void (const std::uint8_t* buf, std::size_t bufSize, unsigned broadcastRadius)>;
62
67 using BrokerSendDataReqCb = std::function<void (const std::uint8_t* buf, std::size_t bufSize)>;
68
73 using TerminationReqCb = std::function<void ()>;
74
78 using BrokerReconnectReqCb = std::function<void ()>;
79
85 using ClientConnectedReportCb = std::function<void (const std::string& clientId)>;
86
91 using AuthInfoReqCb = std::function<AuthInfo (const std::string& clientId)>;
92
95 using ErrorReportCb = std::function<void (const char* msg)>;
96
106 using FwdEncSessionCreatedReportCb = std::function<bool (Session* session)>;
107
112 using FwdEncSessionDeletedReportCb = std::function<void (Session* session)>;
113
116
119
125
132
139
146
153
160
168
176
180
186
191
195 void setGatewayId(std::uint8_t value);
196
198 std::uint8_t getGatewayId() const;
199
207 void setRetryPeriod(unsigned value);
208
210 unsigned getRetryPeriod() const;
211
219 void setRetryCount(unsigned value);
220
222 unsigned getRetryCount() const;
223
232 void setSleepingClientMsgLimit(std::size_t value);
233
235 std::size_t getSleepingClientMsgLimit() const;
236
240 void setDefaultClientId(const std::string& value);
241
243 const std::string& getDefaultClientId() const;
244
255 void setPubOnlyKeepAlive(std::uint16_t value);
256
258 std::uint16_t getPubOnlyKeepAlive() const;
259
265 bool start();
266
268 void stop();
269
272 bool isRunning() const;
273
277 void tick();
278
288 std::size_t dataFromClient(const std::uint8_t* buf, std::size_t len);
289
299 std::size_t dataFromBroker(const std::uint8_t* buf, std::size_t len);
300
307 void setBrokerConnected(bool connected);
308
310 bool getBrokerConnected() const;
311
316 bool addPredefinedTopic(const std::string& topic, std::uint16_t topicId);
317
322 bool setTopicIdAllocationRange(std::uint16_t minVal, std::uint16_t maxVal);
323
324private:
325 std::unique_ptr<SessionImpl> m_pImpl;
326};
327
328} // namespace cc_mqttsn_gateway
329
Interface for Session entity.
Definition Session.h:31
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:78
std::pair< std::string, BinaryData > AuthInfo
Type of authentication information.
Definition Session.h:40
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:95
std::function< unsigned()> CancelTickWaitReqCb
Type of callback, used to cancel existing time measurement.
Definition Session.h:54
std::function< void(unsigned value)> NextTickProgramReqCb
Type of callback, used to request new time measurement.
Definition Session.h:46
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:73
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:106
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:35
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:61
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:112
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:67
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:91
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:85
Main namespace for all classes / functions of the gateway library.