CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
host_api_wifi.cpp
Go to the documentation of this file.
1
15
19
20#include <cstring>
21
22extern "C" void plg_log_warn(const char* msg);
23
24namespace {
25
28}
29
30} // namespace
31
32extern "C" {
33
34int host_wifi_request(uint32_t /*timeout_ms*/)
35{
37}
38
44
49
50int host_wifi_ssid(char* out, size_t out_size)
51{
52 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
53 auto* w = wifi();
54 if (!w) return HOST_ERR_NOT_FOUND;
55 // The IWifiController contract does not guarantee a non-null SSID, so never
56 // hand a NULL source to strncpy.
57 const char* ssid = w->getCurrentSsid();
58 std::strncpy(out, ssid ? ssid : "", out_size - 1);
59 out[out_size - 1] = '\0';
60 return HOST_OK;
61}
62
63int host_wifi_ip(char* out, size_t out_size)
64{
65 if (!out || out_size == 0) return HOST_ERR_INVALID_ARG;
66 auto* w = wifi();
67 if (!w) return HOST_ERR_NOT_FOUND;
68 return w->getIpAddress(out, out_size) ? HOST_OK : HOST_ERR_GENERIC;
69}
70
71int8_t host_wifi_rssi(void)
72{
73 auto* w = wifi();
74 return w ? w->getRssi() : 0;
75}
76
77int host_wifi_mac(uint8_t* out)
78{
79 if (!out) return HOST_ERR_INVALID_ARG;
80 auto* w = wifi();
81 if (!w) return HOST_ERR_NOT_FOUND;
82 return w->getMacAddress(out) ? HOST_OK : HOST_ERR_GENERIC;
83}
84
86{
87 auto* w = wifi();
88 if (!w) return HOST_ERR_NOT_FOUND;
89 return w->startScan() ? HOST_OK : HOST_ERR_GENERIC;
90}
91
93{
94 auto* w = wifi();
95 return w ? w->isScanComplete() : false;
96}
97
99{
100 if (!out || !count) return HOST_ERR_INVALID_ARG;
101 auto* w = wifi();
102 if (!w) return HOST_ERR_NOT_FOUND;
103 constexpr uint8_t MAX_SCAN = cdc::hal::IWifiController::MAX_SCAN_RESULTS;
104 uint8_t cap = (*count > MAX_SCAN) ? MAX_SCAN : static_cast<uint8_t>(*count);
105 cdc::hal::WifiScanResult tmp[MAX_SCAN];
106 uint8_t n = w->getScanResults(tmp, cap);
107 for (uint8_t i = 0; i < n; ++i) {
108 std::memcpy(out[i].ssid, tmp[i].ssid, sizeof(out[i].ssid));
109 std::memcpy(out[i].bssid, tmp[i].bssid, sizeof(out[i].bssid));
110 out[i].rssi = tmp[i].rssi;
111 out[i].channel = tmp[i].channel;
112 out[i].auth_mode = static_cast<uint8_t>(tmp[i].security);
113 }
114 *count = n;
115 return HOST_OK;
116}
117
118} // extern "C"
static constexpr uint8_t MAX_SCAN_RESULTS
void release()
Releases a previously acquired WiFi hold.
bool acquire()
Acquires a hold on the WiFi connection for a plugin/host caller.
static WifiHandlers & instance()
Returns singleton Wi-Fi handlers instance.
bool isConnected() const
Returns whether Wi-Fi is currently connected.
bool host_wifi_scan_done(void)
True when the scan started by host_wifi_start_scan has finished.
int host_wifi_scan_results(wifi_scan_result_t *out, size_t *count)
Read results from the last completed scan.
int host_wifi_request(uint32_t)
Request the shared WiFi radio and wait up to timeout_ms for join.
int host_wifi_ip(char *out, size_t out_size)
Copy the current IPv4 address as dotted decimal into out.
bool host_wifi_is_connected(void)
True when WiFi STA is associated and has an IP.
int host_wifi_release(void)
Release the WiFi radio held by this plugin.
int8_t host_wifi_rssi(void)
Current AP signal strength in dBm.
int host_wifi_start_scan(void)
Start an asynchronous WiFi scan.
int host_wifi_ssid(char *out, size_t out_size)
Copy the currently joined SSID into out.
CDC Badge OS plugin host API - canonical C ABI contract.
#define HOST_OK
Definition host_api.h:37
#define HOST_ERR_INVALID_ARG
Definition host_api.h:39
#define HOST_ERR_TIMEOUT
Definition host_api.h:42
#define HOST_ERR_NOT_FOUND
Definition host_api.h:41
#define HOST_ERR_GENERIC
Definition host_api.h:38
void plg_log_warn(const char *msg)
int host_wifi_mac(uint8_t *out)
IWifiController * getWifiControllerInstance()
Returns the singleton Wi-Fi controller service instance.