1 /*
2 * Copyright (c) 2023 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef SENSOR_MGMT_H_
8 #define SENSOR_MGMT_H_
9
10 #include <zephyr/sensing/sensing_datatypes.h>
11 #include <zephyr/sensing/sensing_sensor.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/slist.h>
14 #include <zephyr/sys/ring_buffer.h>
15 #include <string.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #define for_each_sensor(sensor) \
22 STRUCT_SECTION_FOREACH(sensing_sensor, sensor)
23
24 #define for_each_sensor_reverse(sensor) \
25 STRUCT_SECTION_START_EXTERN(sensing_sensor); \
26 STRUCT_SECTION_END_EXTERN(sensing_sensor); \
27 for (struct sensing_sensor *sensor = STRUCT_SECTION_END(sensing_sensor) \
28 - 1; \
29 ({ __ASSERT(sensor >= STRUCT_SECTION_START(sensing_sensor) - 1, \
30 "unexpected list start location"); \
31 sensor >= STRUCT_SECTION_START(sensing_sensor); }); \
32 sensor--)
33
34
35 #define for_each_client_conn(sensor, client) \
36 SYS_SLIST_FOR_EACH_CONTAINER(&sensor->client_list, client, snode)
37
38 #define EXEC_TIME_INIT 0
39 #define EXEC_TIME_OFF UINT64_MAX
40
41 extern struct rtio sensing_rtio_ctx;
42 /**
43 * @struct sensing_context
44 * @brief sensing subsystem context to include global variables
45 */
46 struct sensing_context {
47 bool sensing_initialized;
48 struct k_sem event_sem;
49 atomic_t event_flag;
50 };
51
52 int open_sensor(struct sensing_sensor *sensor, struct sensing_connection **conn);
53 int close_sensor(struct sensing_connection **conn);
54 int sensing_register_callback(struct sensing_connection *conn,
55 struct sensing_callback_list *cb_list);
56 int set_interval(struct sensing_connection *conn, uint32_t interval);
57 int get_interval(struct sensing_connection *con, uint32_t *sensitivity);
58 int set_sensitivity(struct sensing_connection *conn, int8_t index, uint32_t interval);
59 int get_sensitivity(struct sensing_connection *con, int8_t index, uint32_t *sensitivity);
60
get_sensor_by_dev(const struct device * dev)61 static inline struct sensing_sensor *get_sensor_by_dev(const struct device *dev)
62 {
63 STRUCT_SECTION_FOREACH(sensing_sensor, sensor) {
64 if (sensor->dev == dev) {
65 return sensor;
66 }
67 }
68
69 __ASSERT(true, "device %s is not a sensing sensor", dev->name);
70
71 return NULL;
72 }
73
get_reporter_sensor(struct sensing_sensor * sensor,int index)74 static inline struct sensing_sensor *get_reporter_sensor(struct sensing_sensor *sensor, int index)
75 {
76 if (!sensor || index >= sensor->reporter_num) {
77 return NULL;
78 }
79
80 return sensor->conns[index].source;
81 }
82
get_sensor_info(struct sensing_connection * conn)83 static inline const struct sensing_sensor_info *get_sensor_info(struct sensing_connection *conn)
84 {
85 __ASSERT(conn, "get sensor info, connection not be NULL");
86
87 __ASSERT(conn->source, "get sensor info, sensing_sensor is NULL");
88
89 return conn->source->info;
90 }
91
92 /* check if client has requested data from reporter */
is_client_request_data(struct sensing_connection * conn)93 static inline bool is_client_request_data(struct sensing_connection *conn)
94 {
95 return conn->interval != 0;
96 }
97
get_us(void)98 static inline uint64_t get_us(void)
99 {
100 return k_ticks_to_us_floor64(k_uptime_ticks());
101 }
102
is_sensor_state_ready(struct sensing_sensor * sensor)103 static inline bool is_sensor_state_ready(struct sensing_sensor *sensor)
104 {
105 return (sensor->state == SENSING_SENSOR_STATE_READY);
106 }
107
108 /* this function is used to decide whether filtering sensitivity checking
109 * for example: filter sensitivity checking if sensitivity value is 0.
110 */
is_filtering_sensitivity(int * sensitivity)111 static inline bool is_filtering_sensitivity(int *sensitivity)
112 {
113 bool filtering = false;
114
115 __ASSERT(sensitivity, "sensitivity should not be NULL");
116 for (int i = 0; i < CONFIG_SENSING_MAX_SENSITIVITY_COUNT; i++) {
117 if (sensitivity[i] != 0) {
118 filtering = true;
119 break;
120 }
121 }
122
123 return filtering;
124 }
125
126 /**
127 * @}
128 */
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 #endif /* SENSOR_MGMT_H_ */
135