tcp_devpoll.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /****************************************************************************
  2. * net/tcp/tcp_devpoll.c
  3. * Driver poll for the availability of TCP TX data
  4. *
  5. * Copyright (C) 2007-2009, 2016-2017 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_TCP)
  45. #include <stdint.h>
  46. #include <assert.h>
  47. #include <debug.h>
  48. #include <nuttx/net/netconfig.h>
  49. #include <nuttx/net/netdev.h>
  50. #include <nuttx/net/tcp.h>
  51. #include "devif/devif.h"
  52. #include "tcp/tcp.h"
  53. /****************************************************************************
  54. * Public Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: tcp_poll
  58. *
  59. * Description:
  60. * Poll a TCP connection structure for availability of TX data
  61. *
  62. * Input Parameters:
  63. * dev - The device driver structure to use in the send operation
  64. * conn - The TCP "connection" to poll for TX data
  65. *
  66. * Returned Value:
  67. * None
  68. *
  69. * Assumptions:
  70. * Called with the network locked.
  71. *
  72. ****************************************************************************/
  73. void tcp_poll(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn)
  74. {
  75. uint16_t result;
  76. /* Discard any currently buffered data */
  77. dev->d_len = 0;
  78. dev->d_sndlen = 0;
  79. /* Verify that the connection is established. */
  80. if ((conn->tcpstateflags & TCP_STATE_MASK) == TCP_ESTABLISHED)
  81. {
  82. /* The TCP connection is established and, hence, should be bound
  83. * to a device. Make sure that the polling device is the one that
  84. * we are bound to.
  85. */
  86. DEBUGASSERT(conn->dev != NULL);
  87. if (dev == conn->dev)
  88. {
  89. /* Set up for the callback. We can't know in advance if the
  90. * application is going to send a IPv4 or an IPv6 packet, so this
  91. * setup may not actually be used.
  92. */
  93. #if defined(CONFIG_NET_IPv6) && defined(CONFIG_NET_IPv4)
  94. if (conn->domain == PF_INET)
  95. {
  96. tcp_ipv4_select(dev);
  97. }
  98. else
  99. {
  100. tcp_ipv6_select(dev);
  101. }
  102. #elif defined(CONFIG_NET_IPv4)
  103. tcp_ipv4_select(dev);
  104. #else /* if defined(CONFIG_NET_IPv6) */
  105. tcp_ipv6_select(dev);
  106. #endif
  107. /* Perform the callback */
  108. result = tcp_callback(dev, conn, TCP_POLL);
  109. /* Handle the callback response */
  110. tcp_appsend(dev, conn, result);
  111. }
  112. }
  113. }
  114. #endif /* CONFIG_NET && CONFIG_NET_TCP */