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 Reply Children
  • 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

Related