Skip to content

Commit

Permalink
Merge pull request #1871 from LedgerHQ/bugfix/FAT-588
Browse files Browse the repository at this point in the history
bugfix/FAT-588 Improve LLM background device communication
  • Loading branch information
juan-cortes authored Nov 30, 2022
2 parents 5000d82 + 3718f98 commit 027689f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-feet-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

Allow runner errors to bubble up and not be silenced
5 changes: 5 additions & 0 deletions .changeset/giant-walls-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/react-native-hw-transport-ble": patch
---

Provide a forceful disconnection mechanism for close request on BleTransport
5 changes: 5 additions & 0 deletions .changeset/sour-countries-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

Enabled BLE background capabilities again
11 changes: 6 additions & 5 deletions apps/ledger-live-mobile/ios/ledgerlivemobile/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Braze</key>
<dict>
<key>Endpoint</key>
<string>sdk.fra-02.braze.eu</string>
</dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down Expand Up @@ -103,6 +108,7 @@
</array>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>fetch</string>
<string>remote-notification</string>
</array>
Expand All @@ -118,10 +124,5 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>Braze</key>
<dict>
<key>Endpoint</key>
<string>sdk.fra-02.braze.eu</string>
</dict>
</dict>
</plist>
16 changes: 9 additions & 7 deletions libs/ledger-live-common/src/apps/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ export const runAllWithProgress = (
return concat(
...getActionPlan(state).map((appOp) => runAppOp(state, appOp, exec))
).pipe(
map(
(event) =>
<Action>{
type: "onRunnerEvent",
event,
}
),
map((event) => {
if (event.type === "runError") {
throw event.error;
}
return <Action>{
type: "onRunnerEvent",
event,
};
}),
scan(reducer, state),
mergeMap((s) => {
// Nb if you also want to expose the uninstall queue, feel free.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { decoratePromiseErrors, remapError } from "./remapErrors";
let connectOptions: Record<string, unknown> = {
requestMTU: 156,
connectionPriority: 1,
forceDisconnectTimeout: 4000,
};
const transportsCache = {};
const bleManager = new BleManager();
Expand All @@ -58,12 +59,10 @@ type ReconnectionConfig = {
delayAfterFirstPairing: number;
};
let reconnectionConfig: ReconnectionConfig | null | undefined = {
pairingThreshold: 1000,
pairingThreshold: 2000,
delayAfterFirstPairing: 4000,
};
export function setReconnectionConfig(
config: ReconnectionConfig | null | undefined
) {
export function setReconnectionConfig(config: ReconnectionConfig | null): void {
reconnectionConfig = config;
}

Expand All @@ -75,6 +74,11 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) {
if (typeof deviceOrId === "string") {
if (transportsCache[deviceOrId]) {
log("ble-verbose", "Transport in cache, using that.");
const maybeTimeout = transportsCache[deviceOrId].disconnectTimeout;
if (maybeTimeout) {
log("ble-verbose", "Clearing queued disconnect");
clearTimeout(transportsCache[deviceOrId].disconnectTimeout);
}
return transportsCache[deviceOrId];
}

Expand Down Expand Up @@ -408,6 +412,7 @@ export default class BluetoothTransport extends Transport {
static disconnect = async (id: any) => {
log("ble-verbose", `user disconnect(${id})`);
await bleManager.cancelDeviceConnection(id);
await delay(1000); // Nb Test to improve stability of re-connections.
};
id: string;
device: Device;
Expand All @@ -417,6 +422,7 @@ export default class BluetoothTransport extends Transport {
notifyObservable: Observable<any>;
deviceModel: DeviceModel;
notYetDisconnected = true;
disconnectTimeout: null | ReturnType<typeof setTimeout> = null;

constructor(
device: Device,
Expand Down Expand Up @@ -537,9 +543,31 @@ export default class BluetoothTransport extends Transport {
}
};

async close() {
if (this.exchangeBusyPromise) {
await this.exchangeBusyPromise;
async close(): Promise<void> {
// Clear any potential leftover timeouts
if (this.disconnectTimeout) {
clearTimeout(this.disconnectTimeout);
}

let resolve;
const disconnectPromise = new Promise<void>((res) => {
resolve = res;
});

// Queue a disconnect
this.disconnectTimeout = setTimeout(() => {
BluetoothTransport.disconnect(this.id);
resolve();
}, connectOptions.forceDisconnectTimeout as number);

// For cases where an exchange hasn't resolve and we triggered a `close` we
// introduce a timeout to forcefully disconnect in order to unblock subsequent
// usages of the `withDevice` logic.
await Promise.race([
this.exchangeBusyPromise || Promise.resolve(),
disconnectPromise,
]);

return;
}
}

0 comments on commit 027689f

Please sign in to comment.