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

ThreadX + NetX Duo + Mbed TLS HTTPS client demo for EK-RA8D2. More...

#include <stdint.h>
#include <string.h>
#include "ra8_attributes.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_boot_entry.h"
#include "ra8_cgc.h"
#include "ra8_err.h"
#include "ra8_eth.h"
#include "ra8_isr.h"
#include "ra8_rsip.h"
#include "ra8_time.h"
#include "mbedtls/net_sockets.h"
#include "mbedtls/platform.h"
#include "mbedtls/private/ctr_drbg.h"
#include "mbedtls/private/entropy.h"
#include "mbedtls/private/sha256.h"
#include "mbedtls/ssl.h"
#include "mbedtls/x509_crt.h"
#include "nx_api.h"
#include "nx_ether_driver_ra8_eth.h"
#include "psa/crypto.h"
#include "tx_api.h"
Include dependency graph for main.c:

Go to the source code of this file.

Enumerations

enum  demo_config_t : uint32_t {
  k_demo_baud = 115200U ,
  k_demo_thread_stack = 16384U ,
  k_demo_ip_thread_pri = 1U ,
  k_demo_app_thread_pri = 8U ,
  k_demo_packet_size = 1568U ,
  k_demo_packet_count = 16U ,
  k_demo_pool_bytes = 32768U ,
  k_demo_byte_pool_size = 65536U ,
  k_demo_ip_stack = 4096U ,
  k_demo_arp_cache = 1024U ,
  k_demo_https_port = 443U ,
  k_demo_recv_window = 4096U ,
  k_demo_socket_ttl = 64U ,
  k_demo_recv_timeout = 200U ,
  k_demo_handshake_max = 3000U ,
  k_demo_log_buf_bytes = 80U ,
  k_demo_dump_bytes = 1024U ,
  k_demo_request_buf = 256U ,
  k_demo_response_buf = 4096U ,
  k_demo_drbg_seed_len = 32U
}
 Compile-time settings for the HTTPS client demo. More...
enum  demo_ip_octet_t : uint8_t {
  k_demo_ip_oct_a = 0U ,
  k_demo_ip_oct_b = 1U ,
  k_demo_ip_oct_c = 2U ,
  k_demo_ip_oct_d = 3U
}
 Octet indices into a packed IPv4 address. More...
enum  demo_ip_shift_t : uint8_t {
  k_demo_ip_shift_a = 24U ,
  k_demo_ip_shift_b = 16U ,
  k_demo_ip_shift_c = 8U ,
  k_demo_ip_shift_d = 0U
}
 Bit shifts that pack a 4-octet IPv4 into NetX's ULONG. More...
enum  demo_mac_word_shift_t : uint8_t {
  k_demo_mac_msw_shift_b0 = 8U ,
  k_demo_mac_msw_shift_b1 = 0U ,
  k_demo_mac_lsw_shift_b2 = 24U ,
  k_demo_mac_lsw_shift_b3 = 16U ,
  k_demo_mac_lsw_shift_b4 = 8U ,
  k_demo_mac_lsw_shift_b5 = 0U
}
 Shifts that pack the 6-byte MAC into msw/lsw ULONG pairs. More...
enum  demo_ipv4_t : uint16_t {
  k_demo_ipaddr_0 = 192U ,
  k_demo_ipaddr_1 = 168U ,
  k_demo_ipaddr_2 = 1U ,
  k_demo_ipaddr_3 = 42U ,
  k_demo_netmask_b = 255U ,
  k_demo_hostip_0 = 93U ,
  k_demo_hostip_1 = 184U ,
  k_demo_hostip_2 = 216U ,
  k_demo_hostip_3 = 34U
}
 Demo IPv4 addresses (board, gateway, host) as octet enums. More...
enum  http_ascii_t : uint8_t {
  k_ascii_cr = 0x0DU ,
  k_ascii_lf = 0x0AU
}
 ASCII control bytes used by the HTTP line scanner. More...
enum  demo_mac_idx_t : uint8_t {
  k_demo_mac_idx_0 = 0U ,
  k_demo_mac_idx_1 = 1U ,
  k_demo_mac_idx_2 = 2U ,
  k_demo_mac_idx_3 = 3U ,
  k_demo_mac_idx_4 = 4U ,
  k_demo_mac_idx_5 = 5U
}
 MAC-address byte indices. More...

Functions

static void internal_demo_panic_halt (void)
 Halt forever in WFI.
static void internal_demo_setup_or_halt (void)
 Bring CGC + the J-Link OB VCOM console + on-board Ethernet + RSIP up.
static void internal_demo_print (const char *s)
 Convenience wrapper to write a NUL-terminated string to SCI8.
static void internal_demo_write_bytes (const uint8_t *buf, uint32_t len)
 Write an arbitrary byte buffer to SCI8 (no NUL required).
static ULONG internal_demo_pack_ip (const uint8_t *octets)
 Pack a 4-octet IPv4 array into NetX's host-order ULONG.
static void internal_demo_pack_mac (ULONG *msw, ULONG *lsw)
 Pack the local MAC into the msw/lsw fields NetX expects.
