jolly submitted this change.
Introduce 'operation' parameters to main file
Instead of having several command line options to trigger various
operations, operational argument(s) are used. Because an operation runs
exclusively, arguments for only one operation can be given at the same
time.
New operations are added in later patches, so they can use the
operational arguments as well and do not need to introduce new command
line options.
Revert from getopt_long() back to getopt(), because long options are not
required for this approach.
Related: SYS#8101
Change-Id: I6b960f840820990de40fb07332669ddbfcdf1e7d
---
M src/ipa/main.c
1 file changed, 70 insertions(+), 49 deletions(-)
diff --git a/src/ipa/main.c b/src/ipa/main.c
index fae8476..93e8c60 100644
--- a/src/ipa/main.c
+++ b/src/ipa/main.c
@@ -24,6 +24,12 @@
#define DEFAULT_NVSTATE_PATH "./nvstate.bin"
#define DEFAULT_ESIPA_REQ_RETRIES 3
+enum operation {
+ OPER_GET_EIM_PACKAGE,
+ OPER_ADD_INITIAL_EIM,
+ OPER_EUICC_MEMORY_RESET,
+};
+
bool running = true;
bool prfle_inst_consent(char *sm_dp_plus_address, char *ac_token)
@@ -39,26 +45,29 @@
return false;
}
-static void print_help(void)
+static void print_help(const char *prog_name)
{
+ printf("Usage: %s [OPTIONS] [OPERATION]\n", prog_name);
printf("options:\n");
- printf(" -h ......................... print this text.\n");
- printf(" -t TAC ..................... set TAC (default: %s)\n", DEFAULT_TAC);
- printf(" -e eimId ................... set preferred eIM (in case the eUICC has multiple)\n");
- printf(" -r N ....................... set reader number (default: %d)\n", DEFAULT_READER_NUMBER);
- printf(" -c N ....................... set logical channel number (default: %d)\n", DEFAULT_CHANNEL_NUMBER);
- printf(" -f PATH .................... set initial eIM configuration\n");
- printf(" -m ......................... reset eUICC memory\n");
- printf(" --refresh-flag.............. Make eUICC send a CAT refresh after switching profile\n");
- printf(" -n PATH .................... path to nvstate file (default: %s)\n", DEFAULT_NVSTATE_PATH);
- printf(" -y NUM ..................... number of retries for ESipa requests (default: %u)\n",
+ printf(" -h .......................... print this text.\n");
+ printf(" -t TAC ...................... set TAC (default: %s)\n", DEFAULT_TAC);
+ printf(" -e eimId .................... set preferred eIM (in case the eUICC has multiple)\n");
+ printf(" -r N ........................ set reader number (default: %d)\n", DEFAULT_READER_NUMBER);
+ printf(" -c N ........................ set logical channel number (default: %d)\n", DEFAULT_CHANNEL_NUMBER);
+ printf(" -R .......................... Make eUICC send a CAT refresh after switching profile\n");
+ printf(" -n PATH ..................... path to nvstate file (default: %s)\n", DEFAULT_NVSTATE_PATH);
+ printf(" -y NUM ...................... number of retries for ESipa requests (default: %u)\n",
DEFAULT_ESIPA_REQ_RETRIES);
- printf(" -a ......................... ask end user for consent\n");
- printf(" -C ......................... CA (Certificate Authority) Bundle file\n");
- printf(" -S ......................... disable HTTPS\n");
- printf(" -I ......................... disable SSL certificate verification (insecure)\n");
- printf(" -E ......................... emulate IoT eUICC (compatibility mode to use consumer eUICCs)\n");
- printf(" -1 ......................... force the IPAd to process only one eUICC package (debug, use with caution)\n");
+ printf(" -a .......................... ask end user for consent\n");
+ printf(" -C .......................... CA (Certificate Authority) Bundle file\n");
+ printf(" -S .......................... disable HTTPS\n");
+ printf(" -I .......................... disable SSL certificate verification (insecure)\n");
+ printf(" -E .......................... emulate IoT eUICC (compatibility mode to use consumer eUICCs)\n");
+ printf(" -1 .......................... force the IPAd to process only one eUICC package (debug, use with caution)\n");
+ printf("operation:\n");
+ printf(" get-eim-package ............. Ask eIM for eIM package and process it (default operation)\n");
+ printf(" add-initial-eim PATH ........ set initial eIM configuration from given file\n");
+ printf(" euicc-memory-reset .......... reset eUICC memory\n");
}
struct ipa_buf *load_ber_from_file(char *dir, char *file)
@@ -141,13 +150,12 @@
struct ipa_context *ctx = NULL;
int opt;
int rc;
- char *getopt_initial_eim_cfg_file = NULL;
- bool getopt_euicc_memory_reset = false;
+ enum operation operation = OPER_GET_EIM_PACKAGE;
+ char *initial_eim_cfg_file = NULL;
char *getopt_nvstate_path = DEFAULT_NVSTATE_PATH;
struct ipa_buf *nvstate_load = NULL;
struct ipa_buf *nvstate_save = NULL;
bool getopt_one_euicc_pkg_only = false;
- int option_index = 0;
signal(SIGUSR1, sig_usr1);
@@ -159,24 +167,15 @@
ipa_binary_from_hexstr(cfg.tac, sizeof(cfg.tac), DEFAULT_TAC);
cfg.esipa_req_retries = DEFAULT_ESIPA_REQ_RETRIES;
- enum {
- OPT_REFRESHFLAG = 256,
- };
-
- struct option long_options[] = {
- { "refresh-flag", no_argument, NULL, OPT_REFRESHFLAG},
- { NULL, 0, NULL, 0}
- };
-
/* Overwrite configuration values with user defined parameters */
while (1) {
- opt = getopt_long(argc, argv, "ht:e:r:c:f:mn:C:SIEy:a1", long_options, &option_index);
+ opt = getopt(argc, argv, "ht:e:r:c:Rn:C:SIEy:a1");
if (opt == -1)
break;
switch (opt) {
case 'h':
- print_help();
+ print_help(argv[0]);
exit(0);
break;
case 't':
@@ -191,13 +190,7 @@
case 'c':
cfg.euicc_channel = atoi(optarg);
break;
- case 'f':
- getopt_initial_eim_cfg_file = optarg;
- break;
- case 'm':
- getopt_euicc_memory_reset = true;
- break;
- case OPT_REFRESHFLAG:
+ case 'R':
cfg.refresh_flag = true;
break;
case 'n':
@@ -225,9 +218,33 @@
getopt_one_euicc_pkg_only = true;
break;
default:
- printf("unhandled option: %c!\n", opt);
+ printf("unsupported option: %c!\n", opt);
+ opt = -1;
break;
- };
+ }
+ if (opt == -1)
+ exit(opt);
+ }
+
+ if (optind < argc) {
+ if (!strcmp(argv[optind], "add-initial-eim")) {
+ if (optind + 1 >= argc) {
+ printf("operation requires two parameters: %s PATH\n", argv[optind]);
+ exit(-1);
+ }
+ operation = OPER_ADD_INITIAL_EIM;
+ initial_eim_cfg_file = argv[++optind];
+ } else if (!strcmp(argv[optind], "euicc-memory-reset")) {
+ operation = OPER_EUICC_MEMORY_RESET;
+ } else if (!!strcmp(argv[optind], "get-eim-package")) {
+ printf("unknown operation: %s\n", argv[optind]);
+ exit(-1);
+ }
+ optind++;
+ if (optind < argc) {
+ printf("illegal extra operation parameter: %s\n", argv[optind]);
+ exit(-1);
+ }
}
/* Display current config */
@@ -273,15 +290,8 @@
goto leave;
}
- if (getopt_initial_eim_cfg_file) {
- /* Load initial eIM configuration */
- struct ipa_buf *eim_cfg = load_ber_from_file(NULL, getopt_initial_eim_cfg_file);
- ipa_add_init_eim_cfg(ctx, eim_cfg);
- IPA_FREE(eim_cfg);
- } else if (getopt_euicc_memory_reset) {
- /* Perform an eUICC memory reset */
- ipa_euicc_mem_rst(ctx, true, true, true, true, true);
- } else {
+ switch (operation) {
+ case OPER_GET_EIM_PACKAGE:
IPA_LOGP(SMAIN, LINFO, "-----------------------------8<-----------------------------\n");
rc = eim_init(ctx);
if (rc < 0) {
@@ -326,6 +336,17 @@
goto leave;
}
}
+ break;
+ case OPER_ADD_INITIAL_EIM:
+ /* Load initial eIM configuration */
+ struct ipa_buf *eim_cfg = load_ber_from_file(NULL, initial_eim_cfg_file);
+ ipa_add_init_eim_cfg(ctx, eim_cfg);
+ IPA_FREE(eim_cfg);
+ break;
+ case OPER_EUICC_MEMORY_RESET:
+ /* Perform an eUICC memory reset */
+ ipa_euicc_mem_rst(ctx, true, true, true, true, true);
+ break;
}
leave:
To view, visit change 43202. To unsubscribe, or for help writing mail filters, visit settings.