Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.hardware.location; |
| 18 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.HashMap; |
| 26 | |
| 27 | /** |
| 28 | * @hide |
| 29 | */ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 30 | public class ContextHubService extends IContextHubService.Stub { |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 31 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 32 | public static final String CONTEXTHUB_SERVICE = "contexthub_service"; |
| 33 | |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 34 | private static final String TAG = "ContextHubService"; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 35 | private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE; |
| 36 | private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '" |
| 37 | + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 38 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 39 | |
| 40 | public static final int ANY_HUB = -1; |
| 41 | public static final int MSG_LOAD_NANO_APP = 5; |
| 42 | public static final int MSG_UNLOAD_NANO_APP = 2; |
| 43 | |
| 44 | private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown"; |
| 45 | private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN; |
| 46 | private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN; |
| 47 | private static final int PRE_LOADED_APP_MEM_REQ = 0; |
| 48 | |
| 49 | private static final int MSG_HEADER_SIZE = 4; |
| 50 | private static final int MSG_FIELD_TYPE = 0; |
| 51 | private static final int MSG_FIELD_VERSION = 1; |
| 52 | private static final int MSG_FIELD_HUB_HANDLE = 2; |
| 53 | private static final int MSG_FIELD_APP_INSTANCE = 3; |
| 54 | |
| 55 | private static final int OS_APP_INSTANCE = -1; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 56 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 57 | private final Context mContext; |
| 58 | |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 59 | private HashMap<Integer, NanoAppInstanceInfo> mNanoAppHash; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 60 | private ContextHubInfo[] mContextHubInfo; |
| 61 | private IContextHubCallback mCallback; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 62 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 63 | private native int nativeSendMessage(int[] header, byte[] data); |
| 64 | private native ContextHubInfo[] nativeInitialize(); |
| 65 | |
| 66 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 67 | public ContextHubService(Context context) { |
| 68 | mContext = context; |
| 69 | mContextHubInfo = nativeInitialize(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 70 | mNanoAppHash = new HashMap<Integer, NanoAppInstanceInfo>(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 71 | |
| 72 | for (int i = 0; i < mContextHubInfo.length; i++) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 73 | Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId() |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 74 | + ", name: " + mContextHubInfo[i].getName()); |
| 75 | } |
| 76 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 77 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 78 | @Override |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 79 | public int registerCallback(IContextHubCallback callback) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 80 | checkPermissions(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 81 | synchronized(this) { |
| 82 | mCallback = callback; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 83 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int[] getContextHubHandles() throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 89 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 90 | int [] returnArray = new int[mContextHubInfo.length]; |
| 91 | |
| 92 | for (int i = 0; i < returnArray.length; ++i) { |
| 93 | returnArray[i] = i + 1; //valid handles from 1...n |
| 94 | Log.d(TAG, String.format("Hub %s is mapped to %d", |
| 95 | mContextHubInfo[i].getName(), returnArray[i])); |
| 96 | } |
| 97 | |
| 98 | return returnArray; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 103 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 104 | contextHubHandle -= 1; |
| 105 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
| 106 | return null; // null means fail |
| 107 | } |
| 108 | |
| 109 | return mContextHubInfo[contextHubHandle]; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 114 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 115 | contextHubHandle -= 1; |
| 116 | |
| 117 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
| 118 | return -1; // negative handle are invalid, means failed |
| 119 | } |
| 120 | |
| 121 | // Call Native interface here |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 122 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
| 123 | msgHeader[MSG_FIELD_HUB_HANDLE] = contextHubHandle; |
| 124 | msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; |
| 125 | msgHeader[MSG_FIELD_VERSION] = 0; |
| 126 | msgHeader[MSG_FIELD_TYPE] = MSG_LOAD_NANO_APP; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 127 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 128 | if (nativeSendMessage(msgHeader, app.getAppBinary()) != 0) { |
| 129 | return -1; |
| 130 | } |
| 131 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 132 | return 0; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 133 | } |
| 134 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { |
| 137 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 138 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); |
| 139 | if (info == null) { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 140 | return -1; //means failed |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Call Native interface here |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 144 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
| 145 | msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB; |
| 146 | msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; |
| 147 | msgHeader[MSG_FIELD_VERSION] = 0; |
| 148 | msgHeader[MSG_FIELD_TYPE] = MSG_UNLOAD_NANO_APP; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 149 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 150 | if(nativeSendMessage(msgHeader, null) != 0) { |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 155 | return 0; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 156 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 157 | |
| 158 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 159 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) |
| 160 | throws RemoteException { |
| 161 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 162 | // This assumes that all the nanoAppInfo is current. This is reasonable |
| 163 | // for the use cases for tightly controlled nanoApps. |
| 164 | if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 165 | return mNanoAppHash.get(nanoAppInstanceHandle); |
| 166 | } else { |
| 167 | return null; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | @Override |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 172 | public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 173 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 174 | ArrayList<Integer> foundInstances = new ArrayList<Integer>(); |
| 175 | |
| 176 | for(Integer nanoAppInstance : mNanoAppHash.keySet()) { |
| 177 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); |
| 178 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 179 | if (filter.testMatch(info)){ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 180 | foundInstances.add(nanoAppInstance); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | int[] retArray = new int[foundInstances.size()]; |
| 185 | for (int i = 0; i < foundInstances.size(); i++) { |
| 186 | retArray[i] = foundInstances.get(i).intValue(); |
| 187 | } |
| 188 | |
| 189 | return retArray; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 193 | public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) |
| 194 | throws RemoteException { |
| 195 | checkPermissions(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 196 | |
| 197 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
| 198 | msgHeader[MSG_FIELD_HUB_HANDLE] = hubHandle; |
| 199 | msgHeader[MSG_FIELD_APP_INSTANCE] = nanoAppHandle; |
| 200 | msgHeader[MSG_FIELD_VERSION] = msg.getVersion(); |
| 201 | msgHeader[MSG_FIELD_TYPE] = msg.getMsgType(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 202 | |
| 203 | return nativeSendMessage(msgHeader, msg.getData()); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 204 | } |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 205 | |
| 206 | private void checkPermissions() { |
| 207 | mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE); |
| 208 | } |
Ashutosh Joshi | 2c697fb | 2016-04-01 20:48:13 +0000 | [diff] [blame] | 209 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 210 | private int onMessageReceipt(int[] header, byte[] data) { |
| 211 | if (header == null || data == null || header.length < MSG_HEADER_SIZE) { |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | synchronized(this) { |
| 216 | if (mCallback != null) { |
| 217 | ContextHubMessage msg = new ContextHubMessage(header[MSG_FIELD_TYPE], |
| 218 | header[MSG_FIELD_VERSION], |
| 219 | data); |
| 220 | |
| 221 | try { |
| 222 | mCallback.onMessageReceipt(header[MSG_FIELD_HUB_HANDLE], |
| 223 | header[MSG_FIELD_APP_INSTANCE], |
| 224 | msg); |
| 225 | } catch (Exception e) { |
| 226 | Log.w(TAG, "Exception " + e + " when calling remote callback"); |
| 227 | return -1; |
| 228 | } |
| 229 | } else { |
| 230 | Log.d(TAG, "Message Callback is NULL"); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) { |
| 238 | // App Id encodes vendor & version |
| 239 | NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo(); |
| 240 | |
| 241 | appInfo.setAppId(appId); |
| 242 | appInfo.setAppVersion(appVersion); |
| 243 | appInfo.setName(PRE_LOADED_APP_NAME); |
| 244 | appInfo.setContexthubId(hubHandle); |
| 245 | appInfo.setHandle(appInstanceHandle); |
| 246 | appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER); |
| 247 | appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 248 | appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 249 | appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 250 | |
| 251 | mNanoAppHash.put(appInstanceHandle, appInfo); |
| 252 | Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId |
| 253 | + " version " + appVersion); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | } |