From 6239cc6fe1d18ce4365c56346a09c7963dfa8f10 Mon Sep 17 00:00:00 2001 From: Ashutosh Joshi Date: Mon, 4 Apr 2016 16:19:29 -0700 Subject: [PATCH] Add dumpsys capabilities for context hub service Add useful debug hook for context hub service. Fix constants Use helper functions to address OS Change-Id: Iba5f24a9a1fd809c3d83752953389d373ff334ca --- .../hardware/location/ContextHubInfo.java | 18 +++++ .../hardware/location/ContextHubService.java | 66 +++++++++++++------ .../hardware/location/MemoryRegion.java | 27 ++++++++ .../android/hardware/location/NanoApp.java | 10 +++ .../location/NanoAppInstanceInfo.java | 11 ++++ ...id_hardware_location_ContextHubService.cpp | 7 +- 6 files changed, 117 insertions(+), 22 deletions(-) diff --git a/core/java/android/hardware/location/ContextHubInfo.java b/core/java/android/hardware/location/ContextHubInfo.java index ae44f1d68c3cb..194b9ee587807 100644 --- a/core/java/android/hardware/location/ContextHubInfo.java +++ b/core/java/android/hardware/location/ContextHubInfo.java @@ -345,6 +345,24 @@ public class ContextHubInfo { mMemoryRegions = Arrays.copyOf(memoryRegions, memoryRegions.length); } + @Override + public String toString() { + String retVal = ""; + retVal += "Id : " + mId; + retVal += ", Name : " + mName; + retVal += "\n\tVendor : " + mVendor; + retVal += ", ToolChain : " + mToolchain; + retVal += "\n\tPlatformVersion : " + mPlatformVersion; + retVal += ", StaticSwVersion : " + mStaticSwVersion; + retVal += "\n\tPeakMips : " + mPeakMips; + retVal += ", StoppedPowerDraw : " + mStoppedPowerDrawMw + " mW"; + retVal += ", PeakPowerDraw : " + mPeakPowerDrawMw + " mW"; + retVal += "\n\tSupported sensors : " + Arrays.toString(mSupportedSensors); + retVal += "\n\tMemory Regions : " + Arrays.toString(mMemoryRegions); + + return retVal; + } + private ContextHubInfo(Parcel in) { mId = in.readInt(); mName = in.readString(); diff --git a/core/java/android/hardware/location/ContextHubService.java b/core/java/android/hardware/location/ContextHubService.java index 3e6cb63d9f633..2b9b97463888a 100644 --- a/core/java/android/hardware/location/ContextHubService.java +++ b/core/java/android/hardware/location/ContextHubService.java @@ -18,9 +18,12 @@ package android.hardware.location; import android.Manifest; import android.content.Context; +import android.content.pm.PackageManager; import android.os.RemoteException; import android.util.Log; +import java.io.FileDescriptor; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; @@ -28,13 +31,12 @@ import java.util.HashMap; * @hide */ public class ContextHubService extends IContextHubService.Stub { - public static final String CONTEXTHUB_SERVICE = "contexthub_service"; private static final String TAG = "ContextHubService"; private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE; private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '" - + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware"; + + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware"; public static final int ANY_HUB = -1; @@ -78,8 +80,8 @@ public class ContextHubService extends IContextHubService.Stub { @Override public int registerCallback(IContextHubCallback callback) throws RemoteException { checkPermissions(); - synchronized(this) { - mCallback = callback; + synchronized (this) { + mCallback = callback; } return 0; } @@ -87,10 +89,10 @@ public class ContextHubService extends IContextHubService.Stub { @Override public int[] getContextHubHandles() throws RemoteException { checkPermissions(); - int [] returnArray = new int[mContextHubInfo.length]; + int[] returnArray = new int[mContextHubInfo.length]; for (int i = 0; i < returnArray.length; ++i) { - returnArray[i] = i + 1; //valid handles from 1...n + returnArray[i] = i; Log.d(TAG, String.format("Hub %s is mapped to %d", mContextHubInfo[i].getName(), returnArray[i])); } @@ -101,7 +103,6 @@ public class ContextHubService extends IContextHubService.Stub { @Override public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException { checkPermissions(); - contextHubHandle -= 1; if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { return null; // null means fail } @@ -112,13 +113,12 @@ public class ContextHubService extends IContextHubService.Stub { @Override public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException { checkPermissions(); - contextHubHandle -= 1; if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { - return -1; // negative handle are invalid, means failed + Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle); + return -1; } - // Call Native interface here int[] msgHeader = new int[MSG_HEADER_SIZE]; msgHeader[MSG_FIELD_HUB_HANDLE] = contextHubHandle; msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; @@ -147,7 +147,7 @@ public class ContextHubService extends IContextHubService.Stub { msgHeader[MSG_FIELD_VERSION] = 0; msgHeader[MSG_FIELD_TYPE] = MSG_UNLOAD_NANO_APP; - if(nativeSendMessage(msgHeader, null) != 0) { + if (nativeSendMessage(msgHeader, null) != 0) { return -1; } @@ -157,7 +157,7 @@ public class ContextHubService extends IContextHubService.Stub { @Override public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) - throws RemoteException { + throws RemoteException { checkPermissions(); // This assumes that all the nanoAppInfo is current. This is reasonable // for the use cases for tightly controlled nanoApps. @@ -173,10 +173,10 @@ public class ContextHubService extends IContextHubService.Stub { checkPermissions(); ArrayList foundInstances = new ArrayList(); - for(Integer nanoAppInstance : mNanoAppHash.keySet()) { + for (Integer nanoAppInstance: mNanoAppHash.keySet()) { NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); - if (filter.testMatch(info)){ + if (filter.testMatch(info)) { foundInstances.add(nanoAppInstance); } } @@ -191,7 +191,7 @@ public class ContextHubService extends IContextHubService.Stub { @Override public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) - throws RemoteException { + throws RemoteException { checkPermissions(); int[] msgHeader = new int[MSG_HEADER_SIZE]; @@ -203,6 +203,32 @@ public class ContextHubService extends IContextHubService.Stub { return nativeSendMessage(msgHeader, msg.getData()); } + @Override + protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { + if (mContext.checkCallingOrSelfPermission("android.permission.DUMP") + != PackageManager.PERMISSION_GRANTED) { + pw.println("Permission Denial: can't dump contexthub_service"); + return; + } + + pw.println("Dumping ContextHub Service"); + + pw.println(""); + // dump ContextHubInfo + pw.println("=================== CONTEXT HUBS ===================="); + for (int i = 0; i < mContextHubInfo.length; i++) { + pw.println("Handle " + i + " : " + mContextHubInfo[i].toString()); + } + pw.println(""); + pw.println("=================== NANOAPPS ===================="); + // Dump nanoAppHash + for (Integer nanoAppInstance: mNanoAppHash.keySet()) { + pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString()); + } + + // dump eventLog + } + private void checkPermissions() { mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE); } @@ -212,16 +238,16 @@ public class ContextHubService extends IContextHubService.Stub { return -1; } - synchronized(this) { + synchronized (this) { if (mCallback != null) { ContextHubMessage msg = new ContextHubMessage(header[MSG_FIELD_TYPE], - header[MSG_FIELD_VERSION], - data); + header[MSG_FIELD_VERSION], + data); try { mCallback.onMessageReceipt(header[MSG_FIELD_HUB_HANDLE], - header[MSG_FIELD_APP_INSTANCE], - msg); + header[MSG_FIELD_APP_INSTANCE], + msg); } catch (Exception e) { Log.w(TAG, "Exception " + e + " when calling remote callback"); return -1; diff --git a/core/java/android/hardware/location/MemoryRegion.java b/core/java/android/hardware/location/MemoryRegion.java index d100de2575497..857434ea75b98 100644 --- a/core/java/android/hardware/location/MemoryRegion.java +++ b/core/java/android/hardware/location/MemoryRegion.java @@ -78,6 +78,33 @@ public class MemoryRegion implements Parcelable{ return mIsExecutable; } + @Override + public String toString() { + String mask = ""; + + if (isReadable()) { + mask += "r"; + } else { + mask += "-"; + } + + if (isWritable()) { + mask += "w"; + } else { + mask += "-"; + } + + if (isExecutable()) { + mask += "x"; + } else { + mask += "-"; + } + + String retVal = "[ " + mSizeBytesFree + "/ " + mSizeBytes + " ] : " + mask; + + return retVal; + } + @Override public int describeContents() { return 0; diff --git a/core/java/android/hardware/location/NanoApp.java b/core/java/android/hardware/location/NanoApp.java index b447b62457122..c8f3439aee700 100644 --- a/core/java/android/hardware/location/NanoApp.java +++ b/core/java/android/hardware/location/NanoApp.java @@ -284,4 +284,14 @@ public class NanoApp { return new NanoApp[size]; } }; + + @Override + public String toString() { + String retVal = "Id : " + mAppId; + retVal += ", Version : " + mAppVersion; + retVal += ", Name : " + mName; + retVal += ", Publisher : " + mPublisher; + + return retVal; + } } diff --git a/core/java/android/hardware/location/NanoAppInstanceInfo.java b/core/java/android/hardware/location/NanoAppInstanceInfo.java index 977f645db199c..e842ec65a3e71 100644 --- a/core/java/android/hardware/location/NanoAppInstanceInfo.java +++ b/core/java/android/hardware/location/NanoAppInstanceInfo.java @@ -320,4 +320,15 @@ public class NanoAppInstanceInfo { return new NanoAppInstanceInfo[size]; } }; + + @Override + public String toString() { + String retVal = "handle : " + mHandle; + retVal += ", Id : 0x" + Long.toHexString(mAppId); + retVal += ", Version : " + mAppVersion; + retVal += ", Name : " + mName; + retVal += ", Publisher : " + mPublisher; + + return retVal; + } } diff --git a/core/jni/android_hardware_location_ContextHubService.cpp b/core/jni/android_hardware_location_ContextHubService.cpp index 80ae550710be7..91a3b4f770521 100644 --- a/core/jni/android_hardware_location_ContextHubService.cpp +++ b/core/jni/android_hardware_location_ContextHubService.cpp @@ -382,6 +382,9 @@ int handle_os_message(uint32_t msgType, uint32_t hubHandle, char *msg, int msgLen) { int retVal; + //ALOGD("Rcd OS message from hubHandle %" PRIu32 " type %" PRIu32 " length %d", + // hubHandle, msgType, msgLen); + switch(msgType) { case CONTEXT_HUB_APPS_ENABLE: retVal = 0; @@ -633,7 +636,7 @@ static jint nativeSendMessage(JNIEnv *env, jobject instance, jintArray header_, if (numHeaderElements >= MSG_HEADER_SIZE) { - int setAddressSuccess; + bool setAddressSuccess; int hubId; hub_message_t msg; @@ -654,7 +657,7 @@ static jint nativeSendMessage(JNIEnv *env, jobject instance, jintArray header_, ALOGD("Could not find app instance %d on hubHandle %d, setAddress %d", header[HEADER_FIELD_APP_INSTANCE], header[HEADER_FIELD_HUB_HANDLE], - setAddressSuccess); + (int)setAddressSuccess); } } else { ALOGD("Malformed header len");