comparison ctrl-client/osmo-ctrl-client.c @ 20:2230a763713f

ctrl-client: import from osmo-sysmon git repo
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 17 Dec 2023 08:43:13 +0000
parents
children
comparison
equal deleted inserted replaced
19:9ff041d85da5 20:2230a763713f
1 /* 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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "simple_ctrl.h"
24
25 #include <osmocom/core/msgb.h>
26 #include <osmocom/core/logging.h>
27 #include <osmocom/core/application.h>
28
29 static struct log_info log_info = {};
30
31 static void exit_help(void)
32 {
33 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");
37 exit(2);
38 }
39
40 int 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
55 sch = simple_ctrl_open(NULL, host, port, 1000);
56 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);
73 } 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();
84
85 exit(0);
86 }