|
5 | 5 | * SPDX-License-Identifier: Apache-2.0 |
6 | 6 | */ |
7 | 7 |
|
8 | | -#include <zephyr/kernel.h> |
9 | | -#include <zephyr/fs/fs.h> |
10 | | -#include "ocre_core_external.h" |
11 | | -#include <zephyr/net/net_if.h> |
12 | | -#include <zephyr/net/dhcpv4.h> |
13 | | - |
14 | | -#include <ocre/ocre.h> |
15 | | -#include <ocre/ocre_container_runtime/ocre_container_runtime.h> |
16 | | - |
17 | | -#ifdef HAS_GENERATED_INPUT |
18 | | -#include <ocre/ocre_input_file.g> |
19 | | -#else |
20 | | -#include <ocre/ocre_input_file.h> |
21 | | -#endif |
22 | | - |
23 | | -void create_sample_container(); |
24 | | -int ocre_network_init(); |
25 | | - |
26 | | -int main(int argc, char *argv[]) { |
27 | | - ocre_cs_ctx ctx; |
28 | | - ocre_container_init_arguments_t args; |
29 | | - char *container_filename = "hello"; |
30 | | - |
31 | | -#ifdef CONFIG_OCRE_NETWORKING |
32 | | - int net_status = ocre_network_init(); |
33 | | - if (net_status < 0) { |
34 | | - printf("Unable to connect to network\n"); |
35 | | - } else { |
36 | | - printf("Network is UP\n"); |
37 | | - } |
38 | | -#endif |
39 | | - |
40 | | - ocre_app_storage_init(); |
41 | | - |
42 | | - // Step 1: Initialize the Ocre runtime |
43 | | - ocre_container_runtime_status_t ret = ocre_container_runtime_init(&ctx, &args); |
44 | | - |
45 | | - if (ret == RUNTIME_STATUS_INITIALIZED) { |
46 | | - printf("\n\nOcre runtime started\n"); |
47 | | - |
48 | | - create_sample_container(container_filename); |
49 | | - |
50 | | - // Step 2: Create the container, this allocates and loads the container binary |
51 | | - ocre_container_data_t ocre_container_data; |
52 | | - int container_ID; |
53 | | - |
54 | | - ocre_container_data.heap_size = 0; |
55 | | - snprintf(ocre_container_data.name, sizeof(ocre_container_data.name), "Hello World"); |
56 | | - snprintf(ocre_container_data.sha256, sizeof(ocre_container_data.sha256), "%s", container_filename); |
57 | | - ocre_container_data.timers = 0; |
58 | | - ocre_container_runtime_create_container(&ctx, &ocre_container_data, &container_ID, NULL); |
59 | | - |
60 | | - // Step 3: Execute the container |
61 | | - ocre_container_runtime_run_container(container_ID, NULL); |
62 | | - // Loop forever, without this the application will exit and stop all execution |
63 | | - while (true) { |
64 | | - core_sleep_ms(1000); |
| 8 | + #include <zephyr/kernel.h> |
| 9 | + #include <zephyr/fs/fs.h> |
| 10 | + #include "ocre_core_external.h" |
| 11 | + #include <zephyr/net/net_if.h> |
| 12 | + #include <zephyr/net/dhcpv4.h> |
| 13 | + |
| 14 | + #include <ocre/ocre.h> |
| 15 | + #include <ocre/ocre_container_runtime/ocre_container_runtime.h> |
| 16 | + |
| 17 | + #ifdef HAS_WASM_MANIFEST |
| 18 | + #include "wasm_manifest.h" |
| 19 | + #endif |
| 20 | + |
| 21 | + void create_containers_from_manifest(ocre_cs_ctx *ctx); |
| 22 | + int ocre_network_init(); |
| 23 | + |
| 24 | + int main(int argc, char *argv[]) { |
| 25 | + ocre_cs_ctx ctx; |
| 26 | + ocre_container_init_arguments_t args; |
| 27 | + |
| 28 | + #ifdef CONFIG_OCRE_NETWORKING |
| 29 | + int net_status = ocre_network_init(); |
| 30 | + if (net_status < 0) { |
| 31 | + printf("Unable to connect to network\n"); |
| 32 | + } else { |
| 33 | + printf("Network is UP\n"); |
| 34 | + } |
| 35 | + #endif |
| 36 | + |
| 37 | + ocre_app_storage_init(); |
| 38 | + |
| 39 | + // Step 1: Initialize the Ocre runtime |
| 40 | + ocre_container_runtime_status_t ret = ocre_container_runtime_init(&ctx, &args); |
| 41 | + |
| 42 | + if (ret == RUNTIME_STATUS_INITIALIZED) { |
| 43 | + printf("\n\nOcre runtime started\n"); |
| 44 | + |
| 45 | + #ifdef HAS_WASM_MANIFEST |
| 46 | + // Create and run all containers from manifest |
| 47 | + printf("Found %d WASM binaries to load\n", WASM_BINARY_COUNT); |
| 48 | + create_containers_from_manifest(&ctx); |
| 49 | + #else |
| 50 | + printf("ERROR: No WASM binaries provided. Build with -f option.\n"); |
| 51 | + #endif |
| 52 | + |
| 53 | + // Loop forever, without this the application will exit and stop all execution |
| 54 | + while (true) { |
| 55 | + core_sleep_ms(1000); |
| 56 | + } |
| 57 | + |
| 58 | + } else { |
| 59 | + printf("\n\nOcre runtime failed to start.\n"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + #ifdef HAS_WASM_MANIFEST |
| 64 | + /** |
| 65 | + * Create and run all containers from the embedded WASM manifest |
| 66 | + */ |
| 67 | + void create_containers_from_manifest(ocre_cs_ctx *ctx) { |
| 68 | + int container_IDs[WASM_BINARY_COUNT]; |
| 69 | + |
| 70 | + // Step 1: Write all WASM binaries to filesystem |
| 71 | + for (int i = 0; i < WASM_BINARY_COUNT; i++) { |
| 72 | + const wasm_binary_entry_t *entry = wasm_get(i); |
| 73 | + size_t actual_size = wasm_get_size(i); // ← Use the helper function! |
| 74 | + |
| 75 | + printf("Loading WASM: %s (%zu bytes) from flash\n", entry->name, actual_size); |
| 76 | + |
| 77 | + // Write to filesystem |
| 78 | + char file_path[64]; |
| 79 | + struct fs_file_t f; |
| 80 | + snprintf(file_path, sizeof(file_path), "/lfs/ocre/images/%s.bin", entry->name); |
| 81 | + |
| 82 | + fs_file_t_init(&f); |
| 83 | + int res = fs_open(&f, file_path, FS_O_CREATE | FS_O_RDWR); |
| 84 | + if (res < 0) { |
| 85 | + printf(" ERROR: Failed to create file: %d\n", res); |
| 86 | + continue; |
65 | 87 | } |
66 | 88 |
|
67 | | - } else { |
68 | | - printf("\n\nOcre runtime failed to start.\n"); |
| 89 | + fs_write(&f, entry->data, actual_size); // ← Use actual_size |
| 90 | + fs_close(&f); |
| 91 | + printf(" Written to: %s\n", file_path); |
69 | 92 | } |
70 | | -} |
71 | 93 |
|
72 | | -/** |
73 | | - * Creates a container image file using the sample "hello-word" WASM module |
74 | | - * This is for demostration purposes only and does not perform any error checking. |
75 | | - * |
76 | | - * @param file_name a string containing the name of the file to create |
77 | | - */ |
| 94 | + // Step 2: Create all containers |
| 95 | + for (int i = 0; i < WASM_BINARY_COUNT; i++) { |
| 96 | + const wasm_binary_entry_t *entry = wasm_get(i); |
| 97 | + |
| 98 | + ocre_container_data_t container_data; |
| 99 | + container_data.heap_size = 0; |
| 100 | + snprintf(container_data.name, sizeof(container_data.name), "%s", entry->name); |
| 101 | + snprintf(container_data.sha256, sizeof(container_data.sha256), "%s", entry->name); |
| 102 | + container_data.timers = 0; |
| 103 | + |
| 104 | + ocre_container_status_t status = ocre_container_runtime_create_container(ctx, &container_data, |
| 105 | + &container_IDs[i], NULL); |
| 106 | + // Check for CREATED status, not 0! |
| 107 | + if (status == CONTAINER_STATUS_CREATED) { |
| 108 | + printf("Container requested: %s (ID: %d)\n", entry->name, container_IDs[i]); |
| 109 | + core_sleep_ms(100); |
| 110 | + } else { |
| 111 | + printf("ERROR: Failed to request container %s: %d\n", entry->name, status); |
| 112 | + container_IDs[i] = -1; |
| 113 | + } |
| 114 | + } |
78 | 115 |
|
79 | | -void create_sample_container(char *file_name) { |
80 | | - static char file_path[64]; |
81 | | - struct fs_file_t f; |
82 | | - snprintf((char *)&file_path, 64, "/lfs/ocre/images/%s.bin", file_name); |
83 | | - int res; |
| 116 | + // Give containers time to actually be created asynchronously |
| 117 | + printf("Waiting for containers to be created...\n"); |
| 118 | + core_sleep_ms(500); // Wait for async creation to complete |
84 | 119 |
|
85 | | - fs_file_t_init(&f); |
86 | | - res = fs_open(&f, file_path, FS_O_CREATE | FS_O_RDWR); |
| 120 | + // Run all containers |
| 121 | + for (int i = 0; i < WASM_BINARY_COUNT; i++) { |
| 122 | + if (container_IDs[i] >= 0) { |
| 123 | + const wasm_binary_entry_t *entry = wasm_get(i); |
| 124 | + printf("Starting container: %s\n", entry->name); |
| 125 | + ocre_container_runtime_run_container(container_IDs[i], NULL); |
| 126 | + } |
| 127 | + } |
87 | 128 |
|
88 | | - fs_write(&f, &wasm_binary, wasm_binary_len); |
89 | | - fs_close(&f); |
| 129 | + printf("All containers started!\n"); |
90 | 130 | } |
| 131 | + #endif |
91 | 132 |
|
92 | | -int ocre_network_init() { |
| 133 | + int ocre_network_init() { |
| 134 | + struct net_if *iface = net_if_get_default(); |
| 135 | + net_dhcpv4_start(iface); |
93 | 136 |
|
94 | | - struct net_if *iface = net_if_get_default(); |
95 | | - net_dhcpv4_start(iface); |
| 137 | + printf("Waiting for network to be ready...\n"); |
96 | 138 |
|
97 | | - printf("Waiting for network to be ready...\n"); |
| 139 | + int sleep_cnt = 0; |
| 140 | + while (!net_if_is_up(iface) && (sleep_cnt < 10)) { |
| 141 | + k_sleep(K_MSEC(200)); |
| 142 | + sleep_cnt++; |
| 143 | + } |
98 | 144 |
|
99 | | - int sleep_cnt = 0; |
100 | | - while (!net_if_is_up(iface) && (sleep_cnt < 10)) { |
101 | | - k_sleep(K_MSEC(200)); |
102 | | - sleep_cnt++; |
103 | | - } |
| 145 | + if (!net_if_is_up(iface)) { |
| 146 | + return -ENOTCONN; |
| 147 | + } |
104 | 148 |
|
105 | | - if (!net_if_is_up(iface)) { |
106 | | - return -ENOTCONN; |
107 | | - } |
108 | | - |
109 | | - return 0; |
110 | | -} |
| 149 | + return 0; |
| 150 | + } |
0 commit comments