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 /**   User Datagram Protocol (UDP)                                        */
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_udp.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_udp_socket_bytes_available                      PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function determines the number of bytes available on a UDP     */
45 /*    socket for reception.                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    socket_ptr                            Pointer to the UDP sockete    */
50 /*    bytes_available                       Number of bytes returned to   */
51 /*                                             the caller.                */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    tx_mutex_get                          Obtain protection             */
60 /*    tx_mutex_put                          Release protection            */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_udp_socket_bytes_available(NX_UDP_SOCKET * socket_ptr,ULONG * bytes_available)75 UINT  _nx_udp_socket_bytes_available(NX_UDP_SOCKET *socket_ptr, ULONG *bytes_available)
76 {
77 NX_IP     *ip_ptr;
78 NX_PACKET *packet_ptr;
79 INT        count;
80 
81     /* Setup IP pointer. */
82     ip_ptr = socket_ptr -> nx_udp_socket_ip_ptr;
83 
84     /* Obtain the IP mutex so we can examine the bound port.  */
85     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
86 
87     *bytes_available = 0;
88 
89     /* Determine if the socket is currently bound. */
90     if (!socket_ptr -> nx_udp_socket_bound_next)
91     {
92         /* Release mutex */
93         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
94 
95         return(NX_NOT_SUCCESSFUL);
96     }
97 
98     packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
99 
100     /* Loop through all the packets on the queue and find out the total
101        number of bytes in the rx queue that are available to the applciation. */
102     for (count = 0; count < (INT)(socket_ptr -> nx_udp_socket_receive_count); count++)
103     {
104 
105         *bytes_available += (packet_ptr -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER));
106 
107         /* Move on to the next packet. */
108         packet_ptr = packet_ptr -> nx_packet_queue_next;
109     }
110 
111     /* Release protection.  */
112     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
113 
114     return(NX_SUCCESS);
115 }
116 
117