Exemple #1
0
        private static void NotifyCurrentStatus(ConnectionProfile p)
        {
            if (p == null)
            {
                Debug.WriteLine("No service.");
                InvokeNotification("No service.");
                return;
            }

            if (p.IsWwanConnectionProfile)
            {
                Debug.WriteLine("Current: WWan");
                var sb = new StringBuilder();
                sb.AppendLine("WWan: " + p.GetNetworkNames().Aggregate((a, b) => a + " " + b));

                if (p.GetSignalBars() != null)
                {
                    sb.AppendLine("Level: " + p.GetSignalBars().Value);
                }

                var detail = p.WwanConnectionProfileDetails;
                if (detail != null)
                {
                    sb.AppendLine("APN: " + detail.AccessPointName);
                    sb.AppendLine("IPKind: " + detail.IPKind);
                    sb.AppendLine("Registration state: " + detail.GetNetworkRegistrationState().ToString());
                    sb.AppendLine("Connection type: " + detail.GetCurrentDataClass().ToString());
                }

                InvokeNotification(sb.ToString());
            }
            else if (p.IsWlanConnectionProfile)
            {
                Debug.WriteLine("Current: WLan");
                var sb = new StringBuilder();
                sb.Append("WLan: " + p.ProfileName);
                sb.Append(" ");
                sb.Append(p.GetNetworkNames().Aggregate((a, b) => a + " " + b));
                sb.Append(" Level: ");
                if (p.GetSignalBars() != null)
                {
                    sb.Append(p.GetSignalBars().Value);
                }
                InvokeNotification(sb.ToString());
            }
            else
            {
                Debug.WriteLine("No service.");
                InvokeNotification("No service.");
            }
        }
Exemple #2
0
        /// <summary>
        /// Updates  the current object based on profile passed.
        /// </summary>
        /// <param name="profile">instance of <see cref="ConnectionProfile"/></param>
        public virtual void UpdateConnectionInformation(ConnectionProfile profile)
        {
            networkNames.Clear();

            if (profile == null)
            {
                ConnectionType      = ConnectionType.Unknown;
                ConnectivityLevel   = NetworkConnectivityLevel.None;
                IsInternetAvailable = false;
                ConnectionCost      = null;
                SignalStrength      = null;

                return;
            }

            switch (profile.NetworkAdapter.IanaInterfaceType)
            {
            case 6:
                ConnectionType = ConnectionType.Ethernet;
                break;

            case 71:
                ConnectionType = ConnectionType.WiFi;
                break;

            case 243:
            case 244:
                ConnectionType = ConnectionType.Data;
                break;

            default:
                ConnectionType = ConnectionType.Unknown;
                break;
            }

            ConnectivityLevel = profile.GetNetworkConnectivityLevel();
            ConnectionCost    = profile.GetConnectionCost();
            SignalStrength    = profile.GetSignalBars();

            var names = profile.GetNetworkNames();

            if (names?.Count > 0)
            {
                networkNames.AddRange(names);
            }

            switch (ConnectivityLevel)
            {
            case NetworkConnectivityLevel.None:
            case NetworkConnectivityLevel.LocalAccess:
                IsInternetAvailable = false;
                break;

            default:
                IsInternetAvailable = true;
                break;
            }
        }
        public static async Task <ConnectionInfo> FromConnectionProfile(ConnectionProfile profile)
        {
            var connectionInfo = new ConnectionInfo
            {
                Name              = profile.ProfileName,
                IsWlan            = profile.IsWlanConnectionProfile,
                IsWwan            = profile.IsWwanConnectionProfile,
                ConnectivityLevel =
                    profile.GetNetworkConnectivityLevel().ToString(),
                DomainConnectivityLevel =
                    profile.GetDomainConnectivityLevel().ToString()
            };

            var costType = profile.GetConnectionCost();

            connectionInfo.CostType = costType.NetworkCostType.ToString();
            connectionInfo.Flags    = string.Format(
                "{0} {1} {2}",
                costType.ApproachingDataLimit ? "Approaching Data Limit" : string.Empty,
                costType.OverDataLimit ? "Over Data Limit" : string.Empty,
                costType.Roaming ? "Roaming" : string.Empty).Trim();

            connectionInfo.NetworkAdapterId = profile.ServiceProviderGuid;

            if (profile.NetworkAdapter != null)
            {
                connectionInfo.IncomingBitsPerSecond = (long)profile.NetworkAdapter.InboundMaxBitsPerSecond;
                connectionInfo.OutgoingBitsPerSecond = (long)profile.NetworkAdapter.OutboundMaxBitsPerSecond;
                connectionInfo.NetworkType           = profile.NetworkAdapter.NetworkItem.GetNetworkTypes().ToString();
            }

            if (profile.NetworkSecuritySettings != null)
            {
                connectionInfo.AuthenticationType = profile.NetworkSecuritySettings.NetworkAuthenticationType.ToString();
                connectionInfo.EncryptionType     = profile.NetworkSecuritySettings.NetworkEncryptionType.ToString();
            }

            connectionInfo.SignalBars = profile.GetSignalBars();

            connectionInfo.DataPlan = DataPlanInfo.FromProfile(profile);

            var usage =
                await
                profile.GetNetworkUsageAsync(
                    DateTimeOffset.Now.AddDays(-1),
                    DateTimeOffset.Now,
                    DataUsageGranularity.Total,
                    new NetworkUsageStates { Roaming = TriStates.DoNotCare, Shared = TriStates.DoNotCare });

            if (usage != null && usage.Count > 0)
            {
                connectionInfo.BytesReceivedLastDay = usage[0].BytesReceived;
                connectionInfo.BytesSentLastDay     = usage[0].BytesSent;
            }

            return(connectionInfo);
        }