static UINT internal_demo_netx_bring_up (void)
 Bring NetX Duo up: pool, IP, ARP, TCP, ICMP.
static UINT internal_demo_tcp_connect (void)
 Open a NetX Duo TCP socket and connect to www.example.com:443.
static int internal_demo_bio_send (void *ctx, const unsigned char *buf, size_t len)
 Mbed TLS BIO send callback bound to nx_tcp_socket_send.
static int internal_demo_bio_recv (void *ctx, unsigned char *buf, size_t len)
 Mbed TLS BIO recv callback bound to nx_tcp_socket_receive.
static int internal_demo_entropy_source (void *ctx, unsigned char *buf, size_t len, size_t *olen)
 Pull entropy from the RSIP TRNG into Mbed TLS.
static int internal_demo_verify_cert_pin (void)
 Verify the peer's leaf certificate matches our compile-time pin.
static int internal_demo_http_get (void)
 Issue the HTTP GET and dump the first k_demo_dump_bytes to SCI8.
static int internal_demo_tls_session (void)
 Run the TLS bring-up: DRBG, config, configured pin policy, GET, dump.
static void internal_demo_thread_entry (ULONG thread_input)
 ThreadX worker entry: bring NetX up, run TLS session, dump body.
static void * internal_demo_calloc (size_t n, size_t size)
 mbedtls_calloc hook backed by the ThreadX byte pool.
static void internal_demo_free (void *p)
 mbedtls_free hook backed by the ThreadX byte pool.
void tx_application_define (void *first_unused_memory)
 ThreadX system-define hook: build worker thread + byte pool.
void main (void)
 Application entry.

Variables

static const uint8_t s_demo_mac [6] = {0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U}
static const uint8_t s_demo_ip [4]
 IPv4 address: 192.168.1.42 / 255.255.255.0.
static const uint8_t s_demo_mask [4] = {k_demo_netmask_b, k_demo_netmask_b, k_demo_netmask_b, 0U}
 Subnet mask: 255.255.255.0.
static const uint8_t s_demo_gw [4] = {k_demo_ipaddr_0, k_demo_ipaddr_1, k_demo_ipaddr_2, 1U}
 Default gateway: 192.168.1.1.
static const uint8_t s_demo_host_ip [4]
 Static IPv4 for www.example.com (legacy edge-of-net IP).
static const char s_demo_host_name [] = "www.example.com"
 HTTP/1.1 Host header literal – matches the IP pin above.
static const uint8_t s_demo_cert_pin_sha256 [32]
 SHA-256 of the expected leaf certificate DER, compile-time pinned.
static NX_PACKET_POOL s_packet_pool
static NX_IP s_ip
static NX_TCP_SOCKET s_tls_socket
static UCHAR s_pool_memory [k_demo_pool_bytes]
static ULONG s_ip_stack [k_demo_ip_stack/sizeof(ULONG)]
static ULONG s_arp_cache [k_demo_arp_cache/sizeof(ULONG)]
static TX_THREAD s_demo_thread
static UCHAR s_demo_stack [k_demo_thread_stack]
static TX_BYTE_POOL s_byte_pool
static UCHAR s_byte_pool_memory [k_demo_byte_pool_size]
static mbedtls_ssl_context s_ssl
static mbedtls_ssl_config s_ssl_cfg
static mbedtls_ctr_drbg_context s_drbg
static mbedtls_entropy_context s_entropy
static UCHAR s_request_buf [k_demo_request_buf]
static UCHAR s_response_buf [k_demo_response_buf]
static UCHAR s_tls_send_staging [k_demo_pool_bytes]

Detailed Description

ThreadX + NetX Duo + Mbed TLS HTTPS client demo for EK-RA8D2.

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

Brings the chip up the same way threadx_netx_tcp_echo does (CGC -> SCI8 @ 115200 8N1, RMII pins routed) and then hands control to ThreadX. The single worker thread:

  1. nx_system_initialize() + packet pool / IP / ARP / TCP / ICMP enable on the static address 192.168.1.42 / 24.
  2. Initialises ra8_rsip for its TRNG entropy source. The RSIP-E50D AES / SHA-256 engines are not functional on this silicon, so all handshake crypto runs in Mbed TLS software.
  3. Seeds Mbed TLS's CTR_DRBG from ra8_rsip_trng_read.
  4. Opens a NetX TCP socket to www.example.com:443 (static IP 93.184.216.34 so the demo runs without DNS).
  5. Wires Mbed TLS's BIO callbacks to nx_tcp_socket_send / nx_tcp_socket_receive – both block on a ThreadX timer.
  6. Runs the TLS handshake. On success, computes SHA-256 of the peer's leaf certificate DER and compares against the compile-time pin s_demo_cert_pin_sha256. A mismatch is a fatal error – we will not send the HTTP request.
  7. Sends GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n.
  8. Reads the response, prefixes [https] to every line, and dumps the first k_demo_dump_bytes of the body to SCI8. The demo is deliberately single-shot: after the response is dumped (or any step fails), the worker thread parks. Re-flashing is the easiest way to re-run the demo.
    Test recipe (lab):
  1. Plug an Ethernet cable from EK-RA8D2 J64 to the workstation. Bridge / NAT the workstation interface to the public Internet so the board can reach 93.184.216.34:443 – a typical setup is sudo ip link set ... main br0 on Linux or sharing the Wi-Fi adapter on macOS.
  2. Open a serial terminal on the J-Link OB CDC port at 115200 8N1, e.g. picocom -b 115200 /dev/cu.usbmodem0001234567891.
  3. just apps::hardware::flash threadx_https_client.
  4. Reset the board. Within ~3 seconds you should see: [https] handshake OK, cert pin matches, dumping body followed by the first 1 KiB of www.example.com.
    Author
    Brighton Sikarskie
    Date
    2026-04-29
    Since
    0.1.0

