From 6b46779198fbf89c6688e5e9ddf12503ea29dbcd Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Mon, 25 Nov 2019 22:37:11 -0800 Subject: Refactor mqtt binary, add make target --- Makefile | 7 +++++++ mfi-mqtt.c | 55 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index bdce54d..b667d15 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +BUILDROOT := $(HOME)/tmp/buildroot-2019.02.7 + .PHONY: all all: CGO_ENABLED=0 go build -o mfi_homekit main.go @@ -8,3 +10,8 @@ docker: cd docker; \ docker build -t docker.crute.me/mfi_homekit:latest .; \ docker push docker.crute.me/mfi_homekit:latest + +mfi-mqtt-mips: mfi-mqtt.c + $(BUILDROOT)/output/host/bin/mips-buildroot-linux-uclibc-gcc \ + -Wall -o $@ $< \ + -lpthread -lmosquitto -ljson-c -static diff --git a/mfi-mqtt.c b/mfi-mqtt.c index 91b9212..0155039 100644 --- a/mfi-mqtt.c +++ b/mfi-mqtt.c @@ -1,12 +1,3 @@ -/* -/home/mcrute/tmp/buildroot-2019.02.7/output/host/bin/mips-buildroot-linux-uclibc-gcc \ - -o mfi-mqtt ~/test.c -Wall \ - -I/home/mcrute/tmp/buildroot-2019.02.7/output/host/mips-buildroot-linux-uclibc/sysroot/usr/include/json-c \ - -lpthread -lmosquitto -ljson-c -static - -scp -o KexAlgorithms=+diffie-hellman-group1-sha1 mfi-mqtt admin@192.168.2.10: -*/ - #include #include #include @@ -22,11 +13,10 @@ scp -o KexAlgorithms=+diffie-hellman-group1-sha1 mfi-mqtt admin@192.168.2.10: #include #include -#include +#include #include static volatile sig_atomic_t closing_time = 0; -struct mosquitto *mosq = NULL; void get_primary_ip_address(char ip_address[15]); @@ -415,19 +405,15 @@ void cleanup_crap_processes() { system("pkill avahi-daemon"); } -int main(int argc, char *argv[]) -{ - int rc; +struct mosquitto * connect_to_broker(char *host, int port) { + int rc; char *id; - pthread_t pinger_thread_h, control_thread_h, light_management_thread_h; - - cleanup_crap_processes(); + struct mosquitto *mosq; - set_signal_handler(); mosquitto_lib_init(); if (client_id_generate(&id)) { - return 1; + return NULL; } mosq = mosquitto_new(id, true, NULL); @@ -441,7 +427,7 @@ int main(int argc, char *argv[]) break; } mosquitto_lib_cleanup(); - return 1; + return NULL; } int protocol_version = MQTT_PROTOCOL_V311; @@ -451,7 +437,7 @@ int main(int argc, char *argv[]) mosquitto_connect_with_flags_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); - rc = mosquitto_connect(mosq, "172.16.0.191", 1883, 60); + rc = mosquitto_connect(mosq, host, port, 60); if (rc > 0) { if (rc == MOSQ_ERR_ERRNO) { fprintf(stderr, "Error: %s\n", strerror(errno)); @@ -459,8 +445,30 @@ int main(int argc, char *argv[]) fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc)); } mosquitto_lib_cleanup(); - return 1; + return NULL; } + + return mosq; +} + +void shutdown_broker(struct mosquitto *mosq) { + mosquitto_destroy(mosq); + mosquitto_lib_cleanup(); +} + +int main(int argc, char *argv[]) +{ + struct mosquitto *mosq; + pthread_t pinger_thread_h, control_thread_h, light_management_thread_h; + + cleanup_crap_processes(); + + set_signal_handler(); + + mosq = connect_to_broker("172.16.0.191", 1883); + if (!mosq) { + return 1; + } fprintf(stderr, "Connected to broker\n"); pthread_create(&pinger_thread_h, NULL, (void * (*)(void *))pinger_thread, mosq); @@ -479,8 +487,7 @@ int main(int argc, char *argv[]) } } while(true); - mosquitto_destroy(mosq); - mosquitto_lib_cleanup(); + shutdown_broker(mosq); return 0; } -- cgit v1.2.3