Skip to content

Commit 6161dc1

Browse files
authored
always free incoming commands from memory (#36)
1 parent b005210 commit 6161dc1

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

main.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
static int pipe_out_fd;
1919
static pthread_mutex_t pipe_out_mutex;
20+
static camera_t *cam;
2021
static text_t *text;
2122
static encoder_t *enc;
2223

@@ -51,6 +52,29 @@ static void on_error() {
5152
pthread_mutex_unlock(&pipe_out_mutex);
5253
}
5354

55+
static bool handle_command(const uint8_t *buf, uint32_t size) {
56+
switch (buf[0]) {
57+
case 'e':
58+
return false;
59+
60+
case 'c':
61+
{
62+
parameters_t params;
63+
bool ok = parameters_unserialize(&params, &buf[1], size-1);
64+
if (!ok) {
65+
printf("skipping reloading parameters since they are invalid: %s\n", parameters_get_error());
66+
return true;
67+
}
68+
69+
camera_reload_params(cam, &params);
70+
encoder_reload_params(enc, &params);
71+
parameters_destroy(&params);
72+
}
73+
}
74+
75+
return true;
76+
}
77+
5478
int main() {
5579
if (getenv("TEST") != NULL) {
5680
printf("test passed\n");
@@ -74,7 +98,6 @@ int main() {
7498
pthread_mutex_init(&pipe_out_mutex, NULL);
7599
pthread_mutex_lock(&pipe_out_mutex);
76100

77-
camera_t *cam;
78101
ok = camera_create(
79102
&params,
80103
on_frame,
@@ -116,26 +139,13 @@ int main() {
116139

117140
while (true) {
118141
uint8_t *buf;
119-
uint32_t n = pipe_read(pipe_in_fd, &buf);
120-
121-
switch (buf[0]) {
122-
case 'e':
123-
return 0;
124-
125-
case 'c':
126-
{
127-
parameters_t params;
128-
bool ok = parameters_unserialize(&params, &buf[1], n-1);
129-
free(buf);
130-
if (!ok) {
131-
printf("skipping reloading parameters since they are invalid: %s\n", parameters_get_error());
132-
continue;
133-
}
134-
135-
camera_reload_params(cam, &params);
136-
encoder_reload_params(enc, &params);
137-
parameters_destroy(&params);
138-
}
142+
uint32_t size = pipe_read(pipe_in_fd, &buf);
143+
144+
bool ok = handle_command(buf, size);
145+
free(buf);
146+
147+
if (!ok) {
148+
break;
139149
}
140150
}
141151

0 commit comments

Comments
 (0)