"use strict"; var p7 = { device: {}, make: {}, send: {}, receive: {}, decode: {}, commands: {}, receiveBuffer : new Uint8Array(1032 + 1) }; (function() { /* ---------------------Utilitaries--------------------- */ /* Sum individual bytes * NOT + 1, as defined in fxReverse documentation. * Be sure it's under 256! */ Array.prototype.p7Checksum = Uint8Array.prototype.p7Checksum = function () { return (((~(this.reduce((accumulator, currentValue) => accumulator + currentValue))) + 1) & 0xFF) } /* Convert a string into an array of ascii numbers */ String.prototype.toAscii = function () { return this.split('').map((char) => char.charCodeAt(0)) } /* Convert an hex number into an array of ascii values * Use the uppercase representation of the number */ Number.prototype.hexToAscii = function (padding) { return this.toString(16).toUpperCase().padStart(padding, 0).toAscii(); } /* Convert an ascii array into a string */ 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 */ Array.prototype.asciiToHex = Uint8Array.prototype.asciiToHex = function () { return Number('0x' + this.asciiToString()); } /* ---------------------Packet making--------------------- */ //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) ...subtype.hexToAscii(2), //Subtype (ST) 0x30]); //Extended (EX) /* Create an extended command packet */ /* Return the packet */ p7.make.extendedCommandPacket = (subtype, datatype, fileSize, commandArguments) => { let data = [0x30, 0x30, //Overwrite (OW) ...[0x30, 0x30], //Data type (DT) ?? ...fileSize.hexToAscii(8), //File size (FS) ...commandArguments.flatMap((element) => element.length.hexToAscii(2)), //Size of Data {1..6} (SD{1..6}) ...commandArguments.join('').toAscii()]; //Data {1..6} (D{1..6}) return p7.make.checksum([0x01, //Type (T) ...subtype.hexToAscii(2), //Subtype (ST) 0x31, //Extended (EX) ...data.length.hexToAscii(4), //Data size (DS) ...data]); //Data (D) } /* Create a data packet */ /* Return the packet */ p7.make.dataPacket = (subtype, totalNumber, currentNumber, data) => p7.make.checksum([0x02, //Type field ...subtype.hexToAscii(2), //Subtype (ST) field 0x31, //Extended (EX) field ...(data.length + 8).hexToAscii(4), //Data size (DS) field ...totalNumber.hexToAscii(4), //Total number (TN) subfield ...currentNumber.hexToAscii(4), //Current number (CN) subfield ...data]); //Data (DD) subfield /* ---------------------Packet decoding--------------------- */ /* Return all the informations of the device, and trim string's overage (255) */ p7.decode.extendedAckPacket = () => { return { 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() } }; /* Return the value (as an number) of the Overwrite (OW) subfield of the Data (D) field of an extended command packet */ p7.decode.commandPacketOverWrite = () => p7.receiveBuffer.slice(8, 10).asciiToHex(); /* Return the value (as an number) of the Data type (DT) subfield of the Data (D) field of an extended command packet */ p7.decode.commandPacketDataType = () => p7.receiveBuffer.slice(10, 12).asciiToHex(); /* Return the value (as an number) of the FileSize (FS) subfield of the Data (D) field of an extended command packet */ p7.decode.commandPacketFileSize = () => p7.receiveBuffer.slice(12, 20).asciiToHex(); /* Return the value (as a string) of the requested Data (D) subfield of the Data (D) field of an extended command packet */ p7.decode.commandPacketDataField = function (field) { let fieldSize = []; /* Decode the length of Data {1 - ${length}} (D{1 - ${length}}) subfields */ for (let index = 20, i = 0; i < field ; i++) fieldSize.push(p7.receiveBuffer.slice(index, (index += 2)).asciiToHex()); /* Get the requested field size */ let fieldLength = fieldSize.pop(); /* Get the index of the requested field */ let fieldIndex = (fieldSize.length) ? fieldSize.reduce((accumulator, currentValue) => accumulator + currentValue) + 32 : 32; /* Return the ascii array as a string */ return p7.receiveBuffer.slice(fieldIndex, fieldIndex + fieldLength).asciiToString(); } p7.decode.commandPacketAllDataFields = function () { let dataFieldSize = []; let index = 20 while(index < 32) dataFieldSize.push(p7.receiveBuffer.subarray(index, (index += 2)).asciiToHex()); return dataFieldSize.map((element) => p7.receiveBuffer.slice(index, (index += element)).asciiToString()); } /* ---------------------Packet receiving--------------------- */ p7.receive.packet = function () { console.log("%cReceiving...", "color: orange"); var packetInfo = {}; var transfered = 0; //Already transfered bytes return p7.device.transferIn(2, 64) .then(function receive(transferInResult) { if (!transferInResult.data.byteLength) return p7.device.transferIn(2, 64).then(receive); p7.receiveBuffer.set(new Uint8Array(transferInResult.data.buffer), 0); 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 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 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; throw err; }); } /* ---------------------Packet sending--------------------- */ p7.send.packet = function (buffer) { console.log("%cSending packet...", "color: green"); return p7.device.transferOut(1, Uint8Array.from(buffer)) .catch((err) => { err.message = "Couldn't send the packet: " + err.message; throw err; }) .then(() => p7.receive.packet()) .catch((err) => { err.message = "Couldn't receive the response packet: " + err.message; throw err; }); } /* Send a single command * Return the informations about the response ack packet */ p7.send.singleCommand = (subtype, datatype, fileSize, commandArguments) => p7.send.packet(p7.make.extendedCommandPacket(subtype, datatype, fileSize, commandArguments)) .then((responsePacketInfo) => { //Check if the packet is an ack packet if (!(responsePacketInfo.type === p7PacketType.ack && responsePacketInfo.subtype === ackSubtype.default)) throw new Error("The calculator didn't send the expected ack packet!"); }); /* Send a single command request * Return an array containing the filtered (with `callbackFunction`) Data (D) field of each response command packet */ p7.send.singleCommandRoleswap = function (subtype, datatype, fileSize, commandArguments, callbackFunction) { var filteredData = []; return p7.send.singleCommand(subtype, datatype, fileSize, commandArguments) .then((responsePacketInfo) => p7.send.packet(p7.make.basicPacket(p7PacketType.roleswap, roleswapSubtype.default))) .then(function receiveRequestedCommand(responsePacketInfo) { if (responsePacketInfo.type !== p7PacketType.command) return filteredData; filteredData.push(callbackFunction()); return p7.send.packet(p7.make.basicPacket(p7PacketType.ack, ackSubtype.default)).then(receiveRequestedCommand); }); } /* ---------------------Initialization and exiting--------------------- */ /* Connect to the calculator */ p7.connect = () => navigator.usb.requestDevice({filters:[ {'vendorId': 0x07cf , 'productId': 0x6101}, //fx-9750gII {'vendorId': 0x07cf , 'productId': 0x6102}] //fx-CP400 }).then((device) => { p7.device = device return p7.device.open(); }).then(() => { if (p7.device.configuration === null) return p7.device.selectConfiguration(1); //Only existing one so it should be selected by default, just checking }).then(() => { if (p7.device.configuration.interfaces[0].claimed === false) return p7.device.claimInterface(0); //Same as for the configuration }); /* 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); } */ /* Initialize the connection with the calculator */ p7.init = () => p7.connect() .catch(() => { throw new Error("Couldn't connect to the calculator!"); }).then(() => p7.send.packet(p7.make.basicPacket(p7PacketType.check, checkSubtype.initialization)) .catch(() => { throw new Error("Couldn't send the initialisation packet!"); }).then(() => p7.send.packet(p7.make.basicPacket(p7PacketType.command, sysCommandSubtype.getDeviceInfo)) .catch(() => { throw new Error("Couldn't ask for the device infos!"); }).then(() => p7.decode.extendedAckPacket()))); /* End the connexion with the calculator */ p7.exit = async function () { } document.getElementById('connect').addEventListener('click', async function () { var time = performance.now(); return p7.init() .catch((err) => { console.error(err); return err; }).then((deviceInfo) => { console.log(deviceInfo); //Main commands p7.commands.list('fls0') .then((fileList) => console.log('The following files are in the flash memory: %o', fileList)) .then(() => p7.commands.getCapacity('fls0')) .then((capacity) => console.log('Flash memory free space: ' + capacity + 'o')) .then(() => console.log('Done. (in ' + Math.round(performance.now() - time) + 'ms)')); }) }); })();