1 /*
2  * Copyright (c) 2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sensing/sensing.h>
9 #include <zephyr/sensing/sensing_sensor.h>
10 #include <zephyr/sys/__assert.h>
11 #include <zephyr/logging/log.h>
12 #include "sensor_mgmt.h"
13 
14 LOG_MODULE_DECLARE(sensing, CONFIG_SENSING_LOG_LEVEL);
15 
sensing_iodev_submit(struct rtio_iodev_sqe * iodev_sqe)16 static void sensing_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
17 {
18 	struct sensing_sensor *sensor = (struct sensing_sensor *)iodev_sqe->sqe.userdata;
19 	const struct device *dev = sensor->dev;
20 	const struct sensor_driver_api *api = dev->api;
21 
22 	if (api->submit != NULL) {
23 		api->submit(dev, iodev_sqe);
24 	} else {
25 		LOG_ERR("submit function not supported for device %p %s!\n", dev, dev->name);
26 		rtio_iodev_sqe_err(iodev_sqe, -ENOTSUP);
27 	}
28 }
29 
30 const struct rtio_iodev_api __sensing_iodev_api = {
31 	.submit = sensing_iodev_submit,
32 };
33 
sensing_sensor_get_reporters(const struct device * dev,int type,sensing_sensor_handle_t * reporter_handles,int max_handles)34 int sensing_sensor_get_reporters(const struct device *dev, int type,
35 		sensing_sensor_handle_t *reporter_handles,
36 		int max_handles)
37 {
38 	struct sensing_sensor *sensor = get_sensor_by_dev(dev);
39 	int i, num = 0;
40 
41 	for (i = 0; i < sensor->reporter_num && num < max_handles; ++i) {
42 		if (type == sensor->conns[i].source->info->type
43 				|| type == SENSING_SENSOR_TYPE_ALL) {
44 			reporter_handles[num] = &sensor->conns[i];
45 			num++;
46 		}
47 	}
48 
49 	return num;
50 }
51 
sensing_sensor_get_reporters_count(const struct device * dev,int type)52 int sensing_sensor_get_reporters_count(const struct device *dev, int type)
53 {
54 	struct sensing_sensor *sensor = get_sensor_by_dev(dev);
55 	int i, num = 0;
56 
57 	for (i = 0; i < sensor->reporter_num; ++i) {
58 		if (type == sensor->conns[i].source->info->type
59 				|| type == SENSING_SENSOR_TYPE_ALL) {
60 			num++;
61 		}
62 	}
63 
64 	return num;
65 }
66