summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2019-11-26 14:37:49 -0800
committerMike Crute <mike@crute.us>2019-11-26 14:37:49 -0800
commit15573b8deb2bdd582b4e039680abb5cb56f66c08 (patch)
treed69543cced6aefbde4fe172919ef86e8456207a5
parent6bb494fdc7b785867f7a458ee092838c4685546c (diff)
downloadmfi_homekit-15573b8deb2bdd582b4e039680abb5cb56f66c08.tar.bz2
mfi_homekit-15573b8deb2bdd582b4e039680abb5cb56f66c08.tar.xz
mfi_homekit-15573b8deb2bdd582b4e039680abb5cb56f66c08.zip
Add usage and version
-rw-r--r--mfi-mqtt/Makefile10
-rw-r--r--mfi-mqtt/mfi-mqtt.c78
2 files changed, 83 insertions, 5 deletions
diff --git a/mfi-mqtt/Makefile b/mfi-mqtt/Makefile
index a30dfbd..96b1016 100644
--- a/mfi-mqtt/Makefile
+++ b/mfi-mqtt/Makefile
@@ -1,12 +1,18 @@
1BUILDROOT := $(HOME)/tmp/buildroot-2019.02.7 1BUILDROOT := $(HOME)/tmp/buildroot-2019.02.7
2MIPS_GCC := $(BUILDROOT)/output/host/bin/mips-buildroot-linux-uclibc-gcc 2MIPS_GCC := $(BUILDROOT)/output/host/bin/mips-buildroot-linux-uclibc-gcc
3MIPS_STRIP := $(BUILDROOT)/output/host/mips-buildroot-linux-uclibc/bin/strip
4VERSION := $(shell git describe --tags --dirty --long)
5
6mfi-mqtt-mips-stripped: mfi-mqtt-mips
7 cp $< $@
8 $(MIPS_STRIP) $@
3 9
4mfi-mqtt-mips: reporting.o mosquitto.o mfi-mqtt.o 10mfi-mqtt-mips: reporting.o mosquitto.o mfi-mqtt.o
5 $(MIPS_GCC) -o $@ $^ -lpthread -lmosquitto -ljson-c -static 11 $(MIPS_GCC) -o $@ $^ -lpthread -lmosquitto -ljson-c -static
6 12
7%.o: %.c 13%.o: %.c
8 $(MIPS_GCC) -Wall -c -o $@ $< 14 $(MIPS_GCC) -DVERSION=$(VERSION) -Wall -c -o $@ $<
9 15
10.PHONY: clean 16.PHONY: clean
11clean: 17clean:
12 rm -f *.o mfi-mqtt-mips 18 rm -f *.o mfi-mqtt-mips mfi-mqtt-mips-stripped
diff --git a/mfi-mqtt/mfi-mqtt.c b/mfi-mqtt/mfi-mqtt.c
index 25527bc..535fcb3 100644
--- a/mfi-mqtt/mfi-mqtt.c
+++ b/mfi-mqtt/mfi-mqtt.c
@@ -5,7 +5,9 @@
5#include <time.h> 5#include <time.h>
6#include <unistd.h> 6#include <unistd.h>
7#include <signal.h> 7#include <signal.h>
8#include <getopt.h>
8#include <pthread.h> 9#include <pthread.h>
10#include <libgen.h>
9 11
10#include <json-c/json.h> 12#include <json-c/json.h>
11#include <mosquitto.h> 13#include <mosquitto.h>
@@ -13,6 +15,10 @@
13#include "mosquitto.h" 15#include "mosquitto.h"
14#include "reporting.h" 16#include "reporting.h"
15 17
18#define XSTR(s) STR(s)
19#define STR(s) #s
20
21static char *APP_VERSION = XSTR(VERSION);
16static volatile sig_atomic_t closing_time = 0; 22static volatile sig_atomic_t closing_time = 0;
17 23
18typedef struct control_message { 24typedef struct control_message {
@@ -229,8 +235,7 @@ void cleanup_crap_processes() {
229 system("pkill avahi-daemon"); 235 system("pkill avahi-daemon");
230} 236}
231 237
232int main(int argc, char *argv[]) 238int real_main(char *broker, int broker_port) {
233{
234 struct mosquitto *mosq; 239 struct mosquitto *mosq;
235 pthread_t pinger_thread_h, control_thread_h, light_management_thread_h; 240 pthread_t pinger_thread_h, control_thread_h, light_management_thread_h;
236 241
@@ -238,7 +243,7 @@ int main(int argc, char *argv[])
238 243
239 set_signal_handler(); 244 set_signal_handler();
240 245
241 mosq = connect_to_broker("172.16.0.191", 1883); 246 mosq = connect_to_broker(broker, broker_port);
242 if (!mosq) { 247 if (!mosq) {
243 return 1; 248 return 1;
244 } 249 }
@@ -267,3 +272,70 @@ int main(int argc, char *argv[])
267 272
268 return 0; 273 return 0;
269} 274}
275
276void print_usage(char *name) {
277 printf("usage: %s [--help|-h] [--version|-V] [--broker|-b] [--port|-p]\n", name);
278 printf("Control mFi Power Strip via MQTT messages\n\n");
279
280 printf(" -b, --broker Broker hostname or IP address [REQUIRED]\n");
281 printf(" -p, --port Broker port, default: 1883\n");
282 printf(" -h, --help Show this message\n");
283 printf(" -V, --version Show application version info\n");
284
285 free(name);
286}
287
288int main(int argc, char *argv[])
289{
290 int c;
291 int rc;
292 char *broker;
293 int port = 1883;
294 int option_index = 0;
295
296 while (true) {
297 static struct option long_options[] = {
298 {"help", no_argument, 0, 'h'},
299 {"version", no_argument, 0, 'V'},
300 {"broker", required_argument, 0, 'b'},
301 {"port", required_argument, 0, 'p'},
302 {0, 0, 0, 0}
303 };
304
305 if ((c = getopt_long(argc, argv, "hVb:p:", long_options, &option_index)) == -1) {
306 break;
307 }
308
309 switch (c) {
310 case 'h':
311 print_usage(strdup(basename(argv[0])));
312 return 0;
313
314 case 'V':
315 fprintf(stdout, "%s version %s\n", basename(argv[0]), APP_VERSION);
316 return 0;
317
318 case 'b':
319 broker = strdup(optarg);
320 break;
321
322 case 'p':
323 port = atoi(optarg);
324 if (port > 65536 || port <= 0) {
325 fprintf(stderr, "error: invalid port specified\n");
326 return 1;
327 }
328 break;
329 }
330 }
331
332 if (!broker) {
333 fprintf(stderr, "error: no broker configured\n");
334 return 1;
335 }
336
337 rc = real_main(broker, port);
338 free(broker);
339
340 return rc;
341}