MQTT-SN Gateway Library
Library that allows implementation of MQTT-SN gateway.
Config

The Config object is responsible to provide a common way to parse the configuration file and provide a convenient interface to retrieve any configuration values, which can later be applied to the Gateway and/or Session objects. The format of the configuration file is described in the example config file provided with the project.

Allocation

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

Interface for Config entity.
Definition: Config.h:34

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

CC_MqttsnConfigHandle cc_mqttsn_gw_config_alloc(void)
Allocate Config object.
Handle for configuration object used in all cc_mqttsn_gw_config_* functions.
Definition: gateway_all.h:464

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

void cc_mqttsn_gw_config_free(CC_MqttsnConfigHandle config)
Free allocated Config object.

Read and Parse Configuration

The gateway application may have multiple configuration options. These options and their parameters may be listed in a special configuration file. The expected format of the file is as following:

  • The comment starts with '#' symbol and ends with new line chracter. The comments are ignored when parsing the file
  • The configuration option contains with key and value pair. The first word (until the first white space character) in the configuration line is considered to be a key, all the rest (until the end of the line) is considered to be a value.

The Config object assigns various default values to all the configuration options when constructed. It may be requested to read and parse the configuration file. In this case, the Config object will update the configuration values, recorded in its internal state, with new values read from file. If some option does not appear to be listed in the file, the default value will be reported when inquired.

C++ interface allows usage any of standard input streams:

std::ifstream stream("/path/to/config/file");
config.read(stream);
void read(std::istream &stream)
Read configuration from input stream.

C interface also allows either reading from file:

cc_mqttsn_gw_config_read(handle, "/path/to/config/file");
bool cc_mqttsn_gw_config_read(CC_MqttsnConfigHandle config, const char *filename)
Read configuration file.

or parsing the string, if configuration file contents have been read from file into a buffer earlier:

char buf[4096] = {0};
... /* read data from buf into buf, put \0 at the end */
void cc_mqttsn_gw_config_parse(CC_MqttsnConfigHandle config, const char *str)
Parse configuration contents from string.

Gateway ID

Both Gateway (see Gateway ID) and Session (see Gateway ID) objects require knowledge of gateway numeric ID to be able to properly report it to the client(s).

C++ interface:

std::uint8_t id = config.gatewayId();
std::uint8_t gatewayId() const
Get gateway numeric ID.

C interface:

unsigned char id = cc_mqttsn_gw_config_id(handle);
unsigned char cc_mqttsn_gw_config_id(CC_MqttsnConfigHandle config)
Get gateway numeric ID.

Advertise Period

The Gateway object sends ADVERTISE message periodically. It needs to be configured with propere period value (see Advertise Period). Use the following interface to retrieve this value from the Config object.

C++ interface:

std::uint16_t period = config.advertisePeriod();
std::uint16_t advertisePeriod() const
Get advertise period.

C interface:

unsigned short period = cc_mqttsn_gw_config_advertise_period(handle);
unsigned short cc_mqttsn_gw_config_advertise_period(CC_MqttsnConfigHandle config)
Get advertise period.

Retry Configuration

The Session object can be configured with period between its retry attempts as well as number of such attempts (see Retry Attempts). Use the following interface of the Config object to retreive these values:

C++ interface:

unsigned period = config.retryPeriod();
unsigned attempts = config.retryCount();
unsigned retryCount() const
Get number of retry attempts.
unsigned retryPeriod() const
Get retry period.

C interface:

unsigned period = cc_mqttsn_gw_config_retry_period(handle);
unsigned attempts = cc_mqttsn_gw_config_retry_count(handle);
unsigned cc_mqttsn_gw_config_retry_count(CC_MqttsnConfigHandle config)
Get number of retry attempts.
unsigned cc_mqttsn_gw_config_retry_period(CC_MqttsnConfigHandle config)
Get retry period.

Default Client ID

Can be used in Session object configuration (see Default Client ID).

C++ interface:

const std::string& clientId = config.defaultClientId();
const std::string & defaultClientId() const
Get default client ID.

C interface:

const char* clientId = cc_mqttsn_gw_config_default_client_id(handle);
const char * cc_mqttsn_gw_config_default_client_id(CC_MqttsnConfigHandle config)
Get default client ID.

Keep Alive Period for Publish Only Clients

Can be used in Session object configuration (see Publish Only Client).

C++ interface:

std::uint16_t keepAlivePeriod = config.pubOnlyKeepAlive();
std::uint16_t pubOnlyKeepAlive() const
Get keep alive period for publish only clients.

C interface:

unsigned short keepAlivePeriod = cc_mqttsn_gw_config_pub_only_keep_alive(handle);
unsigned cc_mqttsn_gw_config_pub_only_keep_alive(CC_MqttsnConfigHandle config)
Get keep alive period for publish only clients.

Limit Messages for Sleeping Clients

Can be used in Session object configuration (see Sleeping Client).

C++ interface:

std::size_t limit = config.sleepingClientMsgLimit();
std::size_t sleepingClientMsgLimit() const
Get limit for max number of messages to accumulate for sleeping clients.

C interface:

unsigned cc_mqttsn_gw_config_sleeping_client_msg_limit(CC_MqttsnConfigHandle config)
Get limit for max number of messages to accumulate for sleeping clients.

Predefined Topics

The Session object can be configured with number of predefined topics (see Predefined Topics). Retrieve this information from the Config object using the following API.

C++ interface:

