MQTT-SN Gateway Library
Library that allows implementation of MQTT-SN gateway.
Loading...
Searching...
No Matches
Gateway

The Gateway object is responsible to advertise the gateway presence to all the possible clients on MQTT-SN network. If such advertisement is not needed, there is no need to create or use the Gateway object.

Allocation

When using C++ interface, just instantiate object of cc_mqttsn_gateway::Gateway class. The destruction of the object will clean up all acquired resources.

Interface for Gateway entity.
Definition Gateway.h:27

When using C interface, the allocation is performed using cc_mqttsn_gw_alloc()

struct CC_MqttsnGateway * CC_MqttsnGatewayHandle
Handle for gateway object used in all cc_mqttsn_gw_* functions.
Definition gateway_all.h:27
CC_MqttsnGatewayHandle cc_mqttsn_gw_alloc(void)
Allocate Gateway object.

and de-allocation is performed using cc_mqttsn_gw_free() functions.

void cc_mqttsn_gw_free(CC_MqttsnGatewayHandle gw)
Free allocated Gateway object.

Sending Data

As was stated, the Gateway object is responsible to periodically broadcast ADVERTISE messages to all MQTT-SN clients on the network to advertise its presence. The library just generates the binary data, that needs to be sent, and uses a callback function to request the driving code to send this data over I/O link being used. The required callback needs to be provided by the driving code.

C++ interface:

[](const std::uint8_t* buf, std::size_t bufLen)
{
... // Broadcast the requested data
});
void setSendDataReqCb(SendDataReqCb &&func)
Set the callback to be invoked when new ADVERTISE message needs to be broadcasted.

C interface:

void my_advertise_broadcast(void* userData, const unsigned char* buf, unsigned bufLen)
{
... /* Broadcast the provided buffer */
}
cc_mqttsn_gw_set_advertise_broadcast_req_cb(gw, &my_advertise_broadcast, someUserData);
void cc_mqttsn_gw_set_advertise_broadcast_req_cb(CC_MqttsnGatewayHandle gw, CC_MqttsnGwBroadcastReqCb cb, void *data)
Set callback that requests to send serialised ADVERTISE message.

Time Measurement

As was mentioned earlier there is a need to send ADVERTISE messages periodically. As the result the Gateway object needs to perform some time measurement. It relies on the driving code to provide such service. There is a need to set appropriate callback:

C++ interface:

[](unsigned duration)
{
... // Set timer to expire after duration milliseconds
// After expiry call gw.tick()
};
void setNextTickProgramReqCb(NextTickProgramReqCb &&func)
Set the callback to be invoked when new time measurement is required.

C interface:

void my_tick_req(void* userData, unsigned duration)
{
... /* Set timer to expire after duration milliseconds */
... /* After expiry call cc_mqttsn_gw_tick() */
}
cc_mqttsn_gw_set_tick_req_cb(gw, &my_tick_req, someUserData);
void cc_mqttsn_gw_set_tick_req_cb(CC_MqttsnGatewayHandle gw, CC_MqttsnGwTickReqCb cb, void *data)
Set callback that requests to perform time measurement.

After the requested time expires, the driving code needs to notify the Gateway object. It must call the appropriate tick() function.

C++ interface:

gw.tick();
void tick()
Notify the Gateway object about requested time period expiry.

C interface:

void cc_mqttsn_gw_tick(CC_MqttsnGatewayHandle gw)
Notify the Gateway object about requested time expiry.

Advertise Period

The ADVERTISE message must be sent periodically. The message itself contains information when next ADVERTISE message will be sent. The Gateway object cannot operate without the advertise period being set.

C++ interface:

gw.setAdvertisePeriod(900); // The advertise period is in seconds
void setAdvertisePeriod(std::uint16_t value)
Set period after which ADVERTISE message needs to be constatly sent.

C interface:

cc_mqttsn_gw_set_advertise_period(gw, 900); /* The advertise period is in seconds */
void cc_mqttsn_gw_set_advertise_period(CC_MqttsnGatewayHandle gw, unsigned short value)
Set the advertise period.

It is also possible to retrieve the current configuration:

C++ interface:

std::uint16_t period = gw.getAdvertisePeriod();
std::uint16_t getAdvertisePeriod() const
Get current configuration of the ADVERTISE message period.

C interface:

unsigned short period = cc_mqttsn_gw_get_advertise_period(gw);
unsigned short cc_mqttsn_gw_get_advertise_period(CC_MqttsnGatewayHandle gw)
Get current configuration of the advertise period.

Gateway ID

The ADVERTISE message also contain 1 byte of numeric gateway ID. If the actual gateway ID differs from default value 0. It must be provided to the Gateway object.

C++ interface:

gw.setGatewayId(5);
void setGatewayId(std::uint8_t value)
Set gateway numeric ID to be advertised.

C interface:

void cc_mqttsn_gw_set_id(CC_MqttsnGatewayHandle gw, unsigned char id)
Set the numeric gateway ID.

It is also possible to retrieve the current configuration:

C++ interface:

std::uint8_t id = gw.getGatewayId();
std::uint8_t getGatewayId() const
Get current gateway numeric ID configuration.

C interface:

unsigned char id = cc_mqttsn_gw_get_id(gw);
unsigned char cc_mqttsn_gw_get_id(CC_MqttsnGatewayHandle gw)
Get current configuration of the numeric gateway id.

Start Operation

After all the callbacks have been set and the advertise period was provided, the operation of the Gateway object needs to be properly started.

C++ interface:

if (!gw.start()) {
... // Something is not configured properly
}
bool start()
Start the operation of the object.

C interface:

... /* Something is not configured properly */
}
bool cc_mqttsn_gw_start(CC_MqttsnGatewayHandle gw)
Start operation of the Gateway object.

If the start is successful, the Gateway object will immediatelly request to send new ADVERTISE message and will request a time measurement for the next time.

If the Gateway's operation needs to be paused for a while, use cc_mqttsn_gateway::Gateway::stop() or cc_mqttsn_gw_stop() functions. The operation can be resumed using cc_mqttsn_gateway::Gateway::start() or cc_mqttsn_gw_start() respectively.

C++ interface:

gw.stop();
void stop()
Stop the operation of the object.

C interface:

void cc_mqttsn_gw_stop(CC_MqttsnGatewayHandle gw)
Stop operation of the Gateway object.