Android: Add a service

来自个人维基
2023年3月15日 (三) 11:28free6d1823讨论 | 贡献的版本

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索
  1. 1. aosp/hardware/interfaces/<myservice>/1.0
  • Android.bp
hidl_interface {
    name: "android.hardware.<myservice>@1.0",
    root: "android.hardware",
    vndk: {
        enabled: false,
    },
    srcs: [
        "types.hal",
        "I<Myservice>.hal",
    ],
    interfaces: [
        "android.hidl.base@1.0",
    ],
    gen_java: false,
}
  • I<Myservice>.hal
package android.hardware.<myservice>@1.0;
 
interface I<Myservice> {
    func1() generates (<Myservice>Error error);
    func2(handle h, Param1 p1) generates (<MyService>Error error);
};
  • types.hal
package android.hardware.<myservie>@1.0;
 
enum <MyService>Error : uint8_t {
    <MYSERVICE>_SUCCESS = 0,
};
struct Param1 {
    ....
};
  • default/
 - Android.bp
 - simple project source
 - service.cpp
#include <sched.h>
 
#include <android/hardware/<myservice>/1.0/I<Myservice>.h>
#include <binder/ProcessState.h>
#include <hidl/HidlTransportSupport.h>
 
using android::hardware::myservice::V1_0::I<Myservice>;
 
int main() {
    // the conventional HAL might start binder services
    android::ProcessState::initWithDriver("/dev/vndbinder");
    android::ProcessState::self()->setThreadPoolMaxThreadCount(4);
    android::ProcessState::self()->startThreadPool();
 
    // same as SF main thread
    struct sched_param param = {0};
    param.sched_priority = 2;
    if (sched_setscheduler(0, SCHED_FIFO | SCHED_RESET_ON_FORK, &param) != 0) {
        ALOGE("Couldn't set SCHED_FIFO: %d", errno);
    }
 
    android::hardware::configureRpcThreadpool(4, true /* will join */);
 
    android::sp<IMyservice> service = new android::hardware::<myservice>::implementation::<Myservice>();
    if (service == nullptr) {
        ALOGE("failed to create device!");
        return -1;
    }
 
    if (service ->func1() != <Myservice>Error::<MYSERVICE>_SUCCESS) {
        ALOGE("failed to init device");
        return -1;
    }
 
    if (service->registerAsService() != android::NO_ERROR) {
        ALOGE("failed to register service");
        return 1;
    }
 
    android::hardware::joinRpcThreadpool();
 
    ALOGE("service is terminating");
    return 1;
}