-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ethernet.cs
209 lines (175 loc) · 6.7 KB
/
Ethernet.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Threading;
using GHI.OSHW.Hardware;
using Microsoft.SPOT.Hardware;
namespace Redecode.Archimede
{
public delegate void OnConnectHandler();
public delegate void OnDisconnectHandler();
public class Ethernet
{
public static string static_IP;
public static string static_Mask;
public static string static_Gateway;
public static byte[] MACAddress;
public static string IPAddress;
public static event OnConnectHandler OnConnect;
public static event OnDisconnectHandler OnDisconnect;
//--- this is for Default
public static void UseDHCP()
{
static_IP = null;
static_Mask = null;
static_Gateway = null;
}
public static void UseStaticIp(string ip, string mask, string gateway)
{
static_IP = ip;
static_Mask = mask;
static_Gateway = gateway;
}
public static void UseMacAddress(byte[] mac)
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
var networkInterface = interfaces[0];
MACAddress = mac;
bool different = false;
for (int i = 0; i < MACAddress.Length; i++)
{
if (different = (MACAddress[i] != networkInterface.PhysicalAddress[i]))
{
break;
}
}
if (different)
{
Log.Debug("SETTING MAC Address");
networkInterface.PhysicalAddress = MACAddress;
PowerState.RebootDevice(true);
}
}
public static void UseMacAddress(string mac)
{
string[] nums = mac.Split(':');
byte[] bytes = new byte[nums.Length];
for (int i=0; i<nums.Length; i++) {
bytes[i] = (byte)Convert.ToInt32(nums[i], 16);
}
UseMacAddress(bytes);
}
public static void Connect()
{
Log.Debug("IPAddress "+IPAddress);
if (IPAddress != null)
{
return; //-- Already connected
}
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
var networkInterface = interfaces[0];
while (IPAddress == null)
{
try
{
if (static_IP != null)
{
//networkInterface.ReleaseDhcpLease();
networkInterface.EnableStaticIP(static_IP, static_Mask, static_Gateway);
//networkInterface.EnableStaticDns(new string[] { "8.8.8.8" });
//if (MACAddress != null)
//{
// networkInterface.PhysicalAddress = MACAddress;
//}
}
else
{
//Microsoft.SPOT.Hardware.PowerState.RebootDevice();
//networkInterface.EnableDynamicDns();
/*if (MACAddress != null)
{
bool different = false;
for (int i=0; i<MACAddress.Length; i++) {
if (different = (MACAddress[i] != networkInterface.PhysicalAddress[i])) {
break;
}
}
if (different)
{
Log.Debug("SETTING MAC Address");
networkInterface.PhysicalAddress = MACAddress;
//networkInterface.RenewDhcpLease();
PowerState.RebootDevice(true);
Thread.Sleep(1500);
}
}*/
//networkInterface.EnableStaticIP("0.0.0.0", "0.0.0.0", "0.0.0.0");
//networkInterface.EnableDynamicDns();
/*if (networkInterface.IPAddress != "0.0.0.0")
{
networkInterface.RenewDhcpLease();
Thread.Sleep(1000);
}*/
networkInterface.EnableDhcp();
int t=0;
while ((networkInterface.IPAddress == "0.0.0.0") && t++ < 30)
{
Thread.Sleep(100);
/*
try
{
int k = 0;
while (networkInterface.IPAddress == "0.0.0.0" && k++ < 20)
{
k++;
Thread.Sleep(100);
}
}
catch
{
}*/
}
/*
if (networkInterface.IPAddress == "0.0.0.0")
{
networkInterface.RenewDhcpLease();
Thread.Sleep(200);
}*/
}
if (networkInterface.IPAddress != "0.0.0.0")
{
IPAddress = networkInterface.IPAddress;
}
}
catch
{
IPAddress = null;
}
}
Log.Debug("NEW IPAddress " + IPAddress);
if (OnConnect != null)
{
OnConnect();
}
}
public static void Disconnect()
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
var networkInterface = interfaces[0];
if (IPAddress == null)
{
return; //-- Already disconnected
}
IPAddress = null;
if (OnDisconnect != null)
{
OnDisconnect();
}
if (static_IP == null)
{
networkInterface.RenewDhcpLease();
Thread.Sleep(1000);
}
}
}
}