Changed packet making system

js-code-approach
neiviv-ui 1 year ago
parent c9da87a7fb
commit b74b61a396
  1. 185
      internal.js

@ -8,7 +8,6 @@ var p7 = {
decode: {},
commands: {},
sendBuffer : new Uint8Array(1032),
receiveBuffer : new Uint8Array(1032 + 1)
};
@ -23,7 +22,7 @@ var p7 = {
/* Sum individual bytes
* NOT + 1, as defined in fxReverse documentation.
* Be sure it's under 256! */
Uint8Array.prototype.p7Checksum = function () {
Array.prototype.p7Checksum = Uint8Array.prototype.p7Checksum = function () {
return (((~(this.reduce((accumulator, currentValue) => accumulator + currentValue))) + 1) & 0xFF)
}
@ -39,98 +38,54 @@ var p7 = {
}
/* Convert an ascii array into a string */
Uint8Array.prototype.asciiToString = function () {
Array.prototype.asciiToString = Uint8Array.prototype.asciiToString = function () {
let string = [];
this.forEach((element) => string.push(String.fromCharCode(element)));
return string.join('');
}
/* Convert an ascii array representing an hex number into a number */
Uint8Array.prototype.asciiToHex = function () {
Array.prototype.asciiToHex = Uint8Array.prototype.asciiToHex = function () {
return Number('0x' + this.asciiToString());
}
/* ---------------------Packet making--------------------- */
/* Create a basic (Non-Extended) packet and push it to p7.sendBuffer */
/* return it's length (6) */
p7.make.basicPacket = (type, subtype) => {
//Set the Type (T), Subtype (ST) and Extended (EX) fields
p7.sendBuffer.set([type]
.concat(subtype.hexToAscii(2))
.concat([0x30])
, 0);
//Set the Checksum (CS) field
p7.sendBuffer.set(p7.sendBuffer.slice(1, 4).p7Checksum().hexToAscii(2), 4);
//Return the packet length
return 6;
}
/* Fill the Data (D) field of a command packet in p7.sendBuffer */
/* return the Data (D) field length */
p7.make.commandPacketDataField = function (datatype, fileSize, commandArguments) {
//Set Overwrite (OW), Data type (DT) and File size (FS) fields
p7.sendBuffer.set([0x30, 0x30, /* Overwrite */
0x30, 0x30] /* Data type field ?? */
.concat(fileSize.hexToAscii(8)) /* File size field */
, 8);
let index = 18;
//Set Size of Data {1-6} (SD{1-6}) fields
commandArguments.forEach(element => p7.sendBuffer.set(element.length.hexToAscii(2), (index += 2)));
//Set Data {1-6} (D{1-6}) fields
let data = commandArguments.join('');
p7.sendBuffer.set(data.toAscii(), 32);
return data.length + 24; /* Overwrite (OW), Data type (DT), File size (FS) and Data size (DS) subfields of the Data (D) field of the packet */;
}
/* Create an extended command packet and push it to p7.sendBuffer */
/* return it's length */
p7.make.extendedCommandPacket = function (subtype, datatype, fileSize, commandArguments) {
let dataSize = p7.make.commandPacketDataField(datatype, fileSize, commandArguments); /* Data field */
//Add the checksum to a packet
p7.make.checksum = (packet) => packet.concat(packet.slice(1, packet.length).p7Checksum().hexToAscii(2));
/* Create a basic (Non-Extended) packet */
/* Return the packet */
p7.make.basicPacket = (type, subtype) => p7.make.checksum([type] //Type (T)
.concat(subtype.hexToAscii(2)) //Subtype (ST)
.concat([0x30])); //Extended (EX)
/* Create an extended command packet */
/* Return the packet */
p7.make.extendedCommandPacket = (subtype, datatype, fileSize, commandArguments) => {
let data = [0x30, 0x30] //Overwrite (OW)
.concat([0x30, 0x30]) //Data type (DT) ??
.concat(fileSize.hexToAscii(8)) //File size (FS)
.concat(commandArguments.flatMap((element) => element.length.hexToAscii(2))) //Size of Data {1..6} (SD{1..6})
.concat(commandArguments.join('').toAscii()); //Data {1..6} (D{1..6})
//Set Type (T), Subtype (ST), Extended (EX) and Data size (DT) fields
p7.sendBuffer.set([0x01] /* Type field */
.concat(subtype.hexToAscii(2)) /* Subtype field */
.concat([0x31]) /* Extended field */
.concat(dataSize.hexToAscii(4)) /* Data size field */
, 0);
//Set Checksum (CS) field
p7.sendBuffer.set(p7.sendBuffer.slice(1, dataSize + 8).p7Checksum().hexToAscii(2), dataSize + 8);
//Return it's length
return dataSize + 10 /* Type (T), Subtype (ST), Extended (EX), Data size (DT) and Checksum (CS) fields */;
return p7.make.checksum([0x01] //Type (T)
.concat(subtype.hexToAscii(2)) //Subtype (ST)
.concat([0x31]) //Extended (EX)
.concat(data.length.hexToAscii(4)) //Data size (DS)
.concat(data)); //Data (D)
}
/* Create a data packet and push it to p7.sendBuffer */
/* return it's length */
p7.make.dataPacket = function (subtype, totalNumber, currentNumber, data) {
/* Set Type (T), Subtype (ST), Extended (EX), Data size (DS),
* Data (D) fields and Data (D) subfields :
* Total Number (TN), Current Number (CN), and Data (DD) */
p7.sendBuffer.set([0x02] /* Type field */
.concat(subtype.hexToAscii(2)) /* Subtype (ST) field */
.concat([0x31]) /* Extended (EX) field */
.concat( /* Data size (DS) field */
(data.length + 8 /* Total number (TN) and Current number (CN) subfields */ ).hexToAscii(4))
.concat(totalNumber.hexToAscii(4)) /* Total number (TN) subfield */
.concat(currentNumber.hexToAscii(4)) /* Current number (CN) subfield */
.concat(data) /* Data (DD) subfield */
, 0);
//Set Checksum (CS) field
p7.sendBuffer.set(p7.sendBuffer.slice(1, data.length + 16).p7Checksum().hexToAscii(2), data.length + 16);
//Return it's length
return data.length + 18 /* All other fields and subfields */;
}
/* Create a data packet */
/* Return the packet */
p7.make.dataPacket = (subtype, totalNumber, currentNumber, data) =>
p7.make.checksum([0x02] //Type field
.concat(subtype.hexToAscii(2)) //Subtype (ST) field
.concat([0x31]) //Extended (EX) field
.concat((data.length + 8 /* Total number (TN) and Current number (CN) subfields */ ).hexToAscii(4)) //Data size (DS) field
.concat(totalNumber.hexToAscii(4)) //Total number (TN) subfield
.concat(currentNumber.hexToAscii(4)) //Current number (CN) subfield
.concat(data)); //Data (DD) subfield
/* ---------------------Packet decoding--------------------- */
@ -198,52 +153,47 @@ var p7 = {
p7.receive.packet = function () {
console.log("%cReceiving...", "color: orange");
var packetInfo = {}
var transfered = 0;
var packetInfo = {};
var transfered = 0; //Already transfered bytes
return p7.device.transferIn(2, 64)
.then(function receive(transferInResult) {
if (!transferInResult.data.byteLength) {
console.log('The buffer was empty, trying again!');
return p7.device.transferIn(2, 64).then(receive);
}
if (!transferInResult.data.byteLength)
return p7.device.transferIn(2, 64).then(receive);
p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), 0);
p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), 0);
packetInfo.length = 6; //Type (T), Subtype (S), Extended (EX) and Checksum (CS) fields
packetInfo.length = 6; //Type (T), Subtype (S), Extended (EX) and Checksum (CS) fields
//Set Type (T) field
packetInfo.type = p7.receiveBuffer[0];
//Set Type (T) field
packetInfo.type = p7.receiveBuffer[0];
//Set Subtype (ST) field
packetInfo.subtype = p7.receiveBuffer.slice(1, 2).asciiToHex();
//Set Subtype (ST) field
packetInfo.subtype = p7.receiveBuffer.slice(1, 2).asciiToHex();
//Set Extended (EX) field
packetInfo.extended = (p7.receiveBuffer[3] === 0x31);
}).then(() => {
if (packetInfo.extended) {
console.log('Packet is extended, receiving...');
//Set Data size (DS) field
packetInfo.dataSize = p7.receiveBuffer.slice(4, 8).asciiToHex();
//Set Extended (EX) field
if ((packetInfo.extended = (p7.receiveBuffer[3] === 0x31))) {
//Set Data size (DS) field
packetInfo.dataSize = p7.receiveBuffer.slice(4, 8).asciiToHex();
packetInfo.length += packetInfo.dataSize + 4; //Data size field
packetInfo.length += packetInfo.dataSize + 4; //Data size field
var transfered = 64;
if ((transfered += 64) < packetInfo.length)
return p7.device.transferIn(2, 64)
.then(function receiveRemainingPackets(transferInResult) {
p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), transfered);
if ((transfered += 64) < packetInfo.length)
return p7.device.transferIn(2, 64).then(receiveRemainingPackets);
})
});
}
}).then(() => {
//Compute checksum
let checksumError = ((packetInfo.checksum = p7.receiveBuffer.slice(packetInfo.length - 2, packetInfo.length).asciiToHex()) !== p7.receiveBuffer.slice(1, packetInfo.length - 2).p7Checksum());
/* TODO: handle checksum errors */
console.log(checksumError);
return packetInfo;
}).catch((err) => {
err.message = "Couldn't receive the " + transfered + " to " + (transfered + 64) + " bytes of the packet: " + err.message;
@ -254,28 +204,12 @@ var p7 = {
/* ---------------------Packet sending--------------------- */
p7.send.packet = async function (length) {
let responsePacketInfo = undefined;
let tries = 0;
p7.send.packet = async function (buffer) {
console.log("%cSending packet...", "color: green");
try {
await p7.device.transferOut(1, p7.sendBuffer.slice(0, length));
} catch (err) {
err.message = "Couldn't send the packet: " + err.message;
throw err;
}
try {
responsePacketInfo = await p7.receive.packet();
} catch (err) {
err.message = "Couldn't receive the response packet: " + err.message;
throw err;
}
return responsePacketInfo;
return p7.device.transferOut(1, Uint8Array.from(buffer))
.then(() => p7.receive.packet());
}
p7.send.basicPacket = async (type, subtype) => await p7.send.packet(p7.make.basicPacket(type, subtype));
p7.send.extendedCommandPacket = async (subtype, datatype, fileSize, commandArguments) => await p7.send.packet(p7.make.extendedCommandPacket(subtype, datatype, fileSize, commandArguments));
@ -457,6 +391,7 @@ var p7 = {
document.getElementById('connect').addEventListener('click',
async function () {
var time = performance.now();
try {
await p7.init();
@ -474,8 +409,6 @@ var p7 = {
console.log(await p7.commands.list('fls0'));
console.log(await p7.commands.createDirectory('test', 'fls0'));
console.log(await p7.commands.list('fls0'));
console.log(performance.now() - time);
});
})();
Loading…
Cancel
Save