... // iterate over the list and assign values to @b Session object(s).
const PredefinedTopicsList & predefinedTopics() const
Get access to the list of predefined topics.
std::vector< PredefinedTopicInfo > PredefinedTopicsList
Type of list containing predefined topics.
Definition: Config.h:56

C interface:

unsigned count = cc_mqttsn_gw_config_get_predefined_topics(handle, infos, 100);
unsigned idx;
for (idx = 0; idx < count; idx++) {
const CC_MqttsnPredefinedTopicInfo* elem = infos[idx];
... /* assign elem info to @b Session object(s).
* }
*
unsigned cc_mqttsn_gw_config_get_predefined_topics(CC_MqttsnConfigHandle config, CC_MqttsnPredefinedTopicInfo *buf, unsigned bufLen)
Read information about available topic IDs into a buffer.
Info about single predefined topic.
Definition: gateway_all.h:448

C interface also allows to retrieve number of predefined topics in case there is no known limit for the buffer upfront and it needs to be dynamically allocated:

unsigned cc_mqttsn_gw_config_available_predefined_topics(CC_MqttsnConfigHandle config)
Get number of available predefined topic IDs.

Authentication Information

The Session object can inquire authentication information when some client attempts connection (see Client Authentication). Retrieve this information from the Config object using the following API.

C++ interface:

std::vector< AuthInfo > AuthInfosList
Type of list containing authentication information for multiple clients.
Definition: Config.h:67
const AuthInfosList & authInfos() const
Get access to list of authentication informations.

C interface:

CC_MqttsnAuthInfo infos[100];
unsigned count = cc_mqttsn_gw_config_get_auth_infos(handle, infos, 100);
... // use up to count infos.
unsigned cc_mqttsn_gw_config_get_auth_infos(CC_MqttsnConfigHandle config, CC_MqttsnAuthInfo *buf, unsigned bufLen)
Read clients' authentication information into a buffer.
Authentication infor for a single client.
Definition: gateway_all.h:456

C interface also allows to retrieve total number of authentication infos available. It can be useful in case there is no known limit for the buffer upfront and it needs to be dynamically allocated:

unsigned cc_mqttsn_gw_config_available_auth_infos(CC_MqttsnConfigHandle config)
Get number of available authenticatin infos for all the clients.

Topic ID Allocation Range

The Session object can be configured to limit its range of topic IDs, which are allocated for newly registered topic strings (see Allocating Topic IDs). Retrieve this information from the Config object using the following API.

C++ interface:

std::uint16_t minTopicId = range.first;
std::uint16_t maxTopicId = range.second;
std::pair< std::uint16_t, std::uint16_t > TopicIdsRange
Range of topic IDs.
Definition: Config.h:72
TopicIdsRange topicIdAllocRange() const
Get range of allowed topic IDs for allocation.

C interface:

unsigned short minTopicId = 0;
unsigned short maxTopicId = 0;
cc_mqttsn_gw_config_topic_id_alloc_range(handle, &minTopicId, &maxTopicId);
void cc_mqttsn_gw_config_topic_id_alloc_range(CC_MqttsnConfigHandle config, unsigned short *min, unsigned short *max)
Get range of allowed topic IDs for allocation.

Broker Address and Port

The MQTT-SN gateway application must connect and forward traffic to MQTT broker. Below are API functions that can be used to retrieve the TCP/IP address and port of the broker.

C++ interface

const std::string& addr = config.brokerTcpHostAddress();
std::uint16_t port = config.brokerTcpHostPort();
std::uint16_t brokerTcpHostPort() const
Get TCP/IP port of the broker.
const std::string & brokerTcpHostAddress() const
Get TCP/IP address of the broker.

C interface

const char* addr = cc_mqttsn_gw_config_broker_address(handle);
unsigned short port = cc_mqttsn_gw_config_broker_port(handle);
const char * cc_mqttsn_gw_config_broker_address(CC_MqttsnConfigHandle config)
Get TCP/IP address of the broker.
unsigned short cc_mqttsn_gw_config_broker_port(CC_MqttsnConfigHandle config)
Get TCP/IP port of the broker.

Custom Configuration Values

The Config object has a list of predefined options it recognises in the configuration file. It also accumulates all the options it doesn't recognise. As a reminder: the first word (until the first white character) in the configuration line is considered to be a key, and the rest of the string (from the next non-white character until the end of the line) is considered to be a value. The Config option allows access to such values for the driving code to parse and use.

C++ interface

auto iters = map.equal_range("my_custom_config");
for (auto iter = iters.first; iter != iters.second; ++iter) {
const std::string& value = iter->second;
... // Parse and use the value
}
std::multimap< std::string, std::string > ConfigMap
Full configuration map.
Definition: Config.h:42
const ConfigMap & configMap() const
Get access to the full configuration map.

NOTE, that cc_mqttsn_gateway::Config::ConfigMap type is a multimap, it allows multiple entries for the same key.

C interface:
First, retrieve the number of available entries for the provided configuration key:

unsigned count = cc_mqttsn_gw_config_values_count(handle, "my_custom_config");
unsigned cc_mqttsn_gw_config_values_count(CC_MqttsnConfigHandle config, const char *key)
Get number of available configuration values for the provided key.

Then, access the available configuration values:

unsigned idx;
for (idx = 0; idx < count; idx++) {
const char* value = cc_mqttsn_gw_config_get_value(handle, "my_custom_config", idx);
... /* Parse and use the value */
}
const char * cc_mqttsn_gw_config_get_value(CC_MqttsnConfigHandle config, const char *key, unsigned idx)
Get the available value for the configuration key.