Commit Graph

67 Commits

Author SHA1 Message Date
Johan Ström b91368594d
MQTT: trigger conn failure callback if DNS success but connection failed (#3477) 2021-12-06 13:38:48 +01:00
Nathaniel Wesley Filardo c695a451ee
First round of MQTT fixes (#3360)
* mqtt: remove concept of connection timeout

Just rely on the network stack to tell us when things have gone south.

* mqtt: remove write-only mqtt_state.port field

* mqtt: drop useless conditional

* mqtt: decouple message sent flag from timer

* mqtt: reconnect callback does not need to hang up

The network stack has certainly done that for us at this point.
Similarly, since we're about to call mqtt_socket_disconnected, don't
bother unregistering the timer here, either.

* mqtt: don't tick once per second

Set the timer for the duration of the wait and cancel it on the other side.

* mqtt: defer message queue destruction to _disconnect

We're going to want to publish a disconnect message for real, so doing
this in _close does no one any favors

* mqtt: miscellaneous cleanups

No functional change intended

* mqtt: close() should send disconnect message for real

This means waiting for _sent() to fire again before telling the network
stack to disconnect.

* mqtt: tidy connect and dns

- Push the self-ref to after all allocations and error returns

- Don't try to extract IPv4 from the domain string ourselves, let the
  resolver, since it can

- Don't try to connect to localhost.  That can't possibly work.

* mqtt: common up some callback invocations

* mqtt: don't retransmit messages on timeout

There's no point in retransmitting messages on timeout; the network
stack will be trying to do it for us anyway.

* mqtt: remove unnecessary NULL udata checks

* mqtt: hold strings in Lua, not C

Eliminates a host of C-side allocations.

While here, move the rest of the mqtt_connect_info structure out to its
own thing, and pack some flags using a bitfield.

* mqtt: mqtt_socket_on use lua_checkoption

* mqtt: slightly augment debug messages

These changes have made some debugging ever so slightly easier.
2021-01-05 12:07:09 +01:00
Caleb Mingle e547c2a094 mqtt: fix connfail callback
I've not been able to get the mqtt `connfail` callback to work.

I'm consistently receiving `method not supported` errors:
```
application.lua:53: method not supported
stack traceback:
        [C]: in function 'on'
        application.lua:53: in main chunk
        [C]: in function 'dofile'
        init.lua:18: in function <init.lua:6>
```

Example code:
```
function on_connection_failed(client, reason)
    print("mqtt connection failed: " .. reason)
end

m:on("connfail", on_connection_failed)
```

I believed this to be caused by the incorrect length comparison for `connfail`
that is updated here.

Once I changed that, the error went away, however the callback was never called.

I believe the callback was never called because of an incorrect assignment.

However, I saw this somewhat confusing description in the docs so this
assignment may be expected?
> The second (failure) callback aliases with the "connfail" callback available through :on(). (The "offline" callback is only called after an already established connection becomes closed. If the connect() call fails to establish a connection, the callback passed to :connect() is called and nothing else.)
2020-08-11 17:20:17 +01:00
Datong Sun 95f5191cd3 Fixed an regression that MQTT client timer is disarmed prematurely when
connecting to server.

Inside af426d0315, the `mqtt_socket_timer`
function was modified so that instead of checking the presense of
allocated `mud->pesp_conn` structure, `mud->connected` field was used
on determining if the timer need to be disarmed.

However, this is not entirely correct. If the TCP socket is actively
connecting and haven't timed out yet, then `mud->connected` is also
`false` and the timer will think the connection is broken and
disarms itself. This has two consequences:

* The connection timeout counter is no longer decremented and checked
* After connection succeeds, keepalive heartbeat is no longer being
  sent (#3166). This is particularly noticeable in MQTT over TLS
  connections, because those usually takes longer than 1 second
  to finish and the timer would had chance to execute before connection
  is established

This commit checks the presense of `pesp_conn->proto.tcp` pointer
instead, which was allocated in the same place as the (old) `pesp_conn`
struct, and according to my test indeed fixes the above issue.
2020-06-30 13:20:17 +01:00
Terry Ellison bbeb09b695 Squashed updates do get Lua51 and Lua53 working (#3075)
-  Lots of minor but nasty bugfixes to get all tests to run clean
-  core lua and test suite fixes to allow luac -F to run cleanly against test suite
-  next tranch to get LFS working
-  luac.cross -a options plus fixes from feedback
-  UART fixes and lua.c merge
-  commit of wip prior to rebaselining against current dev
-  more tweaks
2020-06-09 22:26:52 +02:00
Nathaniel Wesley Filardo c8c90fe4e1 app/modules: prune some unnecessary metamethods 2020-06-09 22:26:52 +02:00
Nathaniel Wesley Filardo bf478e0cc4 mqtt: drop LUA_GCSTOP/GCRESTART dance 2020-06-09 22:26:52 +02:00
Nathaniel Wesley Filardo 9f8c2aea6f mqtt: remove dead store 2020-06-09 22:26:52 +02:00
Nathaniel Wesley Filardo af426d0315 Networking rampage and accumulated fixes (#3060)
* espconn: remove unused espconn code, take 1

This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 .
It removes a bunch of functions that were never called in our tree.

* espconn: De-orbit espconn_gethostbyname

Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004

While here, remove `mqtt`'s charming DNS-retry logic (which is neither
shared with nor duplicated in other modules) and update its :connect()
return value behavior and documentation.

* espconn: remove scary global pktinfo

A write-only global!  How about that.

* net: remove deprecated methods

All the TLS stuff moved over there a long time ago, and
net_createUDPSocket should just do what it says on the tin.

* espconn_secure: remove ESPCONN_SERVER support

We can barely function as a TLS client; being a TLS server seems like a
real stretch.  This code was never called from Lua anyway.

* espconn_secure: more code removal

* espconn_secure: simplify ssl options structure

There is nothing "ssl_packet" about this structure.  Get rid of the
terrifying "pbuffer" pointer.

Squash two structure types together and eliminate an unused field.

* espconn_secure: refactor mbedtls_msg_info_load

Split out espconn_mbedtls_parse, which we can use as part of our effort
towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032

* espconn_secure: introduce TLS cert/key callbacks

The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032
Subsequent work will remove the old mechanism.

* tls: add deprecation warnings

* luacheck: net.ifinfo is a thing now

* tls: remove use of espconn->reverse

* mqtt: stop using espconn->reverse

Instead, just place the espconn structure itself at the top of the user
data.  This enlarges the structure somewhat but removes one more layer
of dynamic heap usage and NULL checks.

While here, simplify the code a bit.

* mqtt: remove redundant pointer to connect_info

Everywhere we have the mqtt_state_t we also have the lmqtt_userdata.

* mqtt: doc fixes

* mqtt: note bug

* tls: allow :on(...,nil) to unregister a callback
2020-06-09 22:26:52 +02:00
Nathaniel Wesley Filardo 30f706fb03 WIP: MQTT fixes (#2986)
* mqtt: expose "connfail" callback via :on()

This makes it just like all the other callbacks in the module and is a
revision of behavior called out in
https://github.com/nodemcu/nodemcu-firmware/pull/2967

* mqtt: clarify when puback callback fires

* mqtt: Don't reference stack buffers from the heap

The confusingly-named "mqtt_connection_t" object is just a triple of
  - a serialized mqtt message pointer and length
  - a buffer pointer (to which the above can be written)
  - a message identifier

The last of these must be passed around the mqtt state machine, but the
first two are very local and the buffer is always sourced from the C
stack.  Unfortunately, because the entire structure is persisted in the
heap, some callers assume that they can always use the structure without
reinitialization (see mqtt_socket_close), which will trash the C stack.

Sever the pairing between message id and local state, punt the local
state entirely out of the heap, and rename things to be less confusing.
2020-06-09 22:26:52 +02:00
Terry Ellison 98c2c0520d
Dev make cleanup (#2842) 2019-07-23 18:47:18 +03:00
Johny Mattsson f7a545b951
Evict c_types.h, tidy up a other c_prefixes. (#2841) 2019-07-23 14:22:38 +10:00
Johny Mattsson 526d21dab4 Major cleanup - c_whatever is finally history. (#2838)
The PR removed the bulk of non-newlib headers from the NodeMCU source base.  
app/libc has now been cut down to the bare minimum overrides to shadow the 
corresponding functions in the SDK's libc. The old c_xyz.h headerfiles have been 
nuked in favour of the standard <xyz.h> headers, with a few exceptions over in 
sdk-overrides. Again, shipping a libc.a without headers is a terrible thing to do. We're 
still living on a prayer that libc was configured the same was as a default-configured
xtensa gcc toolchain assumes it is. That part I cannot do anything about, unfortunately, 
but it's no worse than it has been before.

This enables our source files to compile successfully using the standard header files, 
and use the typical malloc()/calloc()/realloc()/free(), the strwhatever()s and 
memwhatever()s. These end up, through macro and linker magic, mapped to the 
appropriate SDK or ROM functions.
2019-07-22 00:58:21 +03:00
Nathaniel Wesley Filardo 9f8b74debd MQTT tweaks (#2822)
* mqtt:connect() secure parameter should be boolean

Continue to honor the old 0/1 values, but make them undocumented and add
a deprecation warning to the code and docs.  Eventually, this should go
away.

* mqtt: rip out deprecated autoreconnect

* mqtt: expose all the callbacks via :on
2019-07-16 11:30:41 +02:00
Terry Ellison 1990f95740
Updated ROTables 1st tranch (#2742)
Updated ROTables
2019-05-08 14:08:20 +03:00
Nathaniel Wesley Filardo 30ff0a1620 Raise MQTT_CONNECT_TIMEOUT (#2674)
Should fix https://github.com/nodemcu/nodemcu-firmware/issues/2576
2019-02-19 22:33:25 +01:00
Arnim Läuger d7da14d69e MQTT: print deprecation message regardless of autoreconnect value (#2668) 2019-02-14 23:32:16 +01:00
Johan Ström 2d958750b5 Handle large/chunked/fragmented MQTT messages properly (#2571)
* MQTT: handle large/chunked/fragmented messages properly

If a message spans multiple TCP packets it must be buffered before
delivered to LUA. Prior code did not do this at all, so this "patch"
really adds proper handling of fragmented MQTT packets.
This could also occur if multiple small messages was sent in a
single TCP packet, and the last message did not completely fit in that
packet.

Introduces a new option to the mqtt.Client constructor:
max_publish_length which defaults to 1024

Introduces a new 'overflow' callback.

Fixes issue #2308 and proper fix for PR #2544.

* mqtt.md: clarified heap allocation

* mqtt: ensure ack is sent for overflowed publish

If QoS is used we should still acknowledge that we received it, or server might retransmit it later.
2018-11-30 22:12:46 +01:00
Johan Ström 33613be550 Increase MQTT recv buffer size to support MTU 1500 (#2308) (#2544)
Any TCP packet with more than 1024 bytes of payload was silently
dropped. With MTU of 1500 the TCP payload can be up to 1460 bytes
(1500 - 20(IP hdr) - 20(TCP hdr))
2018-11-13 23:43:24 +01:00
dnc40085 96e5c026a8 Refactor timer suspend portion of node.sleep (pmsleep) (#2287)
* pmsleep refactor
* Shortened swtmr disabled message 
* Added swtimer debug module option to user_modules.h.
* Added comments to user_config.h.
* Fixed error in documentation for node.sleep()
* remove blank sntp.c that got added in during rebase onto dev(6218b92)
* Added #ifdefs around SWTIMER_REG_CB to prevent inclusion of disabled
code
2018-04-13 21:41:14 +02:00
Frank Exoo e9ed1a8c59 Disable MQTT connect error callback after successful connection (#2262)
Disable the error callback of mqtt.client:connect() after a successful connection.
This will prevent this function to be called after a future disconnection.
Instead the "offline" function is called.
2018-02-13 09:25:20 +01:00
Frank Exoo ba36cb3d2d MQTT re-connect issue (#2256)
Fix to start sending PINGREQ after a reconnect.
2018-02-09 08:13:24 +01:00
Marcel Stör b645100d28 Add autoconnect deprecation warning to MQTT code 2017-04-04 22:52:51 +02:00
Philip Gladstone 66ffa6cdc4 Fix the error callback from not being called sometimes (#1683)
* Fix the error callback from not being called sometimes
* Moved the setting of the reconnect status to after the connack is recevied
* Increase the irom0_seg size
* Updated the documentation
* Make it clearer that autoreconnect is deprecated
2017-04-04 22:22:04 +02:00
Philip Gladstone b74a9dbdf7 Make the MQTT PING functionality work better. (#1557)
Deal with flow control stopped case
2016-11-08 21:00:17 +01:00
Yury Popov be263dacde Set espconn's secure buffer size once at initialization (#1466)
Fixes #1457
2016-09-04 22:45:46 +02:00
Yury Popov 3c55e8c026 MQTT memory / connection issues fix (#1445) 2016-08-14 10:32:11 +02:00
Arnim Läuger 2bb6e407a3 Initialize cb for unsubscribe and unref it upon socket deletion. (#1305)
Thank you.
2016-05-21 08:25:16 -04:00
philip 501ef9d566 Fix bugs with mqtt connection error handling 2016-05-09 22:04:23 -04:00
philip ab950ff518 Removed useless checks before calling luaL_unref 2016-03-25 23:35:07 -04:00
philip 426e805c43 Initial version of unsubscribe 2016-03-20 18:44:06 -04:00
jfollas 92eb21cd02 Merge branch 'dev' into mqtt-connack
# Conflicts:
#	app/modules/mqtt.c
2016-03-17 20:22:12 -04:00
philip d3fe213fbe Fix bug with PINGREQ not being sent on a timeout.
Remove the offending code
2016-03-16 08:13:17 -04:00
philip 8f355d5d5f Fix problem with subscribing to multiple topics 2016-03-09 22:45:20 -05:00
philip dd6359e7db Try to make qos~=0 work better
Added send_if_possible to clean up the code
2016-03-09 21:57:27 -05:00
jfollas aa50eca4f6 Refactoring of MQTT module to consolidate duplicate code into a function
- per @pjsg's suggestion
2016-03-06 20:11:16 -05:00
jfollas 0abe2fe964 MQTT Client - CONNACK processing
- Process the CONNACK message received from the broker after Connect
- Provide feedback to Lua via failure callback on client:connect()
- Also provide failure information for other situations not covered by CONNACK
2016-03-06 16:33:57 -05:00
Marcel Stör 2c687313de Merge pull request #1002 from pjsg/mqtt
Fix memory exhaustion in mqtt under circumstances from issue #975
2016-02-10 12:58:26 +01:00
Uri Shaked 31a62a9eb1 Fix secure MQTT connections
Call `espconn_secure_set_size()` before calling `espconn_secure_connect()`, similar to how the http module works
2016-02-07 18:11:30 +02:00
philip c9cf02ba31 Send any queued packets when possible 2016-02-01 22:29:32 -05:00
devsaurus 1c32d20502 improve error checking in mqtt module
connect(), close(), subscribe(), publish()
2016-01-28 21:36:38 +01:00
Johny Mattsson 182d45f928 Enable -std=gnu11 for modules dir.
Plus boat-load of fixes to actually make that build.
2016-01-20 19:40:47 +11:00
Johny Mattsson 4e8ef87d03 Reworked module registration, removed modules.h
Module creation & registration now made a lot simpler. In essence,
each module file is now self-contained and only needs a

  NODEMCU_MODULE(MYNAME, "myname", myname_map, luaopen_myname);

line to both be automatically recognised by the Lua initialization
as well as honor the LUA_USE_MODULES_MYNAME #define.
2015-12-16 16:09:16 +11:00
TerryE b773290b8c Major cleanup of module registration pass 2.
carrying on Johny's edits as per my comments on #810
2015-12-16 11:21:40 +11:00
Johny Mattsson 6b6456be47 Major cleanup of module registration.
As per #810 & #796, only LUA_OPTIMIZE_MEMORY=2 & MIN_OPT_LEVEL=2 are
supported when building. This commit effects that limitation.

With this change modules/auxmods.h no longer needs to be updated for
every new module, nor do module writers need to cater for a hypothetical
LUA_OPTIMIZE_MEMORY < 2 scenario.
2015-12-12 15:03:40 +11:00
Johny Mattsson dabfe83a7d Unbreak build from #735 merge.
Now with more #ifdef.
2015-11-10 18:48:39 +11:00
Antoine b2167bdf3a mqtt:close() send the DISCONNECT message (fix #673) 2015-11-06 07:32:04 -07:00
Jens Jensen c39d317b2b Fix mqtt connection closing when not publishing (fixes #359)
should not reset keep_alive_tick when receiving as this can prevent
sending keepalive (PINGREQ), which can result in server disconnect.
2015-11-05 20:21:53 -07:00
kvakanet e2dd98ba10 Update mqtt.c fix for #679
Patch for  "mqtt LWT qos and retain parameters are parsed incorrectly #679"
2015-11-02 20:14:43 -07:00
Jonathan karras 57c2e6eacd Fix for MQTT PUBACK loop
Conflicts:
	app/modules/mqtt.c
2015-10-29 20:02:08 -06:00