赞
踩
android12-release
SensorService.java
实际操作Native层SensorService.cpp
;对应Bn服务端。
应用获取Context.SEARCH_SERVICE
实际操作Native层SensorManager.cpp
;对应Bp客户端。
frameworks/base/services/core/java/com/android/server/sensors/SensorService.java
public SensorService(Context ctx) { super(ctx); synchronized (mLock) { mSensorServiceStart = SystemServerInitThreadPool.submit(() -> { TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog(); traceLog.traceBegin(START_NATIVE_SENSOR_SERVICE); long ptr = startSensorServiceNative(new ProximityListenerDelegate()); synchronized (mLock) { mPtr = ptr; } traceLog.traceEnd(); }, START_NATIVE_SENSOR_SERVICE); } } @Override public void onStart() { LocalServices.addService(SensorManagerInternal.class, new LocalService()); }
frameworks/base/core/java/android/hardware/SystemSensorManager.java
/** {@hide} */ public SystemSensorManager(Context context, Looper mainLooper) { synchronized (sLock) { if (!sNativeClassInited) { sNativeClassInited = true; nativeClassInit(); } } mMainLooper = mainLooper; ApplicationInfo appInfo = context.getApplicationInfo(); mTargetSdkLevel = appInfo.targetSdkVersion; mContext = context; mNativeInstance = nativeCreate(context.getOpPackageName()); mIsPackageDebuggable = (0 != (appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE)); PackageManager packageManager = context.getPackageManager(); mHasHighSamplingRateSensorsPermission = (PERMISSION_GRANTED == packageManager.checkPermission( HIGH_SAMPLING_RATE_SENSORS_PERMISSION, appInfo.packageName)); // initialize the sensor list for (int index = 0;; ++index) { Sensor sensor = new Sensor(); if (!nativeGetSensorAtIndex(mNativeInstance, sensor, index)) break; mFullSensorsList.add(sensor); mHandleToSensor.put(sensor.getHandle(), sensor); } }
ServiceManager
中添加"sensorservice"
frameworks/base/services/core/jni/com_android_server_sensor_SensorService.cpp
NativeSensorService::NativeSensorService(JNIEnv* env, jobject listener)
: mProximityActiveListenerDelegate(new ProximityActiveListenerDelegate(env, listener)) {
if (base::GetBoolProperty("system_init.startsensorservice", true)) {
sp<IServiceManager> sm(defaultServiceManager());
mService = new SensorService();
sm->addService(String16(SensorService::getServiceName()), mService,
false /* allowIsolated */, IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
}
}
SensorService
继承BnSensorServer
,实现onTransact
方法一边Bp客户端调用,Code标签
对应调用方法,Parcel& data
参数,Parcel* reply
接收返回值,flags = 0
存在默认值。
frameworks/native/libs/sensor/include/sensor/ISensorServer.h
class BnSensorServer : public BnInterface<ISensorServer>
{
public:
virtual status_t shellCommand(int in, int out, int err,
Vector<String16>& args) = 0;
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
frameworks/native/libs/sensor/ISensorServer.cpp
enum { GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION, CREATE_SENSOR_EVENT_CONNECTION, ENABLE_DATA_INJECTION, GET_DYNAMIC_SENSOR_LIST, CREATE_SENSOR_DIRECT_CONNECTION, SET_OPERATION_PARAMETER, }; status_t BnSensorServer::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case GET_SENSOR_LIST: { CHECK_INTERFACE(ISensorServer, data, reply); const String16& opPackageName = data.readString16(); Vector<Sensor> v(getSensorList(opPackageName)); size_t n = v.size(); reply->writeUint32(static_cast<uint32_t>(n)); for (size_t i = 0; i < n; i++) { reply->write(v[i]); } return NO_ERROR; } case CREATE_SENSOR_EVENT_CONNECTION: { CHECK_INTERFACE(ISensorServer, data, reply); String8 packageName = data.readString8(); int32_t mode = data.readInt32(); const String16& opPackageName = data.readString16(); const String16& attributionTag = data.readString16(); sp<ISensorEventConnection> connection(createSensorEventConnection(packageName, mode, opPackageName, attributionTag)); reply->writeStrongBinder(IInterface::asBinder(connection)); return NO_ERROR; } case ENABLE_DATA_INJECTION: { CHECK_INTERFACE(ISensorServer, data, reply); int32_t ret = isDataInjectionEnabled(); reply->writeInt32(static_cast<int32_t>(ret)); return NO_ERROR; } case GET_DYNAMIC_SENSOR_LIST: { CHECK_INTERFACE(ISensorServer, data, reply); const String16& opPackageName = data.readString16(); Vector<Sensor> v(getDynamicSensorList(opPackageName)); size_t n = v.size(); reply->writeUint32(static_cast<uint32_t>(n)); for (size_t i = 0; i < n; i++) { reply->write(v[i]); } return NO_ERROR; } case CREATE_SENSOR_DIRECT_CONNECTION: { CHECK_INTERFACE(ISensorServer, data, reply); const String16& opPackageName = data.readString16(); uint32_t size = data.readUint32(); int32_t type = data.readInt32(); int32_t format = data.readInt32(); native_handle_t *resource = data.readNativeHandle(); // Avoid a crash in native_handle_close if resource is nullptr if (resource == nullptr) { return BAD_VALUE; } sp<ISensorEventConnection> ch = createSensorDirectConnection(opPackageName, size, type, format, resource); native_handle_close(resource); native_handle_delete(resource); reply->writeStrongBinder(IInterface::asBinder(ch)); return NO_ERROR; } case SET_OPERATION_PARAMETER: { CHECK_INTERFACE(ISensorServer, data, reply); int32_t handle; int32_t type; Vector<float> floats; Vector<int32_t> ints; uint32_t count; handle = data.readInt32(); type = data.readInt32(); count = data.readUint32(); if (count > (data.dataAvail() / sizeof(float))) { return BAD_VALUE; } floats.resize(count); for (auto &i : floats) { i = data.readFloat(); } count = data.readUint32(); if (count > (data.dataAvail() / sizeof(int32_t))) { return BAD_VALUE; } ints.resize(count); for (auto &i : ints) { i = data.readInt32(); } int32_t ret = setOperationParameter(handle, type, floats, ints); reply->writeInt32(ret); return NO_ERROR; } case SHELL_COMMAND_TRANSACTION: { int in = data.readFileDescriptor(); int out = data.readFileDescriptor(); int err = data.readFileDescriptor(); int argc = data.readInt32(); Vector<String16> args; for (int i = 0; i < argc && data.dataAvail() > 0; i++) { args.add(data.readString16()); } sp<IBinder> unusedCallback; sp<IResultReceiver> resultReceiver; status_t status; if ((status = data.readNullableStrongBinder(&unusedCallback)) != NO_ERROR) { return status; } if ((status = data.readNullableStrongBinder(&resultReceiver)) != NO_ERROR) { return status; } status = shellCommand(in, out, err, args); if (resultReceiver != nullptr) { resultReceiver->send(status); } return NO_ERROR; } } return BBinder::onTransact(code, data, reply, flags); }
应用获取
Context.SEARCH_SERVICE
实际操作Native层SensorManager.cpp
;对应Bp客户端获取查看 Binder系列2-ServiceManager 中模板interface_cast
=> 模板::android::sp<Bp##INTERFACE>::make(obj)
Bp端中方法如getSensorList()
,通过remote()->transact(GET_SENSOR_LIST, data, &reply);
最终调用到Bn端的onTransact
方法,GET_SENSOR_LIST
作为Code标签
调用对应方法,Parcel& data
参数,Parcel* reply
接收返回值,flags = 0
有默认值。
frameworks/native/libs/sensor/include/sensor/ISensorServer.h
frameworks/native/libs/sensor/ISensorServer.cpp
enum { GET_SENSOR_LIST = IBinder::FIRST_CALL_TRANSACTION, CREATE_SENSOR_EVENT_CONNECTION, ENABLE_DATA_INJECTION, GET_DYNAMIC_SENSOR_LIST, CREATE_SENSOR_DIRECT_CONNECTION, SET_OPERATION_PARAMETER, }; class BpSensorServer : public BpInterface<ISensorServer> { public: explicit BpSensorServer(const sp<IBinder>& impl) : BpInterface<ISensorServer>(impl) { } virtual ~BpSensorServer(); virtual Vector<Sensor> getSensorList(const String16& opPackageName) { Parcel data, reply; data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); data.writeString16(opPackageName); remote()->transact(GET_SENSOR_LIST, data, &reply); Sensor s; Vector<Sensor> v; uint32_t n = reply.readUint32(); v.setCapacity(n); while (n) { n--; reply.read(s); v.add(s); } return v; } virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName) { Parcel data, reply; data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); data.writeString16(opPackageName); remote()->transact(GET_DYNAMIC_SENSOR_LIST, data, &reply); Sensor s; Vector<Sensor> v; uint32_t n = reply.readUint32(); v.setCapacity(n); while (n) { n--; reply.read(s); v.add(s); } return v; } virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName, int mode, const String16& opPackageName, const String16& attributionTag) { Parcel data, reply; data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); data.writeString8(packageName); data.writeInt32(mode); data.writeString16(opPackageName); data.writeString16(attributionTag); remote()->transact(CREATE_SENSOR_EVENT_CONNECTION, data, &reply); return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); } virtual int isDataInjectionEnabled() { Parcel data, reply; data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); remote()->transact(ENABLE_DATA_INJECTION, data, &reply); return reply.readInt32(); } virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName, uint32_t size, int32_t type, int32_t format, const native_handle_t *resource) { Parcel data, reply; data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); data.writeString16(opPackageName); data.writeUint32(size); data.writeInt32(type); data.writeInt32(format); data.writeNativeHandle(resource); remote()->transact(CREATE_SENSOR_DIRECT_CONNECTION, data, &reply); return interface_cast<ISensorEventConnection>(reply.readStrongBinder()); } virtual int setOperationParameter(int32_t handle, int32_t type, const Vector<float> &floats, const Vector<int32_t> &ints) { Parcel data, reply; data.writeInterfaceToken(ISensorServer::getInterfaceDescriptor()); data.writeInt32(handle); data.writeInt32(type); data.writeUint32(static_cast<uint32_t>(floats.size())); for (auto i : floats) { data.writeFloat(i); } data.writeUint32(static_cast<uint32_t>(ints.size())); for (auto i : ints) { data.writeInt32(i); } remote()->transact(SET_OPERATION_PARAMETER, data, &reply); return reply.readInt32(); } }; // Out-of-line virtual method definition to trigger vtable emission in this // translation unit (see clang warning -Wweak-vtables) BpSensorServer::~BpSensorServer() {}
对应Bp客户端获取查看 Binder系列2-ServiceManager 中
模板interface_cast
=> 模板::android::sp<Bp##INTERFACE>::make(obj)
frameworks/native/libs/binder/include/binder/IServiceManager.h
template<typename INTERFACE>
status_t getService(const String16& name, sp<INTERFACE>* outService)
{
const sp<IServiceManager> sm = defaultServiceManager();
if (sm != nullptr) {
*outService = interface_cast<INTERFACE>(sm->getService(name));
if ((*outService) != nullptr) return NO_ERROR;
}
return NAME_NOT_FOUND;
}
frameworks/native/libs/binder/include/binder/IInterface.h
//... ... template<typename INTERFACE> inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) { return INTERFACE::asInterface(obj); } //... ... static ::android::sp<I##INTERFACE> asInterface( \ const ::android::sp<::android::IBinder>& obj); \ //... ... ::android::sp<I##INTERFACE> I##INTERFACE::asInterface( \ const ::android::sp<::android::IBinder>& obj) \ { \ ::android::sp<I##INTERFACE> intr; \ if (obj != nullptr) { \ intr = ::android::sp<I##INTERFACE>::cast( \ obj->queryLocalInterface(I##INTERFACE::descriptor)); \ if (intr == nullptr) { \ intr = ::android::sp<Bp##INTERFACE>::make(obj); \ } \ } \ return intr; \ } \
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。