Allow sdb to listen on both usb and TCP.
[sdk/target/sdbd.git] / src / commandline_sdbd.c
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "commandline_sdbd.h"
18 #include "sdb.h"
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <getopt.h>
23
24
25 /*!
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.
29  *
30  * @param optarg optarg from getopt
31  * @param host Where to put host part string
32  * @param port Where to put port part int
33  *
34  * @returns \ref SDBD_COMMANDLINE_SUCCESS on success
35  *          or \ref SDBD_COMMANDLINE_FAILURE otherwise
36  */
37 int split_host_port(const char *optarg, char **host, int *port);
38
39 int parse_sdbd_commandline(SdbdCommandlineArgs *sdbd_args, int argc, char *argv[]) {
40         int split_retval;
41
42         int opt;
43         int long_index = 0;
44
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 },
52                 { NULL, 0, NULL, 0 }
53         };
54
55         optind = 1;     /* the index of the next element to be processed in argv */
56
57         while ((opt = getopt_long(argc, argv, "", long_options, &long_index)) != -1) {
58                 switch (opt) {
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) {
64                                 return split_retval;
65                         }
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;
71                         }
72                         break;
73                 case ARG_S_SENSORS:
74                         split_retval = split_host_port(optarg,
75                                         &sdbd_args->sensors.host,
76                                         &sdbd_args->sensors.port);
77                         if (split_retval != SDBD_COMMANDLINE_SUCCESS) {
78                                 return split_retval;
79                         }
80                         break;
81                 case ARG_S_SDB:
82                         split_retval = split_host_port(optarg,
83                                         &sdbd_args->sdb.host,
84                                         &sdbd_args->sdb.port);
85                         if (split_retval != SDBD_COMMANDLINE_SUCCESS) {
86                                 return split_retval;
87                         }
88                         break;
89                 case ARG_S_SDBD_LISTEN_PORT:
90                         if (sscanf(optarg, "%d", &sdbd_args->sdbd_port) < 1) {
91                                 return SDBD_COMMANDLINE_FAILURE;
92                         }
93                         break;
94                 case ARG_S_HELP:
95                     return SDBD_COMMANDLINE_HELP;
96                 case ARG_S_USAGE:
97                     return SDBD_COMMANDLINE_USAGE;
98                 case 1:
99                         return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT;
100                 case '?':
101                         return SDBD_COMMANDLINE_FAILURE_UNKNOWN_OPT;
102                 default:
103                         return SDBD_COMMANDLINE_FAILURE;
104                 }
105         }
106
107         return SDBD_COMMANDLINE_SUCCESS;
108 }
109
110
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;
114
115         sdbd_args->sdb.host = strdup(QEMU_FORWARD_IP);
116         sdbd_args->sdb.port = DEFAULT_SDB_PORT;
117
118         // by default don't listen on local transport
119         sdbd_args->sdbd_port = -1;
120 }
121
122
123 int split_host_port(const char *optarg, char **host, int *port) {
124         const char *colon = strchr(optarg, ':');
125         char *old_val = NULL;
126
127         if (colon) {
128                 old_val = *host;
129                 *host = strndup(optarg, colon - optarg);
130                 if (sscanf(colon + 1, "%d", port) < 1) {
131                         return SDBD_COMMANDLINE_FAILURE;
132                 }
133         } else {
134                 return SDBD_COMMANDLINE_FAILURE;
135         }
136
137         if (old_val) {
138                 free(old_val);
139         }
140         return SDBD_COMMANDLINE_SUCCESS;
141 }
142
143
144 void clear_sdbd_commandline_args(SdbdCommandlineArgs *sdbd_args) {
145     free(sdbd_args->emulator.host);
146     sdbd_args->emulator.host = NULL;
147
148     free(sdbd_args->sdb.host);
149     sdbd_args->sdb.host = NULL;
150
151     free(sdbd_args->sensors.host);
152     sdbd_args->sensors.host = NULL;
153
154         memset(sdbd_args, 0, sizeof(SdbdCommandlineArgs));
155 }
156
157
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"
166             ;
167
168     fprintf(stream, format,
169             ARG_S_EMULATOR_VM_NAME, ARG_EMULATOR_VM_NAME,
170             ARG_S_SDB, ARG_SDB,
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
175             );
176 }