Exemple #4
0
        public static bool IsSameCondition(this ConnectionProfile self, ConnectionProfile target, ProfileComparisonRule rule)
        {
            if (self == null || target == null || rule == null)
            {
                return(false);
            }

            if (
                ((!rule.CompareProfileName) || self.ProfileName == target.ProfileName) &&
                ((!rule.CompareSigmalBars) || self.GetSignalBars() == target.GetSignalBars())
                )

            {
                // no difference
            }
            else
            {
                return(false);
            }

            if (self.IsWlanConnectionProfile && target.IsWlanConnectionProfile)
            {
                if (self.WlanConnectionProfileDetails == null ||
                    target.WlanConnectionProfileDetails == null)
                {
                    return(false);
                }

                // compare Wlan detail info
                return((!rule.CompareSsid) || self.WlanConnectionProfileDetails.GetConnectedSsid() == target.WlanConnectionProfileDetails.GetConnectedSsid());
            }
            else if (self.IsWwanConnectionProfile && target.IsWwanConnectionProfile)
            {
                if (self.WwanConnectionProfileDetails == null ||
                    target.WwanConnectionProfileDetails == null)
                {
                    return(false);
                }

                // compare wwan detail.
                var selfD   = self.WwanConnectionProfileDetails;
                var targetD = target.WwanConnectionProfileDetails;
                return(
                    ((!rule.CompareApn) || selfD.AccessPointName == targetD.AccessPointName) &&
                    ((!rule.CompareDataClass) || selfD.GetCurrentDataClass() == targetD.GetCurrentDataClass()) &&
                    ((!rule.CompareIPKind) || selfD.IPKind == targetD.IPKind)
                    );
            }
            else
            {
                return(!rule.CompareMethod);
            }
        }
        //
        //Get Connection Profile name and cost information
        //
        string GetConnectionProfile(ConnectionProfile connectionProfile)
        {
            string connectionProfileInfo = string.Empty;

            if (connectionProfile != null)
            {
                connectionProfileInfo = "Profile Name : " + connectionProfile.ProfileName + "\n";

                switch (connectionProfile.GetNetworkConnectivityLevel())
                {
                case NetworkConnectivityLevel.None:
                    connectionProfileInfo += "Connectivity Level : None\n";
                    break;

                case NetworkConnectivityLevel.LocalAccess:
                    connectionProfileInfo += "Connectivity Level : Local Access\n";
                    break;

                case NetworkConnectivityLevel.ConstrainedInternetAccess:
                    connectionProfileInfo += "Connectivity Level : Constrained Internet Access\n";
                    break;

                case NetworkConnectivityLevel.InternetAccess:
                    connectionProfileInfo += "Connectivity Level : Internet Access\n";
                    break;
                }

                switch (connectionProfile.GetDomainConnectivityLevel())
                {
                case DomainConnectivityLevel.None:
                    connectionProfileInfo += "Domain Connectivity Level : None\n";
                    break;

                case DomainConnectivityLevel.Unauthenticated:
                    connectionProfileInfo += "Domain Connectivity Level : Unauthenticated\n";
                    break;

                case DomainConnectivityLevel.Authenticated:
                    connectionProfileInfo += "Domain Connectivity Level : Authenticated\n";
                    break;
                }

                //Get Connection Cost information
                ConnectionCost connectionCost = connectionProfile.GetConnectionCost();
                connectionProfileInfo += GetConnectionCostInfo(connectionCost);

                //Get Dataplan Status information
                DataPlanStatus dataPlanStatus = connectionProfile.GetDataPlanStatus();
                connectionProfileInfo += GetDataPlanStatusInfo(dataPlanStatus);

                //Get Network Security Settings
                NetworkSecuritySettings netSecuritySettings = connectionProfile.NetworkSecuritySettings;
                connectionProfileInfo += GetNetworkSecuritySettingsInfo(netSecuritySettings);

                //Get Wlan Connection Profile Details if this is a Wlan ConnectionProfile
                if (connectionProfile.IsWlanConnectionProfile)
                {
                    WlanConnectionProfileDetails wlanConnectionProfileDetails = connectionProfile.WlanConnectionProfileDetails;
                    connectionProfileInfo += GetWlanConnectionProfileDetailsInfo(wlanConnectionProfileDetails);
                }

                //Get Wwan Connection Profile Details if this is a Wwan ConnectionProfile
                if (connectionProfile.IsWwanConnectionProfile)
                {
                    WwanConnectionProfileDetails wwanConnectionProfileDetails = connectionProfile.WwanConnectionProfileDetails;
                    connectionProfileInfo += GetWwanConnectionProfileDetailsInfo(wwanConnectionProfileDetails);
                }

                //Get the Service Provider GUID
                if (connectionProfile.ServiceProviderGuid.HasValue)
                {
                    connectionProfileInfo += "====================\n";
                    connectionProfileInfo += "Service Provider GUID: " + connectionProfile.ServiceProviderGuid + "\n";
                }

                //Get the number of signal bars
                if (connectionProfile.GetSignalBars().HasValue)
                {
                    connectionProfileInfo += "====================\n";
                    connectionProfileInfo += "Signal Bars: " + connectionProfile.GetSignalBars() + "\n";
                }
            }
            return(connectionProfileInfo);
        }
        /// <summary>
        /// Updates  the current object based on profile passed.
        /// </summary>
        /// <param name="profile">instance of <see cref="ConnectionProfile"/></param>
        public void UpdateConnectionInformation(ConnectionProfile profile)
        {
            LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Updating connection info...");

            if (profile == null)
            {
                LogService.Log(MLogLevel.LOG_LEVEL_WARNING, "There is no connection profile");
                Reset();
                return;
            }

            LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Getting interface type...");
            uint ianaInterfaceType = profile.NetworkAdapter?.IanaInterfaceType ?? 0;

            switch (ianaInterfaceType)
            {
            case 6:
                ConnectionType = ConnectionType.Ethernet;
                break;

            case 71:
                ConnectionType = ConnectionType.WiFi;
                break;

            case 243:
            case 244:
                ConnectionType = ConnectionType.Data;
                break;

            default:
                ConnectionType = ConnectionType.Unknown;
                break;
            }

            LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Getting netework name...");
            NetworkName = profile.ProfileName;

            // Update the IP address
            LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Getting IP address...");
            if (profile?.NetworkAdapter != null)
            {
                var hostname = NetworkInformation.GetHostNames().FirstOrDefault(hn =>
                                                                                hn?.IPInformation?.NetworkAdapter?.NetworkAdapterId == profile.NetworkAdapter.NetworkAdapterId);

                if (hostname != null)
                {
                    IpAddress = hostname.CanonicalName;
                }
            }

            LogService.Log(MLogLevel.LOG_LEVEL_INFO, "Getting connectivity level...");
            ConnectivityLevel = profile.GetNetworkConnectivityLevel();
            switch (ConnectivityLevel)
            {
            case NetworkConnectivityLevel.None:
            case NetworkConnectivityLevel.LocalAccess:
                IsInternetAvailable = false;
                break;

            case NetworkConnectivityLevel.InternetAccess:
            case NetworkConnectivityLevel.ConstrainedInternetAccess:
            default:
                IsInternetAvailable = true;
                break;
            }

            ConnectionCost = profile.GetConnectionCost();
            SignalStrength = profile.GetSignalBars();

            NetworkHelper.LogConnectivityInfo();
        }
