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