annotate ctrl-client/osmo-ctrl-client.c @ 22:7bfe3e21eaae default tip

top Makefile: add ctrl-client
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 17 Dec 2023 08:53:35 +0000
parents 2230a763713f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /* Simple command-line client against the Osmocom CTRL interface */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 /* (C) 2018 by Harald Welte <laforge@gnumonks.org>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * All Rights Reserved.
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 *
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * SPDX-License-Identifier: GPL-2.0+
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * (at your option) any later version.
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 *
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful,
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * GNU General Public License for more details.
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 */
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include <stdio.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include <stdlib.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #include <string.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include "simple_ctrl.h"
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 #include <osmocom/core/msgb.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 #include <osmocom/core/logging.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 #include <osmocom/core/application.h>
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 static struct log_info log_info = {};
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 static void exit_help(void)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 printf("Usage:\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 printf("\tosmo-ctrl-client HOST PORT get VARIABLE\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 printf("\tosmo-ctrl-client HOST PORT set VARIABLE VALUE\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 printf("\tosmo-ctrl-client HOST PORT monitor\n");
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 exit(2);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 int main(int argc, char **argv)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 struct simple_ctrl_handle *sch;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 const char *host;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 uint16_t port;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 int rc;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (argc < 4)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 exit_help();
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 host = argv[1];
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 port = atoi(argv[2]);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 osmo_init_logging2(NULL, &log_info);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 sch = simple_ctrl_open(NULL, host, port, 1000);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (!sch)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 exit(1);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (!strcmp(argv[3], "get")) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 char *val;
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (argc < 5)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 exit_help();
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 val = simple_ctrl_get(sch, argv[4]);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (!val)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(2);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 printf("%s\n", val);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 } else if (!strcmp(argv[3], "set")) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (argc < 6)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 exit_help();
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 rc = simple_ctrl_set(sch, argv[4], argv[5]);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (rc < 0)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 exit(1);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 } else if (!strcmp(argv[3], "monitor")) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 simple_ctrl_set_timeout(sch, 0);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 while (true) {
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 struct msgb *msg = simple_ctrl_receive(sch);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 if (!msg)
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 exit(1);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 printf("%s", (char *) msgb_l2(msg));
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 msgb_free(msg);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 } else
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 exit_help();
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 exit(0);
2230a763713f ctrl-client: import from osmo-sysmon git repo
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }