ieee802154_conn.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /****************************************************************************
  2. * net/ieee802154/ieee802154_conn.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  21. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  26. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Included Files
  35. ****************************************************************************/
  36. #include <nuttx/config.h>
  37. #include <semaphore.h>
  38. #include <string.h>
  39. #include <assert.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include <arch/irq.h>
  43. #include <nuttx/mm/iob.h>
  44. #include <nuttx/net/netconfig.h>
  45. #include <nuttx/net/net.h>
  46. #include <nuttx/net/netdev.h>
  47. #include <nuttx/wireless/ieee802154/ieee802154_mac.h>
  48. #include "devif/devif.h"
  49. #include "ieee802154/ieee802154.h"
  50. #ifdef CONFIG_NET_IEEE802154
  51. /****************************************************************************
  52. * Private Data
  53. ****************************************************************************/
  54. /* The array containing all packet socket connections. Protected via the
  55. * network lock.
  56. */
  57. static struct ieee802154_conn_s
  58. g_ieee802154_connections[CONFIG_NET_IEEE802154_NCONNS];
  59. /* A list of all free packet socket connections */
  60. static dq_queue_t g_free_ieee802154_connections;
  61. /* A list of all allocated packet socket connections */
  62. static dq_queue_t g_active_ieee802154_connections;
  63. /****************************************************************************
  64. * Public Functions
  65. ****************************************************************************/
  66. /****************************************************************************
  67. * Name: ieee802154_conn_initialize
  68. *
  69. * Description:
  70. * Initialize the IEEE 802.15.4 connection structure allocator. Called
  71. * once and only from ieee802154_initialize().
  72. *
  73. * Assumptions:
  74. * Called early in the initialization sequence
  75. *
  76. ****************************************************************************/
  77. void ieee802154_conn_initialize(void)
  78. {
  79. int i;
  80. /* Initialize the queues */
  81. dq_init(&g_free_ieee802154_connections);
  82. dq_init(&g_active_ieee802154_connections);
  83. for (i = 0; i < CONFIG_NET_IEEE802154_NCONNS; i++)
  84. {
  85. /* Link each pre-allocated connection structure into the free list. */
  86. dq_addlast(&g_ieee802154_connections[i].node,
  87. &g_free_ieee802154_connections);
  88. }
  89. }
  90. /****************************************************************************
  91. * Name: ieee802154_conn_alloc()
  92. *
  93. * Description:
  94. * Allocate a new, uninitialized packet socket connection structure. This
  95. * is normally something done by the implementation of the socket() API
  96. *
  97. ****************************************************************************/
  98. FAR struct ieee802154_conn_s *ieee802154_conn_alloc(void)
  99. {
  100. FAR struct ieee802154_conn_s *conn;
  101. /* The free list is protected by the network lock. */
  102. net_lock();
  103. conn = (FAR struct ieee802154_conn_s *)
  104. dq_remfirst(&g_free_ieee802154_connections);
  105. if (conn)
  106. {
  107. /* Enqueue the connection into the active list */
  108. memset(conn, 0, sizeof(struct ieee802154_conn_s));
  109. dq_addlast(&conn->node, &g_active_ieee802154_connections);
  110. }
  111. net_unlock();
  112. return conn;
  113. }
  114. /****************************************************************************
  115. * Name: ieee802154_conn_free()
  116. *
  117. * Description:
  118. * Free a packet socket connection structure that is no longer in use.
  119. * This should be done by the implementation of close().
  120. *
  121. ****************************************************************************/
  122. void ieee802154_conn_free(FAR struct ieee802154_conn_s *conn)
  123. {
  124. FAR struct ieee802154_container_s *container;
  125. FAR struct ieee802154_container_s *next;
  126. /* The free list is protected by the network lock. */
  127. DEBUGASSERT(conn->crefs == 0);
  128. /* Remove the connection from the active list */
  129. net_lock();
  130. dq_rem(&conn->node, &g_active_ieee802154_connections);
  131. /* Check if there any any frames attached to the container */
  132. for (container = conn->rxhead; container != NULL; container = next)
  133. {
  134. /* Remove the frame from the list */
  135. next = container->ic_flink;
  136. container->ic_flink = NULL;
  137. /* Free the contained frame data (should be only one in chain) */
  138. if (container->ic_iob)
  139. {
  140. iob_free(container->ic_iob);
  141. }
  142. /* And free the container itself */
  143. ieee802154_container_free(container);
  144. }
  145. /* Free the connection */
  146. dq_addlast(&conn->node, &g_free_ieee802154_connections);
  147. net_unlock();
  148. }
  149. /****************************************************************************
  150. * Name: ieee802154_conn_active()
  151. *
  152. * Description:
  153. * Find a connection structure that is the appropriate
  154. * connection to be used with the provided IEEE 802.15.4 header
  155. *
  156. * Assumptions:
  157. * This function is called from network logic at with the network locked.
  158. *
  159. ****************************************************************************/
  160. FAR struct ieee802154_conn_s *
  161. ieee802154_conn_active(FAR const struct ieee802154_data_ind_s *meta)
  162. {
  163. FAR struct ieee802154_conn_s *conn;
  164. DEBUGASSERT(meta != NULL);
  165. for (conn = (FAR struct ieee802154_conn_s *)g_active_ieee802154_connections.head;
  166. conn != NULL;
  167. conn = (FAR struct ieee802154_conn_s *)conn->node.flink)
  168. {
  169. /* Does the destination address match the bound address of the socket.
  170. *
  171. * REVISIT: Currently and explicit address must be assigned. Should we
  172. * support some moral equivalent to INADDR_ANY?
  173. */
  174. if (meta->dest.mode != conn->laddr.s_mode)
  175. {
  176. continue;
  177. }
  178. if (meta->dest.mode == IEEE802154_ADDRMODE_SHORT &&
  179. !IEEE802154_SADDRCMP(meta->dest.saddr, conn->laddr.s_saddr))
  180. {
  181. continue;
  182. }
  183. if (meta->dest.mode == IEEE802154_ADDRMODE_EXTENDED &&
  184. !IEEE802154_EADDRCMP(meta->dest.saddr, conn->laddr.s_eaddr))
  185. {
  186. continue;
  187. }
  188. /* Is the socket "connected?" to a remote peer? If so, check if the
  189. * source address matches the connected remote adress.
  190. */
  191. switch (conn->raddr.s_mode)
  192. {
  193. case IEEE802154_ADDRMODE_NONE:
  194. return conn; /* No.. accept the connection */
  195. case IEEE802154_ADDRMODE_SHORT:
  196. if (IEEE802154_SADDRCMP(meta->dest.saddr, conn->raddr.s_saddr))
  197. {
  198. return conn;
  199. }
  200. break;
  201. case IEEE802154_ADDRMODE_EXTENDED:
  202. if (IEEE802154_EADDRCMP(meta->dest.eaddr, conn->raddr.s_eaddr))
  203. {
  204. return conn;
  205. }
  206. break;
  207. default:
  208. nerr("ERROR: Invalid address mode: %u\n", conn->raddr.s_mode);
  209. return NULL;
  210. }
  211. }
  212. return conn;
  213. }
  214. /****************************************************************************
  215. * Name: ieee802154_conn_next()
  216. *
  217. * Description:
  218. * Traverse the list of allocated packet connections
  219. *
  220. * Assumptions:
  221. * This function is called from network logic at with the network locked.
  222. *
  223. ****************************************************************************/
  224. FAR struct ieee802154_conn_s *
  225. ieee802154_conn_next(FAR struct ieee802154_conn_s *conn)
  226. {
  227. if (!conn)
  228. {
  229. return (FAR struct ieee802154_conn_s *)
  230. g_active_ieee802154_connections.head;
  231. }
  232. else
  233. {
  234. return (FAR struct ieee802154_conn_s *)conn->node.flink;
  235. }
  236. }
  237. #endif /* CONFIG_NET_IEEE802154 */