webP7/index.js

234 lines
9.7 KiB
JavaScript

"use strict";
let device = undefined;
let fileList = undefined;
let p7SendBuffer = new Uint8Array(1032); //Max send buffer size
let p7ReceiveBuffer = new Uint8Array(1032 + 1); //Max receive buffer size
let p7ReceivedPacketInfo = {
'type' : undefined,
'subtype' : undefined,
'extended' : undefined,
'dataSize' : undefined,
'data' : undefined,
'checksum' : undefined,
'packetSize': undefined
};
let p7DeviceInformation = {
'hardwareIdentifier' : undefined,
'processorIdentifier' : undefined,
'preprogrammedROMCapacity' : undefined,
'flashROMCapacity' : undefined,
'ramCapacity' : undefined,
'prepogrammedROMVersion' : undefined,
'bootCodeVersion' : undefined,
'bootCodeOffset' : undefined,
'bootCodeSize' : undefined,
'osCodeVersion' : undefined,
'osCodeOffset' : undefined,
'osCodeSize' : undefined,
'protocolVersion' : undefined,
'productID' : undefined,
'nameSetByUserInSystem' : undefined
};
var fls0 = [];
var crd0 = [];
/* List the files of a filesystem (fls0, crd0...) */
/* Return 0 on success and the error on failure */
async function list(fileSystem) {
let err = 0;
//Send a command packet requesting the calculator to list it's file (in the flash memory) and receive the response packet
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.fileInfoTransferAllRequest, 0, 0, 0, ["", "", "", "", fileSystem, ""]))
return "Couldn't send the command packet to the calculator : " + err;
//Send a roleswap packet so that the calculator can become the active side and send the list of it's files (in flash memory)
if (err = await p7SendBasicPacket(p7PacketType.roleswap, roleswapSubtype.default))
return "Couldn't send the roleswap packet : " + err;
//Reset the list since the calculator send each time all it's elements
window[fileSystem] = [];
//Receive and parse the file list
while(p7ReceivedPacketInfo.type !== p7PacketType.roleswap) {
window[fileSystem].push(createFileObject(String.fromCharCode.apply(null, p7CommandPacketGetField(4)), String.fromCharCode.apply(null, p7CommandPacketGetField(0)), String.fromCharCode.apply(null, p7CommandPacketGetField(1)), p7CommandPacketGetFilesize()));
if (err = await p7SendBasicPacket(p7PacketType.ack, ackSubtype.default))
return "Couldn't send the acknowledgement packet : " + err;
}
return 0;
};
async function createDirectory(name, filesystem) {
let err = 0;
//Send a command packet which asks the calculator to create a directory
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.createDirectory, 0, 0, 0, [name, "", "", "", filesystem, ""]))
return 'Couldn\'t send the command : ' + err;
//Check the response packet type
if (p7ReceivedPacketInfo.type === p7PacketType.ack)
return 0;
return "The response packet wasn't as expected.";
}
async function deleteDirectory(name, filesystem) {
let err = 0;
//Send a command packet which asks the calculator to delete a directory
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.deleteDirectory, 0, 0, 0, [name, "", "", "", filesystem, ""]))
return 'Couldn\'t send the command : ' + err;
//Check the response packet type
if (p7ReceivedPacketInfo.type === p7PacketType.ack)
return 0;
return "The response packet wasn't as expected.";
}
/* Doesn't work ?? */
async function renameDirectory(oldName, newName, filesystem) {
let err = 0;
//Send a command packet which asks the calculator to delete a directory
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.renameDirectory, 0, 0, 0, [oldName, newName, "", "", filesystem, ""]))
return 'Couldn\'t send the command : ' + err;
//check the response packet type
if (p7ReceivedPacketInfo.type === p7PacketType.ack)
return 0;
return "The response packet wasn't as expected.";
}
/* Doesn't work ?? */
async function changeDirectory(name, filesystem) {
let err = 0;
//Send a command packet which asks the calculator change working directory
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.changeDirectory, 0, 0, 0, [name, "", "", "", filesystem, ""]))
return 'Couldn\'t send the command : ' + err;
if (p7ReceivedPacketInfo.type === p7PacketType.ack)
return 0;
return "The response packet wasn't as expected.";
}
/* TODO: finish it */
async function fileTransfer(file, path, fileSystem) {
let err = 0;
console.log('Sending file transfer request');
//Send a command packet which asks the calculator if we can send a file
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.fileTransfer, 0, 0, file.size, [path, file.name, "", "", fileSystem, ""]))
return 'Couldn\'t send the command : ' + err;
console.log(p7SendBuffer.slice(0, 64));
if (p7ReceivedPacketInfo.type === p7PacketType.error && p7ReceivedPacketInfo.subtype === errorSubtype.overwriteRequest) {
if (err = (confirm('The file already exists, overwrite ?')) ? await p7SendBasicPacket(p7PacketType.ack, ackSubtype.overwriteYes) : await p7SendBasicPacket(p7PacketType.error, errorSubtype.overwriteNo))
return 'Couldn\'t send the overwrite response packet : ' + err;
if (p7ReceivedPacketInfo.type === p7PacketType.error && p7ReceivedPacketInfo.subtype === errorSubtype.overwriteImpossible)
return 'The calculator couldn\'t overwrite the file : ' + err;
}
console.log('Sending data');
if (err = await p7SendData(flashCommandSubtype.fileTransfer, content))
return 'Couldn\'t send the file : ' + err;
if (p7ReceivedPacketInfo.type ==! p7PacketType.ack)
return "unexpected response packet";
return 0;
}
/* TODO: add promise system */
function getCapacity(fileSystem) {
return new Promise((resolve, reject) => {
let err = 0;
//Send a command packet requesting the calculator to send it's capacity (free memory)
if (err = p7SendExtendedCommandPacket(flashCommandSubtype.capacityTransmitRequest, 0, 0, 0, ["", "", "", "", fileSystem, ""]))
reject('Couldn\'t send the command packet to the calculator : ' + err);
//Send a roleswap packet so that the calculator can become the active side and send it's capacity
if (err = p7SendBasicPacket(p7PacketType.roleswap, roleswapSubtype.default))
reject('Couldn\'t send the roleswap packet : ' + err);
let freeMemory = p7CommandPacketGetFilesize();
//Send an acknowledgement packet and receive the response packet
if (err = p7SendBasicPacket(p7PacketType.ack, roleswapSubtype.default))
reject('Couldn\'t send the acknowledgement packet : ' + err);
if (p7ReceivedPacketInfo.type !== p7PacketType.roleswap)
reject('The calculator didn\'t respond with the expected packet : ' + p7ReceivedPacketInfo.type + ' (packet type) ' + p7ReceivedPacketInfo.subtype + ' (packet subtype).');
//Everything went right
resolve(freeMemory);
});
};
async function optimize(fileSystem) {
let err = 0
if (err = await p7SendExtendedCommandPacket(flashCommandSubtype.optimizeFilesystem, 0, 0, 0, ["", "", "", "", fileSystem, ""]))
return "Couldn't optimize the fileSystem : " + err;
return 0;
}
/* Main function */
/* Return 0 on success and the error on failure */
async function talkToCalculator() {
let err = await p7Initialization();
if (err)
return "Failed to initiate the connection : " + err;
console.log("%cExecute user request", "font-size: 14px; color: white");
//if (err = fileTransfer(0, ))
let capacity = 0;
console.log(getCapacity('fls0').then((success) => capacity = success, (failure) => failure));
console.log(capacity);
//var file = document.getElementById('input').files[0];
//var file = await window.showOpenFilePicker();
//await fileTransfer(file, "", 'fls0');
//await list('fls0');
//console.log(file);
/* Terminate packet handling */
// //Check if the packet is a terminate packet
// if (p7ReceivedPacketInfo.type === p7PacketType.terminate)
// return 'The calculator closed the connection' + (err = await p7DecodeTerminatePacket()) ? ';but there was an error exiting the session : ' + err : '.';
document.getElementById('demo').innerHTML =
'Hardware identifier : ' + p7DeviceInformation['Hardware identifier'] +
'<br>Processor identifier : ' + p7DeviceInformation['Processor identifier'] +
'<br>Preprogrammed ROM capaciy : ' + p7DeviceInformation['Preprogrammed ROM capaciy']+
'<br>Flash ROM capacity : ' + p7DeviceInformation['Flash ROM capacity'] +
'<br>RAM capacity : ' + p7DeviceInformation['RAM capacity'] +
'<br>Prepogrammed ROM version : ' + p7DeviceInformation['Prepogrammed ROM version'] +
'<br>Boot code version : ' + p7DeviceInformation['Boot code version'] +
'<br>Boot code offset : ' + p7DeviceInformation['Boot code offset'] +
'<br>Boot code size : ' + p7DeviceInformation['Boot code size'] +
'<br>Os code version : ' + p7DeviceInformation['Os code version'] +
'<br>Os code offset : ' + p7DeviceInformation['Os code offset'] +
'<br>Os code size : ' + p7DeviceInformation['Os code size'] +
'<br>Protocol version : ' + p7DeviceInformation['Protocol version'] +
'<br>Product ID : ' + p7DeviceInformation['Product ID'] +
'<br>Name set by user in system : ' + p7DeviceInformation['Name set by user in system'];
await device.transferOut(1, p7SendBuffer.slice(0, p7MakeBasicPacket(p7PacketType['terminate'],
terminateSubtype['default'])));
}
window.onload = (event) => {
document.querySelector('#connect').onclick = async () => {
let err = 0;
if (err = await talkToCalculator())
console.log('%c%s', "color: red", err);
}
}