Example #1
0
static void IRAM_ATTR esp_now_scan_peers_done (void)
{
    mutex_lock(&_esp_now_dev.dev_lock);

    esp_err_t ret;
    uint16_t ap_num;

    ret = esp_wifi_scan_get_ap_num(&ap_num);
    DEBUG("wifi_scan_get_ap_num ret=%d num=%d\n", ret ,ap_num);

    if (ret == ESP_OK && ap_num) {
        uint32_t state;
        /* reallocation of memory must not be disturbed */
        critical_enter_var(state);
        /* allocate memory for APs record list blockwise and fetch them the list */
        if (ap_num > aps_size) {
            if (aps) {
                /* free allocated AP record list memory */
                aps_size = 0;
                free (aps);
            }
            /* allocate new memory */
            aps_size = (ap_num & ~(ESP_NOW_APS_BLOCK_SIZE - 1)) + ESP_NOW_APS_BLOCK_SIZE;
            aps = malloc(sizeof(wifi_ap_record_t) * aps_size);
            ap_num = aps_size;
        }
        critical_exit_var(state);

        ret = esp_wifi_scan_get_ap_records(&ap_num, aps);

        DEBUG("wifi_scan_get_aps ret=%d num=%d\n", ret, ap_num);

        critical_enter_var(state);
        /* iterate over APs records */
        for (uint16_t i = 0; i < ap_num; i++) {

            /* check whether the AP is an ESP_NOW node which is not already a peer */
            if (strncmp((char*)aps[i].ssid, ESP_NOW_AP_PREFIX, ESP_NOW_AP_PREFIX_LEN) == 0 &&
                !esp_now_is_peer_exist(aps[i].bssid)) {
                /* add the AP as peer */
                _esp_now_add_peer(aps[i].bssid, aps[i].primary, esp_now_params.key);
            }
        }
        critical_exit_var(state);
    }

    #if ENABLE_DEBUG
    esp_now_peer_num_t peer_num;
    esp_now_get_peer_num (&peer_num);
    DEBUG("associated peers total=%d, encrypted=%d\n",
          peer_num.total_num, peer_num.encrypt_num);
    #endif

    _esp_now_scan_peers_done = true;

    /* set the time for next scan */
    xtimer_set (&_esp_now_scan_peers_timer, esp_now_params.scan_period);

    mutex_unlock(&_esp_now_dev.dev_lock);
}
Example #2
0
static void scan_task(void *pvParameters)
{
	while(1) {
		xEventGroupWaitBits(wifi_event_group, SCAN_DONE_BIT, false, true, portMAX_DELAY);
        ESP_LOGI(TAG, "WIFI scan doen");
		xEventGroupClearBits(wifi_event_group, SCAN_DONE_BIT);

		uint16_t apCount = 0;
		esp_wifi_scan_get_ap_num(&apCount);
		printf("Number of access points found: %d\n", apCount);
		if (apCount == 0) {
			ESP_LOGI(TAG, "Nothing AP found");
			return;
		}
		wifi_ap_record_t *list = (wifi_ap_record_t *)malloc(sizeof(wifi_ap_record_t) * apCount);
		ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, list));
		int i;
		printf("======================================================================\n");
		printf("             SSID             |    RSSI    |           AUTH           \n");
		printf("======================================================================\n");
		for (i=0; i<apCount; i++) {
			char *authmode;
			switch(list[i].authmode) {
			case WIFI_AUTH_OPEN:
               authmode = "WIFI_AUTH_OPEN";
               break;
            case WIFI_AUTH_WEP:
               authmode = "WIFI_AUTH_WEP";
               break;           
            case WIFI_AUTH_WPA_PSK:
               authmode = "WIFI_AUTH_WPA_PSK";
               break;           
            case WIFI_AUTH_WPA2_PSK:
               authmode = "WIFI_AUTH_WPA2_PSK";
               break;           
            case WIFI_AUTH_WPA_WPA2_PSK:
               authmode = "WIFI_AUTH_WPA_WPA2_PSK";
               break;
            default:
               authmode = "Unknown";
               break;
			}
			printf("%26.26s    |    % 4d    |    %22.22s\n",list[i].ssid, list[i].rssi, authmode);
		}
		free(list);
		printf("\n\n");

		// scan again
		vTaskDelay(2000 / portTICK_PERIOD_MS);
		//The true parameter cause the function to block until the scan is done.
		ESP_ERROR_CHECK(esp_wifi_scan_start(&scanConf, true));
	}


}
/**
 * private
 * scan callback
 * @param result  void *arg
 * @param status STATUS
 */
void WiFiScanClass::_scanDone()
{
    WiFiScanClass::_scanComplete = true;
    WiFiScanClass::_scanStarted = false;
    esp_wifi_scan_get_ap_num(&(WiFiScanClass::_scanCount));
    if(WiFiScanClass::_scanCount) {
        WiFiScanClass::_scanResult = new wifi_ap_record_t[WiFiScanClass::_scanCount];
        if(WiFiScanClass::_scanResult) {
            esp_wifi_scan_get_ap_records(&(WiFiScanClass::_scanCount), (wifi_ap_record_t*)_scanResult);
        } else {
            //no memory
            WiFiScanClass::_scanCount = 0;
        }
    }
}