ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
c6_cam_net.c File Reference

NetX DHCP and a single-connection HTTP camera server over ESP32-C6. More...

#include <stdint.h>
#include <string.h>
#include "c6_camera_server.h"
#include "nx_api.h"
#include "nx_ether_driver_c6.h"
#include "nxd_dhcp_client.h"
#include "ra8_attributes.h"
#include "ra8_c6link.h"
#include "ra8_err.h"
#include "tx_api.h"
Include dependency graph for c6_cam_net.c:

Go to the source code of this file.

Enumerations

enum  c6_cam_net_format_t : uint32_t {
  k_c6_cam_net_decimal_digits = 10U ,
  k_c6_cam_http_get_min = 5U
}
 Decimal formatting and minimum HTTP request sizes. More...

Functions

static UINT internal_c6_cam_net_create (void)
 Create the static NetX packet pool and IP instance.
ra8_err_t c6_cam_net_up (ra8_c6link_t *link, const ra8_c6link_mac_t *mac, c6_cam_lease_t *out)
 Bind NetX to an associated raw C6 link and obtain a DHCP lease.
static UINT internal_c6_cam_http_send (const void *data, uint32_t bytes)
 Send an exact byte span over the accepted HTTP socket.
static uint32_t internal_c6_cam_append (char *out, uint32_t at, uint32_t cap, const char *text)
 Append bounded text to a fixed HTTP header buffer.
static uint32_t internal_c6_cam_append_u32 (char *out, uint32_t at, uint32_t cap, uint32_t value)
 Append an unsigned decimal value to a fixed buffer.
static UINT internal_c6_cam_http_response (const char *type, const void *body, uint32_t body_bytes, const c6_cam_frame_timing_t *timing, uint32_t timestamp_ms)
 Send one complete close-delimited HTTP response.
static UINT internal_c6_cam_http_stream (void)
 Serve a bounded multipart MJPEG sequence.
static bool internal_c6_cam_request_is (const char *request, uint32_t bytes, const char *path)
 Match a bounded HTTP GET request path.
static void internal_c6_cam_http_frame (void)
 Capture and serve one JPEG still response.
static void internal_c6_cam_http_audio (void)
 Snapshot and serve the current audio window.
static void internal_c6_cam_http_dispatch (const char *request, uint32_t bytes)
 Dispatch a bounded HTTP request to one route handler.
static void internal_c6_cam_http_handle (void)
 Receive and dispatch one HTTP request.
void c6_cam_http_serve (void)
 Serve the browser UI and fresh JPEG frames forever.

Variables

static NX_PACKET_POOL s_pool
static uint8_t s_pool_mem [k_c6_cam_net_pool_bytes]
static NX_IP s_ip
static uint8_t s_ip_stack [k_c6_cam_net_ip_stack]
static uint8_t s_arp_cache [k_c6_cam_net_arp_bytes]
static NX_DHCP s_dhcp
static NX_TCP_SOCKET s_http_socket
static CHAR s_pool_name [] = "c6_cam_pool"
static CHAR s_ip_name [] = "c6_cam_ip"
static CHAR s_dhcp_name [] = "c6_cam_dhcp"
static CHAR s_socket_name [] = "c6_cam_http"
static const char s_page []

Detailed Description

NetX DHCP and a single-connection HTTP camera server over ESP32-C6.

Tag
[Ring 6 / APP] {World: S}

The C6 remains the validated esp-hosted L2 co-processor. NetX Duo, DHCP, TCP, HTTP parsing, camera capture and JPEG generation all run on RA8. The page uses one persistent multipart/MJPEG connection. /frame.jpg remains available for still capture, diagnostics, and HIL qualification.

Since
0.1.0

Definition in file c6_cam_net.c.

Enumeration Type Documentation

◆ c6_cam_net_format_t

enum c6_cam_net_format_t : uint32_t

Decimal formatting and minimum HTTP request sizes.

Enumerator
k_c6_cam_net_decimal_digits 

Decimal digits in a 32-bit integer.

k_c6_cam_http_get_min 

Minimum bytes in an HTTP GET prefix.

Definition at line 221 of file c6_cam_net.c.

Function Documentation

◆ c6_cam_http_serve()

void c6_cam_http_serve ( void )

Serve the browser UI and fresh JPEG frames forever.

Definition at line 550 of file c6_cam_net.c.

References c6_cam_puts(), internal_c6_cam_http_handle(), k_c6_cam_http_port, k_c6_cam_listen_backlog, k_c6_cam_tcp_window, s_http_socket, s_ip, s_socket_name, and tx_thread_sleep.

Referenced by internal_c6_cam_worker_entry().

◆ c6_cam_net_up()

◆ internal_c6_cam_append()

