internal static IEnumerable <ProfilePack> EnumerateProfiles(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList.Select(x => new InterfaceInfo(x)))
                {
                    var interfaceIsConnected = (interfaceInfo.State == InterfaceState.Connected);

                    var interfaceIsRadioOn = interfaceIsConnected || IsInterfaceRadioOn(container.Content, interfaceInfo.Id);

                    var availableNetworkList = Base.GetAvailableNetworkList(container.Content.Handle, interfaceInfo.Id)
                                               .Where(x => !string.IsNullOrWhiteSpace(x.strProfileName))
                                               .ToArray();

                    var connection = Base.GetConnectionAttributes(container.Content.Handle, interfaceInfo.Id);

                    var profileInfoList = Base.GetProfileInfoList(container.Content.Handle, interfaceInfo.Id);

                    int position = 0;

                    foreach (var profileInfo in profileInfoList)
                    {
                        var availableNetwork   = availableNetworkList.FirstOrDefault(x => string.Equals(x.strProfileName, profileInfo.strProfileName, StringComparison.Ordinal));
                        var signalQuality      = (int)availableNetwork.wlanSignalQuality;
                        var profileIsConnected = interfaceIsConnected && string.Equals(connection.strProfileName, profileInfo.strProfileName, StringComparison.Ordinal);

                        var profileXml = Base.GetProfile(container.Content.Handle, interfaceInfo.Id, profileInfo.strProfileName, out uint profileTypeFlag);
                        if (string.IsNullOrWhiteSpace(profileXml))
                        {
                            continue;
                        }

                        if (!ProfileTypeConverter.TryConvert(profileTypeFlag, out ProfileType profileType))
                        {
                            continue;
                        }

                        //Debug.WriteLine("Interface: {0}, Profile: {1}, Position: {2}, RadioOn: {3}, Signal: {4}, Connected: {5}",
                        //	interfaceInfo.Description,
                        //	profileInfo.strProfileName,
                        //	position,
                        //	interfaceIsRadioOn,
                        //	signalQuality,
                        //	profileIsConnected);

                        yield return(new ProfilePack(
                                         name: profileInfo.strProfileName,
                                         interfaceInfo: interfaceInfo,
                                         profileType: profileType,
                                         profileXml: profileXml,
                                         position: position++,
                                         isRadioOn: interfaceIsRadioOn,
                                         signalQuality: signalQuality,
                                         isConnected: profileIsConnected));
                    }
                }
            }
        }
        internal static IEnumerable <AvailableNetworkPack> EnumerateAvailableNetworks(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList.Select(x => new InterfaceInfo(x)))
                {
                    var availableNetworkList = Base.GetAvailableNetworkList(container.Content.Handle, interfaceInfo.Id);

                    foreach (var availableNetwork in availableNetworkList)
                    {
                        if (!BssTypeConverter.TryConvert(availableNetwork.dot11BssType, out BssType bssType))
                        {
                            continue;
                        }

                        //Debug.WriteLine("Interface: {0}, SSID: {1}, Signal: {2}, Security: {3}",
                        //	interfaceInfo.Description,
                        //	availableNetwork.dot11Ssid,
                        //	availableNetwork.wlanSignalQuality,
                        //	availableNetwork.bSecurityEnabled);

                        yield return(new AvailableNetworkPack(
                                         interfaceInfo: interfaceInfo,
                                         ssid: new NetworkIdentifier(availableNetwork.dot11Ssid.ToBytes(), availableNetwork.dot11Ssid.ToString()),
                                         bssType: bssType,
                                         signalQuality: (int)availableNetwork.wlanSignalQuality,
                                         isSecurityEnabled: availableNetwork.bSecurityEnabled,
                                         profileName: availableNetwork.strProfileName));
                    }
                }
            }
        }
        internal static IEnumerable <AvailableNetworkPack> EnumerateAvailableNetworks(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in EnumerateInterfaces(container.Content))
            {
                foreach (var availableNetwork in Base.GetAvailableNetworkList(container.Content.Handle, interfaceInfo.Id))
                {
                    if (!BssTypeConverter.TryConvert(availableNetwork.dot11BssType, out BssType bssType))
                    {
                        continue;
                    }

                    if (!AuthenticationAlgorithmConverter.TryConvert(availableNetwork.dot11DefaultAuthAlgorithm, out AuthenticationAlgorithm authenticationAlgorithm))
                    {
                        continue;
                    }

                    if (!CipherAlgorithmConverter.TryConvert(availableNetwork.dot11DefaultCipherAlgorithm, out CipherAlgorithm cipherAlgorithm))
                    {
                        continue;
                    }

                    yield return(new AvailableNetworkPack(
                                     interfaceInfo: interfaceInfo,
                                     ssid: new NetworkIdentifier(availableNetwork.dot11Ssid),
                                     bssType: bssType,
                                     signalQuality: (int)availableNetwork.wlanSignalQuality,
                                     isSecurityEnabled: availableNetwork.bSecurityEnabled,
                                     profileName: availableNetwork.strProfileName,
                                     authenticationAlgorithm: authenticationAlgorithm,
                                     cipherAlgorithm: cipherAlgorithm));
                }
            }
        }
        internal static IEnumerable <InterfaceInfo> EnumerateInterfaces(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            return(Base.GetInterfaceInfoList(container.Content.Handle)
                   .Select(x => new InterfaceInfo(x)));
        }
        internal static IEnumerable <NetworkIdentifier> EnumerateConnectedNetworkSsids(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList)
                {
                    var connection = Base.GetConnectionAttributes(container.Content.Handle, interfaceInfo.InterfaceGuid);
                    if (connection.isState != WLAN_INTERFACE_STATE.wlan_interface_state_connected)
                    {
                        continue;
                    }

                    var association = connection.wlanAssociationAttributes;

                    //Debug.WriteLine("Interface: {0}, SSID: {1}, BSSID: {2}, Signal: {3}",
                    //	interfaceInfo.strInterfaceDescription,
                    //	association.dot11Ssid,
                    //	association.dot11Bssid,
                    //	association.wlanSignalQuality);

                    yield return(new NetworkIdentifier(association.dot11Ssid.ToBytes(), association.dot11Ssid.ToString()));
                }
            }
        }
        internal static IEnumerable <ProfilePack> EnumerateProfiles(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in EnumerateInterfaces(container.Content))
            {
                int position = 0;

                foreach (var profileInfo in Base.GetProfileInfoList(container.Content.Handle, interfaceInfo.Id))
                {
                    var profileXml = Base.GetProfile(container.Content.Handle, interfaceInfo.Id, profileInfo.strProfileName, out uint profileTypeFlag);
                    if (string.IsNullOrWhiteSpace(profileXml))
                    {
                        continue;
                    }

                    if (!ProfileTypeConverter.TryConvert(profileTypeFlag, out ProfileType profileType))
                    {
                        continue;
                    }

                    yield return(new ProfilePack(
                                     name: profileInfo.strProfileName,
                                     interfaceInfo: interfaceInfo,
                                     profileType: profileType,
                                     profileXml: profileXml,
                                     position: position++));
                }
            }
        }
        private static IEnumerable <RadioSet> EnumerateInterfaceRadioSets(Base.WlanClient client, Guid interfaceId)
        {
            var capability = Base.GetInterfaceCapability(client.Handle, interfaceId);
            var states     = Base.GetPhyRadioStates(client.Handle, interfaceId);         // The underlying collection is array.

            if ((capability.interfaceType == WLAN_INTERFACE_TYPE.wlan_interface_type_invalid) ||
                (capability.dwNumberOfSupportedPhys != states.Count()))
            {
                return(Enumerable.Empty <RadioSet>());
            }

            return(Enumerable.Zip(
                       capability.dot11PhyTypes,
                       states.OrderBy(x => x.dwPhyIndex),
                       (x, y) => new RadioSet(
                           type: PhyTypeConverter.Convert(x),
                           hardwareOn: ConvertToNullableBoolean(y.dot11HardwareRadioState),
                           softwareOn: ConvertToNullableBoolean(y.dot11SoftwareRadioState))));

            bool?ConvertToNullableBoolean(DOT11_RADIO_STATE source)
            {
                switch (source)
                {
                case DOT11_RADIO_STATE.dot11_radio_state_on:
                    return(true);

                case DOT11_RADIO_STATE.dot11_radio_state_off:
                    return(false);

                default:
                    return(null);
                }
            }
        }
        internal static bool ConnectNetwork(Base.WlanClient client, Guid interfaceId, string profileName, BssType bssType, PhysicalAddress bssid = null)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            if (bssid is not null)
            {
                var dot11MacAddress = new DOT11_MAC_ADDRESS {
                    ucDot11MacAddress = bssid.GetAddressBytes()
                };
                return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType), dot11MacAddress));
            }
            else
            {
                return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType)));
            }
        }
        internal static InterfaceRadio GetInterfaceRadio(Base.WlanClient client, Guid interfaceId)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var capability = Base.GetInterfaceCapability(container.Content.Handle, interfaceId);
                var states     = Base.GetPhyRadioStates(container.Content.Handle, interfaceId);             // The underlying collection is array.

                if ((capability.interfaceType == WLAN_INTERFACE_TYPE.wlan_interface_type_invalid) ||
                    (capability.dwNumberOfSupportedPhys != states.Count()))
                {
                    return(null);
                }

                var radioSets = Enumerable.Zip(
                    capability.dot11PhyTypes,
                    states.OrderBy(x => x.dwPhyIndex),
                    (x, y) => new RadioSet(
                        type: PhyTypeConverter.Convert(x),
                        softwareOn: ConvertToNullableBoolean(y.dot11SoftwareRadioState),
                        hardwareOn: ConvertToNullableBoolean(y.dot11HardwareRadioState)));

                return(new InterfaceRadio(
                           id: interfaceId,
                           radioSets: radioSets));
            }
        }
        internal static bool DisconnectNetwork(Base.WlanClient client, Guid interfaceId)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            return(Base.Disconnect(container.Content.Handle, interfaceId));
        }
