<?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>udp_with_psm sample. ERROR: Address family not supported by protocol family</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/47145/udp_with_psm-sample-error-address-family-not-supported-by-protocol-family</link><description>Hi, 
 
 I am trying out the udp_with_psm example from: https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/udp_with_psm as a first step in sending data via UDP/IP. I have edited the example by using an ntp host from Sweden</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 10 May 2019 12:50:29 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/47145/udp_with_psm-sample-error-address-family-not-supported-by-protocol-family" /><item><title>RE: udp_with_psm sample. ERROR: Address family not supported by protocol family</title><link>https://devzone.nordicsemi.com/thread/186437?ContentTypeID=1</link><pubDate>Fri, 10 May 2019 12:50:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c714944-6371-431e-b1e9-9e50a51d95a5</guid><dc:creator>Martin Lesund</dc:creator><description>&lt;p&gt;Hi Samuel,&lt;/p&gt;
&lt;p&gt;Please make sure you are actually running the nb-iot mode.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fref_at_commands%2FREF%2Fat_commands%2Fmob_termination_ctrl_status%2Fxsystemmode_set.html"&gt;AT%XSYSTEMMODE?&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="text-decoration:underline;"&gt;Then you could try to disable&amp;nbsp;ePCO:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AT%XEPCO=0&amp;nbsp; //&lt;/strong&gt;disables ePCO&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;AT+CFUN=0 //&lt;/strong&gt;saves the settings&lt;/p&gt;
&lt;p&gt;Then you should be able to run the example on the nb-iot network.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;or you could add:&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;strong&gt;CONFIG_LTE_LEGACY_PCO_MODE=y&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;in the prj.conf file of the project, which does the same thing but inside the application itself.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Let me know if you have any issues.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: udp_with_psm sample. ERROR: Address family not supported by protocol family</title><link>https://devzone.nordicsemi.com/thread/186349?ContentTypeID=1</link><pubDate>Fri, 10 May 2019 09:48:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8f952dd6-7e76-46d2-b2be-76316d2a8033</guid><dc:creator>Samuel</dc:creator><description>&lt;p&gt;Update:&lt;/p&gt;
&lt;p&gt;I borrowed some code from the CoAP client sample to make the changes I wanted.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;net/socket.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;uart.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;lte_lc.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
extern int errno;

static struct sockaddr_storage server;

//#define HOST &amp;quot;gbg1.ntp.se&amp;quot;
//#define PORT 123
#define RECV_BUF_SIZE 1024
#define SEND_BUF_SIZE 1024

u8_t recv_buf[RECV_BUF_SIZE];

/* As it is close to impossible to find
 * a proper UDP server, we just connect
 * to a ntp server and read the response
 */
struct ntp_format {
	u8_t flags;
	u8_t stratum; /* stratum */
	u8_t poll; /* poll interval */
	s8_t precision; /* precision */
	u32_t rootdelay; /* root delay */
	u32_t rootdisp; /* root dispersion */
	u32_t refid; /* reference ID */
	u32_t reftime_sec; /* reference time */
	u32_t reftime_frac; /* reference time */
	u32_t org_sec; /* origin timestamp */
	u32_t org_frac; /* origin timestamp */
	u32_t rec_sec; /* receive timestamp */
	u32_t rec_frac; /* receive timestamp */
	u32_t xmt_sec; /* transmit timestamp */
	u32_t xmt_frac; /* transmit timestamp */
};

