Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device.getDirectory #28

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/byte-mangling.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
asciiToBytes,
bytesToAscii,

chunkArray
chunkArray,
mergeBuffers
}

function constructRawPacket(packet) {
Expand Down Expand Up @@ -78,8 +79,8 @@ function constructParameters(params) {
intToBytes(params.length, 2),
...params.map(param => [
intToBytes(param.type, 2),
intToBytes(param.size, 2),
intToBytes(param.value, param.size)
intToBytes(param.value.length, 2),
...param.value
].flat())
].flat());
}
Expand All @@ -97,18 +98,18 @@ function destructParameters(params) {
* 7-X X parameter value
* X- repeat pattern N times
*/
const result = [];
const result = {};
const count = bytesToInt(params.slice(0,2));
let j = 2;
for ( let i = 0; i < count; i++ ) {
const size = bytesToInt(params.slice(j+3,j+5));
result.push({
type: bytesToInt(params.slice(j,j+2)),
ok: params[j+2] == 0,
size,
value: bytesToInt(params.slice(j+5, j+5+size))
});
j += 5 + size;
const type = bytesToInt(params.slice(j,j+2));
if(params[j+2] == 0) {
const size = bytesToInt(params.slice(j+3,j+5));
result[type] = params.slice(j+5, j+5+size);
j += 5 + size;
} else {
j += 3;
}
}
return result;
}
Expand Down Expand Up @@ -195,3 +196,18 @@ function chunkArray(array, sizes) {
}
return result;
}

// Merge multiple byte arrays into a single byte array
function mergeBuffers(buffers) {
let totalLength = 0;
for(const buffer of buffers) {
totalLength += buffer.byteLength;
}
const result = new Uint8Array(totalLength);
let offset = 0;
for(const buffer of buffers) {
result.set(buffer, offset);
offset += buffer.byteLength;
}
return result;
}
49 changes: 27 additions & 22 deletions src/dusb/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,40 @@ module.exports = class Device {
}

// Expect a virtual packet of a certain type, returns the packet if the type
// matches. Throws an error otherwise. Note that this function does not
// support a fragmented data transfer, and as such can't be used to receive a
// program from the calculator.
// matches. Throws an error otherwise.
async expect(virtualType) {
const raw = b.destructRawPacket(await this._receive());

if ( raw.type != v.rawPacketTypes.DUSB_RPKT_VIRT_DATA_LAST )
throw `Expected raw packet type VIRT_DATA_LAST, but got ${raw.type} instead`;
const packet = await this.expectAny();
if ( packet.type != virtualType ) {
throw `Expected virtual packet type ${virtualType}, but got ${packet.type} instead`;
}
return packet;
}

