abis: Activate the newest available installed on the BTS

Select thew newest software from all available file versions.
diff --git a/openbsc/include/openbsc/abis_nm.h b/openbsc/include/openbsc/abis_nm.h
index 51f11e9..4d6f866 100644
--- a/openbsc/include/openbsc/abis_nm.h
+++ b/openbsc/include/openbsc/abis_nm.h
@@ -181,5 +181,6 @@
 
 int abis_nm_parse_sw_config(const uint8_t *data, const size_t len,
 			struct abis_nm_sw_descr *res, const int res_len);
+int abis_nm_select_newest_sw(const struct abis_nm_sw_descr *sw, const size_t len);
 
 #endif /* _NM_H */
diff --git a/openbsc/src/libbsc/abis_nm.c b/openbsc/src/libbsc/abis_nm.c
index 7485a6c..b74e772 100644
--- a/openbsc/src/libbsc/abis_nm.c
+++ b/openbsc/src/libbsc/abis_nm.c
@@ -421,6 +421,22 @@
 	return desc_pos;
 }
 
+int abis_nm_select_newest_sw(const struct abis_nm_sw_descr *sw_descr,
+				const size_t size)
+{
+	int res = 0;
+	int i;
+
+	for (i = 1; i < size; ++i) {
+		if (memcmp(sw_descr[res].file_ver, sw_descr[i].file_ver,
+			OSMO_MIN(sw_descr[i].file_ver_len, sw_descr[res].file_ver_len)) < 0) {
+			res = i;
+		}
+	}
+
+	return res;
+}
+
 static int abis_nm_rx_sw_act_req(struct msgb *mb)
 {
 	struct abis_om_hdr *oh = msgb_l2(mb);
@@ -428,8 +444,8 @@
 	struct e1inp_sign_link *sign_link = mb->dst;
 	struct tlv_parsed tp;
 	const uint8_t *sw_config;
-	int ret, sw_config_len;
-	struct abis_nm_sw_descr sw_descr[1];
+	int ret, sw_config_len, len;
+	struct abis_nm_sw_descr sw_descr[5];
 
 	abis_nm_debugp_foh(DNM, foh);
 
@@ -460,18 +476,21 @@
 	}
 
 	/* Parse up to two sw descriptions from the data */
-	ret = abis_nm_parse_sw_config(sw_config, sw_config_len,
+	len = abis_nm_parse_sw_config(sw_config, sw_config_len,
 				&sw_descr[0], ARRAY_SIZE(sw_descr));
-	if (ret <= 0) {
+	if (len <= 0) {
 		LOGP(DNM, LOGL_ERROR, "Failed to parse SW Config.\n");
 		return -EINVAL;
 	}
 
+	ret = abis_nm_select_newest_sw(&sw_descr[0], len);
+	DEBUGP(DNM, "Selected sw description %d of %d\n", ret, len);
+
 	return ipacc_sw_activate(sign_link->trx->bts, foh->obj_class,
 				 foh->obj_inst.bts_nr,
 				 foh->obj_inst.trx_nr,
 				 foh->obj_inst.ts_nr,
-				 sw_descr[0].start, sw_descr[0].len);
+				 sw_descr[ret].start, sw_descr[ret].len);
 }
 
 /* Receive a CHANGE_ADM_STATE_ACK, parse the TLV and update local state */
diff --git a/openbsc/tests/abis/abis_test.c b/openbsc/tests/abis/abis_test.c
index a2f6a05..4942a1e 100644
--- a/openbsc/tests/abis/abis_test.c
+++ b/openbsc/tests/abis/abis_test.c
@@ -38,6 +38,16 @@
 	66, 18, 0, 3, 9, 7, 5, 19, 0, 3, 6, 7, 8,
 };
 
