igmp_msg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. * net/igmp/igmp_msg.c
  3. *
  4. * Copyright (C) 2010-2011, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * The NuttX implementation of IGMP was inspired by the IGMP add-on for the
  8. * lwIP TCP/IP stack by Steve Reynolds:
  9. *
  10. * Copyright (c) 2002 CITEL Technologies Ltd.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of CITEL Technologies Ltd nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/net/netconfig.h>
  46. #include <nuttx/net/net.h>
  47. #include <nuttx/net/igmp.h>
  48. #include "devif/devif.h"
  49. #include "netdev/netdev.h"
  50. #include "igmp/igmp.h"
  51. #ifdef CONFIG_NET_IGMP
  52. /****************************************************************************
  53. * Public Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: igmp_schedmsg
  57. *
  58. * Description:
  59. * Schedule a message to be send at the next driver polling interval.
  60. *
  61. * Assumptions:
  62. * The network is locked.
  63. *
  64. ****************************************************************************/
  65. int igmp_schedmsg(FAR struct igmp_group_s *group, uint8_t msgid)
  66. {
  67. FAR struct net_driver_s *dev;
  68. DEBUGASSERT(group != NULL && !IS_SCHEDMSG(group->flags));
  69. DEBUGASSERT(group->ifindex > 0);
  70. /* Get the device instance associated with the interface index of the group */
  71. dev = netdev_findbyindex(group->ifindex);
  72. if (dev == NULL)
  73. {
  74. nerr("ERROR: No device for this interface index: %u\n",
  75. group->ifindex);
  76. return -ENODEV;
  77. }
  78. /* Schedule the message */
  79. group->msgid = msgid;
  80. SET_SCHEDMSG(group->flags);
  81. /* Notify the device that we have a packet to send */
  82. netdev_txnotify_dev(dev);
  83. return OK;
  84. }
  85. /****************************************************************************
  86. * Name: igmp_waitmsg
  87. *
  88. * Description:
  89. * Schedule a message to be send at the next driver polling interval and
  90. * block, waiting for the message to be sent.
  91. *
  92. * Assumptions:
  93. * The network is locked.
  94. *
  95. ****************************************************************************/
  96. int igmp_waitmsg(FAR struct igmp_group_s *group, uint8_t msgid)
  97. {
  98. int ret;
  99. /* Schedule to send the message */
  100. DEBUGASSERT(!IS_WAITMSG(group->flags));
  101. SET_WAITMSG(group->flags);
  102. ret = igmp_schedmsg(group, msgid);
  103. if (ret < 0)
  104. {
  105. nerr("ERROR: Failed to schedule the message: %d\n", ret);
  106. goto errout;
  107. }
  108. /* Then wait for the message to be sent */
  109. while (IS_SCHEDMSG(group->flags))
  110. {
  111. /* Wait for the semaphore to be posted */
  112. while ((ret = net_lockedwait(&group->sem)) < 0)
  113. {
  114. /* The only error that should occur from net_lockedwait() is if
  115. * the wait is awakened by a signal or, perhaps, canceled.
  116. */
  117. DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
  118. if (ret != -EINTR)
  119. {
  120. break;
  121. }
  122. ret = OK;
  123. }
  124. }
  125. /* The message has been sent and we are no longer waiting */
  126. errout:
  127. CLR_WAITMSG(group->flags);
  128. return ret;
  129. }
  130. #endif /* CONFIG_NET_IGMP */