dmihai03 opened a new pull request, #3618: URL: https://github.com/apache/nuttx-apps/pull/3618
## Summary This PR adds a MQTT-C subscriber client example to examples/mqttc, alongside the existing publisher, and makes it robust enough to run on a real (Wi-Fi) target. The subscriber (mqttc_sub) is built as a separate program, selectable independently from the publisher. Beyond the basic subscribe loop, the change addresses several issues found when running on NuttX hardware: - Automatic reconnect. The client is now set up through mqtt_init_reconnect() with a reconnect_client() callback that rebuilds the transport (TCP + optional TLS), re-runs mqtt_reinit(), and re-issues CONNECT/SUBSCRIBE. This handles both the initial connection and recovery after broker/link errors, retrying with a short back-off instead of aborting. The callback intentionally does not return until a transport connection exists, because mqtt_sync() would otherwise operate on the sentinel socketfd (-1) and crash. - Wait for the network before connecting. wait_for_network() polls the interface (netlib_get_ipv4addr) until it has a usable IPv4 address or a timeout elapses, avoiding the spurious first-attempt ENETUNREACH/connect() failure that occurs when the example starts before Wi-Fi association/DHCP complete. - mbedTLS non-blocking BIO callbacks. Custom mqttc_net_send/mqttc_net_recv wrappers inspect errno directly and return MBEDTLS_ERR_SSL_WANT_READ/WRITE on EAGAIN. The stock mbedtls_net_recv() relies on fcntl(F_GETFL) reporting O_NONBLOCK, which NuttX does not retain (it applies non-blocking mode via FIONBIO), so the stock callback turned a benign EAGAIN into MBEDTLS_ERR_NET_RECV_FAILED. - Bounded message copy. publish_callback() now copies the incoming payload into a fixed-size buffer with an explicit length clamp instead of null-terminating the library's receive buffer in place (a potential out-of-bounds write). - Kconfig cleanup + TLS toggle. Split publisher/subscriber program-name options, added EXAMPLES_MQTTC_SUB to enable the subscriber, and added EXAMPLES_MQTTC_MBEDTLS to enable MQTT-over-TLS (guarded by NETUTILS_MQTTC_WITH_MBEDTLS). Both the CMake and Make build paths were updated accordingly. - Replaced the embedded PolarSSL test CA with the broker's own CA certificate (used when no CA file is supplied via -c). ## Impact - Users: New optional example program mqttc_sub. Disabled by default (EXAMPLES_MQTTC_SUB=n), so existing configurations are unaffected unless explicitly enabled. - Build: Adds Kconfig symbols and build rules in both CMakeLists.txt and Makefile. - Security: TLS support is opt-in via mbedTLS. Note the example embeds a CA certificate in firmware and uses MBEDTLS_SSL_VERIFY_OPTIONAL; the -c option allows supplying a CA at runtime. Users should substitute their own broker CA. ## Testing Host: - OS: Linux 7.0.12-arch1-1 - Compiler: xtensa-esp32-elf-gcc (crosstool-NG esp-12.2.0_20230208) 12.2.0 Target: - Arch / board:config: esp32-devkitc:wifi Steps: 1. Enabled EXAMPLES_MQTTC, EXAMPLES_MQTTC_SUB (and EXAMPLES_MQTTC_MBEDTLS for the TLS run). 2. Built and flashed; ran mqttc_sub -h <broker> -p <port> -t <topic> -q <qos>. 3. Published to the topic from another client and confirmed Received: ... output. 4. Verified reconnect by restarting the broker / dropping Wi-Fi and observing the subscriber re-subscribe automatically Broker logs when subscribing: ``` 1782043828: New connection from 10.42.0.218:5289 on port 1883. 1782043828: New client connected from 10.42.0.218:5289 as auto - FCF3834C- B431 - FCE3 -10 AA -78 D82014BC2E ( p4 ,c1 , k400 ) . 1782043828: No will message specified . 1782043828: Sending CONNACK to auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E (0 , 0) 1782043828: Received SUBSCRIBE from auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E 1782043828: test ( QoS 2) 1782043828: auto - FCF3834C - B431 - FCE3 -10 AA -78 D82014BC2E 0 test 1782043828: Sending SUBACK to auto - FCF3834C - B431 - FCE3 -10 AA -78 D82014BC2E ``` Reconnect ping sent once in 5 minutes: ``` 1782044129: Received PINGREQ from auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E 1782044129: Sending PINGRESP to auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E 1782044430: Received PINGREQ from auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E 1782044430: Sending PINGRESP to auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E 1782044731: Received PINGREQ from auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E 1782044731: Sending PINGRESP to auto - FCF3834C - B431 - FCE3 -10 AA -78D82014BC2E ``` Broker forwarding QoS 2 message: ``` 1782064339: New connection from 10.42.0.156:27670 on port 1883. 1782064339: New client connected from 10.42.0.156:27670 as auto -2D924D93 - E286 -247 E -9118 -2251 D88786D6 ( p4 , c1 , k400 ) . 1782064339: No will message specified . 1782064339: Sending CONNACK to auto -2 D924D93 - E286 -247 E -9118 -2251D88786D6 (0 , 0) 1782064339: Received PUBLISH from auto -2 D924D93 - E286 -247 E -9118 -2251D88786D6 ( d0 , q2 , r0 , m46161 , ’ test ’ , ... (11 bytes ) ) 1782064339: Sending PUBREC to auto -2 D924D93 - E286 -247 E -9118 -2251 D88786D6( m46161 , rc0 ) 1782064339: Received PUBREL from auto -2 D924D93 - E286 -247 E -9118 -2251D88786D6 ( Mid : 46161) 1782064339: Sending PUBLISH to auto -36 E9D2AE -81 C2 -3 C45 -6 EA1 -085D5F124942 ( d0 , q2 , r0 , m2 , ’ test ’ , ... (11 bytes ) ) 1782064339: Sending PUBCOMP to auto -2 D924D93 - E286 -247 E -9118 -2251D88786D6 ( m46161 ) 1782064339: Received PUBREC from auto -36 E9D2AE -81 C2 -3 C45 -6 EA1 -085D5F124942 ( Mid : 2) 1782064339: Sending PUBREL to auto -36 E9D2AE -81 C2 -3 C45 -6 EA1 -085 D5F124942( m2 ) 1782064339: Received PUBCOMP from auto -36 E9D2AE -81 C2 -3 C45 -6 EA1 -085D5F124942 ( Mid : 2 , RC :0) ``` Heap usage: - client without TLS: 14kB - client running TLS: 60kB Flash memory usage (total image size): - client without TLS: 802kB - client running TLS: 1MB -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
