public static byte[] fromCoojaToCC2420(RadioPacket packet) {
byte cc2420Data[] = new byte[6+127];
int pos = 0;
byte packetData[] = packet.getPacketData();
byte len; /* total packet minus preamble(4), synch(1) and length(1) */
CCITT_CRC cc2420CRC = new CCITT_CRC();
short contikiCRC = 0;
/* 4 bytes preamble */
if (WITH_PREAMBLE) {
cc2420Data[pos++] = 0;
cc2420Data[pos++] = 0;
cc2420Data[pos++] = 0;
cc2420Data[pos++] = 0;
}
/* 1 byte synch */
if (WITH_SYNCH) {
cc2420Data[pos++] = 0x7A;
}
/* 1 byte length */
len = (byte) packetData.length;
if (WITH_XMAC) {
len += 6;
}
if (WITH_CHECKSUM) {
len += 2;
}
if (WITH_TIMESTAMP) {
len += 3;
}
if (WITH_FOOTER) {
len += 2;
}
cc2420Data[pos++] = len;
cc2420CRC.setCRC(0);
/* 4 byte X-MAC: not implemented */
if (WITH_XMAC) {
cc2420Data[pos++] = 1; /* TYPE_DATA */
cc2420CRC.addBitrev(1);
contikiCRC = CRCCoder.crc16Add((byte)0, contikiCRC);
cc2420Data[pos++] = 0;
cc2420CRC.addBitrev(0);
contikiCRC = CRCCoder.crc16Add((byte)0, contikiCRC);
cc2420Data[pos++] = 0; /* XXX sender: 0.0 */
cc2420CRC.addBitrev(0);
contikiCRC = CRCCoder.crc16Add((byte)0, contikiCRC);
cc2420Data[pos++] = 0;
cc2420CRC.addBitrev(0);
contikiCRC = CRCCoder.crc16Add((byte)0, contikiCRC);
cc2420Data[pos++] = 0; /* XXX receiver: 0.0 */
cc2420CRC.addBitrev(0);
contikiCRC = CRCCoder.crc16Add((byte)0, contikiCRC);
cc2420Data[pos++] = 0;
cc2420CRC.addBitrev(0);
contikiCRC = CRCCoder.crc16Add((byte)0, contikiCRC);
}
/* Payload */
for (byte b : packetData) {
contikiCRC = CRCCoder.crc16Add(b, contikiCRC);
cc2420CRC.addBitrev(b & 0xFF);
}
System.arraycopy(packetData, 0, cc2420Data, pos, packetData.length);
pos += packetData.length;
/* 2 bytes checksum */
if (WITH_CHECKSUM) {
cc2420Data[pos++] = (byte) (contikiCRC & 0xff);
cc2420CRC.addBitrev(contikiCRC & 0xFF);
cc2420Data[pos++] = (byte) ((contikiCRC >> 8) & 0xff);
cc2420CRC.addBitrev((contikiCRC >> 8) & 0xFF);
}
/* (TODO) 3 bytes timestamp */
if (WITH_TIMESTAMP) {
cc2420Data[pos++] = 0;
cc2420CRC.addBitrev(0);
cc2420Data[pos++] = 0;
cc2420CRC.addBitrev(0);
cc2420Data[pos++] = 0;
cc2420CRC.addBitrev(0);
}
/* 2 bytes footer: CC2420's CRC */
if (WITH_FOOTER) {
cc2420Data[pos++] = (byte) cc2420CRC.getCRCHi();
cc2420Data[pos++] = (byte) cc2420CRC.getCRCLow();
}
byte cc2420DataStripped[] = new byte[pos];
System.arraycopy(cc2420Data, 0, cc2420DataStripped, 0, pos);