Transports should close the socket in case of `setsockopt()` failure,
otherwise we end up with a leaked socket, as it won't be closed
elsewhere.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Add an option in MQTT client context to take advantage of the
"TLS_CERT_NOCOPY" option when using TLS socket transport.
Signed-off-by: Lucas Dietrich <ld.adecy@gmail.com>
The condition checks whether the connection was established or not. The
return value should reflect that.
Signed-off-by: Caspar Friedrich <c.s.w.friedrich@gmail.com>
In case zsock_sendmsg did not send all of the data requested, update the
`struct msghdr` content and retry.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Function parameters can be checked without MQTT instance lock being
held. Additionally if NULL parameter would be passed (which this check
tries to handle), then function would return without releasing lock.
Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
Add new custom transport type.
This allows user defined transport for MQTT communication.
The user must implement the transport procedure.
Fixes **#27015**
Signed-off-by: Vlad Tuhut <vlad.tuhut@raptor-technologies.ro>
Add NET_HEXDUMP_DBG/ERR/WARN/INFO macros, then use them for new
MQTT_HEXDUMP_TRC/ERR/WARN/INFO macros.
Log struct mqtt_utf8 using MQTT_HEXDUMP_TRC. One cannot safely log
mqtt_utf8 strings due to no guarantee of a NULL terminator being
present. Also, logging without log_strdup() as if it were a NULL
terminated string asserts when CONFIG_LOG_IMMEDIATE=n. This solves
both issues.
Signed-off-by: Pete Skeggs <peter.skeggs@nordicsemi.no>
In mqtt_keepalive_time_left(), return -1 if keep alive messages are
disabled by setting CONFIG_MQTT_KEEPALIVE=0.
This allows to use mqtt_keepalive_time_left() directly as an input
for poll(). If no keep-alive is expected, -1 would indicate
that poll() can block until new data is available on the socket.
Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
Using zephyr's internals zsock_ calls make mqtt library more compatible,
now it does not depend on NET_SOCKETS_POSIX_NAMES.
Signed-off-by: Jan Pohanka <xhpohanka@gmail.com>
MQTT client state is protected using mutex. That mutex however is
temporarily unlocked when calling event callbacks. This means that in
client_disconnect() transport can already be disconnected, but without
marking it as such in client->internal.state.
When mutex is unlocked in event_notify() function, then there are two
possible paths of failure:
1) First possibility is when RX and TX are called from two separate
threads, so that the other thread gets resumed and functions like
verify_tx_state() (e.g. in mqtt_publish()) allow to continue
communication over disconnected medium.
2) Another possibility is that user calls mqtt_abort() or
mqtt_disconnect() in event handler.
In both cases MQTT library tries to send or receive data, possibly
followed by second close() of underlying file descriptor.
Prevent using disconnected transport by clearing MQTT client state right
after calling mqtt_transport_disconnect(), without releasing mutex, even
for a while.
Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
Currently, the application could receive a duplicate CONNACK event, in
case the server rejected the connection at MQTT level (with an error
code provided with CONNACK message). A subsequent connection close (with
`mqtt_abort` for instance) would produce the duplicate event.
Fix this by reporting back to the MQTT engine, that the connection was
refused, so it can close the connection rightaway. Rework the event
notification logic, so that DISCONNECT event instead of a duplicate
CONNACK event is notified in that case.
Also, prevent the MQTT engine from notyfing DISCONNECT event in case of
socket errors during initial connection phase (i. e. before
`mqtt_connect` function finished).
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Cleansession is hardcoded to 1 but some use case might require 0
to make the sessions persistent (ie get messages sent while the
client was offline)
Signed-off-by: Xavier Naveira <xnaveira@gmail.com>
Identify when received PUBLISH message is malformed and overall packet
length received is smaller than parsed variable header lenght.
Add unit test to cover this case.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Verify more strictly that data read from the transport fits into RX
buffer. Switch to unsigned integers, where possible, to prevent
unnecessary signed/unsigned operations.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
The standard allows up to 4 bytes of packet length data, while current
implementation parsed up to 5 bytes.
Add additional unit test, which verifies that error is reported in case
of invalid packet length.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Contents of mqtt_3_1_0_proto_desc and mqtt_3_1_1_proto_desc were logged
with following code:
MQTT_TRC("Encoding Protocol Description. Str:%s Size:%08x.",
mqtt_proto_desc->utf8, mqtt_proto_desc->size);
This resulted in invalid log, since they were not NULL-terminated
strings. Use MQTT_UTF8_LITERAL() to initialize both utf8 strings to make
sure they are NULL-terminated now and valid to print and
log. Additionally this makes the code a bit shorter.
Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
There are scenarios where there is a NAT firewall in between MQTT
client and server. In such case, the NAT TCP timeout may be shorter
than MQTT keepalive timeout and TCP timeout. The the MQTT ping
request message is dropped by the NAT firewall, so that it cannot be
received by the server, resulting in void MQTT ping response message.
There is no TCP FIN or RST at all. The application looks hang-up
until TCP timeout happens on the client side, which may take too
long.
Therefore, the event MQTT_EVT_PINGRESP is added to inform the
application that the route between client and server is still valid.
Signed-off-by: PK Chan <pak.kee.chan@nordicsemi.no>
Add new transport handler for MQTT, with sendmsg-like functionality.
This allows TCP transport to send PUBLISH packets w/o fragmentation at
the TCP layer. Implement this new functionality for all existing
transports.
Fixes#22679
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
`mqtt_transport_write` failue was logged with `errno` value which is not
correct as the return value from the function is valid in this case.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Users of mqtt_live() have no idea when it actually sends a ping.
As a result it's very hard to know when to use mqtt_input() to
process the incoming PINGACK.
Instead of returning a 0 result when a ping isn't generated in
mqtt_live(), let's return -EAGAIN.
Signed-off-by: Michael Scott <mike@foundries.io>
The MQTT connection is closed in case an mqtt_ping fails anyway, so
it's better to let the application know early that something went
wrong.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Add function that returns remaining time until next keep alive message
shall be sent. Such function could be used for instance as a source
for `poll` timeout.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
After sending the MQTT disconnect message, no response is expected,
therefore it makes little sense to delay the socket closure.
So far it was expected to call `mqtt_input` function after calling
`mqtt_disconnect` in order to close the socket, which is
counter-intuitive. Simplify this, by closing the socket rightaway
in the `mqtt_disconnect` function.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Maintain a simple count of how many PINGREQ have been sent for the
current connection that have not had a corresponding PINGRESP. Nothing
is done with this information internal to the MQTT driver, but it is
exposed to the application layer to monitor as desired.
Signed-off-by: Justin Brzozoski <justin.brzozoski@signal-fire.com>
Remove leading/trailing blank lines in .c, .h, .py, .rst, .yml, and
.yaml files.
Will avoid failures with the new CI test in
https://github.com/zephyrproject-rtos/ci-tools/pull/112, though it only
checks changed files.
Move the 'target-notes' target in boards/xtensa/odroid_go/doc/index.rst
to get rid of the trailing blank line there. It was probably misplaced.
Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
Use this short header style in all Kconfig files:
# <description>
# <copyright>
# <license>
...
Also change all <description>s from
# Kconfig[.extension] - Foo-related options
to just
# Foo-related options
It's clear enough that it's about Kconfig.
The <description> cleanup was done with this command, along with some
manual cleanup (big letter at the start, etc.)
git ls-files '*Kconfig*' | \
xargs sed -i -E '1 s/#\s*Kconfig[\w.-]*\s*-\s*/# /'
Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
The MQTT transport API functions are already documented in
mqtt_transport.h so need to duplicate them in individual
transport .c file.
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Current SOCKS5 based connections in mqtt are only
TCP (nonsecure) based. To support TLS based SOCKS5
connections, new methods needs to be introduced.
Instead, removed CONFIG_MQTT_LIB_SOCKS based implementation.
And now mqtt provides an api to set proxy
(mqtt_client_set_proxy()) details. That's enough,
socket layer will take care of making connections through
proxy server.
Fixes: #17037
Signed-off-by: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
This change will allow an MQTT client to override the compile-time
keepalive if desired. The change is structured such that the
compile-time default will still be setup by calling mqtt_client_init,
but can be changed by the application before calling mqtt_connect if
desired.
Signed-off-by: Justin Brzozoski <justin.brzozoski@signal-fire.com>
move misc/mutex.h to sys/mutex.h and
create a shim for backward-compatibility.
No functional changes to the headers.
A warning in the shim can be controlled with CONFIG_COMPAT_INCLUDES.
Related to #16539
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This function uses mqtt_read_publish_payload_blocking to perform a
blocking read of the specified number of bytes.
When reading out a payload, the normal use case is to read the
entire payload. This function facilitates that use case.
Signed-off-by: Håkon Øye Amundsen <haakon.amundsen@nordicsemi.no>
It is convenient to have a blocking version of
`mqtt_read_publish_payload` function, for cases when it is called from
the event handler. Therefore, extend the 'mqtt_read_publish_payload'
argument list with information whether the call should block or not.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
Update the files which contain no license information with the
'Apache-2.0' SPDX license identifier. Many source files in the tree are
missing licensing information, which makes it harder for compliance
tools to determine the correct license.
By default all files without license information are under the default
license of Zephyr, which is Apache version 2.
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commits adds a new MQTT transport. The purpose is to be able to
connect to a MQTT broker through a SOCKS5 proxy.
Signed-off-by: Tomasz Gorochowik <tgorochowik@antmicro.com>
As the legacy library has been removed, we no longer need to
differentiate betwen MQTT implementations. Therefore align the library
folder name with other libraries and remove the `_sock` suffix.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
The old and deprecated net-app based MQTT library is removed.
See the BSD socket based MQTT library for a replacement.
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Remove network specific default and max log level setting
and start to use the zephyr logging values for those.
Remove LOG_MODULE_REGISTER() from net_core.h and place the
calls into .c files. This is done in order to avoid weird
compiler errors in some cases and to make the code look similar
as other subsystems.
Fixes#11343Fixes#11659
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
Rename existing headers and sybols to mqtt_legacy, to allow new
implementation to keep old config and header names.
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
The return of memset is never checked. This patch explicitly ignore
the return to avoid MISRA-C violations.
The only directory excluded directory was ext/* since it contains
only imported code.
Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
There have a funtion mqtt_rx_unsuback defined but not used.
So add it into mqtt_parser and fix the missing case.
Fixes#8431
Signed-off-by: Xuan Ze <119524428@qq.com>
Bool symbols implicitly default to 'n'.
A 'default n' can make sense e.g. in a Kconfig.defconfig file, if you
want to override a 'default y' on the base definition of the symbol. It
isn't used like that on any of these symbols though.
Remove some 'default ""' properties on string symbols too.
Also make definitions more consistent by converting some
config FOO
<type>
prompt "foo"
definitions to a shorter form:
config FOO
<type> "foo"
This shorthand works for int/hex/string symbols too, not just for bool
symbols.
Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>