p7.receive.packet() function simplified

js-code-approach
neiviv-ui 1 year ago
parent 47f43a456f
commit 5a55cbac2d
  1. 282
      internal.js

@ -43,7 +43,7 @@ var p7 = {
}
p7.sendBuffer = new Uint8Array(1032);
p7.receiveBuffer = undefined; //new Uint8Array(1032 + 1);
p7.receiveBuffer = new Uint8Array(1032 + 1);
/* ---------------------Packet making--------------------- */
@ -132,21 +132,21 @@ var p7 = {
/* Return all the informations of the device, and trim string's overage (255) */
p7.decode.extendedAckPacket = () => {
return {
hardwareIdentifier : p7ReceiveBuffer.slice(8, 16).asciiToString(),
processorIdentifier : p7ReceiveBuffer.slice(16, 32).asciiToString(),
preprogrammedROMCapacity : p7ReceiveBuffer.slice(32, 40).asciiToString(),
flashROMCapacity : p7ReceiveBuffer.slice(40, 48).asciiToString(),
ramCapacity : p7ReceiveBuffer.slice(48, 56).asciiToString(),
prepogrammedROMVersion : p7ReceiveBuffer.slice(56, 72).filter(number => number !== 255).asciiToString(),
bootCodeVersion : p7ReceiveBuffer.slice(72, 88).filter(number => number !== 255).asciiToString(),
bootCodeOffset : p7ReceiveBuffer.slice(88, 96).filter(number => number !== 255).asciiToString(),
bootCodeSize : p7ReceiveBuffer.slice(96, 104).filter(number => number !== 255).asciiToString(),
osCodeVersion : p7ReceiveBuffer.slice(104, 120).filter(number => number !== 255).asciiToString(),
osCodeOffset : p7ReceiveBuffer.slice(120, 128).asciiToString(),
osCodeSize : p7ReceiveBuffer.slice(128, 136).asciiToString(),
protocolVersion : p7ReceiveBuffer.slice(136, 140).asciiToString(),
productID : p7ReceiveBuffer.slice(140, 156).filter(number => number !== 255).asciiToString(),
username : p7ReceiveBuffer.slice(156, 172).filter(number => number !== 255).asciiToString()
hardwareIdentifier : p7.receiveBuffer.slice(8, 16).asciiToString(),
processorIdentifier : p7.receiveBuffer.slice(16, 32).asciiToString(),
preprogrammedROMCapacity : p7.receiveBuffer.slice(32, 40).asciiToString(),
flashROMCapacity : p7.receiveBuffer.slice(40, 48).asciiToString(),
ramCapacity : p7.receiveBuffer.slice(48, 56).asciiToString(),
prepogrammedROMVersion : p7.receiveBuffer.slice(56, 72).filter(number => number !== 255).asciiToString(),
bootCodeVersion : p7.receiveBuffer.slice(72, 88).filter(number => number !== 255).asciiToString(),
bootCodeOffset : p7.receiveBuffer.slice(88, 96).filter(number => number !== 255).asciiToString(),
bootCodeSize : p7.receiveBuffer.slice(96, 104).filter(number => number !== 255).asciiToString(),
osCodeVersion : p7.receiveBuffer.slice(104, 120).filter(number => number !== 255).asciiToString(),
osCodeOffset : p7.receiveBuffer.slice(120, 128).asciiToString(),
osCodeSize : p7.receiveBuffer.slice(128, 136).asciiToString(),
protocolVersion : p7.receiveBuffer.slice(136, 140).asciiToString(),
productID : p7.receiveBuffer.slice(140, 156).filter(number => number !== 255).asciiToString(),
username : p7.receiveBuffer.slice(156, 172).filter(number => number !== 255).asciiToString()
}
};
@ -156,17 +156,18 @@ var p7 = {
/* ---------------------Packet receiving--------------------- */
p7.receive.packet = async function () {
console.log("%cReceiving...", "color: orange");
let checksumError = 0;
let transferInResult = undefined;
try {
let transferInResult = undefined;
while (! (transferInResult = await p7.device.transferIn(2, 64)).data.byteLength)
console.log('The buffer was empty, trying again!');
p7.receiveBuffer = new Uint8Array(transferInResult.data.buffer);
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);
}
console.log(p7.receiveBuffer);
let packetInfo = {};
packetInfo.length = 6; /* Type (T), Subtype (S), Extended (EX) and Checksum (CS) fields */
@ -180,18 +181,29 @@ var p7 = {
//Set Extended (EX) field
packetInfo.extended = (p7.receiveBuffer[3] === 0x31);
if (!packetInfo.extended) {
/* Compute checksum */
checksumError = ((packetInfo.checksum = p7.receiveBuffer.slice(4, 5).asciiToHex) !== p7.receiveBuffer.slice(1, 4).checksum());
} else {
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) {
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);
}
}
console.log(packetInfo);
/* 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);
}
/* ---------------------Packet sending--------------------- */
@ -228,6 +240,106 @@ var p7 = {
console.log(p7.device);
/* Receive and send some usb control message, as defined in fxReverse documentation
*
* Adapted version (some parameters changed since fxReverse's writing) of usb_control_msg() function's protoype
* from linux-manual : https://manpages.debian.org/jessie-backports/linux-manual-4.8/usb_control_msg.9.en.html
*
* int usb_control_msg(struct usb_device * dev,
* uint8_t requesttype,
* uint8_t request,
* uint16_t value,
* uint16_t index,
* void * data,
* uint16_t size,
* int timeout);
*
* fxReverse2:13:
*
* int init_connection() {
* char *buffer = calloc(0x29, sizeof(char));
*
* //Receive 0x12 bytes of data
* usb_control_message(usb_handle,
* 0x80, = 0b10000000
* - D7 = 1 -> Data transfer direction: Device-to-host
* - D6...D5 = 0 -> Type: Standard
* - D4...D0 = 0 -> Recipient: Device
* 0x6,
* 0x0100,
* 0x0,
* buffer,
* 0x12,
* 200);
*
* //Same arguments (except timeout), but value is 0x200 and it receives 0x29 bytes of data
* usb_control_message(usb_handle, 0x80, 0x6, 0x200, 0, buffer, 0x29, 250);
*
*
* usb_control_message(usb_handle,
* 0x41, = 0b01000001
* - D7 = 0 -> Data transfer direction: Host-to-device
* - D6...D5 = 2 -> Type: Vendor
* - D4...D0 = 1 -> Interface: Interface
* 0x1,
* 0x0,
* 0,
* buffer,
* 0x0,
* 250);
*
* free(buffer);
* return 0;
* }
****************************************************************
let transferInResult = undefined;
let controlData = undefined;
try {
transferInResult = await p7.device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: 0x06, // GET_DESCRIPTOR
value: 0x0100, // Descriptor Type and Descriptor Index
index: 0x0000
}, 0x12); // Length
} catch (err) {
console.log(err);
}
controlData = new Uint8Array(transferInResult.data.buffer);
console.log('vendor id : 0x' + controlData[9].toString(16).padStart(2, 0) + controlData[8].toString(16).padStart(2,0));
console.log('product id : 0x' + controlData[11].toString(16).padStart(2, 0) + controlData[10].toString(16).padStart(2,0));
try {
transferInResult = await p7.device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: 0x06, // GET_DESCRIPTOR
value: 0x0200, // Descriptor Type and Descriptor Index
index: 0x0000
}, 0x29); // Length
} catch (err) {
console.log(err);
}
controlData = new Uint8Array(transferInResult.data.buffer);
console.log(controlData);
try {
await p7.device.controlTransferOut({
requestType: 'vendor',
recipient: 'interface',
request: 0x01,
value: 0x0000,
index: 0x0000
}, controlData);
} catch (err) {
console.log(err);
} */
/* Everything went right */
return 0;
}
@ -248,121 +360,21 @@ var p7 = {
return err;
}
/* Receive and send some usb control message, as defined in fxReverse documentation
*
* Adapted version (some parameters changed since fxReverse's writing) of usb_control_msg() function's protoype
* from linux-manual : https://manpages.debian.org/jessie-backports/linux-manual-4.8/usb_control_msg.9.en.html
*
* int usb_control_msg(struct usb_device * dev,
* uint8_t requesttype,
* uint8_t request,
* uint16_t value,
* uint16_t index,
* void * data,
* uint16_t size,
* int timeout);
*
* fxReverse2:13:
*
* int init_connection() {
* char *buffer = calloc(0x29, sizeof(char));
*
* //Receive 0x12 bytes of data
* usb_control_message(usb_handle,
* 0x80, = 0b10000000
* - D7 = 1 -> Data transfer direction: Device-to-host
* - D6...D5 = 0 -> Type: Standard
* - D4...D0 = 0 -> Recipient: Device
* 0x6,
* 0x0100,
* 0x0,
* buffer,
* 0x12,
* 200);
*
* //Same arguments (except timeout), but value is 0x200 and it receives 0x29 bytes of data
* usb_control_message(usb_handle, 0x80, 0x6, 0x200, 0, buffer, 0x29, 250);
*
*
* usb_control_message(usb_handle,
* 0x41, = 0b01000001
* - D7 = 0 -> Data transfer direction: Host-to-device
* - D6...D5 = 2 -> Type: Vendor
* - D4...D0 = 1 -> Interface: Interface
* 0x1,
* 0x0,
* 0,
* buffer,
* 0x0,
* 250);
*
* free(buffer);
* return 0;
* }
****************************************************************
let transferInResult = undefined;
let controlData = undefined;
try {
transferInResult = await p7.device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: 0x06, // GET_DESCRIPTOR
value: 0x0100, // Descriptor Type and Descriptor Index
index: 0x0000
}, 0x12); // Length
} catch (err) {
console.log(err);
}
try {
await p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.initialization));
await p7.receive.packet();
} catch (err) {
console.log(err);
}
controlData = new Uint8Array(transferInResult.data.buffer);
console.log('vendor id : 0x' + controlData[9].toString(16).padStart(2, 0) + controlData[8].toString(16).padStart(2,0));
console.log('product id : 0x' + controlData[11].toString(16).padStart(2, 0) + controlData[10].toString(16).padStart(2,0));
try {
transferInResult = await p7.device.controlTransferIn({
requestType: 'standard',
recipient: 'device',
request: 0x06, // GET_DESCRIPTOR
value: 0x0200, // Descriptor Type and Descriptor Index
index: 0x0000
}, 0x29); // Length
} catch (err) {
console.log(err);
}
controlData = new Uint8Array(transferInResult.data.buffer);
console.log(controlData);
try {
await p7.device.controlTransferOut({
requestType: 'vendor',
recipient: 'interface',
request: 0x01,
value: 0x0000,
index: 0x0000
}, controlData);
} catch (err) {
console.log(err);
} */
try {
await p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.initialization));
await p7.receive.packet();
} catch (err) {
console.log(err);
}
await p7.send.packet(p7.make.basicPacket(p7PacketType.command, sysCommandSubtype.getDeviceInfo));
await p7.receive.packet();
await p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.default));
console.log(p7.decode.extendedAckPacket());
await p7.receive.packet();
}
);
});
})();
Loading…
Cancel
Save