1 // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /**
16  * \file ctrl_sock.h
17  * \brief Control Socket for select() wakeup
18  *
19  * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread
20  * sleeping on select. This is a common requirement for sending
21  * control commands to a network server. This control socket API
22  * facilitates the same.
23  */
24 #ifndef _CTRL_SOCK_H_
25 #define _CTRL_SOCK_H_
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /**
32  * @brief Create a control socket
33  *
34  *      LWIP doesn't allow an easy mechanism to on-demand wakeup a thread
35  *      sleeping on select. This is a common requirement for sending
36  *      control commands to a network server. This control socket API
37  *      facilitates the same.
38  *
39  *      This API will create a UDP control socket on the specified port. It
40  *      will return a socket descriptor that can then be added to your
41  *      fd_set in select()
42  *
43  * @param[in] port the local port on which the control socket will listen
44  *
45  * @return - the socket descriptor that can be added to the fd_set in select.
46  *         - an error code if less than zero
47  */
48 int cs_create_ctrl_sock(int port);
49 
50 /**
51  * @brief Free the control socket
52  *
53  *      This frees up the control socket that was earlier created using
54  *      cs_create_ctrl_sock()
55  *
56  * @param[in] fd the socket descriptor associated with this control socket
57  */
58 void cs_free_ctrl_sock(int fd);
59 
60 /**
61  * @brief Send data to control socket
62  *
63  *      This API sends data to the control socket. If a server is blocked
64  *      on select() with the control socket, this call will wake up that
65  *      server.
66  *
67  * @param[in] send_fd the socket for sending ctrl messages
68  * @param[in] port the port on which the control socket was created
69  * @param[in] data pointer to a buffer that contains data to send on the socket
70  * @param[in] data_len the length of the data contained in the buffer pointed to be data
71  *
72  * @return  - the number of bytes sent to the control socket
73  *          - an error code if less than zero
74  */
75 int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len);
76 
77 /**
78  * @brief Receive data from control socket
79  *
80  *      This API receives any data that was sent to the control
81  *      socket. This will be typically called from the server thread to
82  *      process any commands on this socket.
83  *
84  * @param[in] fd the socket descriptor of the control socket
85  * @param[in] data pointer to a buffer that will be used to store
86  * received from the control socket
87  * @param[in] data_len the maximum length of the data that can be
88  * stored in the buffer pointed by data
89  *
90  * @return  - the number of bytes received from the control socket
91  *          - an error code if less than zero
92  */
93 int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #endif /* ! _CTRL_SOCK_H_ */
100