diff --git a/README.md b/README.md
index bb99f6d..11cce70 100644
--- a/README.md
+++ b/README.md
@@ -48,13 +48,17 @@ Some of the key features include:
- [PSoC™ 62S2 evaluation kit (CY8CEVAL-062S2-LAI-4373M2)](https://www.cypress.com/documentation/development-kitsboards/psoc-62s2-evaluation-kit-cy8ceval-062s2)
+- [CYW943907AEVAL1F Evaluation Kit(CYW943907AEVAL1F)](https://www.cypress.com/documentation/development-kitsboards/cyw943907aeval1f-evaluation-kit)
+
+- [CYW954907AEVAL1F Evaluation Kit(CYW954907AEVAL1F)](https://www.cypress.com/documentation/development-kitsboards/cyw954907aeval1f-evaluation-kit)
+
## Dependent libraries
This MQTT client library depends on the following libraries. These libraries are included by default.
-- [Wi-Fi middleware core](https://github.com/cypresssemiconductorco/wifi-mw-core)
+- [Wi-Fi middleware core](https://github.com/Infineon/wifi-mw-core)
-- [AWS IoT device SDK port](https://github.com/cypresssemiconductorco/aws-iot-device-sdk-port)
+- [AWS IoT device SDK port](https://github.com/Infineon/aws-iot-device-sdk-port)
- [AWS IoT device SDK MQTT library](https://github.com/aws/aws-iot-device-sdk-embedded-C/tree/202103.00)
@@ -76,6 +80,8 @@ This MQTT client library depends on the following libraries. These libraries are
`CY_MQTT_MESSAGE_SEND_TIMEOUT_MS` | MQTT message send timeout | `DEFINES += CY_MQTT_MESSAGE_SEND_TIMEOUT_MS=3000`
`CY_MQTT_MESSAGE_RECEIVE_TIMEOUT_MS` | MQTT message receive timeout | `DEFINES += CY_MQTT_MESSAGE_RECEIVE_TIMEOUT_MS=500`
`CY_MQTT_MAX_RETRY_VALUE` | MQTT library retry mechanism for MQTT publish/subscribe/unsubscribe messages if the acknowledgement is not received from the broker on time. You can configure the maximum number of retries. | `DEFINES += CY_MQTT_MAX_RETRY_VALUE=3`
+ `CY_MQTT_MAX_OUTGOING_PUBLISHES` | If user need to perform multiple publish operations simultaneously on a single MQTT instance, then `CY_MQTT_MAX_OUTGOING_PUBLISHES` macro needs be configured with the number of simultaneous publish operation that user wants to perform. For the default value of this macro, refer to MQTT library API header file. This macro can be configured by adding a define in the application Makefile. | `DEFINES += CY_MQTT_MAX_OUTGOING_PUBLISHES=2`
+ `CY_MQTT_MAX_OUTGOING_SUBSCRIBES` | If user need to perform multiple subscribe operations simultaneously on a single MQTT instance, then `CY_MQTT_MAX_OUTGOING_SUBSCRIBES` macro needs be configured with the number of simultaneous subscribe operation that user wants to perform. For the default value of this macro, refer to MQTT library API header file. This macro can be configured by adding a define in the application Makefile. | `DEFINES += CY_MQTT_MAX_OUTGOING_SUBSCRIBES=2`
`MQTT_PINGRESP_TIMEOUT_MS` | A "reasonable amount of time" (timeout value) to wait for keepalive response from the MQTT broker | `DEFINES += MQTT_PINGRESP_TIMEOUT_MS=5000`
`MQTT_RECV_POLLING_TIMEOUT_MS` | A "maximum polling duration" that is allowed without any data reception from the network for the incoming packet | `DEFINES += MQTT_RECV_POLLING_TIMEOUT_MS=1000`
`MQTT_SEND_RETRY_TIMEOUT_MS` | A "maximum duration" that is allowed for no data transmission over the network through the transport send function | `DEFINES += MQTT_SEND_RETRY_TIMEOUT_MS=500`
@@ -83,9 +89,9 @@ This MQTT client library depends on the following libraries. These libraries are
**Note:** It is important to note that having `MQTT_RECV_POLLING_TIMEOUT_MS` timeout too short will result in MQTT being disconnected due to the possibility of partial data being received. If you have small TCP buffers and a high latency network, the optimum value for the timeout can be surprisingly long. In such cases, optimum value for the timeout can be better determined based on experimenting the MQTT applications with payloads bigger than the TCP buffer. See [AWS coreMQTT documentation](https://docs.aws.amazon.com/embedded-csdk/202103.00/lib-ref/libraries/standard/coreMQTT/docs/doxygen/output/html/mqtt_timeouts.html#mqtt_timeouts_receive_polling) for more details.
-5. Review and make the required changes to the pre-defined configuration files bundled with the wifi-mw-core library for FreeRTOS, lwIP, and Mbed TLS. See [README.md](https://github.com/cypresssemiconductorco/wifi-mw-core/blob/master/README.md) for details.
+5. Review and make the required changes to the pre-defined configuration files bundled with the wifi-mw-core library for FreeRTOS, lwIP, and Mbed TLS. See [README.md](https://github.com/Infineon/wifi-mw-core/blob/master/README.md) for details.
-6. Define the following COMPONENTS in the application Makefile for the MQTT library. See [README.md](https://github.com/cypresssemiconductorco/wifi-mw-core/blob/master/README.md).
+6. Define the following COMPONENTS in the application Makefile for the MQTT library. See [README.md](https://github.com/Infineon/wifi-mw-core/blob/master/README.md).
```
COMPONENTS=FREERTOS MBEDTLS LWIP SECURE_SOCKETS
```
@@ -122,7 +128,7 @@ The MQTT library disables all debug log messages by default. Do the following to
DEFINES+=ENABLE_MQTT_LOGS
```
-2. Call the `cy_log_init()` function provided by the *cy-log* module. cy-log is part of the *connectivity-utilities* library. See [connectivity-utilities library API documentation](https://cypresssemiconductorco.github.io/connectivity-utilities/api_reference_manual/html/group__logging__utils.html) for cy-log details.
+2. Call the `cy_log_init()` function provided by the *cy-log* module. cy-log is part of the *connectivity-utilities* library. See [connectivity-utilities library API documentation](https://infineon.github.io/connectivity-utilities/api_reference_manual/html/group__logging__utils.html) for cy-log details.
## Usage notes
@@ -197,7 +203,7 @@ Do the following to establish a connection with the broker using credentials sto
- [MQTT client library RELEASE.md](./RELEASE.md)
-- [MQTT client API documentation](https://cypresssemiconductorco.github.io/mqtt/api_reference_manual/html/index.html)
+- [MQTT client API documentation](https://infineon.github.io/mqtt/api_reference_manual/html/index.html)
- [ModusToolbox™ software environment, quick start guide, documentation, and videos](https://www.cypress.com/products/modustoolbox-software-environment)
diff --git a/RELEASE.md b/RELEASE.md
index f0c2e20..deee1dd 100644
--- a/RELEASE.md
+++ b/RELEASE.md
@@ -10,6 +10,10 @@ See the [README.md](./README.md) for a complete description of the MQTT client l
## Changelog
+### v3.3.0
+
+- Added support for CYW943907AEVAL1F and CYW954907AEVAL1F kits
+
### v3.2.0
- Added support for secured kit (for example: CY8CKIT-064S0S2-4343W)
diff --git a/docs/api_reference_manual/html/index.html b/docs/api_reference_manual/html/index.html
index 51ef670..4d2a99d 100644
--- a/docs/api_reference_manual/html/index.html
+++ b/docs/api_reference_manual/html/index.html
@@ -118,11 +118,13 @@
This MQTT client library depends on the following libraries. Both these libraries are included by default.
-- MQTT client library RELEASE.md .
+- MQTT client library RELEASE.md .
- ModusToolbox™ software environment, quick start guide, documentation, and videos .
- AWS-IoT device SDK Library .
- ModusToolbox™ code examples .
@@ -239,7 +243,7 @@
For secured platform CY8CKIT-064S0S2-4343W:
Code snippets
@@ -247,19 +251,19 @@
Code Snippet 1: Initialize an MQTT network socket
This code snippet demonstrates the initialization of the network sockets required for the MQTT library.
void snippet_cy_mqtt_init( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
}
}
Code Snippet 2: Creating an MQTT instance for secured connection with the MQTT broker
-This code snippet demonstrates the initialization of the configuration structures required for creating an MQTT client handle for establishing a secured connection with an MQTT broker, and the usage of the cy_mqtt_create() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char client_cert[] = { };
static const char client_key[] = { };
static const char root_ca_cert[] = { };
cy_mqtt_t secure_mqtthandle;
void mqtt_secure_event_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_create_secure( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
credentials.client_cert = (const char *) &client_cert;
credentials.client_cert_size = sizeof( client_cert );
credentials.private_key = (const char *) &client_key;
credentials.private_key_size = sizeof( client_key );
credentials.root_ca = (const char *) &root_ca_cert;
credentials.root_ca_size = sizeof( root_ca_cert );
security = &credentials;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&secure_mqtthandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
+
This code snippet demonstrates the initialization of the configuration structures required for creating an MQTT client handle for establishing a secured connection with an MQTT broker, and the usage of the cy_mqtt_create() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char client_cert[] = { "Paste client certificate here" };
static const char client_key[] = { "Paste client key here" };
static const char root_ca_cert[] = { "Paste RootCA certificate here" };
cy_mqtt_t secure_mqtthandle;
void mqtt_secure_event_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_create_secure( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
credentials.client_cert = (const char *) &client_cert;
credentials.client_cert_size = sizeof( client_cert );
credentials.private_key = (const char *) &client_key;
credentials.private_key_size = sizeof( client_key );
credentials.root_ca = (const char *) &root_ca_cert;
credentials.root_ca_size = sizeof( root_ca_cert );
security = &credentials;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&secure_mqtthandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 3: Creating an MQTT instance for a non-secured connection with an MQTT broker
This code snippet demonstrates the initialization of the configuration structures required for creating an MQTT client handle for establishing a non-secured connection with an MQTT broker, and the usage of the cy_mqtt_create() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_NON_SECURED ( 1883 )
#define NETWORK_BUFFER_SIZE ( 1024U )
cy_mqtt_t nonsecure_mqtthandle;
void mqtt_non_secure_event_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_create_non_secure( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
broker_info.
port = MQTT_PORT_NON_SECURED;
NULL, &broker_info,
&nonsecure_mqtthandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 4: Establishing an MQTT client session
-This code snippet demonstrates the steps to establish an MQTT client session with an MQTT broker using the previously created MQTT handle, and the usage of the cy_mqtt_connect() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char clientcert[] = { };
static const char clientkey[] = { };
static const char rootcacertificate[] = { };
cy_mqtt_t mqtthandle;
static void mqtt_event_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_connect( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
qos = MQTT_QOS2;
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &clientcert;
credentials.client_cert_size = sizeof( clientcert );
credentials.private_key = (const char *) &clientkey;
credentials.private_key_size = sizeof( clientkey );
credentials.root_ca = (const char *) &rootcacertificate;
credentials.root_ca_size = sizeof( rootcacertificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&mqtthandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
+
This code snippet demonstrates the steps to establish an MQTT client session with an MQTT broker using the previously created MQTT handle, and the usage of the cy_mqtt_connect() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char clientcert[] = { "Paste client certificate here" };
static const char clientkey[] = { "Paste client key here" };
static const char rootcacertificate[] = { "Paste RootCA certificate here" };
cy_mqtt_t mqtthandle;
static void mqtt_event_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_connect( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &clientcert;
credentials.client_cert_size = sizeof( clientcert );
credentials.private_key = (const char *) &clientkey;
credentials.private_key_size = sizeof( clientkey );
credentials.root_ca = (const char *) &rootcacertificate;
credentials.root_ca_size = sizeof( rootcacertificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&mqtthandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 5: MQTT subscribe and unsubscribe
-This code snippet demonstrates the steps to subscribe for particular topic, unsubscribe from a previously subscribed MQTT topic, and the usage of the cy_mqtt_subscribe() and cy_mqtt_unsubscribe() API functions.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define MQTT_NUM_OF_SUBS_IN_SINGLE_REQ ( 1U )
#define MQTT_NUM_OF_UNSUBS_IN_SINGLE_REQ MQTT_NUM_OF_SUBS_IN_SINGLE_REQ
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char client_certificate[] = { };
static const char client_private_key[] = { };
static const char root_ca_certificate[] = { };
cy_mqtt_t mqtt_handle;
void mqtt_eventcb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_subscribe_unsubscribe( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
qos = MQTT_QOS2;
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &client_certificate;
credentials.client_cert_size = sizeof( client_certificate );
credentials.private_key = (const char *) &client_private_key;
credentials.private_key_size = sizeof( client_private_key );
credentials.root_ca = (const char *) &root_ca_certificate;
credentials.root_ca_size = sizeof( root_ca_certificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&mqtt_handle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
sub_msg[0].
qos = MQTT_QOS2;
sub_msg[0].
topic = MQTT_TOPIC;
TestRes =
cy_mqtt_subscribe( mqtt_handle, &sub_msg[0], MQTT_NUM_OF_SUBS_IN_SINGLE_REQ );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
{
}
else
{
}
unsub_msg[0].
qos = MQTT_QOS2;
unsub_msg[0].
topic = MQTT_TOPIC;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
+
This code snippet demonstrates the steps to subscribe for particular topic, unsubscribe from a previously subscribed MQTT topic, and the usage of the cy_mqtt_subscribe() and cy_mqtt_unsubscribe() API functions.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define MQTT_NUM_OF_SUBS_IN_SINGLE_REQ ( 1U )
#define MQTT_NUM_OF_UNSUBS_IN_SINGLE_REQ MQTT_NUM_OF_SUBS_IN_SINGLE_REQ
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char client_certificate[] = { "Paste client certificate here" };
static const char client_private_key[] = { "Paste client key here" };
static const char root_ca_certificate[] = { "Paste RootCA certificate here" };
cy_mqtt_t mqtt_handle;
void mqtt_eventcb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_subscribe_unsubscribe( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &client_certificate;
credentials.client_cert_size = sizeof( client_certificate );
credentials.private_key = (const char *) &client_private_key;
credentials.private_key_size = sizeof( client_private_key );
credentials.root_ca = (const char *) &root_ca_certificate;
credentials.root_ca_size = sizeof( root_ca_certificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&mqtt_handle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
sub_msg[0].
topic = MQTT_TOPIC;
TestRes =
cy_mqtt_subscribe( mqtt_handle, &sub_msg[0], MQTT_NUM_OF_SUBS_IN_SINGLE_REQ );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
{
}
else
{
}
unsub_msg[0].
topic = MQTT_TOPIC;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 6: MQTT publish
-This code snippet demonstrates the steps to publish messages on an MQTT topic, and the usage of the cy_mqtt_publish() API function. You can choose an appropriate QoS level for the application. In this snippet, the QoS level is set as 2.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define MQTT_PUB_MESSAGE "Publish - Hello MQTT!"
#define MQTT_PUB_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_PUB_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char clientcertificate[] = { };
static const char clientprivate_key[] = { };
static const char rootca_certificate[] = { };
cy_mqtt_t MQTThandle;
void mqtt_event_call_back( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_publish( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
qos = MQTT_QOS2;
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &clientcertificate;
credentials.client_cert_size = sizeof( clientcertificate );
credentials.private_key = (const char *) &clientprivate_key;
credentials.private_key_size = sizeof( clientprivate_key );
credentials.root_ca = (const char *) &rootca_certificate;
credentials.root_ca_size = sizeof( rootca_certificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&MQTThandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
pub_msg.
topic = MQTT_TOPIC;
pub_msg.
payload = MQTT_PUB_MESSAGE;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
+
This code snippet demonstrates the steps to publish messages on an MQTT topic, and the usage of the cy_mqtt_publish() API function. You can choose an appropriate QoS level for the application. In this snippet, the QoS level is set as 2.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define MQTT_PUB_MESSAGE "Publish - Hello MQTT!"
#define MQTT_PUB_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_PUB_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char clientcertificate[] = { "Paste client certificate here" };
static const char clientprivate_key[] = { "Paste client key here" };
static const char rootca_certificate[] = { "Paste RootCA certificate here" };
cy_mqtt_t MQTThandle;
void mqtt_event_call_back( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_publish( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &clientcertificate;
credentials.client_cert_size = sizeof( clientcertificate );
credentials.private_key = (const char *) &clientprivate_key;
credentials.private_key_size = sizeof( clientprivate_key );
credentials.root_ca = (const char *) &rootca_certificate;
credentials.root_ca_size = sizeof( rootca_certificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&MQTThandle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
pub_msg.
topic = MQTT_TOPIC;
pub_msg.
payload = MQTT_PUB_MESSAGE;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 7: MQTT disconnect
-This code snippet demonstrates the steps to end an already established MQTT client session with an MQTT broker, and the usage of the cy_mqtt_disconnect() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char MQTT_clientcertificate[] = { };
static const char MQTT_clientprivate_key[] = { };
static const char MQTT_rootca_certificate[] = { };
cy_mqtt_t MQTT_handle;
void mqtt_events_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_disconnect( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
qos = MQTT_QOS2;
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &MQTT_clientcertificate;
credentials.client_cert_size = sizeof( MQTT_clientcertificate );
credentials.private_key = (const char *) &MQTT_clientprivate_key;
credentials.private_key_size = sizeof( MQTT_clientprivate_key );
credentials.root_ca = (const char *) &MQTT_rootca_certificate;
credentials.root_ca_size = sizeof( MQTT_rootca_certificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&MQTT_handle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
+
This code snippet demonstrates the steps to end an already established MQTT client session with an MQTT broker, and the usage of the cy_mqtt_disconnect() API function.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char MQTT_clientcertificate[] = { "Paste client certificate here" };
static const char MQTT_clientprivate_key[] = { "Paste client key here" };
static const char MQTT_rootca_certificate[] = { "Paste RootCA certificate here" };
cy_mqtt_t MQTT_handle;
void mqtt_events_cb( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_disconnect( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
will_msg.
topic = MQTT_TOPIC;
will_msg.
payload = MQTT_WILL_MESSAGE;
credentials.client_cert = (const char *) &MQTT_clientcertificate;
credentials.client_cert_size = sizeof( MQTT_clientcertificate );
credentials.private_key = (const char *) &MQTT_clientprivate_key;
credentials.private_key_size = sizeof( MQTT_clientprivate_key );
credentials.root_ca = (const char *) &MQTT_rootca_certificate;
credentials.root_ca_size = sizeof( MQTT_rootca_certificate );
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&MQTT_handle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 8: MQTT delete and deinit
-This code snippet demonstrates the de-allocation of the configuration structures and resources which are allocated during an MQTT client handle creation, and the usage of the cy_mqtt_delete() and cy_mqtt_deinit() API functions.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char MQTT_client_certificate[] = { };
static const char MQTT_client_key[] = { };
static const char MQTT_root_ca_certificate[] = { };
cy_mqtt_t MQTT_obj_handle;
void mqtt_callback( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_delete( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
credentials.client_cert = (const char *) &MQTT_client_certificate;
credentials.client_cert_size = sizeof( MQTT_client_certificate );
credentials.private_key = (const char *) &MQTT_client_key;
credentials.private_key_size = sizeof( MQTT_client_key );
credentials.root_ca = (const char *) &MQTT_root_ca_certificate;
credentials.root_ca_size = sizeof( MQTT_root_ca_certificate );
security = &credentials;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&MQTT_obj_handle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
mqtthandle = NULL;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
+
This code snippet demonstrates the de-allocation of the configuration structures and resources which are allocated during an MQTT client handle creation, and the usage of the cy_mqtt_delete() and cy_mqtt_deinit() API functions.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define MQTT_QOS2 ( 2U )
#define MQTT_TOPIC "Test_Topic"
#define MQTT_TOPIC_LENGTH ( ( uint16_t ) ( sizeof( MQTT_TOPIC ) - 1 ) )
#define MQTT_WILL_MESSAGE "Will message - World!"
#define MQTT_WILL_MESSAGE_LENGTH ( ( uint16_t ) ( sizeof( MQTT_WILL_MESSAGE ) - 1 ) )
#define NETWORK_BUFFER_SIZE ( 1024U )
static const char MQTT_client_certificate[] = { "Paste client certificate here" };
static const char MQTT_client_key[] = { "Paste client key here" };
static const char MQTT_root_ca_certificate[] = { "Paste RootCA certificate here" };
cy_mqtt_t MQTT_obj_handle;
void mqtt_callback( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
void snippet_cy_mqtt_delete( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
credentials.client_cert = (const char *) &MQTT_client_certificate;
credentials.client_cert_size = sizeof( MQTT_client_certificate );
credentials.private_key = (const char *) &MQTT_client_key;
credentials.private_key_size = sizeof( MQTT_client_key );
credentials.root_ca = (const char *) &MQTT_root_ca_certificate;
credentials.root_ca_size = sizeof( MQTT_root_ca_certificate );
security = &credentials;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&MQTT_obj_handle );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
mqtthandle = NULL;
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
Code Snippet 9: MQTT client on the secured platform CY8CKIT-064S0S2-4343W
This code snippet demonstrates the steps to establish an MQTT client session using the certificates and keys stored in the secured memory.
#define MQTT_BROKER " "
#define MQTT_PORT_SECURED ( 8883 )
#define MQTT_CLIENT_IDENTIFIER "CypressTest"
#define MQTT_CLIENT_IDENTIFIER_LENGTH ( ( uint16_t ) ( sizeof( MQTT_CLIENT_IDENTIFIER ) - 1 ) )
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS ( 60U )
#define NETWORK_BUFFER_SIZE ( 1024U )
cy_mqtt_t mqtthandle_pkcs;
static void mqtt_event_cb_pkcs( cy_mqtt_t mqtt_handle,
cy_mqtt_event_t event,
void *user_data )
{
printf( "\nMQTT App callback with handle : %p \n", mqtt_handle );
(void)user_data;
{
printf( "\nEvent : Received MQTT Disconnect event.\n" );
{
printf( "Reason : MQTT Ping response not received within keep-alive response timeout... \n\n" );
break;
printf( "Reason : Network is disconnected... \n\n" );
break;
printf( "Reason : MQTT packet send or receive operation failed... \n\n" );
break;
printf( "Reason : Bad response from MQTT broker... \n\n" );
break;
default :
printf( "\n Unknown disconnect reason .....\n" );
break;
}
break;
printf( "\nEvent : Received MQTT subscribed message receive event.\n" );
received_msg = &(event.data.pub_msg.received_message);
printf(
"Incoming Publish Topic Name: %.*s\n", received_msg->
topic_len, received_msg->
topic );
printf(
"Incoming Publish Message : %.*s.\n\n", (
int )received_msg->
payload_len, (
const char * )received_msg->
payload );
break;
default :
printf( "\nUNKNOWN EVENT .....\n" );
break;
}
}
#ifdef CY_TFM_PSA_SUPPORTED
static struct ns_mailbox_queue_t ns_mailbox_queue;
static void tfm_ns_multi_core_boot(void)
{
int32_t ret;
if (tfm_ns_wait_for_s_cpu_ready())
{
for (;;)
{
}
}
ret = tfm_ns_mailbox_init(&ns_mailbox_queue);
if (ret != MAILBOX_SUCCESS)
{
for (;;)
{
}
}
}
#endif
void snippet_cy_mqtt_connect_pkcs( void )
{
cy_rslt_t TestRes = CY_RSLT_SUCCESS;
cy_awsport_ssl_credentials_t credentials;
cy_awsport_ssl_credentials_t *security = NULL;
uint8_t *buffer = NULL;
(void)TestRes;
#ifdef CY_TFM_PSA_SUPPORTED
tfm_ns_multi_core_boot();
tfm_ns_interface_init();
#endif
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
buffer = (uint8_t *) malloc( sizeof(uint8_t) * NETWORK_BUFFER_SIZE );
if( buffer == NULL )
{
return;
}
memset( &credentials, 0x00, sizeof( cy_awsport_ssl_credentials_t ) );
credentials.client_cert = NULL;
credentials.client_cert_size = 0;
credentials.private_key = NULL;
credentials.private_key_size = 0;
credentials.root_ca = NULL;
credentials.root_ca_size = 0;
security = &credentials;
connect_info.
client_id = MQTT_CLIENT_IDENTIFIER;
broker_info.
port = MQTT_PORT_SECURED;
security, &broker_info,
&mqtthandle_pkcs );
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
if( TestRes != CY_RSLT_SUCCESS )
{
return;
}
}
diff --git a/include/cy_mqtt_api.h b/include/cy_mqtt_api.h
index a09b611..239c4e9 100644
--- a/include/cy_mqtt_api.h
+++ b/include/cy_mqtt_api.h
@@ -231,14 +231,16 @@ extern "C" {
* Configure value of maximum number of outgoing publishes maintained in MQTT library
* until an ack is received from the broker.
*/
+#ifndef CY_MQTT_MAX_OUTGOING_PUBLISHES
#define CY_MQTT_MAX_OUTGOING_PUBLISHES ( 1U )
-
+#endif
/**
* Configure value of maximum number of outgoing subscription topics maintained in MQTT library
* until an ack is received from the broker.
*/
+#ifndef CY_MQTT_MAX_OUTGOING_SUBSCRIBES
#define CY_MQTT_MAX_OUTGOING_SUBSCRIBES ( 5U )
-
+#endif
/**
* @}
*/
diff --git a/source/cy_mqtt_api.c b/source/cy_mqtt_api.c
index 6397be8..2b0405c 100644
--- a/source/cy_mqtt_api.c
+++ b/source/cy_mqtt_api.c
@@ -371,7 +371,6 @@ static cy_rslt_t mqtt_get_next_free_index_for_publish( cy_mqtt_object_t *mqtt_ob
{
cy_rslt_t result = CY_RSLT_SUCCESS;
uint8_t index = 0;
- bool slot_found = false;
if( (mqtt_obj == NULL) || (pindex == NULL) )
{
@@ -379,6 +378,16 @@ static cy_rslt_t mqtt_get_next_free_index_for_publish( cy_mqtt_object_t *mqtt_ob
return CY_RSLT_MODULE_MQTT_BADARG;
}
+ cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_DEBUG, "\nmqtt_get_next_free_index_for_publish - Acquiring Mutex %p ", mqtt_obj->process_mutex );
+ result = cy_rtos_get_mutex( &(mqtt_obj->process_mutex), CY_RTOS_NEVER_TIMEOUT );
+ if( result != CY_RSLT_SUCCESS )
+ {
+ cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_ERR, "\ncy_rtos_get_mutex for Mutex %p failed with Error : [0x%X] ", mqtt_obj->process_mutex, (unsigned int)result );
+ return result;
+ }
+
+ cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_DEBUG, "\nmqtt_get_next_free_index_for_publish - Acquired Mutex %p ", mqtt_obj->process_mutex );
+
for( index = 0; index < CY_MQTT_MAX_OUTGOING_PUBLISHES; index++ )
{
/* A free index is marked by the invalid packet ID.
@@ -386,17 +395,23 @@ static cy_rslt_t mqtt_get_next_free_index_for_publish( cy_mqtt_object_t *mqtt_ob
if( mqtt_obj->outgoing_pub_packets[ index ].packetid == MQTT_PACKET_ID_INVALID )
{
result = CY_RSLT_SUCCESS;
- slot_found = true;
+ mqtt_obj->outgoing_pub_packets[ index ].packetid = MQTT_GetPacketId( &(mqtt_obj->mqtt_context) );
+ *pindex = index;
break;
}
}
- if( slot_found == true )
+ cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_DEBUG, "\nmqtt_get_next_free_index_for_publish - Releasing Mutex %p ", mqtt_obj->process_mutex );
+ result = cy_rtos_set_mutex( &(mqtt_obj->process_mutex) );
+ if( result != CY_RSLT_SUCCESS )
{
- /* Copy the available index into the output param. */
- *pindex = index;
+ cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_ERR, "\ncy_rtos_set_mutex for Mutex %p failed with Error : [0x%X] ", (unsigned int)result );
+
+ return result;
}
- else
+ cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_DEBUG, "\nmqtt_get_next_free_index_for_publish - Released Mutex %p ", mqtt_obj->process_mutex );
+
+ if( index >= CY_MQTT_MAX_OUTGOING_PUBLISHES )
{
result = CY_RSLT_MODULE_MQTT_ERROR;
}
@@ -1762,7 +1777,6 @@ cy_rslt_t cy_mqtt_publish( cy_mqtt_t mqtt_handle, cy_mqtt_publish_info_t *pubmsg
cy_mqtt_log_msg( CYLF_MIDDLEWARE, CY_LOG_DEBUG, "\ncy_mqtt_publish - Acquired Mutex %p ", mqtt_obj->process_mutex );
/* Get a new packet ID. */
- mqtt_obj->outgoing_pub_packets[ publishIndex ].packetid = MQTT_GetPacketId( &(mqtt_obj->mqtt_context) );
mqtt_obj->pub_ack_status.packetid = mqtt_obj->outgoing_pub_packets[ publishIndex ].packetid;
/* Stop MQTT Ping Timer */
diff --git a/version.xml b/version.xml
index cf26b7b..8303a7c 100644
--- a/version.xml
+++ b/version.xml
@@ -1 +1 @@
-3.2.0.1164
+3.3.0.1179