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

Commit a83a1dc3 authored by Ashutosh Joshi's avatar Ashutosh Joshi Committed by Android (Google) Code Review
Browse files

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

parents 5d580baa b741e3b3
Loading
Loading
Loading
Loading
+2 −16
Original line number Diff line number Diff line
@@ -15271,6 +15271,7 @@ package android.hardware.location {
    ctor public ContextHubInfo();
    method public int describeContents();
    method public int getId();
    method public int getMaxPacketLengthBytes();
    method public android.hardware.location.MemoryRegion[] getMemoryRegions();
    method public java.lang.String getName();
    method public float getPeakMips();
@@ -15298,10 +15299,6 @@ package android.hardware.location {
    method public int sendMessage(int, int, android.hardware.location.ContextHubMessage);
    method public int unloadNanoApp(int);
    method public int unregisterCallback(android.hardware.location.ContextHubManager.Callback);
    field public static final int ANY_HUB = -1; // 0xffffffff
    field public static final int MSG_DATA_SEND = 3; // 0x3
    field public static final int MSG_LOAD_NANO_APP = 1; // 0x1
    field public static final int MSG_UNLOAD_NANO_APP = 2; // 0x2
  }
  public static abstract class ContextHubManager.Callback {
@@ -15495,7 +15492,7 @@ package android.hardware.location {
  public class NanoAppInstanceInfo {
    ctor public NanoAppInstanceInfo();
    method public int describeContents();
    method public int getAppId();
    method public long getAppId();
    method public int getAppVersion();
    method public int getContexthubId();
    method public int getHandle();
@@ -15506,17 +15503,6 @@ package android.hardware.location {
    method public int getNeededWriteMemBytes();
    method public int[] getOutputEvents();
    method public java.lang.String getPublisher();
    method public void setAppId(int);
    method public void setAppVersion(int);
    method public void setContexthubId(int);
    method public void setHandle(int);
    method public void setName(java.lang.String);
    method public void setNeededExecMemBytes(int);
    method public void setNeededReadMemBytes(int);
    method public void setNeededSensors(int[]);
    method public void setNeededWriteMemBytes(int);
    method public void setOutputEvents(int[]);
    method public void setPublisher(java.lang.String);
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.hardware.location.NanoAppInstanceInfo> CREATOR;
  }
+23 −1
Original line number Diff line number Diff line
@@ -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
     *
+17 −29
Original line number Diff line number Diff line
@@ -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");
        }
    }

+8 −3
Original line number Diff line number Diff line
@@ -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);
+105 −48
Original line number Diff line number Diff line
@@ -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 {
        checkPermissions();
        synchronized(this) {
          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");
        }

        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;

        if(nativeSendMessage(msgHeader, null) != 0) {
            return -1;
        }

        return nativeSendMessage(msgHeader, null);
        // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
        return 0;
    }

    @Override
@@ -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;
    }
}
Loading