Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cad495eb authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Allows message transactions to be used by any transaction owner

Today, reliable message transactions are limited by host endpoints. To extend its use for sessions, expand the host endpoint ID to an owner ID which can be defined by a new transaction type, guaranteeing uniqueness.

Bug: 381102453
Flag: android.chre.flags.offload_implementation
Test: Compile
Test: CHQTS reliable message test pass
Change-Id: I8dc68776e1391eaed33ca2d561041d4c3a9e5aeb
parent 24630204
Loading
Loading
Loading
Loading
+24 −11
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.location.contexthub;

import android.chre.flags.Flags;
import android.hardware.location.ContextHubTransaction;
import android.hardware.location.NanoAppState;

@@ -46,7 +47,11 @@ abstract class ContextHubServiceTransaction {
    /** The number of times the transaction has been started (start function called). */
    private int mNumCompletedStartCalls;

    private final short mHostEndpointId;
    /**
     * A unique identifier for the entity which owns this transaction, scoped by the transaction
     * type.
     */
    private final int mOwnerId;

    private boolean mIsComplete = false;

@@ -59,7 +64,7 @@ abstract class ContextHubServiceTransaction {
        mNextRetryTime = Long.MAX_VALUE;
        mTimeoutTime = Long.MAX_VALUE;
        mNumCompletedStartCalls = 0;
        mHostEndpointId = Short.MAX_VALUE;
        mOwnerId = Integer.MAX_VALUE;
    }

    ContextHubServiceTransaction(int id, int type, long nanoAppId,
@@ -72,11 +77,11 @@ abstract class ContextHubServiceTransaction {
        mNextRetryTime = Long.MAX_VALUE;
        mTimeoutTime = Long.MAX_VALUE;
        mNumCompletedStartCalls = 0;
        mHostEndpointId = Short.MAX_VALUE;
        mOwnerId = Integer.MAX_VALUE;
    }

    ContextHubServiceTransaction(int id, int type, String packageName,
            int messageSequenceNumber, short hostEndpointId) {
    ContextHubServiceTransaction(
            int id, int type, String packageName, int messageSequenceNumber, int ownerId) {
        mTransactionId = id;
        mTransactionType = type;
        mNanoAppId = Long.MAX_VALUE;
@@ -85,7 +90,7 @@ abstract class ContextHubServiceTransaction {
        mNextRetryTime = Long.MAX_VALUE;
        mTimeoutTime = Long.MAX_VALUE;
        mNumCompletedStartCalls = 0;
        mHostEndpointId = hostEndpointId;
        mOwnerId = ownerId;
    }

    /**
@@ -147,8 +152,15 @@ abstract class ContextHubServiceTransaction {
        return mNumCompletedStartCalls;
    }

    short getHostEndpointId() {
        return mHostEndpointId;
    /**
     * @return A unique identifier for the entity owning this transaction.
     */
    long getOwnerId() {
        if (Flags.offloadImplementation()) {
            return ((long) mTransactionType << 32) | (0x00000000FFFFFFFFL & mOwnerId);
        } else {
            return mOwnerId;
        }
    }

    /**
@@ -215,15 +227,16 @@ abstract class ContextHubServiceTransaction {
            out.append(", messageSequenceNumber = ");
            out.append(mMessageSequenceNumber);
        }
        if (mTransactionType == ContextHubTransaction.TYPE_RELIABLE_MESSAGE) {
        if (mTransactionType == ContextHubTransaction.TYPE_RELIABLE_MESSAGE
                || mTransactionType == ContextHubTransaction.TYPE_HUB_MESSAGE_REQUIRES_RESPONSE) {
            out.append(", nextRetryTime = ");
            out.append(mNextRetryTime);
            out.append(", timeoutTime = ");
            out.append(mTimeoutTime);
            out.append(", numCompletedStartCalls = ");
            out.append(mNumCompletedStartCalls);
            out.append(", hostEndpointId = ");
            out.append(mHostEndpointId);
            out.append(", ownerId = ");
            out.append(getOwnerId());
        }
        out.append(")");

+22 −17
Original line number Diff line number Diff line
@@ -84,9 +84,9 @@ import java.util.concurrent.atomic.AtomicInteger;
    protected final Map<Integer, ContextHubServiceTransaction> mReliableMessageTransactionMap =
            new HashMap<>();

    /** A set of host endpoint IDs that have an active pending transaction. */
    /** A set of IDs of transaction owners that have an active pending transaction. */
    @GuardedBy("mReliableMessageLock")
    protected final Set<Short> mReliableMessageHostEndpointIdActiveSet = new HashSet<>();
    protected final Set<Long> mReliableMessageOwnerIdActiveSet = new HashSet<>();

    protected final AtomicInteger mNextAvailableId = new AtomicInteger();

@@ -355,7 +355,7 @@ import java.util.concurrent.atomic.AtomicInteger;
    /**
     * Creates a transaction to send a reliable message.
     *
     * @param hostEndpointId      The ID of the host endpoint sending the message.
     * @param ownerId The ID of the transaction owner.
     * @param contextHubId The ID of the hub to send the message to.
     * @param message The message to send.
     * @param transactionCallback The callback of the transactions.
@@ -363,19 +363,24 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @return The generated transaction.
     */
    /* package */ ContextHubServiceTransaction createMessageTransaction(
            short hostEndpointId, int contextHubId, NanoAppMessage message,
            IContextHubTransactionCallback transactionCallback, String packageName) {
        return new ContextHubServiceTransaction(mNextAvailableId.getAndIncrement(),
                ContextHubTransaction.TYPE_RELIABLE_MESSAGE, packageName,
                mNextAvailableMessageSequenceNumber.getAndIncrement(), hostEndpointId) {
            short ownerId,
            int contextHubId,
            NanoAppMessage message,
            IContextHubTransactionCallback transactionCallback,
            String packageName) {
        return new ContextHubServiceTransaction(
                mNextAvailableId.getAndIncrement(),
                ContextHubTransaction.TYPE_RELIABLE_MESSAGE,
                packageName,
                mNextAvailableMessageSequenceNumber.getAndIncrement(),
                ownerId) {
            @Override
            /* package */ int onTransact() {
                try {
                    message.setIsReliable(/* isReliable= */ true);
                    message.setMessageSequenceNumber(getMessageSequenceNumber());

                    return mContextHubProxy.sendMessageToContextHub(hostEndpointId, contextHubId,
                            message);
                    return mContextHubProxy.sendMessageToContextHub(ownerId, contextHubId, message);
                } catch (RemoteException e) {
                    Log.e(TAG, "RemoteException while trying to send a reliable message", e);
                    return ContextHubTransaction.RESULT_FAILED_UNKNOWN;
@@ -766,10 +771,10 @@ import java.util.concurrent.atomic.AtomicInteger;
                        mReliableMessageTransactionMap.entrySet().iterator();
                while (iter.hasNext()) {
                    ContextHubServiceTransaction transaction = iter.next().getValue();
                    short hostEndpointId = transaction.getHostEndpointId();
                    long ownerId = transaction.getOwnerId();
                    int numCompletedStartCalls = transaction.getNumCompletedStartCalls();
                    if (numCompletedStartCalls == 0
                            && mReliableMessageHostEndpointIdActiveSet.contains(hostEndpointId)) {
                            && mReliableMessageOwnerIdActiveSet.contains(ownerId)) {
                        continue;
                    }

@@ -871,7 +876,7 @@ import java.util.concurrent.atomic.AtomicInteger;
        } else {
            iter.remove();
        }
        mReliableMessageHostEndpointIdActiveSet.remove(transaction.getHostEndpointId());
        mReliableMessageOwnerIdActiveSet.remove(transaction.getOwnerId());
    }

    /**
@@ -906,7 +911,7 @@ import java.util.concurrent.atomic.AtomicInteger;
            transaction.setTimeoutTime(now + RELIABLE_MESSAGE_TIMEOUT.toNanos());
        }
        transaction.setNumCompletedStartCalls(numCompletedStartCalls + 1);
        mReliableMessageHostEndpointIdActiveSet.add(transaction.getHostEndpointId());
        mReliableMessageOwnerIdActiveSet.add(transaction.getOwnerId());
    }

    private int toStatsTransactionResult(@ContextHubTransaction.Result int result) {
+4 −18
Original line number Diff line number Diff line
@@ -18,28 +18,14 @@ package com.android.server.location.contexthub;

import android.chre.flags.Flags;
import android.hardware.location.ContextHubTransaction;
import android.hardware.location.IContextHubTransactionCallback;
import android.hardware.location.NanoAppBinary;
import android.hardware.location.NanoAppMessage;
import android.hardware.location.NanoAppState;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;

import java.time.Duration;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Manages transactions at the Context Hub Service.
@@ -326,10 +312,10 @@ import java.util.concurrent.atomic.AtomicInteger;
                    mReliableMessageTransactionMap.entrySet().iterator();
            while (iter.hasNext()) {
                ContextHubServiceTransaction transaction = iter.next().getValue();
                short hostEndpointId = transaction.getHostEndpointId();
                long ownerId = transaction.getOwnerId();
                int numCompletedStartCalls = transaction.getNumCompletedStartCalls();
                if (numCompletedStartCalls == 0
                        && mReliableMessageHostEndpointIdActiveSet.contains(hostEndpointId)) {
                        && mReliableMessageOwnerIdActiveSet.contains(ownerId)) {
                    continue;
                }

@@ -394,7 +380,7 @@ import java.util.concurrent.atomic.AtomicInteger;
        } else {
            iter.remove();
        }
        mReliableMessageHostEndpointIdActiveSet.remove(transaction.getHostEndpointId());
        mReliableMessageOwnerIdActiveSet.remove(transaction.getOwnerId());

        Log.d(
                TAG,
@@ -436,7 +422,7 @@ import java.util.concurrent.atomic.AtomicInteger;
            transaction.setTimeoutTime(now + RELIABLE_MESSAGE_TIMEOUT.toNanos());
        }
        transaction.setNumCompletedStartCalls(numCompletedStartCalls + 1);
        mReliableMessageHostEndpointIdActiveSet.add(transaction.getHostEndpointId());
        mReliableMessageOwnerIdActiveSet.add(transaction.getOwnerId());
    }

    @Override