use of android.net.InterfaceConfiguration in project XobotOS by xamarin.
the class WifiStateMachine method startTethering.
private boolean startTethering(ArrayList<String> available) {
boolean wifiAvailable = false;
checkAndSetConnectivityInstance();
String[] wifiRegexs = mCm.getTetherableWifiRegexs();
for (String intf : available) {
for (String regex : wifiRegexs) {
if (intf.matches(regex)) {
InterfaceConfiguration ifcg = null;
try {
ifcg = mNwService.getInterfaceConfig(intf);
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.addr = new LinkAddress(NetworkUtils.numericToInetAddress("192.168.43.1"), 24);
ifcg.interfaceFlags = "[up]";
mNwService.setInterfaceConfig(intf, ifcg);
}
} catch (Exception e) {
loge("Error configuring interface " + intf + ", :" + e);
return false;
}
if (mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
loge("Error tethering on " + intf);
return false;
}
return true;
}
}
}
// We found no interfaces to tether
return false;
}
use of android.net.InterfaceConfiguration in project XobotOS by xamarin.
the class BluetoothPanProfileHandler method enableTethering.
// configured when we start tethering
private String enableTethering(String iface) {
debugLog("updateTetherState:" + iface);
IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
// bring toggle the interfaces
String[] currentIfaces = new String[0];
try {
currentIfaces = service.listInterfaces();
} catch (Exception e) {
Log.e(TAG, "Error listing Interfaces :" + e);
return null;
}
boolean found = false;
for (String currIface : currentIfaces) {
if (currIface.equals(iface)) {
found = true;
break;
}
}
if (!found)
return null;
String address = createNewTetheringAddressLocked();
if (address == null)
return null;
InterfaceConfiguration ifcg = null;
try {
ifcg = service.getInterfaceConfig(iface);
if (ifcg != null) {
InetAddress addr = null;
if (ifcg.addr == null || (addr = ifcg.addr.getAddress()) == null || addr.equals(NetworkUtils.numericToInetAddress("0.0.0.0")) || addr.equals(NetworkUtils.numericToInetAddress("::0"))) {
addr = NetworkUtils.numericToInetAddress(address);
}
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
ifcg.addr = new LinkAddress(addr, BLUETOOTH_PREFIX_LENGTH);
ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
ifcg.interfaceFlags = ifcg.interfaceFlags.replace(" ", " ");
service.setInterfaceConfig(iface, ifcg);
if (cm.tether(iface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
Log.e(TAG, "Error tethering " + iface);
}
}
} catch (Exception e) {
Log.e(TAG, "Error configuring interface " + iface + ", :" + e);
return null;
}
return address;
}
use of android.net.InterfaceConfiguration in project android_frameworks_base by AOSPA.
the class NetworkManagementService method getInterfaceConfig.
@Override
public InterfaceConfiguration getInterfaceConfig(String iface) {
mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
final NativeDaemonEvent event;
try {
event = mConnector.execute("interface", "getcfg", iface);
} catch (NativeDaemonConnectorException e) {
throw e.rethrowAsParcelableException();
}
event.checkCode(InterfaceGetCfgResult);
// Rsp: 213 xx:xx:xx:xx:xx:xx yyy.yyy.yyy.yyy zzz flag1 flag2 flag3
final StringTokenizer st = new StringTokenizer(event.getMessage());
InterfaceConfiguration cfg;
try {
cfg = new InterfaceConfiguration();
cfg.setHardwareAddress(st.nextToken(" "));
InetAddress addr = null;
int prefixLength = 0;
try {
addr = NetworkUtils.numericToInetAddress(st.nextToken());
} catch (IllegalArgumentException iae) {
Slog.e(TAG, "Failed to parse ipaddr", iae);
}
try {
prefixLength = Integer.parseInt(st.nextToken());
} catch (NumberFormatException nfe) {
Slog.e(TAG, "Failed to parse prefixLength", nfe);
}
cfg.setLinkAddress(new LinkAddress(addr, prefixLength));
while (st.hasMoreTokens()) {
cfg.setFlag(st.nextToken());
}
} catch (NoSuchElementException nsee) {
throw new IllegalStateException("Invalid response from daemon: " + event);
}
return cfg;
}
use of android.net.InterfaceConfiguration in project android_frameworks_base by AOSPA.
the class CommonTimeManagementService method reevaluateServiceState.
private void reevaluateServiceState() {
String bindIface = null;
byte bestScore = -1;
try {
// Check to see if this interface is suitable to use for time synchronization.
//
// TODO : This selection algorithm needs to be enhanced for use with mobile devices. In
// particular, the choice of whether to a wireless interface or not should not be an all
// or nothing thing controlled by properties. It would probably be better if the
// platform had some concept of public wireless networks vs. home or friendly wireless
// networks (something a user would configure in settings or when a new interface is
// added). Then this algorithm could pick only wireless interfaces which were flagged
// as friendly, and be dormant when on public wireless networks.
//
// Another issue which needs to be dealt with is the use of driver supplied interface
// name to determine the network type. The fact that the wireless interface on a device
// is named "wlan0" is just a matter of convention; its not a 100% rule. For example,
// there are devices out there where the wireless is name "tiwlan0", not "wlan0". The
// internal network management interfaces in Android have all of the information needed
// to make a proper classification, there is just no way (currently) to fetch an
// interface's type (available from the ConnectionManager) as well as its address
// (available from either the java.net interfaces or from the NetworkManagment service).
// Both can enumerate interfaces, but that is no way to correlate their results (no
// common shared key; although using the interface name in the connection manager would
// be a good start). Until this gets resolved, we resort to substring searching for
// tags like wlan and eth.
//
String[] ifaceList = mNetMgr.listInterfaces();
if (null != ifaceList) {
for (String iface : ifaceList) {
byte thisScore = -1;
for (InterfaceScoreRule r : IFACE_SCORE_RULES) {
if (iface.contains(r.mPrefix)) {
thisScore = r.mScore;
break;
}
}
if (thisScore <= bestScore)
continue;
InterfaceConfiguration config = mNetMgr.getInterfaceConfig(iface);
if (null == config)
continue;
if (config.isActive()) {
bindIface = iface;
bestScore = thisScore;
}
}
}
} catch (RemoteException e) {
// Bad news; we should not be getting remote exceptions from the connectivity manager
// since it is running in SystemServer along side of us. It probably does not matter
// what we do here, but go ahead and unbind the common time service in this case, just
// so we have some defined behavior.
bindIface = null;
}
boolean doRebind = true;
synchronized (mLock) {
if ((null != bindIface) && (null == mCurIface)) {
Log.e(TAG, String.format("Binding common time service to %s.", bindIface));
mCurIface = bindIface;
} else if ((null == bindIface) && (null != mCurIface)) {
Log.e(TAG, "Unbinding common time service.");
mCurIface = null;
} else if ((null != bindIface) && (null != mCurIface) && !bindIface.equals(mCurIface)) {
Log.e(TAG, String.format("Switching common time service binding from %s to %s.", mCurIface, bindIface));
mCurIface = bindIface;
} else {
doRebind = false;
}
}
if (doRebind && (null != mCTConfig)) {
byte newPrio = (bestScore > 0) ? (byte) (bestScore * BASE_SERVER_PRIO) : BASE_SERVER_PRIO;
if (newPrio != mEffectivePrio) {
mEffectivePrio = newPrio;
mCTConfig.setMasterElectionPriority(mEffectivePrio);
}
int res = mCTConfig.setNetworkBinding(mCurIface);
if (res != CommonTimeConfig.SUCCESS)
scheduleTimeConfigReconnect();
else if (NO_INTERFACE_TIMEOUT >= 0) {
mNoInterfaceHandler.removeCallbacks(mNoInterfaceRunnable);
if (null == mCurIface)
mNoInterfaceHandler.postDelayed(mNoInterfaceRunnable, NO_INTERFACE_TIMEOUT);
}
}
}
use of android.net.InterfaceConfiguration in project android_frameworks_base by AOSPA.
the class TetherInterfaceStateMachine method configureIfaceIp.
// configured when we start tethering and unconfig'd on error or conclusion
private boolean configureIfaceIp(boolean enabled) {
if (VDBG)
Log.d(TAG, "configureIfaceIp(" + enabled + ")");
String ipAsString = null;
int prefixLen = 0;
if (mInterfaceType == ConnectivityManager.TETHERING_USB) {
ipAsString = USB_NEAR_IFACE_ADDR;
prefixLen = USB_PREFIX_LENGTH;
} else if (mInterfaceType == ConnectivityManager.TETHERING_WIFI) {
ipAsString = WIFI_HOST_IFACE_ADDR;
prefixLen = WIFI_HOST_IFACE_PREFIX_LENGTH;
} else if (mInterfaceType == ConnectivityManager.TETHERING_WIGIG) {
ipAsString = WIGIG_HOST_IFACE_ADDR;
prefixLen = WIGIG_HOST_IFACE_PREFIX_LENGTH;
} else {
// Nothing to do, BT does this elsewhere.
return true;
}
InterfaceConfiguration ifcg = null;
try {
ifcg = mNMService.getInterfaceConfig(mIfaceName);
if (ifcg != null) {
InetAddress addr = NetworkUtils.numericToInetAddress(ipAsString);
ifcg.setLinkAddress(new LinkAddress(addr, prefixLen));
if (enabled) {
ifcg.setInterfaceUp();
} else {
ifcg.setInterfaceDown();
}
ifcg.clearFlag("running");
mNMService.setInterfaceConfig(mIfaceName, ifcg);
}
} catch (Exception e) {
Log.e(TAG, "Error configuring interface " + mIfaceName, e);
return false;
}
return true;
}
Aggregations