aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lufa/Projects/Webserver/Lib/uip/uip-split.c
blob: 5222a05b63e337e2ff3e73662ba036a78c3d10f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2004, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file is part of the Contiki operating system.
 *
 * Author: Adam Dunkels <adam@sics.se>
 *
 * $Id: uip-split.c,v 1.2 2008/10/14 13:39:12 julienabeille Exp $
 */

#include "uip-split.h"


#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])

/*-----------------------------------------------------------------------------*/
void
uip_split_output(void)
{
#if UIP_TCP
  u16_t tcplen, len1, len2;

  /* We only try to split maximum sized TCP segments. */
  if(BUF->proto == UIP_PROTO_TCP  && uip_len == UIP_BUFSIZE) {

    tcplen = uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN;
    /* Split the segment in two. If the original packet length was
       odd, we make the second packet one byte larger. */
    len1 = len2 = tcplen / 2;
    if(len1 + len2 < tcplen) {
      ++len2;
    }

    /* Create the first packet. This is done by altering the length
       field of the IP header and updating the checksums. */
    uip_len = len1 + UIP_TCPIP_HLEN + UIP_LLH_LEN;
#if UIP_CONF_IPV6
    /* For IPv6, the IP length field does not include the IPv6 IP header
       length. */
    BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
    BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
    BUF->len[0] = (uip_len - UIP_LLH_LEN) >> 8;
    BUF->len[1] = (uip_len - UIP_LLH_LEN) & 0xff;
#endif /* UIP_CONF_IPV6 */

    /* Recalculate the TCP checksum. */
    BUF->tcpchksum = 0;
    BUF->tcpchksum = ~(uip_tcpchksum());

#if !UIP_CONF_IPV6
    /* Recalculate the IP checksum. */
    BUF->ipchksum = 0;
    BUF->ipchksum = ~(uip_ipchksum());
#endif /* UIP_CONF_IPV6 */

    /* Transmit the first packet. */
#if UIP_CONF_IPV6
    tcpip_ipv6_output();
#else
	if (USB_CurrentMode == USB_MODE_Device)
	  RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, uip_len);
	else
	  RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, uip_len);
#endif /* UIP_CONF_IPV6 */

    /* Now, create the second packet. To do this, it is not enough to
       just alter the length field, but we must also update the TCP
       sequence number and point the uip_appdata to a new place in
       memory. This place is determined by the length of the first
       packet (len1). */
    uip_len = len2 + UIP_TCPIP_HLEN + UIP_LLH_LEN;
#if UIP_CONF_IPV6
    /* For IPv6, the IP length field does not include the IPv6 IP header
       length. */
    BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
    BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
    BUF->len[0] = (uip_len  - UIP_LLH_LEN) >> 8;
    BUF->len[1] = (uip_len - UIP_LLH_LEN) & 0xff;
#endif /* UIP_CONF_IPV6 */

    memcpy(uip_appdata, (u8_t *)uip_appdata + len1, len2);

    uip_add32(BUF->seqno, len1);
    BUF->seqno[0] = uip_acc32[0];
    BUF->seqno[1] = uip_acc32[1];
    BUF->seqno[2] = uip_acc32[2];
    BUF->seqno[3] = uip_acc32[3];

    /* Recalculate the TCP checksum. */
    BUF->tcpchksum = 0;
    BUF->tcpchksum = ~(uip_tcpchksum());

#if !UIP_CONF_IPV6
    /* Recalculate the IP checksum. */
    BUF->ipchksum = 0;
    BUF->ipchksum = ~(uip_ipchksum());
#endif /* UIP_CONF_IPV6 */

    /* Transmit the second packet. */
#if UIP_CONF_IPV6
    tcpip_ipv6_output();
#else
	if (USB_CurrentMode == USB_MODE_Device)
	  RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, uip_len);
	else
	  RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, uip_len);
#endif /* UIP_CONF_IPV6 */
    return;
  }
#endif /* UIP_TCP */

  /*    uip_fw_output();*/
#if UIP_CONF_IPV6
	tcpip_ipv6_output();
#else
	if (USB_CurrentMode == USB_MODE_Device)
	  RNDIS_Device_SendPacket(&Ethernet_RNDIS_Interface_Device, uip_buf, uip_len);
	else
	  RNDIS_Host_SendPacket(&Ethernet_RNDIS_Interface_Host, uip_buf, uip_len);
#endif /* UIP_CONF_IPV6 */
}

