1 /*
2 * Copyright (c) 2023 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/sensing/sensing.h>
8 #include <zephyr/sensing/sensing_sensor.h>
9 #include <stdlib.h>
10 #include "sensor_mgmt.h"
11
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_DECLARE(sensing, CONFIG_SENSING_LOG_LEVEL);
14
15 /* sensing_open_sensor is normally called by applications: hid, chre, zephyr main, etc */
sensing_open_sensor(const struct sensing_sensor_info * sensor_info,struct sensing_callback_list * cb_list,sensing_sensor_handle_t * handle)16 int sensing_open_sensor(const struct sensing_sensor_info *sensor_info,
17 struct sensing_callback_list *cb_list,
18 sensing_sensor_handle_t *handle)
19 {
20 int ret = 0;
21
22 if (sensor_info == NULL || handle == NULL) {
23 return -ENODEV;
24 }
25
26 STRUCT_SECTION_FOREACH(sensing_sensor, sensor) {
27 if (sensor_info == sensor->info) {
28 ret = open_sensor(sensor, (struct sensing_connection **)handle);
29 if (ret) {
30 return -EINVAL;
31 }
32 break;
33 }
34 }
35
36 return sensing_register_callback(*handle, cb_list);
37 }
38
sensing_open_sensor_by_dt(const struct device * dev,struct sensing_callback_list * cb_list,sensing_sensor_handle_t * handle)39 int sensing_open_sensor_by_dt(const struct device *dev,
40 struct sensing_callback_list *cb_list,
41 sensing_sensor_handle_t *handle)
42 {
43 int ret = 0;
44 struct sensing_sensor *sensor;
45
46 if (handle == NULL) {
47 return -ENODEV;
48 }
49
50 sensor = get_sensor_by_dev(dev);
51 if (sensor == NULL) {
52 LOG_ERR("cannot get sensor from dev:%p", dev);
53 return -ENODEV;
54 }
55
56 ret = open_sensor(sensor, (struct sensing_connection **)handle);
57 if (ret) {
58 return -EINVAL;
59 }
60
61 return sensing_register_callback(*handle, cb_list);
62 }
63
64 /* sensing_close_sensor is normally called by applications: hid, chre, zephyr main, etc */
sensing_close_sensor(sensing_sensor_handle_t * handle)65 int sensing_close_sensor(sensing_sensor_handle_t *handle)
66 {
67 if (handle == NULL) {
68 return -ENODEV;
69 }
70
71 return close_sensor((struct sensing_connection **)handle);
72 }
73
sensing_set_config(sensing_sensor_handle_t handle,struct sensing_sensor_config * configs,int count)74 int sensing_set_config(sensing_sensor_handle_t handle,
75 struct sensing_sensor_config *configs,
76 int count)
77 {
78 struct sensing_sensor_config *cfg;
79 int i, ret = 0;
80
81 if (handle == NULL || configs == NULL) {
82 return -ENODEV;
83 }
84
85 if (count <= 0 || count > SENSING_SENSOR_ATTRIBUTE_MAX) {
86 LOG_ERR("invalid config count:%d", count);
87 return -EINVAL;
88 }
89
90 for (i = 0; i < count; i++) {
91 cfg = &configs[i];
92 switch (cfg->attri) {
93 case SENSING_SENSOR_ATTRIBUTE_INTERVAL:
94 ret |= set_interval(handle, cfg->interval);
95 break;
96
97 case SENSING_SENSOR_ATTRIBUTE_SENSITIVITY:
98 ret |= set_sensitivity(handle, cfg->data_field, cfg->sensitivity);
99 break;
100
101 case SENSING_SENSOR_ATTRIBUTE_LATENCY:
102 break;
103
104 default:
105 ret = -EINVAL;
106 LOG_ERR("invalid config attribute:%d\n", cfg->attri);
107 break;
108 }
109 }
110
111 return ret;
112 }
113
sensing_get_config(sensing_sensor_handle_t handle,struct sensing_sensor_config * configs,int count)114 int sensing_get_config(sensing_sensor_handle_t handle,
115 struct sensing_sensor_config *configs,
116 int count)
117 {
118 struct sensing_sensor_config *cfg;
119 int i, ret = 0;
120
121 if (handle == NULL || configs == NULL) {
122 return -ENODEV;
123 }
124
125 if (count <= 0 || count > SENSING_SENSOR_ATTRIBUTE_MAX) {
126 LOG_ERR("invalid config count:%d", count);
127 return -EINVAL;
128 }
129
130 for (i = 0; i < count; i++) {
131 cfg = &configs[i];
132 switch (cfg->attri) {
133 case SENSING_SENSOR_ATTRIBUTE_INTERVAL:
134 ret |= get_interval(handle, &cfg->interval);
135 break;
136
137 case SENSING_SENSOR_ATTRIBUTE_SENSITIVITY:
138 ret |= get_sensitivity(handle, cfg->data_field, &cfg->sensitivity);
139 break;
140
141 case SENSING_SENSOR_ATTRIBUTE_LATENCY:
142 break;
143
144 default:
145 ret = -EINVAL;
146 LOG_ERR("invalid config attribute:%d\n", cfg->attri);
147 break;
148 }
149 }
150
151 return ret;
152 }
153
sensing_get_sensor_info(sensing_sensor_handle_t handle)154 const struct sensing_sensor_info *sensing_get_sensor_info(sensing_sensor_handle_t handle)
155 {
156 return get_sensor_info(handle);
157 }
158