From 6891ca12b06cfecbc5a16759e592051f144e6b80 Mon Sep 17 00:00:00 2001 From: Laurent Deru Date: Fri, 12 Jun 2015 12:59:51 +0200 Subject: [PATCH 1/2] Add crc to packets send by coojamote --- .../contikimote/interfaces/ContikiRadio.java | 15 +++- .../org/contikios/cooja/util/CCITT_CRC.java | 89 +++++++++++++++++++ 2 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tools/cooja/java/org/contikios/cooja/util/CCITT_CRC.java diff --git a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiRadio.java b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiRadio.java index 5502b9a8a..bbc4a2abd 100644 --- a/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiRadio.java +++ b/tools/cooja/java/org/contikios/cooja/contikimote/interfaces/ContikiRadio.java @@ -48,7 +48,7 @@ import org.contikios.cooja.interfaces.Position; import org.contikios.cooja.interfaces.Radio; import org.contikios.cooja.mote.memory.VarMemory; import org.contikios.cooja.radiomediums.UDGM; - +import org.contikios.cooja.util.CCITT_CRC; /** * Packet radio transceiver mote interface. * @@ -200,7 +200,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA packetToMote = null; myMoteMemory.setIntValueOf("simInSize", 0); } else { - myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length); + myMoteMemory.setIntValueOf("simInSize", packetToMote.getPacketData().length - 2); myMoteMemory.setByteArray("simInDataBuffer", packetToMote.getPacketData()); } @@ -330,7 +330,7 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA /* New transmission */ int size = myMoteMemory.getIntValueOf("simOutSize"); if (!isTransmitting && size > 0) { - packetFromMote = new COOJARadioPacket(myMoteMemory.getByteArray("simOutDataBuffer", size)); + packetFromMote = new COOJARadioPacket(myMoteMemory.getByteArray("simOutDataBuffer", size + 2)); if (packetFromMote.getPacketData() == null || packetFromMote.getPacketData().length == 0) { logger.warn("Skipping zero sized Contiki packet (no buffer)"); @@ -339,6 +339,15 @@ public class ContikiRadio extends Radio implements ContikiMoteInterface, PolledA return; } + byte[] data = packetFromMote.getPacketData(); + CCITT_CRC txCrc = new CCITT_CRC(); + txCrc.setCRC(0); + for (int i = 0; i < size; i++) { + txCrc.addBitrev(data[i]); + } + data[size] = (byte)txCrc.getCRCHi(); + data[size + 1] = (byte)txCrc.getCRCLow(); + isTransmitting = true; /* Calculate transmission duration (us) */ diff --git a/tools/cooja/java/org/contikios/cooja/util/CCITT_CRC.java b/tools/cooja/java/org/contikios/cooja/util/CCITT_CRC.java new file mode 100644 index 000000000..ea991208b --- /dev/null +++ b/tools/cooja/java/org/contikios/cooja/util/CCITT_CRC.java @@ -0,0 +1,89 @@ +/** + * Copyright (c) 2008, 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 MSPSim. + * + * ----------------------------------------------------------------- + * + * Author : Joakim Eriksson + */ + +package org.contikios.cooja.util; + +/* basic CRC-CCITT code */ +public class CCITT_CRC { + int crc; + + public int getCRC() { + return crc; + } + + /* this will only work with zero... */ + public void setCRC(int val) { + crc = val; + } + + public void clr() { + crc = 0xffff; + } + + public void addBitrev(int data) { + add(bitrev(data)); + } + + public int getCRCLow() { + return bitrev(crc & 0xff); + } + + public int getCRCHi() { + return bitrev(crc >> 8); + } + + + public int add(int data) { + int newCrc = ((crc >> 8) & 0xff) | (crc << 8) & 0xffff; + newCrc ^= (data & 0xff); + newCrc ^= (newCrc & 0xff) >> 4; + newCrc ^= (newCrc << 12) & 0xffff; + newCrc ^= (newCrc & 0xff) << 5; + crc = newCrc & 0xffff; + return crc; + } + + public int getCRCBitrev() { + return getCRCLow() + (getCRCHi() << 8); + } + + public static final String hex = "0123456789abcdef"; + + private static int bitrev(int data) { + return ((data << 7) & 0x80) | ((data << 5) & 0x40) | + (data << 3) & 0x20 | (data << 1) & 0x10 | + (data >> 7) & 0x01 | (data >> 5) & 0x02 | + (data >> 3) & 0x04 | (data >> 1) & 0x08; + } +} From 68c284b955fb839085e3898e745a79b97efe0bed Mon Sep 17 00:00:00 2001 From: Laurent Deru Date: Fri, 12 Jun 2015 13:01:18 +0200 Subject: [PATCH 2/2] Do not add a CRC to packet incoming from RfListener --- .../cooja/mspmote/interfaces/CC2420RadioPacketConverter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/CC2420RadioPacketConverter.java b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/CC2420RadioPacketConverter.java index fd62da50f..a4eebaaab 100644 --- a/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/CC2420RadioPacketConverter.java +++ b/tools/cooja/apps/mspsim/src/org/contikios/cooja/mspmote/interfaces/CC2420RadioPacketConverter.java @@ -46,7 +46,7 @@ public class CC2420RadioPacketConverter { public static final boolean WITH_XMAC = false; /* XXX No longer supported. Cross-level requires NULLMAC */ public static final boolean WITH_CHECKSUM = false; /* Contiki checksum. Not CC2420's built-in. */ public static final boolean WITH_TIMESTAMP = false; /* Contiki timestamp */ - public static final boolean WITH_FOOTER = true; /* CC2420's checksum */ + public static final boolean WITH_FOOTER = false; /* CC2420's checksum */ public static byte[] fromCoojaToCC2420(RadioPacket packet) { byte cc2420Data[] = new byte[6+127];