Skip to content

Commit

Permalink
Cleaning up the code again
Browse files Browse the repository at this point in the history
  • Loading branch information
wberube committed Aug 29, 2024
1 parent 7a62ac2 commit f42ddfb
Show file tree
Hide file tree
Showing 26 changed files with 56 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ OBJ := $(shell find ./ -name '*.c' | sed -e "s/\.c/\.o/")
$(CC) $< -I src $(OPT) -c -o ../obj/$@

divinus: $(OBJ)
$(CC) $(addprefix ../obj/,$^) -I src -rdynamic $(OPT) -o ../$@
$(CC) $(addprefix ../obj/,$^) -rdynamic $(OPT) -o ../$@
2 changes: 0 additions & 2 deletions src/hal/hisi/v1_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
1 change: 1 addition & 0 deletions src/hal/hisi/v1_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
2 changes: 0 additions & 2 deletions src/hal/hisi/v2_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
1 change: 1 addition & 0 deletions src/hal/hisi/v2_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
2 changes: 0 additions & 2 deletions src/hal/hisi/v3_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
1 change: 1 addition & 0 deletions src/hal/hisi/v3_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
2 changes: 0 additions & 2 deletions src/hal/hisi/v4_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
1 change: 1 addition & 0 deletions src/hal/hisi/v4_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
2 changes: 0 additions & 2 deletions src/hal/inge/t31_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
2 changes: 2 additions & 0 deletions src/hal/inge/t31_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "../config.h"

#include <sys/select.h>

extern char keepRunning;

extern hal_chnstate t31_state[T31_VENC_CHN_NUM];
Expand Down
2 changes: 0 additions & 2 deletions src/hal/plus/cvi_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
1 change: 1 addition & 0 deletions src/hal/plus/cvi_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
2 changes: 0 additions & 2 deletions src/hal/plus/gm_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
2 changes: 0 additions & 2 deletions src/hal/plus/rk_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
1 change: 1 addition & 0 deletions src/hal/plus/rk_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fcntl.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <unistd.h>

extern char keepRunning;
Expand Down
2 changes: 0 additions & 2 deletions src/hal/star/i3_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
2 changes: 0 additions & 2 deletions src/hal/star/i6_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
2 changes: 2 additions & 0 deletions src/hal/star/i6_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

#include "../support.h"

#include <sys/select.h>

extern char keepRunning;

extern int (*i6_aud_cb)(hal_audframe*);
Expand Down
2 changes: 0 additions & 2 deletions src/hal/star/i6c_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
2 changes: 2 additions & 0 deletions src/hal/star/i6c_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "i6c_venc.h"
#include "i6c_vif.h"

#include <sys/select.h>

extern char keepRunning;

extern int (*i6c_aud_cb)(hal_audframe*);
Expand Down
2 changes: 0 additions & 2 deletions src/hal/star/i6f_common.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#pragma once

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>

#include "../symbols.h"
#include "../types.h"
Expand Down
2 changes: 2 additions & 0 deletions src/hal/star/i6f_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "i6f_venc.h"
#include "i6f_vif.h"

#include <sys/select.h>

extern char keepRunning;

extern int (*i6f_aud_cb)(hal_audframe*);
Expand Down
37 changes: 34 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,48 @@
#include <unistd.h>

rtsp_handle rtspHandle;
char graceful = 0;
char graceful = 0, keepRunning = 1;

void handle_error(int signo) {
write(STDERR_FILENO, "Error occurred! Quitting...\n", 28);
keepRunning = 0;
exit(EXIT_FAILURE);
}

void handle_exit(int signo) {
write(STDERR_FILENO, "Graceful shutdown...\n", 21);
keepRunning = 0;
graceful = 1;
}

int main(int argc, char *argv[]) {
{
char signal_error[] = {SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV};
char signal_exit[] = {SIGINT, SIGQUIT, SIGTERM};
char signal_null[] = {EPIPE, SIGPIPE};

for (char *s = signal_error; s < (&signal_error)[1]; s++)
signal(*s, handle_error);
for (char *s = signal_exit; s < (&signal_exit)[1]; s++)
signal(*s, handle_exit);
for (char *s = signal_null; s < (&signal_null)[1]; s++)
signal(*s, NULL);
}

hal_identify();

if (!*family)
HAL_ERROR("hal", "Unsupported chip family! Quitting...\n");

fprintf(stderr, "Divinus for %s\n", family);
fprintf(stderr, "\033[7m Divinus for %s \033[0m\n", family);
fprintf(stderr, "Chip ID: %s\n", chip);

if (parse_app_config() != CONFIG_OK)
HAL_ERROR("hal", "Can't load app config 'divinus.yaml'\n");

if (app_config.watchdog)
watchdog_start(app_config.watchdog);

if (app_config.mdns_enable)
start_mdns();

Expand Down Expand Up @@ -75,9 +103,12 @@ int main(int argc, char *argv[]) {
if (app_config.mdns_enable)
stop_mdns();

if (app_config.watchdog)
watchdog_stop();

if (!graceful)
restore_app_config();

fprintf(stderr, "Main thread is shutting down...\n");
return EXIT_SUCCESS;
}
}
29 changes: 3 additions & 26 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
IMPORT_STR(.rodata, "../res/index.html", indexhtml);
extern const char indexhtml[];

char keepRunning = 1;

enum StreamType {
STREAM_H26X,
STREAM_JPEG,
Expand Down Expand Up @@ -1124,11 +1122,11 @@ void *server_thread(void *vargp) {
short result = strtol(value, &remain, 10);
if (remain == value) continue;
t.tv_sec = result;
clock_settime(CLOCK_REALTIME, &t);
clock_settime(0, &t);
}
}
}
clock_gettime(CLOCK_REALTIME, &t);
clock_gettime(0, &t);
int respLen = sprintf(response,
"HTTP/1.1 200 OK\r\n" \
"Content-Type: application/json;charset=UTF-8\r\n" \
Expand All @@ -1153,28 +1151,10 @@ void *server_thread(void *vargp) {
return NULL;
}

void sig_handler(int signo) {
HAL_INFO("server", "Graceful shutdown...\n");
keepRunning = 0;
graceful = 1;
}
void epipe_handler(int signo) { printf("EPIPE\n"); }
void spipe_handler(int signo) { printf("SIGPIPE\n"); }

int server_fd = -1;
pthread_t server_thread_id;

int start_server() {
signal(SIGINT, sig_handler);
signal(SIGQUIT, sig_handler);
signal(SIGTERM, sig_handler);

signal(SIGPIPE, spipe_handler);
signal(EPIPE, epipe_handler);

if (app_config.watchdog)
watchdog_start(app_config.watchdog);

for (uint32_t i = 0; i < MAX_CLIENTS; ++i) {
client_fds[i].socket_fd = -1;
client_fds[i].type = -1;
Expand Down Expand Up @@ -1213,8 +1193,5 @@ int stop_server() {
pthread_mutex_destroy(&client_fds_mutex);
HAL_INFO("server", "Shutting down server...\n");

if (app_config.watchdog)
watchdog_stop();

return EXIT_SUCCESS;
}
}
4 changes: 4 additions & 0 deletions src/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ __attribute__((noreturn)) void __stack_chk_fail(void) {
panic("Stack smashing detected");
#endif
}

void __stack_chk_fail_local(void) {
__stack_chk_fail();
}

0 comments on commit f42ddfb

Please sign in to comment.