From 1d1ac5409597ef668e52c6f27293eaf9af575e33 Mon Sep 17 00:00:00 2001 From: Ashutosh Joshi Date: Mon, 18 Jan 2016 17:19:27 -0800 Subject: [PATCH] Context hub API for applications Adding a System API for applications to access a context hub. The API is not expected to be exposed to third party Android applications in the medium term. Change-Id: Ia2222317c913de6901f3912e4c89b5cfa398be85 --- Android.mk | 2 + api/system-current.txt | 143 +++++++ .../hardware/location/ContextHubInfo.aidl | 21 ++ .../hardware/location/ContextHubInfo.java | 351 ++++++++++++++++++ .../hardware/location/ContextHubManager.java | 278 ++++++++++++++ .../hardware/location/ContextHubMessage.aidl | 22 ++ .../hardware/location/ContextHubMessage.java | 129 +++++++ .../hardware/location/ContextHubService.java | 203 ++++++++++ .../location/IContextHubCallback.aidl | 24 ++ .../hardware/location/IContextHubService.aidl | 53 +++ .../hardware/location/MemoryRegion.java | 115 ++++++ .../android/hardware/location/NanoApp.aidl | 21 ++ .../android/hardware/location/NanoApp.java | 290 +++++++++++++++ .../hardware/location/NanoAppFilter.aidl | 21 ++ .../hardware/location/NanoAppFilter.java | 139 +++++++ .../location/NanoAppInstanceInfo.aidl | 21 ++ .../location/NanoAppInstanceInfo.java | 302 +++++++++++++++ 17 files changed, 2135 insertions(+) create mode 100644 core/java/android/hardware/location/ContextHubInfo.aidl create mode 100644 core/java/android/hardware/location/ContextHubInfo.java create mode 100644 core/java/android/hardware/location/ContextHubManager.java create mode 100644 core/java/android/hardware/location/ContextHubMessage.aidl create mode 100644 core/java/android/hardware/location/ContextHubMessage.java create mode 100644 core/java/android/hardware/location/ContextHubService.java create mode 100644 core/java/android/hardware/location/IContextHubCallback.aidl create mode 100644 core/java/android/hardware/location/IContextHubService.aidl create mode 100644 core/java/android/hardware/location/MemoryRegion.java create mode 100644 core/java/android/hardware/location/NanoApp.aidl create mode 100644 core/java/android/hardware/location/NanoApp.java create mode 100644 core/java/android/hardware/location/NanoAppFilter.aidl create mode 100644 core/java/android/hardware/location/NanoAppFilter.java create mode 100644 core/java/android/hardware/location/NanoAppInstanceInfo.aidl create mode 100644 core/java/android/hardware/location/NanoAppInstanceInfo.java diff --git a/Android.mk b/Android.mk index 53e892f3351f1..057de678a417c 100644 --- a/Android.mk +++ b/Android.mk @@ -187,6 +187,8 @@ LOCAL_SRC_FILES += \ core/java/android/hardware/location/IGeofenceHardware.aidl \ core/java/android/hardware/location/IGeofenceHardwareCallback.aidl \ core/java/android/hardware/location/IGeofenceHardwareMonitorCallback.aidl \ + core/java/android/hardware/location/IContextHubCallback.aidl \ + core/java/android/hardware/location/IContextHubService.aidl \ core/java/android/hardware/soundtrigger/IRecognitionStatusCallback.aidl \ core/java/android/hardware/usb/IUsbManager.aidl \ core/java/android/net/ICaptivePortal.aidl \ diff --git a/api/system-current.txt b/api/system-current.txt index 302ebe13187ce..6f951aeb8b90a 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -15055,6 +15055,66 @@ package android.hardware.input { package android.hardware.location { + public class ContextHubInfo { + ctor public ContextHubInfo(); + method public int describeContents(); + method public int getId(); + method public android.hardware.location.MemoryRegion[] getMemoryRegions(); + method public java.lang.String getName(); + method public float getPeakMips(); + method public float getPeakPowerDrawMw(); + method public int getPlatformVersion(); + method public float getSleepPowerDrawMw(); + method public int getStaticSwVersion(); + method public float getStoppedPowerDrawMw(); + method public int[] getSupportedSensors(); + method public java.lang.String getToolchain(); + method public int getToolchainVersion(); + method public java.lang.String getVendor(); + method public void setId(int); + method public void setMemoryRegions(android.hardware.location.MemoryRegion[]); + method public void setName(java.lang.String); + method public void setPeakMips(float); + method public void setPeakPowerDrawMw(float); + method public void setPlatformVersion(int); + method public void setSleepPowerDrawMw(float); + method public void setStaticSwVersion(int); + method public void setStoppedPowerDrawMw(float); + method public void setSupportedSensors(int[]); + method public void setToolchain(java.lang.String); + method public void setToolchainVersion(int); + method public void setVendor(java.lang.String); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator CREATOR; + } + + public final class ContextHubManager { + method public java.lang.Integer[] findNanoAppOnHub(int, android.hardware.location.NanoAppFilter); + method public int[] getContexthubHandles(); + method public android.hardware.location.ContextHubInfo getContexthubInfo(int); + method public android.hardware.location.NanoAppInstanceInfo getNanoAppInstanceInfo(int); + method public int loadNanoApp(int, android.hardware.location.NanoApp); + method public int sendMessage(int, int, android.hardware.location.ContextHubMessage); + method public int unloadNanoApp(int); + 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 class ContextHubMessage { + ctor public ContextHubMessage(int, int, byte[]); + method public int describeContents(); + method public byte[] getData(); + method public int getMsgType(); + method public int getVersion(); + method public void setMsgData(byte[]); + method public void setMsgType(int); + method public void setVersion(int); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator CREATOR; + } + public final class GeofenceHardware { ctor public GeofenceHardware(android.hardware.location.IGeofenceHardware); method public boolean addGeofence(int, int, android.hardware.location.GeofenceHardwareRequest, android.hardware.location.GeofenceHardwareCallback); @@ -15171,6 +15231,89 @@ package android.hardware.location { method public abstract void onMonitoringSystemChange(android.hardware.location.GeofenceHardwareMonitorEvent) throws android.os.RemoteException; } + public class MemoryRegion implements android.os.Parcelable { + ctor public MemoryRegion(android.os.Parcel); + method public int describeContents(); + method public int getCapacityBytes(); + method public int getFreeCapacityBytes(); + method public boolean isExecutable(); + method public boolean isReadable(); + method public boolean isWritable(); + method public void writeToParcel(android.os.Parcel, int); + field public static final android.os.Parcelable.Creator CREATOR; + } + + public class NanoApp { + ctor public NanoApp(); + method public int describeContents(); + method public byte[] getAppBinary(); + method public int getAppId(); + method public int getAppVersion(); + method public java.lang.String getName(); + method public int getNeededExecMemBytes(); + method public int getNeededReadMemBytes(); + method public int[] getNeededSensors(); + method public int getNeededWriteMemBytes(); + method public int[] getOutputEvents(); + method public java.lang.String getPublisher(); + method public void setAppBinary(byte[]); + method public void setAppId(int); + method public void setAppVersion(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 CREATOR; + } + + public class NanoAppFilter { + ctor public NanoAppFilter(long, int, int, long); + method public int describeContents(); + method public boolean testMatch(android.hardware.location.NanoAppInstanceInfo); + method public void writeToParcel(android.os.Parcel, int); + field public static final int APP_ANY = -1; // 0xffffffff + field public static final android.os.Parcelable.Creator CREATOR; + field public static final int FLAGS_VERSION_ANY = -1; // 0xffffffff + field public static final int FLAGS_VERSION_GREAT_THAN = 2; // 0x2 + field public static final int FLAGS_VERSION_LESS_THAN = 4; // 0x4 + field public static final int FLAGS_VERSION_STRICTLY_EQUAL = 8; // 0x8 + field public static final int HUB_ANY = -1; // 0xffffffff + field public static final int VENDOR_ANY = -1; // 0xffffffff + } + + public class NanoAppInstanceInfo { + ctor public NanoAppInstanceInfo(); + method public int describeContents(); + method public int getAppId(); + method public int getAppVersion(); + method public int getContexthubId(); + method public int getHandle(); + method public java.lang.String getName(); + method public int getNeededExecMemBytes(); + method public int getNeededReadMemBytes(); + method public int[] getNeededSensors(); + 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 CREATOR; + } + } package android.hardware.radio { diff --git a/core/java/android/hardware/location/ContextHubInfo.aidl b/core/java/android/hardware/location/ContextHubInfo.aidl new file mode 100644 index 0000000000000..1a9221a02cc3f --- /dev/null +++ b/core/java/android/hardware/location/ContextHubInfo.aidl @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; +/* +@hide +*/ +parcelable ContextHubInfo; diff --git a/core/java/android/hardware/location/ContextHubInfo.java b/core/java/android/hardware/location/ContextHubInfo.java new file mode 100644 index 0000000000000..e47c541ee62be --- /dev/null +++ b/core/java/android/hardware/location/ContextHubInfo.java @@ -0,0 +1,351 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.hardware.location; + +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; + +import java.util.Arrays; + +/** + * @hide + */ +@SystemApi +public class ContextHubInfo { + private int mId; + private String mName; + private String mVendor; + private String mToolchain; + private int mPlatformVersion; + private int mStaticSwVersion; + private int mToolchainVersion; + private float mPeakMips; + private float mStoppedPowerDrawMw; + private float mSleepPowerDrawMw; + private float mPeakPowerDrawMw; + + private int[] mSupportedSensors; + + private MemoryRegion[] mMemoryRegions; + + public ContextHubInfo() { + } + + /** + * get the context hub unique identifer + * + * @return int - unique system wide identifier + */ + public int getId() { + return mId; + } + + /** + * set the context hub unique identifer + * + * @param id - unique system wide identifier for the hub + */ + public void setId(int id) { + mId = id; + } + + /** + * get a string as a hub name + * + * @return String - a name for the hub + */ + public String getName() { + return mName; + } + + /** + * set a string as the hub name + * + * @param String - the name for the hub + */ + public void setName(String name) { + mName = name; + } + + /** + * get a string as the vendor name + * + * @return String - a name for the vendor + */ + public String getVendor() { + return mVendor; + } + + /** + * set a string as the vendor name + * + * @param String - a name for the vendor + */ + public void setVendor(String vendor) { + mVendor = vendor; + } + + /** + * get tool chain string + * + * @return String - description of the tool chain + */ + public String getToolchain() { + return mToolchain; + } + + /** + * set tool chain string + * + * @param String - description of the tool chain + */ + public void setToolchain(String toolchain) { + mToolchain = toolchain; + } + + /** + * get platform version + * + * @return int - platform version number + */ + public int getPlatformVersion() { + return mPlatformVersion; + } + + /** + * set platform version + * + * @param platformVersion - platform version number + */ + public void setPlatformVersion(int platformVersion) { + mPlatformVersion = platformVersion; + } + + /** + * get static platform version number + * + * @return int - platform version number + */ + public int getStaticSwVersion() { + return mStaticSwVersion; + } + + /** + * set platform software version + * + * @param staticSwVersion - platform static s/w version number + */ + public void setStaticSwVersion(int staticSwVersion) { + mStaticSwVersion = staticSwVersion; + } + + /** + * get the tool chain version + * + * @return int - the tool chain version + */ + public int getToolchainVersion() { + return mToolchainVersion; + } + + /** + * set the tool chain version number + * + * @param toolchainVersion - tool chain version number + */ + public void setToolchainVersion(int toolchainVersion) { + mToolchainVersion = toolchainVersion; + } + + /** + * get the peak processing mips the hub can support + * + * @return float - peak MIPS that this hub can deliver + */ + public float getPeakMips() { + return mPeakMips; + } + + /** + * set the peak mips that this hub can support + * + * @param peakMips - peak mips this hub can deliver + */ + public void setPeakMips(float peakMips) { + mPeakMips = peakMips; + } + + /** + * get the stopped power draw in milliwatts + * This assumes that the hub enter a stopped state - which is + * different from the sleep state. Latencies on exiting the + * sleep state are typically higher and expect to be in multiple + * milliseconds. + * + * @return float - power draw by the hub in stopped state + */ + public float getStoppedPowerDrawMw() { + return mStoppedPowerDrawMw; + } + + /** + * Set the power consumed by the hub in stopped state + * + * @param stoppedPowerDrawMw - stopped power in milli watts + */ + public void setStoppedPowerDrawMw(float stoppedPowerDrawMw) { + mStoppedPowerDrawMw = stoppedPowerDrawMw; + } + + /** + * get the power draw of the hub in sleep mode. This assumes + * that the hub supports a sleep mode in which the power draw is + * lower than the power consumed when the hub is actively + * processing. As a guideline, assume that the hub should be + * able to enter sleep mode if it knows reliably on completion + * of some task that the next interrupt/scheduled work item is + * at least 250 milliseconds later. + * + * @return float - sleep power draw in milli watts + */ + public float getSleepPowerDrawMw() { + return mSleepPowerDrawMw; + } + + /** + * Set the sleep power draw in milliwatts + * + * @param sleepPowerDrawMw - sleep power draw in milliwatts. + */ + public void setSleepPowerDrawMw(float sleepPowerDrawMw) { + mSleepPowerDrawMw = sleepPowerDrawMw; + } + + /** + * get the peak powe draw of the hub. This is the power consumed + * by the hub at maximum load. + * + * @return float - peak power draw + */ + public float getPeakPowerDrawMw() { + return mPeakPowerDrawMw; + } + + /** + * set the peak power draw of the hub + * + * @param peakPowerDrawMw - peak power draw of the hub in + * milliwatts. + */ + public void setPeakPowerDrawMw(float peakPowerDrawMw) { + mPeakPowerDrawMw = peakPowerDrawMw; + } + + /** + * get the sensors supported by this hub + * + * @return int[] - all the supported sensors on this hub + * + * @see ContextHubManager + */ + public int[] getSupportedSensors() { + return Arrays.copyOf(mSupportedSensors, mSupportedSensors.length); + } + + /** + * get the various memory regions on this hub + * + * @return MemoryRegion[] - all the memory regions on this hub + * + * @see MemoryRegion + */ + public MemoryRegion[] getMemoryRegions() { + return Arrays.copyOf(mMemoryRegions, mMemoryRegions.length); + } + + /** + * set the supported sensors on this hub + * + * @param supportedSensors - supported sensors on this hub + */ + public void setSupportedSensors(int[] supportedSensors) { + mSupportedSensors = Arrays.copyOf(supportedSensors, supportedSensors.length); + } + + /** + * set memory regions for this hub + * + * @param memoryRegions - memory regions information + * + * @see MemoryRegion + */ + public void setMemoryRegions(MemoryRegion[] memoryRegions) { + mMemoryRegions = Arrays.copyOf(memoryRegions, memoryRegions.length); + } + + private ContextHubInfo(Parcel in) { + mId = in.readInt(); + mName = in.readString(); + mVendor = in.readString(); + mToolchain = in.readString(); + mPlatformVersion = in.readInt(); + mToolchainVersion = in.readInt(); + mStaticSwVersion = in.readInt(); + mPeakMips = in.readFloat(); + mStoppedPowerDrawMw = in.readFloat(); + mSleepPowerDrawMw = in.readFloat(); + mPeakPowerDrawMw = in.readFloat(); + + int numSupportedSensors = in.readInt(); + mSupportedSensors = new int[numSupportedSensors]; + in.readIntArray(mSupportedSensors); + mMemoryRegions = in.createTypedArray(MemoryRegion.CREATOR); + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel out, int flags) { + out.writeInt(mId); + out.writeString(mName); + out.writeString(mVendor); + out.writeString(mToolchain); + out.writeInt(mPlatformVersion); + out.writeInt(mToolchainVersion); + out.writeInt(mStaticSwVersion); + out.writeFloat(mPeakMips); + out.writeFloat(mStoppedPowerDrawMw); + out.writeFloat(mSleepPowerDrawMw); + out.writeFloat(mPeakPowerDrawMw); + + out.writeInt(mSupportedSensors.length); + out.writeIntArray(mSupportedSensors); + out.writeTypedArray(mMemoryRegions, flags); + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public ContextHubInfo createFromParcel(Parcel in) { + return new ContextHubInfo(in); + } + + public ContextHubInfo[] newArray(int size) { + return new ContextHubInfo[size]; + } + }; +} diff --git a/core/java/android/hardware/location/ContextHubManager.java b/core/java/android/hardware/location/ContextHubManager.java new file mode 100644 index 0000000000000..301b2e49a19c6 --- /dev/null +++ b/core/java/android/hardware/location/ContextHubManager.java @@ -0,0 +1,278 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package android.hardware.location; + +import android.annotation.SystemApi; +import android.hardware.location.NanoAppInstanceInfo; +import android.content.ComponentName; +import android.content.Context; +import android.content.ServiceConnection; +import android.Manifest; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Log; + +import java.util.ArrayList; +import java.util.HashMap; + +/** + * A class that exposes the Context hubs on a device to + * applicaions. + * + * Please not that this class is not expected to be used by + * unbundled applications. Also, calling applications are + * expected to have LOCTION_HARDWARE premissions to use this + * class. + * + * @hide + */ +@SystemApi +public final class ContextHubManager { + + private static final String TAG = "ContextHubManager"; + private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); + 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"; + + private Context mContext; + private IContextHubService mContextHubService; + private boolean mContextHubConnected; + + /** + * A special context hub identifer 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; + + + /** + * Get a handle to all the context hubs in the system + * @return array of context hub handles + */ + public int[] getContexthubHandles() { + int[] retVal = null; + if(mContextHubConnected) { + try { + retVal = mContextHubService.getContextHubHandles(); + }catch (RemoteException e) { + Log.e (TAG, "Could not fetch context hub handles :" + e.toString()); + } + } + return retVal; + } + + /** + * Get more information about a specific hub. + * + * @param contexthubHandle Handle of context hub + * + * @return ContextHubInfo returned information about the hub + * + * @see ContextHubInfo + */ + public ContextHubInfo getContexthubInfo(int contexthubHandle) { + ContextHubInfo retVal = null; + if(mContextHubConnected) { + try { + retVal = mContextHubService.getContextHubInfo(contexthubHandle); + }catch (RemoteException e) { + Log.e (TAG, "Could not fetch context hub info :" + e.toString()); + } + } + + return(retVal); + } + + /** + * Load a nanoapp on a specified context hub + * + * @param hubHandle handle of context hub to load the app on. + * @param app the nanoApp to load on the hub + * + * @return int nanoAppInstance of the loaded nanoApp on success, + * -1 otherwise + * + * @see NanoApp + */ + public int loadNanoApp(int hubHandle, NanoApp app) { + int retVal = -1; + + if(mContextHubConnected) { + try { + retVal = mContextHubService.loadNanoApp(hubHandle, app); + }catch (RemoteException e) { + Log.e (TAG, "Could not fetch load nanoApp :" + e.toString()); + } + } + + return retVal; + } + + /** + * Unload a specified nanoApp + * + * @param nanoAppInstanceHandle handle of the nanoApp to load + * + * @return int 0 on success, -1 otherewise + */ + public int unloadNanoApp(int nanoAppInstanceHandle) { + int retVal = -1; + + if(mContextHubConnected) { + try { + retVal = mContextHubService.unloadNanoApp(nanoAppInstanceHandle); + }catch (RemoteException e) { + Log.e (TAG, "Could not fetch unload nanoApp :" + e.toString()); + } + } + + return retVal; + } + + /** + * get information about the nano app instance + * + * @param nanoAppInstanceHandle handle of the nanoAppInstance + * + * @return NanoAppInstanceInfo Inforamtion about the nano app + * instance. + * + * @see NanoAppInstanceInfo + */ + public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) { + NanoAppInstanceInfo retVal = null; + + if(mContextHubConnected) { + try { + retVal = mContextHubService.getNanoAppInstanceInfo(nanoAppInstanceHandle); + }catch (RemoteException e) { + Log.e (TAG, "Could not fetch nanoApp info :" + e.toString()); + } + } + + return retVal; + } + + /** + * Find a specified nano app on the system + * + * @param hubHandle handle of hub to search for nano app + * @param filter filter specifying the search criteria for app + * + * @see NanoAppFilter + * + * @return Integer[] Array of handles to any found nano apps + */ + public Integer[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) { + int[] temp; + Integer[] retVal = null; + + if(mContextHubConnected) { + try { + temp = mContextHubService.findNanoAppOnHub(hubHandle, filter); + retVal = new Integer[temp.length]; + for (int i = 0; i < temp.length; i++) { + retVal[i] = temp[i]; + } + }catch (RemoteException e) { + Log.e (TAG, "Could not query nanoApp instance :" + e.toString()); + } + } + + return retVal; + } + + /** + * Send a message to a spcific nano app instance on a context + * hub + * + * + * @param hubHandle handle of the hub to send the message to + * @param nanoAppHandle handle of the nano app to send to + * @param msg Message to be sent + * + * @see ContextHubMessage + * + * @return int 0 on success, -1 otherwise + */ + public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) { + int retVal = -1; + + if(mContextHubConnected) { + try { + retVal = mContextHubService.sendMessage(hubHandle, nanoAppHandle, msg); + }catch (RemoteException e) { + Log.e (TAG, "Could not fetch send message :" + e.toString()); + } + } + + return retVal; + } + + private void checkPermissions() { + mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE); + } + + private IContextHubCallback.Stub mClientCallback = new IContextHubCallback.Stub() { + @Override + public void onMessageReceipt(int hubId, int nanoAppId, ContextHubMessage msg) throws RemoteException { + + } + }; + + private ContextHubManager(Context context) { + checkPermissions(); + mContext = context; + mContextHubConnected = false; + } + + private ServiceConnection mServiceConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + mContextHubService = IContextHubService.Stub.asInterface(service); + mContextHubConnected = true; + + // Register our Callback + try { + mContextHubService.registerCallBack(mClientCallback); + } catch (RemoteException e) { + Log.e(TAG, "Could not register callback with context hub service :" + e.toString()); + } + Log.d(TAG, "contexthub manager connected to " + name.toString()); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + mContextHubService = null; + mContextHubConnected = false; + Log.d(TAG, "contexthub manager disconnected from " + name.toString()); + } + }; + +} diff --git a/core/java/android/hardware/location/ContextHubMessage.aidl b/core/java/android/hardware/location/ContextHubMessage.aidl new file mode 100644 index 0000000000000..915f1ec683c96 --- /dev/null +++ b/core/java/android/hardware/location/ContextHubMessage.aidl @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; +/* +@hide +*/ +parcelable ContextHubMessage; + diff --git a/core/java/android/hardware/location/ContextHubMessage.java b/core/java/android/hardware/location/ContextHubMessage.java new file mode 100644 index 0000000000000..954e97dc7fce7 --- /dev/null +++ b/core/java/android/hardware/location/ContextHubMessage.java @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + + +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; + +import java.util.Arrays; + +/** + * @hide + */ +@SystemApi +public class ContextHubMessage { + private int mType; + private int mVersion; + private byte[]mData; + + /** + * Get the message type + * + * @return int - message type + */ + public int getMsgType() { + return mType; + } + + /** + * get message version + * + * @return int - message version + */ + public int getVersion() { + return mVersion; + } + + /** + * get message data + * + * @return byte[] - message data buffer + */ + public byte[] getData() { + return Arrays.copyOf(mData, mData.length); + } + + /** + * set message type + * + * @param msgType - message type + */ + public void setMsgType(int msgType) { + mType = msgType; + } + + /** + * Set message version + * + * @param version - message version + */ + public void setVersion(int version) { + mVersion = version; + } + + /** + * set message data + * + * @param data - message buffer + */ + public void setMsgData(byte[] data) { + mData = Arrays.copyOf(data, data.length); + } + + /** + * Constructor for a context hub message + * + * @param msgType - message type + * @param version - version + * @param data - message buffer + */ + public ContextHubMessage(int msgType, int version, byte[] data) { + mType = msgType; + mVersion = version; + mData = Arrays.copyOf(data, data.length); + } + + public int describeContents() { + return 0; + } + + private ContextHubMessage(Parcel in) { + mType = in.readInt(); + mVersion = in.readInt(); + byte[] byteBuffer = new byte[in.readInt()]; + in.readByteArray(byteBuffer); + } + public void writeToParcel(Parcel out, int flags) { + out.writeInt(mType); + out.writeInt(mVersion); + out.writeInt(mData.length); + out.writeByteArray(mData); + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public ContextHubMessage createFromParcel(Parcel in) { + return new ContextHubMessage(in); + } + + public ContextHubMessage[] newArray(int size) { + return new ContextHubMessage[size]; + } + }; +} diff --git a/core/java/android/hardware/location/ContextHubService.java b/core/java/android/hardware/location/ContextHubService.java new file mode 100644 index 0000000000000..a2a13c6f82896 --- /dev/null +++ b/core/java/android/hardware/location/ContextHubService.java @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.app.Service; +import android.content.Context; +import android.content.Intent; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Log; + +import java.util.ArrayList; +import java.util.HashMap; + +/** + * @hide + */ +public class ContextHubService extends Service { + + private static final String TAG = "ContextHubService"; + private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); + + private static ContextHubService sSingletonInstance; + private static final Object sSingletonInstanceLock = new Object(); + + private HashMap mHubHash; + private HashMap mNanoAppHash; + private ContextHubInfo[] mContexthubInfo; + + + private native int nativeSendMessage(int[] header, byte[] data); + private native ContextHubInfo[] nativeInitialize(); + + private int onMessageReceipt(int[] header, byte[] data) { + return 0; + } + private void initialize() { + mContexthubInfo = nativeInitialize(); + + mHubHash = new HashMap(); + + for (int i = 0; i < mContexthubInfo.length; i++) { + mHubHash.put(i + 1, mContexthubInfo[i]); // Avoiding zero + } + } + + private ContextHubService(Context context) { + initialize(); + Log.d(TAG, "Created from " + context.toString()); + } + + public static ContextHubService getInstance(Context context) { + synchronized (sSingletonInstanceLock) { + if (sSingletonInstance == null) { + sSingletonInstance = new ContextHubService(context); + } + return sSingletonInstance; + } + } + + @Override + public void onCreate() { + super.onCreate(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private final IContextHubService.Stub mBinder = new IContextHubService.Stub() { + + private IContextHubCallback callback; + + @Override + public int registerCallBack(IContextHubCallback callback) throws RemoteException{ + this.callback = callback; + return 0; + } + + @Override + public int[] getContextHubHandles() throws RemoteException { + int [] returnArray = new int[mHubHash.size()]; + int i = 0; + for (int key : mHubHash.keySet()) { + // Add any filtering here + returnArray[i] = key; + i++; + } + return returnArray; + } + + @Override + public ContextHubInfo getContextHubInfo(int contexthubHandle) throws RemoteException { + return mHubHash.get(contexthubHandle); + } + + @Override + public int loadNanoApp(int hubHandle, NanoApp app) throws RemoteException { + if (!mHubHash.containsKey(hubHandle)) { + return -1; + } else { + // Call Native interface here + int[] msgHeader = new int[8]; + msgHeader[0] = ContextHubManager.MSG_LOAD_NANO_APP; + msgHeader[1] = app.getAppId(); + msgHeader[2] = app.getAppVersion(); + msgHeader[3] = 0; // LOADING_HINTS + msgHeader[4] = hubHandle; + + int handle = nativeSendMessage(msgHeader, app.getAppBinary()); + + // if successful, add an entry to mNanoAppHash + + if(handle > 0) { + return 0; + } else { + + return -1; + } + } + } + + @Override + public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { + if(!mNanoAppHash.containsKey(nanoAppInstanceHandle)) { + return -1; + } else { + NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); + // Call Native interface here + int[] msgHeader = new int[8]; + msgHeader[0] = ContextHubManager.MSG_UNLOAD_NANO_APP; + msgHeader[1] = info.getContexthubId(); + msgHeader[2] = info.getHandle(); + + int result = nativeSendMessage(msgHeader, null); + // if successful, remove the entry in mNanoAppHash + if(result == 0) { + mNanoAppHash.remove(nanoAppInstanceHandle); + } + return(result); + } + } + + @Override + public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) throws RemoteException { + // This assumes that all the nanoAppInfo is current. This is reasonable + // for the use cases for tightly controlled nanoApps. + // + if(!mNanoAppHash.containsKey(nanoAppInstanceHandle)) { + return(mNanoAppHash.get(nanoAppInstanceHandle)); + } else { + return null; + } + } + + @Override + public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { + ArrayList foundInstances = new ArrayList(); + + for(Integer nanoAppInstance : mNanoAppHash.keySet()) { + NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); + + if(filter.testMatch(info)){ + foundInstances.add(nanoAppInstance); + } + } + + int[] retArray = new int[foundInstances.size()]; + for (int i = 0; i < foundInstances.size(); i++) { + retArray[i] = foundInstances.get(i).intValue(); + } + + return retArray; + } + + @Override + public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) throws RemoteException { + 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(); + + return (nativeSendMessage(msgHeader, msg.getData())); + } + }; +} diff --git a/core/java/android/hardware/location/IContextHubCallback.aidl b/core/java/android/hardware/location/IContextHubCallback.aidl new file mode 100644 index 0000000000000..45b1ef494ac26 --- /dev/null +++ b/core/java/android/hardware/location/IContextHubCallback.aidl @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.hardware.location.ContextHubMessage; + +/** @hide */ +oneway interface IContextHubCallback { + void onMessageReceipt(int hubId, int nanoAppId, in ContextHubMessage msg); +} diff --git a/core/java/android/hardware/location/IContextHubService.aidl b/core/java/android/hardware/location/IContextHubService.aidl new file mode 100644 index 0000000000000..b2db0b2cdd236 --- /dev/null +++ b/core/java/android/hardware/location/IContextHubService.aidl @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +// Declare any non-default types here with import statements +import android.hardware.location.ContextHubMessage; +import android.hardware.location.ContextHubInfo; +import android.hardware.location.NanoApp; +import android.hardware.location.NanoAppInstanceInfo; +import android.hardware.location.NanoAppFilter; +import android.hardware.location.IContextHubCallback; + +/** @hide */ +interface IContextHubService { + + // register a callback to receive messages + int registerCallBack(in IContextHubCallback callback); + + // Gets a list of available context hub handles + int[] getContextHubHandles(); + + // Get the properties of a hub + ContextHubInfo getContextHubInfo(int contextHubHandle); + + // Load a nanoapp on a specified context hub + int loadNanoApp(int hubHandle, in NanoApp app); + + // Unload a nanoapp instance + int unloadNanoApp(int nanoAppInstanceHandle); + + // get information about a nanoAppInstance + NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle); + + // find all nanoApp instances matching some filter + int[] findNanoAppOnHub(int hubHandle, in NanoAppFilter filter); + + // send a message to a nanoApp + int sendMessage(int hubHandle, int nanoAppHandle, in ContextHubMessage msg); +} diff --git a/core/java/android/hardware/location/MemoryRegion.java b/core/java/android/hardware/location/MemoryRegion.java new file mode 100644 index 0000000000000..e8c761527c2c0 --- /dev/null +++ b/core/java/android/hardware/location/MemoryRegion.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * @hide + */ + +@SystemApi +public class MemoryRegion implements Parcelable{ + + private int mSizeBytes; + private int mSizeBytesFree; + private boolean mIsReadable; + private boolean mIsWritable; + private boolean mIsExecutable; + + /** + * get the capacity of the memory region in bytes + * + * @return int - the memory capacity in bytes + */ + public int getCapacityBytes() { + return mSizeBytes; + } + + /** + * get the free capacity of the memory region in bytes + * + * @return int - free bytes + */ + public int getFreeCapacityBytes() { + return mSizeBytesFree; + } + + /** + * Is the memory readable + * + * @return boolean - true if memory is readable, false otherwise + */ + public boolean isReadable() { + return mIsReadable; + } + + /** + * Is the memory writable + * + * @return boolean - true if memory is writable, false otherwise + */ + public boolean isWritable() { + return mIsWritable; + } + + /** + * Is the memory executable + * + * @return boolean - true if memory is executable, false + * otherwise + */ + public boolean isExecutable() { + return mIsExecutable; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(mSizeBytes); + dest.writeInt(mSizeBytesFree); + dest.writeInt(mIsReadable ? 1 : 0); + dest.writeInt(mIsWritable ? 1 : 0); + dest.writeInt(mIsExecutable ? 1 : 0); + } + + public MemoryRegion(Parcel source) { + mSizeBytes = source.readInt(); + mSizeBytesFree = source.readInt(); + mIsReadable = source.readInt() != 0; + mIsWritable = source.readInt() != 0; + mIsExecutable = source.readInt() != 0; + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public MemoryRegion createFromParcel(Parcel in) { + return new MemoryRegion(in); + } + + public MemoryRegion[] newArray(int size) { + return new MemoryRegion[size]; + } + }; + +} diff --git a/core/java/android/hardware/location/NanoApp.aidl b/core/java/android/hardware/location/NanoApp.aidl new file mode 100644 index 0000000000000..d32c44a5c44a1 --- /dev/null +++ b/core/java/android/hardware/location/NanoApp.aidl @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; +/* +@hide +*/ +parcelable NanoApp; diff --git a/core/java/android/hardware/location/NanoApp.java b/core/java/android/hardware/location/NanoApp.java new file mode 100644 index 0000000000000..36d181f91fba7 --- /dev/null +++ b/core/java/android/hardware/location/NanoApp.java @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + + +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * A class describing nano apps. + * A nano app is a piece of executable code that can be + * downloaded onto a specific architecture. These are targtted + * for low power compute domains on a device. + * + * Nano apps are expected to be used only by bundled apps only + * at this time. + * + * @hide + */ +@SystemApi +public class NanoApp { + private String mPublisher; + private String mName; + + private int mAppId; + private int mAppVersion; + + private int mNeededReadMemBytes; + private int mNeededWriteMemBytes; + private int mNeededExecMemBytes; + + private int[] mNeededSensors; + private int[] mOutputEvents; + private byte[] mAppBinary; + + public NanoApp() { + } + + /** + * Set the publisher name + * + * @param publisher name of the publisher of this nano app + */ + public void setPublisher(String publisher) { + mPublisher = publisher; + } + + /** + * set the name of the app + * + * @param name name of the app + */ + public void setName(String name) { + mName = name; + } + + /** + * set the app identifier + * + * @param appId add identifier + */ + public void setAppId(int appId) { + mAppId = appId; + } + + /** + * Set the app version + * + * @param appVersion app version + */ + public void setAppVersion(int appVersion) { + mAppVersion = appVersion; + } + + /** + * set memory needed as read only + * + * @param neededReadMemBytes + * read only memory needed in bytes + */ + public void setNeededReadMemBytes(int neededReadMemBytes) { + mNeededReadMemBytes = neededReadMemBytes; + } + + /** + * set writable memory needed in bytes + * + * @param neededWriteMemBytes + * writable memory needed in bytes + */ + public void setNeededWriteMemBytes(int neededWriteMemBytes) { + mNeededWriteMemBytes = neededWriteMemBytes; + } + + /** + * set executable memory needed + * + * @param neededExecMemBytes + * executable memory needed in bytes + */ + public void setNeededExecMemBytes(int neededExecMemBytes) { + mNeededExecMemBytes = neededExecMemBytes; + } + + /** + * set the sensors needed for this app + * + * @param neededSensors + * needed Sensors + */ + public void setNeededSensors(int[] neededSensors) { + mNeededSensors = neededSensors; + } + + public void setOutputEvents(int[] outputEvents) { + mOutputEvents = outputEvents; + } + + /** + * set output events returned by the nano app + * + * @param appBinary generated events + */ + public void setAppBinary(byte[] appBinary) { + mAppBinary = appBinary; + } + + + /** + * get the publisher name + * + * @return publisher name + */ + public String getPublisher() { + return mPublisher; + } + + /** + * get the name of the app + * + * @return app name + */ + public String getName() { + return mName; + } + + /** + * get the identifier of the app + * + * @return identifier for this app + */ + public int getAppId() { + return mAppId; + } + + /** + * get the app version + * + * @return app version + */ + public int getAppVersion() { + return mAppVersion; + } + + /** + * get the ammount of readable memory needed by this app + * + * @return readable memory needed in bytes + */ + public int getNeededReadMemBytes() { + return mNeededReadMemBytes; + } + + /** + * get the ammount og writable memory needed in bytes + * + * @return writable memory needed in bytes + */ + public int getNeededWriteMemBytes() { + return mNeededWriteMemBytes; + } + + /** + * executable memory needed in bytes + * + * @return executable memory needed in bytes + */ + public int getNeededExecMemBytes() { + return mNeededExecMemBytes; + } + + /** + * get the sensors needed by this app + * + * @return sensors needed + */ + public int[] getNeededSensors() { + return mNeededSensors; + } + + /** + * get the events generated by this app + * + * @return generated events + */ + public int[] getOutputEvents() { + return mOutputEvents; + } + + /** + * get the binary for this app + * + * @return app binary + */ + public byte[] getAppBinary() { + return mAppBinary; + } + + private NanoApp(Parcel in) { + mPublisher = in.readString(); + mName = in.readString(); + + mAppId = in.readInt(); + mAppVersion = in.readInt(); + mNeededReadMemBytes = in.readInt(); + mNeededWriteMemBytes = in.readInt(); + mNeededExecMemBytes = in.readInt(); + + int mNeededSensorsLength = in.readInt(); + mNeededSensors = new int[mNeededSensorsLength]; + in.readIntArray(mNeededSensors); + + int mOutputEventsLength = in.readInt(); + mOutputEvents = new int[mOutputEventsLength]; + in.readIntArray(mOutputEvents); + + int binaryLength = in.readInt(); + mAppBinary = new byte[binaryLength]; + in.readByteArray(mAppBinary); + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel out, int flags) { + out.writeString(mPublisher); + out.writeString(mName); + out.writeInt(mAppId); + out.writeInt(mAppVersion); + out.writeInt(mNeededReadMemBytes); + out.writeInt(mNeededWriteMemBytes); + out.writeInt(mNeededExecMemBytes); + + out.writeInt(mNeededSensors.length); + out.writeIntArray(mNeededSensors); + + out.writeInt(mOutputEvents.length); + out.writeIntArray(mOutputEvents); + + out.writeInt(mAppBinary.length); + out.writeByteArray(mAppBinary); + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public NanoApp createFromParcel(Parcel in) { + return new NanoApp(in); + } + + public NanoApp[] newArray(int size) { + return new NanoApp[size]; + } + }; +} diff --git a/core/java/android/hardware/location/NanoAppFilter.aidl b/core/java/android/hardware/location/NanoAppFilter.aidl new file mode 100644 index 0000000000000..cc6d4755997ae --- /dev/null +++ b/core/java/android/hardware/location/NanoAppFilter.aidl @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; +/* +@hide +*/ +parcelable NanoAppFilter; diff --git a/core/java/android/hardware/location/NanoAppFilter.java b/core/java/android/hardware/location/NanoAppFilter.java new file mode 100644 index 0000000000000..ac341e417b644 --- /dev/null +++ b/core/java/android/hardware/location/NanoAppFilter.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + + +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * @hide + */ +@SystemApi +public class NanoAppFilter { + + // The appId, can be set to APP_ID_ANY + private long mAppId; + + // Version to filter apps + private int mAppVersion; + + // filtering spec for version + private int mVersionRestrictionMask; + + // If APP_ID is any, then a match is performef with the vendor mask + private long mAppIdVendorMask; + + // Id of the context hub this instance is expected on + private int mContextHubId; + + /** + * Flag indicating any version. With this flag set, all versions shall match provided version. + */ + public static final int FLAGS_VERSION_ANY = -1; + /** + * If this flag is set, only versions strictly greater than the version specified shall match. + */ + public static final int FLAGS_VERSION_GREAT_THAN = 2; + /** + * If this flag is set, only versions strictly less than the version specified shall match. + */ + public static final int FLAGS_VERSION_LESS_THAN = 4; + public static final int FLAGS_VERSION_STRICTLY_EQUAL = 8; + + /** + * If this flag is set, only versions strictly equal to the version specified shall match. + */ + public static final int APP_ANY = -1; + + /** + * If this flag is set, all vendors shall match. + */ + public static final int VENDOR_ANY = -1; + + /** + * If this flag is set, any hub shall match. + */ + public static final int HUB_ANY = -1; + + private NanoAppFilter(Parcel in) { + mAppId = in.readLong(); + mAppVersion = in.readInt(); + mVersionRestrictionMask = in.readInt(); + mAppIdVendorMask = in.readInt(); + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel out, int flags) { + + out.writeLong(mAppId); + out.writeInt(mAppVersion); + out.writeInt(mVersionRestrictionMask); + out.writeLong(mAppIdVendorMask); + } + + /** + * Create a filter + * + * @param appId application id + * @param appVersion application version + * @param versionMask version + * @param vendorMask vendor + */ + public NanoAppFilter(long appId, int appVersion, int versionMask, long vendorMask) { + mAppId = appId; + mAppVersion = appVersion; + mVersionRestrictionMask = versionMask; + mAppIdVendorMask = vendorMask; + } + + private boolean versionsMatch(int versionRestrictionMask, int expected, int actual){ + // some refactoring of version restriction mask is needed, until then, return all + return true; + } + /** + * + * @param nano app instance info + * + * @return true if this is a match, false otherwise + */ + public boolean testMatch(NanoAppInstanceInfo info) { + if ((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; + } + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public NanoAppFilter createFromParcel(Parcel in) { + return new NanoAppFilter(in); + } + + public NanoAppFilter[] newArray(int size) { + return new NanoAppFilter[size]; + } + }; +} diff --git a/core/java/android/hardware/location/NanoAppInstanceInfo.aidl b/core/java/android/hardware/location/NanoAppInstanceInfo.aidl new file mode 100644 index 0000000000000..c8c40d7be2339 --- /dev/null +++ b/core/java/android/hardware/location/NanoAppInstanceInfo.aidl @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; +/* +@hide +*/ +parcelable NanoAppInstanceInfo; \ No newline at end of file diff --git a/core/java/android/hardware/location/NanoAppInstanceInfo.java b/core/java/android/hardware/location/NanoAppInstanceInfo.java new file mode 100644 index 0000000000000..ac62919b79317 --- /dev/null +++ b/core/java/android/hardware/location/NanoAppInstanceInfo.java @@ -0,0 +1,302 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.hardware.location; + + +import android.annotation.SystemApi; +import android.os.Parcel; +import android.os.Parcelable; + +/** + * @hide + */ +@SystemApi +public class NanoAppInstanceInfo { + private String mPublisher; + private String mName; + + private int mAppId; + private int mAppVersion; + + private int mNeededReadMemBytes; + private int mNeededWriteMemBytes; + private int mNeededExecMemBytes; + + private int[] mNeededSensors; + private int[] mOutputEvents; + + private int mContexthubId; + private int mHandle; + + public NanoAppInstanceInfo() { + } + + /** + * get the publisher of this app + * + * @return String - name of the publisher + */ + public String getPublisher() { + return mPublisher; + } + + + /** + * set the publisher name for the app + * + * @param publisher - name of the publisher + */ + public void setPublisher(String publisher) { + mPublisher = publisher; + } + + /** + * get the name of the app + * + * @return String - name of the app + */ + public String getName() { + return mName; + } + + /** + * set the name of the app + * + * @param name - name of the app + */ + public void setName(String name) { + mName = name; + } + + /** + * Get the application identifier + * + * @return int - application identifier + */ + public int getAppId() { + return mAppId; + } + + /** + * Set the application identifier + * + * @param appId - application identifier + */ + public void setAppId(int appId) { + mAppId = appId; + } + + /** + * Set the application version + * + * @return int - version of the app + */ + public int getAppVersion() { + return mAppVersion; + } + + /** + * Set the application version + * + * @param appVersion - version of the app + */ + public void setAppVersion(int appVersion) { + mAppVersion = appVersion; + } + + /** + * Get the read memory needed by the app + * + * @return int - readable memory needed in bytes + */ + public int getNeededReadMemBytes() { + return mNeededReadMemBytes; + } + + /** + * Set the read memory needed by the app + * + * @param neededReadMemBytes - readable Memory needed in bytes + */ + public void setNeededReadMemBytes(int neededReadMemBytes) { + mNeededReadMemBytes = neededReadMemBytes; + } + + /** + * get writable memory needed by the app + * + * @return int - writable memory needed by the app + */ + public int getNeededWriteMemBytes() { + return mNeededWriteMemBytes; + } + + /** + * set writable memory needed by the app + * + * @param neededWriteMemBytes - writable memory needed by the + * app + */ + public void setNeededWriteMemBytes(int neededWriteMemBytes) { + mNeededWriteMemBytes = neededWriteMemBytes; + } + + /** + * get executable memory needed by the app + * + * @return int - executable memory needed by the app + */ + public int getNeededExecMemBytes() { + return mNeededExecMemBytes; + } + + /** + * set executable memory needed by the app + * + * @param neededExecMemBytes - executable memory needed by the + * app + */ + public void setNeededExecMemBytes(int neededExecMemBytes) { + mNeededExecMemBytes = neededExecMemBytes; + } + + /** + * Get the sensors needed by this app + * + * @return int[] all the required sensors needed by this app + */ + public int[] getNeededSensors() { + return mNeededSensors; + } + + /** + * set the sensors needed by this app + * + * @param neededSensors - all the sensors needed by this app + */ + public void setNeededSensors(int[] neededSensors) { + mNeededSensors = neededSensors; + } + + /** + * get the events generated by this app + * + * @return all the events that can be generated by this app + */ + public int[] getOutputEvents() { + return mOutputEvents; + } + + /** + * set the output events that can be generated by this app + * + * @param outputEvents - the events that may be generated by + * this app + */ + public void setOutputEvents(int[] outputEvents) { + mOutputEvents = outputEvents; + } + + /** + * get the context hub identifier + * + * @return int - system unique hub identifier + */ + public int getContexthubId() { + return mContexthubId; + } + + /** + * set the context hub identifier + * + * @param contexthubId - system wide unique identifier + */ + public void setContexthubId(int contexthubId) { + mContexthubId = contexthubId; + } + + /** + * get a handle to the nano app instance + * + * @return int - handle to this instance + */ + public int getHandle() { + return mHandle; + } + + /** + * set the handle for an app instance + * + * @param handle - handle to this instance + */ + public void setHandle(int handle) { + mHandle = handle; + } + + + private NanoAppInstanceInfo(Parcel in) { + mPublisher = in.readString(); + mName = in.readString(); + + mAppId = in.readInt(); + mAppVersion = in.readInt(); + mNeededReadMemBytes = in.readInt(); + mNeededWriteMemBytes = in.readInt(); + mNeededExecMemBytes = in.readInt(); + + int mNeededSensorsLength = in.readInt(); + mNeededSensors = new int[mNeededSensorsLength]; + in.readIntArray(mNeededSensors); + + int mOutputEventsLength = in.readInt(); + mOutputEvents = new int[mOutputEventsLength]; + in.readIntArray(mOutputEvents); + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel out, int flags) { + out.writeString(mPublisher); + out.writeString(mName); + out.writeInt(mAppId); + out.writeInt(mAppVersion); + out.writeInt(mContexthubId); + out.writeInt(mNeededReadMemBytes); + out.writeInt(mNeededWriteMemBytes); + out.writeInt(mNeededExecMemBytes); + + out.writeInt(mNeededSensors.length); + out.writeIntArray(mNeededSensors); + + out.writeInt(mOutputEvents.length); + out.writeIntArray(mOutputEvents); + + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public NanoAppInstanceInfo createFromParcel(Parcel in) { + return new NanoAppInstanceInfo(in); + } + + public NanoAppInstanceInfo[] newArray(int size) { + return new NanoAppInstanceInfo[size]; + } + }; +}