Be flexible on setting camera params

This commit is contained in:
Kamil Trzcinski 2022-07-07 15:14:39 +02:00
parent f7e5b0bcbe
commit bb4bbdc6ea
3 changed files with 69 additions and 8 deletions

View File

@ -16,18 +16,43 @@ extern unsigned int html_jmuxer_min_js_len;
camera_t *camera;
void camera_http_option(http_worker_t *worker, FILE *stream)
void *camera_http_set_option(http_worker_t *worker, FILE *stream, const char *key, const char *value, void *headersp)
{
if (strstr(worker->client_method, "autofocus")) {
if (camera && !device_set_option_string(camera->camera, "AfTrigger", "1")) {
http_200(stream, "Auto-focus triggered.\r\n");
} else {
http_500(stream, "Cannot set auto-focus.\r\n");
bool *headers = headersp;
if (!camera) {
if (!*headers) {
http_500(stream, "");
*headers = true;
}
return;
fprintf(stream, "No camera attached.\r\n");
return NULL;
}
http_404(stream, "No option found.\r\n");
if (device_set_option_string(camera->camera, key, value) == 0) {
if (!*headers) {
http_200(stream, "");
*headers = true;
}
fprintf(stream, "The '%s' was set to '%s'.\r\n", key, value);
} else {
if (!*headers) {
http_500(stream, "");
*headers = true;
}
fprintf(stream, "Cannot set '%s' to '%s'.\r\n", key, value);
}
return NULL;
}
void camera_http_option(http_worker_t *worker, FILE *stream)
{
bool headers = false;
http_enum_params(worker, stream, camera_http_set_option, &headers);
if (!headers) {
http_404(stream, "No options passed.\r\n");
}
}
http_method_t http_methods[] = {

View File

@ -52,6 +52,40 @@ error:
return -1;
}
void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, void *opaque)
{
const char *params = strstr(worker->client_method, "?");
if (!params) {
return NULL;
}
void *ret = NULL;
char *start = strdup(params + 1);
char *string = start;
char *option;
// Drop after ` `
if ((option = strstr(start, " ")) != NULL) {
*option = 0;
}
while ((option = strsep(&string, "&")) != NULL) {
char *value = option;
char *key = strsep(&value, "=");
ret = fn(worker, stream, key, value, opaque);
if (ret) {
break;
}
// consume all separators
while (strsep(&value, "="));
}
free(start);
return ret;
}
static void http_process(http_worker_t *worker, FILE *stream)
{
// Read headers

View File

@ -11,6 +11,7 @@ typedef struct buffer_s buffer_t;
typedef struct http_worker_s http_worker_t;
typedef void (*http_method_fn)(struct http_worker_s *worker, FILE *stream);
typedef void *(*http_param_fn)(struct http_worker_s *worker, FILE *stream, const char *key, const char *value, void *opaque);
#define BUFSIZE 256
@ -48,6 +49,7 @@ void http_content(http_worker_t *worker, FILE *stream);
void http_200(FILE *stream, const char *data);
void http_404(FILE *stream, const char *data);
void http_500(FILE *stream, const char *data);
void *http_enum_params(http_worker_t *worker, FILE *stream, http_param_fn fn, void *opaque);
// M-JPEG
void http_snapshot(http_worker_t *worker, FILE *stream);