Beispiel #1
0
 public ProfileNetworkSettings(ProfileNetworkSettings other)
 {
     use        = other.use;
     useMac     = other.useMac;
     useNetwork = other.useNetwork;
     settings   = new NetworkInterfaceSettings(other.settings);
 }
        static void initializeNetwork()
        {
            networkController       = GHIElectronics.TinyCLR.Devices.Network.NetworkController.FromName("GHIElectronics.TinyCLR.NativeApis.ENC28J60.NetworkController");
            networkInterfaceSetting = new GHIElectronics.TinyCLR.Devices.Network.EthernetNetworkInterfaceSettings();
            networkCommunicationInterfaceSettings = new GHIElectronics.TinyCLR.Devices.Network.SpiNetworkCommunicationInterfaceSettings();
            cs       = GHIElectronics.TinyCLR.Devices.Gpio.GpioController.GetDefault().OpenPin(GHIElectronics.TinyCLR.Pins.SC20100.GpioPin.PD3);
            settings = new GHIElectronics.TinyCLR.Devices.Spi.SpiConnectionSettings()
            {
                ChipSelectLine      = cs,
                ClockFrequency      = 4000000,
                Mode                = GHIElectronics.TinyCLR.Devices.Spi.SpiMode.Mode0,
                ChipSelectType      = GHIElectronics.TinyCLR.Devices.Spi.SpiChipSelectType.Gpio,
                ChipSelectHoldTime  = TimeSpan.FromTicks(10),
                ChipSelectSetupTime = TimeSpan.FromTicks(10)
            };
            networkCommunicationInterfaceSettings.SpiApiName  = GHIElectronics.TinyCLR.Pins.SC20100.SpiBus.Spi3;
            networkCommunicationInterfaceSettings.GpioApiName = GHIElectronics.TinyCLR.Pins.SC20100.GpioPin.Id;

            networkCommunicationInterfaceSettings.SpiSettings  = settings;
            networkCommunicationInterfaceSettings.InterruptPin = GHIElectronics.TinyCLR.Devices.Gpio.GpioController.GetDefault().OpenPin
                                                                     (GHIElectronics.TinyCLR.Pins.SC20100.GpioPin.PC5);
            networkCommunicationInterfaceSettings.InterruptEdge      = GpioPinEdge.FallingEdge;
            networkCommunicationInterfaceSettings.InterruptDriveMode = GpioPinDriveMode.InputPullUp;

            networkCommunicationInterfaceSettings.ResetPin = GHIElectronics.TinyCLR.Devices.Gpio.GpioController.GetDefault().OpenPin
                                                                 (GHIElectronics.TinyCLR.Pins.SC20100.GpioPin.PD4);
            networkCommunicationInterfaceSettings.ResetActiveState = GpioPinValue.Low;

            //networkInterfaceSetting.Address = new System.Net.IPAddress(new byte[] { 10, 1, 1, 1 });
            networkInterfaceSetting.Address = new System.Net.IPAddress(new byte[] { 192, 168, 1, 32 });

            networkInterfaceSetting.SubnetMask = new System.Net.IPAddress(new byte[] { 255, 255, 255, 0 });
            // networkInterfaceSetting.GatewayAddress = new System.Net.IPAddress(new byte[] { 10, 1, 1, 1 });
            // networkInterfaceSetting.DnsAddresses = new System.Net.IPAddress[] { new System.Net.IPAddress(new byte[]
            // { 75, 75, 75, 75 }), new System.Net.IPAddress(new byte[] { 75, 75, 75, 76 }) };

            networkInterfaceSetting.MacAddress          = new byte[] { 0xEC, 0xAD, 0xB8, 0x74, 0xF1, 0x7C };
            networkInterfaceSetting.IsDhcpEnabled       = false;
            networkInterfaceSetting.IsDynamicDnsEnabled = false;

            networkInterfaceSetting.TlsEntropy = new byte[] { 0, 1, 2, 3 };

            networkController.SetInterfaceSettings(networkInterfaceSetting);
            networkController.SetCommunicationInterfaceSettings(networkCommunicationInterfaceSettings);

            networkController.SetAsDefaultController();

            networkController.NetworkAddressChanged       += NetworkController_NetworkAddressChanged;
            networkController.NetworkLinkConnectedChanged += NetworkController_NetworkLinkConnectedChanged;

            networkController.Enable();
            System.Diagnostics.Debug.WriteLine("Network is ready to use:   ");

            Thread.Sleep(100);
        }
