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 #ifndef USING_PLAINTEXT_H
28 #define USING_PLAINTEXT_H
29 
30 /**************************************************/
31 /******* DO NOT CHANGE the following order ********/
32 /**************************************************/
33 
34 /* Logging related header files are required to be included in the following order:
35  * 1. Include the header file "logging_levels.h".
36  * 2. Define LIBRARY_LOG_NAME and  LIBRARY_LOG_LEVEL.
37  * 3. Include the header file "logging_stack.h".
38  */
39 
40 /* Include header that defines log levels. */
41 #include "logging_levels.h"
42 
43 /* Logging configuration for the Sockets. */
44 #ifndef LIBRARY_LOG_NAME
45     #define LIBRARY_LOG_NAME     "PlaintextTransport"
46 #endif
47 #ifndef LIBRARY_LOG_LEVEL
48     #define LIBRARY_LOG_LEVEL    LOG_ERROR
49 #endif
50 
51 /* Prototype for the function used to print to console on Windows simulator
52  * of FreeRTOS.
53  * The function prints to the console before the network is connected;
54  * then a UDP port after the network has connected. */
55 extern void vLoggingPrintf( const char * pcFormatString,
56                             ... );
57 
58 /* Map the SdkLog macro to the logging function to enable logging
59  * on Windows simulator. */
60 #ifndef SdkLog
61     #define SdkLog( message )    vLoggingPrintf message
62 #endif
63 
64 #include "logging_stack.h"
65 
66 /************ End of logging configuration ****************/
67 
68 /* TCP Sockets Wrapper include.*/
69 #include "tcp_sockets_wrapper.h"
70 
71 /* Transport interface include. */
72 #include "transport_interface.h"
73 
74 /**
75  * @brief Parameters for the network context that uses FreeRTOS+TCP sockets.
76  */
77 typedef struct PlaintextTransportParams
78 {
79     Socket_t tcpSocket;
80 } PlaintextTransportParams_t;
81 
82 /**
83  * @brief Plain text transport Connect / Disconnect return status.
84  */
85 typedef enum PlaintextTransportStatus
86 {
87     PLAINTEXT_TRANSPORT_SUCCESS = 1,           /**< Function successfully completed. */
88     PLAINTEXT_TRANSPORT_INVALID_PARAMETER = 2, /**< At least one parameter was invalid. */
89     PLAINTEXT_TRANSPORT_CONNECT_FAILURE = 3    /**< Initial connection to the server failed. */
90 } PlaintextTransportStatus_t;
91 
92 /**
93  * @brief Create a TCP connection with FreeRTOS sockets.
94  *
95  * @param[out] pNetworkContext Pointer to a network context to contain the
96  * initialized socket handle.
97  * @param[in] pHostName The hostname of the remote endpoint.
98  * @param[in] port The destination port.
99  * @param[in] receiveTimeoutMs Receive socket timeout.
100  *
101  * @return #PLAINTEXT_TRANSPORT_SUCCESS, #PLAINTEXT_TRANSPORT_INVALID_PARAMETER,
102  * or #PLAINTEXT_TRANSPORT_CONNECT_FAILURE.
103  */
104 PlaintextTransportStatus_t Plaintext_FreeRTOS_Connect( NetworkContext_t * pNetworkContext,
105                                                        const char * pHostName,
106                                                        uint16_t port,
107                                                        uint32_t receiveTimeoutMs,
108                                                        uint32_t sendTimeoutMs );
109 
110 /**
111  * @brief Gracefully disconnect an established TCP connection.
112  *
113  * @param[in] pNetworkContext Network context containing the TCP socket handle.
114  *
115  * @return #PLAINTEXT_TRANSPORT_SUCCESS, or #PLAINTEXT_TRANSPORT_INVALID_PARAMETER.
116  */
117 PlaintextTransportStatus_t Plaintext_FreeRTOS_Disconnect( const NetworkContext_t * pNetworkContext );
118 
119 /**
120  * @brief Receives data from an established TCP connection.
121  *
122  * @note When the number of bytes requested is 1, the TCP socket's Rx stream
123  * is checked for available bytes to read. If there are none, this function
124  * immediately returns 0 without blocking.
125  *
126  * @param[in] pNetworkContext The network context containing the TCP socket
127  * handle.
128  * @param[out] pBuffer Buffer to receive bytes into.
129  * @param[in] bytesToRecv Number of bytes to receive from the network.
130  *
131  * @return Number of bytes received if successful; 0 if the socket times out;
132  * Negative value on error.
133  */
134 int32_t Plaintext_FreeRTOS_recv( NetworkContext_t * pNetworkContext,
135                                  void * pBuffer,
136                                  size_t bytesToRecv );
137 
138 /**
139  * @brief Sends data over an established TCP connection.
140  *
141  * @param[in] pNetworkContext The network context containing the TCP socket
142  * handle.
143  * @param[in] pBuffer Buffer containing the bytes to send.
144  * @param[in] bytesToSend Number of bytes to send from the buffer.
145  *
146  * @return Number of bytes sent on success; else a negative value.
147  */
148 int32_t Plaintext_FreeRTOS_send( NetworkContext_t * pNetworkContext,
149                                  const void * pBuffer,
150                                  size_t bytesToSend );
151 
152 #endif /* ifndef USING_PLAINTEXT_H */
153