netdown_notifier.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /****************************************************************************
  2. * net/netdev/netdown_notifier.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
  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 <sys/types.h>
  40. #include <assert.h>
  41. #include <nuttx/wqueue.h>
  42. #include <nuttx/mm/iob.h>
  43. #include <nuttx/net/netdev.h>
  44. #include "netdev/netdev.h"
  45. #ifdef CONFIG_NETDOWN_NOTIFIER
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: netdown_notifier_setup
  51. *
  52. * Description:
  53. * Set up to perform a callback to the worker function the network goes
  54. * down. The worker function will execute on the low priority worker
  55. * thread.
  56. *
  57. * Input Parameters:
  58. * worker - The worker function to execute on the low priority work
  59. * queue when data is available in the UDP read-ahead buffer.
  60. * dev - The network driver to be monitored
  61. * arg - A user-defined argument that will be available to the worker
  62. * function when it runs.
  63. * Returned Value:
  64. * > 0 - The notification is in place. The returned value is a key that
  65. * may be used later in a call to netdown_notifier_teardown().
  66. * == 0 - The the device is already down. No notification will be
  67. * provided.
  68. * < 0 - An unexpected error occurred and notification will occur. The
  69. * returned value is a negated errno value that indicates the
  70. * nature of the failure.
  71. *
  72. ****************************************************************************/
  73. int netdown_notifier_setup(worker_t worker, FAR struct net_driver_s *dev,
  74. FAR void *arg)
  75. {
  76. struct work_notifier_s info;
  77. DEBUGASSERT(worker != NULL);
  78. /* If network driver is already down, then return zero without setting up
  79. * the notification.
  80. */
  81. if ((dev->d_flags & IFF_UP) == 0)
  82. {
  83. return 0;
  84. }
  85. /* Otherwise, this is just a simple wrapper around work_notifer_setup(). */
  86. info.evtype = WORK_NET_DOWN;
  87. info.qid = LPWORK;
  88. info.qualifier = dev;
  89. info.arg = arg;
  90. info.worker = worker;
  91. return work_notifier_setup(&info);
  92. }
  93. /****************************************************************************
  94. * Name: netdown_notifier_teardown
  95. *
  96. * Description:
  97. * Eliminate a network down notification previously setup by
  98. * netdown_notifier_setup(). This function should only be called if the
  99. * notification should be aborted prior to the notification. The
  100. * notification will automatically be torn down after the notification.
  101. *
  102. * Input Parameters:
  103. * key - The key value returned from a previous call to
  104. * netdown_notifier_setup().
  105. *
  106. * Returned Value:
  107. * Zero (OK) is returned on success; a negated errno value is returned on
  108. * any failure.
  109. *
  110. ****************************************************************************/
  111. int netdown_notifier_teardown(int key)
  112. {
  113. /* This is just a simple wrapper around work_notifier_teardown(). */
  114. return work_notifier_teardown(key);
  115. }
  116. /****************************************************************************
  117. * Name: netdown_notifier_signal
  118. *
  119. * Description:
  120. * A network has gone down has been buffered. Execute worker thread
  121. * functions for all threads monitoring the state of the device.
  122. *
  123. * Input Parameters:
  124. * dev - The TCP connection where read-ahead data was just buffered.
  125. *
  126. * Returned Value:
  127. * None.
  128. *
  129. ****************************************************************************/
  130. void netdown_notifier_signal(FAR struct net_driver_s *dev)
  131. {
  132. /* This is just a simple wrapper around work_notifier_signal(). */
  133. return work_notifier_signal(WORK_NET_DOWN, dev);
  134. }
  135. #endif /* CONFIG_NETDOWN_NOTIFIER */