From 41e78cd483ef6ff3969cd1b8637b3b710d05cdbc Mon Sep 17 00:00:00 2001 From: josesimoes Date: Thu, 19 Apr 2018 11:21:52 +0100 Subject: [PATCH] Fix issues with wireless config save/load - The string fields weren't being properly handled - Add constructors to config base classes to ensure that the byte arrays size match - Minor improvements in WPF test app Signed-off-by: josesimoes --- source/USB Test App WPF/MainWindow.xaml.cs | 25 ++++++++++++++----- .../DeviceConfiguration.cs | 14 ++++++----- .../NetworkConfigurationBase.cs | 6 +++++ .../DeviceConfiguration/Wireless80211Base.cs | 14 +++++++++-- .../WireProtocol/Commands.cs | 6 ++++- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/source/USB Test App WPF/MainWindow.xaml.cs b/source/USB Test App WPF/MainWindow.xaml.cs index 7143d2d0..cb8f08dd 100644 --- a/source/USB Test App WPF/MainWindow.xaml.cs +++ b/source/USB Test App WPF/MainWindow.xaml.cs @@ -739,17 +739,30 @@ private async void SetDeviceConfigButton_Click(object sender, RoutedEventArgs e) //deviceConfig.NetworkConfiguraton.MacAddress = new byte[] { 0, 0x80, 0xe1, 0x01, 0x35, 0x56 }; //deviceConfig.NetworkConfiguraton.StartupAddressMode = DeviceConfiguration.AddressMode.DHCP; - // set new network configuration on the specific class - DeviceConfiguration.NetworkConfigurationProperties newDeviceNetworkConfiguration = new DeviceConfiguration.NetworkConfigurationProperties(); - newDeviceNetworkConfiguration.MacAddress = new byte[] { 0, 0x80, 0xe1, 0x01, 0x35, 0x56 }; - newDeviceNetworkConfiguration.InterfaceType = nanoFramework.Tools.Debugger.NetworkInterfaceType.Ethernet; - newDeviceNetworkConfiguration.StartupAddressMode = AddressMode.DHCP; + // update new network configuration + DeviceConfiguration.NetworkConfigurationProperties newDeviceNetworkConfiguration = new DeviceConfiguration.NetworkConfigurationProperties + { + MacAddress = new byte[] { 0, 0x80, 0xe1, 0x01, 0x35, 0x56 }, + InterfaceType = nanoFramework.Tools.Debugger.NetworkInterfaceType.Ethernet, + StartupAddressMode = AddressMode.DHCP, - newDeviceNetworkConfiguration.IPv4DNSAddress1 = IPAddress.Parse("192.168.1.254"); + IPv4DNSAddress1 = IPAddress.Parse("192.168.1.254"), + }; // write device configuration to device var returnValue = device.DebugEngine.UpdateDeviceConfiguration(newDeviceNetworkConfiguration, 0); + // add new wireless 802.11 configuration + DeviceConfiguration.Wireless80211ConfigurationProperties newWireless80211Configuration = new DeviceConfiguration.Wireless80211ConfigurationProperties() + { + Id = 44, + Ssid = "Nice_Ssid", + Password = "1234", + }; + + // write wireless configuration to device + returnValue = device.DebugEngine.UpdateDeviceConfiguration(newWireless80211Configuration, 0); + Debug.WriteLine(""); Debug.WriteLine(""); Debug.WriteLine($"device config update result: {returnValue}"); diff --git a/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/DeviceConfiguration.cs b/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/DeviceConfiguration.cs index 826654cd..87627dfd 100644 --- a/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/DeviceConfiguration.cs +++ b/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/DeviceConfiguration.cs @@ -210,8 +210,8 @@ public Wireless80211ConfigurationProperties(Wireless80211ConfigurationBase confi Authentication = (AuthenticationType)config.Authentication; Encryption = (EncryptionType)config.Encryption; Radio = (RadioType)config.Radio; - Ssid = config.Ssid; - Password = config.Password; + Ssid = Encoding.UTF8.GetString(config.Ssid).Trim('\0'); + Password = Encoding.UTF8.GetString(config.Password).Trim('\0'); // reset unknown flag IsUnknown = false; @@ -225,13 +225,15 @@ public static explicit operator Wireless80211ConfigurationBase(Wireless80211Conf Marker = Encoding.UTF8.GetBytes(MarkerConfigurationWireless80211_v1), Id = value.Id, - Authentication = (byte)value.Authentication, Encryption = (byte)value.Encryption, Radio = (byte)value.Radio, - Ssid = value.Ssid, - Password = value.Password, - }; + }; + + // the following ones are strings so they need to be copied over to the array + // this is required to when serializing the class the struct size matches the one in the native end + Array.Copy(Encoding.UTF8.GetBytes(value.Ssid), 0, networkWirelessConfig.Ssid, 0, value.Ssid.Length); + Array.Copy(Encoding.UTF8.GetBytes(value.Password), 0, networkWirelessConfig.Password, 0, value.Password.Length); return networkWirelessConfig; } diff --git a/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/NetworkConfigurationBase.cs b/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/NetworkConfigurationBase.cs index 56cf8bef..a50e21f2 100644 --- a/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/NetworkConfigurationBase.cs +++ b/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/NetworkConfigurationBase.cs @@ -83,5 +83,11 @@ public class NetworkConfigurationBase /// Address mode (static, DHCP or auto IP) /// public byte StartupAddressMode; + + public NetworkConfigurationBase() + { + // need to init this here to match the expected size on the struct to be sent to the device + Marker = new byte[4]; + } } } \ No newline at end of file diff --git a/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/Wireless80211Base.cs b/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/Wireless80211Base.cs index f2c4552c..b32e7562 100644 --- a/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/Wireless80211Base.cs +++ b/source/nanoFramework.Tools.DebugLibrary.Shared/DeviceConfiguration/Wireless80211Base.cs @@ -39,12 +39,22 @@ public class Wireless80211ConfigurationBase /// /// Network SSID + /// 32 bytes length. /// - public string Ssid; + public byte[] Ssid; /// /// Network password + /// 64 bytes length. /// - public string Password; + public byte[] Password; + + public Wireless80211ConfigurationBase() + { + // need to init these here to match the expected size on the struct to be sent to the device + Marker = new byte[4]; + Ssid = new byte[32]; + Password = new byte[64]; + } } } \ No newline at end of file diff --git a/source/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Commands.cs b/source/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Commands.cs index b935f558..96cf997f 100644 --- a/source/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Commands.cs +++ b/source/nanoFramework.Tools.DebugLibrary.Shared/WireProtocol/Commands.cs @@ -312,7 +312,9 @@ public NetworkWirelessConfiguration() Authentication = (byte)AuthenticationType.None ; Encryption = (byte)EncryptionType.None; Radio = (byte)RadioType.None; - } + Ssid = new byte[32]; + Password = new byte[64]; + } public void PrepareForDeserialize(int size, byte[] data, Converter converter) { @@ -321,6 +323,8 @@ public void PrepareForDeserialize(int size, byte[] data, Converter converter) Authentication = (byte)AuthenticationType.None; Encryption = (byte)EncryptionType.None; Radio = (byte)RadioType.None; + Ssid = new byte[32]; + Password = new byte[64]; } } }