arp_notify.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /****************************************************************************
  2. * net/arp/arp_notify.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <debug.h>
  27. #include <netinet/in.h>
  28. #include <nuttx/irq.h>
  29. #include <nuttx/net/net.h>
  30. #include "arp/arp.h"
  31. #ifdef CONFIG_NET_ARP_SEND
  32. /****************************************************************************
  33. * Private Data
  34. ****************************************************************************/
  35. /* List of tasks waiting for ARP events */
  36. static FAR struct arp_notify_s *g_arp_waiters;
  37. /****************************************************************************
  38. * Public Functions
  39. ****************************************************************************/
  40. /****************************************************************************
  41. * Name: arp_wait_setup
  42. *
  43. * Description:
  44. * Called BEFORE an ARP request is sent. This function sets up the ARP
  45. * response timeout before the ARP request is sent so that there is
  46. * no race condition when arp_wait() is called.
  47. *
  48. * Assumptions:
  49. * This function is called from ARP send and executes in the normal
  50. * tasking environment.
  51. *
  52. ****************************************************************************/
  53. void arp_wait_setup(in_addr_t ipaddr, FAR struct arp_notify_s *notify)
  54. {
  55. irqstate_t flags;
  56. /* Initialize the wait structure */
  57. notify->nt_ipaddr = ipaddr;
  58. notify->nt_result = -ETIMEDOUT;
  59. /* This semaphore is used for signaling and, hence, should not have
  60. * priority inheritance enabled.
  61. */
  62. nxsem_init(&notify->nt_sem, 0, 0);
  63. nxsem_set_protocol(&notify->nt_sem, SEM_PRIO_NONE);
  64. /* Add the wait structure to the list with interrupts disabled */
  65. flags = enter_critical_section();
  66. notify->nt_flink = g_arp_waiters;
  67. g_arp_waiters = notify;
  68. leave_critical_section(flags);
  69. }
  70. /****************************************************************************
  71. * Name: arp_wait_cancel
  72. *
  73. * Description:
  74. * Cancel any wait set after arp_wait_setup is called but before arp_wait()
  75. * is called (arp_wait() will automatically cancel the wait).
  76. *
  77. * Assumptions:
  78. * This function may execute with interrupts disabled when called from
  79. * arp_wait().
  80. *
  81. ****************************************************************************/
  82. int arp_wait_cancel(FAR struct arp_notify_s *notify)
  83. {
  84. FAR struct arp_notify_s *curr;
  85. FAR struct arp_notify_s *prev;
  86. irqstate_t flags;
  87. int ret = -ENOENT;
  88. /* Remove our wait structure from the list (we may no longer be at the
  89. * head of the list).
  90. */
  91. flags = enter_critical_section();
  92. for (prev = NULL, curr = g_arp_waiters;
  93. curr && curr != notify;
  94. prev = curr, curr = curr->nt_flink)
  95. {
  96. }
  97. DEBUGASSERT(curr && curr == notify);
  98. if (curr)
  99. {
  100. if (prev)
  101. {
  102. prev->nt_flink = notify->nt_flink;
  103. }
  104. else
  105. {
  106. g_arp_waiters = notify->nt_flink;
  107. }
  108. ret = OK;
  109. }
  110. leave_critical_section(flags);
  111. nxsem_destroy(&notify->nt_sem);
  112. return ret;
  113. }
  114. /****************************************************************************
  115. * Name: arp_wait
  116. *
  117. * Description:
  118. * Called each time that a ARP request is sent. This function will sleep
  119. * until either: (1) the matching ARP response is received, or (2) a
  120. * timeout occurs.
  121. *
  122. * Assumptions:
  123. * This function is called from ARP send must execute with the network
  124. * locked.
  125. *
  126. ****************************************************************************/
  127. int arp_wait(FAR struct arp_notify_s *notify, unsigned int timeout)
  128. {
  129. int ret;
  130. /* And wait for the ARP response (or a timeout). */
  131. net_timedwait_uninterruptible(&notify->nt_sem, timeout);
  132. /* Then get the real result of the transfer */
  133. ret = notify->nt_result;
  134. /* Remove our wait structure from the list (we may no longer be at the
  135. * head of the list).
  136. */
  137. arp_wait_cancel(notify);
  138. return ret;
  139. }
  140. /****************************************************************************
  141. * Name: arp_notify
  142. *
  143. * Description:
  144. * Called each time that a ARP response is received in order to wake-up
  145. * any threads that may be waiting for this particular ARP response.
  146. *
  147. * Assumptions:
  148. * This function is called from the MAC device driver indirectly through
  149. * arp_arpin() will execute with the network locked.
  150. *
  151. ****************************************************************************/
  152. void arp_notify(in_addr_t ipaddr)
  153. {
  154. FAR struct arp_notify_s *curr;
  155. /* Find an entry with the matching IP address in the list of waiters */
  156. for (curr = g_arp_waiters; curr; curr = curr->nt_flink)
  157. {
  158. /* Does this entry match? If the result is okay, then we have
  159. * already notified this waiter and it has not yet taken the
  160. * entry from the list.
  161. */
  162. if (curr->nt_result != OK && curr->nt_ipaddr == ipaddr)
  163. {
  164. /* Yes.. Signal the waiting, returning success */
  165. curr->nt_result = OK;
  166. nxsem_post(&curr->nt_sem);
  167. break;
  168. }
  169. }
  170. }
  171. #endif /* CONFIG_NET_ARP_SEND */