Skip to content

Commit 1881b88

Browse files
committed
drivers: video: sw_generator: refactor fmt/frmival selection
This addresses a minor bug where the format where all fields are 0 could be passed to video_set_fmt() by mistake and be accepted unchanged by video_sw_generator_set_fmt(), which would match against fmts[] terminator. This also makes use of IN_RANGE() and applies it to the function video_sw_generator_enum_frmival(). Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 4bd15f7 commit 1881b88

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

drivers/video/video_sw_generator.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/drivers/video.h>
1111
#include <zephyr/drivers/video-controls.h>
1212
#include <zephyr/logging/log.h>
13+
#include <zephyr/sys/util.h>
1314

1415
LOG_MODULE_REGISTER(video_sw_generator, CONFIG_VIDEO_LOG_LEVEL);
1516

@@ -64,16 +65,16 @@ static int video_sw_generator_set_fmt(const struct device *dev, enum video_endpo
6465
struct video_format *fmt)
6566
{
6667
struct video_sw_generator_data *data = dev->data;
67-
int i = 0;
68+
int i;
6869

6970
if (ep != VIDEO_EP_OUT && ep != VIDEO_EP_ALL) {
7071
return -EINVAL;
7172
}
7273

7374
for (i = 0; i < ARRAY_SIZE(fmts); ++i) {
74-
if (fmt->pixelformat == fmts[i].pixelformat && fmt->width >= fmts[i].width_min &&
75-
fmt->width <= fmts[i].width_max && fmt->height >= fmts[i].height_min &&
76-
fmt->height <= fmts[i].height_max) {
75+
if (fmt->pixelformat == fmts[i].pixelformat &&
76+
IN_RANGE(fmt->width, fmts[i].width_min, fmts[i].width_max) &&
77+
IN_RANGE(fmt->height, fmts[i].height_min, fmts[i].height_max)) {
7778
break;
7879
}
7980
}
@@ -313,18 +314,20 @@ static int video_sw_generator_enum_frmival(const struct device *dev, enum video_
313314
{
314315
int i = 0;
315316

316-
if (ep != VIDEO_EP_OUT || fie->index) {
317+
if (ep != VIDEO_EP_OUT || fie->index > 0) {
317318
return -EINVAL;
318319
}
319320

320-
while (fmts[i].pixelformat && (fmts[i].pixelformat != fie->format->pixelformat)) {
321-
i++;
321+
for (i = 0; fmts[i].pixelformat != 0; ++i) {
322+
if (fie->format->pixelformat == fmts[i].pixelformat &&
323+
IN_RANGE(fie->format->width, fmts[i].width_min, fmts[i].width_max) &&
324+
IN_RANGE(fie->format->height, fmts[i].height_min, fmts[i].height_max)) {
325+
break;
326+
}
322327
}
323328

324-
if ((i == ARRAY_SIZE(fmts)) || (fie->format->width > fmts[i].width_max) ||
325-
(fie->format->width < fmts[i].width_min) ||
326-
(fie->format->height > fmts[i].height_max) ||
327-
(fie->format->height < fmts[i].height_min)) {
329+
if (fmts[i].pixelformat == 0) {
330+
LOG_ERR("Nothing matching the requested format was found");
328331
return -EINVAL;
329332
}
330333

0 commit comments

Comments
 (0)