ieee802154_finddev.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************************
  2. * net/ieee802154/ieee802154_finddev.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
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <assert.h>
  40. #include <nuttx/net/net.h>
  41. #include <nuttx/net/radiodev.h>
  42. #include <nuttx/net/ieee802154.h>
  43. #include "netdev/netdev.h"
  44. #include "ieee802154/ieee802154.h"
  45. #ifdef CONFIG_NET_IEEE802154
  46. /****************************************************************************
  47. * Private Types
  48. ****************************************************************************/
  49. struct ieee802154_finddev_s
  50. {
  51. FAR const struct ieee802154_saddr_s *addr;
  52. FAR struct radio_driver_s *radio;
  53. };
  54. /****************************************************************************
  55. * Private Functions
  56. ****************************************************************************/
  57. /****************************************************************************
  58. * Name: ieee802154_dev_callback
  59. *
  60. * Description:
  61. * Check if this device matches the connections local address.
  62. *
  63. * Input Parameters:
  64. * dev - The next network device in the enumeration
  65. *
  66. * Returned Value:
  67. * 0 if there is no match (meaning to continue looking); 1 if there is a
  68. * match (meaning to stop the search).
  69. *
  70. ****************************************************************************/
  71. static int ieee802154_dev_callback(FAR struct net_driver_s *dev, FAR void *arg)
  72. {
  73. FAR struct ieee802154_finddev_s *match =
  74. (FAR struct ieee802154_finddev_s *)arg;
  75. DEBUGASSERT(dev != NULL && match != NULL && match->addr != NULL);
  76. /* First, check if this network device is an IEEE 802.15.4 radio and that
  77. * it has an assigned IEEE 802.15.4 address.
  78. */
  79. if (dev->d_lltype == NET_LL_IEEE802154 && dev->d_mac.radio.nv_addrlen > 0)
  80. {
  81. DEBUGASSERT(dev->d_mac.radio.nv_addrlen == 2 ||
  82. dev->d_mac.radio.nv_addrlen == 8);
  83. /* Does the address mode match? */
  84. if (match->addr->s_mode == IEEE802154_ADDRMODE_SHORT &&
  85. dev->d_mac.radio.nv_addrlen == 2)
  86. {
  87. /* Does the device address match */
  88. if (IEEE802154_SADDRCMP(dev->d_mac.radio.nv_addr,
  89. match->addr->s_saddr))
  90. {
  91. /* Yes.. save the match and return 1 to stop the search */
  92. match->radio = (FAR struct radio_driver_s *)dev;
  93. return 1;
  94. }
  95. }
  96. else if (match->addr->s_mode == IEEE802154_ADDRMODE_EXTENDED &&
  97. dev->d_mac.radio.nv_addrlen == 8)
  98. {
  99. /* Does the device address match */
  100. if (IEEE802154_EADDRCMP(dev->d_mac.radio.nv_addr,
  101. match->addr->s_eaddr))
  102. {
  103. /* Yes.. save the match and return 1 to stop the search */
  104. match->radio = (FAR struct radio_driver_s *)dev;
  105. return 1;
  106. }
  107. }
  108. }
  109. /* Keep looking */
  110. return 0;
  111. }
  112. /****************************************************************************
  113. * Public Functions
  114. ****************************************************************************/
  115. /****************************************************************************
  116. * Name: ieee802154_find_device
  117. *
  118. * Description:
  119. * Select the network driver to use with the IEEE802154 transaction.
  120. *
  121. * Input Parameters:
  122. * conn - IEEE802154 connection structure (not currently used).
  123. * addr - The address to match the devices assigned address
  124. *
  125. * Returned Value:
  126. * A pointer to the network driver to use. NULL is returned on any
  127. * failure.
  128. *
  129. ****************************************************************************/
  130. FAR struct radio_driver_s *
  131. ieee802154_find_device(FAR struct ieee802154_conn_s *conn,
  132. FAR const struct ieee802154_saddr_s *addr)
  133. {
  134. struct ieee802154_finddev_s match;
  135. int ret;
  136. DEBUGASSERT(conn != NULL);
  137. match.addr = addr;
  138. match.radio = NULL;
  139. /* If the socket is not bound to a local address, then return a failure */
  140. if (addr->s_mode == IEEE802154_ADDRMODE_NONE)
  141. {
  142. return NULL;
  143. }
  144. DEBUGASSERT(addr->s_mode == IEEE802154_ADDRMODE_SHORT ||
  145. addr->s_mode == IEEE802154_ADDRMODE_EXTENDED);
  146. /* Other, search for the IEEE 802.15.4 network device whose MAC is equal to
  147. * the sockets bound local address.
  148. */
  149. ret = netdev_foreach(ieee802154_dev_callback, (FAR void *)&match);
  150. if (ret == 1)
  151. {
  152. DEBUGASSERT(match.radio != NULL);
  153. return match.radio;
  154. }
  155. return NULL;
  156. }
  157. #endif /* CONFIG_NET_IEEE802154 */