1 /*
2 * Copyright (c) 2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/sensing/sensing.h>
11 #include <zephyr/sensing/sensing_sensor.h>
12
13 #define DT_DRV_COMPAT zephyr_sensing
14 #define MAX_SENSOR_TYPES 2
15 #define DT_SENSOR_INFO(node) \
16 { \
17 .name = DT_NODE_FULL_NAME(node), \
18 .friendly_name = DT_PROP(node, friendly_name), \
19 .sensor_types = DT_PROP(node, sensor_types), \
20 .sensor_type_count = DT_PROP_LEN(node, sensor_types), \
21 }
22
23 struct sensor_info_t {
24 const char *name;
25 const char *friendly_name;
26 const int sensor_types[MAX_SENSOR_TYPES];
27 int sensor_type_count;
28 };
29
30 static const struct sensor_info_t sensors[] = {
31 DT_FOREACH_CHILD_STATUS_OKAY_SEP(DT_DRV_INST(0), DT_SENSOR_INFO, (,))
32 };
33
get_total_sensor_counts(void)34 static int get_total_sensor_counts(void)
35 {
36 int total = 0;
37
38 for (int i = 0; i < ARRAY_SIZE(sensors); i++) {
39 total += sensors[i].sensor_type_count;
40 }
41 return total;
42 }
43
check_sensor_type(const struct sensing_sensor_info * info,const struct sensor_info_t * sensor)44 static bool check_sensor_type(const struct sensing_sensor_info *info,
45 const struct sensor_info_t *sensor)
46 {
47 for (int i = 0; i < sensor->sensor_type_count; ++i) {
48 if (info->type == sensor->sensor_types[i]) {
49 return true;
50 }
51 }
52 return false;
53 }
54
55 /**
56 * @brief Test Get Sensors
57 *
58 * This test verifies sensing_get_sensors.
59 */
ZTEST(sensing_tests,test_sensing_get_sensors)60 ZTEST(sensing_tests, test_sensing_get_sensors)
61 {
62 const struct sensing_sensor_info *info;
63 int ret, total_sensor_counts = get_total_sensor_counts();
64 int num = total_sensor_counts;
65
66 ret = sensing_get_sensors(&num, &info);
67 zassert_equal(ret, 0, "Sensing Get Sensors failed");
68 zassert_equal(num, total_sensor_counts, "Expected %d sensors, but got %d",
69 total_sensor_counts, num);
70 zassert_not_null(info, "Expected sensor info to be not null");
71
72 for (int i = 0; i < num; ++i) {
73 bool found = false;
74
75 for (int j = 0; j < ARRAY_SIZE(sensors); ++j) {
76 if (strcmp(info[i].name, sensors[j].name) == 0) {
77 zassert_true(strcmp(info[i].friendly_name,
78 sensors[j].friendly_name) == 0,
79 "Mismatch in friendly name for sensor '%s'",
80 info[i].name);
81 zassert_true(check_sensor_type(&info[i], &sensors[j]),
82 "Mismatch in sensor type for sensor '%s'",
83 info[i].name);
84 found = true;
85 break;
86 }
87 }
88 zassert_true(found, "Sensor '%s' not found", info[i].name);
89 }
90 }
91
92 ZTEST_SUITE(sensing_tests, NULL, NULL, NULL, NULL, NULL);
93