SensorService中Binder案例

news2024/11/17 15:30:51

SensorService中Binder案例

  • 1、FWK实际操作在Native层
  • 2、Native层中代码实现Bn/Bp端
    • 2.1 代码实现Bn端
    • 2.2 代码实现Bp端
      • 2.2.1 模板interface_cast

android12-release


1、FWK实际操作在Native层

SensorService.java实际操作Native层SensorService.cpp;对应Bn服务端。
应用获取Context.SEARCH_SERVICE实际操作Native层SensorManager.cpp;对应Bp客户端。
在这里插入图片描述

frameworks/base/services/core/java/com/android/server/sensors/SensorService.java

public SensorService(Context ctx) {
    super(ctx);
    synchronized (mLock) {
        mSensorServiceStart = SystemServerInitThreadPool.submit(() -> {
            TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog();
            traceLog.traceBegin(START_NATIVE_SENSOR_SERVICE);
            long ptr = startSensorServiceNative(new ProximityListenerDelegate());
            synchronized (mLock) {
                mPtr = ptr;
            }
            traceLog.traceEnd();
        }, START_NATIVE_SENSOR_SERVICE);
    }
}

@Override
public void onStart() {
    LocalServices.addService(SensorManagerInternal.class, new LocalService());
}

frameworks/base/core/java/android/hardware/SystemSensorManager.java

/** {@hide} */
public SystemSensorManager(Context context, Looper mainLooper) {
    synchronized (sLock) {
        if (!sNativeClassInited) {
            sNativeClassInited = true;
            nativeClassInit();
        }
    }

    mMainLooper = mainLooper;
    ApplicationInfo appInfo = context.getApplicationInfo();
    mTargetSdkLevel = appInfo.targetSdkVersion;
    mContext = context;
    mNativeInstance = nativeCreate(context.getOpPackageName());
    mIsPackageDebuggable = (0 != (appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE));
    PackageManager packageManager = context.getPackageManager();
    mHasHighSamplingRateSensorsPermission =
            (PERMISSION_GRANTED == packageManager.checkPermission(
                    HIGH_SAMPLING_RATE_SENSORS_PERMISSION,
                    appInfo.packageName));

    // initialize the sensor list
    for (int index = 0;; ++index) {
        Sensor sensor = new Sensor();
        if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break;
        mFullSensorsList.add(sensor);
        mHandleToSensor.put(sensor.getHandle(), sensor);
    }
}

2、Native层中代码实现Bn/Bp端

ServiceManager中添加"sensorservice"

frameworks/base/services/core/jni/com_android_server_sensor_SensorService.cpp