+static const uint8_t load_config[] = {
+	0x42, 0x12, 0x00, 0x08, 0x31, 0x36, 0x38, 0x64,
+	0x34, 0x37, 0x32, 0x00, 0x13, 0x00, 0x0b, 0x76,
+	0x32, 0x30, 0x30, 0x62, 0x31, 0x34, 0x33, 0x64,
+	0x30, 0x00, 0x42, 0x12, 0x00, 0x08, 0x31, 0x36,
+	0x38, 0x64, 0x34, 0x37, 0x32, 0x00, 0x13, 0x00,
+	0x0b, 0x76, 0x32, 0x30, 0x30, 0x62, 0x31, 0x34,
+	0x33, 0x64, 0x31, 0x00
+};
+
 static void test_simple_sw_config(void)
 {
 	struct abis_nm_sw_descr descr[1];
@@ -107,12 +117,53 @@
 	printf("file_ver: %s\n", osmo_hexdump(descr[1].file_ver, descr[1].file_ver_len));
 }
 
+static void test_sw_selection(void)
+{
+	struct abis_nm_sw_descr descr[8], tmp;
+	int rc, pos;
+
+	rc = abis_nm_parse_sw_config(load_config, ARRAY_SIZE(load_config),
+				&descr[0], ARRAY_SIZE(descr));
+	if (rc != 2) {
+		printf("FAILED to parse the File Id/File version\n");
+		abort();
+	}
+
+	printf("Start: %u len: %zu\n", descr[0].start - dual_config, descr[0].len);
+	printf("file_id:  %s\n", osmo_hexdump(descr[0].file_id, descr[0].file_id_len));
+	printf("file_ver: %s\n", osmo_hexdump(descr[0].file_ver, descr[0].file_ver_len));
+
+	printf("Start: %u len: %zu\n", descr[1].start - dual_config, descr[1].len);
+	printf("file_id:  %s\n", osmo_hexdump(descr[1].file_id, descr[1].file_id_len));
+	printf("file_ver: %s\n", osmo_hexdump(descr[1].file_ver, descr[1].file_ver_len));
+
+	/* start */
+	pos = abis_nm_select_newest_sw(descr, rc);
+	if (pos != 1) {
+		printf("Selected the wrong version: %d\n", pos);
+		abort();
+	}
+	printf("SELECTED: %d\n", pos);
+
+	/* shuffle */
+	tmp = descr[0];
+	descr[0] = descr[1];
+	descr[1] = tmp;
+	pos = abis_nm_select_newest_sw(descr, rc);
+	if (pos != 0) {
+		printf("Selected the wrong version: %d\n", pos);
+		abort();
+	}
+	printf("SELECTED: %d\n", pos);
+}
+
 int main(int argc, char **argv)
 {
 	osmo_init_logging(&log_info);
 	test_simple_sw_config();
 	test_simple_sw_short();
 	test_dual_sw_config();
+	test_sw_selection();
 
 	return EXIT_SUCCESS;
 }
diff --git a/openbsc/tests/abis/abis_test.ok b/openbsc/tests/abis/abis_test.ok
index 6401988..ba1da33 100644
--- a/openbsc/tests/abis/abis_test.ok
+++ b/openbsc/tests/abis/abis_test.ok
@@ -1,9 +1,17 @@
 Start: 0 len: 13
-file_id:  01 02 03
-file_ver: 03 04 05
-Start: 13 len: 26
-file_id:  01 02 03
-file_ver: 03 04 05
-Start: 26 len: 13
-file_id:  09 07 05
-file_ver: 06 07 08
+file_id:  01 02 03 
+file_ver: 03 04 05 
+Start: 0 len: 13
+file_id:  01 02 03 
+file_ver: 03 04 05 
+Start: 13 len: 13
+file_id:  09 07 05 
+file_ver: 06 07 08 
+Start: 51 len: 26
+file_id:  31 36 38 64 34 37 32 00 
+file_ver: 76 32 30 30 62 31 34 33 64 30 00 
+Start: 77 len: 26
+file_id:  31 36 38 64 34 37 32 00 
+file_ver: 76 32 30 30 62 31 34 33 64 31 00 
+SELECTED: 1
+SELECTED: 0