Exemple #11
0
        internal static bool GetInterfaceAutoConfig(Base.WlanClient client, Guid interfaceId)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                return(Base.GetAutoConfig(container.Content.Handle, interfaceId).GetValueOrDefault());
            }
        }
        internal static IEnumerable <string> EnumerateProfileNames(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in Base.GetInterfaceInfoList(container.Content.Handle))
            {
                foreach (var profileInfo in Base.GetProfileInfoList(container.Content.Handle, interfaceInfo.InterfaceGuid))
                {
                    yield return(profileInfo.strProfileName);
                }
            }
        }
        internal static IEnumerable <NetworkIdentifier> EnumerateAvailableNetworkSsids(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in Base.GetInterfaceInfoList(container.Content.Handle))
            {
                foreach (var availableNetwork in Base.GetAvailableNetworkList(container.Content.Handle, interfaceInfo.InterfaceGuid))
                {
                    yield return(new NetworkIdentifier(availableNetwork.dot11Ssid));
                }
            }
        }
        internal static IEnumerable <InterfaceInfoEx> EnumerateInterfacesEx(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList)
                {
                    var connection = Base.GetConnectionAttributes(container.Content.Handle, interfaceInfo.InterfaceGuid);

                    yield return(new InterfaceInfoEx(interfaceInfo, connection));
                }
            }
        }
        internal static IEnumerable <BssNetworkPack> EnumerateBssNetworks(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList.Select(x => new InterfaceInfo(container.Content.Handle, x)))
                {
                    var networkList = EnumerateAvailableNetworks(client);

                    var networkBssEntryList = Base.GetNetworkBssEntryList(container.Content.Handle, interfaceInfo.Id);

                    foreach (var networkBssEntry in networkBssEntryList)
                    {
                        if (!BssTypeConverter.TryConvert(networkBssEntry.dot11BssType, out BssType bssType))
                        {
                            continue;
                        }

                        //Debug.WriteLine("Interface: {0}, SSID: {1}, BSSID: {2}, Signal: {3} Link: {4}, Frequency: {5}",
                        //	interfaceInfo.Description,
                        //	networkBssEntry.dot11Ssid,
                        //	networkBssEntry.dot11Bssid,
                        //	networkBssEntry.lRssi,
                        //	networkBssEntry.uLinkQuality,
                        //	networkBssEntry.ulChCenterFrequency);
                        var network = networkList.Where(o => o.Ssid.ToString() == new NetworkIdentifier(networkBssEntry.dot11Ssid.ToBytes(), networkBssEntry.dot11Ssid.ToString(), networkBssEntry.dot11Ssid.uSSIDLength).ToString());

                        //this program work get available network first and get bssnetwork list.
                        //so if can not found network. just skip the bss network.
                        //it just happen if you don't have a luck.
                        if (!network.Any())
                        {
                            continue;
                        }

                        yield return(new BssNetworkPack(
                                         interfaceInfo: interfaceInfo,
                                         network: network.First(),
                                         ssid: new NetworkIdentifier(networkBssEntry.dot11Ssid.ToBytes(), networkBssEntry.dot11Ssid.ToString(), networkBssEntry.dot11Ssid.uSSIDLength),
                                         bssType: bssType,
                                         bssid: new NetworkIdentifier(networkBssEntry.dot11Bssid.ToBytes(), networkBssEntry.dot11Bssid.ToString(), networkBssEntry.dot11Ssid.uSSIDLength),
                                         signalStrength: networkBssEntry.lRssi,
                                         linkQuality: (int)networkBssEntry.uLinkQuality,
                                         frequency: (int)networkBssEntry.ulChCenterFrequency,
                                         channel: DetectChannel(networkBssEntry.ulChCenterFrequency)));
                    }
                }
            }
        }
        private static bool IsInterfaceRadioOn(Base.WlanClient client, Guid interfaceId)
        {
            var states = Base.GetPhyRadioStates(client.Handle, interfaceId);

            if (!states.Any())
            {
                return(false);
            }

            var hardwareOn = ConvertToNullableBoolean(states.First().dot11HardwareRadioState);
            var softwareOn = ConvertToNullableBoolean(states.First().dot11SoftwareRadioState);

            return(hardwareOn.GetValueOrDefault() &&
                   softwareOn.GetValueOrDefault());
        }
        internal static bool DeleteProfile(Base.WlanClient client, Guid interfaceId, string profileName)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            return(Base.DeleteProfile(container.Content.Handle, interfaceId, profileName));
        }
        internal static IEnumerable <BssNetworkPack> EnumerateBssNetworks(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in EnumerateInterfaces(container.Content))
            {
                foreach (var networkBssEntry in Base.GetNetworkBssEntryList(container.Content.Handle, interfaceInfo.Id))
                {
                    if (TryConvertBssNetwork(interfaceInfo, networkBssEntry, out BssNetworkPack bssNetwork))
                    {
                        yield return(bssNetwork);
                    }
                }
            }
        }
