From 1b04d44247c00366af84cb541b50cb0cbc6cb348 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Thu, 2 Apr 2020 21:07:27 +0300 Subject: [PATCH] net: http: Refactor because of timeout overhaul Use k_timeout_t internally, no change to user API. Clarify the documentation of the timeout parameter that it is in milliseconds. Signed-off-by: Jukka Rissanen --- include/net/http_client.h | 3 ++- samples/net/sockets/http_client/src/main.c | 2 +- subsys/net/lib/http/http_client.c | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/net/http_client.h b/include/net/http_client.h index e310aaa5fb6..a8c0ef18f38 100644 --- a/include/net/http_client.h +++ b/include/net/http_client.h @@ -174,7 +174,7 @@ struct http_client_internal_data { int sock; /** Request timeout */ - s32_t timeout; + k_timeout_t timeout; }; /** @@ -271,6 +271,7 @@ struct http_request { * @param req HTTP request information * @param timeout Max timeout to wait for the data. The timeout value cannot be * 0 as there would be no time to receive the data. + * The timeout value is in milliseconds. * @param user_data User specified data that is passed to the callback. * * @return <0 if error, >=0 amount of data sent to the server diff --git a/samples/net/sockets/http_client/src/main.c b/samples/net/sockets/http_client/src/main.c index 2a9db583395..87eeb821ce9 100644 --- a/samples/net/sockets/http_client/src/main.c +++ b/samples/net/sockets/http_client/src/main.c @@ -152,7 +152,7 @@ void main(void) struct sockaddr_in6 addr6; struct sockaddr_in addr4; int sock4 = -1, sock6 = -1; - s32_t timeout = K_SECONDS(3); + s32_t timeout = 3 * MSEC_PER_SEC; int ret; int port = HTTP_PORT; diff --git a/subsys/net/lib/http/http_client.c b/subsys/net/lib/http/http_client.c index 9f34075c626..4b0c34278dc 100644 --- a/subsys/net/lib/http/http_client.c +++ b/subsys/net/lib/http/http_client.c @@ -490,9 +490,14 @@ int http_client_req(int sock, struct http_request *req, req->internal.response.recv_buf = req->recv_buf; req->internal.response.recv_buf_len = req->recv_buf_len; req->internal.user_data = user_data; - req->internal.timeout = timeout; req->internal.sock = sock; + if (timeout == NET_WAIT_FOREVER) { + req->internal.timeout = K_FOREVER; + } else { + req->internal.timeout = K_MSEC(timeout); + } + method = http_method_str(req->method); ret = http_send_data(sock, send_buf, send_buf_max_len, &send_buf_pos, @@ -635,9 +640,11 @@ int http_client_req(int sock, struct http_request *req, http_client_init_parser(&req->internal.parser, &req->internal.parser_settings); - if (timeout != K_FOREVER && timeout != K_NO_WAIT) { + if (!K_TIMEOUT_EQ(req->internal.timeout, K_FOREVER) && + !K_TIMEOUT_EQ(req->internal.timeout, K_NO_WAIT)) { k_delayed_work_init(&req->internal.work, http_timeout); - (void)k_delayed_work_submit(&req->internal.work, timeout); + (void)k_delayed_work_submit(&req->internal.work, + req->internal.timeout); } /* Request is sent, now wait data to be received */ @@ -648,7 +655,8 @@ int http_client_req(int sock, struct http_request *req, NET_DBG("Received %d bytes", total_recv); } - if (timeout != K_FOREVER && timeout != K_NO_WAIT) { + if (!K_TIMEOUT_EQ(req->internal.timeout, K_FOREVER) && + !K_TIMEOUT_EQ(req->internal.timeout, K_NO_WAIT)) { (void)k_delayed_work_cancel(&req->internal.work); }