<commit_before>// The author disclaims copyright to this source code.
#include "network.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "mdns.h"
#include "network_secret.h"

static const char* TAG = "network";

static EventGroupHandle_t network_event_group;
static mdns_server_t* network_mdns;

static void network_log_ip(tcpip_adapter_if_t tcp_if) {
	tcpip_adapter_ip_info_t ip_info;
	ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(tcp_if, &ip_info));
	ESP_LOGI(TAG, "ip: %s", ip4addr_ntoa(&ip_info.ip));
	ESP_LOGI(TAG, "netmask: %s", ip4addr_ntoa(&ip_info.netmask));
	ESP_LOGI(TAG, "gateway: %s", ip4addr_ntoa(&ip_info.gw));
}

static esp_err_t event_handler(void *ctx, system_event_t *event) {
	switch (event->event_id) {
	case SYSTEM_EVENT_STA_START:
		esp_wifi_connect();
		break;
	case SYSTEM_EVENT_STA_GOT_IP:
		network_log_ip(TCPIP_ADAPTER_IF_STA);
		xEventGroupSetBits(network_event_group, BIT0);
		break;
	case SYSTEM_EVENT_STA_LOST_IP:
		xEventGroupClearBits(network_event_group, BIT0);
		break;
	case SYSTEM_EVENT_AP_START:
		xEventGroupSetBits(network_event_group, BIT0);
		network_log_ip(TCPIP_ADAPTER_IF_AP);
		break;
	case SYSTEM_EVENT_AP_STOP:
		xEventGroupClearBits(network_event_group, BIT0);
		break;
	case SYSTEM_EVENT_ETH_GOT_IP:
		network_log_ip(TCPIP_ADAPTER_IF_ETH);
		break;
	default:
		// see 'event' debug statements
		break;
	}

	return ESP_OK;
}

static bool sta_begin() {
	ESP_LOGD(TAG, ">sta_begin")

	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
	wifi_config_t wifi_config = //
			{ //
			.sta = //
					{ //
					.ssid = NETWORK_STA_SSID, //
							.password = <PASSWORD>, //
							.bssid_set = false, //
							.bssid[0] = 0, //
							.bssid[1] = 0, //
							.bssid[2] = 0, //
							.bssid[3] = 0, //
							.bssid[4] = 0, //
							.bssid[5] = 0, //
							.channel = 6, //
					}, //
			};

	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
	ESP_ERROR_CHECK(esp_wifi_start());

	EventBits_t status = xEventGroupWaitBits(network_event_group, BIT0, pdFALSE, pdTRUE,
			(CONFIG_STA_SEARCH_SECONDS * 1000) / portTICK_PERIOD_MS);
	bool found = (status & BIT0);

	ESP_LOGD(TAG, "<sta_begin")
	return found;
}

static void sta_end() {
	ESP_LOGD(TAG, ">sta_end")

	ESP_ERROR_CHECK(esp_wifi_stop());
	tcpip_adapter_stop(TCPIP_ADAPTER_IF_STA);

	ESP_LOGD(TAG, "<sta_end")
}

static void ap_begin() {<commit_msg>remove WiFi channel from station configuration<commit_after>// The author disclaims copyright to this source code.
#include "network.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "mdns.h"
#include "network_secret.h"

static const char* TAG = "network";

static EventGroupHandle_t network_event_group;
static mdns_server_t* network_mdns;

static void network_log_ip(tcpip_adapter_if_t tcp_if) {
	tcpip_adapter_ip_info_t ip_info;
	ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(tcp_if, &ip_info));
	ESP_LOGI(TAG, "ip: %s", ip4addr_ntoa(&ip_info.ip));
	ESP_LOGI(TAG, "netmask: %s", ip4addr_ntoa(&ip_info.netmask));
	ESP_LOGI(TAG, "gateway: %s", ip4addr_ntoa(&ip_info.gw));
}

static esp_err_t event_handler(void *ctx, system_event_t *event) {
	switch (event->event_id) {
	case SYSTEM_EVENT_STA_START:
		esp_wifi_connect();
		break;
	case SYSTEM_EVENT_STA_GOT_IP:
		network_log_ip(TCPIP_ADAPTER_IF_STA);
		xEventGroupSetBits(network_event_group, BIT0);
		break;
	case SYSTEM_EVENT_STA_LOST_IP:
		xEventGroupClearBits(network_event_group, BIT0);
		break;
	case SYSTEM_EVENT_AP_START:
		xEventGroupSetBits(network_event_group, BIT0);
		network_log_ip(TCPIP_ADAPTER_IF_AP);
		break;
	case SYSTEM_EVENT_AP_STOP:
		xEventGroupClearBits(network_event_group, BIT0);
		break;
	case SYSTEM_EVENT_ETH_GOT_IP:
		network_log_ip(TCPIP_ADAPTER_IF_ETH);
		break;
	default:
		// see 'event' debug statements
		break;
	}

	return ESP_OK;
}

static bool sta_begin() {
	ESP_LOGD(TAG, ">sta_begin")

	ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
	wifi_config_t wifi_config = //
			{ //
			.sta = //
					{ //
					.ssid = NETWORK_STA_SSID, //
							.password = <PASSWORD>, //
							.bssid_set = false, //
					}, //
			};

	ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
	ESP_ERROR_CHECK(esp_wifi_start());

	EventBits_t status = xEventGroupWaitBits(network_event_group, BIT0, pdFALSE, pdTRUE,
			(CONFIG_STA_SEARCH_SECONDS * 1000) / portTICK_PERIOD_MS);
	bool found = (status & BIT0);

	ESP_LOGD(TAG, "<sta_begin")
	return found;
}

static void sta_end() {
	ESP_LOGD(TAG, ">sta_end")

	ESP_ERROR_CHECK(esp_wifi_stop());
	tcpip_adapter_stop(TCPIP_ADAPTER_IF_STA);

	ESP_LOGD(TAG, "<sta_end")
}

static void ap_begin() {
{'fraction_non_alphanumeric': 0.12628398791540785, 'fraction_numerical': 0.00785498489425982, 'mean_word_length': 5.161290322580645, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2795143', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2045, 'n_words': 333}