pespin has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmocom-bb/+/30950 )


Change subject: layer23: Add VTY supports for l23_apps
......................................................................

layer23: Add VTY supports for l23_apps

Initial VTY "boilerplate" code for modem app is already added in
this commit as a showcase what's needed by an app to have the VTY
config file read and VTY interface initialized.

Change-Id: Ife3a3373e5a9c0c8e5959ac714e140e72d6c363a
---
M src/host/layer23/include/osmocom/bb/common/l23_app.h
M src/host/layer23/src/common/main.c
M src/host/layer23/src/misc/Makefile.am
M src/host/layer23/src/modem/app_modem.c
4 files changed, 71 insertions(+), 22 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/50/30950/1

diff --git a/src/host/layer23/include/osmocom/bb/common/l23_app.h 
b/src/host/layer23/include/osmocom/bb/common/l23_app.h
index cb68d43..633c764 100644
--- a/src/host/layer23/include/osmocom/bb/common/l23_app.h
+++ b/src/host/layer23/include/osmocom/bb/common/l23_app.h
@@ -2,6 +2,7 @@
 #define _L23_APP_H

 struct option;
+struct vty_app_info;

 /* Options supported by the l23 app */
 enum {
@@ -10,7 +11,6 @@
        L23_OPT_TAP     = 4,
        L23_OPT_VTY     = 8,
        L23_OPT_DBG     = 16,
-       L23_OPT_VTYIP   = 32,
 };

 extern void *l23_ctx;
@@ -29,12 +29,14 @@
 struct l23_app_info {
        const char *copyright;
        const char *contribution;
+       struct vty_app_info *vty_info; /* L23_OPT_VTY */

        char *getopt_string;
        int (*cfg_supported)();
        int (*cfg_print_help)();
        int (*cfg_getopt_opt)(struct option **options);
        int (*cfg_handle_opt)(int c,const char *optarg);
+       int (*vty_init)(void);
 };

 extern struct l23_app_info *l23_app_info();
diff --git a/src/host/layer23/src/common/main.c 
b/src/host/layer23/src/common/main.c
index dcf557f..28d2406 100644
--- a/src/host/layer23/src/common/main.c
+++ b/src/host/layer23/src/common/main.c
@@ -33,6 +33,10 @@
 #include <osmocom/core/gsmtap_util.h>
 #include <osmocom/core/gsmtap.h>
 #include <osmocom/core/utils.h>
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/logging.h>
+#include <osmocom/vty/telnet_interface.h>
+#include <osmocom/vty/ports.h>
 
 #include <arpa/inet.h>

@@ -51,9 +55,8 @@
 struct llist_head ms_list;
 static struct osmocom_ms *ms = NULL;
 static char *gsmtap_ip = NULL;
-static char *vty_ip = "127.0.0.1";
+static char *config_file = NULL;

-unsigned short vty_port = 4247;
 int (*l23_app_start)(struct osmocom_ms *ms) = NULL;
 int (*l23_app_work)(struct osmocom_ms *ms) = NULL;
 int (*l23_app_exit)(struct osmocom_ms *ms) = NULL;
@@ -97,16 +100,11 @@
                printf("  -i --gsmtap-ip        The destination IP used for 
GSMTAP.\n");

        if (options & L23_OPT_VTY)
-               printf("  -v --vty-port         The VTY port number to telnet "
-                       "to. (default %u)\n", vty_port);
+               printf("  -c --config-file      The path to the VTY 
configuration file.");

        if (options & L23_OPT_DBG)
                printf("  -d --debug            Change debug flags.\n");

-       if (options & L23_OPT_VTYIP)
-               printf("  -u --vty-ip           The VTY IP to bind telnet to. "
-                       "(default %s)\n", vty_ip);
-
        if (app && app->cfg_print_help)
                app->cfg_print_help();
 }
@@ -123,14 +121,13 @@
                {"sap", 1, 0, 'S'},
                {"arfcn", 1, 0, 'a'},
                {"gsmtap-ip", 1, 0, 'i'},
-               {"vty-ip", 1, 0, 'u'},
-               {"vty-port", 1, 0, 'v'},
+               {"config-file", 1, 0, 'c'},
                {"debug", 1, 0, 'd'},
        };


        app = l23_app_info();
-       *opt = talloc_asprintf(l23_ctx, "hs:S:a:i:v:d:u:%s",
+       *opt = talloc_asprintf(l23_ctx, "hs:S:a:i:c:d:%s",
                               app && app->getopt_string ? app->getopt_string : 
"");

        len = ARRAY_SIZE(long_options);
@@ -143,9 +140,8 @@
                memcpy(*option + len, app_opp, app_len * sizeof(struct option));
 }

-static void handle_options(int argc, char **argv)
+static void handle_options(int argc, char **argv, struct l23_app_info *app)
 {
-       struct l23_app_info *app = l23_app_info();
        struct option *long_options;
        char *opt;

@@ -177,11 +173,8 @@
                case 'i':
                        gsmtap_ip = optarg;
                        break;
-               case 'u':
-                       vty_ip = optarg;
-                       break;
-               case 'v':
-                       vty_port = atoi(optarg);
+               case 'c':
+                       config_file = optarg;
                        break;
                case 'd':
                        log_parse_category_mask(osmo_stderr_target, optarg);
@@ -221,9 +214,17 @@
               app && app->contribution ? app->contribution : "");
 }

