aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmo-bts-omldummy/main.c
diff options
context:
space:
mode:
authorNeels Hofmeyr <neels@hofmeyr.de>2021-04-02 23:15:43 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2021-04-22 20:45:02 +0200
commit708fca343e1430f1d429025d357fc4c1df74abd5 (patch)
treeb1600d8b4fd02d1810d0d589ecf572a5cfd9bc8f /src/osmo-bts-omldummy/main.c
parent823cd529649b667af20bfa4e383255a8ed5931f1 (diff)
omldummy: introduce using getopt_long
Prepare for adding the --features cmdline arg following in a subsequent patch. Related: SYS#4895 Change-Id: I72ccf65ba894e87ee7b0f6bed879f94728f34ccc
Diffstat (limited to 'src/osmo-bts-omldummy/main.c')
-rw-r--r--src/osmo-bts-omldummy/main.c80
1 files changed, 68 insertions, 12 deletions
diff --git a/src/osmo-bts-omldummy/main.c b/src/osmo-bts-omldummy/main.c
index 22d8758d..183a5e5b 100644
--- a/src/osmo-bts-omldummy/main.c
+++ b/src/osmo-bts-omldummy/main.c
@@ -1,3 +1,9 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define _GNU_SOURCE
+#include <getopt.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/application.h>
@@ -6,6 +12,64 @@
#include <osmo-bts/bts.h>
#include <osmo-bts/oml.h>
+static void print_usage(const char *prog_name)
+{
+ printf("Usage: %s [-h] dst_host site_id [trx_num]\n", prog_name);
+}
+
+static void print_help(const char *prog_name)
+{
+ print_usage(prog_name);
+ printf(" -h --help This text.\n");
+}
+
+struct {
+ char *dst_host;
+ int site_id;
+ int trx_num;
+} cmdline = {
+ .trx_num = 8,
+};
+
+void parse_cmdline(int argc, char **argv)
+{
+ while (1) {
+ int option_index = 0, c;
+ static struct option long_options[] = {
+ {"help", 0, 0, 'h'},
+ {0}
+ };
+
+ c = getopt_long(argc, argv, "h", long_options, &option_index);
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'h':
+ print_help(argv[0]);
+ exit(0);
+ default:
+ /* catch unknown options *as well as* missing arguments. */
+ fprintf(stderr, "Error in command line options. Exiting.\n");
+ exit(-1);
+ }
+ }
+
+ if (optind + 2 > argc) {
+ print_usage(argv[0]);
+ exit(1);
+ }
+
+ cmdline.dst_host = argv[optind];
+ cmdline.site_id = atoi(argv[optind + 1]);
+ if (optind + 2 < argc)
+ cmdline.trx_num = atoi(argv[optind + 2]);
+
+ if (optind + 3 < argc) {
+ print_usage(argv[0]);
+ exit(1);
+ }
+}
int main(int argc, char **argv)
{
@@ -14,14 +78,7 @@ int main(int argc, char **argv)
struct e1inp_line *line;
int i;
- if (argc < 3) {
- fprintf(stderr, "Usage: %s dst_host site_id [trx_num]\n", argv[0]);
- return 1;
- }
-
- char *dst_host = argv[1];
- int site_id = atoi(argv[2]);
- int trx_num = argc > 3 ? atoi(argv[3]) : 8;
+ parse_cmdline(argc, argv);
tall_bts_ctx = talloc_named_const(NULL, 1, "OsmoBTS context");
msgb_talloc_ctx_init(tall_bts_ctx, 10*1024);
@@ -31,11 +88,11 @@ int main(int argc, char **argv)
bts = gsm_bts_alloc(tall_bts_ctx, 0);
if (!bts)
exit(1);
- bts->ip_access.site_id = site_id;
+ bts->ip_access.site_id = cmdline.site_id;
bts->ip_access.bts_id = 0;
/* Additional TRXs */
- for (i = 1; i < trx_num; i++) {
+ for (i = 1; i < cmdline.trx_num; i++) {
trx = gsm_bts_trx_alloc(bts);
if (!trx)
exit(1);
@@ -46,8 +103,7 @@ int main(int argc, char **argv)
//btsb = bts_role_bts(bts);
abis_init(bts);
-
- line = abis_open(bts, dst_host, "OMLdummy");
+ line = abis_open(bts, cmdline.dst_host, "OMLdummy");
if (!line)
exit(2);