bsc/nitb: Allow to set the GPRS mode through the ctrl command

Create a control command to read and modify the gprs mode. Use
the get_string_value to indicate if the value was found or not.
This is useful for the ctrl interface where I didn't want to
replicate "none", "gprs" and "egprs". Share code to verify that
a BTS supports the mode.

Related: SYS#591
diff --git a/openbsc/include/openbsc/gsm_data.h b/openbsc/include/openbsc/gsm_data.h
index f78f244..bf1ac7c 100644
--- a/openbsc/include/openbsc/gsm_data.h
+++ b/openbsc/include/openbsc/gsm_data.h
@@ -403,8 +403,9 @@
 enum rrlp_mode rrlp_mode_parse(const char *arg);
 const char *rrlp_mode_name(enum rrlp_mode mode);
 
-enum bts_gprs_mode bts_gprs_mode_parse(const char *arg);
+enum bts_gprs_mode bts_gprs_mode_parse(const char *arg, int *valid);
 const char *bts_gprs_mode_name(enum bts_gprs_mode mode);
+int bts_gprs_mode_is_compat(struct gsm_bts *bts, enum bts_gprs_mode mode);
 
 int gsm48_ra_id_by_bts(uint8_t *buf, struct gsm_bts *bts);
 void gprs_ra_id_by_bts(struct gprs_ra_id *raid, struct gsm_bts *bts);
diff --git a/openbsc/src/libbsc/bsc_ctrl_commands.c b/openbsc/src/libbsc/bsc_ctrl_commands.c
index aea4a9b..368d0e7 100644
--- a/openbsc/src/libbsc/bsc_ctrl_commands.c
+++ b/openbsc/src/libbsc/bsc_ctrl_commands.c
@@ -1,6 +1,6 @@
 /*
- * (C) 2013-2014 by Holger Hans Peter Freyther
- * (C) 2013-2014 by sysmocom s.f.m.c. GmbH
+ * (C) 2013-2015 by Holger Hans Peter Freyther
+ * (C) 2013-2015 by sysmocom s.f.m.c. GmbH
  *
  * All Rights Reserved
  *
@@ -299,6 +299,45 @@
 }
 CTRL_CMD_DEFINE(bts_oml_conn, "oml-connection-state");
 
+static int verify_bts_gprs_mode(struct ctrl_cmd *cmd, const char *value, void *_data)
+{
+	int valid;
+	enum bts_gprs_mode mode;
+	struct gsm_bts *bts = cmd->node;
+
+	mode = bts_gprs_mode_parse(value, &valid);
+	if (!valid) {
+		cmd->reply = "Mode is not known";
+		return 1;
+	}
+
+	if (!bts_gprs_mode_is_compat(bts, mode)) {
+		cmd->reply = "bts does not support this mode";
+		return 1;
+	}
+
+	return 0;
+}
+
+static int get_bts_gprs_mode(struct ctrl_cmd *cmd, void *data)
+{
+	struct gsm_bts *bts = cmd->node;
+
+	cmd->reply = talloc_strdup(cmd, bts_gprs_mode_name(bts->gprs.mode));
+	return CTRL_CMD_REPLY;
+}
+
+static int set_bts_gprs_mode(struct ctrl_cmd *cmd, void *data)
+{
+	struct gsm_bts *bts = cmd->node;
+
+	bts->gprs.mode = bts_gprs_mode_parse(cmd->value, NULL);
+	return get_bts_gprs_mode(cmd, data);
+}
+
+CTRL_CMD_DEFINE(bts_gprs_mode, "gprs-mode");
+
+
 /* TRX related commands below here */
 CTRL_HELPER_GET_INT(trx_max_power, struct gsm_bts_trx, max_power_red);
 static int verify_trx_max_power(struct ctrl_cmd *cmd, const char *value, void *_data)
@@ -356,6 +395,7 @@
 	rc |= ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_si);
 	rc |= ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_chan_load);
 	rc |= ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_oml_conn);
+	rc |= ctrl_cmd_install(CTRL_NODE_BTS, &cmd_bts_gprs_mode);
 
 	rc |= ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_max_power);
 	rc |= ctrl_cmd_install(CTRL_NODE_TRX, &cmd_trx_arfcn);
