Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Long wait on MQTT Connect #11

Closed
sblancodiez opened this issue Dec 28, 2017 · 8 comments
Closed

Long wait on MQTT Connect #11

sblancodiez opened this issue Dec 28, 2017 · 8 comments

Comments

@sblancodiez
Copy link

sblancodiez commented Dec 28, 2017

Hello,

I am playing with Amazon FreeRTOS and when I connect to the broker I observe a delay equal to the timeout value (something like 12 or 14 seconds) before the ACK is processed.

I think the problem is that after sending the connection message the prvManageConnections function does not get anything in the socket read and then sets the next timeout to the full timeout. I think it should check the socket often before waiting for the full time.

Maybe it is a naive fix but I changed in MQTT_Periodic the following:

/* If this Tx operation has not timed out yet, update when the next earliest timeout will happen. */
ulNextTimeoutTicks = mqttMIN( ulNextTimeoutTicks, mqttbufferGET_PACKET_TIMEOUT_TICKS( xBuffer ) );

to the following (including the FreeRTOS header):

/* If this Tx operation has not timed out yet, update when the next earliest timeout will happen. */
ulNextTimeoutTicks = mqttMIN( ulNextTimeoutTicks, 300 / portTICK_PERIOD_MS );

This way, it will check every 300ms for data in the socket instead of waiting.

Is this correct?

@RichardBarry
Copy link
Contributor

Hi - thanks for the information. We will have a look and report back. Which chip/board are you using?

@sblancodiez
Copy link
Author

Hi,

Custom board with an Infineon XMC4500, ethernet connection.

@alexa-noxon
Copy link
Contributor

Thanks for the update.

Are you using v1.0.1 of aws_mqtt_agent.c/aws_mqtt_lib.c? v1.0.1 is the most recent and was uploaded about a week and a half ago. You can find the file version at the top of the file above the license agreement.

Have you ported configPRINTF? If so, could you share the log outputs?

@dcgaws
Copy link
Contributor

dcgaws commented Jan 2, 2018

sblancodiez, happy new year! The behavior you're describing also reminds me of a common gotcha that occurs when your device has an otherwise valid AWS IoT credential, but the associated policy in the AWS IoT "Thing" registry is incorrect or incomplete. The resulting behavior in that case is that the device will connect TCP, negotiate TLS, but then immediately be disconnected by the server. Essentially, the authentication succeeds, but the subsequent authorization check fails.

If that sounds like possibly what you're observing, can you please review this forum thread? In summary, that thread describes how to use the AWS CLI to double-check "Thing" credential and policy configuration.

@sblancodiez
Copy link
Author

Hi,

Right now I can't run it for logging, I will do that when possible.

I used both 1.0.0 and 1.0.1 with the same results. The issue is that if when the MQTT task does the first non-blocking recv it does not get data, it sleeps the full timeout value before trying recv again. Since the first recv is called very fast after sending the connect command, and it has a long timeout, you get a very long and not needed wait.

I am not using AWS, I am testing against a Mosquitto broker in my local network. Also, I am not using the demo exactly as is; I am using TLS so obviously there is a parameter change in the setup part.

Best regards,

@dcgaws
Copy link
Contributor

dcgaws commented Jan 12, 2018

Hello, here's an example of how I tested my own instance of Mosquitto running on the internet. I tested the connection using the Mosquitto pub/sub command-line applications in order to confirm basic routing and connectivity. Then I transferred those settings to my Amazon FreeRTOS device.

Start by running the server. I'm using the Windows version and requiring TLS. However, unlike the AWS IoT broker, I'm not requiring TLS certificate client authentication.

mosquitto.exe -p 443 -v

From the same network that the device is on (i.e., if other than where your server is running), start a subscriber:

mosquitto_sub.exe -h xyz.us-west-2.compute.amazonaws.com -p 443 -q 0 -t string/StringTopic

Next, publish a message on the same topic from a different client command window:

mosquitto_pub.exe -h xyz.us-west-2.compute.amazonaws.com -p 443 -q 0 -t string/StringTopic -m hi

Confirm that the message you published is received by your subscriber instance and printed to its console output.

Once that's working, transfer the settings to the appropriate code locations in your Amazon FreeRTOS project. For example, the DNS name (or IP address) of your MQTT broker should be assigned to clientcredentialMQTT_BROKER_ENDPOINT in aws_clientcredential.h. In that same file, the port number that your MQTT broker is listening on should be assigned to clientcredentialMQTT_BROKER_PORT.

In aws_hello_world.c, ensure that echoCLIENT_ID is unique to your environment. That is, no two MQTT clients should be using the same ID.

Ensure that the sixth parameter of your MQTTAgentConnectParams_t is set to pdTRUE if you successfully tested your broker using TLS. Set that field to pdFALSE if you successfully tested your broker with TCP/IP without TLS.

Likewise, if you are using TLS, the last two parameters of MQTTAgentConnectParams_t must be your encoded server certificate and its size, respectively. By default, when using TLS, our client libraries enforce server certificate trust verification.

@sblancodiez
Copy link
Author

Yes, all that was already accounted for and it is working. The issue and possible solution is slow connection because how timeouts are managed. Since I did not write the library I just wanted to share that and ask if there is a negative consequence on the change I proposed, but without change it works although I get around 12 seconds of wait in the connect phase and with my change I get no appreciable wait (300ms as written).

