From 1ea90c415879a2ea123af97d0d14b7137aa5a243 Mon Sep 17 00:00:00 2001 From: James Cotton Date: Thu, 10 Mar 2011 14:36:37 -0600 Subject: [PATCH] Fix the CRC calculation for java sending --- .../src/org/openpilot/uavtalk/UAVTalk.java | 13 ++++--- .../tests/org/openpilot/uavtalk/TalkTest.java | 34 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/androidgcs/src/org/openpilot/uavtalk/UAVTalk.java b/androidgcs/src/org/openpilot/uavtalk/UAVTalk.java index 92b5585de..d656aa707 100644 --- a/androidgcs/src/org/openpilot/uavtalk/UAVTalk.java +++ b/androidgcs/src/org/openpilot/uavtalk/UAVTalk.java @@ -175,7 +175,12 @@ public class UAVTalk extends Observable{ e.printStackTrace(); break; } - //System.out.println("Received byte " + val + " in state + " + rxState); + if(val == -1) { + System.out.println("End of stream, terminating processInputStream thread"); + break; + } + + System.out.println("Received byte " + val + " in state + " + rxState); processInputByte(val); } } @@ -760,7 +765,7 @@ public class UAVTalk extends Observable{ } // Calculate checksum - bbuf.put((byte) (updateCRC(0, bbuf.array()) & 0xff)); + bbuf.put((byte) (updateCRC(0, bbuf.array(), bbuf.position()) & 0xff)); try { int packlen = bbuf.position(); @@ -818,9 +823,9 @@ public class UAVTalk extends Observable{ { return crc_table[crc ^ (data & 0xff)]; } - int updateCRC(int crc, byte [] data) + int updateCRC(int crc, byte [] data, int length) { - for (int i = 0; i < data.length; i++) + for (int i = 0; i < length; i++) crc = updateCRC(crc, (int) data[i]); return crc; } diff --git a/androidgcs/tests/org/openpilot/uavtalk/TalkTest.java b/androidgcs/tests/org/openpilot/uavtalk/TalkTest.java index 044ddfd06..91f3aa198 100644 --- a/androidgcs/tests/org/openpilot/uavtalk/TalkTest.java +++ b/androidgcs/tests/org/openpilot/uavtalk/TalkTest.java @@ -28,6 +28,16 @@ public class TalkTest { static final int PORT_NUM = 7777; boolean succeed = false; + byte[] flightStatsConnected = + {0x3c,0x20,0x1d,0x00, + (byte) 0x5e,(byte) 0x26,(byte) 0x0c,(byte) 0x66, + 0x03,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00, + 0x00,(byte) 0xAE}; + @BeforeClass public static void setUpBeforeClass() throws Exception { objMngr = new UAVObjectManager(); @@ -89,40 +99,40 @@ public class TalkTest { obj.getField("Status").setValue("Connected"); talk.sendObject(obj, false, false); - + System.out.println("Size: " + os.size()); byte [] array = os.toByteArray(); for(int i = 0; i < array.length; i++) { System.out.print("0x" + Integer.toHexString((int) array[i] & 0xff)); + System.out.print("/0x" + Integer.toHexString((int) flightStatsConnected[i] & 0xff)); if(i != array.length-1) - System.out.print(", "); + System.out.print("\n"); } System.out.print("\n"); + for(int i = 0; i < array.length; i++) + assertEquals(os.toByteArray()[i], flightStatsConnected[i]); } @Test - public void testReceiveObject() { - ByteArrayInputStream is = new ByteArrayInputStream(new byte[0], 0, 0); + public void testReceiveObject() throws InterruptedException { + ByteArrayInputStream is = new ByteArrayInputStream(flightStatsConnected, 0, flightStatsConnected.length); ByteArrayOutputStream os = new ByteArrayOutputStream(100); - // Send object to create the test packet (should hard code in test string) - UAVTalk talk = new UAVTalk(is,os,objMngr); + // Make the Status wrong initially UAVObject obj = objMngr.getObject("FlightTelemetryStats"); - obj.getField("Status").setValue("Connected"); - talk.sendObject(obj, false, false); - obj.getField("Status").setValue("Disconnected"); // Test receiving from that stream - is = new ByteArrayInputStream(os.toByteArray(), 0, os.size()); - talk = new UAVTalk(is,os,objMngr); + UAVTalk talk = new UAVTalk(is,os,objMngr); Thread inputStream = talk.getInputProcessThread(); inputStream.start(); + Thread.sleep(1000); + System.out.println("Should be FlightTelemetry Stats:"); System.out.println(objMngr.getObject("FlightTelemetryStats").toString()); - fail("Not working yet"); + assertEquals(obj.getField("Status").getValue(), new String("Connected")); }