diff --git a/openbsc/src/libbsc/bsc_vty.c b/openbsc/src/libbsc/bsc_vty.c
index 2857494..588be26 100644
--- a/openbsc/src/libbsc/bsc_vty.c
+++ b/openbsc/src/libbsc/bsc_vty.c
@@ -2500,16 +2500,9 @@
 	"EGPRS (EDGE) Enabled on this BTS\n")
 {
 	struct gsm_bts *bts = vty->index;
-	enum bts_gprs_mode mode = bts_gprs_mode_parse(argv[0]);
+	enum bts_gprs_mode mode = bts_gprs_mode_parse(argv[0], NULL);
 
-	if (mode != BTS_GPRS_NONE &&
-	    !gsm_bts_has_feature(bts, BTS_FEAT_GPRS)) {
-		vty_out(vty, "This BTS type does not support %s%s", argv[0],
-			VTY_NEWLINE);
-		return CMD_WARNING;
-	}
-	if (mode == BTS_GPRS_EGPRS &&
-	    !gsm_bts_has_feature(bts, BTS_FEAT_EGPRS)) {
+	if (!bts_gprs_mode_is_compat(bts, mode)) {
 		vty_out(vty, "This BTS type does not support %s%s", argv[0],
 			VTY_NEWLINE);
 		return CMD_WARNING;
diff --git a/openbsc/src/libcommon/gsm_data.c b/openbsc/src/libcommon/gsm_data.c
index 7cb1d38..ef98f7d 100644
--- a/openbsc/src/libcommon/gsm_data.c
+++ b/openbsc/src/libcommon/gsm_data.c
@@ -199,9 +199,14 @@
 	{ 0,			NULL }
 };
 
-enum bts_gprs_mode bts_gprs_mode_parse(const char *arg)
+enum bts_gprs_mode bts_gprs_mode_parse(const char *arg, int *valid)
 {
-	return get_string_value(bts_gprs_mode_names, arg);
+	int rc;
+
+	rc = get_string_value(bts_gprs_mode_names, arg);
+	if (valid)
+		*valid = rc != -EINVAL;
+	return rc;
 }
 
 const char *bts_gprs_mode_name(enum bts_gprs_mode mode)
@@ -209,6 +214,20 @@
 	return get_value_string(bts_gprs_mode_names, mode);
 }
 
+int bts_gprs_mode_is_compat(struct gsm_bts *bts, enum bts_gprs_mode mode)
+{
+	if (mode != BTS_GPRS_NONE &&
+	    !gsm_bts_has_feature(bts, BTS_FEAT_GPRS)) {
+		return 0;
+	}
+	if (mode == BTS_GPRS_EGPRS &&
+	    !gsm_bts_has_feature(bts, BTS_FEAT_EGPRS)) {
+		return 0;
+	}
+
+	return 1;
+}
+
 struct gsm_meas_rep *lchan_next_meas_rep(struct gsm_lchan *lchan)
 {
 	struct gsm_meas_rep *meas_rep;
diff --git a/openbsc/tests/ctrl_test_runner.py b/openbsc/tests/ctrl_test_runner.py
index a1e1d4d..502da76 100644
--- a/openbsc/tests/ctrl_test_runner.py
+++ b/openbsc/tests/ctrl_test_runner.py
@@ -491,6 +491,25 @@
         self.assertEquals(r['mtype'], 'SET_REPLY')
         self.assertEquals(r['value'], 'Tried to drop the BTS')
 
+    def testGprsMode(self):
+        r = self.do_get('bts.0.gprs-mode')
+        self.assertEquals(r['mtype'], 'GET_REPLY')
+        self.assertEquals(r['var'], 'bts.0.gprs-mode')
+        self.assertEquals(r['value'], 'none')
+
+        r = self.do_set('bts.0.gprs-mode', 'bla')
+        self.assertEquals(r['mtype'], 'ERROR')
+        self.assertEquals(r['error'], 'Mode is not known')
+
+        r = self.do_set('bts.0.gprs-mode', 'egprs')
+        self.assertEquals(r['mtype'], 'SET_REPLY')
+        self.assertEquals(r['value'], 'egprs')
+
+        r = self.do_get('bts.0.gprs-mode')
+        self.assertEquals(r['mtype'], 'GET_REPLY')
+        self.assertEquals(r['var'], 'bts.0.gprs-mode')
+        self.assertEquals(r['value'], 'egprs')
+
 class TestCtrlNAT(TestCtrlBase):
 
     def ctrl_command(self):