udp_devpoll.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /****************************************************************************
  2. * net/udp/udp_devpoll.c
  3. * Network device poll for the availability of UDP TX data
  4. *
  5. * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Adapted for NuttX from logic in uIP which also has a BSD-like license:
  9. *
  10. * Original author Adam Dunkels <adam@dunkels.com>
  11. * Copyright () 2001-2003, Adam Dunkels.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. The name of the author may not be used to endorse or promote
  24. * products derived from this software without specific prior
  25. * written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  31. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  35. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ****************************************************************************/
  40. /****************************************************************************
  41. * Included Files
  42. ****************************************************************************/
  43. #include <nuttx/config.h>
  44. #if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
  45. #include <debug.h>
  46. #include <nuttx/net/netconfig.h>
  47. #include <nuttx/net/netdev.h>
  48. #include <nuttx/net/udp.h>
  49. #include "devif/devif.h"
  50. #include "udp/udp.h"
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: udp_poll
  56. *
  57. * Description:
  58. * Poll a UDP "connection" structure for availability of TX data
  59. *
  60. * Input Parameters:
  61. * dev - The device driver structure to use in the send operation
  62. * conn - The UDP "connection" to poll for TX data
  63. *
  64. * Returned Value:
  65. * None
  66. *
  67. * Assumptions:
  68. * The network is locked.
  69. *
  70. ****************************************************************************/
  71. void udp_poll(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn)
  72. {
  73. /* Verify that the UDP connection is valid */
  74. if (conn->lport != 0)
  75. {
  76. /* Set up for the callback. We can't know in advance if the application
  77. * is going to send a IPv4 or an IPv6 packet, so this setup may not
  78. * actually be used.
  79. */
  80. #if defined(CONFIG_NET_IPv4)
  81. udp_ipv4_select(dev);
  82. #else /* if defined(CONFIG_NET_IPv6) */
  83. udp_ipv6_select(dev);
  84. #endif
  85. dev->d_len = 0;
  86. dev->d_sndlen = 0;
  87. /* Perform the application callback */
  88. (void)udp_callback(dev, conn, UDP_POLL);
  89. /* If the application has data to send, setup the UDP/IP header */
  90. if (dev->d_sndlen > 0)
  91. {
  92. udp_send(dev, conn);
  93. return;
  94. }
  95. }
  96. /* Make sure that d_len is zero meaning that there is nothing to be sent */
  97. dev->d_len = 0;
  98. }
  99. #endif /* CONFIG_NET && CONFIG_NET_UDP */