From da00f4eb7bf4cdcd463756ce0b73e5a4540bf9ff Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Wed, 1 May 2024 21:58:28 -0400 Subject: Test server --- Makefile | 3 +-- build/artifacts/main.o | Bin 1440 -> 2944 bytes build/osm-thermald | Bin 20352 -> 20944 bytes src/main.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index b3ce19a..5e85957 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,6 @@ SRC_DIR = src BUILD_DIR = build OBJ_DIR = $(BUILD_DIR)/artifacts -INCLUDE_DIR = ./include SRCS = $(notdir $(wildcard $(SRC_DIR)/*.c)) OBJS = $(addsuffix .o, $(basename $(SRCS))) @@ -13,7 +12,7 @@ build: build_dir $(OBJS) $(CC) -lopensmarts -o $(BUILD_DIR)/osm-thermald $(addprefix $(OBJ_DIR)/, $(OBJS)) %.o: $(SRC_DIR)/%.c - $(CC) $(CFLAGS) -c -I$(INCLUDE_DIR) -o $(BUILD_DIR)/artifacts/$@ $< + $(CC) $(CFLAGS) -c -o $(BUILD_DIR)/artifacts/$@ $< build_dir: mkdir -p $(BUILD_DIR) diff --git a/build/artifacts/main.o b/build/artifacts/main.o index 5a4750b..977df23 100644 Binary files a/build/artifacts/main.o and b/build/artifacts/main.o differ diff --git a/build/osm-thermald b/build/osm-thermald index c61aa94..f32e6b5 100755 Binary files a/build/osm-thermald and b/build/osm-thermald differ diff --git a/src/main.c b/src/main.c index 8b9582c..5db59d4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,69 @@ -#include #include +#include +#include +#include +#include + #include +#include + +volatile double temperature = 0; +volatile double humidity = 0; + +int _sock_connect(void *data) +{ + int fd = (uintptr_t)data; + + double temp = temperature; + write(fd, &temp, sizeof(temp)); + close(fd); + + return 0; +} + +int _temp_poll(void *data) +{ + // struct timespec sleeper = {.tv_sec = 1}; + while(1) + { + temperature = 0.1; + // humidity += 0.1; + //printf("Hello"); + + // while(thrd_sleep(&sleeper, NULL)) + // {} + } + thrd_exit(0); +} + int main(int argc, char **argv) { int fd = osm_open_onboard(NULL); - if (fd >= 0) + + if (fd == -1) { - close(fd); + perror("Error opening socket for osm-thermald"); + return 1; } + + thrd_t poll; + thrd_create(&poll, (thrd_start_t) &_temp_poll, 0); + thrd_detach(poll); + + Vector threads = osm_listen_and_accept(fd, _sock_connect); + close(fd); + + // Close all threads + for(unsigned int i = 0; i < threads.count; i++) + { + thrd_t *thread = vect_get(&threads, i); + thrd_join(*thread, NULL); + } + + // Cleanup + vect_end(&threads); + thrd_join(poll, NULL); + return 0; } -- cgit v1.2.3