Skip to content

Commit

Permalink
Add esim enable and delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Jan 17, 2025
1 parent 8357454 commit 32763fc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
13 changes: 12 additions & 1 deletion src/cli/esim.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,25 @@ module.exports = ({ commandProcessor, root }) => {
});

commandProcessor.createCommand(esim, 'enable', 'Enables a downloaded eSIM profile', {
handler: (args) => {
handler: () => {
const ESimCommands = require('../cmd/esim');
return new ESimCommands().enableCommand();
},
examples: {
'$0 $command': 'TBD'
}
});

commandProcessor.createCommand(esim, 'delete', 'Deletes an ICCID profile on the eSIM', {
params: '<iccid>',
handler: (args) => {
const ESimCommands = require('../cmd/esim');
return new ESimCommands().deleteCommand(args.params.iccid);
},
examples: {
'$0 $command': 'TBD'
}
});
return esim;
};

87 changes: 38 additions & 49 deletions src/cmd/esim.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@ module.exports = class ESimCommands extends CLICommandBase {
const device = await this.serial.whatSerialPortDidYouMean();
if (device.type === 'Tachyon') {
this.isTachyon = true;
await this.doEnable(device);
}
}

await this.doEnable(device);
async deleteCommand(iccid) {
this.verbose = true;
const device = await this.serial.whatSerialPortDidYouMean();
if (device.type === 'Tachyon') {
this.isTachyon = true;
await this.doDelete(device, iccid);
}
}

// Populate the availableProvisioningData set with the indices of the input JSON data
Expand Down Expand Up @@ -257,65 +265,46 @@ module.exports = class ESimCommands extends CLICommandBase {
}

async doEnable(device) {
let provisionOutputLogs = [];
let timestamp = new Date().toISOString().replace(/:/g, '-');
let success = false;

const outputJsonFile = path.join(this.outputFolder, `${this.isTachyon ? 'tachyon' : device.deviceId}_${timestamp}.json`);

const processOutput = async (failedLogs = []) => {
const logs = Array.isArray(failedLogs) ? failedLogs : [failedLogs];
provisionOutputLogs.push({
step: 'final_step',
timestamp: new Date().toISOString().replace(/:/g, '-'),
success: success ? 'success' : 'failed',
details: {
rawLogs: success ? ['Profile enable successful'] : ['Profile enable failed', ...logs],
}
});
await this._changeLed(device, success ? PROVISIONING_SUCCESS : PROVISIONING_FAILURE);
this._addToJson(outputJsonFile, provisionOutputLogs.filter(Boolean));
};

const TACHYON_QLRIL_WAIT_TIMEOUT = 20000;

try {
const port = device.port;

// Start qlril-app through ADB for Tachyon
const qlrilStep = await this._initializeQlril();
provisionOutputLogs.push(qlrilStep);
if (qlrilStep?.status === 'failed') {
await processOutput();
return;
}

const iccidsOnDevice = await this._getIccidOnDevice(port);

const iccidToEnable = this._getIccidToEnable(iccidsOnDevice);
provisionOutputLogs.push(`ICCID to enable: ${iccidToEnable}`);
if (iccidToEnable === null) {
await processOutput('No profile found on the device to enable');
return;
}

const enableResp = await this._enableProfile(port, iccidToEnable);
provisionOutputLogs.push(enableResp);
if (enableResp.status === 'failed') {
await processOutput();
return;
}
this.adbProcess = execa('adb', ['shell', 'qlril-app', 'enable', iccidToEnable]);

await new Promise((resolve) => setTimeout(resolve, TACHYON_QLRIL_WAIT_TIMEOUT));

await this._verifyIccidEnaled(port, iccidToEnable);

console.log('Profile enabled successfully');
} catch (error) {
console.error(`Failed to enable profile: ${error.message}`);
} finally {
this._exitQlril();
}
}

const verifyIccidEnabledResp = await this._verifyIccidEnaled(port, iccidToEnable);
provisionOutputLogs.push(verifyIccidEnabledResp);
if (verifyIccidEnabledResp.status === 'failed') {
await processOutput();
async doDelete(device, iccid) {
try {
const port = device.port;

const iccidsOnDevice = await this._getIccidOnDevice(port);
if (!iccidsOnDevice.includes(iccid)) {
console.log(`ICCID ${iccid} not found on the device`);
return;
}

success = true;
console.log('Profile enabled successfully');
await processOutput();
await this._initializeQlril();

await execa(this.lpa, ['disable', iccid, `--serial=${port}`]);
await execa(this.lpa, ['delete', iccid, `--serial=${port}`]);

console.log('Profile deleted successfully');
} catch (error) {
await processOutput(error.message);
console.error(`Failed to delete profile: ${error.message}`);
} finally {
this._exitQlril();
}
Expand Down

0 comments on commit 32763fc

Please sign in to comment.