summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2019-11-25 22:37:11 -0800
committerMike Crute <mike@crute.us>2019-11-25 22:37:11 -0800
commit6b46779198fbf89c6688e5e9ddf12503ea29dbcd (patch)
treed0081ca63dbd3d189e8e03bb77e91392b83b8528
parent5a5b04b795364c15be0a2917e42c0b37edbcd50b (diff)
downloadmfi_homekit-6b46779198fbf89c6688e5e9ddf12503ea29dbcd.tar.bz2
mfi_homekit-6b46779198fbf89c6688e5e9ddf12503ea29dbcd.tar.xz
mfi_homekit-6b46779198fbf89c6688e5e9ddf12503ea29dbcd.zip
Refactor mqtt binary, add make target
-rw-r--r--Makefile7
-rw-r--r--mfi-mqtt.c55
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 @@
1BUILDROOT := $(HOME)/tmp/buildroot-2019.02.7
2
1.PHONY: all 3.PHONY: all
2all: 4all:
3 CGO_ENABLED=0 go build -o mfi_homekit main.go 5 CGO_ENABLED=0 go build -o mfi_homekit main.go
@@ -8,3 +10,8 @@ docker:
8 cd docker; \ 10 cd docker; \
9 docker build -t docker.crute.me/mfi_homekit:latest .; \ 11 docker build -t docker.crute.me/mfi_homekit:latest .; \
10 docker push docker.crute.me/mfi_homekit:latest 12 docker push docker.crute.me/mfi_homekit:latest
13
14mfi-mqtt-mips: mfi-mqtt.c
15 $(BUILDROOT)/output/host/bin/mips-buildroot-linux-uclibc-gcc \
16 -Wall -o $@ $< \
17 -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 @@
1/*
2/home/mcrute/tmp/buildroot-2019.02.7/output/host/bin/mips-buildroot-linux-uclibc-gcc \
3 -o mfi-mqtt ~/test.c -Wall \
4 -I/home/mcrute/tmp/buildroot-2019.02.7/output/host/mips-buildroot-linux-uclibc/sysroot/usr/include/json-c \
5 -lpthread -lmosquitto -ljson-c -static
6
7scp -o KexAlgorithms=+diffie-hellman-group1-sha1 mfi-mqtt admin@192.168.2.10:
8*/
9
10#include <assert.h> 1#include <assert.h>
11#include <errno.h> 2#include <errno.h>
12#include <stdio.h> 3#include <stdio.h>
@@ -22,11 +13,10 @@ scp -o KexAlgorithms=+diffie-hellman-group1-sha1 mfi-mqtt admin@192.168.2.10:
22#include <sys/ioctl.h> 13#include <sys/ioctl.h>
23#include <arpa/inet.h> 14#include <arpa/inet.h>
24 15
25#include <json.h> 16#include <json-c/json.h>
26#include <mosquitto.h> 17#include <mosquitto.h>
27 18
28static volatile sig_atomic_t closing_time = 0; 19static volatile sig_atomic_t closing_time = 0;
29struct mosquitto *mosq = NULL;
30 20
31void get_primary_ip_address(char ip_address[15]); 21void get_primary_ip_address(char ip_address[15]);
32 22
@@ -415,19 +405,15 @@ void cleanup_crap_processes() {
415 system("pkill avahi-daemon"); 405 system("pkill avahi-daemon");
416} 406}
417 407
418int main(int argc, char *argv[]) 408struct mosquitto * connect_to_broker(char *host, int port) {
419{ 409 int rc;
420 int rc;
421 char *id; 410 char *id;
422 pthread_t pinger_thread_h, control_thread_h, light_management_thread_h; 411 struct mosquitto *mosq;
423
424 cleanup_crap_processes();
425 412
426 set_signal_handler();
427 mosquitto_lib_init(); 413 mosquitto_lib_init();
428 414
429 if (client_id_generate(&id)) { 415 if (client_id_generate(&id)) {
430 return 1; 416 return NULL;
431 } 417 }
432 418
433 mosq = mosquitto_new(id, true, NULL); 419 mosq = mosquitto_new(id, true, NULL);
@@ -441,7 +427,7 @@ int main(int argc, char *argv[])
441 break; 427 break;
442 } 428 }
443 mosquitto_lib_cleanup(); 429 mosquitto_lib_cleanup();
444 return 1; 430 return NULL;
445 } 431 }
446 432
447 int protocol_version = MQTT_PROTOCOL_V311; 433 int protocol_version = MQTT_PROTOCOL_V311;
@@ -451,7 +437,7 @@ int main(int argc, char *argv[])
451 mosquitto_connect_with_flags_callback_set(mosq, my_connect_callback); 437 mosquitto_connect_with_flags_callback_set(mosq, my_connect_callback);
452 mosquitto_message_callback_set(mosq, my_message_callback); 438 mosquitto_message_callback_set(mosq, my_message_callback);
453 439
454 rc = mosquitto_connect(mosq, "172.16.0.191", 1883, 60); 440 rc = mosquitto_connect(mosq, host, port, 60);
455 if (rc > 0) { 441 if (rc > 0) {
456 if (rc == MOSQ_ERR_ERRNO) { 442 if (rc == MOSQ_ERR_ERRNO) {
457 fprintf(stderr, "Error: %s\n", strerror(errno)); 443 fprintf(stderr, "Error: %s\n", strerror(errno));
@@ -459,8 +445,30 @@ int main(int argc, char *argv[])
459 fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc)); 445 fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc));
460 } 446 }
461 mosquitto_lib_cleanup(); 447 mosquitto_lib_cleanup();
462 return 1; 448 return NULL;
463 } 449 }
450
451 return mosq;
452}
453
454void shutdown_broker(struct mosquitto *mosq) {
455 mosquitto_destroy(mosq);
456 mosquitto_lib_cleanup();
457}
458
459int main(int argc, char *argv[])
460{
461 struct mosquitto *mosq;
462 pthread_t pinger_thread_h, control_thread_h, light_management_thread_h;
463
464 cleanup_crap_processes();
465
466 set_signal_handler();
467
468 mosq = connect_to_broker("172.16.0.191", 1883);
469 if (!mosq) {
470 return 1;
471 }
464 fprintf(stderr, "Connected to broker\n"); 472 fprintf(stderr, "Connected to broker\n");
465 473
466 pthread_create(&pinger_thread_h, NULL, (void * (*)(void *))pinger_thread, mosq); 474 pthread_create(&pinger_thread_h, NULL, (void * (*)(void *))pinger_thread, mosq);
@@ -479,8 +487,7 @@ int main(int argc, char *argv[])
479 } 487 }
480 } while(true); 488 } while(true);
481 489
482 mosquitto_destroy(mosq); 490 shutdown_broker(mosq);
483 mosquitto_lib_cleanup();
484 491
485 return 0; 492 return 0;
486} 493}