1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.ethernet;
18
19import android.net.IpConfiguration;
20import android.net.IpConfiguration.IpAssignment;
21import android.net.IpConfiguration.ProxySettings;
22import android.os.Environment;
23import android.util.Log;
24import android.util.SparseArray;
25
26import com.android.server.net.IpConfigStore;
27
28
29/**
30 * This class provides an API to store and manage Ethernet network configuration.
31 */
32public class EthernetConfigStore extends IpConfigStore {
33    private static final String TAG = "EthernetConfigStore";
34
35    private static final String ipConfigFile = Environment.getDataDirectory() +
36            "/misc/ethernet/ipconfig.txt";
37
38    public EthernetConfigStore() {
39    }
40
41    public IpConfiguration readIpAndProxyConfigurations() {
42        SparseArray<IpConfiguration> networks = readIpAndProxyConfigurations(ipConfigFile);
43
44        if (networks.size() == 0) {
45            Log.w(TAG, "No Ethernet configuration found. Using default.");
46            return new IpConfiguration(IpAssignment.DHCP, ProxySettings.NONE, null, null);
47        }
48
49        if (networks.size() > 1) {
50            // Currently we only support a single Ethernet interface.
51            Log.w(TAG, "Multiple Ethernet configurations detected. Only reading first one.");
52        }
53
54        return networks.valueAt(0);
55    }
56
57    public void writeIpAndProxyConfigurations(IpConfiguration config) {
58        SparseArray<IpConfiguration> networks = new SparseArray<IpConfiguration>();
59        networks.put(0, config);
60        writeIpAndProxyConfigurations(ipConfigFile, networks);
61    }
62}
63