Skip to content

Commit 62cc42b

Browse files
srberardArturSir
andauthored
Added dynamic sensor discovery (#58)
* Added dynamic sensor discovery Co-authored-by: ArturSir <artur@atym.io> Signed-off-by: Stephen Berard <stephen.berard@outlook.com> --------- Signed-off-by: Stephen Berard <stephen.berard@outlook.com> Co-authored-by: ArturSir <artur@atym.io>
1 parent 66c44f5 commit 62cc42b

6 files changed

Lines changed: 55 additions & 53 deletions

File tree

Kconfig

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ config OCRE_LOG_DEBUG
5757
help
5858
Enable Ocre debug logging
5959

60+
config OCRE_LOG_ERR
61+
bool "Error logging"
62+
default n
63+
help
64+
Enable Ocre debug logging
65+
66+
config OCRE_LOG_WARN
67+
bool "Warn logging"
68+
default n
69+
help
70+
Enable Ocre debug logging
71+
72+
config OCRE_LOG_INF
73+
bool "Info logging"
74+
default n
75+
help
76+
Enable Ocre debug logging
77+
6078
if OCRE
6179

6280
module = OCRE
@@ -96,7 +114,6 @@ config OCRE_CONTAINER_DEFAULT_STACK_SIZE
96114
default 2048
97115
help
98116
The default value used for a container's stack size.
99-
100117

101118
config MAX_TIMERS
102119
int "Maximum number of timers"
@@ -162,4 +179,4 @@ config MESSAGING_MAX_SUBSCRIPTIONS
162179
help
163180
Number of maximum subscriptions for Container Messaging
164181

165-
endmenu
182+
endmenu

prj.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=-1
6565

6666
# Ocre configuration
6767
CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=512000
68-
CONFIG_OCRE_CONTAINER_DEFAULT_HEAP_SIZE=32768
69-
CONFIG_OCRE_CONTAINER_DEFAULT_STACK_SIZE=16384
68+
CONFIG_OCRE_CONTAINER_DEFAULT_HEAP_SIZE=4096
69+
CONFIG_OCRE_CONTAINER_DEFAULT_STACK_SIZE=4096
7070
CONFIG_LOG_TRACE_SHORT_TIMESTAMP=y
7171

7272
# Ocre Feature Options

src/ocre/api/ocre_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ NativeSymbol ocre_api_table[] = {
114114
// GPIO API
115115
{"ocre_gpio_init", ocre_gpio_wasm_init, "()i", NULL},
116116
{"ocre_gpio_configure", ocre_gpio_wasm_configure, "(iii)i", NULL},
117-
{"ocre_gpio_set", ocre_gpio_wasm_set, "(iii)i", NULL},
118-
{"ocre_gpio_get", ocre_gpio_wasm_get, "(ii)i", NULL},
119-
{"ocre_gpio_toggle", ocre_gpio_wasm_toggle, "(ii)i", NULL},
117+
{"ocre_gpio_pin_set", ocre_gpio_wasm_set, "(iii)i", NULL},
118+
{"ocre_gpio_pin_get", ocre_gpio_wasm_get, "(ii)i", NULL},
119+
{"ocre_gpio_pin_toggle", ocre_gpio_wasm_toggle, "(ii)i", NULL},
120120
{"ocre_gpio_register_callback", ocre_gpio_wasm_register_callback, "(ii)i", NULL},
121121
{"ocre_gpio_unregister_callback", ocre_gpio_wasm_unregister_callback, "(ii)i", NULL},
122122
#endif

src/ocre/container_messaging/messaging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ typedef struct ocre_msg {
1717
// payload format (MIME type??)
1818
char *content_type;
1919

20-
// payload of the request, currently only support attr_container_t type
20+
// payload of the request
2121
void *payload;
2222

2323
// length in bytes of the payload

src/ocre/ocre_gpio/ocre_gpio.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
#include <stdbool.h>
1313
#include "wasm_export.h"
1414

15-
#define CONFIG_BOARD_B_U585I_IOT02A
16-
// #define CONFIG_BOARD_ESP32C3_DEVKITM
17-
1815
// Default configuration values if not provided by Kconfig
1916
#ifndef CONFIG_OCRE_GPIO_MAX_PINS
2017
#define CONFIG_OCRE_GPIO_MAX_PINS 32

src/ocre/ocre_sensors/ocre_sensors.c

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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)};
3628
typedef 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};
4335
static int sensor_count = 0;
4436
static 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-
6038
static 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

Comments
 (0)