int blocking_recv(int fd, u8_t *buf, u32_t size, u32_t flags)
{
	int err;

	do {
		err = recv(fd, buf, size, flags);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

int blocking_recvfrom(int fd, void *buf, u32_t size, u32_t flags,
		      struct sockaddr *src_addr, socklen_t *addrlen)
{
	int err;

	do {
		err = recvfrom(fd, buf, size, flags, src_addr, addrlen);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

int blocking_send(int fd, u8_t *buf, u32_t size, u32_t flags)
{
	int err;

	do {
		err = send(fd, buf, size, flags);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len)
{
	int err;

	do {
		err = connect(fd, local_addr, len);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

void setup_psm(void)
{
	/*
	* GPRS Timer 3 value (octet 3)
	*
	* Bits 5 to 1 represent the binary coded timer value.
	*
	* Bits 6 to 8 defines the timer value unit for the GPRS timer as follows:
	* Bits 
	* 8 7 6
	* 0 0 0 value is incremented in multiples of 10 minutes 
	* 0 0 1 value is incremented in multiples of 1 hour 
	* 0 1 0 value is incremented in multiples of 10 hours
	* 0 1 1 value is incremented in multiples of 2 seconds
	* 1 0 0 value is incremented in multiples of 30 seconds
	* 1 0 1 value is incremented in multiples of 1 minute
	* 1 1 0 value is incremented in multiples of 320 hours (NOTE 1)
	* 1 1 1 value indicates that the timer is deactivated (NOTE 2).
	*/
	char psm_settings[] = CONFIG_LTE_PSM_REQ_RPTAU;
	printk(&amp;quot;PSM bits: %c%c%c\n&amp;quot;, psm_settings[0], psm_settings[1],
	       psm_settings[2]);
	printk(&amp;quot;PSM Interval: %c%c%c%c%c\n&amp;quot;, psm_settings[3], psm_settings[4],
	       psm_settings[5], psm_settings[6], psm_settings[7]);
	int err = lte_lc_psm_req(true);
	if (err &amp;lt; 0) {
		printk(&amp;quot;Error setting PSM: %d Errno: %d\n&amp;quot;, err, errno);
	}
}

void app_udp_start(void)
{       
        int err;
	struct addrinfo *result;
	socklen_t addrlen = sizeof(struct sockaddr_storage);
        struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_DGRAM
	};

	err = getaddrinfo(CONFIG_SERVER_HOSTNAME, NULL, &amp;amp;hints, &amp;amp;result);
	if (err != 0) {
		printk(&amp;quot;ERROR: getaddrinfo failed %d\n&amp;quot;, err);
		return -EIO;
	}

	if (result == NULL) {
		printk(&amp;quot;ERROR: Address not found\n&amp;quot;);
		return -ENOENT;
	}

	/* IPv4 Address. */
	struct sockaddr_in *server4 = ((struct sockaddr_in *)&amp;amp;server);

	server4-&amp;gt;sin_addr.s_addr =
		((struct sockaddr_in *)result-&amp;gt;ai_addr)-&amp;gt;sin_addr.s_addr;
	server4-&amp;gt;sin_family = AF_INET;
	server4-&amp;gt;sin_port = htons(CONFIG_SERVER_PORT);

	printk(&amp;quot;IPv4 Address found 0x%08x\n&amp;quot;, server4-&amp;gt;sin_addr.s_addr);

	/* Free the address. */
	freeaddrinfo(result);
	
	//((struct sockaddr_in *)res-&amp;gt;ai_addr)-&amp;gt;sin_port = htons(PORT);
	struct sockaddr_in local_addr;

	local_addr.sin_family = AF_INET;
	local_addr.sin_port = htons(0);
	local_addr.sin_addr.s_addr = 0;

	int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (client_fd &amp;lt; 0) {
		printk(&amp;quot;client_fd: %d\n\r&amp;quot;, client_fd);
		goto error;
	}

	err = bind(client_fd, (struct sockaddr *)&amp;amp;local_addr,
		   sizeof(local_addr));
	if (err &amp;lt; 0) {
		printk(&amp;quot;bind err: %d errno: %d\n\r&amp;quot;, err, errno);
		goto error;
	}

	err = connect(client_fd, (struct sockaddr *)result-&amp;gt;ai_addr,
		      sizeof(struct sockaddr_in));
	if (err &amp;lt; 0) {
		printk(&amp;quot;connect err: %d errno: %d\n\r&amp;quot;, err, errno);
		goto error;
	}

	/* Just hard code the packet format */
	u8_t send_buf[sizeof(struct ntp_format)] = { 0xe3 };

	/*err = sendto(client_fd, send_buf, sizeof(send_buf), 0,
		     (struct sockaddr *)res-&amp;gt;ai_addr, addrlen);*/
	err = send(client_fd, send_buf, sizeof(send_buf), 0);
	printk(&amp;quot;sendto ret: %d\n\r&amp;quot;, err);
	if (err &amp;lt; 0) {
		printk(&amp;quot;sendto err: %d errno: %d\n\r&amp;quot;, err, errno);
		goto error;
	}

	err = blocking_recvfrom(client_fd, recv_buf, sizeof(recv_buf), 0,
				(struct sockaddr *)result-&amp;gt;ai_addr, &amp;amp;addrlen);
	if (err &amp;lt; 0) {
		printk(&amp;quot;recvfrom err: %d errno: %d\n\r&amp;quot;, err, errno);
		goto error;
	} else {
		printk(&amp;quot;Got data back: &amp;quot;);
		for (int i = 0; i &amp;lt; err; i++) {
			printk(&amp;quot;%x &amp;quot;, recv_buf[i]);
		}
		printk(&amp;quot;\n&amp;quot;);
	}

error:
	printk(&amp;quot;Finished\n&amp;quot;);
	(void)close(client_fd);
}

static volatile bool run_udp;

void app_timer_handler(struct k_timer *dummy)
{
	static u32_t minutes;

	minutes++;
	/* This shall match the PSM interval */
	if (minutes % 10 == 0) {
		run_udp = true;
	}
	printk(&amp;quot;Elapsed time: %d\n&amp;quot;, minutes);
}

K_TIMER_DEFINE(app_timer, app_timer_handler, NULL);

void timer_init(void)
{
	k_timer_start(&amp;amp;app_timer, K_MINUTES(1), K_MINUTES(1));
}

/**@brief Configures modem to provide LTE link. Blocks until link is
 * successfully established.
 */

int main(void)
{
	if (!IS_ENABLED(CONFIG_AT_HOST_LIBRARY)) {
		/* Stop the UART RX for power consumption reasons */
		NRF_UARTE0_NS-&amp;gt;TASKS_STOPRX = 1;
	}

	printk(&amp;quot;UDP test with PSM\n&amp;quot;);
        app_udp_start();
	setup_psm();
	timer_init();
	while (1) {
		k_sleep(500);
		if (run_udp == true) {
			printk(&amp;quot;Run UDP\n&amp;quot;);
			run_udp = false;
			app_udp_start();
		}
	}
	return 1;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However I still get an error. In this case I get:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1557481179691v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;So I guess it is a problem with DNS as suggested in :&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/44294/using-getaddrinfo-for-dns-lookups-nrf9160-dk"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/44294/using-getaddrinfo-for-dns-lookups-nrf9160-dk&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I am using an NB-IoT sim-card from Telia in Sweden with the latest&amp;nbsp;firmware: 0.7.0-15.alpha&lt;/p&gt;
&lt;p&gt;Is there a solution to this problem at the moment?&lt;/p&gt;
&lt;p&gt;Kind regards&lt;/p&gt;
&lt;p&gt;Samuel J&amp;ouml;nsson&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>