const virtual = b.destructVirtualPacket(raw.data);
async expectAny() {
let raw;
const raw_packets = [];

switch(virtual.type) {
// Receive raw packets until we get a VIRT_DATA_LAST packet
do {
raw = b.destructRawPacket(await this._receive());
raw_packets.push(raw);
} while ( raw.type == v.rawPacketTypes.DUSB_RPKT_VIRT_DATA );

// Did we get what we expected?
case virtualType:
await this._sendAck();
return virtual;
if ( raw.type != v.rawPacketTypes.DUSB_RPKT_VIRT_DATA_LAST )
throw `Expected raw packet type VIRT_DATA_LAST, but got ${raw.type} instead`;

// If not, did we get a delay request?
case v.virtualPacketTypes.DUSB_VPKT_DELAY_ACK:
await this._sendAck();
const delay = b.bytesToInt(virtual.data);
await this.wait(delay / 1000);
return this.expect(virtualType);
const combinedData = b.mergeBuffers(raw_packets.map((x) => x.data));

// Otherwise, we have a problem here
default:
throw `Expected virtual packet type ${virtualType}, but got ${virtual.type} instead`;
const virtual = b.destructVirtualPacket(combinedData);

if ( virtual.type == v.virtualPacketTypes.DUSB_VPKT_DELAY_ACK ) {
await this._sendAck();
const delay = b.bytesToInt(virtual.data);
await this.wait(delay / 1000);
return await this.expectAny();
} else {
await this._sendAck();
return virtual;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/dusb/magic-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = {
// https://github.com/debrouxl/tilibs/blob/master/libticalcs/trunk/src/dusb_cmd.h

attributes: {
DUSB_AID_VAR_SIZE: 0x01,
DUSB_AID_VAR_TYPE: 0x02,
DUSB_AID_ARCHIVED: 0x03,
DUSB_AID_VAR_VERSION: 0x08,
Expand Down
75 changes: 59 additions & 16 deletions src/dusb/ti84series.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,74 @@ module.exports = class Ti84series {
await this._d.expect(v.virtualPacketTypes.DUSB_VPKT_DATA_ACK);
}

// Request the amount of free RAM and Flash memory
async getFreeMem() {
async getParameters(attributes) {
await this._d.send({
type: v.virtualPacketTypes.DUSB_VPKT_PARM_REQ,
data: [
0, 2,
b.intToBytes(v.parameters.DUSB_PID_FREE_RAM, 2),
b.intToBytes(v.parameters.DUSB_PID_FREE_FLASH, 2)
b.intToBytes(attributes.length, 2),
...attributes.map(x => b.intToBytes(x, 2))
].flat()
});

const paramsResponse = await this._d.expect(v.virtualPacketTypes.DUSB_VPKT_PARM_DATA);
const params = b.destructParameters(paramsResponse.data);
if ( !params.every(p => p.ok) )
throw 'Could not succesfully get all parameters';
return b.destructParameters(paramsResponse.data);
}

// Request the amount of free RAM and Flash memory
async getFreeMem() {
const params = await this.getParameters([
v.parameters.DUSB_PID_FREE_RAM,
v.parameters.DUSB_PID_FREE_FLASH,
]);

if ( !params[v.parameters.DUSB_PID_FREE_RAM] )
throw 'Could not get amount of free RAM';
if ( !params[v.parameters.DUSB_PID_FREE_FLASH] )
throw 'Could not get amount of free FLASH';

return {
ram: params.find(p => p.type == v.parameters.DUSB_PID_FREE_RAM).value,
flash: params.find(p => p.type == v.parameters.DUSB_PID_FREE_FLASH).value,
ram: b.bytesToInt(params[v.parameters.DUSB_PID_FREE_RAM]),
flash: b.bytesToInt(params[v.parameters.DUSB_PID_FREE_FLASH]),
};
}

async getDirectory() {
await this._d.send({
type: v.virtualPacketTypes.DUSB_VPKT_DIR_REQ,
data: [
0, 0, 0, 3, // attribute count
0, 1, // size
0, 2, // type
0, 3, // archived
0, 1, 0, 1, 0, 1, 1 // ???
]
});

const result = [];
let variable;
while ( variable = await this._getDirectoryEntry() )
result.push(variable);
return result;
}

async _getDirectoryEntry() {
const packet = await this._d.expectAny();
if ( packet.type == v.virtualPacketTypes.DUSB_VPKT_EOT )
return false;
if ( packet.type != v.virtualPacketTypes.DUSB_VPKT_VAR_HDR )
throw `Expected virtual packet type ${v.virtualPacketTypes.DUSB_VPKT_VAR_HDR} (DUSB_VPKT_VAR_HDR), but got ${packet.type} instead`;

const nameLength = b.bytesToInt(packet.data.slice(0, 2));
const name = b.bytesToAscii(packet.data.slice(2, 2 + nameLength));

const attributes = b.destructParameters(packet.data.slice(2 + nameLength + 1));
const size = b.bytesToInt(attributes[v.attributes.DUSB_AID_VAR_SIZE]);
const type = b.bytesToInt(attributes[v.attributes.DUSB_AID_VAR_TYPE].slice(2));
const archived = attributes[v.attributes.DUSB_AID_ARCHIVED][0] == 1;

return {name, size, type, archived};
}

// Send a TI file to the calculator
async sendFile(file) {
for ( let i = 0; i < file.entries.length; i++ ) {
Expand Down Expand Up @@ -119,18 +165,15 @@ module.exports = class Ti84series {
return b.constructParameters([
{
type: v.attributes.DUSB_AID_VAR_TYPE,
size: 4,
value: 0xF0070000 + entry.type
value: b.intToBytes(0xF0070000 + entry.type, 4)
},
{
type: v.attributes.DUSB_AID_ARCHIVED,
size: 1,
value: entry.attributes && entry.attributes.archived ? 1 : 0
value: b.intToBytes(entry.attributes && entry.attributes.archived ? 1 : 0, 1)
},
{
type: v.attributes.DUSB_AID_VAR_VERSION,
size: 4,
value: entry.attributes && entry.attributes.version || 0
value: b.intToBytes(entry.attributes && entry.attributes.version || 0, 1)
}
]);
}
Expand Down
4 changes: 4 additions & 0 deletions src/ticalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ module.exports = {
}
},

setSupportLevel: level => {
calculators = calculatorsForSupportLevel(level);
},

choose: async (options = {}) => {
const usb = options.usb || findOrCreateWebUSBRecording();

Expand Down
56 changes: 31 additions & 25 deletions test/byte-mangling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ describe('byte-mangling.js', () => {
expect(b.constructParameters([
{
type: 0x59FA,
size: 3,
value: 0x123456
value: new Uint8Array([0x12,0x34,0x56])
},
{
type: 0x0001,
size: 1,
value: 6
value: new Uint8Array([6])
}
])).toEqual(
new Uint8Array([0,2, 0x59,0xFA, 0,3, 0x12,0x34,0x56, 0x00,0x01, 0,1, 6])
Expand All @@ -87,29 +85,13 @@ describe('byte-mangling.js', () => {
new Uint8Array([
0,3,
0xDE,0x35, 0, 0,2, 0xAB,0xCD,
0x02,0x00, 1, 0,5, 0,0,0x12,0x34,0x56,
0x02,0x00, 1,
0x00,0x4A, 0, 0,5, 0x12,0x34,0x56,0x78,0x90
])
)).toEqual([
{
type: 0xDE35,
ok: true,
size: 2,
value: 0xABCD
},
{
type: 0x0200,
ok: false,
size: 5,
value: 0x123456
},
{
type: 0x004A,
ok: true,
size: 5,
value: 0x1234567890
}
]);
)).toEqual({
"56885": new Uint8Array([0xAB,0xCD]),
"74": new Uint8Array([0x12,0x34,0x56,0x78,0x90])
});
});
});

Expand Down Expand Up @@ -246,4 +228,28 @@ describe('byte-mangling.js', () => {
});
});

describe('mergeBuffers', () => {
it('combines multiple arrays', () => {
expect(b.mergeBuffers(
[new Uint8Array([1,2,3]), new Uint8Array([4,5,6]), new Uint8Array([7,8,9])]
)).toEqual(
new Uint8Array([1,2,3,4,5,6,7,8,9])
);
});
it('handles empty arrays', () => {
expect(b.mergeBuffers(
[new Uint8Array([1,2,3]), new Uint8Array([]), new Uint8Array([7,8,9])]
)).toEqual(
new Uint8Array([1,2,3,7,8,9])
);
});
it('preserves a single array', () => {
expect(b.mergeBuffers(
[new Uint8Array([1,2,3])]
)).toEqual(
new Uint8Array([1,2,3])
);
});
});

});
Loading