From 2d675d8b0115a127a2baebd9774d9bf5e50c5c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Thu, 11 Jul 2024 13:58:14 -0400 Subject: [PATCH] Various corrections, continuing the logging modifications --- src/gpio.c | 26 ++++++++++++------------ src/gpio.h | 10 ++------- src/hal/star/i6c_rgn.h | 4 +--- src/hal/star/i6c_snr.h | 2 +- src/media.c | 13 ------------ src/media.h | 10 +++++++++ src/night.c | 7 ------- src/night.h | 7 +++++++ src/server.c | 46 +++++++++++++++++++----------------------- 9 files changed, 55 insertions(+), 70 deletions(-) diff --git a/src/gpio.c b/src/gpio.c index 9717956..6b8d829 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -18,7 +18,7 @@ int gpio_init(void) { while (*path) { if (access(*path++, F_OK)) continue; if ((fd_gpio = open(*(path - 1), O_RDWR)) < 0) - GPIO_ERROR("Unable to open the device %s!\n", *(path - 1)); + HAL_ERROR("gpio", "Unable to open the device %s!\n", *(path - 1)); else break; } @@ -28,7 +28,7 @@ int gpio_init(void) { int ret = ioctl(fd_gpio, GPIO_GET_CHIPINFO_IOCTL, &info); if (ret == -1) { gpio_deinit(); - GPIO_ERROR("Unable to enumerate the GPIO lines!\n"); + HAL_ERROR("gpio", "Unable to enumerate the GPIO lines!\n"); } else gpio_count = info.lines; return EXIT_SUCCESS; @@ -40,13 +40,13 @@ int gpio_read(char pin, bool *value) { int ret = ioctl(fd_gpio, GPIO_GET_LINEHANDLE_IOCTL, &req); if (ret == -1) - GPIO_ERROR("Unable to request a read on GPIO pin %d (error #%d)!\n", pin, errno); + HAL_ERROR("gpio", "Unable to request a read on GPIO pin %d (error #%d)!\n", pin, errno); struct gpiohandle_data data; ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data); if (ret == -1) { close(req.fd); - GPIO_ERROR("Unable to read the value of GPIO pin %d (error #%d)!\n", pin, errno); + HAL_ERROR("gpio", "Unable to read the value of GPIO pin %d (error #%d)!\n", pin, errno); } *value = data.values[0]; @@ -60,7 +60,7 @@ int gpio_write(char pin, bool value) { int ret = ioctl(fd_gpio, GPIO_GET_LINEHANDLE_IOCTL, &req); if (ret == -1) - GPIO_ERROR("Unable to request a write on GPIO pin %d (error #%d)!\n", pin, errno); + HAL_ERROR("gpio", "Unable to request a write on GPIO pin %d (error #%d)!\n", pin, errno); close(req.fd); @@ -78,10 +78,10 @@ static inline int gpio_direction(char pin, char *mode) { sprintf(path, "/sys/class/gpio/gpio%d/direction", pin); int fd = open(path, O_WRONLY); if (!fd) - GPIO_ERROR("Unable to control the direction of GPIO pin %d!\n", pin); + HAL_ERROR("gpio", "Unable to control the direction of GPIO pin %d!\n", pin); if (write(fd, mode, strlen(mode)) < 0) { close(fd); - GPIO_ERROR("Unable to set the direction of GPIO pin %d!\n", pin); + HAL_ERROR("gpio", "Unable to set the direction of GPIO pin %d!\n", pin); } close(fd); @@ -93,13 +93,13 @@ static inline int gpio_export(char pin, bool create) { int fd = open(create ? "/sys/class/gpio/export" : "/sys/class/gpio/unexport", O_WRONLY); if (!fd) - GPIO_ERROR("Unable to (un)export a GPIO pin!\n"); + HAL_ERROR("gpio", "Unable to (un)export a GPIO pin!\n"); char val[4]; sprintf(val, "%d", pin); if (write(fd, val, strlen(val)) < 0) { close(fd); - GPIO_ERROR("Unable to %s GPIO pin %d!\n", + HAL_ERROR("gpio", "Unable to %s GPIO pin %d!\n", create ? "export" : "unexport", pin); } @@ -115,14 +115,14 @@ int gpio_read(char pin, bool *value) { sprintf(path, "/sys/class/gpio/gpio%d/value", pin); int fd = open(path, O_RDONLY); if (!fd) - GPIO_ERROR("Unable to read from GPIO pin %d!\n", pin); + HAL_ERROR("gpio", "Unable to read from GPIO pin %d!\n", pin); char val = 0; lseek(fd, 0, SEEK_SET); read(fd, &val, 0); if (!val) { close(fd); - GPIO_ERROR("Unable to read from GPIO pin %d!\n", pin); + HAL_ERROR("gpio", "Unable to read from GPIO pin %d!\n", pin); } *value = val - 0x30; close(fd); @@ -141,12 +141,12 @@ int gpio_write(char pin, bool value) { sprintf(path, "/sys/class/gpio/gpio%d/value", pin); int fd = open(path, O_WRONLY); if (!fd) - GPIO_ERROR("Unable to write to GPIO pin %d!\n", pin); + HAL_ERROR("gpio", "Unable to write to GPIO pin %d!\n", pin); char val = value ? '1' : '0'; if (write(fd, &val, 1) < 0) { close(fd); - GPIO_ERROR("Unable to write to GPIO pin %d!\n", pin); + HAL_ERROR("gpio", "Unable to write to GPIO pin %d!\n", pin); } close(fd); diff --git a/src/gpio.h b/src/gpio.h index 1193910..6a9e013 100644 --- a/src/gpio.h +++ b/src/gpio.h @@ -1,3 +1,5 @@ +#include "common.h" + #include #include #include @@ -14,14 +16,6 @@ #include #endif -#define GPIO_ERROR(x, ...) \ - do { \ - fprintf(stderr, "[gpio] \033[31m"); \ - fprintf(stderr, (x), ##__VA_ARGS__); \ - fprintf(stderr, "\033[0m"); \ - return EXIT_FAILURE; \ - } while (0) - void gpio_deinit(void); int gpio_init(void); int gpio_read(char pin, bool *value); diff --git a/src/hal/star/i6c_rgn.h b/src/hal/star/i6c_rgn.h index ec19d5a..7b30ae0 100644 --- a/src/hal/star/i6c_rgn.h +++ b/src/hal/star/i6c_rgn.h @@ -137,10 +137,8 @@ static int i6c_rgn_load(i6c_rgn_impl *rgn_lib) { return EXIT_FAILURE; if (!(rgn_lib->fnGetChannelConfig = (int(*)(unsigned short chip, unsigned int handle, i6c_sys_bind *dest, i6c_rgn_chn *config)) - hal_symbol_load("i6c_rgn", rgn_lib->handle, "MI_RGN_GetDisplayAttr"))) { - fprintf(stderr, "[i6c_rgn] Failed to acquire symbol MI_RGN_GetDisplayAttr!\n"); + hal_symbol_load("i6c_rgn", rgn_lib->handle, "MI_RGN_GetDisplayAttr"))) return EXIT_FAILURE; - } if (!(rgn_lib->fnSetChannelConfig = (int(*)(unsigned short chip, unsigned int handle, i6c_sys_bind *dest, i6c_rgn_chn *config)) hal_symbol_load("i6c_rgn", rgn_lib->handle, "MI_RGN_SetDisplayAttr"))) diff --git a/src/hal/star/i6c_snr.h b/src/hal/star/i6c_snr.h index 5a3a2b3..8ba6ed6 100644 --- a/src/hal/star/i6c_snr.h +++ b/src/hal/star/i6c_snr.h @@ -107,7 +107,7 @@ static int i6c_snr_load(i6c_snr_impl *snr_lib) { return EXIT_FAILURE; if (!(snr_lib->fnEnable = (int(*)(unsigned int sensor)) - hal_symbol_load("i6c_snr", snr_lib->handle, "MI_SNR_Enable"))); + hal_symbol_load("i6c_snr", snr_lib->handle, "MI_SNR_Enable"))) return EXIT_FAILURE; if (!(snr_lib->fnSetFramerate = (int(*)(unsigned int sensor, unsigned int framerate)) diff --git a/src/media.c b/src/media.c index 9290543..1f711d7 100644 --- a/src/media.c +++ b/src/media.c @@ -1,17 +1,5 @@ #include "media.h" -#include -#include -#include -#include -#include -#include - -#include "error.h" -#include "http_post.h" -#include "jpeg.h" -#include "server.h" - pthread_mutex_t aencMtx, chnMtx, mp4Mtx; pthread_t aencPid = 0, audPid = 0, ispPid = 0, vidPid = 0; @@ -477,7 +465,6 @@ int enable_mp4(void) { if (ret) HAL_ERROR("media", "Creating encoder %d failed with %#x!\n%s\n", index, ret, errstr(ret)); - return EXIT_FAILURE; mp4_set_config(app_config.mp4_width, app_config.mp4_height, app_config.mp4_fps, app_config.audio_enable ? HAL_AUDCODEC_MP3 : HAL_AUDCODEC_UNSPEC, diff --git a/src/media.h b/src/media.h index 749d8de..febb2f7 100644 --- a/src/media.h +++ b/src/media.h @@ -1,10 +1,20 @@ #pragma once #include "common.h" +#include "error.h" +#include "http_post.h" #include "lib/shine/layer3.h" +#include "jpeg.h" #include "rtsp/rtsp_server.h" +#include "server.h" +#include +#include +#include +#include +#include #include +#include extern rtsp_handle rtspHandle; diff --git a/src/night.c b/src/night.c index 495a2db..26ce90f 100644 --- a/src/night.c +++ b/src/night.c @@ -1,12 +1,5 @@ #include "night.h" -#include -#include -#include -#include -#include -#include - static bool night_mode = false; pthread_t nightPid = 0; diff --git a/src/night.h b/src/night.h index c1c3dfc..81e4c36 100644 --- a/src/night.h +++ b/src/night.h @@ -4,6 +4,13 @@ #include "gpio.h" #include "media.h" +#include +#include +#include +#include +#include +#include + void set_night_mode(bool night); bool night_mode_is_enabled(); void *night_thread(); diff --git a/src/server.c b/src/server.c index b367e31..9571acb 100644 --- a/src/server.c +++ b/src/server.c @@ -1,7 +1,5 @@ #include "server.h" -#define tag "[server] " - char keepRunning = 1; enum StreamType { @@ -295,13 +293,12 @@ struct jpegtask { void *send_jpeg_thread(void *vargp) { struct jpegtask task = *((struct jpegtask *)vargp); hal_jpegdata jpeg = {0}; - printf( - tag "Requesting a JPEG snapshot (%ux%u, qfactor %u, color2Gray %d)...\n", + HAL_INFO("server", "Requesting a JPEG snapshot (%ux%u, qfactor %u, color2Gray %d)...\n", task.width, task.height, task.qfactor, task.color2Gray); int ret = jpeg_get(task.width, task.height, task.qfactor, task.color2Gray, &jpeg); if (ret) { - printf("Failed to receive a JPEG snapshot...\n"); + HAL_DANGER("server", "Failed to receive a JPEG snapshot...\n"); static char response[] = "HTTP/1.1 503 Internal Error\r\nContent-Length: 11\r\nConnection: " "close\r\n\r\nHello, 503!"; @@ -311,7 +308,7 @@ void *send_jpeg_thread(void *vargp) { close_socket_fd(task.client_fd); return NULL; } - printf(tag "JPEG snapshot has been received!\n"); + HAL_INFO("server", "JPEG snapshot has been received!\n"); char buf[1024]; int buf_len = sprintf( buf, @@ -323,7 +320,7 @@ void *send_jpeg_thread(void *vargp) { send_to_fd(task.client_fd, "\r\n", 2); close_socket_fd(task.client_fd); free(jpeg.data); - printf(tag "JPEG snapshot has been sent!\n"); + HAL_INFO("server", "JPEG snapshot has been sent!\n"); return NULL; } @@ -497,7 +494,7 @@ void parse_request(int client_fd, char *request) { uri = strtok_r(NULL, " \t", &state); prot = strtok_r(NULL, " \t\r\n", &state); - fprintf(stderr, tag "\x1b[32mNew request: (%s) %s\n" + HAL_INFO("server", "\x1b[32mNew request: (%s) %s\n" " Received from: %s\x1b[0m\n", method, uri, inet_ntoa(client_sock.sin_addr)); @@ -532,10 +529,10 @@ void parse_request(int client_fd, char *request) { while (l && total < paysize) { received = recv(client_fd, request + total, REQSIZE - total, 0); if (received < 0) { - fputs(tag "recv() error\n", stderr); + HAL_WARNING("server", "recv() error\n", stderr); break; } else if (!received) { - fputs(tag "Client disconnected unexpectedly\n", stderr); + HAL_WARNING("server", "Client disconnected unexpectedly\n", stderr); break; } total += received; @@ -548,7 +545,7 @@ void *server_thread(void *vargp) { int server_fd = *((int *)vargp); int enable = 1; if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) { - printf(tag "setsockopt(SO_REUSEADDR) failed"); + HAL_WARNING("server", "setsockopt(SO_REUSEADDR) failed"); fflush(stdout); } struct sockaddr_in server, client; @@ -557,7 +554,7 @@ void *server_thread(void *vargp) { server.sin_addr.s_addr = htonl(INADDR_ANY); int res = bind(server_fd, (struct sockaddr *)&server, sizeof(server)); if (res != 0) { - printf(tag "%s (%d)\n", strerror(errno), errno); + HAL_DANGER("server", "%s (%d)\n", strerror(errno), errno); keepRunning = 0; close_socket_fd(server_fd); return NULL; @@ -575,9 +572,9 @@ void *server_thread(void *vargp) { total = 0; received = recv(client_fd, request, REQSIZE, 0); if (received < 0) - fputs(tag "recv() error\n", stderr); + HAL_WARNING("server", "recv() error\n", stderr); else if (!received) - fputs(tag "Client disconnected unexpectedly\n", stderr); + HAL_WARNING("server", "Client disconnected unexpectedly\n", stderr); total += received; if (total <= 0) continue; @@ -1192,12 +1189,12 @@ void *server_thread(void *vargp) { free(request); close_socket_fd(server_fd); - printf(tag "Thread has exited\n"); + HAL_INFO("server", "Thread has exited\n"); return NULL; } void sig_handler(int signo) { - printf(tag "Graceful shutdown...\n"); + HAL_INFO("server", "Graceful shutdown...\n"); keepRunning = 0; } void epipe_handler(int signo) { printf("EPIPE\n"); } @@ -1232,14 +1229,13 @@ int start_server() { size_t stacksize; pthread_attr_getstacksize(&thread_attr, &stacksize); size_t new_stacksize = app_config.web_server_thread_stack_size + REQSIZE; - if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) { - printf(tag "Can't set stack size %zu\n", new_stacksize); - } - pthread_create( - &server_thread_id, &thread_attr, server_thread, (void *)&server_fd); - if (pthread_attr_setstacksize(&thread_attr, stacksize)) { - printf(tag "Can't set stack size %zu\n", stacksize); - } + if (pthread_attr_setstacksize(&thread_attr, new_stacksize)) + HAL_WARNING("server", "Can't set stack size %zu\n", new_stacksize); + if (pthread_create( + &server_thread_id, &thread_attr, server_thread, (void *)&server_fd)) + HAL_ERROR("server", "Starting the server thread failed!\n"); + if (pthread_attr_setstacksize(&thread_attr, stacksize)) + HAL_DANGER("server", "Can't set stack size %zu\n", stacksize); pthread_attr_destroy(&thread_attr); } @@ -1254,7 +1250,7 @@ int stop_server() { pthread_join(server_thread_id, NULL); pthread_mutex_destroy(&client_fds_mutex); - printf(tag "Shutting down server...\n"); + HAL_INFO("server", "Shutting down server...\n"); if (app_config.watchdog) watchdog_stop();