-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathNetworkHelpers.cs
135 lines (110 loc) · 4.4 KB
/
NetworkHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
namespace nanoFramework.Networking
{
public class NetworkHelpers
{
private const string c_SSID = "REPLACE-WITH-YOUR-SSID";
private const string c_AP_PASSWORD = "REPLACE-WITH-YOUR-WIFI-KEY";
private static bool _requiresDateTime;
static public ManualResetEvent IpAddressAvailable = new ManualResetEvent(false);
static public ManualResetEvent DateTimeAvailable = new ManualResetEvent(false);
internal static void SetupAndConnectNetwork(bool requiresDateTime = false)
{
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
_requiresDateTime = requiresDateTime;
new Thread(WorkingThread).Start();
}
internal static void WorkingThread()
{
do
{
Debug.WriteLine("Waiting for network available...");
Thread.Sleep(500);
}
while (!NetworkInterface.GetIsNetworkAvailable());
NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
if (nis.Length > 0)
{
// get the first interface
NetworkInterface ni = nis[0];
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
// network interface is Wi-Fi
Debug.WriteLine("Network connection is: Wi-Fi");
Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
// note on checking the 802.11 configuration
// on secure devices (like the TI CC3220SF) the password can't be read
// so we can't use the code block bellow to automatically set the profile
if ((wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) &&
(wc.Ssid != "" && wc.Password == ""))
{
// have to update Wi-Fi configuration
wc.Ssid = c_SSID;
wc.Password = c_AP_PASSWORD;
wc.SaveConfiguration();
}
else
{
// Wi-Fi configuration matches
// (or can't be validated)
}
}
else
{
// network interface is Ethernet
Debug.WriteLine("Network connection is: Ethernet");
}
// check if we have an IP
CheckIP();
if (_requiresDateTime)
{
IpAddressAvailable.WaitOne();
SetDateTime();
}
}
else
{
throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
}
}
private static void SetDateTime()
{
Debug.WriteLine("Setting up system clock...");
// if SNTP is available and enabled on target device this can be skipped because we should have a valid date & time
while (DateTime.UtcNow.Year < 2018)
{
Debug.WriteLine("Waiting for valid date time...");
// wait for valid date & time
Thread.Sleep(1000);
}
Debug.WriteLine($"System time is: {DateTime.UtcNow.ToString()}");
DateTimeAvailable.Set();
}
private static void CheckIP()
{
var myAddress = IPGlobalProperties.GetIPAddress();
if (myAddress != IPAddress.Any &&
myAddress.ToString() != "1")
{
Debug.WriteLine($"We have and IP: {myAddress}");
IpAddressAvailable.Set();
}
else
{
Debug.WriteLine("No IP...");
}
}
static void AddressChangedCallback(object sender, EventArgs e)
{
CheckIP();
}
}
}