Harald Welte | 3e5ab69 | 2018-06-04 04:26:20 +0200 | [diff] [blame] | 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 | * 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 Welte | d28c5f6 | 2018-06-04 12:04:57 +0200 | [diff] [blame] | 30 | #include <osmocom/core/msgb.h> |
Harald Welte | 3e5ab69 | 2018-06-04 04:26:20 +0200 | [diff] [blame] | 31 | #include <osmocom/core/logging.h> |
| 32 | #include <osmocom/core/application.h> |
| 33 | |
| 34 | static struct log_info log_info = {}; |
| 35 | |
| 36 | static void exit_help(void) |
| 37 | { |
Harald Welte | 01bf3e9 | 2018-06-04 12:05:13 +0200 | [diff] [blame] | 38 | printf("Usage:\n"); |
| 39 | printf("\tosmo-ctrl-client HOST PORT get VARIABLE\n"); |
| 40 | printf("\tosmo-ctrl-client HOST PORT set VARIABLE VALUE\n"); |
| 41 | printf("\tosmo-ctrl-client HOST PORT monitor\n"); |
Harald Welte | 3e5ab69 | 2018-06-04 04:26:20 +0200 | [diff] [blame] | 42 | exit(2); |
| 43 | } |
| 44 | |
| 45 | int main(int argc, char **argv) |
| 46 | { |
| 47 | struct simple_ctrl_handle *sch; |
| 48 | const char *host; |
| 49 | uint16_t port; |
| 50 | int rc; |
| 51 | |
| 52 | if (argc < 4) |
| 53 | exit_help(); |
| 54 | |
| 55 | host = argv[1]; |
| 56 | port = atoi(argv[2]); |
| 57 | |
| 58 | osmo_init_logging2(NULL, &log_info); |
| 59 | |
Harald Welte | 0e9d369 | 2018-06-04 11:52:39 +0200 | [diff] [blame] | 60 | sch = simple_ctrl_open(NULL, host, port, 1000); |
Harald Welte | 3e5ab69 | 2018-06-04 04:26:20 +0200 | [diff] [blame] | 61 | if (!sch) |
| 62 | exit(1); |
| 63 | |
| 64 | if (!strcmp(argv[3], "get")) { |
| 65 | char *val; |
| 66 | if (argc < 5) |
| 67 | exit_help(); |
| 68 | val = simple_ctrl_get(sch, argv[4]); |
| 69 | if (!val) |
| 70 | exit(2); |
| 71 | printf("%s\n", val); |
| 72 | } else if (!strcmp(argv[3], "set")) { |
| 73 | if (argc < 6) |
| 74 | exit_help(); |
| 75 | rc = simple_ctrl_set(sch, argv[4], argv[5]); |
| 76 | if (rc < 0) |
| 77 | exit(1); |
Harald Welte | d28c5f6 | 2018-06-04 12:04:57 +0200 | [diff] [blame] | 78 | } else if (!strcmp(argv[3], "monitor")) { |
| 79 | simple_ctrl_set_timeout(sch, 0); |
| 80 | while (true) { |
| 81 | struct msgb *msg = simple_ctrl_receive(sch); |
| 82 | if (!msg) |
| 83 | exit(1); |
| 84 | printf("%s", (char *) msgb_l2(msg)); |
| 85 | msgb_free(msg); |
| 86 | } |
| 87 | } else |
| 88 | exit_help(); |
Harald Welte | 3e5ab69 | 2018-06-04 04:26:20 +0200 | [diff] [blame] | 89 | |
| 90 | exit(0); |
| 91 | } |