前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android HIDL机制学习

Android HIDL机制学习

作者头像
用户7557625
发布2020-07-15 11:10:08
2.1K0
发布2020-07-15 11:10:08
举报
前言:

在Android O之前,HAL是一个个的.so库,通过dlopen来进行打开,库和framework位于同一个进程。如下图所示:

在这里插入图片描述
在这里插入图片描述

在Android O之后,framework和hal运行于不同的进程,所有的HAL采用新的HIDL技术来完成。

在这里插入图片描述
在这里插入图片描述
一、HIDL简单介绍

HIDL是Android8.0新出的一个技能,以service和client的方式实现hal接口,目的是想使Android系统和BSP解绑,使系统升级更加方便。HIDL的使用方法一般是先提供.hal文件,然后使用hidl-gen工具生成 框架源文件和Android.bp编译工具文件,之后填充生成的源文件和定制Android.bp编译文件 官方介绍

二、以WiFi获取 MacAddress 为例看一下谷歌的实现

这是一个完整的 HIDL 接口的实现: /frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiNative.java

代码语言:javascript
复制
/**
 * Makes a callback to HIDL to getMacAddress from supplicant
 *
 * @param ifaceName Name of the interface.
 * @return string containing the MAC address, or null on a failed call
 */
public String getMacAddress(@NonNull String ifaceName) {
    return mSupplicantStaIfaceHal.getMacAddress(ifaceName);
}

/frameworks/opt/net/wifi/service/java/com/android/server/wifi/SupplicantStaIfaceHal.java

代码语言:javascript
复制
/**
 * Makes a callback to HIDL to getMacAddress from supplicant
 *
 * @param ifaceName Name of the interface.
 * @return string containing the MAC address, or null on a failed call
 */
public String getMacAddress(@NonNull String ifaceName) {
    synchronized (mLock) {
        final String methodStr = "getMacAddress";
        ISupplicantStaIface iface = checkSupplicantStaIfaceAndLogFailure(ifaceName, methodStr);
        if (iface == null) return null;
        Mutable<String> gotMac = new Mutable<>();
        try {
            iface.getMacAddress((SupplicantStatus status,
                    byte[/* 6 */] macAddr) -> {
                if (checkStatusAndLogFailure(status, methodStr)) {
                    gotMac.value = NativeUtil.macAddressFromByteArray(macAddr);
                }
            });
        } catch (RemoteException e) {
            handleRemoteException(e, methodStr);
        }
        return gotMac.value;
    }
}

/hardware/interfaces/wifi/supplicant/1.0/ISupplicantStaIface.hal

代码语言:javascript
复制
/**
 * Send driver command to get MAC address of the device.
 *
 * @return status Status of the operation.
 *         Possible status codes:
 *         |SupplicantStatusCode.SUCCESS|,
 *         |SupplicantStatusCode.FAILURE_UNKNOWN|,
 *         |SupplicantStatusCode.FAILURE_IFACE_INVALID|
 * @return macAddr MAC address of the device.
 */
getMacAddress()
    generates (SupplicantStatus status, MacAddress macAddr);

/external/wpa_supplicant_8/wpa_supplicant/hidl/1.0/sta_iface.cpp

代码语言:javascript
复制
Return<void> StaIface::getMacAddress(getMacAddress_cb _hidl_cb)
{
	return validateAndCall(
	    this, SupplicantStatusCode::FAILURE_IFACE_INVALID,
	    &StaIface::getMacAddressInternal, _hidl_cb);
}
代码语言:javascript
复制
StaIface::getMacAddressInternal()
{
	struct wpa_supplicant *wpa_s = retrieveIfacePtr();
	std::vector<char> cmd(
	    kGetMacAddress, kGetMacAddress + sizeof(kGetMacAddress));
	char driver_cmd_reply_buf[4096] = {};
	int ret = wpa_drv_driver_cmd(
	    wpa_s, cmd.data(), driver_cmd_reply_buf,
	    sizeof(driver_cmd_reply_buf));
	// Reply is of the format: "Macaddr = XX:XX:XX:XX:XX:XX"
	std::string reply_str = driver_cmd_reply_buf;
	if (ret < 0 || reply_str.empty() ||
	    reply_str.find("=") == std::string::npos) {
		return {{SupplicantStatusCode::FAILURE_UNKNOWN, ""}, {}};
	}
	// Remove all whitespace first and then split using the delimiter "=".
	reply_str.erase(
	    remove_if(reply_str.begin(), reply_str.end(), isspace),
	    reply_str.end());
	std::string mac_addr_str =
	    reply_str.substr(reply_str.find("=") + 1, reply_str.size());
	std::array<uint8_t, 6> mac_addr;
	if (hwaddr_aton(mac_addr_str.c_str(), mac_addr.data())) {
		return {{SupplicantStatusCode::FAILURE_UNKNOWN, ""}, {}};
	}
	return {{SupplicantStatusCode::SUCCESS, ""}, mac_addr};
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言:
  • 一、HIDL简单介绍
  • 二、以WiFi获取 MacAddress 为例看一下谷歌的实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档