android Q softAp流程

App调用接口
public static final int TETHERING_WIFI      = 0;
mConnectivityManager.startTethering(TETHERING_WIFI, true /* showProvisioningUi */,
        mOnStartTetheringCallback, new Handler(Looper.getMainLooper()));
ConnectivityManager.java 客户端提供接口
try {
      String pkgName = mContext.getOpPackageName();
      Log.i(TAG, "startTethering caller:" + pkgName);
      mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
  } catch (RemoteException e) {
      Log.e(TAG, "Exception trying to start tethering.", e);
      wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
  }
ConnectivityService.java 服务端执行
@Override
   public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi,
           String callerPkg) {
       ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg);
       if (!isTetheringSupported()) {
           receiver.send(ConnectivityManager.TETHER_ERROR_UNSUPPORTED, null);
           return;
       }
       mTethering.startTethering(type, receiver, showProvisioningUi);
   }
Tethering.java
public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi) {
	mEntitlementMgr.startProvisioningIfNeeded(type, showProvisioningUi);
	enableTetheringInternal(type, true /* enabled */, receiver);
}
private void enableTetheringInternal(int type, boolean enable, ResultReceiver receiver) {
        int result;
        switch (type) {
            case TETHERING_WIFI:
                result = setWifiTethering(enable);
                sendTetherResult(receiver, result);
                break;
            case TETHERING_USB:
                result = setUsbTethering(enable);
                sendTetherResult(receiver, result);
                break;
            case TETHERING_BLUETOOTH:
                setBluetoothTethering(enable, receiver);
                break;
            default:
                Log.w(TAG, "Invalid tether type.");
                sendTetherResult(receiver, TETHER_ERROR_UNKNOWN_IFACE);
        }
    }
private int setWifiTethering(final boolean enable) {
	   final long ident = Binder.clearCallingIdentity();
	   try {
	       synchronized (mPublicSync) {
	           final WifiManager mgr = getWifiManager();
	           if (mgr == null) {
	               mLog.e("setWifiTethering: failed to get WifiManager!");
	               return TETHER_ERROR_SERVICE_UNAVAIL;
	           }
	           if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) ||
	               (!enable && mgr.stopSoftAp())) {
	               mWifiTetherRequested = enable;
	               return TETHER_ERROR_NO_ERROR;
	           }
	       }
	   } finally {
	       Binder.restoreCallingIdentity(ident);
	   }
	
	   return TETHER_ERROR_MASTER_ERROR;
	}
    private void sendTetherResult(ResultReceiver receiver, int result) {
        if (receiver != null) {
            receiver.send(result, null);
        }
    }
这里ResultReceiver 根据setWifiTethering的结果发送了一个 广播出去,暂时掐断不管,只看mgr.startSoftAp();
WifiManager.java
    public boolean startSoftAp(@Nullable WifiConfiguration wifiConfig) {
       try {
           return mService.startSoftAp(wifiConfig);//Tethering.java传递的参数到这里是null
       } catch (RemoteException e) {
           throw e.rethrowFromSystemServer();
       }
   }
然后 BaseWifiService extends IWifiManager.Stub
WifiServiceImpl extends BaseWifiService,看WifiServiceImpl.java
    @Override
    public boolean startSoftAp(WifiConfiguration wifiConfig) {
	mLog.info("startSoftAp Band:"+wifiConfig);
        // NETWORK_STACK is a signature only permission.
        enforceNetworkStackPermission();

        mLog.info("startSoftAp uid=%").c(Binder.getCallingUid()).flush();

        synchronized (mLocalOnlyHotspotRequests) {
            // If a tethering request comes in while we have an existing tethering session, return
            // error.
            if (mIfaceIpModes.contains(WifiManager.IFACE_IP_MODE_TETHERED)) {
                mLog.err("Tethering is already active.").flush();
                return false;
            }
            // If a tethering request comes in while we have LOHS running (or requested), call stop
            // for softap mode and restart softap with the tethering config.
            if (!isConcurrentLohsAndTetheringSupported() && !mLocalOnlyHotspotRequests.isEmpty()) {
                stopSoftApInternal(WifiManager.IFACE_IP_MODE_LOCAL_ONLY);
            }
            return startSoftApInternal(wifiConfig, WifiManager.IFACE_IP_MODE_TETHERED);
        }
    }
    
    private boolean startSoftApInternal(WifiConfiguration wifiConfig, int mode) {
        mLog.trace("startSoftApInternal uid=% mode=%")
                .c(Binder.getCallingUid()).c(mode).flush();

        // null wifiConfig is a meaningful input for CMD_SET_AP
        if (wifiConfig == null || WifiApConfigStore.validateApWifiConfiguration(wifiConfig)) {
            SoftApModeConfiguration softApConfig = new SoftApModeConfiguration(mode, wifiConfig);
            mWifiController.sendMessage(CMD_SET_AP, 1, 0, softApConfig);//看这里
	    Slog.d(TAG,"softApConfig Band"+wifiConfig);
            return true;
        }
        Slog.e(TAG, "Invalid WifiConfiguration");
        return false;
    }
