<commit_before>/**
 * Copyright (c) 2011, 2014 Eurotech and/or its affiliates
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Eurotech
 */
package org.eclipse.kura.net.admin.visitor.linux;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.core.net.NetworkConfiguration;
import org.eclipse.kura.core.net.NetworkConfigurationVisitor;
import org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl;
import org.eclipse.kura.core.util.ProcessUtil;
import org.eclipse.kura.linux.net.util.KuraConstants;
import org.eclipse.kura.net.NetConfig;
import org.eclipse.kura.net.NetConfigIP4;
import org.eclipse.kura.net.NetInterfaceAddressConfig;
import org.eclipse.kura.net.NetInterfaceConfig;
import org.eclipse.kura.net.NetInterfaceStatus;
import org.eclipse.kura.net.NetInterfaceType;
import org.eclipse.kura.net.admin.visitor.linux.util.KuranetConfig;
import org.eclipse.kura.net.admin.visitor.linux.util.WpaSupplicantUtil;
import org.eclipse.kura.net.wifi.WifiCiphers;
import org.eclipse.kura.net.wifi.WifiConfig;
import org.eclipse.kura.net.wifi.WifiMode;
import org.eclipse.kura.net.wifi.WifiSecurity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WpaSupplicantConfigWriter implements NetworkConfigurationVisitor {
	private static final Logger s_logger = LoggerFactory.getLogger(WpaSupplicantConfigWriter.class);
	
	private static final String WPA_CONFIG_FILE = "/etc/wpa_supplicant.conf";
	private static final String WPA_TMP_CONFIG_FILE = "/etc/wpa_supplicant.conf.tmp";
	private static final String TMP_WPA_CONFIG_FILE = "/tmp/wpa_supplicant.conf";
	
	private static final String OS_VERSION = System.getProperty("kura.os.version");
	
	private static final String HEXES = "0123456789ABCDEF";
	
	private static WpaSupplicantConfigWriter s_instance;
	
	public static WpaSupplicantConfigWriter getInstance() {
		if (s_instance == null) {
			s_instance = new WpaSupplicantConfigWriter();
		}
		
		return s_instance;
	}

	@Override
	public void visit(NetworkConfiguration config) throws KuraException {
		List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = config.getModifiedNetInterfaceConfigs();
        
        for(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
            if(netInterfaceConfig.getType() == NetInterfaceType.WIFI) {
                // ignore 'mon' interface
                if(netInterfaceConfig.getName().startsWith("mon.")) {
                    continue;
                }
                
                writeConfig(netInterfaceConfig);
            }
        }

	}
	
	public void generateTempWpaSupplicantConf(WifiConfig wifiConfig, String interfaceName) throws KuraException {

		try {
			this.generateWpaSupplicantConf(wifiConfig, interfaceName, TMP_WPA_CONFIG_FILE);
		} catch (Exception e) {
			throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
		}
	}
	
	private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException{
		String interfaceName = netInterfaceConfig.getName();
        s_logger.debug("Writing wpa_supplicant config for " + interfaceName);
        
        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        
        if(netInterfaceAddressConfigs.size() > 0) {
        
	        for(NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
	        	if(netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
	        		List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
	        		NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
	        		WifiMode wifiMode = ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).getMode();
	        		WifiConfig infraConfig = null;
	        		WifiConfig adhocConfig = null;
	        		WifiConfig wpaSupplicantConfig = null;
	        		
	        		// Get the wifi configs
	        		if(netConfigs != null) {
	        			for(NetConfig netConfig : netConfigs){
				        	if(netConfig instanceof WifiConfig) {
				        	    if(((WifiConfig) netConfig).getMode() == WifiMode.ADHOC) {
				        	        adhocConfig = (WifiConfig) netConfig;
				        	    } else if (((WifiConfig) netConfig).getMode() == WifiMode.INFRA) {<commit_msg>added the generateTempWpaSupplicantConf() method that cteates an 'empty' wpasupplicant.conf file in the /tmp directory that is being used to start supplicant for AP scanning<commit_after>/**
 * Copyright (c) 2011, 2014 Eurotech and/or its affiliates
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Eurotech
 */
package org.eclipse.kura.net.admin.visitor.linux;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.core.net.NetworkConfiguration;
import org.eclipse.kura.core.net.NetworkConfigurationVisitor;
import org.eclipse.kura.core.net.WifiInterfaceAddressConfigImpl;
import org.eclipse.kura.core.util.ProcessUtil;
import org.eclipse.kura.linux.net.util.KuraConstants;
import org.eclipse.kura.net.NetConfig;
import org.eclipse.kura.net.NetConfigIP4;
import org.eclipse.kura.net.NetInterfaceAddressConfig;
import org.eclipse.kura.net.NetInterfaceConfig;
import org.eclipse.kura.net.NetInterfaceStatus;
import org.eclipse.kura.net.NetInterfaceType;
import org.eclipse.kura.net.admin.visitor.linux.util.KuranetConfig;
import org.eclipse.kura.net.admin.visitor.linux.util.WpaSupplicantUtil;
import org.eclipse.kura.net.wifi.WifiCiphers;
import org.eclipse.kura.net.wifi.WifiConfig;
import org.eclipse.kura.net.wifi.WifiMode;
import org.eclipse.kura.net.wifi.WifiSecurity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WpaSupplicantConfigWriter implements NetworkConfigurationVisitor {
	private static final Logger s_logger = LoggerFactory.getLogger(WpaSupplicantConfigWriter.class);
	
	private static final String WPA_CONFIG_FILE = "/etc/wpa_supplicant.conf";
	private static final String WPA_TMP_CONFIG_FILE = "/etc/wpa_supplicant.conf.tmp";
	private static final String TMP_WPA_CONFIG_FILE = "/tmp/wpa_supplicant.conf";
	
	private static final String OS_VERSION = System.getProperty("kura.os.version");
	
	private static final String HEXES = "0123456789ABCDEF";
	
	private static WpaSupplicantConfigWriter s_instance;
	
	public static WpaSupplicantConfigWriter getInstance() {
		if (s_instance == null) {
			s_instance = new WpaSupplicantConfigWriter();
		}
		
		return s_instance;
	}

	@Override
	public void visit(NetworkConfiguration config) throws KuraException {
		List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = config.getModifiedNetInterfaceConfigs();
        
        for(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
            if(netInterfaceConfig.getType() == NetInterfaceType.WIFI) {
                // ignore 'mon' interface
                if(netInterfaceConfig.getName().startsWith("mon.")) {
                    continue;
                }
                
                writeConfig(netInterfaceConfig);
            }
        }

	}
	
	public void generateTempWpaSupplicantConf(WifiConfig wifiConfig, String interfaceName) throws KuraException {

		try {
			this.generateWpaSupplicantConf(wifiConfig, interfaceName, TMP_WPA_CONFIG_FILE);
		} catch (Exception e) {
			throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
		}
	}
	
	public void generateTempWpaSupplicantConf() throws KuraException {
		
		InputStream is = getClass().getResourceAsStream("/src/main/resources/wifi/wpasupplicant.conf");;
		try {
			String fileAsString = readInputStreamAsString(is);
			File outputFile = new File(TMP_WPA_CONFIG_FILE);
			copyFile(fileAsString, outputFile);
		} catch (Exception e) {
			throw KuraException.internalError("Failed to generate wpa_supplicant.conf");
		}
	}
	
	private void writeConfig(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException{
		String interfaceName = netInterfaceConfig.getName();
        s_logger.debug("Writing wpa_supplicant config for " + interfaceName);
        
        List<? extends NetInterfaceAddressConfig> netInterfaceAddressConfigs = netInterfaceConfig.getNetInterfaceAddresses();
        
        if(netInterfaceAddressConfigs.size() > 0) {
        
	        for(NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceAddressConfigs) {
	        	if(netInterfaceAddressConfig instanceof WifiInterfaceAddressConfigImpl) {
	        		List<NetConfig> netConfigs = netInterfaceAddressConfig.getConfigs();
	        		NetInterfaceStatus netInterfaceStatus = NetInterfaceStatus.netIPv4StatusDisabled;
	        		WifiMode wifiMode = ((WifiInterfaceAddressConfigImpl) netInterfaceAddressConfig).getMode();
	        		WifiConfig infraConfig = null;
	        		WifiConfig adhocConfig = null;
	        		WifiConfig wpaSupplicantConfig = null;
	        		
	        		// Get the wifi configs
	        		if(netConfigs != null) {
	        			for(NetConfig netConfig : netConfigs){
				        	if(netConfig instanceof WifiConfig) {
				        	    if(((WifiConfig) netConfig).getMode() == WifiMode.ADHOC) {
				        	        adhocConfig = (WifiConfig) netConfig;
				        	    } else if (((WifiConfig) netConfig).getMode() == WifiMode.INFRA) {
{'fraction_non_alphanumeric': 0.08743535721126221, 'fraction_numerical': 0.005171423099023176, 'mean_word_length': 4.013442150744119, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5716821', 'n_tokens_mistral': 3213, 'n_tokens_neox': 3015, 'n_words': 599}