Handle checksum errors and resend requests

This commit is contained in:
neiviv-ui 2021-07-21 17:13:22 +02:00
parent 5a55cbac2d
commit bbe2db66f4
3 changed files with 62 additions and 48 deletions

View File

@ -22,5 +22,5 @@ The buttons are actually diconnected, if you want to test, dive through the code
# Thanks to :
Simon Lothar for his fx-reverse doc
Cakeisalie5 for his p7 command line tool
- Simon Lothar for his fx-reverse doc
- Cakeisalie5 for his p7 command line tool

View File

@ -1,14 +1 @@
'use strict'
/*function test(){
throw 'geswg';
console.log('err');
}
try {
test();
console.log('err');
} catch(err) {
console.error(err);
}*/
'use strict'

View File

@ -9,7 +9,11 @@ var p7 = {
};
(function() {
/* ---------------------Global variables--------------------- */
var checksumTries = 0;
/* ---------------------Utilitaries--------------------- */
/* Sum individual bytes
@ -157,64 +161,85 @@ var p7 = {
p7.receive.packet = async function () {
console.log("%cReceiving...", "color: orange");
let checksumError = 0;
let transferInResult = undefined;
let transferInResult = undefined;
try {
while (! (transferInResult = await p7.device.transferIn(2, 64)).data.byteLength)
console.log('The buffer was empty, trying again!');
p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), 0);
} catch (err) {
throw new Error("Couldn't receive the first 64 bytes of the packet: " + err.message);
}
let packetInfo = {};
let packetInfo = {}
packetInfo.length = 6; /* Type (T), Subtype (S), Extended (EX) and Checksum (CS) fields */
//Set Type (T) field
packetInfo.type = p7.receiveBuffer[0];
//Set Subtype (ST) field
packetInfo.subtype = p7.receiveBuffer.slice(1, 2).asciiToHex();
//Set Extended (EX) field
packetInfo.extended = (p7.receiveBuffer[3] === 0x31);
if (packetInfo.extended) {
//Set Data size (DS) field
packetInfo.dataSize = p7.receiveBuffer.slice(4, 8).asciiToHex();
packetInfo.length += packetInfo.dataSize + 4 /* Data size field */;
let transfered = 0;
while (transfered < packetInfo.length) {
for (let transfered = 64 ; transfered < packetInfo.length ; transfered += 64) {
try {
transferInResult = await p7.device.transferIn(2, 64);
} catch (err) {
throw new Error("Couldn't receive the " + transfered + "th and following 64 bytes of the packet: " + err.message)
}
p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), transfered += 64);
p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), transfered);
}
}
/* Compute checksum */
checksumError = ((packetInfo.checksum = p7.receiveBuffer.slice(packetInfo.length - 2, packetInfo.length).asciiToHex()) !== p7.receiveBuffer.slice(1, packetInfo.length - 2).p7Checksum());
console.log(checksumError);
/* Compute checksum */
let checksumError = ((packetInfo.checksum = p7.receiveBuffer.slice(packetInfo.length - 2, packetInfo.length).asciiToHex()) !== p7.receiveBuffer.slice(1, packetInfo.length - 2).p7Checksum());
if (checksumError && (checksumTries++) < 3) {
console.log('there was a checksum error');
try {
packetInfo = await p7.send.packet(p7.make.basicPacket(p7PacketType.error, errorSubtype.resendRequest));
} catch (err) {
throw new Error("Couldn't send the resend request: " + err.message);
}
}
if (checksumTries === 4)
throw new Error("There were three checksum in a row, the calculator or the cable may be broken!");
return packetInfo;
}
/* ---------------------Packet sending--------------------- */
/* Doesn't work for now on */
p7.send.packet = async function (length) {
try {
await p7.device.transferOut(1, p7.sendBuffer.slice(0, length));
} catch (err) {
throw new Error("Couldn't send the packet: " + err.message);
let responsePacketInfo = undefined;
let tries = 0;
console.log("%cSending packet...", "color: green");
for ( ; tries === 0 || (responsePacketInfo.type === p7PacketType.error && responsePacketInfo.subtype === errorSubtype.resendRequest && tries < 3) ; tries++) {
try {
await p7.device.transferOut(1, p7.sendBuffer.slice(0, length));
} catch (err) {
throw new Error("Couldn't send the packet: " + err.message);
}
try {
responsePacketInfo = await p7.receive.packet();
} catch (err) {
throw new Error("Couldn't receive the response packet: " + err.message);
}
}
if (tries === 3)
throw new Error("Received three resend request in a row, abandon");
return responsePacketInfo;
};
/* ---------------------Initialization and exiting--------------------- */
@ -362,16 +387,18 @@ var p7 = {
try {
await p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.initialization));
await p7.receive.packet();
} catch (err) {
console.log(err);
console.error(err);
return err;
}
await p7.send.packet(p7.make.basicPacket(p7PacketType.command, sysCommandSubtype.getDeviceInfo));
await p7.receive.packet();
try {
await p7.send.packet(p7.make.basicPacket(p7PacketType.command, sysCommandSubtype.getDeviceInfo));
} catch (err) {
console.error(err);
return err;
}
console.log(p7.decode.extendedAckPacket());