Definition in file main.c.

Enumeration Type Documentation

◆ demo_config_t

enum demo_config_t : uint32_t

Compile-time settings for the HTTPS client demo.

Mirrors the threadx_netx_tcp_echo sizing for NetX Duo; the additional Mbed TLS state is sized inside the per-thread stack and the byte pool below.

Enumerator
k_demo_baud 

Demo baud.

k_demo_thread_stack 

Demo thread stack.

k_demo_ip_thread_pri 

Demo IP thread priority.

k_demo_app_thread_pri 

Demo app thread priority.

k_demo_packet_size 

Demo packet size.

k_demo_packet_count 

Demo packet count.

k_demo_pool_bytes 

Demo pool bytes.

k_demo_byte_pool_size 

Demo byte pool size.

k_demo_ip_stack 

Demo IP stack.

k_demo_arp_cache 

Demo arp cache.

k_demo_https_port 

Demo HTTPS port.

k_demo_recv_window 

Demo recv window.

k_demo_socket_ttl 

Demo socket ttl.

k_demo_recv_timeout 

Ticks; ~2 s at TX_TIMER_TICKS_PER_SECOND=100.

k_demo_handshake_max 

Max ticks waiting for handshake.

k_demo_log_buf_bytes 

Demo log buffer bytes.

k_demo_dump_bytes 

First 1 KiB of body to SCI8.

k_demo_request_buf 

Demo request buffer.

k_demo_response_buf 

Demo response buffer.

k_demo_drbg_seed_len 

Bytes pulled from RSIP TRNG.

Definition at line 87 of file main.c.

◆ demo_ip_octet_t

enum demo_ip_octet_t : uint8_t

Octet indices into a packed IPv4 address.

Enumerator
k_demo_ip_oct_a 

Demo IP oct a.

k_demo_ip_oct_b 

Demo IP oct b.

k_demo_ip_oct_c 

Demo IP oct c.

k_demo_ip_oct_d 

Demo IP oct d.

Definition at line 114 of file main.c.

◆ demo_ip_shift_t

enum demo_ip_shift_t : uint8_t

Bit shifts that pack a 4-octet IPv4 into NetX's ULONG.

Enumerator
k_demo_ip_shift_a 

Demo IP shift a.

k_demo_ip_shift_b 

Demo IP shift b.

k_demo_ip_shift_c 

Demo IP shift c.

k_demo_ip_shift_d 

Demo IP shift d.

Definition at line 125 of file main.c.

◆ demo_ipv4_t

enum demo_ipv4_t : uint16_t

Demo IPv4 addresses (board, gateway, host) as octet enums.

Enumerator
k_demo_ipaddr_0 

192.168.1.42 board.

k_demo_ipaddr_1 

Demo ipaddr 1.

k_demo_ipaddr_2 

Demo ipaddr 2.

k_demo_ipaddr_3 

Demo ipaddr 3.

k_demo_netmask_b 

255.255.255.0 (first three octets).

k_demo_hostip_0 

93.184.216.34 (www.example.com).

k_demo_hostip_1 

Demo hostip 1.

k_demo_hostip_2 

Demo hostip 2.

k_demo_hostip_3 

Demo hostip 3.

Definition at line 146 of file main.c.

◆ demo_mac_idx_t

enum demo_mac_idx_t : uint8_t

MAC-address byte indices.

Enumerator
k_demo_mac_idx_0 

Demo MAC index 0.

k_demo_mac_idx_1 

Demo MAC index 1.

k_demo_mac_idx_2 

Demo MAC index 2.

k_demo_mac_idx_3 

Demo MAC index 3.

k_demo_mac_idx_4 

Demo MAC index 4.

k_demo_mac_idx_5 

Demo MAC index 5.

Definition at line 165 of file main.c.

◆ demo_mac_word_shift_t

enum demo_mac_word_shift_t : uint8_t

Shifts that pack the 6-byte MAC into msw/lsw ULONG pairs.

Enumerator
k_demo_mac_msw_shift_b0 

Demo MAC msw shift b0.

k_demo_mac_msw_shift_b1 

Demo MAC msw shift b1.

k_demo_mac_lsw_shift_b2 

Demo MAC lsw shift b2.

k_demo_mac_lsw_shift_b3 

