/*
 * Copyright (C) 2010 The Android-X86 Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Yi Sun <beyounn@gmail.com>
 */


package com.maxwell.net.ethernet;


import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;

/**
 * Describes the state of any Ethernet connection that is active or
 * is in the process of being set up.
 */

public class EthernetDevInfo implements Parcelable {
    /**
     * The ethernet interface is configured by dhcp
     */
    public static final String ETHERNET_CONN_MODE_DHCP= "dhcp";
    /**
     * The ethernet interface is configured manually
     */
    public static final String ETHERNET_CONN_MODE_MANUAL = "manual";

    private String dev_name;
    private String mac_addr;	
    private String ipaddr;
    private String netmask;
    private String route;
    private String dns1;
    private String dns2;
    private String mode;
    private String nettype;
    private int prefixlength;
    
    /* Vendor specific information (from RFC 2132)*/
    private String vendorInfo;
    private String provisioning_url;
    private String tftpserver_url;
    private String sipserver_url;

    private static final String INTERFACE = "eth0";
    private static final String ETHERNET_IPV4 = "ipv4";
    private static final String ETHERNET_IPV6 = "ipv6";

    public EthernetDevInfo () {
        dev_name = null;
	mac_addr = EthernetNative.getInterfaceMacAddress();
        ipaddr = null;
        dns1 = null;
        dns2 = null;
        route = null;
        netmask = null;
        mode = ETHERNET_CONN_MODE_DHCP;
        nettype = null;
        prefixlength = -1;
        vendorInfo = null;
        provisioning_url = null;
        tftpserver_url = null;
        sipserver_url = null;
    }

    /**
     * save interface name into the configuration
     */
    public void setIfName(String ifname) {
        this.dev_name = INTERFACE;
    }

    /**
     * Returns the interface name from the saved configuration
     * @return interface name
     */
    public String getIfName() {
        return INTERFACE;
    }

    public void setIpAddress(String ip) {
        this.ipaddr = ip;
    }

    public String getIpAddress( ) {
        return this.ipaddr;
    }

    public void setMacAddress(String mac_addr) {
        this.mac_addr = mac_addr;
    }

    public String getMacAddress( ) {
        return this.mac_addr;
    }	

    public void setNetMask(String ip) {
        this.netmask = ip;
    }

    public String getNetMask( ) {
        return this.netmask;
    }

    public void setRouteAddr(String route) {
        this.route = route;
    }

    public String getRouteAddr() {
        return this.route;
    }

    public void setDnsAddr(String dns1) {
        this.dns1 = dns1;
    }

    public String getDnsAddr( ) {
        return this.dns1;
    }

    public void setDns2Addr(String dns2) {
        this.dns2 = dns2;
    }

    public String getDns2Addr( ) {
        return this.dns2;
    }

    public void setNetType(String nettype) {
        this.nettype = nettype;
    }

    public String getNetType( ) {
        return this.nettype;
    }

    public void setPrefixLength(int prefixlength) {
        this.prefixlength = prefixlength;
    }

    public int getPrefixLength( ) {
        return this.prefixlength;
    }

    /* Vendor specific information (from RFC 2132)*/
    public void setVendorInfo(String vendorInfo) {
        this.vendorInfo = vendorInfo;
    }

    public String getVendorInfo( ) {
        return this.vendorInfo;
    }

    public void setProvServerUrl(String provisioning_url) {
        this.provisioning_url = provisioning_url;
    }

    public String getProvServerUrl( ) {
        return this.provisioning_url;
    }

    public void setTftpServerUrl(String tftpserver_url) {
        this.tftpserver_url = tftpserver_url;
    }

    public String getTftpServerUrl( ) {
        return this.tftpserver_url;
    }

    public void setSipServerUrl(String sipserver_url) {
        this.sipserver_url = sipserver_url;
    }

    public String getSipServerUrl( ) {
        return this.sipserver_url;
    }

    /**
     * Set ethernet configuration mode
     * @param mode {@code ETHERNET_CONN_MODE_DHCP} for dhcp {@code ETHERNET_CONN_MODE_MANUAL} for manual configure
     * @return
     */
    public boolean setConnectMode(String mode) {
        if (mode.equals(ETHERNET_CONN_MODE_DHCP) || mode.equals(ETHERNET_CONN_MODE_MANUAL)) {
            this.mode = mode;
            return true;
        }
        return false;
    }

    public String getConnectMode() {
        return this.mode;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.dev_name);
        dest.writeString(this.ipaddr);
        dest.writeString(this.netmask);
        dest.writeString(this.route);
        dest.writeString(this.dns1);
        dest.writeString(this.dns2);
        dest.writeString(this.mode);
        dest.writeString(this.nettype);
        dest.writeInt(this.prefixlength);
        dest.writeString(this.vendorInfo);
        dest.writeString(this.provisioning_url);
        dest.writeString(this.tftpserver_url);
        dest.writeString(this.sipserver_url);
    }

    /** Implement the Parcelable interface {@hide} */
    public static final Creator<EthernetDevInfo> CREATOR = new Creator<EthernetDevInfo>() {
        public EthernetDevInfo createFromParcel(Parcel in) {
            EthernetDevInfo info = new EthernetDevInfo();
            info.setIfName(in.readString());
            info.setIpAddress(in.readString());
            info.setNetMask(in.readString());
            info.setRouteAddr(in.readString());
            info.setDnsAddr(in.readString());
            info.setDns2Addr(in.readString());
            info.setConnectMode(in.readString());
            info.setNetType(in.readString());
            info.setPrefixLength(in.readInt());
            info.setVendorInfo(in.readString());
            info.setProvServerUrl(in.readString());
            info.setTftpServerUrl(in.readString());
            info.setSipServerUrl(in.readString());
            return info;
        }

        public EthernetDevInfo[] newArray(int size) {
            return new EthernetDevInfo[size];
        }
    };
}
