@@ -13,26 +13,18 @@ LOG_MODULE_DECLARE(ocre_sensors, OCRE_LOG_LEVEL);
1313
1414#include "ocre_sensors.h"
1515
16- /*
17- * Build a list of device nodes from the devicetree if:
18- * 1. OCRE_SENSORS is enabled
19- * 2. The 'devices' node exists in the devicetree
20- * 3. The 'devices' node has at least one child
21- * 4. The 'devices' node has property label
22- */
2316#define DEVICE_NODE DT_PATH(devices)
24- #define HAS_DEVICE_NODES DT_NODE_EXISTS(DEVICE_NODE) && DT_CHILD_NUM(DEVICE_NODE) > 0
25-
17+ #define HAS_DEVICE_NODES DT_NODE_EXISTS(DEVICE_NODE) && DT_PROP_LEN(DEVICE_NODE, device_list) > 1
2618#if (CONFIG_OCRE_SENSORS ) && (HAS_DEVICE_NODES )
27- #define DEVICE_NODES_LIST (node_id ) COND_CODE_1(DT_NODE_HAS_PROP(node_id, label), (DT_PROP(node_id, label), ), ())
28- static const char * device_nodes [] = {DT_FOREACH_CHILD (DEVICE_NODE , DEVICE_NODES_LIST )};
29- #define DEVICE_NODES_COUNT (sizeof(device_nodes) / sizeof(device_nodes[0]))
19+ #define EXTRACT_LABELS (node_id , prop , idx ) DT_PROP_BY_PHANDLE_IDX_OR(node_id, prop, idx, label, "undefined")
20+ static const char * sensor_discovery_names [] = {
21+ DT_FOREACH_PROP_ELEM_SEP (DEVICE_NODE , device_list , EXTRACT_LABELS , (, ))};
22+ static int sensor_names_count = sizeof (sensor_discovery_names ) / sizeof (sensor_discovery_names [0 ]);
3023#else
31- static const char * device_nodes [] = {};
32- #define DEVICE_NODES_COUNT 0
24+ static const char * sensor_discovery_names [] = {};
25+ static int sensor_names_count = 0 ;
3326#endif
3427
35- static const char * device_nodes [] = {DT_FOREACH_CHILD (DEVICE_NODE , DEVICE_NODES_LIST )};
3628typedef struct {
3729 const struct device * device ;
3830 ocre_sensor_t info ;
@@ -43,20 +35,6 @@ static ocre_sensor_internal_t sensors[CONFIG_MAX_SENSORS] = {0};
4335static int sensor_count = 0 ;
4436static char sensor_names [CONFIG_MAX_SENSORS ][CONFIG_MAX_SENSOR_NAME_LENGTH ];
4537
46- static bool is_custom_device (const struct device * dev ) {
47- if (!dev ) {
48- LOG_ERR ("Device is NULL" );
49- return false;
50- }
51-
52- for (int i = 0 ; i < DEVICE_NODES_COUNT ; i ++ ) {
53- if (strcmp (dev -> name , device_nodes [i ]) == 0 ) {
54- return true;
55- }
56- }
57- return false;
58- }
59-
6038static int set_opened_channels (const struct device * dev , sensor_channel_t * channels ) {
6139 if (!channels ) {
6240 LOG_ERR ("Channels array is NULL" );
@@ -104,41 +82,51 @@ int ocre_sensors_discover(wasm_exec_env_t exec_env) {
10482 LOG_ERR ("Device list is NULL. Possible memory corruption!" );
10583 return -1 ;
10684 }
107-
10885 if (device_count == 0 ) {
10986 LOG_ERR ("No static devices found" );
11087 return -1 ;
11188 }
11289
11390 LOG_INF ("Total static devices found: %zu" , device_count );
11491
115- if (!device_nodes [0 ]) {
116- LOG_ERR ("No devices found in DeviceTree!" );
117- return -1 ;
118- }
119-
12092 for (size_t i = 0 ; i < device_count && sensor_count < CONFIG_MAX_SENSORS ; i ++ ) {
12193 if (!dev [i ].name ) {
12294 LOG_ERR ("Device %zu has NULL name, skipping!" , i );
12395 continue ;
12496 }
12597
126- if (!is_custom_device (& dev [i ])) {
127- LOG_WRN ("Skipping non-custom device: %s" , dev [i ].name );
128- continue ;
129- }
98+ LOG_INF ("Checking device: %s" , dev [i ].name );
13099
131- if (!device_is_ready (& dev [i ])) {
132- LOG_WRN ("Device %s is not ready, skipping" , dev [i ].name );
100+ // Check if device name is in the sensor discovery list
101+ bool sensor_found = false;
102+ for (int j = 0 ; j < sensor_names_count ; j ++ ) {
103+ if (strcmp (dev [i ].name , sensor_discovery_names [j ]) == 0 ) {
104+ sensor_found = true;
105+ break ;
106+ }
107+ }
108+ if (!sensor_found ) {
109+ LOG_WRN ("Skipping device, not in sensor list: %s" , dev [i ].name );
133110 continue ;
134111 }
135112
113+ // if (!device_is_ready(&dev[i])) {
114+ // LOG_WRN("Device %s is not ready, skipping", dev[i].name);
115+ // continue;
116+ // }
117+
136118 const struct sensor_driver_api * api = (const struct sensor_driver_api * )dev [i ].api ;
137119 if (!api || !api -> channel_get ) {
138120 LOG_WRN ("Device %s does not support sensor API or channel_get, skipping" , dev [i ].name );
139121 continue ;
140122 }
141123
124+ // Ensure we don't exceed sensor limit
125+ if (sensor_count >= CONFIG_MAX_SENSORS ) {
126+ LOG_WRN ("Max sensor limit reached, skipping device: %s" , dev [i ].name );
127+ continue ;
128+ }
129+
142130 // Initialize the sensor
143131 ocre_sensor_internal_t * sensor = & sensors [sensor_count ];
144132 sensor -> device = & dev [i ];
@@ -156,7 +144,7 @@ int ocre_sensors_discover(wasm_exec_env_t exec_env) {
156144 continue ;
157145 }
158146
159- LOG_INF ("Device %s has %d channels" , sensor -> info . sensor_name , sensor -> info .num_channels );
147+ LOG_INF ("Device has %d channels" , sensor -> info .num_channels );
160148 sensor_count ++ ;
161149 }
162150
0 commit comments