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

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

Merge changes I1711739d,I8f978e2d,I8dc68776 into main

* changes:
  Implements endpoint messaging
  Adds package name to the endpoint
  Allows message transactions to be used by any transaction owner
parents dea088a4 b44757c4
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -355,7 +355,10 @@ public class HubEndpoint {
        }
        try {
            IContextHubEndpoint serviceToken =
                    service.registerEndpoint(mPendingHubEndpointInfo, mServiceCallback);
                    service.registerEndpoint(
                            mPendingHubEndpointInfo,
                            mServiceCallback,
                            mPendingHubEndpointInfo.getTag());
            mAssignedHubEndpointInfo = serviceToken.getAssignedHubEndpointInfo();
            mServiceToken = serviceToken;
        } catch (RemoteException e) {
+1 −1
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ interface IContextHubService {

    // Register an endpoint with the context hub
    @EnforcePermission("ACCESS_CONTEXT_HUB")
    IContextHubEndpoint registerEndpoint(in HubEndpointInfo pendingEndpointInfo, in IContextHubEndpointCallback callback);
    IContextHubEndpoint registerEndpoint(in HubEndpointInfo pendingEndpointInfo, in IContextHubEndpointCallback callback, String packageName);

    // Register an endpoint discovery callback (id)
    @EnforcePermission("ACCESS_CONTEXT_HUB")
+77 −3
Original line number Diff line number Diff line
@@ -18,10 +18,14 @@ package com.android.server.location.contexthub;

import android.content.Context;
import android.hardware.contexthub.EndpointInfo;
import android.hardware.contexthub.ErrorCode;
import android.hardware.contexthub.HubEndpointInfo;
import android.hardware.contexthub.HubMessage;
import android.hardware.contexthub.IContextHubEndpoint;
import android.hardware.contexthub.IContextHubEndpointCallback;
import android.hardware.contexthub.Message;
import android.hardware.contexthub.MessageDeliveryStatus;
import android.hardware.location.ContextHubTransaction;
import android.hardware.location.IContextHubTransactionCallback;
import android.os.IBinder;
import android.os.RemoteException;
@@ -78,18 +82,28 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    @GuardedBy("mOpenSessionLock")
    private final Set<Integer> mActiveRemoteSessionIds = new HashSet<>();

    /** The package name of the app that created the endpoint */
    private final String mPackageName;

    /* Transaction manager used for sending reliable messages */
    private final ContextHubTransactionManager mTransactionManager;

    /* package */ ContextHubEndpointBroker(
            Context context,
            IContextHubWrapper contextHubProxy,
            ContextHubEndpointManager endpointManager,
            EndpointInfo halEndpointInfo,
            IContextHubEndpointCallback callback) {
            IContextHubEndpointCallback callback,
            String packageName,
            ContextHubTransactionManager transactionManager) {
        mContext = context;
        mContextHubProxy = contextHubProxy;
        mEndpointManager = endpointManager;
        mEndpointInfo = new HubEndpointInfo(halEndpointInfo);
        mHalEndpointInfo = halEndpointInfo;
        mContextHubEndpointCallback = callback;
        mPackageName = packageName;
        mTransactionManager = transactionManager;
    }

    @Override
@@ -175,12 +189,57 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
    @Override
    public void sendMessage(
            int sessionId, HubMessage message, IContextHubTransactionCallback callback) {
        // TODO(b/381102453): Implement this
        ContextHubServiceUtil.checkPermissions(mContext);
        Message halMessage = ContextHubServiceUtil.createHalMessage(message);
        synchronized (mOpenSessionLock) {
            if (!mActiveSessionIds.contains(sessionId)
                    && !mActiveRemoteSessionIds.contains(sessionId)) {
                throw new SecurityException(
                        "sendMessage called on inactive session (id= " + sessionId + ")");
            }
        }

        // TODO(b/381102453): Handle permissions
        if (callback == null) {
            try {
                mContextHubProxy.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);
            try {
                mTransactionManager.addTransaction(transaction);
            } catch (IllegalStateException e) {
                Log.e(
                        TAG,
                        "Unable to add a transaction in sendMessageToEndpoint "
                                + "(session ID = "
                                + sessionId
                                + ")",
                        e);
                transaction.onTransactionComplete(
                        ContextHubTransaction.RESULT_FAILED_SERVICE_INTERNAL_FAILURE);
            }
        }
    }

    @Override
    public void sendMessageDeliveryStatus(int sessionId, int messageSeqNumber, byte errorCode) {
        // TODO(b/381102453): Implement this
        ContextHubServiceUtil.checkPermissions(mContext);
        MessageDeliveryStatus status = new MessageDeliveryStatus();
        status.messageSequenceNumber = messageSeqNumber;
        status.errorCode = errorCode;
        try {
            mContextHubProxy.sendMessageDeliveryStatusToEndpoint(sessionId, status);
        } catch (RemoteException e) {
            Log.w(
                    TAG,
                    "Exception while sending message delivery status on session " + sessionId,
                    e);
        }
    }

    /** Invoked when the underlying binder of this broker has died at the client process. */
@@ -238,6 +297,21 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        }
    }

    /* package */ void onMessageReceived(int sessionId, HubMessage message) {
        if (mContextHubEndpointCallback != null) {
            try {
                mContextHubEndpointCallback.onMessageReceived(sessionId, message);
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException while calling onMessageReceived", e);
            }
        }
    }

    /* package */ void onMessageDeliveryStatusReceived(
            int sessionId, int sequenceNumber, byte errorCode) {
        mTransactionManager.onMessageDeliveryResponse(sequenceNumber, errorCode == ErrorCode.OK);
    }

    /* package */ boolean hasSessionId(int sessionId) {
        synchronized (mOpenSessionLock) {
            return mPendingSessionIds.contains(sessionId)
+47 −3
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.location.contexthub;
import android.content.Context;
import android.hardware.contexthub.EndpointInfo;
import android.hardware.contexthub.HubEndpointInfo;
import android.hardware.contexthub.HubMessage;
import android.hardware.contexthub.IContextHubEndpoint;
import android.hardware.contexthub.IContextHubEndpointCallback;
import android.os.RemoteException;
@@ -59,6 +60,8 @@ import java.util.concurrent.ConcurrentHashMap;

    private final HubInfoRegistry mHubInfoRegistry;

    private final ContextHubTransactionManager mTransactionManager;

    /** A map of endpoint IDs to brokers currently registered. */
    private final Map<Long, ContextHubEndpointBroker> mEndpointMap = new ConcurrentHashMap<>();

@@ -93,10 +96,14 @@ import java.util.concurrent.ConcurrentHashMap;
    private final boolean mSessionIdsValid;

    /* package */ ContextHubEndpointManager(
            Context context, IContextHubWrapper contextHubProxy, HubInfoRegistry hubInfoRegistry) {
            Context context,
            IContextHubWrapper contextHubProxy,
            HubInfoRegistry hubInfoRegistry,
            ContextHubTransactionManager transactionManager) {
        mContext = context;
        mContextHubProxy = contextHubProxy;
        mHubInfoRegistry = hubInfoRegistry;
        mTransactionManager = transactionManager;
        int[] range = null;
        try {
            range = mContextHubProxy.requestSessionIdRange(SERVICE_SESSION_RANGE);
@@ -132,11 +139,14 @@ import java.util.concurrent.ConcurrentHashMap;
     *
     * @param pendingEndpointInfo the object describing the endpoint being registered
     * @param callback the callback interface of the endpoint to register
     * @param packageName the name of the package of the calling client
     * @return the endpoint interface
     * @throws IllegalStateException if max number of endpoints have already registered
     */
    /* package */ IContextHubEndpoint registerEndpoint(
            HubEndpointInfo pendingEndpointInfo, IContextHubEndpointCallback callback)
            HubEndpointInfo pendingEndpointInfo,
            IContextHubEndpointCallback callback,
            String packageName)
            throws RemoteException {
        if (!mSessionIdsValid) {
            throw new IllegalStateException("ContextHubEndpointManager failed to initialize");
@@ -158,7 +168,9 @@ import java.util.concurrent.ConcurrentHashMap;
                        mContextHubProxy,
                        this /* endpointManager */,
                        halEndpointInfo,
                        callback);
                        callback,
                        packageName,
                        mTransactionManager);
        mEndpointMap.put(endpointId, broker);

        try {
@@ -283,6 +295,38 @@ import java.util.concurrent.ConcurrentHashMap;
        }
    }

    @Override
    public void onMessageReceived(int sessionId, HubMessage message) {
        boolean callbackInvoked = false;
        for (ContextHubEndpointBroker broker : mEndpointMap.values()) {
            if (broker.hasSessionId(sessionId)) {
                broker.onMessageReceived(sessionId, message);
                callbackInvoked = true;
                break;
            }
        }

        if (!callbackInvoked) {
            Log.w(TAG, "onMessageReceived: unknown session ID " + sessionId);
        }
    }

    @Override
    public void onMessageDeliveryStatusReceived(int sessionId, int sequenceNumber, byte errorCode) {
        boolean callbackInvoked = false;
        for (ContextHubEndpointBroker broker : mEndpointMap.values()) {
            if (broker.hasSessionId(sessionId)) {
                broker.onMessageDeliveryStatusReceived(sessionId, sequenceNumber, errorCode);
                callbackInvoked = true;
                break;
            }
        }

        if (!callbackInvoked) {
            Log.w(TAG, "onMessageDeliveryStatusReceived: unknown session ID " + sessionId);
        }
    }

    /** @return an available endpoint ID */
    private long getNewEndpointId() {
        synchronized (mEndpointLock) {
+20 −7
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package com.android.server.location.contexthub;

import android.hardware.contexthub.EndpointId;
import android.hardware.contexthub.HubEndpointInfo;
import android.hardware.contexthub.HubMessage;
import android.hardware.contexthub.IEndpointCallback;
import android.hardware.contexthub.Message;
import android.hardware.contexthub.MessageDeliveryStatus;
@@ -51,6 +52,12 @@ public class ContextHubHalEndpointCallback

        /** Called when a requested endpoint open session is completed */
        void onEndpointSessionOpenComplete(int sessionId);

        /** Called when a message is received for the session */
        void onMessageReceived(int sessionId, HubMessage message);

        /** Called when a message delivery status is received for the session */
        void onMessageDeliveryStatusReceived(int sessionId, int sequenceNumber, byte errorCode);
    }

    ContextHubHalEndpointCallback(
@@ -83,13 +90,6 @@ public class ContextHubHalEndpointCallback
        mEndpointLifecycleCallback.onEndpointStopped(endpointIds, reason);
    }

    @Override
    public void onMessageReceived(int i, Message message) throws RemoteException {}

    @Override
    public void onMessageDeliveryStatusReceived(int i, MessageDeliveryStatus messageDeliveryStatus)
            throws RemoteException {}

    @Override
    public void onEndpointSessionOpenRequest(
            int i, EndpointId destination, EndpointId initiator, String s) throws RemoteException {
@@ -110,6 +110,19 @@ public class ContextHubHalEndpointCallback
        mEndpointSessionCallback.onEndpointSessionOpenComplete(i);
    }

    @Override
    public void onMessageReceived(int i, Message message) throws RemoteException {
        HubMessage hubMessage = ContextHubServiceUtil.createHubMessage(message);
        mEndpointSessionCallback.onMessageReceived(i, hubMessage);
    }

    @Override
    public void onMessageDeliveryStatusReceived(int i, MessageDeliveryStatus messageDeliveryStatus)
            throws RemoteException {
        mEndpointSessionCallback.onMessageDeliveryStatusReceived(
                i, messageDeliveryStatus.messageSequenceNumber, messageDeliveryStatus.errorCode);
    }

    @Override
    public int getInterfaceVersion() throws RemoteException {
        return IEndpointCallback.VERSION;
Loading