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