Skip to content

Commit 22ddf3d

Browse files
rluboslemrey
authored andcommitted
samples: nrf_cloud: switch to Zephyr MQTT
As MQTT library is now available in Zephyr upstream, align nrf_cloud library to use it instead. Signed-off-by: Robert Lubos <[email protected]>
1 parent 11f9078 commit 22ddf3d

File tree

7 files changed

+45
-30
lines changed

7 files changed

+45
-30
lines changed

applications/asset_tracker/prj.conf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ CONFIG_NET_NATIVE=n
1818
CONFIG_NET_SOCKETS=y
1919
CONFIG_NET_SOCKETS_OFFLOAD=y
2020

21-
# MQTT
22-
CONFIG_MQTT_SOCKET_LIB=y
23-
CONFIG_MQTT_LIB_TLS=y
24-
CONFIG_MQTT_MAX_PACKET_LENGTH=2048
25-
2621
# LTE link control
2722
CONFIG_POWER_OPTIMIZATION_ENABLE=n
2823
CONFIG_LTE_LINK_CONTROL=y

applications/asset_tracker/prj_nrf9160_pca20035ns.conf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ CONFIG_NET_NATIVE=n
1818
CONFIG_NET_SOCKETS=y
1919
CONFIG_NET_SOCKETS_OFFLOAD=y
2020

21-
# MQTT
22-
CONFIG_MQTT_SOCKET_LIB=y
23-
CONFIG_MQTT_LIB_TLS=y
24-
CONFIG_MQTT_MAX_PACKET_LENGTH=2048
25-
2621
# LTE link control
2722
CONFIG_POWER_OPTIMIZATION_ENABLE=n
2823
CONFIG_LTE_LINK_CONTROL=y

applications/asset_tracker/prj_qemu_x86.conf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ CONFIG_CONSOLE_SUBSYS=y
3333
CONFIG_CONSOLE_HANDLER=y
3434
CONFIG_CONSOLE_GETCHAR=y
3535

36-
CONFIG_MQTT_SOCKET_LIB=y
37-
CONFIG_MQTT_MAX_PACKET_LENGTH=2048
38-
CONFIG_MQTT_LIB_TLS=y
3936
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
4037

4138
# TLS configuration

include/net/mqtt_socket.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ struct mqtt_sec_config {
320320
u32_t sec_tag_count;
321321

322322
/** Indicates the list of security tags to be used for the session. */
323-
sec_tag_t *seg_tag_list;
323+
sec_tag_t *sec_tag_list;
324324

325325
/** Peer hostname for ceritificate verification.
326326
* May be NULL to skip hostname verification.

samples/nrf9160/lte_ble_gateway/prj.conf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ CONFIG_NET_NATIVE=n
1515
CONFIG_NET_SOCKETS=y
1616
CONFIG_NET_SOCKETS_OFFLOAD=y
1717

18-
# MQTT
19-
CONFIG_MQTT_SOCKET_LIB=y
20-
CONFIG_MQTT_LIB_TLS=y
21-
CONFIG_MQTT_MAX_PACKET_LENGTH=2048
22-
2318
# LTE link control
2419
CONFIG_LTE_LINK_CONTROL=y
2520
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n

subsys/net/lib/nrf_cloud/Kconfig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
menuconfig NRF_CLOUD
77
bool "nRF Cloud library"
88
select CJSON_LIB
9-
select MQTT_SOCKET_LIB
9+
select MQTT_LIB
10+
select MQTT_LIB_TLS
1011

1112
if NRF_CLOUD
1213

@@ -45,6 +46,17 @@ config NRF_CLOUD_PORT
4546
config NRF_CLOUD_IPV6
4647
bool "Configure nRF Cloud library to use IPv6 addressing. Otherwise IPv4 is used."
4748

49+
config NRF_CLOUD_MQTT_MESSAGE_BUFFER_LEN
50+
int "Size of the buffer for MQTT library."
51+
default 256
52+
help
53+
Specifies maximum message size can be transmitted/received through
54+
MQTT (exluding MQTT PUBLISH payload).
55+
56+
config NRF_CLOUD_MQTT_PAYLOAD_BUFFER_LEN
57+
int "Size of the buffer for MQTT PUBLISH payload."
58+
default 2048
59+
4860
module=NRF_CLOUD
4961
module-dep=LOG
5062
module-str=Log level for nRF Cloud

subsys/net/lib/nrf_cloud/src/nrf_cloud_transport.c

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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

9598
static 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. */
461477
static 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)
845866
void nct_process(void)
846867
{
847868
mqtt_input(&nct.client);
848-
mqtt_live();
869+
mqtt_live(&nct.client);
849870
}
850871

851872
int nct_socket_get(void)

0 commit comments

Comments
 (0)