Demo MAC lsw shift b3.

k_demo_mac_lsw_shift_b4 

Demo MAC lsw shift b4.

k_demo_mac_lsw_shift_b5 

Demo MAC lsw shift b5.

Definition at line 136 of file main.c.

◆ http_ascii_t

enum http_ascii_t : uint8_t

ASCII control bytes used by the HTTP line scanner.

Enumerator
k_ascii_cr 

Carriage return.

k_ascii_lf 

Line feed.

Definition at line 159 of file main.c.

Function Documentation

◆ internal_demo_bio_recv()

int internal_demo_bio_recv ( void * ctx,
unsigned char * buf,
size_t len )
static

Mbed TLS BIO recv callback bound to nx_tcp_socket_receive.

Receives one NetX packet, copies a bounded payload into the TLS buffer, releases the packet, and translates NetX wait states to Mbed TLS.

Parameters
[in]ctxOpaque context (unused).
[out]bufOutput buffer.
[in]lenMaximum bytes to read.
Returns
Bytes copied, MBEDTLS_ERR_SSL_WANT_READ if the socket has nothing yet, or a negative MBEDTLS_ERR_* on error.
Return values
0Empty output capacity performs no socket operation.
MBEDTLS_ERR_SSL_WANT_READNo packet arrived before the timeout.
Precondition
Socket is connected.
buf references len writable bytes when len is nonzero.
Postcondition
Any received NetX packet is released before return.
At most len bytes are reported to Mbed TLS.
Note
The opaque callback context is unused because the demo has one socket.
Since
0.1.0

Definition at line 573 of file main.c.

References k_demo_recv_timeout, RA8_INTERNAL, and s_tls_socket.

Referenced by internal_demo_tls_session().

◆ internal_demo_bio_send()

int internal_demo_bio_send ( void * ctx,
const unsigned char * buf,
size_t len )
static

Mbed TLS BIO send callback bound to nx_tcp_socket_send.

Allocates one NetX packet, appends the requested TLS bytes, and transfers packet ownership to the connected socket on success.

Parameters
[in]ctxOpaque context (unused – we use the file-scope socket).
[in]bufBytes Mbed TLS wants to send.
[in]lenBuffer length.
Returns
Bytes written, or a negative MBEDTLS_ERR_* on failure.
Return values
0Empty input requires no socket operation.
MBEDTLS_ERR_SSL_WANT_WRITENetX temporarily cannot accept a packet.
Precondition
Socket is connected.
buf references len readable bytes when len is nonzero.
Postcondition
Successful sends return len and transfer the packet to NetX.
Failed sends release any packet still owned by this callback.
Note
The opaque callback context is unused because the demo has one socket.
Since
0.1.0

Definition at line 524 of file main.c.

References k_demo_recv_timeout, memcpy(), RA8_INTERNAL, s_packet_pool, s_tls_send_staging, and s_tls_socket.

Referenced by internal_demo_tls_session().

◆ internal_demo_calloc()

void * internal_demo_calloc ( size_t n,
size_t size )
static

mbedtls_calloc hook backed by the ThreadX byte pool.

Computes the requested byte count safely with overflow detection, performs a nonblocking pool allocation, clears the complete span, and returns it to Mbed TLS.

Parameters
[in]nElement count.
[in]sizeElement size in bytes.
Returns
Newly allocated zeroed memory, or NULL on failure.
Return values
nullptrThe size is zero, multiplication overflows, or the pool is exhausted.
non-NULLA zero-filled ThreadX-owned allocation.
Precondition
s_byte_pool has been created.
No pool mutex inversion exists in the calling context.
Postcondition
Successful storage is zero-initialized over the requested byte count.
Failure leaves the byte pool with no allocation owned by the caller.
Note
This hook never waits, preventing allocator deadlock inside TLS paths.
Since
0.1.0

Definition at line 896 of file main.c.

References memset(), RA8_INTERNAL, s_byte_pool, and TX_SUCCESS.

Referenced by tx_application_define().

◆ internal_demo_entropy_source()

int internal_demo_entropy_source ( void * ctx,
unsigned char * buf,
size_t len,
size_t * olen )
static

Pull entropy from the RSIP TRNG into Mbed TLS.

Mbed TLS supplies the f_rng signature int f_rng(void* ctx, unsigned char* buf, size_t len); we bind it to ra8_rsip_trng_read so seeding the CTR_DRBG never depends on software entropy collectors.

Parameters
[in]ctxUnused.
[out]bufOutput buffer.
[in]lenNumber of bytes requested.
[out]olenReceives the number of bytes actually written (always len on success).
Returns
0 on success.
Return values
0Exactly len bytes were produced.
MBEDTLS_ERR_ENTROPY_SOURCE_FAILEDAn argument or RSIP request failed.
Precondition
RSIP initialization and BIST completed successfully.
buf and olen are non-NULL caller-owned storage.
Postcondition
On success *olen equals the requested length.
On failure no success length is reported to Mbed TLS.
Note
The callback does not maintain entropy state between invocations.
Since
0.1.0

Definition at line 620 of file main.c.

