diff options
| author | 2024-12-11 09:53:11 -0800 | |
|---|---|---|
| committer | 2024-12-11 09:53:11 -0800 | |
| commit | 88cac5de88915796fc839cfa9ccd40550935b67e (patch) | |
| tree | b04cf8c1b0f80c19297c4ea7ff07ef7bbabe5497 | |
| parent | dea088a4dfc5f24f3e8a25e8fddd79a69f2460b6 (diff) | |
| parent | b44757c4bdebd3b156a8f10c10ab3c77ee8b66d5 (diff) | |
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
11 files changed, 320 insertions, 67 deletions
diff --git a/core/java/android/hardware/contexthub/HubEndpoint.java b/core/java/android/hardware/contexthub/HubEndpoint.java index ee4675c6a46f..b251aa1abc0d 100644 --- a/core/java/android/hardware/contexthub/HubEndpoint.java +++ b/core/java/android/hardware/contexthub/HubEndpoint.java @@ -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) { diff --git a/core/java/android/hardware/location/IContextHubService.aidl b/core/java/android/hardware/location/IContextHubService.aidl index f14aadcab474..2a472375a00f 100644 --- a/core/java/android/hardware/location/IContextHubService.aidl +++ b/core/java/android/hardware/location/IContextHubService.aidl @@ -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") diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubEndpointBroker.java b/services/core/java/com/android/server/location/contexthub/ContextHubEndpointBroker.java index ef73463122ff..9b9d52a019da 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubEndpointBroker.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubEndpointBroker.java @@ -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) diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubEndpointManager.java b/services/core/java/com/android/server/location/contexthub/ContextHubEndpointManager.java index 155a92a21368..07df7f994b24 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubEndpointManager.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubEndpointManager.java @@ -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) { diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubHalEndpointCallback.java b/services/core/java/com/android/server/location/contexthub/ContextHubHalEndpointCallback.java index 9d52c6a020f4..f1f2217fc3c4 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubHalEndpointCallback.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubHalEndpointCallback.java @@ -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( @@ -84,13 +91,6 @@ public class ContextHubHalEndpointCallback } @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 { HubEndpointInfo.HubEndpointIdentifier destinationId = @@ -111,6 +111,19 @@ public class ContextHubHalEndpointCallback } @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; } diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubService.java b/services/core/java/com/android/server/location/contexthub/ContextHubService.java index d916eda693d8..165f9d3340e3 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -334,7 +334,8 @@ public class ContextHubService extends IContextHubService.Stub { try { registry = new HubInfoRegistry(mContextHubWrapper); mEndpointManager = - new ContextHubEndpointManager(mContext, mContextHubWrapper, registry); + new ContextHubEndpointManager( + mContext, mContextHubWrapper, registry, mTransactionManager); Log.i(TAG, "Enabling generic offload API"); } catch (UnsupportedOperationException e) { mEndpointManager = null; @@ -794,14 +795,16 @@ public class ContextHubService extends IContextHubService.Stub { @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) @Override public IContextHubEndpoint registerEndpoint( - HubEndpointInfo pendingHubEndpointInfo, IContextHubEndpointCallback callback) + HubEndpointInfo pendingHubEndpointInfo, + IContextHubEndpointCallback callback, + String packageName) throws RemoteException { super.registerEndpoint_enforcePermission(); if (mEndpointManager == null) { Log.e(TAG, "Endpoint manager failed to initialize"); throw new UnsupportedOperationException("Endpoint registration is not supported"); } - return mEndpointManager.registerEndpoint(pendingHubEndpointInfo, callback); + return mEndpointManager.registerEndpoint(pendingHubEndpointInfo, callback, packageName); } @android.annotation.EnforcePermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubServiceTransaction.java b/services/core/java/com/android/server/location/contexthub/ContextHubServiceTransaction.java index 3aea6d533295..4e96b4442a33 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubServiceTransaction.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubServiceTransaction.java @@ -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(")"); diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubServiceUtil.java b/services/core/java/com/android/server/location/contexthub/ContextHubServiceUtil.java index 05be427f9daf..a215b4600d44 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubServiceUtil.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubServiceUtil.java @@ -20,7 +20,9 @@ import android.Manifest; import android.content.Context; import android.hardware.contexthub.EndpointInfo; import android.hardware.contexthub.HubEndpointInfo; +import android.hardware.contexthub.HubMessage; import android.hardware.contexthub.HubServiceInfo; +import android.hardware.contexthub.Message; import android.hardware.contexthub.V1_0.AsyncEventType; import android.hardware.contexthub.V1_0.ContextHubMsg; import android.hardware.contexthub.V1_0.HostEndPoint; @@ -465,4 +467,39 @@ import java.util.List; } return outputInfo; } + + /** + * Converts a HubMessage object to a AIDL HAL Message object. + * + * @param message the HubMessage message to convert + * @return the AIDL HAL message + */ + /* package */ + static Message createHalMessage(HubMessage message) { + Message outMessage = new Message(); + outMessage.flags = + message.getDeliveryParams().isResponseRequired() + ? Message.FLAG_REQUIRES_DELIVERY_STATUS + : 0; + outMessage.permissions = new String[0]; + outMessage.sequenceNumber = message.getMessageSequenceNumber(); + outMessage.type = message.getMessageType(); + outMessage.content = message.getMessageBody(); + return outMessage; + } + + /** + * Converts a AIDL HAL Message object to a HubMessage object. + * + * @param message the AIDL HAL Message message to convert + * @return the HubMessage + */ + /* package */ + static HubMessage createHubMessage(Message message) { + boolean isReliable = (message.flags & Message.FLAG_REQUIRES_DELIVERY_STATUS) != 0; + return HubMessage.createMessage( + message.type, + message.content, + HubMessage.DeliveryParams.makeBasic().setResponseRequired(isReliable)); + } } diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java index ccfa61b400b6..5dd40ea97a64 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java @@ -17,6 +17,7 @@ package com.android.server.location.contexthub; import android.chre.flags.Flags; +import android.hardware.contexthub.Message; import android.hardware.location.ContextHubTransaction; import android.hardware.location.IContextHubTransactionCallback; import android.hardware.location.NanoAppBinary; @@ -84,9 +85,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,27 +356,32 @@ 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 contextHubId The ID of the hub to send the message to. - * @param message The message to send. + * @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. - * @param packageName The host package associated with this transaction. + * @param packageName The host package associated with this transaction. * @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; @@ -394,6 +400,48 @@ import java.util.concurrent.atomic.AtomicInteger; } /** + * Creates a transaction to send a message through a session. + * + * @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( + int sessionId, + Message message, + String packageName, + IContextHubTransactionCallback transactionCallback) { + return new ContextHubServiceTransaction( + mNextAvailableId.getAndIncrement(), + ContextHubTransaction.TYPE_HUB_MESSAGE_REQUIRES_RESPONSE, + packageName, + mNextAvailableMessageSequenceNumber.getAndIncrement(), + sessionId) { + @Override + /* package */ int onTransact() { + try { + message.sequenceNumber = getMessageSequenceNumber(); + mContextHubProxy.sendMessageToEndpoint(sessionId, message); + return ContextHubTransaction.RESULT_SUCCESS; + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while trying to send a session message", e); + return ContextHubTransaction.RESULT_FAILED_UNKNOWN; + } + } + + @Override + /* package */ void onTransactionComplete(@ContextHubTransaction.Result int result) { + try { + transactionCallback.onTransactionComplete(result); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException while calling client onTransactionComplete", e); + } + } + }; + } + + /** * Creates a transaction for querying for a list of nanoapps. * * @param contextHubId the ID of the hub to query @@ -452,9 +500,14 @@ import java.util.concurrent.atomic.AtomicInteger; mTransactionRecordDeque.add(new TransactionRecord(transaction.toString())); } - if (Flags.reliableMessageRetrySupportService() - && transaction.getTransactionType() - == ContextHubTransaction.TYPE_RELIABLE_MESSAGE) { + boolean isReliableMessage = + Flags.reliableMessageRetrySupportService() + && (transaction.getTransactionType() + == ContextHubTransaction.TYPE_RELIABLE_MESSAGE); + boolean isEndpointMessage = + (transaction.getTransactionType() + == ContextHubTransaction.TYPE_HUB_MESSAGE_REQUIRES_RESPONSE); + if (isReliableMessage || isEndpointMessage) { synchronized (mReliableMessageLock) { if (mReliableMessageTransactionMap.size() >= MAX_PENDING_REQUESTS) { throw new IllegalStateException( @@ -766,10 +819,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 +924,7 @@ import java.util.concurrent.atomic.AtomicInteger; } else { iter.remove(); } - mReliableMessageHostEndpointIdActiveSet.remove(transaction.getHostEndpointId()); + mReliableMessageOwnerIdActiveSet.remove(transaction.getOwnerId()); } /** @@ -906,7 +959,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) { diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManagerOld.java b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManagerOld.java index a67fa308a6ea..657375d2cbd7 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManagerOld.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManagerOld.java @@ -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 diff --git a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java index 6cb942980403..e1df503eccdb 100644 --- a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java @@ -21,6 +21,7 @@ import android.chre.flags.Flags; import android.hardware.contexthub.EndpointId; import android.hardware.contexthub.HostEndpointInfo; import android.hardware.contexthub.HubEndpointInfo; +import android.hardware.contexthub.Message; import android.hardware.contexthub.MessageDeliveryStatus; import android.hardware.contexthub.NanSessionRequest; import android.hardware.contexthub.V1_0.ContextHub; @@ -264,6 +265,13 @@ public abstract class IContextHubWrapper { /** Notifies the completion of a session opened by the HAL */ public void endpointSessionOpenComplete(int sessionId) throws RemoteException {} + /** Sends a message to a remote endpoint */ + public void sendMessageToEndpoint(int sessionId, Message msg) throws RemoteException {} + + /** Sends a message delivery status to a remote endpoint */ + public void sendMessageDeliveryStatusToEndpoint(int sessionId, MessageDeliveryStatus msgStatus) + throws RemoteException {} + /** * @return True if this version of the Contexthub HAL supports Location setting notifications. */ @@ -757,6 +765,25 @@ public abstract class IContextHubWrapper { hub.endpointSessionOpenComplete(sessionId); } + @Override + public void sendMessageToEndpoint(int sessionId, Message msg) throws RemoteException { + android.hardware.contexthub.IContextHub hub = getHub(); + if (hub == null) { + return; + } + hub.sendMessageToEndpoint(sessionId, msg); + } + + @Override + public void sendMessageDeliveryStatusToEndpoint( + int sessionId, MessageDeliveryStatus msgStatus) throws RemoteException { + android.hardware.contexthub.IContextHub hub = getHub(); + if (hub == null) { + return; + } + hub.sendMessageDeliveryStatusToEndpoint(sessionId, msgStatus); + } + public boolean supportsLocationSettingNotifications() { return true; } |