当前位置:   article > 正文

Android 11 framework学习之热点 打开TetherManager_tetheringmanager服务是什么

tetheringmanager服务是什么

android 11打开热点时序图如下,下面将根据时序图分析:
enable ap
WifiTetherFragment是实现开关热点的Fragment,调用TetherManager::startTethering()去开启WiFi共享

  1. /*** com.android.car.settings.wifi.WifiTetherFragment.startTethering ***/
  2. private void startTethering() {
  3. mTetheringManager.startTethering(ConnectivityManager.TETHERING_WIFI,
  4. ConcurrentUtils.DIRECT_EXECUTOR,
  5. new TetheringManager.StartTetheringCallback() {
  6. @Override
  7. public void onTetheringFailed(final int result) {
  8. mTetherSwitch.setChecked(false);
  9. mTetherSwitch.setEnabled(true);
  10. }
  11. });
  12. }

TetheringManager内部通过ITetheringConnector实现具体功能,类似于WifiManager

  1. /*** android.net.TetheringManager.startTethering ***/
  2.  public void startTethering(@NonNull final TetheringRequest request,
  3.         @NonNull final Executor executor, @NonNull final StartTetheringCallback callback) {
  4.     final String callerPkg = mContext.getOpPackageName();
  5.     Log.i(TAG, "startTethering caller:" + callerPkg);
  6.     ...
  7.     getConnector(c -> c.startTethering(request.getParcel(), callerPkg, listener));
  8. }


传入的Tethering type是ConnectivityManager.TETHERING_WIFI,直接看TetheringService
startTethering之前需要check是否支持操作热点

  1. /*** com.android.networkstack.tethering.TetheringService.TetheringConnector.startTethering ***/
  2. public void startTethering(TetheringRequestParcel request, String callerPkg, IIntResultListener listener) {
  3.     if (checkAndNotifyCommonError(callerPkg, request.exemptFromEntitlementCheck /* onlyAllowPrivileged */, listener)) {
  4.          return;
  5.         }
  6.     mTethering.startTethering(request, listener);
  7. }

在enableTetheringInternal之前需要检查没有其他打开热点的请求

  1. /*** com.android.networkstack.tethering.Tethering.startTethering ***/
  2. void startTethering(final TetheringRequestParcel request, final IIntResultListener listener) {
  3.     mHandler.post(() -> {
  4.         ...
  5.         if (unfinishedRequest != null
  6.                 && !TetheringUtils.isTetheringRequestEquals(unfinishedRequest, request)) {
  7.             enableTetheringInternal(request.tetheringType, false /* disabled */, null);
  8.             mEntitlementMgr.stopProvisioningIfNeeded(request.tetheringType);
  9.         }
  10.         ...
  11.         enableTetheringInternal(request.tetheringType, true /* enabled */, listener);
  12.     });
  13. }

打开对应请求的功能,这里对应的应该是TETHERING_WIFI

  1. /*** com.android.networkstack.tethering.Tethering.enableTetheringInternal ***/
  2. private void enableTetheringInternal(int type, boolean enable, final IIntResultListener listener) {
  3.     int result = TETHER_ERROR_NO_ERROR;
  4.     switch (type) {
  5.         case TETHERING_WIFI:
  6.             result = setWifiTethering(enable);
  7.             break;
  8.         ...
  9.     }
  10.     ...
  11. }

获取WifiManager对象后操作startTetheredHotspot

  1. /*** com.android.networkstack.tethering.Tethering.setWifiTethering ***/
  2. private int setWifiTethering(final boolean enable) {
  3.     ...
  4.     if ((enable && mgr.startTetheredHotspot(null /* use existing softap config */))
  5.             || (!enable && mgr.stopSoftAp())) {
  6.                 mWifiTetherRequested = enable;
  7.                 return TETHER_ERROR_NO_ERROR;
  8.             }
  9.     ...
  10. }
  1. /*** android.net.wifi.WifiManager.startTetheredHotspot ***/
  2. public boolean startTetheredHotspot(@Nullable SoftApConfiguration softApConfig) {
  3. try {
  4. return mService.startTetheredHotspot(softApConfig);
  5. } catch (RemoteException e) {
  6. throw e.rethrowFromSystemServer();
  7. }
  8. }

调用内部方法startSoftApInternal

  1. /*** com.android.server.wifi.WifiServiceImpl.startTetheredHotspot ***/
  2. public boolean startTetheredHotspot(@Nullable SoftApConfiguration softApConfig) {
  3.     ...
  4.     if (!startSoftApInternal(new SoftApModeConfiguration(
  5.             WifiManager.IFACE_IP_MODE_TETHERED, softApConfig,
  6.             mTetheredSoftApTracker.getSoftApCapability()))) {
  7.         mTetheredSoftApTracker.setFailedWhileEnabling();
  8.         return false;
  9.     }
  10.     return true;
  11. }


封装了状态机中的startSoftAp接口

  1. /*** com.android.server.wifi.WifiServiceImpl.startSoftApInternal ***/
  2. private boolean startSoftApInternal(SoftApModeConfiguration apConfig) {
  3.     ...
  4.     mActiveModeWarden.startSoftAp(apConfig);
  5.     return true;
  6. }

通过WifiController状态机,发送热点状态消息

  1. /*** com.android.server.wifi.ActiveModeWarden.startSoftAp ***/
  2. public void startSoftAp(SoftApModeConfiguration softApConfig) {
  3.     mWifiController.sendMessage(WifiController.CMD_SET_AP, 1, 0, softApConfig);
  4. }

