This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF9160 MQTT Client error -12 (ENOMEM) when Host url is very long

Hello, 

I'm using the mqtt_simple example with the following changes to add WebSockets Secure Support:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
# Networking
CONFIG_NET_NATIVE=y
# MQTT
CONFIG_MQTT_LIB_TLS=y
CONFIG_MQTT_LIB_WEBSOCKET=y
CONFIG_WEBSOCKET_CLIENT=y
# Logs
CONFIG_NET_LOG=y
CONFIG_MQTT_LOG_LEVEL_DBG=y
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Also I set the Application MQTT topics, clientId, host, port and everything using the CONFIG.

The only change I made in the code was this:

Fullscreen
1
client->transport.type = MQTT_TRANSPORT_SECURE_WEBSOCKET;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The issue is that our Amazon AWS IoT broker url is very long, exactly 1.681 bytes, this is because Amazon give us a url with the token and all the credentials embedded in the url. When I run the code I'm seeing a ENOMEM (-12) when calling the mqtt_connect function.

This is the log:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
*** Booting Zephyr OS build v2.4.0-ncs1 ***
The MQTT simple sample started
Provisioning certificates
LTE Link Connecting ...<CR><LF>
+CEREG: 2,"3174","02D71604",7,0,0,"11100000","11100000"
+CSCON: 1
+CEREG: 1,"3174","02D71604",7,,,"11100000","11100000"
LTE Link Connected!
IPv4 Address found 3.211.121.218
ERROR: mqtt_connect -12
[00:00:03.320,220] <ESC>·[0m<dbg> net_mqtt.client_connect: (0x20020e08): Using transport type 3<ESC>·[0m
[00:00:03.320,587] <ESC>·[0m<dbg> net_mqtt_sock_tls.mqtt_client_tls_connect: (0x20020e08): Created socket 1<ESC>·[0
[00:00:03.321,044] <ESC>·[0m<dbg> net_mqtt_websocket.mqtt_client_websocket_connect: (0x20020e08): mqtt_client_tls_connect: -12<ESC>·[0m
[00:00:03.321,075] <ESC>·[0m<dbg> net_mqtt.mqtt_connect: (0x20020e08): client_connect: -12<ESC>·[0m
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I was able to debug the error to the following function in mqtt_transport_socket_tls.c

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int mqtt_client_tls_connect(struct mqtt_client *client)
{
.
.
.
if (tls_config->hostname) {
ret = setsockopt(client->transport.tls.sock, SOL_TLS,
TLS_HOSTNAME, tls_config->hostname,
strlen(tls_config->hostname));
if (ret < 0) {
goto error;
}
}
.
.
.
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

So my question is how I can somehow increase the memory that the setsockopt function uses when setting the hostname on the socket.

I already tested increasing the main stack and heap memory using the CONFIG with no luck.

Fullscreen
1
2
3
# Memory
CONFIG_MAIN_STACK_SIZE=8192
CONFIG_HEAP_MEM_POOL_SIZE=16384
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Btw I'm using NCS v1.4.0 and modem fw 1.2.3.

Any help will be appreciated.

Thanks!