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.

211 lines
6.3 KiB

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "miniwebcam.h"
#include "configuration.h"
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;
ssl_key = DEFAULT_SSL_KEY;
ssl_cert = DEFAULT_SSL_CERT;
initflags = 0;
vdev_height = DEFAULT_VDEV_HEIGHT;
vdev_width = DEFAULT_VDEV_WIDTH;
web_height = DEFAULT_WEB_HEIGHT;
web_width = DEFAULT_WEB_WIDTH;
};
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-height", vdev_height);
jp.AddObject("vdev-width", vdev_width);
jp.AddObject("web-height", web_height);
jp.AddObject("web-width", web_width);
//
// 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) {
fprintf (stderr, "Could not read json file '%s'. Error: %s\n", fn.c_str(), strerror(errno));
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.GetValueInt("web-height", &i)) web_height = i;
if (jp.GetValueInt("web-width", &i)) web_width = i;
if (jp.GetValueString("vdev-device", &s)) vdev_device = s;
if (jp.GetValueInt("vdev-iomode", &i)) vdev_iomode = i;
if (jp.GetValueInt("vdev-height", &i)) vdev_height = i;
if (jp.GetValueInt("vdev-width", &i)) vdev_width = i;
//
// 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], "-websize") == 0) {
if ((i=i+2) < argc) {
web_width = atoi(argv[i-1]);
web_height = atoi(argv[i]);
}
else
ErrorExit("missing web resolution parameter", -1);
}
if (strcmp(argv[i], "-vdevsize") == 0) {
if ((i=i+2) < argc) {
vdev_width = atoi(argv[i-1]);
vdev_height = atoi(argv[i]);
}
else
ErrorExit("missing video device 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], "-vdevdevice") == 0) {
if (++i < argc) {
vdev_device = argv[i];
}
else
ErrorExit("missing device 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 INT INT define the web output resolution\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 (" -vdevsize INT INT define video input resolution\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");
}