Skip to content

Commit

Permalink
[SQUASH ME] ng_sixlowpan: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
miri64 committed Apr 10, 2015
1 parent 206db5a commit a2c0e06
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sys/include/net/ng_sixlowpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ extern "C" {
#define NG_SIXLOWPAN_MSG_QUEUE_SIZE (8U)
#endif

#define NG_SIXLOWPAN_DISPATCH_IPV6 (0x41)
#define NG_SIXLOWPAN_UNCOMPRESSED (0x41)

kernel_pid_t ng_sixlowpan_init(void);

Expand Down
94 changes: 49 additions & 45 deletions sys/net/network_layer/ng_sixlowpan/ng_sixlowpan.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ void _handle_receive(ng_pktsnip_t *pkt)
LL_SEARCH_SCALAR(pkt, payload, type, NG_NETTYPE_SIXLOWPAN);

if ((payload == NULL) || (payload->size < 1)) {
DEBUG("sixlowpan: Received packet has no 6LoWPAN payload\n");
DEBUG("6lo: Received packet has no 6LoWPAN payload\n");
ng_pktbuf_release(pkt);
}

dispatch = payload->data;

if (dispatch[0] == NG_SIXLOWPAN_DISPATCH_IPV6) {
if (dispatch[0] == NG_SIXLOWPAN_UNCOMPRESSED) {
ng_pktsnip_t *sixlowpan;
DEBUG("sixlowpan: received uncompressed IPv6 packet\n");
DEBUG("6lo: received uncompressed IPv6 packet\n");
payload = ng_pktbuf_start_write(payload);

if (payload == NULL) {
DEBUG("sixlowpan: can not get write access on received packet\n");
DEBUG("6lo: can not get write access on received packet\n");
#if defined(DEVELHELP) && defined(ENABLE_DEBUG)
ng_pktbuf_stats();
#endif
Expand All @@ -61,14 +61,8 @@ void _handle_receive(ng_pktsnip_t *pkt)
LL_DELETE(pkt, sixlowpan);
ng_pktbuf_release(sixlowpan);
}
#ifdef MODULE_NG_SIXLOWPAN_FRAG
else if (ng_sixlowpan_frag_is((ng_sixlowpan_frag_t *)dispatch)) {
DEBUG("sixlowpan: received 6LoWPAN fragment\n");
ng_sixlowpan_frag_handle_pkt(pkt);
}
#endif
else {
DEBUG("sixlowpan: dispatch %02x ... was not recognized\n",
DEBUG("6lo: dispatch %02x ... was not recognized\n",
dispatch[0]);
ng_pktbuf_release(pkt);
return;
Expand All @@ -87,7 +81,7 @@ void _handle_receive(ng_pktsnip_t *pkt)
ng_pktbuf_hold(pkt, ng_netreg_num(NG_NETTYPE_IPV6, NG_NETREG_DEMUX_CTX_ALL) - 1);

while (entry) {
DEBUG("sixlowpan: Send receive command for %p to %" PRIu16 "\n",
DEBUG("6lo: Send receive command for %p to %" PRIu16 "\n",
(void *)pkt, entry->pid);
ng_netapi_receive(entry->pid, pkt);
entry = ng_netreg_getnext(entry);
Expand All @@ -99,10 +93,12 @@ void _handle_send(ng_pktsnip_t *pkt)
ng_netif_hdr_t *hdr;
ng_pktsnip_t *ipv6, *sixlowpan;
ng_sixlowpan_netif_t *iface;
size_t payload_len, datagram_size;
uint16_t max_frag_size;
uint8_t *disp;

if ((pkt == NULL) || (pkt->size < sizeof(ng_netif_hdr_t))) {
DEBUG("sixlowpan: Sending packet has no netif header\n");
DEBUG("6lo: Sending packet has no netif header\n");
ng_pktbuf_release(pkt);
return;
}
Expand All @@ -111,28 +107,52 @@ void _handle_send(ng_pktsnip_t *pkt)
ipv6 = pkt->next;

if ((ipv6 == NULL) || (ipv6->type != NG_NETTYPE_IPV6)) {
DEBUG("sixlowpan: Sending packet has no IPv6 header\n");
DEBUG("6lo: Sending packet has no IPv6 header\n");
ng_pktbuf_release(pkt);
return;
}

/* payload length and datagram size are different in that the payload
* length is the length of the IPv6 datagram + 6LoWPAN dispatches,
* while the datagram size is the size of only the IPv6 datagram */
payload_len = ng_pkt_len(ipv6);
datagram_size = (uint16_t)payload_len;

/* use sixlowpan packet snip as temporary one */
sixlowpan = ng_pktbuf_start_write(pkt);

if (sixlowpan == NULL) {
DEBUG("sixlowpan: no space left in packet buffer\n");
DEBUG("6lo: no space left in packet buffer\n");
ng_pktbuf_release(pkt);
return;
}

pkt = sixlowpan;

DEBUG("6lo: Send uncompressed\n");

sixlowpan = ng_pktbuf_add(NULL, NULL, sizeof(uint8_t), NG_NETTYPE_SIXLOWPAN);

if (sixlowpan == NULL) {
DEBUG("6lo: no space left in packet buffer\n");
ng_pktbuf_release(pkt);
return;
}

sixlowpan->next = ipv6;
pkt->next = sixlowpan;
disp = sixlowpan->data;
disp[0] = NG_SIXLOWPAN_UNCOMPRESSED;
payload_len++;

iface = ng_sixlowpan_netif_get(hdr->if_pid);

if (iface == NULL) {
if (ng_netapi_get(hdr->if_pid, NETCONF_OPT_MAX_PACKET_SIZE,
0, &max_frag_size, sizeof(max_frag_size)) >= 0) {
0, &max_frag_size, sizeof(max_frag_size)) < 0) {
/* if error we assume it works */
DEBUG("6lo: can not get max packet size from interface %"
PRIkernel_pid "\n", hdr->if_pid);
max_frag_size = UINT16_MAX;
}

Expand All @@ -142,36 +162,20 @@ void _handle_send(ng_pktsnip_t *pkt)
max_frag_size = iface->max_frag_size;
}

DEBUG("6lo: max_frag_size = %" PRIu16 " for interface %"
PRIkernel_pid "\n", max_frag_size, hdr->if_pid);

/* IP should not send anything here if it is not a 6LoWPAN interface,
* so we don't need to check for NULL pointers */
if (ng_pkt_len(pkt) <= max_frag_size) {
uint8_t *disp;

sixlowpan = ng_pktbuf_add(NULL, NULL, sizeof(uint8_t), NG_NETTYPE_SIXLOWPAN);

if (sixlowpan == NULL) {
DEBUG("sixlowpan: no space left in packet buffer\n");
ng_pktbuf_release(pkt);
return;
}

sixlowpan->next = ipv6;
pkt->next = sixlowpan;
disp = sixlowpan->data;
disp[0] = NG_SIXLOWPAN_DISPATCH_IPV6;

while (sixlowpan) {
od_hex_dump(sixlowpan->data, sixlowpan->size, 0);
sixlowpan = sixlowpan->next;
}

DEBUG("sixlowpan: Send send command for %p to %" PRIu16 "\n",
if (payload_len <= max_frag_size) {
DEBUG("6lo: Send SND command for %p to %" PRIu16 "\n",
(void *)pkt, hdr->if_pid);
ng_netapi_send(hdr->if_pid, pkt);

return;
}
DEBUG("sixlowpan: packet too big\n");
DEBUG("6lo: packet too big (%u> %" PRIu16 ")\n",
(unsigned int)payload_len, max_frag_size);
}

static void *_sixlowpan_thread(void *args)
Expand All @@ -193,29 +197,29 @@ static void *_sixlowpan_thread(void *args)

/* start event loop */
while (1) {
DEBUG("sixlowpan: waiting for incoming message.\n");
DEBUG("6lo: waiting for incoming message.\n");
msg_receive(&msg);

switch (msg.type) {
case NG_NETAPI_MSG_TYPE_RCV:
DEBUG("sixlowpan: NG_NETDEV_MSG_TYPE_RCV received\n");
DEBUG("6lo: NG_NETDEV_MSG_TYPE_RCV received\n");
_handle_receive((ng_pktsnip_t *)msg.content.ptr);
break;

case NG_NETAPI_MSG_TYPE_SND:
DEBUG("sixlowpan: NG_NETDEV_MSG_TYPE_SND received\n");
DEBUG("6lo: NG_NETDEV_MSG_TYPE_SND received\n");
_handle_send((ng_pktsnip_t *)msg.content.ptr);
break;

case NG_NETAPI_MSG_TYPE_GET:
case NG_NETAPI_MSG_TYPE_SET:
DEBUG("sixlowpan: reply to unsupported get/set\n");
DEBUG("6lo: reply to unsupported get/set\n");
reply.content.value = -ENOTSUP;
msg_reply(&msg, &reply);
break;

default:
DEBUG("sixlowpan: operation not supported\n");
DEBUG("6lo: operation not supported\n");
break;
}
}
Expand All @@ -231,7 +235,7 @@ kernel_pid_t ng_sixlowpan_init(void)

_sixlowpan_pid = thread_create(_sixlowpan_stack, NG_SIXLOWPAN_STACK_SIZE,
NG_SIXLOWPAN_PRIO, CREATE_STACKTEST,
_sixlowpan_thread, NULL, "6lowpan");
_sixlowpan_thread, NULL, "6lo");

if (_sixlowpan_pid <= 0) {
kernel_pid_t res = _sixlowpan_pid;
Expand Down

0 comments on commit a2c0e06

Please # to comment.