References k_ra8_ok, and ra8_rsip_trng_read().

Referenced by internal_demo_tls_session().

◆ internal_demo_free()

void internal_demo_free ( void * p)
static

mbedtls_free hook backed by the ThreadX byte pool.

Releases a non-NULL allocation back to the byte pool while treating NULL exactly as the standard free contract requires.

Parameters
[in]pPointer previously returned by internal_demo_calloc (NULL OK).
Precondition
p is NULL or denotes a live allocation from s_byte_pool.
No other context is concurrently releasing the same allocation.
Postcondition
Non-NULL storage is returned to the ThreadX byte pool.
NULL input leaves the pool unchanged.
Note
Release errors are ignored because the Mbed TLS free hook has no status.
Since
0.1.0

Definition at line 925 of file main.c.

References RA8_INTERNAL.

Referenced by tx_application_define().

◆ internal_demo_http_get()

int internal_demo_http_get ( void )
static

Issue the HTTP GET and dump the first k_demo_dump_bytes to SCI8.

Formats and fully writes a bounded request, scans streamed response chunks for the header terminator, then emits only the capped body prefix.

Returns
0 on success.
Return values
0The request completed without a TLS read/write error.
-1Request formatting exceeded the fixed request buffer.
Precondition
TLS handshake is complete and the cert pin verified.
Request and response file-scope buffers are exclusively owned here.
Postcondition
Up to k_demo_dump_bytes of body have been emitted.
HTTP headers are not written to the diagnostic console.
Note
WANT_READ and WANT_WRITE are retried because the BIO is nonblocking.
Since
0.1.0

Definition at line 683 of file main.c.

References internal_demo_write_bytes(), k_ascii_cr, k_ascii_lf, k_demo_dump_bytes, memcpy(), RA8_INTERNAL, s_demo_host_name, s_request_buf, s_response_buf, s_ssl, and strlen().

Referenced by internal_demo_tls_session().

◆ internal_demo_netx_bring_up()

UINT internal_demo_netx_bring_up ( void )
static

Bring NetX Duo up: pool, IP, ARP, TCP, ICMP.

No listen socket.

Creates the packet pool and IP instance, installs the fixed local addresses, then enables the protocols needed by outbound HTTPS.

Returns
NX_SUCCESS on success, propagating NetX error codes.
Return values
NX_SUCCESSThe outbound IPv4/TCP stack is ready.
nonzeroA NetX creation or protocol-enable operation failed.
Precondition
nx_system_initialize has run.
File-scope NetX storage is not owned by another IP instance.
Postcondition
On success the IP stack is ready for outbound TCP.
On error no later protocol-enable step is attempted.
Note
ICMP and gateway configuration are non-fatal conveniences for the demo.
Since
0.1.0

Definition at line 416 of file main.c.

References internal_demo_pack_ip(), internal_demo_pack_mac(), k_demo_ip_thread_pri, k_demo_packet_size, nx_ether_driver_ra8_eth(), RA8_INTERNAL, s_arp_cache, s_demo_gw, s_demo_ip, s_demo_mask, s_ip, s_ip_name, s_ip_stack, s_packet_pool, s_packet_pool_name, and s_pool_memory.

Referenced by internal_demo_thread_entry().

◆ internal_demo_pack_ip()

ULONG internal_demo_pack_ip ( const uint8_t * octets)
static

Pack a 4-octet IPv4 array into NetX's host-order ULONG.

Places each octet at the NetX-defined significance so the result can be passed directly to IP, mask, and gateway APIs.

Parameters
[in]octetsIPv4 address (4 bytes).
Returns
Host-order packed address suitable for NetX.
Return values
0..UINT32_MAXThe packed four-octet address.
Precondition
octets is non-NULL.
The array contains at least four readable bytes.
Postcondition
The returned word contains all four input octets in network order.
The input array is not modified.
Note
This helper performs no address validity or subnet checks.
Since
0.1.0

Definition at line 371 of file main.c.

References k_demo_ip_oct_a, k_demo_ip_oct_b, k_demo_ip_oct_c, k_demo_ip_oct_d, k_demo_ip_shift_a, k_demo_ip_shift_b, k_demo_ip_shift_c, k_demo_ip_shift_d, and RA8_INTERNAL.

Referenced by internal_demo_netx_bring_up(), and internal_demo_tcp_connect().

◆ internal_demo_pack_mac()

void internal_demo_pack_mac ( ULONG * msw,
ULONG * lsw )
static

Pack the local MAC into the msw/lsw fields NetX expects.

Splits the immutable six-byte demo address into NetX's two-word physical-address representation.

Parameters
[out]mswMost-significant word (octets 0-1).
[out]lswLeast-significant word (octets 2-5).
Precondition
msw is non-NULL and writable.
lsw is non-NULL and writable.
Postcondition
*msw contains MAC octets zero and one.
*lsw contains MAC octets two through five.
Note
The locally administered address is compile-time fixed for this demo.
Since
0.1.0

Definition at line 392 of file main.c.