Exemple #7
0
        /// <summary>
        /// Updates  the current object based on profile passed.
        /// </summary>
        /// <param name="profile">instance of <see cref="ConnectionProfile"/></param>
        public void UpdateConnectionInformation(ConnectionProfile profile)
        {
            if (profile == null)
            {
                Reset();

                return;
            }

            uint ianaInterfaceType = profile.NetworkAdapter?.IanaInterfaceType ?? 0;

            switch (ianaInterfaceType)
            {
            case 6:
                ConnectionType = ConnectionType.Ethernet;
                break;

            case 71:
                ConnectionType = ConnectionType.WiFi;
                break;

            case 243:
            case 244:
                ConnectionType = ConnectionType.Data;
                break;

            default:
                ConnectionType = ConnectionType.Unknown;
                break;
            }

            NetworkName = profile.ProfileName;

            // Update the IP address
            if (profile?.NetworkAdapter != null)
            {
                var hostname = NetworkInformation.GetHostNames().SingleOrDefault(hn =>
                                                                                 hn?.IPInformation?.NetworkAdapter?.NetworkAdapterId == profile.NetworkAdapter.NetworkAdapterId);

                if (hostname != null)
                {
                    IpAddress = hostname.CanonicalName;
                }
            }

            ConnectivityLevel = profile.GetNetworkConnectivityLevel();

            switch (ConnectivityLevel)
            {
            case NetworkConnectivityLevel.None:
            case NetworkConnectivityLevel.LocalAccess:
                IsInternetAvailable = false;
                break;

            case NetworkConnectivityLevel.InternetAccess:
            case NetworkConnectivityLevel.ConstrainedInternetAccess:
            default:
                IsInternetAvailable = true;
                break;
            }

            ConnectionCost = profile.GetConnectionCost();
            SignalStrength = profile.GetSignalBars();
        }
Exemple #8
0
 internal static string GetSignalStrength(ConnectionProfile connectionProfile)
 {
     return(connectionProfile.GetSignalBars().HasValue ? connectionProfile.GetSignalBars().ToString() : "Unknown");
 }