blob: 5e9f3550635fcb59b0ca3c381261bed4da3ad930 [file] [log] [blame]
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -08001/*
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 Joshi420e45e2016-12-20 16:34:41 -080017package com.android.server.location;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080018
destradaa8bad3fe2016-03-15 12:33:40 -070019import android.Manifest;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080020import android.content.Context;
Ashutosh Joshi6239cc62016-04-04 16:19:29 -070021import android.content.pm.PackageManager;
Ashutosh Joshi420e45e2016-12-20 16:34:41 -080022import android.hardware.location.ContextHubInfo;
23import android.hardware.location.ContextHubManager;
24import android.hardware.location.ContextHubMessage;
25import android.hardware.location.IContextHubService;
26import android.hardware.location.IContextHubCallback;
27import android.hardware.location.NanoAppFilter;
28import android.hardware.location.NanoApp;
29import android.hardware.location.NanoAppInstanceInfo;
destradaa78cebca2016-04-14 18:40:14 -070030import android.os.RemoteCallbackList;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080031import android.os.RemoteException;
32import android.util.Log;
33
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060034import com.android.internal.util.DumpUtils;
35
Ashutosh Joshi6239cc62016-04-04 16:19:29 -070036import java.io.FileDescriptor;
37import java.io.PrintWriter;
Ashutosh Joshi19753cc2016-11-23 13:56:39 -080038import java.nio.ByteBuffer;
39import java.nio.ByteOrder;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080040import java.util.ArrayList;
Brian Duddiec3d8a522016-06-14 15:12:26 -070041import java.util.concurrent.ConcurrentHashMap;
Ashutosh Joshi19753cc2016-11-23 13:56:39 -080042import java.util.HashMap;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080043
44/**
45 * @hide
46 */
Peng Xu9ff7d222016-02-11 13:02:05 -080047public class ContextHubService extends IContextHubService.Stub {
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080048 private static final String TAG = "ContextHubService";
destradaa8bad3fe2016-03-15 12:33:40 -070049 private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE;
50 private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '"
Ashutosh Joshi6239cc62016-04-04 16:19:29 -070051 + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware";
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080052
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070053 public static final int ANY_HUB = -1;
Ashutosh Joshicafdee92016-04-04 16:19:29 -070054 public static final int MSG_LOAD_NANO_APP = 3;
55 public static final int MSG_UNLOAD_NANO_APP = 4;
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070056
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 Joshi54787a52016-04-27 11:19:16 -070063 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 Joshib741e3b2016-03-29 09:19:56 -070071
72 private static final int OS_APP_INSTANCE = -1;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080073
Peng Xu9ff7d222016-02-11 13:02:05 -080074 private final Context mContext;
Brian Duddiec3d8a522016-06-14 15:12:26 -070075 private final ConcurrentHashMap<Integer, NanoAppInstanceInfo> mNanoAppHash =
76 new ConcurrentHashMap<>();
destradaa78cebca2016-04-14 18:40:14 -070077 private final ContextHubInfo[] mContextHubInfo;
78 private final RemoteCallbackList<IContextHubCallback> mCallbacksList =
79 new RemoteCallbackList<>();
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080080
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070081 private native int nativeSendMessage(int[] header, byte[] data);
82 private native ContextHubInfo[] nativeInitialize();
83
Peng Xu9ff7d222016-02-11 13:02:05 -080084 public ContextHubService(Context context) {
85 mContext = context;
86 mContextHubInfo = nativeInitialize();
87
88 for (int i = 0; i < mContextHubInfo.length; i++) {
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070089 Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
Peng Xu9ff7d222016-02-11 13:02:05 -080090 + ", name: " + mContextHubInfo[i].getName());
91 }
92 }
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080093
Peng Xu9ff7d222016-02-11 13:02:05 -080094 @Override
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070095 public int registerCallback(IContextHubCallback callback) throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -070096 checkPermissions();
destradaa78cebca2016-04-14 18:40:14 -070097 mCallbacksList.register(callback);
Ashutosh Joshi1d941812017-03-09 15:21:24 -080098 Log.d(TAG, "Added callback, total callbacks " +
99 mCallbacksList.getRegisteredCallbackCount());
Peng Xu9ff7d222016-02-11 13:02:05 -0800100 return 0;
101 }
102
103 @Override
104 public int[] getContextHubHandles() throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700105 checkPermissions();
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700106 int[] returnArray = new int[mContextHubInfo.length];
Peng Xu9ff7d222016-02-11 13:02:05 -0800107
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800108 Log.d(TAG, "System supports " + returnArray.length + " hubs");
Peng Xu9ff7d222016-02-11 13:02:05 -0800109 for (int i = 0; i < returnArray.length; ++i) {
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700110 returnArray[i] = i;
Peng Xu9ff7d222016-02-11 13:02:05 -0800111 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 {
destradaa8bad3fe2016-03-15 12:33:40 -0700120 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800121 if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) {
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800122 Log.e(TAG, "Invalid context hub handle " + contextHubHandle);
Peng Xu9ff7d222016-02-11 13:02:05 -0800123 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 {
destradaa8bad3fe2016-03-15 12:33:40 -0700131 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800132
133 if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) {
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700134 Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle);
135 return -1;
Peng Xu9ff7d222016-02-11 13:02:05 -0800136 }
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600137 if (app == null) {
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800138 Log.e(TAG, "Invalid null app");
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600139 return -1;
140 }
Peng Xu9ff7d222016-02-11 13:02:05 -0800141
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700142 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 Xu9ff7d222016-02-11 13:02:05 -0800152
Ashutosh Joshi11864402016-07-15 13:46:22 -0700153 int errVal = nativeSendMessage(msgHeader, app.getAppBinary());
154 if (errVal != 0) {
155 Log.e(TAG, "Send Message returns error" + contextHubHandle);
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700156 return -1;
157 }
Ashutosh Joshi11864402016-07-15 13:46:22 -0700158
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700159 // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
160 return 0;
Peng Xu9ff7d222016-02-11 13:02:05 -0800161 }
162
destradaa8bad3fe2016-03-15 12:33:40 -0700163 @Override
164 public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException {
165 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800166 NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle);
167 if (info == null) {
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800168 Log.e(TAG, "Cannot find app with handle " + nanoAppInstanceHandle);
destradaa8bad3fe2016-03-15 12:33:40 -0700169 return -1; //means failed
Peng Xu9ff7d222016-02-11 13:02:05 -0800170 }
171
172 // Call Native interface here
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700173 int[] msgHeader = new int[MSG_HEADER_SIZE];
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700174 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 Xu9ff7d222016-02-11 13:02:05 -0800178
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700179 byte msg[] = new byte[0];
180
181 if (nativeSendMessage(msgHeader, msg) != 0) {
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800182 Log.e(TAG, "native send message fails");
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700183 return -1;
184 }
185
186 // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
187 return 0;
destradaa8bad3fe2016-03-15 12:33:40 -0700188 }
Peng Xu9ff7d222016-02-11 13:02:05 -0800189
190 @Override
destradaa8bad3fe2016-03-15 12:33:40 -0700191 public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle)
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700192 throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700193 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800194 // 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 Joshi1d941812017-03-09 15:21:24 -0800199 Log.e(TAG, "Could not find nanoApp with handle " + nanoAppInstanceHandle);
Peng Xu9ff7d222016-02-11 13:02:05 -0800200 return null;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800201 }
202 }
203
204 @Override
Peng Xu9ff7d222016-02-11 13:02:05 -0800205 public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700206 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800207 ArrayList<Integer> foundInstances = new ArrayList<Integer>();
208
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700209 for (Integer nanoAppInstance: mNanoAppHash.keySet()) {
Peng Xu9ff7d222016-02-11 13:02:05 -0800210 NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance);
211
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700212 if (filter.testMatch(info)) {
Peng Xu9ff7d222016-02-11 13:02:05 -0800213 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 Joshi1d941812017-03-09 15:21:24 -0800222 Log.w(TAG, "Found " + retArray.length + " apps on hub handle " + hubHandle);
Peng Xu9ff7d222016-02-11 13:02:05 -0800223 return retArray;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800224 }
225
226 @Override
destradaa8bad3fe2016-03-15 12:33:40 -0700227 public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg)
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700228 throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700229 checkPermissions();
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700230
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600231 if (msg == null || msg.getData() == null) {
232 Log.w(TAG, "null ptr");
233 return -1;
234 }
235
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700236 int[] msgHeader = new int[MSG_HEADER_SIZE];
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700237 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 Xu9ff7d222016-02-11 13:02:05 -0800241
242 return nativeSendMessage(msgHeader, msg.getData());
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800243 }
destradaa8bad3fe2016-03-15 12:33:40 -0700244
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700245 @Override
246 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600247 if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700248
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
destradaa8bad3fe2016-03-15 12:33:40 -0700267 private void checkPermissions() {
268 mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE);
269 }
Ashutosh Joshi2c697fb2016-04-01 20:48:13 +0000270
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700271 private int onMessageReceipt(int[] header, byte[] data) {
272 if (header == null || data == null || header.length < MSG_HEADER_SIZE) {
273 return -1;
274 }
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800275
destradaa78cebca2016-04-14 18:40:14 -0700276 int callbacksCount = mCallbacksList.beginBroadcast();
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800277 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
destradaa78cebca2016-04-14 18:40:14 -0700285 if (callbacksCount < 1) {
286 Log.v(TAG, "No message callbacks registered.");
287 return 0;
288 }
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700289
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800290 ContextHubMessage msg = new ContextHubMessage(msgType, msgVersion, data);
destradaa78cebca2016-04-14 18:40:14 -0700291 for (int i = 0; i < callbacksCount; ++i) {
292 IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
293 try {
Ashutosh Joshi1d941812017-03-09 15:21:24 -0800294 callback.onMessageReceipt(hubHandle, appInstance, msg);
destradaa78cebca2016-04-14 18:40:14 -0700295 } catch (RemoteException e) {
296 Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
297 continue;
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700298 }
299 }
destradaa78cebca2016-04-14 18:40:14 -0700300 mCallbacksList.finishBroadcast();
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700301 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 Kaiserfe6d4f52016-08-19 10:24:07 -0700318 String action;
319 if (mNanoAppHash.containsKey(appInstanceHandle)) {
320 action = "Updated";
321 } else {
322 action = "Added";
323 }
324
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700325 mNanoAppHash.put(appInstanceHandle, appInfo);
Greg Kaiserfe6d4f52016-08-19 10:24:07 -0700326 Log.d(TAG, action + " app instance " + appInstanceHandle + " with id "
327 + appId + " version " + appVersion);
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700328
329 return 0;
330 }
Brian Duddiec3d8a522016-06-14 15:12:26 -0700331
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700332 private int deleteAppInstance(int appInstanceHandle) {
333 if (mNanoAppHash.remove(appInstanceHandle) == null) {
334 return -1;
335 }
336
337 return 0;
338 }
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700339}