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

Add optional user data field to device structure #30308

Closed
aloebs29 opened this issue Nov 30, 2020 · 1 comment
Closed

Add optional user data field to device structure #30308

aloebs29 opened this issue Nov 30, 2020 · 1 comment
Labels
Enhancement Changes/Updates/Additions to existing features

Comments

@aloebs29
Copy link

Is your enhancement proposal related to a problem? Please describe.
I'm trying to write a C++ class which receives a struct device * member via dependency injection. In this case, the device is a sensor, and I'd like to setup a trigger callback. I must use a static function as the callback, since the sensor trigger callback does not provide the this argument. This means that I am unable to reference non-static member data of my class.

Describe the solution you'd like
Add a "user data" field to the device structure. This could be enabled via a config option as such:

struct device {
	/** Name of the device instance */
	const char *name;
	/** Address of device instance config information */
	const void *config;
	/** Address of the API structure exposed by the device instance */
	const void *api;
	/** Address of the device instance private data */
	void *const data;
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
	/** Power Management function */
	int (*device_pm_control)(const struct device *dev, uint32_t command,
				 void *context, device_pm_cb cb, void *arg);
	/** Pointer to device instance power management data */
	struct device_pm *const pm;
#endif
#ifdef CONFIG_DEVICE_USER_DATA
	/** Pointer to user data */
	void *user_data;
#endif
};

This would allow me to write this to the user data section of the device structure when an instance of my class is constructed, and my sensor trigger callback (which will still be static) can use this user data field (since the trigger callback does provide a pointer to the device structure) to access non-static member data.

I imagine that this user data field would be useful in other cases as well.

@aloebs29 aloebs29 added the Enhancement Changes/Updates/Additions to existing features label Nov 30, 2020
@aloebs29
Copy link
Author

This won't work since device_get_binding() returns const struct device * (user data would not be mutable).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Changes/Updates/Additions to existing features
Projects
None yet
Development

No branches or pull requests

1 participant