Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
device = await bleManager.connectToDevice(deviceOrId);
} else {
throw e;
}
}
}
if (!device) {
throw new CantOpenDevice();
}
} else {
device = deviceOrId;
}
if (!(await device.isConnected())) {
log("ble-verbose", "not connected. connecting...");
try {
await device.connect(connectOptions);
} catch (e) {
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
// eslint-disable-next-line require-atomic-updates
connectOptions = {};
await device.connect();
} else {
throw e;
}
}
}
await device.discoverAllServicesAndCharacteristics();
let res = retrieveInfos(device);
if (!device) {
// works for iOS but not Android
const devices = await bleManager.devices([deviceOrId]);
log("ble-verbose", `found ${devices.length} devices`);
[device] = devices;
}
if (!device) {
const connectedDevices = await bleManager.connectedDevices(
getBluetoothServiceUuids()
);
const connectedDevicesFiltered = connectedDevices.filter(
d => d.id === deviceOrId
);
log(
"ble-verbose",
`found ${connectedDevicesFiltered.length} connected devices`
);
[device] = connectedDevicesFiltered;
}
if (!device) {
log("ble-verbose", `connectToDevice(${deviceOrId})`);
try {
device = await bleManager.connectToDevice(deviceOrId, connectOptions);
} catch (e) {
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
// eslint-disable-next-line require-atomic-updates
connectOptions = {};
device = await bleManager.connectToDevice(deviceOrId);
} else {
this.exchangeAtomicImpl(async () => {
const { channel, packetSize } = this;
log("apdu", "=> " + apdu.toString("hex"));
const framing = hidFraming(channel, packetSize);
// Write...
const blocks = framing.makeBlocks(apdu);
for (let i = 0; i < blocks.length; i++) {
log("hid-frame", "=> " + blocks[i].toString("hex"));
await this.device.sendReport(0, blocks[i]);
}
// Read...
let result;
let acc;
while (!(result = framing.getReducedResult(acc))) {
const buffer = await this.read();
log("hid-frame", "<= " + buffer.toString("hex"));
acc = framing.reduceResponse(acc, buffer);
}
log("apdu", "<= " + result.toString("hex"));
return result;
}).catch(e => {
if (e && e.message && e.message.includes("write")) {
async function resetLibcore() {
log('clear-cache', 'resetLibcore...')
// we need to stop everything that is happening right now, like syncs
await killInternalProcess()
log('clear-cache', 'killed.')
await removeSQLite()
log('clear-cache', 'removeSQLite done.')
}
async function resetLibcore() {
log('clear-cache', 'resetLibcore...')
// we need to stop everything that is happening right now, like syncs
await killInternalProcess()
log('clear-cache', 'killed.')
await removeSQLite()
log('clear-cache', 'removeSQLite done.')
}
const framing = hidFraming(channel, packetSize);
// Write...
const blocks = framing.makeBlocks(apdu);
for (let i = 0; i < blocks.length; i++) {
log("hid-frame", "=> " + blocks[i].toString("hex"));
await this.writeHID(blocks[i]);
}
// Read...
let result;
let acc;
while (!(result = framing.getReducedResult(acc))) {
const buffer = await this.readHID();
log("hid-frame", "<= " + buffer.toString("hex"));
acc = framing.reduceResponse(acc, buffer);
}
log("apdu", "<= " + result.toString("hex"));
return result;
});
async function load(): Promise {
if (core) {
return core;
}
if (!corePromise) {
if (!loadCoreImpl) {
console.warn("loadCore implementation is missing");
throw new Error("loadCoreImpl missing");
}
log("libcore/access", "load core impl");
corePromise = loadCoreImpl();
}
core = await corePromise;
return core;
}
export async function softReset({ cleanAccountsCache }: *) {
log('clear-cache', 'clearBridgeCache()')
clearBridgeCache()
log('clear-cache', 'cleanAccountsCache()')
cleanAccountsCache()
await delay(500)
log('clear-cache', 'db.cleanCache()')
await db.cleanCache()
await resetLibcore()
log('clear-cache', 'reload()')
reload()
}
write = async (buffer: Buffer) => {
log("ble-frame", "=> " + buffer.toString("hex"));
await this.writeCharacteristic.writeValue(buffer);
};
export async function reset() {
log("libcore/access", "reset");
if (!core) return;
invariant(libcoreJobsCounter === 0, "some libcore jobs are still running");
await core.getPoolInstance().freshResetAll();
core = null;
corePromise = null;
}