diff --git a/adafruit-ble-file-transfer.js b/adafruit-ble-file-transfer.js index 4df3986..feb9d74 100644 --- a/adafruit-ble-file-transfer.js +++ b/adafruit-ble-file-transfer.js @@ -124,13 +124,11 @@ class FileTransferClient { } else if (this._command == MOVE_STATUS) { this._command = await this.processMoveStatus(new DataView(this._buffer.buffer, 0, this._offset)); } else { - console.log("Unknown Command :" + this._command); + console.log("Unknown Command: " + this._command); } if (this._command != THIS_COMMAND) { //reset buffer this._offset = 0; - } else { - //wait for more } } @@ -198,13 +196,12 @@ class FileTransferClient { let freeSpace = payload.getUint32(16, true); if (status != STATUS_OK) { if (status == STATUS_ERROR_USB_MOUNTED) { - console.log("Unable to write while USB connected"); + this._reject("Unable to write while USB connected"); } else if (status == STATUS_ERROR) { - console.log("Invalid Path"); + this._reject("Invalid Path"); } else { - console.log("Unknown Status", status); + this._reject("Unknown Status: " + status); } - this._reject(status); this._reject = null; this._resolve = null; return ANY_COMMAND; @@ -237,13 +234,12 @@ class FileTransferClient { let chunkLength = payload.getUint32(12, true); if (status != STATUS_OK) { if (status == STATUS_ERROR_USB_MOUNTED) { - console.log("Unable to read while USB connected"); + this._reject("Unable to read while USB connected"); } else if (status == STATUS_ERROR) { - console.log("Invalid Path"); + this._reject("Invalid Path"); } else { - console.log("Unknown Status", status); + this._reject("Unknown Status: " + status); } - this._reject(status); this._resolve = null; this._reject = null; this._incomingFile = null; @@ -286,23 +282,19 @@ class FileTransferClient { let b = this._buffer.buffer; const headerSize = 28; let cmd, path; + let flags, modificationTime, fileSize; let status = payload.getUint8(1); let pathLength = payload.getUint16(2, true); let i = payload.getUint32(4, true); let totalItems = payload.getUint32(8, true); - let flags = payload.getUint32(12, true); - let modificationTime = payload.getBigUint64(16, true); - let fileSize = payload.getUint32(24, true); - if (status != STATUS_OK) { if (status == STATUS_ERROR_USB_MOUNTED) { - console.log("Unable to read while USB connected"); + this._reject("Unable to read while USB connected"); } else if (status == STATUS_ERROR) { - console.log("Invalid Path"); + this._reject("Invalid Path"); } else { - console.log("Unknown Status", status); + this._reject("Unknown Status: " + status); } - this._reject(status); this._resolve = null; this._reject = null; return ANY_COMMAND; @@ -314,28 +306,21 @@ class FileTransferClient { if (offset + headerSize + pathLength > payload.byteLength) { break; } - cmd = payload.getUint8(offset + 0); - status = payload.getUint8(offset + 1); pathLength = payload.getUint16(offset + 2, true); i = payload.getUint32(offset + 4, true); totalItems = payload.getUint32(offset + 8, true); - flags = payload.getUint32(offset + 12, true); - modificationTime = payload.getBigUint64(offset + 16, true); - fileSize = payload.getUint32(offset + 24, true); offset += headerSize + pathLength; } - if (i < totalItems - 1) { + // Check if we have all items and all expected data for last item + if (i < totalItems - 1 || payload.byteLength < offset + headerSize) { // need more return THIS_COMMAND; } - // full payload + // full payload, now process it offset = 0; while (offset < payload.byteLength) { - if (offset + headerSize > payload.byteLength) { - break; - } cmd = payload.getUint8(offset + 0); status = payload.getUint8(offset + 1); pathLength = payload.getUint16(offset + 2, true); @@ -344,7 +329,6 @@ class FileTransferClient { flags = payload.getUint32(offset + 12, true); modificationTime = payload.getBigUint64(offset + 16, true); fileSize = payload.getUint32(offset + 24, true); - if (cmd != LISTDIR_ENTRY) { throw new ProtocolError(); } @@ -383,13 +367,12 @@ class FileTransferClient { if (status != STATUS_OK) { if (status == STATUS_ERROR_USB_MOUNTED) { - console.log("Unable to write while USB connected"); + this._reject("Unable to write while USB connected"); } else if (status == STATUS_ERROR) { - console.log("Invalid Path"); + this._reject("Invalid Path"); } else { - console.log("Unknown Status", status); + this._reject("Unknown Status: " + status); } - this._reject(status); } else { this._resolve(true); } @@ -409,13 +392,12 @@ class FileTransferClient { let status = payload.getUint8(1); if (status != STATUS_OK) { if (status == STATUS_ERROR_USB_MOUNTED) { - console.log("Unable to write while USB connected"); + this._reject("Unable to write while USB connected"); } else if (status == STATUS_ERROR) { - console.log("File or Folder not found"); + this._reject("File or Folder not found"); } else { - console.log("Unknown Status", status); + this._reject("Unknown Status: " + status); } - this._reject(status); } else { this._resolve(true); } @@ -435,13 +417,12 @@ class FileTransferClient { let status = payload.getUint8(1); if (status != STATUS_OK) { if (status == STATUS_ERROR_USB_MOUNTED) { - console.log("Unable to write while USB connected"); + this._reject("Unable to write while USB connected"); } else if (status == STATUS_ERROR) { - console.log("Unable to move file"); + this._reject("Unable to move file"); } else { - console.log("Unknown Status", status); + this._reject("Unknown Status: " + status); } - this._reject(status); } else { this._resolve(true); } @@ -454,7 +435,7 @@ class FileTransferClient { async makeDir(path, modificationTime) { await this.checkConnection(); if (modificationTime === undefined) { - modificationTime = Date.now() + modificationTime = Date.now(); } let encoded = new TextEncoder().encode(path); var header = new ArrayBuffer(16); @@ -477,9 +458,6 @@ class FileTransferClient { // Returns a list of tuples, one tuple for each file or directory in the given path async listDir(path) { await this.checkConnection(); - - this._incomingOffset = 0; - let encoded = new TextEncoder().encode(path); var header = new ArrayBuffer(4); var view = new DataView(header); diff --git a/package-lock.json b/package-lock.json index 42af8da..e804c64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@adafruit/ble-file-transfer", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@adafruit/ble-file-transfer", - "version": "1.0.0", + "version": "1.0.1", "license": "MIT", "devDependencies": {} } diff --git a/package.json b/package.json index 6257f5c..fce754a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "publishConfig": { "registry": "https://npm.pkg.github.com" }, - "version": "1.0.0", + "version": "1.0.1", "description": "WebBluetooth library for transferring files over BLE", "main": "adafruit-ble-file-transfer.js", "dependencies": {