WifiStateMachine学习笔记
WifiStateMachine
1. 初始化
传入接口名称wlanInterface
新建一个WiFi类型的NetworkInfo
发一个ssid为null的广播
电池
NetworkManagementService
P2p是否支持mP2pSupported
基于接口名称wlanInterface的WifiNative
基于WifiNative的WifiConfigStore
基于WifiNative的WifiMonitor
WifiInfo
SupplicantStateTracker
两个LinkProperties: mLinkProperties + mNetlinkLinkProperties
mWifiP2pManager
一个InterfaceObserver。让NetworkManagementService帮忙专门监视ip address的修改
AlarmManager
普通扫描的intent:scanIntent + mScanIntent ACTION_START_SCAN + SCAN_REQUEST
批量扫描的intent: batchedIntent + mBatchedScanIntervalIntent ACTION_REFRESH_BATCHED_SCAN
framework默认扫描间隔: mDefaultFrameworkScanIntervalMs
driver延迟stop时间: mDriverStopDelayMs
后台扫描支持: mBackgroundScanSupported
P2p设备类型 : mPrimaryDeviceType
user的省电选择: mUserWantsSuspendOpt
监听Tethering网络状态改变的BroadcastReceiver : ACTION_TETHER_STATE_CHANGED
监听扫描请求的BroadcastReceiver : ACTION_START_SCAN
监听屏幕状态变化的BroadcastReceiver : ACTION_SCREEN_ON + ACTION_SCREEN_OFF +ACTION_REFRESH_BATCHED_SCAN
监听延迟stop driver请求的BroadcastReceiver :ACTION_DELAYED_DRIVER_STOP
监听user对SUSPEND选项的改动: WIFI_SUSPEND_OPTIMIZATIONS_ENABLED
监听开机完成选项:ACTION_BOOT_COMPLETED
LruCache类型的列表: mScanResultCache
电源powerManager
休眠锁: mWakeLock + mSuspendWakeLock
SoftAP省电Intent : SoftAPIdleIntent + mSoftAPIdleIntent
监听SOFTAP和STA连接断开情况: ACTION_SOFTAP_STA_CONNECTED + ACTION_SOFTAP_STA_DISCONNECTED
连接better热点的BroadcastReceiver: ASUS_CHOOSE_BESTAP_ACTION
enable全部网络: EnableAllNetworkIntent + mEnableAllNetworkIntent + ACTION_ENABLE_ALL_NETWORK
监听SIM卡状态的BroadcastReceiver: ACTION_SIM_STATE_CHANGED
以下是状态机结构 :
0. mDefaultState:
1. mInitialState 状态机的初始状态
2. mSupplicantStartingState
3. mSupplicantStartedState
3.1 mDriverStartingState
3.2 mDriverStartedState
3.2.1 mScanModeState
3.2.2 mConnectModeState
3.2.2.1 mL2ConnectedState
3.2.2.2 mDisconnectingState
3.2.2.3 mDisconnectedState
3.2.2.4 mWpsRunningState
3.3 mWaitForP2pDisableState
3.4 mDriverStoppingState
3.5 mDriverStoppedState
4. mSupplicantStoppingState
5. mSoftApStartingState
6. mSoftApStartedState
6.1 mTetheringState
6.2 mTetheredState
6.3 mUntetheringState
每一个状态机包含例如以下流程:
1. enter阶段
2. processMessage阶段
3. exit阶段
待状态机开动后通知一下 :WIFI_SCAN_AVAILABLE
看一下每一个状态机处理什么:
1. mInitialState 状态机的初始状态
enter阶段:
卸载driver
与p2p状态机建立连接
SoftAP的配置状态机WifiApConfigStore新建并载入配置
与SoftAP的状态机WifiApConfigStore建立连接
processMessage阶段:
处理的消息有:
CMD_START_SUPPLICANT :
mWifiNative先载入driver
NetworkManagementService载入STA类型的firmware
supplicant起来前最好setInterfaceDown
NetworkManagementService设置setInterfaceIpv6PrivacyExtensions ?
NetworkManagementService禁掉disableIpv6 ?
mWifiMonitor杀死执行中的killSupplicant
mWifiNative开启startSupplicant
设置wifi状态WIFI_STATE_ENABLING: 发送广播WIFI_STATE_CHANGED_ACTION
mWifiMonitor開始监视supplicant的执行: startMonitoring
转移到其它状态: 2. mSupplicantStartingState
CMD_START_AP :
mWifiNative先载入driver
设置softAP状态: WIFI_AP_STATE_ENABLING
转移到其它状态 : 5. mSoftApStartingState
2. mSupplicantStartingState
processMessage阶段 :
处理的消息有:
WifiMonitor.SUP_CONNECTION_EVENT :
设置状态WIFI_STATE_ENABLED
向mSupplicantStateTracker发送消息CMD_RESET_SUPPLICANT_STATE,表明如今supplicant状态不明
WifiInfo设置MAC地址:setMacAddress
mWifiConfigStore载入并使能全部网络loadAndEnableAllNetworks
初始化WPS连接: initializeWpsDetails
发送广播表明supplicant连接成功:sendSupplicantConnectionChangedBroadcast
转移到其它状态: 3.2 mDriverStartedState
WifiMonitor.SUP_DISCONNECTION_EVENT :
会尝试SUPPLICANT_RESTART_TRIES(=5)次来启动并连接supplicant
若重新启动次数超过5次,设置状态WIFI_STATE_UNKNOWN并转移到状态1. mInitialState
否则killSupplicant,转移到状态1. mInitialState,发送CMD_START_SUPPLICANT间隔SUPPLICANT_RESTART_INTERVAL_MSECS重新启动supplicant
其它消息留给mDefaultState处理
3.2 mDriverStartedState
enter阶段 :
通知电池一声: updateBatteryWorkSource + mWakeLock.setWorkSource(newSource);
设置蓝牙共存扫描模式,有些扫描參数要改动以降低对BT的干扰: setBluetoothCoexistenceScanMode
设置country code :setCountryCode
设置频带:setFrequencyBand
设置当前网络状态信息,即改动mNetworkInfo: setNetworkDetailedState
设置ipv6 packet filter,只是滤ipv6 packet,原因百度:stopFilteringMulticastV6Packets
设置ipv4 packet filter:startFilteringMulticastV4Packets + stopFilteringMulticastV4Packets
依据user设置来执行STA的三个操作状态:
CONNECT_MODE,能够扫描并连接到AP,转移到其它状态mDisconnectedState
SCAN_ONLY_MODE,仅仅可扫描AP , 转移到其它状态3.2.1 mScanModeState
SCAN_ONLY_WIFI_OFF_MODE,仅仅可在Wifibutton关闭时扫描AP, 转移到其它状态mScanModeState
接收来自屏幕的广播,
如有收到广播。setSuspendOptimizations
否则。处理屏幕变化handleScreenStateChanged
设置省电 :setPowerSave
能够让P2p准备连接了: WifiStateMachine.CMD_ENABLE_P2P
发送广播告诉全部人能够開始扫描了:WIFI_SCAN_AVAILABLE
processMessage阶段 :
处理的消息有:
CMD_START_SCAN:
是谁发起的扫描:noteScanStart
開始native扫描 : startScanNative + WifiNative.SCAN_WITH_CONNECTION_SETUP
CMD_SET_BATCHED_SCAN:
记录批量扫描设置recordBatchedScanSettings
開始批量扫描startBatchedScan
CMD_SET_COUNTRY_CODE:
设置country code :setCountryCode
CMD_SET_FREQUENCY_BAND:
设置band:setBand
flush old data - like scan results:bssFlush
频带设置好后获取最新扫描结果: startScanNative
CMD_BLUETOOTH_ADAPTER_STATE_CHANGE:
蓝牙适配器状态改变:
设置蓝牙兼容扫描模式 :setBluetoothCoexistenceScanMode
CMD_STOP_DRIVER:
mWifiConfigStore禁掉全部网络:disableAllNetworks
发送stop driver命令 :CMD_DELAYED_STOP_DRIVER
CMD_START_DRIVER:
同意全部网络:enableAllNetworks
CMD_DELAYED_STOP_DRIVER:
关闭driver :stopDriver
转移到mWaitForP2pDisableState
CMD_START_PACKET_FILTERING:
startFilteringMulticastV6Packets + startFilteringMulticastV4Packets
CMD_STOP_PACKET_FILTERING:
stopFilteringMulticastV6Packets + stopFilteringMulticastV4Packets
CMD_SET_SUSPEND_OPT_ENABLED:
依据屏幕开关调整suspend: setSuspendOptimizationsNative(SUSPEND_DUE_TO_SCREEN, true);
CMD_SET_HIGH_PERF_MODE:
依据user设置性能调整suspend: setSuspendOptimizationsNative(SUSPEND_DUE_TO_HIGH_PERF, false);
CMD_ENABLE_TDLS:
开启TDLS: startTdls
ASUS_EVENT_DEL_PMKID:
移除SIM卡的同一时候清楚PMK信息 :delPmkId
exit阶段 :
通知电池一声: updateBatteryWorkSource
发送广播告诉全部人能够開始扫描了:WIFI_SCAN_AVAILABLE
3.2.1 mScanModeState
processMessage阶段:
处理的消息有:
CMD_SET_OPERATIONAL_MODE:
CMD_START_SCAN:
3.2.2.3 mDisconnectedState
enter阶段:
推断是否由于p2p暂时断开p2p连接
获取framework扫描间隔时间mFrameworkScanIntervalMs
后台扫描会取消当前的常规扫描,造成没有扫描结果返回,所以当存在等待运行的常规扫描时不要开启后台扫描
当前没有保存不论什么网络,supplicant停止获取AP信息的periodic scan;可是须要定时开启periodic scan来通知user周围有open network
processMessage阶段:
处理的消息有:
CMD_NO_NETWORKS_PERIODIC_SCAN:
这是没有保存不论什么网络时须要定时开启的扫描
FORGET_NETWORK:
CMD_REMOVE_NETWORK:
不处理,这是没有保存不论什么网络时须要定时开启的扫描
CMD_SET_OPERATIONAL_MODE:
依据user设置来执行STA的三个操作状态:
CONNECT_MODE。能够扫描并连接到AP,转移到其它状态mDisconnectedState
SCAN_ONLY_MODE,仅仅可扫描AP , 转移到其它状态3.2.1 mScanModeState
SCAN_ONLY_WIFI_OFF_MODE,仅仅可在Wifibutton关闭时扫描AP, 转移到其它状态mScanModeState
CMD_ENABLE_BACKGROUND_SCAN:
后台扫描
NETWORK_DISCONNECTION_EVENT:
SUPPLICANT_STATE_CHANGE_EVENT:
设置网络细节状态:setNetworkDetailedState
CMD_START_SCAN:
后台扫描
SCAN_RESULTS_EVENT:
NOT_HANDLED。交给parent处理
P2P_CONNECTION_CHANGED:
处理p2p连接状态的改变
CMD_RECONNECT:
CMD_REASSOCIATE:
exit阶段:
使能后台扫描
3.2.2.4 mWpsRunningState
processMessage阶段:
处理的消息有:
WPS_SUCCESS_EVENT:
NETWORK_CONNECTION_EVENT:
WPS_OVERLAP_EVENT:
WPS_FAIL_EVENT:
WPS_TIMEOUT_EVENT:
START_WPS:
CANCEL_WPS:
exit阶段:
5. mSoftApStartingState
enter阶段:
消息是CMD_START_AP,检查config是否有效
config有效,发送消息CMD_SET_AP_CONFIG来设置config,然后打开softap: startSoftApWithConfig
processMessage阶段:
处理的消息有:
exit阶段:
6.2 mTetheredState
enter阶段:
设置一个RTC_WAKEUP。用于定时发送mSoftAPIdleIntent
processMessage阶段:
处理的消息有:
exit阶段:
6.3 mUntetheringState
enter阶段:
5秒后发送一个超时消息,关闭tethering
processMessage阶段:
处理的消息有:
CMD_TETHER_STATE_CHANGE:
CMD_TETHER_NOTIFICATION_TIMED_OUT:
exit阶段:
1. 初始化
传入接口名称wlanInterface
新建一个WiFi类型的NetworkInfo
发一个ssid为null的广播
电池
NetworkManagementService
P2p是否支持mP2pSupported
基于接口名称wlanInterface的WifiNative
基于WifiNative的WifiConfigStore
基于WifiNative的WifiMonitor
WifiInfo
SupplicantStateTracker
两个LinkProperties: mLinkProperties + mNetlinkLinkProperties
mWifiP2pManager
一个InterfaceObserver。让NetworkManagementService帮忙专门监视ip address的修改
AlarmManager
普通扫描的intent:scanIntent + mScanIntent ACTION_START_SCAN + SCAN_REQUEST
批量扫描的intent: batchedIntent + mBatchedScanIntervalIntent ACTION_REFRESH_BATCHED_SCAN
framework默认扫描间隔: mDefaultFrameworkScanIntervalMs
driver延迟stop时间: mDriverStopDelayMs
后台扫描支持: mBackgroundScanSupported
P2p设备类型 : mPrimaryDeviceType
user的省电选择: mUserWantsSuspendOpt
监听Tethering网络状态改变的BroadcastReceiver : ACTION_TETHER_STATE_CHANGED
监听扫描请求的BroadcastReceiver : ACTION_START_SCAN
监听屏幕状态变化的BroadcastReceiver : ACTION_SCREEN_ON + ACTION_SCREEN_OFF +ACTION_REFRESH_BATCHED_SCAN
监听延迟stop driver请求的BroadcastReceiver :ACTION_DELAYED_DRIVER_STOP
监听user对SUSPEND选项的改动: WIFI_SUSPEND_OPTIMIZATIONS_ENABLED
监听开机完成选项:ACTION_BOOT_COMPLETED
LruCache类型的列表: mScanResultCache
电源powerManager
休眠锁: mWakeLock + mSuspendWakeLock
SoftAP省电Intent : SoftAPIdleIntent + mSoftAPIdleIntent
监听SOFTAP和STA连接断开情况: ACTION_SOFTAP_STA_CONNECTED + ACTION_SOFTAP_STA_DISCONNECTED
连接better热点的BroadcastReceiver: ASUS_CHOOSE_BESTAP_ACTION
enable全部网络: EnableAllNetworkIntent + mEnableAllNetworkIntent + ACTION_ENABLE_ALL_NETWORK
监听SIM卡状态的BroadcastReceiver: ACTION_SIM_STATE_CHANGED
以下是状态机结构 :
0. mDefaultState:
1. mInitialState 状态机的初始状态
2. mSupplicantStartingState
3. mSupplicantStartedState
3.1 mDriverStartingState
3.2 mDriverStartedState
3.2.1 mScanModeState
3.2.2 mConnectModeState
3.2.2.1 mL2ConnectedState
3.2.2.2 mDisconnectingState
3.2.2.3 mDisconnectedState
3.2.2.4 mWpsRunningState
3.3 mWaitForP2pDisableState
3.4 mDriverStoppingState
3.5 mDriverStoppedState
4. mSupplicantStoppingState
5. mSoftApStartingState
6. mSoftApStartedState
6.1 mTetheringState
6.2 mTetheredState
6.3 mUntetheringState
每一个状态机包含例如以下流程:
1. enter阶段
2. processMessage阶段
3. exit阶段
待状态机开动后通知一下 :WIFI_SCAN_AVAILABLE
看一下每一个状态机处理什么:
1. mInitialState 状态机的初始状态
enter阶段:
卸载driver
与p2p状态机建立连接
SoftAP的配置状态机WifiApConfigStore新建并载入配置
与SoftAP的状态机WifiApConfigStore建立连接
processMessage阶段:
处理的消息有:
CMD_START_SUPPLICANT :
mWifiNative先载入driver
NetworkManagementService载入STA类型的firmware
supplicant起来前最好setInterfaceDown
NetworkManagementService设置setInterfaceIpv6PrivacyExtensions ?
NetworkManagementService禁掉disableIpv6 ?
mWifiMonitor杀死执行中的killSupplicant
mWifiNative开启startSupplicant
设置wifi状态WIFI_STATE_ENABLING: 发送广播WIFI_STATE_CHANGED_ACTION
mWifiMonitor開始监视supplicant的执行: startMonitoring
转移到其它状态: 2. mSupplicantStartingState
CMD_START_AP :
mWifiNative先载入driver
设置softAP状态: WIFI_AP_STATE_ENABLING
转移到其它状态 : 5. mSoftApStartingState
2. mSupplicantStartingState
processMessage阶段 :
处理的消息有:
WifiMonitor.SUP_CONNECTION_EVENT :
设置状态WIFI_STATE_ENABLED
向mSupplicantStateTracker发送消息CMD_RESET_SUPPLICANT_STATE,表明如今supplicant状态不明
WifiInfo设置MAC地址:setMacAddress
mWifiConfigStore载入并使能全部网络loadAndEnableAllNetworks
初始化WPS连接: initializeWpsDetails
发送广播表明supplicant连接成功:sendSupplicantConnectionChangedBroadcast
转移到其它状态: 3.2 mDriverStartedState
WifiMonitor.SUP_DISCONNECTION_EVENT :
会尝试SUPPLICANT_RESTART_TRIES(=5)次来启动并连接supplicant
若重新启动次数超过5次,设置状态WIFI_STATE_UNKNOWN并转移到状态1. mInitialState
否则killSupplicant,转移到状态1. mInitialState,发送CMD_START_SUPPLICANT间隔SUPPLICANT_RESTART_INTERVAL_MSECS重新启动supplicant
其它消息留给mDefaultState处理
3.2 mDriverStartedState
enter阶段 :
通知电池一声: updateBatteryWorkSource + mWakeLock.setWorkSource(newSource);
设置蓝牙共存扫描模式,有些扫描參数要改动以降低对BT的干扰: setBluetoothCoexistenceScanMode
设置country code :setCountryCode
设置频带:setFrequencyBand
设置当前网络状态信息,即改动mNetworkInfo: setNetworkDetailedState
设置ipv6 packet filter,只是滤ipv6 packet,原因百度:stopFilteringMulticastV6Packets
设置ipv4 packet filter:startFilteringMulticastV4Packets + stopFilteringMulticastV4Packets
依据user设置来执行STA的三个操作状态:
CONNECT_MODE,能够扫描并连接到AP,转移到其它状态mDisconnectedState
SCAN_ONLY_MODE,仅仅可扫描AP , 转移到其它状态3.2.1 mScanModeState
SCAN_ONLY_WIFI_OFF_MODE,仅仅可在Wifibutton关闭时扫描AP, 转移到其它状态mScanModeState
接收来自屏幕的广播,
如有收到广播。setSuspendOptimizations
否则。处理屏幕变化handleScreenStateChanged
设置省电 :setPowerSave
能够让P2p准备连接了: WifiStateMachine.CMD_ENABLE_P2P
发送广播告诉全部人能够開始扫描了:WIFI_SCAN_AVAILABLE
processMessage阶段 :
处理的消息有:
CMD_START_SCAN:
是谁发起的扫描:noteScanStart
開始native扫描 : startScanNative + WifiNative.SCAN_WITH_CONNECTION_SETUP
CMD_SET_BATCHED_SCAN:
记录批量扫描设置recordBatchedScanSettings
開始批量扫描startBatchedScan
CMD_SET_COUNTRY_CODE:
设置country code :setCountryCode
CMD_SET_FREQUENCY_BAND:
设置band:setBand
flush old data - like scan results:bssFlush
频带设置好后获取最新扫描结果: startScanNative
CMD_BLUETOOTH_ADAPTER_STATE_CHANGE:
蓝牙适配器状态改变:
设置蓝牙兼容扫描模式 :setBluetoothCoexistenceScanMode
CMD_STOP_DRIVER:
mWifiConfigStore禁掉全部网络:disableAllNetworks
发送stop driver命令 :CMD_DELAYED_STOP_DRIVER
CMD_START_DRIVER:
同意全部网络:enableAllNetworks
CMD_DELAYED_STOP_DRIVER:
关闭driver :stopDriver
转移到mWaitForP2pDisableState
CMD_START_PACKET_FILTERING:
startFilteringMulticastV6Packets + startFilteringMulticastV4Packets
CMD_STOP_PACKET_FILTERING:
stopFilteringMulticastV6Packets + stopFilteringMulticastV4Packets
CMD_SET_SUSPEND_OPT_ENABLED:
依据屏幕开关调整suspend: setSuspendOptimizationsNative(SUSPEND_DUE_TO_SCREEN, true);
CMD_SET_HIGH_PERF_MODE:
依据user设置性能调整suspend: setSuspendOptimizationsNative(SUSPEND_DUE_TO_HIGH_PERF, false);
CMD_ENABLE_TDLS:
开启TDLS: startTdls
ASUS_EVENT_DEL_PMKID:
移除SIM卡的同一时候清楚PMK信息 :delPmkId
exit阶段 :
通知电池一声: updateBatteryWorkSource
发送广播告诉全部人能够開始扫描了:WIFI_SCAN_AVAILABLE
3.2.1 mScanModeState
processMessage阶段:
处理的消息有:
CMD_SET_OPERATIONAL_MODE:
CMD_START_SCAN:
3.2.2.3 mDisconnectedState
enter阶段:
推断是否由于p2p暂时断开p2p连接
获取framework扫描间隔时间mFrameworkScanIntervalMs
后台扫描会取消当前的常规扫描,造成没有扫描结果返回,所以当存在等待运行的常规扫描时不要开启后台扫描
当前没有保存不论什么网络,supplicant停止获取AP信息的periodic scan;可是须要定时开启periodic scan来通知user周围有open network
processMessage阶段:
处理的消息有:
CMD_NO_NETWORKS_PERIODIC_SCAN:
这是没有保存不论什么网络时须要定时开启的扫描
FORGET_NETWORK:
CMD_REMOVE_NETWORK:
不处理,这是没有保存不论什么网络时须要定时开启的扫描
CMD_SET_OPERATIONAL_MODE:
依据user设置来执行STA的三个操作状态:
CONNECT_MODE。能够扫描并连接到AP,转移到其它状态mDisconnectedState
SCAN_ONLY_MODE,仅仅可扫描AP , 转移到其它状态3.2.1 mScanModeState
SCAN_ONLY_WIFI_OFF_MODE,仅仅可在Wifibutton关闭时扫描AP, 转移到其它状态mScanModeState
CMD_ENABLE_BACKGROUND_SCAN:
后台扫描
NETWORK_DISCONNECTION_EVENT:
SUPPLICANT_STATE_CHANGE_EVENT:
设置网络细节状态:setNetworkDetailedState
CMD_START_SCAN:
后台扫描
SCAN_RESULTS_EVENT:
NOT_HANDLED。交给parent处理
P2P_CONNECTION_CHANGED:
处理p2p连接状态的改变
CMD_RECONNECT:
CMD_REASSOCIATE:
exit阶段:
使能后台扫描
3.2.2.4 mWpsRunningState
processMessage阶段:
处理的消息有:
WPS_SUCCESS_EVENT:
NETWORK_CONNECTION_EVENT:
WPS_OVERLAP_EVENT:
WPS_FAIL_EVENT:
WPS_TIMEOUT_EVENT:
START_WPS:
CANCEL_WPS:
exit阶段:
5. mSoftApStartingState
enter阶段:
消息是CMD_START_AP,检查config是否有效
config有效,发送消息CMD_SET_AP_CONFIG来设置config,然后打开softap: startSoftApWithConfig
processMessage阶段:
处理的消息有:
exit阶段:
6.2 mTetheredState
enter阶段:
设置一个RTC_WAKEUP。用于定时发送mSoftAPIdleIntent
processMessage阶段:
处理的消息有:
exit阶段:
6.3 mUntetheringState
enter阶段:
5秒后发送一个超时消息,关闭tethering
processMessage阶段:
处理的消息有:
CMD_TETHER_STATE_CHANGE:
CMD_TETHER_NOTIFICATION_TIMED_OUT:
exit阶段: