psock.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2004, Swedish Institute of Computer Science.
  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 Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the uIP TCP/IP stack
  30. *
  31. * Author: Adam Dunkels <adam@sics.se>
  32. *
  33. * $Id: psock.h,v 1.3 2006/06/12 08:00:30 adam Exp $
  34. */
  35. /**
  36. * \defgroup psock Protosockets library
  37. * @{
  38. *
  39. * The protosocket library provides an interface to the uIP stack that is
  40. * similar to the traditional BSD socket interface. Unlike programs
  41. * written for the ordinary uIP event-driven interface, programs
  42. * written with the protosocket library are executed in a sequential
  43. * fashion and does not have to be implemented as explicit state
  44. * machines.
  45. *
  46. * Protosockets only work with TCP connections.
  47. *
  48. * The protosocket library uses \ref pt protothreads to provide
  49. * sequential control flow. This makes the protosockets lightweight in
  50. * terms of memory, but also means that protosockets inherits the
  51. * functional limitations of protothreads. Each protosocket lives only
  52. * within a single function. Automatic variables (stack variables) are
  53. * not retained across a protosocket library function call.
  54. *
  55. * \note Because the protosocket library uses protothreads, local
  56. * variables will not always be saved across a call to a protosocket
  57. * library function. It is therefore advised that local variables are
  58. * used with extreme care.
  59. *
  60. * The protosocket library provides functions for sending data without
  61. * having to deal with retransmissions and acknowledgements, as well
  62. * as functions for reading data without having to deal with data
  63. * being split across more than one TCP segment.
  64. *
  65. * Because each protosocket runs as a protothread, the protosocket has to be
  66. * started with a call to PSOCK_BEGIN() at the start of the function
  67. * in which the protosocket is used. Similarly, the protosocket protothread can
  68. * be terminated by a call to PSOCK_EXIT().
  69. *
  70. */
  71. /**
  72. * \file
  73. * Protosocket library header file
  74. * \author
  75. * Adam Dunkels <adam@sics.se>
  76. *
  77. */
  78. #ifndef __PSOCK_H__
  79. #define __PSOCK_H__
  80. #include "uipopt.h"
  81. #include "pt.h"
  82. /*
  83. * The structure that holds the state of a buffer.
  84. *
  85. * This structure holds the state of a uIP buffer. The structure has
  86. * no user-visible elements, but is used through the functions
  87. * provided by the library.
  88. *
  89. */
  90. struct psock_buf {
  91. u8_t *ptr;
  92. unsigned short left;
  93. };
  94. /**
  95. * The representation of a protosocket.
  96. *
  97. * The protosocket structrure is an opaque structure with no user-visible
  98. * elements.
  99. */
  100. struct psock {
  101. struct pt pt, psockpt; /* Protothreads - one that's using the psock
  102. functions, and one that runs inside the
  103. psock functions. */
  104. const u8_t *sendptr; /* Pointer to the next data to be sent. */
  105. u8_t *readptr; /* Pointer to the next data to be read. */
  106. char *bufptr; /* Pointer to the buffer used for buffering
  107. incoming data. */
  108. u16_t sendlen; /* The number of bytes left to be sent. */
  109. u16_t readlen; /* The number of bytes left to be read. */
  110. struct psock_buf buf; /* The structure holding the state of the
  111. input buffer. */
  112. unsigned int bufsize; /* The size of the input buffer. */
  113. unsigned char state; /* The state of the protosocket. */
  114. };
  115. void psock_init(struct psock *psock, char *buffer, unsigned int buffersize);
  116. /**
  117. * Initialize a protosocket.
  118. *
  119. * This macro initializes a protosocket and must be called before the
  120. * protosocket is used. The initialization also specifies the input buffer
  121. * for the protosocket.
  122. *
  123. * \param psock (struct psock *) A pointer to the protosocket to be
  124. * initialized
  125. *
  126. * \param buffer (char *) A pointer to the input buffer for the
  127. * protosocket.
  128. *
  129. * \param buffersize (unsigned int) The size of the input buffer.
  130. *
  131. * \hideinitializer
  132. */
  133. #define PSOCK_INIT(psock, buffer, buffersize) \
  134. psock_init(psock, buffer, buffersize)
  135. /**
  136. * Start the protosocket protothread in a function.
  137. *
  138. * This macro starts the protothread associated with the protosocket and
  139. * must come before other protosocket calls in the function it is used.
  140. *
  141. * \param psock (struct psock *) A pointer to the protosocket to be
  142. * started.
  143. *
  144. * \hideinitializer
  145. */
  146. #define PSOCK_BEGIN(psock) PT_BEGIN(&((psock)->pt))
  147. PT_THREAD(psock_send(struct psock *psock, const char *buf, unsigned int len));
  148. /**
  149. * Send data.
  150. *
  151. * This macro sends data over a protosocket. The protosocket protothread blocks
  152. * until all data has been sent and is known to have been received by
  153. * the remote end of the TCP connection.
  154. *
  155. * \param psock (struct psock *) A pointer to the protosocket over which
  156. * data is to be sent.
  157. *
  158. * \param data (char *) A pointer to the data that is to be sent.
  159. *
  160. * \param datalen (unsigned int) The length of the data that is to be
  161. * sent.
  162. *
  163. * \hideinitializer
  164. */
  165. #define PSOCK_SEND(psock, data, datalen) \
  166. PT_WAIT_THREAD(&((psock)->pt), psock_send(psock, data, datalen))
  167. /**
  168. * \brief Send a null-terminated string.
  169. * \param psock Pointer to the protosocket.
  170. * \param str The string to be sent.
  171. *
  172. * This function sends a null-terminated string over the
  173. * protosocket.
  174. *
  175. * \hideinitializer
  176. */
  177. #define PSOCK_SEND_STR(psock, str) \
  178. PT_WAIT_THREAD(&((psock)->pt), psock_send(psock, str, strlen(str)))
  179. PT_THREAD(psock_generator_send(struct psock *psock,
  180. unsigned short (*f)(void *), void *arg));
  181. /**
  182. * \brief Generate data with a function and send it
  183. * \param psock Pointer to the protosocket.
  184. * \param generator Pointer to the generator function
  185. * \param arg Argument to the generator function
  186. *
  187. * This function generates data and sends it over the
  188. * protosocket. This can be used to dynamically generate
  189. * data for a transmission, instead of generating the data
  190. * in a buffer beforehand. This function reduces the need for
  191. * buffer memory. The generator function is implemented by
  192. * the application, and a pointer to the function is given
  193. * as an argument with the call to PSOCK_GENERATOR_SEND().
  194. *
  195. * The generator function should place the generated data
  196. * directly in the uip_appdata buffer, and return the
  197. * length of the generated data. The generator function is
  198. * called by the protosocket layer when the data first is
  199. * sent, and once for every retransmission that is needed.
  200. *
  201. * \hideinitializer
  202. */
  203. #define PSOCK_GENERATOR_SEND(psock, generator, arg) \
  204. PT_WAIT_THREAD(&((psock)->pt), \
  205. psock_generator_send(psock, generator, arg))
  206. /**
  207. * Close a protosocket.
  208. *
  209. * This macro closes a protosocket and can only be called from within the
  210. * protothread in which the protosocket lives.
  211. *
  212. * \param psock (struct psock *) A pointer to the protosocket that is to
  213. * be closed.
  214. *
  215. * \hideinitializer
  216. */
  217. #define PSOCK_CLOSE(psock) uip_close()
  218. PT_THREAD(psock_readbuf(struct psock *psock));
  219. /**
  220. * Read data until the buffer is full.
  221. *
  222. * This macro will block waiting for data and read the data into the
  223. * input buffer specified with the call to PSOCK_INIT(). Data is read
  224. * until the buffer is full..
  225. *
  226. * \param psock (struct psock *) A pointer to the protosocket from which
  227. * data should be read.
  228. *
  229. * \hideinitializer
  230. */
  231. #define PSOCK_READBUF(psock) \
  232. PT_WAIT_THREAD(&((psock)->pt), psock_readbuf(psock))
  233. PT_THREAD(psock_readto(struct psock *psock, unsigned char c));
  234. /**
  235. * Read data up to a specified character.
  236. *
  237. * This macro will block waiting for data and read the data into the
  238. * input buffer specified with the call to PSOCK_INIT(). Data is only
  239. * read until the specifieed character appears in the data stream.
  240. *
  241. * \param psock (struct psock *) A pointer to the protosocket from which
  242. * data should be read.
  243. *
  244. * \param c (char) The character at which to stop reading.
  245. *
  246. * \hideinitializer
  247. */
  248. #define PSOCK_READTO(psock, c) \
  249. PT_WAIT_THREAD(&((psock)->pt), psock_readto(psock, c))
  250. /**
  251. * The length of the data that was previously read.
  252. *
  253. * This macro returns the length of the data that was previously read
  254. * using PSOCK_READTO() or PSOCK_READ().
  255. *
  256. * \param psock (struct psock *) A pointer to the protosocket holding the data.
  257. *
  258. * \hideinitializer
  259. */
  260. #define PSOCK_DATALEN(psock) psock_datalen(psock)
  261. u16_t psock_datalen(struct psock *psock);
  262. /**
  263. * Exit the protosocket's protothread.
  264. *
  265. * This macro terminates the protothread of the protosocket and should
  266. * almost always be used in conjunction with PSOCK_CLOSE().
  267. *
  268. * \sa PSOCK_CLOSE_EXIT()
  269. *
  270. * \param psock (struct psock *) A pointer to the protosocket.
  271. *
  272. * \hideinitializer
  273. */
  274. #define PSOCK_EXIT(psock) PT_EXIT(&((psock)->pt))
  275. /**
  276. * Close a protosocket and exit the protosocket's protothread.
  277. *
  278. * This macro closes a protosocket and exits the protosocket's protothread.
  279. *
  280. * \param psock (struct psock *) A pointer to the protosocket.
  281. *
  282. * \hideinitializer
  283. */
  284. #define PSOCK_CLOSE_EXIT(psock) \
  285. do { \
  286. PSOCK_CLOSE(psock); \
  287. PSOCK_EXIT(psock); \
  288. } while(0)
  289. /**
  290. * Declare the end of a protosocket's protothread.
  291. *
  292. * This macro is used for declaring that the protosocket's protothread
  293. * ends. It must always be used together with a matching PSOCK_BEGIN()
  294. * macro.
  295. *
  296. * \param psock (struct psock *) A pointer to the protosocket.
  297. *
  298. * \hideinitializer
  299. */
  300. #define PSOCK_END(psock) PT_END(&((psock)->pt))
  301. char psock_newdata(struct psock *s);
  302. /**
  303. * Check if new data has arrived on a protosocket.
  304. *
  305. * This macro is used in conjunction with the PSOCK_WAIT_UNTIL()
  306. * macro to check if data has arrived on a protosocket.
  307. *
  308. * \param psock (struct psock *) A pointer to the protosocket.
  309. *
  310. * \hideinitializer
  311. */
  312. #define PSOCK_NEWDATA(psock) psock_newdata(psock)
  313. /**
  314. * Wait until a condition is true.
  315. *
  316. * This macro blocks the protothread until the specified condition is
  317. * true. The macro PSOCK_NEWDATA() can be used to check if new data
  318. * arrives when the protosocket is waiting.
  319. *
  320. * Typically, this macro is used as follows:
  321. *
  322. \code
  323. PT_THREAD(thread(struct psock *s, struct timer *t))
  324. {
  325. PSOCK_BEGIN(s);
  326. PSOCK_WAIT_UNTIL(s, PSOCK_NEWADATA(s) || timer_expired(t));
  327. if(PSOCK_NEWDATA(s)) {
  328. PSOCK_READTO(s, '\n');
  329. } else {
  330. handle_timed_out(s);
  331. }
  332. PSOCK_END(s);
  333. }
  334. \endcode
  335. *
  336. * \param psock (struct psock *) A pointer to the protosocket.
  337. * \param condition The condition to wait for.
  338. *
  339. * \hideinitializer
  340. */
  341. #define PSOCK_WAIT_UNTIL(psock, condition) \
  342. PT_WAIT_UNTIL(&((psock)->pt), (condition));
  343. #define PSOCK_WAIT_THREAD(psock, condition) \
  344. PT_WAIT_THREAD(&((psock)->pt), (condition))
  345. #endif /* __PSOCK_H__ */
  346. /** @} */