uint32_t internal_c6_cam_append ( char * out,
uint32_t at,
uint32_t cap,
const char * text )
static

Append bounded text to a fixed HTTP header buffer.

Copies until NUL or capacity and returns the next write position.

Parameters
[out]outDestination character buffer.
[in]atInitial write offset.
[in]capDestination capacity.
[in]textNUL-terminated source text.
Returns
Updated write offset.
Return values
uint32_tFirst unwritten offset, never greater than cap.
Precondition
out addresses cap writable bytes.
text is nonnull and NUL-terminated.
Postcondition
Source text is unchanged.
Only destination bytes in [at, return) are modified.
Note
Truncation is represented by a return value equal to cap.
Since
0.1.0

Definition at line 212 of file c6_cam_net.c.

Referenced by internal_c6_cam_http_response(), and internal_c6_cam_http_stream().

◆ internal_c6_cam_append_u32()

uint32_t internal_c6_cam_append_u32 ( char * out,
uint32_t at,
uint32_t cap,
uint32_t value )
static

Append an unsigned decimal value to a fixed buffer.

Formats digits in reverse stack storage and copies them in display order.

Parameters
[out]outDestination character buffer.
[in]atInitial write offset.
[in]capDestination capacity.
[in]valueValue to append.
Returns
Updated write offset.
Return values
uint32_tFirst unwritten offset, never greater than cap.
Precondition
out addresses cap writable bytes.
at is no greater than cap.
Postcondition
The value's digits are appended until capacity is exhausted.
No destination byte before at is modified.
Note
No NUL terminator is appended.
Since
0.1.0

Definition at line 243 of file c6_cam_net.c.

References k_c6_cam_net_decimal_digits.

Referenced by internal_c6_cam_http_response(), and internal_c6_cam_http_stream().

◆ internal_c6_cam_http_audio()

void internal_c6_cam_http_audio ( void )
static

Snapshot and serve the current audio window.

Requests a stable WAV snapshot from the ping-pong audio backend and returns either that snapshot or a bounded not-ready response to the client.

Precondition
Audio state and HTTP packet resources are initialized.
The connected socket may accept one complete response attempt.
Postcondition
A WAV or bounded text not-ready response is attempted.
Successful snapshots retain their capture timestamp in the response.
Note
Successful responses include the capture timestamp.
Since
0.1.0

Definition at line 462 of file c6_cam_net.c.

References c6_cam_audio_snapshot_wav(), c6_cam_put_u32(), c6_cam_puts(), internal_c6_cam_http_response(), k_ra8_ok, and RA8_INTERNAL.

Referenced by internal_c6_cam_http_dispatch().

◆ internal_c6_cam_http_dispatch()

void internal_c6_cam_http_dispatch ( const char * request,
uint32_t bytes )
static

Dispatch a bounded HTTP request to one route handler.

Matches the request path against the stream, still-frame, audio, and health endpoints, falling back to the embedded browser page.

Parameters
[in]requestExtracted request prefix.
[in]bytesExtracted request-byte count.
Precondition
request addresses at least bytes readable bytes.
Camera, audio, and HTTP response resources are initialized.
Postcondition
At most one response handler is invoked.
Unrecognized routes receive the embedded browser page.
Note
Handler response failures are resolved by the caller's socket teardown.
Since
0.1.0

Definition at line 498 of file c6_cam_net.c.

References internal_c6_cam_http_audio(), internal_c6_cam_http_frame(), internal_c6_cam_http_response(), internal_c6_cam_http_stream(), internal_c6_cam_request_is(), RA8_INTERNAL, and s_page.

Referenced by internal_c6_cam_http_handle().

◆ internal_c6_cam_http_frame()

void internal_c6_cam_http_frame ( void )
static

Capture and serve one JPEG still response.

Captures through the configured camera backend, emits the JPEG with timing metadata on success, and otherwise sends a bounded diagnostic body.

Precondition
Camera state and HTTP packet resources are initialized.
The connected socket may accept one complete response attempt.
Postcondition
A JPEG or bounded text error response is attempted.
Capture timing or the backend error is reported on the console.
Note
Capture diagnostics are emitted on the console.
Since
0.1.0

Definition at line 419 of file c6_cam_net.c.

References c6_cam_camera_capture_jpeg(), c6_cam_camera_report_last_error(), c6_cam_put_u32(), c6_cam_puts(), c6_cam_frame_timing_t::capture_ms, c6_cam_frame_timing_t::convert_ms, c6_cam_frame_timing_t::encode_ms, internal_c6_cam_http_response(), k_ra8_ok, ra8_err_to_str(), RA8_INTERNAL, and c6_cam_frame_timing_t::timestamp_ms.

Referenced by internal_c6_cam_http_dispatch().

◆ internal_c6_cam_http_handle()

