Commit e69ce68d authored by Tom Burdick's avatar Tom Burdick Committed by Carles Cufi
Browse files

sensing: Fix initializer compile-time constant

Initializers must be compile time constants (not expressions!) for clang
to be happy. Clang rightfully pointed out that the callback_list member
of sensing_connection was being initialized using an expression that was
not compile-time constant.

The macros passed along a pointer created by taking the address of a
global constant and then at initialization time attempting to
dereference it using the * operator. So in the end the compiler has
identifier its trying to first take the address of and then later
dereference in an initializer. Clang didn't appreciate this, though gcc
seemed to be just fine.

Actually clang 17 might work just fine with this as well, but clang 16
(the clang I tried) didn't like it at all.

Instead store the pointer to the callback list rather than copying the
struct. This could be done the other way and not passing a
pointer through the macros perhaps.

Signed-off-by: default avatarTom Burdick <thomas.burdick@intel.com>
parent fe4056a7
Loading
Loading
Loading
Loading
+6 −4
Changes for include/zephyr/sensing/sensing.h: 6 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -196,7 +196,8 @@ int sensing_get_sensors(int *num_sensors, const struct sensing_sensor_info **inf
 *
 * @param info The sensor info got from \ref sensing_get_sensors
 *
 * @param cb_list callback list to be registered to sensing.
 * @param cb_list callback list to be registered to sensing, must have a static
 *                lifetime.
 *
 * @param handle The opened instance handle, if failed will be set to NULL.
 *
@@ -204,7 +205,7 @@ int sensing_get_sensors(int *num_sensors, const struct sensing_sensor_info **inf
 */
int sensing_open_sensor(
		const struct sensing_sensor_info *info,
		const struct sensing_callback_list *cb_list,
		struct sensing_callback_list *cb_list,
		sensing_sensor_handle_t *handle);

/**
@@ -217,14 +218,15 @@ int sensing_open_sensor(
 *
 * @param dev pointer device get from device tree.
 *
 * @param cb_list callback list to be registered to sensing.
 * @param cb_list callback list to be registered to sensing, must have a static
 *                lifetime.
 *
 * @param handle The opened instance handle, if failed will be set to NULL.
 *
 * @return 0 on success or negative error value on failure.
 */
int sensing_open_sensor_by_dt(
		const struct device *dev, const struct sensing_callback_list *cb_list,
		const struct device *dev, struct sensing_callback_list *cb_list,
		sensing_sensor_handle_t *handle);

/**
+2 −2
Changes for include/zephyr/sensing/sensing_sensor.h: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ struct sensing_connection {
	/* client(sink) next consume time */
	uint64_t next_consume_time;
	/* post data to application */
	struct sensing_callback_list callback_list;
	struct sensing_callback_list *callback_list;
};

/**
@@ -136,7 +136,7 @@ extern struct sensing_sensor SENSING_SENSOR_SOURCE_NAME(idx, node); \

#define SENSING_CONNECTION_INITIALIZER(source_name, cb_list_ptr)	\
{									\
	.callback_list = *cb_list_ptr,					\
	.callback_list = cb_list_ptr,					\
	.source = &source_name,						\
}

+6 −4
Changes for samples/subsys/sensing/simple/src/main.c: 6 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -38,14 +38,16 @@ static void hinge_angle_data_event_callback(sensing_sensor_handle_t handle, cons
	LOG_INF("handle:%p, Sensor:%s data:(v:%d)", handle, info->name, sample->readings[0].v);
}

int main(void)
{
	const struct sensing_callback_list base_acc_cb_list = {
static struct sensing_callback_list base_acc_cb_list = {
	.on_data_event = &acc_data_event_callback,
};
	const struct sensing_callback_list hinge_angle_cb_list = {

static struct sensing_callback_list hinge_angle_cb_list = {
	.on_data_event = &hinge_angle_data_event_callback,
};

int main(void)
{
	const struct sensing_sensor_info *info;
	sensing_sensor_handle_t base_acc;
	sensing_sensor_handle_t hinge_angle;
+3 −3
Changes for subsys/sensing/dispatch.c: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -59,13 +59,13 @@ static int send_data_to_clients(struct sensing_sensor *sensor,

		update_client_consume_time(sensor, conn);

		if (!conn->callback_list.on_data_event) {
		if (!conn->callback_list->on_data_event) {
			LOG_WRN("sensor:%s event callback not registered",
					conn->source->dev->name);
			continue;
		}
		conn->callback_list.on_data_event(conn, data,
				conn->callback_list.context);
		conn->callback_list->on_data_event(conn, data,
				conn->callback_list->context);
	}

	return 0;
+2 −2
Changes for subsys/sensing/sensing.c: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ LOG_MODULE_DECLARE(sensing, CONFIG_SENSING_LOG_LEVEL);

/* sensing_open_sensor is normally called by applications: hid, chre, zephyr main, etc */
int sensing_open_sensor(const struct sensing_sensor_info *sensor_info,
			const struct sensing_callback_list *cb_list,
			struct sensing_callback_list *cb_list,
			sensing_sensor_handle_t *handle)
{
	int ret = 0;
@@ -37,7 +37,7 @@ int sensing_open_sensor(const struct sensing_sensor_info *sensor_info,
}

int sensing_open_sensor_by_dt(const struct device *dev,
			      const struct sensing_callback_list *cb_list,
			      struct sensing_callback_list *cb_list,
			      sensing_sensor_handle_t *handle)
{
	int ret = 0;
Loading