Exemple #19
0
        internal static async Task <IEnumerable <Guid> > ScanNetworksAsync(Base.WlanNotificationClient client, TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (timeout <= TimeSpan.Zero)
            {
                throw new ArgumentException(nameof(timeout));
            }

            using (var container = new DisposableContainer <Base.WlanNotificationClient>(client))
            {
                var interfaceIds = Base.GetInterfaceInfoList(container.Content.Handle)
                                   .Select(x => x.InterfaceGuid)
                                   .ToArray();

                var tcs     = new TaskCompletionSource <bool>();
                var counter = new ScanCounter(() => Task.Run(() => tcs.TrySetResult(true)), interfaceIds);

                container.Content.NotificationReceived += (sender, data) =>
                {
                    switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode)
                    {
                    case WLAN_NOTIFICATION_ACM.wlan_notification_acm_scan_complete:
                        counter.SetSuccess(data.InterfaceGuid);
                        break;

                    case WLAN_NOTIFICATION_ACM.wlan_notification_acm_scan_fail:
                        counter.SetFailure(data.InterfaceGuid);
                        break;
                    }
                };

                foreach (var interfaceId in interfaceIds)
                {
                    var result = Base.Scan(container.Content.Handle, interfaceId);
                    if (!result)
                    {
                        counter.SetFailure(interfaceId);
                    }
                }

                using (cancellationToken.Register(() => tcs.TrySetCanceled()))
                {
                    var scanTask = tcs.Task;
                    await Task.WhenAny(scanTask, Task.Delay(timeout, cancellationToken));

                    return(counter.Results);
                }
            }
        }
        internal static bool TurnInterfaceRadio(Base.WlanClient client, Guid interfaceId, DOT11_RADIO_STATE radioState)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var phyRadioState = new WLAN_PHY_RADIO_STATE {
                    dot11SoftwareRadioState = radioState,
                };

                return(Base.SetPhyRadioState(container.Content.Handle, interfaceId, phyRadioState));
            }
        }
