Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uip-split.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2004, Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the Institute nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * This file is part of the Contiki operating system.
  30. *
  31. * Author: Adam Dunkels <[email protected]>
  32. *
  33. * $Id: uip-split.c,v 1.2 2008/10/14 13:39:12 julienabeille Exp $
  34. */
  35. #include "uip-split.h"
  36. #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
  37. /*-----------------------------------------------------------------------------*/
  38. void
  39. uip_split_output(void)
  40. {
  41. #if UIP_TCP
  42. u16_t tcplen, len1, len2;
  43. /* We only try to split maximum sized TCP segments. */
  44. if(BUF->proto == UIP_PROTO_TCP && uip_len == UIP_BUFSIZE) {
  45. tcplen = uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN;
  46. /* Split the segment in two. If the original packet length was
  47. odd, we make the second packet one byte larger. */
  48. len1 = len2 = tcplen / 2;
  49. if(len1 + len2 < tcplen) {
  50. ++len2;
  51. }
  52. /* Create the first packet. This is done by altering the length
  53. field of the IP header and updating the checksums. */
  54. uip_len = len1 + UIP_TCPIP_HLEN + UIP_LLH_LEN;
  55. #if UIP_CONF_IPV6
  56. /* For IPv6, the IP length field does not include the IPv6 IP header
  57. length. */
  58. BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
  59. BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
  60. #else /* UIP_CONF_IPV6 */
  61. BUF->len[0] = (uip_len - UIP_LLH_LEN) >> 8;
  62. BUF->len[1] = (uip_len - UIP_LLH_LEN) & 0xff;
  63. #endif /* UIP_CONF_IPV6 */
  64. /* Recalculate the TCP checksum. */
  65. BUF->tcpchksum = 0;
  66. BUF->tcpchksum = ~(uip_tcpchksum());
  67. #if !UIP_CONF_IPV6
  68. /* Recalculate the IP checksum. */
  69. BUF->ipchksum = 0;
  70. BUF->ipchksum = ~(uip_ipchksum());
  71. #endif /* UIP_CONF_IPV6 */
  72. /* Transmit the first packet. */
  73. #if UIP_CONF_IPV6
  74. tcpip_ipv6_output();
  75. #else
  76. if (USB_CurrentMode == USB_MODE_Device)
  77. RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, uip_len);
  78. else
  79. RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, uip_len);
  80. #endif /* UIP_CONF_IPV6 */
  81. /* Now, create the second packet. To do this, it is not enough to
  82. just alter the length field, but we must also update the TCP
  83. sequence number and point the uip_appdata to a new place in
  84. memory. This place is determined by the length of the first
  85. packet (len1). */
  86. uip_len = len2 + UIP_TCPIP_HLEN + UIP_LLH_LEN;
  87. #if UIP_CONF_IPV6
  88. /* For IPv6, the IP length field does not include the IPv6 IP header
  89. length. */
  90. BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
  91. BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
  92. #else /* UIP_CONF_IPV6 */
  93. BUF->len[0] = (uip_len - UIP_LLH_LEN) >> 8;
  94. BUF->len[1] = (uip_len - UIP_LLH_LEN) & 0xff;
  95. #endif /* UIP_CONF_IPV6 */
  96. memcpy(uip_appdata, (u8_t *)uip_appdata + len1, len2);
  97. uip_add32(BUF->seqno, len1);
  98. BUF->seqno[0] = uip_acc32[0];
  99. BUF->seqno[1] = uip_acc32[1];
  100. BUF->seqno[2] = uip_acc32[2];
  101. BUF->seqno[3] = uip_acc32[3];
  102. /* Recalculate the TCP checksum. */
  103. BUF->tcpchksum = 0;
  104. BUF->tcpchksum = ~(uip_tcpchksum());
  105. #if !UIP_CONF_IPV6
  106. /* Recalculate the IP checksum. */
  107. BUF->ipchksum = 0;
  108. BUF->ipchksum = ~(uip_ipchksum());
  109. #endif /* UIP_CONF_IPV6 */
  110. /* Transmit the second packet. */
  111. #if UIP_CONF_IPV6
  112. tcpip_ipv6_output();
  113. #else
  114. if (USB_CurrentMode == USB_MODE_Device)
  115. RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, uip_len);
  116. else
  117. RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, uip_len);
  118. #endif /* UIP_CONF_IPV6 */
  119. return;
  120. }
  121. #endif /* UIP_TCP */
  122. /* uip_fw_output();*/
  123. #if UIP_CONF_IPV6
  124. tcpip_ipv6_output();
  125. #else
  126. if (USB_CurrentMode == USB_MODE_Device)
  127. RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, uip_len);
  128. else
  129. RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, uip_len);
  130. #endif /* UIP_CONF_IPV6 */
  131. }
  132. /*-----------------------------------------------------------------------------*/