Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit aa2f20e

Browse files
Serk352geoperez
authored andcommitted
Current network adapter (#133)
* GetCurrentAdapterName added * better password length handling * Update src/Unosquare.RaspberryIO/Computer/NetworkSettings.cs Co-Authored-By: Carlos Solorzano <[email protected]> * Update src/Unosquare.RaspberryIO/Computer/NetworkSettings.cs Co-Authored-By: Carlos Solorzano <[email protected]> * fix if
1 parent ef8c8d4 commit aa2f20e

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Unosquare.RaspberryIO/Computer/NetworkSettings.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,17 @@ public async Task<List<WirelessNetworkInfo>> RetrieveWirelessNetworks(string[] a
9898
/// </summary>
9999
/// <param name="adapterName">Name of the adapter.</param>
100100
/// <param name="networkSsid">The network ssid.</param>
101-
/// <param name="password">The password.</param>
101+
/// <param name="password">The password (8 characters as minimum length).</param>
102102
/// <param name="countryCode">The 2-letter country code in uppercase. Default is US.</param>
103103
/// <returns>True if successful. Otherwise, false.</returns>
104104
public async Task<bool> SetupWirelessNetwork(string adapterName, string networkSsid, string password = null, string countryCode = "US")
105105
{
106106
// TODO: Get the country where the device is located to set 'country' param in payload var
107107
var payload = $"country={countryCode}\nctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\nupdate_config=1\n";
108+
109+
if (!string.IsNullOrWhiteSpace(password) && password.Length < 8)
110+
throw new InvalidOperationException("The password must be at least 8 characters length.");
111+
108112
payload += string.IsNullOrEmpty(password)
109113
? $"network={{\n\tssid=\"{networkSsid}\"\n\tkey_mgmt=NONE\n\t}}\n"
110114
: $"network={{\n\tssid=\"{networkSsid}\"\n\tpsk=\"{password}\"\n\t}}\n";
@@ -215,12 +219,25 @@ public async Task<List<NetworkAdapterInfo>> RetrieveAdapters()
215219
return result.OrderBy(x => x.Name).ToList();
216220
}
217221

222+
/// <summary>
223+
/// Retrieves the current network adapter.
224+
/// </summary>
225+
/// <returns>The name of the current network adapter.</returns>
226+
public static async Task<string> GetCurrentAdapterName()
227+
{
228+
var result = await ProcessRunner.GetProcessOutputAsync("route").ConfigureAwait(false);
229+
var defaultLine = result.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
230+
.FirstOrDefault(l => l.StartsWith("default", StringComparison.OrdinalIgnoreCase));
231+
232+
return defaultLine?.Trim().Substring(defaultLine.LastIndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1);
233+
}
234+
218235
/// <summary>
219236
/// Retrieves current wireless connected network name.
220237
/// </summary>
221238
/// <returns>The connected network name.</returns>
222239
public Task<string> GetWirelessNetworkName() => ProcessRunner.GetProcessOutputAsync("iwgetid", "-r");
223-
240+
224241
private static void GetIPv4(string indentedLine, NetworkAdapterInfo adapter)
225242
{
226243
var addressText = ParseOutputTagFromLine(indentedLine, "inet addr:") ??

0 commit comments

Comments
 (0)