igmp_msg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  23. * contributors may be used to endorse or promote products derived from
  24. * this software 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
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF 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
  71. * group
  72. */
  73. dev = netdev_findbyindex(group->ifindex);
  74. if (dev == NULL)
  75. {
  76. nerr("ERROR: No device for this interface index: %u\n",
  77. group->ifindex);
  78. return -ENODEV;
  79. }
  80. /* Schedule the message */
  81. group->msgid = msgid;
  82. SET_SCHEDMSG(group->flags);
  83. /* Notify the device that we have a packet to send */
  84. netdev_txnotify_dev(dev);
  85. return OK;
  86. }
  87. /****************************************************************************
  88. * Name: igmp_waitmsg
  89. *
  90. * Description:
  91. * Schedule a message to be send at the next driver polling interval and
  92. * block, waiting for the message to be sent.
  93. *
  94. * Assumptions:
  95. * The network is locked.
  96. *
  97. ****************************************************************************/
  98. int igmp_waitmsg(FAR struct igmp_group_s *group, uint8_t msgid)
  99. {
  100. int ret;
  101. /* Schedule to send the message */
  102. DEBUGASSERT(!IS_WAITMSG(group->flags));
  103. SET_WAITMSG(group->flags);
  104. ret = igmp_schedmsg(group, msgid);
  105. if (ret < 0)
  106. {
  107. nerr("ERROR: Failed to schedule the message: %d\n", ret);
  108. goto errout;
  109. }
  110. /* Then wait for the message to be sent */
  111. while (IS_SCHEDMSG(group->flags))
  112. {
  113. /* Wait for the semaphore to be posted */
  114. ret = net_lockedwait_uninterruptible(&group->sem);
  115. if (ret < 0)
  116. {
  117. break;
  118. }
  119. }
  120. /* The message has been sent and we are no longer waiting */
  121. errout:
  122. CLR_WAITMSG(group->flags);
  123. return ret;
  124. }
  125. #endif /* CONFIG_NET_IGMP */