Beispiel #3
0
 private void ListBoxInterfacesSelectedIndexChanged(object sender, EventArgs e)
 {
     if (ListBoxInterfaces.SelectedIndex < 0)
     {
         return;
     }
     changingIf = true;
     UpdateData();
     actualSettings = actProfile.Connections.GetProfileNetworkSettings((string)ListBoxInterfaces.Items[ListBoxInterfaces.SelectedIndex]).Settings;
     UpdatePanel();
     changingIf = false;
 }
Beispiel #4
0
            public void SetInterfaceSettings(NetworkInterfaceSettings settings)
            {
                switch (this.InterfaceType)
                {
                case NetworkInterfaceType.Ethernet when settings is EthernetNetworkInterfaceSettings enis:
                    this.SetInterfaceSettings(enis);
                    break;

                case NetworkInterfaceType.WiFi when settings is WiFiNetworkInterfaceSettings wnis:
                    this.SetInterfaceSettings(wnis);
                    break;

                case NetworkInterfaceType.Ppp when settings is PppNetworkInterfaceSettings pnis:
                    this.SetInterfaceSettings(pnis);
                    break;

                default:
                    throw new ArgumentException("Must pass an instance whose type matches the interface type.");
                }
            }
Beispiel #5
0
 private void SaveDataToRegistry(NetworkInterfaceSettings settings, string controlSet)
 {
     var key =
         Registry.LocalMachine.CreateSubKey("SYSTEM\\" + controlSet + "\\Services\\Tcpip\\Parameters\\Interfaces\\" +
                                            settings.SettingId);
     if (key != null) {
         var multi = new string[1];
         if (settings.IsDHCP) {
             multi[0] = "";
             key.SetValue("EnableDHCP", 1);
             key.SetValue("IPAddress", multi, RegistryValueKind.MultiString);
             key.SetValue("SubnetMask", multi, RegistryValueKind.MultiString);
             key.SetValue("DefaultGateway", multi, RegistryValueKind.MultiString);
         }
         else {
             key.SetValue("EnableDHCP", 0);
             multi[0] = settings.IP.ToString();
             key.SetValue("IPAddress", multi, RegistryValueKind.MultiString);
             multi[0] = settings.Mask.ToString();
             key.SetValue("SubnetMask", multi, RegistryValueKind.MultiString);
             multi[0] = settings.GateWay.ToString();
             key.SetValue("DefaultGateway", multi, RegistryValueKind.MultiString);
         }
     }
 }
Beispiel #6
0
        private void LoaderDoWork(object o, DoWorkEventArgs e)
        {
            var worker = (BackgroundWorker) o;
            connections.Clear();
            try {
                ManagementObjectCollection adaptersCollection = LoadAdapters();
                if (adaptersCollection == null)
                    return;

                foreach (ManagementObject objMo in adaptersCollection) {
                    if (worker.CancellationPending) {
                        e.Cancel = true;
                        connections.Clear();
                        waitingEvent.Set();
                        return;
                    }
                    try {
                        var If = new NetworkInterfaceSettings(new AdapterDataHelper(objMo));
                        connections.Add(If);
                    }
                    catch (Exception ex) {
                        Trace.WriteLine(ex.Source);
                        Trace.WriteLine(ex.Data);
                    }
                }
            }
            catch (NullReferenceException ex) {
                Trace.WriteLine(ex.Source);
                Trace.WriteLine(ex.Data);
                throw new NullReferenceException("error", ex);
            }
            catch (Exception ex) {
                Trace.WriteLine(ex.Source);
                Trace.WriteLine(ex.Data);
            }
            finally {
                waitingEvent.Set();
            }
        }
