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

Commit da43e007 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "contexthub: Refactor for api change" into main

parents 259c4d27 a0224a8b
Loading
Loading
Loading
Loading
+12 −11
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.hardware.contexthub.HubEndpointInfo;
import android.hardware.contexthub.HubMessage;
import android.hardware.contexthub.IContextHubEndpoint;
import android.hardware.contexthub.IContextHubEndpointCallback;
import android.hardware.contexthub.IEndpointCommunication;
import android.hardware.contexthub.Message;
import android.hardware.contexthub.MessageDeliveryStatus;
import android.hardware.location.ContextHubTransaction;
@@ -50,8 +51,8 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    /** The context of the service. */
    private final Context mContext;

    /** The proxy to talk to the Context Hub HAL. */
    private final IContextHubWrapper mContextHubProxy;
    /** The proxy to talk to the Context Hub HAL for endpoint communication. */
    private final IEndpointCommunication mHubInterface;

    /** The manager that registered this endpoint. */
    private final ContextHubEndpointManager mEndpointManager;
@@ -90,14 +91,14 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub

    /* package */ ContextHubEndpointBroker(
            Context context,
            IContextHubWrapper contextHubProxy,
            IEndpointCommunication hubInterface,
            ContextHubEndpointManager endpointManager,
            EndpointInfo halEndpointInfo,
            IContextHubEndpointCallback callback,
            String packageName,
            ContextHubTransactionManager transactionManager) {
        mContext = context;
        mContextHubProxy = contextHubProxy;
        mHubInterface = hubInterface;
        mEndpointManager = endpointManager;
        mEndpointInfo = new HubEndpointInfo(halEndpointInfo);
        mHalEndpointInfo = halEndpointInfo;
@@ -123,7 +124,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        synchronized (mOpenSessionLock) {
            try {
                mPendingSessionIds.add(sessionId);
                mContextHubProxy.openEndpointSession(
                mHubInterface.openEndpointSession(
                        sessionId,
                        halEndpointInfo.id,
                        mHalEndpointInfo.id,
@@ -145,7 +146,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        super.closeSession_enforcePermission();
        if (!mIsRegistered.get()) throw new IllegalStateException("Endpoint is not registered");
        try {
            mContextHubProxy.closeEndpointSession(
            mHubInterface.closeEndpointSession(
                    sessionId, ContextHubServiceUtil.toHalReason(reason));
        } catch (RemoteException | IllegalArgumentException | UnsupportedOperationException e) {
            Log.e(TAG, "Exception while calling HAL closeEndpointSession", e);
@@ -159,7 +160,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        super.unregister_enforcePermission();
        mIsRegistered.set(false);
        try {
            mContextHubProxy.unregisterEndpoint(mHalEndpointInfo);
            mHubInterface.unregisterEndpoint(mHalEndpointInfo);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException while calling HAL unregisterEndpoint", e);
        }
@@ -183,7 +184,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        super.openSessionRequestComplete_enforcePermission();
        synchronized (mOpenSessionLock) {
            try {
                mContextHubProxy.endpointSessionOpenComplete(sessionId);
                mHubInterface.endpointSessionOpenComplete(sessionId);
                mActiveRemoteSessionIds.add(sessionId);
            } catch (RemoteException | IllegalArgumentException | UnsupportedOperationException e) {
                Log.e(TAG, "Exception while calling endpointSessionOpenComplete", e);
@@ -208,14 +209,14 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        // TODO(b/381102453): Handle permissions
        if (callback == null) {
            try {
                mContextHubProxy.sendMessageToEndpoint(sessionId, halMessage);
                mHubInterface.sendMessageToEndpoint(sessionId, halMessage);
            } catch (RemoteException e) {
                Log.w(TAG, "Exception while sending message on session " + sessionId, e);
            }
        } else {
            ContextHubServiceTransaction transaction =
                    mTransactionManager.createSessionMessageTransaction(
                            sessionId, halMessage, mPackageName, callback);
                            mHubInterface, sessionId, halMessage, mPackageName, callback);
            try {
                mTransactionManager.addTransaction(transaction);
            } catch (IllegalStateException e) {
@@ -240,7 +241,7 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        status.messageSequenceNumber = messageSeqNumber;
        status.errorCode = errorCode;
        try {
            mContextHubProxy.sendMessageDeliveryStatusToEndpoint(sessionId, status);
            mHubInterface.sendMessageDeliveryStatusToEndpoint(sessionId, status);
        } catch (RemoteException e) {
            Log.w(
                    TAG,
+82 −28
Original line number Diff line number Diff line
@@ -17,11 +17,14 @@
package com.android.server.location.contexthub;

import android.content.Context;
import android.hardware.contexthub.ContextHubInfo;
import android.hardware.contexthub.EndpointInfo;
import android.hardware.contexthub.HubEndpointInfo;
import android.hardware.contexthub.HubInfo;
import android.hardware.contexthub.HubMessage;
import android.hardware.contexthub.IContextHubEndpoint;
import android.hardware.contexthub.IContextHubEndpointCallback;
import android.hardware.contexthub.IEndpointCommunication;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.util.Log;
@@ -75,11 +78,11 @@ import java.util.concurrent.ConcurrentHashMap;
    @GuardedBy("mEndpointLock")
    private long mNextEndpointId = -2;

    /** The minimum session ID reservable by endpoints (retrieved from HAL) */
    private final int mMinSessionId;
    /** The minimum session ID reservable by endpoints (retrieved from HAL in init()) */
    private int mMinSessionId = -1;

    /** The minimum session ID reservable by endpoints (retrieved from HAL) */
    private final int mMaxSessionId;
    /** The minimum session ID reservable by endpoints (retrieved from HAL in init()) */
    private int mMaxSessionId = -1;

    /** Variables for managing session ID creation */
    private final Object mSessionIdLock = new Object();
@@ -92,8 +95,11 @@ import java.util.concurrent.ConcurrentHashMap;
    @GuardedBy("mSessionIdLock")
    private int mNextSessionId = 0;

    /** Initialized to true if all initialization in the constructor succeeds. */
    private final boolean mSessionIdsValid;
    /** Set true if init() succeeds */
    private boolean mSessionIdsValid = false;

    /** The interface for endpoint communication (retrieved from HAL in init()) */
    private IEndpointCommunication mHubInterface = null;

    /* package */ ContextHubEndpointManager(
            Context context,
@@ -104,34 +110,73 @@ import java.util.concurrent.ConcurrentHashMap;
        mContextHubProxy = contextHubProxy;
        mHubInfoRegistry = hubInfoRegistry;
        mTransactionManager = transactionManager;
    }

    /**
     * Initializes this class.
     *
     * This is separate from the constructor so that this may be passed into the callback registered
     * with the HAL.
     *
     * @throws InstantiationException on any failure
     */
    /* package */ void init() throws InstantiationException {
        if (mSessionIdsValid) {
            throw new IllegalStateException("Already initialized");
        }
        try {
            HubInfo info = new HubInfo();
            info.hubId = SERVICE_HUB_ID;
            // TODO(b/387291125): Populate the ContextHubInfo with real values.
            ContextHubInfo contextHubInfo = new ContextHubInfo();
            contextHubInfo.name = "";
            contextHubInfo.vendor = "";
            contextHubInfo.toolchain = "";
            contextHubInfo.supportedPermissions = new String[0];
            info.hubDetails = HubInfo.HubDetails.contextHubInfo(contextHubInfo);
            mHubInterface = mContextHubProxy.registerEndpointHub(
                    new ContextHubHalEndpointCallback(mHubInfoRegistry, this),
                    info);
            if (mHubInterface == null) {
                throw new IllegalStateException("Received null IEndpointCommunication");
            }
        } catch (RemoteException | IllegalStateException | ServiceSpecificException
                 | UnsupportedOperationException e) {
            String error = "Failed to register ContextHubService as message hub";
            Log.e(TAG, error, e);
            throw new InstantiationException(error);
        }

        int[] range = null;
        try {
            range = mContextHubProxy.requestSessionIdRange(SERVICE_SESSION_RANGE);
            range = mHubInterface.requestSessionIdRange(SERVICE_SESSION_RANGE);
            if (range != null && range.length < SERVICE_SESSION_RANGE_LENGTH) {
                Log.e(TAG, "Invalid session ID range: range array size = " + range.length);
                range = null;
                String error = "Invalid session ID range: range array size = " + range.length;
                Log.e(TAG, error);
                unregisterHub();
                throw new InstantiationException(error);
            }
        } catch (RemoteException | IllegalArgumentException | ServiceSpecificException e) {
            Log.e(TAG, "Exception while calling HAL requestSessionIdRange", e);
            String error = "Exception while calling HAL requestSessionIdRange";
            Log.e(TAG, error, e);
            unregisterHub();
            throw new InstantiationException(error);
        }

        if (range == null) {
            mMinSessionId = -1;
            mMaxSessionId = -1;
            mSessionIdsValid = false;
        } else {
        mMinSessionId = range[0];
        mMaxSessionId = range[1];
        if (!isSessionIdRangeValid(mMinSessionId, mMaxSessionId)) {
                Log.e(
                        TAG,
                        "Invalid session ID range: max=" + mMaxSessionId + " min=" + mMinSessionId);
                mSessionIdsValid = false;
            } else {
                mNextSessionId = mMinSessionId;
                mSessionIdsValid = true;
            String error =
                    "Invalid session ID range: max=" + mMaxSessionId + " min=" + mMinSessionId;
            Log.e(TAG, error);
            unregisterHub();
            throw new InstantiationException(error);
        }

        synchronized (mSessionIdLock) {
            mNextSessionId = mMinSessionId;
        }
        mSessionIdsValid = true;
    }

    /**
@@ -157,7 +202,7 @@ import java.util.concurrent.ConcurrentHashMap;
                ContextHubServiceUtil.createHalEndpointInfo(
                        pendingEndpointInfo, endpointId, SERVICE_HUB_ID);
        try {
            mContextHubProxy.registerEndpoint(halEndpointInfo);
            mHubInterface.registerEndpoint(halEndpointInfo);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException while calling HAL registerEndpoint", e);
            throw e;
@@ -165,7 +210,7 @@ import java.util.concurrent.ConcurrentHashMap;
        broker =
                new ContextHubEndpointBroker(
                        mContext,
                        mContextHubProxy,
                        mHubInterface,
                        this /* endpointManager */,
                        halEndpointInfo,
                        callback,
@@ -327,6 +372,15 @@ import java.util.concurrent.ConcurrentHashMap;
        }
    }

    /** Unregister the hub (called during init() failure). Silence errors. */
    private void unregisterHub() {
        try {
            mHubInterface.unregister();
        } catch (RemoteException | IllegalStateException e) {
            Log.e(TAG, "Failed to unregister from HAL on init failure", e);
        }
    }

    /** @return an available endpoint ID */
    private long getNewEndpointId() {
        synchronized (mEndpointLock) {
+2 −14
Original line number Diff line number Diff line
@@ -338,8 +338,9 @@ public class ContextHubService extends IContextHubService.Stub {
                mEndpointManager =
                        new ContextHubEndpointManager(
                                mContext, mContextHubWrapper, registry, mTransactionManager);
                mEndpointManager.init();
                Log.i(TAG, "Enabling generic offload API");
            } catch (UnsupportedOperationException e) {
            } catch (InstantiationException e) {
                mEndpointManager = null;
                registry = null;
                Log.w(TAG, "Generic offload API not supported, disabling");
@@ -352,7 +353,6 @@ public class ContextHubService extends IContextHubService.Stub {
        }

        initDefaultClientMap();
        initEndpointCallback();

        initLocationSettingNotifications();
        initWifiSettingNotifications();
@@ -531,18 +531,6 @@ public class ContextHubService extends IContextHubService.Stub {
        mDefaultClientMap = Collections.unmodifiableMap(defaultClientMap);
    }

    private void initEndpointCallback() {
        if (mHubInfoRegistry == null) {
            return;
        }
        try {
            mContextHubWrapper.registerEndpointCallback(
                    new ContextHubHalEndpointCallback(mHubInfoRegistry, mEndpointManager));
        } catch (RemoteException | UnsupportedOperationException e) {
            Log.e(TAG, "Exception while registering IEndpointCallback", e);
        }
    }

    /**
     * Initializes existing callbacks with the mContextHubWrapper for every context hub
     */
+4 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.server.location.contexthub;

import android.chre.flags.Flags;
import android.hardware.contexthub.IEndpointCommunication;
import android.hardware.contexthub.Message;
import android.hardware.location.ContextHubTransaction;
import android.hardware.location.IContextHubTransactionCallback;
@@ -402,12 +403,14 @@ import java.util.concurrent.atomic.AtomicInteger;
    /**
     * Creates a transaction to send a message through a session.
     *
     * @param hubInterface Interface for interacting with other endpoint hubs.
     * @param sessionId The ID of the endpoint session the message should be sent through.
     * @param message The message to send.
     * @param transactionCallback The callback of the transactions.
     * @return The generated transaction.
     */
    /* package */ ContextHubServiceTransaction createSessionMessageTransaction(
            IEndpointCommunication hubInterface,
            int sessionId,
            Message message,
            String packageName,
@@ -422,7 +425,7 @@ import java.util.concurrent.atomic.AtomicInteger;
            /* package */ int onTransact() {
                try {
                    message.sequenceNumber = getMessageSequenceNumber();
                    mContextHubProxy.sendMessageToEndpoint(sessionId, message);
                    hubInterface.sendMessageToEndpoint(sessionId, message);
                    return ContextHubTransaction.RESULT_SUCCESS;
                } catch (RemoteException e) {
                    Log.e(TAG, "RemoteException while trying to send a session message", e);
+9 −3
Original line number Diff line number Diff line
@@ -98,10 +98,16 @@ class HubInfoRegistry implements ContextHubHalEndpointCallback.IEndpointLifecycl

    private final Object mCallbackLock = new Object();

    HubInfoRegistry(IContextHubWrapper contextHubWrapper) {
    HubInfoRegistry(IContextHubWrapper contextHubWrapper) throws InstantiationException {
        mContextHubWrapper = contextHubWrapper;
        try {
            refreshCachedHubs();
            refreshCachedEndpoints();
        } catch (UnsupportedOperationException e) {
            String error = "Failed to update hub and endpoint cache";
            Log.e(TAG, error, e);
            throw new InstantiationException(error);
        }
    }

    /** Retrieve the list of hubs available. */
Loading