Andriod AP开启调用

作者: NiceDream | 来源:发表于2017-06-19 18:48 被阅读109次

    opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java

    public void setWifiApEnabled(WifiConfiguration wifiConfig, boolean enabled) {
            enforceChangePermission();
            ConnectivityManager.enforceTetherChangePermission(mContext);
            UserManager um = UserManager.get(mContext);
            if (um.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) {
                throw new SecurityException("DISALLOW_CONFIG_TETHERING is enabled for this user.");
            }
            // null wifiConfig is a meaningful input for CMD_SET_AP
            if (wifiConfig == null || wifiConfig.isValid()) {
                mWifiController.obtainMessage(CMD_SET_AP, enabled ? 1 : 0, 0, wifiConfig).sendToTarget();
            } else {
                Slog.e(TAG, "Invalid WifiConfiguration");
            }
        }
    
    public void setHostApRunning(WifiConfiguration wifiConfig, boolean enable) {
            if (enable) {
                sendMessage(CMD_START_AP, wifiConfig);
            } else {
                sendMessage(CMD_STOP_AP);
            }
        }
    
    case CMD_START_AP:
                        if (mWifiNative.loadDriver()) {
                            setWifiApState(WIFI_AP_STATE_ENABLING);
                            transitionTo(mSoftApStartingState);
                        } else {
                            loge("Failed to load driver for softap");
                        }
    
    class SoftApStartingState extends State {
            @Override
            public void enter() {
                final Message message = getCurrentMessage();
                if (message.what == CMD_START_AP) {
                    final WifiConfiguration config = (WifiConfiguration) message.obj;
    
                    if (config == null) {
                        mWifiApConfigChannel.sendMessage(CMD_REQUEST_AP_CONFIG);
                    } else {
                        mWifiApConfigChannel.sendMessage(CMD_SET_AP_CONFIG, config);
                        startSoftApWithConfig(config);
                    }
                } else {
                    throw new RuntimeException("Illegal transition to SoftApStartingState: " + message);
                }
            }
            @Override
            public boolean processMessage(Message message) {
                logStateAndMessage(message, getClass().getSimpleName());
    
                switch(message.what) {
                    case CMD_START_SUPPLICANT:
                    case CMD_STOP_SUPPLICANT:
                    case CMD_START_AP:
                    case CMD_STOP_AP:
                    case CMD_START_DRIVER:
                    case CMD_STOP_DRIVER:
                    case CMD_SET_OPERATIONAL_MODE:
                    case CMD_SET_COUNTRY_CODE:
                    case CMD_SET_FREQUENCY_BAND:
                    case CMD_START_PACKET_FILTERING:
                    case CMD_STOP_PACKET_FILTERING:
                    case CMD_TETHER_STATE_CHANGE:
                        deferMessage(message);
                        break;
                    case WifiStateMachine.CMD_RESPONSE_AP_CONFIG:
                        WifiConfiguration config = (WifiConfiguration) message.obj;
                        if (config != null) {
                            startSoftApWithConfig(config);
                        } else {
                            loge("Softap config is null!");
                            sendMessage(CMD_START_AP_FAILURE);
                        }
                        break;
                    case CMD_START_AP_SUCCESS:
                        setWifiApState(WIFI_AP_STATE_ENABLED);
                        transitionTo(mSoftApStartedState);
                        break;
                    case CMD_START_AP_FAILURE:
                        setWifiApState(WIFI_AP_STATE_FAILED);
                        transitionTo(mInitialState);
                        break;
                    default:
                        return NOT_HANDLED;
                }
                return HANDLED;
            }
        }
    
    private void startSoftApWithConfig(final WifiConfiguration config) {
            // Start hostapd on a separate thread
            new Thread(new Runnable() {
                public void run() {
                    try {
                        mNwService.startAccessPoint(config, mInterfaceName);
                    } catch (Exception e) {
                        loge("Exception in softap start " + e);
                        try {
                            mNwService.stopAccessPoint(mInterfaceName);
                            mNwService.startAccessPoint(config, mInterfaceName);
                        } catch (Exception e1) {
                            loge("Exception in softap re-start " + e1);
                            sendMessage(CMD_START_AP_FAILURE);
                            return;
                        }
                    }
                    if (DBG) log("Soft AP start successful");
                    sendMessage(CMD_START_AP_SUCCESS);
                    if(!mSoftApWakeLock.isHeld()) {
                        loge("---- mSoftApWakeLock.acquire ----");
                        mSoftApWakeLock.acquire();
                    }
                }
            }).start();
        }
    

    services/core/java/com/android/server/NetworkManagementService.java

    @Override
        public void setAccessPoint(WifiConfiguration wifiConfig, String wlanIface) {
            mContext.enforceCallingOrSelfPermission(CONNECTIVITY_INTERNAL, TAG);
            try {
                if (wifiConfig == null) {
                    mConnector.execute("softap", "set", wlanIface);
                } else {
                    mConnector.execute("softap", "set", wlanIface, wifiConfig.SSID,
                                       "broadcast", "6", getSecurityType(wifiConfig),
                                       new SensitiveArg(wifiConfig.preSharedKey));
                }
            } catch (NativeDaemonConnectorException e) {
                throw e.rethrowAsParcelableException();
            }
        }
    

    base/services/core/java/com/android/server/NativeDaemonConnector.java

    public NativeDaemonEvent[] execute(int timeout, String cmd, Object... args)
                throws NativeDaemonConnectorException {
            final long startTime = SystemClock.elapsedRealtime();
    
            final ArrayList<NativeDaemonEvent> events = Lists.newArrayList();
    
            final StringBuilder rawBuilder = new StringBuilder();
            final StringBuilder logBuilder = new StringBuilder();
            final int sequenceNumber = mSequenceNumber.incrementAndGet();
    
            makeCommand(rawBuilder, logBuilder, sequenceNumber, cmd, args);
    
            final String rawCmd = rawBuilder.toString();
            final String logCmd = logBuilder.toString();
    
            log("SND -> {" + logCmd + "}");
    
            synchronized (mDaemonLock) {
                if (mOutputStream == null) {
                    throw new NativeDaemonConnectorException("missing output stream");
                } else {
                    try {
                        mOutputStream.write(rawCmd.getBytes(StandardCharsets.UTF_8));
                    } catch (IOException e) {
                        throw new NativeDaemonConnectorException("problem sending command", e);
                    }
                }
            }
    
            NativeDaemonEvent event = null;
            do {
                event = mResponseQueue.remove(sequenceNumber, timeout, logCmd);
                if (event == null) {
                    loge("timed-out waiting for response to " + logCmd);
                    throw new NativeDaemonFailureException(logCmd, event);
                }
                if (VDBG) log("RMV <- {" + event + "}");
                events.add(event);
            } while (event.isClassContinue());
    
            final long endTime = SystemClock.elapsedRealtime();
            if (endTime - startTime > WARN_EXECUTE_DELAY_MS) {
                loge("NDC Command {" + logCmd + "} took too long (" + (endTime - startTime) + "ms)");
            }
    
            if (event.isClassClientError()) {
                throw new NativeDaemonArgumentException(logCmd, event);
            }
            if (event.isClassServerError()) {
                throw new NativeDaemonFailureException(logCmd, event);
            }
    
            return events.toArray(new NativeDaemonEvent[events.size()]);
        }
    

    相关文章

      网友评论

      本文标题:Andriod AP开启调用

      本文链接:https://www.haomeiwen.com/subject/lzknqxtx.html