MQTT client implementation using the DA16XXX WiFi module on RA MCUs.
Overview
This Middleware module supplies an implementation for the MQTT Client on the DA16XXX module. The network stack is run on the DA16XXX and this module inherits and extends the core functionality from the DA16XXX WiFi driver. The DA16XXX MQTT module only supports FreeRTOS currently (Microsoft Azure support expected in future release).
The DA16XXX module supports the following modules:
Refer to WiFi Onchip DA16XXX Framework Driver (rm_wifi_da16xxx).
Features
The MQTT Onchip da16xxx Middleware driver supplies these features:
- Supports connect/disconnect to an MQTT broker via hostname, port, and user credentials
- Supports unsecure and secure connection via TLS encryption
- Supports the MQTT subscribe/publish model for multiple topics
- Supports other optional configurations such as MQTT v3.1.1, Quality-of-service (QoS) level, TLS cipher suites, and ALPNs
Configuration
Build Time Configurations for rm_mqtt_onchip_da16xxx
The following build time configurations are defined in fsp_cfg/rm_mqtt_onchip_da16xxx_cfg.h:
Configuration | Options | Default | Description |
Parameter Checking |
-
Default (BSP)
-
Enabled
-
Disabled
| Default (BSP) | If selected code for parameter checking is included in the build. |
Size of MQTT RX buffer | Must be an integer greater than 0 | 512 | Size in bytes of the MQTT buffer used for receiving subscribed data. Must be an integer greater than 0. |
Size of MQTT TX buffer | Must be an integer greater than 200 and less than 2064 | 512 | Size in bytes of the MQTT buffer used for sending commands and publishing data. Maximum publishing length is 2063 bytes |
Refer to WiFi Onchip DA16XXX Framework Driver (rm_wifi_da16xxx).
Note: This module provides the basic configurations in the FSP Configurator required to connect to an MQTT broker such as credential information (user, password, client key, etc.), publish/subscribe topics, and optional configurations as required. The user can then copy these configurations to a new structure if they want to connect to a new endpoint/make changes to the configurations dynamically.
Interrupt Configuration
Refer to UART (r_sci_uart). R_SCI_UART_Open() is called by WiFi Onchip DA16XXX Framework Driver (rm_wifi_da16xxx).
Clock Configuration
Refer to UART (r_sci_uart).
Pin Configuration
Refer to UART (r_sci_uart). R_SCI_UART_Open() is called by WiFi Onchip DA16XXX Framework Driver (rm_wifi_da16xxx)
Usage Notes
Refer to WiFi Onchip DA16XXX Framework Driver (rm_wifi_da16xxx).
- This module is not a standalone module - ensure that the WiFi module APIs are invoked first to configure the DA16XXX module and connect to an Access Point before using the MQTT module where the default buffer size for incoming TLS is 6KB and outgoing TLS is 4KB.
- When the user intends to use secure TLS in their application, they need to provide certificates (root certificate authority, client certificate, and private key) in a header file
- The user must provide certificate macro names in the FSP Configurator properties, as well as the name of the header file (to link the module, certificates, and header together)
- If the user provides these credentials in the Configurator, they must create a new header file in their /src/ folder with the same name as they provided in the Configurator properties. When the code is generated, a build error will occur if the header file is not created
- If only using username and password, these fields can be left blank (default), and a header file does not need to be created
Limitations
Refer to WiFi Onchip DA16XXX Framework Driver (rm_wifi_da16xxx).
- Ensure that DA16XXX SDK version v3.2.6 or later is being used, where the default buffer size for incoming TLS is 6KB and outgoing TLS is 4KB. Using earlier versions may result in failure to connect to an MQTT broker with TLS (unless buffer size settings are altered manually)
- For publish/subscribe topics, a separate QoS level cannot be set per topic - only one QoS level can be set which applies to all messages
- Currently the module only supports unsecured MQTT with username/password or secure MQTT/TLS with certificates (root CA, client certificate, and private key). The module does not accept username/password with certificates.
Examples
Basic Example
This is a basic example of minimal use of MQTT Middleware in an application.
void mqtt_onchip_basic_example (void)
{
{
.xPassword.xWPA.cPassphrase = WIFI_PWD,
.ucSSID = WIFI_SSID,
.xPassword.xWPA.ucLength = sizeof(WIFI_PWD),
.ucSSIDLength = sizeof(WIFI_SSID),
};
assert(FSP_SUCCESS == mqtt_err);
assert(FSP_SUCCESS == mqtt_err);
assert(FSP_SUCCESS == mqtt_err);
assert(FSP_SUCCESS == mqtt_err);
do
{
} while (cb_flag == 0);
assert(FSP_SUCCESS == mqtt_err);
assert(FSP_SUCCESS == mqtt_err);
}
◆ mqtt_onchip_da16xxx_sub_info_t
struct mqtt_onchip_da16xxx_sub_info_t |
MQTT SUBSCRIBE packet parameters
Data Fields |
mqtt_onchip_da16xxx_qos_t |
qos |
Quality of Service for subscription. |
const char * |
p_topic_filter |
Topic filter to subscribe to. |
uint16_t |
topic_filter_length |
Length of subscription topic filter. |
◆ mqtt_onchip_da16xxx_pub_info_t
struct mqtt_onchip_da16xxx_pub_info_t |
MQTT PUBLISH packet parameters
Data Fields |
mqtt_onchip_da16xxx_qos_t |
qos |
Quality of Service for subscription. |
const char * |
p_topic_name |
Topic name on which the message is published. |
uint16_t |
topic_name_Length |
Length of topic name. |
const char * |
p_payload |
Message payload. |
uint32_t |
payload_length |
Message payload length. |
◆ mqtt_onchip_da16xxx_callback_args_t
struct mqtt_onchip_da16xxx_callback_args_t |
MQTT Packet info structure to be passed to user callback
Data Fields |
uint8_t * |
p_data |
Payload received from subscribed MQTT topic. |
const char * |
p_topic |
Topic to which the message payload belongs to. |
uint32_t |
data_length |
Length of the MQTT payload. |
void const * |
p_context |
Placeholder for user data. |
◆ mqtt_onchip_da16xxx_cfg_t
struct mqtt_onchip_da16xxx_cfg_t |
◆ mqtt_onchip_da16xxx_instance_ctrl_t
struct mqtt_onchip_da16xxx_instance_ctrl_t |
MQTT_ONCHIP_DA16XXX private control block. DO NOT MODIFY.
Data Fields |
uint8_t |
cmd_tx_buff[MQTT_ONCHIP_DA16XXX_CFG_CMD_TX_BUF_SIZE] |
Command send buffer. |
uint8_t |
cmd_rx_buff[MQTT_ONCHIP_DA16XXX_CFG_CMD_RX_BUF_SIZE] |
Command receive buffer. |
uint32_t |
open |
Flag to indicate if MQTT has been opened. |
bool |
is_mqtt_connected |
Flag to track MQTT connection status. |
mqtt_onchip_da16xxx_cfg_t const * |
p_cfg |
Pointer to p_cfg for MQTT. |
◆ mqtt_onchip_da16xxx_qos_t
MQTT Quality-of-service (QoS) levels
Enumerator |
---|
MQTT_ONCHIP_DA16XXX_QOS_0 | Delivery at most once.
|
MQTT_ONCHIP_DA16XXX_QOS_1 | Delivery at least once.
|
MQTT_ONCHIP_DA16XXX_QOS_2 | Delivery exactly once.
|
◆ mqtt_onchip_da16xxx_tls_cipher_suites_t
MQTT TLS Cipher Suites
Enumerator |
---|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA | TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA protocol.
|
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA | TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA protocol.
|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 | TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 protocol.
|
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 | TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 protocol.
|
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 | TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol.
|
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 | TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol.
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA | TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA protocol.
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA | TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA protocol.
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 | TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 protocol.
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 | TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 protocol.
|
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 | TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 protocol.
|
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 | TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 protocol.
|
◆ RM_MQTT_DA16XXX_Open()
Initialize the DA16XXX on-chip MQTT Client service.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
[in] | p_cfg | Pointer to MQTT Client configuration structure. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_WIFI_FAILED | Error occurred with command to Wifi module. |
FSP_ERR_ASSERTION | The p_cfg instance is NULL. |
FSP_ERR_INVALID_ARGUMENT | Data size is too large or NULL. |
FSP_ERR_ALREADY_OPEN | The instance has already been opened. |
◆ RM_MQTT_DA16XXX_Disconnect()
Disconnect from DA16XXX MQTT Client service.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_WIFI_FAILED | Error occurred with command to Wifi module. |
FSP_ERR_ASSERTION | The p_ctrl instance is NULL. |
FSP_ERR_NOT_OPEN | The instance has not been opened or the client is not connected. |
◆ RM_MQTT_DA16XXX_Connect()
Configure and connect the DA16XXX MQTT Client service.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
[in] | timeout_ms | Timeout in milliseconds. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_WIFI_FAILED | Error occurred with command to Wifi module. |
FSP_ERR_ASSERTION | The p_ctrl is NULL. |
FSP_ERR_NOT_OPEN | The instance has not been opened. |
FSP_ERR_IN_USE | The MQTT client is already connected. |
FSP_ERR_INVALID_DATA | Response does not contain Connect status. |
◆ RM_MQTT_DA16XXX_Publish()
Publish a message for a given MQTT topic.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
[in] | p_pub_info | MQTT Publish packet parameters. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_WIFI_FAILED | Error occurred with command to Wifi module. |
FSP_ERR_ASSERTION | The p_ctrl, p_pub_info is NULL. |
FSP_ERR_NOT_OPEN | The instance has not been opened or the client is not connected. |
FSP_ERR_INVALID_DATA | Data size is too large. |
◆ RM_MQTT_DA16XXX_Subscribe()
Subscribe to DA16XXX MQTT topics.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
[in] | p_sub_info | List of MQTT subscription info. |
[in] | subscription_count | Number of topics to subscribe to. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_WIFI_FAILED | Error occurred with command to Wifi module. |
FSP_ERR_ASSERTION | The p_ctrl, p_sub_info is NULL or subscription_count is 0. |
FSP_ERR_NOT_OPEN | The instance has not been opened. |
FSP_ERR_INVALID_DATA | Data size is too large. |
◆ RM_MQTT_DA16XXX_UnSubscribe()
Unsubscribe from DA16XXX MQTT topics.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
[in] | p_sub_info | List of MQTT subscription info. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_WIFI_FAILED | Error occurred with command to Wifi module. |
FSP_ERR_ASSERTION | The p_ctrl, p_sub_info is NULL. |
FSP_ERR_NOT_OPEN | The instance has not been opened or the client is not connected. |
FSP_ERR_INVALID_DATA | Data size is too large. |
◆ RM_MQTT_DA16XXX_Receive()
Receive data subscribed to on DA16XXX MQTT Client service.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
[in] | p_cfg | Pointer to MQTT Client configuration structure. |
- Return values
-
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_ASSERTION | The p_ctrl, p_textstring, p_ip_addr is NULL. |
FSP_ERR_NOT_OPEN | The instance has not been opened or the client is not connected. |
FSP_ERR_INVALID_DATA | Receive function did not receive valid publish data. |
◆ RM_MQTT_DA16XXX_Close()
Close the DA16XXX MQTT Client service.
- Parameters
-
[in] | p_ctrl | Pointer to MQTT Client instance control structure. |
- Return values
-
FSP_ERR_NOT_OPEN | The instance has not been opened. |
FSP_SUCCESS | Function completed successfully. |
FSP_ERR_ASSERTION | The p_ctrl, p_textstring, p_ip_addr is NULL. |