HAL binder是Android O(8.0)专门用于HAL(Hardware Abstract Layer)层(native)进程与其clients之间的通信机制(clients可以是native进程,也可以是Java Framework进程)。 HAL binder替代了早先使用的socket通信,其kernel层实际是基于原有的binder驱动,但为了配合Client与Server之间的数据传输,需要使用特定的中间层HIDL来进行接口与数据的转换。那么,相对之前的HAL通信方式(socket),基于HIDL的HAL通信有什么优势了?从系统架构的角度,HIDL为客户端与服务端提供了清晰的接口;从效率的角度,binder IPC实际在传输数据上只有一次拷贝,而socket实际传输需要两次数据拷贝。
service hwservicemanager /system/bin/hwservicemanager user system disabled group system readproc critical onrestart setprop hwservicemanager.ready false onrestart class_restart hal onrestart class_restart early_hal writepid /dev/cpuset/system-background/tasks classanimation
/** * Manages all the hidl hals on a device. * * Terminology: * Package: "android.hidl.manager" * Major version: "1" * Minor version: "0" * Version: "1.0" * Interface name: "IServiceManager" * Fully-qualified interface name: "android.hidl.manager@1.0::IServiceManager" * Instance name: "manager" * Fully-qualified instance name: "android.hidl.manager@1.0::IServiceManager/manager" */ interface IServiceManager {
// Retrieve an existing service that supports the requested version. get(string fqName, string name) generates (interface service);
/** * Register a service. The service manager must retrieve the (inherited) * interfaces that this service implements, and register them along with * the service. * */ add(string name, interface service) generates (bool success);
// Get the transport of a service. getTransport(string fqName, string name) generates (Transport transport);
// List all registered services. Must be sorted. list() generates (vec<string> fqInstanceNames);
// List all instances of a particular service. Must be sorted. listByInterface(string fqName) generates (vec<string> instanceNames);
/** * Register for service notifications for a particular service. Must support * multiple registrations. */ registerForNotifications(string fqName, string name, IServiceNotification callback) generates (bool success);
...
/** * When the passthrough service manager returns a service via * get(string, string), it must dispatch a registerPassthroughClient call * to the binderized service manager to indicate the current process has * called get(). Binderized service manager must record this PID, which can * be retrieved via debugDump. */ // 注册直通式客户端 registerPassthroughClient(string fqName, string name); };
// Tell IPCThreadState we're the service manager sp<BnHwServiceManager> service = newBnHwServiceManager(manager); IPCThreadState::self()->setTheContextObject(service); // Then tell binder kernel ioctl(binder_fd, BINDER_SET_CONTEXT_MGR, 0);