diff --git a/mosquitto_broker/Makefile b/mosquitto_broker/Makefile new file mode 100644 index 0000000..c264aa0 --- /dev/null +++ b/mosquitto_broker/Makefile @@ -0,0 +1,11 @@ +CC=gcc +CFLAGS= -lmosquitto + +all: mqtt-broker.o + $(CC) $(CFLAGS) -o mqtt-broker mqtt-broker.o + +mqtt-broker.o: + $(CC) $(CFLAGS) -c mqtt-broker.c + +clean: + rm mqtt-broker.o mqtt-broker \ No newline at end of file diff --git a/mosquitto_broker/mqtt-broker.c b/mosquitto_broker/mqtt-broker.c new file mode 100644 index 0000000..b457c29 --- /dev/null +++ b/mosquitto_broker/mqtt-broker.c @@ -0,0 +1,74 @@ +#include +#include + + +void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) +{ + if(message->payloadlen){ + printf("%s %s\n", message->topic, message->payload); + }else{ + printf("%s (null)\n", message->topic); + } + fflush(stdout); +} + +void my_connect_callback(struct mosquitto *mosq, void *userdata, int result) +{ + int i; + if(!result){ + /* Subscribe to broker information topics on successful connect. */ + //mosquitto_subscribe(mosq, NULL, "$SYS/#", 2); + mosquitto_subscribe(mosq, NULL, "testtopic", 2); + }else{ + fprintf(stderr, "Connect failed\n"); + } +} + +void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos) +{ + int i; + + printf("Subscribed (mid: %d): %d", mid, granted_qos[0]); + for(i=1; i