WifiCommand流程 wpa_supplicant启动流程 Wifiservice启动流程(转)

本文介绍了WiFi信号强度(RSSI)的计算方法及其在不同层级的显示规则,并详细解析了信号强度及链接速度(LINKSPEED_CMD)的获取流程。

1. 信号强度算法

    WifiManager.java

 
  1. /** Anything worse than or equal to this will show 0 bars. */  
  2. private static final int MIN_RSSI = -100;  
  3.   
  4. /** Anything better than or equal to this will show the max bars. */  
  5. private static final int MAX_RSSI = -55;  
  6.   
  7. /** 
  8.  * Calculates the level of the signal. This should be used any time a signal 
  9.  * is being shown. 
  10.  * 
  11.  * @param rssi The power of the signal measured in RSSI. 
  12.  * @param numLevels The number of levels to consider in the calculated 
  13.  *            level. 
  14.  * @return A level of the signal, given in the range of 0 to numLevels-1 
  15.  *         (both inclusive). 
  16.  */  
  17. public static int calculateSignalLevel(int rssi, int numLevels) {  
  18.     /* in general, numLevels is 4  */  
  19.     if (rssi <= MIN_RSSI) {  
  20.         return 0;  
  21.     } else if (rssi >= MAX_RSSI) {  
  22.         return numLevels - 1;  
  23.     } else {  
  24.         float inputRange = (MAX_RSSI - MIN_RSSI);  
  25.         float outputRange = (numLevels - 1);  
  26.           
  27.         return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);  
  28.     }  
  29. }  
    /** Anything worse than or equal to this will show 0 bars. */
    private static final int MIN_RSSI = -100;

    /** Anything better than or equal to this will show the max bars. */
    private static final int MAX_RSSI = -55;

    /**
     * Calculates the level of the signal. This should be used any time a signal
     * is being shown.
     *
     * @param rssi The power of the signal measured in RSSI.
     * @param numLevels The number of levels to consider in the calculated
     *            level.
     * @return A level of the signal, given in the range of 0 to numLevels-1
     *         (both inclusive).
     */
    public static int calculateSignalLevel(int rssi, int numLevels) {
        /* in general, numLevels is 4  */
        if (rssi <= MIN_RSSI) {
            return 0;
        } else if (rssi >= MAX_RSSI) {
            return numLevels - 1;
        } else {
            float inputRange = (MAX_RSSI - MIN_RSSI);
            float outputRange = (numLevels - 1);
            
            return (int)((float)(rssi - MIN_RSSI) * outputRange / inputRange);
        }
    }

 

2. WiFi Command流程

 

3. wpa_supplicant启动流程

4. WifiService启动流程

5. SIGNAL_POLL调用流程

 
  1. eloop_run->..  
  2. wpa_supplicant_ctrl_iface_receive-> //接收并处理来自framework的command   
  3. wpa_supplicant_ctrl_iface_process-> (SIGNAL_POLL)  
  4. wpa_supplicant_signal_poll->  
  5. wpa_drv_signal_poll  (struct wpa_supplicant *wpa_s,struct wpa_signal_info *si)->  
  6. wpa_driver_signal_poll  (void *priv, struct wpa_signal_info *si)->  
  7.     wpa_driver_wext_driver_cmd(priv, RSSI_CMD, buf, sizeof(buf))或  //driver_cmd_wext.c   
  8.     wpa_driver_wext_driver_cmd(priv, LINKSPEED_CMD, buf, sizeof(buf))->  
  9.         
  10.       struct iwreq iwr;  
  11.       iwr.u.data.pointer = buf;  
  12.       iwr.u.data.length = buf_len;  
  13.       ioctl(drv->ioctl_sock, SIOCSIWPRIV, &iwr);  
  14.       在Kernel中对应函数:  
  15.       cfg80211_wext_setpriv (wext-compat.c)  
  16.       RSSI_CMD:  
  17.       cfg80211_wireless_stats (获取当前已连接AP的信号强度等信息)  
  18.   
  19.      对于上面的LINKSPEED_CMD,如果ioctl不成功,则调用ioctl(drv->ioctl_sock, SIOCGIWRATE, &wrq)  
  20.      在Kernel中对应函数:  
  21.      cfg80211_wext_giwrate (获取当前已连接AP的发送速度)  
  22.   
  23.      //每个AP对应的信息        
  24.      struct station_info {  
  25.     u32 filled;  
  26.     u32 connected_time;  
  27.     u32 inactive_time;  
  28.     u32 rx_bytes;  
  29.     u32 tx_bytes;  
  30.     u16 llid;  
  31.     u16 plid;  
  32.     u8 plink_state;  
  33.     s8 signal;  //信号强度   
  34.     s8 signal_avg;  
  35.     struct rate_info txrate;  //发送速度   
  36.     struct rate_info rxrate;  
  37.     u32 rx_packets;  
  38.     u32 tx_packets;  
  39.     u32 tx_retries;  
  40.     u32 tx_failed;  
  41.     u32 rx_dropped_misc;  
  42.     struct sta_bss_parameters bss_param;  
  43.   
  44.     int generation;  
  45.     };  
