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 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Ashutosh Joshi | 420e45e | 2016-12-20 16:34:41 -0800 | [diff] [blame] | 22 | import android.hardware.location.ContextHubInfo; |
| 23 | import android.hardware.location.ContextHubManager; |
| 24 | import android.hardware.location.ContextHubMessage; |
| 25 | import android.hardware.location.IContextHubService; |
| 26 | import android.hardware.location.IContextHubCallback; |
| 27 | import android.hardware.location.NanoAppFilter; |
| 28 | import android.hardware.location.NanoApp; |
| 29 | import android.hardware.location.NanoAppInstanceInfo; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 30 | import android.os.RemoteCallbackList; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.util.Log; |
| 33 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame^] | 34 | import com.android.internal.util.DumpUtils; |
| 35 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 36 | import java.io.FileDescriptor; |
| 37 | import java.io.PrintWriter; |
Ashutosh Joshi | 19753cc | 2016-11-23 13:56:39 -0800 | [diff] [blame] | 38 | import java.nio.ByteBuffer; |
| 39 | import java.nio.ByteOrder; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 40 | import java.util.ArrayList; |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 41 | import java.util.concurrent.ConcurrentHashMap; |
Ashutosh Joshi | 19753cc | 2016-11-23 13:56:39 -0800 | [diff] [blame] | 42 | import java.util.HashMap; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * @hide |
| 46 | */ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 47 | public class ContextHubService extends IContextHubService.Stub { |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 48 | private static final String TAG = "ContextHubService"; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 49 | private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE; |
| 50 | private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '" |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 51 | + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 52 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 53 | public static final int ANY_HUB = -1; |
Ashutosh Joshi | cafdee9 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 54 | public static final int MSG_LOAD_NANO_APP = 3; |
| 55 | public static final int MSG_UNLOAD_NANO_APP = 4; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 56 | |
| 57 | private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown"; |
| 58 | private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN; |
| 59 | private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN; |
| 60 | private static final int PRE_LOADED_APP_MEM_REQ = 0; |
| 61 | |
| 62 | private static final int MSG_HEADER_SIZE = 4; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 63 | private static final int HEADER_FIELD_MSG_TYPE = 0; |
| 64 | private static final int HEADER_FIELD_MSG_VERSION = 1; |
| 65 | private static final int HEADER_FIELD_HUB_HANDLE = 2; |
| 66 | private static final int HEADER_FIELD_APP_INSTANCE = 3; |
| 67 | |
| 68 | private static final int HEADER_FIELD_LOAD_APP_ID_LO = MSG_HEADER_SIZE; |
| 69 | private static final int HEADER_FIELD_LOAD_APP_ID_HI = MSG_HEADER_SIZE + 1; |
| 70 | private static final int MSG_LOAD_APP_HEADER_SIZE = MSG_HEADER_SIZE + 2; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 71 | |
| 72 | private static final int OS_APP_INSTANCE = -1; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 73 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 74 | private final Context mContext; |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 75 | private final ConcurrentHashMap<Integer, NanoAppInstanceInfo> mNanoAppHash = |
| 76 | new ConcurrentHashMap<>(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 77 | private final ContextHubInfo[] mContextHubInfo; |
| 78 | private final RemoteCallbackList<IContextHubCallback> mCallbacksList = |
| 79 | new RemoteCallbackList<>(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 80 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 81 | private native int nativeSendMessage(int[] header, byte[] data); |
| 82 | private native ContextHubInfo[] nativeInitialize(); |
| 83 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 84 | public ContextHubService(Context context) { |
| 85 | mContext = context; |
| 86 | mContextHubInfo = nativeInitialize(); |
| 87 | |
| 88 | for (int i = 0; i < mContextHubInfo.length; i++) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 89 | Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId() |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 90 | + ", name: " + mContextHubInfo[i].getName()); |
| 91 | } |
| 92 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 93 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 94 | @Override |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 95 | public int registerCallback(IContextHubCallback callback) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 96 | checkPermissions(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 97 | mCallbacksList.register(callback); |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 98 | Log.d(TAG, "Added callback, total callbacks " + |
| 99 | mCallbacksList.getRegisteredCallbackCount()); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int[] getContextHubHandles() throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 105 | checkPermissions(); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 106 | int[] returnArray = new int[mContextHubInfo.length]; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 107 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 108 | Log.d(TAG, "System supports " + returnArray.length + " hubs"); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 109 | for (int i = 0; i < returnArray.length; ++i) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 110 | returnArray[i] = i; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 111 | Log.d(TAG, String.format("Hub %s is mapped to %d", |
| 112 | mContextHubInfo[i].getName(), returnArray[i])); |
| 113 | } |
| 114 | |
| 115 | return returnArray; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 120 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 121 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 122 | Log.e(TAG, "Invalid context hub handle " + contextHubHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 123 | return null; // null means fail |
| 124 | } |
| 125 | |
| 126 | return mContextHubInfo[contextHubHandle]; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 131 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 132 | |
| 133 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 134 | Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle); |
| 135 | return -1; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 136 | } |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 137 | if (app == null) { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 138 | Log.e(TAG, "Invalid null app"); |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 139 | return -1; |
| 140 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 141 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 142 | int[] msgHeader = new int[MSG_LOAD_APP_HEADER_SIZE]; |
| 143 | msgHeader[HEADER_FIELD_HUB_HANDLE] = contextHubHandle; |
| 144 | msgHeader[HEADER_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; |
| 145 | msgHeader[HEADER_FIELD_MSG_VERSION] = 0; |
| 146 | msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_LOAD_NANO_APP; |
| 147 | |
| 148 | long appId = app.getAppId(); |
| 149 | |
| 150 | msgHeader[HEADER_FIELD_LOAD_APP_ID_LO] = (int)(appId & 0xFFFFFFFF); |
| 151 | msgHeader[HEADER_FIELD_LOAD_APP_ID_HI] = (int)((appId >> 32) & 0xFFFFFFFF); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 152 | |
Ashutosh Joshi | 1186440 | 2016-07-15 13:46:22 -0700 | [diff] [blame] | 153 | int errVal = nativeSendMessage(msgHeader, app.getAppBinary()); |
| 154 | if (errVal != 0) { |
| 155 | Log.e(TAG, "Send Message returns error" + contextHubHandle); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 156 | return -1; |
| 157 | } |
Ashutosh Joshi | 1186440 | 2016-07-15 13:46:22 -0700 | [diff] [blame] | 158 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 159 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 160 | return 0; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 161 | } |
| 162 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { |
| 165 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 166 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); |
| 167 | if (info == null) { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 168 | Log.e(TAG, "Cannot find app with handle " + nanoAppInstanceHandle); |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 169 | return -1; //means failed |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Call Native interface here |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 173 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 174 | msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB; |
| 175 | msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppInstanceHandle; |
| 176 | msgHeader[HEADER_FIELD_MSG_VERSION] = 0; |
| 177 | msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_UNLOAD_NANO_APP; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 178 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 179 | byte msg[] = new byte[0]; |
| 180 | |
| 181 | if (nativeSendMessage(msgHeader, msg) != 0) { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 182 | Log.e(TAG, "native send message fails"); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 187 | return 0; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 188 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 189 | |
| 190 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 191 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 192 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 193 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 194 | // This assumes that all the nanoAppInfo is current. This is reasonable |
| 195 | // for the use cases for tightly controlled nanoApps. |
| 196 | if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 197 | return mNanoAppHash.get(nanoAppInstanceHandle); |
| 198 | } else { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 199 | Log.e(TAG, "Could not find nanoApp with handle " + nanoAppInstanceHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 200 | return null; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 205 | public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 206 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 207 | ArrayList<Integer> foundInstances = new ArrayList<Integer>(); |
| 208 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 209 | for (Integer nanoAppInstance: mNanoAppHash.keySet()) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 210 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); |
| 211 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 212 | if (filter.testMatch(info)) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 213 | foundInstances.add(nanoAppInstance); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | int[] retArray = new int[foundInstances.size()]; |
| 218 | for (int i = 0; i < foundInstances.size(); i++) { |
| 219 | retArray[i] = foundInstances.get(i).intValue(); |
| 220 | } |
| 221 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 222 | Log.w(TAG, "Found " + retArray.length + " apps on hub handle " + hubHandle); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 223 | return retArray; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 227 | public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 228 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 229 | checkPermissions(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 230 | |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 231 | if (msg == null || msg.getData() == null) { |
| 232 | Log.w(TAG, "null ptr"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 236 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 237 | msgHeader[HEADER_FIELD_HUB_HANDLE] = hubHandle; |
| 238 | msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppHandle; |
| 239 | msgHeader[HEADER_FIELD_MSG_VERSION] = msg.getVersion(); |
| 240 | msgHeader[HEADER_FIELD_MSG_TYPE] = msg.getMsgType(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 241 | |
| 242 | return nativeSendMessage(msgHeader, msg.getData()); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 243 | } |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 244 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 245 | @Override |
| 246 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame^] | 247 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 248 | |
| 249 | pw.println("Dumping ContextHub Service"); |
| 250 | |
| 251 | pw.println(""); |
| 252 | // dump ContextHubInfo |
| 253 | pw.println("=================== CONTEXT HUBS ===================="); |
| 254 | for (int i = 0; i < mContextHubInfo.length; i++) { |
| 255 | pw.println("Handle " + i + " : " + mContextHubInfo[i].toString()); |
| 256 | } |
| 257 | pw.println(""); |
| 258 | pw.println("=================== NANOAPPS ===================="); |
| 259 | // Dump nanoAppHash |
| 260 | for (Integer nanoAppInstance: mNanoAppHash.keySet()) { |
| 261 | pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString()); |
| 262 | } |
| 263 | |
| 264 | // dump eventLog |
| 265 | } |
| 266 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 267 | private void checkPermissions() { |
| 268 | mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE); |
| 269 | } |
Ashutosh Joshi | 2c697fb | 2016-04-01 20:48:13 +0000 | [diff] [blame] | 270 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 271 | private int onMessageReceipt(int[] header, byte[] data) { |
| 272 | if (header == null || data == null || header.length < MSG_HEADER_SIZE) { |
| 273 | return -1; |
| 274 | } |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 275 | |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 276 | int callbacksCount = mCallbacksList.beginBroadcast(); |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 277 | int msgType = header[HEADER_FIELD_MSG_TYPE]; |
| 278 | int msgVersion = header[HEADER_FIELD_MSG_VERSION]; |
| 279 | int hubHandle = header[HEADER_FIELD_HUB_HANDLE]; |
| 280 | int appInstance = header[HEADER_FIELD_APP_INSTANCE]; |
| 281 | |
| 282 | Log.d(TAG, "Sending message " + msgType + " version " + msgVersion + " from hubHandle " + |
| 283 | hubHandle + ", appInstance " + appInstance + ", callBackCount " + callbacksCount); |
| 284 | |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 285 | if (callbacksCount < 1) { |
| 286 | Log.v(TAG, "No message callbacks registered."); |
| 287 | return 0; |
| 288 | } |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 289 | |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 290 | ContextHubMessage msg = new ContextHubMessage(msgType, msgVersion, data); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 291 | for (int i = 0; i < callbacksCount; ++i) { |
| 292 | IContextHubCallback callback = mCallbacksList.getBroadcastItem(i); |
| 293 | try { |
Ashutosh Joshi | 1d94181 | 2017-03-09 15:21:24 -0800 | [diff] [blame] | 294 | callback.onMessageReceipt(hubHandle, appInstance, msg); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 295 | } catch (RemoteException e) { |
| 296 | Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ")."); |
| 297 | continue; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame] | 300 | mCallbacksList.finishBroadcast(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) { |
| 305 | // App Id encodes vendor & version |
| 306 | NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo(); |
| 307 | |
| 308 | appInfo.setAppId(appId); |
| 309 | appInfo.setAppVersion(appVersion); |
| 310 | appInfo.setName(PRE_LOADED_APP_NAME); |
| 311 | appInfo.setContexthubId(hubHandle); |
| 312 | appInfo.setHandle(appInstanceHandle); |
| 313 | appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER); |
| 314 | appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 315 | appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 316 | appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 317 | |
Greg Kaiser | fe6d4f5 | 2016-08-19 10:24:07 -0700 | [diff] [blame] | 318 | String action; |
| 319 | if (mNanoAppHash.containsKey(appInstanceHandle)) { |
| 320 | action = "Updated"; |
| 321 | } else { |
| 322 | action = "Added"; |
| 323 | } |
| 324 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 325 | mNanoAppHash.put(appInstanceHandle, appInfo); |
Greg Kaiser | fe6d4f5 | 2016-08-19 10:24:07 -0700 | [diff] [blame] | 326 | Log.d(TAG, action + " app instance " + appInstanceHandle + " with id " |
| 327 | + appId + " version " + appVersion); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |
Brian Duddie | c3d8a52 | 2016-06-14 15:12:26 -0700 | [diff] [blame] | 331 | |
Ashutosh Joshi | 54787a5 | 2016-04-27 11:19:16 -0700 | [diff] [blame] | 332 | private int deleteAppInstance(int appInstanceHandle) { |
| 333 | if (mNanoAppHash.remove(appInstanceHandle) == null) { |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 339 | } |