References k_demo_mac_idx_0, k_demo_mac_idx_1, k_demo_mac_idx_2, k_demo_mac_idx_3, k_demo_mac_idx_4, k_demo_mac_idx_5, k_demo_mac_lsw_shift_b2, k_demo_mac_lsw_shift_b3, k_demo_mac_lsw_shift_b4, k_demo_mac_lsw_shift_b5, k_demo_mac_msw_shift_b0, k_demo_mac_msw_shift_b1, RA8_INTERNAL, and s_demo_mac.

Referenced by internal_demo_netx_bring_up().

◆ internal_demo_panic_halt()

void internal_demo_panic_halt ( void )
static

Halt forever in WFI.

Called on any fatal error.

Retains the failed boot context while preventing any further network, crypto, or board accesses.

Precondition
Called only after a fatal error.
No recovery path remains for this boot attempt.
Postcondition
CPU is parked.
Peripheral state is left unchanged until reset.
Note
No diagnostic write is attempted because console setup may have failed.
Since
0.1.0

Definition at line 265 of file main.c.

References RA8_INTERNAL.

Referenced by internal_demo_ble_or_halt(), internal_demo_clocks_or_halt(), internal_demo_setup_or_halt(), and main().

◆ internal_demo_print()

void internal_demo_print ( const char * s)
static

Convenience wrapper to write a NUL-terminated string to SCI8.

Measures the payload locally and submits exactly those bytes to the BSP console without dynamic allocation.

Parameters
[in]sNUL-terminated ASCII string; NULL is a no-op.
Precondition
s != NULL.
The BSP console has been initialized.
Postcondition
Bytes are queued in the SCI8 TX FIFO (best-effort).
A NULL argument returns without accessing memory or the console.
Note
Console errors are intentionally non-fatal diagnostic loss.
Since
0.1.0

Definition at line 326 of file main.c.

References ra8_board_uart_console_write(), RA8_INTERNAL, and strlen().

Referenced by internal_demo_thread_entry(), internal_demo_tls_session(), and main().

◆ internal_demo_setup_or_halt()

void internal_demo_setup_or_halt ( void )
static

Bring CGC + the J-Link OB VCOM console + on-board Ethernet + RSIP up.

Initializes each dependency in clock-to-peripheral order and parks immediately if any mandatory service cannot be established.

Precondition
Reset_Handler / SystemInit complete.
The application is still in single-threaded boot context.
Postcondition
On success the BSP console is sending at 115200 8N1, the on-board RGMII PHY pins + ETHA0 / RMAC0 are initialized, and the RSIP engine has finished BIST.
On failure the CPU is parked before ThreadX starts.
Note
RSIP BIST runs once here so TLS entropy requests do not repeat it.
Since
0.1.0

Definition at line 285 of file main.c.

References internal_demo_panic_halt(), k_demo_baud, k_ra8_clock_id_cpuclk0, k_ra8_ok, ra8_board_ethernet_init(), ra8_board_uart_console_init(), ra8_cgc_get_clock_hz(), ra8_cgc_init(), RA8_INTERNAL, ra8_rsip_init(), and ra8_time_init().

Referenced by main().

◆ internal_demo_tcp_connect()

UINT internal_demo_tcp_connect ( void )
static

Open a NetX Duo TCP socket and connect to www.example.com:443.

Creates and binds one client socket, then performs a bounded-wait connection to the configured server address and HTTPS port.

Returns
NX_SUCCESS on success.
Return values
NX_SUCCESSThe socket is connected.
nonzeroSocket creation, binding, or connection failed.
Precondition
internal_demo_netx_bring_up has returned NX_SUCCESS.
s_tls_socket is not already created or bound.
Postcondition
On success s_tls_socket is connected and ready for I/O.
On failure no TLS handshake is attempted by this helper.
Note
Socket teardown is omitted because the worker performs one transaction.
Since
0.1.0

Definition at line 479 of file main.c.

References internal_demo_pack_ip(), k_demo_handshake_max, k_demo_https_port, k_demo_recv_window, k_demo_socket_ttl, RA8_INTERNAL, s_demo_host_ip, s_ip, s_socket_name, and s_tls_socket.

Referenced by internal_demo_thread_entry().

◆ internal_demo_thread_entry()

void internal_demo_thread_entry ( ULONG thread_input)
static

ThreadX worker entry: bring NetX up, run TLS session, dump body.

Initializes PSA, brings up the IP stack, connects TCP, and runs one HTTPS transaction, logging the first failure rather than retrying forever.

Parameters
[in]thread_inputUnused.
Precondition
nx_system_initialize has run.
ThreadX invokes the entry with the demo's static worker stack active.
Postcondition
The thread entry returns after completion or the first logged failure.
The input token and caller-owned ThreadX scheduler state are unchanged.
Note
This one-shot sample intentionally leaves retry policy to a real app.
Since
0.1.0

Definition at line 852 of file main.c.

References internal_demo_netx_bring_up(), internal_demo_print(), internal_demo_tcp_connect(), internal_demo_tls_session(), and RA8_INTERNAL.

Referenced by tx_application_define().

◆ internal_demo_tls_session()

int internal_demo_tls_session ( void )
static

Run the TLS bring-up: DRBG, config, configured pin policy, GET, dump.

Configures one client session over the connected NetX socket, completes the handshake, applies the configured pin policy, and issues the GET. DEMO_DISABLE_CERT_PIN is an explicit bring-up-only override.

Returns
0 on success.
Return values
0TLS, configured pin policy, and HTTP processing succeeded.
-1A configuration, hostname, or pinning step failed.
Precondition
internal_demo_tcp_connect returned NX_SUCCESS.
PSA crypto has been initialized with the RSIP random hook available.
Postcondition
On success the configured session has completed the bounded HTTP GET.
No HTTP request is sent if handshake or enabled pin verification fails.
Note
TLS contexts are file-scope to keep the ThreadX worker stack bounded.
Since
0.1.0

Definition at line 780 of file main.c.

References internal_demo_bio_recv(), internal_demo_bio_send(), internal_demo_entropy_source(), internal_demo_http_get(), internal_demo_print(), internal_demo_verify_cert_pin(), RA8_INTERNAL, s_demo_host_name, s_drbg, s_entropy, s_ssl, and s_ssl_cfg.

Referenced by internal_demo_thread_entry().

◆ internal_demo_verify_cert_pin()

int internal_demo_verify_cert_pin ( void )
static

Verify the peer's leaf certificate matches our compile-time pin.

Hashes the peer's leaf DER with ra8_rsip_sha256 and compares against s_demo_cert_pin_sha256. Mismatch is fatal – the caller must abort the handshake without sending the HTTP request.

Returns
Zero only when the peer leaf certificate matches the stored pin.
Return values
0Pin matches.
-1Pin mismatch (or no peer cert available).
Precondition
Mbed TLS handshake has reached at least MBEDTLS_SSL_SERVER_CERTIFICATE.
RSIP SHA-256 is initialized and available.
Postcondition
The peer certificate bytes and stored pin remain unchanged.
A mismatch produces no HTTP request or response-body output.
Note
This function is compiled only when certificate pinning is enabled.
Since
0.1.0

Definition at line 652 of file main.c.

References k_ra8_ok, memcmp(), RA8_INTERNAL, ra8_rsip_sha256(), s_demo_cert_pin_sha256, and s_ssl.

Referenced by internal_demo_tls_session().

◆ internal_demo_write_bytes()

void internal_demo_write_bytes ( const uint8_t * buf,
uint32_t len )
static

Write an arbitrary byte buffer to SCI8 (no NUL required).

Sends one bounded byte span directly to the BSP console, allowing response-body fragments containing non-text bytes.

Parameters
[in]bufBytes to emit.
[in]lenLength in bytes.
Precondition
buf references at least len readable bytes when len is nonzero.
The BSP console has been initialized.
Postcondition
One best-effort console write is attempted for a nonempty span.
Null or empty input returns without touching the console.
Note
Partial or failed diagnostic writes do not abort the HTTPS transaction.
Since
0.1.0

Definition at line 348 of file main.c.

References ra8_board_uart_console_write(), and RA8_INTERNAL.

Referenced by internal_demo_http_get().

◆ main()

void main ( void )

Application entry.

The application entry point Reset_Handler hands control to.

Brings up clocks + UART + RMII pins, then ThreadX.

Precondition
Reset_Handler has copied .data and zeroed .bss.
Postcondition
On clean entry the kernel runs the worker thread once.
Since
0.1.0

Definition at line 976 of file main.c.

References internal_demo_panic_halt(), internal_demo_print(), internal_demo_setup_or_halt(), and ra8_isr_globals_enable().

◆ tx_application_define()

void tx_application_define ( void * first_unused_memory)

ThreadX system-define hook: build worker thread + byte pool.

Parameters
[in]first_unused_memoryPointer to free RAM provided by the ThreadX port; unused – we statically allocate.
Precondition
tx_kernel_enter() has been called.
Postcondition
One worker thread is created and NetX + Mbed TLS are initialized inside it.
Since
0.1.0

Definition at line 942 of file main.c.

References internal_demo_calloc(), internal_demo_free(), internal_demo_thread_entry(), k_demo_app_thread_pri, s_byte_pool, s_byte_pool_memory, s_byte_pool_name, s_demo_stack, s_demo_thread, s_thread_name, TX_AUTO_START, TX_NO_TIME_SLICE, and tx_thread_create.

Variable Documentation

◆ s_arp_cache

◆ s_byte_pool

TX_BYTE_POOL s_byte_pool
static

Definition at line 239 of file main.c.

Referenced by demo_calloc(), internal_demo_calloc(), and tx_application_define().

◆ s_byte_pool_memory

UCHAR s_byte_pool_memory[k_demo_byte_pool_size]
static

Definition at line 240 of file main.c.

Referenced by tx_application_define().

◆ s_demo_cert_pin_sha256

const uint8_t s_demo_cert_pin_sha256[32]
static
Initial value:
= {
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U, 0x00U,
}

SHA-256 of the expected leaf certificate DER, compile-time pinned.

