vty: make retrieve-imeisv-early configurable

Prepare for Rhizomatica's subscriber on demand use case, in which the
network access is disabled by default for new subscribers, but the IMEI
is required in the HLR to find out which user has which IMSI. Due to the
network access being disabled, the location update request towards the
HLR fails and the MS gets rejected, so we need to get the IMEI early.

Related: OS#2542, OS#3755
Change-Id: I256224194c3b8caf2b58a88d11dccd32c569201f
diff --git a/src/libmsc/msc_vty.c b/src/libmsc/msc_vty.c
index 2990735..bd369ae 100644
--- a/src/libmsc/msc_vty.c
+++ b/src/libmsc/msc_vty.c
@@ -455,13 +455,24 @@
 }
 
 DEFUN(cfg_msc_check_imei_rqd, cfg_msc_check_imei_rqd_cmd,
-      "check-imei-rqd (0|1)",
+      "check-imei-rqd (0|1|early)",
       "Send each IMEI to the EIR to ask if it is permitted or not. The EIR is implemented as part of OsmoHLR, "
       "and can optionally save the IMEI in the HLR.\n"
       "Do not send IMEIs to the EIR\n"
-      "Send each IMEI to the EIR\n")
+      "Send each IMEI to the EIR\n"
+      "Send each IMEI to the EIR, and do it at the start of the location update. This allows the EIR to receive the"
+      " IMEI, even if the MS would get rejected when the MSC sends the location update request to the HLR.\n")
 {
-	gsmnet->vlr->cfg.check_imei_rqd = atoi(argv[0]) ? true : false;
+	if (strcmp(argv[0], "0") == 0) {
+		gsmnet->vlr->cfg.check_imei_rqd = false;
+		gsmnet->vlr->cfg.retrieve_imeisv_early = false;
+	} else if (strcmp(argv[0], "1") == 0) {
+		gsmnet->vlr->cfg.check_imei_rqd = true;
+		gsmnet->vlr->cfg.retrieve_imeisv_early = false;
+	} else if (strcmp(argv[0], "early") == 0) {
+		gsmnet->vlr->cfg.check_imei_rqd = true;
+		gsmnet->vlr->cfg.retrieve_imeisv_early = true;
+	}
 	return CMD_SUCCESS;
 }
 
@@ -581,9 +592,12 @@
 		vty_out(vty, " auth-tuple-reuse-on-error 1%s",
 			VTY_NEWLINE);
 
-	if (gsmnet->vlr->cfg.check_imei_rqd)
-		vty_out(vty, " check-imei-rqd 1 %s",
-			VTY_NEWLINE);
+	if (gsmnet->vlr->cfg.check_imei_rqd) {
+		if (gsmnet->vlr->cfg.retrieve_imeisv_early)
+			vty_out(vty, " check-imei-rqd early%s", VTY_NEWLINE);
+		else
+			vty_out(vty, " check-imei-rqd 1%s", VTY_NEWLINE);
+	}
 
 	if (gsmnet->paging_response_timer != MSC_PAGING_RESPONSE_TIMER_DEFAULT)
 		vty_out(vty, " paging response-timer %u%s", gsmnet->paging_response_timer, VTY_NEWLINE);
diff --git a/tests/test_nodes.vty b/tests/test_nodes.vty
index 28eaefb..cdb3f8a 100644
--- a/tests/test_nodes.vty
+++ b/tests/test_nodes.vty
@@ -38,7 +38,7 @@
   no assign-tmsi
   auth-tuple-max-reuse-count <-1-2147483647>
   auth-tuple-reuse-on-error (0|1)
-  check-imei-rqd (0|1)
+  check-imei-rqd (0|1|early)
   cs7-instance-a <0-15>
   cs7-instance-iu <0-15>
   paging response-timer (default|<1-65535>)