1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Transmission Control Protocol (TCP)                                 */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_tcp.h"
30 #ifdef NX_ENABLE_HTTP_PROXY
31 #include "nx_http_proxy_client.h"
32 #endif /* NX_ENABLE_HTTP_PROXY */
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _nx_tcp_socket_state_wait                           PORTABLE C      */
40 /*                                                           6.2.0        */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Yuxin Zhou, Microsoft Corporation                                   */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function waits for the specified socket to reach the specified */
48 /*    TCP state.                                                          */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    socket_ptr                            Pointer to socket             */
53 /*    desired_state                         Desired TCP state             */
54 /*    wait_option                           Suspension option             */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    status                                Completion status             */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_thread_sleep                       Sleep to wait for state change*/
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application                                                         */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
73 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  10-31-2022     Wenhui Xie               Modified comment(s), and      */
76 /*                                            supported HTTP Proxy,       */
77 /*                                            resulting in version 6.2.0  */
78 /*                                                                        */
79 /**************************************************************************/
_nx_tcp_socket_state_wait(NX_TCP_SOCKET * socket_ptr,UINT desired_state,ULONG wait_option)80 UINT  _nx_tcp_socket_state_wait(NX_TCP_SOCKET *socket_ptr, UINT desired_state, ULONG wait_option)
81 {
82 
83     /* If trace is enabled, insert this event into the trace buffer.  */
84     NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_SOCKET_STATE_WAIT, socket_ptr -> nx_tcp_socket_ip_ptr, socket_ptr, desired_state, socket_ptr -> nx_tcp_socket_state, NX_TRACE_TCP_EVENTS, 0, 0);
85 
86     /* Loop to wait for the desired socket state.   */
87     for (;;)
88     {
89 
90         /* Determine if the socket pointer is still valid.  */
91         if (socket_ptr -> nx_tcp_socket_id != NX_TCP_ID)
92         {
93 
94             /* Not still valid, return an error.  */
95             return(NX_PTR_ERROR);
96         }
97 
98         /* Determine if the desired state is present.  */
99         if (socket_ptr -> nx_tcp_socket_state == desired_state)
100         {
101 
102 #ifdef NX_ENABLE_HTTP_PROXY
103             if ((desired_state != NX_TCP_ESTABLISHED) ||
104                 (!socket_ptr -> nx_tcp_socket_ip_ptr -> nx_ip_http_proxy_enable) ||
105                 (!socket_ptr -> nx_tcp_socket_client_type) ||
106                 (socket_ptr -> nx_tcp_socket_http_proxy_state == NX_HTTP_PROXY_STATE_CONNECTED))
107 #endif /* NX_ENABLE_HTTP_PROXY */
108             {
109                 /* The desired state is present, return success!  */
110                 return(NX_SUCCESS);
111             }
112         }
113 
114         /* Check to see if there is more time to wait.  */
115         if (wait_option)
116         {
117 
118             /* Yes, there is more time... sleep for a tick.  */
119             tx_thread_sleep(1);
120 
121             /* Decrease the wait time.  */
122             wait_option--;
123         }
124         else
125         {
126 
127             /* Timeout, just return an error.  */
128             return(NX_NOT_SUCCESSFUL);
129         }
130     }
131 }
132 
133