1 /*
2  * Copyright (c) 2024 Meta Platforms
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT vnd_fake_sensor
8 
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/logging/log.h>
12 
13 LOG_MODULE_REGISTER(fake_sensor);
14 
init(const struct device * dev)15 static int init(const struct device *dev)
16 {
17 	ARG_UNUSED(dev);
18 
19 	return 0;
20 }
21 
attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)22 static int attr_set(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr,
23 		    const struct sensor_value *val)
24 {
25 	LOG_DBG("[%s] dev: %p, chan: %d, attr: %d, val1: %d, val2: %d", __func__, dev, chan, attr,
26 		val->val1, val->val2);
27 
28 	return 0;
29 }
30 
attr_get(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,struct sensor_value * val)31 static int attr_get(const struct device *dev, enum sensor_channel chan, enum sensor_attribute attr,
32 		    struct sensor_value *val)
33 {
34 	LOG_DBG("[%s] dev: %p, chan: %d, attr: %d", __func__, dev, chan, attr);
35 
36 	val->val1 = chan;
37 	val->val2 = attr * 100000;
38 
39 	return 0;
40 }
41 
sample_fetch(const struct device * dev,enum sensor_channel chan)42 static int sample_fetch(const struct device *dev, enum sensor_channel chan)
43 {
44 	LOG_DBG("[%s] dev: %p, chan: %d", __func__, dev, chan);
45 
46 	return 0;
47 }
48 
channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)49 static int channel_get(const struct device *dev, enum sensor_channel chan, struct sensor_value *val)
50 {
51 	LOG_DBG("[%s] dev: %p, chan: %d", __func__, dev, chan);
52 
53 	switch (chan) {
54 	case SENSOR_CHAN_ACCEL_XYZ:
55 		__fallthrough;
56 	case SENSOR_CHAN_GYRO_XYZ:
57 		__fallthrough;
58 	case SENSOR_CHAN_MAGN_XYZ:
59 		__fallthrough;
60 	case SENSOR_CHAN_POS_DXYZ:
61 		for (int i = 0; i < 3; i++, val++) {
62 			val->val1 = chan;
63 			val->val2 = 1;
64 		}
65 		break;
66 	default:
67 		val->val1 = chan;
68 		val->val2 = 1;
69 		break;
70 	}
71 
72 	return 0;
73 }
74 
trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)75 static int trigger_set(const struct device *dev, const struct sensor_trigger *trig,
76 		       sensor_trigger_handler_t handler)
77 {
78 	LOG_DBG("[%s - %s] dev: %p, trig->chan: %d, trig->type: %d, handler: %p", __func__,
79 		(handler == NULL) ? "off" : "on", dev, trig->chan, trig->type, handler);
80 
81 	return 0;
82 }
83 
84 static DEVICE_API(sensor, api) = {
85 	.attr_get = attr_get,
86 	.attr_set = attr_set,
87 	.sample_fetch = sample_fetch,
88 	.channel_get = channel_get,
89 	.trigger_set = trigger_set,
90 };
91 
92 #define VND_SENSOR_INIT(n)                                                                         \
93 	SENSOR_DEVICE_DT_INST_DEFINE(n, init, NULL, NULL, NULL, POST_KERNEL,                       \
94 				     CONFIG_SENSOR_INIT_PRIORITY, &api);
95 
96 DT_INST_FOREACH_STATUS_OKAY(VND_SENSOR_INIT)
97