99
1010#include <zephyr.h>
1111#include <stdio.h>
12- #include <net/mqtt_socket .h>
12+ #include <net/mqtt .h>
1313#include <net/socket.h>
1414#include <logging/log.h>
1515#include <misc/util.h>
@@ -90,6 +90,9 @@ static struct nct {
9090 struct mqtt_utf8 dc_rx_endp ;
9191 struct mqtt_utf8 dc_m_endp ;
9292 u32_t message_id ;
93+ u8_t rx_buf [CONFIG_NRF_CLOUD_MQTT_MESSAGE_BUFFER_LEN ];
94+ u8_t tx_buf [CONFIG_NRF_CLOUD_MQTT_MESSAGE_BUFFER_LEN ];
95+ u8_t payload_buf [CONFIG_NRF_CLOUD_MQTT_PAYLOAD_BUFFER_LEN ];
9396} nct ;
9497
9598static const struct mqtt_topic nct_cc_rx_list [] = {
@@ -343,7 +346,7 @@ static int nct_provision(void)
343346 nct .tls_config .cipher_count = 0 ;
344347 nct .tls_config .cipher_list = NULL ;
345348 nct .tls_config .sec_tag_count = ARRAY_SIZE (sec_tag_list );
346- nct .tls_config .seg_tag_list = sec_tag_list ;
349+ nct .tls_config .sec_tag_list = sec_tag_list ;
347350 nct .tls_config .hostname = NRF_CLOUD_HOSTNAME ;
348351
349352#if defined(CONFIG_NRF_CLOUD_PROVISION_CERTIFICATES )
@@ -446,6 +449,10 @@ int nct_mqtt_connect(void)
446449 nct .client .user_name = NULL ;
447450#if defined(CONFIG_MQTT_LIB_TLS )
448451 nct .client .transport .type = MQTT_TRANSPORT_SECURE ;
452+ nct .client .rx_buf = nct .rx_buf ;
453+ nct .client .rx_buf_size = sizeof (nct .rx_buf );
454+ nct .client .tx_buf = nct .tx_buf ;
455+ nct .client .tx_buf_size = sizeof (nct .tx_buf );
449456
450457 struct mqtt_sec_config * tls_config = & nct .client .transport .tls .config ;
451458
@@ -457,6 +464,15 @@ int nct_mqtt_connect(void)
457464 return mqtt_connect (& nct .client );
458465}
459466
467+ static int publish_get_payload (struct mqtt_client * client , size_t length )
468+ {
469+ if (length > sizeof (nct .payload_buf )) {
470+ return - EMSGSIZE ;
471+ }
472+
473+ return mqtt_readall_publish_payload (client , nct .payload_buf , length );
474+ }
475+
460476/* Handle MQTT events. */
461477static void nct_mqtt_evt_handler (struct mqtt_client * const mqtt_client ,
462478 const struct mqtt_evt * _mqtt_evt )
@@ -483,14 +499,24 @@ static void nct_mqtt_evt_handler(struct mqtt_client *const mqtt_client,
483499 p -> message_id ,
484500 p -> message .payload .len );
485501
502+ int err = publish_get_payload (mqtt_client ,
503+ p -> message .payload .len );
504+
505+ if (err < 0 ) {
506+ LOG_ERR ("publish_get_payload: failed %d" , err );
507+ mqtt_disconnect (mqtt_client );
508+ event_notify = false;
509+ break ;
510+ }
511+
486512 /* If the data arrives on one of the subscribed control channel
487513 * topic. Then we notify the same.
488514 */
489515 if (control_channel_topic_match (
490516 NCT_RX_LIST , & p -> message .topic , & cc .opcode )) {
491517
492518 cc .id = p -> message_id ;
493- cc .data .ptr = p -> message . payload . data ;
519+ cc .data .ptr = nct . payload_buf ;
494520 cc .data .len = p -> message .payload .len ;
495521
496522 evt .type = NCT_EVT_CC_RX_DATA ;
@@ -582,12 +608,7 @@ int nct_init(void)
582608 return err ;
583609 }
584610
585- err = nct_provision ();
586- if (err ) {
587- return err ;
588- }
589-
590- return mqtt_init ();
611+ return nct_provision ();
591612}
592613
593614#if defined(CONFIG_NRF_CLOUD_STATIC_IPV4 )
@@ -845,7 +866,7 @@ int nct_disconnect(void)
845866void nct_process (void )
846867{
847868 mqtt_input (& nct .client );
848- mqtt_live ();
869+ mqtt_live (& nct . client );
849870}
850871
851872int nct_socket_get (void )
0 commit comments