void internal_c6_cam_http_handle ( void )
static

Receive and dispatch one HTTP request.

Extracts a bounded request prefix and serves stream, frame, audio, health, or UI content.

Precondition
s_http_socket is connected to one client.
Camera, audio, packet pool, and IP state are initialized.
Postcondition
Any received request packet is released exactly once.
At most one route handler is selected for the request.
Note
Response failures are handled by subsequent socket teardown.
Since
0.1.0

Definition at line 532 of file c6_cam_net.c.

References internal_c6_cam_http_dispatch(), k_c6_cam_request_max, RA8_INTERNAL, and s_http_socket.

Referenced by c6_cam_http_serve().

◆ internal_c6_cam_http_response()

UINT internal_c6_cam_http_response ( const char * type,
const void * body,
uint32_t body_bytes,
const c6_cam_frame_timing_t * timing,
uint32_t timestamp_ms )
static

Send one complete close-delimited HTTP response.

Builds bounded headers with optional timing and timestamp metadata, then sends the body.

Parameters
[in]typeNUL-terminated MIME content type.
[in]bodyReadable response body.
[in]body_bytesResponse body size.
[in]timingOptional camera timing metadata.
[in]timestamp_msOptional monotonic media timestamp.
Returns
NetX status code.
Return values
NX_SUCCESSHeader and body were sent.
NX_NOT_SUCCESSFULA socket send failed.
Precondition
type is NUL-terminated and body spans body_bytes bytes.
The HTTP socket is connected.
Postcondition
The body is sent only after a successful header send.
Caller-owned response data remains unchanged.
Note
A zero timestamp suppresses the timestamp header.
Since
0.1.0

Definition at line 275 of file c6_cam_net.c.

References c6_cam_frame_timing_t::capture_ms, c6_cam_frame_timing_t::convert_ms, c6_cam_frame_timing_t::encode_ms, internal_c6_cam_append(), internal_c6_cam_append_u32(), internal_c6_cam_http_send(), and RA8_INTERNAL.

Referenced by internal_c6_cam_http_audio(), internal_c6_cam_http_dispatch(), and internal_c6_cam_http_frame().

◆ internal_c6_cam_http_send()

UINT internal_c6_cam_http_send ( const void * data,
uint32_t bytes )
static

Send an exact byte span over the accepted HTTP socket.

Chunks caller data into bounded NetX packets and sends them in order.

Parameters
[in]dataReadable body or header bytes.
[in]bytesTotal bytes to transmit.
Returns
NetX status code.
Return values
NX_SUCCESSThe entire span was queued.
NX_NOT_SUCCESSFULPacket allocation, append, or socket send failed.
Precondition
data addresses at least bytes readable bytes.
s_http_socket is connected and s_pool is initialized.
Postcondition
On success, exactly bytes were sent in order.
Failed unsent packets are released before return.
Note
Exact non-success codes are propagated from NetX.
Since
0.1.0

Definition at line 163 of file c6_cam_net.c.

References k_c6_cam_http_chunk, RA8_INTERNAL, s_http_socket, and s_pool.

Referenced by internal_c6_cam_http_response(), and internal_c6_cam_http_stream().

◆ internal_c6_cam_http_stream()

UINT internal_c6_cam_http_stream ( void )
static

Serve a bounded multipart MJPEG sequence.

Captures a fresh JPEG for each part and emits per-frame timing diagnostics.

Returns
NetX status code.
Return values
NX_SUCCESSThe configured frame sequence completed.
NX_NOT_SUCCESSFULCapture or socket transmission failed.
Precondition
The HTTP socket is connected.
The selected camera backend is initialized.
Postcondition
Every successful frame is emitted as one complete multipart section.
Capture failures emit backend diagnostics before return.
Note
Clients reconnect after the bounded sequence or any failure.
Since
0.1.0

Definition at line 325 of file c6_cam_net.c.

References c6_cam_camera_capture_jpeg(), c6_cam_camera_report_last_error(), c6_cam_put_u32(), c6_cam_puts(), c6_cam_frame_timing_t::capture_ms, c6_cam_frame_timing_t::convert_ms, c6_cam_frame_timing_t::encode_ms, internal_c6_cam_append(), internal_c6_cam_append_u32(), internal_c6_cam_http_send(), k_c6_cam_stream_frames, k_ra8_ok, and RA8_INTERNAL.

Referenced by internal_c6_cam_http_dispatch().

◆ internal_c6_cam_net_create()

UINT internal_c6_cam_net_create ( void )
static

Create the static NetX packet pool and IP instance.

Enables ARP, UDP, TCP, and ICMP over the already-bound C6 Ethernet driver.

