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 | |
| 17 | package android.hardware.location; |
| 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; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame^] | 22 | import android.os.RemoteCallbackList; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 23 | import android.os.RemoteException; |
| 24 | import android.util.Log; |
| 25 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 26 | import java.io.FileDescriptor; |
| 27 | import java.io.PrintWriter; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 28 | import java.util.ArrayList; |
| 29 | import java.util.HashMap; |
| 30 | |
| 31 | /** |
| 32 | * @hide |
| 33 | */ |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 34 | public class ContextHubService extends IContextHubService.Stub { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 35 | public static final String CONTEXTHUB_SERVICE = "contexthub_service"; |
| 36 | |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 37 | private static final String TAG = "ContextHubService"; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 38 | private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE; |
| 39 | private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '" |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 40 | + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware"; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 41 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 42 | |
| 43 | public static final int ANY_HUB = -1; |
Ashutosh Joshi | cafdee9 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 44 | public static final int MSG_LOAD_NANO_APP = 3; |
| 45 | public static final int MSG_UNLOAD_NANO_APP = 4; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 46 | |
| 47 | private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown"; |
| 48 | private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN; |
| 49 | private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN; |
| 50 | private static final int PRE_LOADED_APP_MEM_REQ = 0; |
| 51 | |
| 52 | private static final int MSG_HEADER_SIZE = 4; |
| 53 | private static final int MSG_FIELD_TYPE = 0; |
| 54 | private static final int MSG_FIELD_VERSION = 1; |
| 55 | private static final int MSG_FIELD_HUB_HANDLE = 2; |
| 56 | private static final int MSG_FIELD_APP_INSTANCE = 3; |
| 57 | |
| 58 | private static final int OS_APP_INSTANCE = -1; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 59 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 60 | private final Context mContext; |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame^] | 61 | private final HashMap<Integer, NanoAppInstanceInfo> mNanoAppHash = new HashMap<>(); |
| 62 | private final ContextHubInfo[] mContextHubInfo; |
| 63 | private final RemoteCallbackList<IContextHubCallback> mCallbacksList = |
| 64 | new RemoteCallbackList<>(); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 65 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 66 | private native int nativeSendMessage(int[] header, byte[] data); |
| 67 | private native ContextHubInfo[] nativeInitialize(); |
| 68 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 69 | public ContextHubService(Context context) { |
| 70 | mContext = context; |
| 71 | mContextHubInfo = nativeInitialize(); |
| 72 | |
| 73 | for (int i = 0; i < mContextHubInfo.length; i++) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 74 | Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId() |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 75 | + ", name: " + mContextHubInfo[i].getName()); |
| 76 | } |
| 77 | } |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 78 | |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 79 | @Override |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 80 | public int registerCallback(IContextHubCallback callback) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 81 | checkPermissions(); |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame^] | 82 | mCallbacksList.register(callback); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int[] getContextHubHandles() throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 88 | checkPermissions(); |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 89 | int[] returnArray = new int[mContextHubInfo.length]; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 90 | |
| 91 | for (int i = 0; i < returnArray.length; ++i) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 92 | returnArray[i] = i; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 93 | Log.d(TAG, String.format("Hub %s is mapped to %d", |
| 94 | mContextHubInfo[i].getName(), returnArray[i])); |
| 95 | } |
| 96 | |
| 97 | return returnArray; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 102 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 103 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
| 104 | return null; // null means fail |
| 105 | } |
| 106 | |
| 107 | return mContextHubInfo[contextHubHandle]; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 112 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 113 | |
| 114 | if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) { |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 115 | Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle); |
| 116 | return -1; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 119 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
| 120 | msgHeader[MSG_FIELD_HUB_HANDLE] = contextHubHandle; |
| 121 | msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; |
| 122 | msgHeader[MSG_FIELD_VERSION] = 0; |
| 123 | msgHeader[MSG_FIELD_TYPE] = MSG_LOAD_NANO_APP; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 124 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 125 | if (nativeSendMessage(msgHeader, app.getAppBinary()) != 0) { |
| 126 | return -1; |
| 127 | } |
| 128 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 129 | return 0; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 130 | } |
| 131 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException { |
| 134 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 135 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle); |
| 136 | if (info == null) { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 137 | return -1; //means failed |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Call Native interface here |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 141 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
| 142 | msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB; |
| 143 | msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE; |
| 144 | msgHeader[MSG_FIELD_VERSION] = 0; |
| 145 | msgHeader[MSG_FIELD_TYPE] = MSG_UNLOAD_NANO_APP; |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 146 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 147 | if (nativeSendMessage(msgHeader, null) != 0) { |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app |
| 152 | return 0; |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 153 | } |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 154 | |
| 155 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 156 | public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle) |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 157 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 158 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 159 | // This assumes that all the nanoAppInfo is current. This is reasonable |
| 160 | // for the use cases for tightly controlled nanoApps. |
| 161 | if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) { |
| 162 | return mNanoAppHash.get(nanoAppInstanceHandle); |
| 163 | } else { |
| 164 | return null; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | @Override |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 169 | public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 170 | checkPermissions(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 171 | ArrayList<Integer> foundInstances = new ArrayList<Integer>(); |
| 172 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 173 | for (Integer nanoAppInstance: mNanoAppHash.keySet()) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 174 | NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance); |
| 175 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 176 | if (filter.testMatch(info)) { |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 177 | foundInstances.add(nanoAppInstance); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | int[] retArray = new int[foundInstances.size()]; |
| 182 | for (int i = 0; i < foundInstances.size(); i++) { |
| 183 | retArray[i] = foundInstances.get(i).intValue(); |
| 184 | } |
| 185 | |
| 186 | return retArray; |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 190 | public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg) |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 191 | throws RemoteException { |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 192 | checkPermissions(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 193 | |
| 194 | int[] msgHeader = new int[MSG_HEADER_SIZE]; |
| 195 | msgHeader[MSG_FIELD_HUB_HANDLE] = hubHandle; |
| 196 | msgHeader[MSG_FIELD_APP_INSTANCE] = nanoAppHandle; |
| 197 | msgHeader[MSG_FIELD_VERSION] = msg.getVersion(); |
| 198 | msgHeader[MSG_FIELD_TYPE] = msg.getMsgType(); |
Peng Xu | 9ff7d22 | 2016-02-11 13:02:05 -0800 | [diff] [blame] | 199 | |
| 200 | return nativeSendMessage(msgHeader, msg.getData()); |
Ashutosh Joshi | 1d1ac54 | 2016-01-18 17:19:27 -0800 | [diff] [blame] | 201 | } |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 202 | |
Ashutosh Joshi | 6239cc6 | 2016-04-04 16:19:29 -0700 | [diff] [blame] | 203 | @Override |
| 204 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 205 | if (mContext.checkCallingOrSelfPermission("android.permission.DUMP") |
| 206 | != PackageManager.PERMISSION_GRANTED) { |
| 207 | pw.println("Permission Denial: can't dump contexthub_service"); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | pw.println("Dumping ContextHub Service"); |
| 212 | |
| 213 | pw.println(""); |
| 214 | // dump ContextHubInfo |
| 215 | pw.println("=================== CONTEXT HUBS ===================="); |
| 216 | for (int i = 0; i < mContextHubInfo.length; i++) { |
| 217 | pw.println("Handle " + i + " : " + mContextHubInfo[i].toString()); |
| 218 | } |
| 219 | pw.println(""); |
| 220 | pw.println("=================== NANOAPPS ===================="); |
| 221 | // Dump nanoAppHash |
| 222 | for (Integer nanoAppInstance: mNanoAppHash.keySet()) { |
| 223 | pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString()); |
| 224 | } |
| 225 | |
| 226 | // dump eventLog |
| 227 | } |
| 228 | |
destradaa | 8bad3fe | 2016-03-15 12:33:40 -0700 | [diff] [blame] | 229 | private void checkPermissions() { |
| 230 | mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE); |
| 231 | } |
Ashutosh Joshi | 2c697fb | 2016-04-01 20:48:13 +0000 | [diff] [blame] | 232 | |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 233 | private int onMessageReceipt(int[] header, byte[] data) { |
| 234 | if (header == null || data == null || header.length < MSG_HEADER_SIZE) { |
| 235 | return -1; |
| 236 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame^] | 237 | int callbacksCount = mCallbacksList.beginBroadcast(); |
| 238 | if (callbacksCount < 1) { |
| 239 | Log.v(TAG, "No message callbacks registered."); |
| 240 | return 0; |
| 241 | } |
| 242 | ContextHubMessage message = |
| 243 | new ContextHubMessage(header[MSG_FIELD_TYPE], header[MSG_FIELD_VERSION], data); |
| 244 | for (int i = 0; i < callbacksCount; ++i) { |
| 245 | IContextHubCallback callback = mCallbacksList.getBroadcastItem(i); |
| 246 | try { |
| 247 | callback.onMessageReceipt( |
| 248 | header[MSG_FIELD_HUB_HANDLE], |
| 249 | header[MSG_FIELD_APP_INSTANCE], |
| 250 | message); |
| 251 | } catch (RemoteException e) { |
| 252 | Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ")."); |
| 253 | continue; |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
destradaa | 78cebca | 2016-04-14 18:40:14 -0700 | [diff] [blame^] | 256 | mCallbacksList.finishBroadcast(); |
Ashutosh Joshi | b741e3b | 2016-03-29 09:19:56 -0700 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) { |
| 261 | // App Id encodes vendor & version |
| 262 | NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo(); |
| 263 | |
| 264 | appInfo.setAppId(appId); |
| 265 | appInfo.setAppVersion(appVersion); |
| 266 | appInfo.setName(PRE_LOADED_APP_NAME); |
| 267 | appInfo.setContexthubId(hubHandle); |
| 268 | appInfo.setHandle(appInstanceHandle); |
| 269 | appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER); |
| 270 | appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 271 | appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 272 | appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ); |
| 273 | |
| 274 | mNanoAppHash.put(appInstanceHandle, appInfo); |
| 275 | Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId |
| 276 | + " version " + appVersion); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | } |