LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_wifi.h
Go to the documentation of this file.
1 /*
2  * LEGO® MINDSTORMS EV3
3  *
4  * Copyright (C) 2010-2013 The LEGO Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef C_WIFI_H_
22 #define C_WIFI_H_
23 #define _GNU_SOURCE
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/stat.h>
31 #include <arpa/inet.h>
32 #include <fcntl.h>
33 #include <netdb.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <sys/un.h>
37 #include <errno.h>
38 #include <bytecodes.h>
39 
40 #include "wpa_ctrl.h"
41 
42 #include "common.h"
43 
44 #include "lms2012.h"
45 
46 #define WIFI_PERSISTENT_PATH vmSETTINGS_DIR // FileSys guidance ;-)
47 #define WIFI_PERSISTENT_FILENAME "WiFiConnections.dat" // Persistent storage for KNOWN connections
48 
49 #define MAC_ADDRESS_LENGTH 18 // xx:xx:xx:xx:xx:xx + /0x00
50 #define FREQUENCY_LENGTH 5
51 #define SIGNAL_LEVEL_LENGTH 4
52 #define SECURITY_LENGTH 129 //33
53 #define FRIENDLY_NAME_LENGTH 33
54 
55 #define PSK_LENGTH 65 // 32 bytes = 256 bit + /0x00
56 #define KEY_MGMT_LENGTH 33
57 #define PAIRWISE_LENGTH 33
58 #define GROUP_LENGTH 33
59 #define PROTO_LENGTH 33
60 
61 #define WIFI_INIT_TIMEOUT 10 //60
62 #define WIFI_INIT_DELAY 10
63 
64 #define BROADCAST_IP_LOW "255" // "192.168.0.255"
65 #define BROADCAST_PORT 3015 // UDP
66 #define TCP_PORT 5555
67 #define BEACON_TIME 5 // 5 sec's between BEACONs
68 
69 #define TIME_FOR_WIFI_DONGLE_CHECK 10
70 
71 #define BLUETOOTH_SER_LENGTH 13 // No "white" space separators
72 #define BRICK_HOSTNAME_LENGTH (NAME_LENGTH + 1)
73 
74 enum // WiFiStates
75 {
76  WIFI_NOT_INITIATED, // NOTHING INIT'ed
77  WIFI_INIT, // Do the time consumption stuff
78  WIFI_INITIATED, // Initiated and ready for AP:
79  // We have the H/W stack up and running
80  READY_FOR_AP_SEARCH, // Stack ready for further AP search
81  SEARCH_APS, // As it says
82  SEARCH_PENDING, // Time consumption search
83  AP_LIST_UPDATED, // User have forced a new search
85  WIFI_CONNECTED_TO_AP, // User has connected to an AP
86  UDP_NOT_INITIATED, // Ready for starting Beacons
87  INIT_UDP_CONNECTION, // Do the init via UDP
88  UDP_FIRST_TX, // Temporary state after start Beacon TX
89  UDP_VISIBLE, // We TX'es a lot of Beacon
90  UDP_CONNECTED, // We have an active negotiation connection
91  TCP_NOT_CONNECTED, // Waiting for the PC to connect via TCP
92  TCP_CONNECTED, // We have a TCP connection established
93  CLOSED // UDP/TCP closed
94 };
95 
96 enum // States the STACK goes through
97 {
98  NOT_INIT = 0x00,
101  DONE = 0x80
102 };
103 
104 enum // States the TCP connection can be in (READ)
105 {
106  TCP_IDLE = 0x00,
112 };
113 
114 typedef enum
115 {
118 }
120 
121 typedef enum
122 {
125 }
126 KNOWN_HW;
127 
128 typedef enum
129 {
132 }
133 TCP_STATES;
134 
135 enum // WiFi AP flags "capabilities" and state
136 {
137  VISIBLE = 0x1,
138  CONNECTED = 0x02,
139  WPA2 = 0x04,
140  KNOWN = 0x08,
141  UNKNOWN = 0x80
142 };
143 
144 #define AP_FLAG_ADJUST_FOR_STORAGE ((unsigned char)(~(VISIBLE + CONNECTED + UNKNOWN))) // Set/reset for persistent storage
145 
146 enum // The Brick WiFi state
147 {
150  SEARCHING = 0x02,
151 };
152 
153 typedef struct
154 {
155  char mac_address[MAC_ADDRESS_LENGTH]; // as it tells
156  char frequency[FREQUENCY_LENGTH]; // additional info - not used
157  char signal_level[SIGNAL_LEVEL_LENGTH]; // -
158  char security[SECURITY_LENGTH]; // Only WPA2 or NONE
159  char friendly_name[FRIENDLY_NAME_LENGTH]; // The name, the user will see aka SSID
160  char key_management[KEY_MGMT_LENGTH];
161  char pre_shared_key[PSK_LENGTH]; // Preshared Key (Encryption)
162  char pairwise_ciphers[PAIRWISE_LENGTH];
163  char group_ciphers[GROUP_LENGTH];
164  char proto[PROTO_LENGTH];
165  unsigned char ap_flags; // Holds the capabilities etc. of the AP
166 }
167 aps;
168 
169 // Common Network stuff
170 // --------------------
171 
172 RESULT cWiFiGetIpAddr(char* IpAddress);
173 
174 RESULT cWiFiGetMyMacAddr(char* MacAddress);
175 
176 RESULT cWiFiKnownDongleAttached(void); // Known H/W
177 
178 //UDP functions
179 //-------------
180 
181 RESULT cWiFiTxingBeacons(void); // Are we active in tx beacons??
182 
183 void cWiFiUdpClientClose(void);
184 
185 // TCP functions
186 // -------------
187 
188 RESULT cWiFiTcpConnected(void); // TCP connection established?
189 
190 //RESULT cWiFiTcpTempClose(void);
191 
193 
195 
196 // WPA and AP stuff
197 // ----------------
198 void cWiFiMoveUpInList(int Index); // Direct UI function
199 
200 void cWiFiMoveDownInList(int Index); // Direct UI function
201 
202 void cWiFiDeleteInList(int Index); // Direct UI function
203 
204 RESULT cWiFiGetApMacAddr(char* MacAddr, int Index);
205 
206 RESULT cWiFiGetHiddenMacAddr(char* MacAddr, int Index);
207 
208 RESULT cWiFiGetName(char *ApName, int Index, char Length); // Get the FriendlyName owned by ApTable[Index]
209 
210 RESULT cWiFiSetName(char *ApName, int Index); // Set the FriendlyName @ ApTable[Index] // Hidden!?
211 
212 RESULT cWiFiSetSsid(char *Ssid);
213 
214 RESULT cWiFiSetKeyManagToWpa2(void);
215 
216 RESULT cWiFiSetKeyManagToNone(void);
217 
218 RESULT cWiFiGetIndexFromName(char *Name, UBYTE *Index);
219 
220 void cWiFiSetEncryptToWpa2(int Index);
221 
222 void cWiFiSetEncryptToNone(int Index);
223 
224 void cWiFiSetKnown(int Index);
225 
226 void cWiFiDeleteAsKnown(int LocalIndex);
227 
228 unsigned char cWiFiGetFlags(int Index); // Get Flags owned by ApTable[Index]
229 
230 RESULT cWiFiConnectToAp(int Index);
231 
232 RESULT cWiFiMakePsk(char *ApSsid, char *PassPhrase, int Index);
233 
234 int cWiFiGetApListSize(void);
235 
236 void cWiFiIncApListSize(void);
237 
238 RESULT cWiFiAddHidden(char *HiddenApName, char *Security, char *PassWord);
239 
240 RESULT cWiFiScanForAPs(void);
241 
242 RESULT cWiFiGetOnStatus(void);
243 
244 // Common Control
245 // --------------
246 
247 RESULT cWiFiGetStatus(void);
248 
249 void cWiFiSetBtSerialNo(void);
250 
251 void cWiFiSetBrickName(void);
252 
253 void cWiFiControl(void);
254 
255 RESULT cWiFiTurnOn(void); // TURN ON
256 
257 RESULT cWiFiTurnOff(void); // TURN OFF
258 
259 RESULT cWiFiExit(void);
260 
261 RESULT cWiFiInit(void);
262 
263 #endif /* C_WIFI_H_ */
264 
RESULT cWiFiTcpConnected(void)
Definition: c_wifi.c:2619
void cWiFiControl(void)
Definition: c_wifi.c:3462
#define GROUP_LENGTH
Definition: c_wifi.h:58
#define PSK_LENGTH
Definition: c_wifi.h:55
RESULT cWiFiTxingBeacons(void)
Definition: c_wifi.c:2611
Definition: c_wifi.h:153
RESULT cWiFiGetStatus(void)
Definition: c_wifi.c:2601
#define MAC_ADDRESS_LENGTH
Definition: c_wifi.h:49
TCP_STATES
Definition: c_wifi.h:128
void cWiFiSetBrickName(void)
Definition: c_wifi.c:3247
RESULT cWiFiGetOnStatus(void)
Definition: c_wifi.c:3875
#define FRIENDLY_NAME_LENGTH
Definition: c_wifi.h:53
Definition: c_wifi.h:139
RESULT cWiFiSetKeyManagToWpa2(void)
Definition: c_wifi.c:979
void cWiFiDeleteAsKnown(int LocalIndex)
Definition: c_wifi.c:1112
RESULT cWiFiGetIpAddr(char *IpAddress)
Definition: c_wifi.c:1197
RESULT cWiFiGetApMacAddr(char *MacAddr, int Index)
Definition: c_wifi.c:1064
Definition: c_wifi.h:131
RESULT cWiFiAddHidden(char *HiddenApName, char *Security, char *PassWord)
Definition: c_wifi.c:1119
RESULT cWiFiConnectToAp(int Index)
Definition: c_wifi.c:2011
unsigned char ap_flags
Definition: c_wifi.h:165
BEACON_MODE
Definition: c_wifi.h:114
void cWiFiSetKnown(int Index)
Definition: c_wifi.c:1107
RESULT cWiFiTurnOn(void)
Definition: c_wifi.c:3885
#define PROTO_LENGTH
Definition: c_wifi.h:59
#define FREQUENCY_LENGTH
Definition: c_wifi.h:50
RESULT cWiFiInit(void)
Definition: c_wifi.c:4006
RESULT cWiFiGetName(char *ApName, int Index, char Length)
Definition: c_wifi.c:1681
void cWiFiMoveDownInList(int Index)
Definition: c_wifi.c:586
RESULT cWiFiGetIndexFromName(char *Name, UBYTE *Index)
Definition: c_wifi.c:2152
RESULT cWiFiExit(void)
Definition: c_wifi.c:3998
RESULT cWiFiTurnOff(void)
Definition: c_wifi.c:3929
void cWiFiSetBtSerialNo(void)
Definition: c_wifi.c:3234
int cWiFiGetApListSize(void)
Definition: c_wifi.c:2544
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
Definition: lmstypes.h:29
void cWiFiDeleteInList(int Index)
Definition: c_wifi.c:597
#define SIGNAL_LEVEL_LENGTH
Definition: c_wifi.h:51
RESULT cWiFiKnownDongleAttached(void)
Definition: c_wifi.c:1311
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
Definition: lmstypes.h:30
void cWiFiUdpClientClose(void)
Definition: c_wifi.c:577
char Buffer[1024]
Definition: c_wifi.c:102
RESULT cWiFiScanForAPs(void)
Definition: c_wifi.c:2477
Definition: c_wifi.h:93
RESULT cWiFiSetKeyManagToNone(void)
Definition: c_wifi.c:1006
unsigned char cWiFiGetFlags(int Index)
Definition: c_wifi.c:1726
RESULT cWiFiGetHiddenMacAddr(char *MacAddr, int Index)
Definition: c_wifi.c:1156
RESULT cWiFiSetName(char *ApName, int Index)
Definition: c_wifi.c:1699
Definition: c_wifi.h:101
void cWiFiSetEncryptToNone(int Index)
Definition: c_wifi.c:1099
#define SECURITY_LENGTH
Definition: c_wifi.h:52
RESULT cWiFiGetMyMacAddr(char *MacAddress)
Definition: c_wifi.c:1209
#define KEY_MGMT_LENGTH
Definition: c_wifi.h:56
RESULT cWiFiMakePsk(char *ApSsid, char *PassPhrase, int Index)
Definition: c_wifi.c:2078
void cWiFiMoveUpInList(int Index)
Definition: c_wifi.c:465
RESULT cWiFiSetSsid(char *Ssid)
Definition: c_wifi.c:927
#define PAIRWISE_LENGTH
Definition: c_wifi.h:57
KNOWN_HW
Definition: c_wifi.h:121
UWORD cWiFiReadTcp(UBYTE *Buffer, UWORD Length)
Definition: c_wifi.c:2824
Definition: c_wifi.h:140
void cWiFiSetEncryptToWpa2(int Index)
Definition: c_wifi.c:1076
void cWiFiIncApListSize(void)
Definition: c_wifi.c:2536
Definition: c_wifi.h:116
UWORD cWiFiWriteTcp(UBYTE *Buffer, UWORD Length)
Definition: c_wifi.c:2757