sms: Give smsc its own VTY config node

The pre-historic sms_queue code used to have very strange aspects,
such as having some parameters (max-failure, max-pending) which could
only be sent from the 'enable' node, but not from a config file.

Before adding more configuration parameters, let's clean this up by
introducing a proper VTY config node for the 'smsc'; move the existing
config commands there and add new ones for max-failure and max-pending.

As the sms_queue data structure is only allocated after the config file
parsing happens, we are introducing a new 'sms_queue_config' data
structure.  This encapsulates the public readable/writable config
parameters.

Change-Id: Ie8e0ab1a9f979337ff06544b9ab3820954d9804a
diff --git a/src/libmsc/smsc_vty.c b/src/libmsc/smsc_vty.c
new file mode 100644
index 0000000..cdb81c6
--- /dev/null
+++ b/src/libmsc/smsc_vty.c
@@ -0,0 +1,168 @@
+/* SMSC interface to VTY */
+/* (C) 2016-2018 by sysmocom s.m.f.c. GmbH <info@sysmocom.de>
+ * Based on OpenBSC interface to quagga VTY (libmsc/vty_interface_layer3.c)
+ * (C) 2009-2022 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2009-2011 by Holger Hans Peter Freyther
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "config.h"
+
+#include <osmocom/vty/command.h>
+#include <osmocom/vty/logging.h>
+#include <osmocom/vty/misc.h>
+
+#include <osmocom/msc/vty.h>
+#include <osmocom/msc/gsm_data.h>
+#include <osmocom/msc/sms_queue.h>
+
+
+static struct gsm_network *gsmnet;
+static struct sms_queue_config *smqcfg;
+
+/***********************************************************************
+ * SMSC Config Node
+ ***********************************************************************/
+
+static struct cmd_node smsc_node = {
+	SMSC_NODE,
+	"%s(config-smsc)# ",
+	1,
+};
+
+DEFUN(cfg_smsc, cfg_smsc_cmd,
+      "smsc", "Configure SMSC options")
+{
+	vty->node = SMSC_NODE;
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sms_database, cfg_sms_database_cmd,
+	"database PATH",
+	"Set the path to the MSC-SMS database file\n"
+	"Relative or absolute file system path to the database file (default is '" SMS_DEFAULT_DB_FILE_PATH "')\n")
+{
+	osmo_talloc_replace_string(smqcfg, &smqcfg->db_file_path, argv[0]);
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sms_queue_max, cfg_sms_queue_max_cmd,
+      "queue max-pending <1-500>",
+      "SMS Queue\n" "SMS to deliver in parallel\n" "Amount\n")
+{
+	smqcfg->max_pending = atoi(argv[0]);
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_sms_queue_fail, cfg_sms_queue_fail_cmd,
+      "queue max-failure <1-500>",
+      "SMS Queue\n" "Maximum number of delivery failures before giving up\n" "Amount\n")
+{
+	smqcfg->max_fail = atoi(argv[0]);
+	return CMD_SUCCESS;
+}
+
+/***********************************************************************
+ * View / Enable Node
+ ***********************************************************************/
+
+DEFUN(show_smsqueue,
+      show_smsqueue_cmd,
+      "show sms-queue",
+      SHOW_STR "Display SMSqueue statistics\n")
+{
+	sms_queue_stats(gsmnet->sms_queue, vty);
+	return CMD_SUCCESS;
+}
+
+DEFUN(smsqueue_trigger,
+      smsqueue_trigger_cmd,
+      "sms-queue trigger",
+      "SMS Queue\n" "Trigger sending messages\n")
+{
+	sms_queue_trigger(gsmnet->sms_queue);
+	return CMD_SUCCESS;
+}
+
+DEFUN(smsqueue_max,
+      smsqueue_max_cmd,
+      "sms-queue max-pending <1-500>",
+      "SMS Queue\n" "SMS to deliver in parallel\n" "Amount\n")
+{
+	int max_pending = atoi(argv[0]);
+	vty_out(vty, "%% SMSqueue old max: %d new: %d%s",
+		smqcfg->max_pending, max_pending, VTY_NEWLINE);
+	smqcfg->max_pending = max_pending;
+	return CMD_SUCCESS;
+}
+
+DEFUN(smsqueue_clear,
+      smsqueue_clear_cmd,
+      "sms-queue clear",
+      "SMS Queue\n" "Clear the queue of pending SMS\n")
+{
+	sms_queue_clear(gsmnet->sms_queue);
+	return CMD_SUCCESS;
+}
+
+DEFUN(smsqueue_fail,
+      smsqueue_fail_cmd,
+      "sms-queue max-failure <1-500>",
+      "SMS Queue\n" "Maximum amount of delivery failures\n" "Amount\n")
+{
+	int max_fail = atoi(argv[0]);
+	vty_out(vty, "%% SMSqueue max failure old: %d new: %d%s",
+		smqcfg->max_fail, max_fail, VTY_NEWLINE);
+	smqcfg->max_fail = max_fail;
+	return CMD_SUCCESS;
+}
+
+static int config_write_smsc(struct vty *vty)
+{
+	vty_out(vty, "smsc%s", VTY_NEWLINE);
+
+	if (smqcfg->db_file_path && strcmp(smqcfg->db_file_path, SMS_DEFAULT_DB_FILE_PATH))
+		vty_out(vty, " database %s%s", smqcfg->db_file_path, VTY_NEWLINE);
+
+	vty_out(vty, " queue max-pending %u%s", smqcfg->max_pending, VTY_NEWLINE);
+	vty_out(vty, " queue max-failure %u%s", smqcfg->max_fail, VTY_NEWLINE);
+
+	return 0;
+}
+
+void smsc_vty_init(struct gsm_network *msc_network)
+{
+	OSMO_ASSERT(gsmnet == NULL);
+	gsmnet = msc_network;
+	smqcfg = msc_network->sms_queue_cfg;
+
+	/* config node */
+	install_element(CONFIG_NODE, &cfg_smsc_cmd);
+	install_node(&smsc_node, config_write_smsc);
+	install_element(SMSC_NODE, &cfg_sms_database_cmd);
+	install_element(SMSC_NODE, &cfg_sms_queue_max_cmd);
+	install_element(SMSC_NODE, &cfg_sms_queue_fail_cmd);
+
+	/* enable node */
+	install_element(ENABLE_NODE, &smsqueue_trigger_cmd);
+	install_element(ENABLE_NODE, &smsqueue_max_cmd);
+	install_element(ENABLE_NODE, &smsqueue_clear_cmd);
+	install_element(ENABLE_NODE, &smsqueue_fail_cmd);
+
+	/* view / enable node */
+	install_element_ve(&show_smsqueue_cmd);
+}