Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions drivers/src/wireless/cyw43439/ioctl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const response_poll_interval = 10;
// SDPCM (Serial Data Packet Communication Module) header
// SDIO/SPI Bus Layer (SDPCM) header
// ref: https://iosoft.blog/2022/12/06/picowi_part3/
const SDP_Header = extern struct {
pub const SDP_Header = extern struct {
len: u16,
not_len: u16,
/// Rx/Tx sequence number
Expand Down Expand Up @@ -71,7 +71,7 @@ const SDP_Header = extern struct {
}
};

const CDC_Header = extern struct {
pub const CDC_Header = extern struct {
cmd: Cmd,
outlen: u16,
inlen: u16 = 0,
Expand Down Expand Up @@ -104,7 +104,7 @@ pub const Cmd = enum(u32) {
_,
};

const BDC_Header = extern struct {
pub const BDC_Header = extern struct {
flags: u8,
priority: u8,
flags2: u8,
Expand Down Expand Up @@ -428,7 +428,7 @@ pub fn hex_to_bytes(comptime hex: []const u8) [hex.len / 2]u8 {
return res;
}

const EventPacket = extern struct {
pub const EventPacket = extern struct {
eth: EthernetHeader,
hdr: EventHeader,
msg: EventMessage,
Expand Down
204 changes: 200 additions & 4 deletions drivers/src/wireless/cyw43439/wifi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ fn response_poll(self: *Self, cmd: ioctl.Cmd, data: ?[]u8) !usize {
return error.Cyw43InvalidCommandStatus;
}
},
else => self.log_response(&rsp),
.event => self.handle_event(&rsp),
.data => self.log_response(&rsp),
}
}
log.err("ioctl: missing response in response_poll", .{});
Expand All @@ -367,6 +368,9 @@ fn response_poll(self: *Self, cmd: ioctl.Cmd, data: ?[]u8) !usize {
pub const JoinOptions = struct {
security: Security = .wpa2_psk,
wait_ms: u32 = 30 * 1000,
/// Only enable when retrying the same network and credentials. An automatic
/// reassociation completed during configuration can satisfy this request.
preserve_auto_reconnect: bool = false,

pub const Security = enum {
open,
Expand All @@ -378,6 +382,8 @@ pub const JoinOptions = struct {

pub fn join(self: *Self, ssid: []const u8, pwd: []const u8, opt: JoinOptions) !JoinPoller {
self.err = null;
self.event_log = .{};
self.join_state = .none;

var buf: [64]u8 = @splat(0); // space for 10 addresses
// Enable multicast
Expand Down Expand Up @@ -433,13 +439,29 @@ pub fn join(self: *Self, ssid: []const u8, pwd: []const u8, opt: JoinOptions) !J
.open => &.{ 0, 0, 0, 0 },
});

try self.set_cmd(.set_ssid, ioctl.encode_ssid(&buf, ssid));
// The radio can automatically reassociate while command replies are being
// polled. When retrying the same credentials, keep that authenticated link
// instead of clearing its events and aborting it with another set_ssid.
if (opt.preserve_auto_reconnect and self.event_log.authenticated_reassociation()) {
// Satisfy join_state() without issuing another set_ssid command.
self.event_log.set_ssid = .success;
self.err = null;
self.join_state = .joined;
log.debug("preserving authenticated automatic reassociation", .{});
return .{ .parent = self };
}

// Configuration may have consumed events from the previous association.
// Start the new attempt before set_ssid: its response can be interleaved
// with authentication/link events, including a completed join.
self.err = null;
self.event_log = .{};
self.join_state = .joining;
if (opt.security == .open) {
self.event_log.psk_sup = .success;
}

self.join_state = .joining;
try self.set_cmd(.set_ssid, ioctl.encode_ssid(&buf, ssid));
return .{ .parent = self };
}

Expand Down Expand Up @@ -535,7 +557,8 @@ fn handle_event(self: *Self, rsp: *ioctl.Response) void {
);
switch (evt.event_type) {
.link => {
event_log.link = state(evt.status == .success and evt.flags & 1 > 0);
const link_up: u16 = 1;
event_log.link = state(evt.status == .success and evt.flags & link_up > 0);
},
.psk_sup => {
event_log.psk_sup =
Expand Down Expand Up @@ -905,6 +928,13 @@ const EventLog = packed struct {
assoc: EventState = .none,
join: EventState = .none,

// Automatic reassociation has no set_ssid completion. Authentication, link
// and key setup must succeed, with either association or join confirmed.
fn authenticated_reassociation(e: EventLog) bool {
return e.auth == .success and e.link == .success and e.psk_sup == .success and
(e.assoc == .success or e.join == .success);
}

pub fn err(e: EventLog) !void {
if (e.auth == .fail) return error.Cyw43Authentication;
if (e.link == .fail) return error.Cyw43LinkDown;
Expand Down Expand Up @@ -933,6 +963,24 @@ pub const JoinState = enum {

const testing = std.testing;

test "authenticated reassociation requires a secure associated link" {
var e: EventLog = .{ .auth = .success, .link = .success, .psk_sup = .success };
try testing.expect(!e.authenticated_reassociation());
e.assoc = .success;
try testing.expect(e.authenticated_reassociation());
e.assoc = .none;
e.join = .success;
try testing.expect(e.authenticated_reassociation());
e.auth = .none;
try testing.expect(!e.authenticated_reassociation());
e.auth = .success;
e.link = .fail;
try testing.expect(!e.authenticated_reassociation());
e.link = .success;
e.psk_sup = .fail;
try testing.expect(!e.authenticated_reassociation());
}

test "events to join_state" {
var e: EventLog = .{};
try testing.expectEqual(.none, e.join_state());
Expand Down Expand Up @@ -1071,3 +1119,151 @@ test "receive capacity rejects oversized packets and polls full Ethernet frames"
try wifi.poll();
try testing.expectEqual(@as(usize, 1), mock.packet_reads);
}

// Simulated radio replies for the join tests.
const JoinTestPackets = struct {
const sdpcm_len = @sizeOf(ioctl.SDP_Header);
const control_len = sdpcm_len + @sizeOf(ioctl.CDC_Header);
// Exercise padding before and after the BDC header.
const bus_padding = 2;
const bdc_padding_words = 1;
const event_len = sdpcm_len + bus_padding + @sizeOf(ioctl.BDC_Header) +
bdc_padding_words * @sizeOf(u32) + @sizeOf(ioctl.EventPacket);

fn status(len: u16) u32 {
var value = mem.zeroes(BusStatus);
value.f2_packet_available = true;
value.packet_length = @intCast(len);
return @bitCast(value);
}

fn reply(words: []u32, typ: ?ioctl.EventType, command: ioctl.Cmd, id: u16) void {
const len: u16 = if (typ != null) event_len else control_len;
@memset(words, 0);
var writer: std.Io.Writer = .fixed(mem.sliceAsBytes(words));
const sdp: ioctl.SDP_Header = .{
.len = len,
.not_len = ~len,
.seq = 0,
.cf = .{ .chan = if (typ != null) .event else .control },
.hdrlen = sdpcm_len + if (typ != null) @as(u8, bus_padding) else 0,
};
writer.writeStruct(sdp, .little) catch unreachable;
if (typ) |event_type| {
writer.splatByteAll(0, bus_padding) catch unreachable;
const bdc: ioctl.BDC_Header = .{
.flags = 0x20, // BDC version 2.
.priority = 0,
.flags2 = 0,
.offset = bdc_padding_words,
};
writer.writeStruct(bdc, .little) catch unreachable;
writer.splatByteAll(0, bdc_padding_words * @sizeOf(u32)) catch unreachable;
var event = mem.zeroes(ioctl.EventPacket);
event.eth.ether_type = 0x886c; // Broadcom event.
event.hdr.oui = .{ 0x00, 0x10, 0x18 }; // Broadcom vendor ID.
if (event_type == .link) {
const link_up: u16 = 1;
event.msg.flags = link_up;
}
event.msg.event_type = event_type;
event.msg.status = if (event_type == .psk_sup) .unsolicited else .success;
writer.writeStruct(event, .big) catch unreachable;
} else {
const cdc: ioctl.CDC_Header = .{
.cmd = command,
.outlen = 0,
.id = id,
};
writer.writeStruct(cdc, .little) catch unreachable;
}
}
};

test "control response polling preserves interleaved reconnect events" {
const request_id = 7;
const Mock = struct {
index: usize = 0,
const events = [_]ioctl.EventType{ .auth, .reassoc, .link, .psk_sup, .join, .set_ssid };
fn read(ptr: *anyopaque, words: []u32) void {
const self: *@This() = @ptrCast(@alignCast(ptr));
const is_event = self.index < events.len;
const len: u16 = if (is_event) JoinTestPackets.event_len else JoinTestPackets.control_len;
if (words.len == 2) { // Bus status, followed by trailing SPI status.
words[0] = JoinTestPackets.status(len);
words[1] = 0;
return;
}
JoinTestPackets.reply(words, if (is_event) events[self.index] else null, .set_ssid, request_id);
self.index += 1;
}
fn write(_: *anyopaque, _: []u32) void {
unreachable;
}
fn irq(_: *anyopaque) void {}
fn sleep(_: u32) void {}
};
var mock: Mock = .{};
var bus: Bus = .{
.spi = .{ .ptr = &mock, .vtable = &.{ .read = Mock.read, .write = Mock.write, .irq_cleared = Mock.irq } },
.sleep_ms = Mock.sleep,
};
var wifi: Self = .{ .bus = &bus, .join_state = .joining, .request_id = request_id };
try testing.expectEqual(@as(usize, 0), try wifi.response_poll(.set_ssid, null));
try testing.expectEqual(JoinState.joined, wifi.join_state);
try testing.expectEqual(EventLog.EventState.success, wifi.event_log.auth);
try testing.expectEqual(EventLog.EventState.success, wifi.event_log.assoc);
try testing.expectEqual(EventLog.EventState.success, wifi.event_log.psk_sup);
}

test "join preserves automatic reassociation only when explicitly requested" {
const Mock = struct {
cmd: ioctl.Cmd = .set_var,
id: u16 = 0,
event_index: usize = 0,
set_ssid_count: usize = 0,
const events = [_]ioctl.EventType{ .auth, .reassoc, .link, .join, .psk_sup };
fn write(ptr: *anyopaque, words: []u32) void {
const self: *@This() = @ptrCast(@alignCast(ptr));
const packet = mem.sliceAsBytes(words)[@sizeOf(u32)..]; // skip SPI command word
var reader: std.Io.Reader = .fixed(packet);
const sdp = reader.takeStruct(ioctl.SDP_Header, .little) catch unreachable;
_ = reader.discard(@fromBackingInt(sdp.hdrlen - @sizeOf(ioctl.SDP_Header))) catch unreachable;
const cdc = reader.takeStruct(ioctl.CDC_Header, .little) catch unreachable;
self.cmd = cdc.cmd;
self.id = cdc.id;
if (self.cmd == .set_ssid) self.set_ssid_count += 1;
}
fn read(ptr: *anyopaque, words: []u32) void {
const self: *@This() = @ptrCast(@alignCast(ptr));
const is_event = self.cmd == .set_wpa_auth and self.event_index < events.len;
const len: u16 = if (is_event) JoinTestPackets.event_len else JoinTestPackets.control_len;
if (words.len == 2) { // Bus status, followed by trailing SPI status.
words[0] = JoinTestPackets.status(len);
words[1] = 0;
return;
}
JoinTestPackets.reply(words, if (is_event) events[self.event_index] else null, self.cmd, self.id);
if (is_event) self.event_index += 1;
}
fn irq(_: *anyopaque) void {}
fn sleep(_: u32) void {}
};
for ([_]bool{ true, false }) |preserve| {
var mock: Mock = .{};
var bus: Bus = .{
.spi = .{ .ptr = &mock, .vtable = &.{ .read = Mock.read, .write = Mock.write, .irq_cleared = Mock.irq } },
.sleep_ms = Mock.sleep,
};
var wifi: Self = .{ .bus = &bus };
var poller = try wifi.join("test-network", "test-password", .{ .preserve_auto_reconnect = preserve });
if (preserve) {
try testing.expectEqual(JoinState.joined, wifi.join_state);
try testing.expectEqual(@as(usize, 0), mock.set_ssid_count);
try testing.expect(!try poller.poll());
} else {
try testing.expectEqual(JoinState.joining, wifi.join_state);
try testing.expectEqual(@as(usize, 1), mock.set_ssid_count);
}
}
}
Loading