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

Commit 264e6fd6 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Implements debug dump for ContextHubEndpointManager

Bug: 381173093
Flag: android.chre.flags.offload_implementation
Test: adb shell dumpsys contexthub
Change-Id: I95337112a0619907bf7e2548ed423011b9dab807
parent 0982ecbc
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -374,6 +374,34 @@ public class ContextHubEndpointBroker extends IContextHubEndpoint.Stub
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(mEndpointInfo).append(", ");
        sb.append("package: ").append(mPackageName).append(", ");
        synchronized (mWakeLock) {
            sb.append("wakelock: ").append(mWakeLock);
        }
        synchronized (mOpenSessionLock) {
            if (mSessionInfoMap.size() != 0) {
                sb.append(System.lineSeparator());
                sb.append(" sessions: ");
                sb.append(System.lineSeparator());
            }
            for (int i = 0; i < mSessionInfoMap.size(); i++) {
                int id = mSessionInfoMap.keyAt(i);
                int count = i + 1;
                sb.append(
                        "  " + count + ". id="
                                + id
                                + ", remote:"
                                + mSessionInfoMap.get(id).getRemoteEndpointInfo());
                sb.append(System.lineSeparator());
            }
        }
        return sb.toString();
    }

    /**
     * Registers this endpoints with the Context Hub HAL.
     *
+73 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.location.contexthub;

import android.annotation.IntDef;
import android.content.Context;
import android.hardware.contexthub.ContextHubInfo;
import android.hardware.contexthub.EndpointInfo;
@@ -34,8 +35,11 @@ import android.util.Log;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -105,6 +109,48 @@ import java.util.function.Consumer;
    /** The interface for endpoint communication (retrieved from HAL in init()) */
    private IEndpointCommunication mHubInterface = null;

    /*
     * The list of previous registration records.
     */
    private static final int NUM_CLIENT_RECORDS = 20;
    private final ConcurrentLinkedEvictingDeque<RegistrationRecord> mRegistrationRecordDeque =
            new ConcurrentLinkedEvictingDeque<>(NUM_CLIENT_RECORDS);

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(
            prefix = {"ACTION_"},
            value = {
                ACTION_REGISTERED,
                ACTION_UNREGISTERED,
            })
    public @interface Action {}

    public static final int ACTION_REGISTERED = 0;
    public static final int ACTION_UNREGISTERED = 1;

    /** A container class to store a record of ContextHubEndpointBroker registrations. */
    private class RegistrationRecord {
        private final String mBroker;
        private final int mAction;
        private final long mTimestamp;

        RegistrationRecord(String broker, @Action int action) {
            mBroker = broker;
            mAction = action;
            mTimestamp = System.currentTimeMillis();
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(ContextHubServiceUtil.formatDateFromTimestamp(mTimestamp));
            sb.append(" ");
            sb.append(mAction == ACTION_REGISTERED ? "+ " : "- ");
            sb.append(mBroker);
            return sb.toString();
        }
    }

    /* package */ ContextHubEndpointManager(
            Context context,
            IContextHubWrapper contextHubProxy,
@@ -228,6 +274,7 @@ import java.util.function.Consumer;
            return null;
        }

        mRegistrationRecordDeque.add(new RegistrationRecord(broker.toString(), ACTION_REGISTERED));
        Log.d(TAG, "Registered endpoint with ID = " + endpointId);
        return IContextHubEndpoint.Stub.asInterface(broker);
    }
@@ -274,7 +321,11 @@ import java.util.function.Consumer;
     * @param endpointId The ID of the endpoint to unregister.
     */
    /* package */ void unregisterEndpoint(long endpointId) {
        mEndpointMap.remove(endpointId);
        ContextHubEndpointBroker broker = mEndpointMap.remove(endpointId);
        if (broker != null) {
            mRegistrationRecordDeque.add(
                    new RegistrationRecord(broker.toString(), ACTION_UNREGISTERED));
        }
    }

    /** Invoked by the service when the Context Hub HAL restarts. */
@@ -366,6 +417,27 @@ import java.util.function.Consumer;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        int count = 1;
        for (ContextHubEndpointBroker broker : mEndpointMap.values()) {
            sb.append(count + ". " + broker);
            sb.append(System.lineSeparator());
            count++;
        }

        sb.append(System.lineSeparator());
        sb.append("Registration History:");
        sb.append(System.lineSeparator());
        Iterator<RegistrationRecord> it = mRegistrationRecordDeque.descendingIterator();
        while (it.hasNext()) {
            sb.append(it.next());
            sb.append(System.lineSeparator());
        }
        return sb.toString();
    }

    /**
     * Invokes a callback for a session with matching ID.
     *
+6 −0
Original line number Diff line number Diff line
@@ -1579,6 +1579,12 @@ public class ContextHubService extends IContextHubService.Stub {
        pw.println("=================== CLIENTS ====================");
        pw.println(mClientManager);

        if (mEndpointManager != null) {
            pw.println("");
            pw.println("=================== ENDPOINTS ====================");
            pw.println(mEndpointManager);
        }

        pw.println("");
        pw.println("=================== TRANSACTIONS ====================");
        pw.println(mTransactionManager);