ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
alphabet_soup_cli.c
Go to the documentation of this file.
1
9
10#include <string.h>
11
12#include "alphabet_soup.h"
14#include "fw_if_fs.h"
15#include "fw_if_fs_posix.h"
16
23
25 char* out_root,
26 size_t root_cap,
27 char* out_leaf,
28 size_t leaf_cap)
29{
30 if ((filepath == nullptr) || (out_root == nullptr) || (out_leaf == nullptr)) {
31 return k_ra8_err_null_ptr;
32 }
33 const size_t len = strlen(filepath);
34 if ((len == 0U) || (len >= root_cap) || ((len + 1U) >= leaf_cap)) {
36 }
37 const char* last_slash = strrchr(filepath, '/');
38 if (last_slash == nullptr) {
39 out_root[0] = '.';
40 out_root[1] = '\0';
41 out_leaf[0] = '/';
42 (void)memcpy(&out_leaf[1], filepath, len + 1U);
43 } else if (last_slash == filepath) {
44 out_root[0] = '/';
45 out_root[1] = '\0';
46 (void)memcpy(out_leaf, last_slash, len + 1U);
47 } else {
48 const size_t dir_len = len - strlen(last_slash);
49 if (dir_len >= root_cap) {
51 }
52 (void)memcpy(out_root, filepath, dir_len);
53 out_root[dir_len] = '\0';
54 (void)memcpy(out_leaf, last_slash, len - dir_len + 1U);
55 }
56 return k_ra8_ok;
57}
58
60 uint8_t* buffer,
61 uint32_t capacity,
62 uint32_t* out_size)
63{
64 if ((file == nullptr) || (buffer == nullptr) || (out_size == nullptr)) {
65 return k_ra8_err_null_ptr;
66 }
67 uint32_t total = 0U;
68 uint32_t chunk_bytes = 0U;
69 for (uint32_t step = 0U;
70 step < (((uint32_t)k_soup_max_file_capacity / (uint32_t)k_cli_read_chunk_bytes) + 2U);
71 ++step) {
72 chunk_bytes = 0U;
73 uint32_t remaining = capacity - total;
74 if (remaining == 0U) {
76 }
77 const uint32_t read_limit =
78 (remaining < k_cli_read_chunk_bytes) ? remaining : k_cli_read_chunk_bytes;
79 const ra8_err_t err = fw_fs_read(file, &buffer[total], read_limit, &chunk_bytes);
80 if (err != k_ra8_ok) {
81 return err;
82 }
83 if (chunk_bytes == 0U) {
84 break;
85 }
86 total += chunk_bytes;
87 }
88 *out_size = total;
89 return k_ra8_ok;
90}
91
93 uint8_t* out_buf,
94 uint32_t buf_cap,
95 uint32_t* out_len)
96{
97 char root_dir[k_cli_path_buffer_capacity] = {};
98 char portable_path[k_cli_path_buffer_capacity] = {};
100 root_dir,
101 sizeof(root_dir),
102 portable_path,
103 sizeof(portable_path)) != k_ra8_ok) {
105 }
106 fw_fs_t fs = {};
107 fw_fs_posix_state_t posix_state = {.root_fd = -1};
108 const fw_fs_posix_cfg_t cfg = {.root_path = root_dir, .removable_media = false};
109 if (fw_fs_posix_init(&fs, &posix_state, &cfg) != k_ra8_ok) {
110 return k_ra8_fail;
111 }
112 alignas(max_align_t) uint8_t file_work[k_cli_file_work_capacity] = {};
113 fw_fs_file_t file = {};
114 if (fw_fs_open(&fs.streams,
115 portable_path,
117 &file,
118 file_work,
119 sizeof(file_work)) != k_ra8_ok) {
120 (void)fw_fs_posix_deinit(&posix_state);
121 return k_ra8_err_not_found;
122 }
123 const ra8_err_t err = priv_alphabet_soup_read_all(&file, out_buf, buf_cap, out_len);
124 (void)fw_fs_close(&file);
125 (void)fw_fs_posix_deinit(&posix_state);
126 return err;
127}
Word search puzzle solver API and bounded workspace definitions.
@ k_soup_max_file_capacity
Maximum supported puzzle file size.
ra8_err_t priv_alphabet_soup_read_all(fw_fs_file_t *file, uint8_t *buffer, uint32_t capacity, uint32_t *out_size)
ra8_err_t priv_alphabet_soup_load_file_contents(const char *filepath, uint8_t *out_buf, uint32_t buf_cap, uint32_t *out_len)
alphabet_soup_cli_limit_t
Buffer sizes and limits for host CLI file ingestion.
@ k_cli_file_work_capacity
File handle workspace bytes.
@ k_cli_read_chunk_bytes
Bounded read chunk bytes.
@ k_cli_path_buffer_capacity
Host path buffer bytes.
ra8_err_t priv_alphabet_soup_split_path(const char *filepath, char *out_root, size_t root_cap, char *out_leaf, size_t leaf_cap)
Private host-file helpers for the Alphabet Soup CLI wrapper.
Architecture-neutral filesystem namespace, stream, and transaction ports.
ra8_err_t fw_fs_open(const fw_fs_stream_port_t *streams, const char *path, fw_fs_open_mode_t mode, fw_fs_file_t *file, void *workspace, uint32_t workspace_size)
Open a file into a caller-owned handle and backend workspace.
Definition fw_if_fs.c:648
ra8_err_t fw_fs_read(fw_fs_file_t *file, uint8_t *dst, uint32_t cap, uint32_t *out_read)
Read up to cap bytes; zero bytes is EOF.
Definition fw_if_fs.c:699
ra8_err_t fw_fs_close(fw_fs_file_t *file)
Close and consume an open handle.
Definition fw_if_fs.c:783
Root-confined hosted POSIX adapter for fw_if_fs.
ra8_err_t fw_fs_posix_deinit(fw_fs_posix_state_t *state)
Close the root descriptor; no bound operation is valid afterward.
ra8_err_t fw_fs_posix_init(fw_fs_t *out, fw_fs_posix_state_t *state, const fw_fs_posix_cfg_t *cfg)
Open/configure a root-confined POSIX binding.
@ k_fw_fs_open_read
Existing file, read-only.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
@ k_ra8_fail
Generic unspecified failure.
Definition ra8_err.h:133
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_null_ptr
Pointer was NULL where a valid pointer was required.
Definition ra8_err.h:478
@ k_ra8_err_not_found
Requested item not found (lookup / search missed).
Definition ra8_err.h:173
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
size_t strlen(const char *s)
Calculate string length.
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.
char * strrchr(const char *s, int c)
Locate last occurrence of character in string.
Caller-owned open file; fields are private to the facade.
POSIX composition-root settings.
Caller-owned POSIX adapter state.
One complete composition-root filesystem binding.
fw_fs_stream_port_t streams
Byte-stream operations.