mld_msg.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /****************************************************************************
  2. * net/mld/mld_msg.c
  3. *
  4. * Copyright (C) 2018 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. Neither the name of CITEL Technologies Ltd nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Included Files
  35. ****************************************************************************/
  36. #include <nuttx/config.h>
  37. #include <assert.h>
  38. #include <debug.h>
  39. #include <nuttx/net/netconfig.h>
  40. #include <nuttx/net/net.h>
  41. #include <nuttx/net/mld.h>
  42. #include "devif/devif.h"
  43. #include "netdev/netdev.h"
  44. #include "mld/mld.h"
  45. #ifdef CONFIG_NET_MLD
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: mld_schedmsg
  51. *
  52. * Description:
  53. * Schedule a message to be send at the next driver polling interval.
  54. *
  55. * Assumptions:
  56. * The network is locked
  57. *
  58. ****************************************************************************/
  59. int mld_schedmsg(FAR struct mld_group_s *group, uint8_t msgtype)
  60. {
  61. FAR struct net_driver_s *dev;
  62. DEBUGASSERT(group != NULL && !IS_MLD_SCHEDMSG(group->flags));
  63. DEBUGASSERT(group->ifindex > 0);
  64. /* Get the device instance associated with the interface index of the group */
  65. dev = netdev_findbyindex(group->ifindex);
  66. if (dev == NULL)
  67. {
  68. mlderr("ERROR: No device for this interface index: %u\n",
  69. group->ifindex);
  70. return -ENODEV;
  71. }
  72. /* Schedule the message */
  73. group->msgtype = msgtype;
  74. SET_MLD_SCHEDMSG(group->flags);
  75. /* Notify the device that we have a packet to send */
  76. netdev_txnotify_dev(dev);
  77. return OK;
  78. }
  79. /****************************************************************************
  80. * Name: mld_waitmsg
  81. *
  82. * Description:
  83. * Schedule a message to be send at the next driver polling interval and
  84. * block, waiting for the message to be sent.
  85. *
  86. * Assumptions:
  87. * The network is locked
  88. *
  89. ****************************************************************************/
  90. int mld_waitmsg(FAR struct mld_group_s *group, uint8_t msgtype)
  91. {
  92. int ret;
  93. /* Schedule to send the message */
  94. DEBUGASSERT(!IS_MLD_WAITMSG(group->flags));
  95. SET_MLD_WAITMSG(group->flags);
  96. ret = mld_schedmsg(group, msgtype);
  97. if (ret < 0)
  98. {
  99. mlderr("ERROR: Failed to schedule the message: %d\n", ret);
  100. goto errout;
  101. }
  102. /* Then wait for the message to be sent */
  103. while (IS_MLD_SCHEDMSG(group->flags))
  104. {
  105. /* Wait for the semaphore to be posted */
  106. ret = net_lockedwait_uninterruptible(&group->sem);
  107. if (ret < 0)
  108. {
  109. break;
  110. }
  111. }
  112. /* The message has been sent and we are no longer waiting */
  113. errout:
  114. CLR_MLD_WAITMSG(group->flags);
  115. return ret;
  116. }
  117. #endif /* CONFIG_NET_MLD */