Lines Matching +full:poll +full:- +full:timeout +full:- +full:ms

3  * Copyright (c) 2018-2019 Foundries.io
5 * SPDX-License-Identifier: Apache-2.0
68 #define THREAD_PRIORITY K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1)
70 #define THREAD_PRIORITY K_PRIO_PREEMPT(CONFIG_NUM_PREEMPT_PRIORITIES - 1)
90 uint32_t call_period; /* ms */
91 int64_t next_timestamp; /* ms */
131 zvfs_eventfd_write(sock_fds[MAX_POLL_FD - 1].fd, 1); in lwm2m_engine_wake_up()
137 if (client_ctx->sock_fd < 0) { in lwm2m_open_socket()
140 if (IS_ENABLED(CONFIG_LWM2M_DTLS_SUPPORT) && client_ctx->use_dtls) { in lwm2m_open_socket()
141 client_ctx->sock_fd = zsock_socket(client_ctx->remote_addr.sa_family, in lwm2m_open_socket()
144 client_ctx->sock_fd = in lwm2m_open_socket()
145 zsock_socket(client_ctx->remote_addr.sa_family, SOCK_DGRAM, in lwm2m_open_socket()
149 if (client_ctx->sock_fd < 0) { in lwm2m_open_socket()
151 return -errno; in lwm2m_open_socket()
164 if (client_ctx->sock_fd >= 0) { in lwm2m_close_socket()
165 int ret = zsock_close(client_ctx->sock_fd); in lwm2m_close_socket()
169 ret = -errno; in lwm2m_close_socket()
174 client_ctx->sock_fd = -1; in lwm2m_close_socket()
175 client_ctx->connection_suspended = true; in lwm2m_close_socket()
178 client_ctx->buffer_client_messages = true; in lwm2m_close_socket()
189 if (client_ctx->sock_fd >= 0 && !client_ctx->connection_suspended) { in lwm2m_socket_suspend()
190 int socket_temp_id = client_ctx->sock_fd; in lwm2m_socket_suspend()
193 client_ctx->sock_fd = -1; in lwm2m_socket_suspend()
197 client_ctx->sock_fd = socket_temp_id; in lwm2m_socket_suspend()
199 if (client_ctx->set_socket_state) { in lwm2m_socket_suspend()
200 client_ctx->set_socket_state(client_ctx->sock_fd, in lwm2m_socket_suspend()
212 if (client_ctx->connection_suspended) { in lwm2m_engine_connection_resume()
217 client_ctx->connection_suspended = false; in lwm2m_engine_connection_resume()
225 client_ctx->connection_suspended = false; in lwm2m_engine_connection_resume()
236 client_ctx->buffer_client_messages = false; in lwm2m_push_queued_buffers()
238 while (!sys_slist_is_empty(&client_ctx->queued_messages)) { in lwm2m_push_queued_buffers()
239 sys_snode_t *msg_node = sys_slist_get(&client_ctx->queued_messages); in lwm2m_push_queued_buffers()
246 msg->pending->t0 = k_uptime_get(); in lwm2m_push_queued_buffers()
247 sys_slist_append(&msg->ctx->pending_sends, &msg->node); in lwm2m_push_queued_buffers()
256 if (!client_ctx->bootstrap_mode) { in lwm2m_engine_bootstrap_override()
261 if (path->obj_id == LWM2M_OBJECT_SECURITY_ID || path->obj_id == LWM2M_OBJECT_SERVER_ID) { in lwm2m_engine_bootstrap_override()
275 o_f = lwm2m_get_engine_obj_field(obj_inst->obj, msg->path.res_id); in lwm2m_engine_validate_write_access()
277 return -ENOENT; in lwm2m_engine_validate_write_access()
283 !lwm2m_engine_bootstrap_override(msg->ctx, &msg->path)) { in lwm2m_engine_validate_write_access()
284 return -EPERM; in lwm2m_engine_validate_write_access()
287 if (!obj_inst->resources || obj_inst->resource_count == 0U) { in lwm2m_engine_validate_write_access()
288 return -EINVAL; in lwm2m_engine_validate_write_access()
325 if (msg->path.level > 2) { in bootstrap_delete()
326 return -EPERM; in bootstrap_delete()
329 if (msg->path.level == 2) { in bootstrap_delete()
330 if (!bootstrap_delete_allowed(msg->path.obj_id, msg->path.obj_inst_id)) { in bootstrap_delete()
331 return -EPERM; in bootstrap_delete()
334 return lwm2m_delete_obj_inst(msg->path.obj_id, msg->path.obj_inst_id); in bootstrap_delete()
340 * - LwM2M Bootstrap-Server Account (Bootstrap Security object, ID 0) in bootstrap_delete()
341 * - Device object (ID 3) in bootstrap_delete()
344 if (msg->path.level == 1 && obj_inst->obj->obj_id != msg->path.obj_id) { in bootstrap_delete()
348 if (!bootstrap_delete_allowed(obj_inst->obj->obj_id, obj_inst->obj_inst_id)) { in bootstrap_delete()
352 ret = lwm2m_delete_obj_inst(obj_inst->obj->obj_id, obj_inst->obj_inst_id); in bootstrap_delete()
374 for (i = 0, p = client_ctx->pendings; i < ARRAY_SIZE(client_ctx->pendings); i++, p++) { in retransmit_request()
375 if (!p->timeout) { in retransmit_request()
379 remaining = p->t0 + p->timeout; in retransmit_request()
387 if (!p->retries) { in retransmit_request()
389 if (msg->message_timeout_cb) { in retransmit_request()
390 msg->message_timeout_cb(msg); in retransmit_request()
395 if (msg->acknowledged) { in retransmit_request()
397 * timeout in case no response arrives. in retransmit_request()
421 if (srv->next_timestamp < next) { in engine_next_service_timestamp()
422 next = srv->next_timestamp; in engine_next_service_timestamp()
434 return -EINVAL; in engine_add_srv()
450 return -ENOMEM; in engine_add_srv()
497 return -ENOENT; in lwm2m_engine_update_service_period()
509 if (timestamp >= srv->next_timestamp) { in lwm2m_engine_service()
510 k_work_handler_t work = srv->service_work; in lwm2m_engine_service()
512 if (srv->call_period) { in lwm2m_engine_service()
513 srv->next_timestamp = k_uptime_get() + srv->call_period; in lwm2m_engine_service()
515 sys_slist_find_and_remove(&engine_service_list, &srv->node); in lwm2m_engine_service()
516 srv->service_work = NULL; in lwm2m_engine_service()
537 /* Last poll-handle is reserved for control socket */ in lwm2m_socket_add()
538 if (sock_nfds >= (MAX_POLL_FD - 1)) { in lwm2m_socket_add()
539 return -ENOMEM; in lwm2m_socket_add()
543 return -ENOMEM; in lwm2m_socket_add()
548 sock_fds[sock_nfds].fd = ctx->sock_fd; in lwm2m_socket_add()
563 sock_fds[i].fd = ctx->sock_fd; in lwm2m_socket_update()
567 return -1; in lwm2m_socket_update()
577 sock_nfds--; in lwm2m_socket_del()
588 sock_fds[sock_nfds].fd = -1; in lwm2m_socket_del()
602 SYS_SLIST_FOR_EACH_CONTAINER(&ctx->observer, obs, node) { in check_notifications()
603 if (!obs->event_timestamp) { in check_notifications()
607 if (obs->event_timestamp < next) { in check_notifications()
608 next = obs->event_timestamp; in check_notifications()
611 if (timestamp < obs->event_timestamp) { in check_notifications()
615 if (obs->active_notify != NULL) { in check_notifications()
620 if (rc == -ENOMEM) { in check_notifications()
624 obs->event_timestamp = in check_notifications()
625 engine_observe_shedule_next_event(obs, ctx->srv_obj_inst, timestamp); in check_notifications()
626 obs->last_timestamp = timestamp; in check_notifications()
655 if (!ctx || !ctx->set_socket_state) { in hint_socket_state()
661 empty = sys_slist_is_empty(&ctx->pending_sends) && in hint_socket_state()
662 sys_slist_is_empty(&ctx->queued_messages); in hint_socket_state()
664 empty = sys_slist_is_empty(&ctx->pending_sends); in hint_socket_state()
666 pendings = coap_pendings_count(ctx->pendings, ARRAY_SIZE(ctx->pendings)); in hint_socket_state()
675 bool ongoing_block_tx = coap_block_has_more(&ongoing_tx->cpkt); in hint_socket_state()
678 ctx->set_socket_state(ctx->sock_fd, LWM2M_SOCKET_STATE_ONGOING); in hint_socket_state()
679 } else if (ongoing_tx->type == COAP_TYPE_CON) { in hint_socket_state()
680 ctx->set_socket_state(ctx->sock_fd, LWM2M_SOCKET_STATE_ONE_RESPONSE); in hint_socket_state()
682 ctx->set_socket_state(ctx->sock_fd, LWM2M_SOCKET_STATE_LAST); in hint_socket_state()
685 ctx->set_socket_state(ctx->sock_fd, LWM2M_SOCKET_STATE_NO_DATA); in hint_socket_state()
697 len = zsock_recvfrom(client_ctx->sock_fd, in_buf, sizeof(in_buf) - 1, ZSOCK_MSG_DONTWAIT, in socket_recv_message()
702 return -errno; in socket_recv_message()
706 if (client_ctx->fault_cb != NULL) { in socket_recv_message()
707 client_ctx->fault_cb(errno); in socket_recv_message()
709 return -errno; in socket_recv_message()
730 msg_node = sys_slist_get(&ctx->pending_sends); in socket_send_message()
738 if (!msg || !msg->ctx) { in socket_send_message()
740 return -EINVAL; in socket_send_message()
743 if (msg->type == COAP_TYPE_CON) { in socket_send_message()
744 coap_pending_cycle(msg->pending); in socket_send_message()
749 rc = zsock_send(msg->ctx->sock_fd, msg->cpkt.data, msg->cpkt.offset, 0); in socket_send_message()
753 rc = -errno; in socket_send_message()
758 if (msg->type != COAP_TYPE_CON) { in socket_send_message()
774 set_pollout = !sys_slist_is_empty(&sock_ctx[i]->pending_sends); in socket_reset_pollfd_events()
794 int64_t timeout, next_tx; in socket_loop() local
833 is_empty = sys_slist_is_empty(&ctx->pending_sends); in socket_loop()
854 timeout = next > now ? next - now : 0; in socket_loop()
856 /* prevent roll-over */ in socket_loop()
857 timeout = timeout > INT32_MAX ? INT32_MAX : timeout; in socket_loop()
859 timeout = timeout > ENGINE_SLEEP_MS ? ENGINE_SLEEP_MS : timeout; in socket_loop()
862 rc = zsock_poll(sock_fds, MAX_POLL_FD, timeout); in socket_loop()
864 LOG_ERR("Error in poll:%d", errno); in socket_loop()
874 i == (MAX_POLL_FD - 1)) { in socket_loop()
881 if (sock_ctx[i] != NULL && sock_ctx[i]->sock_fd < 0) { in socket_loop()
886 LOG_ERR("Poll reported a socket error, %02x.", revents); in socket_loop()
887 if (sock_ctx[i] != NULL && sock_ctx[i]->fault_cb != NULL) { in socket_loop()
888 sock_ctx[i]->fault_cb(EIO); in socket_loop()
908 if (rc < 0 && rc != -EAGAIN) { in socket_loop()
909 LOG_ERR("send() reported a socket error, %d", -rc); in socket_loop()
910 if (sock_ctx[i] != NULL && sock_ctx[i]->fault_cb != NULL) { in socket_loop()
911 sock_ctx[i]->fault_cb(-rc); in socket_loop()
932 static const char pem_start[] = "-----BEGIN"; in is_pem()
937 if (strncmp(pem_start, (const char *) buf, sizeof(pem_start) - 1) == 0) { in is_pem()
951 ret = lwm2m_get_res_buf(&LWM2M_OBJ(0, client_ctx->sec_obj_inst, res_id), &cred, &max_len, in load_tls_type()
954 LOG_ERR("Unable to get resource data for %d/%d/%d", 0, client_ctx->sec_obj_inst, in load_tls_type()
961 return -EINVAL; in load_tls_type()
964 /* LwM2M registry stores strings without NULL-terminator, so we need to ensure that in load_tls_type()
970 return -EINVAL; in load_tls_type()
976 ret = tls_credential_add(client_ctx->tls_tag, type, cred, cred_len); in load_tls_type()
978 LOG_ERR("Error setting cred tag %d type %d: Error %d", client_ctx->tls_tag, type, in load_tls_type()
989 delete_tls_credentials(ctx->tls_tag); in lwm2m_load_psk_credentials()
1004 delete_tls_credentials(ctx->tls_tag); in lwm2m_load_x509_credentials()
1025 return -EOPNOTSUPP; in lwm2m_load_psk_credentials()
1030 return -EOPNOTSUPP; in lwm2m_load_x509_credentials()
1038 if (ctx->use_dtls) { in lwm2m_load_tls_credentials()
1039 return -EINVAL; in lwm2m_load_tls_credentials()
1047 return -EOPNOTSUPP; in lwm2m_load_tls_credentials()
1065 if (ctx->use_dtls) { in lwm2m_set_default_sockopt()
1069 ctx->tls_tag, in lwm2m_set_default_sockopt()
1072 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_tag_list, in lwm2m_set_default_sockopt()
1075 ret = -errno; in lwm2m_set_default_sockopt()
1083 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_SESSION_CACHE, in lwm2m_set_default_sockopt()
1086 ret = -errno; in lwm2m_set_default_sockopt()
1095 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_DTLS_CID, &cid, in lwm2m_set_default_sockopt()
1098 ret = -errno; in lwm2m_set_default_sockopt()
1104 if (ctx->desthostname != NULL && lwm2m_security_mode(ctx) == LWM2M_SECURITY_CERT) { in lwm2m_set_default_sockopt()
1106 tmp = ctx->desthostname[ctx->desthostnamelen]; in lwm2m_set_default_sockopt()
1109 ctx->desthostname[ctx->desthostnamelen] = '\0'; in lwm2m_set_default_sockopt()
1112 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_HOSTNAME, in lwm2m_set_default_sockopt()
1113 ctx->desthostname, ctx->desthostnamelen); in lwm2m_set_default_sockopt()
1116 ctx->desthostname[ctx->desthostnamelen] = tmp; in lwm2m_set_default_sockopt()
1118 ret = -errno; in lwm2m_set_default_sockopt()
1125 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_PEER_VERIFY, &verify, in lwm2m_set_default_sockopt()
1135 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_PEER_VERIFY, &verify, in lwm2m_set_default_sockopt()
1144 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_CIPHERSUITE_LIST, in lwm2m_set_default_sockopt()
1151 ret = zsock_setsockopt(ctx->sock_fd, SOL_TLS, TLS_CIPHERSUITE_LIST, in lwm2m_set_default_sockopt()
1159 return -EOPNOTSUPP; in lwm2m_set_default_sockopt()
1163 if (!IS_ENABLED(CONFIG_LWM2M_DTLS_SUPPORT) && ctx->use_dtls) { in lwm2m_set_default_sockopt()
1164 return -EOPNOTSUPP; in lwm2m_set_default_sockopt()
1177 if (client_ctx->use_dtls) { in lwm2m_socket_start()
1178 if (client_ctx->load_credentials) { in lwm2m_socket_start()
1179 ret = client_ctx->load_credentials(client_ctx); in lwm2m_socket_start()
1189 if (client_ctx->sock_fd < 0) { in lwm2m_socket_start()
1196 if (client_ctx->set_socketoptions) { in lwm2m_socket_start()
1197 ret = client_ctx->set_socketoptions(client_ctx); in lwm2m_socket_start()
1205 if ((client_ctx->remote_addr).sa_family == AF_INET) { in lwm2m_socket_start()
1207 } else if ((client_ctx->remote_addr).sa_family == AF_INET6) { in lwm2m_socket_start()
1210 ret = -EPROTONOSUPPORT; in lwm2m_socket_start()
1214 if (zsock_connect(client_ctx->sock_fd, &client_ctx->remote_addr, addr_len) < 0) { in lwm2m_socket_start()
1215 ret = -errno; in lwm2m_socket_start()
1220 flags = zsock_fcntl(client_ctx->sock_fd, F_GETFL, 0); in lwm2m_socket_start()
1221 if (flags == -1) { in lwm2m_socket_start()
1222 ret = -errno; in lwm2m_socket_start()
1226 ret = zsock_fcntl(client_ctx->sock_fd, F_SETFL, flags | O_NONBLOCK); in lwm2m_socket_start()
1227 if (ret == -1) { in lwm2m_socket_start()
1228 ret = -errno; in lwm2m_socket_start()
1233 LOG_INF("Connected, sock id %d", client_ctx->sock_fd); in lwm2m_socket_start()
1242 int sock_fd = client_ctx->sock_fd; in lwm2m_socket_close()
1245 client_ctx->sock_fd = -1; in lwm2m_socket_close()
1268 ret = lwm2m_get_res_buf(&LWM2M_OBJ(0, client_ctx->sec_obj_inst, 0), (void **)&url, NULL, in lwm2m_engine_start()
1310 return -EPERM; in lwm2m_engine_resume()
1331 (void)k_mutex_lock(&ctx->lock, K_FOREVER); in lwm2m_client_lock()
1336 k_mutex_unlock(&ctx->lock); in lwm2m_client_unlock()
1345 /* Reset all socket handles to -1 so unused ones are ignored by zsock_poll() */ in lwm2m_engine_init()
1347 sock_fds[i].fd = -1; in lwm2m_engine_init()
1354 if (efd == -1) { in lwm2m_engine_init()
1358 return -err; in lwm2m_engine_init()
1360 /* Last poll-handle is reserved for control eventfd */ in lwm2m_engine_init()
1361 sock_fds[MAX_POLL_FD - 1].fd = efd; in lwm2m_engine_init()
1372 int ret = init->f(); in lwm2m_engine_init()
1383 k_thread_name_set(&engine_thread_data, "lwm2m-sock-recv"); in lwm2m_engine_init()