RA Flexible Software Package Documentation  Release v5.6.0

 
AWS MQTT

This module provides the AWS MQTT integration documentation.

Overview

The AWS MQTT library can connect to either AWS or a third party MQTT broker such as Mosquitto. The documentation for the library can be found at the following link: coreMQTT.

Features

Configuration

Memory Usage

The AWS CoreMQTT library relies heavily on dynamic memory allocation for thread/task creation as well as other uses. Depending on the configuration it may be required to provide as much as 110k heap. To decrease this it is recommended to tweak the thread stack configuration values based on usage. Notable values are:

FreeRTOS Thread

FreeRTOS Plus TCP

Usage Notes

Limitations

Examples

Connection example using MbedTLS/PKCS11 Transport Interface

#define EXAMPLE_PDN_CONTEXT_NUMBER (1)
#define EXAMPLE_TIMEOUT_MS (5000)
#define EXAMPLE_DISABLE_SNI (false)
#define EXAMPLE_MQTT_HOST ("mqtt_host_server")
#define EXAMPLE_MQTT_HOST_PORT (8883)
#define EXAMPLE_MQTT_SEND_TIMEOUT (5000)
#define EXAMPLE_MQTT_RECEIVE_TIMEOUT (5000)
#define EXAMPLE_MQTT_TOPIC ("example_topic")
#define EXAMPLE_MQTT_CLIENT_IDENTIFIER ("client_id")
#define EXAMPLE_MQTT_KEEP_ALIVE_SECONDS (60)
#define EXAMPLE_MQTT_PAYLOAD ("helloworld")
struct NetworkContext
{
TlsTransportParams_t * pParams;
};
static const char SERVER_CERTIFICATE_PEM[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIDazCCAlOgAwIBAgIURabL79ayIywQv0y8SPnbZ1FYDRIwDQYJKoZIhvcNAQEL\n"
"BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM\n"
"GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0xOTA5MTEyMTIyMjZaFw0yMDA5\n"
"MTAyMTIyMjZaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw\n"
"HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb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Q6\n" \
"wVa0Mx1PlA4enY2rfE3WXP8bzjleSOwR75JXqG2WbPC0/cszwbyPWOEqRpBZfvD/\n" \
"QFkKx06xp1C09XwiQanr2gDucYXHeEKg/9iuJV1UkMQp95ojlhtSXdRZV7/l4pmN\n" \
"fpB2vcAptX/4gY4tDrWMO08JNnRjE7duC+rmmk1hAoGAS4L0QLCNB/h2JOq+Uuhn\n" \
"/FGfmOVfFPFrA6D3DbxcxpWUWVWzSLvb0SOphryzxbfEKyau7V5KbDp7ZSU/IC20\n" \
"KOygjSEkAkDi7fjrrTRW/Cgg6g6G4YIOBO4qCtHdDbwJMHNdk6096qw5EZS67qLp\n" \
"Apz5OZ5zChySjri/+HnTxJECgYBysGSP6IJ3fytplTtAshnU5JU2BWpi3ViBoXoE\n" \
"bndilajWhvJO8dEqBB5OfAcCF0y6TnWtlT8oH21LHnjcNKlsRw0Dvllbd1oylybx\n" \
"3da41dRG0sCEtoflMB7nHdDLt/DZDnoKtVvyFG6gfP47utn+Ahgn+Zp6K+46J3eP\n" \
"s3g8AQKBgE/PJiaF8pbBXaZOuwRRA9GOMSbDIF6+jBYTYp4L9wk4+LZArKtyI+4k\n" \
"Md2DUvHwMC+ddOtKqjYnLm+V5cSbvu7aPvBZtwxghzTUDcf7EvnA3V/bQBh3R0z7\n" \
"pVsxTyGRmBSeLdbUWACUbX9LXdpudarPAJ59daWmP3mBEVmWdzUw\n" \
"-----END RSA PRIVATE KEY-----"
/* Callback to handle MQTT events, user should add their own processing */
static void prvEventCallback(MQTTContext_t * pxMQTTContext,
MQTTPacketInfo_t * pxPacketInfo,
MQTTDeserializedInfo_t * pxDeserializedInfo);
static void prvEventCallback (MQTTContext_t * pxMQTTContext,
MQTTPacketInfo_t * pxPacketInfo,
MQTTDeserializedInfo_t * pxDeserializedInfo)
{
FSP_PARAMETER_NOT_USED(pxMQTTContext);
FSP_PARAMETER_NOT_USED(pxPacketInfo);
FSP_PARAMETER_NOT_USED(pxDeserializedInfo);
}
/* Callback to get current time */
static uint32_t prvGetTimeMs(void);
static uint32_t prvGetTimeMs (void)
{
TickType_t xTickCount = 0;
uint32_t ulTimeMs = 0UL;
/* Get the current tick count. */
xTickCount = xTaskGetTickCount();
/* Convert the ticks to milliseconds. */
ulTimeMs = (uint32_t) xTickCount * (1000U / configTICK_RATE_HZ); // NOLINT(readability-magic-numbers)
return ulTimeMs;
}
NetworkContext_t networkContext;
void rm_aws_transport_interface_port_basic_example (void)
{
ProvisioningParams_t params;
NetworkCredentials_t credentials;
TransportInterface_t transport;
MQTTContext_t mqtt_context;
TlsTransportParams_t transport_params;
static uint8_t buffer[1024]; // NOLINT(readability-magic-numbers)
MQTTFixedBuffer_t networkBuffer;
MQTTConnectInfo_t connectInfo;
MQTTPublishInfo_t lwtInfo;
bool session_present;
/* Open little FS flash and format in order to store keys */
assert(FSP_SUCCESS == RM_LITTLEFS_FLASH_Open(g_rm_littlefs0.p_ctrl, g_rm_littlefs0.p_cfg));
assert(0 == lfs_format(&g_rm_littlefs0_lfs, &g_rm_littlefs0_lfs_cfg));
assert(0 == lfs_mount(&g_rm_littlefs0_lfs, &g_rm_littlefs0_lfs_cfg));
/* Initialize the crypto hardware acceleration. */
/* Write the keys into a secure region in data flash. */
params.pucClientCertificate = (uint8_t *) keyCLIENT_CERTIFICATE_PEM;
params.ulClientCertificateLength = sizeof(keyCLIENT_CERTIFICATE_PEM);
params.pucClientPrivateKey = (uint8_t *) keyCLIENT_PRIVATE_KEY_PEM;
params.ulClientPrivateKeyLength = sizeof(keyCLIENT_PRIVATE_KEY_PEM);
params.pucJITPCertificate = NULL;
params.ulJITPCertificateLength = 0;
vAlternateKeyProvisioning(&params);
/* Initialize network context */
networkContext.pParams = &transport_params;
/* Setup network credentials */
credentials.pAlpnProtos = NULL;
credentials.disableSni = EXAMPLE_DISABLE_SNI;
credentials.pRootCa = (const unsigned char *) SERVER_CERTIFICATE_PEM;
credentials.rootCaSize = sizeof(SERVER_CERTIFICATE_PEM);
credentials.pUserName = NULL;
credentials.userNameSize = 0;
credentials.pPassword = NULL;
credentials.passwordSize = 0;
credentials.pClientCertLabel = pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS;
credentials.pPrivateKeyLabel = pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS;
/* Set transport interface */
transport.pNetworkContext = &networkContext;
transport.recv = TLS_FreeRTOS_recv;
transport.send = TLS_FreeRTOS_send;
/* Connect to a MQTT host */
TLS_FreeRTOS_Connect(&networkContext,
EXAMPLE_MQTT_HOST,
EXAMPLE_MQTT_HOST_PORT,
&credentials,
EXAMPLE_MQTT_RECEIVE_TIMEOUT,
EXAMPLE_MQTT_SEND_TIMEOUT);
/* Fill the values for network buffer. */
networkBuffer.pBuffer = buffer;
networkBuffer.size = 1024; // NOLINT
/* Initialize MQTT */
assert(MQTTSuccess == MQTT_Init(&mqtt_context, &transport, prvGetTimeMs, prvEventCallback, &networkBuffer));
/* Set connection info for MQTT session */
connectInfo.cleanSession = true;
connectInfo.clientIdentifierLength = sizeof(EXAMPLE_MQTT_CLIENT_IDENTIFIER) - 1;
connectInfo.pClientIdentifier = EXAMPLE_MQTT_CLIENT_IDENTIFIER;
connectInfo.keepAliveSeconds = EXAMPLE_MQTT_KEEP_ALIVE_SECONDS;
connectInfo.pUserName = NULL;
connectInfo.userNameLength = 0U;
connectInfo.pPassword = NULL;
connectInfo.passwordLength = 0U;
/* LWT Info. */
lwtInfo.pTopicName = EXAMPLE_MQTT_TOPIC;
lwtInfo.topicNameLength = sizeof(EXAMPLE_MQTT_TOPIC) - 1;
lwtInfo.pPayload = EXAMPLE_MQTT_PAYLOAD;
lwtInfo.payloadLength = strlen(EXAMPLE_MQTT_PAYLOAD);
lwtInfo.qos = MQTTQoS0;
lwtInfo.dup = false;
lwtInfo.retain = false;
/* Send MQTT CONNECT packet to broker. */
assert(MQTTSuccess == MQTT_Connect(&mqtt_context, &connectInfo, &lwtInfo, 20000, &session_present));
}