channel type: Improve code readability for channel type#1404
channel type: Improve code readability for channel type#1404RubenNatanael wants to merge 1 commit intomainfrom
Conversation
Currently, the channel type is stored as enum iio_chan_type, even when it represents a hwmon_chan_type. By changing this field to int, we improve code readability and flexibility. This change also enables the implementation of validation checks, such as in hwmon_channel_get_type(), which now issues a warning if a hwmon function is incorrectly called for a non-hwmon channel and vice versa. Signed-off-by: Ruben Mujdei <ruben.mujdei@analog.com>
a1bcdd7 to
c1611e0
Compare
nunojsa
left a comment
There was a problem hiding this comment.
I'm not sure I agree with this change, It seems like unneeded churn to me. It is fair to assume that users will call hwmon_channel_get_type() on an hwmon device. If not, they need to fix their app anyways! So I would either:
- Leave as-is;
- Or be strict about it. Meaning, return an error in case we try to get an IIO channel from an hwmon device or vice-versa.
Honestly I would probably go with 1. For 2, we might need to change the API a bit...
Having said the above, we should have introduced different types for hwmon devices. Yeah, it would have been more complicated but we would not have these discussions.
| if (!iio_device_is_hwmon(iio_channel_get_device(chn))) { | ||
| chn_warn(chn, "hwmon_channel_get_type() called on non-hwmon channel\n"); | ||
| } | ||
| return (enum hwmon_chan_type)chn->type; |
There was a problem hiding this comment.
well, we give a warning but prevent nothing... IOW, we continue to do as before.
There was a problem hiding this comment.
I agree. Like I said in the issue, this doesn't stop the error from happening, it's just a 'hint' for developers who assume that the member enum iio_chan_type type in struct iio_channel can't be an hwmon_chan_type. That said, I'm not entirely sure about this change either, so I'd like to hear other opinions.
There was a problem hiding this comment.
well, we give a warning but prevent nothing... IOW, we continue to do as before.
Yes, we continue as before which aligns with your suggestion: "1. Leave as-is;"
Myself and @RubenNatanael looked at this from multiple angles and also came to the same conclusion as you and @rgetz that this isn't an issue, that there isn't an incorrect mapping taking place anywhere. Well, that not entirely true, because the only actor doing this mapping the debugger. So, we said, since we got to this point let's just fix that. Additionally, we thought to warn when hwmon_channel_get_type() and iio_channel_get_type() aren't used properly. But I see that at the same time we've updated the documentation asking users to make sure they are using the proper iio device by calling iio_device_is_hwmon(). But we're also calling it inside hwmon_channel_get_type() and iio_channel_get_type(). Maybe we just stick with updating the documentation and not issue the warnings.
But I don't see any harm in fixing the storage type for channel information.
Ack and agree - it's not only unneeded - but also will break things. We want to have people treat |
|
Hi all and thanks for all responses. I didn't expect the #1401 will lead to so quick reactions. It doesn't happen so often in Open Source community. I believe we all agree that actually there is nothing wrong in the current implementation, if you use it correctly it works as expected. The only problem is about confusion faced to developers who are based on reading the code and not documentation. And the fact is that the code is the best documentation. Unfortunately, if you see The fact is that in existing implementation developer is confused when seeing Anyway, whatever decision will be made, I am glad of having such quick responds, thank you. |
What breaks with this change? For end users (libiio clients), nothing changes. No change in functionality or API. |
|
The kernel uses a enum. Libiio should remain sync'ed with the kernel. The patch weakens the type system by changing As you point out - the "problem" seems to be a debugger issue. From the discussion:
That means the motivation is essentially:
Changing internal types to accommodate debugger output is generally a bad tradeoff. As also pointed out - Documentation or debugger scripts would be the correct fix. If you really want to fix it - use a tagged union. (but unsure if the complexity is worth it)... you add two new enums: and then in and then you get to do: The compiler will catch misuse. but to go back to:
Type checking. Now anything can be stored: type = 4200 the patch removes compile-time checking, and forces people to debug it run time. |
That's a very good point! This PR could be simplified to just improving the documentation for |
PR Description
Currently, the channel type is stored as enum iio_chan_type, even when it represents a hwmon_chan_type. By changing this field to int, we improve code readability and flexibility. This change also enables the implementation of validation checks, such as in hwmon_channel_get_type(), which now issues a warning if a hwmon function is incorrectly called for a non-hwmon channel and vice versa.
PR Type
PR Checklist