• Hidl Service - Java Service共享内存


    1. Hidl介绍

    2. Android组件之Service

    3. 通讯框架

    3.1 框架

    3.2 实现

    3.2.2.  Hidl 部分以binary形式存在.  关键文件如下:

    Android.bp:

    1. // FIXME: your file license if you have one
    2. cc_binary {
    3. name: "android.hardware.xxxx@1.0-service",
    4. init_rc: ["android.hardware.xxxx@1.0-service.rc"],
    5. vintf_fragments: ["android.hardware.xxxx@1.0-service.xml"],
    6. relative_install_path: "hw",
    7. vendor: true,
    8. srcs: [
    9. "xxxxHidl.cpp",
    10. "service.cpp",
    11. ],
    12. shared_libs: [
    13. "liblog",
    14. "libdl",
    15. "libutils",
    16. "libcutils",
    17. "libhardware",
    18. "libhidlbase",
    19. "libhidlmemory",
    20. "libhidltransport",
    21. "android.hidl.allocator@1.0",
    22. "android.hidl.memory@1.0",
    23. "android.hardware.xxxx@1.0",
    24. ],
    25. }

    android.hardware.xxxx@1.0-service.xml:

    1. <manifest version="1.0" type="device">
    2. <hal format="hidl">
    3. <name>android.hardware.xxxx</name>
    4. <transport>hwbinder</transport>
    5. <version>1.0</version>//版本
    6. <interface>
    7. <name>IxxxxHidl</name>//hidl接口名称
    8. <instance>default</instance>
    9. </interface>
    10. </hal>
    11. </manifest>

    android.hardware.xxxx@1.0-service.rc:

    1. service xxxx1_0 /vendor/bin/hw/android.hardware.xxxx@1.0-service
    2. class hal
    3. user system
    4. group system

    service.cpp:

    1. #define LOG_TAG "android.hardware.xxxx@1.0-service"
    2. #include <android/hardware/xxxx/1.0/IxxxxHidl.h>
    3. #include <hidl/LegacySupport.h>
    4. #include <hidl/HidlTransportSupport.h>
    5. #include "xxxxHidl.h"
    6. using android::hardware::xxxx::V1_0::IxxxxHidl;
    7. using android::hardware::xxxx::V1_0::implementation::xxxxHidl;
    8. //using android::hardware::defaultPassthroughServiceImplementation;
    9. using android::hardware::configureRpcThreadpool;
    10. using android::hardware::joinRpcThreadpool;
    11. using android::sp;
    12. using android::status_t;
    13. using android::OK;
    14. int main() {
    15. configureRpcThreadpool(3, true);//max thread number for request.
    16. sp<IxxxxHidl> service = new xxxxHidl;
    17. status_t status = service->registerAsService();//注册服务
    18. ALOGW_IF(status != OK, "Could not register xxxxHidl v1.0");
    19. ALOGD("Default service is ready.");
    20. joinRpcThreadpool();
    21. return 1;
    22. }

    3.2.3 Hidl共享内存

    1. namespace {
    2. // hidl数组转共享内存接口//
    3. // Moves the data from the vector into allocated shared memory,
    4. // emptying the vector.
    5. // It is assumed that the passed hidl_memory is a null object, so it's
    6. // not reset if the vector is empty.
    7. // The caller needs to keep the returned sp as long as
    8. // the data is needed.
    9. std::pair<bool, sp> moveVectorToMemory(hidl_vec* v, hidl_memory* mem) {
    10. sp memory;
    11. if (v->size() == 0) {
    12. return std::make_pair(true, memory);
    13. }
    14. sp ashmem = IAllocator::getService("ashmem");
    15. if (ashmem == 0) {
    16. ALOGE("Failed to retrieve ashmem allocator service");
    17. return std::make_pair(false, memory);
    18. }
    19. bool success = false;
    20. Return r = ashmem->allocate(v->size(), [&](bool s, const hidl_memory& m) {
    21. success = s;
    22. if (success) *mem = m;
    23. });
    24. if (r.isOk() && success) {
    25. memory = hardware::mapMemory(*mem);
    26. if (memory != 0) {
    27. memory->update();
    28. memcpy(memory->getPointer(), v->data(), v->size());
    29. memory->commit();
    30. v->resize(0);
    31. return std::make_pair(true, memory);
    32. } else {
    33. ALOGE("Failed to map allocated ashmem");
    34. }
    35. } else {
    36. ALOGE("Failed to allocate %llu bytes from ashmem", (unsigned long long)v->size());
    37. }
    38. return std::make_pair(false, memory);
    39. }
    40. }

    使用案例://moveVectorToMemory使用例子

    1. /*****************************************************************************
    2. function: ProcessData
    3. description: socket data callback workflow
    4. input: uint8_t*,uint32_t
    5. output: none
    6. return: 0 indicates success.
    7. -1 indicates failed.
    8. *****************************************************************************/
    9. uint32_t VmSocketClient::ProcessData(uint8_t *buf,uint32_t length)
    10. {
    11. //callback data//
    12. pthread_mutex_lock(&mLock_read);//lock//
    13. hidl_vec<uint8_t> rx_data;
    14. rx_data.setToExternal(buf, length);
    15. mHidlBuffer.length = rx_data.size();
    16. moveVectorToMemory(&rx_data,&(mHidlBuffer.data));
    17. if(mCallback!=NULL)
    18. mCallback->processData(mHidlBuffer);
    19. pthread_mutex_unlock(&mLock_read);//unlock//
    20. return 0;
    21. }

    callback接口://IxxxxHidlCallback.hal

    1. interface IxxxxHidlCallback {
    2. oneway processData(xxxx_buffer buf);
    3. oneway notify(uint32_t msg);
    4. };

    type.hal 定义:

    1. struct xxxx_buffer {
    2. uint64_t id;
    3. uint32_t length;
    4. memory data; //内存
    5. };

    3.2.4 java共享内存:

    1. import android.os.HidlMemory;
    2. import android.os.HidlMemoryUtil;
    3. import android.hardware.xxxx.V1_0.IxxxxHidlCallback;
    4. import android.hardware.xxxx.V1_0.xxxx_buffer;
    5. //xxxx hidl callback
    6. private class HidlCallback extends IxxxxHidlCallback.Stub {
    7. private xxxxHidlManager mManager;
    8. HidlCallback(xxxxHidlManager manager) {
    9. this.mManager = manager;
    10. }
    11. @Override
    12. public void processData(Spicom_buffer buf)
    13. {
    14. //内存转Byte数组
    15. byte[] buffer = HidlMemoryUtil.hidlMemoryToByteArray(buf.data);
    16. int length = buf.length;
    17. }
    18. @Override
    19. public void notify(int msg)
    20. {
    21. }
    22. }
    23. //end

  • 相关阅读:
    [附源码]计算机毕业设计影院管理系统Springboot程序
    Axios
    【中间件】redis部分理论
    FFmpeg源码分析:AVFilterGraph与AVFilter
    ROS-Noetic安装与环境搭建-Ubuntu20
    Windows 10系统下载----生成iso镜像文件
    【NLP】使用递归神经网络对序列数据进行建模 (Pytorch)
    Linux基础:文件权限详细说明(全)
    猜数字--
    阿里云记录保存
  • 原文地址:https://blog.csdn.net/liaochaoyun/article/details/126932511