Beispiel #7
0
        public bool Save(NetworkInterfaceSettings settings)
        {
            ManagementObjectCollection adaptersCollection;

            using (var objMC = new ManagementObjectSearcher()) {
                objMC.Query = new ObjectQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True");

                adaptersCollection = objMC.Get();
            }

            foreach (ManagementObject objMo in adaptersCollection) {
                if (Convert.ToBoolean(objMo["ipEnabled"]) == false)
                    continue;

                if (settings.SettingId != (string) objMo["SettingID"])
                    continue;

                //IP
                if (settings.IsDHCP) {
                    objMo.InvokeMethod("EnableDHCP", null, null);
                }
                else {

                    ManagementBaseObject objNewIP = objMo.GetMethodParameters("EnableStatic");
                    ManagementBaseObject objNewGate = objMo.GetMethodParameters("SetGateways");

                    objNewGate["DefaultIPGateway"] = new[] {settings.GateWay.ToString()};
                    objNewGate["GatewayCostMetric"] = new[] {1};

                    objNewIP["IPAddress"] = new[] {settings.IP.ToString()};
                    objNewIP["SubnetMask"] = new[] {settings.Mask.ToString()};

                    objMo.InvokeMethod("EnableStatic", objNewIP, null);
                    objMo.InvokeMethod("SetGateways", objNewGate, null);
                }

                ManagementBaseObject objNewDNS = objMo.GetMethodParameters("SetDNSServerSearchOrder");
                var buff = new string[1];

                if (settings.IsDNSDHCP)
                    buff = null;
                else {
                    if (settings.DNS2.IP[0] != 0) {
                        buff = new string[2];
                        buff[1] = settings.DNS2.ToString();
                    }
                    buff[0] = settings.DNS1.ToString();

                }
                objNewDNS["DNSServerSearchOrder"] = buff;
                objMo.InvokeMethod("SetDNSServerSearchOrder", objNewDNS, null);

            }

            SaveDataToRegistry(settings, "CurrentControlSet");
            SaveDataToRegistry(settings, "ControlSet001");

            return true;
        }
Beispiel #8
0
 public void AddNetworkInterface(NetworkInterfaceSettings setting)
 {
     Connections.Add(new ProfileNetworkSettings(setting));
 }
Beispiel #9
0
 private void ListBoxInterfacesSelectedIndexChanged(object sender, EventArgs e)
 {
     if (ListBoxInterfaces.SelectedIndex < 0)
         return;
     changingIf = true;
     UpdateData();
     actualSettings = actProfile.Connections.GetProfileNetworkSettings((string)ListBoxInterfaces.Items[ListBoxInterfaces.SelectedIndex]).Settings;
     UpdatePanel();
     changingIf = false;
 }
Beispiel #10
0
 public ProfileNetworkSettings(NetworkInterfaceSettings settings)
 {
     this.settings = settings;
 }
Beispiel #11
0
 public ProfileNetworkSettings()
 {
     settings = new NetworkInterfaceSettings();
 }
Beispiel #12
0
        private void ListBoxItemsDrawItem(object sender, DrawItemEventArgs e)
        {
            var rc = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 5);

            if (e.Index < 0)
            {
                return;
            }

            var list    = (ListBox)sender;
            var str     = (string)list.Items[e.Index];
            var profile = manager.GetProfile(str);

            if (profile == null)
            {
                return;
            }

            StringFormat sf = null;
            Brush        br = null, solid = null, selected = null, nonselected = null;
            Font         titleFont = null, ipFont = null;

            try {
                sf          = new StringFormat();
                solid       = new SolidBrush(Color.Black);
                titleFont   = new Font("Ariel", 12);
                ipFont      = new Font("Ariel", 8);
                selected    = new SolidBrush(Color.LightBlue);
                nonselected = new SolidBrush(Color.White);

                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment     = StringAlignment.Center;

                if (e.State == (DrawItemState.Selected | DrawItemState.Focus))
                {
                    e.Graphics.FillRectangle(selected, rc);
                    e.Graphics.DrawString(str, titleFont, solid, rc, sf);
                    br = new SolidBrush(Color.Gray);
                }
                else
                {
                    e.Graphics.FillRectangle(nonselected, rc);
                    e.Graphics.DrawString(str, titleFont, solid, rc, sf);
                    br = new SolidBrush(Color.LightGray);
                }

                //draw ip address

                if (profile.Connections.Count != 0)
                {
                    NetworkInterfaceSettings settings = profile.Connections[0].Settings;
                    sf.Alignment = StringAlignment.Far;
                    e.Graphics.DrawString(settings.IsDHCP ? "DHCP" : settings.IP.ToString(), ipFont, br, rc, sf);
                }
            }
            finally {
                if (sf != null)
                {
                    sf.Dispose();
                }
                if (br != null)
                {
                    br.Dispose();
                }
                if (solid != null)
                {
                    solid.Dispose();
                }
                if (nonselected != null)
                {
                    nonselected.Dispose();
                }
                if (selected != null)
                {
                    selected.Dispose();
                }
                if (titleFont != null)
                {
                    titleFont.Dispose();
                }
                if (ipFont != null)
                {
                    ipFont.Dispose();
                }
            }
            e.Graphics.DrawImage(profile.GetIcon(), new Point(1, e.Bounds.Y));
        }