You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
8.7 KiB

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "miniwebcam.h"
#include "configuration.h"
int get_resolution(const char *text, int *width, int *height) {
char *c;
if ((c = strchr ((char *)text, 'X')) == NULL)
if ((c = strchr ((char *)text, 'x')) == NULL) return 0;
*width = atoi (text);
*height = atoi (c+1);
return 1;
}
Configuration config;
/*
* Setup default values for all configuration options
*/
Configuration::Configuration() {
http_port = DEFAULT_HTTP_PORT;
https_port = DEFAULT_HTTPS_PORT;
filename = DEFAULT_CONFIG_FILE;
runasdaemon = 0;
vdev_iomode = DEFAULT_VDEV_IOMODE;
vdev_device = DEFAULT_VDEV_DEVICE;
vdev_format = "";
vdev_dumpfile = "";
vdev_dumppath = "";
vdev_cdevice = "";
ssl_key = DEFAULT_SSL_KEY;
ssl_cert = DEFAULT_SSL_CERT;
initflags = 0;
vdev_height = DEFAULT_VDEV_HEIGHT;
vdev_width = DEFAULT_VDEV_WIDTH;
web_height = -1;
web_width = -1;
web_imagerefresh = 250;
};
Configuration::~Configuration() {
};
/*
* print current configuration
*/
int Configuration::PrintConfig() {
JSONParse jp;
//
// save current language and set to default C
std::string savedlocale = setlocale(LC_ALL, NULL);
setlocale (LC_ALL, "C");
jp.AddObject("http_port", http_port);
jp.AddObject("https_port", https_port);
jp.AddObject("ssl-key-file", ssl_key);
jp.AddObject("ssl-cert-file", ssl_cert);
jp.AddObject("vdev-iomode", vdev_iomode);
jp.AddObject("vdev-device", vdev_device);
jp.AddObject("vdev-cdevice", vdev_cdevice);
jp.AddObject("vdev-size", to_string(vdev_width) + "x" + to_string(vdev_height));
jp.AddObject("vdev-format", vdev_format);
jp.AddObject("web-size" , to_string(web_width) + "x" + to_string(web_height));
jp.AddObject("web-refresh", web_imagerefresh);
jp.AddObject("vdev-dumpath", vdev_dumppath);
//
// output the json string
printf ("%s\n", jp.ToString().c_str());
//
// restore language
setlocale (LC_ALL, savedlocale.c_str());
return 1;
};
int Configuration::LoadFile(std::string fn) {
JSONParse jp;
int i;
std::string s;
//
// save current language and set to default C
std::string savedlocale = setlocale(LC_ALL, NULL);
setlocale (LC_ALL, "C");
// read from file
if (jp.LoadFromFile(fn) != 0) {
return 0;
}
if (jp.GetValueInt("http_port", &i)) http_port = i;
if (jp.GetValueInt("https_port", &i)) https_port = i;
if (jp.GetValueString("ssl-key-file", &s)) ssl_key = s;
if (jp.GetValueString("ssl-cert-file", &s)) ssl_cert = s;
if (jp.GetValueString("web-size", &s))
if (get_resolution(s.c_str(), &web_width, &web_height) == 0) {
debug ("Configfile: %s Wrong web-size: %s", fn.c_str(), s.c_str());
}
if (jp.GetValueInt("web-refresh", &i)) web_imagerefresh = i;
if (jp.GetValueString("vdev-device", &s)) vdev_device = s;
if (jp.GetValueString("vdev-cdevice", &s)) vdev_cdevice = s;
if (jp.GetValueString("vdev-format", &s)) vdev_format = s;
if (jp.GetValueString("vdev-dumppath", &s)) vdev_dumppath = s;
if (jp.GetValueInt("vdev-iomode", &i)) vdev_iomode = i;
if (jp.GetValueString("vdev-size", &s))
if (get_resolution(s.c_str(), &vdev_width, &vdev_height) == 0) {
debug ("Configfile: %s Wrong vdev-size: %s", fn.c_str(), s.c_str());
}
//
// restore language
setlocale (LC_ALL, savedlocale.c_str());
return 1;
}
int Configuration::LoadArgs(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-F") == 0) runasdaemon = 0;
if (strcmp(argv[i], "-D") == 0) runasdaemon = 1;
if (strcmp(argv[i], "-dump_config") == 0) initflags |= CONF_INITFLAGS_PRINT;
if (strcmp(argv[i], "-config") == 0) {
if (++i < argc) {
filename = argv[i];
}
else
ErrorExit("config file missing", -1);
}
if (strcmp(argv[i], "-h") == 0) initflags |= CONF_INITFLAGS_HELP;
if (strcmp(argv[i], "--help") == 0) initflags |= CONF_INITFLAGS_HELP;
if (strcmp(argv[i], "-http_port") == 0) {
if (++i < argc) {
http_port = atoi(argv[i]);
}
else
ErrorExit("missing port parameter", -1);
}
if (strcmp(argv[i], "-https_port") == 0) {
if (++i < argc) {
https_port = atoi(argv[i]);
}
else
ErrorExit("missing port parameter", -1);
}
if (strcmp(argv[i], "-webrefresh") == 0) {
if (++i < argc) {
web_imagerefresh = atoi(argv[i]);
}
else
ErrorExit("missing ms refresh time", -1);
}
if (strcmp(argv[i], "-websize") == 0) {
if (++i < argc) {
if (get_resolution(argv[i], &web_width, &web_height) == 0)
ErrorExit("wrong web resolution parameter", -1);
}
else
ErrorExit("missing web resolution parameter", -1);
}
if (strcmp(argv[i], "-vdevsize") == 0) {
if (++i < argc) {
if (get_resolution(argv[i], &vdev_width, &vdev_height) == 0)
ErrorExit("wrong video resolution parameter", -1);
}
else
ErrorExit("missing video resolution parameter", -1);
}
if (strcmp(argv[i], "-vdeviomode") == 0) {
if (++i < argc) {
vdev_iomode = atoi(argv[i]);
}
else
ErrorExit("missing iomode parameter", -1);
}
if (strcmp(argv[i], "-vdevformat") == 0) {
if (++i < argc) {
vdev_format = argv[i];
}
else
ErrorExit("missing device parameter", -1);
}
if (strcmp(argv[i], "-vdevdevice") == 0) {
if (++i < argc) {
vdev_device = argv[i];
}
else
ErrorExit("missing device parameter", -1);
}
if (strcmp(argv[i], "-vdevcdevice") == 0) {
if (++i < argc) {
vdev_cdevice = argv[i];
}
else
ErrorExit("missing ctrl (subdev) device parameter", -1);
}
if (strcmp(argv[i], "-vdevdumpfile") == 0) {
if (++i < argc) {
vdev_dumpfile = argv[i];
}
else
ErrorExit("missing dump file parameter", -1);
}
if (strcmp(argv[i], "-vdevdumppath") == 0) {
if (++i < argc) {
vdev_dumppath = argv[i];
}
else
ErrorExit("missing dump path parameter", -1);
}
if (strcmp(argv[i], "-sslkey") == 0) {
if (++i < argc) {
ssl_key = argv[i];
}
else
ErrorExit("missing sslkey file", -1);
}
if (strcmp(argv[i], "-sslcert") == 0) {
if (++i < argc) {
ssl_cert = argv[i];
}
else
ErrorExit("missing sslcertfile", -1);
}
}
return 1;
};
void Configuration::Help() {
printf ("Parameters:\n");
printf (" -F run in foreground\n");
printf (" -D run as daemon\n");
printf ("\n");
printf (" -http_port INT port to listen for http connections\n");
printf (" -https_port INT port to listen for https connections\n");
printf (" -sslkey FILE ssl key file\n");
printf (" -sslcert FILE ssl certfile\n");
printf ("\n");
printf (" -websize INTxINT define the web output resolution\n");
printf (" -webrefresh INT refresh rate for the snapshot\n");
printf ("\n");
printf (" -vdeviomode INT IOMode to read the video data, 0-read, 1-MMap\n");
printf (" -vdevdevice FILE Device File i.e. /dev/video2\n");
printf (" -vdevcdevice FILE Device File i.e. /dev/v4l-subdev2\n");
printf (" -vdevsize INTxINT define video input resolution\n");
printf (" -vdevformat FORMAT 4 Char Format code - see v4l2 documentation (videodev2.h)\n");
printf (" -vdevdumpfile FILE file to read raw image data\n");
printf (" -vdevdumppath PATH path to save dump data to\n");
printf ("\n");
printf (" -config FILE load this configfile\n");
printf (" -dump_config print the config file\n");
printf ("\n");
printf (" -H print this help\n");
printf ("\n\nSample:\n");
printf (" ./miniwebcam -sslkey ./ssl-key.pem -sslcert ./ssl-cert.pem -websize 800x600\n");
}