blob: 7dd354e46e28fd694aa6f6bc7f3ca63dbe7fefca [file] [log] [blame]
Harald Welte3e5ab692018-06-04 04:26:20 +02001/* Simple command-line client against the Osmocom CTRL interface */
2
3/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "simple_ctrl.h"
29
Harald Welted28c5f62018-06-04 12:04:57 +020030#include <osmocom/core/msgb.h>
Harald Welte3e5ab692018-06-04 04:26:20 +020031#include <osmocom/core/logging.h>
32#include <osmocom/core/application.h>
33
34static struct log_info log_info = {};
35
36static void exit_help(void)
37{
38 printf("help!\n");
39 exit(2);
40}
41
42int main(int argc, char **argv)
43{
44 struct simple_ctrl_handle *sch;
45 const char *host;
46 uint16_t port;
47 int rc;
48
49 if (argc < 4)
50 exit_help();
51
52 host = argv[1];
53 port = atoi(argv[2]);
54
55 osmo_init_logging2(NULL, &log_info);
56
Harald Welte0e9d3692018-06-04 11:52:39 +020057 sch = simple_ctrl_open(NULL, host, port, 1000);
Harald Welte3e5ab692018-06-04 04:26:20 +020058 if (!sch)
59 exit(1);
60
61 if (!strcmp(argv[3], "get")) {
62 char *val;
63 if (argc < 5)
64 exit_help();
65 val = simple_ctrl_get(sch, argv[4]);
66 if (!val)
67 exit(2);
68 printf("%s\n", val);
69 } else if (!strcmp(argv[3], "set")) {
70 if (argc < 6)
71 exit_help();
72 rc = simple_ctrl_set(sch, argv[4], argv[5]);
73 if (rc < 0)
74 exit(1);
Harald Welted28c5f62018-06-04 12:04:57 +020075 } else if (!strcmp(argv[3], "monitor")) {
76 simple_ctrl_set_timeout(sch, 0);
77 while (true) {
78 struct msgb *msg = simple_ctrl_receive(sch);
79 if (!msg)
80 exit(1);
81 printf("%s", (char *) msgb_l2(msg));
82 msgb_free(msg);
83 }
84 } else
85 exit_help();
Harald Welte3e5ab692018-06-04 04:26:20 +020086
87 exit(0);
88}