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.location.NanoAppInstanceInfo;
21 import android.hardware.location.NanoAppState;
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 of
35  * 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      * <p>
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 nanoappStateList the list of loaded nanoapps
152      */
153     /* package */
updateCache(int contextHubId, List<NanoAppState> nanoappStateList)154     synchronized void updateCache(int contextHubId, List<NanoAppState> nanoappStateList) {
155         HashSet<Long> nanoAppIdSet = new HashSet<>();
156         for (NanoAppState nanoappState : nanoappStateList) {
157             handleQueryAppEntry(
158                     contextHubId, nanoappState.getNanoAppId(),
159                     (int) nanoappState.getNanoAppVersion());
160             nanoAppIdSet.add(nanoappState.getNanoAppId());
161         }
162 
163         Iterator<NanoAppInstanceInfo> iterator = mNanoAppHash.values().iterator();
164         while (iterator.hasNext()) {
165             NanoAppInstanceInfo info = iterator.next();
166             if (info.getContexthubId() == contextHubId &&
167                     !nanoAppIdSet.contains(info.getAppId())) {
168                 iterator.remove();
169             }
170         }
171     }
172 
173     /**
174      * If the nanoapp exists in the cache, then the entry is updated. Otherwise, inserts a new
175      * instance of the nanoapp in the cache. This method should only be invoked from updateCache.
176      *
177      * @param contextHubId   the ID of the hub the nanoapp is loaded in
178      * @param nanoAppId      the unique 64-bit ID of the nanoapp
179      * @param nanoAppVersion the version of the nanoapp
180      */
handleQueryAppEntry(int contextHubId, long nanoAppId, int nanoAppVersion)181     private void handleQueryAppEntry(int contextHubId, long nanoAppId, int nanoAppVersion) {
182         int nanoAppHandle = getNanoAppHandle(contextHubId, nanoAppId);
183         if (nanoAppHandle == -1) {
184             addNanoAppInstance(contextHubId, nanoAppId, nanoAppVersion);
185         } else {
186             NanoAppInstanceInfo info = mNanoAppHash.get(nanoAppHandle);
187             if (info.getAppVersion() != nanoAppVersion) {
188                 mNanoAppHash.put(nanoAppHandle, new NanoAppInstanceInfo(
189                         nanoAppHandle, nanoAppId, nanoAppVersion, contextHubId));
190                 if (ENABLE_LOG_DEBUG) {
191                     Log.v(TAG, "Updated app instance with handle " + nanoAppHandle + " at hub "
192                             + contextHubId + ": ID=0x" + Long.toHexString(nanoAppId)
193                             + ", version=0x" + Integer.toHexString(nanoAppVersion));
194                 }
195             }
196         }
197     }
198 }
199