ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_io_stream_posix.c
Go to the documentation of this file.
1
17
18#ifndef RA8_OFF_TARGET
19#error "port/posix is host-only and must never be compiled or linked into target firmware."
20#endif
21
22#include "ra8_io_stream_posix.h"
23
24#include <errno.h>
25#include <stdint.h> // ra8-keep-include: `uint8_t` used directly
26#include <unistd.h>
27
28#include "ra8_attributes.h" // ra8-keep-include: `RA8_INTERNAL` and `RA8_PRIV` used directly
29#include "ra8_err.h" // ra8-keep-include: `ra8_err_t` used directly
32
37
61{
62 if (error_number == 0) {
64 }
65 if (error_number == EAGAIN
66#if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN)
67 || error_number == EWOULDBLOCK
68#endif
69 ) {
71 }
72 if (error_number == EBADF) {
74 }
75 if (error_number == ENOSPC || error_number == EDQUOT) {
76 return k_ra8_err_no_mem;
77 }
78 if (error_number == EACCES || error_number == EPERM) {
80 }
81 if (error_number == EFBIG || error_number == EOVERFLOW) {
83 }
84 if (error_number == EINVAL) {
86 }
87 if (error_number == EPIPE || error_number == EIO) {
89 }
90 return k_ra8_fail;
91}
92
120 const uint8_t* bytes,
121 uint32_t length,
122 uint32_t* done,
123 uint32_t* interrupts,
125 void* writer_context)
126{
127 int error_number = 0;
128 const int64_t result = writer(writer_context, fd, &bytes[*done], length - *done, &error_number);
129 if (result < 0) {
130 if (error_number == EINTR) {
131 ++*interrupts;
132 if (*interrupts > (uint32_t)k_posix_write_interrupt_limit) {
134 }
135 return k_ra8_ok;
136 }
137 return internal_map_errno(error_number);
138 }
139 if ((result == 0) || ((uint64_t)result > (uint64_t)(length - *done))) {
141 }
142 *done += (uint32_t)result;
143 return k_ra8_ok;
144}
145
147 const uint8_t* bytes,
148 uint32_t length,
149 uint32_t* out_written,
151 void* writer_context)
152{
153 if ((bytes == nullptr) || (out_written == nullptr) || (writer == nullptr)) {
154 return k_ra8_err_null_ptr;
155 }
156 *out_written = 0U;
157 uint32_t done = 0U;
158 uint32_t interrupts = 0U;
159 RA8_LOOP_BOUND(UINT32_MAX);
160 while (done < length) {
161 const ra8_err_t err =
162 internal_write_loop_step(fd, bytes, length, &done, &interrupts, writer, writer_context);
163 if (err != k_ra8_ok) {
164 *out_written = done;
165 return err;
166 }
167 }
168 *out_written = done;
169 return k_ra8_ok;
170}
171
197RA8_INTERNAL static int64_t
198internal_native_write(void* context, int fd, const uint8_t* bytes, uint32_t length, int* out_errno)
199{
200 (void)context;
201 const ssize_t result = write(fd, bytes, (size_t)length);
202 *out_errno = (result < 0) ? errno : 0;
203 return (int64_t)result;
204}
205
231internal_posix_write(void* context, const uint8_t* bytes, uint32_t length, uint32_t* out_written)
232{
233 const ra8_io_stream_posix_state_t* state = (const ra8_io_stream_posix_state_t*)context;
235 bytes,
236 length,
237 out_written,
239 nullptr);
240}
241
244 .write = internal_posix_write,
245 .flush = nullptr,
246};
247
250{
251 if ((stream == nullptr) || (state == nullptr)) {
252 return k_ra8_err_null_ptr;
253 }
254 if (fd < 0) {
256 }
257 ra8_io_stream_posix_state_t candidate = {.fd = fd};
258 const ra8_err_t error = ra8_io_stream_bind(stream, &s_posix_stream_iface, state);
259 if (error != k_ra8_ok) {
260 return error;
261 }
262 *state = candidate;
263 return k_ra8_ok;
264}
-proof
Annotation-attribute framework macros for ra8-firmware.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
#define RA8_LOOP_BOUND(ceiling)
NASA Power-of-10 Rule 2: bind ONE loop to a compile-time ceiling.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
Error Code Definitions for ra8-firmware.
@ k_ra8_err_retry_limit
Retry budget exhausted – operation still failing after all attempts.
Definition ra8_err.h:447
@ k_ra8_err_no_mem
Static buffer exhausted (no dynamic memory on this project).
Definition ra8_err.h:142
@ k_ra8_err_would_block
Non-blocking operation would have blocked.
Definition ra8_err.h:210
@ 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_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ 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_access_denied
Operation refused because the target is protected against it.
Definition ra8_err.h:276
@ k_ra8_err_protocol_error
Protocol-level error (e.g.
Definition ra8_err.h:429
@ k_ra8_err_comm_error
Generic communication error (use a more specific code when possible).
Definition ra8_err.h:399
@ 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
static ra8_err_t internal_native_write(void *file_ctx, const void *buf, uint32_t bytes)
Write a native ra8_fs stream.
struct ra8_io_stream_iface ra8_io_stream_iface_t
Public implementer contract for ra8_io byte-stream backends.
ra8_err_t ra8_io_stream_bind(ra8_io_stream_t *stream, const ra8_io_stream_iface_t *iface, void *context)
Bind a backend vtable and caller-owned state into a stream handle.
static int64_t internal_native_write(void *context, int fd, const uint8_t *bytes, uint32_t length, int *out_errno)
Invoke the native POSIX descriptor-write primitive once.
ra8_err_t priv_ra8_io_stream_posix_write_loop(int fd, const uint8_t *bytes, uint32_t length, uint32_t *out_written, ra8_io_stream_posix_write_fn_t writer, void *writer_context)
Complete one descriptor write through an injected raw primitive.
static ra8_err_t internal_write_loop_step(int fd, const uint8_t *bytes, uint32_t length, uint32_t *done, uint32_t *interrupts, ra8_io_stream_posix_write_fn_t writer, void *writer_context)
Perform one write attempt within the bounded retry loop.
ra8_io_stream_posix_const_t
POSIX descriptor-write retry limits.
@ k_posix_write_interrupt_limit
Maximum interrupted attempts.
ra8_err_t ra8_io_stream_posix_init(ra8_io_stream_t *stream, ra8_io_stream_posix_state_t *state, int fd)
Bind a borrowed writable descriptor as a byte-stream sink.
static const ra8_io_stream_iface_t s_posix_stream_iface
Immutable borrowed-descriptor stream backend.
static ra8_err_t internal_map_errno(int error_number)
Map one descriptor-write errno value to the canonical vocabulary.
static ra8_err_t internal_posix_write(void *context, const uint8_t *bytes, uint32_t length, uint32_t *out_written)
Stream-backend callback for one borrowed POSIX descriptor.
Borrowed raw-descriptor backend for the portable byte-stream facade.
Private exact-write engine for the POSIX stream adapter.
int64_t(* ra8_io_stream_posix_write_fn_t)(void *context, int fd, const uint8_t *bytes, uint32_t length, int *out_errno)
Injected descriptor-write primitive used by the exact-write loop.
Caller-owned state retained by one POSIX stream binding.
int fd
Borrowed writable descriptor; never closed by the adapter.
Caller-allocated byte-stream handle binding a sink to its context.