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; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 23 | import android.hardware.contexthub.V1_0.HubAppInfo; |
| 24 | import android.hardware.contexthub.V1_0.IContexthub; |
| 25 | import android.hardware.contexthub.V1_0.IContexthubCallback; |
| 26 | import android.hardware.contexthub.V1_0.Result; |
| 27 | import android.hardware.contexthub.V1_0.TransactionResult; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 28 | import android.hardware.location.ContextHubInfo; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 29 | import android.hardware.location.ContextHubMessage; |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 30 | import android.hardware.location.ContextHubTransaction; |
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 | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 40 | import android.hardware.location.NanoAppMessage; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 41 | import android.hardware.location.NanoAppState; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 42 | import android.os.RemoteCallbackList; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 43 | import android.os.RemoteException; |
| 44 | import android.util.Log; |
| 45 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 46 | import com.android.internal.util.DumpUtils; |
| 47 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 48 | import java.io.FileDescriptor; |
| 49 | import java.io.PrintWriter; |
Ashutosh Joshi | 19753cc | 2016-11-23 13:56:39 -0800 | [diff] [blame] | 50 | import java.nio.ByteBuffer; |
| 51 | import java.nio.ByteOrder; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 53 | import java.util.Collections; |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 54 | import java.util.HashMap; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 55 | import java.util.List; |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 56 | import java.util.Map; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 57 | import java.util.NoSuchElementException; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * @hide |
| 61 | */ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 62 | public class ContextHubService extends IContextHubService.Stub { |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 63 | private static final String TAG = "ContextHubService"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 64 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 65 | /* |
| 66 | * Constants for the type of transaction that is defined by ContextHubService. |
| 67 | * This is used to report the transaction callback to clients, and is different from |
| 68 | * ContextHubTransaction.Type. |
| 69 | */ |
| 70 | public static final int MSG_ENABLE_NANO_APP = 1; |
| 71 | public static final int MSG_DISABLE_NANO_APP = 2; |
| 72 | public static final int MSG_LOAD_NANO_APP = 3; |
Ashutosh Joshi | cafdee9 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 73 | public static final int MSG_UNLOAD_NANO_APP = 4; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 74 | public static final int MSG_QUERY_NANO_APPS = 5; |
| 75 | public static final int MSG_QUERY_MEMORY = 6; |
| 76 | public static final int MSG_HUB_RESET = 7; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 77 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 78 | private static final int OS_APP_INSTANCE = -1; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 79 | |
Arthur Ishiguro | b4f5987 | 2018-07-31 16:38:08 -0700 | [diff] [blame^] | 80 | /* |
| 81 | * Local flag to enable debug logging. |
| 82 | */ |
| 83 | private static final boolean DEBUG_LOG_ENABLED = false; |
| 84 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 85 | private final Context mContext; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 86 | |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 87 | private final Map<Integer, ContextHubInfo> mContextHubIdToInfoMap; |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 88 | private final List<ContextHubInfo> mContextHubInfoList; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 89 | private final RemoteCallbackList<IContextHubCallback> mCallbacksList = |
| 90 | new RemoteCallbackList<>(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 91 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 92 | // Proxy object to communicate with the Context Hub HAL |
| 93 | private final IContexthub mContextHubProxy; |
| 94 | |
| 95 | // The manager for transaction queue |
| 96 | private final ContextHubTransactionManager mTransactionManager; |
| 97 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 98 | // The manager for sending messages to/from clients |
| 99 | private final ContextHubClientManager mClientManager; |
| 100 | |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 101 | // The default client for old API clients |
| 102 | private final Map<Integer, IContextHubClient> mDefaultClientMap; |
| 103 | |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 104 | // The manager for the internal nanoapp state cache |
| 105 | private final NanoAppStateManager mNanoAppStateManager = new NanoAppStateManager(); |
| 106 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Class extending the callback to register with a Context Hub. |
| 109 | */ |
| 110 | private class ContextHubServiceCallback extends IContexthubCallback.Stub { |
| 111 | private final int mContextHubId; |
| 112 | |
| 113 | ContextHubServiceCallback(int contextHubId) { |
| 114 | mContextHubId = contextHubId; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void handleClientMsg(ContextHubMsg message) { |
| 119 | handleClientMessageCallback(mContextHubId, message); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void handleTxnResult(int transactionId, int result) { |
| 124 | handleTransactionResultCallback(mContextHubId, transactionId, result); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void handleHubEvent(int eventType) { |
| 129 | handleHubEventCallback(mContextHubId, eventType); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void handleAppAbort(long nanoAppId, int abortCode) { |
| 134 | handleAppAbortCallback(mContextHubId, nanoAppId, abortCode); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void handleAppsInfo(ArrayList<HubAppInfo> nanoAppInfoList) { |
| 139 | handleQueryAppsCallback(mContextHubId, nanoAppInfoList); |
| 140 | } |
| 141 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 142 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 143 | public ContextHubService(Context context) { |
| 144 | mContext = context; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 145 | |
| 146 | mContextHubProxy = getContextHubProxy(); |
| 147 | if (mContextHubProxy == null) { |
| 148 | mTransactionManager = null; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 149 | mClientManager = null; |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 150 | mDefaultClientMap = Collections.emptyMap(); |
| 151 | mContextHubIdToInfoMap = Collections.emptyMap(); |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 152 | mContextHubInfoList = Collections.emptyList(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 156 | mClientManager = new ContextHubClientManager(mContext, mContextHubProxy); |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 157 | mTransactionManager = new ContextHubTransactionManager( |
| 158 | mContextHubProxy, mClientManager, mNanoAppStateManager); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 159 | |
| 160 | List<ContextHub> hubList; |
| 161 | try { |
| 162 | hubList = mContextHubProxy.getHubs(); |
| 163 | } catch (RemoteException e) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 164 | Log.e(TAG, "RemoteException while getting Context Hub info", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 165 | hubList = Collections.emptyList(); |
| 166 | } |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 167 | mContextHubIdToInfoMap = Collections.unmodifiableMap( |
| 168 | ContextHubServiceUtil.createContextHubInfoMap(hubList)); |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 169 | mContextHubInfoList = new ArrayList<>(mContextHubIdToInfoMap.values()); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 170 | |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 171 | HashMap<Integer, IContextHubClient> defaultClientMap = new HashMap<>(); |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 172 | for (int contextHubId : mContextHubIdToInfoMap.keySet()) { |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 173 | IContextHubClient client = mClientManager.registerClient( |
| 174 | createDefaultClientCallback(contextHubId), contextHubId); |
| 175 | defaultClientMap.put(contextHubId, client); |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 176 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 177 | try { |
| 178 | mContextHubProxy.registerCallback( |
| 179 | contextHubId, new ContextHubServiceCallback(contextHubId)); |
| 180 | } catch (RemoteException e) { |
| 181 | Log.e(TAG, "RemoteException while registering service callback for hub (ID = " |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 182 | + contextHubId + ")", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 183 | } |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 184 | |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 185 | // Do a query to initialize the service cache list of nanoapps |
| 186 | // TODO(b/69270990): Remove this when old API is deprecated |
| 187 | queryNanoAppsInternal(contextHubId); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 188 | } |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 189 | mDefaultClientMap = Collections.unmodifiableMap(defaultClientMap); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 190 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 191 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 192 | /** |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 193 | * Creates a default client callback for old API clients. |
| 194 | * |
| 195 | * @param contextHubId the ID of the hub to attach this client to |
| 196 | * @return the internal callback interface |
| 197 | */ |
| 198 | private IContextHubClientCallback createDefaultClientCallback(int contextHubId) { |
| 199 | return new IContextHubClientCallback.Stub() { |
| 200 | @Override |
| 201 | public void onMessageFromNanoApp(NanoAppMessage message) { |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 202 | int nanoAppHandle = mNanoAppStateManager.getNanoAppHandle( |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 203 | contextHubId, message.getNanoAppId()); |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 204 | |
| 205 | onMessageReceiptOldApi( |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 206 | message.getMessageType(), contextHubId, nanoAppHandle, |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 207 | message.getMessageBody()); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void onHubReset() { |
Arthur Ishiguro | 6d47c54 | 2017-11-17 15:49:07 -0800 | [diff] [blame] | 212 | byte[] data = {TransactionResult.SUCCESS}; |
| 213 | onMessageReceiptOldApi(MSG_HUB_RESET, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void onNanoAppAborted(long nanoAppId, int abortCode) { |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onNanoAppLoaded(long nanoAppId) { |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void onNanoAppUnloaded(long nanoAppId) { |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void onNanoAppEnabled(long nanoAppId) { |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void onNanoAppDisabled(long nanoAppId) { |
| 234 | } |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | /** |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 239 | * @return the IContexthub proxy interface |
| 240 | */ |
| 241 | private IContexthub getContextHubProxy() { |
| 242 | IContexthub proxy = null; |
| 243 | try { |
| 244 | proxy = IContexthub.getService(true /* retry */); |
| 245 | } catch (RemoteException e) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 246 | Log.e(TAG, "RemoteException while attaching to Context Hub HAL proxy", e); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 247 | } catch (NoSuchElementException e) { |
| 248 | Log.i(TAG, "Context Hub HAL service not found"); |
| 249 | } |
| 250 | |
| 251 | return proxy; |
| 252 | } |
| 253 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 254 | @Override |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 255 | public int registerCallback(IContextHubCallback callback) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 256 | checkPermissions(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 257 | mCallbacksList.register(callback); |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 258 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 259 | Log.d(TAG, "Added callback, total callbacks " + |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 260 | mCallbacksList.getRegisteredCallbackCount()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public int[] getContextHubHandles() throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 266 | checkPermissions(); |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 267 | return ContextHubServiceUtil.createPrimitiveIntArray(mContextHubIdToInfoMap.keySet()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 271 | public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 272 | checkPermissions(); |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 273 | if (!mContextHubIdToInfoMap.containsKey(contextHubHandle)) { |
| 274 | Log.e(TAG, "Invalid Context Hub handle " + contextHubHandle + " in getContextHubInfo"); |
| 275 | return null; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 278 | return mContextHubIdToInfoMap.get(contextHubHandle); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Arthur Ishiguro | fdbbd46 | 2017-11-27 16:33:36 -0800 | [diff] [blame] | 282 | * Returns a List of ContextHubInfo object describing the available hubs. |
| 283 | * |
| 284 | * @return the List of ContextHubInfo objects |
| 285 | */ |
| 286 | @Override |
| 287 | public List<ContextHubInfo> getContextHubs() throws RemoteException { |
| 288 | checkPermissions(); |
| 289 | return mContextHubInfoList; |
| 290 | } |
| 291 | |
| 292 | /** |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 293 | * Creates an internal load transaction callback to be used for old API clients |
| 294 | * |
| 295 | * @param contextHubId the ID of the hub to load the binary |
| 296 | * @param nanoAppBinary the binary to load |
| 297 | * @return the callback interface |
| 298 | */ |
| 299 | private IContextHubTransactionCallback createLoadTransactionCallback( |
| 300 | int contextHubId, NanoAppBinary nanoAppBinary) { |
| 301 | return new IContextHubTransactionCallback.Stub() { |
| 302 | @Override |
| 303 | public void onTransactionComplete(int result) { |
| 304 | handleLoadResponseOldApi(contextHubId, result, nanoAppBinary); |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void onQueryResponse(int result, List<NanoAppState> nanoAppStateList) { |
| 309 | } |
| 310 | }; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Creates an internal unload transaction callback to be used for old API clients |
| 315 | * |
| 316 | * @param contextHubId the ID of the hub to unload the nanoapp |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 317 | * @return the callback interface |
| 318 | */ |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 319 | private IContextHubTransactionCallback createUnloadTransactionCallback(int contextHubId) { |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 320 | return new IContextHubTransactionCallback.Stub() { |
| 321 | @Override |
| 322 | public void onTransactionComplete(int result) { |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 323 | handleUnloadResponseOldApi(contextHubId, result); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void onQueryResponse(int result, List<NanoAppState> nanoAppStateList) { |
| 328 | } |
| 329 | }; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Creates an internal query transaction callback to be used for old API clients |
| 334 | * |
| 335 | * @param contextHubId the ID of the hub to query |
| 336 | * @return the callback interface |
| 337 | */ |
| 338 | private IContextHubTransactionCallback createQueryTransactionCallback(int contextHubId) { |
| 339 | return new IContextHubTransactionCallback.Stub() { |
| 340 | @Override |
| 341 | public void onTransactionComplete(int result) { |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void onQueryResponse(int result, List<NanoAppState> nanoAppStateList) { |
| 346 | byte[] data = {(byte) result}; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 347 | onMessageReceiptOldApi(MSG_QUERY_NANO_APPS, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 348 | } |
| 349 | }; |
| 350 | } |
| 351 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 352 | @Override |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 353 | public int loadNanoApp(int contextHubHandle, NanoApp nanoApp) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 354 | checkPermissions(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 355 | if (mContextHubProxy == null) { |
| 356 | return -1; |
| 357 | } |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 358 | if (!isValidContextHubId(contextHubHandle)) { |
| 359 | Log.e(TAG, "Invalid Context Hub handle " + contextHubHandle + " in loadNanoApp"); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 360 | return -1; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 361 | } |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 362 | if (nanoApp == null) { |
| 363 | Log.e(TAG, "NanoApp cannot be null in loadNanoApp"); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 364 | return -1; |
| 365 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 366 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 367 | // Create an internal IContextHubTransactionCallback for the old API clients |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 368 | NanoAppBinary nanoAppBinary = new NanoAppBinary(nanoApp.getAppBinary()); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 369 | IContextHubTransactionCallback onCompleteCallback = |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 370 | createLoadTransactionCallback(contextHubHandle, nanoAppBinary); |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 371 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 372 | ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction( |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 373 | contextHubHandle, nanoAppBinary, onCompleteCallback); |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 374 | |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 375 | mTransactionManager.addTransaction(transaction); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 376 | return 0; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 377 | } |
| 378 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 379 | @Override |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 380 | public int unloadNanoApp(int nanoAppHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 381 | checkPermissions(); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 382 | if (mContextHubProxy == null) { |
| 383 | return -1; |
| 384 | } |
| 385 | |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 386 | NanoAppInstanceInfo info = |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 387 | mNanoAppStateManager.getNanoAppInstanceInfo(nanoAppHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 388 | if (info == null) { |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 389 | Log.e(TAG, "Invalid nanoapp handle " + nanoAppHandle + " in unloadNanoApp"); |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 390 | return -1; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 393 | int contextHubId = info.getContexthubId(); |
| 394 | long nanoAppId = info.getAppId(); |
| 395 | IContextHubTransactionCallback onCompleteCallback = |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 396 | createUnloadTransactionCallback(contextHubId); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 397 | ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction( |
| 398 | contextHubId, nanoAppId, onCompleteCallback); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 399 | |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 400 | mTransactionManager.addTransaction(transaction); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 401 | return 0; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 402 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 403 | |
| 404 | @Override |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 405 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 406 | checkPermissions(); |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 407 | |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 408 | return mNanoAppStateManager.getNanoAppInstanceInfo(nanoAppHandle); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 412 | public int[] findNanoAppOnHub( |
| 413 | int contextHubHandle, NanoAppFilter filter) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 414 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 415 | |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 416 | ArrayList<Integer> foundInstances = new ArrayList<>(); |
| 417 | for (NanoAppInstanceInfo info : mNanoAppStateManager.getNanoAppInstanceInfoCollection()) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 418 | if (filter.testMatch(info)) { |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 419 | foundInstances.add(info.getHandle()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | int[] retArray = new int[foundInstances.size()]; |
| 424 | for (int i = 0; i < foundInstances.size(); i++) { |
| 425 | retArray[i] = foundInstances.get(i).intValue(); |
| 426 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 427 | return retArray; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 430 | /** |
| 431 | * Performs a query at the specified hub. |
| 432 | * |
| 433 | * This method should only be invoked internally by the service, either to update the service |
| 434 | * cache or as a result of an explicit query requested by a client through the sendMessage API. |
| 435 | * |
| 436 | * @param contextHubId the ID of the hub to do the query |
| 437 | * @return the result of the query |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 438 | * |
| 439 | * @throws IllegalStateException if the transaction queue is full |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 440 | */ |
| 441 | private int queryNanoAppsInternal(int contextHubId) { |
| 442 | if (mContextHubProxy == null) { |
| 443 | return Result.UNKNOWN_FAILURE; |
| 444 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 445 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 446 | IContextHubTransactionCallback onCompleteCallback = |
| 447 | createQueryTransactionCallback(contextHubId); |
| 448 | ContextHubServiceTransaction transaction = mTransactionManager.createQueryTransaction( |
| 449 | contextHubId, onCompleteCallback); |
| 450 | |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 451 | mTransactionManager.addTransaction(transaction); |
| 452 | return Result.OK; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | @Override |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 456 | public int sendMessage(int contextHubHandle, int nanoAppHandle, ContextHubMessage msg) |
| 457 | throws RemoteException { |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 458 | checkPermissions(); |
| 459 | if (mContextHubProxy == null) { |
| 460 | return -1; |
| 461 | } |
| 462 | if (msg == null) { |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 463 | Log.e(TAG, "ContextHubMessage cannot be null in sendMessage"); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 464 | return -1; |
| 465 | } |
| 466 | if (msg.getData() == null) { |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 467 | Log.e(TAG, "ContextHubMessage message body cannot be null in sendMessage"); |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 468 | return -1; |
| 469 | } |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 470 | if (!isValidContextHubId(contextHubHandle)) { |
| 471 | Log.e(TAG, "Invalid Context Hub handle " + contextHubHandle + " in sendMessage"); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 472 | return -1; |
| 473 | } |
| 474 | |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 475 | boolean success = false; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 476 | if (nanoAppHandle == OS_APP_INSTANCE) { |
| 477 | if (msg.getMsgType() == MSG_QUERY_NANO_APPS) { |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 478 | success = (queryNanoAppsInternal(contextHubHandle) == Result.OK); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 479 | } else { |
| 480 | Log.e(TAG, "Invalid OS message params of type " + msg.getMsgType()); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 481 | } |
| 482 | } else { |
| 483 | NanoAppInstanceInfo info = getNanoAppInstanceInfo(nanoAppHandle); |
| 484 | if (info != null) { |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 485 | NanoAppMessage message = NanoAppMessage.createMessageToNanoApp( |
| 486 | info.getAppId(), msg.getMsgType(), msg.getData()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 487 | |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 488 | IContextHubClient client = mDefaultClientMap.get(contextHubHandle); |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 489 | success = (client.sendMessageToNanoApp(message) == |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 490 | ContextHubTransaction.RESULT_SUCCESS); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 491 | } else { |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 492 | Log.e(TAG, "Failed to send nanoapp message - nanoapp with handle " |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 493 | + nanoAppHandle + " does not exist."); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Arthur Ishiguro | ebb0e86 | 2017-11-17 14:55:32 -0800 | [diff] [blame] | 497 | return success ? 0 : -1; |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Handles a unicast or broadcast message from a nanoapp. |
| 502 | * |
| 503 | * @param contextHubId the ID of the hub the message came from |
| 504 | * @param message the message contents |
| 505 | */ |
| 506 | private void handleClientMessageCallback(int contextHubId, ContextHubMsg message) { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 507 | mClientManager.onMessageFromNanoApp(contextHubId, message); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * A helper function to handle a load response from the Context Hub for the old API. |
| 512 | * |
| 513 | * TODO(b/69270990): Remove this once the old APIs are obsolete. |
| 514 | */ |
| 515 | private void handleLoadResponseOldApi( |
| 516 | int contextHubId, int result, NanoAppBinary nanoAppBinary) { |
| 517 | if (nanoAppBinary == null) { |
| 518 | Log.e(TAG, "Nanoapp binary field was null for a load transaction"); |
| 519 | return; |
| 520 | } |
| 521 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 522 | byte[] data = new byte[5]; |
| 523 | data[0] = (byte) result; |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 524 | int nanoAppHandle = mNanoAppStateManager.getNanoAppHandle( |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 525 | contextHubId, nanoAppBinary.getNanoAppId()); |
Arthur Ishiguro | 2fc5a4b | 2017-12-11 15:15:44 -0800 | [diff] [blame] | 526 | ByteBuffer.wrap(data, 1, 4).order(ByteOrder.nativeOrder()).putInt(nanoAppHandle); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 527 | |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 528 | onMessageReceiptOldApi(MSG_LOAD_NANO_APP, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /** |
| 532 | * A helper function to handle an unload response from the Context Hub for the old API. |
| 533 | * |
| 534 | * TODO(b/69270990): Remove this once the old APIs are obsolete. |
| 535 | */ |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 536 | private void handleUnloadResponseOldApi(int contextHubId, int result) { |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 537 | byte[] data = new byte[1]; |
| 538 | data[0] = (byte) result; |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 539 | onMessageReceiptOldApi(MSG_UNLOAD_NANO_APP, contextHubId, OS_APP_INSTANCE, data); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Handles a transaction response from a Context Hub. |
| 544 | * |
| 545 | * @param contextHubId the ID of the hub the response came from |
| 546 | * @param transactionId the ID of the transaction |
| 547 | * @param result the result of the transaction reported by the hub |
| 548 | */ |
| 549 | private void handleTransactionResultCallback(int contextHubId, int transactionId, int result) { |
| 550 | mTransactionManager.onTransactionResponse(transactionId, result); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Handles an asynchronous event from a Context Hub. |
| 555 | * |
| 556 | * @param contextHubId the ID of the hub the response came from |
| 557 | * @param eventType the type of the event as defined in Context Hub HAL AsyncEventType |
| 558 | */ |
| 559 | private void handleHubEventCallback(int contextHubId, int eventType) { |
| 560 | if (eventType == AsyncEventType.RESTARTED) { |
| 561 | mTransactionManager.onHubReset(); |
| 562 | queryNanoAppsInternal(contextHubId); |
| 563 | |
Arthur Ishiguro | 6d47c54 | 2017-11-17 15:49:07 -0800 | [diff] [blame] | 564 | mClientManager.onHubReset(contextHubId); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 565 | } else { |
| 566 | Log.i(TAG, "Received unknown hub event (hub ID = " + contextHubId + ", type = " |
| 567 | + eventType + ")"); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Handles an asynchronous abort event of a nanoapp. |
| 573 | * |
| 574 | * @param contextHubId the ID of the hub that the nanoapp aborted in |
| 575 | * @param nanoAppId the ID of the aborted nanoapp |
| 576 | * @param abortCode the nanoapp-specific abort code |
| 577 | */ |
| 578 | private void handleAppAbortCallback(int contextHubId, long nanoAppId, int abortCode) { |
Arthur Ishiguro | 02ff50b | 2017-12-18 10:02:35 -0800 | [diff] [blame] | 579 | mClientManager.onNanoAppAborted(contextHubId, nanoAppId, abortCode); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Handles a query response from a Context Hub. |
| 584 | * |
| 585 | * @param contextHubId the ID of the hub of the response |
| 586 | * @param nanoAppInfoList the list of loaded nanoapps |
| 587 | */ |
| 588 | private void handleQueryAppsCallback(int contextHubId, List<HubAppInfo> nanoAppInfoList) { |
| 589 | List<NanoAppState> nanoAppStateList = |
| 590 | ContextHubServiceUtil.createNanoAppStateList(nanoAppInfoList); |
| 591 | |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 592 | mNanoAppStateManager.updateCache(contextHubId, nanoAppInfoList); |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 593 | mTransactionManager.onQueryResponse(nanoAppStateList); |
| 594 | } |
| 595 | |
| 596 | /** |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 597 | * @param contextHubId the hub ID to validate |
| 598 | * @return {@code true} if the ID represents that of an available hub, {@code false} otherwise |
| 599 | */ |
| 600 | private boolean isValidContextHubId(int contextHubId) { |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 601 | return mContextHubIdToInfoMap.containsKey(contextHubId); |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Creates and registers a client at the service for the specified Context Hub. |
| 606 | * |
| 607 | * @param clientCallback the client interface to register with the service |
| 608 | * @param contextHubId the ID of the hub this client is attached to |
| 609 | * @return the generated client interface, null if registration was unsuccessful |
| 610 | * |
| 611 | * @throws IllegalArgumentException if contextHubId is not a valid ID |
| 612 | * @throws IllegalStateException if max number of clients have already registered |
| 613 | * @throws NullPointerException if clientCallback is null |
| 614 | */ |
| 615 | @Override |
| 616 | public IContextHubClient createClient( |
| 617 | IContextHubClientCallback clientCallback, int contextHubId) throws RemoteException { |
| 618 | checkPermissions(); |
| 619 | if (!isValidContextHubId(contextHubId)) { |
| 620 | throw new IllegalArgumentException("Invalid context hub ID " + contextHubId); |
| 621 | } |
| 622 | if (clientCallback == null) { |
| 623 | throw new NullPointerException("Cannot register client with null callback"); |
| 624 | } |
| 625 | |
| 626 | return mClientManager.registerClient(clientCallback, contextHubId); |
| 627 | } |
| 628 | |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 629 | /** |
| 630 | * Loads a nanoapp binary at the specified Context hub. |
| 631 | * |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 632 | * @param contextHubId the ID of the hub to load the binary |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 633 | * @param transactionCallback the client-facing transaction callback interface |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 634 | * @param nanoAppBinary the binary to load |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 635 | * |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 636 | * @throws IllegalStateException if the transaction queue is full |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 637 | */ |
| 638 | @Override |
| 639 | public void loadNanoAppOnHub( |
| 640 | int contextHubId, IContextHubTransactionCallback transactionCallback, |
| 641 | NanoAppBinary nanoAppBinary) throws RemoteException { |
| 642 | checkPermissions(); |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 643 | if (!checkHalProxyAndContextHubId( |
| 644 | contextHubId, transactionCallback, ContextHubTransaction.TYPE_LOAD_NANOAPP)) { |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 645 | return; |
| 646 | } |
| 647 | if (nanoAppBinary == null) { |
| 648 | Log.e(TAG, "NanoAppBinary cannot be null in loadNanoAppOnHub"); |
| 649 | transactionCallback.onTransactionComplete( |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 650 | ContextHubTransaction.RESULT_FAILED_BAD_PARAMS); |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
| 654 | ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction( |
| 655 | contextHubId, nanoAppBinary, transactionCallback); |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 656 | mTransactionManager.addTransaction(transaction); |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Unloads a nanoapp from the specified Context Hub. |
| 661 | * |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 662 | * @param contextHubId the ID of the hub to unload the nanoapp |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 663 | * @param transactionCallback the client-facing transaction callback interface |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 664 | * @param nanoAppId the ID of the nanoapp to unload |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 665 | * |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 666 | * @throws IllegalStateException if the transaction queue is full |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 667 | */ |
| 668 | @Override |
| 669 | public void unloadNanoAppFromHub( |
| 670 | int contextHubId, IContextHubTransactionCallback transactionCallback, long nanoAppId) |
| 671 | throws RemoteException { |
| 672 | checkPermissions(); |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 673 | if (!checkHalProxyAndContextHubId( |
| 674 | contextHubId, transactionCallback, ContextHubTransaction.TYPE_UNLOAD_NANOAPP)) { |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 675 | return; |
| 676 | } |
| 677 | |
| 678 | ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction( |
| 679 | contextHubId, nanoAppId, transactionCallback); |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 680 | mTransactionManager.addTransaction(transaction); |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 681 | } |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 682 | |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 683 | /** |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 684 | * Enables a nanoapp at the specified Context Hub. |
| 685 | * |
| 686 | * @param contextHubId the ID of the hub to enable the nanoapp |
| 687 | * @param transactionCallback the client-facing transaction callback interface |
| 688 | * @param nanoAppId the ID of the nanoapp to enable |
| 689 | * |
| 690 | * @throws IllegalStateException if the transaction queue is full |
| 691 | */ |
| 692 | @Override |
| 693 | public void enableNanoApp( |
| 694 | int contextHubId, IContextHubTransactionCallback transactionCallback, long nanoAppId) |
| 695 | throws RemoteException { |
| 696 | checkPermissions(); |
| 697 | if (!checkHalProxyAndContextHubId( |
| 698 | contextHubId, transactionCallback, ContextHubTransaction.TYPE_ENABLE_NANOAPP)) { |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | ContextHubServiceTransaction transaction = mTransactionManager.createEnableTransaction( |
| 703 | contextHubId, nanoAppId, transactionCallback); |
| 704 | mTransactionManager.addTransaction(transaction); |
| 705 | } |
| 706 | |
| 707 | /** |
Arthur Ishiguro | 54e1a89 | 2017-12-12 15:09:31 -0800 | [diff] [blame] | 708 | * Disables a nanoapp at the specified Context Hub. |
| 709 | * |
| 710 | * @param contextHubId the ID of the hub to disable the nanoapp |
| 711 | * @param transactionCallback the client-facing transaction callback interface |
| 712 | * @param nanoAppId the ID of the nanoapp to disable |
| 713 | * |
| 714 | * @throws IllegalStateException if the transaction queue is full |
| 715 | */ |
| 716 | @Override |
| 717 | public void disableNanoApp( |
| 718 | int contextHubId, IContextHubTransactionCallback transactionCallback, long nanoAppId) |
| 719 | throws RemoteException { |
| 720 | checkPermissions(); |
| 721 | if (!checkHalProxyAndContextHubId( |
| 722 | contextHubId, transactionCallback, ContextHubTransaction.TYPE_DISABLE_NANOAPP)) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | ContextHubServiceTransaction transaction = mTransactionManager.createDisableTransaction( |
| 727 | contextHubId, nanoAppId, transactionCallback); |
| 728 | mTransactionManager.addTransaction(transaction); |
| 729 | } |
| 730 | |
| 731 | /** |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 732 | * Queries for a list of nanoapps from the specified Context hub. |
| 733 | * |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 734 | * @param contextHubId the ID of the hub to query |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 735 | * @param transactionCallback the client-facing transaction callback interface |
| 736 | * |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 737 | * @throws IllegalStateException if the transaction queue is full |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 738 | */ |
| 739 | @Override |
| 740 | public void queryNanoApps(int contextHubId, IContextHubTransactionCallback transactionCallback) |
| 741 | throws RemoteException { |
| 742 | checkPermissions(); |
| 743 | if (!checkHalProxyAndContextHubId( |
| 744 | contextHubId, transactionCallback, ContextHubTransaction.TYPE_QUERY_NANOAPPS)) { |
| 745 | return; |
| 746 | } |
| 747 | |
Arthur Ishiguro | 0ed545c | 2017-12-12 15:01:32 -0800 | [diff] [blame] | 748 | ContextHubServiceTransaction transaction = mTransactionManager.createQueryTransaction( |
| 749 | contextHubId, transactionCallback); |
Arthur Ishiguro | e60f91a | 2017-11-30 09:48:00 -0800 | [diff] [blame] | 750 | mTransactionManager.addTransaction(transaction); |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 753 | @Override |
| 754 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 755 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 756 | |
| 757 | pw.println("Dumping ContextHub Service"); |
| 758 | |
| 759 | pw.println(""); |
| 760 | // dump ContextHubInfo |
| 761 | pw.println("=================== CONTEXT HUBS ===================="); |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 762 | for (ContextHubInfo hubInfo : mContextHubIdToInfoMap.values()) { |
| 763 | pw.println(hubInfo); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 764 | } |
| 765 | pw.println(""); |
| 766 | pw.println("=================== NANOAPPS ===================="); |
| 767 | // Dump nanoAppHash |
Arthur Ishiguro | fb9e4c7 | 2017-11-21 15:33:21 -0800 | [diff] [blame] | 768 | for (NanoAppInstanceInfo info : mNanoAppStateManager.getNanoAppInstanceInfoCollection()) { |
| 769 | pw.println(info); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // dump eventLog |
| 773 | } |
| 774 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 775 | private void checkPermissions() { |
Arthur Ishiguro | 4e39aa1 | 2017-11-14 14:59:08 -0800 | [diff] [blame] | 776 | ContextHubServiceUtil.checkPermissions(mContext); |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 777 | } |
Ashutosh Joshi | 2c697fb | 2016-04-01 20:48:13 +0000 | [diff] [blame] | 778 | |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 779 | private int onMessageReceiptOldApi( |
| 780 | int msgType, int contextHubHandle, int appInstance, byte[] data) { |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 781 | if (data == null) { |
| 782 | return -1; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 783 | } |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 784 | |
Arthur Ishiguro | 7a23a96 | 2017-11-01 10:52:28 -0700 | [diff] [blame] | 785 | int msgVersion = 0; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 786 | int callbacksCount = mCallbacksList.beginBroadcast(); |
Arthur Ishiguro | b4f5987 | 2018-07-31 16:38:08 -0700 | [diff] [blame^] | 787 | if (DEBUG_LOG_ENABLED) { |
| 788 | Log.v(TAG, "Sending message " + msgType + " version " + msgVersion + " from hubHandle " |
| 789 | + contextHubHandle + ", appInstance " + appInstance + ", callBackCount " |
| 790 | + callbacksCount); |
| 791 | } |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 792 | |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 793 | if (callbacksCount < 1) { |
Arthur Ishiguro | b4f5987 | 2018-07-31 16:38:08 -0700 | [diff] [blame^] | 794 | if (DEBUG_LOG_ENABLED) { |
| 795 | Log.v(TAG, "No message callbacks registered."); |
| 796 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 797 | return 0; |
| 798 | } |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 799 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 800 | ContextHubMessage msg = new ContextHubMessage(msgType, msgVersion, data); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 801 | for (int i = 0; i < callbacksCount; ++i) { |
| 802 | IContextHubCallback callback = mCallbacksList.getBroadcastItem(i); |
| 803 | try { |
Arthur Ishiguro | ab7113d | 2017-12-15 14:32:51 -0800 | [diff] [blame] | 804 | callback.onMessageReceipt(contextHubHandle, appInstance, msg); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 805 | } catch (RemoteException e) { |
| 806 | Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ")."); |
| 807 | continue; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 810 | mCallbacksList.finishBroadcast(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 814 | /** |
| 815 | * Validates the HAL proxy state and context hub ID to see if we can start the transaction. |
| 816 | * |
| 817 | * @param contextHubId the ID of the hub to start the transaction |
| 818 | * @param callback the client transaction callback interface |
| 819 | * @param transactionType the type of the transaction |
| 820 | * |
| 821 | * @return {@code true} if mContextHubProxy and contextHubId is valid, {@code false} otherwise |
| 822 | */ |
| 823 | private boolean checkHalProxyAndContextHubId( |
| 824 | int contextHubId, IContextHubTransactionCallback callback, |
| 825 | @ContextHubTransaction.Type int transactionType) { |
| 826 | if (mContextHubProxy == null) { |
| 827 | try { |
| 828 | callback.onTransactionComplete( |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 829 | ContextHubTransaction.RESULT_FAILED_HAL_UNAVAILABLE); |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 830 | } catch (RemoteException e) { |
| 831 | Log.e(TAG, "RemoteException while calling onTransactionComplete", e); |
| 832 | } |
| 833 | return false; |
| 834 | } |
| 835 | if (!isValidContextHubId(contextHubId)) { |
| 836 | Log.e(TAG, "Cannot start " |
| 837 | + ContextHubTransaction.typeToString(transactionType, false /* upperCase */) |
| 838 | + " transaction for invalid hub ID " + contextHubId); |
| 839 | try { |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 840 | callback.onTransactionComplete(ContextHubTransaction.RESULT_FAILED_BAD_PARAMS); |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 841 | } catch (RemoteException e) { |
| 842 | Log.e(TAG, "RemoteException while calling onTransactionComplete", e); |
| 843 | } |
| 844 | return false; |
| 845 | } |
| 846 | |
| 847 | return true; |
| 848 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 849 | } |