android 8.1 java直接通过ServiceManager.getService 获取cpp service机制

android8.1 java可以通过ServiceManager可获取native底Service, 节省了一些java层和native层service的重写与jni代码。使用了代码自动生成aidl机制

例如ServiceManager.getService("media.camera")

1. "media.camera" Service创建与发布

frameworks/av/camera/cameraserver/main_cameraserver.cpp

int main(int argc __unused, char** argv __unused)
{
    signal(SIGPIPE, SIG_IGN);


    // Set 3 threads for HIDL calls
    hardware::configureRpcThreadpool(3, /*willjoin*/ false);


    sp proc(ProcessState::self());
    sp sm = defaultServiceManager();
    ALOGI("ServiceManager: %p", sm.get());
    CameraService::instantiate();   //创建并发布service
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();
}

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

static void instantiate() { publish(); }


static status_t publish(bool allowIsolated = false) {
    sp sm(defaultServiceManager());
    return sm->addService(
            String16(SERVICE::getServiceName()),    //CameraService::getServiceName为“media.camera”
            new SERVICE(), allowIsolated);  // new Service类, 并加入ServiceManager
}

cpp类通过继承BinderService, 通过调用instantiate 或 publish方法,创建自身类并加入ServiceManager中管理,

CameraService.cpp继承关系如下:

class CameraService :
    public BinderService,
    public virtual ::android::hardware::BnCameraService,
    public virtual IBinder::DeathRecipient,
    public camera_module_callbacks_t,
    public virtual CameraProviderManager::StatusListener
{

2. java中获取CameraService

    frameworks/base/core/java/android/hardware/camera2/CameraManager.java

IBinder cameraServiceBinder = ServiceManager.getService(CAMERA_SERVICE_BINDER_NAME);  //CAMERA_SERVICE_BINDER_NAME为
                                                                                        "media.camera"
 
ICameraService cameraService = ICameraService.Stub.asInterface(cameraServiceBinder);

    ServiceManager.getService返回的IBinder最后转换成ICameraService类型

    ICameraService类型,在java中通过aidl定义

    frameworks/av/camera/aidl/android/hardware/ICameraService.aidl

    android系统会根据ICameraService.aidl文件自动生成ICameraService.java,其中实现ICameraService.Stub和ICameraService.Proxy


    而CameraSerivce.cpp中没有看到ICameraService接口,只有其中继承binder server端接口为BnCameraService

    BnCameraService.h为系统自动生成, 在out目录下

 4 #include 
 5 #include 
 6 
 7 namespace android {
 8 
 9 namespace hardware {
 10
 11 class BnCameraService : public ::android::BnInterface {
 12 public:
 13 ::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags = 0) override;
 14 };  // class BnCameraService

    可以看出其继承了ICameraService, 所以在java中可以转为ICameraService类型。

3. cpp中查找ICameraService类定义

  在cpp中没有找到ICameraService定义, 根据上面include, 有一个ICarmeraService.h源码中没有,所以这个文件也是系统自动生成的,在out目录下

    但是在native代码中没有找到对ICameraService类型的定义,或者类似java中aidl这样的文件,唯一可能就是native中也是用的java中定义的接口来自动生成代码

frameworks/av/camera/Android.bp中包含了java的ICameraService.aidl文件
28    srcs: [
29        // AIDL files for camera interfaces
30        // The headers for these interfaces will be available to any modules that
31        // include libcamera_client, at the path "aidl/package/path/BnFoo.h"
32        "aidl/android/hardware/ICameraService.aidl",    //包含ICameraService.aidl
33        "aidl/android/hardware/ICameraServiceListener.aidl",
34        "aidl/android/hardware/ICameraServiceProxy.aidl",
35        "aidl/android/hardware/camera2/ICameraDeviceCallbacks.aidl",
36        "aidl/android/hardware/camera2/ICameraDeviceUser.aidl",
37
38
39        // Source for camera interface parcelables, and manually-written interfaces
40        "Camera.cpp",
41        "CameraMetadata.cpp",
42        "CameraParameters.cpp",
43        "CaptureResult.cpp",
44        "CameraParameters2.cpp",
45        "ICamera.cpp",
46        "ICameraClient.cpp",
47        "ICameraRecordingProxy.cpp",
48        "ICameraRecordingProxyListener.cpp",
49        "camera2/CaptureRequest.cpp",
50        "camera2/OutputConfiguration.cpp",
51        "camera2/SubmitInfo.cpp",
52        "CameraBase.cpp",
53        "CameraUtils.cpp",
54        "VendorTagDescriptor.cpp",
55    ],



 
  
 
  
 
  

你可能感兴趣的:(android 8.1 java直接通过ServiceManager.getService 获取cpp service机制)