1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_LINKIT_WIFI_h
11#define _MITOV_LINKIT_WIFI_h
12
13#include <Mitov.h>
14#include <Mitov_BasicEthernet.h>
15#include <LWiFi.h>
16#include <LWiFiServer.h>
17#include <LWiFiClient.h>
18#include <LWiFiUDP.h>
19
20namespace Mitov
21{
22//---------------------------------------------------------------------------
23 enum LinkItWiFiEncription { liweAuto, liweWEP, liweWPA };
24//---------------------------------------------------------------------------
25 class LinkItRemoteAccessPoint
26 {
27 public:
28 bool Enabled = true;
29 String SSID;
30 String Password;
31 LinkItWiFiEncription Encription = liweAuto;
32
33 public:
34 bool Connect()
35 {
36 if( ! Enabled )
37 return false;
38
39 if( SSID == "" )
40 return false;
41
42 if( Password == "" )
43 return ( LWiFi.connect( SSID.c_str() ) > 0 );
44
45
46 switch( Encription )
47 {
48 case liweAuto:
49 {
50 if( LWiFi.connectWPA( SSID.c_str(), Password.c_str() ) > 0 )
51 return true;
52
53 return( LWiFi.connectWEP( SSID.c_str(), Password.c_str() ) > 0 );
54 }
55
56 case liweWEP:
57 return( LWiFi.connectWEP( SSID.c_str(), Password.c_str() ) > 0 );
58
59 case liweWPA:
60 return( LWiFi.connectWPA( SSID.c_str(), Password.c_str() ) > 0 );
61 }
62 }
63 };
64//---------------------------------------------------------------------------
65 class LinkItWiFiModule : public OpenWire::Component
66 {
67 typedef OpenWire::Component inherited;
68
69 public:
70 OpenWire::SinkPin ScanNetworksInputPin;
71 OpenWire::SourcePin FoundSignalStrengthOutputPin;
72 OpenWire::SourcePin FoundSSIDOutputPin;
73
74 OpenWire::SourcePin AddressOutputPin;
75 OpenWire::SourcePin MACOutputPin;
76
77 OpenWire::SourcePin SignalStrengthOutputPin;
78 OpenWire::SourcePin BSSIDOutputPin;
79 OpenWire::SourcePin GatewayIPOutputPin;
80 OpenWire::SourcePin SubnetMaskIPOutputPin;
81
82 OpenWire::SinkPin CheckSignalStrengthInputPin;
83
84 public:
85 Mitov::SimpleObjectList<LinkItRemoteAccessPoint*> AccessPoints;
86 Mitov::SimpleList<BasicEthernetSocket<LinkItWiFiModule>*> Sockets;
87
88 bool Enabled = true;
89
90 public:
91 bool IsStarted = false;
92
93 public:
94 void SetEnabled( bool AValue )
95 {
96 if( Enabled == AValue )
97 return;
98
99 Enabled = AValue;
100 if( Enabled )
101 StartEthernet();
102
103 else
104 StopEthernet();
105
106 }
107
108 public:
109 virtual bool GetIPFromHostName( String AHostName, ::IPAddress &AAdress )
110 {
111 bool AResult = ( LWiFi.hostByName( AHostName.c_str(), AAdress ) == 1 );
112 if( ! AResult )
113 AAdress = INADDR_NONE;
114
115 return AResult;
116 }
117
118 protected:
119 virtual void SystemInit()
120 {
121 if( Enabled )
122 StartEthernet();
123
124 inherited::SystemInit();
125 }
126
127 void StopEthernet()
128 {
129 for( int i = 0; i < Sockets.size(); ++i )
130 Sockets[ i ]->StopSocket();
131
132 LWiFi.end();
133 IsStarted = false;
134 }
135
136 void StartEthernet()
137 {
138/*
139 if( ! AccessPoints.length() )
140 return;
141
142 if( ! Sockets.length() )
143 return;
144*/
145 LWiFi.begin();
146 for( int i = 0; i < AccessPoints.size(); ++i )
147 if( AccessPoints[ i ]->Connect() )
148 {
149 IsStarted = true;
150 if( MACOutputPin.IsConnected() )
151 {
152 uint8_t AMACAddress[VM_WLAN_WNDRV_MAC_ADDRESS_LEN] = {0};
153
154 LWiFi.macAddress( AMACAddress );
155
156 char AMACString[ VM_WLAN_WNDRV_MAC_ADDRESS_LEN * 3 + 1 ];
157 sprintf( AMACString, "%02X-%02X-%02X-%02X-%02X-%02X", AMACString[ 0 ], AMACString[ 1 ], AMACString[ 2 ], AMACString[ 3 ], AMACString[ 4 ], AMACString[ 5 ] );
158 MACOutputPin.Notify( AMACString );
159 }
160
161 if( AddressOutputPin.IsConnected() )
162 {
163 IPAddress ALocalIPAddress = LWiFi.localIP();
164 char AIPString[ 4 * 4 + 1 ];
165 sprintf( AIPString, "%u.%u.%u.%u", ALocalIPAddress[ 0 ], ALocalIPAddress[ 1 ], ALocalIPAddress[ 2 ], ALocalIPAddress[ 3 ] );
166
167// String ALocalIPAddressString = String( ALocalIPAddress[ 0 ] ) + "." + String( ALocalIPAddress[ 1 ] + "." + ALocalIPAddress[ 2 ] + "." + ALocalIPAddress[ 3 ];
168 AddressOutputPin.Notify( AIPString );
169 }
170
171 if( SignalStrengthOutputPin.IsConnected() )
172 {
173 int32_t ASetrength = LWiFi.RSSI();
174 SignalStrengthOutputPin.Notify( &ASetrength );
175 }
176
177 if( BSSIDOutputPin.IsConnected() )
178 {
179 uint8_t AMACAddress[VM_WLAN_WNDRV_MAC_ADDRESS_LEN] = {0};
180
181 LWiFi.BSSID( AMACAddress );
182
183 char AMACString[ VM_WLAN_WNDRV_MAC_ADDRESS_LEN * 3 + 1 ];
184 sprintf( AMACString, "%02X-%02X-%02X-%02X-%02X-%02X", AMACString[ 0 ], AMACString[ 1 ], AMACString[ 2 ], AMACString[ 3 ], AMACString[ 4 ], AMACString[ 5 ] );
185 BSSIDOutputPin.Notify( AMACString );
186 }
187
188 if( GatewayIPOutputPin.IsConnected() )
189 {
190 IPAddress ALocalIPAddress = LWiFi.gatewayIP();
191 char AIPString[ 4 * 4 + 1 ];
192 sprintf( AIPString, "%u.%u.%u.%u", ALocalIPAddress[ 0 ], ALocalIPAddress[ 1 ], ALocalIPAddress[ 2 ], ALocalIPAddress[ 3 ] );
193
194// String ALocalIPAddressString = String( ALocalIPAddress[ 0 ] ) + "." + String( ALocalIPAddress[ 1 ] + "." + ALocalIPAddress[ 2 ] + "." + ALocalIPAddress[ 3 ];
195 AddressOutputPin.Notify( AIPString );
196 }
197
198 if( SubnetMaskIPOutputPin.IsConnected() )
199 {
200 IPAddress ALocalIPAddress = LWiFi.subnetMask();
201 char AIPString[ 4 * 4 + 1 ];
202 sprintf( AIPString, "%u.%u.%u.%u", ALocalIPAddress[ 0 ], ALocalIPAddress[ 1 ], ALocalIPAddress[ 2 ], ALocalIPAddress[ 3 ] );
203
204// String ALocalIPAddressString = String( ALocalIPAddress[ 0 ] ) + "." + String( ALocalIPAddress[ 1 ] + "." + ALocalIPAddress[ 2 ] + "." + ALocalIPAddress[ 3 ];
205 SubnetMaskIPOutputPin.Notify( AIPString );
206 }
207
208 break;
209 }
210
211 }
212
213 protected:
214 void DoScanNetworks( void *_Data )
215 {
216 int nearbyAccessPointCount = LWiFi.scanNetworks();
217 for( int i = 0; i < nearbyAccessPointCount; i ++ )
218 {
219 FoundSSIDOutputPin.Notify( LWiFi.SSID( i ));
220
221 int32_t ASetrength = LWiFi.RSSI( i );
222 FoundSignalStrengthOutputPin.Notify( &ASetrength );
223 }
224 }
225
226 void DoCheckSignalStrength( void *_Data )
227 {
228 if( IsStarted )
229 if( SignalStrengthOutputPin.IsConnected() )
230 {
231 int32_t ASetrength = LWiFi.RSSI();
232 SignalStrengthOutputPin.Notify( &ASetrength );
233 }
234 }
235
236 public:
237 LinkItWiFiModule()
238 {
239 ScanNetworksInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&LinkItWiFiModule::DoScanNetworks );
240 CheckSignalStrengthInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&LinkItWiFiModule::DoCheckSignalStrength );
241 }
242
243 };
244//---------------------------------------------------------------------------
245 class LinkItWiFiTCPClientSocket : public TCPClientSocket<Mitov::LinkItWiFiModule,LWiFiClient>
246 {
247 typedef TCPClientSocket<Mitov::LinkItWiFiModule,LWiFiClient> inherited;
248
249 public:
250 virtual bool CanSend() override
251 {
252 return inherited::Enabled && inherited::FOwner.Enabled && FClient;
253 }
254
255 public:
256 using inherited::inherited;
257
258 };
259//---------------------------------------------------------------------------
260 class LinkItWiFiTCPServerSocket : public TCPServerSocket<Mitov::LinkItWiFiModule,LWiFiServer,LWiFiClient>
261 {
262 typedef TCPServerSocket<Mitov::LinkItWiFiModule,LWiFiServer,LWiFiClient> inherited;
263
264 public:
265 virtual bool CanSend() override
266 {
267 return inherited::Enabled && inherited::FOwner.Enabled && FClient;
268 }
269
270 public:
271 using inherited::inherited;
272
273 };
274//---------------------------------------------------------------------------
275 class LinkItWiFiUDPSocket : public UDPSocket<Mitov::LinkItWiFiModule,LWiFiUDP>
276 {
277 typedef UDPSocket<Mitov::LinkItWiFiModule,LWiFiUDP> inherited;
278
279 public:
280 using inherited::inherited;
281
282 };
283//---------------------------------------------------------------------------
284}
285
286#endif