Debian Codegraph @

cvd_src/Linux/v4lcontrol.cc

raw symbols SCIP
1 #include <cvd/Linux/v4lcontrol.h>
2
3 #include <iostream>
4 #include <sstream>
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <string>
10 #include <sys/ioctl.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13
14 using namespace std;
15
16 template <typename T>
17 static inline double toUnit(T val, T min, T max)
18 {
19 double interval = max - min;
20 return (val - min) / interval;
21 }
22
23 template <typename T>
24 static inline T fromUnit(double val, T min, T max)
25 {
26 double interval = max - min;
27 return static_cast<T>(val * interval - min);
28 }
29
30 namespace CVD
31 {
32
33 Exceptions::V4LControl::DeviceOpen::DeviceOpen(string device)
34 : All("V4LControl: failed to open \"" + device + "\": " + strerror(errno))
35 {
36 }
37
38 Exceptions::V4LControl::ParameterNotSupported::ParameterNotSupported(string parameter)
39 : All("V4LControl: parameter \"" + parameter + "\" is not supported.")
40 {
41 }
42
43 Exceptions::V4LControl::ParameterNotSupported::ParameterNotSupported(unsigned int id)
44 : All("V4LControl: parameter " + std::to_string(id) + " is not supported.")
45 {
46 }
47
48 Exceptions::V4LControl::GetValue::GetValue(string parameter)
49 : All("V4LControl: query value \"" + parameter + "\" failed: " + strerror(errno))
50 {
51 }
52
53 Exceptions::V4LControl::SetValue::SetValue(string parameter)
54 : All("V4LControl: setting value \"" + parameter + "\" failed: " + strerror(errno))
55 {
56 }
57
58 Exceptions::V4LControl::QueryParameters::QueryParameters(string msg)
59 : All("V4LControl: Querying parameters failed: \"" + msg + "\": " + strerror(errno))
60 {
61 }
62
63 V4LControl::V4LControl(int fd, bool report)
64 : device(fd)
65 , deviceName("")
66 , reportErrors(report)
67 {
68 memset(&control, 0, sizeof(control));
69 queryControls();
70 }
71
72 V4LControl::V4LControl(const std::string& name, bool report)
73 : device(0)
74 , deviceName(name)
75 , reportErrors(report)
76 {
77 if(-1 == (device = open(deviceName.c_str(), O_RDWR | O_NONBLOCK)))
78 {
79 throw Exceptions::V4LControl::DeviceOpen(deviceName);
80 }
81 memset(&control, 0, sizeof(control));
82 queryControls();
83 }
84
85 void V4LControl::exposure(int value)
86 {
87 if(!isSupported(V4L2_CID_EXPOSURE))
88 return;
89 set(V4L2_CID_EXPOSURE, value);
90 }
91
92 int V4LControl::exposure(void)
93 {
94 if(!isSupported(V4L2_CID_EXPOSURE))
95 return 0;
96 return get(V4L2_CID_EXPOSURE);
97 }
98
99 void V4LControl::autoexposure(bool value)
100 {
101 if(!isSupported("Auto Exposure"))
102 return;
103 set("Auto Exposure", value);
104 }
105
106 bool V4LControl::autoexposure()
107 {
108 if(!isSupported("Auto Exposure"))
109 return false;
110 return get("Auto Exposure");
111 }
112
113 void V4LControl::gain(double value)
114 {
115 if(!isSupported(V4L2_CID_GAIN))
116 return;
117 set(V4L2_CID_GAIN, fromUnit(value, min(V4L2_CID_GAIN), max(V4L2_CID_GAIN)));
118 }
119
120 double V4LControl::gain(void)
121 {
122 if(!isSupported(V4L2_CID_GAIN))
123 return 0;
124 return toUnit(get(V4L2_CID_GAIN), min(V4L2_CID_GAIN), max(V4L2_CID_GAIN));
125 }
126
127 void V4LControl::autogain(bool value)
128 {
129 if(!isSupported(V4L2_CID_AUTOGAIN))
130 return;
131 set(V4L2_CID_AUTOGAIN, value);
132 }
133
134 bool V4LControl::autogain(void)
135 {
136 if(!isSupported(V4L2_CID_AUTOGAIN))
137 return false;
138 return get(V4L2_CID_AUTOGAIN);
139 }
140
141 void V4LControl::brightness(double value)
142 {
143 if(!isSupported(V4L2_CID_BRIGHTNESS))
144 return;
145 set(V4L2_CID_BRIGHTNESS, fromUnit(value, min(V4L2_CID_BRIGHTNESS), max(V4L2_CID_BRIGHTNESS)));
146 }
147
148 double V4LControl::brightness(void)
149 {
150 if(!isSupported(V4L2_CID_BRIGHTNESS))
151 return 0;
152 return toUnit(get(V4L2_CID_BRIGHTNESS), min(V4L2_CID_BRIGHTNESS), max(V4L2_CID_BRIGHTNESS));
153 }
154
155 void V4LControl::contrast(double value)
156 {
157 if(!isSupported(V4L2_CID_CONTRAST))
158 return;
159 set(V4L2_CID_CONTRAST, fromUnit(value, min(V4L2_CID_CONTRAST), max(V4L2_CID_CONTRAST)));
160 }
161
162 double V4LControl::contrast(void)
163 {
164 if(!isSupported(V4L2_CID_CONTRAST))
165 return 0;
166 return toUnit(get(V4L2_CID_CONTRAST), min(V4L2_CID_CONTRAST), max(V4L2_CID_CONTRAST));
167 }
168
169 void V4LControl::saturation(double value)
170 {
171 if(!isSupported(V4L2_CID_SATURATION))
172 return;
173 set(V4L2_CID_SATURATION, fromUnit(value, min(V4L2_CID_SATURATION), max(V4L2_CID_SATURATION)));
174 }
175
176 double V4LControl::saturation(void)
177 {
178 if(!isSupported(V4L2_CID_SATURATION))
179 return 0;
180 return toUnit(get(V4L2_CID_SATURATION), min(V4L2_CID_SATURATION), max(V4L2_CID_SATURATION));
181 }
182
183 void V4LControl::queryControls(void)
184 {
185 controlData.clear();
186 controlNames.clear();
187 menuData.clear();
188
189 struct v4l2_queryctrl queryctrl;
190 memset(&queryctrl, 0, sizeof(queryctrl));
191 for(unsigned int i = V4L2_CID_BASE; i < V4L2_CID_LASTP1; i++)
192 {
193 queryctrl.id = i;
194 if(0 == getQueryStruct(queryctrl))
195 {
196 if(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
197 continue;
198 controlData[queryctrl.id] = queryctrl;
199 controlNames[(const char*)queryctrl.name] = queryctrl.id;
200 if(queryctrl.type == V4L2_CTRL_TYPE_MENU)
201 {
202 vector<v4l2_querymenu> menus;
203 getMenuStruct(queryctrl.id, menus);
204 menuData[queryctrl.id].swap(menus);
205 }
206 }
207 else
208 {
209 if(errno != EINVAL)
210 {
211 throw Exceptions::V4LControl::QueryParameters("standard controls");
212 }
213 }
214 }
215 for(queryctrl.id = V4L2_CID_PRIVATE_BASE;; queryctrl.id++)
216 {
217 if(0 == getQueryStruct(queryctrl))
218 {
219 if(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED)
220 continue;
221 controlData[queryctrl.id] = queryctrl;
222 controlNames[(const char*)queryctrl.name] = queryctrl.id;
223 if(queryctrl.type == V4L2_CTRL_TYPE_MENU)
224 {
225 vector<v4l2_querymenu> menus;
226 getMenuStruct(queryctrl.id, menus);
227 menuData[queryctrl.id].swap(menus);
228 }
229 }
230 else
231 {
232 break;
233 }
234 }
235 }
236
237 unsigned int V4LControl::getId(const std::string& name) const
238 {
239 map<string, unsigned int>::const_iterator id = controlNames.find(name);
240 if(id == controlNames.end())
241 return V4L2_CID_LASTP1;
242 return id->second;
243 }
244
245 string V4LControl::getName(unsigned int id) const
246 {
247 map<unsigned int, v4l2_queryctrl>::const_iterator control = controlData.find(id);
248 if(control == controlData.end())
249 return "";
250 return (const char*)control->second.name;
251 }
252
253 vector<unsigned int> V4LControl::supportedParameters(void) const
254 {
255 vector<unsigned int> result(controlNames.size());
256 vector<unsigned int>::iterator id = result.begin();
257 for(map<string, unsigned int>::const_iterator param = controlNames.begin(); param != controlNames.end(); ++param, ++id)
258 *id = param->second;
259 return result;
260 }
261
262 vector<string> V4LControl::supportedParameterNames(void) const
263 {
264 vector<string> result(controlNames.size());
265 vector<string>::iterator id = result.begin();
266 for(map<string, unsigned int>::const_iterator param = controlNames.begin(); param != controlNames.end(); ++param, ++id)
267 *id = param->first;
268 return result;
269 }
270
271 void V4LControl::set(unsigned int id, int value)
272 {
273 control.id = id;
274 if(!isSupported(control.id))
275 throw Exceptions::V4LControl::ParameterNotSupported(id);
276 control.value = value;
277 if(-1 == setControlStruct(control))
278 if(errno != EBUSY)
279 throw Exceptions::V4LControl::SetValue(getName(id));
280 }
281
282 int V4LControl::get(unsigned int id)
283 {
284 control.id = id;
285 if(!isSupported(control.id))
286 throw Exceptions::V4LControl::ParameterNotSupported(id);
287 if(-1 == getControlStruct(control))
288 if(errno != EBUSY)
289 throw Exceptions::V4LControl::GetValue(getName(id));
290 return control.value;
291 }
292
293 int V4LControl::type(unsigned int id)
294 {
295 return controlData[id].type;
296 }
297
298 map<unsigned int, string> V4LControl::menuValues(unsigned int id) const
299 {
300 if(!isSupported(id))
301 throw Exceptions::V4LControl::ParameterNotSupported(id);
302 map<unsigned int, string> result;
303 const vector<v4l2_querymenu>& menu = menuData.find(id)->second;
304 for(vector<v4l2_querymenu>::const_iterator m = menu.begin(); m != menu.end(); m++)
305 result[m->index] = (const char*)m->name;
306 return result;
307 }
308
309 int V4LControl::defaultValue(unsigned int id) const
310 {
311 if(!isSupported(id))
312 throw Exceptions::V4LControl::ParameterNotSupported(id);
313 return controlData.find(id)->second.default_value;
314 }
315
316 int V4LControl::min(unsigned int id) const
317 {
318 if(!isSupported(id))
319 throw Exceptions::V4LControl::ParameterNotSupported(id);
320 return controlData.find(id)->second.minimum;
321 }
322
323 int V4LControl::max(unsigned int id) const
324 {
325 if(!isSupported(id))
326 throw Exceptions::V4LControl::ParameterNotSupported(id);
327 return controlData.find(id)->second.maximum;
328 }
329
330 int V4LControl::step(unsigned int id) const
331 {
332 if(!isSupported(id))
333 throw Exceptions::V4LControl::ParameterNotSupported(id);
334 return controlData.find(id)->second.step;
335 }
336
337 int V4LControl::getQueryStruct(v4l2_queryctrl& query) const
338 {
339 return ioctl(device, VIDIOC_QUERYCTRL, &query);
340 }
341
342 void V4LControl::getMenuStruct(unsigned int id, vector<v4l2_querymenu>& menu) const
343 {
344 if(!isSupported(id))
345 throw Exceptions::V4LControl::ParameterNotSupported(id);
346 menu.clear();
347 struct v4l2_querymenu querymenu;
348 memset(&querymenu, 0, sizeof(querymenu));
349 querymenu.id = id;
350 int last;
351 const struct v4l2_queryctrl& data = controlData.find(id)->second;
352
353 for(querymenu.index = data.minimum; (int)querymenu.index <= data.maximum; querymenu.index++)
354 {
355 if(0 != (last = ioctl(device, VIDIOC_QUERYMENU, &querymenu)))
356 throw Exceptions::V4LControl::QueryParameters("reading menu item");
357 menu.push_back(querymenu);
358 }
359 }
360
361 int V4LControl::setControlStruct(v4l2_control& control)
362 {
363 return ioctl(device, VIDIOC_S_CTRL, &control);
364 }
365
366 int V4LControl::getControlStruct(v4l2_control& control) const
367 {
368 return ioctl(device, VIDIOC_G_CTRL, &control);
369 }
370
371 }