+static struct vty_app_info l23_vty_info = {
+       .name = "OsmocomBB",
+       .version = PACKAGE_VERSION,
+       //.go_parent_cb = ms_vty_go_parent,
+};
+
 int main(int argc, char **argv)
 {
        int rc;
+       struct l23_app_info *app;
+       unsigned int app_supp_opt = 0x00;

        INIT_LLIST_HEAD(&ms_list);

@@ -257,14 +258,42 @@
        lapdm_channel_init(&ms->lapdm_channel, LAPDM_MODE_MS);
        lapdm_channel_set_l1(&ms->lapdm_channel, l1ctl_ph_prim_cb, ms);

-       handle_options(argc, argv);
-
        rc = l23_app_init(ms);
        if (rc < 0) {
                fprintf(stderr, "Failed during l23_app_init()\n");
                exit(1);
        }

+       app = l23_app_info();
+       if (app && app->cfg_supported != NULL)
+               app_supp_opt = app->cfg_supported();
+
+       handle_options(argc, argv, app);
+
+       if (app_supp_opt & L23_OPT_VTY) {
+               OSMO_ASSERT(app->vty_info);
+               app->vty_info->tall_ctx = l23_ctx;
+               vty_init(app->vty_info ? : &l23_vty_info);
+               logging_vty_add_cmds();
+               if (app->vty_init)
+                       app->vty_init();
+               if (config_file) {
+                       rc = vty_read_config_file(config_file, NULL);
+                       if (rc < 0) {
+                               LOGP(DLGLOBAL, LOGL_FATAL,
+                                    "Failed to parse the configuration file 
'%s'\n", config_file);
+                               exit(1);
+                       }
+                       LOGP(DLGLOBAL, LOGL_INFO, "Using configuration from 
'%s'\n", config_file);
+               }
+               rc = telnet_init_default(l23_ctx, NULL, OSMO_VTY_PORT_BB);
+               if (rc < 0) {
+                       LOGP(DMOB, LOGL_FATAL, "Cannot init VTY on %s port %u: 
%s\n",
+                               vty_get_bind_addr(), OSMO_VTY_PORT_BB, 
strerror(errno));
+                       return rc;
+               }
+       }
+
        rc = layer2_open(ms, layer2_socket_path);
        if (rc < 0) {
                fprintf(stderr, "Failed during layer2_open()\n");
@@ -299,5 +328,7 @@
                osmo_select_main(0);
        }

+       talloc_free(config_file);
+
        return 0;
 }
diff --git a/src/host/layer23/src/misc/Makefile.am 
b/src/host/layer23/src/misc/Makefile.am
index 869fe7b..358be74 100644
--- a/src/host/layer23/src/misc/Makefile.am
+++ b/src/host/layer23/src/misc/Makefile.am
@@ -6,6 +6,7 @@
 AM_CFLAGS = \
        -Wall \
        $(LIBOSMOCORE_CFLAGS) \
+       $(LIBOSMOVTY_CFLAGS) \
        $(LIBOSMOGSM_CFLAGS) \
        $(LIBOSMOGPRSRLCMAC_CFLAGS) \
        $(LIBGPS_CFLAGS) \
@@ -14,6 +15,7 @@
 LDADD = \
        $(top_builddir)/src/common/liblayer23.a \
        $(LIBOSMOCORE_LIBS) \
+       $(LIBOSMOVTY_LIBS) \
        $(LIBOSMOGSM_LIBS) \
        $(LIBOSMOCODEC_LIBS) \
        $(LIBOSMOGPRSRLCMAC_LIBS) \
diff --git a/src/host/layer23/src/modem/app_modem.c 
b/src/host/layer23/src/modem/app_modem.c
index df3b3fc..97282af 100644
--- a/src/host/layer23/src/modem/app_modem.c
+++ b/src/host/layer23/src/modem/app_modem.c
@@ -26,6 +26,7 @@
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/signal.h>
 #include <osmocom/core/application.h>
+#include <osmocom/vty/vty.h>

 #include <osmocom/gsm/rsl.h>
 #include <osmocom/gsm/tlv.h>
@@ -379,12 +380,25 @@

 static int l23_cfg_supported(void)
 {
-       return L23_OPT_ARFCN | L23_OPT_TAP | L23_OPT_DBG;
+       return L23_OPT_ARFCN | L23_OPT_TAP | L23_OPT_VTY | L23_OPT_DBG;
 }

+static int l23_vty_init(void)
+{
+       /* TODO: add sample specific vty nodes/cmds */
+       return 0;
+}
+
+static struct vty_app_info _modem_vty_info = {
+       .name = "modem",
+       .version = PACKAGE_VERSION,
+};
+
 static struct l23_app_info info = {
        .copyright = "Copyright (C) 2022 by sysmocom - s.m.f.c. GmbH 
<i...@sysmocom.de>\n",
        .cfg_supported = &l23_cfg_supported,
+       .vty_info = &_modem_vty_info,
+       .vty_init = l23_vty_init,
 };

 struct l23_app_info *l23_app_info(void)

--
To view, visit https://gerrit.osmocom.org/c/osmocom-bb/+/30950
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-Change-Id: Ife3a3373e5a9c0c8e5959ac714e140e72d6c363a
Gerrit-Change-Number: 30950
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pes...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to