1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/******************************************************************************#
# guvcview http://guvcview.sourceforge.net #
# #
# Paulo Assis <pj.assis@gmail.com> #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
*******************************************************************************/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "control_profile.h"
#include "gviewv4l2core.h"
#include "v4l2_controls.h"
// #include "../config.h"
extern int verbosity;
/*
* save the device control values into a profile file
* args:
* vd - pointer to video device data
* filename - profile filename
*
* asserts:
* vd is not null
*
* returns: error code (0 -E_OK)
*/
int save_control_profile(v4l2_dev_t *vd, const char *filename) {
/*assertions*/
assert(vd != NULL);
FILE *fp;
fp = fopen(filename, "w");
if (fp == NULL) {
fprintf(
stderr,
"V4L2_CORE: (save_control_profile) Could not open %s for write: %s\n",
filename, strerror(errno));
return (E_FILE_IO_ERR);
} else {
if (vd->list_device_controls) {
v4l2_ctrl_t *current = vd->list_device_controls;
/*write header*/
fprintf(fp, "#V4L2/CTRL/0.0.2\n");
fprintf(fp, "APP{\"%s\"}\n", PACKAGE_STRING);
/*write control data*/
fprintf(fp, "# control data\n");
for (; current != NULL; current = current->next) {
if ((current->control.flags & V4L2_CTRL_FLAG_WRITE_ONLY) ||
(current->control.flags & V4L2_CTRL_FLAG_READ_ONLY) ||
(current->control.flags & V4L2_CTRL_FLAG_GRABBED)) {
if (verbosity > 0)
printf("V4L2_CORE: (save_control_profile) skiping control 0x%08x\n",
current->control.id);
continue;
}
fprintf(fp, "#%s\n", current->control.name);
switch (current->control.type) {
case V4L2_CTRL_TYPE_STRING:
fprintf(fp, "ID{0x%08x};CHK{%i:%i:%i:0}=STR{\"%s\"}\n",
current->control.id, current->control.minimum,
current->control.maximum, current->control.step,
current->string);
break;
case V4L2_CTRL_TYPE_INTEGER64:
fprintf(fp, "ID{0x%08x};CHK{0:0:0:0}=VAL64{%" PRId64 "}\n",
current->control.id, current->value64);
break;
default:
fprintf(fp, "ID{0x%08x};CHK{%i:%i:%i:%i}=VAL{%i}\n",
current->control.id, current->control.minimum,
current->control.maximum, current->control.step,
current->control.default_value, current->value);
break;
}
}
}
}
fflush(fp); /*flush stream buffers to filesystem*/
if (fsync(fileno(fp)) || fclose(fp)) {
fprintf(stderr,
"V4L2_CORE: (save_control_profile) write to file failed: %s\n",
strerror(errno));
return (E_FILE_IO_ERR);
}
return (E_OK);
}
/*
* load the device control values from a profile file
* args:
* vd - pointer to video device data
* filename - profile filename
*
* asserts:
* vd is not null
*
* returns: error code (0 -E_OK)
*/
int load_control_profile(v4l2_dev_t *vd, const char *filename) {
/*assertions*/
assert(vd != NULL);
FILE *fp;
int major = 0, minor = 0, rev = 0;
if ((fp = fopen(filename, "r")) != NULL) {
char line[200];
if (fgets(line, sizeof(line), fp) != NULL) {
if (sscanf(line, "#V4L2/CTRL/%3i.%3i.%3i", &major, &minor, &rev) == 3) {
// check standard version if needed
} else {
fprintf(stderr,
"V4L2_CORE: (load_control_profile) no valid header found\n");
fclose(fp);
return (E_NO_DATA);
}
} else {
fprintf(stderr,
"V4L2_CORE: (load_control_profile) no valid header found\n");
fclose(fp);
return (E_NO_DATA);
}
while (fgets(line, sizeof(line), fp) != NULL) {
int id = 0;
int min = 0, max = 0, step = 0, def = 0;
int32_t val = 0;
int64_t val64 = 0;
if ((line[0] != '#') && (line[0] != '\n')) {
if (sscanf(line, "ID{0x%08x};CHK{%5i:%5i:%5i:%5i}=VAL{%5i}", &id, &min,
&max, &step, &def, &val) == 6) {
v4l2_ctrl_t *current = v4l2core_get_control_by_id(vd, id);
if (current) {
/*check values*/
if (current->control.minimum == min &&
current->control.maximum == max &&
current->control.step == step &&
current->control.default_value == def) {
current->value = val;
}
}
} else if (sscanf(line, "ID{0x%08x};CHK{0:0:0:0}=VAL64{%" PRId64 "}",
&id, &val64) == 2) {
v4l2_ctrl_t *current = v4l2core_get_control_by_id(vd, id);
if (current) {
current->value64 = val64;
}
} else if (sscanf(line, "ID{0x%08x};CHK{%5i:%5i:%5i:0}=STR{\"%*s\"}",
&id, &min, &max, &step) == 5) {
v4l2_ctrl_t *current = v4l2core_get_control_by_id(vd, id);
if (current) {
/*check values*/
if (current->control.minimum == min &&
current->control.maximum == max &&
current->control.step == step) {
char str[max + 1];
char fmt[48];
sprintf(fmt, "ID{0x%%*x};CHK{%%*i:%%*i:%%*i:0}==STR{\"%%%is\"}",
max);
sscanf(line, fmt, str);
/*we are only scannig for max chars so this should never happen*/
if (strlen(str) >
max) /*FIXME: should also check (minimum +N*step)*/
{
fprintf(stderr,
"V4L2_CORE: (load_control_profile) string bigger than "
"maximum buffer size (%i > %i)\n",
(int)strlen(str), max);
if (current->string)
free(current->string);
current->string =
strndup(str, max); /*FIXME: does max includes '\0' ?*/
} else {
if (current->string)
free(current->string);
current->string = strndup(str, strlen(str) + 1);
}
}
}
}
}
}
set_v4l2_control_values(vd);
get_v4l2_control_values(vd);
} else {
fprintf(
stderr,
"V4L2_CORE: (load_control_profile) Could not open for %s read: %s\n",
filename, strerror(errno));
return (E_FILE_IO_ERR);
}
fclose(fp);
return (E_OK);
}
|