blob: 893ed3f7c2014907bb0f84f1dcbd78f6cc96818f [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
17package android.hardware.location;
18
Ashutosh Joshi54787a52016-04-27 11:19:16 -070019import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.HashMap;
23
destradaa8bad3fe2016-03-15 12:33:40 -070024import android.Manifest;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080025import android.content.Context;
Ashutosh Joshi6239cc62016-04-04 16:19:29 -070026import android.content.pm.PackageManager;
destradaa78cebca2016-04-14 18:40:14 -070027import android.os.RemoteCallbackList;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080028import android.os.RemoteException;
Brian Duddiec3d8a522016-06-14 15:12:26 -070029import android.os.ServiceManager;
30import android.service.vr.IVrManager;
31import android.service.vr.IVrStateCallbacks;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080032import android.util.Log;
33
Ashutosh Joshi6239cc62016-04-04 16:19:29 -070034import java.io.FileDescriptor;
35import java.io.PrintWriter;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080036import java.util.ArrayList;
Brian Duddiec3d8a522016-06-14 15:12:26 -070037import java.util.concurrent.ConcurrentHashMap;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080038
39/**
40 * @hide
41 */
Peng Xu9ff7d222016-02-11 13:02:05 -080042public class ContextHubService extends IContextHubService.Stub {
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070043 public static final String CONTEXTHUB_SERVICE = "contexthub_service";
44
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080045 private static final String TAG = "ContextHubService";
destradaa8bad3fe2016-03-15 12:33:40 -070046 private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE;
47 private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '"
Ashutosh Joshi6239cc62016-04-04 16:19:29 -070048 + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware";
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080049
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070050
51 public static final int ANY_HUB = -1;
Ashutosh Joshicafdee92016-04-04 16:19:29 -070052 public static final int MSG_LOAD_NANO_APP = 3;
53 public static final int MSG_UNLOAD_NANO_APP = 4;
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070054
55 private static final String PRE_LOADED_GENERIC_UNKNOWN = "Preloaded app, unknown";
56 private static final String PRE_LOADED_APP_NAME = PRE_LOADED_GENERIC_UNKNOWN;
57 private static final String PRE_LOADED_APP_PUBLISHER = PRE_LOADED_GENERIC_UNKNOWN;
58 private static final int PRE_LOADED_APP_MEM_REQ = 0;
59
60 private static final int MSG_HEADER_SIZE = 4;
Ashutosh Joshi54787a52016-04-27 11:19:16 -070061 private static final int HEADER_FIELD_MSG_TYPE = 0;
62 private static final int HEADER_FIELD_MSG_VERSION = 1;
63 private static final int HEADER_FIELD_HUB_HANDLE = 2;
64 private static final int HEADER_FIELD_APP_INSTANCE = 3;
65
66 private static final int HEADER_FIELD_LOAD_APP_ID_LO = MSG_HEADER_SIZE;
67 private static final int HEADER_FIELD_LOAD_APP_ID_HI = MSG_HEADER_SIZE + 1;
68 private static final int MSG_LOAD_APP_HEADER_SIZE = MSG_HEADER_SIZE + 2;
Ashutosh Joshib741e3b2016-03-29 09:19:56 -070069
70 private static final int OS_APP_INSTANCE = -1;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080071
Brian Duddiec3d8a522016-06-14 15:12:26 -070072 private static final long APP_ID_ACTIVITY_RECOGNITION = 0x476f6f676c001000L;
73
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
Brian Duddiec3d8a522016-06-14 15:12:26 -070084 private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
85 @Override
86 public void onVrStateChanged(boolean enabled) {
87 for (NanoAppInstanceInfo app : mNanoAppHash.values()) {
88 if (app.getAppId() == APP_ID_ACTIVITY_RECOGNITION) {
89 sendVrStateChangeMessageToApp(app, enabled);
90 break;
91 }
92 }
93 }
94 };
95
Peng Xu9ff7d222016-02-11 13:02:05 -080096 public ContextHubService(Context context) {
97 mContext = context;
98 mContextHubInfo = nativeInitialize();
99
100 for (int i = 0; i < mContextHubInfo.length; i++) {
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700101 Log.d(TAG, "ContextHub[" + i + "] id: " + mContextHubInfo[i].getId()
Peng Xu9ff7d222016-02-11 13:02:05 -0800102 + ", name: " + mContextHubInfo[i].getName());
103 }
Brian Duddiec3d8a522016-06-14 15:12:26 -0700104
105 if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_VR_MODE)) {
106 IVrManager vrManager =
107 IVrManager.Stub.asInterface(ServiceManager.getService("vrmanager"));
108 if (vrManager != null) {
109 try {
110 vrManager.registerListener(mVrStateCallbacks);
111 } catch (RemoteException e) {
112 Log.e(TAG, "VR state listener registration failed", e);
113 }
114 }
115 }
Peng Xu9ff7d222016-02-11 13:02:05 -0800116 }
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800117
Peng Xu9ff7d222016-02-11 13:02:05 -0800118 @Override
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700119 public int registerCallback(IContextHubCallback callback) throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700120 checkPermissions();
destradaa78cebca2016-04-14 18:40:14 -0700121 mCallbacksList.register(callback);
Peng Xu9ff7d222016-02-11 13:02:05 -0800122 return 0;
123 }
124
125 @Override
126 public int[] getContextHubHandles() throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700127 checkPermissions();
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700128 int[] returnArray = new int[mContextHubInfo.length];
Peng Xu9ff7d222016-02-11 13:02:05 -0800129
130 for (int i = 0; i < returnArray.length; ++i) {
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700131 returnArray[i] = i;
Peng Xu9ff7d222016-02-11 13:02:05 -0800132 Log.d(TAG, String.format("Hub %s is mapped to %d",
133 mContextHubInfo[i].getName(), returnArray[i]));
134 }
135
136 return returnArray;
137 }
138
139 @Override
140 public ContextHubInfo getContextHubInfo(int contextHubHandle) throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700141 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800142 if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) {
143 return null; // null means fail
144 }
145
146 return mContextHubInfo[contextHubHandle];
147 }
148
149 @Override
150 public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700151 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800152
153 if (!(contextHubHandle >= 0 && contextHubHandle < mContextHubInfo.length)) {
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700154 Log.e(TAG, "Invalid contextHubhandle " + contextHubHandle);
155 return -1;
Peng Xu9ff7d222016-02-11 13:02:05 -0800156 }
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600157 if (app == null) {
158 return -1;
159 }
Peng Xu9ff7d222016-02-11 13:02:05 -0800160
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700161 int[] msgHeader = new int[MSG_LOAD_APP_HEADER_SIZE];
162 msgHeader[HEADER_FIELD_HUB_HANDLE] = contextHubHandle;
163 msgHeader[HEADER_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
164 msgHeader[HEADER_FIELD_MSG_VERSION] = 0;
165 msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_LOAD_NANO_APP;
166
167 long appId = app.getAppId();
168
169 msgHeader[HEADER_FIELD_LOAD_APP_ID_LO] = (int)(appId & 0xFFFFFFFF);
170 msgHeader[HEADER_FIELD_LOAD_APP_ID_HI] = (int)((appId >> 32) & 0xFFFFFFFF);
Peng Xu9ff7d222016-02-11 13:02:05 -0800171
Ashutosh Joshi11864402016-07-15 13:46:22 -0700172 int errVal = nativeSendMessage(msgHeader, app.getAppBinary());
173 if (errVal != 0) {
174 Log.e(TAG, "Send Message returns error" + contextHubHandle);
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700175 return -1;
176 }
Ashutosh Joshi11864402016-07-15 13:46:22 -0700177
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700178 // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
179 return 0;
Peng Xu9ff7d222016-02-11 13:02:05 -0800180 }
181
destradaa8bad3fe2016-03-15 12:33:40 -0700182 @Override
183 public int unloadNanoApp(int nanoAppInstanceHandle) throws RemoteException {
184 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800185 NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstanceHandle);
186 if (info == null) {
destradaa8bad3fe2016-03-15 12:33:40 -0700187 return -1; //means failed
Peng Xu9ff7d222016-02-11 13:02:05 -0800188 }
189
190 // Call Native interface here
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700191 int[] msgHeader = new int[MSG_HEADER_SIZE];
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700192 msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB;
193 msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppInstanceHandle;
194 msgHeader[HEADER_FIELD_MSG_VERSION] = 0;
195 msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_UNLOAD_NANO_APP;
Peng Xu9ff7d222016-02-11 13:02:05 -0800196
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700197 byte msg[] = new byte[0];
198
199 if (nativeSendMessage(msgHeader, msg) != 0) {
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700200 return -1;
201 }
202
203 // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
204 return 0;
destradaa8bad3fe2016-03-15 12:33:40 -0700205 }
Peng Xu9ff7d222016-02-11 13:02:05 -0800206
207 @Override
destradaa8bad3fe2016-03-15 12:33:40 -0700208 public NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppInstanceHandle)
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700209 throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700210 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800211 // This assumes that all the nanoAppInfo is current. This is reasonable
212 // for the use cases for tightly controlled nanoApps.
213 if (mNanoAppHash.containsKey(nanoAppInstanceHandle)) {
214 return mNanoAppHash.get(nanoAppInstanceHandle);
215 } else {
216 return null;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800217 }
218 }
219
220 @Override
Peng Xu9ff7d222016-02-11 13:02:05 -0800221 public int[] findNanoAppOnHub(int hubHandle, NanoAppFilter filter) throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700222 checkPermissions();
Peng Xu9ff7d222016-02-11 13:02:05 -0800223 ArrayList<Integer> foundInstances = new ArrayList<Integer>();
224
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700225 for (Integer nanoAppInstance: mNanoAppHash.keySet()) {
Peng Xu9ff7d222016-02-11 13:02:05 -0800226 NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppInstance);
227
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700228 if (filter.testMatch(info)) {
Peng Xu9ff7d222016-02-11 13:02:05 -0800229 foundInstances.add(nanoAppInstance);
230 }
231 }
232
233 int[] retArray = new int[foundInstances.size()];
234 for (int i = 0; i < foundInstances.size(); i++) {
235 retArray[i] = foundInstances.get(i).intValue();
236 }
237
238 return retArray;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800239 }
240
241 @Override
destradaa8bad3fe2016-03-15 12:33:40 -0700242 public int sendMessage(int hubHandle, int nanoAppHandle, ContextHubMessage msg)
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700243 throws RemoteException {
destradaa8bad3fe2016-03-15 12:33:40 -0700244 checkPermissions();
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700245
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600246 if (msg == null || msg.getData() == null) {
247 Log.w(TAG, "null ptr");
248 return -1;
249 }
250
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700251 int[] msgHeader = new int[MSG_HEADER_SIZE];
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700252 msgHeader[HEADER_FIELD_HUB_HANDLE] = hubHandle;
253 msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppHandle;
254 msgHeader[HEADER_FIELD_MSG_VERSION] = msg.getVersion();
255 msgHeader[HEADER_FIELD_MSG_TYPE] = msg.getMsgType();
Peng Xu9ff7d222016-02-11 13:02:05 -0800256
257 return nativeSendMessage(msgHeader, msg.getData());
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800258 }
destradaa8bad3fe2016-03-15 12:33:40 -0700259
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700260 @Override
261 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
262 if (mContext.checkCallingOrSelfPermission("android.permission.DUMP")
263 != PackageManager.PERMISSION_GRANTED) {
264 pw.println("Permission Denial: can't dump contexthub_service");
265 return;
266 }
267
268 pw.println("Dumping ContextHub Service");
269
270 pw.println("");
271 // dump ContextHubInfo
272 pw.println("=================== CONTEXT HUBS ====================");
273 for (int i = 0; i < mContextHubInfo.length; i++) {
274 pw.println("Handle " + i + " : " + mContextHubInfo[i].toString());
275 }
276 pw.println("");
277 pw.println("=================== NANOAPPS ====================");
278 // Dump nanoAppHash
279 for (Integer nanoAppInstance: mNanoAppHash.keySet()) {
280 pw.println(nanoAppInstance + " : " + mNanoAppHash.get(nanoAppInstance).toString());
281 }
282
283 // dump eventLog
284 }
285
destradaa8bad3fe2016-03-15 12:33:40 -0700286 private void checkPermissions() {
287 mContext.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE);
288 }
Ashutosh Joshi2c697fb2016-04-01 20:48:13 +0000289
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700290 private int onMessageReceipt(int[] header, byte[] data) {
291 if (header == null || data == null || header.length < MSG_HEADER_SIZE) {
292 return -1;
293 }
destradaa78cebca2016-04-14 18:40:14 -0700294 int callbacksCount = mCallbacksList.beginBroadcast();
295 if (callbacksCount < 1) {
296 Log.v(TAG, "No message callbacks registered.");
297 return 0;
298 }
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700299
300 ContextHubMessage msg = new ContextHubMessage(header[HEADER_FIELD_MSG_TYPE],
301 header[HEADER_FIELD_MSG_VERSION],
302 data);
destradaa78cebca2016-04-14 18:40:14 -0700303 for (int i = 0; i < callbacksCount; ++i) {
304 IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
305 try {
306 callback.onMessageReceipt(
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700307 header[HEADER_FIELD_HUB_HANDLE],
308 header[HEADER_FIELD_APP_INSTANCE],
309 msg);
destradaa78cebca2016-04-14 18:40:14 -0700310 } catch (RemoteException e) {
311 Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
312 continue;
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700313 }
314 }
destradaa78cebca2016-04-14 18:40:14 -0700315 mCallbacksList.finishBroadcast();
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700316 return 0;
317 }
318
319 private int addAppInstance(int hubHandle, int appInstanceHandle, long appId, int appVersion) {
320 // App Id encodes vendor & version
321 NanoAppInstanceInfo appInfo = new NanoAppInstanceInfo();
322
323 appInfo.setAppId(appId);
324 appInfo.setAppVersion(appVersion);
325 appInfo.setName(PRE_LOADED_APP_NAME);
326 appInfo.setContexthubId(hubHandle);
327 appInfo.setHandle(appInstanceHandle);
328 appInfo.setPublisher(PRE_LOADED_APP_PUBLISHER);
329 appInfo.setNeededExecMemBytes(PRE_LOADED_APP_MEM_REQ);
330 appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ);
331 appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ);
332
333 mNanoAppHash.put(appInstanceHandle, appInfo);
334 Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId
335 + " version " + appVersion);
336
337 return 0;
338 }
Brian Duddiec3d8a522016-06-14 15:12:26 -0700339
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700340 private int deleteAppInstance(int appInstanceHandle) {
341 if (mNanoAppHash.remove(appInstanceHandle) == null) {
342 return -1;
343 }
344
345 return 0;
346 }
347
Brian Duddiec3d8a522016-06-14 15:12:26 -0700348 private void sendVrStateChangeMessageToApp(NanoAppInstanceInfo app, boolean vrModeEnabled) {
349 int[] msgHeader = new int[MSG_HEADER_SIZE];
Ashutosh Joshi54787a52016-04-27 11:19:16 -0700350 msgHeader[HEADER_FIELD_MSG_TYPE] = 0;
351 msgHeader[HEADER_FIELD_MSG_VERSION] = 0;
352 msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB;
353 msgHeader[HEADER_FIELD_APP_INSTANCE] = app.getHandle();
Brian Duddiec3d8a522016-06-14 15:12:26 -0700354
355 byte[] data = new byte[1];
356 data[0] = (byte) ((vrModeEnabled) ? 1 : 0);
357 int ret = nativeSendMessage(msgHeader, data);
358 if (ret != 0) {
359 Log.e(TAG, "Couldn't send VR state change notification (" + ret + ")!");
360 }
361 }
Ashutosh Joshib741e3b2016-03-29 09:19:56 -0700362}