先看一下这个 Android学习 StateMachine与State模式,再看WifiController.java,WifiController extends StateMachine,看StateMachine.java的sendMessage();
    @UnsupportedAppUsage
    public void sendMessage(int what, int arg1, int arg2, Object obj) {
        // mSmHandler can be null if the state machine has quit.
        SmHandler smh = mSmHandler;
        if (smh == null) return;

        smh.sendMessage(obtainMessage(what, arg1, arg2, obj));
    }
WifiController.java中,
class DefaultState extends State,
class DefaultState extends State,
class StaEnabledState extends State,
class StaDisabledWithScanState extends State,
都实现了processMessage()方法,所以看这里有对CMD_SET_AP的处理
         case CMD_SET_AP:
             // note: CMD_SET_AP is handled/dropped in ECM mode - will not start here
             Log.d(TAG,"CMD_SET_AP enterSoftAPMode stopSoftAPMode msg.arg1:"+msg.arg1);
             if (msg.arg1 == 1) {
                 SoftApModeConfiguration config = (SoftApModeConfiguration) msg.obj;
                 mActiveModeWarden.enterSoftAPMode((SoftApModeConfiguration) msg.obj);
             } else {
                 mActiveModeWarden.stopSoftAPMode(msg.arg2);
             }
             break;
          case CMD_SET_AP:
              Log.d(TAG,"CMD_SET_AP WIFI_DISABLED msg.arg1:"+msg.arg1);
              if (msg.arg1 == 1) {
                  // remember that we were disabled, but pass the command up to start softap
                  mSettingsStore.setWifiSavedState(WifiSettingsStore.WIFI_DISABLED);
              }
              return NOT_HANDLED;
      case CMD_SET_AP:
          Log.d(TAG,"CMD_SET_AP setWifiSavedState msg.arg1:"+msg.arg1);
          if (msg.arg1 == 1) {
              // remember that we were disabled, but pass the command up to start softap
              mSettingsStore.setWifiSavedState(WifiSettingsStore.WIFI_DISABLED);
              //这里关闭了wifi,待确认
          }
          return NOT_HANDLED;
          
      case CMD_SET_AP:
        Log.d(TAG,"CMD_SET_AP HANDLED");
       // do not want to start softap if we are in emergency mode
     return HANDLED;
然后看mActiveModeWarden.enterSoftAPMode((SoftApModeConfiguration) msg.obj);
./opt/net/wifi/service/java/com/android/server/wifi/ActiveModeWarden.java
    public void enterSoftAPMode(@NonNull SoftApModeConfiguration wifiConfig) {
        mHandler.post(() -> {
            startSoftAp(wifiConfig);
        });
    }
	private void startSoftAp(SoftApModeConfiguration softapConfig) {
        Log.d(TAG, "Starting SoftApModeManager");

        WifiConfiguration config = softapConfig.getWifiConfiguration();
        //Log.d(TAG,"startSoftAp Band:"+config.apBand);
        if (config != null && config.SSID != null) {
            Log.d(TAG, "Passing config to SoftApManager! " + config);
        } else {
            config = null;
        }

        SoftApCallbackImpl callback = new SoftApCallbackImpl(softapConfig.getTargetMode());
        ActiveModeManager manager = mWifiInjector.makeSoftApManager(callback, softapConfig);
        callback.setActiveModeManager(manager);
        manager.start();
        mActiveModeManagers.add(manager);
        updateBatteryStatsWifiState(true);
    }
然后,重点来了!public class SoftApManager implements ActiveModeManager.java
    public void start() {
        mStateMachine.sendMessage(SoftApStateMachine.CMD_START, mApConfig);
      	Log.d(TAG,"start() BAND:"+mApConfig.apBand);
    }
然后private class IdleState extends State
待续
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值