103 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #include <err.h>
 | |
| #include <errno.h>
 | |
| #include <mosquitto.h>
 | |
| #include <semaphore.h>
 | |
| #include <stdio.h>
 | |
| #include <stdlib.h>
 | |
| #include <signal.h>
 | |
| #include <string.h>
 | |
| #include <sqlite3.h>
 | |
| #include <unistd.h>
 | |
| 
 | |
| #include "db.h"
 | |
| #include "msg.h"
 | |
| #include "serialization.h"
 | |
| 
 | |
| #define BRI "brightness"
 | |
| #define BTN "button"
 | |
| #define HOST "localhost"
 | |
| #define PORT 1883
 | |
| #define USERNAME "user"
 | |
| #define PASSWD "test"
 | |
| 
 | |
| sem_t lock;
 | |
| struct mosquitto *mosq = NULL;
 | |
| sqlite3 *db = NULL;
 | |
| 
 | |
| void mosquitto_server_callback(__attribute__((unused)) struct mosquitto *mosq,
 | |
|                                __attribute__((unused)) void *obj,
 | |
|                                const struct mosquitto_message *message) 
 | |
| {
 | |
| 	struct brightness_msg br_msg;
 | |
| 	struct button_msg btn_msg;
 | |
| 	unsigned char buf[message->payloadlen];
 | |
| 
 | |
| 	memcpy(buf, message->payload, message->payloadlen);
 | |
| 
 | |
| 	if (strcmp(message->topic, BRI) == 0) {
 | |
| 		bzero(&br_msg, sizeof(struct brightness_msg));
 | |
| 		if (unmarshal_brightness_msg((char *)buf, &br_msg) < 0)
 | |
| 			return;
 | |
| 		printf("BRI: %s %d\n", br_msg.id, br_msg.val);
 | |
| 		insert_data(br_msg.id, BRI, br_msg.val);
 | |
| 	} else if (strcmp(message->topic, BTN) == 0) {
 | |
| 		bzero(&btn_msg, sizeof(struct button_msg));
 | |
| 		if (unmarshal_button_msg((char *)buf, &btn_msg) < 0)
 | |
| 			return;
 | |
| 		printf("BTN: %s\n", btn_msg.id);
 | |
| 		insert_data(btn_msg.id, BTN, 1);
 | |
| 	}
 | |
| }
 | |
| 
 | |
| void clean_server(void)
 | |
| {
 | |
| 	if (mosq != NULL)
 | |
| 		mosquitto_destroy(mosq);
 | |
| 	mosquitto_lib_cleanup();
 | |
| 	clean_db(db);
 | |
| }
 | |
| 
 | |
| void sigint_handler(__attribute__((unused)) int num)
 | |
| {
 | |
| 	sem_post(&lock);
 | |
| }
 | |
| 
 | |
| int main(void)
 | |
| {
 | |
| 	struct sigaction sa;
 | |
| 
 | |
| 	if (mosquitto_lib_init() != MOSQ_ERR_SUCCESS)
 | |
| 		errx(EXIT_FAILURE, "libmosquitto failed to initialize");
 | |
| 
 | |
| 	atexit(clean_server);
 | |
| 	init_db(&db);
 | |
| 
 | |
| 	if (!(mosq = mosquitto_new(NULL, true, NULL)))
 | |
| 		errx(EXIT_FAILURE, "mosquitto_new: %s", strerror(errno));
 | |
| 
 | |
| 	mosquitto_username_pw_set(mosq, USERNAME, PASSWD);
 | |
| 
 | |
| 	mosquitto_message_callback_set(mosq, mosquitto_server_callback);
 | |
| 
 | |
| 	sa.sa_handler = sigint_handler;
 | |
| 	sigemptyset(&sa.sa_mask);
 | |
| 	sa.sa_flags = SA_RESTART;
 | |
| 	sigaction(SIGINT, &sa, NULL);
 | |
| 
 | |
| 	if (mosquitto_connect(mosq, HOST, 1883, 5) != MOSQ_ERR_SUCCESS)
 | |
| 		errx(EXIT_FAILURE, "mosquitto_connect: %s:%d %s", HOST, PORT, strerror(errno));
 | |
| 
 | |
| 	if (mosquitto_subscribe(mosq, NULL, BRI, 0) != MOSQ_ERR_SUCCESS)
 | |
| 		errx(EXIT_FAILURE, "mosquitto_subscribe: %s %s", BRI, strerror(errno));
 | |
| 	if (mosquitto_subscribe(mosq, NULL, BTN, 0) != MOSQ_ERR_SUCCESS)
 | |
| 		errx(EXIT_FAILURE, "mosquitto_subscribe: %s %s", BTN, strerror(errno));
 | |
| 
 | |
| 	mosquitto_loop_start(mosq);
 | |
| 	sem_wait(&lock);
 | |
| 
 | |
| 	if (mosquitto_disconnect(mosq) != MOSQ_ERR_SUCCESS)
 | |
| 		warnx("mosquitto_disconnect: %s:%d already disconnected", HOST, PORT);
 | |
| 
 | |
| 	return 0;
 | |
| }
 |