net: Make sure Contiki will not remove received packet

We must not touch uip_len(buf) value as Contiki IP stack
will use it to determine what it should do with the
received packet. So after we have received the packet,
we update separate datalen variable in net_buf that will
store the correct length of the packet.

Change-Id: Iab79b741508e95e581d1727645e6b1d1eacded4c
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2015-07-31 16:24:28 +03:00 committed by Anas Nashif
commit e6d1925a96
7 changed files with 23 additions and 12 deletions

View file

@ -97,6 +97,7 @@ struct net_buf {
/** @cond ignore */
/* uIP stack specific data */
uint16_t len;
uint8_t uip_ext_len;
uint8_t uip_ext_bitmap;
uint8_t uip_ext_opt_offset;
@ -123,13 +124,16 @@ struct net_buf {
/* @endcond */
/** Buffer data length */
uint16_t len;
uint16_t datalen;
/** Buffer head pointer */
uint8_t *data;
/** Actual network buffer storage */
uint8_t buf[NET_BUF_MAX_DATA];
};
#define net_buf_data(buf) ((buf)->data)
#define net_buf_datalen(buf) ((buf)->datalen)
/** @cond ignore */
/* Macros to access net_buf when inside Contiki stack */
#define uip_buf(buf) ((buf)->buf)

View file

@ -124,8 +124,8 @@ int net_set_mac(uint8_t *mac, uint8_t len);
*
* @details Application can call this function if it has received
* a network packet from peer. The application needs to write
* reply data into net_buf. The app can use uip_appdata(buf) and
* uip_appdatalen(buf) to set the application data and length.
* reply data into net_buf. The app can use net_buf_data(buf) and
* net_buf_datalen(buf) to set the application data and length.
*
* @param context Network context
* @param buf Network buffer containing the network data.

View file

@ -330,9 +330,10 @@ static void udp_packet_receive(struct simple_udp_connection *c,
}
uip_appdatalen(buf) = datalen;
buf->datalen = datalen;
NET_DBG("packet received buf %p context %p len %d\n",
buf, context, datalen);
NET_DBG("packet received buf %p context %p len %d appdatalen %d\n",
buf, context, buf->len, datalen);
nano_fifo_put(net_context_get_queue(context), buf);
}
@ -424,6 +425,13 @@ static void udp_packet_reply(struct simple_udp_connection *c,
NET_DBG("packet reply buf %p context %p len %d queue %p\n",
buf, context, buf->len, queue);
/* Contiki stack will overwrite the uip_len(buf) and
* uip_appdatalen(buf) values, so in order to allow
* the application to use them, copy the values here.
*/
buf->datalen = uip_len(buf);
buf->data = uip_appdata(buf);
nano_fifo_put(queue, buf);
}

View file

@ -108,14 +108,14 @@ static inline struct net_buf *prepare_reply(const char *name,
const char *type,
struct net_buf *buf)
{
PRINT("%s: %sreceived %d bytes\n", name, type, uip_appdatalen(buf));
PRINT("%s: %sreceived %d bytes\n", name, type, net_buf_datalen(buf));
/* In this test we reverse the received bytes.
* We could just pass the data back as is but
* this way it is possible to see how the app
* can manipulate the received data.
*/
reverse(uip_appdata(buf), uip_appdatalen(buf));
reverse(net_buf_data(buf), net_buf_datalen(buf));
/* Set the mac address of the peer in net_buf because
* there is no radio layer involved in this test app.

View file

@ -137,7 +137,7 @@ void helloLoop(const char *taskname, ksem_t mySem, ksem_t otherSem)
buf = net_receive(ctx, TICKS_NONE);
if (buf) {
PRINT("%s: received %d bytes\n", taskname,
uip_appdatalen(buf));
net_buf_datalen(buf));
net_buf_put(buf);
}
@ -209,7 +209,7 @@ void fiberEntry(void)
buf = net_receive(ctx, TICKS_NONE);
if (buf) {
PRINT("%s: received %d bytes\n", __FUNCTION__,
uip_appdatalen(buf));
net_buf_datalen(buf));
net_buf_put(buf);
}

View file

@ -131,7 +131,7 @@ void fiberEntry(void)
buf = net_receive(ctx, TICKS_NONE);
if (buf) {
PRINT("%s: received %d bytes\n", __FUNCTION__,
uip_appdatalen(buf));
net_buf_datalen(buf));
net_buf_put(buf);
}

View file

@ -163,8 +163,7 @@ static void receive_data(const char *taskname, struct net_context *ctx)
buf = net_receive(ctx, TICKS_NONE);
if (buf) {
PRINT("%s: %s(): received %d bytes\n%s\n", taskname,
__FUNCTION__, uip_appdatalen(buf),
uip_appdata(buf));
__FUNCTION__, net_buf_data(buf), net_buf_datalen(buf));
net_buf_put(buf);
}
}