LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_wifi.c
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 
39 #include "c_wifi.h"
40 
41 
42 // Volatile data
43 // Used for search AP's (all active - known/new)
44 #define MAX_AP_ENTRIES 16
45 static aps ApTable[MAX_AP_ENTRIES];
46 static int ApTableSize = 0;
47 
48 // Persistent data
49 // Used for storing OLD and KNOWN AP's
50 #define MAX_AP_STORAGE_ENTRIES 16
51 static aps ApStoreTable[MAX_AP_STORAGE_ENTRIES];
52 static int ApStoreTableSize;
53 
54 static aps TempApStorage; // As the name says - used when moving around in the list(s)
55 
56 static char BtSerialNo[13]; // Storage for the BlueTooth Serial Number
57 
58 static char BrickName[NAME_LENGTH + 1]; // BrickName for discovery and/or friendly info
59 
60 static char MyIp4Address[16]; // Our / "The local IP" address
61 
62 static char MyBroadCastAdr[16]; // Broadcast address (the MASK included)
63 
64 static char MyHwMacAddress[18]; // HardWare MAC address
65 
66 static char LogicalIfName[6]; //Only wlan0->wlan9 + /0x0
67 
68 /*// Known friends - i.e. friendly pc's etc. STORED as a PASS-NAME
69 #define MAX_ACCESS_NAME_LENGTH 20
70 #define MAX_ACCESS_NAME_COUNT 20
71 static char AccessNameTable[MAX_ACCESS_NAME_COUNT][MAX_ACCESS_NAME_LENGTH];
72 */
73 
74 #define WPA_SUPPLICANT_GLOBAL_SOCKET "/var/run/wpa_supplicant-global"
75 #define WPA_SUPPLICANT_CONTROL_SOCKET "/var/run/wpa_supplicant"
76 
77 // The HARDWARE_SEARCH_STRING is hardcoded for now - future releases will/should have
78 // the HARDWARE_SEARCH_STRINGS (S!) loaded from a file which has an ASCII definition
79 // of available H/W and a description of the drivers to load. I.e. a simple and download-
80 // able way of integrating new and more drivers. NO commandline stuff.
81 
82 #define HARDWARE_SEARCH_STRING "ID 0846:9030 NetGear, Inc."
83 
84 #define WIFI_VENDOR_ID "0846"
85 #define WIFI_DONGLE_ID "9030"
86 
87 
88 // The following could be set to variable/editable strings via methodes, but for now....
89 
90 //static char* ControlPath = "/var/run/wpa_supplicant/";
91 //static char* CtrlConfigPathName = "/etc/wpa_supplicant.conf";
92 
93 #define WPA_APPLIKATION_PATH "" // Not defined yet
94 
95 static struct wpa_ctrl *ctrl_conn; // Control "handle" for the wpa_control interface
96 
97 struct timeval TimerStartVal, TimerCurrentVal;
98 struct timeval DongleCheckStartVal, DongleCheckCurrentVal;
99 
100 unsigned int TimeOut = 0;
101 
102 char Buffer[1024];
103 char *BufPtr = Buffer;
104 int BufLen = sizeof(Buffer);
105 
107 
110 
113 // UDP
115 char Server[32]; // PlaceHolder for the UDP listen Server Name/IP
117 struct sockaddr_in ServerAddr;
118 socklen_t ServerAddrLen = sizeof(ServerAddr);
120 struct timeval UdpWait;
121 int BroadCast = 1;
124 int WiFiStatus = OK;
125 RESULT WiFiOnStatus = FAIL;
126 int TcpConnectionSocket = 0; /* connection socket */
127 struct sockaddr_in servaddr, client_addr;
132 
133 // ******************************************************************************
134 
135 void cWiFiStartTimer(void) // Start the Timer
136 {
137  gettimeofday(&TimerStartVal, NULL);
138 }
139 
140 RESULT cWiFiKillUdHcPc(void)
141 {
142  RESULT Ret = FAIL;
143  char Cmd[48];
144 
145  // Only one instance at a time :-)
146 
147  #undef DEBUG
148  //#define DEBUG
149  #ifdef DEBUG
150  printf("We'll try to kill udhcpc\r\n");
151  #endif
152  strcpy(Cmd, "killall udhcpc &> /dev/null");
153 
154  #undef DEBUG
155  //#define DEBUG
156  #ifdef DEBUG
157  printf("\r\nCmd = %s\r\n", Cmd);
158  #endif
159 
160  if(system(Cmd) == 0)
161  {
162  #undef DEBUG
163  //#define DEBUG
164  #ifdef DEBUG
165  printf("\r\nThe kill Cmd returns OK!!\r\n");
166  #endif
167 
168  Ret = OK;
169  }
170 
171  return Ret;
172 }
173 
175 {
176  // Get starttime
177  gettimeofday(&DongleCheckStartVal, NULL);
178 }
179 
181 {
182  // Get elapsed time from last dongle check
183  gettimeofday(&DongleCheckCurrentVal, NULL);
184  return (int)(DongleCheckCurrentVal.tv_sec - DongleCheckStartVal.tv_sec);
185 }
186 
187 int cWiFiCheckTimer(void) // Get Elapsed time in seconds
188 {
189  // Get actual time and calculate elapsed time
190  gettimeofday(&TimerCurrentVal, NULL);
191  return (int)(TimerCurrentVal.tv_sec - TimerStartVal.tv_sec);
192 }
193 
194 // Used primarily for debug (development :-))
195 void wpa_control_message_callback(char *message, size_t length)
196 {
197  // #define DEBUG
198  #undef DEBUG
199  #ifdef DEBUG
200  printf("%s\n", message);
201  #endif
202 }
203 
204 int do_wpa_command(struct wpa_ctrl * control, char *command )
205 {
206  char command_return[10];
207  size_t len_command_return = sizeof(command_return) - 1; // Leave space for a terminating ASCIIZ
208  int ret_value;
209 
210  memset(command_return, 0x00, len_command_return); // Reset to be sure!!
211 
212  // add more "nanny"ware look into good ref.... 8-)
213  if (control != NULL)
214  {
215  ret_value = wpa_ctrl_request( control,
216  command,
217  strlen(command),
218  command_return,
219  &len_command_return,
221 
222  command_return[len_command_return] = '\0';
223  }
224  else
225  ret_value = -1;
226 
227  return ret_value;
228 }
229 
230 // Opens - i.e. startup the WpaSupplicant in the background
231 
232 int cWiFiStartWpaSupplicant(char* ConfigPath,char* IFName)
233 {
234  // Fire up the WPA Supplicant - returns 0 (zero) if OK
235  char StartCmd[128];
236 
237  // Build the command string for startting the Control
238  strcpy(StartCmd, "./wpa_supplicant -Dwext -i");
239  strcat(StartCmd, IFName);
240  strcat(StartCmd, " -c");
241  strcat(StartCmd, ConfigPath);
242  strcat(StartCmd, " -B");
243  strcat(StartCmd, "&> /dev/null");
244 
245  #undef DEBUG
246  #ifdef DEBUG
247  printf("\r\nStart WPA_Supplicant: %s\r\n", StartCmd);
248  #endif
249  return system(StartCmd);
250 }
251 
252 struct wpa_ctrl * cWiFiOpenCtrlInterFace(char *CtrlPath, char *InterFace)
253 {
254  char OpenCtrlCmd[64];
255  strcpy(OpenCtrlCmd, CtrlPath);
256  strcat(OpenCtrlCmd, InterFace);
257  return wpa_ctrl_open(OpenCtrlCmd);
258 }
259 
260 int cWiFiPopulateKnownApList(void) // At startup
261 { // Read already know "stuff" from the persistent storage
262  int RetVal = 0;
263  char FileName[128];
264  FILE *PersistentFile = NULL;
265  int ArrayIterator = 0;
266  aps OneApRecord;
267 
268  strcpy(FileName, WIFI_PERSISTENT_PATH);
269  strcat(FileName, "/");
270  strcat(FileName, WIFI_PERSISTENT_FILENAME);
271 
272  ApStoreTableSize = 0; // ZERO size - NO known - if none in file
273  PersistentFile = fopen(FileName, "rb");
274 
275  //#define DEBUG
276  #undef DEBUG
277  #ifdef DEBUG
278  printf("\r\nKnownlist:\r\n");
279  #endif
280 
281  if ( PersistentFile != NULL )
282  {
283  while (fread(&OneApRecord, sizeof OneApRecord, 1, PersistentFile) == 1)
284  {
285  memcpy(&(ApStoreTable[ArrayIterator++]), &OneApRecord, sizeof(aps));
286 
287  //#define DEBUG
288  #undef DEBUG
289  #ifdef DEBUG
290  printf("Populating ApStoreTable[x] == KnownList\n\r");
291  printf("\r\nApStoreTable[%d].mac_address = %s\r\n", (ArrayIterator - 1), ApStoreTable[ArrayIterator - 1].mac_address);
292  printf("\r\nApStoreTable[%d].security = %s\r\n", (ArrayIterator - 1), ApStoreTable[ArrayIterator - 1].security);
293  printf("\r\nApStoreTable[%d].friendly_name = %s\r\n", (ArrayIterator - 1), ApStoreTable[ArrayIterator - 1].friendly_name);
294  printf("\r\nApStoreTable[%d].pre_shared_key = %s\r\n", (ArrayIterator - 1), ApStoreTable[ArrayIterator - 1].pre_shared_key);
295  printf("\r\nApStoreTable[%d].ap_flags = %d\r\n", (ArrayIterator - 1), ApStoreTable[ArrayIterator - 1].ap_flags);
296  #endif
297  }
298  fclose(PersistentFile);
299  ApStoreTableSize = ArrayIterator; // Store the KNOWN table size
300 
301 //***************************************** TEST STUFF **********************************
302  //
303  // Used for a stable "environment" - i.e. the AP's are stable and always there :-)
304  //
305  //#define TEST
306  #undef TEST
307  #ifdef TEST
308  printf("\r\nSimulated KnownList:\r\n");
309 
310  ApStoreTableSize = 5;
311 
312  strcpy(ApStoreTable[0].mac_address, "00:24:01:43:69:6e");
313  strcpy(ApStoreTable[0].security, "[WPA2-PSK-CCMP][WPS][ESS]");
314  strcpy(ApStoreTable[0].friendly_name, "FFF");
315  strcpy(ApStoreTable[0].pre_shared_key, "dbc9c6cf6d4d8e9405842d2e4ab726f33f10de62c30254ff19512af0f");
316  ApStoreTable[0].ap_flags = 0x0C;
317 
318  strcpy(ApStoreTable[1].mac_address, "00:24:01:43:69:01");
319  strcpy(ApStoreTable[1].security, "[WPA2]");
320  strcpy(ApStoreTable[1].friendly_name, "OOO");
321  strcpy(ApStoreTable[1].pre_shared_key, "");
322  ApStoreTable[1].ap_flags = 0x0C;
323 
324  strcpy(ApStoreTable[2].mac_address, "00:24:01:43:69:02");
325  strcpy(ApStoreTable[2].security, "[WPA2-PSK-CCMP]");
326  strcpy(ApStoreTable[2].friendly_name, "AAA");
327  strcpy(ApStoreTable[2].pre_shared_key, "");
328  ApStoreTable[2].ap_flags = 0x0C;
329 
330  strcpy(ApStoreTable[3].mac_address, "00:24:01:43:69:03");
331  strcpy(ApStoreTable[3].security, "[WPA2-PSK-CCMP][ESS]");
332  strcpy(ApStoreTable[3].friendly_name, "JJJ");
333  strcpy(ApStoreTable[3].pre_shared_key, "");
334  ApStoreTable[3].ap_flags = 0x0C;
335 
336  strcpy(ApStoreTable[4].mac_address, "00:24:01:43:69:04");
337  strcpy(ApStoreTable[4].security, "[WPA2-PSK][ESS]");
338  strcpy(ApStoreTable[4].friendly_name, "KKK");
339  strcpy(ApStoreTable[4].pre_shared_key, "");
340  ApStoreTable[4].ap_flags = 0x0C;
341 
342  /*strcpy(ApStoreTable[5].mac_address, "00:24:01:43:69:05");
343  strcpy(ApStoreTable[5].security, "");
344  strcpy(ApStoreTable[5].friendly_name, "Hugo05");
345  strcpy(ApStoreTable[5].pre_shared_key, "");
346  ApStoreTable[5].ap_flags = 0x08;*/
347  #endif
348 
349 //*********************************** End of TEST STUFF **********************************
350 
351  }
352  else
353  {
354  RetVal = -1;
355  }
356 
357  return RetVal;
358 }
359 
360 int cWiFiStoreKnownApList(void) // At exit of main application
361 { // Store known "stuff" in the (new) correct order
362  int RetVal = 0;
363  char FileName[128];
364  FILE *PersistentFile = NULL;
365  int ArrayIterator = 0;
366  aps OneApRecord;
367 
368  strcpy(FileName, WIFI_PERSISTENT_PATH);
369  strcat(FileName, "/");
370  strcat(FileName, WIFI_PERSISTENT_FILENAME);
371 
372  //#define DEBUG
373  #undef DEBUG
374  #ifdef DEBUG
375  printf("AP_FLAG_ADJUST_FOR_STORAGE = %X\n\r", AP_FLAG_ADJUST_FOR_STORAGE);
376  printf("Persistent FileName = %s ApStoreTableSize = %d\n\r", FileName, ApStoreTableSize);
377  #endif
378 
379  PersistentFile = fopen(FileName, "wb");
380  if ( PersistentFile != NULL )
381  {
382  for(ArrayIterator = 0; ArrayIterator < ApStoreTableSize; ArrayIterator++)
383  {
384 
385  memcpy(&OneApRecord, &(ApStoreTable[ArrayIterator]), sizeof(aps));
386 
387  OneApRecord.ap_flags &= AP_FLAG_ADJUST_FOR_STORAGE;
388 
389  //#define DEBUG
390  #undef DEBUG
391  #ifdef DEBUG
392  printf("cWiFiStoreKnownApList ApStoreTableSize = %d, ArrayIterator = %d\n\r", ApStoreTableSize, ArrayIterator);
393  printf("OneApRecord.ap_flags before: %X\n\r", OneApRecord.ap_flags);
394  #endif
395 
396  fwrite(&OneApRecord, sizeof OneApRecord, 1, PersistentFile);
397 
398  #undef DEBUG
399  //#define DEBUG
400  #ifdef DEBUG
401  printf("OneApRecord.ap_flags after, just after save: %X\n\r", OneApRecord.ap_flags);
402  printf("Writing KnownList I.e. ApStoreTable[x] to Persistent file\n\r");
403  printf("\r\nApStoreTable[%d].mac_address = %s\r\n", (ArrayIterator), ApStoreTable[ArrayIterator].mac_address);
404  printf("\r\nApStoreTable[%d].security = %s\r\n", (ArrayIterator), ApStoreTable[ArrayIterator].security);
405  printf("\r\nApStoreTable[%d].friendly_name = %s\r\n", (ArrayIterator), ApStoreTable[ArrayIterator].friendly_name);
406  printf("\r\nApStoreTable[%d].pre_shared_key = %s\r\n", (ArrayIterator), ApStoreTable[ArrayIterator].pre_shared_key);
407  printf("\r\nApStoreTable[%d].ap_flags = %d\r\n", (ArrayIterator), ApStoreTable[ArrayIterator].ap_flags);
408  #endif
409  }
410  RetVal = ApStoreTableSize;
411  fclose(PersistentFile);
412 
413  }
414  else
415  {
416  #undef DEBUG
417  //#define DEBUG
418  #ifdef DEBUG
419  printf("Handle to PersistentFile == NULL :-(\n\r");
420  #endif
421 
422  RetVal = -1;
423  }
424  return RetVal;
425 }
426 
427 void cWiFiRestoreActualApRecord(int Destination) // Restore a preserved record to ACTUAL
428 {
429  memcpy(&(ApTable[Destination].mac_address), &TempApStorage, sizeof(aps));
430 }
431 
432 void cWiFiPreserveActualApRecord(int Index) // Preserve an ACTUAL record
433 {
434  memcpy(&TempApStorage, &(ApTable[Index].mac_address), sizeof(aps));
435 }
436 
437 void cWiFiMoveUpInStoreList(int Index)
438 {
439  // Make entry "n" become "n-1" i.e. index--
440  if(Index > 0)
441  memcpy(&(ApStoreTable[Index - 1]), &(ApStoreTable[Index]), sizeof(aps));
442 }
443 
445 {
446  // Make entry "n" become "n+1" i.e. index++
447  if(Index < (MAX_AP_STORAGE_ENTRIES - 2))
448  memcpy(&(ApStoreTable[Index + 1]), &(ApStoreTable[Index]), sizeof(aps));
449 }
450 
451 void cWiFiMoveUpInActualList(int Index)
452 {
453  // Make "n" become "n-1" - lower index, but higher order (newer/active etc.)
454  if(Index > 0)
455  memcpy(&(ApTable[Index - 1]), &(ApTable[Index]), sizeof(aps));
456 }
457 
459 {
460  // Make "n" become "n+1" - lower priority (older/inactive etc.)
461  if(Index < (MAX_AP_ENTRIES - 2))
462  memcpy(&(ApTable[Index + 1]), &(ApTable[Index]), sizeof(aps));
463 }
464 
465 void cWiFiMoveUpInList(int Index) // Direct UI function
466 {
467  // Make "n" become "n-1" and "n-1" moved down to "n"
468 
469  if(Index > 0) // We can move up
470  {
471  cWiFiPreserveActualApRecord(Index - 1);
472  memcpy(&(ApTable[Index - 1]), &(ApTable[Index]), sizeof(aps)); // n to n-1
474  }
475 }
476 
477 RESULT cWiFiRemoveNetwork(void)
478 {
479 RESULT Ret = FAIL;
480  char CmdReturn[10];
481  int RetVal;
482  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
483 
484  //#define DEBUG
485  #undef DEBUG
486  #ifdef DEBUG
487  printf("Remove Network called\n\r");
488  #endif
489 
490  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
491 
492  if(ctrl_conn != NULL)
493  {
494  RetVal = wpa_ctrl_request( ctrl_conn,
495  "REMOVE_NETWORK all",
496  strlen("REMOVE_NETWORK all"),
497  CmdReturn,
498  &LenCmdReturn,
499  NULL);
500  sleep(3); // Force some cycles
501  CmdReturn[LenCmdReturn] = '\0';
502 
503  #undef DEBUG
504  //#define DEBUG
505  #ifdef DEBUG
506  printf("Remove returns %s - RetVal = %d\n\r", CmdReturn, RetVal);
507  #endif
508 
509  if(strstr(CmdReturn, "OK") != NULL)
510  {
511  //#define DEBUG
512  #undef DEBUG
513  #ifdef DEBUG
514  printf("Remove Network returned OK\n\r");
515  #endif
516  Ret = OK;
517  }
518  else
519  {
520  //#define DEBUG
521  #undef DEBUG
522  #ifdef DEBUG
523  printf("Remove Network was NOT OK :-(\n\r");
524  #endif
525  }
526  }
527  return Ret;
528 }
529 
530 RESULT cWiFiTcpClose(void)
531 {
532  int res;
533  UBYTE buffer[128];
534  RESULT Result = OK;
535 
536  WiFiStatus = BUSY;
537 
538  int z; /* Status code */
539 
540  struct linger so_linger;
541 
542  so_linger.l_onoff = TRUE;
543  so_linger.l_linger = 0;
544  z = setsockopt(TcpConnectionSocket,
545  SOL_SOCKET,
546  SO_LINGER,
547  &so_linger,
548  sizeof so_linger);
549 
550  if ( shutdown(TcpConnectionSocket, 2) < 0 )
551  {
552  do
553  {
554  #undef DEBUG
555  //#define DEBUG
556  #ifdef DEBUG
557  printf("In the do_while\r\n");
558  #endif
559  res = read(TcpConnectionSocket, buffer, 100);
560  if(res < 0 ) break;
561  }
562  while (res != 0);
563 
564  #undef DEBUG
565  //#define DEBUG
566  #ifdef DEBUG
567  printf("\r\nError calling Tcp shutdown()\r\n");
568  #endif
569  }
570 
572  WiFiStatus = OK; // We are at a known state :-)
574  return Result;
575 }
576 
578 {
579  WiFiStatus = FAIL; // Async announcement of FAIL
581  BeaconTx = NO_TX; // Disable Beacon
582  if(close(UdpSocketDescriptor) == 0)
583  WiFiStatus = OK; // Socket kill
584 }
585 
586 void cWiFiMoveDownInList(int Index) // Direct UI function
587 {
588  // Make "n" become "n+1" and "n+1" moved up to "n"
589  if(Index < (MAX_AP_ENTRIES - 2))
590  {
591  cWiFiPreserveActualApRecord(Index + 1);
592  memcpy(&(ApTable[Index + 1]), &(ApTable[Index]), sizeof(aps));
594  }
595 }
596 
597 void cWiFiDeleteInList(int Index) // Direct UI function
598 {
599  // Delete "n" - making "n+1, n+2" => "n, n+1"
600  int i;
601 
602  WiFiStatus = FAIL; // Until other....
603 
604  if((Index < ApTableSize) && ((ApTable[0].ap_flags & CONNECTED) != CONNECTED)) // DO NOT outside or ACTIVE!!
605  {
606  for(i = Index + 1; i < (ApTableSize + 1); i++)
607  {
608  memcpy(&(ApTable[i - 1]), &(ApTable[i]), sizeof(aps));
609  }
610  ApTableSize--;
611  WiFiStatus = OK; // Somecleaning done :-)
612  }
613 }
614 
615 void cWiFiDeleteInStoreList(int Index)
616 {
617  // Delete "n" - making "n+1, n+2" => "n, n+1"
618  int i;
619 
620  if(Index < ApStoreTableSize)
621  {
622  for(i = Index; i < (ApStoreTableSize - 1); i++)
623  {
624  memcpy(&(ApStoreTable[i]), &(ApStoreTable[i + 1]), sizeof(aps));
625  }
626  ApStoreTableSize--;
627  }
628 }
629 
630 void cWifiMoveAllActualDown(int SourcePointer) // Lower priority
631 {
632  int i;
633 
634  i = SourcePointer - 1;
635  do
636  {
638  }
639  while(i >= 0);
640 }
641 
642 void cWiFiMoveAllStoreDown(int SourcePointer) // Lower priority
643 {
644  int i;
645 
646  i = SourcePointer - 1;
647  do
648  {
650  }
651  while(i >= 0);
652 }
653 
654 void cWiFiCopyStoreToActual(int StoreIndex, int ActualIndex) // Move from STORED to ACTUAL
655 {
656  memcpy(&(ApTable[StoreIndex]), &(ApStoreTable[ActualIndex]), sizeof(aps));
657 }
658 
659 void cWiFiCopyActualToStore(int ActualIndex, int StoreIndex) // Move from ACTUAL to STORED
660 {
661  memcpy(&(ApStoreTable[StoreIndex]), &(ApTable[ActualIndex]), sizeof(aps));
662 }
663 
664 RESULT cWiFiCheckAndDelete(int Index) // Check for entry in STORED list - if present -> DELETE it
665 {
666  int FoundIndex;
667  RESULT Found = FAIL;
668  int i;
669 
670  for(i = 0; i < ApStoreTableSize; i++)
671  {
672  if(strcmp(ApTable[Index].mac_address, ApStoreTable[i].mac_address) == 0)
673  {
674  FoundIndex = i;
675  Found = OK;
676  }
677  }
678  if (Found == OK)
679  {
680  cWiFiDeleteInStoreList(FoundIndex);
681  // If found and deleted
682  ApStoreTableSize--;
683  }
684  return Found; // true if FOUND and DELETED
685 }
686 
687 void cWiFiAddToKnownApList(int Index) // When a connection is made to a new AP
688 {
689  // Place in Persistent data as TOP item - ALWAYS
690  int SourcePointer;
691 
692  // First adjust for storage - is the table already full??
693  if(ApStoreTableSize < (MAX_AP_STORAGE_ENTRIES - 1))
694  {
695  SourcePointer = ApStoreTableSize;
696  }
697  else
698  {
699  SourcePointer = ApStoreTableSize-1;
700  }
701 
702  // Move all entries one down
703  cWiFiMoveAllStoreDown(SourcePointer);
704  // Copy to TOP
705  cWiFiCopyActualToStore(Index, 0);
706 
707  ApStoreTableSize++;
708  if(ApStoreTableSize > (MAX_AP_STORAGE_ENTRIES - 1))
709  ApStoreTableSize = (MAX_AP_STORAGE_ENTRIES - 1);
710 }
711 
712 void cWiFiPreserveStorageApRecord(int Index) // Preserve a STORED record
713 {
714  memcpy(&TempApStorage, &(ApStoreTable[Index].mac_address), sizeof(aps));
715 }
716 
717 void cWiFiRestoreStorageApRecord(int Destination) // Restore a preserved record to STORED
718 {
719  memcpy(&(ApStoreTable[Destination].mac_address), &TempApStorage, sizeof(aps));
720 }
721 
723 {
724  // Merge newly scanned and visible AP's with the KNOWN LIST
725  // Prioritized - LAST used first, second used next (every-
726  // thing visible and known) also prioritized first.
727  // 1) KNOWN and VISIBLE first
728  // 2) VISIBLE
729  // 3) KNOWN but NOT visible
730  int KnownListIterator;
731  int VisibleListIterator;
732  RESULT Found = FAIL;
733  int KnownCounter = 0; // Points into the KNOWN part of the scanned
734  int i;
735 
736  for (KnownListIterator = 0; KnownListIterator < ApStoreTableSize; KnownListIterator++)
737  {
738  VisibleListIterator = 0;
739  Found = FAIL;
740 
741  do
742  {
743  if(strcmp(ApTable[VisibleListIterator].mac_address, ApStoreTable[KnownListIterator].mac_address) == 0) // They're equal
744  { // Known and visible
745  // Move this Known to ApTable[KnownListIterator]
746 
747  // First Save entry @ ApTable[VisibleListIterator]
748 
749  // Then move all between KnownCounter (topmost) and VisibleListIterator (lowest) 1 position down
750 
751  for(i = VisibleListIterator; i > KnownCounter; i--)
752  {
754  }
755 
756  cWiFiCopyStoreToActual(KnownListIterator, KnownCounter); // Move from STORED to ACTUAL
757 
758  ApTable[KnownCounter].ap_flags |= (KNOWN + VISIBLE); // Set flags
759 
760  Found = OK;
761  KnownCounter++; // Used for populate rest of list with KNOWN if any
762  }
763 
764  if(Found == FAIL)
765  {
766  VisibleListIterator++;
767  if(VisibleListIterator >= MAX_AP_ENTRIES)
768  {
769  Found = OK; // Even if NOT found
770  // Add to KNOWN but NOT visible in bottom of list
771  cWiFiCopyStoreToActual(ApTableSize, KnownListIterator);
772 
773  ApTable[ApTableSize].ap_flags |= KNOWN;
774  ApTable[ApTableSize].ap_flags &= ~(CONNECTED);
775  ApTable[ApTableSize].ap_flags &= ~(VISIBLE);
776  ApTableSize++;
777  }
778  }
779  }
780  while(Found == FAIL);
781  }
782  return OK;
783 }
784 
785 RESULT cWiFiTerminate(void)
786 {
787  RESULT Ret = FAIL;
788  char CmdReturn[10];
789  int RetVal;
790  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
791 
792  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
793 
794  if(ctrl_conn != NULL)
795  {
796  RetVal = wpa_ctrl_request( ctrl_conn,
797  "TERMINATE",
798  strlen("TERMINATE"),
799  CmdReturn,
800  &LenCmdReturn,
801  NULL);
802  CmdReturn[LenCmdReturn] = '\0';
803 
804  #undef DEBUG
805  //#define DEBUG
806  #ifdef DEBUG
807  printf("WiFi terminate : RetVal %d , CmdReturn %s\n\r", RetVal, CmdReturn);
808  #endif
809 
810  if(strstr(CmdReturn, "OK") != NULL)
811  {
812  Ret = OK;
813  }
814  }
815  return Ret;
816 }
817 
818 RESULT cWiFiAddNetwork(void)
819 {
820  RESULT Ret = FAIL;
821  char aCmdReturn[128];
822  int RetVal;
823  size_t LenaCmdReturn = sizeof(aCmdReturn) - 1; // We leave space for a terminating /0x00
824 
825  memset(aCmdReturn, 0x00, LenaCmdReturn); // Reset to be sure!!
826 
827  #undef DEBUG
828  //#define DEBUG
829  #ifdef DEBUG
830  printf("Beginning of AddNetwork\n\r");
831  #endif
832 
833  if(ctrl_conn != NULL)
834  {
835  #undef DEBUG
836  //#define DEBUG
837  #ifdef DEBUG
838  printf("Internal in AddNetwork - before the real call\n\r");
839  #endif
840 
841  RetVal = wpa_ctrl_request( ctrl_conn,
842  "ADD_NETWORK",
843  strlen("ADD_NETWORK"),
844  aCmdReturn,
845  &LenaCmdReturn,
846  NULL);
847  sleep(3); // Force some cycles
848  aCmdReturn[LenaCmdReturn] = '\0';
849 
850  #undef DEBUG
851  //#define DEBUG
852  #ifdef DEBUG
853  printf("Add returns %s - RetVal = %d\n\r", aCmdReturn, RetVal);
854  #endif
855 
856  if((aCmdReturn[0] == '0') && (aCmdReturn[1] == 10)) // LF
857  {
858  #undef DEBUG
859  //#define DEBUG
860  #ifdef DEBUG
861  printf("0 (zero) in return at AddNetwork\n\r");
862  #endif
863 
864  Ret = OK;
865  }
866  else
867  {
868  #undef DEBUG
869  //#define DEBUG
870  #ifdef DEBUG
871  printf("AddNetwork NOT OK 0 = %d, 1 = %d !!\r\n", aCmdReturn[0], aCmdReturn[1]);
872  #endif
873  }
874  }
875  return Ret;
876 }
877 
878 RESULT cWiFiSetScanSsidToOne(void) // The WPA_Supplicant should also be happy, when APs are invisible
879 {
880  RESULT Ret = FAIL;
881  char CmdReturn[10];
882  char Cmd[128];
883  int RetVal;
884  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
885 
886  WiFiStatus = BUSY;
887  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
888 
889  #undef DEBUG
890  //#define DEBUG
891  #ifdef DEBUG
892  printf("\n\rEntry of SET_ScanSsidToOne to ONE\n\r");
893  #endif
894 
895  if(ctrl_conn != NULL)
896  {
897  strcpy(Cmd, "SET_NETWORK 0 scan_ssid 1");
898 
899  RetVal = wpa_ctrl_request( ctrl_conn,
900  Cmd,
901  strlen(Cmd),
902  CmdReturn,
903  &LenCmdReturn,
904  NULL);
905 
906  CmdReturn[LenCmdReturn] = '\0';
907 
908  //#define DEBUG
909  #undef DEBUG
910  #ifdef DEBUG
911  printf("ap_scan: CMD= %s, Return= %s", Cmd, CmdReturn);
912  #endif
913 
914  if(strstr(CmdReturn, "OK") != NULL)
915  {
916  Ret = OK;
917  WiFiStatus = OK;
918  }
919  else
920  {
921  WiFiStatus = FAIL;
922  }
923  }
924  return Ret;
925 }
926 
927 RESULT cWiFiSetSsid(char *Ssid)
928 {
929  RESULT Ret = FAIL;
930  char CmdReturn[10];
931  char Cmd[128];
932  int RetVal;
933  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
934 
935  WiFiStatus = BUSY;
936  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
937 
938  #undef DEBUG
939  //#define DEBUG
940  #ifdef DEBUG
941  printf("\n\rEntry of SET_SSID in c_wifi\n\r");
942  #endif
943 
944  if(ctrl_conn != NULL)
945  {
946  strcpy(Cmd, "SET_NETWORK 0 ssid \"");
947  strcat(Cmd, Ssid);
948  strcat(Cmd, "\"");
949 
950 
951  RetVal = wpa_ctrl_request( ctrl_conn,
952  Cmd,
953  strlen(Cmd),
954  CmdReturn,
955  &LenCmdReturn,
956  NULL);
957 
958  CmdReturn[LenCmdReturn] = '\0';
959 
960  //#define DEBUG
961  #undef DEBUG
962  #ifdef DEBUG
963  printf("SetSSID: CMD= %s, Return= %s", Cmd, CmdReturn);
964  #endif
965 
966  if(strstr(CmdReturn, "OK") != NULL)
967  {
968  Ret = OK;
969  WiFiStatus = OK;
970  }
971  else
972  {
973  WiFiStatus = FAIL;
974  }
975  }
976  return Ret;
977 }
978 
980 {
981  RESULT Ret = FAIL;
982  char CmdReturn[10];
983  int RetVal;
984  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
985 
986  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
987 
988  if(ctrl_conn != NULL)
989  {
990  RetVal = wpa_ctrl_request( ctrl_conn,
991  "SET_NETWORK 0 key_mgmt WPA-PSK",
992  strlen("SET_NETWORK 0 key_mgmt WPA-PSK"),
993  CmdReturn,
994  &LenCmdReturn,
995  NULL);
996 
997  CmdReturn[LenCmdReturn] = '\0';
998  if(strstr(CmdReturn, "OK") != NULL)
999  {
1000  Ret = OK;
1001  }
1002  }
1003  return Ret;
1004 }
1005 
1007 {
1008  RESULT Ret = FAIL;
1009  char CmdReturn[10];
1010  int RetVal;
1011  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1012 
1013  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1014 
1015  if(ctrl_conn != NULL)
1016  {
1017  RetVal = wpa_ctrl_request( ctrl_conn,
1018  "SET_NETWORK 0 key_mgmt NONE",
1019  strlen("SET_NETWORK 0 key_mgmt NONE"),
1020  CmdReturn,
1021  &LenCmdReturn,
1022  NULL);
1023 
1024  CmdReturn[LenCmdReturn] = '\0';
1025  if(strstr(CmdReturn, "OK") != NULL)
1026  {
1027  Ret = OK;
1028  }
1029  }
1030  return Ret;
1031 }
1032 
1033 RESULT cWiFiSetPsk(char *Psk )
1034 {
1035  RESULT Ret = FAIL;
1036  char CmdReturn[10];
1037  int RetVal;
1038  char Cmd[128];
1039  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1040 
1041  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1042 
1043  if(ctrl_conn != NULL)
1044  {
1045  strcpy(Cmd, "SET_NETWORK 0 psk ");
1046  strcat(Cmd, Psk);
1047 
1048  RetVal = wpa_ctrl_request( ctrl_conn,
1049  Cmd,
1050  strlen(Cmd),
1051  CmdReturn,
1052  &LenCmdReturn,
1053  NULL);
1054 
1055  CmdReturn[LenCmdReturn] = '\0';
1056  if(strstr(CmdReturn, "OK") != NULL)
1057  {
1058  Ret = OK;
1059  }
1060  }
1061  return Ret;
1062 }
1063 
1064 RESULT cWiFiGetApMacAddr(char* MacAddr, int Index)
1065 {
1066  // Get the MAC address of the Indexed AP
1067  RESULT Result = FAIL;
1068  if(!(Index > (ApTableSize - 1)))
1069  {
1070  strcpy(MacAddr, ApTable[Index].mac_address);
1071  Result = OK;
1072  }
1073  return Result;
1074 }
1075 
1076 void cWiFiSetEncryptToWpa2(int Index)
1077 {
1078  WiFiStatus = BUSY; // NOT async, but if...
1079 
1080  ApTable[Index].ap_flags |= WPA2;
1081 
1082  #undef DEBUG
1083  //#define DEBUG
1084  #ifdef DEBUG
1085  printf("\r\nApTable[Index = %d].security before = %s\r\n", Index, ApTable[Index].security);
1086  #endif
1087 
1088  strcpy(ApTable[Index].security, "WPA2");
1089 
1090  #undef DEBUG
1091  //#define DEBUG
1092  #ifdef DEBUG
1093  printf("\r\nApTable[Index = %d].security after = %s\r\n", Index, ApTable[Index].security);
1094  #endif
1095 
1096  WiFiStatus = OK; // Clean properety settings
1097 }
1098 
1099 void cWiFiSetEncryptToNone(int Index)
1100 {
1101  WiFiStatus = BUSY; // NOT async, but if...
1102  ApTable[Index].ap_flags &= ~WPA2;
1103  strcpy(ApTable[Index].security, "NONE");
1104  WiFiStatus = OK; // Clean properety settings
1105 }
1106 
1107 void cWiFiSetKnown(int Index)
1108 {
1109  ApTable[Index].ap_flags |= KNOWN;
1110 }
1111 
1112 void cWiFiDeleteAsKnown(int LocalIndex)
1113 {
1114  // Leave entry only remove the KNOWN flag
1115  WiFiStatus = OK;
1116  ApTable[LocalIndex].ap_flags &= ~KNOWN;
1117 }
1118 
1119 RESULT cWiFiAddHidden(char *HiddenApName, char *Security, char *PassWord)
1120 {
1121  RESULT Result = FAIL;
1122  int Index = ApTableSize;
1123 
1124  if(ApTableSize < MAX_AP_ENTRIES)
1125  {
1126  Index = ApTableSize++;
1127  if(ApTableSize >= MAX_AP_ENTRIES) // Can only be <=, but....
1128  ApTableSize = MAX_AP_ENTRIES;
1129  }
1130  else
1131  {
1132  ApTableSize = MAX_AP_ENTRIES; // To be sure..
1133  Index = MAX_AP_ENTRIES - 1;
1134  }
1135 
1136  ApTable[Index].ap_flags = 0; // Clear flags - valid values set below
1137  strcpy(ApTable[Index].friendly_name, HiddenApName);
1138 
1139  if(strstr(Security, "WPA2") != NULL)
1140  {
1141  ApTable[Index].ap_flags |= WPA2;
1142  strcpy(ApTable[Index].security, "WPA2"); // Only exact
1143  }
1144 
1145  ApTable[Index].ap_flags |= KNOWN; // It's hidden so.... we must know it
1146  // else we would not be here ;-)
1147 
1148  if(cWiFiMakePsk(HiddenApName, PassWord, Index) == OK)
1149  {
1150  Result = OK;
1151  }
1152 
1153  return Result;
1154 }
1155 
1156 RESULT cWiFiGetHiddenMacAddr(char* MacAddr, int Index)
1157 {
1158  // Get the MAC address of the Indexed "HIDDEN" AP
1159  RESULT Result = FAIL;
1160  char IwConfigCmd[32];
1161  FILE *FilePointer;
1162 
1163  if(!(Index > (ApTableSize - 1)))
1164  {
1165  // First get the "HIDDEN" MAC addr.
1166  strcpy(IwConfigCmd, "iwconfig");
1167  strcat(IwConfigCmd, " ");
1168  strcat(IwConfigCmd, LogicalIfName);
1169  FilePointer = popen(IwConfigCmd, "r");
1170  if (FilePointer)
1171  {
1172  char *OneLine = NULL;
1173  char *TempPointer;
1174  size_t NumberOfBytes;
1175 
1176  while((getline(&OneLine, &NumberOfBytes, FilePointer) > 0) && OneLine)
1177  {
1178  if((OneLine = strstr(OneLine, "Access Point:")) > 0)
1179  {
1180  OneLine += 14;
1181  TempPointer = OneLine + 17;
1182  *TempPointer = '\0';
1183  strcpy(MacAddr, OneLine);
1184  Result = OK;
1185  break;
1186  }
1187  }
1188  }
1189  pclose(FilePointer);
1190  // Store it and return it
1191  strcpy(ApTable[Index].mac_address, MacAddr);
1192  Result = OK;
1193  }
1194  return Result;
1195 }
1196 
1197 RESULT cWiFiGetIpAddr(char* IpAddress)
1198 {
1199  RESULT Result = FAIL;
1200 
1201  if(strstr(MyIp4Address, "???") == NULL)
1202  {
1203  strcpy(IpAddress, MyIp4Address);
1204  Result = OK;
1205  }
1206  return Result;
1207 }
1208 
1209 RESULT cWiFiGetMyMacAddr(char* MacAddress)
1210 {
1211  RESULT Result = FAIL;
1212 
1213  if(strstr(MyHwMacAddress, "??") == NULL)
1214  {
1215  strcpy(MacAddress, MyHwMacAddress);
1216  Result = OK;
1217  }
1218  return Result;
1219 }
1220 
1221 RESULT cWiFiFindMacAddr(void)
1222 {
1223  // Get the MAC address of the HardWare Interface using the Logical "InterFace"
1224  // e.g. wlan0 and store it in the fixed location
1225  // MyHwMacAddress
1226 
1227  RESULT Result = FAIL;
1228  char IfConfigCmd[32];
1229  strcpy(IfConfigCmd, "ifconfig");
1230  strcat(IfConfigCmd, " ");
1231  strcat(IfConfigCmd, LogicalIfName);
1232  FILE *FilePointer = popen(IfConfigCmd, "r");
1233 
1234  const char *Source;
1235  char *Destination;
1236 
1237 
1238  if (FilePointer)
1239  {
1240  char *OneLine = NULL;
1241  char *TempPointer;
1242  size_t NumberOfBytes;
1243 
1244  while((getline(&OneLine, &NumberOfBytes, FilePointer) > 0) && OneLine)
1245  {
1246  if((OneLine = strstr(OneLine, "HWaddr")) > 0)
1247  {
1248  OneLine+=7;
1249  TempPointer = OneLine + 17;
1250  *TempPointer = '\0';
1251 
1252  Source = OneLine;
1253  Destination = OneLine;
1254 
1255  do // new
1256  {
1257  while(*Source == ':')
1258  ++Source;
1259  }
1260  while((*Destination++ = *Source++) != '\0');
1261  strcpy(MyHwMacAddress, OneLine);
1262  Result = OK;
1263 
1264  #undef DEBUG
1265  //#define DEBUG
1266  #ifdef DEBUG
1267  printf("cWiFiFindMacAddr -> MAC adresse = %s\n\r", MyHwMacAddress);
1268  #endif
1269 
1270  break;
1271  }
1272  }
1273  }
1274  pclose(FilePointer);
1275  return Result;
1276 }
1277 
1279 {
1280  // Is a KNOWN Dongle attached? We uses the
1281  // "KNOWN Manufacture ID and Device ID" to
1282  // detect the HardWare Dongle. No Drivers has to
1283  // be loaded before using :-)
1284 
1285  RESULT Result = FAIL;
1286 
1287  FILE *FilePointer = NULL;
1288  char FindDongle[10];
1289  char *OneLine = NULL;
1290  size_t NumberOfBytes;
1291 
1292  strcpy(FindDongle, "lsusb");
1293 
1294  FilePointer = popen(FindDongle, "r");
1295 
1296  if (FilePointer)
1297  {
1298  while((getline(&OneLine, &NumberOfBytes, FilePointer) > 0) && OneLine)
1299  {
1300  if((OneLine = strstr(OneLine, HARDWARE_SEARCH_STRING)) > 0)
1301  {
1302  Result = OK;
1303  break;
1304  }
1305  }
1306  pclose(FilePointer);
1307  }
1308  return Result;
1309 }
1310 
1312 {
1313  FILE *pIdVendor = NULL;
1314  FILE *pIdProduct = NULL;
1315  char VendorBuffer[64];
1316  char ProductBuffer[64];
1317  //char *VendorId;
1318  //char *ProductId;
1319  RESULT Result = FAIL;
1320 
1321  pIdVendor = fopen("/sys/bus/usb/devices/1-1/idVendor", "r");
1322 
1323  pIdProduct = fopen("/sys/bus/usb/devices/1-1/idProduct", "r");
1324 
1325  if((pIdVendor != NULL) && (pIdProduct != NULL))
1326  {
1327  if(fgets(VendorBuffer, sizeof (VendorBuffer), pIdVendor) != NULL)
1328  {
1329  if(fgets(ProductBuffer, sizeof (ProductBuffer), pIdProduct) != NULL)
1330  {
1331  if((strstr(ProductBuffer, WIFI_DONGLE_ID) > 0) && (strstr(VendorBuffer, WIFI_VENDOR_ID) > 0))
1332  Result = OK;
1333  }
1334  }
1335  }
1336 
1337  if(pIdVendor != NULL)
1338  fclose(pIdVendor);
1339 
1340  if(pIdProduct != NULL)
1341  fclose(pIdProduct);
1342 
1343  if(Result != OK) // Not found - Do we have a hub?
1344  {
1345  Result = cWiFiDeepDongleSearch();
1346  }
1347  return Result;
1348 }
1349 
1350 RESULT cWiFiFindIpAddr(void)
1351 {
1352  // Get the IP addresses of the Interface named in the var "InterFaceName"
1353  // e.g. wlan0, wlan2.. and store it in the fixed locations
1354  // MyIp4Address, MyBroadCastAdr
1355 
1356  RESULT Result = FAIL;
1357  char IfConfigCmd[32];
1358  strcpy(IfConfigCmd, "ifconfig");
1359  strcat(IfConfigCmd, " ");
1360  strcat(IfConfigCmd, LogicalIfName);
1361 
1362  #undef DEBUG
1363  //#define DEBUG
1364  #ifdef DEBUG
1365  printf("\r\nIfConfigCmd = %s\r\n", IfConfigCmd);
1366  #endif
1367 
1368  FILE *FilePointer = popen(IfConfigCmd, "r");
1369 
1370  if (FilePointer)
1371  {
1372  char *OneLine = NULL;
1373  char *TempPointer;
1374  size_t NumberOfBytes;
1375  uint TempIp0;
1376  uint TempIp1;
1377  uint TempIp2;
1378  uint TempIp3;
1379 
1380  while((getline(&OneLine, &NumberOfBytes, FilePointer) > 0) && OneLine)
1381  {
1382  if((OneLine = strstr(OneLine, "inet addr:")) > 0)
1383  {
1384  OneLine+=10;
1385  if ((TempPointer = strchr(OneLine, ' ')) > 0)
1386  {
1387  *TempPointer = '\0';
1388  strcpy(MyIp4Address, OneLine);
1389 
1390  OneLine = TempPointer + 1;
1391  if((OneLine = strstr(OneLine, "Bcast:")) > 0)
1392  {
1393  OneLine+=6;
1394  if ((TempPointer = strchr(OneLine, ' ')) > 0)
1395  {
1396  *TempPointer = '\0';
1397  strcpy(MyBroadCastAdr, OneLine);
1398  if(strstr(MyBroadCastAdr, "0.0.0.0"))
1399  {
1400  sscanf(MyIp4Address, "%d.%d.%d.%d", &TempIp3, &TempIp2, &TempIp1, &TempIp0);
1401  sprintf(MyBroadCastAdr, "%d.%d.%d.%d", TempIp3, TempIp2, TempIp1, 255);
1402  }
1403 
1404  Result = OK;
1405  break;
1406  }
1407  }
1408  }
1409  }
1410  }
1411  pclose(FilePointer);
1412  }
1413  return Result;
1414 }
1415 
1416 RESULT cWiFiRequestIpAdr(char *Interface)
1417 {
1418  RESULT Ret = FAIL;
1419  char Cmd[48];
1420 
1421  #undef DEBUG
1422  //#define DEBUG
1423  #ifdef DEBUG
1424  printf("\r\nIP address = %s\r\n", MyIp4Address);
1425  #endif
1426 
1427  // strcpy(Cmd, "busybox udhcpc -t 5 -q -i");
1428  // strcpy(Cmd, "busybox udhcpc -t5 -A2 -n -i");
1429  // added quit on lease
1430  strcpy(Cmd, "udhcpc -t5 -A2 -nq -i");
1431  strcat(Cmd, Interface);
1432  strcat(Cmd, " &> /dev/null");
1433 
1434  #undef DEBUG
1435  //#define DEBUG
1436  #ifdef DEBUG
1437  printf("\r\nCmd = %s\r\n", Cmd);
1438  #endif
1439 
1440  if(system(Cmd) == 0)
1441  {
1442  #undef DEBUG
1443  //#define DEBUG
1444  #ifdef DEBUG
1445  printf("\r\nsystem(Cmd) == 0\r\n");
1446  #endif
1447 
1448  cWiFiFindIpAddr(); // Get and Store IP address
1449 
1450  #undef DEBUG
1451  //#define DEBUG
1452  #ifdef DEBUG
1453  printf("\r\nAfter cWiFiFindIpAddr()\r\n");
1454  #endif
1455 
1456  Ret = OK;
1457  }
1458  return Ret;
1459 }
1460 
1462 {
1463  RESULT Ret = FAIL;
1464  char CmdReturn[10];
1465  int RetVal;
1466  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1467 
1468  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1469 
1470  if(ctrl_conn != NULL)
1471  {
1472  RetVal = wpa_ctrl_request( ctrl_conn,
1473  "SET_NETWORK 0 pairwise CCMP TKIP",
1474  strlen("SET_NETWORK 0 pairwise CCMP TKIP"),
1475  CmdReturn,
1476  &LenCmdReturn,
1477  NULL);
1478 
1479  CmdReturn[LenCmdReturn] = '\0';
1480  if(strstr(CmdReturn, "OK") != NULL)
1481  {
1482  Ret = OK;
1483  }
1484  }
1485  return Ret;
1486 }
1487 
1488 RESULT cWiFiSetGroupCcmp(void)
1489 {
1490  RESULT Ret = FAIL;
1491  char CmdReturn[10];
1492  int RetVal;
1493  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1494 
1495  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1496 
1497  if(ctrl_conn != NULL)
1498  {
1499  RetVal = wpa_ctrl_request( ctrl_conn,
1500  "SET_NETWORK 0 group CCMP TKIP",
1501  strlen("SET_NETWORK 0 group CCMP TKIP"),
1502  CmdReturn,
1503  &LenCmdReturn,
1504  NULL);
1505  CmdReturn[LenCmdReturn] = '\0';
1506  if(strstr(CmdReturn, "OK") != NULL)
1507  {
1508  Ret = OK;
1509  }
1510  }
1511  return Ret;
1512 }
1513 
1514 
1515 
1516 RESULT cWiFiSetProtoRsn(void)
1517 {
1518  RESULT Ret = FAIL;
1519  char CmdReturn[10];
1520  int RetVal;
1521  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1522 
1523  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1524 
1525  if(ctrl_conn != NULL)
1526  {
1527  RetVal = wpa_ctrl_request( ctrl_conn,
1528  "SET_NETWORK 0 proto RSN",
1529  strlen("SET_NETWORK 0 proto RSN"),
1530  CmdReturn,
1531  &LenCmdReturn,
1532  NULL);
1533 
1534  CmdReturn[LenCmdReturn] = '\0';
1535  if(strstr(CmdReturn, "OK") != NULL)
1536  {
1537  Ret = OK;
1538  }
1539  }
1540  return Ret;
1541 }
1542 
1544 {
1545  RESULT Ret = FAIL;
1546  char CmdReturn[10];
1547  int RetVal;
1548  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1549 
1550  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1551 
1552  if(ctrl_conn != NULL)
1553  {
1554  RetVal = wpa_ctrl_request( ctrl_conn,
1555  "ENABLE_NETWORK 0",
1556  strlen("ENABLE_NETWORK 0"),
1557  CmdReturn,
1558  &LenCmdReturn,
1559  NULL);
1560 
1561  CmdReturn[LenCmdReturn] = '\0';
1562  if(strstr(CmdReturn, "OK") != NULL)
1563  {
1564  Ret = OK;
1565  }
1566  }
1567  return Ret;
1568 }
1569 
1570 RESULT cWiFiWpaStatus(void)
1571 {
1572  RESULT Ret = FAIL;
1573  char CmdReturn[256];
1574  int RetVal;
1575  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1576 
1577  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1578 
1579  do
1580  {
1581  // TODO Some escape
1582  if(ctrl_conn != NULL)
1583  {
1584  RetVal = wpa_ctrl_request( ctrl_conn,
1585  "STATUS",
1586  strlen("STATUS"),
1587  CmdReturn,
1588  &LenCmdReturn,
1589  NULL);
1590 
1591  CmdReturn[LenCmdReturn] = '\0';
1592 
1593  //#define DEBUG
1594  #undef DEBUG
1595  #ifdef DEBUG
1596  printf("cWiFiWpaStatus = %s\n\r", CmdReturn);
1597  #endif
1598  }
1599  }
1600  while(strstr(CmdReturn, "bssid") == NULL);
1601 
1602  Ret = OK;
1603  return Ret;
1604 }
1605 
1606 RESULT cWiFiDisconnect(void)
1607 {
1608  RESULT Ret = FAIL;
1609  char CmdReturn[10];
1610  int RetVal;
1611  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1612 
1613  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1614 
1615  if(ctrl_conn != NULL)
1616  {
1617  RetVal = wpa_ctrl_request( ctrl_conn,
1618  "DISCONNECT",
1619  strlen("DISCONNECT"),
1620  CmdReturn,
1621  &LenCmdReturn,
1622  NULL);
1623 
1624  sleep(2);
1625  CmdReturn[LenCmdReturn] = '\0';
1626 
1627  if(strstr(CmdReturn, "OK") != NULL)
1628  {
1629  //#define DEBUG
1630  #undef DEBUG
1631  #ifdef DEBUG
1632  printf("Disconnect OK\n\r");
1633  #endif
1634 
1635  Ret = OK;
1636  }
1637  }
1638  return Ret;
1639 }
1640 
1641 
1642 RESULT cWiFiReconnect(void)
1643 {
1644  RESULT Ret = FAIL;
1645  char CmdReturn[10];
1646  int RetVal;
1647  size_t LenCmdReturn = sizeof(CmdReturn) - 1; // We leave space for a terminating /0x00
1648 
1649  memset(CmdReturn, 0x00, LenCmdReturn); // Reset to be sure!!
1650 
1651  //#define DEBUG
1652  #undef DEBUG
1653  #ifdef DEBUG
1654  printf("Start of Reconnect...\n\r");
1655  #endif
1656 
1657  if(ctrl_conn != NULL)
1658  {
1659  RetVal = wpa_ctrl_request( ctrl_conn,
1660  "RECONNECT",
1661  strlen("RECONNECT"),
1662  CmdReturn,
1663  &LenCmdReturn,
1664  NULL);
1665  CmdReturn[LenCmdReturn] = '\0';
1666 
1667  if(strstr(CmdReturn, "OK") != NULL)
1668  {
1669  //#define DEBUG
1670  #undef DEBUG
1671  #ifdef DEBUG
1672  printf("Reconnect OK\n\r");
1673  #endif
1674 
1675  Ret = OK;
1676  }
1677  }
1678  return Ret;
1679 }
1680 
1681 RESULT cWiFiGetName(char *ApName, int Index, char Length) // Get the FriendlyName owned by ApTable[Index]
1682 {
1683  RESULT Result = FAIL;
1684  if(strlen(ApTable[Index].friendly_name) >= 1)
1685  {
1686  strncpy(ApName, ApTable[Index].friendly_name, (int)(Length - 1));
1687 
1688  #undef DEBUG
1689  //#define DEBUG
1690  #ifdef DEBUG
1691  printf("\r\ncWiFiGetName called with Index = %d => %s\r\n", Index, ApTable[Index].friendly_name);
1692  #endif
1693 
1694  Result = OK;
1695  }
1696  return Result;
1697 }
1698 
1699 RESULT cWiFiSetName(char *ApName, int Index) // Set the FriendlyName @ ApTable[Index]
1700 {
1701  RESULT Result = OK;
1702 
1703  #undef DEBUG
1704  //#define DEBUG
1705  #ifdef DEBUG
1706  printf("\r\nApTable[Index = %d].friendly_name before = %s\r\n", Index, ApTable[Index].friendly_name);
1707  printf("\r\nSetName name = %s, Index = %d\r\n", ApName, Index);
1708  if(WiFiStatus == BUSY) printf("\r\nWiFiStatus == BUSY\r\n");
1709  #endif
1710 
1711  strcpy(ApTable[Index].friendly_name, ApName);
1712  // If we changes name etc. We must have some thoughts
1713  // about an OK state of the WiFi or we want it to be
1714  WiFiStatus = OK;
1715 
1716  #undef DEBUG
1717  //#define DEBUG
1718  #ifdef DEBUG
1719  printf("\r\nApTable[Index = %d].friendly_name after = %s\r\n", Index, ApTable[Index].friendly_name);
1720  #endif
1721 
1722  return Result;
1723 }
1724 
1725 
1726 unsigned char cWiFiGetFlags(int Index) // Get Flags owned by ApTable[Index]
1727 {
1728  return ApTable[Index].ap_flags;
1729 }
1730 
1732 {
1733  int j;
1734 
1735  if(ApTableSize > 1)
1736  {
1737  for(j = 1; j < ApTableSize; j++)
1738  ApTable[j].ap_flags &= (UBYTE)(~CONNECTED);
1739  }
1740 }
1741 
1742 void cWiFiTcpShutDown(int iSocket)
1743 {
1744  UBYTE buffer[128];
1745  int res;
1746 
1747  shutdown(iSocket, 2);
1748 
1749  do
1750  {
1751  res = read(iSocket, buffer, 100);
1752  if (res < 0) break;
1753  }
1754  while(res != 0);
1755 
1756  close(iSocket);
1757 
1758 }
1759 
1761 {
1762  cWiFiTcpClose();
1763 
1765 
1766  //Disconnect
1767  cWiFiDisconnect();
1768  //Kill udhcpc
1769  cWiFiKillUdHcPc();
1770 
1771  //Remove current network
1773  sleep(3); // Give some time for cleaning before polluting again ;-)
1774 }
1775 
1776 RESULT cWiFiMakeConnectionToAp(int Index)
1777 {
1778  RESULT Result = FAIL;
1779  RESULT LocalResult = FAIL;
1780  int Tries;
1781 
1782  //#define DEBUG
1783  #undef DEBUG
1784  #ifdef DEBUG
1785  printf("cWiFiMakeConnectionToAp() with Index %d, WiFiStatus = %d\n\r", Index, WiFiStatus);
1786  #endif
1787 
1788  Tries = 0;
1789  do
1790  {
1791  if(cWiFiAddNetwork() == OK)
1792  {
1793  //#define DEBUG
1794  #undef DEBUG
1795  #ifdef DEBUG
1796  printf("Try no: %d\n\r", Tries);
1797  #endif
1798 
1799  LocalResult = OK;
1800  break;
1801  }
1802  else
1803  {
1804  //#define DEBUG
1805  #undef DEBUG
1806  #ifdef DEBUG
1807  printf("AddNetwork returns garbage\n\r");
1808  #endif
1809 
1810  sleep(3); // Force some cycles
1811 
1813  }
1814  Tries++;
1815  }
1816  while((Tries < 3) && (LocalResult == FAIL));
1817 
1818  if(LocalResult == FAIL)
1819  {
1820  //#define DEBUG
1821  #undef DEBUG
1822  #ifdef DEBUG
1823  printf("\r\nERROR_001\r\n");
1824  #endif
1825 
1826  return Result;
1827  }
1828 
1829  // Set the SSID - already known
1830  if(cWiFiSetSsid(ApTable[Index].friendly_name) != OK)
1831  {
1832  //#define DEBUG
1833  #undef DEBUG
1834  #ifdef DEBUG
1835  printf("\r\nERROR_002\r\n");
1836  #endif
1837 
1838  return Result;
1839  }
1840 
1841  if(cWiFiSetScanSsidToOne() != OK)
1842  {
1843  //#define DEBUG
1844  #undef DEBUG
1845  #ifdef DEBUG
1846  printf("\r\nERROR_cWiFiSetScanSsidToOne()\r\n");
1847  #endif
1848 
1849  return Result;
1850  }
1851 
1852  // WPA2 capability?
1853  if((ApTable[Index].ap_flags & WPA2) == WPA2)
1854  {
1855  //#define DEBUG
1856  #undef DEBUG
1857  #ifdef DEBUG
1858  printf("\r\nWPA2 flag set\r\n");
1859  #endif
1860 
1861  // Set KEY MANAGEMENT
1862  if(cWiFiSetKeyManagToWpa2() != OK)
1863  {
1864  //#define DEBUG
1865  #undef DEBUG
1866  #ifdef DEBUG
1867  printf("\r\nERROR_003\r\n");
1868  #endif
1869 
1870  return Result;
1871  }
1872 
1873  // Set PSK (pre-shared key)
1874  if(strlen(ApTable[Index].pre_shared_key) < 1)
1875  {
1876  //#define DEBUG
1877  #undef DEBUG
1878  #ifdef DEBUG
1879  printf("\r\nERROR_004 pre_shared_key = %s, Index = %d, friendly_name = %s\r\n", ApTable[Index].pre_shared_key, Index, ApTable[Index].friendly_name);
1880  #endif
1881 
1882  return Result;
1883  }
1884  else
1885  {
1886  if(cWiFiSetPsk(ApTable[Index].pre_shared_key) != OK)
1887  {
1888  //#define DEBUG
1889  #undef DEBUG
1890  #ifdef DEBUG
1891  printf("\r\nERROR_005 pre_shared_key = %s, Index = %d, friendly_name = %s\r\n", ApTable[Index].pre_shared_key, Index, ApTable[Index].friendly_name);
1892  #endif
1893 
1894  return Result;
1895  }
1896  }
1897 
1898  // Set PAIR WISE CIPHERS
1899  if(cWiFiSetPairWiseCcmp() != OK)
1900  {
1901  //#define DEBUG
1902  #undef DEBUG
1903  #ifdef DEBUG
1904  printf("\r\nERROR_006\r\n");
1905  #endif
1906 
1907  return Result;
1908  }
1909 
1910  // Set GROUP CIPHERS
1911  if(cWiFiSetGroupCcmp() != OK)
1912  {
1913  //#define DEBUG
1914  #undef DEBUG
1915  #ifdef DEBUG
1916  printf("\r\nERROR_007\r\n");
1917  #endif
1918 
1919  return Result;
1920  }
1921 
1922  // SET PROTOCOL
1923  if(cWiFiSetProtoRsn() != OK)
1924  {
1925  //#define DEBUG
1926  #undef DEBUG
1927  #ifdef DEBUG
1928  printf("\r\nERROR_008\r\n");
1929  #endif
1930 
1931  return Result;
1932  }
1933  }
1934  else
1935  {
1936  // Without WPA2!!
1937  // Set KEY MANAGEMENT to NONE
1938  if(cWiFiSetKeyManagToNone() != OK)
1939  {
1940  //#define DEBUG
1941  #undef DEBUG
1942  #ifdef DEBUG
1943  printf("\r\nERROR_009\r\n");
1944  #endif
1945 
1946  return Result;
1947  }
1948 
1949  }
1950 
1951  // ENABLE NETWORK
1952  if(cWiFiSetEnableNetwork() != OK)
1953  {
1954  //#define DEBUG
1955  #undef DEBUG
1956  #ifdef DEBUG
1957  printf("\r\nERROR_010\r\n");
1958  #endif
1959 
1960  return Result;
1961  }
1962 
1963  sleep(1); // Force some system cycles
1964 
1965  // Reconnect
1966  cWiFiReconnect();
1967 
1968  // We need to get an (new) IP-address - network could have changed as well
1969 
1970  strcpy(MyIp4Address, "???"); // Just to be sure - it's the only stuff
1971 
1972  cWiFiKillUdHcPc(); // No leftover
1973 
1974  sleep(1); // Force some system cycles
1975 
1976  cWiFiRequestIpAdr(LogicalIfName);
1977 
1978  //#define DEBUG
1979  #undef DEBUG
1980  #ifdef DEBUG
1981  printf("Here is the (new) IP address: %s\n\r", MyIp4Address);
1982  #endif
1983 
1984  if(strstr(MyIp4Address, "???") == NULL)
1985  {
1986  //#define DEBUG
1987  #undef DEBUG
1988  #ifdef DEBUG
1989  printf("MyIp4Address is OK - and NOT set to ??? => %s", MyIp4Address);
1990  #endif
1991 
1992  Result = OK; // Assigned an IP
1993  }
1994 
1995  //#define DEBUG
1996  #undef DEBUG
1997  #ifdef DEBUG
1998  printf("\r\nDid we reach the end with an OK? cWiFiMakeConnectionToAp(Index)\r\n");
1999  if(Result == OK)
2000  printf("\r\nResult == OK\r\n");
2001  else
2002  printf("\r\nResult == NOT!!!! OK\r\n");
2003  #endif
2004 
2005  return Result; // Only OK if an valid association & an IP address has been leased
2006  // Association could give OK even when not fully associated, but a valid
2007  // IP address shows (some) valid connection :-)
2008 }
2009 
2010 
2011 RESULT cWiFiConnectToAp(int Index)
2012 {
2013  RESULT Result = FAIL;
2014 
2015  // Make ApTable[Index] active - "if we're lucky" ;-)
2016 
2017  WiFiStatus = BUSY;
2018 
2019  //#define DEBUG
2020  #undef DEBUG
2021  #ifdef DEBUG
2022  printf("\r\ncWiFiConnectToAp(int Index = %d)\r\n", Index);
2023  #endif
2024 
2025  if(cWiFiMakeConnectionToAp(Index) == OK)
2026  {
2027  Result = OK;
2028 
2029  //#define DEBUG
2030  #undef DEBUG
2031  #ifdef DEBUG
2032  printf("\r\ncWiFiMakeConnectionToAp(Index = %d) == OK)\r\n", Index);
2033  #endif
2034 
2035  // Move the active to TOP
2036  // Save selected for TOP placing
2038 
2039  // Move all others down one step until index
2040  cWifiMoveAllActualDown(Index);
2041 
2042  // Restore and Save @ TOP
2044 
2045  // Delete if already in KNOWN list
2047 
2048  // Mark the ACTUAL as KNOWN (i.e. set flag)
2049  ApTable[0].ap_flags |= KNOWN;
2050 
2051  // Mark the ACTUAL as CONNECTED (i.e. set flag)
2052  ApTable[0].ap_flags |= CONNECTED;
2053 
2054  // Remove any "already connected" - i.e. remove flag(s)
2056 
2057  // This VERY Ap should now be stored as MOST WANTED @ next WiFi session
2059 
2061  WiFiStatus = OK;
2062  }
2063  else
2064  {
2066  WiFiStatus = FAIL;
2067 
2068  //#define DEBUG
2069  #undef DEBUG
2070  #ifdef DEBUG
2071  printf("\r\nConnect FAILed.. READY_FOR_AP_SEARCH again\r\n");
2072  #endif
2073  }
2074 
2075  return Result;
2076 }
2077 
2078 RESULT cWiFiMakePsk(char *ApSsid, char *PassPhrase, int Index) // Make the pre-shared key from
2079 { // Supplied SSID and PassPhrase
2080  RESULT Ret = OK; // And store it in ApTable[Index]
2081  char WpaPassPhraseCmd[64];
2082 
2083  WiFiStatus = BUSY; // NOT async, but if...
2084 
2085  //#define DEBUG
2086  #undef DEBUG
2087  #ifdef DEBUG
2088  printf("\r\ncWiFiMakePsk\r\n");
2089  printf("\n\rSSID = %s, PassPhrase = %s, Index = %d\r\n", ApSsid, PassPhrase, Index);
2090  #endif
2091 
2092  // Construct CommandLine
2093  strcpy(WpaPassPhraseCmd, WPA_APPLIKATION_PATH);
2094  strcat(WpaPassPhraseCmd, "./wpa_passphrase \"");
2095  strcat(WpaPassPhraseCmd, ApSsid);
2096  strcat(WpaPassPhraseCmd, "\" ");
2097  strcat(WpaPassPhraseCmd, PassPhrase);
2098 
2099  //#define DEBUG
2100  #undef DEBUG
2101  #ifdef DEBUG
2102  printf("WpaPassPhraseCmd = ->%s<-\r\n", WpaPassPhraseCmd);
2103  #endif
2104 
2105  FILE *FilePointer = popen(WpaPassPhraseCmd, "r");
2106  int i;
2107 
2108  if (FilePointer)
2109  {
2110  char *PskLine = NULL;
2111  size_t NumberOfBytes;
2112 
2113  for (i = 0; i < 4; i++)
2114  {
2115  if(!((getline(&PskLine, &NumberOfBytes, FilePointer) > 0) && PskLine))
2116  {
2117  //Ret = FAIL;
2118  //WiFiStatus = FAIL;
2119  }
2120  else
2121  {
2122  Ret = OK;
2123  //WiFiStatus = OK;
2124  }
2125  }
2126 
2127  if(Ret == OK)
2128  {
2129  //#define DEBUG
2130  #undef DEBUG
2131  #ifdef DEBUG
2132  printf("\r\nPre_Shared_Key Made = %s, %d\r\n", PskLine, NumberOfBytes);
2133  printf("\r\nApTable[Index = %d].pre_shared_key = %s before\r\n", Index, ApTable[Index].pre_shared_key);
2134  #endif
2135 
2136  strcpy(ApTable[Index].pre_shared_key, PskLine + 5);
2137  ApTable[Index].pre_shared_key[strlen(ApTable[Index].pre_shared_key)- 1] = '\x0'; // remove linefeed
2138 
2139  //#define DEBUG
2140  #undef DEBUG
2141  #ifdef DEBUG
2142  printf("\r\nApTable[Index = %d].pre_shared_key = %s after store etc.\r\n", Index, ApTable[Index].pre_shared_key);
2143  printf("\r\nApTable[Index = %d].ap_flags = %d after store etc.\r\n", Index, ApTable[Index].ap_flags);
2144  #endif
2145  WiFiStatus = OK;
2146  }
2147  pclose(FilePointer);
2148  }
2149  return Ret;
2150 }
2151 
2152 RESULT cWiFiGetIndexFromName(char *Name, UBYTE *Index)
2153 {
2154  int i = 0;
2155  int ListEnd = 0;
2156  RESULT Result = FAIL;
2157 
2158  ListEnd = cWiFiGetApListSize() + 1;
2159 
2160  //#define DEBUG
2161  #undef DEBUG
2162  #ifdef DEBUG
2163  if(WiFiStatus == BUSY)
2164  printf("\r\nWiFiStatus == BUSY\r\n");
2165  else
2166  printf("\r\nWiFiStatus == NOT!!!! BUSY\r\n");
2167 
2168  printf("\r\nName2Index = %s\r\n", Name);
2169  #endif
2170 
2171  while(i < ListEnd)
2172  {
2173  //#define DEBUG
2174  #undef DEBUG
2175  #ifdef DEBUG
2176  printf("\r\nApTable[%d].friendly_name = %s\r\n", i, ApTable[i].friendly_name);
2177  #endif
2178 
2179  if(strcmp(ApTable[i].friendly_name, Name) == 0) // Found
2180  {
2181  *Index = (UBYTE)i; // Index return
2182  Result = OK; // Found OK
2183 
2184  //#define DEBUG
2185  #undef DEBUG
2186  #ifdef DEBUG
2187  printf("\r\nOK Index = %d\r\n", i);
2188  #endif
2189 
2190  break;
2191  }
2192  else
2193  i++;
2194  }
2195 
2196  //#define DEBUG
2197  #undef DEBUG
2198  #ifdef DEBUG
2199  printf("\r\nName2Index Index = %d\r\n", i);
2200  #endif
2201 
2202  return Result; // OK if FOUND else FAIL
2203 }
2204 
2205 int cWiFiIsItAFriend(char *mac_address) // Is the actual AP known?
2206 {
2207  int j;
2208  int RetVal = UNKNOWN;
2209 
2210  for (j = 0; j < ApStoreTableSize; j++)
2211  {
2212  if (strcmp(mac_address,ApStoreTable[j].mac_address) == 0)
2213  {
2214  RetVal = KNOWN;
2215  break;
2216  }
2217  }
2218  return RetVal;
2219 }
2220 
2222 {
2223  int HighestPower = -120;
2224  int SrcIndex = 0;
2225  int i;
2226  int StartIndex = 0;
2227  aps TempAp;
2228 
2229  while(StartIndex < ApTableSize)
2230  {
2231  for(i = StartIndex; i < ApTableSize; i++)
2232  {
2233  if(atoi(ApTable[i].signal_level) > HighestPower)
2234  {
2235  SrcIndex = i;
2236  HighestPower = atoi(ApTable[i].signal_level);
2237  }
2238  }
2239 
2240  // exchange highest found with were are we
2241  memcpy(&TempAp, &(ApTable[StartIndex].mac_address), sizeof(aps));
2242  memcpy(&(ApTable[StartIndex].mac_address), &(ApTable[SrcIndex].mac_address), sizeof(aps));
2243  memcpy(&(ApTable[SrcIndex].mac_address), &TempAp, sizeof(aps));
2244 
2245  StartIndex++;
2246  HighestPower = -120; // reset
2247  }
2248 }
2249 
2250 RESULT cWiFiStoreActualApList() // Store the latest SCAN result(s)
2251 { // If SSID not broadcasted, the FreindlyName is set to "???"
2252  // WPA status flag and Known flag also set if...
2253  int Status = -1; // Not responding
2254  char* StrElement;
2255  int EndOfTable = 0;
2256  int TableEntryPointer = 0;
2257  RESULT Result = FAIL;
2258 
2259  char SubElement[129]; // Safety for networks with a lot of security strings
2260  char ScanResponse[2048];
2261  size_t LenScanResponse = sizeof(ScanResponse) - 1; // Space for a trailing /0x10
2262 
2263  WiFiStatus = BUSY; // Should already be BUSY
2264 
2265  if (ctrl_conn != NULL)
2266  {
2267  Status = wpa_ctrl_request( ctrl_conn,
2268  "SCAN_RESULTS",
2269  strlen("SCAN_RESULTS"),
2270  ScanResponse,
2271  &LenScanResponse,
2272  NULL);
2273 
2274  //#define DEBUG
2275  #undef DEBUG
2276  #ifdef DEBUG
2277  printf("\r\nScanresult:\r\n %s\r\n", ScanResponse);
2278  #endif
2279 
2280  if((LenScanResponse > 32) && (Status == 0)) // At least some of the header on return
2281  { // and OK Status return
2282  ScanResponse[LenScanResponse] = '\x0a'; // Last line END
2283 
2284  StrElement = strtok(ScanResponse, "\x0a"); // Dummy read to remove HEADER
2285 
2286  do
2287  {
2288  StrElement = strtok (NULL, "\x09");
2289  if((StrElement != NULL) && (StrElement[2] == ':'))
2290  {
2291  strcpy(ApTable[TableEntryPointer].mac_address, StrElement);
2292 
2293  StrElement = strtok (NULL, "\x09");
2294  strcpy(ApTable[TableEntryPointer].frequency, StrElement);
2295 
2296  StrElement = strtok (NULL, "\x09");
2297  strcpy(ApTable[TableEntryPointer].signal_level, StrElement);
2298 
2299  // Read Security and SSID if it is shown
2300  StrElement = strtok (NULL, "\x0a");
2301 
2302  if(StrElement[strlen(StrElement) - 1] == '\x09')
2303  {
2304  // NO SSID SHOWN
2305  StrElement[strlen(StrElement) - 1] = '\x00';
2306  strcpy(ApTable[TableEntryPointer].security, StrElement);
2307  strcpy(ApTable[TableEntryPointer].friendly_name, "??????");
2308  }
2309  else
2310  {
2311  // We have to separate SECURITY AND SSID
2312  sscanf( StrElement, "%s", ApTable[TableEntryPointer].security);
2313 
2314  // Get rest, which contains the SSID with spaces include - if any :-)
2315  strncpy(SubElement, StrElement + strlen(ApTable[TableEntryPointer].security) + 1, (strlen(StrElement) - strlen(ApTable[TableEntryPointer].security)));
2316 
2317  SubElement[strlen(StrElement) + 1] = 0x00; // END
2318 
2319  strcpy(ApTable[TableEntryPointer].friendly_name, SubElement);
2320  }
2321 
2322  // Reset AP_FLAGS
2323  ApTable[TableEntryPointer].ap_flags = 0x00;
2324 
2325  // Add Visible to Flags (Would not be here, if NOT) but set for the UI to use
2326  ApTable[TableEntryPointer].ap_flags |= VISIBLE;
2327 
2328  // Check for security
2329  if(strstr(ApTable[TableEntryPointer].security, "WPA2") != NULL)
2330  {
2331  ApTable[TableEntryPointer].ap_flags |= WPA2;
2332  }
2333 
2334  // Is it a known "fellow"?
2335  if(cWiFiIsItAFriend(&(ApTable[TableEntryPointer].mac_address[0]))== KNOWN)
2336  {
2337  ApTable[TableEntryPointer].ap_flags |= KNOWN;
2338  }
2339 
2340  TableEntryPointer++;
2341  if(TableEntryPointer > MAX_AP_ENTRIES)
2342  {
2343  EndOfTable = 1;
2344  TableEntryPointer = MAX_AP_ENTRIES;
2345  }
2346  }
2347  else EndOfTable = 1;
2348  }
2349  while(EndOfTable < 1);
2350 
2351  ApTableSize = TableEntryPointer;
2352 
2353  if(TableEntryPointer > 0)
2354  {
2355  cWiFiSortBySignalLevel(); // If many equal named APs the very strongest should be at top
2356  }
2357 
2358  if(TableEntryPointer > 0)
2359  {
2360  // Merge ACTUAL- and KNOWN LIST into a prioritized list
2362  cWiFiClearExceptZero(); // Reset previous CONNECT-flags except current connected
2363  }
2364  }
2365  }
2366 
2367  // ******************************** TEST STUFF *******************************************************
2368  //
2369  // Used as a stable and known "environment" - i.e. the AP's are stable and always there :-)
2370  //
2371  //#define TEST
2372  #undef TEST
2373  #ifdef TEST
2374  printf("\r\nSimulated Scanned List:\r\n");
2375 
2376  ApTableSize = 6;
2377 
2378  /* strcpy(ApTable[0].mac_address, "00:24:01:43:69:10");
2379  strcpy(ApTable[0].security, "[WPA2-PSK-CCMP][WPS][ESS]");
2380  strcpy(ApTable[0].friendly_name, "New10");
2381  strcpy(ApTable[0].pre_shared_key, "dbc9c6cf6d4d8e9405842d2e4ab726f33f10de62c30254ff19512af0f");
2382  ApTable[0].ap_flags = 0x05;
2383 
2384  strcpy(ApTable[1].mac_address, "00:24:01:43:69:11");
2385  strcpy(ApTable[1].security, "[WPA2]");
2386  strcpy(ApTable[1].friendly_name, "New11");
2387  strcpy(ApTable[1].pre_shared_key, "");
2388  ApTable[1].ap_flags = 0x05;
2389 
2390  strcpy(ApTable[2].mac_address, "00:24:01:43:69:12");
2391  strcpy(ApTable[2].security, "[WPA2-PSK-CCMP]");
2392  strcpy(ApTable[2].friendly_name, "New12");
2393  strcpy(ApTable[2].pre_shared_key, "");
2394  ApTable[2].ap_flags = 0x05;
2395 
2396  strcpy(ApTable[3].mac_address, "00:24:01:43:69:13");
2397  strcpy(ApTable[3].security, "[WPA2-PSK-CCMP][ESS]");
2398  strcpy(ApTable[3].friendly_name, "New13");
2399  strcpy(ApTable[3].pre_shared_key, "");
2400  ApTable[3].ap_flags = 0x05;*/
2401 
2402  strcpy(ApTable[0].mac_address, "00:24:01:43:69:14");
2403  strcpy(ApTable[0].security, "[WPA2-PSK][ESS]");
2404  strcpy(ApTable[0].friendly_name, "XXX");
2405  strcpy(ApTable[0].pre_shared_key, "");
2406  ApTable[0].ap_flags = 0x05;
2407 
2408  strcpy(ApTable[1].mac_address, "00:24:01:43:69:02");
2409  strcpy(ApTable[1].security, "[WPA2-PSK-CCMP]");
2410  strcpy(ApTable[1].friendly_name, "AAA");
2411  strcpy(ApTable[1].pre_shared_key, "");
2412  ApTable[1].ap_flags = 0x0C;
2413 
2414  strcpy(ApTable[2].mac_address, "00:24:01:43:69:6e");
2415  strcpy(ApTable[2].security, "[WPA2-PSK-CCMP][WPS][ESS]");
2416  strcpy(ApTable[2].friendly_name, "FFF");
2417  strcpy(ApTable[2].pre_shared_key, "dbc9c6cf6d4d8e9405842d2e4ab726f33f10de62c30254ff19512af0f");
2418  ApStoreTable[2].ap_flags = 0x0C;
2419 
2420  strcpy(ApTable[3].mac_address, "00:24:01:43:69:01");
2421  strcpy(ApTable[3].security, "[WPA2]");
2422  strcpy(ApTable[3].friendly_name, "OOO");
2423  strcpy(ApTable[3].pre_shared_key, "");
2424  ApTable[3].ap_flags = 0x0C;
2425 
2426  strcpy(ApTable[4].mac_address, "00:24:01:43:69:98");
2427  strcpy(ApTable[4].security, "");
2428  strcpy(ApTable[4].friendly_name, "HHH");
2429  strcpy(ApTable[4].pre_shared_key, "");
2430  ApTable[4].ap_flags = 0x01;
2431 
2432  strcpy(ApTable[5].mac_address, "00:24:01:43:69:99");
2433  strcpy(ApTable[5].security, "");
2434  strcpy(ApTable[5].friendly_name, "WWW");
2435  strcpy(ApTable[5].pre_shared_key, "");
2436  ApTable[5].ap_flags = 0x01;
2437 
2438  printf("\rApTable before MERGE:\r\n");
2439  int k;
2440  for(k = 0; k < ApTableSize; k++)
2441  {
2442  printf("\rApTable[%d].mac_address = %s\r\n", k, ApTable[k].mac_address);
2443  printf("\rApTable[%d].security = %s\r\n", k, ApTable[k].security);
2444  printf("\rApTable[%d].friendly_name = %s\r\n", k, ApTable[k].friendly_name);
2445  printf("\rApTable[%d].pre_shared_key = %s\r\n", k, ApTable[k].pre_shared_key);
2446  printf("\rApTable[%d].ap_flags = %d\r\n", k, ApTable[k].ap_flags);
2447  }
2448 
2450 
2451  printf("\r\nApTable after MERGE:\r\n");
2452  for(k = 0; k < ApTableSize; k++)
2453  {
2454  printf("\rApTable[%d].mac_address = %s\r\n", k, ApTable[k].mac_address);
2455  printf("\rApTable[%d].security = %s\r\n", k, ApTable[k].security);
2456  printf("\rApTable[%d].friendly_name = %s\r\n", k, ApTable[k].friendly_name);
2457  printf("\rApTable[%d].pre_shared_key = %s\r\n", k, ApTable[k].pre_shared_key);
2458  printf("\rApTable[%d].ap_flags = %d\r\n", k, ApTable[k].ap_flags);
2459  }
2460  #endif
2461 
2462 //######################################### END OF TEST STUFF ##################################################
2463 
2464  Result = OK;
2465  WiFiStatus = OK;
2467 
2468  //#define DEBUG
2469  #undef DEBUG
2470  #ifdef DEBUG
2471  printf("\r\nStored list OK - STATUS = OK\r\n");
2472  #endif
2473 
2474  return Result;
2475 }
2476 
2478 {
2479  RESULT Result = FAIL; // HARD ERROR
2480  int Status = 0;
2481  char ScanResponse[10];
2482  size_t LenScanResponse = sizeof(ScanResponse) - 1; // Space for a trailing /0
2483 
2485 
2486  WiFiStatus = FAIL; // If an OK search is started, we get BUSY otherwise we always get
2487  // a SOFT ERROR (FAIL)
2488  BeaconTx = NO_TX;
2489  cWiFiStartTimer(); // Start the Timer
2490 
2491  cWiFiCleanUpOldStuff(); // Kill OLD portion in the list etc.
2492 
2493  if(cWiFiGetOnStatus() == OK)
2494  {
2495  if (ctrl_conn != NULL)
2496  {
2497  Status = wpa_ctrl_request( ctrl_conn,
2498  "SCAN",
2499  strlen("SCAN"),
2500 
2501  ScanResponse,
2502  &LenScanResponse,
2503  NULL);
2504 
2505  ScanResponse[LenScanResponse] = '\0';
2506 
2507  if(strstr(ScanResponse, "OK") != NULL)
2508  {
2510 
2511  //#define DEBUG
2512  #undef DEBUG
2513  #ifdef DEBUG
2514  printf("\r\nSCAN list OK - STATUS = BUSY\r\n");
2515  #endif
2516  WiFiStatus = BUSY;
2517  Result = OK;
2518  }
2519  else
2520  {
2521  WiFiConnectionState = WIFI_INITIATED; // OK init, but NO SEARCH
2522  // The FAIL return is already set above!
2523 
2524  //#define DEBUG
2525  #undef DEBUG
2526  #ifdef DEBUG
2527  printf("\r\nSCAN list FAILED - STATUS = FAIL\r\n");
2528  #endif
2529  }
2530  }
2531  } // End if(cWiFiGetOnStatus == OK)
2532 
2533  return Result;
2534 }
2535 
2537 {
2538  if(ApTableSize < MAX_AP_ENTRIES)
2539  {
2540  ApTableSize++;
2541  }
2542 }
2543 
2545 {
2546  return ApTableSize;
2547 }
2548 
2549 int cWiFiWpaPing(void) // "PING" the WpaControl InterFace
2550 {
2551  int RetVal = -1; // Not responding
2552  int Status = 0;
2553  char PingResponse[32];
2554  size_t Len_PingResponse = sizeof(PingResponse) - 1; // Space for a trailing /0
2555 
2556  if (ctrl_conn != NULL)
2557  {
2558  Status = wpa_ctrl_request( ctrl_conn,
2559  "PING",
2560  strlen("PING"),
2561  PingResponse,
2562  &Len_PingResponse,
2563  NULL);
2564 
2565  PingResponse[Len_PingResponse] = '\0';
2566  if(strcmp(PingResponse, "PONG") == 0)
2567  RetVal = 0;
2568  else
2569  RetVal = Status;
2570  }
2571  return RetVal;
2572 }
2573 
2574 RESULT cWiFiGetLogicalName(void) // Get the Logical Name of the Interface
2575 { // E.g. wlan2
2576 
2577  RESULT Result = FAIL;
2578  char IwConfigCmd[32];
2579  strcpy(IwConfigCmd, "./iwconfig");
2580  FILE *FilePointer = popen(IwConfigCmd, "r");
2581 
2582  if (FilePointer)
2583  {
2584  char *OneLine = NULL;
2585  size_t NumberOfBytes;
2586  while((getline(&OneLine, &NumberOfBytes, FilePointer) > 0) && OneLine)
2587  {
2588  if(strstr(OneLine, "IEEE 802"))
2589  {
2590  OneLine[5] = 0x00;
2591  strcpy(LogicalIfName, OneLine);
2592  Result = OK;
2593  break;
2594  }
2595  }
2596  pclose(FilePointer);
2597  }
2598  return Result;
2599 }
2600 
2601 RESULT cWiFiGetStatus(void)
2602 {
2603  //#define DEBUG
2604  #undef DEBUG
2605  #ifdef DEBUG
2606  printf("WiFiStatus => GetResult = %d\n\r", WiFiStatus);
2607  #endif
2608  return WiFiStatus;
2609 }
2610 
2611 RESULT cWiFiTxingBeacons(void) // Are we active in tx beacons??
2612 {
2613  RESULT Result = FAIL;
2614  if(BeaconTx == TX_BEACON)
2615  Result = OK;
2616  return Result;
2617 }
2618 
2619 RESULT cWiFiTcpConnected(void) // TCP connection established?
2620 { // Can we communicate...
2621  RESULT Result = FAIL;
2622  if(TcpState == TCP_UP)
2623  Result = OK;
2624  return Result;
2625 }
2626 
2628 {
2629  RESULT Result = OK;
2630  int Temp, SetOn;
2631 
2632  WiFiStatus = FAIL;
2633  /* Create a listening socket IPv4, TCP and only single protocol */
2634 
2635  //#define DEBUG
2636  #undef DEBUG
2637  #ifdef DEBUG
2638  printf("Start of cWiFiInitTcpServer()...TCPListenServer = %d \n\r", TCPListenServer);
2639  #endif
2640 
2641  if ( TCPListenServer == 0) // close(TCPListenServer);
2642  {
2643  //#define DEBUG
2644  #undef DEBUG
2645  #ifdef DEBUG
2646  printf("TCPListenServer == 0 in cWiFiInitTcpServer()...\n\r");
2647  #endif
2648 
2649  if ( (TCPListenServer = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
2650  {
2651  //#define DEBUG
2652  #undef DEBUG
2653  #ifdef DEBUG
2654  printf("\r\nError creating listening socket in cWiFiInitTcpServer()...\r\n");
2655  #endif
2656 
2657  return Result; // Bail out with a SOFT error in WiFiStatus
2658  }
2659 
2660  /* Reset the socket address structure *
2661  * and fill in the relevant data members */
2662 
2663  memset(&servaddr, 0, sizeof(servaddr));
2664  servaddr.sin_family = AF_INET; // IPv4
2665  servaddr.sin_addr.s_addr = htonl(INADDR_ANY); // Any address
2666  servaddr.sin_port = htons(TCP_PORT); // The TCP port no. E.g. 5555
2667 
2668  Temp = fcntl(TCPListenServer, F_GETFL, 0);
2669 
2670  fcntl(TCPListenServer, F_SETFL, Temp | O_NONBLOCK); // Make the socket NON_BLOCKING
2671 
2672  /* Allow reuse of the socket ADDRESS *
2673  * and PORT. Client disconnecting/reconnecting */
2674 
2675  SetOn = 1; // Set On to ON ;-)
2676  Temp = setsockopt( TCPListenServer, SOL_SOCKET, SO_REUSEADDR, &SetOn, sizeof(SetOn));
2677 
2678  /* Bind the socket address to the *
2679  * listening socket, and call listen() */
2680 
2681  if ( bind(TCPListenServer, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
2682  {
2683  //#define DEBUG
2684  #undef DEBUG
2685  #ifdef DEBUG
2686  printf("\r\nError calling bind()\r\n");
2687  printf("errno = %d\n\r", errno);
2688  #endif
2689 
2690  return Result; // Bail out with a SOFT error in WiFiStatus
2691  }
2692  } // End of the "DO A SINGLE TIME"
2693 
2694  if ( listen(TCPListenServer, 1) < 0 )
2695  {
2696  //#define DEBUG
2697  #undef DEBUG
2698  #ifdef DEBUG
2699  printf("\r\nError calling listen()\r\n");
2700  #endif
2701 
2702  return Result; // Bail out with a SOFT error in WiFiStatus
2703  }
2704 
2705  // Else show debug text below.....
2706  //#define DEBUG
2707  #undef DEBUG
2708  #ifdef DEBUG
2709  printf("\r\nWAITING for a CLIENT.......\r\n");
2710  #endif
2711 
2712  WiFiStatus = OK;
2713  return OK; // Create socket, bind and listen succeeded
2714  // Or reuse
2715 }
2716 
2718 {
2719  uint size = sizeof(servaddr);
2720  RESULT Result = BUSY;
2721 
2722  WiFiStatus = OK; // We're waiting in a "positive" way ;-)
2723 
2724  usleep(1000); // Just to be sure some cycles ... :-)
2725 
2726  if((TcpConnectionSocket = accept(TCPListenServer, (struct sockaddr *)&servaddr, &size) ) < 0)
2727  {
2728  #undef DEBUG
2729  //#define DEBUG
2730  #ifdef DEBUG
2731  printf("\r\nError calling accept() - returns: %d\r", TcpConnectionSocket);
2732  #endif
2733  }
2734  else
2735  {
2736  Result = OK;
2737  WiFiStatus = OK;
2739 
2740  #undef DEBUG
2741  //#define DEBUG
2742  #ifdef DEBUG
2743  //printf("\r\nConnected.... :-) %s : %d\r\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
2744  printf("\r\nConnected.... :-) %s : %d\r\n", inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
2745  #endif
2746  }
2747 
2748  #undef DEBUG
2749  //#define DEBUG
2750  #ifdef DEBUG
2751  printf("\r\nNow we're waiting for input TCP ...\r\n");
2752  #endif
2753 
2754  return Result;
2755 }
2756 
2758 {
2759  uint DataWritten = 0; // Nothing written (BUSY)
2760  struct timeval WriteTimeVal; // Always called from COM, so FAIL == also 0
2761  fd_set WriteFdSet;
2762 
2763  if(Length > 0)
2764  {
2765  //#define DEBUG
2766  #undef DEBUG
2767  #ifdef DEBUG
2768  printf("\r\ncWiFiWriteTcp Length: %d\r\n", Length);
2769  // Code below used for "hunting" packets of correct length
2770  // but with length bytes set to "0000" and payload all zeroed
2771 
2772  int i;
2773  if((Buffer[0] == 0) && (Buffer[1] == 0))
2774  {
2775  printf("\r\ncERROR in first 2 entries - WiFiWriteTcp Length: %d\r\n", Length);
2776  for (i = 0; i < Length; i++)
2777  {
2778  printf("\r\nFAIL!!! Buffer[%d] = 0x%x\r\n", i, Buffer[i]);
2779  }
2780  }
2781  #endif
2782 
2784  {
2785  WriteTimeVal.tv_sec = 0; // NON blocking test use
2786  WriteTimeVal.tv_usec = 0; // I.e. NO timeout
2787  FD_ZERO(&WriteFdSet);
2788  FD_SET(TcpConnectionSocket, &WriteFdSet);
2789 
2790  select(TcpConnectionSocket + 1, NULL, &WriteFdSet, NULL, &WriteTimeVal);
2791  if (FD_ISSET(TcpConnectionSocket, &WriteFdSet))
2792  {
2793  // We can Write
2794  DataWritten = write(TcpConnectionSocket, Buffer, Length);
2795 
2796  #undef DEBUG
2797  //#define DEBUG
2798  #ifdef DEBUG
2799  if (DataWritten != Length)
2800  printf("\r\nDataWritten = %d, Length = %d\r\n", DataWritten, Length);
2801  #endif
2802 
2803  // DataWritten = Data sent, zero = socket busy or -1 = FAIL
2804  }
2805  }
2806  }
2807  return DataWritten;
2808 }
2809 
2810 RESULT cWiFiResetTcp(void)
2811 {
2812  RESULT Result = FAIL;
2813 
2814  //#define DEBUG
2815  #undef DEBUG
2816  #ifdef DEBUG
2817  printf("\r\nRESET - client disconnected!\r\n");
2818  #endif
2819 
2820  Result = cWiFiTcpClose();
2821  return Result;
2822 }
2823 
2825 {
2826  int DataRead = 0; // Nothing read also sent if NOT initiated
2827  // COM always polls!!
2828  struct timeval ReadTimeVal;
2829  fd_set ReadFdSet;
2830 
2832  {
2833  ReadTimeVal.tv_sec = 0; // NON blocking test use
2834  ReadTimeVal.tv_usec = 0; // I.e. NO timeout
2835  FD_ZERO(&ReadFdSet);
2836  FD_SET(TcpConnectionSocket, &ReadFdSet);
2837 
2838  select(TcpConnectionSocket + 1, &ReadFdSet, NULL, NULL, &ReadTimeVal);
2839  if (FD_ISSET(TcpConnectionSocket, &ReadFdSet))
2840  {
2841  #undef DEBUG
2842  //#define DEBUG
2843  #ifdef DEBUG
2844  printf("\r\nTcpReadState = %d\r\n", TcpReadState);
2845  #endif
2846 
2847  switch(TcpReadState)
2848  {
2849  case TCP_IDLE: // Do Nothing
2850  break;
2851 
2852  case TCP_WAIT_ON_START:
2853  //#define DEBUG
2854  #undef DEBUG
2855  #ifdef DEBUG
2856  printf("TCP_WAIT_ON_START:\r\n");
2857  #endif
2858 
2859  DataRead = read(TcpConnectionSocket, Buffer, 100); // Fixed TEXT
2860 
2861  //#define DEBUG
2862  #undef DEBUG
2863  #ifdef DEBUG
2864  printf("\r\nDataRead = %d, Buffer = \r\n", DataRead);
2865  int ii;
2866  if(DataRead > 0)
2867  {
2868  for (ii = 0; ii < DataRead; ii++)
2869  {
2870  printf("0x%x, ", Buffer[ii]);
2871  }
2872  }
2873  else
2874  printf("DataRead shows FAIL: %d", DataRead);
2875  printf("\n\r");
2876  #endif
2877 
2878  if(DataRead == 0) // We've a disconnect
2879  {
2880  cWiFiResetTcp();
2881  break;
2882  }
2883 
2884  if(strstr((char*)Buffer, "ET /target?sn=") > 0)
2885  {
2886  //#define DEBUG
2887  #undef DEBUG
2888  #ifdef DEBUG
2889  printf("\r\nTCP_WAIT_ON_START and ET /target?sn= found :-) DataRead = %d, Length = %d, Buffer = %s\r\n", DataRead, Length, Buffer);
2890  #endif
2891 
2892  // A match found => UNLOCK
2893  // Say OK back
2894  cWiFiWriteTcp((UBYTE*)"Accept:EV340\r\n\r\n", 16);
2896  }
2897 
2898  DataRead = 0; // No COM-module activity yet
2899  break;
2900 
2901  case TCP_WAIT_ON_LENGTH: // We can should read the length of the message
2902  // The packets can be split from the client
2903  // I.e. Length bytes (2) can be send as a subset
2904  // the Sequence can also arrive as a single pair of bytes
2905  // and the finally the payload will be received
2906 
2907  TcpReadBufPointer = 0; // Begin on new buffer :-)
2908 
2909  //#define DEBUG
2910  #undef DEBUG
2911  #ifdef DEBUG
2912  printf("TCP_WAIT_ON_LENGTH:\r\n");
2913  #endif
2914 
2915  DataRead = read(TcpConnectionSocket, Buffer, 2);
2916  if(DataRead == 0) // We've a disconnect
2917  {
2918  cWiFiResetTcp();
2919  break;
2920  }
2921 
2922  TcpRestLen = (UWORD)(Buffer[0] + Buffer[1] * 256);
2923  TcpTotalLength = (UWORD)(TcpRestLen + 2);
2924  if(TcpTotalLength > Length)
2926  else
2928 
2929  TcpReadBufPointer += DataRead; // Position in ReadBuffer adjust
2930  DataRead = 0; // Signal NO data yet
2931 
2932  //#define DEBUG
2933  #undef DEBUG
2934  #ifdef DEBUG
2935  printf("\n\r*************** NEW TX *************\r\n");
2936  printf("TCP_WAIT_ON_LENGTH TcpRestLen = %d, Length = %d\r\n", TcpRestLen, Length);
2937  #endif
2938 
2939  break;
2940 
2942  //#define DEBUG
2943  #undef DEBUG
2944  #ifdef DEBUG
2945  printf("TCP_WAIT_ON_ONLY_CHUNK: BufferStart = %d\r\n", TcpReadBufPointer);
2946  #endif
2947 
2948  DataRead = read(TcpConnectionSocket, &(Buffer[TcpReadBufPointer]), TcpRestLen);
2949 
2950  //#define DEBUG
2951  #undef DEBUG
2952  #ifdef DEBUG
2953  printf("DataRead = %d\r\n",DataRead);
2954  printf("BufferPointer = %p\r\n", &(Buffer[TcpReadBufPointer]));
2955  #endif
2956 
2957  if(DataRead == 0) // We've a disconnect
2958  {
2959  cWiFiResetTcp();
2960  break;
2961  }
2962 
2963  TcpReadBufPointer += DataRead;
2964 
2965  if(TcpRestLen == DataRead)
2966  {
2967  DataRead = TcpTotalLength; // Total count read
2969  }
2970  else
2971  {
2972  TcpRestLen -= DataRead; // Still some bytes in this only chunk
2973  DataRead = 0; // No COMM job yet
2974  }
2975 
2976  //#define DEBUG
2977  #undef DEBUG
2978  #ifdef DEBUG
2979  int i;
2980 
2981  for(i = 0; i < TcpTotalLength; i++)
2982  {
2983  printf("ReadBuffer[%d] = 0x%x\r\n", i, Buffer[i]);
2984  }
2985  #endif
2986 
2987  //#define DEBUG
2988  #undef DEBUG
2989  #ifdef DEBUG
2990  printf("TcpRestLen = %d, DataRead incl. 2 = %d, Length = %d\r\n", TcpRestLen, DataRead, Length);
2991  #endif
2992 
2993  break;
2994 
2996  //#define DEBUG
2997  #undef DEBUG
2998  #ifdef DEBUG
2999  printf("TCP_WAIT_ON_FIRST_CHUNK:\r\n");
3000  #endif
3001 
3002  DataRead = read(TcpConnectionSocket, &(Buffer[TcpReadBufPointer]), (Length - 2));
3003  if(DataRead == 0) // We've a disconnect
3004  {
3005  cWiFiResetTcp();
3006  break;
3007  }
3008 
3009  //#define DEBUG
3010  #undef DEBUG
3011  #ifdef DEBUG
3012  printf("DataRead = %d\r\n", DataRead);
3013  #endif
3014 
3015  TcpRestLen -= DataRead;
3017  DataRead += 2;
3018 
3019  //#define DEBUG
3020  #undef DEBUG
3021  #ifdef DEBUG
3022  printf("\r\nTCP_WAIT_ON_FIRST_CHUNK TcpRestLen = %d, DataRead incl. 2 = %d, Length = %d\r\n", TcpRestLen, DataRead, Length);
3023  #endif
3024 
3025  break;
3026 
3027 
3029  //#define DEBUG
3030  #undef DEBUG
3031  #ifdef DEBUG
3032  printf("TCP_WAIT_COLLECT_BYTES:\r\n");
3033  #endif
3034 
3035  TcpReadBufPointer = 0;
3036  if(TcpRestLen < Length)
3037  DataRead = read(TcpConnectionSocket, &(Buffer[TcpReadBufPointer]), TcpRestLen);
3038  else
3039  DataRead = read(TcpConnectionSocket, &(Buffer[TcpReadBufPointer]), Length);
3040 
3041  //#define DEBUG
3042  #undef DEBUG
3043  #ifdef DEBUG
3044  printf("DataRead = %d\r\n", DataRead);
3045  #endif
3046 
3047  if(DataRead == 0) // We've a disconnect
3048  {
3049  cWiFiResetTcp();
3050  break;
3051  }
3052 
3053  TcpRestLen -= DataRead;
3054  if(TcpRestLen == 0)
3055  {
3057  }
3058 
3059  //#define DEBUG
3060  #undef DEBUG
3061  #ifdef DEBUG
3062  printf("\r\nTCP_WAIT_COLLECT_BYTES TcpRestLen = %d, DataRead incl. 2 = %d, Length = %d\r\n", TcpRestLen, DataRead, Length);
3063  #endif
3064 
3065  break;
3066 
3067  default: // Should never go here....
3069  break;
3070  }
3071  }
3072  }
3073 
3074  return DataRead;
3075 }
3076 
3077 RESULT cWiFiBeaconAnswer(void) // Wait for a "dating" PC
3078 {
3079  // This wait is performed as a NON-BLOCKING wait
3080  RESULT Result = FAIL;
3081  struct timeval TimeOutVal;
3082  fd_set ReadFdSet;
3083  TimeOutVal.tv_sec = 0;
3084  TimeOutVal.tv_usec = 0; // No Timeout val only use for NON-blocking test
3085 
3086  ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
3087 
3088  FD_ZERO(&ReadFdSet);
3089  FD_SET(UdpSocketDescriptor, &ReadFdSet);
3090 
3091  select(UdpSocketDescriptor + 1, &ReadFdSet, NULL, NULL, &TimeOutVal);
3092 
3093  if (FD_ISSET(UdpSocketDescriptor, &ReadFdSet))
3094  {
3095  // Beacon Answer is ready - we hope
3096  UdpRxCount = recvfrom(UdpSocketDescriptor, BufPtr, BufLen, 0, (struct sockaddr *)&ServerAddr, &ServerAddrLen);
3097  if(UdpRxCount < 0)
3098  {
3099  //#define DEBUG
3100  #undef DEBUG
3101  #ifdef DEBUG
3102  printf("\r\nUDP Client - recvfrom() ERROR UdpRxCount < 0!!!!\r\n");
3103  #endif
3104  }
3105  else
3106  {
3107 
3108  //#define DEBUG
3109  #undef DEBUG
3110  #ifdef DEBUG
3111  printf("\r\nUDP client received the following: \"%s\" message\\r\n", BufPtr);
3112  printf(" \r\nfrom port %d, address %s\r\n", ntohs(ServerAddr.sin_port), inet_ntoa(ServerAddr.sin_addr));
3113  #endif
3114 
3115  Result = OK;
3116  }
3117  }
3118  return Result;
3119 }
3120 
3122 
3123 {
3124  RESULT Result = FAIL;
3125 
3126  ServerAddr.sin_port = htons(BROADCAST_PORT);
3127  ServerAddr.sin_addr.s_addr = inet_addr(MyBroadCastAdr);
3128 
3129  //#define DEBUG
3130  #undef DEBUG
3131  #ifdef DEBUG
3132  printf("\r\nUDP BROADCAST to port %d, address %s\r\n", ntohs(ServerAddr.sin_port), inet_ntoa(ServerAddr.sin_addr));
3133  #endif
3134 
3135  cWiFiSetBtSerialNo(); // Be sure to have updated data :-)
3136  cWiFiSetBrickName(); // -
3137  sprintf(Buffer,"Serial-Number: %s\r\nPort: %d\r\nName: %s\r\nProtocol: EV3\r\n", BtSerialNo, TCP_PORT, BrickName);
3138 
3139  UdpTxCount = sendto(UdpSocketDescriptor, Buffer, strlen(Buffer), 0, (struct sockaddr *)&ServerAddr, sizeof(ServerAddr));
3140  if(UdpTxCount < 0)
3141  {
3142  //#define DEBUG
3143  #undef DEBUG
3144  #ifdef DEBUG
3145  printf("\r\nUDP SendTo ERROR : %d\n\r", UdpTxCount);
3146  #endif
3147 
3148  cWiFiUdpClientClose(); // Kill the auto-beacon stuff
3149  }
3150  else
3151  {
3152  //#define DEBUG
3153  #undef DEBUG
3154  #ifdef DEBUG
3155  printf("\r\nUDP Client - SendTo() is OK! UdpTxCount = %d\r\n", UdpTxCount);
3156  printf("\r\nWaiting on a reply from UDP server...zzzzz zzzz - Send UNICAST only to me :-)\r\n");
3157  #endif
3158 
3159  Result = OK;
3160  }
3161  return Result;
3162 }
3163 
3165 {
3166  RESULT Result = FAIL;
3167  int Temp;
3168 
3170 
3171  /* Get a socket descriptor for UDP client (Beacon) */
3172  if((UdpSocketDescriptor = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
3173  {
3174  //#define DEBUG
3175  #undef DEBUG
3176  #ifdef DEBUG
3177  printf("\r\nUDP Client - socket() error\r\n");
3178  #endif
3179  }
3180  else
3181  {
3182  //#define DEBUG
3183  #undef DEBUG
3184  #ifdef DEBUG
3185  printf("\r\nUDP Client - socket() is OK!\r\n");
3186  printf("\r\nBroadCast Adr from Ifconfig = %s\r\n", MyBroadCastAdr);
3187  #endif
3188  ServerAddr.sin_family = AF_INET;
3189  ServerAddr.sin_port = htons(BROADCAST_PORT);
3190 
3191  if((ServerAddr.sin_addr.s_addr = inet_addr(MyBroadCastAdr)) == (unsigned long)INADDR_NONE)
3192  {
3193  //#define DEBUG
3194  #undef DEBUG
3195  #ifdef DEBUG
3196  printf("\r\nHOST addr == INADDR_NONE :-( \r\n");
3197  #endif
3198  }
3199  else
3200  {
3201  if(setsockopt(UdpSocketDescriptor, SOL_SOCKET, SO_BROADCAST, &BroadCast, sizeof(BroadCast)) < 0)
3202  {
3203  //#define DEBUG
3204  #undef DEBUG
3205  #ifdef DEBUG
3206  printf("\r\nCould not setsockopt SO_BROADCAST\r\n");
3207  #endif
3208  }
3209  else
3210  {
3211  Temp = fcntl(UdpSocketDescriptor, F_GETFL, 0);
3212  fcntl(UdpSocketDescriptor, F_SETFL, Temp | O_NONBLOCK); // Make the socket NON_BLOCKING
3213 
3214  //#define DEBUG
3215  #undef DEBUG
3216  #ifdef DEBUG
3217  printf("\r\nSet SOCKET NON-BLOCKING :-)...\r\n");
3218  #endif
3219 
3220  Result = OK;
3221  }
3222  }
3223 
3224  //#define DEBUG
3225  #undef DEBUG
3226  #ifdef DEBUG
3227  printf("\r\nINIT UDP ServerAddr.sin_port = %d, ServerAddr.sin_addr.s_addr = %s\r\n", ntohs(ServerAddr.sin_port), inet_ntoa(ServerAddr.sin_addr));
3228  #endif
3229 
3230  }
3231  return Result;
3232 }
3233 
3235 {
3236  FILE *File;
3237 
3238  // Get the file-based BT SerialNo
3239  File = fopen("./settings/BTser","r");
3240  if(File != NULL) // if something to read
3241  {
3242  fgets(BtSerialNo, BLUETOOTH_SER_LENGTH, File);
3243  fclose(File);
3244  }
3245 }
3246 
3248 {
3249  FILE *File;
3250 
3251  // Get the file-based BrickName
3252  File = fopen("./settings/BrickName","r");
3253  if(File != NULL) // if something to read
3254  {
3255  fgets(BrickName, BRICK_HOSTNAME_LENGTH, File);
3256  fclose(File);
3257  }
3258 }
3259 
3260 int cWiFiCheckForLoadedModule(char *NameToCheck)
3261 {
3262  FILE *pProcModules;
3263  char ReadBuffer[64];
3264  const char WhiteSpaces[] = " ,-;:."; // "- " should be the ones, but...
3265  char *DriverName;
3266  int RetResult = FALSE;
3267 
3268  pProcModules = fopen("/proc/modules", "r");
3269 
3270  while (fgets(ReadBuffer, sizeof (ReadBuffer), pProcModules) != NULL)
3271  {
3272  DriverName = strtok(ReadBuffer, WhiteSpaces);
3273  if(strcmp(DriverName, NameToCheck) == 0)
3274  {
3275  //#define DEBUG
3276  #undef DEBUG
3277  #ifdef DEBUG
3278  printf("NameToCheck found: %s\r\n", NameToCheck);
3279  #endif
3280  RetResult = TRUE;
3281  break;
3282  }
3283  else
3284  {
3285  //#define DEBUG // Just added as DEBUG :-)
3286  #undef DEBUG
3287  #ifdef DEBUG
3288  printf("NameToCheck NOT found: %s\r\n", NameToCheck);
3289  #endif
3290  }
3291  }
3292  fclose(pProcModules);
3293  return RetResult;
3294 }
3295 
3297 {
3298  //#define DEBUG
3299  #undef DEBUG
3300  #ifdef DEBUG
3301  if(!(cWiFiCheckForLoadedModule("compat")))
3302  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/compat/compat.ko");
3303  #else
3304  if(!(cWiFiCheckForLoadedModule("compat")))
3305  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/compat/compat.ko &> /dev/null");
3306  #endif
3307 
3308  //#define DEBUG
3309  #undef DEBUG
3310  #ifdef DEBUG
3311  if(!(cWiFiCheckForLoadedModule("cfg80211")))
3312  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/net/wireless/cfg80211.ko");
3313  #else
3314  if(!(cWiFiCheckForLoadedModule("cfg80211")))
3315  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/net/wireless/cfg80211.ko &> /dev/null");
3316  #endif
3317 
3318  //#define DEBUG
3319  #undef DEBUG
3320  #ifdef DEBUG
3321  if(!(cWiFiCheckForLoadedModule("mac80211")))
3322  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/net/mac80211/mac80211.ko");
3323  #else
3324  if(!(cWiFiCheckForLoadedModule("mac80211")))
3325  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/net/mac80211/mac80211.ko &> /dev/null");
3326  #endif
3327 
3328  //#define DEBUG
3329  #undef DEBUG
3330  #ifdef DEBUG
3331  if(!(cWiFiCheckForLoadedModule("ath")))
3332  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath.ko");
3333  #else
3334  if(!(cWiFiCheckForLoadedModule("ath")))
3335  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath.ko &> /dev/null");
3336  #endif
3337 
3338  //#define DEBUG
3339  #undef DEBUG
3340  #ifdef DEBUG
3341  if(!(cWiFiCheckForLoadedModule("ath9k_hw")))
3342  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k_hw.ko");
3343  #else
3344  if(!(cWiFiCheckForLoadedModule("ath9k_hw")))
3345  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k_hw.ko &> /dev/null");
3346  #endif
3347 
3348  //#define DEBUG
3349  #undef DEBUG
3350  #ifdef DEBUG
3351  if(!(cWiFiCheckForLoadedModule("ath9k_common")))
3352  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k_common.ko");
3353  #else
3354  if(!(cWiFiCheckForLoadedModule("ath9k_common")))
3355  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k_common.ko &> /dev/null");
3356  #endif
3357 
3358  //#define DEBUG
3359  #undef DEBUG
3360  #ifdef DEBUG
3361  if(!(cWiFiCheckForLoadedModule("ath9k")))
3362  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k.ko");
3363  #else
3364  if(!(cWiFiCheckForLoadedModule("ath9k")))
3365  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k.ko &> /dev/null");
3366  #endif
3367 
3368  //#define DEBUG
3369  #undef DEBUG
3370  #ifdef DEBUG
3371  if(!(cWiFiCheckForLoadedModule("ath9k_htc")))
3372  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k_htc.ko");
3373  #else
3374  if(!(cWiFiCheckForLoadedModule("ath9k_htc")))
3375  system("/sbin/insmod /lib/modules/2.6.33-rc4/kernel/drivers/net/wireless/ath/ath9k/ath9k_htc.ko &> /dev/null");
3376  #endif
3377 }
3378 
3380 {
3381  //#define DEBUG
3382  #undef DEBUG
3383  #ifdef DEBUG
3384  if(cWiFiCheckForLoadedModule("ath9k_htc"))
3385  system("/sbin/rmmod ath9k_htc.ko");
3386  #else
3387  if(cWiFiCheckForLoadedModule("ath9k_htc"))
3388  system("/sbin/rmmod ath9k_htc.ko &> /dev/null");
3389  #endif
3390 
3391  //#define DEBUG
3392  #undef DEBUG
3393  #ifdef DEBUG
3394  if(cWiFiCheckForLoadedModule("ath9k"))
3395  system("/sbin/rmmod ath9k.ko");
3396  #else
3397  if(cWiFiCheckForLoadedModule("ath9k"))
3398  system("/sbin/rmmod ath9k.ko &> /dev/null");
3399  #endif
3400 
3401  //#define DEBUG
3402  #undef DEBUG
3403  #ifdef DEBUG
3404  if(cWiFiCheckForLoadedModule("ath9k_common"))
3405  system("/sbin/rmmod ath9k_common.ko");
3406  #else
3407  if(cWiFiCheckForLoadedModule("ath9k_common"))
3408  system("/sbin/rmmod ath9k_common.ko &> /dev/null");
3409  #endif
3410 
3411  //#define DEBUG
3412  #undef DEBUG
3413  #ifdef DEBUG
3414  if(cWiFiCheckForLoadedModule("ath9k_hw"))
3415  system("/sbin/rmmod ath9k_hw.ko");
3416  #else
3417  if(cWiFiCheckForLoadedModule("ath9k_hw"))
3418  system("/sbin/rmmod ath9k_hw.ko &> /dev/null");
3419  #endif
3420 
3421  //#define DEBUG
3422  #undef DEBUG
3423  #ifdef DEBUG
3424  if(cWiFiCheckForLoadedModule("ath"))
3425  system("/sbin/rmmod ath.ko");
3426  #else
3427  if(cWiFiCheckForLoadedModule("ath"))
3428  system("/sbin/rmmod ath.ko &> /dev/null");
3429  #endif
3430 
3431  //#define DEBUG
3432  #undef DEBUG
3433  #ifdef DEBUG
3434  if(cWiFiCheckForLoadedModule("mac80211"))
3435  system("/sbin/rmmod mac80211.ko");
3436  #else
3437  if(cWiFiCheckForLoadedModule("mac80211"))
3438  system("/sbin/rmmod mac80211.ko &> /dev/null");
3439  #endif
3440 
3441  //#define DEBUG
3442  #undef DEBUG
3443  #ifdef DEBUG
3444  if(cWiFiCheckForLoadedModule("cfg80211"))
3445  system("/sbin/rmmod cfg80211.ko");
3446  #else
3447  if(cWiFiCheckForLoadedModule("cfg80211"))
3448  system("/sbin/rmmod cfg80211.ko &> /dev/null");
3449  #endif
3450 
3451  //#define DEBUG
3452  #undef DEBUG
3453  #ifdef DEBUG
3454  if(cWiFiCheckForLoadedModule("compat"))
3455  system("/sbin/rmmod compat.ko");
3456  #else
3457  if(cWiFiCheckForLoadedModule("compat"))
3458  system("/sbin/rmmod compat.ko &> /dev/null");
3459  #endif
3460 }
3461 
3462 void cWiFiControl(void)
3463 {
3464  struct stat st;
3465  char Command[128];
3466 
3467  if(BeaconTx == TX_BEACON) // Do we have to TX the beacons?
3468  {
3469  if(cWiFiCheckTimer() >= BEACON_TIME) // If is it time for it?
3470  {
3471  if(cWiFiTransmitBeacon() != OK) // Did we manage to TX one?
3472  {
3473  // Error handling - TODO: Should be user friendly
3474  }
3475  else
3476  {
3477  #undef DEBUG
3478  //#define DEBUG
3479  #ifdef DEBUG
3480  printf("\n\rOK beacon TX\n\r");
3481  #endif
3482 
3483  cWiFiStartTimer(); // Reset for another timing (Beacon)
3484  }
3485  }
3486  }
3487 
3488  switch( WiFiConnectionState)
3489  {
3490 
3491  case WIFI_NOT_INITIATED: // NOTHING INIT'ed
3492  // Idle
3493 
3494  #undef DEBUG
3495  //#define DEBUG
3496  #ifdef DEBUG
3497  printf("\r\nIDLE... %d\r", WiFiStatus); // Too many prints if enabled ;-)
3498  #endif
3499 
3500  break;
3501 
3502  case WIFI_INIT: // Do the time consumption stuff
3503  switch( InitState )
3504  {
3505  case NOT_INIT: // Start the Wpa_Supplicant in BackGround using
3506  // a very "thin" .conf file
3507 
3508  #undef DEBUG
3509  //#define DEBUG
3510  #ifdef DEBUG
3511  printf("\r\nWIFI_INIT, NOT_INIT foer FindMacAddr... %d\r\n", WiFiStatus);
3512  #endif
3513 
3514  cWiFiFindMacAddr(); // Get the H/W MAC-address
3515 
3516  #undef DEBUG
3517  //#define DEBUG
3518  #ifdef DEBUG
3519  printf("\r\nWIFI_INIT, NOT_INIT efter FindMacAddr... %d\r\n", WiFiStatus);
3520  #endif
3521 
3522  cWiFiStartTimer();
3523 
3524  #undef DEBUG
3525  //#define DEBUG
3526  #ifdef DEBUG
3527  printf("\r\nWIFI_INIT, NOT_INIT foer system... %d\r\n", WiFiStatus);
3528  #endif
3529 
3530  cWiFiStartWpaSupplicant("/etc/wpa_supplicant.conf", LogicalIfName);
3531  //system("./wpa_supplicant -Dwext -iwlan<X> -c/etc/wpa_supplicant.conf -B");
3533  break;
3534 
3537  {
3538  strcpy(Command, "/var/run/wpa_supplicant/");
3539  strcat(Command, LogicalIfName);
3540  if(stat(Command, &st) == 0)
3541  {
3542  #undef DEBUG
3543  //#define DEBUG
3544  #ifdef DEBUG
3545  printf("\r\nWIFI_INIT, LOAD_SUPPLICANT => STAT OK %d\r\n", WiFiStatus);
3546  #endif
3547 
3548  // Ensure (help) Interface to become ready
3549  strcpy(Command, "ifconfig ");
3550  strcat(Command, LogicalIfName);
3551  strcat(Command, " down > /dev/null");
3552 
3553  system(Command);
3554 
3555  strcpy(Command, "ifconfig ");
3556  strcat(Command, LogicalIfName);
3557  strcat(Command, " up > /dev/null");
3558 
3559  system(Command);
3560 
3562  }
3563  //else keep waiting
3564  }
3565  else
3566  {
3567  #undef DEBUG
3568  //#define DEBUG
3569  #ifdef DEBUG
3570  printf("\r\nWIFI_INIT, LOAD_SUPPLICANT => Timed out\r\n");
3571  #endif
3572 
3573  WiFiStatus = FAIL;
3574  WiFiConnectionState = WIFI_NOT_INITIATED; // We're sleeping until user select ON
3575  InitState = NOT_INIT;
3576  }
3577  break;
3578 
3579  case WAIT_ON_INTERFACE: // Wait for the Control stuff to be ready
3580  // Get "handle" to Control Interface
3581  strcpy(Command, "/var/run/wpa_supplicant/");
3582  strcat(Command, LogicalIfName);
3583 
3584  if((ctrl_conn = wpa_ctrl_open(Command)) > 0)
3585  {
3586  if(cWiFiWpaPing() == 0)
3587  {
3588  #undef DEBUG
3589  //#define DEBUG
3590  #ifdef DEBUG
3591  printf("\r\nWIFI_INIT, WAIT_ON_INTERFACE => Ping OK %d\r\n", WiFiStatus);
3592  #endif
3593 
3595  WiFiStatus = OK;
3597  InitState = DONE;
3598  }
3599  else
3600  {
3601  #undef DEBUG
3602  //#define DEBUG
3603  #ifdef DEBUG
3604  printf("\r\nWIFI_INIT, WAIT_ON_INTERFACE => PING U/S\r\n");
3605  #endif
3606 
3607  WiFiStatus = FAIL;
3608  cWiFiExit();
3609  }
3610  }
3611 
3612  break;
3613 
3614  default: break;
3615  }
3616  break;
3617 
3618  case WIFI_INITIATED: // Temporary state - WiFi lower Stuff turned ON
3619  WiFiOnStatus = OK;
3621 
3622  #undef DEBUG
3623  //#define DEBUG
3624  #ifdef DEBUG
3625  printf("\r\nWIFI_INITIATED %d\r\n", WiFiStatus);
3626  #endif
3627 
3629 
3630  #undef DEBUG
3631  //#define DEBUG
3632  #ifdef DEBUG
3633  printf("\r\nREADY for search -> %d\r\n", WiFiStatus);
3634  #endif
3635 
3636  break;
3637 
3638  case READY_FOR_AP_SEARCH: // We can select SEARCH i.e. Press Connections on the U.I.
3639  // We have the H/W stack up and running
3640  break;
3641 
3642  case SEARCH_APS: // Polite wait
3643 
3644  //#define DEBUG
3645  #undef DEBUG
3646  #ifdef DEBUG
3647  printf("\r\nSEARCH_APS:\r\n");
3648  printf("\r\ncIndex = %d => %s\r\n", 0, ApTable[0].friendly_name);
3649  printf("\r\ncIndex = %d => %s\r\n", 1, ApTable[1].friendly_name);
3650  printf("\r\ncIndex = %d => %s\r\n", 2, ApTable[2].friendly_name);
3651  #endif
3652 
3653  break;
3654 
3655  case SEARCH_PENDING:
3656  // Wait some time for things to show up...
3657 
3658  //#define DEBUG
3659  #undef DEBUG
3660  #ifdef DEBUG
3661  printf("cWiFiCheckTimer() = %d\r", cWiFiCheckTimer());
3662  #endif
3663 
3664  // Give some time for the stuff to show up
3665  if(20 <= cWiFiCheckTimer()) // Get Elapsed time in seconds
3666  {
3667  // Getting the list and update the visible list
3669  }
3670 
3671  #undef DEBUG
3672  //#define DEBUG
3673  #ifdef DEBUG
3674  printf("\r\nSEARCH_PENDING:\r\n");
3675  printf("\r\ncIndex = %d => %s\r\n", 0, ApTable[0].friendly_name);
3676  printf("\r\ncIndex = %d => %s\r\n", 1, ApTable[1].friendly_name);
3677  printf("\r\ncIndex = %d => %s\r\n", 2, ApTable[2].friendly_name);
3678  #endif
3679 
3680  #undef DEBUG
3681  //#define DEBUG
3682  #ifdef DEBUG
3683  printf("\r\nSTORED CORRECT:\r\n");
3684  printf("\r\ncIndex = %d => %s\r\n", 0, ApTable[0].friendly_name);
3685  printf("\r\ncIndex = %d => %s\r\n", 1, ApTable[1].friendly_name);
3686  printf("\r\ncIndex = %d => %s\r\n", 2, ApTable[2].friendly_name);
3687  #endif
3688 
3689  #undef DEBUG
3690  //#define DEBUG
3691  #ifdef DEBUG
3692  printf("\r\nSEARCH DONE\r\n");
3693  #endif
3694 
3695  break;
3696 
3697  case AP_LIST_UPDATED: // Relaxed state until connection wanted
3698 
3699  #undef DEBUG
3700  //#define DEBUG
3701  #ifdef DEBUG
3702  printf("\r\nAP_LIST_UPDATED ApTableSize = %d:\r\n", ApTableSize);
3703  printf("\r\ncIndex = %d => %s\r\n", 0, ApTable[0].friendly_name);
3704  printf("\r\ncIndex = %d => %s\r\n", 1, ApTable[1].friendly_name);
3705  printf("\r\ncIndex = %d => %s\r\n", 2, ApTable[2].friendly_name);
3706  #endif
3707 
3708  break;
3709 
3710  case AP_CONNECTING: // First connecting to the selected AP
3711  break;
3712 
3713  case WIFI_CONNECTED_TO_AP: // We have an active AP connection
3714  // Then get a valid IP address via DHCP
3715  break;
3716 
3717  case UDP_NOT_INITIATED: // We have an valid IP address
3718  // Initiated, connected and ready for UDP
3719  // I.e. ready for starting Beacons
3720 
3721  #undef DEBUG
3722  //#define DEBUG
3723  #ifdef DEBUG
3724  printf("\r\nHer er UDP_NOT_INITIATED\r\n");
3725  #endif
3726 
3728 
3729  break;
3730 
3731  case INIT_UDP_CONNECTION:
3732  WiFiStatus = BUSY; // We're still waiting
3733  memset(Buffer, 0x00, sizeof(Buffer)); // Reset TX buffer
3734 
3735  #undef DEBUG
3736  //#define DEBUG
3737  #ifdef DEBUG
3738  printf("\r\nLige foer cWiFiInitUdpConnection()\r\n");
3739  #endif
3740 
3741  if(cWiFiInitUdpConnection() == OK)
3742  {
3743  #undef DEBUG
3744  //#define DEBUG
3745  #ifdef DEBUG
3746  printf("\r\nUDP connection READY @ INIT_UDP_CONNECTION\r\n");
3747  #endif
3748 
3749  if(cWiFiTransmitBeacon() == OK) // Did we manage to TX one?
3750  {
3752  BeaconTx = TX_BEACON; // Enable Beacon
3753  cWiFiStartTimer(); // Start timing (Beacon)
3754  WiFiStatus = OK;
3755  }
3756  else
3757  {
3758  // TODO: Some ERROR handling where to go - should be user friendly
3759  }
3760  }
3761  else
3762  {
3763  #undef DEBUG
3764  //#define DEBUG
3765  #ifdef DEBUG
3766  printf("\r\nUDP connection FAILed @ INIT_UDP_CONNECTION\r\n");
3767  #endif
3768 
3769  WiFiStatus = FAIL;
3771  }
3772  break;
3773 
3774  case UDP_FIRST_TX: if(cWiFiCheckTimer() >= 2) // Allow some time before..
3775  {
3777  }
3778  break;
3779 
3780  case UDP_VISIBLE: // TX'ing beacon via UDP
3781  // We're tx'ing beacons, but waiting for connection
3782  // Are there any "dating" PC's?
3783  // Non-blocking test
3784 
3786 
3787  break;
3788 
3789  case UDP_CONNECTED: // We have an active negotiation connection
3790  // Temp state between OK UDP negotiation
3791  // and the "real" TCP communication
3792 
3793  if(cWiFiInitTcpServer() == OK)
3794  {
3795  #undef DEBUG
3796  //#define DEBUG
3797  #ifdef DEBUG
3798  printf("\r\nTCP init OK @ UDP_CONNECTED\r\n");
3799  #endif
3800 
3802  }
3803  break;
3804 
3805  case TCP_NOT_CONNECTED: // Waiting for the PC to connect via TCP
3806  // Non-blocking test
3807  if(cWiFiWaitForTcpConnection() == OK)
3808  {
3809  #undef DEBUG
3810  //#define DEBUG
3811  #ifdef DEBUG
3812  printf("\r\nTCP_CONNECTED @ TCP_NOT_CONNECTED\r\n");
3813  #endif
3814 
3815  TcpState = TCP_UP;
3817  // We are connected so we can tell the
3818  // world....
3819  // And we're ready to TX/RX :-)
3820  WiFiStatus = OK; // Not busy any longer
3821  }
3822  break;
3823 
3824  case TCP_CONNECTED: // We have a TCP connection established
3825 
3826  #undef DEBUG
3827  //#define DEBUG
3828  #ifdef DEBUG
3829  printf("\r\nTCP_CONNECTED @ TCP_CONNECTED.... And then.....\r\n");
3830  #endif
3831 
3832  break;
3833 
3834  case CLOSED: // UDP/TCP closed
3835 
3836  break;
3837 
3838  default:
3839 
3840  break;
3841  }
3842  if(WiFiOnStatus == OK) // Íf some logic fired up
3843  { // Check for the "volatile" USB dongle connection
3844  if(cWiFiTimeFromLastDongleCheck() >= TIME_FOR_WIFI_DONGLE_CHECK) // Don't misuse the CPU cycles
3845  {
3846  #undef DEBUG
3847  //#define DEBUG
3848  #ifdef DEBUG
3849  printf("Check for Dongle....");
3850  #endif
3851 
3852  if(cWiFiKnownDongleAttached() != OK)
3853  {
3854  #undef DEBUG
3855  //#define DEBUG
3856  #ifdef DEBUG
3857  printf("FAIL!!\n\r");
3858  #endif
3859  cWiFiTurnOff(); // No H/W
3860  }
3861  else
3862  {
3864  #undef DEBUG
3865  //#define DEBUG
3866  #ifdef DEBUG
3867  printf("OK!!\n\r");
3868  #endif
3869  }
3870  }
3871  }
3872 }
3873 
3874 
3875 RESULT cWiFiGetOnStatus(void)
3876 {
3877  //#define DEBUG
3878  #undef DEBUG
3879  #ifdef DEBUG
3880  printf("cWiFiGetOnStatus returns: %d\r\n", WiFiOnStatus);
3881  #endif
3882  return WiFiOnStatus;
3883 }
3884 
3885 RESULT cWiFiTurnOn(void) // TURN ON
3886 {
3887  RESULT Result = FAIL;
3888  char Command[64];
3889 
3890  WiFiStatus = BUSY; // We will use some (a lot of) time before we're ready
3891 
3892  if(WiFiConnectionState == WIFI_NOT_INITIATED) // Only try to initiated un- stuff ;-)
3893  {
3894  cWiFiInit();
3895  cWiFiLoadAthHwModules(); // Load the foundation for the rest
3897 
3898  //#define DEBUG
3899  #undef DEBUG
3900  #ifdef DEBUG
3901  printf("\r\ncWiFiTurnOn and LOGIC NAME FOUND: %s\r\n", LogicalIfName);
3902  #endif
3903 
3904  // Remove the Control Interface - if exist...
3905  strcpy(Command, "rm /var/run/wpa_supplicant/");
3906  strcat(Command, LogicalIfName);
3907 
3908  strcat(Command, " &> /dev/null");
3909  system(Command);
3910 
3911  Result = OK;
3913  WiFiOnStatus = OK;
3914  }
3915  else
3916  {
3917  // Just debugging stuff
3918  //#define DEBUG
3919  #undef DEBUG
3920  #ifdef DEBUG
3921  printf("\r\ncWiFiTurnOn refused ALREADY ON or should be....\r\n");
3922  #endif
3923  }
3924 
3925  WiFiStatus = OK;
3926  return Result;
3927 }
3928 
3929 RESULT cWiFiTurnOff(void) // TURN OFF
3930 {
3931  RESULT Result = FAIL;
3932  int PC = 0;
3933 
3934  //#define DEBUG
3935  #undef DEBUG
3936  #ifdef DEBUG
3937  printf("cWiFiTurnOff called...\r\n");
3938  #endif
3939 
3940  WiFiOnStatus = FAIL;
3941 
3942  if (ctrl_conn != NULL)
3943  {
3944  cWiFiKillUdHcPc();
3945  BeaconTx = NO_TX;
3946 
3947  // Store persistent data - i.e. all the old and new connections and -data
3948 
3949  PC = cWiFiStoreKnownApList();
3950  if(PC >= 0)
3951  Result = OK;
3952 
3953  //#define DEBUG
3954  #undef DEBUG
3955  #ifdef DEBUG
3956  printf("cWiFiStoreKnownApList() returned %d\n\r", PC);
3957  #endif
3958 
3959  cWiFiTcpClose();
3960 
3962 
3963  cWiFiTerminate();
3964 
3965  //#define DEBUG
3966  #undef DEBUG
3967  #ifdef DEBUG
3968  if(WiFiStatus == OK)
3969  printf("At TurnOff cWiFiTerminate()-> WiFiStatus == OK\n\r");
3970  else
3971  printf("At TurnOff cWiFiTerminate()-> WiFiStatus == NOT ok\n\r");
3972  #endif
3973 
3975  InitState = NOT_INIT;
3976  wpa_ctrl_close(ctrl_conn);
3977  ctrl_conn = NULL;
3978  cWiFiUnLoadAthHwModules(); // Unload the foundation for the rest
3980  }
3981 
3982  //#define DEBUG
3983  #undef DEBUG
3984  #ifdef DEBUG
3985  if(WiFiStatus == OK)
3986  printf("At TurnOff -> WiFiStatus == OK\n\r");
3987  else
3988  printf("At TurnOff -> WiFiStatus == NOT ok\n\r");
3989  #endif
3990 
3991  WiFiStatus = OK; // NO FAIL BREAKing
3992  Result = OK; // -
3993  ApTable[0].ap_flags &= ~(CONNECTED); // We should hopefully be disconnected as well
3994  // I.e. Turned OFF!
3995  return Result;
3996 }
3997 
3998 RESULT cWiFiExit(void)
3999 {
4000  RESULT Result = FAIL;
4001 
4002  Result = cWiFiTurnOff();
4003  return Result;
4004 }
4005 
4006 RESULT cWiFiInit(void)
4007 {
4008  RESULT Result = FAIL;
4009 
4010  //#define DEBUG
4011  #undef DEBUG
4012  #ifdef DEBUG
4013  printf("\r\ncWiFiInit START %d\r\n", WiFiStatus);
4014  #endif
4015 
4016  WiFiOnStatus = FAIL;
4017  BeaconTx = NO_TX;
4018  WiFiConnectionState = WIFI_NOT_INITIATED; // We're sleeping until user select ON
4019  InitState = NOT_INIT;
4021  // Set IP address to UNKNOWN
4022  strcpy(MyIp4Address, "???");
4023  // Set MAC address of Interface to UNKNOWN (NOT read yet)
4024  strcpy(MyHwMacAddress, "??:??:??:??:??:??");
4027  WiFiStatus = OK;
4028  Result = OK;
4029 
4030  //#define DEBUG
4031  #undef DEBUG
4032  #ifdef DEBUG
4033  printf("\r\nWiFiStatus = %d\r\n", WiFiStatus);
4034  #endif
4035 
4036  return Result;
4037 }
int cWiFiPopulateKnownApList(void)
Definition: c_wifi.c:260
void cWiFiSortBySignalLevel()
Definition: c_wifi.c:2221
void cWiFiCleanUpOldStuff(void)
Definition: c_wifi.c:1760
RESULT cWiFiInitTcpServer()
Definition: c_wifi.c:2627
UBYTE TcpReadState
Definition: c_wifi.c:131
int TCPListenServer
Definition: c_wifi.c:128
RESULT cWiFiSetProtoRsn(void)
Definition: c_wifi.c:1516
UWORD cWiFiWriteTcp(UBYTE *Buffer, UWORD Length)
Definition: c_wifi.c:2757
RESULT cWiFiGetLogicalName(void)
Definition: c_wifi.c:2574
int TcpConnectionSocket
Definition: c_wifi.c:126
RESULT cWiFiGetStatus(void)
Definition: c_wifi.c:2601
Definition: lms2012.h:558
RESULT cWiFiFindMacAddr(void)
Definition: c_wifi.c:1221
Definition: c_wifi.h:153
struct wpa_ctrl * cWiFiOpenCtrlInterFace(char *CtrlPath, char *InterFace)
Definition: c_wifi.c:252
RESULT cWiFiStoreActualApList()
Definition: c_wifi.c:2250
RESULT cWiFiInitUdpConnection(void)
Definition: c_wifi.c:3164
int cWiFiCheckTimer(void)
Definition: c_wifi.c:187
RESULT cWiFiGetName(char *ApName, int Index, char Length)
Definition: c_wifi.c:1681
void cWiFiMoveDownInList(int Index)
Definition: c_wifi.c:586
RESULT cWiFiWaitForTcpConnection(void)
Definition: c_wifi.c:2717
struct timeval TimerStartVal TimerCurrentVal
Definition: c_wifi.c:97
int UdpTxCount
Definition: c_wifi.c:116
#define WIFI_VENDOR_ID
Definition: c_wifi.c:84
#define HARDWARE_SEARCH_STRING
Definition: c_wifi.c:82
#define WIFI_PERSISTENT_PATH
Definition: c_wifi.h:46
#define BRICK_HOSTNAME_LENGTH
Definition: c_wifi.h:72
#define BLUETOOTH_SER_LENGTH
Definition: c_wifi.h:71
RESULT cWiFiTcpClose(void)
Definition: c_wifi.c:530
struct timeval UdpWait
Definition: c_wifi.c:120
struct sockaddr_in ServerAddr
Definition: c_wifi.c:117
#define WPA_APPLIKATION_PATH
Definition: c_wifi.c:93
void cWiFiSetBrickName(void)
Definition: c_wifi.c:3247
TCP_STATES
Definition: c_wifi.h:128
#define MAX_AP_ENTRIES
Definition: c_wifi.c:44
void cWiFiCopyStoreToActual(int StoreIndex, int ActualIndex)
Definition: c_wifi.c:654
RESULT cWiFiReconnect(void)
Definition: c_wifi.c:1642
TCP_STATES TcpState
Definition: c_wifi.c:123
unsigned int TimeOut
Definition: c_wifi.c:100
#define TCP_PORT
Definition: c_wifi.h:66
RESULT cWiFiSetSsid(char *Ssid)
Definition: c_wifi.c:927
void cWiFiControl(void)
Definition: c_wifi.c:3462
RESULT cWiFiSetKeyManagToWpa2(void)
Definition: c_wifi.c:979
RESULT cWiFiKnownDongleAttached(void)
Definition: c_wifi.c:1311
void cWifiMoveAllActualDown(int SourcePointer)
Definition: c_wifi.c:630
Definition: lms2012.h:557
RESULT cWiFiGetOnStatus(void)
Definition: c_wifi.c:3875
RESULT cWiFiTxingBeacons(void)
Definition: c_wifi.c:2611
void cWiFiMoveDownInActualList(int Index)
Definition: c_wifi.c:458
Definition: c_wifi.h:139
int cWiFiCheckForLoadedModule(char *NameToCheck)
Definition: c_wifi.c:3260
void cWiFiLoadAthHwModules(void)
Definition: c_wifi.c:3296
RESULT cWiFiSetKeyManagToNone(void)
Definition: c_wifi.c:1006
void cWiFiStartDongleCheckTimer(void)
Definition: c_wifi.c:174
RESULT cWiFiSetScanSsidToOne(void)
Definition: c_wifi.c:878
RESULT cWiFiAddHidden(char *HiddenApName, char *Security, char *PassWord)
Definition: c_wifi.c:1119
fd_set UdpReadFlags
Definition: c_wifi.c:119
#define WIFI_DONGLE_ID
Definition: c_wifi.c:85
void cWiFiRestoreStorageApRecord(int Destination)
Definition: c_wifi.c:717
struct timeval DongleCheckStartVal DongleCheckCurrentVal
Definition: c_wifi.c:98
RESULT cWiFiDeepDongleSearch(void)
Definition: c_wifi.c:1278
Definition: c_wifi.h:131
RESULT cWiFiTurnOn(void)
Definition: c_wifi.c:3885
int cWiFiStartWpaSupplicant(char *ConfigPath, char *IFName)
Definition: c_wifi.c:232
void cWiFiMoveUpInActualList(int Index)
Definition: c_wifi.c:451
#define BROADCAST_PORT
Definition: c_wifi.h:65
int do_wpa_command(struct wpa_ctrl *control, char *command)
Definition: c_wifi.c:204
#define AP_FLAG_ADJUST_FOR_STORAGE
Definition: c_wifi.h:144
RESULT cWiFiFindIpAddr(void)
Definition: c_wifi.c:1350
unsigned char ap_flags
Definition: c_wifi.h:165
void cWiFiDeleteInStoreList(int Index)
Definition: c_wifi.c:615
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len, void(*msg_cb)(char *msg, size_t len))
Definition: wpa_ctrl.c:208
char Server[32]
Definition: c_wifi.c:115
#define BEACON_TIME
Definition: c_wifi.h:67
BEACON_MODE
Definition: c_wifi.h:114
void cWiFiRestoreActualApRecord(int Destination)
Definition: c_wifi.c:427
RESULT cWiFiCheckAndDelete(int Index)
Definition: c_wifi.c:664
void wpa_ctrl_close(struct wpa_ctrl *ctrl)
Definition: wpa_ctrl.c:137
#define WIFI_INIT_TIMEOUT
Definition: c_wifi.h:61
int WiFiStatus
Definition: c_wifi.c:124
void cWiFiUdpClientClose(void)
Definition: c_wifi.c:577
void cWiFiDeleteAsKnown(int LocalIndex)
Definition: c_wifi.c:1112
RESULT cWiFiTerminate(void)
Definition: c_wifi.c:785
RESULT cWiFiInit(void)
Definition: c_wifi.c:4006
RESULT cWiFiGetIpAddr(char *IpAddress)
Definition: c_wifi.c:1197
void cWiFiClearExceptZero()
Definition: c_wifi.c:1731
struct sockaddr_in servaddr client_addr
Definition: c_wifi.c:127
void cWiFiAddToKnownApList(int Index)
Definition: c_wifi.c:687
void cWiFiSetKnown(int Index)
Definition: c_wifi.c:1107
RESULT cWiFiExit(void)
Definition: c_wifi.c:3998
void cWiFiDeleteInList(int Index)
Definition: c_wifi.c:597
RESULT cWiFiSetName(char *ApName, int Index)
Definition: c_wifi.c:1699
RESULT cWiFiSetEnableNetwork(void)
Definition: c_wifi.c:1543
RESULT cWiFiMakeConnectionToAp(int Index)
Definition: c_wifi.c:1776
struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
Definition: wpa_ctrl.c:65
RESULT cWiFiConnectToAp(int Index)
Definition: c_wifi.c:2011
RESULT cWiFiTransmitBeacon(void)
Definition: c_wifi.c:3121
RESULT cWiFiRequestIpAdr(char *Interface)
Definition: c_wifi.c:1416
RESULT cWiFiTurnOff(void)
Definition: c_wifi.c:3929
void cWiFiSetBtSerialNo(void)
Definition: c_wifi.c:3234
void cWiFiUnLoadAthHwModules(void)
Definition: c_wifi.c:3379
void cWiFiStartTimer(void)
Definition: c_wifi.c:135
RESULT cWiFiDisconnect(void)
Definition: c_wifi.c:1606
int UdpRxCount
Definition: c_wifi.c:116
RESULT cWiFiRemoveNetwork(void)
Definition: c_wifi.c:477
RESULT cWiFiAddNetwork(void)
Definition: c_wifi.c:818
RESULT cWiFiKillUdHcPc(void)
Definition: c_wifi.c:140
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
Definition: lmstypes.h:29
int BroadCast
Definition: c_wifi.c:121
int WiFiConnectionState
Definition: c_wifi.c:111
int BufLen
Definition: c_wifi.c:104
int cWiFiIsItAFriend(char *mac_address)
Definition: c_wifi.c:2205
char * BufPtr
Definition: c_wifi.c:103
RESULT WiFiOnStatus
Definition: c_wifi.c:125
int InitState
Definition: c_wifi.c:112
char pre_shared_key[PSK_LENGTH]
Definition: c_wifi.h:161
RESULT cWiFiGetIndexFromName(char *Name, UBYTE *Index)
Definition: c_wifi.c:2152
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
Definition: lmstypes.h:30
char Buffer[1024]
Definition: c_wifi.c:102
RESULT cWiFiSetPairWiseCcmp(void)
Definition: c_wifi.c:1461
#define MAX_AP_STORAGE_ENTRIES
Definition: c_wifi.c:50
RESULT cWiFiGetHiddenMacAddr(char *MacAddr, int Index)
Definition: c_wifi.c:1156
int cWiFiTimeFromLastDongleCheck(void)
Definition: c_wifi.c:180
Definition: c_wifi.h:93
void cWiFiMoveUpInStoreList(int Index)
Definition: c_wifi.c:437
KNOWN_HW KnownWiFiDongle
Definition: c_wifi.c:106
UWORD TcpRestLen
Definition: c_wifi.c:130
void cWiFiSetEncryptToNone(int Index)
Definition: c_wifi.c:1099
void cWiFiMoveAllStoreDown(int SourcePointer)
Definition: c_wifi.c:642
void cWiFiCopyActualToStore(int ActualIndex, int StoreIndex)
Definition: c_wifi.c:659
int cWiFiGetApListSize(void)
Definition: c_wifi.c:2544
Definition: c_wifi.h:101
RESULT cWiFiMakePsk(char *ApSsid, char *PassPhrase, int Index)
Definition: c_wifi.c:2078
void cWiFiMoveUpInList(int Index)
Definition: c_wifi.c:465
unsigned char cWiFiGetFlags(int Index)
Definition: c_wifi.c:1726
RESULT cWiFiSetGroupCcmp(void)
Definition: c_wifi.c:1488
int UdpSocketDescriptor
Definition: c_wifi.c:114
void cWiFiMoveDownInStoreList(int Index)
Definition: c_wifi.c:444
RESULT cWiFiScanForAPs()
Definition: c_wifi.c:2477
void cWiFiPreserveActualApRecord(int Index)
Definition: c_wifi.c:432
RESULT cWiFiMergeActualAndKnownTable(void)
Definition: c_wifi.c:722
void cWiFiSetEncryptToWpa2(int Index)
Definition: c_wifi.c:1076
RESULT cWiFiGetMyMacAddr(char *MacAddress)
Definition: c_wifi.c:1209
RESULT cWiFiResetTcp(void)
Definition: c_wifi.c:2810
fd_set UdpWriteFlags
Definition: c_wifi.c:119
UWORD cWiFiReadTcp(UBYTE *Buffer, UWORD Length)
Definition: c_wifi.c:2824
RESULT cWiFiSetPsk(char *Psk)
Definition: c_wifi.c:1033
int cWiFiWpaPing(void)
Definition: c_wifi.c:2549
KNOWN_HW
Definition: c_wifi.h:121
uint TcpReadBufPointer
Definition: c_wifi.c:109
void cWiFiPreserveStorageApRecord(int Index)
Definition: c_wifi.c:712
UBYTE TcpReadBuffer[1024]
Definition: c_wifi.c:108
RESULT cWiFiBeaconAnswer(void)
Definition: c_wifi.c:3077
UWORD TcpTotalLength
Definition: c_wifi.c:129
void cWiFiIncApListSize(void)
Definition: c_wifi.c:2536
int cWiFiStoreKnownApList(void)
Definition: c_wifi.c:360
#define TIME_FOR_WIFI_DONGLE_CHECK
Definition: c_wifi.h:69
RESULT cWiFiGetApMacAddr(char *MacAddr, int Index)
Definition: c_wifi.c:1064
#define WIFI_PERSISTENT_FILENAME
Definition: c_wifi.h:47
Definition: c_wifi.h:140
void cWiFiTcpShutDown(int iSocket)
Definition: c_wifi.c:1742
RESULT cWiFiTcpConnected(void)
Definition: c_wifi.c:2619
Definition: c_wifi.h:116
BEACON_MODE BeaconTx
Definition: c_wifi.c:122
socklen_t ServerAddrLen
Definition: c_wifi.c:118
#define NULL
void wpa_control_message_callback(char *message, size_t length)
Definition: c_wifi.c:195
RESULT cWiFiWpaStatus(void)
Definition: c_wifi.c:1570