Exemple #21
0
        internal static async Task <bool> DisconnectNetworkAsync(Base.WlanNotificationClient client, Guid interfaceId, TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            if (timeout <= TimeSpan.Zero)
            {
                throw new ArgumentException(nameof(timeout));
            }

            using (var container = new DisposableContainer <Base.WlanNotificationClient>(client))
            {
                var tcs = new TaskCompletionSource <bool>();

                container.Content.NotificationReceived += (sender, data) =>
                {
                    if (data.InterfaceGuid != interfaceId)
                    {
                        return;
                    }

                    switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode)
                    {
                    case WLAN_NOTIFICATION_ACM.wlan_notification_acm_disconnected:
                        Task.Run(() => tcs.TrySetResult(true));
                        break;
                    }
                };

                var result = Base.Disconnect(container.Content.Handle, interfaceId);
                if (!result)
                {
                    tcs.SetResult(false);
                }

                using (cancellationToken.Register(() => tcs.TrySetCanceled()))
                {
                    var disconnectTask = tcs.Task;
                    var completedTask  = await Task.WhenAny(disconnectTask, Task.Delay(timeout, cancellationToken));

                    return((completedTask == disconnectTask) && disconnectTask.IsCompleted && disconnectTask.Result);
                }
            }
        }
        internal static bool SetProfile(Base.WlanClient client, Guid interfaceId, ProfileType profileType, string profileXml, string profileSecurity, bool overwrite)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileXml))
            {
                throw new ArgumentNullException(nameof(profileXml));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            var profileTypeFlag = ProfileTypeConverter.ConvertBack(profileType);

            return(Base.SetProfile(container.Content.Handle, interfaceId, profileTypeFlag, profileXml, profileSecurity, overwrite));
        }
        internal static bool ConnectNetwork(Base.WlanClient client, Guid interfaceId, string profileName, BssType bssType)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType)));
            }
        }
        internal static IEnumerable <NetworkIdentifier> EnumerateConnectedNetworkSsids(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in Base.GetInterfaceInfoList(container.Content.Handle))
            {
                var connection = Base.GetConnectionAttributes(container.Content.Handle, interfaceInfo.InterfaceGuid);
                if (connection.isState != WLAN_INTERFACE_STATE.wlan_interface_state_connected)
                {
                    continue;
                }

                var association = connection.wlanAssociationAttributes;

                yield return(new NetworkIdentifier(association.dot11Ssid));
            }
        }
        internal static bool SetProfileEapXmlUserData(Base.WlanClient client, Guid interfaceId, string profileName, EapXmlType eapXmlType, string userDataXml)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            if (string.IsNullOrWhiteSpace(userDataXml))
            {
                throw new ArgumentNullException(nameof(userDataXml));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            var eapXmlTypeFlag = EapXmlTypeConverter.ConvertBack(eapXmlType);

            return(Base.SetProfileEapXmlUserData(container.Content.Handle, interfaceId, profileName, eapXmlTypeFlag, userDataXml));
        }
        internal static bool SetProfilePosition(Base.WlanClient client, Guid interfaceId, string profileName, int position)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }
            if (position < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(position), "The position must not be negative.");
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            return(Base.SetProfilePosition(container.Content.Handle, interfaceId, profileName, (uint)position));
        }
        private static IEnumerable <RadioSet> EnumerateInterfaceRadioSets(Base.WlanClient client, Guid interfaceId)
        {
            var capability = Base.GetInterfaceCapability(client.Handle, interfaceId);
            var states     = Base.GetPhyRadioStates(client.Handle, interfaceId);         // The underlying collection is array.

            if ((capability.interfaceType == WLAN_INTERFACE_TYPE.wlan_interface_type_invalid) ||
                (capability.dwNumberOfSupportedPhys != states.Count()) ||
                (capability.dot11PhyTypes?.Any() is not true))                 // This value may be null.
            {
                return(Enumerable.Empty <RadioSet>());
            }

            return(Enumerable.Zip(
                       capability.dot11PhyTypes,
                       states.OrderBy(x => x.dwPhyIndex),
                       (x, y) => new RadioSet(
                           type: PhyTypeConverter.Convert(x),
                           hardwareOn: ConvertToNullableBoolean(y.dot11HardwareRadioState),
                           softwareOn: ConvertToNullableBoolean(y.dot11SoftwareRadioState))));
Exemple #28
0
        internal static IEnumerable <ProfileRadioPack> EnumerateProfileRadios(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                foreach (var interfaceConnectionInfo in EnumerateInterfaceConnections(container.Content))
                {
                    var availableNetworkGroups = EnumerateAvailableNetworkGroups(container.Content, interfaceConnectionInfo)
                                                 .Where(x => !string.IsNullOrWhiteSpace(x.ProfileName))
                                                 .ToArray();

                    int position = 0;

                    foreach (var profileInfo in Base.GetProfileInfoList(container.Content.Handle, interfaceConnectionInfo.Id))
                    {
                        var profileXml = Base.GetProfile(container.Content.Handle, interfaceConnectionInfo.Id, profileInfo.strProfileName, out uint profileTypeFlag);
                        if (string.IsNullOrWhiteSpace(profileXml))
                        {
                            continue;
                        }

                        if (!ProfileTypeConverter.TryConvert(profileTypeFlag, out ProfileType profileType))
                        {
                            continue;
                        }

                        var availableNetworkGroup = availableNetworkGroups.FirstOrDefault(x => string.Equals(x.ProfileName, profileInfo.strProfileName, StringComparison.Ordinal));

                        yield return(new ProfileRadioPack(
                                         name: profileInfo.strProfileName,
                                         interfaceInfo: interfaceConnectionInfo,
                                         profileType: profileType,
                                         profileXml: profileXml,
                                         position: position++,
                                         signalQuality: (availableNetworkGroup?.SignalQuality ?? 0),
                                         linkQuality: (availableNetworkGroup?.LinkQuality ?? 0),
                                         frequency: (availableNetworkGroup?.Frequency ?? 0),
                                         band: (availableNetworkGroup?.Band ?? 0),
                                         channel: (availableNetworkGroup?.Channel ?? 0)));
                    }
                }
            }
        }
        internal static IEnumerable <InterfaceConnectionInfo> EnumerateInterfaceConnections(Base.WlanClient client)
        {
            using var container = new DisposableContainer <Base.WlanClient>(client);

            foreach (var interfaceInfo in Base.GetInterfaceInfoList(container.Content.Handle))
            {
                var connection = Base.GetConnectionAttributes(container.Content.Handle, interfaceInfo.InterfaceGuid);

                var isConnected = (interfaceInfo.isState == WLAN_INTERFACE_STATE.wlan_interface_state_connected);
                var isRadioOn   = isConnected ||
                                  EnumerateInterfaceRadioSets(container.Content, interfaceInfo.InterfaceGuid).Any(x => x.On.GetValueOrDefault());

                yield return(new InterfaceConnectionInfo(
                                 interfaceInfo,
                                 connectionMode: ConnectionModeConverter.Convert(connection.wlanConnectionMode),
                                 isRadioOn: isRadioOn,
                                 isConnected: isConnected,
                                 profileName: connection.strProfileName));
            }
        }
        internal static IEnumerable <string> EnumerateProfileNames(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList)
                {
                    var profileInfoList = Base.GetProfileInfoList(container.Content.Handle, interfaceInfo.InterfaceGuid);

                    foreach (var profileInfo in profileInfoList)
                    {
                        //Debug.WriteLine("Interface: {0}, Profile: {1}",
                        //	interfaceInfo.strInterfaceDescription,
                        //	profileInfo.strProfileName);

                        yield return(profileInfo.strProfileName);
                    }
                }
            }
        }