master
  1const std = @import("std");
  2const uefi = std.os.uefi;
  3const Guid = uefi.Guid;
  4const Event = uefi.Event;
  5const Handle = uefi.Handle;
  6const Status = uefi.Status;
  7const Time = uefi.Time;
  8const SimpleNetwork = uefi.protocol.SimpleNetwork;
  9const MacAddress = uefi.MacAddress;
 10const cc = uefi.cc;
 11const Error = Status.Error;
 12
 13pub const ManagedNetwork = extern struct {
 14    _get_mode_data: *const fn (*const ManagedNetwork, ?*Config, ?*SimpleNetwork) callconv(cc) Status,
 15    _configure: *const fn (*ManagedNetwork, ?*const Config) callconv(cc) Status,
 16    _mcast_ip_to_mac: *const fn (*ManagedNetwork, bool, *const anyopaque, *MacAddress) callconv(cc) Status,
 17    _groups: *const fn (*ManagedNetwork, bool, ?*const MacAddress) callconv(cc) Status,
 18    _transmit: *const fn (*ManagedNetwork, *CompletionToken) callconv(cc) Status,
 19    _receive: *const fn (*ManagedNetwork, *CompletionToken) callconv(cc) Status,
 20    _cancel: *const fn (*ManagedNetwork, ?*const CompletionToken) callconv(cc) Status,
 21    _poll: *const fn (*ManagedNetwork) callconv(cc) Status,
 22
 23    pub const GetModeDataError = uefi.UnexpectedError || error{
 24        InvalidParameter,
 25        Unsupported,
 26        NotStarted,
 27    } || Error;
 28    pub const ConfigureError = uefi.UnexpectedError || error{
 29        InvalidParameter,
 30        OutOfResources,
 31        Unsupported,
 32        DeviceError,
 33    } || Error;
 34    pub const McastIpToMacError = uefi.UnexpectedError || error{
 35        InvalidParameter,
 36        NotStarted,
 37        Unsupported,
 38        DeviceError,
 39    } || Error;
 40    pub const GroupsError = uefi.UnexpectedError || error{
 41        InvalidParameter,
 42        NotStarted,
 43        AlreadyStarted,
 44        NotFound,
 45        DeviceError,
 46        Unsupported,
 47    } || Error;
 48    pub const TransmitError = uefi.UnexpectedError || error{
 49        NotStarted,
 50        InvalidParameter,
 51        AccessDenied,
 52        OutOfResources,
 53        DeviceError,
 54        NotReady,
 55        NoMedia,
 56    };
 57    pub const ReceiveError = uefi.UnexpectedError || error{
 58        NotStarted,
 59        InvalidParameter,
 60        OutOfResources,
 61        DeviceError,
 62        AccessDenied,
 63        NotReady,
 64        NoMedia,
 65    };
 66    pub const CancelError = uefi.UnexpectedError || error{
 67        NotStarted,
 68        InvalidParameter,
 69        NotFound,
 70    };
 71    pub const PollError = uefi.UnexpectedError || error{
 72        NotStarted,
 73        DeviceError,
 74        NotReady,
 75        Timeout,
 76    };
 77
 78    pub const GetModeDataData = struct {
 79        mnp_config: Config,
 80        snp_mode: SimpleNetwork,
 81    };
 82
 83    /// Returns the operational parameters for the current MNP child driver.
 84    /// May also support returning the underlying SNP driver mode data.
 85    pub fn getModeData(self: *const ManagedNetwork) GetModeDataError!GetModeDataData {
 86        var data: GetModeDataData = undefined;
 87        switch (self._get_mode_data(self, &data.mnp_config, &data.snp_mode)) {
 88            .success => return data,
 89            else => |status| {
 90                try status.err();
 91                return uefi.unexpectedStatus(status);
 92            },
 93        }
 94    }
 95
 96    /// Sets or clears the operational parameters for the MNP child driver.
 97    pub fn configure(self: *ManagedNetwork, mnp_config_data: ?*const Config) ConfigureError!void {
 98        switch (self._configure(self, mnp_config_data)) {
 99            .success => {},
100            else => |status| {
101                try status.err();
102                return uefi.unexpectedStatus(status);
103            },
104        }
105    }
106
107    /// Translates an IP multicast address to a hardware (MAC) multicast address.
108    /// This function may be unsupported in some MNP implementations.
109    pub fn mcastIpToMac(
110        self: *ManagedNetwork,
111        ipv6flag: bool,
112        ipaddress: *const uefi.IpAddress,
113    ) McastIpToMacError!MacAddress {
114        var result: MacAddress = undefined;
115        switch (self._mcast_ip_to_mac(self, ipv6flag, ipaddress, &result)) {
116            .success => return result,
117            else => |status| {
118                try status.err();
119                return uefi.unexpectedStatus(status);
120            },
121        }
122    }
123
124    /// Enables and disables receive filters for multicast address.
125    /// This function may be unsupported in some MNP implementations.
126    pub fn groups(
127        self: *ManagedNetwork,
128        join_flag: bool,
129        mac_address: ?*const MacAddress,
130    ) GroupsError!void {
131        switch (self._groups(self, join_flag, mac_address)) {
132            .success => {},
133            else => |status| {
134                try status.err();
135                return uefi.unexpectedStatus(status);
136            },
137        }
138    }
139
140    /// Places asynchronous outgoing data packets into the transmit queue.
141    pub fn transmit(self: *ManagedNetwork, token: *CompletionToken) TransmitError!void {
142        switch (self._transmit(self, token)) {
143            .success => {},
144            .not_started => return Error.NotStarted,
145            .invalid_parameter => return Error.InvalidParameter,
146            .access_denied => return Error.AccessDenied,
147            .out_of_resources => return Error.OutOfResources,
148            .device_error => return Error.DeviceError,
149            .not_ready => return Error.NotReady,
150            .no_media => return Error.NoMedia,
151            else => |status| return uefi.unexpectedStatus(status),
152        }
153    }
154
155    /// Places an asynchronous receiving request into the receiving queue.
156    pub fn receive(self: *ManagedNetwork, token: *CompletionToken) TransmitError!void {
157        switch (self._receive(self, token)) {
158            .success => {},
159            .not_started => return Error.NotStarted,
160            .invalid_parameter => return Error.InvalidParameter,
161            .out_of_resources => return Error.OutOfResources,
162            .device_error => return Error.DeviceError,
163            .access_denied => return Error.AccessDenied,
164            .not_ready => return Error.NotReady,
165            .no_media => return Error.NoMedia,
166            else => |status| return uefi.unexpectedStatus(status),
167        }
168    }
169
170    /// Aborts an asynchronous transmit or receive request.
171    pub fn cancel(self: *ManagedNetwork, token: ?*const CompletionToken) CancelError!void {
172        switch (self._cancel(self, token)) {
173            .success => {},
174            .not_started => return Error.NotStarted,
175            .invalid_parameter => return Error.InvalidParameter,
176            .not_found => return Error.NotFound,
177            else => |status| return uefi.unexpectedStatus(status),
178        }
179    }
180
181    /// Polls for incoming data packets and processes outgoing data packets.
182    pub fn poll(self: *ManagedNetwork) PollError!void {
183        switch (self._poll(self)) {
184            .success => {},
185            .not_started => return Error.NotStarted,
186            .device_error => return Error.DeviceError,
187            .not_ready => return Error.NotReady,
188            .timeout => return Error.Timeout,
189            else => |status| return uefi.unexpectedStatus(status),
190        }
191    }
192
193    pub const guid align(8) = Guid{
194        .time_low = 0x7ab33a91,
195        .time_mid = 0xace5,
196        .time_high_and_version = 0x4326,
197        .clock_seq_high_and_reserved = 0xb5,
198        .clock_seq_low = 0x72,
199        .node = [_]u8{ 0xe7, 0xee, 0x33, 0xd3, 0x9f, 0x16 },
200    };
201
202    pub const ServiceBinding = extern struct {
203        _create_child: *const fn (*const ServiceBinding, *?Handle) callconv(cc) Status,
204        _destroy_child: *const fn (*const ServiceBinding, Handle) callconv(cc) Status,
205
206        pub fn createChild(self: *const ServiceBinding, handle: *?Handle) Status {
207            return self._create_child(self, handle);
208        }
209
210        pub fn destroyChild(self: *const ServiceBinding, handle: Handle) Status {
211            return self._destroy_child(self, handle);
212        }
213
214        pub const guid align(8) = Guid{
215            .time_low = 0xf36ff770,
216            .time_mid = 0xa7e1,
217            .time_high_and_version = 0x42cf,
218            .clock_seq_high_and_reserved = 0x9e,
219            .clock_seq_low = 0xd2,
220            .node = [_]u8{ 0x56, 0xf0, 0xf2, 0x71, 0xf4, 0x4c },
221        };
222    };
223
224    pub const Config = extern struct {
225        received_queue_timeout_value: u32,
226        transmit_queue_timeout_value: u32,
227        protocol_type_filter: u16,
228        enable_unicast_receive: bool,
229        enable_multicast_receive: bool,
230        enable_broadcast_receive: bool,
231        enable_promiscuous_receive: bool,
232        flush_queues_on_reset: bool,
233        enable_receive_timestamps: bool,
234        disable_background_polling: bool,
235    };
236
237    pub const CompletionToken = extern struct {
238        event: Event,
239        status: Status,
240        packet: extern union {
241            rx_data: *ReceiveData,
242            tx_data: *TransmitData,
243        },
244    };
245
246    pub const ReceiveData = extern struct {
247        timestamp: Time,
248        recycle_event: Event,
249        packet_length: u32,
250        header_length: u32,
251        address_length: u32,
252        data_length: u32,
253        broadcast_flag: bool,
254        multicast_flag: bool,
255        promiscuous_flag: bool,
256        protocol_type: u16,
257        destination_address: [*]u8,
258        source_address: [*]u8,
259        media_header: [*]u8,
260        packet_data: [*]u8,
261    };
262
263    pub const TransmitData = extern struct {
264        destination_address: ?*MacAddress,
265        source_address: ?*MacAddress,
266        protocol_type: u16,
267        data_length: u32,
268        header_length: u16,
269        fragment_count: u16,
270
271        pub fn getFragments(self: *TransmitData) []Fragment {
272            return @as([*]Fragment, @ptrCast(@alignCast(@as([*]u8, @ptrCast(self)) + @sizeOf(TransmitData))))[0..self.fragment_count];
273        }
274    };
275
276    pub const Fragment = extern struct {
277        fragment_length: u32,
278        fragment_buffer: [*]u8,
279    };
280};