Merge "Added handling apps query response from context hub" into nyc-dev

This commit is contained in:
Ashutosh Joshi
2016-04-02 04:18:37 +00:00
committed by Android (Google) Code Review
8 changed files with 578 additions and 172 deletions

View File

@@ -37,6 +37,7 @@ public class ContextHubInfo {
private float mStoppedPowerDrawMw;
private float mSleepPowerDrawMw;
private float mPeakPowerDrawMw;
private int mMaxPacketLengthBytes;
private int[] mSupportedSensors;
@@ -45,6 +46,27 @@ public class ContextHubInfo {
public ContextHubInfo() {
}
/**
* returns the maximum number of bytes that can be sent per message to the hub
*
* @return int - maximum bytes that can be transmitted in a
* single packet
*/
public int getMaxPacketLengthBytes() {
return mMaxPacketLengthBytes;
}
/**
* set the context hub unique identifer
*
* @param bytes - Maximum number of bytes per message
*
* @hide
*/
public void setMaxPacketLenBytes(int bytes) {
mMaxPacketLengthBytes = bytes;
}
/**
* get the context hub unique identifer
*
@@ -374,4 +396,4 @@ public class ContextHubInfo {
return new ContextHubInfo[size];
}
};
}
}

View File

@@ -42,23 +42,6 @@ public final class ContextHubManager {
private Callback mCallback;
private Handler mCallbackHandler;
/**
* A special context hub identifier meaning any possible hub on the system.
*/
public static final int ANY_HUB = -1;
/**
* A constant denoting a message to load a a Nano App
*/
public static final int MSG_LOAD_NANO_APP = 1;
/**
* A constant denoting a message to unload a a Nano App
*/
public static final int MSG_UNLOAD_NANO_APP = 2;
/**
* A constant denoting a message to send a message
*/
public static final int MSG_DATA_SEND = 3;
/**
* An interface to receive asynchronous communication from the context hub.
*/
@@ -69,7 +52,7 @@ public final class ContextHubManager {
* Callback function called on message receipt from context hub.
*
* @param hubHandle Handle (system-wide unique identifier) of the hub of the message.
* @param nanoAppHandle Handle (unique identifier) for the app that sent the message.
* @param nanoAppHandle Handle (unique identifier) for app instance that sent the message.
* @param message The context hub message.
*
* @see ContextHubMessage
@@ -89,7 +72,7 @@ public final class ContextHubManager {
try {
retVal = getBinder().getContextHubHandles();
} catch (RemoteException e) {
Log.e(TAG, "Could not fetch context hub handles : " + e);
Log.w(TAG, "Could not fetch context hub handles : " + e);
}
return retVal;
}
@@ -107,7 +90,7 @@ public final class ContextHubManager {
try {
retVal = getBinder().getContextHubInfo(hubHandle);
} catch (RemoteException e) {
Log.e(TAG, "Could not fetch context hub info :" + e);
Log.w(TAG, "Could not fetch context hub info :" + e);
}
return retVal;
@@ -126,6 +109,7 @@ public final class ContextHubManager {
*/
public int loadNanoApp(int hubHandle, NanoApp app) {
int retVal = -1;
if (app == null) {
return retVal;
}
@@ -133,7 +117,7 @@ public final class ContextHubManager {
try {
retVal = getBinder().loadNanoApp(hubHandle, app);
} catch (RemoteException e) {
Log.e(TAG, "Could not fetch load nanoApp :" + e);
Log.w(TAG, "Could not load nanoApp :" + e);
}
return retVal;
@@ -152,7 +136,7 @@ public final class ContextHubManager {
try {
retVal = getBinder().unloadNanoApp(nanoAppHandle);
} catch (RemoteException e) {
Log.e(TAG, "Could not fetch unload nanoApp :" + e);
Log.w(TAG, "Could not fetch unload nanoApp :" + e);
}
return retVal;
@@ -172,7 +156,7 @@ public final class ContextHubManager {
try {
retVal = getBinder().getNanoAppInstanceInfo(nanoAppHandle);
} catch (RemoteException e) {
Log.e(TAG, "Could not fetch nanoApp info :" + e);
Log.w(TAG, "Could not fetch nanoApp info :" + e);
}
return retVal;
@@ -193,7 +177,7 @@ public final class ContextHubManager {
try {
retVal = getBinder().findNanoAppOnHub(hubHandle, filter);
} catch (RemoteException e) {
Log.e(TAG, "Could not query nanoApp instance :" + e);
Log.w(TAG, "Could not query nanoApp instance :" + e);
}
return retVal;
}
@@ -212,10 +196,14 @@ public final class ContextHubManager {
public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage message) {
int retVal = -1;
if (message == null || message.getData() == null) {
Log.w(TAG, "null ptr");
return retVal;
}
try {
retVal = getBinder().sendMessage(hubHandle, nanoAppHandle, message);
} catch (RemoteException e) {
Log.e(TAG, "Could not fetch send message :" + e.toString());
Log.w(TAG, "Could not send message :" + e.toString());
}
return retVal;
@@ -247,7 +235,7 @@ public final class ContextHubManager {
public int registerCallback(Callback callback, Handler handler) {
synchronized(this) {
if (mCallback != null) {
Log.e(TAG, "Max number of callbacks reached!");
Log.w(TAG, "Max number of callbacks reached!");
return -1;
}
mCallback = callback;
@@ -268,7 +256,7 @@ public final class ContextHubManager {
public int unregisterCallback(Callback callback) {
synchronized(this) {
if (callback != mCallback) {
Log.e(TAG, "Cannot recognize callback!");
Log.w(TAG, "Cannot recognize callback!");
return -1;
}
@@ -311,11 +299,11 @@ public final class ContextHubManager {
try {
getBinder().registerCallback(mClientCallback);
} catch (RemoteException e) {
Log.e(TAG, "Could not register callback:" + e);
Log.w(TAG, "Could not register callback:" + e);
}
} else {
Log.d(TAG, "failed to getService");
Log.w(TAG, "failed to getService");
}
}

View File

@@ -16,10 +16,10 @@
package android.hardware.location;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import java.util.Arrays;
@@ -32,6 +32,9 @@ public class ContextHubMessage {
private int mVersion;
private byte[]mData;
private static final String TAG = "ContextHubMessage";
/**
* Get the message type
*
@@ -106,9 +109,11 @@ public class ContextHubMessage {
private ContextHubMessage(Parcel in) {
mType = in.readInt();
mVersion = in.readInt();
byte[] byteBuffer = new byte[in.readInt()];
in.readByteArray(byteBuffer);
int bufferLength = in.readInt();
mData = new byte[bufferLength];
in.readByteArray(mData);
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mType);
out.writeInt(mVersion);

View File

@@ -29,12 +29,30 @@ import java.util.HashMap;
*/
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";
public static final String CONTEXTHUB_SERVICE = "contexthub_service";
public static final int ANY_HUB = -1;
public static final int MSG_LOAD_NANO_APP = 5;
public static final int MSG_UNLOAD_NANO_APP = 2;
private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown";
private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN;
private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN;
private static final int PRE_LOADED_APP_MEM_REQ = 0;
private static final int MSG_HEADER_SIZE = 4;
private static final int MSG_FIELD_TYPE = 0;
private static final int MSG_FIELD_VERSION = 1;
private static final int MSG_FIELD_HUB_HANDLE = 2;
private static final int MSG_FIELD_APP_INSTANCE = 3;
private static final int OS_APP_INSTANCE = -1;
private final Context mContext;
@@ -42,44 +60,27 @@ public class ContextHubService extends IContextHubService.Stub {
private ContextHubInfo[] mContextHubInfo;
private IContextHubCallback mCallback;
private native int nativeSendMessage(int[] header, byte[] data);
private native ContextHubInfo[] nativeInitialize();
public ContextHubService(Context context) {
mContext = context;
mContextHubInfo = nativeInitialize();
mNanoAppHash = new HashMap<Integer, NanoAppInstanceInfo>();
for (int i = 0; i < mContextHubInfo.length; i++) {
Log.v(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
+ ", name: " + mContextHubInfo[i].getName());
}
}
private native int nativeSendMessage(int[] header, byte[] data);
private native ContextHubInfo[] nativeInitialize();
@Override
public int registerCallback(IContextHubCallback callback) throws RemoteException{
public int registerCallback(IContextHubCallback callback) throws RemoteException {
checkPermissions();
mCallback = callback;
return 0;
}
private int onMessageReceipt(int[] header, byte[] data) {
if (mCallback != null) {
// TODO : Defend against unexpected header sizes
// Add abstraction for magic numbers
// onMessageRecipt should pass the right arguments
ContextHubMessage msg = new ContextHubMessage(header[0], header[1], data);
try {
mCallback.onMessageReceipt(0, 0, msg);
} catch (Exception e) {
Log.e(TAG, "Exception " + e + " when calling remote callback");
return -1;
}
} else {
Log.d(TAG, "Message Callback is NULL");
synchronized(this) {
mCallback = callback;
}
return 0;
}
@@ -118,14 +119,17 @@ public class ContextHubService extends IContextHubService.Stub {
}
// Call Native interface here
int[] msgHeader = new int[8];
msgHeader[0] = contextHubHandle;
msgHeader[1] = app.getAppId();
msgHeader[2] = app.getAppVersion();
msgHeader[3] = ContextHubManager.MSG_LOAD_NANO_APP;
msgHeader[4] = 0; // Loading hints
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_HUB_HANDLE] = contextHubHandle;
msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
msgHeader[MSG_FIELD_VERSION] = 0;
msgHeader[MSG_FIELD_TYPE] = MSG_LOAD_NANO_APP;
return nativeSendMessage(msgHeader, app.getAppBinary());
if (nativeSendMessage(msgHeader, app.getAppBinary()) != 0) {
return -1;
}
// Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
return 0;
}
@Override
@@ -137,12 +141,18 @@ public class ContextHubService extends IContextHubService.Stub {
}
// Call Native interface here
int[] msgHeader = new int[8];
msgHeader[0] = info.getContexthubId();
msgHeader[1] = ContextHubManager.MSG_UNLOAD_NANO_APP;
msgHeader[2] = info.getHandle();
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB;
msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
msgHeader[MSG_FIELD_VERSION] = 0;
msgHeader[MSG_FIELD_TYPE] = MSG_UNLOAD_NANO_APP;
return nativeSendMessage(msgHeader, null);
if(nativeSendMessage(msgHeader, null) != 0) {
return -1;
}
// Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
return 0;
}
@Override
@@ -166,7 +176,7 @@ public class ContextHubService extends IContextHubService.Stub {
for(Integer nanoAppInstance : mNanoAppHash.keySet()) {
NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance);
if(filter.testMatch(info)){
if (filter.testMatch(info)){
foundInstances.add(nanoAppInstance);
}
}
@@ -183,12 +193,12 @@ public class ContextHubService extends IContextHubService.Stub {
public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg)
throws RemoteException {
checkPermissions();
int[] msgHeader = new int[8];
msgHeader[0] = ContextHubManager.MSG_DATA_SEND;
msgHeader[1] = hubHandle;
msgHeader[2] = nanoAppHandle;
msgHeader[3] = msg.getMsgType();
msgHeader[4] = msg.getVersion();
int[] msgHeader = new int[MSG_HEADER_SIZE];
msgHeader[MSG_FIELD_HUB_HANDLE] = hubHandle;
msgHeader[MSG_FIELD_APP_INSTANCE] = nanoAppHandle;
msgHeader[MSG_FIELD_VERSION] = msg.getVersion();
msgHeader[MSG_FIELD_TYPE] = msg.getMsgType();
return nativeSendMessage(msgHeader, msg.getData());
}
@@ -196,5 +206,52 @@ public class ContextHubService extends IContextHubService.Stub {
private void checkPermissions() {
mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE);
}
}
private int onMessageReceipt(int[] header, byte[] data) {
if (header == null || data == null || header.length < MSG_HEADER_SIZE) {
return -1;
}
synchronized(this) {
if (mCallback != null) {
ContextHubMessage msg = new ContextHubMessage(header[MSG_FIELD_TYPE],
header[MSG_FIELD_VERSION],
data);
try {
mCallback.onMessageReceipt(header[MSG_FIELD_HUB_HANDLE],
header[MSG_FIELD_APP_INSTANCE],
msg);
} catch (Exception e) {
Log.w(TAG, "Exception " + e + " when calling remote callback");
return -1;
}
} else {
Log.d(TAG, "Message Callback is NULL");
}
}
return 0;
}
private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) {
// App Id encodes vendor & version
NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo();
appInfo.setAppId(appId);
appInfo.setAppVersion(appVersion);
appInfo.setName(PRE_LOADED_APP_NAME);
appInfo.setContexthubId(hubHandle);
appInfo.setHandle(appInstanceHandle);
appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER);
appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ);
appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ);
appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ);
mNanoAppHash.put(appInstanceHandle, appInfo);
Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId
+ " version " + appVersion);
return 0;
}
}

View File

@@ -20,6 +20,7 @@ package android.hardware.location;
import android.annotation.SystemApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
/**
* @hide
@@ -27,6 +28,8 @@ import android.os.Parcelable;
@SystemApi
public class NanoAppFilter {
private static final String TAG = "NanoAppFilter";
// The appId, can be set to APP_ID_ANY
private long mAppId;
@@ -54,6 +57,10 @@ public class NanoAppFilter {
* If this flag is set, only versions strictly less than the version specified shall match.
*/
public static final int FLAGS_VERSION_LESS_THAN = 4;
/**
* If this flag is set, only versions strictly equal to the
* version specified shall match.
*/
public static final int FLAGS_VERSION_STRICTLY_EQUAL = 8;
/**
@@ -117,14 +124,9 @@ public class NanoAppFilter {
* @return true if this is a match, false otherwise
*/
public boolean testMatch(NanoAppInstanceInfo info) {
if ((mContextHubId == HUB_ANY || info.getContexthubId() == mContextHubId) &&
return (mContextHubId == HUB_ANY || info.getContexthubId() == mContextHubId) &&
(mAppId == APP_ANY || info.getAppId() == mAppId) &&
// (mAppIdVendorMask == VENDOR_ANY) TODO : Expose Vendor mask cleanly
(versionsMatch(mVersionRestrictionMask, mAppVersion, info.getAppVersion()))) {
return true;
} else {
return false;
}
(versionsMatch(mVersionRestrictionMask, mAppVersion, info.getAppVersion()));
}
public static final Parcelable.Creator<NanoAppFilter> CREATOR

View File

@@ -29,7 +29,7 @@ public class NanoAppInstanceInfo {
private String mPublisher;
private String mName;
private int mAppId;
private long mAppId;
private int mAppVersion;
private int mNeededReadMemBytes;
@@ -59,6 +59,8 @@ public class NanoAppInstanceInfo {
* set the publisher name for the app
*
* @param publisher - name of the publisher
*
* @hide
*/
public void setPublisher(String publisher) {
mPublisher = publisher;
@@ -77,6 +79,8 @@ public class NanoAppInstanceInfo {
* set the name of the app
*
* @param name - name of the app
*
* @hide
*/
public void setName(String name) {
mName = name;
@@ -87,7 +91,7 @@ public class NanoAppInstanceInfo {
*
* @return int - application identifier
*/
public int getAppId() {
public long getAppId() {
return mAppId;
}
@@ -95,8 +99,10 @@ public class NanoAppInstanceInfo {
* Set the application identifier
*
* @param appId - application identifier
*
* @hide
*/
public void setAppId(int appId) {
public void setAppId(long appId) {
mAppId = appId;
}
@@ -113,6 +119,8 @@ public class NanoAppInstanceInfo {
* Set the application version
*
* @param appVersion - version of the app
*
* @hide
*/
public void setAppVersion(int appVersion) {
mAppVersion = appVersion;
@@ -131,6 +139,8 @@ public class NanoAppInstanceInfo {
* Set the read memory needed by the app
*
* @param neededReadMemBytes - readable Memory needed in bytes
*
* @hide
*/
public void setNeededReadMemBytes(int neededReadMemBytes) {
mNeededReadMemBytes = neededReadMemBytes;
@@ -150,6 +160,8 @@ public class NanoAppInstanceInfo {
*
* @param neededWriteMemBytes - writable memory needed by the
* app
*
* @hide
*/
public void setNeededWriteMemBytes(int neededWriteMemBytes) {
mNeededWriteMemBytes = neededWriteMemBytes;
@@ -169,6 +181,8 @@ public class NanoAppInstanceInfo {
*
* @param neededExecMemBytes - executable memory needed by the
* app
*
* @hide
*/
public void setNeededExecMemBytes(int neededExecMemBytes) {
mNeededExecMemBytes = neededExecMemBytes;
@@ -187,6 +201,8 @@ public class NanoAppInstanceInfo {
* set the sensors needed by this app
*
* @param neededSensors - all the sensors needed by this app
*
* @hide
*/
public void setNeededSensors(int[] neededSensors) {
mNeededSensors = neededSensors;
@@ -206,6 +222,8 @@ public class NanoAppInstanceInfo {
*
* @param outputEvents - the events that may be generated by
* this app
*
* @hide
*/
public void setOutputEvents(int[] outputEvents) {
mOutputEvents = outputEvents;
@@ -224,6 +242,8 @@ public class NanoAppInstanceInfo {
* set the context hub identifier
*
* @param contexthubId - system wide unique identifier
*
* @hide
*/
public void setContexthubId(int contexthubId) {
mContexthubId = contexthubId;
@@ -242,6 +262,8 @@ public class NanoAppInstanceInfo {
* set the handle for an app instance
*
* @param handle - handle to this instance
*
* @hide
*/
public void setHandle(int handle) {
mHandle = handle;
@@ -252,7 +274,7 @@ public class NanoAppInstanceInfo {
mPublisher = in.readString();
mName = in.readString();
mAppId = in.readInt();
mAppId = in.readLong();
mAppVersion = in.readInt();
mNeededReadMemBytes = in.readInt();
mNeededWriteMemBytes = in.readInt();
@@ -274,7 +296,7 @@ public class NanoAppInstanceInfo {
public void writeToParcel(Parcel out, int flags) {
out.writeString(mPublisher);
out.writeString(mName);
out.writeInt(mAppId);
out.writeLong(mAppId);
out.writeInt(mAppVersion);
out.writeInt(mContexthubId);
out.writeInt(mNeededReadMemBytes);
@@ -286,7 +308,6 @@ public class NanoAppInstanceInfo {
out.writeInt(mOutputEvents.length);
out.writeIntArray(mOutputEvents);
}
public static final Parcelable.Creator<NanoAppInstanceInfo> CREATOR