状态机之前应该是DisabledState状态,因此处理此消息流程如下

  1. /*** com.android.server.wifi.ActiveModeWarden.WifiController.DisabledState.processMessageFiltered ***/
  2. public boolean processMessageFiltered(Message msg) {
  3. switch (msg.what) {
  4. ...
  5. case CMD_SET_AP:
  6. // note: CMD_SET_AP is handled/dropped in ECM mode - will not start here
  7. if (msg.arg1 == 1) {
  8. startSoftApModeManager((SoftApModeConfiguration) msg.obj);
  9. transitionTo(mEnabledState);
  10. }
  11. break;
  12. ...
  13. }
  14. return HANDLED;
  15. }
  1. /*** com.android.server.wifi.ActiveModeWarden.startSoftApModeManager ***/
  2. private void startSoftApModeManager(@NonNull SoftApModeConfiguration softApConfig) {
  3. ...
  4. SoftApListener listener = new SoftApListener();
  5. ActiveModeManager manager =
  6. mWifiInjector.makeSoftApManager(listener, callback, softApConfig);
  7. listener.setActiveModeManager(manager);
  8. manager.start();
  9. manager.setRole(getRoleForSoftApIpMode(softApConfig.getTargetMode()));
  10. mActiveModeManagers.add(manager);
  11. }

ActiveModeManager是SoftApManager的基类,实际调用到SoftApManager.start,状态机发送SoftApStateMachine.CMD_START消息

  1. /*** com.android.server.wifi.SoftApManager.start ***/
  2. public void start() {
  3.     mStateMachine.sendMessage(SoftApStateMachine.CMD_START);
  4. }


IdleState为初始状态,因此状态机在IdleState时处理此消息,处理成功之后状态机跳转到StartedState
广播ap状态变更为WIFI_AP_STATE_ENABLING

  1. /*** com.android.server.wifi.SoftApManager.SoftApStateMachine.IdleState.processMessage ***/
  2. case CMD_START:
  3.     SoftApConfiguration config = (SoftApConfiguration) message.obj;
  4.     ...
  5.     updateApState(WifiManager.WIFI_AP_STATE_ENABLING, WifiManager.WIFI_AP_STATE_DISABLED, 0);
  6.     int result = startSoftAp();
  7.     if (result != SUCCESS) 
  8.     ...
  9.     transitionTo(mStartedState);
  10.     break;

最后调到WifiNative,它的状态由SoftApListener回调管理

  1. /*** com.android.server.wifi.SoftApManager.startSoftAp ***/
  2. private int startSoftAp() {
  3.     ...
  4.     if (!mWifiNative.startSoftAp(mApInterfaceName,
  5.               localConfigBuilder.build(), mSoftApListener)) {
  6.         Log.e(TAG, "Soft AP start failed");
  7.         return ERROR_GENERIC;
  8.     }
  9.     ...
  10.     return SUCCESS;
  11. }

native之后
调用到HostapdHal.addAccessPoint

  1. /*** com.android.server.wifi.WifiNative.startSoftAp ***/
  2. ...
  3. if (mHostapHal.isVendorHostapHal()) {
  4. if (!mHostapHal.addVendorAccessPoint(ifaceName, config, listener)) {
  5. ...
  6. }
  7. } else if (!mHostapHal.addAccessPoint(ifaceName, config, listener::onFailure)) {
  8. ...
  9. }
  10. ...

最终调用到hal层addAccessPoint接口

  1. /*** com.android.server.wifi.HostapdHal.addAccessPoint ***/
  2. //Hostapd HAL interface objects
  3. private IHostapd mIHostapd;
  4. ...
  5. status = mIHostapd.addAccessPoint(ifaceParams, nwParamsV1_2.V1_0);
  6. ...

step15之后SoftApStateMachine状态机进入StartedState,进入enter,比较重要的是此时会设置一个默认600000ms(10min)的timeout超时机制,如果此时间段一直没有设备连接该AP,就会自动关闭AP
从WifiNative成功建立了iface之后,调用onUpChanged进行广播状态切换为WifiManager.WIFI_AP_STATE_ENABLED

  1. /*** com.android.server.wifi.SoftApManager.SoftApStateMachine.StatedState.enter ***/
  2. public void enter() {
  3. mIfaceIsUp = false;
  4. mIfaceIsDestroyed = false;
  5. onUpChanged(mWifiNative.isInterfaceUp(mApInterfaceName));
  6. onUpChanged(mWifiNative.isInterfaceUp(mDataInterfaceName));
  7. Handler handler = mStateMachine.getHandler();
  8. mSoftApTimeoutMessage = new WakeupMessage(mContext, handler,
  9. SOFT_AP_SEND_MESSAGE_TIMEOUT_TAG,
  10. SoftApStateMachine.CMD_NO_ASSOCIATED_STATIONS_TIMEOUT);
  11. mSarManager.setSapWifiState(WifiManager.WIFI_AP_STATE_ENABLED);
  12. Log.d(TAG, "Resetting connected clients on start");
  13. mConnectedClients.clear();
  14. mEverReportMetricsForMaxClient = false;
  15. scheduleTimeoutMessage();
  16. }


————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.csdn.net/innocentQ/article/details/135076984

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号