Skip to content

Commit

Permalink
"Midway through working on automatic MTP endpoints detection"
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenkingston committed Sep 11, 2020
1 parent e91ab3a commit a7dead2
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 26 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h1>WebMTP</h1>
<button id="uploadButton" class="button" value="Choose Files!" onclick="document.getElementById('fileInput').click();"> Upload file </button>
<input id="fileInput" type="file" style="display:none;" />
<div id="disconnectB"><button id="disconnectButton" class="button" onclick="closeDevice()"> Disconnect </button></div>
<div id="about" onclick="downloadAsFile()">&iukcy;</div>
<div id="about" onclick="endpoints()">&iukcy;</div>
</div>
</div>

Expand Down
5 changes: 5 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,9 @@ function showStorageInfo(storageObject)
{
storage_info_div.innerText = (freeSpace / (1024 * 1024)).toString() + " MB free" + " / " + (storageSize / (1024 * 1024)).toString() + " MB";
}
}

async function endpoints()
{
await device.getEndpoints();
}
138 changes: 120 additions & 18 deletions mtp/mtp_classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,33 +232,134 @@ class ObjectInfoDataset
}
}

/* Configuration Descriptor class */

class deviceDescriptor
{
constructor(devDescriptorArray, configDescriptorArray)
{
this.bLength = devDescriptorArray[0];
this.bNumConfigurations = devDescriptorArray[17];
this.configDescriptors = new Array(0);
this.configDescriptorLength = 0;

this.lastSliceLength = 0;
console.log("Yes", this.bNumConfigurations)

/* Split configuration Descriptor Arrays */
for (let i = 0; i < this.bNumConfigurations; i++)
{
this.configDescriptorLength = configDescriptor[2 + this.configDescriptorLength];
let configDescriptor = configDescriptorArray.slice(this.lastSliceLength, this.configDescriptorLength + this.lastSliceLength)
this.lastSliceLength = this.configDescriptorLength;
console.log(configDescriptor);
this.configDescriptors.push(new configDescriptor(configDescriptor));
}
}
}

class configDescriptor
{
constructor(configDescriptorArray)
{
this.bLength = configDescriptorArray[0];
this.wTotalLength = configDescriptorArray[2] | configDescriptorArray[3] << 8;
this.bNumInterfaces = configDescriptorArray[4];

this.interfaceDescriptors = new Array(0);


for (let i = 0; i < this.bNumInterfaces; i++)
{

}
}
}

class interfaceDescriptor
{
constructor()
{

}
}

class endpointDescriptor
{
constructor(endpointDescriptorArray)
{
this.bLength = endpointDescriptorArray[0];
this.bmAttributes = endpointDescriptorArray[3];
this.endPointAddr = endpointDescriptorArray[2];
this.endpointDescriptors = new Array(0);

this.endpointDescriptors.push(new endpointDescriptor(endpointDescriptorArray))
}
}

/* Device class for all device operations */

class MTPDevice
{
constructor()
{
this.endpointIn = 1;
this.endpointOut = 2;
this.endpointIn = 0;
this.endpointOut = 0;
this.device = null;
this.sessionOpen = false;
this.storageInfoObjects = new Array(0);
this.objectInfoObjects = new Array(0);
this.transactionID = 0x01;
this.interfaceNumber = 0;
}

async getEndpoints()
{
/* Requesting Device Descriptor */
this.device.controlTransferIn({ requestType: 'standard',
recipient: 'device',
request: 0x06,
value: 0x100,
index: 0x00},12);


this.endpointIn = 1;
this.endpointOut = 1;
// let deviceDescriptorArray = null;
// let configDescriptorArray = null;
//
// /* Requesting Device Descriptor */
// await this.device.controlTransferIn({ requestType: 'standard',
// recipient: 'device',
// request: 0x06,
// value: 0x0100,
// index: 0x00},30)
// .then((input) =>
// {
// deviceDescriptorArray = new Uint8Array(input.data.buffer);
// console.log(deviceDescriptorArray);
// })
//
// /* Requesting Configuration Descriptor */
// await this.device.controlTransferIn({ requestType: 'standard',
// recipient: 'device',
// request: 0x06,
// value: 0x200,
// index: 0x00},128)
// .then((input2) => {
// configDescriptor = new Uint8Array(input2.data.buffer);
// console.log(configDescriptor);
// });
//
// let descriptorObject = new deviceDescriptor(deviceDescriptor, configDescriptor);


var configurationInterfaces = this.device.configuration.interfaces;
let element = configurationInterfaces[0];

element.alternates.forEach(elementalt => {
this.interfaceNumber = element.interfaceNumber;
elementalt.endpoints.forEach(elementendpoint => {
if (elementendpoint.direction === "out" && elementendpoint.type === "bulk") {
this.endpointOut = elementendpoint.endpointNumber;
console.log(this.endpointOut);
}
if (elementendpoint.direction==="in" && elementendpoint.type === "bulk") {
this.endpointIn =elementendpoint.endpointNumber;
console.log(this.endpointIn);
}
})
})
}


Expand Down Expand Up @@ -306,22 +407,23 @@ class MTPDevice

async connectDevice(MTPDevice)
{
try
// try
{
this.device = await navigator.usb.requestDevice({ filters: [{}]});
if (this.device !== undefined)
{
await this.device.open();
await this.device.selectConfiguration(1);
await this.device.claimInterface(0);
await this.getEndpoints();
return true;
}
}
catch
{
console.log("Error connecting to target.");
return false;
}
// catch
// {
// console.log("Error connecting to target.");
// return false;
// }
}

async openSession(MTPDevice)
Expand Down
14 changes: 7 additions & 7 deletions mtp/mtp_operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ async function MTPDeviceInit()
{
device = new MTPDevice();
let status = null;
try
// try
{
let success = await device.getEndpoints();
let success = true;
let connected = false;
if (success)
{
Expand All @@ -25,11 +25,11 @@ async function MTPDeviceInit()
return device;
}
}
catch(err)
{
console.log("Error in USB connection. Error:" + err);
return null;
}
// catch(err)
// {
// console.log("Error in USB connection. Error:" + err);
// return null;
// }
}

async function downloadFile(MTPDevice, storageID, fileID, progressBar)
Expand Down

0 comments on commit a7dead2

Please sign in to comment.