mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-17 02:52:12 +01:00
Fix the CRC calculation for java sending
This commit is contained in:
parent
e12cef6b4b
commit
03b9bbbb8a
@ -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;
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user