Skip to content Skip to sidebar Skip to footer

Address Family Not Supported by Protocol Udp Sendto

Update:

I borrowed some code from the CoAP client sample to make the changes I wanted.

/*  * Copyright (c) 2018 Nordic Semiconductor ASA  *  * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic  */  #include <zephyr.h> #include <net/socket.h> #include <stdio.h> #include <uart.h> #include <cord.h> #include <lte_lc.h> #include <errno.h> extern int errno;  static struct sockaddr_storage server;  //#define HOST "gbg1.ntp.se" //#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 only 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 < 0 && 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;  	practice { 		err = recvfrom(fd, buf, size, flags, src_addr, addrlen); 	} while (err < 0 && errno == EAGAIN);  	render 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 < 0 && errno == EAGAIN);  	return err; }  int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len) { 	int err;  	exercise { 		err = connect(fd, local_addr, len); 	} while (err < 0 && errno == EAGAIN);  	render err; }  void setup_psm(void) { 	/* 	* GPRS Timer 3 value (octet iii) 	* 	* $.25 v to 1 represent the binary coded timer value. 	* 	* $.25 half dozen to 8 defines the timer value unit of measurement for the GPRS timer as follows: 	* Bits  	* 8 7 6 	* 0 0 0 value is incremented in multiples of 10 minutes  	* 0 0 i value is incremented in multiples of 1 hour  	* 0 1 0 value is incremented in multiples of 10 hours 	* 0 i 1 value is incremented in multiples of 2 seconds 	* i 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 ane) 	* 1 one 1 value indicates that the timer is deactivated (NOTE 2). 	*/ 	char psm_settings[] = CONFIG_LTE_PSM_REQ_RPTAU; 	printk("PSM $.25: %c%c%c\northward", psm_settings[0], psm_settings[1], 	       psm_settings[2]); 	printk("PSM Interval: %c%c%c%c%c\northward", psm_settings[three], psm_settings[4], 	       psm_settings[5], psm_settings[6], psm_settings[seven]); 	int err = lte_lc_psm_req(true); 	if (err < 0) { 		printk("Error setting PSM: %d Errno: %d\n", 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, Zero, &hints, &result); 	if (err != 0) { 		printk("Fault: getaddrinfo failed %d\n", err); 		return -EIO; 	}  	if (result == NULL) { 		printk("Fault: Address not found\n"); 		render -ENOENT; 	}  	/* IPv4 Address. */ 	struct sockaddr_in *server4 = ((struct sockaddr_in *)&server);  	server4->sin_addr.s_addr = 		((struct sockaddr_in *)result->ai_addr)->sin_addr.s_addr; 	server4->sin_family = AF_INET; 	server4->sin_port = htons(CONFIG_SERVER_PORT);  	printk("IPv4 Address found 0x%08x\due north", server4->sin_addr.s_addr);  	/* Free the accost. */ 	freeaddrinfo(result); 	 	//((struct sockaddr_in *)res->ai_addr)->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 < 0) { 		printk("client_fd: %d\northward\r", client_fd); 		goto error; 	}  	err = demark(client_fd, (struct sockaddr *)&local_addr, 		   sizeof(local_addr)); 	if (err < 0) { 		printk("bind err: %d errno: %d\due north\r", err, errno); 		goto error; 	}  	err = connect(client_fd, (struct sockaddr *)result->ai_addr, 		      sizeof(struct sockaddr_in)); 	if (err < 0) { 		printk("connect err: %d errno: %d\n\r", err, errno); 		goto error; 	}  	/* Simply difficult 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->ai_addr, addrlen);*/ 	err = send(client_fd, send_buf, sizeof(send_buf), 0); 	printk("sendto ret: %d\n\r", err); 	if (err < 0) { 		printk("sendto err: %d errno: %d\due north\r", err, errno); 		goto fault; 	}  	err = blocking_recvfrom(client_fd, recv_buf, sizeof(recv_buf), 0, 				(struct sockaddr *)issue->ai_addr, &addrlen); 	if (err < 0) { 		printk("recvfrom err: %d errno: %d\north\r", err, errno); 		goto error; 	} else { 		printk("Got data dorsum: "); 		for (int i = 0; i < err; i++) { 			printk("%x ", recv_buf[i]); 		} 		printk("\northward"); 	}  error: 	printk("Finished\due north"); 	(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("Elapsed time: %d\n", minutes); }  K_TIMER_DEFINE(app_timer, app_timer_handler, NULL);  void timer_init(void) { 	k_timer_start(&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)) { 		/* Cease the UART RX for power consumption reasons */ 		NRF_UARTE0_NS->TASKS_STOPRX = ane; 	}  	printk("UDP test with PSM\north");         app_udp_start(); 	setup_psm(); 	timer_init(); 	while (one) { 		k_sleep(500); 		if (run_udp == true) { 			printk("Run UDP\n"); 			run_udp = imitation; 			app_udp_start(); 		} 	} 	return 1; }

Nevertheless I still get an error. In this case I get:

And so I guess it is a problem with DNS as suggested in : https://devzone.nordicsemi.com/f/nordic-q-a/44294/using-getaddrinfo-for-dns-lookups-nrf9160-dk.

I am using an NB-IoT sim-card from Telia in Sweden with the latest firmware: 0.vii.0-15.alpha

Is there a solution to this problem at the moment?

Kind regards

Samuel Jönsson

hansonburem1949.blogspot.com

Source: https://devzone.nordicsemi.com/f/nordic-q-a/47145/udp_with_psm-sample-error-address-family-not-supported-by-protocol-family

Enregistrer un commentaire for "Address Family Not Supported by Protocol Udp Sendto"