/*-----------------------------------------------------------------------------*/
ilbox_t structure to be * initialized * @param[in] buf pointer to the messages buffer as an array of @p msg_t * @param[in] n number of elements in the buffer array * * @init */ void chMBObjectInit(mailbox_t *mbp, msg_t *buf, cnt_t n) { chDbgCheck((mbp != NULL) && (buf != NULL) && (n > (cnt_t)0)); mbp->buffer = buf; mbp->rdptr = buf; mbp->wrptr = buf; mbp->top = &buf[n]; mbp->cnt = (cnt_t)0; mbp->reset = false; chThdQueueObjectInit(&mbp->qw); chThdQueueObjectInit(&mbp->qr); } /** * @brief Resets a @p mailbox_t object. * @details All the waiting threads are resumed with status @p MSG_RESET and * the queued messages are lost. * @post The mailbox is in reset state, all operations will fail and * return @p MSG reset until the mailbox is enabled again using * @p chMBResumeX(). * * @param[in] mbp the pointer to an initialized @p mailbox_t object * * @api */ void chMBReset(mailbox_t *mbp) { chSysLock(); chMBResetI(mbp); chSchRescheduleS(); chSysUnlock(); } /** * @brief Resets a @p mailbox_t object. * @details All the waiting threads are resumed with status @p MSG_RESET and * the queued messages are lost. * @post The mailbox is in reset state, all operations will fail and * return @p MSG reset until the mailbox is enabled again using * @p chMBResumeX(). * * @param[in] mbp the pointer to an initialized @p mailbox_t object * * @api */ void chMBResetI(mailbox_t *mbp) { chDbgCheckClassI(); chDbgCheck(mbp != NULL); mbp->wrptr = mbp->buffer; mbp->rdptr = mbp->buffer; mbp->cnt = (cnt_t)0; mbp->reset = true; chThdDequeueAllI(&mbp->qw, MSG_RESET); chThdDequeueAllI(&mbp->qr, MSG_RESET); } /** * @brief Posts a message into a mailbox. * @details The invoking thread waits until a empty slot in the mailbox becomes * available or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @api */ msg_t chMBPost(mailbox_t *mbp, msg_t msg, systime_t timeout) { msg_t rdymsg; chSysLock(); rdymsg = chMBPostS(mbp, msg, timeout); chSysUnlock(); return rdymsg; } /** * @brief Posts a message into a mailbox. * @details The invoking thread waits until a empty slot in the mailbox becomes * available or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @sclass */ msg_t chMBPostS(mailbox_t *mbp, msg_t msg, systime_t timeout) { msg_t rdymsg; chDbgCheckClassS(); chDbgCheck(mbp != NULL); do { /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a free message slot in queue? if so then post.*/ if (chMBGetFreeCountI(mbp) > (cnt_t)0) { *mbp->wrptr++ = msg; if (mbp->wrptr >= mbp->top) { mbp->wrptr = mbp->buffer; } mbp->cnt++; /* If there is a reader waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qr, MSG_OK); chSchRescheduleS(); return MSG_OK; } /* No space in the queue, waiting for a slot to become available.*/ rdymsg = chThdEnqueueTimeoutS(&mbp->qw, timeout); } while (rdymsg == MSG_OK); return rdymsg; } /** * @brief Posts a message into a mailbox. * @details This variant is non-blocking, the function returns a timeout * condition if the queue is full. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the mailbox is full and the message cannot be * posted. * * @iclass */ msg_t chMBPostI(mailbox_t *mbp, msg_t msg) { chDbgCheckClassI(); chDbgCheck(mbp != NULL); /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a free message slot in queue? if so then post.*/ if (chMBGetFreeCountI(mbp) > (cnt_t)0) { *mbp->wrptr++ = msg; if (mbp->wrptr >= mbp->top) { mbp->wrptr = mbp->buffer; } mbp->cnt++; /* If there is a reader waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qr, MSG_OK); return MSG_OK; } /* No space, immediate timeout.*/ return MSG_TIMEOUT; } /** * @brief Posts an high priority message into a mailbox. * @details The invoking thread waits until a empty slot in the mailbox becomes * available or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @api */ msg_t chMBPostAhead(mailbox_t *mbp, msg_t msg, systime_t timeout) { msg_t rdymsg; chSysLock(); rdymsg = chMBPostAheadS(mbp, msg, timeout); chSysUnlock(); return rdymsg; } /** * @brief Posts an high priority message into a mailbox. * @details The invoking thread waits until a empty slot in the mailbox becomes * available or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @sclass */ msg_t chMBPostAheadS(mailbox_t *mbp, msg_t msg, systime_t timeout) { msg_t rdymsg; chDbgCheckClassS(); chDbgCheck(mbp != NULL); do { /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a free message slot in queue? if so then post.*/ if (chMBGetFreeCountI(mbp) > (cnt_t)0) { if (--mbp->rdptr < mbp->buffer) { mbp->rdptr = mbp->top - 1; } *mbp->rdptr = msg; mbp->cnt++; /* If there is a reader waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qr, MSG_OK); chSchRescheduleS(); return MSG_OK; } /* No space in the queue, waiting for a slot to become available.*/ rdymsg = chThdEnqueueTimeoutS(&mbp->qw, timeout); } while (rdymsg == MSG_OK); return rdymsg; } /** * @brief Posts an high priority message into a mailbox. * @details This variant is non-blocking, the function returns a timeout * condition if the queue is full. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[in] msg the message to be posted on the mailbox * @return The operation status. * @retval MSG_OK if a message has been correctly posted. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the mailbox is full and the message cannot be * posted. * * @iclass */ msg_t chMBPostAheadI(mailbox_t *mbp, msg_t msg) { chDbgCheckClassI(); chDbgCheck(mbp != NULL); /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a free message slot in queue? if so then post.*/ if (chMBGetFreeCountI(mbp) > (cnt_t)0) { if (--mbp->rdptr < mbp->buffer) { mbp->rdptr = mbp->top - 1; } *mbp->rdptr = msg; mbp->cnt++; /* If there is a reader waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qr, MSG_OK); return MSG_OK; } /* No space, immediate timeout.*/ return MSG_TIMEOUT; } /** * @brief Retrieves a message from a mailbox. * @details The invoking thread waits until a message is posted in the mailbox * or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[out] msgp pointer to a message variable for the received message * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly fetched. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @api */ msg_t chMBFetch(mailbox_t *mbp, msg_t *msgp, systime_t timeout) { msg_t rdymsg; chSysLock(); rdymsg = chMBFetchS(mbp, msgp, timeout); chSysUnlock(); return rdymsg; } /** * @brief Retrieves a message from a mailbox. * @details The invoking thread waits until a message is posted in the mailbox * or the specified time runs out. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[out] msgp pointer to a message variable for the received message * @param[in] timeout the number of ticks before the operation timeouts, * the following special values are allowed: * - @a TIME_IMMEDIATE immediate timeout. * - @a TIME_INFINITE no timeout. * . * @return The operation status. * @retval MSG_OK if a message has been correctly fetched. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the operation has timed out. * * @sclass */ msg_t chMBFetchS(mailbox_t *mbp, msg_t *msgp, systime_t timeout) { msg_t rdymsg; chDbgCheckClassS(); chDbgCheck((mbp != NULL) && (msgp != NULL)); do { /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a message in queue? if so then fetch.*/ if (chMBGetUsedCountI(mbp) > (cnt_t)0) { *msgp = *mbp->rdptr++; if (mbp->rdptr >= mbp->top) { mbp->rdptr = mbp->buffer; } mbp->cnt--; /* If there is a writer waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qw, MSG_OK); chSchRescheduleS(); return MSG_OK; } /* No message in the queue, waiting for a message to become available.*/ rdymsg = chThdEnqueueTimeoutS(&mbp->qr, timeout); } while (rdymsg == MSG_OK); return rdymsg; } /** * @brief Retrieves a message from a mailbox. * @details This variant is non-blocking, the function returns a timeout * condition if the queue is empty. * * @param[in] mbp the pointer to an initialized @p mailbox_t object * @param[out] msgp pointer to a message variable for the received message * @return The operation status. * @retval MSG_OK if a message has been correctly fetched. * @retval MSG_RESET if the mailbox has been reset. * @retval MSG_TIMEOUT if the mailbox is empty and a message cannot be * fetched. * * @iclass */ msg_t chMBFetchI(mailbox_t *mbp, msg_t *msgp) { chDbgCheckClassI(); chDbgCheck((mbp != NULL) && (msgp != NULL)); /* If the mailbox is in reset state then returns immediately.*/ if (mbp->reset) { return MSG_RESET; } /* Is there a message in queue? if so then fetch.*/ if (chMBGetUsedCountI(mbp) > (cnt_t)0) { *msgp = *mbp->rdptr++; if (mbp->rdptr >= mbp->top) { mbp->rdptr = mbp->buffer; } mbp->cnt--; /* If there is a writer waiting then makes it ready.*/ chThdDequeueNextI(&mbp->qw, MSG_OK); return MSG_OK; } /* No message, immediate timeout.*/ return MSG_TIMEOUT; } #endif /* CH_CFG_USE_MAILBOXES == TRUE */ /** @} */