Using a AF_INET UDP socket on the nrf9160 I will get ENOMEM when I send a bunch of datagrams over the socket. I understand how there can be a limited number of buffers for sending, but shouldn't sendto() block waiting for one to become available?
The sample below illustrates the problem. In my test I didn't have a server running to receive the datagrams, if it matters. This may be related to my other ticket: https://devzone.nordicsemi.com/f/nordic-q-a/52437/nrf_poll-may-be-consuming-data-when-not-supposed-to-on-nrf9160
CONFIG_BSD_LIBRARY=y CONFIG_NETWORKING=y CONFIG_NET_SOCKETS_OFFLOAD=y CONFIG_NET_SOCKETS=y CONFIG_NET_IPV4=n CONFIG_NET_IPV6=n CONFIG_NET_UDP=n CONFIG_NET_TCP=n CONFIG_HEAP_MEM_POOL_SIZE=1024 CONFIG_DEBUG=y CONFIG_DEBUG_INFO=y # Our CPU has hardware stack limit checking, and it needs to be large CONFIG_HW_STACK_PROTECTION=y CONFIG_MAIN_STACK_SIZE=4096
#include <zephyr.h>
#include <stdio.h>
#include <string.h>
//#include <nrf_socket.h>
#include <net/socket.h>
#define NRF_HTONL(val) ((((uint32_t) (val) & 0xff000000) >> 24) | \
(((uint32_t) (val) & 0x00ff0000) >> 8) | \
(((uint32_t) (val) & 0x0000ff00) << 8) | \
(((uint32_t) (val) & 0x000000ff) << 24))
#define IPADDR(a,b,c,d) (((u32_t)a << 24) | ((u32_t)b << 16) | ((u32_t)c << 8) | (u32_t)d)
static struct sockaddr_in remote_addr;
void lteon( void )
{
int nread;
char buff[100];
int sock = socket(AF_LTE, 0, NPROTO_AT);
send( sock, "AT+CEREG=2", 10, 0 );
send( sock, "AT+CFUN=1", 9, 0 );
memset( buff, 0, 100 );
while( (nread = recv(sock, buff, 100, 0 )) > 0 )
{
printk( "got: %s\n", buff );
if( ! strncmp( buff, "+CEREG: 5", 9) )
break;
memset( buff, 0, 100 );
}
close( sock );
}
void main( void )
{
int counter = 0;
static char buf[256];
int sockfd, bytes_sent;
lteon();
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
k_panic();
remote_addr.sin_family = AF_INET;
#warning change to a real IP address and port
remote_addr.sin_port = 1234;
remote_addr.sin_addr.s_addr = NRF_HTONL(IPADDR(1,2,3,4));
// after about 16 cycles, size=100 produces ENOMEM, or when size=20 produces EOPNOTSUPP
int bytes_to_send = 100;
memset( buf, 'A', bytes_to_send );
while(1) {
printk("count %d\n", ++counter);
bytes_sent = sendto( sockfd, buf, bytes_to_send, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr) );
if (bytes_sent != bytes_to_send) {
printk("send failed, returned %d, errno %d\n", bytes_sent, errno);
k_panic();
}
//k_sleep(100);
}
}