Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: video: gc2145: Include set_resolution() in the format caps check #78731

Merged

Conversation

josuah
Copy link
Collaborator

@josuah josuah commented Sep 19, 2024

The current GalxyCore GC2145 does not guard against a wrong pixelformat passed.

This usually never happens and might be caught by the gc2145_set_output_format() anyway, but this permits to only set drv_data->fmt if the format is valid.

A very minor change!

uLipe
uLipe previously approved these changes Sep 19, 2024
Copy link
Collaborator

@uLipe uLipe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, it is perfectly reasonable grouping the I/O operations only after making sure the the format is supported.

pillo79
pillo79 previously approved these changes Sep 19, 2024
Copy link
Collaborator

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK, though if you are up for another round... in Zephyr usually failures are handled in the ifs, and the function returns at the end if everything is OK. You could refactor set_fmt so that if it does not find a match it returns an error.

@uLipe
Copy link
Collaborator

uLipe commented Sep 19, 2024

@pillo79 ,

ACK, though if you are up for another round... in Zephyr usually failures are handled in the ifs, and the function returns at the end if everything is OK. You could refactor set_fmt so that if it does not find a match it returns an error.

@pillo79 , most of these drivers were written and their logic got oversight after merging, is there a proposal of refactoring them to make them consistent of the Zephyr standard, I have backlog here that I plan to start once I finish my work with the Arduino Nicla, and refactor one driver by one.

@pillo79
Copy link
Collaborator

pillo79 commented Sep 20, 2024

ACK, though if you are up for another round... in Zephyr usually failures are handled in the ifs, and the function returns at the end if everything is OK. You could refactor set_fmt so that if it does not find a match it returns an error.

@pillo79 , most of these drivers were written and their logic got oversight after merging, is there a proposal of refactoring them to make them consistent of the Zephyr standard, I have backlog here that I plan to start once I finish my work with the Arduino Nicla, and refactor one driver by one.

Sure, that's great and that's why I approved in the first place 🙂
I just noted that this PR is actually making the above point slightly worse, in the sense that it moves more code inside the if. My POV was simply "if you're gonna touch it, might as well fix it once". Not a deal breaker at all!

@josuah josuah changed the title drivers: video: gc1245: Include set_resolution() in the format caps check drivers: video: gc2145: Include set_resolution() in the format caps check Sep 20, 2024
@josuah
Copy link
Collaborator Author

josuah commented Sep 20, 2024

is there a proposal of refactoring them to make them consistent of the Zephyr standard

Kind of, maybe the first step is to agree on a "ideal format".
This is the goal of this:

I was tempted to start with the GC2145 as it got a review round directly from a TSC member here:

The first sensors were contributed a long time ago, and it is good practice to pick existing drivers to write new ones.
This leads to a lot of refactoring to do for each and every new sensor to make it look closer to recent Zephyr guidelines.

@josuah josuah dismissed stale reviews from pillo79 and uLipe via 207952e September 21, 2024 12:01
While applying the format, the pixel format and drv_data->fmt were set
immediately, and the resolution was set only if it had a matching
entry on the "caps".

This commit makes sure the requested format matches the caps before
applying the format as well as drv_data->fmt. This does not guards
against partial failure (i.e. only pixelformat set and not
resolution).

Signed-off-by: Josuah Demangeon <[email protected]>
After the error code is checked to be zero, it is possible to return 0
explicitly to help with readability. Also, when available, forward the
return code from the failing function instead of a locally chosen error
code like -EIO.

Signed-off-by: Josuah Demangeon <[email protected]>
uLipe
uLipe previously approved these changes Sep 22, 2024
@josuah
Copy link
Collaborator Author

josuah commented Sep 22, 2024

Force push: the new driver passes the enum resolutions to set_resolution instead of the width/height, so the build did fail. Remove the width and height variables set but not used after the refactoring.

pillo79
pillo79 previously approved these changes Sep 23, 2024
Copy link
Collaborator

@pillo79 pillo79 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK - though the new for is quirky 🙂
Up to you if you wish to amend with something like my comment.

Comment on lines 1041 to 1047
for (int i = 0;; i++) {
if (i == ARRAY_SIZE(fmts)) {
LOG_ERR("Image format not supported\n");
return -ENOTSUP;
}
if (fmts[i].width_min == fmt->width && fmts[i].height_min == fmt->height &&
fmts[i].pixelformat == fmt->pixelformat) {
res = (enum resolutions)i;
break;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think a more common way to write this would be:

	enum resolutions res = RESOLUTIONS_MAX;
	
	for (int i = 0; i < ARRAY_SIZE(fmts); i++) {
		if (fmts[i].width_min == fmt->width && fmts[i].height_min == fmt->height &&
		    fmts[i].pixelformat == fmt->pixelformat) {
			res = (enum resolutions)i;
			break;
		}
	}
	if (res == RESOLUTIONS_MAX) {
		LOG_ERR("Image format not supported\n");
		return -ENOTSUP;
	}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then there is the more obvious for (i = 0; i < N; i++) idiom standing out.
I appreciate the teachings. :)

This aims to make the code more linear by having the for loop
validates the input format rather than search for a match.

Signed-off-by: Josuah Demangeon <[email protected]>
@mmahadevan108 mmahadevan108 merged commit 1596ee0 into zephyrproject-rtos:main Sep 28, 2024
23 checks passed
@josuah josuah deleted the pr-video-gc1245-set-fmt-fix branch October 6, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants