傳感器服務弯淘,是通過binder進行業(yè)務控制榕吼,使用socket進行傳感器感應數(shù)據(jù)傳輸薄料。
客戶端是/frameworks/native/libs/gui/SensorManager.cpp
服務端是/frameworks/native/services/sensorservice/SensorService.cpp
我們走一下使能sensor上電的過程吧狂丝!
/frameworks/base/core/java/android/hardware/SystemSensorManager.java
首先進入enableSensor,其中nSensorEventQueue是jni層創(chuàng)建SensorEventQueue的返回值童本,標識一個queue真屯。
private int enableSensor(
Sensor sensor, int rateUs, int maxBatchReportLatencyUs) {
if (nSensorEventQueue == 0) throw new NullPointerException();
if (sensor == null) throw new NullPointerException();
return nativeEnableSensor(nSensorEventQueue, sensor.getHandle(), rateUs,
maxBatchReportLatencyUs);
}
調(diào)用到j(luò)ni接口nativeEnableSensor
static jint nativeEnableSensor(JNIEnv *env, jclass clazz, jlong eventQ, jint handle, jint rate_us,
jint maxBatchReportLatency) {
sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ));
return receiver->getSensorEventQueue()->enableSensor(handle, rate_us, maxBatchReportLatency,
0);
}
進入receiver的getSensorEventQueue()->enableSensor,看看receiver的定義穷娱,他是在構(gòu)建native層的SensorEventQueue是創(chuàng)建的绑蔫,里面保存著隊列SensorEventQueue
class Receiver : public LooperCallback {
sp<SensorEventQueue> mSensorQueue;
sp<MessageQueue> mMessageQueue;
jobject mReceiverWeakGlobal;
jfloatArray mScratch;
public:
Receiver(const sp<SensorEventQueue>& sensorQueue,
const sp<MessageQueue>& messageQueue,
jobject receiverWeak, jfloatArray scratch) {
JNIEnv* env = AndroidRuntime::getJNIEnv();
mSensorQueue = sensorQueue;
mMessageQueue = messageQueue;
mReceiverWeakGlobal = env->NewGlobalRef(receiverWeak);
mScratch = (jfloatArray)env->NewGlobalRef(scratch);
}
~Receiver() {
JNIEnv* env = AndroidRuntime::getJNIEnv();
env->DeleteGlobalRef(mReceiverWeakGlobal);
env->DeleteGlobalRef(mScratch);
}
sp<SensorEventQueue> getSensorEventQueue() const {
return mSensorQueue;
}
/frameworks/native/libs/gui/SensorEventQueue.cpp
進而進入SensorEventQueue的enableSensor方法运沦,他是通過服務端與客戶端的連接SensorEventConnection來發(fā)送數(shù)據(jù)給服務端SensorService。
status_t SensorEventQueue::enableSensor(Sensor const* sensor) const {
return mSensorEventConnection->enableDisable(sensor->getHandle(), true, 0, 0, false);
}
SensorEventConnection也是基于binder架構(gòu)的配深,客戶端的SensorEventConnection其實是個代理携添,不是真正的SensorEventConnection,真正的SensorEventConnection在服務器端呢篓叶。
/frameworks/native/services/sensorservice/SensorService.cpp
sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName,
int requestedMode, const String16& opPackageName) {
uid_t uid = IPCThreadState::self()->getCallingUid();
sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName,
requestedMode == DATA_INJECTION, opPackageName));
//創(chuàng)建一個SensorEventConnection
return result;
}
//SensorEventConnection的構(gòu)造函數(shù)
SensorService::SensorEventConnection::SensorEventConnection(
const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode,
const String16& opPackageName)
: mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false),
mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL),
mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) {
mChannel = new BitTube(mService->mSocketBufferSize);
#if DEBUG_CONNECTIONS
mEventsReceived = mEventsSentFromCache = mEventsSent = 0;
mTotalAcksNeeded = mTotalAcksReceived = 0;
#endif
}
接下來會利用這個代理的SensorEventConnection構(gòu)造一個隊列SensorEventQueue烈掠,直接傳入代理SensorEventConnection來構(gòu)造隊列。
sp<SensorEventQueue> SensorManager::createEventQueue(String8 packageName, int mode) {
.......
sp<SensorEventQueue> queue;
queue = new SensorEventQueue(connection);
return queue;
}
真正的傳感器測量數(shù)據(jù)傳輸不在SensorEventConnection澜共,而是利用一個管道BitTube向叉,他用socket實現(xiàn)锥腻,便于大量數(shù)據(jù)傳輸嗦董,每個SensorEventConnection都配套一個BitTube,BitTube在SensorEventConnection的構(gòu)造函數(shù)中構(gòu)建了瘦黑。那這個東西在服務器端構(gòu)建了京革,如何傳給客戶端呢?是在SensorEventQueue的onFirstRef()函數(shù)中產(chǎn)生的,利用代理的SensorEventConnection來遠程調(diào)用getSensorChannel幸斥,最后生成本地的管道BitTube匹摇。
void SensorEventQueue::onFirstRef()
{
mSensorChannel = mSensorEventConnection->getSensorChannel();
}
/frameworks/native/libs/gui/ISensorEventConnection.cpp
在客戶端調(diào)用transact,發(fā)送GET_SENSOR_CHANNEL
//客戶端調(diào)用transact
virtual sp<BitTube> getSensorChannel() const
{
Parcel data, reply;
data.writeInterfaceToken(ISensorEventConnection::getInterfaceDescriptor());
remote()->transact(GET_SENSOR_CHANNEL, data, &reply);
return new BitTube(reply);
}
//服務器端處理消息
case GET_SENSOR_CHANNEL: {
CHECK_INTERFACE(ISensorEventConnection, data, reply);
sp<BitTube> channel(getSensorChannel());
channel->writeToParcel(reply);
return NO_ERROR;
}
最后甲葬,服務器和客戶端就利用這個管道BitTube進行通信了廊勃。
但是,然并卵经窖,使能上電并不需要傳輸傳感器數(shù)據(jù),最后會在服務器端SensorService上了下電而已F碌妗!
status_t SensorService::enable(const sp<SensorEventConnection>& connection,
int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags,
const String16& opPackageName)
{
......
BatteryService::enableSensor(connection->getUid(), handle);
return err;
.....
}
參考:
Service與Android系統(tǒng)設(shè)計(6)--- Native Service
Android BitTube進程間數(shù)據(jù)傳遞
Android6.0 Sensor架構(gòu)和問題分析