diff --git a/main.c b/main.c index c0cbf17..64e7257 100644 --- a/main.c +++ b/main.c @@ -17,6 +17,7 @@ static int pipe_out_fd; static pthread_mutex_t pipe_out_mutex; +static camera_t *cam; static text_t *text; static encoder_t *enc; @@ -51,6 +52,29 @@ static void on_error() { pthread_mutex_unlock(&pipe_out_mutex); } +static bool handle_command(const uint8_t *buf, uint32_t size) { + switch (buf[0]) { + case 'e': + return false; + + case 'c': + { + parameters_t params; + bool ok = parameters_unserialize(¶ms, &buf[1], size-1); + if (!ok) { + printf("skipping reloading parameters since they are invalid: %s\n", parameters_get_error()); + return true; + } + + camera_reload_params(cam, ¶ms); + encoder_reload_params(enc, ¶ms); + parameters_destroy(¶ms); + } + } + + return true; +} + int main() { if (getenv("TEST") != NULL) { printf("test passed\n"); @@ -74,7 +98,6 @@ int main() { pthread_mutex_init(&pipe_out_mutex, NULL); pthread_mutex_lock(&pipe_out_mutex); - camera_t *cam; ok = camera_create( ¶ms, on_frame, @@ -116,26 +139,13 @@ int main() { while (true) { uint8_t *buf; - uint32_t n = pipe_read(pipe_in_fd, &buf); - - switch (buf[0]) { - case 'e': - return 0; - - case 'c': - { - parameters_t params; - bool ok = parameters_unserialize(¶ms, &buf[1], n-1); - free(buf); - if (!ok) { - printf("skipping reloading parameters since they are invalid: %s\n", parameters_get_error()); - continue; - } - - camera_reload_params(cam, ¶ms); - encoder_reload_params(enc, ¶ms); - parameters_destroy(¶ms); - } + uint32_t size = pipe_read(pipe_in_fd, &buf); + + bool ok = handle_command(buf, size); + free(buf); + + if (!ok) { + break; } }