Merge "Context hub API for applications"

This commit is contained in:
Ashutosh Joshi
2016-01-28 22:24:39 +00:00
committed by Android (Google) Code Review
17 changed files with 2135 additions and 0 deletions

View File

@@ -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;

View File

@@ -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<ContextHubInfo> CREATOR
= new Parcelable.Creator<ContextHubInfo>() {
public ContextHubInfo createFromParcel(Parcel in) {
return new ContextHubInfo(in);
}
public ContextHubInfo[] newArray(int size) {
return new ContextHubInfo[size];
}
};
}

View File

@@ -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());
}
};
}

View File

@@ -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;

View File

@@ -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<ContextHubMessage> CREATOR
= new Parcelable.Creator<ContextHubMessage>() {
public ContextHubMessage createFromParcel(Parcel in) {
return new ContextHubMessage(in);
}
public ContextHubMessage[] newArray(int size) {
return new ContextHubMessage[size];
}
};
}

View File

@@ -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<Integer, ContextHubInfo> mHubHash;
private HashMap<Integer, NanoAppInstanceInfo> 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<Integer, ContextHubInfo>();
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<Integer> foundInstances = new ArrayList<Integer>();
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()));
}
};
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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<MemoryRegion> CREATOR
= new Parcelable.Creator<MemoryRegion>() {
public MemoryRegion createFromParcel(Parcel in) {
return new MemoryRegion(in);
}
public MemoryRegion[] newArray(int size) {
return new MemoryRegion[size];
}
};
}

View File

@@ -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;

View File

@@ -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<NanoApp> CREATOR
= new Parcelable.Creator<NanoApp>() {
public NanoApp createFromParcel(Parcel in) {
return new NanoApp(in);
}
public NanoApp[] newArray(int size) {
return new NanoApp[size];
}
};
}

View File

@@ -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;

View File

@@ -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<NanoAppFilter> CREATOR
= new Parcelable.Creator<NanoAppFilter>() {
public NanoAppFilter createFromParcel(Parcel in) {
return new NanoAppFilter(in);
}
public NanoAppFilter[] newArray(int size) {
return new NanoAppFilter[size];
}
};
}

View File

@@ -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;

View File

@@ -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<NanoAppInstanceInfo> CREATOR
= new Parcelable.Creator<NanoAppInstanceInfo>() {
public NanoAppInstanceInfo createFromParcel(Parcel in) {
return new NanoAppInstanceInfo(in);
}
public NanoAppInstanceInfo[] newArray(int size) {
return new NanoAppInstanceInfo[size];
}
};
}