Skip to content

Commit

Permalink
Various corrections, continuing the logging modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Jul 11, 2024
1 parent a003747 commit 2d675d8
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 70 deletions.
26 changes: 13 additions & 13 deletions src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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];
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);

Expand Down
10 changes: 2 additions & 8 deletions src/gpio.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "common.h"

#include <errno.h>
#include <fcntl.h>
#include <linux/version.h>
Expand All @@ -14,14 +16,6 @@
#include <string.h>
#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);
Expand Down
4 changes: 1 addition & 3 deletions src/hal/star/i6c_rgn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
2 changes: 1 addition & 1 deletion src/hal/star/i6c_snr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
13 changes: 0 additions & 13 deletions src/media.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
#include "media.h"

#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#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;

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/media.h
Original file line number Diff line number Diff line change
@@ -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 <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>

extern rtsp_handle rtspHandle;

Expand Down
7 changes: 0 additions & 7 deletions src/night.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
#include "night.h"

#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

static bool night_mode = false;
pthread_t nightPid = 0;

Expand Down
7 changes: 7 additions & 0 deletions src/night.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#include "gpio.h"
#include "media.h"

#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

void set_night_mode(bool night);
bool night_mode_is_enabled();
void *night_thread();
Expand Down
46 changes: 21 additions & 25 deletions src/server.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "server.h"

#define tag "[server] "

char keepRunning = 1;

enum StreamType {
Expand Down Expand Up @@ -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!";
Expand All @@ -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,
Expand All @@ -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;
}

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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"); }
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down

0 comments on commit 2d675d8

Please sign in to comment.