// returns access point list
 WiFi::APInfo* STC_FLASHMEM WiFi::getAPList(uint32_t* count, bool rescan)
 {
     if (rescan)
     {
         if (getMode() == AccessPoint)
             setMode(ClientAndAccessPoint);
         wifi_station_scan(NULL, scanDoneCB);
         getAPInfo()->receive();	// wait for completion
     }
     APInfo* infos;
     getAPInfo(&infos, count);
     return infos;
 }
// returns access point list
WiFi::APInfo *STC_FLASHMEM WiFi::getAPList(uint32_t *count, bool rescan, bool canRetry) {
  if (rescan) {
    if (getMode() == AccessPoint)
      setMode(ClientAndAccessPoint);
    wifi_station_scan(NULL, scanDoneCB);
    getAPInfo()->receive(); // wait for completion
  }
  APInfo *infos;
  getAPInfo(&infos, count);
  if (*count == 0 && canRetry) {
    // retry
    return getAPList(count, true, false); // canRetry = false, no more retry to avoid infinite recursive calls
  }
  return infos;
}
void STC_FLASHMEM WiFi::scanDoneCB(void *arg, STATUS status) {
  if (status == OK) {
    // count items
    uint32_t count = 0;
    for (bss_info *bss_link = ((bss_info *)arg)->next.stqe_next; bss_link; bss_link = bss_link->next.stqe_next)
      ++count;
    // fill items
    APInfo *infos;
    getAPInfo(&infos, &count, count);
    for (bss_info *bss_link = ((bss_info *)arg)->next.stqe_next; bss_link;
         bss_link = bss_link->next.stqe_next, ++infos) {
      memcpy(infos->BSSID, bss_link->bssid, 6);
      memset(infos->SSID, 0, 33);
      memcpy(infos->SSID, bss_link->ssid, 32);
      infos->Channel = bss_link->channel;
      infos->RSSI = bss_link->rssi;
      infos->AuthMode = (SecurityProtocol)bss_link->authmode;
      infos->isHidden = (bool)bss_link->is_hidden;
    }
    getAPInfo()->signal();
  }
}
Example #4
0
int scanAp()
{
    FILE       *result, *fp;
    const char *tmpfile = "ap.dat";
    const char *tmpcmd = "iwlist %s scan | egrep 'Cell |ESSID|Quality'";
    char       cmdline[64];
    int        numAP;

    while(EndFlag==OFF) {
        DebugPrintf("Start Scan\n");

        // Make Command
        if(MainDev==WLAN1) {
            sprintf(cmdline, tmpcmd, NameDev2);
        } else if(MainDev==WLAN2) {
            sprintf(cmdline, tmpcmd, NameDev1);
        }

        if ((result=popen(cmdline, "r")) == NULL) {
            perror ("Command error");
        }

        char buf[256];

        // Overwride AP data in 'ap.dat'
        if ((fp=fopen(tmpfile, "w")) == NULL) {
            perror ("File Open error");
        }
        while(!feof(result)) {
            fgets(buf, sizeof(buf), result);
            fputs(buf, fp);
        }
        fclose(fp);
        (void) pclose(result);

        // Get the information of AP
        if((numAP=getNumAP(tmpfile))>0) {
            INFOAP tmpInfoAp[numAP];
            getAPInfo(tmpfile, numAP, tmpInfoAp);

            // Write My Access Point
            if ((fp=fopen(filename, "w")) == NULL) {
                perror ("File Open error");
            }
            fclose(fp);
            if ((fp=fopen(filename, "a")) == NULL) {
                perror ("File Open error");
            }
            int i;
            for(i=0; i<numAP; i++) {
                if(strcmp(tmpInfoAp[i].ESSID, apEssId)==0) {
                    fprintf(fp, "Address : %s\n", tmpInfoAp[i].MacAddr);
                    fprintf(fp, "Quality : %d\n", tmpInfoAp[i].Qual);
                }
            }
            fclose(fp);

            // Success Get My AP
            if(getNumMyAP(filename)>0) {
                ScanFlag=ON;
                DebugPrintf("Finish Scan\n");
            } else {
                ScanFlag=OFF;
            }
        } else {
            ScanFlag=OFF;
        }

        sleep(5);
    }
    return(0);
}