refactoring

This commit is contained in:
Maxim Devaev
2026-07-08 11:03:23 +03:00
parent 41a8eb46ca
commit 6dd5b8709f
10 changed files with 26 additions and 22 deletions

View File

@@ -38,7 +38,7 @@ bool us_au_probe(const char *name) {
// the existence of it in /proc/asound/. // the existence of it in /proc/asound/.
// It's enough for our case. // It's enough for our case.
if (name == NULL) { if (!us_str_is_ok(name)) {
return false; return false;
} }

View File

@@ -64,7 +64,7 @@ us_config_s *us_config_init(const char *config_dir_path) {
if ((config->acap_dev_name = _get_value(jcfg, "acap", "device")) != NULL) { if ((config->acap_dev_name = _get_value(jcfg, "acap", "device")) != NULL) {
config->acap_hz = _get_uint(jcfg, "acap", "sampling_rate", 0); config->acap_hz = _get_uint(jcfg, "acap", "sampling_rate", 0);
config->tc358743_dev_path = _get_value(jcfg, "acap", "tc358743"); config->tc358743_dev_path = _get_value(jcfg, "acap", "tc358743");
if (config->acap_hz == 0 && config->tc358743_dev_path == NULL) { if (config->acap_hz == 0 && !us_str_is_ok(config->tc358743_dev_path)) {
US_LOG_ERROR("Either acap.sampling_rate or acap.tc358743 required"); US_LOG_ERROR("Either acap.sampling_rate or acap.tc358743 required");
goto error; goto error;
} }
@@ -95,7 +95,7 @@ void us_config_destroy(us_config_s *config) {
static char *_get_value(janus_config *jcfg, const char *section, const char *option) { static char *_get_value(janus_config *jcfg, const char *section, const char *option) {
janus_config_category *section_obj = janus_config_get_create(jcfg, NULL, janus_config_type_category, section); janus_config_category *section_obj = janus_config_get_create(jcfg, NULL, janus_config_type_category, section);
janus_config_item *option_obj = janus_config_get(jcfg, section_obj, janus_config_type_item, option); janus_config_item *option_obj = janus_config_get(jcfg, section_obj, janus_config_type_item, option);
if (option_obj == NULL || option_obj->value == NULL || option_obj->value[0] == '\0') { if (option_obj == NULL || !us_str_is_ok(option_obj->value)) {
return NULL; return NULL;
} }
return us_strdup(option_obj->value); return us_strdup(option_obj->value);

View File

@@ -192,10 +192,7 @@ static int _get_acap_hz(uint *hz) {
*hz = _g_config->acap_hz; *hz = _g_config->acap_hz;
return 0; return 0;
} }
if (_g_config->tc358743_dev_path == NULL) { US_A(us_str_is_ok(_g_config->tc358743_dev_path));
US_LOG_ERROR("No configured sampling rate");
return -1;
}
const int fd = open(_g_config->tc358743_dev_path, O_RDWR); const int fd = open(_g_config->tc358743_dev_path, O_RDWR);
if (fd < 0) { if (fd < 0) {
US_LOG_PERROR("Can't open TC358743 V4L2 device"); US_LOG_PERROR("Can't open TC358743 V4L2 device");
@@ -408,7 +405,10 @@ static int _plugin_init(janus_callbacks *gw, const char *config_dir_path) {
US_LOGGING_INIT; US_LOGGING_INIT;
US_LOG_INFO("Initializing PiKVM uStreamer plugin %s ...", US_VERSION); US_LOG_INFO("Initializing PiKVM uStreamer plugin %s ...", US_VERSION);
if (gw == NULL || config_dir_path == NULL || ((_g_config = us_config_init(config_dir_path)) == NULL)) { if (gw == NULL || !us_str_is_ok(config_dir_path)) {
return -1;
}
if ((_g_config = us_config_init(config_dir_path)) == NULL) {
return -1; return -1;
} }
_g_gw = gw; _g_gw = gw;

View File

@@ -177,14 +177,14 @@ int main(int argc, char *argv[]) {
# undef OPT_NUMBER # undef OPT_NUMBER
# undef OPT_SET # undef OPT_SET
if (sink_name == NULL || sink_name[0] == '\0') { if (!us_str_is_ok(sink_name)) {
puts("Missing option --sink. See --help for details."); puts("Missing option --sink. See --help for details.");
return 1; return 1;
} }
_output_context_s ctx = {0}; _output_context_s ctx = {0};
if (out_path && out_path[0] != '\0') { if (us_str_is_ok(out_path)) {
if ((ctx.v_out = (void*)us_output_file_init(out_path, out_json)) == NULL) { if ((ctx.v_out = (void*)us_output_file_init(out_path, out_json)) == NULL) {
return 1; return 1;
} }

View File

@@ -197,7 +197,7 @@ int us_capture_open(us_capture_s *cap) {
} }
_LOG_DEBUG("Capture device fd=%d opened", run->fd); _LOG_DEBUG("Capture device fd=%d opened", run->fd);
if (cap->dv_timings && cap->media_path[0] != '\0') { if (cap->dv_timings && us_str_is_ok(cap->media_path)) {
if (_capture_open_media_pads(cap) < 0) { if (_capture_open_media_pads(cap) < 0) {
goto error_no_device; goto error_no_device;
} }
@@ -232,7 +232,7 @@ int us_capture_open(us_capture_s *cap) {
if (cap->dv_timings && _capture_open_dv_timings(cap, true) < 0) { if (cap->dv_timings && _capture_open_dv_timings(cap, true) < 0) {
goto error; goto error;
} }
if (cap->dv_timings && cap->media_path[0] != '\0') { if (cap->dv_timings && us_str_is_ok(cap->media_path)) {
US_ARRAY_ITERATE(run->media_pads, 0, pad, { US_ARRAY_ITERATE(run->media_pads, 0, pad, {
if (pad->fd >= -1 && _capture_open_media_format(cap, pad) < 0) { if (pad->fd >= -1 && _capture_open_media_format(cap, pad) < 0) {
goto error; goto error;

View File

@@ -100,7 +100,7 @@ INLINE void us_thread_get_name(char *name) { // Always required for logging
|| (defined(__OpenBSD__) && defined(OpenBSD) && OpenBSD >= 201905) \ || (defined(__OpenBSD__) && defined(OpenBSD) && OpenBSD >= 201905) \
|| defined(__DragonFly__) || defined(__DragonFly__)
pthread_get_name_np(pthread_self(), name, US_THREAD_NAME_SIZE); // Also null-terminated pthread_get_name_np(pthread_self(), name, US_THREAD_NAME_SIZE); // Also null-terminated
if (name[0] != '\0') { if (us_str_is_ok(name)) {
retval = 0; retval = 0;
} }
# else # else

View File

@@ -92,6 +92,10 @@ INLINE char *us_strdup(const char *str) {
return new; return new;
} }
INLINE bool us_str_is_ok(const char *str) {
return (str != NULL && str[0] != '\0');
}
INLINE const char *us_bool_to_string(bool flag) { INLINE const char *us_bool_to_string(bool flag) {
return (flag ? "true" : "false"); return (flag ? "true" : "false");
} }

View File

@@ -183,7 +183,7 @@ int us_server_listen(us_server_s *server) {
us_stream_s *const stream = server->stream; us_stream_s *const stream = server->stream;
{ {
if (server->static_path[0] != '\0') { if (us_str_is_ok(server->static_path)) {
_LOG_INFO("Enabling the file server: %s", server->static_path); _LOG_INFO("Enabling the file server: %s", server->static_path);
evhttp_set_gencb(run->http, _http_callback_static, (void*)server); evhttp_set_gencb(run->http, _http_callback_static, (void*)server);
} else { } else {
@@ -202,7 +202,7 @@ int us_server_listen(us_server_s *server) {
evhttp_set_timeout(run->http, server->timeout); evhttp_set_timeout(run->http, server->timeout);
if (server->user[0] != '\0') { if (us_str_is_ok(server->user)) {
char *encoded_token = NULL; char *encoded_token = NULL;
char *raw_token; char *raw_token;
@@ -216,7 +216,7 @@ int us_server_listen(us_server_s *server) {
_LOG_INFO("Using HTTP basic auth"); _LOG_INFO("Using HTTP basic auth");
} }
if (server->unix_path[0] != '\0') { if (us_str_is_ok(server->unix_path)) {
_LOG_DEBUG("Binding server to UNIX socket '%s' ...", server->unix_path); _LOG_DEBUG("Binding server to UNIX socket '%s' ...", server->unix_path);
if ((run->ext_fd = us_evhttp_bind_unix( if ((run->ext_fd = us_evhttp_bind_unix(
run->http, run->http,
@@ -264,7 +264,7 @@ static int _http_preprocess_request(struct evhttp_request *req, us_server_s *ser
atomic_store(&server->stream->run->http->last_req_ts, us_get_now_monotonic()); atomic_store(&server->stream->run->http->last_req_ts, us_get_now_monotonic());
if (server->allow_origin[0] != '\0') { if (us_str_is_ok(server->allow_origin)) {
const char *const cors_headers = us_evhttp_get_header(req, "Access-Control-Request-Headers"); const char *const cors_headers = us_evhttp_get_header(req, "Access-Control-Request-Headers");
const char *const cors_method = us_evhttp_get_header(req, "Access-Control-Request-Method"); const char *const cors_method = us_evhttp_get_header(req, "Access-Control-Request-Method");
@@ -666,7 +666,7 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
if (client->need_initial) { if (client->need_initial) {
_A_EVBUFFER_ADD_PRINTF(buf, "HTTP/1.0 200 OK" RN); _A_EVBUFFER_ADD_PRINTF(buf, "HTTP/1.0 200 OK" RN);
if (client->server->allow_origin[0] != '\0') { if (us_str_is_ok(client->server->allow_origin)) {
const char *const cors_headers = us_evhttp_get_header(client->req, "Access-Control-Request-Headers"); const char *const cors_headers = us_evhttp_get_header(client->req, "Access-Control-Request-Headers");
const char *const cors_method = us_evhttp_get_header(client->req, "Access-Control-Request-Method"); const char *const cors_method = us_evhttp_get_header(client->req, "Access-Control-Request-Method");
@@ -693,8 +693,8 @@ static void _http_callback_stream_write(struct bufferevent *buf_event, void *v_c
"Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY RN "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY RN
RN RN
"--" BOUNDARY RN, "--" BOUNDARY RN,
(server->instance_id[0] == '\0' ? "" : "_"), (us_str_is_ok(server->instance_id) ? "_" : ""),
server->instance_id, (us_str_is_ok(server->instance_id) ? server->instance_id : ""),
(client->key != NULL ? client->key : "0"), (client->key != NULL ? client->key : "0"),
client->id); client->id);

View File

@@ -39,7 +39,7 @@ char *us_find_static_file_path(const char *root_path, const char *req_path) {
char *path = NULL; char *path = NULL;
char *const simplified_path = us_simplify_request_path(req_path); char *const simplified_path = us_simplify_request_path(req_path);
if (simplified_path[0] == '\0') { if (!us_str_is_ok(simplified_path)) {
US_LOG_VERBOSE("HTTP: Invalid request path %s to static", req_path); US_LOG_VERBOSE("HTTP: Invalid request path %s to static", req_path);
goto error; goto error;
} }

View File

@@ -569,7 +569,7 @@ int us_options_parse(
US_LOG_INFO("Starting PiKVM uStreamer %s ...", US_VERSION); US_LOG_INFO("Starting PiKVM uStreamer %s ...", US_VERSION);
# define ADD_SINK(x_label, x_prefix) { \ # define ADD_SINK(x_label, x_prefix) { \
if (x_prefix##_name && x_prefix##_name[0] != '\0') { \ if (us_str_is_ok(x_prefix##_name)) { \
opts->x_prefix = us_memsink_init_opened( \ opts->x_prefix = us_memsink_init_opened( \
x_label, \ x_label, \
x_prefix##_name, \ x_prefix##_name, \