<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76962/mqtt-connection-timeout-setting</link><description>Hi, 
 We have observed that in areas of poor lte connectivity, the client_inti() works well but the mqtt_connect() takes a couple of minutes and times out. 
 I need to change the mqtt connection timeout duration make it 20-30 seconds, since if it does</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 02 Aug 2021 07:32:46 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76962/mqtt-connection-timeout-setting" /><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/322791?ContentTypeID=1</link><pubDate>Mon, 02 Aug 2021 07:32:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8585522a-d61e-4cdd-9ac9-b5699a3c1c9a</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;My apologies for the late reply.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Stratosphere"]I am currently using sdk 1.4.2. should this work on it?[/quote]
&lt;p&gt;This functionality was not added until more recent. Is it technically possible for you to use a newer ncs version?&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/320550?ContentTypeID=1</link><pubDate>Sun, 18 Jul 2021 08:20:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6b56c93-82f3-4dfc-9b63-b4c20a6b0ab5</guid><dc:creator>Stratosphere</dc:creator><description>&lt;p&gt;Hi Haken,&lt;/p&gt;
&lt;p&gt;I am currently using sdk 1.4.2. should this work on it?&lt;/p&gt;
&lt;p&gt;It does not seem to be working for me. here is the console log&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;RRC mode: Connected
getaddrInfo errno: 0
fcntl nonblocking set: 0. fd : 0, flags: 16384, rflags: 16384
In http timer. http state: 3, currentState: 0, httpQuit: 0
In http timer. http state: 3, currentState: 3, httpQuit: 0
http waiting to connect
http: Connected&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;in http timer&amp;quot; is printed every 5 second by a timer callback. &amp;quot;http waiting to connect&amp;quot; is printed from within the do while loop as per code below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;flags = fcntl(fd, F_GETFL, 0);
ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
rflags = fcntl(fd, F_GETFL, 0);	
printk(&amp;quot;fcntl nonblocking set: %d. fd : %d, flags: %d, rflags: %d\n&amp;quot;, ret, fd, flags, rflags);

fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
if (fd == -1) 
{
	if (DEBUG_PRINT_HTTP) printk(&amp;quot;Failed to open socket!\n&amp;quot;);
	errorReason = ERROR_HTTP_SOCKET_FAILED;
	goto clean_up;
}

httpState = 2;
/* Setup TLS socket options */
err = httpTlsSetup(fd);
if (err) 
{
	errorReason = ERROR_HTTP_SECURECLIENT_INIT_FAIL;
	goto clean_up;
}

httpState = 3;

//Connect
{
	waitingToConnect = 0;
	do 
	{
		err = connect(fd, res-&amp;gt;ai_addr , sizeof(struct sockaddr_in));
		k_sleep(100);
		waitingToConnect++;
		printk(&amp;quot;http waiting to connect\n&amp;quot;);
	} while ((err == EINPROGRESS) &amp;amp;&amp;amp; (waitingToConnect &amp;lt;300));

	if (err || waitingToConnect &amp;gt;=300)
	{
		if (DEBUG_PRINT_HTTP) printk(&amp;quot;connect() failed, time: %d, err: %d\n&amp;quot;, waitingToConnect, errno);
		if (errno == 23)
			sys_reboot(0);

		errorReason = ERROR_HTTP_CONNECT_FAILED;
		goto clean_up;
	}
	if (DEBUG_PRINT_HTTP) printk(&amp;quot;http: Connected\n&amp;quot;);
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;&lt;span&gt;http waiting to connect&amp;quot; should be printed every 100ms if connect() is non-blocking. so it seems that connect() is still using a blocking call.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/318763?ContentTypeID=1</link><pubDate>Tue, 06 Jul 2021 11:24:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2abbd284-80ab-479f-851d-9e1a9c82ea70</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;That is up to you how you time out,&amp;nbsp;you can do it this way by breaking out of the loop (and handling the closing scenario), but if the connect() succeeds; you need to handle the scenario of having non-blocking socket operations going on forward (or go back to blocking)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/318684?ContentTypeID=1</link><pubDate>Mon, 05 Jul 2021 16:08:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5e77bbc-6ee7-4a4e-a20f-278a7e8719bf</guid><dc:creator>Stratosphere</dc:creator><description>&lt;p&gt;Thank you so much Haken.&lt;/p&gt;
&lt;p&gt;Will test this out and let you know.\&lt;/p&gt;
&lt;p&gt;so basically, the do-while loop will keep calling connect repetitively until&amp;nbsp;EINPROGRESS is being returned.&amp;nbsp;&lt;br /&gt;If I wanted to exit, say after 20 seconds, i can just break out of the while loop and go to clean up? Will that crash anything when the connect request finally returns a few minutes later but the socket has been closed?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/318580?ContentTypeID=1</link><pubDate>Mon, 05 Jul 2021 09:22:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d6e4a489-3b3e-49fd-bd55-a7dbc75638a9</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you enable non-blocking socket operations, the connect() call will return EINPROGRESS until its done.&lt;/p&gt;
&lt;p&gt;That can be done like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;fcntl.h&amp;gt;

    ....
	printk(&amp;quot;enable non-blocking connect\n&amp;quot;);
	printk(&amp;quot;Will return EINPROGRESS (%d) when in progress\n&amp;quot;, EINPROGRESS);
	int flags = fcntl(fd, F_GETFL, 0);
	int ret = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
	int rflags = fcntl(fd, F_GETFL, 0);	
	
	
	printk(&amp;quot;connect time: %lld\n&amp;quot;, k_uptime_get());
	ret = connect(...)
	printk(&amp;quot;connect time: %lld\n&amp;quot;, k_uptime_get());
	....&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I added the logic to the https_client sample in ncs v1.6.0 just so that you can see that the call returns almost immediately when you enable nonblocking socket operation.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/nonblock_5F00_https_5F00_client_5F00_ncs_5F00_v1.6.0.diff"&gt;devzone.nordicsemi.com/.../nonblock_5F00_https_5F00_client_5F00_ncs_5F00_v1.6.0.diff&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/318494?ContentTypeID=1</link><pubDate>Fri, 02 Jul 2021 22:43:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1d8821e1-3259-459f-a443-e37c7a76b44a</guid><dc:creator>Stratosphere</dc:creator><description>&lt;p&gt;thank you Haken.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Can you help me add a timeout for http connection requests?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Noaman&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Mqtt connection timeout setting</title><link>https://devzone.nordicsemi.com/thread/318260?ContentTypeID=1</link><pubDate>Thu, 01 Jul 2021 14:05:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:30b9adf1-9895-4802-8f12-df7a6ade0a57</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Unfortunately, there is no option to set the timeout at this time, but I will add this as a feature request internally.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>