blob: c64e70516abc6f9a67f2e6712faaf98e6f002a7a [file] [log] [blame]
/*
* Copyright (C) 2017 The Android 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.
*/
package com.android.server.connectivity.tethering;
import static android.provider.Settings.Global.TETHER_OFFLOAD_DISABLED;
import android.content.ContentResolver;
import android.net.LinkProperties;
import android.net.util.SharedLog;
import android.os.Handler;
import android.provider.Settings;
/**
* A class to encapsulate the business logic of programming the tethering
* hardware offload interface.
*
* @hide
*/
public class OffloadController {
private static final String TAG = OffloadController.class.getSimpleName();
private final Handler mHandler;
private final OffloadHardwareInterface mHwInterface;
private final ContentResolver mContentResolver;
private final SharedLog mLog;
private boolean mConfigInitialized;
private boolean mControlInitialized;
private LinkProperties mUpstreamLinkProperties;
public OffloadController(Handler h, OffloadHardwareInterface hwi,
ContentResolver contentResolver, SharedLog log) {
mHandler = h;
mHwInterface = hwi;
mContentResolver = contentResolver;
mLog = log.forSubComponent(TAG);
}
public void start() {
if (isOffloadDisabled() || started()) return;
if (!mConfigInitialized) {
mConfigInitialized = mHwInterface.initOffloadConfig();
if (!mConfigInitialized) {
mLog.i("tethering offload config not supported");
stop();
return;
}
}
mControlInitialized = mHwInterface.initOffloadControl(
new OffloadHardwareInterface.ControlCallback() {
@Override
public void onOffloadEvent(int event) {
mLog.log("got offload event: " + event);
}
@Override
public void onNatTimeoutUpdate(int proto,
String srcAddr, int srcPort,
String dstAddr, int dstPort) {
mLog.log(String.format("NAT timeout update: %s (%s,%s) -> (%s,%s)",
proto, srcAddr, srcPort, dstAddr, dstPort));
}
});
if (!mControlInitialized) {
mLog.i("tethering offload control not supported");
stop();
}
}
public void stop() {
mUpstreamLinkProperties = null;
mHwInterface.stopOffloadControl();
mControlInitialized = false;
mConfigInitialized = false;
}
public void setUpstreamLinkProperties(LinkProperties lp) {
if (!started()) return;
// TODO: setUpstreamParameters().
mUpstreamLinkProperties = lp;
}
// TODO: public void addDownStream(...)
private boolean isOffloadDisabled() {
// Defaults to |false| if not present.
return (Settings.Global.getInt(mContentResolver, TETHER_OFFLOAD_DISABLED, 0) != 0);
}
private boolean started() {
return mConfigInitialized && mControlInitialized;
}
}