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 | |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 17 | package com.android.server.location; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 18 | |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 20 | import android.hardware.contexthub.V1_0.AsyncEventType; |
| 21 | import android.hardware.contexthub.V1_0.ContextHub; |
| 22 | import android.hardware.contexthub.V1_0.ContextHubMsg; |
| 23 | import android.hardware.contexthub.V1_0.HostEndPoint; |
| 24 | import android.hardware.contexthub.V1_0.HubAppInfo; |
| 25 | import android.hardware.contexthub.V1_0.IContexthub; |
| 26 | import android.hardware.contexthub.V1_0.IContexthubCallback; |
| 27 | import android.hardware.contexthub.V1_0.Result; |
| 28 | import android.hardware.contexthub.V1_0.TransactionResult; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 29 | import android.hardware.location.ContextHubInfo; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 30 | import android.hardware.location.ContextHubMessage; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 31 | import android.hardware.location.IContextHubCallback; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 32 | import android.hardware.location.IContextHubClient; |
| 33 | import android.hardware.location.IContextHubClientCallback; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 34 | import android.hardware.location.IContextHubService; |
| 35 | import android.hardware.location.IContextHubTransactionCallback; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 36 | import android.hardware.location.NanoApp; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 37 | import android.hardware.location.NanoAppBinary; |
| 38 | import android.hardware.location.NanoAppFilter; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 39 | import android.hardware.location.NanoAppInstanceInfo; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 40 | import android.hardware.location.NanoAppState; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 41 | import android.os.RemoteCallbackList; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
| 43 | import android.util.Log; |
| 44 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 45 | import com.android.internal.util.DumpUtils; |
| 46 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 47 | import java.io.FileDescriptor; |
| 48 | import java.io.PrintWriter; |
Ashutosh Joshi | 19753cc | 2016-11-23 13:56:39 -0800 | [diff] [blame] | 49 | import java.nio.ByteBuffer; |
| 50 | import java.nio.ByteOrder; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 51 | import java.util.ArrayList; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
| 53 | import java.util.List; |
| 54 | import java.util.NoSuchElementException; |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 55 | import java.util.concurrent.ConcurrentHashMap; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * @hide |
| 59 | */ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 60 | public class ContextHubService extends IContextHubService.Stub { |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 61 | private static final String TAG = "ContextHubService"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 62 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 63 | /* |
| 64 | * Constants for the type of transaction that is defined by ContextHubService. |
| 65 | * This is used to report the transaction callback to clients, and is different from |
| 66 | * ContextHubTransaction.Type. |
| 67 | */ |
| 68 | public static final int MSG_ENABLE_NANO_APP = 1; |
| 69 | public static final int MSG_DISABLE_NANO_APP = 2; |
| 70 | public static final int MSG_LOAD_NANO_APP = 3; |
Ashutosh Joshi | cafdee9 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 71 | public static final int MSG_UNLOAD_NANO_APP = 4; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 72 | public static final int MSG_QUERY_NANO_APPS = 5; |
| 73 | public static final int MSG_QUERY_MEMORY = 6; |
| 74 | public static final int MSG_HUB_RESET = 7; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 75 | |
| 76 | private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown"; |
| 77 | private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN; |
| 78 | private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN; |
| 79 | private static final int PRE_LOADED_APP_MEM_REQ = 0; |
| 80 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 81 | private static final int OS_APP_INSTANCE = -1; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 82 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 83 | private final Context mContext; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 84 | |
| 85 | // TODO(b/69270990): Remove once old ContextHubManager API is deprecated |
| 86 | // Service cache maintaining of instance ID to nanoapp infos |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 87 | private final ConcurrentHashMap<Integer, NanoAppInstanceInfo> mNanoAppHash = |
| 88 | new ConcurrentHashMap<>(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 89 | // The next available instance ID (managed by the service) to assign to a nanoapp |
| 90 | private int mNextAvailableInstanceId = 0; |
| 91 | // A map of the long nanoapp ID to instance ID managed by the service |
| 92 | private final ConcurrentHashMap<Long, Integer> mNanoAppIdToInstanceMap = |
| 93 | new ConcurrentHashMap<>(); |
| 94 | |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 95 | private final ContextHubInfo[] mContextHubInfo; |
| 96 | private final RemoteCallbackList<IContextHubCallback> mCallbacksList = |
| 97 | new RemoteCallbackList<>(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 98 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 99 | // Proxy object to communicate with the Context Hub HAL |
| 100 | private final IContexthub mContextHubProxy; |
| 101 | |
| 102 | // The manager for transaction queue |
| 103 | private final ContextHubTransactionManager mTransactionManager; |
| 104 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 105 | // The manager for sending messages to/from clients |
| 106 | private final ContextHubClientManager mClientManager; |
| 107 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Class extending the callback to register with a Context Hub. |
| 110 | */ |
| 111 | private class ContextHubServiceCallback extends IContexthubCallback.Stub { |
| 112 | private final int mContextHubId; |
| 113 | |
| 114 | ContextHubServiceCallback(int contextHubId) { |
| 115 | mContextHubId = contextHubId; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void handleClientMsg(ContextHubMsg message) { |
| 120 | handleClientMessageCallback(mContextHubId, message); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void handleTxnResult(int transactionId, int result) { |
| 125 | handleTransactionResultCallback(mContextHubId, transactionId, result); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void handleHubEvent(int eventType) { |
| 130 | handleHubEventCallback(mContextHubId, eventType); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void handleAppAbort(long nanoAppId, int abortCode) { |
| 135 | handleAppAbortCallback(mContextHubId, nanoAppId, abortCode); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void handleAppsInfo(ArrayList<HubAppInfo> nanoAppInfoList) { |
| 140 | handleQueryAppsCallback(mContextHubId, nanoAppInfoList); |
| 141 | } |
| 142 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 143 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 144 | public ContextHubService(Context context) { |
| 145 | mContext = context; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 146 | |
| 147 | mContextHubProxy = getContextHubProxy(); |
| 148 | if (mContextHubProxy == null) { |
| 149 | mTransactionManager = null; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 150 | mClientManager = null; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 151 | mContextHubInfo = new ContextHubInfo[0]; |
| 152 | return; |
| 153 | } |
| 154 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 155 | mClientManager = new ContextHubClientManager(mContext, mContextHubProxy); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 156 | mTransactionManager = new ContextHubTransactionManager(mContextHubProxy); |
| 157 | |
| 158 | List<ContextHub> hubList; |
| 159 | try { |
| 160 | hubList = mContextHubProxy.getHubs(); |
| 161 | } catch (RemoteException e) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 162 | Log.e(TAG, "RemoteException while getting Context Hub info", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 163 | hubList = Collections.emptyList(); |
| 164 | } |
| 165 | mContextHubInfo = ContextHubServiceUtil.createContextHubInfoArray(hubList); |
| 166 | |
| 167 | for (ContextHubInfo contextHubInfo : mContextHubInfo) { |
| 168 | int contextHubId = contextHubInfo.getId(); |
| 169 | try { |
| 170 | mContextHubProxy.registerCallback( |
| 171 | contextHubId, new ContextHubServiceCallback(contextHubId)); |
| 172 | } catch (RemoteException e) { |
| 173 | Log.e(TAG, "RemoteException while registering service callback for hub (ID = " |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 174 | + contextHubId + ")", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | // Do a query to initialize the service cache list of nanoapps |
| 179 | // TODO(b/69270990): Remove this when old API is deprecated |
| 180 | for (ContextHubInfo contextHubInfo : mContextHubInfo) { |
| 181 | queryNanoAppsInternal(contextHubInfo.getId()); |
| 182 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 183 | |
| 184 | for (int i = 0; i < mContextHubInfo.length; i++) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 185 | Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId() |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 186 | + ", name: " + mContextHubInfo[i].getName()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 189 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 190 | /** |
| 191 | * @return the IContexthub proxy interface |
| 192 | */ |
| 193 | private IContexthub getContextHubProxy() { |
| 194 | IContexthub proxy = null; |
| 195 | try { |
| 196 | proxy = IContexthub.getService(true /* retry */); |
| 197 | } catch (RemoteException e) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 198 | Log.e(TAG, "RemoteException while attaching to Context Hub HAL proxy", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 199 | } catch (NoSuchElementException e) { |
| 200 | Log.i(TAG, "Context Hub HAL service not found"); |
| 201 | } |
| 202 | |
| 203 | return proxy; |
| 204 | } |
| 205 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 206 | @Override |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 207 | public int registerCallback(IContextHubCallback callback) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 208 | checkPermissions(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 209 | mCallbacksList.register(callback); |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 210 | Log.d(TAG, "Added callback, total callbacks " + |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 211 | mCallbacksList.getRegisteredCallbackCount()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public int[] getContextHubHandles() throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 217 | checkPermissions(); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 218 | int[] returnArray = new int[mContextHubInfo.length]; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 219 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 220 | Log.d(TAG, "System supports " + returnArray.length + " hubs"); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 221 | for (int i = 0; i < returnArray.length; ++i) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 222 | returnArray[i] = i; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 223 | Log.d(TAG, String.format("Hub %s is mapped to %d", |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 224 | mContextHubInfo[i].getName(), returnArray[i])); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return returnArray; |
| 228 | } |
| 229 | |
| 230 | @Override |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 231 | public ContextHubInfo getContextHubInfo(int contextHubId) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 232 | checkPermissions(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 233 | if (!(contextHubId >= 0 && contextHubId < mContextHubInfo.length)) { |
| 234 | Log.e(TAG, "Invalid context hub handle " + contextHubId); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 235 | return null; // null means fail |
| 236 | } |
| 237 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 238 | return mContextHubInfo[contextHubId]; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Creates an internal load transaction callback to be used for old API clients |
| 243 | * |
| 244 | * @param contextHubId the ID of the hub to load the binary |
| 245 | * @param nanoAppBinary the binary to load |
| 246 | * @return the callback interface |
| 247 | */ |
| 248 | private IContextHubTransactionCallback createLoadTransactionCallback( |
| 249 | int contextHubId, NanoAppBinary nanoAppBinary) { |
| 250 | return new IContextHubTransactionCallback.Stub() { |
| 251 | @Override |
| 252 | public void onTransactionComplete(int result) { |
| 253 | handleLoadResponseOldApi(contextHubId, result, nanoAppBinary); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void onQueryResponse(int result, List<NanoAppState> nanoAppStateList) { |
| 258 | } |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Creates an internal unload transaction callback to be used for old API clients |
| 264 | * |
| 265 | * @param contextHubId the ID of the hub to unload the nanoapp |
| 266 | * @param nanoAppId the ID of the nanoapp to unload |
| 267 | * @return the callback interface |
| 268 | */ |
| 269 | private IContextHubTransactionCallback createUnloadTransactionCallback( |
| 270 | int contextHubId, long nanoAppId) { |
| 271 | return new IContextHubTransactionCallback.Stub() { |
| 272 | @Override |
| 273 | public void onTransactionComplete(int result) { |
| 274 | handleUnloadResponseOldApi(contextHubId, result, nanoAppId); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void onQueryResponse(int result, List<NanoAppState> nanoAppStateList) { |
| 279 | } |
| 280 | }; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Creates an internal query transaction callback to be used for old API clients |
| 285 | * |
| 286 | * @param contextHubId the ID of the hub to query |
| 287 | * @return the callback interface |
| 288 | */ |
| 289 | private IContextHubTransactionCallback createQueryTransactionCallback(int contextHubId) { |
| 290 | return new IContextHubTransactionCallback.Stub() { |
| 291 | @Override |
| 292 | public void onTransactionComplete(int result) { |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void onQueryResponse(int result, List<NanoAppState> nanoAppStateList) { |
| 297 | byte[] data = {(byte) result}; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 298 | onMessageReceiptOldApi(MSG_QUERY_NANO_APPS, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 299 | } |
| 300 | }; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Adds a new transaction to the transaction manager queue |
| 305 | * |
| 306 | * @param transaction the transaction to add |
| 307 | * @return the result of adding the transaction |
| 308 | */ |
| 309 | private int addTransaction(ContextHubServiceTransaction transaction) { |
| 310 | int result = Result.OK; |
| 311 | try { |
| 312 | mTransactionManager.addTransaction(transaction); |
| 313 | } catch (IllegalStateException e) { |
| 314 | Log.e(TAG, e.getMessage()); |
| 315 | result = Result.TRANSACTION_PENDING; /* failed */ |
| 316 | } |
| 317 | |
| 318 | return result; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 322 | public int loadNanoApp(int contextHubId, NanoApp app) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 323 | checkPermissions(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 324 | if (mContextHubProxy == null) { |
| 325 | return -1; |
| 326 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 327 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 328 | if (!(contextHubId >= 0 && contextHubId < mContextHubInfo.length)) { |
| 329 | Log.e(TAG, "Invalid contextHubhandle " + contextHubId); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 330 | return -1; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 331 | } |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 332 | if (app == null) { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 333 | Log.e(TAG, "Invalid null app"); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 334 | return -1; |
| 335 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 336 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 337 | // Create an internal IContextHubTransactionCallback for the old API clients |
| 338 | NanoAppBinary nanoAppBinary = new NanoAppBinary(app.getAppBinary()); |
| 339 | IContextHubTransactionCallback onCompleteCallback = |
| 340 | createLoadTransactionCallback(contextHubId, nanoAppBinary); |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 341 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 342 | ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction( |
| 343 | contextHubId, nanoAppBinary, onCompleteCallback); |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 344 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 345 | int result = addTransaction(transaction); |
| 346 | if (result != Result.OK) { |
| 347 | Log.e(TAG, "Failed to load nanoapp with error code " + result); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 348 | return -1; |
| 349 | } |
Ashutosh Joshi | 1186440 | 2016-07-15 13:46:22 -0700 | [diff] [blame] | 350 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 351 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 352 | return 0; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 353 | } |
| 354 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 355 | @Override |
| 356 | public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { |
| 357 | checkPermissions(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 358 | if (mContextHubProxy == null) { |
| 359 | return -1; |
| 360 | } |
| 361 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 362 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); |
| 363 | if (info == null) { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 364 | Log.e(TAG, "Cannot find app with handle " + nanoAppInstanceHandle); |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 365 | return -1; //means failed |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 368 | int contextHubId = info.getContexthubId(); |
| 369 | long nanoAppId = info.getAppId(); |
| 370 | IContextHubTransactionCallback onCompleteCallback = |
| 371 | createUnloadTransactionCallback(contextHubId, nanoAppId); |
| 372 | ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction( |
| 373 | contextHubId, nanoAppId, onCompleteCallback); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 374 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 375 | int result = addTransaction(transaction); |
| 376 | if (result != Result.OK) { |
| 377 | Log.e(TAG, "Failed to unload nanoapp with error code " + result); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 382 | return 0; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 383 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 384 | |
| 385 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 386 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 387 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 388 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 389 | // This assumes that all the nanoAppInfo is current. This is reasonable |
| 390 | // for the use cases for tightly controlled nanoApps. |
| 391 | if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 392 | return mNanoAppHash.get(nanoAppInstanceHandle); |
| 393 | } else { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 394 | Log.e(TAG, "Could not find nanoApp with handle " + nanoAppInstanceHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 395 | return null; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | @Override |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 400 | public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 401 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 402 | ArrayList<Integer> foundInstances = new ArrayList<Integer>(); |
| 403 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 404 | for (Integer nanoAppInstance : mNanoAppHash.keySet()) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 405 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); |
| 406 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 407 | if (filter.testMatch(info)) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 408 | foundInstances.add(nanoAppInstance); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | int[] retArray = new int[foundInstances.size()]; |
| 413 | for (int i = 0; i < foundInstances.size(); i++) { |
| 414 | retArray[i] = foundInstances.get(i).intValue(); |
| 415 | } |
| 416 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 417 | Log.w(TAG, "Found " + retArray.length + " apps on hub handle " + hubHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 418 | return retArray; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Performs a query at the specified hub. |
| 423 | * |
| 424 | * This method should only be invoked internally by the service, either to update the service |
| 425 | * cache or as a result of an explicit query requested by a client through the sendMessage API. |
| 426 | * |
| 427 | * @param contextHubId the ID of the hub to do the query |
| 428 | * @return the result of the query |
| 429 | */ |
| 430 | private int queryNanoAppsInternal(int contextHubId) { |
| 431 | if (mContextHubProxy == null) { |
| 432 | return Result.UNKNOWN_FAILURE; |
| 433 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 434 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 435 | IContextHubTransactionCallback onCompleteCallback = |
| 436 | createQueryTransactionCallback(contextHubId); |
| 437 | ContextHubServiceTransaction transaction = mTransactionManager.createQueryTransaction( |
| 438 | contextHubId, onCompleteCallback); |
| 439 | |
| 440 | return addTransaction(transaction); |
| 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public int sendMessage( |
| 445 | int hubHandle, int nanoAppHandle, ContextHubMessage msg) throws RemoteException { |
| 446 | checkPermissions(); |
| 447 | if (mContextHubProxy == null) { |
| 448 | return -1; |
| 449 | } |
| 450 | if (msg == null) { |
| 451 | Log.e(TAG, "ContextHubMessage cannot be null"); |
| 452 | return -1; |
| 453 | } |
| 454 | if (msg.getData() == null) { |
| 455 | Log.w(TAG, "ContextHubMessage message body cannot be null"); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 456 | return -1; |
| 457 | } |
| 458 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 459 | int result; |
| 460 | if (nanoAppHandle == OS_APP_INSTANCE) { |
| 461 | if (msg.getMsgType() == MSG_QUERY_NANO_APPS) { |
| 462 | result = queryNanoAppsInternal(hubHandle); |
| 463 | } else { |
| 464 | Log.e(TAG, "Invalid OS message params of type " + msg.getMsgType()); |
| 465 | result = Result.BAD_PARAMS; |
| 466 | } |
| 467 | } else { |
| 468 | NanoAppInstanceInfo info = getNanoAppInstanceInfo(nanoAppHandle); |
| 469 | if (info != null) { |
| 470 | ContextHubMsg hubMessage = new ContextHubMsg(); |
| 471 | hubMessage.appName = info.getAppId(); |
| 472 | hubMessage.msgType = msg.getMsgType(); |
| 473 | hubMessage.hostEndPoint = HostEndPoint.UNSPECIFIED; |
| 474 | ContextHubServiceUtil.copyToByteArrayList(msg.getData(), hubMessage.msg); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 475 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 476 | try { |
| 477 | result = mContextHubProxy.sendMessageToHub(hubHandle, hubMessage); |
| 478 | } catch (RemoteException e) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 479 | Log.e(TAG, "Failed to send nanoapp message - RemoteException", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 480 | result = Result.UNKNOWN_FAILURE; |
| 481 | } |
| 482 | } else { |
| 483 | Log.e(TAG, "Failed to send nanoapp message - nanoapp with instance ID " |
| 484 | + nanoAppHandle + " does not exist."); |
| 485 | result = Result.BAD_PARAMS; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return (result == Result.OK ? 0 : -1); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Handles a unicast or broadcast message from a nanoapp. |
| 494 | * |
| 495 | * @param contextHubId the ID of the hub the message came from |
| 496 | * @param message the message contents |
| 497 | */ |
| 498 | private void handleClientMessageCallback(int contextHubId, ContextHubMsg message) { |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 499 | byte[] data = ContextHubServiceUtil.createPrimitiveByteArray(message.msg); |
| 500 | |
| 501 | int nanoAppInstanceId = mNanoAppIdToInstanceMap.containsKey(message.appName) ? |
| 502 | mNanoAppIdToInstanceMap.get(message.appName) : -1; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 503 | onMessageReceiptOldApi(message.msgType, contextHubId, nanoAppInstanceId, data); |
| 504 | |
| 505 | mClientManager.onMessageFromNanoApp(contextHubId, message); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /** |
| 509 | * A helper function to handle a load response from the Context Hub for the old API. |
| 510 | * |
| 511 | * TODO(b/69270990): Remove this once the old APIs are obsolete. |
| 512 | */ |
| 513 | private void handleLoadResponseOldApi( |
| 514 | int contextHubId, int result, NanoAppBinary nanoAppBinary) { |
| 515 | if (nanoAppBinary == null) { |
| 516 | Log.e(TAG, "Nanoapp binary field was null for a load transaction"); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | // NOTE: The legacy JNI code used to do a query right after a load success |
| 521 | // to synchronize the service cache. Instead store the binary that was requested to |
| 522 | // load to update the cache later without doing a query. |
| 523 | int instanceId = 0; |
| 524 | long nanoAppId = nanoAppBinary.getNanoAppId(); |
| 525 | int nanoAppVersion = nanoAppBinary.getNanoAppVersion(); |
| 526 | if (result == TransactionResult.SUCCESS) { |
| 527 | if (mNanoAppIdToInstanceMap.containsKey(nanoAppId)) { |
| 528 | instanceId = mNanoAppIdToInstanceMap.get(nanoAppId); |
| 529 | } else { |
| 530 | instanceId = mNextAvailableInstanceId++; |
| 531 | mNanoAppIdToInstanceMap.put(nanoAppId, instanceId); |
| 532 | } |
| 533 | |
| 534 | addAppInstance(contextHubId, instanceId, nanoAppId, nanoAppVersion); |
| 535 | } |
| 536 | |
| 537 | byte[] data = new byte[5]; |
| 538 | data[0] = (byte) result; |
| 539 | ByteBuffer.wrap(data, 1, 4).order(ByteOrder.nativeOrder()).putInt(instanceId); |
| 540 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 541 | onMessageReceiptOldApi(MSG_LOAD_NANO_APP, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * A helper function to handle an unload response from the Context Hub for the old API. |
| 546 | * |
| 547 | * TODO(b/69270990): Remove this once the old APIs are obsolete. |
| 548 | */ |
| 549 | private void handleUnloadResponseOldApi( |
| 550 | int contextHubId, int result, long nanoAppId) { |
| 551 | if (result == TransactionResult.SUCCESS) { |
| 552 | int instanceId = mNanoAppIdToInstanceMap.get(nanoAppId); |
| 553 | deleteAppInstance(instanceId); |
| 554 | mNanoAppIdToInstanceMap.remove(nanoAppId); |
| 555 | } |
| 556 | |
| 557 | byte[] data = new byte[1]; |
| 558 | data[0] = (byte) result; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 559 | onMessageReceiptOldApi(MSG_UNLOAD_NANO_APP, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | /** |
| 563 | * Handles a transaction response from a Context Hub. |
| 564 | * |
| 565 | * @param contextHubId the ID of the hub the response came from |
| 566 | * @param transactionId the ID of the transaction |
| 567 | * @param result the result of the transaction reported by the hub |
| 568 | */ |
| 569 | private void handleTransactionResultCallback(int contextHubId, int transactionId, int result) { |
| 570 | mTransactionManager.onTransactionResponse(transactionId, result); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Handles an asynchronous event from a Context Hub. |
| 575 | * |
| 576 | * @param contextHubId the ID of the hub the response came from |
| 577 | * @param eventType the type of the event as defined in Context Hub HAL AsyncEventType |
| 578 | */ |
| 579 | private void handleHubEventCallback(int contextHubId, int eventType) { |
| 580 | if (eventType == AsyncEventType.RESTARTED) { |
| 581 | mTransactionManager.onHubReset(); |
| 582 | queryNanoAppsInternal(contextHubId); |
| 583 | |
| 584 | byte[] data = {TransactionResult.SUCCESS}; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 585 | onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 586 | } else { |
| 587 | Log.i(TAG, "Received unknown hub event (hub ID = " + contextHubId + ", type = " |
| 588 | + eventType + ")"); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Handles an asynchronous abort event of a nanoapp. |
| 594 | * |
| 595 | * @param contextHubId the ID of the hub that the nanoapp aborted in |
| 596 | * @param nanoAppId the ID of the aborted nanoapp |
| 597 | * @param abortCode the nanoapp-specific abort code |
| 598 | */ |
| 599 | private void handleAppAbortCallback(int contextHubId, long nanoAppId, int abortCode) { |
| 600 | // TODO(b/31049861): Implement this |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Handles a query response from a Context Hub. |
| 605 | * |
| 606 | * @param contextHubId the ID of the hub of the response |
| 607 | * @param nanoAppInfoList the list of loaded nanoapps |
| 608 | */ |
| 609 | private void handleQueryAppsCallback(int contextHubId, List<HubAppInfo> nanoAppInfoList) { |
| 610 | List<NanoAppState> nanoAppStateList = |
| 611 | ContextHubServiceUtil.createNanoAppStateList(nanoAppInfoList); |
| 612 | |
| 613 | updateServiceCache(contextHubId, nanoAppInfoList); |
| 614 | mTransactionManager.onQueryResponse(nanoAppStateList); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Updates the service's cache of the list of loaded nanoapps using a nanoapp list response. |
| 619 | * |
| 620 | * TODO(b/69270990): Remove this when the old API functionality is removed. |
| 621 | * |
| 622 | * @param contextHubId the ID of the hub the response came from |
| 623 | * @param nanoAppInfoList the list of loaded nanoapps |
| 624 | */ |
| 625 | private void updateServiceCache(int contextHubId, List<HubAppInfo> nanoAppInfoList) { |
| 626 | synchronized (mNanoAppHash) { |
| 627 | for (int instanceId : mNanoAppHash.keySet()) { |
| 628 | if (mNanoAppHash.get(instanceId).getContexthubId() == contextHubId) { |
| 629 | deleteAppInstance(instanceId); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | for (HubAppInfo appInfo : nanoAppInfoList) { |
| 634 | int instanceId; |
| 635 | long nanoAppId = appInfo.appId; |
| 636 | if (mNanoAppIdToInstanceMap.containsKey(nanoAppId)) { |
| 637 | instanceId = mNanoAppIdToInstanceMap.get(nanoAppId); |
| 638 | } else { |
| 639 | instanceId = mNextAvailableInstanceId++; |
| 640 | mNanoAppIdToInstanceMap.put(nanoAppId, instanceId); |
| 641 | } |
| 642 | |
| 643 | addAppInstance(contextHubId, instanceId, nanoAppId, appInfo.version); |
| 644 | } |
| 645 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 646 | } |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 647 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 648 | /** |
| 649 | * @param contextHubId the hub ID to validate |
| 650 | * @return {@code true} if the ID represents that of an available hub, {@code false} otherwise |
| 651 | */ |
| 652 | private boolean isValidContextHubId(int contextHubId) { |
| 653 | for (ContextHubInfo hubInfo : mContextHubInfo) { |
| 654 | if (hubInfo.getId() == contextHubId) { |
| 655 | return true; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Creates and registers a client at the service for the specified Context Hub. |
| 664 | * |
| 665 | * @param clientCallback the client interface to register with the service |
| 666 | * @param contextHubId the ID of the hub this client is attached to |
| 667 | * @return the generated client interface, null if registration was unsuccessful |
| 668 | * |
| 669 | * @throws IllegalArgumentException if contextHubId is not a valid ID |
| 670 | * @throws IllegalStateException if max number of clients have already registered |
| 671 | * @throws NullPointerException if clientCallback is null |
| 672 | */ |
| 673 | @Override |
| 674 | public IContextHubClient createClient( |
| 675 | IContextHubClientCallback clientCallback, int contextHubId) throws RemoteException { |
| 676 | checkPermissions(); |
| 677 | if (!isValidContextHubId(contextHubId)) { |
| 678 | throw new IllegalArgumentException("Invalid context hub ID " + contextHubId); |
| 679 | } |
| 680 | if (clientCallback == null) { |
| 681 | throw new NullPointerException("Cannot register client with null callback"); |
| 682 | } |
| 683 | |
| 684 | return mClientManager.registerClient(clientCallback, contextHubId); |
| 685 | } |
| 686 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 687 | @Override |
| 688 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 689 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 690 | |
| 691 | pw.println("Dumping ContextHub Service"); |
| 692 | |
| 693 | pw.println(""); |
| 694 | // dump ContextHubInfo |
| 695 | pw.println("=================== CONTEXT HUBS ===================="); |
| 696 | for (int i = 0; i < mContextHubInfo.length; i++) { |
| 697 | pw.println("Handle " + i + " : " + mContextHubInfo[i].toString()); |
| 698 | } |
| 699 | pw.println(""); |
| 700 | pw.println("=================== NANOAPPS ===================="); |
| 701 | // Dump nanoAppHash |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 702 | for (Integer nanoAppInstance : mNanoAppHash.keySet()) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 703 | pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString()); |
| 704 | } |
| 705 | |
| 706 | // dump eventLog |
| 707 | } |
| 708 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 709 | private void checkPermissions() { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 710 | ContextHubServiceUtil.checkPermissions(mContext); |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 711 | } |
Ashutosh Joshi | 2c697fb | 2016-04-01 20:48:13 +0000 | [diff] [blame] | 712 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame^] | 713 | private int onMessageReceiptOldApi(int msgType, int hubHandle, int appInstance, byte[] data) { |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 714 | if (data == null) { |
| 715 | return -1; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 716 | } |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 717 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 718 | int msgVersion = 0; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 719 | int callbacksCount = mCallbacksList.beginBroadcast(); |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 720 | Log.d(TAG, "Sending message " + msgType + " version " + msgVersion + " from hubHandle " + |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 721 | hubHandle + ", appInstance " + appInstance + ", callBackCount " + callbacksCount); |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 722 | |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 723 | if (callbacksCount < 1) { |
| 724 | Log.v(TAG, "No message callbacks registered."); |
| 725 | return 0; |
| 726 | } |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 727 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 728 | ContextHubMessage msg = new ContextHubMessage(msgType, msgVersion, data); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 729 | for (int i = 0; i < callbacksCount; ++i) { |
| 730 | IContextHubCallback callback = mCallbacksList.getBroadcastItem(i); |
| 731 | try { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 732 | callback.onMessageReceipt(hubHandle, appInstance, msg); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 733 | } catch (RemoteException e) { |
| 734 | Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ")."); |
| 735 | continue; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 738 | mCallbacksList.finishBroadcast(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) { |
| 743 | // App Id encodes vendor & version |
| 744 | NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo(); |
| 745 | |
| 746 | appInfo.setAppId(appId); |
| 747 | appInfo.setAppVersion(appVersion); |
| 748 | appInfo.setName(PRE_LOADED_APP_NAME); |
| 749 | appInfo.setContexthubId(hubHandle); |
| 750 | appInfo.setHandle(appInstanceHandle); |
| 751 | appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER); |
| 752 | appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 753 | appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 754 | appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 755 | |
Greg Kaiser | fe6d4f5 | 2016-08-19 10:24:07 -0700 | [diff] [blame] | 756 | String action; |
| 757 | if (mNanoAppHash.containsKey(appInstanceHandle)) { |
| 758 | action = "Updated"; |
| 759 | } else { |
| 760 | action = "Added"; |
| 761 | } |
| 762 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 763 | mNanoAppHash.put(appInstanceHandle, appInfo); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 764 | Log.d(TAG, action + " app instance " + appInstanceHandle + " with id 0x" |
| 765 | + Long.toHexString(appId) + " version 0x" + Integer.toHexString(appVersion)); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 766 | |
| 767 | return 0; |
| 768 | } |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 769 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 770 | private int deleteAppInstance(int appInstanceHandle) { |
| 771 | if (mNanoAppHash.remove(appInstanceHandle) == null) { |
| 772 | return -1; |
| 773 | } |
| 774 | |
| 775 | return 0; |
| 776 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 777 | } |