PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/net/ip/tcp-socket.h

https://github.com/jianhuiz/contiki
C Header | 268 lines | 58 code | 21 blank | 189 comment | 0 complexity | 205420cfaf780eb15b174277c43fe145 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (c) 2012-2014, Thingsquare, http://www.thingsquare.com/.
  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
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holder nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  26. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  28. * OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef TCP_SOCKET_H
  32. #define TCP_SOCKET_H
  33. struct tcp_socket;
  34. typedef enum {
  35. TCP_SOCKET_CONNECTED,
  36. TCP_SOCKET_CLOSED,
  37. TCP_SOCKET_TIMEDOUT,
  38. TCP_SOCKET_ABORTED,
  39. TCP_SOCKET_DATA_SENT
  40. } tcp_socket_event_t;
  41. /**
  42. * \brief TCP data callback function
  43. * \param s A pointer to a TCP socket
  44. * \param ptr A user-defined pointer
  45. * \param input_data_ptr A pointer to the incoming data
  46. * \param input_data_len The length of the incoming data
  47. * \return The function should return the number of bytes to leave in the input buffer
  48. *
  49. * The TCP socket input callback function gets
  50. * called whenever there is new data on the socket. The
  51. * function can choose to either consume the data
  52. * directly, or leave it in the buffer for later. The
  53. * function must return the amount of data to leave in the
  54. * buffer. I.e., if the callback function consumes all
  55. * incoming data, it should return 0.
  56. */
  57. typedef int (* tcp_socket_data_callback_t)(struct tcp_socket *s,
  58. void *ptr,
  59. const uint8_t *input_data_ptr,
  60. int input_data_len);
  61. /**
  62. * \brief TCP event callback function
  63. * \param s A pointer to a TCP socket
  64. * \param ptr A user-defined pointer
  65. * \param event The event number
  66. *
  67. * The TCP socket event callback function gets
  68. * called whenever there is an event on a socket, such as
  69. * the socket getting connected or closed.
  70. */
  71. typedef void (* tcp_socket_event_callback_t)(struct tcp_socket *s,
  72. void *ptr,
  73. tcp_socket_event_t event);
  74. struct tcp_socket {
  75. struct tcp_socket *next;
  76. tcp_socket_data_callback_t input_callback;
  77. tcp_socket_event_callback_t event_callback;
  78. void *ptr;
  79. struct process *p;
  80. uint8_t *input_data_ptr;
  81. uint8_t *output_data_ptr;
  82. uint16_t input_data_maxlen;
  83. uint16_t input_data_len;
  84. uint16_t output_data_maxlen;
  85. uint16_t output_data_len;
  86. uint16_t output_data_send_nxt;
  87. uint8_t flags;
  88. uint16_t listen_port;
  89. struct uip_conn *c;
  90. };
  91. enum {
  92. TCP_SOCKET_FLAGS_NONE = 0x00,
  93. TCP_SOCKET_FLAGS_LISTENING = 0x01,
  94. TCP_SOCKET_FLAGS_CLOSING = 0x02,
  95. };
  96. /**
  97. * \brief Register a TCP socket
  98. * \param s A pointer to a TCP socket
  99. * \param ptr A user-defined pointer that will be sent to callbacks for this socket
  100. * \param input_databuf A pointer to a memory area this socket will use for input data
  101. * \param input_databuf_len The size of the input data buffer
  102. * \param output_databuf A pointer to a memory area this socket will use for outgoing data
  103. * \param output_databuf_len The size of the output data buffer
  104. * \param data_callback A pointer to the data callback function for this socket
  105. * \param event_callback A pointer to the event callback function for this socket
  106. * \retval -1 If an error occurs
  107. * \retval 1 If the operation succeeds.
  108. *
  109. * This function registers a TCP socket. The function sets
  110. * up the output and input buffers for the socket and
  111. * callback pointers.
  112. *
  113. * TCP sockets use input and output buffers for incoming
  114. * and outgoing data. The memory for these buffers must be
  115. * allocated by the caller. The size of the buffers
  116. * determine the amount of data that can be received and
  117. * sent, and the principle is that the application that
  118. * sets up the TCP socket will know roughly how large
  119. * these buffers should be. The rule of thumb is that the
  120. * input buffer should be large enough to hold the largest
  121. * application layer message that the application will
  122. * receive and the output buffer should be large enough to
  123. * hold the largest application layer message the
  124. * application will send.
  125. *
  126. * TCP throttles incoming data so that if the input buffer
  127. * is filled, the connection will halt until the
  128. * application has read out the data from the input
  129. * buffer.
  130. *
  131. */
  132. int tcp_socket_register(struct tcp_socket *s, void *ptr,
  133. uint8_t *input_databuf, int input_databuf_len,
  134. uint8_t *output_databuf, int output_databuf_len,
  135. tcp_socket_data_callback_t data_callback,
  136. tcp_socket_event_callback_t event_callback);
  137. /**
  138. * \brief Connect a TCP socket to a remote host
  139. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  140. * \param ipaddr The IP address of the remote host
  141. * \param port The TCP port number, in host byte order, of the remote host
  142. * \retval -1 If an error occurs
  143. * \retval 1 If the operation succeeds.
  144. *
  145. * This function connects a TCP socket to a remote host.
  146. *
  147. * When the socket has connected, the event callback will
  148. * get called with the TCP_SOCKET_CONNECTED event. If the
  149. * remote host does not accept the connection, the
  150. * TCP_SOCKET_ABORTED will be sent to the callback. If the
  151. * connection times out before conecting to the remote
  152. * host, the TCP_SOCKET_TIMEDOUT event is sent to the
  153. * callback.
  154. *
  155. */
  156. int tcp_socket_connect(struct tcp_socket *s,
  157. uip_ipaddr_t *ipaddr,
  158. uint16_t port);
  159. /**
  160. * \brief Start listening on a specific port
  161. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  162. * \param port The TCP port number, in host byte order, of the remote host
  163. * \retval -1 If an error occurs
  164. * \retval 1 If the operation succeeds.
  165. *
  166. * This function causes the TCP socket to start listening
  167. * on the given TCP port.
  168. *
  169. * Several sockets can listen on the same port. If a
  170. * remote host connects to the port, one of the listening
  171. * sockets will get connected and the event callback will
  172. * be called with the TCP_SOCKET_CONNECTED event. When the
  173. * connection closes, the socket will go back to listening
  174. * for new connections.
  175. *
  176. */
  177. int tcp_socket_listen(struct tcp_socket *s,
  178. uint16_t port);
  179. /**
  180. * \brief Stop listening for new connections
  181. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  182. * \retval -1 If an error occurs
  183. * \retval 1 If the operation succeeds.
  184. *
  185. * This function causes a listening TCP socket to stop
  186. * listen. The socket must previously been put into listen
  187. * mode with tcp_socket_listen().
  188. *
  189. */
  190. int tcp_socket_unlisten(struct tcp_socket *s);
  191. /**
  192. * \brief Send data on a connected TCP socket
  193. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  194. * \param dataptr A pointer to the data to be sent
  195. * \param datalen The length of the data to be sent
  196. * \retval -1 If an error occurs
  197. * \return The number of bytes that were successfully sent
  198. *
  199. * This function sends data over a connected TCP
  200. * socket. The data is placed in the output buffer and
  201. * sent to the remote host as soon as possiblce. When the
  202. * data has been acknowledged by the remote host, the
  203. * event callback is sent with the TCP_SOCKET_DATA_SENT
  204. * event.
  205. */
  206. int tcp_socket_send(struct tcp_socket *s,
  207. const uint8_t *dataptr,
  208. int datalen);
  209. /**
  210. * \brief Send a string on a connected TCP socket
  211. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  212. * \param strptr A pointer to the string to be sent
  213. * \retval -1 If an error occurs
  214. * \return The number of bytes that were successfully sent
  215. *
  216. * This is a convenience function for sending strings on a
  217. * TCP socket. The function calls tcp_socket_send() to
  218. * send the string.
  219. */
  220. int tcp_socket_send_str(struct tcp_socket *s,
  221. const char *strptr);
  222. /**
  223. * \brief Close a connected TCP socket
  224. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  225. * \retval -1 If an error occurs
  226. * \retval 1 If the operation succeeds.
  227. *
  228. * This function closes a connected TCP socket. When the
  229. * socket has been successfully closed, the event callback
  230. * is called with the TCP_SOCKET_CLOSED event.
  231. *
  232. */
  233. int tcp_socket_close(struct tcp_socket *s);
  234. /**
  235. * \brief Unregister a registered socket
  236. * \param s A pointer to a TCP socket that must have been previously registered with tcp_socket_register()
  237. * \retval -1 If an error occurs
  238. * \retval 1 If the operation succeeds.
  239. *
  240. * This function unregisters a previously registered
  241. * socket. This must be done if the process will be
  242. * unloaded from memory. If the TCP socket is connected,
  243. * the connection will be reset.
  244. *
  245. */
  246. int tcp_socket_unregister(struct tcp_socket *s);
  247. #endif /* TCP_SOCKET_H */