• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2024, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef PLATFORM_SIMULATION_SOCKET_UTILS_H_
30 #define PLATFORM_SIMULATION_SOCKET_UTILS_H_
31 
32 #include "platform-simulation.h"
33 
34 /**
35  * Represents a socket for communication with other simulation node.
36  *
37  * This is used for emulation of 15.4 radio or other interfaces.
38  */
39 typedef struct utilsSocket
40 {
41     bool     mInitialized; ///< Whether or not initialized.
42     bool     mUseIp6;      ///< Whether IPv6 or IPv4.
43     int      mTxFd;        ///< RX file descriptor.
44     int      mRxFd;        ///< TX file descriptor.
45     uint16_t mPortBase;    ///< Base port number value.
46     uint16_t mPort;        ///< The port number used by this node
47     union
48     {
49         struct sockaddr_in  mSockAddr4; ///< The IPv4 group sock address.
50         struct sockaddr_in6 mSockAddr6; ///< The IPv4 group sock address.
51     } mGroupAddr;                       ///< The group sock address for simulating radio.
52 } utilsSocket;
53 
54 extern const char *gLocalInterface; ///< Local interface name or address to use for sockets
55 
56 /**
57  * Adds a file descriptor (FD) to a given FD set.
58  *
59  * @param[in] aFd      The FD to add.
60  * @param[in] aFdSet   The FD set to add to.
61  * @param[in] aMaxFd   A pointer to track maximum FD in @p aFdSet (can be NULL).
62  */
63 void utilsAddFdToFdSet(int aFd, fd_set *aFdSet, int *aMaxFd);
64 
65 /**
66  * Initializes the socket.
67  *
68  * @param[in] aSocket     The socket to initialize.
69  * @param[in] aPortBase   The base port number value. Nodes will determine their port as `aPortBased + gNodeId`.
70  */
71 void utilsInitSocket(utilsSocket *aSocket, uint16_t aPortBase);
72 
73 /**
74  * De-initializes the socket.
75  *
76  * @param[in] aSocket   The socket to de-initialize.
77  */
78 void utilsDeinitSocket(utilsSocket *aSocket);
79 
80 /**
81  * Adds sockets RX FD to a given FD set.
82  *
83  * @param[in] aSocket   The socket.
84  * @param[in] aFdSet    The (read) FD set to add to.
85  * @param[in] aMaxFd    A pointer to track maximum FD in @p aFdSet (can be NULL).
86  */
87 void utilsAddSocketRxFd(const utilsSocket *aSocket, fd_set *aFdSet, int *aMaxFd);
88 
89 /**
90  * Adds sockets TX FD to a given FD set.
91  *
92  * @param[in] aSocket   The socket.
93  * @param[in] aFdSet    The (write) FD set to add to.
94  * @param[in] aMaxFd    A pointer to track maximum FD in @p aFdSet (can be NULL).
95  */
96 void utilsAddSocketTxFd(const utilsSocket *aSocket, fd_set *aFdSet, int *aMaxFd);
97 
98 /**
99  * Indicates whether the socket can receive.
100  *
101  * @param[in] aSocket       The socket.
102  * @param[in] aReadFdSet    The read FD set.
103  *
104  * @retval TRUE   The socket RX FD is in @p aReadFdSet, and socket can receive.
105  * @retval FALSE  The socket RX FD is not in @p aReadFdSet. Socket is not ready to receive.
106  */
107 bool utilsCanSocketReceive(const utilsSocket *aSocket, const fd_set *aReadFdSet);
108 
109 /**
110  * Indicates whether the socket can send.
111  *
112  * @param[in] aSocket   The socket.
113  * @param[in] aFdSet    The write FD set.
114  *
115  * @retval TRUE   The socket TX FD is in @p aWriteFdSet, and socket can send.
116  * @retval FALSE  The socket TX FD is not in @p aWriteFdSet. Socket is not ready to send.
117  */
118 bool utilsCanSocketSend(const utilsSocket *aSocket, const fd_set *aWriteFdSet);
119 
120 /**
121  * Receives data from socket.
122  *
123  * MUST be used when `utilsCanSocketReceive()` returns `TRUE.
124  *
125  * @param[in] aSocket          The socket.
126  * @param[out] aBuffer         The buffer to output the read content.
127  * @param[in]  aBufferSize     Maximum size of buffer in bytes.
128  * @param[out] aSenderNodeId   A pointer to return the Node ID of the sender (derived from the port number).
129  *                             Can be NULL if not needed.
130  *
131  * @returns The number of received bytes written into @p aBuffer.
132  */
133 uint16_t utilsReceiveFromSocket(const utilsSocket *aSocket,
134                                 void              *aBuffer,
135                                 uint16_t           aBufferSize,
136                                 uint16_t          *aSenderNodeId);
137 
138 /**
139  * Sends data over the socket.
140  *
141  * @param[in] aSocket         The socket.
142  * @param[in] aBuffer         The buffer containing the bytes to sent.
143  * @param[in]  aBufferSize    Size of data in @p buffer in bytes.
144  */
145 void utilsSendOverSocket(const utilsSocket *aSocket, const void *aBuffer, uint16_t aBufferLength);
146 
147 #endif // PLATFORM_SIMULATION_SOCKET_UTILS_H_
148