Returns
NetX status code.
Return values
NX_SUCCESSAll required NetX objects and protocols are ready.
NX_NOT_SUCCESSFULA NetX creation or enable operation failed.
Precondition
nx_system_initialize has completed.
Static pool, stack, and ARP storage are not owned by another NetX instance.
Postcondition
On success, s_pool and s_ip are initialized.
On failure, no later protocol-enable step is attempted.
Note
Exact non-success codes are propagated from NetX.
Since
0.1.0

Definition at line 70 of file c6_cam_net.c.

References k_c6_cam_net_ip_prio, k_c6_cam_net_pkt_payload, nx_ether_driver_c6(), RA8_INTERNAL, s_arp_cache, s_ip, s_ip_name, s_ip_stack, s_pool, s_pool_mem, and s_pool_name.

Referenced by c6_cam_net_up().

◆ internal_c6_cam_request_is()

bool internal_c6_cam_request_is ( const char * request,
uint32_t bytes,
const char * path )
static

Match a bounded HTTP GET request path.

Compares the request target exactly while accepting a query suffix.

Parameters
[in]requestRaw request bytes.
[in]bytesAvailable request-byte count.
[in]pathNUL-terminated path to match.
Returns
Match result.
Return values
trueThe request is a GET for path with optional query text.
falseThe method, length, or path does not match.
Precondition
request addresses at least bytes readable bytes.
path is nonnull and NUL-terminated.
Postcondition
Request and path storage remain unchanged.
No bytes beyond the supplied request bound are read.
Note
HTTP decoding beyond this narrow routing grammar is intentionally absent.
Since
0.1.0

Definition at line 391 of file c6_cam_net.c.

References k_c6_cam_http_get_min, and memcmp().

Referenced by internal_c6_cam_http_dispatch().

Variable Documentation

◆ s_arp_cache

uint8_t s_arp_cache[k_c6_cam_net_arp_bytes]
static

Definition at line 34 of file c6_cam_net.c.

◆ s_dhcp

NX_DHCP s_dhcp
static

Definition at line 35 of file c6_cam_net.c.

Referenced by c6_cam_net_up(), priv_net_dhcp(), and priv_net_dhcp().

◆ s_dhcp_name

CHAR s_dhcp_name[] = "c6_cam_dhcp"
static

Definition at line 39 of file c6_cam_net.c.

Referenced by c6_cam_net_up(), priv_net_dhcp(), and priv_net_dhcp().

◆ s_http_socket

NX_TCP_SOCKET s_http_socket
static

◆ s_ip

NX_IP s_ip
static

Definition at line 32 of file c6_cam_net.c.

◆ s_ip_name

◆ s_ip_stack

uint8_t s_ip_stack[k_c6_cam_net_ip_stack]
static

Definition at line 33 of file c6_cam_net.c.

◆ s_page

const char s_page[]
static
Initial value:
=
"<!doctype html><html><head><meta charset=utf-8>"
"<meta name=viewport content='width=device-width,initial-scale=1'>"
"<title>EK-RA8D2 Camera</title><style>body{margin:0;background:#111;color:#eee;"
"font:16px system-ui;display:grid;place-items:center;min-height:100vh}main{text-align:center}"
"img{width:min(96vw,960px);height:auto;image-rendering:auto;border:1px solid #555}"
"small{display:block;margin-top:.5rem;color:#aaa}audio{margin-top:1rem}</style></head><body><main>"
"<h1>EK-RA8D2 / OV5640</h1><img id=stream src=/stream.mjpg alt='live camera'>"
"<small>bounded multipart MJPEG stream with automatic reconnect</small>"
"<audio controls src=/audio.wav></audio>"
"<small>latest rolling one-second onboard MIC1 recording</small>"
"<script>const s=document.getElementById('stream');s.onerror=()=>{"
"setTimeout(()=>{s.src='/stream.mjpg?t='+Date.now()},1000)}</script>"
"</main></body></html>"

Definition at line 42 of file c6_cam_net.c.

Referenced by cm_draw_page(), cm_draw_progress_bar(), cm_draw_status_bar(), cm_handle_tap(), and internal_c6_cam_http_dispatch().

◆ s_pool

NX_PACKET_POOL s_pool
static

Definition at line 30 of file c6_cam_net.c.

◆ s_pool_mem

uint8_t s_pool_mem[k_c6_cam_net_pool_bytes]
static

Definition at line 31 of file c6_cam_net.c.

Referenced by internal_c6_cam_net_create(), priv_net_create_ip(), and priv_net_create_ip().

◆ s_pool_name

CHAR s_pool_name[] = "c6_cam_pool"
static

Definition at line 37 of file c6_cam_net.c.

Referenced by internal_c6_cam_net_create(), priv_net_create_ip(), and priv_net_create_ip().

◆ s_socket_name

CHAR s_socket_name[] = "c6_cam_http"
static