blob: 351ab9160f62a4aa8056fd01e35d91040e1a86ce [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.
Harald Welte3e5ab692018-06-04 04:26:20 +020017 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "simple_ctrl.h"
24
Harald Welted28c5f62018-06-04 12:04:57 +020025#include <osmocom/core/msgb.h>
Harald Welte3e5ab692018-06-04 04:26:20 +020026#include <osmocom/core/logging.h>
27#include <osmocom/core/application.h>
28
29static struct log_info log_info = {};
30
31static void exit_help(void)
32{
Harald Welte01bf3e92018-06-04 12:05:13 +020033 printf("Usage:\n");
34 printf("\tosmo-ctrl-client HOST PORT get VARIABLE\n");
35 printf("\tosmo-ctrl-client HOST PORT set VARIABLE VALUE\n");
36 printf("\tosmo-ctrl-client HOST PORT monitor\n");
Harald Welte3e5ab692018-06-04 04:26:20 +020037 exit(2);
38}
39
40int main(int argc, char **argv)
41{
42 struct simple_ctrl_handle *sch;
43 const char *host;
44 uint16_t port;
45 int rc;
46
47 if (argc < 4)
48 exit_help();
49
50 host = argv[1];
51 port = atoi(argv[2]);
52
53 osmo_init_logging2(NULL, &log_info);
54
Harald Welte0e9d3692018-06-04 11:52:39 +020055 sch = simple_ctrl_open(NULL, host, port, 1000);
Harald Welte3e5ab692018-06-04 04:26:20 +020056 if (!sch)
57 exit(1);
58
59 if (!strcmp(argv[3], "get")) {
60 char *val;
61 if (argc < 5)
62 exit_help();
63 val = simple_ctrl_get(sch, argv[4]);
64 if (!val)
65 exit(2);
66 printf("%s\n", val);
67 } else if (!strcmp(argv[3], "set")) {
68 if (argc < 6)
69 exit_help();
70 rc = simple_ctrl_set(sch, argv[4], argv[5]);
71 if (rc < 0)
72 exit(1);
Harald Welted28c5f62018-06-04 12:04:57 +020073 } else if (!strcmp(argv[3], "monitor")) {
74 simple_ctrl_set_timeout(sch, 0);
75 while (true) {
76 struct msgb *msg = simple_ctrl_receive(sch);
77 if (!msg)
78 exit(1);
79 printf("%s", (char *) msgb_l2(msg));
80 msgb_free(msg);
81 }
82 } else
83 exit_help();
Harald Welte3e5ab692018-06-04 04:26:20 +020084
85 exit(0);
86}