Skip to content

Commit a818d06

Browse files
MikeDKfabiobaltieri
authored andcommitted
drivers: sensors: apds9960 fix trigger callback context
In the current implementation, the apds9960 trigger callback function is called with the pointer of a driver-internal allocated trigger structure. This structure is not initialized anywhere, so essentially the trigger callback gets called with junk data. Besides the missing initialization, it would be better instead to hold a const pointer to a user allocated sensor_trigger object. This way user code can establish a context with related user data (for example a pointer to a C++ object) by storing its sensor_trigger object within a structure alongside the user data, and then using CONTAINER_OF() macro to get the pointer of the container struct (and thus the user data). Signed-off-by: Michael Kaplan <[email protected]>
1 parent ce3557a commit a818d06

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

drivers/sensor/apds9960/apds9960.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ struct apds9960_data {
230230

231231
#ifdef CONFIG_APDS9960_TRIGGER
232232
sensor_trigger_handler_t p_th_handler;
233-
struct sensor_trigger p_th_trigger;
233+
const struct sensor_trigger *p_th_trigger;
234234
#else
235235
struct k_sem data_sem;
236236
#endif

drivers/sensor/apds9960/apds9960_trigger.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void apds9960_work_cb(struct k_work *work)
2525
const struct device *dev = data->dev;
2626

2727
if (data->p_th_handler != NULL) {
28-
data->p_th_handler(dev, &data->p_th_trigger);
28+
data->p_th_handler(dev, data->p_th_trigger);
2929
}
3030

3131
apds9960_setup_int(dev->config, true);
@@ -75,6 +75,7 @@ int apds9960_trigger_set(const struct device *dev,
7575
case SENSOR_TRIG_THRESHOLD:
7676
if (trig->chan == SENSOR_CHAN_PROX) {
7777
data->p_th_handler = handler;
78+
data->p_th_trigger = trig;
7879
if (i2c_reg_update_byte_dt(&config->i2c,
7980
APDS9960_ENABLE_REG,
8081
APDS9960_ENABLE_PIEN,

0 commit comments

Comments
 (0)