Skip to content

Commit

Permalink
Enable profiles if they already exist
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Feb 6, 2025
1 parent 44dc9d1 commit 530892d
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 deletions src/cmd/esim.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ module.exports = class ESimCommands extends CLICommandBase {
this.isTachyon = true;
}

this._validateArgs(args, { lpa: true, input: true, output: true, binary: !this.isTachyon });
this._validateArgs(args, { lpa: true, input: true, output: false, binary: !this.isTachyon });
await this._generateAvailableProvisioningData();

await this.doProvision(device);
}

async bulkProvisionCommand(args) {
console.log(chalk.red(`Do not use bulk mode for Tachyon${os.EOL}`));
this._validateArgs(args, { lpa: true, input: true, output: true, binary: true });
this._validateArgs(args, { lpa: true, input: true, output: false, binary: true });

await this._generateAvailableProvisioningData();

Expand Down Expand Up @@ -91,7 +91,7 @@ module.exports = class ESimCommands extends CLICommandBase {

async enableCommand(iccid) {
await this._checkForTachyonDevice();
await this.doEnable(iccid);
await this.doEnableTahcyon(iccid);
}

async deleteCommand(args, iccid) {
Expand Down Expand Up @@ -169,7 +169,7 @@ module.exports = class ESimCommands extends CLICommandBase {
return;
}

// Start particle-ril through ADB for Tachyon
// Start particle-tachyon-ril through ADB for Tachyon
const qlrilStep = await this._initializeQlril();
provisionOutputLogs.push(qlrilStep);
if (qlrilStep?.status === 'failed') {
Expand Down Expand Up @@ -206,8 +206,34 @@ module.exports = class ESimCommands extends CLICommandBase {
});

if (existingProfiles.length > 0) {
success = false;
provisionOutputLogs.push('Profiles already exist on the device');
const iccidsOnDevice = await this._getIccidOnDevice(port);
const iccidsOnDeviceNotTest = iccidsOnDevice.filter((iccid) => !TEST_ICCID.includes(iccid));

const iccidToEnable = this._getIccidToEnable({ iccidList: iccidsOnDeviceNotTest });
if (iccidToEnable === null) {
success = false;
await processOutput('No profile found on the device to enable');
return;
}

iccidsOnDeviceNotTest.forEach(async (iccid) => {
const enableResp = await this._enableProfile(port, iccid);
provisionOutputLogs.push(enableResp);
if (enableResp.status === 'failed') {
await processOutput();
return;
}

const verifyIccidEnabledResp = await this._verifyIccidEnaled(port, iccidToEnable);
provisionOutputLogs.push(verifyIccidEnabledResp);
if (verifyIccidEnabledResp.status === 'failed') {
await processOutput();
return;
}
});

success = true;
console.log(`${os.EOL}Profile ${iccidToEnable} enabled for EID ${eid}`);
await processOutput();
return;
}
Expand Down Expand Up @@ -242,7 +268,7 @@ module.exports = class ESimCommands extends CLICommandBase {
return;
}

const iccidToEnable = this._getIccidToEnable(profilesMatch.details.iccidsOnDevice);
const iccidToEnable = this._getIccidToEnable({ iccidList: profilesMatch.details.iccidsOnDevice });
if (iccidToEnable === null) {
success = false;
await processOutput('No profile found on the device to enable');
Expand Down Expand Up @@ -273,9 +299,9 @@ module.exports = class ESimCommands extends CLICommandBase {
}
}

async doEnable(iccid) {
async doEnableTachyon(iccid) {
try {
const { stdout } = await execa('adb', ['shell', 'particle-ril', 'enable', iccid]);
const { stdout } = await execa('adb', ['shell', 'particle-tachyon-ril', 'enable', iccid]);
if (stdout.includes(`ICCID currently active: ${iccid}`)) {
console.log(`ICCID ${iccid} enabled successfully`);
}
Expand Down Expand Up @@ -312,7 +338,7 @@ module.exports = class ESimCommands extends CLICommandBase {

async doList() {
try {
const { stdout } = await execa('adb', ['shell', 'particle-ril', 'listProfiles']);
const { stdout } = await execa('adb', ['shell', 'particle-tachyon-ril', 'listProfiles']);

const iccids = stdout
.trim()
Expand Down Expand Up @@ -343,7 +369,7 @@ module.exports = class ESimCommands extends CLICommandBase {
}
}

this.outputFolder = args?.output || 'esim_loading_logs';
this.outputFolder = args?.output || path.join(process.cwd(), 'esim_loading_logs');
if (!fs.existsSync(this.outputFolder)) {
fs.mkdirSync(this.outputFolder);
}
Expand Down Expand Up @@ -521,7 +547,7 @@ module.exports = class ESimCommands extends CLICommandBase {
}

logAndPush('Initalizing qlril app on Tachyon through adb');
this.adbProcess = execa('adb', ['shell', 'particle-ril', '--port', '/dev/ttyGS2']);
this.adbProcess = execa('adb', ['shell', 'particle-tachyon-ril', '--port', '/dev/ttyGS2']);

try {
await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -782,7 +808,7 @@ module.exports = class ESimCommands extends CLICommandBase {
return stepOutput();
}

_getIccidToEnable(iccidList) {
_getIccidToEnable({ iccidList } = {}) {
// get the first available Twilio ICCID and if not found, get the first available profile
const twilioIccid = iccidList.find((iccid) => iccid.startsWith(TWILIO_ICCID_PREFIX));
return twilioIccid || iccidList[0] || null;
Expand All @@ -799,12 +825,20 @@ module.exports = class ESimCommands extends CLICommandBase {
}
};

const enableProfileCmd = `${this.lpa} enable ${iccid} --serial=${port}`;
const enableProfileResp = await execa(this.lpa, ['enable', `${iccid}`, `--serial=${port}`]);
res.details.rawLogs.push(enableProfileResp.stdout);
res.status = enableProfileResp.stdout.includes('Profile successfully enabled') ? 'success' : 'failed';
res.details.command = enableProfileCmd;
return res;
try {
const enableProfileCmd = `${this.lpa} enable ${iccid} --serial=${port}`;
const enableProfileResp = await execa(this.lpa, ['enable', `${iccid}`, `--serial=${port}`]);
res.details.rawLogs.push(enableProfileResp.stdout);
res.status = enableProfileResp.stdout.includes('Profile successfully enabled') ? 'success' : 'failed';
res.details.command = enableProfileCmd;
return res;
} catch (error) {
if (error.message.includes('profile not in disabled state')) {
res.details.rawLogs.push(`Profile already enabled: ${iccid}`);
return res;
}
throw error;
}
}

async _verifyIccidEnaled(port, iccid) {
Expand Down

0 comments on commit 530892d

Please sign in to comment.