diff simtool/fplmn.c @ 103:9b2cb2b9c910

fc-simtool fplmn-* commands implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 18 Feb 2021 00:17:14 +0000
parents simtool/plmnsel.c@9ce95d9c5c34
children 8a27f45bc1e6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/fplmn.c	Thu Feb 18 00:17:14 2021 +0000
@@ -0,0 +1,143 @@
+/*
+ * This module implements commands for working with EF_FPLMN.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "simresp.h"
+#include "curfile.h"
+#include "file_id.h"
+
+static
+select_ef_fplmn()
+{
+	int rc;
+
+	rc = select_op(DF_GSM);
+	if (rc < 0)
+		return(rc);
+	rc = select_op(EF_FPLMN);
+	if (rc < 0)
+		return(rc);
+	rc = parse_ef_select_response();
+	if (rc < 0)
+		return(rc);
+	if (curfile_structure != 0x00) {
+		fprintf(stderr, "error: EF_FPLMN is not transparent\n");
+		return(-1);
+	}
+	if (curfile_total_size != 12) {
+		fprintf(stderr,
+			"error: EF_FPLMN size does not equal 12 bytes\n");
+		return(-1);
+	}
+	return(0);
+}
+
+cmd_fplmn_dump(argc, argv)
+	char **argv;
+{
+	int rc;
+	u_char *dp;
+	char ascbuf[8];
+	unsigned idx;
+
+	rc = select_ef_fplmn();
+	if (rc < 0)
+		return(rc);
+	rc = readbin_op(0, 12);
+	if (rc < 0)
+		return(rc);
+	dp = sim_resp_data;
+	for (idx = 0; idx < 4; idx++, dp += 3) {
+		if (idx)
+			putchar(' ');
+		if (dp[0] == 0xFF && dp[1] == 0xFF && dp[2] == 0xFF)
+			fputs("-blank-", stdout);
+		else {
+			decode_plmn_3bytes(dp, ascbuf, 1);
+			fputs(ascbuf, stdout);
+		}
+	}
+	putchar('\n');
+	return(0);
+}
+
+cmd_fplmn_write(argc, argv)
+	char **argv;
+{
+	int rc;
+	unsigned idx;
+	u_char rec[3];
+
+	rc = select_ef_fplmn();
+	if (rc < 0)
+		return(rc);
+	idx = strtoul(argv[1], 0, 0);
+	if (idx >= 4) {
+		fprintf(stderr, "error: specified index is out of range\n");
+		return(-1);
+	}
+	rc = encode_plmn_3bytes(argv[2], rec);
+	if (rc < 0)
+		return(rc);
+	return update_bin_op(idx * 3, rec, 3);
+}
+
+cmd_fplmn_erase(argc, argv)
+	char **argv;
+{
+	int rc;
+	unsigned idx, start, end;
+	u_char rec[3];
+
+	rc = select_ef_fplmn();
+	if (rc < 0)
+		return(rc);
+	start = strtoul(argv[1], 0, 0);
+	if (start >= 4) {
+		fprintf(stderr,
+			"error: specified starting index is out of range\n");
+		return(-1);
+	}
+	if (!argv[2])
+		end = start;
+	else if (!strcmp(argv[2], "end"))
+		end = 3;
+	else {
+		end = strtoul(argv[1], 0, 0);
+		if (end >= 4) {
+			fprintf(stderr,
+			"error: specified ending index is out of range\n");
+			return(-1);
+		}
+		if (start > end) {
+			fprintf(stderr,
+				"error: reverse index range specified\n");
+			return(-1);
+		}
+	}
+	memset(rec, 0xFF, 3);
+	for (idx = start; idx <= end; idx++) {
+		rc = update_bin_op(idx * 3, rec, 3);
+		if (rc < 0)
+			return(rc);
+	}
+	return(0);
+}
+
+cmd_fplmn_erase_all(argc, argv)
+	char **argv;
+{
+	int rc;
+	u_char ffbuf[12];
+
+	rc = select_ef_fplmn();
+	if (rc < 0)
+		return(rc);
+	memset(ffbuf, 0xFF, 12);
+	return update_bin_op(0, ffbuf, 12);
+}