NativeSensorService::NativeSensorService(JNIEnv* env, jobject listener)
      : mProximityActiveListenerDelegate(new ProximityActiveListenerDelegate(env, listener)) {
    if (base::GetBoolProperty("system_init.startsensorservice", true)) {
        sp<IServiceManager> sm(defaultServiceManager());
        mService = new SensorService();
        sm->addService(String16(SensorService::getServiceName()), mService,
                       false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
    }
}

2.1 代码实现Bn端

SensorService继承BnSensorServer,实现onTransact方法一边Bp客户端调用,Code标签对应调用方法,Parcel& data参数,Parcel* reply接收返回值,flags = 0存在默认值。

frameworks/native/libs/sensor/include/sensor/ISensorServer.h

class BnSensorServer : public BnInterface<ISensorServer>
{
public:
    virtual status_t shellCommand(int in, int out, int err,
                                  Vector<String16>& args) = 0;

    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
};

frameworks/native/libs/sensor/ISensorServer.cpp

enum {
    GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
    CREATE_SENSOR_EVENT_CONNECTION,
    ENABLE_DATA_INJECTION,
    GET_DYNAMIC_SENSOR_LIST,
    CREATE_SENSOR_DIRECT_CONNECTION,
    SET_OPERATION_PARAMETER,
};


status_t BnSensorServer::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case GET_SENSOR_LIST: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            const String16& opPackageName = data.readString16();
            Vector<Sensor> v(getSensorList(opPackageName));
            size_t n = v.size();
            reply->writeUint32(static_cast<uint32_t>(n));
            for (size_t i = 0; i < n; i++) {
                reply->write(v[i]);
            }
            return NO_ERROR;
        }
        case CREATE_SENSOR_EVENT_CONNECTION: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            String8 packageName = data.readString8();
            int32_t mode = data.readInt32();
            const String16& opPackageName = data.readString16();
            const String16& attributionTag = data.readString16();
            sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode,
                    opPackageName, attributionTag));
            reply->writeStrongBinder(IInterface::asBinder(connection));
            return NO_ERROR;
        }
        case ENABLE_DATA_INJECTION: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            int32_t ret = isDataInjectionEnabled();
            reply->writeInt32(static_cast<int32_t>(ret));
            return NO_ERROR;
        }
        case GET_DYNAMIC_SENSOR_LIST: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            const String16& opPackageName = data.readString16();
            Vector<Sensor> v(getDynamicSensorList(opPackageName));
            size_t n = v.size();
            reply->writeUint32(static_cast<uint32_t>(n));
            for (size_t i = 0; i < n; i++) {
                reply->write(v[i]);
            }
            return NO_ERROR;
        }
        case CREATE_SENSOR_DIRECT_CONNECTION: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            const String16& opPackageName = data.readString16();
            uint32_t size = data.readUint32();
            int32_t type = data.readInt32();
            int32_t format = data.readInt32();
            native_handle_t *resource = data.readNativeHandle();
            // Avoid a crash in native_handle_close if resource is nullptr
            if (resource == nullptr) {
                return BAD_VALUE;
            }
            sp<ISensorEventConnection> ch =
                    createSensorDirectConnection(opPackageName, size, type, format, resource);
            native_handle_close(resource);
            native_handle_delete(resource);
            reply->writeStrongBinder(IInterface::asBinder(ch));
            return NO_ERROR;
        }
        case SET_OPERATION_PARAMETER: {
            CHECK_INTERFACE(ISensorServer, data, reply);
            int32_t handle;
            int32_t type;
            Vector<float> floats;
            Vector<int32_t> ints;
            uint32_t count;

            handle = data.readInt32();
            type = data.readInt32();

            count = data.readUint32();
            if (count > (data.dataAvail() / sizeof(float))) {
              return BAD_VALUE;
            }
            floats.resize(count);
            for (auto &i : floats) {
                i = data.readFloat();
            }

            count = data.readUint32();
            if (count > (data.dataAvail() / sizeof(int32_t))) {
              return BAD_VALUE;
            }
            ints.resize(count);
            for (auto &i : ints) {
                i = data.readInt32();
            }

            int32_t ret = setOperationParameter(handle, type, floats, ints);
            reply->writeInt32(ret);
            return NO_ERROR;
        }
        case SHELL_COMMAND_TRANSACTION: {
            int in = data.readFileDescriptor();
            int out = data.readFileDescriptor();
            int err = data.readFileDescriptor();
            int argc = data.readInt32();
            Vector<String16> args;
            for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
               args.add(data.readString16());
            }
            sp<IBinder> unusedCallback;
            sp<IResultReceiver> resultReceiver;
            status_t status;
            if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) {
                return status;
            }
            if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) {
                return status;
            }
            status = shellCommand(in, out, err, args);
            if (resultReceiver != nullptr) {
                resultReceiver->send(status);
            }
            return NO_ERROR;
        }
    }
    return BBinder::onTransact(code, data, reply, flags);
}

2.2 代码实现Bp端

应用获取Context.SEARCH_SERVICE实际操作Native层SensorManager.cpp;对应Bp客户端获取查看 Binder系列2-ServiceManager 中模板interface_cast => 模板::android::sp<Bp##INTERFACE>::make(obj)

Bp端中方法如getSensorList(),通过remote()->transact(GET_SENSOR_LIST, data, &reply);最终调用到Bn端的onTransact方法,GET_SENSOR_LIST作为Code标签调用对应方法,Parcel& data参数,Parcel* reply接收返回值,flags = 0有默认值。

frameworks/native/libs/sensor/include/sensor/ISensorServer.h
frameworks/native/libs/sensor/ISensorServer.cpp

enum {
    GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION,
    CREATE_SENSOR_EVENT_CONNECTION,
    ENABLE_DATA_INJECTION,
    GET_DYNAMIC_SENSOR_LIST,
    CREATE_SENSOR_DIRECT_CONNECTION,
    SET_OPERATION_PARAMETER,
};

class BpSensorServer : public BpInterface<ISensorServer>
{
public:
    explicit BpSensorServer(const sp<IBinder>& impl)
        : BpInterface<ISensorServer>(impl)
    {
    }

    virtual ~BpSensorServer();

    virtual Vector<Sensor> getSensorList(const String16& opPackageName)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        data.writeString16(opPackageName);
        remote()->transact(GET_SENSOR_LIST, data, &reply);
        Sensor s;
        Vector<Sensor> v;
        uint32_t n = reply.readUint32();
        v.setCapacity(n);
        while (n) {
            n--;
            reply.read(s);
            v.add(s);
        }
        return v;
    }

    virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        data.writeString16(opPackageName);
        remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply);
        Sensor s;
        Vector<Sensor> v;
        uint32_t n = reply.readUint32();
        v.setCapacity(n);
        while (n) {
            n--;
            reply.read(s);
            v.add(s);
        }
        return v;
    }

    virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
             int mode, const String16& opPackageName, const String16& attributionTag)
    {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        data.writeString8(packageName);
        data.writeInt32(mode);
        data.writeString16(opPackageName);
        data.writeString16(attributionTag);
        remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply);
        return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
    }

    virtual int isDataInjectionEnabled() {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        remote()->transact(ENABLE_DATA_INJECTION, data, &reply);
        return reply.readInt32();
    }

    virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
            uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        data.writeString16(opPackageName);
        data.writeUint32(size);
        data.writeInt32(type);
        data.writeInt32(format);
        data.writeNativeHandle(resource);
        remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply);
        return interface_cast<ISensorEventConnection>(reply.readStrongBinder());
    }

    virtual int setOperationParameter(int32_t handle, int32_t type,
                                      const Vector<float> &floats,
                                      const Vector<int32_t> &ints) {
        Parcel data, reply;
        data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor());
        data.writeInt32(handle);
        data.writeInt32(type);
        data.writeUint32(static_cast<uint32_t>(floats.size()));
        for (auto i : floats) {
            data.writeFloat(i);
        }
        data.writeUint32(static_cast<uint32_t>(ints.size()));
        for (auto i : ints) {
            data.writeInt32(i);
        }
        remote()->transact(SET_OPERATION_PARAMETER, data, &reply);
        return reply.readInt32();
    }
};

// Out-of-line virtual method definition to trigger vtable emission in this
// translation unit (see clang warning -Wweak-vtables)
BpSensorServer::~BpSensorServer() {}

2.2.1 模板interface_cast

对应Bp客户端获取查看 Binder系列2-ServiceManager 中模板interface_cast => 模板::android::sp<Bp##INTERFACE>::make(obj)

frameworks/native/libs/binder/include/binder/IServiceManager.h

template<typename INTERFACE>
status_t getService(const String16& name, sp<INTERFACE>* outService)
{
    const sp<IServiceManager> sm = defaultServiceManager();
    if (sm != nullptr) {
        *outService = interface_cast<INTERFACE>(sm->getService(name));
        if ((*outService) != nullptr) return NO_ERROR;
    }
    return NAME_NOT_FOUND;
}

frameworks/native/libs/binder/include/binder/IInterface.h

//... ...
template<typename INTERFACE>
inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
{
    return INTERFACE::asInterface(obj);
}
//... ...
    static ::android::sp<I##INTERFACE> asInterface(                     \
            const ::android::sp<::android::IBinder>& obj);              \