Update this constant whenever www.example.com rotates its leaf certificate. Mismatch is treated as a fatal handshake failure – we will not send the HTTP request. To capture a fresh pin from a development host: echo | openssl s_client -connect www.example.com:443 \ -servername www.example.com 2>/dev/null \ | openssl x509 -outform der | openssl dgst -sha256 The 32-byte digest goes here byte-for-byte. The placeholder below is all-zero and will deliberately fail the pin check until the value is filled in – callers can disable pinning during bring-up by defining DEMO_DISABLE_CERT_PIN.

Definition at line 219 of file main.c.

Referenced by internal_demo_verify_cert_pin().

◆ s_demo_gw

const uint8_t s_demo_gw[4] = {k_demo_ipaddr_0, k_demo_ipaddr_1, k_demo_ipaddr_2, 1U}
static

Default gateway: 192.168.1.1.

Definition at line 186 of file main.c.

Referenced by internal_demo_netx_bring_up().

◆ s_demo_host_ip

const uint8_t s_demo_host_ip[4]
static
Initial value:
@ k_demo_hostip_3
Demo hostip 3.
Definition main.c:155
@ k_demo_hostip_1
Demo hostip 1.
Definition main.c:153
@ k_demo_hostip_2
Demo hostip 2.
Definition main.c:154
@ k_demo_hostip_0
93.184.216.34 (www.example.com).
Definition main.c:152

Static IPv4 for www.example.com (legacy edge-of-net IP).

Pinned to 93.184.216.34 – the historic www.example.com IP. If the address rotates, override at compile time with -DDEMO_HOST_IPV4_OVERRIDE=0x5DB8D822UL etc., or replace this static lookup with a NetX DNS resolver in a future sweep.

Definition at line 196 of file main.c.

Referenced by internal_demo_tcp_connect().

◆ s_demo_host_name

const char s_demo_host_name[] = "www.example.com"
static

HTTP/1.1 Host header literal – matches the IP pin above.

Definition at line 202 of file main.c.

Referenced by internal_demo_http_get(), and internal_demo_tls_session().

◆ s_demo_ip

const uint8_t s_demo_ip[4]
static
Initial value:
@ k_demo_ipaddr_0
192.168.1.42 board.
Definition main.c:147
@ k_demo_ipaddr_2
Demo ipaddr 2.
Definition main.c:149
@ k_demo_ipaddr_3
Demo ipaddr 3.
Definition main.c:150
@ k_demo_ipaddr_1
Demo ipaddr 1.
Definition main.c:148

IPv4 address: 192.168.1.42 / 255.255.255.0.

Definition at line 177 of file main.c.

Referenced by internal_demo_netx_bring_up().

◆ s_demo_mac

const uint8_t s_demo_mac[6] = {0x02U, 0x00U, 0x00U, 0x00U, 0x00U, 0x02U}
static

Definition at line 174 of file main.c.

Referenced by internal_demo_pack_mac().

◆ s_demo_mask

const uint8_t s_demo_mask[4] = {k_demo_netmask_b, k_demo_netmask_b, k_demo_netmask_b, 0U}
static

Subnet mask: 255.255.255.0.

Definition at line 183 of file main.c.

Referenced by internal_demo_netx_bring_up().

◆ s_demo_stack

UCHAR s_demo_stack[k_demo_thread_stack]
static

Definition at line 238 of file main.c.

Referenced by tx_application_define(), and tx_application_define().

◆ s_demo_thread

TX_THREAD s_demo_thread
static

Definition at line 237 of file main.c.

Referenced by tx_application_define(), and tx_application_define().

◆ s_drbg

mbedtls_ctr_drbg_context s_drbg
static

Definition at line 246 of file main.c.

Referenced by internal_demo_tls_session().

◆ s_entropy

mbedtls_entropy_context s_entropy
static

Definition at line 247 of file main.c.

Referenced by internal_demo_tls_session().

◆ s_ip

◆ s_ip_stack

◆ s_packet_pool

NX_PACKET_POOL s_packet_pool
static

◆ s_pool_memory

UCHAR s_pool_memory[k_demo_pool_bytes]
static

Definition at line 231 of file main.c.

Referenced by demo_netx_bring_up(), demo_netx_create_ip(), and internal_demo_netx_bring_up().

◆ s_request_buf

UCHAR s_request_buf[k_demo_request_buf]
static

Definition at line 249 of file main.c.

Referenced by internal_demo_http_get().

◆ s_response_buf

UCHAR s_response_buf[k_demo_response_buf]
static

Definition at line 250 of file main.c.

Referenced by internal_demo_http_get().

◆ s_ssl

mbedtls_ssl_context s_ssl
static

◆ s_ssl_cfg

mbedtls_ssl_config s_ssl_cfg
static

Definition at line 245 of file main.c.

Referenced by internal_demo_tls_session().

◆ s_tls_send_staging

UCHAR s_tls_send_staging[k_demo_pool_bytes]
static

Definition at line 251 of file main.c.

Referenced by internal_demo_bio_send().

◆ s_tls_socket

NX_TCP_SOCKET s_tls_socket
static