Lines Matching refs:client
29 static void cancel_requests_with(struct coap_client *client, int error);
30 static int recv_response(struct coap_client *client, struct coap_packet *response, bool *truncated);
31 static int handle_response(struct coap_client *client, const struct coap_packet *response,
33 static struct coap_client_internal_request *get_request_with_mid(struct coap_client *client,
87 static int coap_client_schedule_poll(struct coap_client *client, int sock, in coap_client_schedule_poll() argument
91 client->fd = sock; in coap_client_schedule_poll()
119 static bool has_ongoing_request(struct coap_client *client) in has_ongoing_request() argument
122 if (client->requests[i].request_ongoing == true) { in has_ongoing_request()
130 static bool has_ongoing_exchange(struct coap_client *client) in has_ongoing_exchange() argument
133 if (client->requests[i].request_ongoing == true || in has_ongoing_exchange()
134 !exchange_lifetime_exceeded(&client->requests[i])) { in has_ongoing_exchange()
142 static bool has_timeout_expired(struct coap_client *client) in has_timeout_expired() argument
145 if (timeout_expired(&client->requests[i])) { in has_timeout_expired()
152 static struct coap_client_internal_request *get_free_request(struct coap_client *client) in get_free_request() argument
155 if (client->requests[i].request_ongoing == false && in get_free_request()
156 exchange_lifetime_exceeded(&client->requests[i])) { in get_free_request()
157 return &client->requests[i]; in get_free_request()
197 static int coap_client_init_request(struct coap_client *client, in coap_client_init_request() argument
206 memset(client->send_buf, 0, sizeof(client->send_buf)); in coap_client_init_request()
216 ret = coap_packet_init(&internal_req->request, client->send_buf, MAX_COAP_MSG_LEN, in coap_client_init_request()
348 int coap_client_req(struct coap_client *client, int sock, const struct sockaddr *addr, in coap_client_req() argument
354 if (client == NULL || sock < 0 || req == NULL || req->path == NULL) { in coap_client_req()
358 k_mutex_lock(&client->lock, K_FOREVER); in coap_client_req()
360 internal_req = get_free_request(client); in coap_client_req()
369 if (client->fd != sock && has_ongoing_request(client)) { in coap_client_req()
376 if (memcmp(&client->address, addr, sizeof(*addr)) != 0) { in coap_client_req()
377 if (has_ongoing_request(client)) { in coap_client_req()
383 memcpy(&client->address, addr, sizeof(*addr)); in coap_client_req()
384 client->socklen = sizeof(client->address); in coap_client_req()
387 if (client->socklen != 0) { in coap_client_req()
388 if (has_ongoing_request(client)) { in coap_client_req()
394 memset(&client->address, 0, sizeof(client->address)); in coap_client_req()
395 client->socklen = 0; in coap_client_req()
401 ret = coap_client_init_request(client, req, internal_req, false); in coap_client_req()
407 if (client->send_echo) { in coap_client_req()
409 client->echo_option.value, client->echo_option.len); in coap_client_req()
414 client->send_echo = false; in coap_client_req()
417 ret = coap_client_schedule_poll(client, sock, req, internal_req); in coap_client_req()
424 &client->address, params); in coap_client_req()
442 &client->address, client->socklen); in coap_client_req()
456 k_mutex_unlock(&client->lock); in coap_client_req()
483 static int resend_request(struct coap_client *client, in resend_request() argument
500 ret = coap_client_init_request(client, &internal_req->coap_request, in resend_request()
507 ret = send_request(client->fd, internal_req->request.data, in resend_request()
508 internal_req->request.offset, 0, &client->address, in resend_request()
509 client->socklen); in resend_request()
528 static void coap_client_resend_handler(struct coap_client *client) in coap_client_resend_handler() argument
532 k_mutex_lock(&client->lock, K_FOREVER); in coap_client_resend_handler()
535 if (timeout_expired(&client->requests[i])) { in coap_client_resend_handler()
536 if (!client->requests[i].coap_request.confirmable) { in coap_client_resend_handler()
537 release_internal_request(&client->requests[i]); in coap_client_resend_handler()
541 ret = resend_request(client, &client->requests[i]); in coap_client_resend_handler()
543 report_callback_error(&client->requests[i], ret); in coap_client_resend_handler()
544 release_internal_request(&client->requests[i]); in coap_client_resend_handler()
549 k_mutex_unlock(&client->lock); in coap_client_resend_handler()
596 struct coap_client *client = get_client(fds[i].fd); in handle_poll() local
598 if (!client) { in handle_poll()
604 coap_client_resend_handler(client); in handle_poll()
610 ret = recv_response(client, &response, &response_truncated); in handle_poll()
616 cancel_requests_with(client, -EIO); in handle_poll()
620 k_mutex_lock(&client->lock, K_FOREVER); in handle_poll()
621 ret = handle_response(client, &response, response_truncated); in handle_poll()
626 k_mutex_unlock(&client->lock); in handle_poll()
630 cancel_requests_with(client, -EIO); in handle_poll()
634 cancel_requests_with(client, -EIO); in handle_poll()
638 cancel_requests_with(client, -EIO); in handle_poll()
645 static int recv_response(struct coap_client *client, struct coap_packet *response, bool *truncated) in recv_response() argument
656 memset(client->recv_buf, 0, sizeof(client->recv_buf)); in recv_response()
657 total_len = receive(client->fd, client->recv_buf, sizeof(client->recv_buf), flags, in recv_response()
658 &client->address, &client->socklen); in recv_response()
668 available_len = MIN(total_len, sizeof(client->recv_buf)); in recv_response()
673 ret = coap_packet_parse(response, client->recv_buf, available_len, NULL, 0); in recv_response()
681 static int send_ack(struct coap_client *client, const struct coap_packet *req, in send_ack() argument
687 ret = coap_ack_init(&ack, req, client->send_buf, MAX_COAP_MSG_LEN, response_code); in send_ack()
693 ret = send_request(client->fd, ack.data, ack.offset, 0, &client->address, client->socklen); in send_ack()
702 static int send_rst(struct coap_client *client, const struct coap_packet *req) in send_rst() argument
707 ret = coap_rst_init(&rst, req, client->send_buf, MAX_COAP_MSG_LEN); in send_rst()
713 ret = send_request(client->fd, rst.data, rst.offset, 0, &client->address, client->socklen); in send_rst()
723 struct coap_client *client, const struct coap_packet *resp) in get_request_with_token() argument
732 if (client->requests[i].request_ongoing || in get_request_with_token()
733 !exchange_lifetime_exceeded(&client->requests[i])) { in get_request_with_token()
734 if (client->requests[i].request_tkl == 0) { in get_request_with_token()
737 if (client->requests[i].request_tkl != response_tkl) { in get_request_with_token()
740 if (memcmp(&client->requests[i].request_token, &response_token, in get_request_with_token()
742 return &client->requests[i]; in get_request_with_token()
750 static struct coap_client_internal_request *get_request_with_mid(struct coap_client *client, in get_request_with_mid() argument
754 if (client->requests[i].request_ongoing) { in get_request_with_mid()
755 if (client->requests[i].last_id == (int)mid) { in get_request_with_mid()
756 return &client->requests[i]; in get_request_with_mid()
769 static int handle_response(struct coap_client *client, const struct coap_packet *response, in handle_response() argument
794 internal_req = get_request_with_mid(client, response_id); in handle_response()
807 internal_req = get_request_with_mid(client, response_id); in handle_response()
818 internal_req = get_request_with_token(client, response); in handle_response()
821 (void) send_rst(client, response); /* Ignore errors, unrelated to our queries */ in handle_response()
826 if (find_echo_option(response, &client->echo_option)) { in handle_response()
829 ret = coap_client_init_request(client, &internal_req->coap_request, in handle_response()
838 client->echo_option.value, in handle_response()
839 client->echo_option.len); in handle_response()
849 &internal_req->request, &client->address, in handle_response()
859 ret = send_request(client->fd, internal_req->request.data, in handle_response()
860 internal_req->request.offset, 0, &client->address, in handle_response()
861 client->socklen); in handle_response()
870 client->send_echo = true; in handle_response()
877 ret = send_ack(client, response, COAP_CODE_EMPTY); in handle_response()
893 (void) send_rst(client, response); in handle_response()
966 ret = coap_client_init_request(client, &internal_req->coap_request, internal_req, in handle_response()
976 &client->address, ¶ms); in handle_response()
983 ret = send_request(client->fd, internal_req->request.data, in handle_response()
984 internal_req->request.offset, 0, &client->address, in handle_response()
985 client->socklen); in handle_response()
1003 static void cancel_requests_with(struct coap_client *client, int error) in cancel_requests_with() argument
1005 k_mutex_lock(&client->lock, K_FOREVER); in cancel_requests_with()
1007 for (int i = 0; i < ARRAY_SIZE(client->requests); i++) { in cancel_requests_with()
1008 if (client->requests[i].request_ongoing == true) { in cancel_requests_with()
1015 report_callback_error(&client->requests[i], error); in cancel_requests_with()
1016 release_internal_request(&client->requests[i]); in cancel_requests_with()
1022 reset_internal_request(&client->requests[i]); in cancel_requests_with()
1025 k_mutex_unlock(&client->lock); in cancel_requests_with()
1029 void coap_client_cancel_requests(struct coap_client *client) in coap_client_cancel_requests() argument
1031 cancel_requests_with(client, -ECANCELED); in coap_client_cancel_requests()
1055 void coap_client_cancel_request(struct coap_client *client, struct coap_client_request *req) in coap_client_cancel_request() argument
1057 k_mutex_lock(&client->lock, K_FOREVER); in coap_client_cancel_request()
1060 if (client->requests[i].request_ongoing && in coap_client_cancel_request()
1061 requests_match(&client->requests[i].coap_request, req)) { in coap_client_cancel_request()
1063 report_callback_error(&client->requests[i], -ECANCELED); in coap_client_cancel_request()
1064 release_internal_request(&client->requests[i]); in coap_client_cancel_request()
1068 k_mutex_unlock(&client->lock); in coap_client_cancel_request()
1094 int coap_client_init(struct coap_client *client, const char *info) in coap_client_init() argument
1096 if (client == NULL) { in coap_client_init()
1106 k_mutex_init(&client->lock); in coap_client_init()
1108 clients[num_clients] = client; in coap_client_init()