Skip to content

Fix AT32 Configuration can't be saved on MacOS #4455

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

Open
wants to merge 5 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
36 changes: 34 additions & 2 deletions src/js/protocols/WebSerial.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { webSerialDevices, vendorIdNames } from "./devices";
import { checkBrowserCompatibility } from "../utils/checkBrowserCompatibilty";
import { GUI } from "../gui";

const logHead = "[SERIAL]";

Expand Down Expand Up @@ -69,7 +70,7 @@ class WebSerial extends EventTarget {
navigator.serial.addEventListener("connect", (e) => this.handleNewDevice(e.target));
navigator.serial.addEventListener("disconnect", (e) => this.handleRemovedDevice(e.target));
}

this.isNeedBatchWrite = false;
this.loadDevices();
}

Expand Down Expand Up @@ -181,6 +182,10 @@ class WebSerial extends EventTarget {

const connectionInfo = this.port.getInfo();
this.connectionInfo = connectionInfo;
this.isNeedBatchWrite = this.checkIsNeedBatchWrite();
if (this.isNeedBatchWrite) {
console.log(`${this.logHead} Enabling batch write mode for AT32 on macOS`);
}
this.writer = this.port.writable.getWriter();
this.reader = this.port.readable.getReader();

Expand Down Expand Up @@ -328,6 +333,29 @@ class WebSerial extends EventTarget {
}
}

checkIsNeedBatchWrite() {
const isMac = GUI.operating_system === "MacOS";
return isMac && vendorIdNames[this.connectionInfo.usbVendorId] === "AT32";
}

async batchWrite(data) {
// AT32 on macOS requires smaller chunks (63 bytes) to work correctly due to
// USB buffer size limitations in the macOS implementation
const batchWriteSize = 63;
let remainingData = data;
while (remainingData.byteLength > batchWriteSize) {
const sliceData = remainingData.slice(0, batchWriteSize);
remainingData = remainingData.slice(batchWriteSize);
try {
await this.writer.write(sliceData);
} catch (error) {
console.error(`${this.logHead} Error writing batch chunk:`, error);
throw error; // Re-throw to be caught by the send method
}
}
await this.writer.write(remainingData);
}

async send(data, callback) {
if (!this.connected || !this.writer) {
console.error(`${logHead} Failed to send data, serial port not open`);
Expand All @@ -338,7 +366,11 @@ class WebSerial extends EventTarget {
}

try {
await this.writer.write(data);
if (this.isNeedBatchWrite) {
await this.batchWrite(data);
} else {
await this.writer.write(data);
}
this.bytesSent += data.byteLength;

const result = { bytesSent: data.byteLength };
Expand Down
2 changes: 1 addition & 1 deletion src/js/utils/checkBrowserCompatibilty.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function getOS() {
let os = "unknown";
const userAgent = window.navigator.userAgent;
const platform = window.navigator?.userAgentData?.platform || window.navigator.platform;
const macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K", "MacOS"];
const macosPlatforms = ["Macintosh", "MacIntel", "MacPPC", "Mac68K", "macOS"];
const windowsPlatforms = ["Win32", "Win64", "Windows", "WinCE"];
const iosPlatforms = ["iPhone", "iPad", "iPod"];

Expand Down
Loading