@aggarg
Copy link
Member

aggarg commented Jan 17, 2018

In order to ensure that the MQTT agent wakes up frequently to check if any data is received on the connected socket, you should define the macro mqttconfigMQTT_TASK_MAX_BLOCK_TICKS (in aws_mqtt_agent_config.h) to a smaller value. Note that the frequent wake up of the MQTT agent to check the received data on the connected socket can be avoided if the board has a capability to inform the MCU (probably some interrupt) about the data received on a connected socket.

alexa-noxon added a commit that referenced this issue Nov 27, 2018
In release 1.4.3, PKCS #11 objects switched from being
accessed by file name to being accessed by label name.

As a result, pkcs11configFILE_NAME_* configs are now a property
of the portable PAL layer and are unused in the config file.
alexa-noxon added a commit that referenced this issue Nov 27, 2018
In release 1.4.3, PKCS #11 objects switched from being
accessed by file name to being accessed by label name.

As a result, pkcs11configFILE_NAME_* configs are now a property
of the portable PAL layer and are unused in the config file.
alexa-noxon added a commit that referenced this issue Nov 28, 2018
PKCS #11 types may follow the format CK_TYPE_PTR or CK_TYPE_PTR_PTR.
This change updates the style checker to recognize _PTR and _PTR_PTR
line endings as pointer types.

This change also allows variables to start with a number.
aggarg added a commit that referenced this issue Nov 29, 2018
Modify style checker for PKCS #11 types
alexa-noxon referenced this issue in renesas-rx/amazon-freertos Dec 18, 2018
[UPDATED] RX65N RSK (Ether) aws_pkcs11_pal.c data storing to FLASH ROM.

Signed-off-by: SheltyDog <hiroki.ishiguro.fv@renesas.com>
sarenameas added a commit that referenced this issue Dec 28, 2018
alexa-noxon added a commit that referenced this issue Dec 28, 2018
Add Dan's TLS file for RSA keys (+ substituted key lookup for helpers
in aws_pkcs11.c).
Add additional configurations to TLS tests for RSA vs EC keys and
CreateObject vs GenerateKeyPair for provisioning.
Fix RSA Sign PKCS #11 test.
alexa-noxon added a commit that referenced this issue Jan 2, 2019
Calls DestroyObject repeatedly on all AWS PKCS #11 Labels until
no more objects with that label found.
alexa-noxon added a commit that referenced this issue Jan 3, 2019
alexa-noxon added a commit that referenced this issue Jan 9, 2019
Free various mbedtls contexts
alexa-noxon added a commit that referenced this issue Jan 10, 2019
For multithreading support, created mutex wrappers to match PKCS #11 expected
function footprint.  Functions calling xInitialzePkcs11Session will get
mutex functions (but have not updated for all places that call C_Initialize
directly).
alexa-noxon added a commit that referenced this issue Jan 30, 2019
Add Dan's TLS file for RSA keys (+ substituted key lookup for helpers
in aws_pkcs11.c).
Add additional configurations to TLS tests for RSA vs EC keys and
CreateObject vs GenerateKeyPair for provisioning.
Fix RSA Sign PKCS #11 test.
alexa-noxon added a commit that referenced this issue Jan 30, 2019
Calls DestroyObject repeatedly on all AWS PKCS #11 Labels until
no more objects with that label found.
alexa-noxon added a commit that referenced this issue Jan 30, 2019
alexa-noxon added a commit that referenced this issue Jan 30, 2019
Free various mbedtls contexts
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 1, 2020
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 1, 2020
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 1, 2020
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 1, 2020
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 1, 2020
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 1, 2020
lundinc2 added a commit that referenced this issue Oct 2, 2020
* Submoduled PKCS #11 repo.

* Removed PKCS #11 docs.

* Removed PKCS #11 wrapper library.

* Removed PKCS #11 utils.

* Removed implementation.

* Moved mbed TLS mutex initialization to the PAL.

* Update build systems.

* Fixed bug in PKCS #11 integration tests. The wrong size for a known EC curve was used.
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Oct 7, 2020
lundinc2 added a commit that referenced this issue Oct 16, 2020
* Remove iot prefix from PKCS files.
* Pin corePKCS v2.0.1
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Dec 15, 2020
lundinc2 added a commit that referenced this issue Dec 17, 2020
* Fix memory leak in PKCS #11 test.
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Feb 11, 2021
lundinc2 added a commit that referenced this issue Feb 11, 2021
* Allow for TLS basic connections by squashing PKCS #11 errors when looking for client credentials.
* Improved error message.
* Added changelog snippet.
* Add comment to describe the reason behind the change necessary for basic TLS.

Co-authored-by: Archit Aggarwal <architag@amazon.com>
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Feb 16, 2021
lundinc2 added a commit to lundinc2/amazon-freertos that referenced this issue Feb 19, 2021
lundinc2 added a commit that referenced this issue Feb 25, 2021
)

* Remove PKCS #11 PAL destoy object config flag.
* Update corePKCS11 submodule.

Add Destroy object implementation to ports lacking one.
jcmvbkbc pushed a commit to foss-xtensa/amazon-freertos that referenced this issue Jan 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants