Android 12 S Native Service的创建流程

 系列文章

Android 12 S ServiceManager原理

Android 12 S Native Service的创建流程

Android 12 S Binder原理之BpBinder,BnBinder以及IInterface介绍

Android 12 S HIDL Service创建流程

Android 12 S 自定义Hal服务selinux权限添加

Android 12 S 自定义Native服务selinux权限添加

Android 12 S java服务调用native服务

Android 12 S 自定义native服务访问java服务


一般创建自定义系统服务的代码目录为:

frameworks/native/services/

新增目录名:customize

在customize下新增目录include用于存放头文件。

在customize下目录结构如下:
CustomizeManagerService.cpp
ICustomizeManagerService.cpp
include/CustomizeManagerService.h
include/ICustomizeManagerService.h

Android.bp

在include/ICustomizeManagerService.h中新增如下内容:

#ifndef ICustomizeManagerService_H
#define ICustomizeManagerService_H
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

namespace android
{
//IInterface:定义的接口,在bp以及bn都要按这个函数去实现,不允许出现多余的入参或者不同的返回值
class ICustomizeManagerService: public IInterface
{
public:
    //关联server端的实现类
    DECLARE_META_INTERFACE(CustomizeManagerService);
    //纯虚函数
    virtual int customeize() = 0;
};

//BnInterface: Server 端类
class BnCustomizeManagerService : public BnInterface
{
public:
    virtual status_t onTransact(uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags = 0);
};

}; //namespace android

#endif

在ICustomizeManagerService.cpp中新增如下内容:

#undef LOG_TAG
#define LOG_TAG "ICustomizeManagerService"
#include 
#include 
#include "ICustomizeManagerService.h"
#include 
#include 
#include 
#include 
#include 

namespace android {
    enum{
        CREATE = IBinder::FIRST_CALL_TRANSACTION,
        CUSTOMIZE,
    };
#define NATIVESERVICE_NAME "customizemanagerservice"
//Client 端
class BpCustomizeManagerService : public BpInterface
{
public:
    explicit BpCustomizeManagerService(const sp& impl)
      : BpInterface (impl)
    {
        ALOGD("create Service \n");
    }

    int customeize(){
        //client和server端的参数或者返回值传递通过Parcel
        Parcel data, reply;
        data.writeInterfaceToken(ICustomizeManagerService::getInterfaceDescriptor());
        remote()->transact(CUSTOMIZE, data, &reply);
        return reply.readInt32();
    }
};

IMPLEMENT_META_INTERFACE(CustomizeManagerService, NATIVESERVICE_NAME);
//Server 端
status_t BnCustomizeManagerService ::onTransact(uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags){
    switch (code)
    {
        case CUSTOMIZE:
        {
            CHECK_INTERFACE(ICustomizeManagerService, data, reply);
            //此处方法必须和Interface中保持一致,实现是在CustomizeManagerService.cpp中实现的
            int ret = customeize();
            reply->writeInt32(ret);
            return NO_ERROR;
        }
        default:
            return BBinder::onTransact(code, data, reply, flags);
     };
}
};  //namespace android


include/CustomizeManagerService.h中新增如下内容

#ifndef CustomizeManagerService_H
#define CustomizeManagerService_H
#include 
#include 
#include "ICustomizeManagerService.h"
#include 
#include 
#include 

namespace android {
using namespace android;

class CustomizeManagerService : public BnCustomizeManagerService  {
public:

    CustomizeManagerService();
    ~CustomizeManagerService();
    int customeize();
};
};
#endif

CustomizeManagerService.cpp中新增如下内容

#undef LOG_TAG
#define LOG_TAG "CustomizeManagerService"
#include 
#include 
#include 
#include 
#include 
#include "CustomizeManagerService .h"
#include 
#include 
#include 
#include 

namespace android {
CustomizeManagerService ::CustomizeManagerService () {
  
}

CustomizeManagerService ::~CustomizeManagerService () {
}


int CustomizeManagerService ::customeize() {
    int ret = 100;
    return ret;
}

};

其中需注意的是IInterface中的纯虚方法,在Bn和Bp去实现的时候,要一模一样

需注意的是Android 12自定义native service时,需要在以下文件中添加自己的服务名字才可以,否则会报错:

error: static_assert failed due to requirement 'internal::allowedManualInterface("xxx")' : Manually written binder interfaces are considered error prone and frequently have bugs. The preferred way to add interfaces is to define an .aidl file to auto-generate the interface. If an interface must be manually written, add its name to the whitelist."

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

在kManualInterfaces中增加自己的服务名即可:


namespace internal {
constexpr const char* const kManualInterfaces[] = {
  "android.app.IActivityManager",
  "android.app.IUidObserver",
  "android.drm.IDrm",
......
+ "customizemanagerserver",
  nullptr,
};

Android.bp

这个里面多了很多访问hal服务以及Ahandler相关的代码

cc_library_shared {
    name: "libcustomizemanagerserver",

    srcs: [
        "CustomizeManagerService.cpp",
        "ICustomizeManagerService.cpp",
    ],

    shared_libs: [
        "libcutils",
        "libbinder",
        "liblog",
        "libutils",
        "libhidltransport",
        "libhidlbase",
        "[email protected]",
        "libstagefright_foundation",
        "libstagefright",
        "libmedia",
        "libmedia_codeclist",
    ],

    cflags: [
        "-Werror",
        "-Wno-error=deprecated-declarations",
        "-Wno-unused-parameter",
        "-Wall",
        "-Wunused-variable",
        "-Wswitch",
    ],
    include_dirs:[
        "frameworks/native/services/customizemanagerserver/include",
        "frameworks/av/media/libstagefright/foundation/include",
        "frameworks/av/media/libstagefright/foundation/include/media/stagefright/foundation",
    ],
    header_libs: [
        "libaudiohal_headers",
    ],
}

cc_binary {
    name: "customizemanagerserver",
    init_rc: ["customizemanagerserver.rc"],

    srcs: [
        "main_customizemanagerserver.cpp",
    ],
    shared_libs: [
        "libcutils",
        "libutils",
        "liblog",
        "libbinder",
        "libcustomizemanagerserver",
        "libhidlbase",
        "libbase",
        "libhidltransport",
        "[email protected]",
        "libstagefright_foundation",
        "libstagefright",
        "libmedia",
        "libmedia_codeclist",
    ],

    include_dirs:[
        "frameworks/native/services/customizemanagerserver/include",
        "frameworks/av/media/libstagefright/foundation/include",
        "frameworks/av/media/libstagefright/foundation/include/media/stagefright/foundation",
    ],
    cflags: [
        "-Werror",
        "-Wno-error=deprecated-declarations",
        "-Wno-unused-parameter",
        "-Wall",
    ],
    header_libs: [
        "libaudiohal_headers",
    ],
}

customizemanagerserver.rc如下:

service customizemanagerserver  /system/bin/customizemanagerserver
    class main
    user system
    group system
    oneshot
on property:sys.boot_completed=1
    start customizemanagerserver

你可能感兴趣的:(Binder,Android,Framework,Native,android,c++)