1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 /**
28  * @file tcp_sockets_wrapper.h
29  * @brief TCP transport functions wrapper.
30  */
31 
32 #ifndef TCP_SOCKETS_WRAPPER_H
33 #define TCP_SOCKETS_WRAPPER_H
34 
35 
36 /* Standard includes. */
37 #include <stdint.h>
38 
39 /* FreeRTOS Kernel includes. */
40 #include "FreeRTOS.h"
41 
42 /* Error codes. */
43 #define TCP_SOCKETS_ERRNO_NONE                ( 0 )   /*!< No error. */
44 #define TCP_SOCKETS_ERRNO_ERROR               ( -1 )  /*!< Catch-all sockets error code. */
45 #define TCP_SOCKETS_ERRNO_EWOULDBLOCK         ( -2 )  /*!< A resource is temporarily unavailable. */
46 #define TCP_SOCKETS_ERRNO_ENOMEM              ( -3 )  /*!< Memory allocation failed. */
47 #define TCP_SOCKETS_ERRNO_EINVAL              ( -4 )  /*!< Invalid argument. */
48 #define TCP_SOCKETS_ERRNO_ENOPROTOOPT         ( -5 )  /*!< A bad option was specified . */
49 #define TCP_SOCKETS_ERRNO_ENOTCONN            ( -6 )  /*!< The supplied socket is not connected. */
50 #define TCP_SOCKETS_ERRNO_EISCONN             ( -7 )  /*!< The supplied socket is already connected. */
51 #define TCP_SOCKETS_ERRNO_ECLOSED             ( -8 )  /*!< The supplied socket has already been closed. */
52 #define TCP_SOCKETS_ERRNO_PERIPHERAL_RESET    ( -9 )  /*!< Communications peripheral has been reset. */
53 #define TCP_SOCKETS_ERRNO_ENOSPC              ( -10 ) /*!< No space left on device */
54 #define TCP_SOCKETS_ERRNO_EINTR               ( -11 ) /*!< Interrupted system call */
55 
56 #ifndef SOCKET_T_TYPEDEFED
57     struct xSOCKET;
58     typedef struct xSOCKET * Socket_t; /**< @brief Socket handle data type. */
59 #endif
60 
61 /**
62  * @brief Establish a connection to server.
63  *
64  * @param[out] pTcpSocket The output parameter to return the created socket descriptor.
65  * @param[in] pHostName Server hostname to connect to.
66  * @param[in] pServerInfo Server port to connect to.
67  * @param[in] receiveTimeoutMs Timeout (in milliseconds) for transport receive.
68  * @param[in] sendTimeoutMs Timeout (in milliseconds) for transport send.
69  *
70  * @note A timeout of 0 means infinite timeout.
71  *
72  * @return Non-zero value on error, 0 on success.
73  */
74 BaseType_t TCP_Sockets_Connect( Socket_t * pTcpSocket,
75                                 const char * pHostName,
76                                 uint16_t port,
77                                 uint32_t receiveTimeoutMs,
78                                 uint32_t sendTimeoutMs );
79 
80 /**
81  * @brief End connection to server.
82  *
83  * @param[in] tcpSocket The socket descriptor.
84  */
85 void TCP_Sockets_Disconnect( Socket_t tcpSocket );
86 
87 /**
88  * @brief Transmit data to the remote socket.
89  *
90  * The socket must have already been created using a call to TCP_Sockets_Connect().
91  *
92  * @param[in] xSocket The handle of the sending socket.
93  * @param[in] pvBuffer The buffer containing the data to be sent.
94  * @param[in] xDataLength The length of the data to be sent.
95  *
96  * @return
97  * * On success, the number of bytes actually sent is returned.
98  * * If an error occurred, a negative value is returned. @ref SocketsErrors
99  */
100 int32_t TCP_Sockets_Send( Socket_t xSocket,
101                           const void * pvBuffer,
102                           size_t xDataLength );
103 
104 /**
105  * @brief Receive data from a TCP socket.
106  *
107  * The socket must have already been created using a call to TCP_Sockets_Connect().
108  *
109  * @param[in] xSocket The handle of the socket from which data is being received.
110  * @param[out] pvBuffer The buffer into which the received data will be placed.
111  * @param[in] xBufferLength The maximum number of bytes which can be received.
112  * pvBuffer must be at least xBufferLength bytes long.
113  *
114  * @return
115  * * If the receive was successful then the number of bytes received (placed in the
116  *   buffer pointed to by pvBuffer) is returned.
117  * * If a timeout occurred before data could be received then 0 is returned (timeout
118  *   is set using @ref SOCKETS_SO_RCVTIMEO).
119  * * If an error occurred, a negative value is returned. @ref SocketsErrors
120  */
121 int32_t TCP_Sockets_Recv( Socket_t xSocket,
122                           void * pvBuffer,
123                           size_t xBufferLength );
124 
125 #endif /* ifndef TCP_SOCKETS_WRAPPER_H */
126