-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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: udc: eliminate C++ errors by casting void* to the target type #69490
drivers: udc: eliminate C++ errors by casting void* to the target type #69490
Conversation
8c497c4
to
78cf4f7
Compare
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
@jfischer-no is there anything to be done to move this PR forward? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here in project, we are using C language. Implicit conversion to a pointer to void is fine and vice versa.
What this PR is trying to fix?
It may be that the bugfix in the branch name is a misnomer, since the Zephyr codebase itself builds without issues. However, Zephyr claims to support applications written in both C and C++. This PR is a step to reach this claim in effect. It has absolutely no adverse effects when compiled into C, but this change is vital to allow compilation into C++. |
@jfischer-no please revisit |
5d69b3e
78cf4f7
to
5d69b3e
Compare
But we do support out-of-tree applications written in C++, where implicit pointer conversion leads to a warning. |
@jfischer-no please revisit. There are many drivers in-tree that do this already so I find it really hard to justify why this would be an issue. Thanks! |
We even have a dedicated test for including device driver headers from C++: https://github.com/zephyrproject-rtos/zephyr/blob/main/tests/lib/cpp/cxx/src/main.cpp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is UDC driver API, for the drivers written in C, for the USB device stack written in C, you have to compile it with C compiler not C++. There is no need for this type conversion in C!
@benedekkupper can you expand on your use case to try and figure out what the issue is here? Are you trying to write a UDC driver in C++, or are you including the The reason I ask is that I don't see this file included in any public header file that you'd want to include from the application, nor it seems to be included by any .c file outside the core of the USB stack. |
I am using the UDC driver (and zephyr itself) as one of the supported platforms of an alternative USB device stack (written in C++). You might be asking, why not using zephyr's own USB device stack instead?
And now a question from my side: why put so much effort into such a well-defined and well-designed separation between the USB device stack and the UDC driver, if you don't let them be used on their own? |
Thanks @benedekkupper! This explains it very clearly. I am very much in favor of this change following your explanation. This is a +1 from me. |
(1) there was a USB device stack there more than two years ago, (2) there is no advantage to doing this for USB transfers.
Certainly not for third-party layers, but to enforce consistent behavior and maintainability of the drivers and stack. Unfortunately, there is no clear separation between user and internal headers/interfaces in Zephyr, such as include/zephyr/uapi, which would make it clearer which interfaces are for users. You provide no information as to why you cannot fix this in your stack where you interface with the UDC API. |
Which didn't have the necessary feature-set that we needed, but I shouldn't need to explain this any more than you did when creating the USB device_next stack.
You are confused, the HID report sizes are calculated and used to populate the USB max packet size fields of the endpoint descriptors. Much less error-prone than having an independent definition of these descriptors, and having to remember to keep the two things in sync.
The issue that this patch is trying to fix is syntactic, I have no control over the C++ syntax rules. I can't not include this C header, because the header provides the UDC API types and functions. |
I run into these issues all the time. If it's in a header we need to make it compatible with C++ which means cast it. |
I am not, especially on nRF USB the maximum allowable interrupt data payload size is 64 bytes or less, so there is no real benefit of using smaller endpoint MPS and no bandwidth issues.
You can add your own header as interface to the UDC API and do there what ever you want. Also you commit message states: |
This header file provides the public API of the UDC driver, however it also contains inline function definitions. Unfortunately these definitions contain implicit pointer conversions, which are one of the cases where valid C syntax is invalid C++ syntax, meaning that any C++ source code that intends to use the UDC API cannot be compiled error-free (or warning-free, if using -fpermissive). Signed-off-by: Benedek Kupper <[email protected]>
5d69b3e
to
8ab9914
Compare
Done (I also rebased the commit), please review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but in general C++ limitations should not weaken our interfaces.
@benedekkupper thanks so much for your patience! Glad this is now merged :) |
When this header is included in a C++ source file, compilation fails due to invalid syntax. It can be converted to a warning with
-fpermissive
, but the long term solution is to just perform the missing casts as done here.