ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
c6_join_net.c
Go to the documentation of this file.
1
24
25#include <stdint.h>
26#include <string.h>
27
28#include "c6_join.h"
29#include "nx_api.h"
30#include "nx_ether_driver_c6.h"
31#include "nxd_dhcp_client.h"
32#include "ra8_c6link.h"
33#include "ra8_err.h"
34#include "tx_api.h"
35
59
61static NX_PACKET_POOL s_pool;
63alignas(4) static uint8_t s_pool_mem[k_c6_join_pool_bytes];
65static NX_IP s_ip;
67alignas(8) static uint8_t s_ip_stack[k_c6_join_ip_stack];
69alignas(4) static uint8_t s_arp_cache[k_c6_join_arp_bytes];
71static NX_DHCP s_dhcp;
73static char s_ping_payload[k_c6_join_ping_len] = "ra8-c6-ping";
75static CHAR s_pool_name[] = "c6_join_pool";
77static CHAR s_ip_name[] = "c6_join_ip";
79static CHAR s_dhcp_name[] = "c6_join_dhcp";
80
81/* Create the packet pool + IP instance and enable the protocols DHCP needs. */
83{
84 UINT s = nx_packet_pool_create(&s_pool,
87 (VOID*)s_pool_mem,
88 (ULONG)sizeof(s_pool_mem));
89 if (s != NX_SUCCESS) {
90 return s;
91 }
92 s = nx_ip_create(&s_ip,
94 IP_ADDRESS(0, 0, 0, 0),
95 IP_ADDRESS(0, 0, 0, 0),
96 &s_pool,
98 (VOID*)s_ip_stack,
99 (ULONG)sizeof(s_ip_stack),
101 if (s != NX_SUCCESS) {
102 return s;
103 }
104 s = nx_arp_enable(&s_ip, (VOID*)s_arp_cache, (ULONG)sizeof(s_arp_cache));
105 if (s != NX_SUCCESS) {
106 return s;
107 }
108 s = nx_udp_enable(&s_ip);
109 if (s != NX_SUCCESS) {
110 return s;
111 }
112 return nx_icmp_enable(&s_ip);
113}
114
115/* Run the DHCP client to a bound lease, then read the lease into out. */
117{
118 UINT s = nx_dhcp_create(&s_dhcp, &s_ip, s_dhcp_name);
119 if (s != NX_SUCCESS) {
120 return s;
121 }
122 s = nx_dhcp_start(&s_dhcp);
123 if (s != NX_SUCCESS) {
124 return s;
125 }
126 ULONG actual = 0U;
127 s = nx_ip_status_check(&s_ip,
128 (ULONG)NX_IP_ADDRESS_RESOLVED,
129 &actual,
131 if (s != NX_SUCCESS) {
132 return s;
133 }
134 ULONG ip = 0U;
135 ULONG mask = 0U;
136 ULONG gw = 0U;
137 ULONG server = 0U;
138 (void)nx_ip_address_get(&s_ip, &ip, &mask);
139 (void)nx_ip_gateway_address_get(&s_ip, &gw);
140 (void)nx_dhcp_server_address_get(&s_dhcp, &server);
141 out->ip = (uint32_t)ip;
142 out->mask = (uint32_t)mask;
143 out->gateway = (uint32_t)gw;
144 out->dhcp_server = (uint32_t)server;
145 return NX_SUCCESS;
146}
147
148/* Send bounded ICMP echoes to the gateway; true once one is answered. */
149static bool priv_net_ping(uint32_t gateway)
150{
151 if (gateway == 0U) {
152 return false;
153 }
154 for (uint32_t i = 0U; i < (uint32_t)k_c6_join_ping_tries; i++) {
155 NX_PACKET* resp = NX_NULL;
156 UINT s = nx_icmp_ping(&s_ip,
157 (ULONG)gateway,
160 &resp,
162 if (s == NX_SUCCESS) {
163 if (resp != NX_NULL) {
164 (void)nx_packet_release(resp);
165 }
166 return true;
167 }
169 }
170 return false;
171}
172
174{
175 if ((link == nullptr) || (mac == nullptr) || (out == nullptr)) {
176 return k_ra8_err_null_ptr;
177 }
178 *out = (c6_join_lease_t){};
179
182 nx_system_initialize();
183
184 if (priv_net_create_ip() != NX_SUCCESS) {
186 }
187 if (priv_net_dhcp(out) != NX_SUCCESS) {
188 *out = (c6_join_lease_t){};
189 return k_ra8_err_timeout;
190 }
191 out->ping_ok = priv_net_ping(out->gateway);
192 return k_ra8_ok;
193}
static CHAR s_dhcp_name[]
Definition c6_cam_net.c:39
static uint8_t s_pool_mem[k_c6_cam_net_pool_bytes]
Definition c6_cam_net.c:31
static CHAR s_pool_name[]
Definition c6_cam_net.c:37
static NX_DHCP s_dhcp
Definition c6_cam_net.c:35
static CHAR s_ip_name[]
Definition c6_cam_net.c:38
Shared contract for the C6 Wi-Fi join + DHCP + reachability app.
@ k_c6_join_dhcp_wait_ms
DHCP lease wait budget, milliseconds/ticks.
Definition c6_join.h:93
@ k_c6_join_ping_gap_ms
Gap between echoes, milliseconds.
Definition c6_join.h:96
@ k_c6_join_ping_tries
ICMP echoes sent to the gateway.
Definition c6_join.h:94
@ k_c6_join_ping_wait_ms
Per-echo reply timeout, milliseconds/ticks.
Definition c6_join.h:95
struct c6_join_lease c6_join_lease_t
static char s_ping_payload[k_c6_join_ping_len]
Fixed ICMP echo payload.
Definition c6_join_net.c:73
ra8_err_t c6_join_net_up(ra8_c6link_t *link, const ra8_c6link_mac_t *mac, c6_join_lease_t *out)
Bring the IP layer up over the associated C6 link and prove traffic.
c6_join_net_size_t
Static sizing for the NetX Duo objects this app owns.
Definition c6_join_net.c:51
@ k_c6_join_ip_prio
NetX IP helper-thread priority.
Definition c6_join_net.c:56
@ k_c6_join_ping_len
ICMP echo payload length, in octets.
Definition c6_join_net.c:57
@ k_c6_join_arp_bytes
ARP cache backing store, in octets.
Definition c6_join_net.c:55
@ k_c6_join_ip_stack
NetX IP helper-thread stack, in octets.
Definition c6_join_net.c:54
@ k_c6_join_pool_bytes
Packet-pool backing store, in octets.
Definition c6_join_net.c:53
@ k_c6_join_pkt_payload
Per-packet payload, in octets.
Definition c6_join_net.c:52
static UINT priv_net_dhcp(c6_join_lease_t *out)
static UINT priv_net_create_ip(void)
Definition c6_join_net.c:82
static bool priv_net_ping(uint32_t gateway)
static ULONG s_ip_stack[k_demo_ip_stack/sizeof(ULONG)]
Definition main.c:232
static NX_IP s_ip
Definition main.c:229
static ULONG s_arp_cache[k_demo_arp_cache/sizeof(ULONG)]
Definition main.c:233
NetX Duo network driver shim that bridges onto the ESP32-C6 link.
void nx_ether_driver_c6_bind(ra8_c6link_t *link)
Bind an open C6 link handle to the driver and arm its mutex.
void nx_ether_driver_c6(NX_IP_DRIVER *driver_req)
NetX Duo link driver entry point for the ESP32-C6 Wi-Fi station.
void nx_ether_driver_c6_set_mac(const uint8_t mac[6])
Tell the driver the station MAC address to stamp on outgoing frames.
Error Code Definitions for ra8-firmware.
@ k_ra8_err_not_initialized
Module not initialized – _init() not yet called successfully.
Definition ra8_err.h:235
@ 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_timeout
Operation exceeded its time budget.
Definition ra8_err.h:188
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
static ra8_esp_hosted_pool_state_t s_pool
Singleton state of the memory and queue half.
unsigned int UINT
ThreadX-compatible unsigned int (host stub).
#define tx_thread_sleep
char CHAR
ThreadX-compatible CHAR (host stub).
unsigned long ULONG
ThreadX-compatible unsigned long (host stub).
uint32_t gateway
Default gateway from the lease; the ping target.
Definition c6_join.h:165
uint32_t mask
Subnet mask from the lease.
Definition c6_join.h:164
uint32_t dhcp_server
Address of the DHCP server that answered.
Definition c6_join.h:166
bool ping_ok
A gateway ICMP echo reply was received.
Definition c6_join.h:167
uint32_t ip
Leased station address, or zero on failure.
Definition c6_join.h:163