/*
* 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 android.net.ethernet;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothHeadset;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkStateTracker;
import android.net.NetworkUtils;
import android.net.NetworkInfo.DetailedState;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;
import android.os.SystemProperties;
import android.util.*;
/**
* Track the state of Ethernet connectivity. All event handling is done here,
* and all changes in connectivity state are initiated here.
*
* @hide
*/
public class EthernetStateTracker extends NetworkStateTracker {
private static final String TAG = "EthernetStateTracker";
public static final int EVENT_DHCP_START = 0;
public static final int EVENT_INTERFACE_CONFIGURATION_SUCCEEDED = 1;
public static final int EVENT_INTERFACE_CONFIGURATION_FAILED = 2;
public static final int EVENT_HW_CONNECTED = 3;
public static final int EVENT_HW_DISCONNECTED = 4;
public static final int EVENT_HW_PHYCONNECTED = 5;
private static final int NOTIFY_ID = 6;
private static final boolean localLOGV = true;
private EthernetManager mEM;
private boolean mServiceStarted;
private boolean mStackConnected;
private boolean mHWConnected;
private boolean mInterfaceStopped;
private DhcpHandler mDhcpTarget;
private String mInterfaceName ;
private DhcpInfo mDhcpInfo;
private EthernetMonitor mMonitor;
private String[] sDnsPropNames;
private boolean mStartingDhcp;
private NotificationManager mNotificationManager;
private Notification mNotification;
private Handler mTrackerTarget;
public EthernetStateTracker(Context context, Handler target) {
super(context, target, ConnectivityManager.TYPE_ETHERNET, 0, "ETH", "");
if (localLOGV) Slog.v(TAG, "Starts...");
if (EthernetNative.initEthernetNative() != 0) {
Slog.e(TAG,"Can not init ethernet device layers");
return;
}
if (localLOGV) Slog.v(TAG,"Successed");
mServiceStarted = true;
HandlerThread dhcpThread = new HandlerThread("DHCP Handler Thread");
dhcpThread.start();
mDhcpTarget = new DhcpHandler(dhcpThread.getLooper(), this);
mMonitor = new EthernetMonitor(this);
mDhcpInfo = new DhcpInfo();
}
/**
* Stop etherent interface
* @param suspend {@code false} disable the interface {@code true} only reset the connection without disable the interface
* @return true
*/
public boolean stopInterface(boolean suspend) {
if (mEM != null) {
EthernetDevInfo info = mEM.getSavedConfig();
if (info != null && mEM.isConfigured()) {
synchronized (mDhcpTarget) {
if(mInterfaceStopped)
return true;
mInterfaceStopped = true;
if (localLOGV) Slog.i(TAG, "stop dhcp and interface++++++++++20120406");
mDhcpTarget.removeMessages(EVENT_DHCP_START);
String ifname = info.getIfName();
if (!NetworkUtils.stopDhcp(ifname)) {
if (localLOGV) Slog.w(TAG, "Could not stop DHCP");
}
NetworkUtils.resetConnections(ifname);
//modify by yegr
//if we disable ethernet,we will notify unable to see the ethernet image
//postNotification(EVENT_HW_DISCONNECTED);
setDetailedState(DetailedState.DISCONNECTED);
//this will cause dhcp restart fail if we process enable and disable ethernet,but delete this code,cause other questions??
//modify by yegr
//if (!suspend)
//NetworkUtils.disableInterface(ifname);
}
}
}
return true;
}
private boolean configureInterface(EthernetDevInfo info) throws UnknownHostException {
//mStackConnected = false;
//mHWConnected = false;
mInterfaceStopped = false;
mStartingDhcp = false;
if (info.getConnectMode().equals(EthernetDevInfo.ETHERNET_CONN_MODE_DHCP)) {
if (localLOGV) Slog.i(TAG, "trigger dhcp for device " + info.getIfName());
sDnsPropNames = new String[] {
"dhcp." + mInterfaceName + ".dns1",
"dhcp." + mInterfaceName + ".dns2"
};
mStartingDhcp = true;
mDhcpTarget.sendEmptyMessage(EVENT_DHCP_START);
} else {
int event;
sDnsPropNames = new String[] {
"net." + mInterfaceName + ".dns1",
"net." + mInterfaceName + ".dns2"
};
mDhcpInfo.ipAddress = lookupHost(info.getIpAddress());
mDhcpInfo.gateway = lookupHost(info.getRouteAddr());
mDhcpInfo.netmask = lookupHost(info.getNetMask());
mDhcpInfo.dns1 = lookupHost(info.getDnsAddr());
mDhcpInfo.dns2 = 0;
if (localLOGV) Slog.i(TAG, "set ip manually " + mDhcpInfo.toString());
NetworkUtils.removeDefaultRoute(info.getIfName());
if (NetworkUtils.configureInterface(info.getIfName(), mDhcpInfo)) {
event = EVENT_INTERFACE_CONFIGURATION_SUCCEEDED;
SystemProperties.set("net.dns1", info.getDnsAddr());
SystemProperties.set("net." + info.getIfName() + ".dns1", info.getDnsAddr());
SystemProperties.set("net." + info.getIfName() + ".dns2", "0.0.0.0");
if (localLOGV) Slog.v(TAG, "Static IP configuration succeeded-20120313");
} else {
event = EVENT_INTERFACE_CONFIGURATION_FAILED;
if (localLOGV) Slog.w(TAG, "Static IP configuration failed-20120313");
}
this.sendEmptyMessage(event);
}
return true;
}
/**
* reset ethernet interface
* @return true
* @throws UnknownHostException
*/
public boolean resetInterface() throws UnknownHostException{
/*
* This will guide us to enabled the enabled device
*/
if (mEM != null) {
EthernetDevInfo info = mEM.getSavedConfig();
if (info != null && mEM.isConfigured()) {
synchronized (this) {
mInterfaceName = info.getIfName();
if (localLOGV) Slog.i(TAG, "reset device " + mInterfaceName);
NetworkUtils.resetConnections(mInterfaceName);
// Stop DHCP
if (mDhcpTarget != null) {
mDhcpTarget.removeMessages(EVENT_DHCP_START);
}
if (!NetworkUtils.stopDhcp(mInterfaceName)) {
没有合适的资源?快使用搜索试试~ 我知道了~
android以太网框架层代码
共18个文件
svn-base:8个
java:6个
aidl:2个
5星 · 超过95%的资源 需积分: 32 167 下载量 200 浏览量
2013-03-25
09:00:25
上传
评论 6
收藏 27KB RAR 举报
温馨提示
android以太网框架层代码直接放在framework base下主要供参考如何实现
资源推荐
资源详情
资源评论
收起资源包目录
android以太网框架层代码直接放在framework base下主要供参考如何实现.rar (18个子文件)
ethernet
EthernetNative.java 1004B
EthernetManager.java 5KB
EthernetService.java 8KB
EthernetMonitor.java 4KB
EthernetDevInfo.java 4KB
EthernetDevInfo.aidl 685B
.svn
all-wcprops 2KB
prop-base
props
tmp
prop-base
props
text-base
text-base
EthernetNative.java.svn-base 1004B
EthernetDevInfo.aidl.svn-base 685B
IEthernetManager.aidl.svn-base 1KB
EthernetDevInfo.java.svn-base 4KB
EthernetManager.java.svn-base 5KB
EthernetService.java.svn-base 8KB
EthernetStateTracker.java.svn-base 17KB
EthernetMonitor.java.svn-base 4KB
entries 1KB
EthernetStateTracker.java 17KB
IEthernetManager.aidl 1KB
共 18 条
- 1
joqian
- 粉丝: 77
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页