blob: 1e8a5c5ef4a25abd4da7f5d02822d37a8292eb48 [file] [log] [blame]
Keun young Park9a91efb2019-11-15 18:10:47 -08001/*
2 * Copyright (C) 2019 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 com.android.car;
18
19import android.annotation.NonNull;
20import android.car.Car;
21import android.car.Car.FeaturerRequestEnum;
22import android.car.CarFeatures;
23import android.content.Context;
24import android.os.Build;
25import android.os.Handler;
26import android.os.HandlerThread;
27import android.util.AtomicFile;
28import android.util.Log;
29
30import com.android.internal.annotations.GuardedBy;
31
32import java.io.BufferedReader;
33import java.io.BufferedWriter;
34import java.io.File;
35import java.io.FileInputStream;
36import java.io.FileNotFoundException;
37import java.io.FileOutputStream;
38import java.io.IOException;
39import java.io.InputStreamReader;
40import java.io.OutputStreamWriter;
41import java.io.PrintWriter;
42import java.nio.charset.StandardCharsets;
43import java.util.ArrayList;
44import java.util.Arrays;
45import java.util.Collections;
46import java.util.HashSet;
47import java.util.List;
48
49/**
50 * Component controlling the feature of car.
51 */
52public final class CarFeatureController implements CarServiceBase {
53
54 private static final String TAG = "CAR.FEATURE";
55
56 // Use HaseSet for better search performance. Memory consumption is fixed and it not an issue.
57 // Should keep alphabetical order under each bucket.
58 // Update CarFeatureTest as well when this is updated.
59 private static final HashSet<String> MANDATORY_FEATURES = new HashSet<>(Arrays.asList(
60 Car.APP_FOCUS_SERVICE,
61 Car.AUDIO_SERVICE,
62 Car.BLUETOOTH_SERVICE,
63 Car.CAR_BUGREPORT_SERVICE,
Keun young Parkc6d80af2020-01-17 18:14:28 -080064 Car.CAR_CONFIGURATION_SERVICE,
Keun young Park9a91efb2019-11-15 18:10:47 -080065 Car.CAR_DRIVING_STATE_SERVICE,
66 Car.CAR_MEDIA_SERVICE,
67 Car.CAR_NAVIGATION_SERVICE,
68 Car.CAR_OCCUPANT_ZONE_SERVICE,
Keun young Parkc6d80af2020-01-17 18:14:28 -080069 Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE,
Keun young Park9a91efb2019-11-15 18:10:47 -080070 Car.CAR_USER_SERVICE,
Keun young Parkc6d80af2020-01-17 18:14:28 -080071 Car.CAR_UX_RESTRICTION_SERVICE,
Keun young Park9a91efb2019-11-15 18:10:47 -080072 Car.INFO_SERVICE,
73 Car.PACKAGE_SERVICE,
74 Car.POWER_SERVICE,
75 Car.PROJECTION_SERVICE,
76 Car.PROPERTY_SERVICE,
77 Car.TEST_SERVICE,
Keun young Parkc6d80af2020-01-17 18:14:28 -080078 // All items below here are deprecated, but still should be supported
Keun young Park9a91efb2019-11-15 18:10:47 -080079 Car.CAR_INSTRUMENT_CLUSTER_SERVICE,
80 Car.CABIN_SERVICE,
81 Car.HVAC_SERVICE,
Keun young Parkc6d80af2020-01-17 18:14:28 -080082 Car.SENSOR_SERVICE,
83 Car.VENDOR_EXTENSION_SERVICE
Keun young Park9a91efb2019-11-15 18:10:47 -080084 ));
85
86 private static final HashSet<String> OPTIONAL_FEATURES = new HashSet<>(Arrays.asList(
Keun young Parkc6d80af2020-01-17 18:14:28 -080087 CarFeatures.FEATURE_CAR_USER_NOTICE_SERVICE,
88 Car.DIAGNOSTIC_SERVICE,
Keun young Park9a91efb2019-11-15 18:10:47 -080089 Car.STORAGE_MONITORING_SERVICE,
Keun young Parkc6d80af2020-01-17 18:14:28 -080090 Car.VMS_SUBSCRIBER_SERVICE
Keun young Park9a91efb2019-11-15 18:10:47 -080091 ));
92
93 private static final String FEATURE_CONFIG_FILE_NAME = "car_feature_config.txt";
94
95 // Last line starts with this with number of features for extra sanity check.
96 private static final String CONFIG_FILE_LAST_LINE_MARKER = ",,";
97
98 // Set once in constructor and not updated. Access it without lock so that it can be accessed
99 // quickly.
100 private final HashSet<String> mEnabledFeatures;
101
102 private final Context mContext;
103
104 private final List<String> mDefaultEnabledFeaturesFromConfig;
105 private final List<String> mDisabledFeaturesFromVhal;
106
107 private final HandlerThread mHandlerThread;
108 private final Handler mHandler;
109
110 private final Object mLock = new Object();
111
112 @GuardedBy("mLock")
113 private final AtomicFile mFeatureConfigFile;
114
115 @GuardedBy("mLock")
116 private final List<String> mPendingEnabledFeatures = new ArrayList<>();
117
118 @GuardedBy("mLock")
119 private final List<String> mPendingDisabledFeatures = new ArrayList<>();
120
121 @GuardedBy("mLock")
122 private HashSet<String> mAvailableExperimentalFeatures = new HashSet<>();
123
124 private final Runnable mDefaultConfigWriter = () -> {
125 persistToFeatureConfigFile();
126 };
127
128 public CarFeatureController(@NonNull Context context,
129 @NonNull String[] defaultEnabledFeaturesFromConfig,
130 @NonNull String[] disabledFeaturesFromVhal, @NonNull File dataDir) {
131 mContext = context;
132 mDefaultEnabledFeaturesFromConfig = Arrays.asList(defaultEnabledFeaturesFromConfig);
133 mDisabledFeaturesFromVhal = Arrays.asList(disabledFeaturesFromVhal);
134 mEnabledFeatures = new HashSet<>(MANDATORY_FEATURES);
135 mFeatureConfigFile = new AtomicFile(new File(dataDir, FEATURE_CONFIG_FILE_NAME), TAG);
136 boolean shouldLoadDefaultConfig = !mFeatureConfigFile.exists();
137 if (!shouldLoadDefaultConfig) {
138 if (!loadFromConfigFileLocked()) {
139 shouldLoadDefaultConfig = true;
140 }
141 }
142 mHandlerThread = new HandlerThread(TAG);
143 mHandlerThread.start();
144 mHandler = new Handler(mHandlerThread.getLooper());
145 // Separate if to use this as backup for failure in loadFromConfigFileLocked()
146 if (shouldLoadDefaultConfig) {
147 parseDefaultConfig();
148 dispatchDefaultConfigUpdate();
149 }
150 }
151
152 @Override
153 public void init() {
154 // nothing should be done here. This should work with only constructor.
155 }
156
157 @Override
158 public void release() {
159 // nothing should be done here.
160 }
161
162 @Override
163 public void dump(PrintWriter writer) {
164 writer.println("*CarFeatureController*");
165 writer.println(" mEnabledFeatures:" + mEnabledFeatures);
166 writer.println(" mDefaultEnabledFeaturesFromConfig:" + mDefaultEnabledFeaturesFromConfig);
167 writer.println(" mDisabledFeaturesFromVhal:" + mDisabledFeaturesFromVhal);
168 synchronized (mLock) {
169 writer.println(" mAvailableExperimentalFeatures:" + mAvailableExperimentalFeatures);
170 writer.println(" mPendingEnabledFeatures:" + mPendingEnabledFeatures);
171 writer.println(" mPendingDisabledFeatures:" + mPendingDisabledFeatures);
172 }
173 }
174
175 /** Check {@link Car#isFeatureEnabled(String)} */
176 public boolean isFeatureEnabled(String featureName) {
177 return mEnabledFeatures.contains(featureName);
178 }
179
180 @FeaturerRequestEnum
181 private int checkFeatureExisting(String featureName) {
182 if (MANDATORY_FEATURES.contains(featureName)) {
183 return Car.FEATURE_REQUEST_MANDATORY;
184 }
185 if (!OPTIONAL_FEATURES.contains(featureName)) {
186 synchronized (mLock) {
187 if (!mAvailableExperimentalFeatures.contains(featureName)) {
188 Log.e(TAG, "enableFeature requested for non-existing feature:"
189 + featureName);
190 return Car.FEATURE_REQUEST_NOT_EXISTING;
191 }
192 }
193 }
194 return Car.FEATURE_REQUEST_SUCCESS;
195 }
196
197 /** Check {@link Car#enableFeature(String)} */
198 public int enableFeature(String featureName) {
199 assertPermission();
200 int checkResult = checkFeatureExisting(featureName);
201 if (checkResult != Car.FEATURE_REQUEST_SUCCESS) {
202 return checkResult;
203 }
204
205 boolean alreadyEnabled = mEnabledFeatures.contains(featureName);
206 boolean shouldUpdateConfigFile = false;
207 synchronized (mLock) {
208 if (mPendingDisabledFeatures.remove(featureName)) {
209 shouldUpdateConfigFile = true;
210 }
211 if (!mPendingEnabledFeatures.contains(featureName) && !alreadyEnabled) {
212 shouldUpdateConfigFile = true;
213 mPendingEnabledFeatures.add(featureName);
214 }
215 }
216 if (shouldUpdateConfigFile) {
217 Log.w(TAG, "Enabling feature in config file:" + featureName);
218 dispatchDefaultConfigUpdate();
219 }
220 if (alreadyEnabled) {
221 return Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE;
222 } else {
223 return Car.FEATURE_REQUEST_SUCCESS;
224 }
225 }
226
227 /** Check {@link Car#disableFeature(String)} */
228 public int disableFeature(String featureName) {
229 assertPermission();
230 int checkResult = checkFeatureExisting(featureName);
231 if (checkResult != Car.FEATURE_REQUEST_SUCCESS) {
232 return checkResult;
233 }
234
235 boolean alreadyDisabled = !mEnabledFeatures.contains(featureName);
236 boolean shouldUpdateConfigFile = false;
237 synchronized (mLock) {
238 if (mPendingEnabledFeatures.remove(featureName)) {
239 shouldUpdateConfigFile = true;
240 }
241 if (!mPendingDisabledFeatures.contains(featureName) && !alreadyDisabled) {
242 shouldUpdateConfigFile = true;
243 mPendingDisabledFeatures.add(featureName);
244 }
245 }
246 if (shouldUpdateConfigFile) {
247 Log.w(TAG, "Disabling feature in config file:" + featureName);
248 dispatchDefaultConfigUpdate();
249 }
250 if (alreadyDisabled) {
251 return Car.FEATURE_REQUEST_ALREADY_IN_THE_STATE;
252 } else {
253 return Car.FEATURE_REQUEST_SUCCESS;
254 }
255 }
256
257 /**
258 * Set available experimental features. Only features set through this call will be allowed to
259 * be enabled for experimental features. Setting this is not allowed for USER build.
260 *
261 * @return True if set is allowed and set. False if experimental feature is not allowed.
262 */
263 public boolean setAvailableExperimentalFeatureList(List<String> experimentalFeatures) {
264 assertPermission();
265 if (Build.IS_USER) {
266 Log.e(TAG, "Experimental feature list set for USER build",
267 new RuntimeException());
268 return false;
269 }
270 synchronized (mLock) {
271 mAvailableExperimentalFeatures.clear();
272 mAvailableExperimentalFeatures.addAll(experimentalFeatures);
273 }
274 return true;
275 }
276
277 /** Check {@link Car#getAllEnabledFeatures()} */
278 public List<String> getAllEnabledFeatures() {
279 assertPermission();
280 return new ArrayList<>(mEnabledFeatures);
281 }
282
283 /** Check {@link Car#getAllPendingDisabledFeatures()} */
284 public List<String> getAllPendingDisabledFeatures() {
285 assertPermission();
286 synchronized (mLock) {
287 return new ArrayList<>(mPendingDisabledFeatures);
288 }
289 }
290
291 /** Check {@link Car#getAllPendingEnabledFeatures()} */
292 public List<String> getAllPendingEnabledFeatures() {
293 assertPermission();
294 synchronized (mLock) {
295 return new ArrayList<>(mPendingEnabledFeatures);
296 }
297 }
298
299 /** Returns currently enabled experimental features */
300 public @NonNull List<String> getEnabledExperimentalFeatures() {
301 if (Build.IS_USER) {
302 Log.e(TAG, "getEnabledExperimentalFeatures called in USER build",
303 new RuntimeException());
304 return Collections.emptyList();
305 }
306 ArrayList<String> experimentalFeature = new ArrayList<>();
307 for (String feature: mEnabledFeatures) {
308 if (MANDATORY_FEATURES.contains(feature)) {
309 continue;
310 }
311 if (OPTIONAL_FEATURES.contains(feature)) {
312 continue;
313 }
314 experimentalFeature.add(feature);
315 }
316 return experimentalFeature;
317 }
318
319 void handleCorruptConfigFileLocked(String msg, String line) {
320 Log.e(TAG, msg + ", considered as corrupt, line:" + line);
321 mEnabledFeatures.clear();
322 }
323
324 private boolean loadFromConfigFileLocked() {
325 // done without lock, should be only called from constructor.
326 FileInputStream fis;
327 try {
328 fis = mFeatureConfigFile.openRead();
329 } catch (FileNotFoundException e) {
330 Log.i(TAG, "Feature config file not found, this could be 1st boot");
331 return false;
332 }
333 try (BufferedReader reader = new BufferedReader(
334 new InputStreamReader(fis, StandardCharsets.UTF_8))) {
335 boolean lastLinePassed = false;
336 while (true) {
337 String line = reader.readLine();
338 if (line == null) {
339 if (!lastLinePassed) {
340 handleCorruptConfigFileLocked("No last line checksum", "");
341 return false;
342 }
343 break;
344 }
345 if (lastLinePassed && !line.isEmpty()) {
346 handleCorruptConfigFileLocked(
347 "Config file has additional line after last line marker", line);
348 return false;
349 } else {
350 if (line.startsWith(CONFIG_FILE_LAST_LINE_MARKER)) {
351 int numberOfFeatures;
352 try {
353 numberOfFeatures = Integer.valueOf(line.substring(
354 CONFIG_FILE_LAST_LINE_MARKER.length()));
355 } catch (NumberFormatException e) {
356 handleCorruptConfigFileLocked(
357 "Config file has corrupt last line, not a number",
358 line);
359 return false;
360 }
361 int actualNumberOfFeatures = mEnabledFeatures.size();
362 if (numberOfFeatures != actualNumberOfFeatures) {
363 handleCorruptConfigFileLocked(
364 "Config file has wrong number of features, expected:"
365 + numberOfFeatures
366 + " actual:" + actualNumberOfFeatures, line);
367 return false;
368 }
369 lastLinePassed = true;
370 } else {
371 mEnabledFeatures.add(line);
372 }
373 }
374 }
375 } catch (IOException e) {
376 Log.w(TAG, "Cannot load config file", e);
377 return false;
378 }
379 Log.i(TAG, "Loaded features:" + mEnabledFeatures);
380 return true;
381 }
382
383 private void persistToFeatureConfigFile() {
384 HashSet<String> features = new HashSet<>(mEnabledFeatures);
385 synchronized (mLock) {
386 features.removeAll(mPendingDisabledFeatures);
387 features.addAll(mPendingEnabledFeatures);
388 FileOutputStream fos;
389 try {
390 fos = mFeatureConfigFile.startWrite();
391 } catch (IOException e) {
392 Log.e(TAG, "Cannot create config file", e);
393 return;
394 }
395 try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos,
396 StandardCharsets.UTF_8))) {
397 Log.i(TAG, "Updating features:" + features);
398 for (String feature : features) {
399 writer.write(feature);
400 writer.newLine();
401 }
402 writer.write(CONFIG_FILE_LAST_LINE_MARKER + features.size());
403 writer.flush();
404 mFeatureConfigFile.finishWrite(fos);
405 } catch (IOException e) {
406 mFeatureConfigFile.failWrite(fos);
407 Log.e(TAG, "Cannot create config file", e);
408 }
409 }
410 }
411
412 private void assertPermission() {
413 ICarImpl.assertPermission(mContext, Car.PERMISSION_CONTROL_CAR_FEATURES);
414 }
415
416 private void dispatchDefaultConfigUpdate() {
417 mHandler.removeCallbacks(mDefaultConfigWriter);
418 mHandler.post(mDefaultConfigWriter);
419 }
420
421 private void parseDefaultConfig() {
422 for (String feature : mDefaultEnabledFeaturesFromConfig) {
423 if (!OPTIONAL_FEATURES.contains(feature)) {
424 throw new IllegalArgumentException(
425 "config_default_enabled_optional_car_features include non-optional "
426 + "features:" + feature);
427 }
428 if (mDisabledFeaturesFromVhal.contains(feature)) {
429 continue;
430 }
431 mEnabledFeatures.add(feature);
432 }
433 Log.i(TAG, "Loaded default features:" + mEnabledFeatures);
434 }
435}