This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Adapting nRF9160's coap_client sample in NCS

Hi,

I am adapting the nRF9160 coap_client sample, and have 2 questions:

  1. If I do not care about responses what is the ideal / cleanest way of configuring the app or underlying socket to ignore / drop them?
  2. On the other hand, if I choose to consume the responses, is it ok to have this handled in a one thread while coap requests are periodically dispatched in via a work_q that leverages a different thread? Could any concurrency issues or race conditions arise in the underlying socket or accompanying polling data structures? A draft of what I have in mind is as follows (note that the handler for send_telemetry_work recursively enqueues the work for subsequent coap requests):
        k_delayed_work_submit_to_queue(&application_work_q, &send_telemetry_work, K_SECONDS(0));
    
        int err, received;
        while (1) {
    		
    		err = wait(POLL_TIMEOUT_S);
    		if (err < 0) {
    			if (err == -EAGAIN) {
    				continue;
    			}
    
    			printk("Poll error, exit...\n");
    			break;
    		}
    
    		received = recv(sock, coap_buf, sizeof(coap_buf), MSG_DONTWAIT);
    		if (received < 0) {
    			if (errno == EAGAIN || errno == EWOULDBLOCK) {
    				printk("socket EAGAIN\n");
    				continue;
    			} else {
    				printk("Socket error, exit...\n");
    				break;
    			}
    		}
    
    		if (received == 0) {
    			printk("Empty datagram\n");
    			continue;
    		}
    
    		err = client_handle_telemetry_send_response(coap_buf, received);
    		if (err < 0) {
    			printk("Invalid response, exit...\n");
    			break;
    		}
    	}

Thanks.

Parents
  • Hi!

    1. There's no way to configure a socket to automatically drop the RX data. The easiest way to achieve such behavior would be a dummy recv() call whenever POLLIN is reported on a socket.

    2. It is ok to poll() for data on one thread and send() requests from another. Then it is up to the application to synchronize requests with responses.

    Best regards,

    Heidi

Reply
  • Hi!

    1. There's no way to configure a socket to automatically drop the RX data. The easiest way to achieve such behavior would be a dummy recv() call whenever POLLIN is reported on a socket.

    2. It is ok to poll() for data on one thread and send() requests from another. Then it is up to the application to synchronize requests with responses.

    Best regards,

    Heidi

Children
Related