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

Is there a shutdown() related function available in socket in nrf9160?

1.1. In function: poll (fds, ARRAY_SIZE (fds),
cloud_keepalive_time_left (cloud_backend) );
How to detect actions that perform socket disconnections locally.

while (true) {
		ret = poll(fds, ARRAY_SIZE(fds),
			   cloud_keepalive_time_left(cloud_backend));
		if (ret < 0) {
			LOG_ERR("poll() returned an error: %d", ret);
			error_handler(ERROR_CLOUD, ret);
			continue;
		}

		if (ret == 0) {
			cloud_ping(cloud_backend);
			continue;
		}

		if ((fds[0].revents & POLLIN) == POLLIN) {
			cloud_input(cloud_backend);
		}

		if ((fds[0].revents & POLLNVAL) == POLLNVAL) {
			if (atomic_get(&reconnect_to_cloud)) {
				k_delayed_work_cancel(&cloud_reboot_work);
				LOG_INF("Attempting reconnect...");
				goto connect;
			}
			LOG_ERR("Socket error: POLLNVAL");
			LOG_ERR("The cloud socket was unexpectedly closed.");
			error_handler(ERROR_CLOUD, -EIO);
			return;
		}

		if ((fds[0].revents & POLLHUP) == POLLHUP) {
			LOG_ERR("Socket error: POLLHUP");
			LOG_ERR("Connection was closed by the cloud.");
			error_handler(ERROR_CLOUD, -EIO);
			return;
		}

		if ((fds[0].revents & POLLERR) == POLLERR) {
			LOG_ERR("Socket error: POLLERR");
			LOG_ERR("Cloud connection was unexpectedly closed.");
			error_handler(ERROR_CLOUD, -EIO);
			return;
		}
	}

2. I queryed that the close socket-related function does not return the relevant event in the poll, and the shutdown function can return to the event detected by the poll, whether there is a function related to shutdown in nrf9160.

Parents
  • Hi,

      

    How to detect actions that perform socket disconnections locally.

    if the file descriptor is closed locally, you should get a POLLNVAL revents. This is the equivilant to EBADF for poll():

    https://stackoverflow.com/questions/25147181/pollhup-vs-pollnval-or-what-is-pollhup

    The function you posted looks to have this already implemented and handled.

     

    I queryed that the close socket-related function does not return the relevant event in the poll, and the shutdown function can return to the event detected by the poll, whether there is a function related to shutdown in nrf9160.

     Are you not getting the correct .revent in case where the "fds" is closed or has a non-existing value? Could you please elaborate on what is not working?

     

    Kind regards,

    Håkon

  • Hi Håkon Alseth,

    When the socket communication is off-grid, it does not tell the upper layer whether it is off-grid, there should be a similar poll err.

    Do not exit poll in time when socket communication is off the grid.

    The customer is experiencing a problem that is consistent with the description of the problem, see this link for details:
    https://www.icode9.com/content-3-357617.html


    Kind regards, Peter.Min

  • Hi Peter,

     

    Can you please point explicitly to which feature of poll is inconsistent?

     

    Cheers,

    Håkon

  • Hi Håkon Alseth,

    1. Customers will encounter problems like this case when they make an app: Case ID: 255728
    In this case scenario, the customer needs this piece of code 1.
    Code 1 is as follows:

        while (1) 
    	{
    		err = poll(&fds, 1, mqtt_keepalive_time_left(&client));
    		if (err < 0) {
    			printk("ERROR: poll %d\n", errno);
    			break;
    		}
    
    		err = mqtt_live(&client);
    		if ((err != 0) && (err != -EAGAIN)) {
    			printk("ERROR: mqtt_live %d\n", err);
    			break;
    		}
    
    		if ((fds.revents & POLLIN) == POLLIN) {
    			err = mqtt_input(&client);
    			if (err != 0) {
    				printk("ERROR: mqtt_input %d\n", err);
    				break;
    			}
    		}
    
    		if ((fds.revents & POLLERR) == POLLERR) {
    			printk("POLLERR\n");
    			break;
    		}
    
    		if ((fds.revents & POLLNVAL) == POLLNVAL) {
    			printk("POLLNVAL\n");
    			break;
    		}
    	}

    execute the disconnect function outside, but get the mqtt or socket break flag within this code,

    2. And the customer tests out that this code is executed elsewhere: err mqtt_disconnect (-client);
    The poll function does not get the return value.

    3. And I query the poll function related knowledge,
    Using the shutdown function to close the socket, poll will have a return value.
    Using the close function to close the socket, poll does not have a return value.

    4. In other platforms you will see the shutdown-related function that closes the socket. The shutdown function to close the socket is not seen in ncs v1.3.

    5. What can I do to return the break flag in time in thepol function in code 1.

  • Hi,

     

    My apologies for misinterpreting.

    I see what you mean now with the poll() behavior. The order of the .revents is parsed in the wrong order, in the case the link has been disconnected (POLLNVAL) and there's a POLLIN event simultaneously.

    I will report this internally.

     

    The shutdown() function doesn't exist in the posix socket API, so it would be handled on a protocol-level.

    To close the socket for mqtt, you should call mqtt_disconnect(...) and mqtt_live(...)

     

    Kind regards,

    Håkon

Reply
  • Hi,

     

    My apologies for misinterpreting.

    I see what you mean now with the poll() behavior. The order of the .revents is parsed in the wrong order, in the case the link has been disconnected (POLLNVAL) and there's a POLLIN event simultaneously.

    I will report this internally.

     

    The shutdown() function doesn't exist in the posix socket API, so it would be handled on a protocol-level.

    To close the socket for mqtt, you should call mqtt_disconnect(...) and mqtt_live(...)

     

    Kind regards,

    Håkon

Children
No Data
Related