aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Mork <bjorn@mork.no>2019-05-24 11:16:06 +0200
committerBjørn Mork <bjorn@mork.no>2019-05-24 11:16:06 +0200
commit9252539dac52559d1efac99571bf8d9acf6b1c58 (patch)
tree1bb9804a8026cdfae92e351afb2e347bf8d8d908
parente13cd7a065f57c68b615d664e9d4736af11be6ef (diff)
cleaning up option defaults handling
Signed-off-by: Bjørn Mork <bjorn@mork.no>
-rw-r--r--obinsectd.c55
1 files changed, 34 insertions, 21 deletions
diff --git a/obinsectd.c b/obinsectd.c
index f49ff9b..9f4c292 100644
--- a/obinsectd.c
+++ b/obinsectd.c
@@ -41,6 +41,12 @@
#endif
#define DESCRIPTION "obinsectd (" VERSION ")"
+#define MQTT_BROKER "localhost"
+#define MQTT_PORT 1883
+#define MQTTS_PORT 8883
+#define MQTT_KEEPALIVE 60
+
+
#define BUFSIZE (1024 * 2)
#ifdef DEBUG
@@ -1180,11 +1186,11 @@ skipframe:
}
static struct option main_options[] = {
- { "help", 0, 0, '?' },
+ { "help", 0, 0, 'h' },
{ "debug", 0, 0, 'd' },
- { "host", 1, 0, 'h' },
+ { "broker", 1, 0, 'b' },
{ "id", 1, 0, 'i' },
{ "keepalive", 1, 0, 'k' },
{ "port", 1, 0, 'p' },
@@ -1211,7 +1217,7 @@ static void usage(const char *prog)
mosquitto_lib_version(&maj, &min, &rev);
printf("%s version %s, using libmosquitto %u.%u.%u\n", prog, VERSION, maj, min, rev);
printf("Usage: %s [-d] -s device\n", prog);
- printf(" [-h host] [-p port] [-u username [-P password]]\n");
+ printf(" [-b hostname] [-p port] [-u username [-P password]]\n");
#ifdef WITH_TLS
printf(" [-i id] [-k keepalive] [--insecure]\n");
printf(" [{--cafile file | --capath dir} [--cert file] [--key file]\n");
@@ -1223,20 +1229,20 @@ static void usage(const char *prog)
printf(" -s : serial device connected to M-Bus. E.g /dev/ttyUSB0. Use '-' to read from stdin\n");
printf("\nMQTT client options:\n");
- printf(" -h : MQTT host. Default is localhost\n");
- printf(" -i : MQTT id\n");
- printf(" -k : MQTT keepalive in seconds. Default is 60\n");
- printf(" -p : MQTT port. Default is 1883 (8883 when using MQTTS)\n");
- printf(" -P : MQTT password\n");
- printf(" -u : MQTT username\n");
+ printf(" -b : Broker hostname or IP address. Default: %s\n", MQTT_BROKER);
+ printf(" -i : Client Id. A random Id is generated by default\n");
+ printf(" -k : Keepalive in seconds. Default: %u\n", MQTT_KEEPALIVE);
+ printf(" -p : Broker TCP port. Default: %u (%u when using MQTTS)\n", MQTT_PORT, MQTTS_PORT);
+ printf(" -P : Password. Default is no authentication\n");
+ printf(" -u : Username\n");
#ifdef WITH_TLS
printf("\nOnly for MQTTS (MQTT over TLS):\n");
- printf(" --insecure : do not check MQTT broker certificate hostname\n");
- printf(" --cafile : path to trusted CA certificates file\n");
- printf(" --capath : path to trusted CA certificates directory\n");
- printf(" --cert : MQTT client certificate\n");
- printf(" --key : MQTT client private key\n");
+ printf(" --insecure : Do not validate brokername against certificate\n");
+ printf(" --cafile : Trusted CA certificates PEM file\n");
+ printf(" --capath : Trusted CA certificates directory\n");
+ printf(" --cert : Client certificate\n");
+ printf(" --key : Client private key\n");
#endif
}
@@ -1251,9 +1257,9 @@ int main(int argc, char *argv[])
struct mosquitto *mosq = NULL;
// mosquitto opts
char *mqttid = NULL;
- char *host = "localhost";
- int port = 1883;
- int keepalive = 60;
+ char *broker = MQTT_BROKER;
+ int port = 0;
+ int keepalive = MQTT_KEEPALIVE;
bool clean_session = true;
// mosquitto auth opts
char *mqttuser = NULL, *mqttpw = NULL;
@@ -1267,16 +1273,17 @@ int main(int argc, char *argv[])
#endif
fprintf(stderr, "%s\n", DESCRIPTION);
- while ((opt = getopt_long(argc, argv, "?dh:i:k:p:P:s:u", main_options, NULL)) != -1) {
+ while ((opt = getopt_long(argc, argv, "?hdb:i:k:p:P:s:u", main_options, NULL)) != -1) {
switch(opt) {
case '?':
+ case 'h':
usage(argv[0]);
return 0;
case 'd':
debug = true;
break;
- case 'h':
- host = optarg;
+ case 'b':
+ broker = optarg;
break;
case 'i':
mqttid = optarg;
@@ -1336,15 +1343,21 @@ int main(int argc, char *argv[])
fprintf(stderr, "Need both cert and key for TLS\n");
goto err;
}
+ /* set default port for MQTTS if not set by command line option */
+ if (!port)
+ port = MQTTS_PORT;
mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, NULL); // FIXME: callback?
// mosquitto_tls_opts_set()
mosquitto_tls_insecure_set(mosq, insecure);
// mosquitto_tls_psk_set()
}
#endif
+ /* set default port if not set by command line or TLS defaults */
+ if (!port)
+ port = MQTT_PORT;
/* connect to broker */
- mosquitto_connect(mosq, host, port, keepalive);
+ mosquitto_connect(mosq, broker, port, keepalive);
/* loop forever */
read_and_parse(serfd, mosq, buf, BUFSIZE);