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