• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 com.android.server.location.contexthub;
18 
19 import android.annotation.Nullable;
20 import android.hardware.contexthub.V1_2.HubAppInfo;
21 import android.hardware.location.NanoAppInstanceInfo;
22 import android.util.Log;
23 
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.function.Consumer;
29 
30 /**
31  * Manages the state of loaded nanoapps at the Context Hubs.
32  *
33  * This class maintains a list of nanoapps that have been informed as loaded at the hubs. The state
34  * should be updated based on the hub callbacks (defined in IContexthubCallback.hal), as a result
35  * of either loadNanoApp, unloadNanoApp, or queryApps.
36  *
37  * The state tracked by this manager is used by clients of ContextHubService that use the old APIs.
38  *
39  * TODO(b/69270990): Remove this class and its logic once the old API is deprecated.
40  *
41  * @hide
42  */
43 /* package */ class NanoAppStateManager {
44     private static final String TAG = "NanoAppStateManager";
45 
46     /*
47      * Enables verbose debug logs for this class.
48      */
49     private static final boolean ENABLE_LOG_DEBUG = true;
50 
51     /*
52      * Service cache maintaining of handle to nanoapp infos.
53      */
54     private final HashMap<Integer, NanoAppInstanceInfo> mNanoAppHash = new HashMap<>();
55 
56     /*
57      * The next nanoapp handle to use.
58      */
59     private int mNextHandle = 0;
60 
61     /**
62      * @param nanoAppHandle the nanoapp handle
63      * @return the NanoAppInstanceInfo for the given nanoapp, or null if the nanoapp does not exist
64      *         in the cache
65      */
66     @Nullable
67     /* package */
getNanoAppInstanceInfo(int nanoAppHandle)68     synchronized NanoAppInstanceInfo getNanoAppInstanceInfo(int nanoAppHandle) {
69         return mNanoAppHash.get(nanoAppHandle);
70     }
71 
72     /**
73      * Invokes a Consumer operation for each NanoAppInstanceInfo entry in the cache
74      *
75      * @param consumer the Consumer operation to perform
76      */
77     /* package */
foreachNanoAppInstanceInfo(Consumer<NanoAppInstanceInfo> consumer)78     synchronized void foreachNanoAppInstanceInfo(Consumer<NanoAppInstanceInfo> consumer) {
79         for (NanoAppInstanceInfo info : mNanoAppHash.values()) {
80             consumer.accept(info);
81         }
82     }
83 
84     /**
85      * @param contextHubId the ID of the hub to search for the instance
86      * @param nanoAppId the unique 64-bit ID of the nanoapp
87      * @return the nanoapp handle, -1 if the nanoapp is not in the cache
88      */
89     /* package */
getNanoAppHandle(int contextHubId, long nanoAppId)90     synchronized int getNanoAppHandle(int contextHubId, long nanoAppId) {
91         for (NanoAppInstanceInfo info : mNanoAppHash.values()) {
92             if (info.getContexthubId() == contextHubId && info.getAppId() == nanoAppId) {
93                 return info.getHandle();
94             }
95         }
96 
97         return -1;
98     }
99 
100     /**
101      * Adds a nanoapp instance to the cache.
102      *
103      * If the cache already contained the nanoapp, the entry is removed and a new nanoapp handle is
104      * generated.
105      *
106      * @param contextHubId the ID of the hub the nanoapp is loaded in
107      * @param nanoAppId the unique 64-bit ID of the nanoapp
108      * @param nanoAppVersion the version of the nanoapp
109      */
110     /* package */
addNanoAppInstance(int contextHubId, long nanoAppId, int nanoAppVersion)111     synchronized void addNanoAppInstance(int contextHubId, long nanoAppId, int nanoAppVersion) {
112         removeNanoAppInstance(contextHubId, nanoAppId);
113         if (mNanoAppHash.size() == Integer.MAX_VALUE) {
114             Log.e(TAG, "Error adding nanoapp instance: max limit exceeded");
115             return;
116         }
117 
118         int nanoAppHandle = mNextHandle;
119         for (int i = 0; i <= Integer.MAX_VALUE; i++) {
120             if (!mNanoAppHash.containsKey(nanoAppHandle)) {
121                 mNanoAppHash.put(nanoAppHandle, new NanoAppInstanceInfo(
122                         nanoAppHandle, nanoAppId, nanoAppVersion, contextHubId));
123                 mNextHandle = (nanoAppHandle == Integer.MAX_VALUE) ? 0 : nanoAppHandle + 1;
124                 break;
125             }
126             nanoAppHandle = (nanoAppHandle == Integer.MAX_VALUE) ? 0 : nanoAppHandle + 1;
127         }
128 
129         if (ENABLE_LOG_DEBUG) {
130             Log.v(TAG, "Added app instance with handle " + nanoAppHandle + " to hub "
131                     + contextHubId + ": ID=0x" + Long.toHexString(nanoAppId)
132                     + ", version=0x" + Integer.toHexString(nanoAppVersion));
133         }
134     }
135 
136     /**
137      * Removes a nanoapp instance from the cache.
138      *
139      * @param nanoAppId the ID of the nanoapp to remove the instance of
140      */
141     /* package */
removeNanoAppInstance(int contextHubId, long nanoAppId)142     synchronized void removeNanoAppInstance(int contextHubId, long nanoAppId) {
143         int nanoAppHandle = getNanoAppHandle(contextHubId, nanoAppId);
144         mNanoAppHash.remove(nanoAppHandle);
145     }
146 
147     /**
148      * Performs a batch update of the nanoapp cache given a nanoapp query response.
149      *
150      * @param contextHubId    the ID of the hub the response came from
151      * @param nanoAppInfoList the list of loaded nanoapps
152      */
153     /* package */
updateCache(int contextHubId, List<HubAppInfo> nanoAppInfoList)154     synchronized void updateCache(int contextHubId, List<HubAppInfo> nanoAppInfoList) {
155         HashSet<Long> nanoAppIdSet = new HashSet<>();
156         for (HubAppInfo appInfo : nanoAppInfoList) {
157             handleQueryAppEntry(contextHubId, appInfo.info_1_0.appId, appInfo.info_1_0.version);
158             nanoAppIdSet.add(appInfo.info_1_0.appId);
159         }
160 
161         Iterator<NanoAppInstanceInfo> iterator = mNanoAppHash.values().iterator();
162         while (iterator.hasNext()) {
163             NanoAppInstanceInfo info = iterator.next();
164             if (info.getContexthubId() == contextHubId &&
165                     !nanoAppIdSet.contains(info.getAppId())) {
166                 iterator.remove();
167             }
168         }
169     }
170 
171     /**
172      * If the nanoapp exists in the cache, then the entry is updated. Otherwise, inserts a new
173      * instance of the nanoapp in the cache. This method should only be invoked from updateCache.
174      *
175      * @param contextHubId the ID of the hub the nanoapp is loaded in
176      * @param nanoAppId the unique 64-bit ID of the nanoapp
177      * @param nanoAppVersion the version of the nanoapp
178      */
handleQueryAppEntry(int contextHubId, long nanoAppId, int nanoAppVersion)179     private void handleQueryAppEntry(int contextHubId, long nanoAppId, int nanoAppVersion) {
180         int nanoAppHandle = getNanoAppHandle(contextHubId, nanoAppId);
181         if (nanoAppHandle == -1) {
182             addNanoAppInstance(contextHubId, nanoAppId, nanoAppVersion);
183         } else {
184             NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppHandle);
185             if (info.getAppVersion() != nanoAppVersion) {
186                 mNanoAppHash.put(nanoAppHandle, new NanoAppInstanceInfo(
187                         nanoAppHandle, nanoAppId, nanoAppVersion, contextHubId));
188                 if (ENABLE_LOG_DEBUG) {
189                     Log.v(TAG, "Updated app instance with handle " + nanoAppHandle + " at hub "
190                             + contextHubId + ": ID=0x" + Long.toHexString(nanoAppId)
191                             + ", version=0x" + Integer.toHexString(nanoAppVersion));
192                 }
193             }
194         }
195     }
196 }
197