ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
ra8_c6link_wifi_sta.c
Go to the documentation of this file.
1
27
28#include <stddef.h>
29#include <stdint.h>
30
31#include "ra8_attributes.h"
32#include "ra8_c6link.h"
33#include "ra8_c6link_internal.h"
34#include "ra8_c6link_wifi.h"
35#include "ra8_secure.h"
36
73
91RA8_INTERNAL static uint8_t internal_c6link_sta_len(const char* text, uint8_t cap)
92{
93 uint8_t n = 0U;
94 while ((n < cap) && (text[n] != '\0')) {
95 n++;
96 }
97 return n;
98}
99
100ra8_err_t ra8_c6link_sta_cfg_set(ra8_c6link_sta_cfg_t* cfg, const char* ssid, const char* pass)
101{
102 if ((cfg == nullptr) || (ssid == nullptr)) {
103 if (cfg != nullptr) {
104 ra8_secure_memzero(cfg, sizeof(*cfg));
105 }
106 return k_ra8_err_null_ptr;
107 }
108 ra8_secure_memzero(cfg, sizeof(*cfg));
109
110 const uint8_t ssid_len = internal_c6link_sta_len(ssid, (uint8_t)sizeof cfg->ssid);
111 if ((ssid_len == 0U) || (ssid_len > (uint8_t)k_ra8_c6link_ssid_max)) {
113 }
114 const uint8_t pass_len =
115 (pass == nullptr) ? 0U : internal_c6link_sta_len(pass, (uint8_t)sizeof cfg->pass);
116 if (pass_len > (uint8_t)k_ra8_c6link_pass_max) {
118 }
119
120 for (uint8_t i = 0U; i < ssid_len; i++) {
121 cfg->ssid[i] = ssid[i];
122 }
123 for (uint8_t i = 0U; i < pass_len; i++) {
124 cfg->pass[i] = pass[i];
125 }
126 cfg->ssid_len = ssid_len;
127 cfg->pass_len = pass_len;
128 return k_ra8_ok;
129}
130
160
179 const ra8_c6link_sta_cfg_t* cfg)
180{
181 for (uint8_t i = 0U; i < cfg->ssid_len; i++) {
182 buf->ssid[i] = (uint8_t)cfg->ssid[i];
183 }
184 for (uint8_t i = 0U; i < cfg->pass_len; i++) {
185 buf->pass[i] = (uint8_t)cfg->pass[i];
186 }
187 for (uint8_t i = 0U; i < (uint8_t)k_ra8_c6link_mac_bytes; i++) {
188 buf->bssid[i] = cfg->bssid.octet[i];
189 }
190}
191
213 const ra8_c6link_sta_cfg_t* cfg)
214{
215 WifiScanThreshold threshold;
216 wifi_scan_threshold__init(&threshold);
217 threshold.authmode = (int32_t)k_ra8_c6link_auth_open;
218
219 WifiPmfConfig pmf;
220 wifi_pmf_config__init(&pmf);
221 pmf.capable = true;
222
224 internal_c6link_sta_stage(&buf, cfg);
225
226 WifiStaConfig sta;
227 wifi_sta_config__init(&sta);
228 sta.ssid.data = buf.ssid;
229 sta.ssid.len = (size_t)cfg->ssid_len;
230 sta.password.data = buf.pass;
231 sta.password.len = (size_t)cfg->pass_len;
232 sta.scan_method = (int32_t)k_ra8_c6link_scan_fast;
233 sta.sort_method = (int32_t)k_ra8_c6link_sort_signal;
234 sta.channel = (uint32_t)cfg->channel;
235 sta.bssid_set = cfg->bssid_set;
236 sta.bssid.data = buf.bssid;
237 sta.bssid.len = cfg->bssid_set ? (size_t)k_ra8_c6link_mac_bytes : 0U;
238 sta.threshold = &threshold;
239 sta.pmf_cfg = &pmf;
240
241 WifiConfig wcfg;
242 wifi_config__init(&wcfg);
243 wcfg.u_case = WIFI_CONFIG__U_STA;
244 wcfg.sta = &sta;
245
246 RpcReqWifiSetConfig body;
247 rpc__req__wifi_set_config__init(&body);
248 body.iface = (int32_t)k_ra8_c6link_iface_sta;
249 body.cfg = &wcfg;
250
251 Rpc req;
252 rpc__init(&req);
253 req.msg_type = RPC_TYPE__Req;
254 req.msg_id = RPC_ID__Req_WifiSetConfig;
255 req.payload_case = RPC__PAYLOAD_REQ_WIFI_SET_CONFIG;
256 req.req_wifi_set_config = &body;
257
258 ra8_c6link_take_ctx_t take = {.link = link,
259 .out = nullptr,
260 .rpc_id = (uint32_t)RPC_ID__Req_WifiSetConfig};
261 const ra8_err_t result = priv_c6link_rpc_call(link,
262 &req,
263 (uint32_t)RPC_ID__Resp_WifiSetConfig,
265 &take);
266 ra8_secure_memzero(&buf, sizeof(buf));
267 return result;
268}
269
271{
272 if ((link == nullptr) || (cfg == nullptr)) {
273 return k_ra8_err_null_ptr;
274 }
275 if (!ra8_c6link_is_open(link)) {
277 }
278 if ((cfg->ssid_len == 0U) || (cfg->ssid_len > (uint8_t)k_ra8_c6link_ssid_max) ||
279 (cfg->pass_len > (uint8_t)k_ra8_c6link_pass_max)) {
281 }
282
283 const ra8_err_t configured = internal_c6link_sta_set_config(link, cfg);
284 if (configured != k_ra8_ok) {
285 return configured;
286 }
287 return priv_c6link_bare_req(link, (uint32_t)RPC_ID__Req_WifiConnect);
288}
289
307RA8_INTERNAL static ra8_err_t internal_c6link_take_mac(void* ctx, const void* msg_v)
308{
310 const Rpc* msg = (const Rpc*)msg_v;
312
313 const RpcRespGetMacAddress* body = msg->resp_get_mac_address;
314 if (body == nullptr) {
316 }
317 const ra8_err_t reported = priv_c6link_resp(take->link, take->rpc_id, body->resp);
318 if (reported != k_ra8_ok) {
319 return reported;
320 }
321 return priv_c6link_copy_mac(out, &body->mac) ? k_ra8_ok : k_ra8_err_protocol_error;
322}
323
325{
326 if ((link == nullptr) || (out == nullptr)) {
327 return k_ra8_err_null_ptr;
328 }
329 if (!ra8_c6link_is_open(link)) {
331 }
332 *out = (ra8_c6link_mac_t){};
333
334 RpcReqGetMacAddress body;
335 rpc__req__get_mac_address__init(&body);
336 /* `Req_GetMACAddress.mode` is a `wifi_interface_t`, not a `wifi_mode_t`: the
337 co-processor passes it straight to `esp_wifi_get_mac()`. Select the station
338 interface (0) so the address returned is the one the radio associates with.
339 `WIFI_IF_AP` (1) hands back the SoftAP address (station address plus one),
340 which no access point ever sees, so DHCP for the station never completes. */
341 body.mode = (int32_t)k_ra8_c6link_iface_sta;
342
343 Rpc req;
344 rpc__init(&req);
345 req.msg_type = RPC_TYPE__Req;
346 req.msg_id = RPC_ID__Req_GetMACAddress;
347 req.payload_case = RPC__PAYLOAD_REQ_GET_MAC_ADDRESS;
348 req.req_get_mac_address = &body;
349
350 ra8_c6link_take_ctx_t take = {.link = link,
351 .out = out,
352 .rpc_id = (uint32_t)RPC_ID__Req_GetMACAddress};
353 return priv_c6link_rpc_call(link,
354 &req,
355 (uint32_t)RPC_ID__Resp_GetMACAddress,
357 &take);
358}
359
378RA8_INTERNAL static ra8_err_t internal_c6link_take_ap(void* ctx, const void* msg_v)
379{
381 const Rpc* msg = (const Rpc*)msg_v;
383
384 const RpcRespWifiStaGetApInfo* body = msg->resp_wifi_sta_get_ap_info;
385 if (body == nullptr) {
387 }
388 const ra8_err_t reported = priv_c6link_resp(take->link, take->rpc_id, body->resp);
389 if (reported != k_ra8_ok) {
390 return reported;
391 }
392 if (body->ap_record == nullptr) {
394 }
395
396 const WifiApRecord* rec = body->ap_record;
397 out->ssid_len = priv_c6link_copy_str(out->ssid, (uint8_t)sizeof out->ssid, &rec->ssid);
398 out->channel = (uint8_t)rec->primary;
399 out->rssi = (int8_t)rec->rssi;
400 out->authmode = rec->authmode;
401 (void)priv_c6link_copy_mac(&out->bssid, &rec->bssid);
402 return k_ra8_ok;
403}
404
406{
407 if ((link == nullptr) || (out == nullptr)) {
408 return k_ra8_err_null_ptr;
409 }
410 if (!ra8_c6link_is_open(link)) {
412 }
413 *out = (ra8_c6link_ap_info_t){};
414
415 RpcReqWifiStaGetApInfo body;
416 rpc__req__wifi_sta_get_ap_info__init(&body);
417
418 Rpc req;
419 rpc__init(&req);
420 req.msg_type = RPC_TYPE__Req;
421 req.msg_id = RPC_ID__Req_WifiStaGetApInfo;
422 req.payload_case = RPC__PAYLOAD_REQ_WIFI_STA_GET_AP_INFO;
423 req.req_wifi_sta_get_ap_info = &body;
424
425 ra8_c6link_take_ctx_t take = {.link = link,
426 .out = out,
427 .rpc_id = (uint32_t)RPC_ID__Req_WifiStaGetApInfo};
428 return priv_c6link_rpc_call(link,
429 &req,
430 (uint32_t)RPC_ID__Resp_WifiStaGetApInfo,
432 &take);
433}
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
@ 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_protocol_error
Protocol-level error (e.g.
Definition ra8_err.h:429
@ 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
Secure-comparison primitives for the crypto / secure-boot paths.
void ra8_secure_memzero(void *ptr, size_t len)
Securely zero a buffer such that the write cannot be optimised away.
Definition ra8_secure.c:37