From acd7649901b884de95f7c5ddbe6105c5e1fcbd78 Mon Sep 17 00:00:00 2001 From: Lei Ju Date: Wed, 1 Jun 2022 09:56:55 -0700 Subject: [PATCH] Add nanoapp messasge records into dump. Test: manually run `adb shell dumpsys contexthub` and GTS test ContextHubSendMessageToHostNanoAppTest. Pass. Bug: 233889535 Change-Id: Icf46a0de8714343efeb605ee80777b85cfa14414 --- .../hardware/location/ContextHubManager.java | 2 +- .../contexthub/ContextHubService.java | 34 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/core/java/android/hardware/location/ContextHubManager.java b/core/java/android/hardware/location/ContextHubManager.java index 96923b0182e40..cec43261d4eaf 100644 --- a/core/java/android/hardware/location/ContextHubManager.java +++ b/core/java/android/hardware/location/ContextHubManager.java @@ -829,7 +829,7 @@ public final class ContextHubManager { /** * Equivalent to - * {@link #createClient(ContextHubInfo, Executor, String, ContextHubClientCallback)} + * {@link #createClient(Context, ContextHubInfo, Executor, ContextHubClientCallback)} * with the {@link Context} being set to null. */ @RequiresPermission(android.Manifest.permission.ACCESS_CONTEXT_HUB) 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 111621da06ceb..95178f4c7883c 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -71,6 +71,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -105,11 +106,12 @@ public class ContextHubService extends IContextHubService.Stub { * {@hide} */ @Retention(RetentionPolicy.SOURCE) - @IntDef(prefix = { "CONTEXT_HUB_EVENT_" }, value = { + @IntDef(prefix = {"CONTEXT_HUB_EVENT_"}, value = { CONTEXT_HUB_EVENT_UNKNOWN, CONTEXT_HUB_EVENT_RESTARTED, }) - public @interface Type { } + public @interface Type { + } public static final int CONTEXT_HUB_EVENT_UNKNOWN = 0; public static final int CONTEXT_HUB_EVENT_RESTARTED = 1; @@ -170,6 +172,10 @@ public class ContextHubService extends IContextHubService.Stub { private final Map mLastRestartTimestampMap = new HashMap<>(); + private static final int MAX_NUM_OF_NANOAPP_MESSAGE_RECORDS = 10; + private final ConcurrentLinkedEvictingDeque mNanoAppMessageRecords = + new ConcurrentLinkedEvictingDeque<>(MAX_NUM_OF_NANOAPP_MESSAGE_RECORDS); + /** * Class extending the callback to register with a Context Hub. */ @@ -346,7 +352,7 @@ public class ContextHubService extends IContextHubService.Stub { public void onReceive(Context context, Intent intent) { if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction()) || BluetoothAdapter.ACTION_BLE_STATE_CHANGED.equals( - intent.getAction())) { + intent.getAction())) { sendBtSettingUpdate(false /* forceUpdate */); } } @@ -684,15 +690,20 @@ public class ContextHubService extends IContextHubService.Stub { /** * Handles a unicast or broadcast message from a nanoapp. * - * @param contextHubId the ID of the hub the message came from + * @param contextHubId the ID of the hub the message came from * @param hostEndpointId the host endpoint ID of the client receiving this message - * @param message the message contents - * @param reqPermissions the permissions required to consume this message + * @param message the message contents + * @param nanoappPermissions the set of permissions the nanoapp holds + * @param messagePermissions the set of permissions that should be used for attributing + * permissions when this message is consumed by a client */ private void handleClientMessageCallback( - int contextHubId, short hostEndpointId, NanoAppMessage message, + int contextHubId, + short hostEndpointId, + NanoAppMessage message, List nanoappPermissions, List messagePermissions) { + mNanoAppMessageRecords.add(message); mClientManager.onMessageFromNanoApp( contextHubId, hostEndpointId, message, nanoappPermissions, messagePermissions); } @@ -1012,6 +1023,13 @@ public class ContextHubService extends IContextHubService.Stub { // Dump nanoAppHash mNanoAppStateManager.foreachNanoAppInstanceInfo((info) -> pw.println(info)); + pw.println(""); + pw.println("=================== NANOAPPS MESSAGES ===================="); + Iterator iterator = mNanoAppMessageRecords.descendingIterator(); + while (iterator.hasNext()) { + pw.println(iterator.next()); + } + pw.println(""); pw.println("=================== CLIENTS ===================="); pw.println(mClientManager); @@ -1227,7 +1245,7 @@ public class ContextHubService extends IContextHubService.Stub { } /** - * Invokes a daily timer to query all context hubs + * Invokes a daily timer to query all context hubs */ private void scheduleDailyMetricSnapshot() { Runnable queryAllContextHub = () -> {