//... ...
    ::android::sp<I##INTERFACE> I##INTERFACE::asInterface(              \
            const ::android::sp<::android::IBinder>& obj)               \
    {                                                                   \
        ::android::sp<I##INTERFACE> intr;                               \
        if (obj != nullptr) {                                           \
            intr = ::android::sp<I##INTERFACE>::cast(                   \
                obj->queryLocalInterface(I##INTERFACE::descriptor));    \
            if (intr == nullptr) {                                      \
                intr = ::android::sp<Bp##INTERFACE>::make(obj);         \
            }                                                           \
        }                                                               \
        return intr;                                                    \
    }                                                                   \

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/920574.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

人机界面通过RJ45口无线连接多台PLC

人机界面是系统和用户之间进行交互和信息交换的媒介&#xff0c;它实现信息的内部形式与人类可以接受形式之间的转换。人机界面产品由硬件和软件两部分组成&#xff0c;硬件部分包括处理器、显示单元、输入单元、通讯接口、数据存贮单元等&#xff0c;HMI软件一般分为两部分&am…

Ensp+Wireshark+VirtualBox+WinPcap

软件下载 [名称]&#xff1a;Ensp及辅助程序 [大小]&#xff1a;830.65MB [语言]&#xff1a;Chinese [安装环境]&#xff1a;Win7/Win8/Win10 [下载链接]&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1KbypgxAGQy07ijSAj3SvsQ 提取码&#xff1a;ly88 软件介…

秋招算法面经集合 | 华为、百度、腾讯、美团等

面试锦囊之面经分享系列&#xff0c;持续更新中 欢迎后台回复"面试"加入讨论组交流噢 写在前面 秋招告一段落&#xff0c;整理文件的时候发现之前记录的面经问题&#xff0c;主要是秋招前期的一些面试&#xff0c;后期由于实习比较忙没花时间整理。希望自己的面经可…

文件/文件夹加密:Newsoftwares Folder Lock 7.9.0 Crack

Newsoftwares Folder Lock 7.9文件夹锁 版本7 防弹数据加密 - 在几秒钟内锁定文件夹 - 即时加密文件 - 密码保护 USB/外部驱动器 - 粉碎并永久删除文件等等...... 视窗 向量 受到数百万人的信赖 82,283,016次下载并且还在增加中... 什么为什么如何 奖项常问问题特征丢失登记感…

BaiqiSoft MstHtmlEditor for .NET Crack

BaiqiSoft MstHtmlEditor for .NET Crack BaiqiSoft MstHtmlEditor获取.NET for win表单被认为是一个可以被用户轻松灵活地集成到C#、VB.NET甚至WPF软件中的元素。负责编辑的控制器&#xff0c;用于.NET Win Forms的MstHtmlEditor&#xff0c;允许用户和开发人员&#xff0c;甚…

专升本的开发历程

这个专升本项目是最近一个搞教育的朋友委托我的&#xff0c;从域名、服务器、设计开发、一直到项目上线都是我团队一手包办的。和大家浅聊一下&#xff1a; “专升本”小程序提供了报考查询、升本资讯、招生院校和考试报名等功能&#xff0c;为考生提供了更加全面和便捷的服务…

标签打印小工具 选择图片打印,按实际尺寸打印。可旋转图片

您可以尝试使用以下标签打印工具&#xff1a; 柯尼卡美能达标签打印机&#xff1a;功能齐全、易于使用的打印机&#xff0c;支持各种标签尺寸和类型。 赛门铁克标签打印机&#xff1a;高速打印、可靠性强的打印机&#xff0c;支持多种操作系统和软件。 齐柏林标签打印机&…

Code Lab - 2

pip install torch-scatter -f https://pytorch-geometric.com/whl/torch-1.10.2cu102.html pip install torch-sparse -f https://pytorch-geometric.com/whl/torch-1.10.2cu102.html pip install torch-geometric pip install ogb 1. PyG Datasets PyG有两个类&#xff0c;用…

资金借道股票ETF狂买1200亿

近日A股市场调整&#xff0c;资金借道股票ETF再现“越跌越买&#xff0c;大跌大买&#xff01;” 昨天的股市出现V字反转&#xff0c;资金借道股票ETF同样“抄底”汹涌&#xff0c;单日净买入资金超过50亿元&#xff0c;今年8月份以来已经狂买接近1200亿元。其中&#xff0c;沪…

ClickHouse进阶(一):ClickHouse 使用场景及分布式集群安装

进入正文前&#xff0c;感谢宝子们订阅专题、点赞、评论、收藏&#xff01;关注IT贫道&#xff0c;获取高质量博客内容&#xff01; &#x1f3e1;个人主页&#xff1a;含各种IT体系技术&#xff0c;IT贫道_Apache Doris,大数据OLAP体系技术栈,Kerberos安全认证-CSDN博客 &…

error while loading shared libraries错误的原因及解決方法——通用解决办法,错误加载xxxx共享库(.so)

在linux下整合log4cpp日志框架时&#xff0c;出现了下面的错误&#xff1a;当加载共享库时出错&#xff0c;找不到共享库 XXXX [roothecs-207177 cworkspace]# ./Log4cppTest ./Log4cppTest: error while loading shared libraries: liblog4cpp.so.5: cannot open shared obj…

svn下载

Download | VisualSVN for Visual Studio svn下载

MySQL数据库——函数-字符串函数、数值函数、日期函数、流程函数

目录 字符串函数 常用函数 练习 数值函数 常用函数 练习 日期函数 常用函数 练习 流程函数 常用函数 练习 函数 是指一段可以直接被另一段程序调用的程序或代码。 MySQL内置函数&#xff0c;主要分为四类&#xff1a; 字符串函数数值函数日期函数流程函数 字符串函…

java+springboot+mysql农业园区管理系统

项目介绍&#xff1a; 使用javaspringbootmysql开发的农业园区管理系统&#xff0c;系统包含超级管理员、管理员、用户角色&#xff0c;功能如下&#xff1a; 超级管理员&#xff1a;管理员管理&#xff1b;用户管理&#xff1b;土地管理&#xff08;租赁&#xff09;&#x…

PATH系统环境变量配置教程【图文步骤】

开发Java程序&#xff0c;需要使用JDK提供的开发工具(比如javac.exe、java.exe等命令)&#xff0c;而这些工具在JDK的安装目录的 bin目录下&#xff0c;如果不配置环境变量&#xff0c;那么这些命令只可以在该目录下执行。我们不可能把所有的java文件都放到JDK 的bin目录下&…

学习JAVA打卡第三十九天

字符串与基本数据的相互转化 Java.lang包中的Integer类调用其类方法public static int parseInt&#xff08;string s&#xff09;可以将由“数字”字符组成的字符序列如“876”&#xff0c;转化成int型数据&#xff0c;例如&#xff1a; int x&#xff1b; string s“876”&a…

如何使用CSS实现一个自适应两栏布局,其中一栏固定宽度,另一栏自适应宽度?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 使用Float属性⭐ 使用Flexbox布局⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅&#xff01;这个专栏是为那些对Web开发感…

Spring练习30---用户列表的展示(下)

1、得到数据之后&#xff0c;我进行封装 2、关键这个方法 3、方法实现一下 4、然后找到Dao的实现&#xff0c;给他实现一下 5、模板查几个表 6、根据userid 查role Id 7、根据他再去查role那张表&#xff0c;两张表以上 8、后面那一块的意思是role表的role Id等于中间表的谁 9…

JWT令牌的介绍

目录 一、什么是JWT 二、JWT令牌和Cookie客户端、Session服务端对比 三、特点与注意事项 四、使用场景 优点&#xff1a; 五、结构组成 一、什么是JWT JWT&#xff08;JSON Web Token&#xff09;是一种用于在网络应用间传递信息的开放标准&#xff08;RFC 7519&#x…

postman接口自动化测试框架实战!

什么是自动化测试 把人对软件的测试行为转化为由机器执行测试行为的一种实践。 例如GUI自动化测试&#xff0c;模拟人去操作软件界面&#xff0c;把人从简单重复的劳动中解放出来。 本质是用代码去测试另一段代码&#xff0c;属于一种软件开发工作&#xff0c;已经开发完成的用…