(一百四十九)Android P WificondControl的setupInterfaceForClientMode

本文主要是介绍(一百四十九)Android P WificondControl的setupInterfaceForClientMode,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.回顾

之前在(一百四十八)Android P wifi启动过程中的sta interface创建 中梳理了sta interface的创建流程,之后还有

           if (mWificondControl.setupInterfaceForClientMode(iface.name) == null) {Log.e(TAG, "Failed to setup iface in wificond on " + iface);teardownInterface(iface.name);mWifiMetrics.incrementNumSetupClientInterfaceFailureDueToWificond();return null;}if (!mSupplicantStaIfaceHal.setupIface(iface.name)) {Log.e(TAG, "Failed to setup iface in supplicant on " + iface);teardownInterface(iface.name);mWifiMetrics.incrementNumSetupClientInterfaceFailureDueToSupplicant();return null;}

先梳理下WificondControl的setupInterfaceForClientMode

 

2.流程梳理

WificondControl

    /*** Setup interface for client mode via wificond.* @return An IClientInterface as wificond client interface binder handler.* Returns null on failure.*/public IClientInterface setupInterfaceForClientMode(@NonNull String ifaceName) {Log.d(TAG, "Setting up interface for client mode");if (!retrieveWificondAndRegisterForDeath()) {return null;}IClientInterface clientInterface = null;try {clientInterface = mWificond.createClientInterface(ifaceName);} catch (RemoteException e1) {Log.e(TAG, "Failed to get IClientInterface due to remote exception");return null;}if (clientInterface == null) {Log.e(TAG, "Could not get IClientInterface instance from wificond");return null;}Binder.allowBlocking(clientInterface.asBinder());// Refresh HandlersmClientInterfaces.put(ifaceName, clientInterface);try {IWifiScannerImpl wificondScanner = clientInterface.getWifiScannerImpl();if (wificondScanner == null) {Log.e(TAG, "Failed to get WificondScannerImpl");return null;}mWificondScanners.put(ifaceName, wificondScanner);Binder.allowBlocking(wificondScanner.asBinder());ScanEventHandler scanEventHandler = new ScanEventHandler(ifaceName);mScanEventHandlers.put(ifaceName,  scanEventHandler);wificondScanner.subscribeScanEvents(scanEventHandler);PnoScanEventHandler pnoScanEventHandler = new PnoScanEventHandler(ifaceName);mPnoScanEventHandlers.put(ifaceName,  pnoScanEventHandler);wificondScanner.subscribePnoScanEvents(pnoScanEventHandler);} catch (RemoteException e) {Log.e(TAG, "Failed to refresh wificond scanner due to remote exception");}return clientInterface;}

system/connectivity/wificond/server.cpp

Status Server::createClientInterface(const std::string& iface_name,sp<IClientInterface>* created_interface) {InterfaceInfo interface;if (!SetupInterface(iface_name, &interface)) {return Status::ok();  // Logging was done internally}unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(wiphy_index_,interface.name,interface.index,interface.mac_address,if_tool_.get(),netlink_utils_,scan_utils_));*created_interface = client_interface->GetBinder();BroadcastClientInterfaceReady(client_interface->GetBinder());client_interfaces_[iface_name] = std::move(client_interface);return Status::ok();
}bool Server::SetupInterface(const std::string& iface_name,InterfaceInfo* interface) {if (!RefreshWiphyIndex()) {return false;}netlink_utils_->SubscribeRegDomainChange(wiphy_index_,std::bind(&Server::OnRegDomainChanged,this,_1));interfaces_.clear();if (!netlink_utils_->GetInterfaces(wiphy_index_, &interfaces_)) {LOG(ERROR) << "Failed to get interfaces info from kernel";return false;}for (const auto& iface : interfaces_) {if (iface.name == iface_name) {*interface = iface;return true;}}LOG(ERROR) << "No usable interface found";return false;
}

这边会从kernel获取所有的interface,然后对照name看有没有对应的,如果有的话,返回一个对应的iface.

看下interfaces是何时初始化的

netlink_utils_

bool NetlinkUtils::GetInterfaces(uint32_t wiphy_index,vector<InterfaceInfo>* interface_info) {NL80211Packet get_interfaces(netlink_manager_->GetFamilyId(),NL80211_CMD_GET_INTERFACE,netlink_manager_->GetSequenceNumber(),getpid());get_interfaces.AddFlag(NLM_F_DUMP);get_interfaces.AddAttribute(NL80211Attr<uint32_t>(NL80211_ATTR_WIPHY, wiphy_index));vector<unique_ptr<const NL80211Packet>> response;if (!netlink_manager_->SendMessageAndGetResponses(get_interfaces, &response)) {LOG(ERROR) << "NL80211_CMD_GET_INTERFACE dump failed";return false;}if (response.empty()) {LOG(ERROR) << "No interface is found";return false;}for (auto& packet : response) {if (packet->GetMessageType() == NLMSG_ERROR) {LOG(ERROR) << "Receive ERROR message: "<< strerror(packet->GetErrorCode());return false;}if (packet->GetMessageType() != netlink_manager_->GetFamilyId()) {LOG(ERROR) << "Wrong message type for new interface message: "<< packet->GetMessageType();return false;}if (packet->GetCommand() != NL80211_CMD_NEW_INTERFACE) {LOG(ERROR) << "Wrong command in response to "<< "an interface dump request: "<< static_cast<int>(packet->GetCommand());return false;}// In some situations, it has been observed that the kernel tells us// about a pseudo interface that does not have a real netdev.  In this// case, responses will have a NL80211_ATTR_WDEV, and not the expected// IFNAME/IFINDEX. In this case we just skip these pseudo interfaces.uint32_t if_index;if (!packet->GetAttributeValue(NL80211_ATTR_IFINDEX, &if_index)) {LOG(DEBUG) << "Failed to get interface index";continue;}// Today we don't check NL80211_ATTR_IFTYPE because at this point of time// driver always reports that interface is in STATION mode. Even when we// are asking interfaces infomation on behalf of tethering, it is still so// because hostapd is supposed to set interface to AP mode later.string if_name;if (!packet->GetAttributeValue(NL80211_ATTR_IFNAME, &if_name)) {LOG(WARNING) << "Failed to get interface name";continue;}vector<uint8_t> if_mac_addr;if (!packet->GetAttributeValue(NL80211_ATTR_MAC, &if_mac_addr)) {LOG(WARNING) << "Failed to get interface mac address";continue;}interface_info->emplace_back(if_index, if_name, if_mac_addr);}return true;
}

通过netlink_manager_发送包,然后依次解析返回回来的包得到if_index/if_name/if_mac_addr压栈到interface_info,最后封装成一个client_interface

这流程很熟悉,之前在(五十七)Android O WiFi的扫描流程梳理续——梳理java与c++之间的aidl-cpp通信 梳理过

这边相当于搭好了从上到kernel的通信道路,这时sta iface应该已经确实创建好了,感觉中间哪里看漏了一步。

 

3.总结

WificondControl的setupInterfaceForClientMode看起来就是在wificond初始化一下interface的通信,获取可以和kernel通信的接口,方便下发scan命令

 

这篇关于(一百四十九)Android P WificondControl的setupInterfaceForClientMode的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/511398

相关文章

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl