Framework基礎:傳感器服務的通信流程(binder與socket)

么多.png

傳感器服務,是通過binder進行業(yè)務控制,使用socket進行傳感器感應數(shù)據(jù)傳輸。

客戶端是/frameworks/native/libs/gui/SensorManager.cpp
服務端是/frameworks/native/services/sensorservice/SensorService.cpp

native binder套路圖.png
通道建立.png

我們走一下使能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)用到jni接口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上了下電而已?。?/h3>
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)設計(6)--- Native Service
Android BitTube進程間數(shù)據(jù)傳遞
Android6.0 Sensor架構(gòu)和問題分析

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容