eloop_run->..
wpa_supplicant_ctrl_iface_receive-> //接收并处理来自framework的command
wpa_supplicant_ctrl_iface_process-> (SIGNAL_POLL)
wpa_supplicant_signal_poll->
wpa_drv_signal_poll  (struct wpa_supplicant *wpa_s,struct wpa_signal_info *si)->
wpa_driver_signal_poll  (void *priv, struct wpa_signal_info *si)->
    wpa_driver_wext_driver_cmd(priv, RSSI_CMD, buf, sizeof(buf))或  //driver_cmd_wext.c
    wpa_driver_wext_driver_cmd(priv, LINKSPEED_CMD, buf, sizeof(buf))->
      
      struct iwreq iwr;
      iwr.u.data.pointer = buf;
      iwr.u.data.length = buf_len;
      ioctl(drv->ioctl_sock, SIOCSIWPRIV, &iwr);
      在Kernel中对应函数:
      cfg80211_wext_setpriv (wext-compat.c)
      RSSI_CMD:
      cfg80211_wireless_stats (获取当前已连接AP的信号强度等信息)

     对于上面的LINKSPEED_CMD,如果ioctl不成功,则调用ioctl(drv->ioctl_sock, SIOCGIWRATE, &wrq)
     在Kernel中对应函数:
     cfg80211_wext_giwrate (获取当前已连接AP的发送速度)

     //每个AP对应的信息     
     struct station_info {
	u32 filled;
	u32 connected_time;
	u32 inactive_time;
	u32 rx_bytes;
	u32 tx_bytes;
	u16 llid;
	u16 plid;
	u8 plink_state;
	s8 signal;  //信号强度
	s8 signal_avg;
	struct rate_info txrate;  //发送速度
	struct rate_info rxrate;
	u32 rx_packets;
	u32 tx_packets;
	u32 tx_retries;
	u32 tx_failed;
	u32 rx_dropped_misc;
	struct sta_bss_parameters bss_param;

	int generation;
    };
### 使用 `wpa_supplicant` 控制 WiFi 启动和关闭 在 Linux 系统中,可以通过一系列命令配合 `wpa_supplicant` 来实现 WiFi启动与关闭功能。以下是具体的命令及相关说明: #### 开启 WiFi 要启用 WiFi 接口(假设接口名为 `wlan0`),可以按照以下方法操作: 1. **确保无线网卡处于活动状态** 需要先确认无线网卡已打开并正常工作。这一步可通过 `ifconfig` 或更现代的 `ip` 命令完成。 ```bash ip link set wlan0 up ``` 上述命令会将 `wlan0` 设置为激活状态[^1]。 2. **启动 wpa_supplicant 进程** 如果尚未运行 `wpa_supplicant`,则需要手动启动该进程,并指定所需的配置文件路径以及驱动程序类型。 ```bash wpa_supplicant -B -D nl80211 -i wlan0 -c /etc/wpa_supplicant.conf ``` 参数解释如下: - `-B`: 表示以后台模式运行。 - `-D nl80211`: 指定使用的驱动程序为 `nl80211`[^1]。 - `-i wlan0`: 明确作用于哪个无线网络设备(这里是 `wlan0`)。 - `-c /etc/wpa_supplicant.conf`: 提供存储有 Wi-Fi 凭证和其他设置的配置文件位置[^4]。 3. **验证连接状态** 可以借助 `wpa_cli` 工具查询当前连接状况或者尝试重新关联到目标 AP。 ```bash wpa_cli status ``` #### 关闭 WiFi 当不再需要使用 WiFi 功能时,应采取措施停止相关服务并将硬件置于低功耗模式下保存电量资源。 1. **终止 wpa_supplicant 服务实例** 找到对应进程中止即可断开现有链接同时释放占用端口等系统资源。 ```bash killall wpa_supplicant ``` 2. **禁用无线适配器** 将之前提到过的无线网卡置入非活跃态从而彻底屏蔽信号收发行为达到节能目的。 ```bash ip link set wlan0 down ``` 以上即完成了基于命令行方式下的WiFi开关切换流程描述[^1][^2]. ```python def toggle_wifi(state, interface=&#39;wlan0&#39;, config_path=&#39;/etc/wpa_supplicant.conf&#39;): import os if state.lower() == &#39;on&#39;: # Enable Interface and Start Service os.system(f&#39;ip link set {interface} up&#39;) os.system(f&#39;wpa_supplicant -B -D nl80211 -i {interface} -c {config_path}&#39;) elif state.lower() == &#39;off&#39;: # Stop Service and Disable Interface os.system(&#39;killall wpa_supplicant&#39;) os.system(f&#39;ip link set {interface} down&#39;) toggle_wifi(&#39;on&#39;) # Example usage to turn on WiFi ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值