net: websocket: Refactor because of timeout overhaul

Mention in websocket API documentation that the timeout value
is in milliseconds. Check timeout values properly using K_TIMEOUT_EQ()
macro.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
diff --git a/include/net/websocket.h b/include/net/websocket.h
index 8711427..ef78bed 100644
--- a/include/net/websocket.h
+++ b/include/net/websocket.h
@@ -117,8 +117,8 @@
  *        packets to the Websocket server.
  * @param req Websocket request. User should allocate and fill the request
  *        data.
- * @param timeout Max timeout to wait for the connection. The timeout value
- *        cannot be 0 as there would be no time to receive the data.
+ * @param timeout Max timeout to wait for the connection. The timeout value is
+ *        in milliseconds. Value NET_WAIT_FOREVER means to wait forever.
  * @param user_data User specified data that is passed to the callback.
  *
  * @return Websocket id to be used when sending/receiving Websocket data.
@@ -142,7 +142,8 @@
  *        must have opcode WEBSOCKET_OPCODE_CONTINUE. If final == true and this
  *        is the only message, then opcode should have proper opcode (text or
  *        binary) set.
- * @param timeout How long to try to send the message.
+ * @param timeout How long to try to send the message. The value is in
+ *        milliseconds. Value NET_WAIT_FOREVER means to wait forever.
  *
  * @return <0 if error, >=0 amount of bytes sent
  */
@@ -162,6 +163,8 @@
  * @param message_type Type of the message.
  * @param remaining How much there is data left in the message after this read.
  * @param timeout How long to try to receive the message.
+ *        The value is in milliseconds. Value NET_WAIT_FOREVER means to wait
+ *        forever.
  *
  * @return <0 if error, >=0 amount of bytes received
  */
diff --git a/samples/net/sockets/websocket_client/src/main.c b/samples/net/sockets/websocket_client/src/main.c
index b8b4528..b0c8d9a 100644
--- a/samples/net/sockets/websocket_client/src/main.c
+++ b/samples/net/sockets/websocket_client/src/main.c
@@ -175,7 +175,7 @@
 static ssize_t sendall_with_ws_api(int sock, const void *buf, size_t len)
 {
 	return websocket_send_msg(sock, buf, len, WEBSOCKET_OPCODE_DATA_TEXT,
-				  true, true, K_FOREVER);
+				  true, true, NET_WAIT_FOREVER);
 }
 
 static ssize_t sendall_with_bsd_api(int sock, const void *buf, size_t len)
@@ -199,7 +199,7 @@
 					 buf_len - read_pos,
 					 &message_type,
 					 &remaining,
-					 K_NO_WAIT);
+					 0);
 		if (ret <= 0) {
 			if (ret == -EAGAIN) {
 				k_sleep(K_MSEC(50));
@@ -326,7 +326,7 @@
 	};
 	int sock4 = -1, sock6 = -1;
 	int websock4 = -1, websock6 = -1;
-	s32_t timeout = K_SECONDS(3);
+	s32_t timeout = 3 * MSEC_PER_SEC;
 	struct sockaddr_in6 addr6;
 	struct sockaddr_in addr4;
 	size_t amount;
diff --git a/subsys/net/lib/websocket/websocket.c b/subsys/net/lib/websocket/websocket.c
index fa3046b..4395439 100644
--- a/subsys/net/lib/websocket/websocket.c
+++ b/subsys/net/lib/websocket/websocket.c
@@ -271,7 +271,6 @@
 	ctx->real_sock = sock;
 	ctx->tmp_buf = wreq->tmp_buf;
 	ctx->tmp_buf_len = wreq->tmp_buf_len;
-	ctx->timeout = timeout;
 	ctx->sec_accept_key = sec_accept_key;
 	ctx->http_cb = wreq->http_cb;
 
@@ -475,8 +474,14 @@
 	 */
 	return verify_sent_and_received_msg(&msg, !(header[1] & BIT(7)));
 #else
+	k_timeout_t tout = K_FOREVER;
+
+	if (timeout != NET_WAIT_FOREVER) {
+		tout = K_MSEC(timeout);
+	}
+
 	return sendmsg(ctx->real_sock, &msg,
-		       timeout == K_NO_WAIT ? MSG_DONTWAIT : 0);
+		       K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
 #endif /* CONFIG_NET_TEST */
 }
 
@@ -659,6 +664,11 @@
 	int recv_len = 0;
 	size_t can_copy, left;
 	int ret;
+	k_timeout_t tout = K_FOREVER;
+
+	if (timeout != NET_WAIT_FOREVER) {
+		tout = K_MSEC(timeout);
+	}
 
 #if defined(CONFIG_NET_TEST)
 	/* Websocket unit test does not use socket layer but feeds
@@ -697,7 +707,7 @@
 #else
 		ret = recv(ctx->real_sock, &ctx->tmp_buf[ctx->tmp_buf_pos],
 			   ctx->tmp_buf_len - ctx->tmp_buf_pos,
-			   timeout == K_NO_WAIT ? MSG_DONTWAIT : 0);
+			   K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
 #endif /* CONFIG_NET_TEST */
 
 		if (ret < 0) {
@@ -783,7 +793,7 @@
 		ret = input_len;
 #else
 		ret = recv(ctx->real_sock, ctx->tmp_buf, ctx->tmp_buf_len,
-			   timeout == K_NO_WAIT ? MSG_DONTWAIT : 0);
+			   K_TIMEOUT_EQ(tout, K_NO_WAIT) ? MSG_DONTWAIT : 0);
 #endif /* CONFIG_NET_TEST */
 
 		if (ret < 0) {
@@ -903,13 +913,13 @@
 
 static ssize_t websocket_read_vmeth(void *obj, void *buffer, size_t count)
 {
-	return (ssize_t)websocket_recv(obj, buffer, count, K_FOREVER);
+	return (ssize_t)websocket_recv(obj, buffer, count, NET_WAIT_FOREVER);
 }
 
 static ssize_t websocket_write_vmeth(void *obj, const void *buffer,
 				     size_t count)
 {
-	return (ssize_t)websocket_send(obj, buffer, count, K_FOREVER);
+	return (ssize_t)websocket_send(obj, buffer, count, NET_WAIT_FOREVER);
 }
 
 static ssize_t websocket_sendto_ctx(void *obj, const void *buf, size_t len,
@@ -918,10 +928,10 @@
 				    socklen_t addrlen)
 {
 	struct websocket_context *ctx = obj;
-	s32_t timeout = K_FOREVER;
+	s32_t timeout = NET_WAIT_FOREVER;
 
 	if (flags & ZSOCK_MSG_DONTWAIT) {
-		timeout = K_NO_WAIT;
+		timeout = 0;
 	}
 
 	ARG_UNUSED(dest_addr);
@@ -935,10 +945,10 @@
 				      socklen_t *addrlen)
 {
 	struct websocket_context *ctx = obj;
-	s32_t timeout = K_FOREVER;
+	s32_t timeout = NET_WAIT_FOREVER;
 
 	if (flags & ZSOCK_MSG_DONTWAIT) {
-		timeout = K_NO_WAIT;
+		timeout = 0;
 	}
 
 	ARG_UNUSED(src_addr);
diff --git a/subsys/net/lib/websocket/websocket_internal.h b/subsys/net/lib/websocket/websocket_internal.h
index 036d4ff..150beb3 100644
--- a/subsys/net/lib/websocket/websocket_internal.h
+++ b/subsys/net/lib/websocket/websocket_internal.h
@@ -78,10 +78,6 @@
 	/** Websocket connection masking value */
 	u32_t masking_value;
 
-	/** Timeout for Websocket operations.
-	 */
-	s32_t timeout;
-
 	/** Amount of data received. */
 	u64_t total_read;
 
diff --git a/tests/net/socket/websocket/src/main.c b/tests/net/socket/websocket/src/main.c
index 9cd78da..5a3dcb0 100644
--- a/tests/net/socket/websocket/src/main.c
+++ b/tests/net/socket/websocket/src/main.c
@@ -73,7 +73,7 @@
 	ctx_ptr = POINTER_TO_INT(&test_data);
 
 	return websocket_recv_msg(ctx_ptr, recv_buf, recv_len,
-				  msg_type, remaining, K_NO_WAIT);
+				  msg_type, remaining, 0);
 }
 
 /* Websocket frame, header is 6 bytes, FIN bit is set, opcode is text (1),
@@ -347,7 +347,7 @@
 	ret = websocket_send_msg(POINTER_TO_INT(&ctx),
 				 lorem_ipsum, test_msg_len,
 				 WEBSOCKET_OPCODE_DATA_TEXT, true, true,
-				 K_FOREVER);
+				 NET_WAIT_FOREVER);
 	zassert_equal(ret, test_msg_len,
 		      "Should have sent %zd bytes but sent %d instead",
 		      test_msg_len, ret);
@@ -367,7 +367,7 @@
 
 	ret = websocket_send_msg(POINTER_TO_INT(&ctx), lorem_ipsum,
 				 test_msg_len, WEBSOCKET_OPCODE_DATA_TEXT,
-				 false, true, K_FOREVER);
+				 false, true, NET_WAIT_FOREVER);
 	zassert_equal(ret, test_msg_len,
 		      "1st should have sent %zd bytes but sent %d instead",
 		      test_msg_len, ret);