2 * Copyright (c) 2014 Samsung Electronics Co., Ltd
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "commandline_sdbd.h"
26 * @fn int split_host_port(const char *optarg, char **host, int *port)
27 * @brief Splits string of form \c "localhost:22" into \c host (string)
28 * and \c port (int) parts.
30 * @param optarg optarg from getopt
31 * @param host Where to put host part string
32 * @param port Where to put port part int
34 * @returns \ref SDBD_COMMANDLINE_SUCCESS on success
35 * or \ref SDBD_COMMANDLINE_FAILURE otherwise
37 int split_host_port(const char *optarg, char **host, int *port);
39 int parse_sdbd_commandline(SdbdCommandlineArgs *sdbd_args, int argc, char *argv[]) {
45 static struct option long_options[] = {
46 { ARG_EMULATOR_VM_NAME, required_argument, NULL, ARG_S_EMULATOR_VM_NAME },
47 { ARG_SENSORS, required_argument, NULL, ARG_S_SENSORS },
48 { ARG_SDB, required_argument, NULL, ARG_S_SDB },
49 { ARG_SDBD_LISTEN_PORT, required_argument, NULL, ARG_S_SDBD_LISTEN_PORT },
50 { ARG_HELP, no_argument, NULL, ARG_S_HELP },
51 { ARG_USAGE, no_argument, NULL, ARG_S_USAGE },
55 optind = 1; /* the index of the next element to be processed in argv */
57 while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) {
59 case ARG_S_EMULATOR_VM_NAME:
60 split_retval = split_host_port(optarg,
61 &sdbd_args->emulator.host,
62 &sdbd_args->emulator.port);
63 if (split_retval != SDBD_COMMANDLINE_SUCCESS) {
66 /* if we are on emulator we listen using local transport
67 * so we should set port to default value but this can
68 * be overwritten by command line options */
69 if (sdbd_args->sdbd_port < 0) {
70 sdbd_args->sdbd_port = DEFAULT_SDB_LOCAL_TRANSPORT_PORT;
74 split_retval = split_host_port(optarg,
75 &sdbd_args->sensors.host,
76 &sdbd_args->sensors.port);
77 if (split_retval != SDBD_COMMANDLINE_SUCCESS) {
82 split_retval = split_host_port(optarg,
84 &sdbd_args->sdb.port);
85 if (split_retval != SDBD_COMMANDLINE_SUCCESS) {
89 case ARG_S_SDBD_LISTEN_PORT:
90 if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) {
91 return SDBD_COMMANDLINE_FAILURE;
95 return SDBD_COMMANDLINE_HELP;
97 return SDBD_COMMANDLINE_USAGE;
99 return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT;
101 return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT;
103 return SDBD_COMMANDLINE_FAILURE;
107 return SDBD_COMMANDLINE_SUCCESS;
111 void apply_sdbd_commandline_defaults(SdbdCommandlineArgs *sdbd_args) {
112 sdbd_args->sensors.host = strdup(QEMU_FORWARD_IP);
113 sdbd_args->sensors.port = DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT;
115 sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP);
116 sdbd_args->sdb.port = DEFAULT_SDB_PORT;
118 // by default don't listen on local transport
119 sdbd_args->sdbd_port = -1;
123 int split_host_port(const char *optarg, char **host, int *port) {
124 const char *colon = strchr(optarg, ':');
125 char *old_val = NULL;
129 *host = strndup(optarg, colon - optarg);
130 if (sscanf(colon + 1, "%d", port) < 1) {
131 return SDBD_COMMANDLINE_FAILURE;
134 return SDBD_COMMANDLINE_FAILURE;
140 return SDBD_COMMANDLINE_SUCCESS;
144 void clear_sdbd_commandline_args(SdbdCommandlineArgs *sdbd_args) {
145 free(sdbd_args->emulator.host);
146 sdbd_args->emulator.host = NULL;
148 free(sdbd_args->sdb.host);
149 sdbd_args->sdb.host = NULL;
151 free(sdbd_args->sensors.host);
152 sdbd_args->sensors.host = NULL;
154 memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs));
158 void print_sdbd_usage_message(FILE *stream) {
159 const char *format = "Usage sdbd [OPTION]...\n"
160 "\t-%c, --%s=HOST:PORT\temulator's name and forward port\n"
161 "\t-%c, --%s=HOST:PORT\thostname or IP and port of sdb listening on host\n"
162 "\t-%c, --%s=HOST:PORT \thostname or IP and port of sensors daemon\n"
163 "\t-%c, --%s=PORT \tport on which sdbd shall be listening on\n"
164 "\t-%c, --%s \tprint help message\n"
165 "\t-%c, --%s \tprint this usage message\n"
168 fprintf(stream, format,
169 ARG_S_EMULATOR_VM_NAME, ARG_EMULATOR_VM_NAME,
171 ARG_S_SENSORS, ARG_SENSORS,
172 ARG_S_SDBD_LISTEN_PORT, ARG_SDBD_LISTEN_PORT,
173 ARG_S_HELP, ARG_HELP,
174 ARG_S_USAGE, ARG_USAGE