1 /*
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 
17 package android.car.hardware;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.car.VehicleAreaType;
24 import android.car.VehicleAreaType.VehicleAreaTypeValue;
25 import android.car.VehiclePropertyType;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.util.SparseArray;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.lang.reflect.Array;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 
37 /**
38  * Represents general information about car property such as data type and min/max ranges for car
39  * areas (if applicable). This class supposed to be immutable, parcelable and could be passed over.
40  *
41  * <p>Use {@link CarPropertyConfig#newBuilder} to create an instance of this class.
42  *
43  * @param <T> refer to Parcel#writeValue(Object) to get a list of all supported types. The class
44  * should be visible to framework as default class loader is being used here.
45  *
46  */
47 public final class CarPropertyConfig<T> implements Parcelable {
48     private final int mAccess;
49     private final int mAreaType;
50     private final int mChangeMode;
51     private final ArrayList<Integer> mConfigArray;
52     private final String mConfigString;
53     private final float mMaxSampleRate;
54     private final float mMinSampleRate;
55     private final int mPropertyId;
56     private final SparseArray<AreaConfig<T>> mSupportedAreas;
57     private final Class<T> mType;
58 
CarPropertyConfig(int access, int areaType, int changeMode, ArrayList<Integer> configArray, String configString, float maxSampleRate, float minSampleRate, int propertyId, SparseArray<AreaConfig<T>> supportedAreas, Class<T> type)59     private CarPropertyConfig(int access, int areaType, int changeMode,
60             ArrayList<Integer> configArray, String configString,
61             float maxSampleRate, float minSampleRate, int propertyId,
62             SparseArray<AreaConfig<T>> supportedAreas, Class<T> type) {
63         mAccess = access;
64         mAreaType = areaType;
65         mChangeMode = changeMode;
66         mConfigArray = configArray;
67         mConfigString = configString;
68         mMaxSampleRate = maxSampleRate;
69         mMinSampleRate = minSampleRate;
70         mPropertyId = propertyId;
71         mSupportedAreas = supportedAreas;
72         mType = type;
73     }
74 
75     /** @hide */
76     @IntDef(prefix = {"VEHICLE_PROPERTY_ACCESS"}, value = {
77         VEHICLE_PROPERTY_ACCESS_NONE,
78         VEHICLE_PROPERTY_ACCESS_READ,
79         VEHICLE_PROPERTY_ACCESS_WRITE,
80         VEHICLE_PROPERTY_ACCESS_READ_WRITE
81     })
82     @Retention(RetentionPolicy.SOURCE)
83     public @interface VehiclePropertyAccessType {}
84 
85     /** Property Access Unknown */
86     public static final int VEHICLE_PROPERTY_ACCESS_NONE = 0;
87     /** The property is readable */
88     public static final int VEHICLE_PROPERTY_ACCESS_READ = 1;
89     /** The property is writable */
90     public static final int VEHICLE_PROPERTY_ACCESS_WRITE = 2;
91     /** The property is readable and writable */
92     public static final int VEHICLE_PROPERTY_ACCESS_READ_WRITE = 3;
93 
94     /** @hide */
95     @IntDef(prefix = {"VEHICLE_PROPERTY_CHANGE_MODE"}, value = {
96         VEHICLE_PROPERTY_CHANGE_MODE_STATIC,
97         VEHICLE_PROPERTY_CHANGE_MODE_ONCHANGE,
98         VEHICLE_PROPERTY_CHANGE_MODE_CONTINUOUS,
99     })
100     @Retention(RetentionPolicy.SOURCE)
101     public @interface VehiclePropertyChangeModeType {}
102 
103     /** Properties of this type must never be changed. */
104     public static final int VEHICLE_PROPERTY_CHANGE_MODE_STATIC = 0;
105     /** Properties of this type must report when there is a change. */
106     public static final int VEHICLE_PROPERTY_CHANGE_MODE_ONCHANGE = 1;
107     /** Properties of this type change continuously. */
108     public static final int VEHICLE_PROPERTY_CHANGE_MODE_CONTINUOUS = 2;
109 
110     /**
111      * Return the access type of the car property.
112      * <p>The access type could be one of the following:
113      * <ul>
114      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_NONE}</li>
115      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_READ}</li>
116      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_WRITE}</li>
117      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_ACCESS_READ_WRITE}</li>
118      * </ul>
119      *
120      * @return the access type of the car property.
121      */
getAccess()122     public @VehiclePropertyAccessType int getAccess() {
123         return mAccess;
124     }
125 
126     /**
127      * Return the area type of the car property.
128      * <p>The area type could be one of the following:
129      * <ul>
130      *   <li>{@link VehicleAreaType#VEHICLE_AREA_TYPE_GLOBAL}</li>
131      *   <li>{@link VehicleAreaType#VEHICLE_AREA_TYPE_WINDOW}</li>
132      *   <li>{@link VehicleAreaType#VEHICLE_AREA_TYPE_SEAT}</li>
133      *   <li>{@link VehicleAreaType#VEHICLE_AREA_TYPE_DOOR}</li>
134      *   <li>{@link VehicleAreaType#VEHICLE_AREA_TYPE_MIRROR}</li>
135      *   <li>{@link VehicleAreaType#VEHICLE_AREA_TYPE_WHEEL}</li>
136      * </ul>
137      *
138      * @return the area type of the car property.
139      */
getAreaType()140     public @VehicleAreaTypeValue int getAreaType() {
141         return mAreaType;
142     }
143 
144     /**
145      * Return the change mode of the car property.
146      *
147      * <p>The change mode could be one of the following:
148      * <ul>
149      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_CHANGE_MODE_STATIC }</li>
150      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_CHANGE_MODE_ONCHANGE}</li>
151      *   <li>{@link CarPropertyConfig#VEHICLE_PROPERTY_CHANGE_MODE_CONTINUOUS}</li>
152      * </ul>
153      *
154      * @return the change mode of properties.
155      */
getChangeMode()156     public @VehiclePropertyChangeModeType int getChangeMode() {
157         return mChangeMode;
158     }
159 
160     /**
161      *
162      * @return Additional configuration parameters. For different properties, configArrays have
163      * different information.
164      */
165     @NonNull
getConfigArray()166     public List<Integer> getConfigArray() {
167         return Collections.unmodifiableList(mConfigArray);
168     }
169 
170     /**
171      *
172      * @return Some properties may require additional information passed over this
173      * string. Most properties do not need to set this.
174      * @hide
175      */
getConfigString()176     public String getConfigString() {
177         return mConfigString;
178     }
179 
180     /**
181      *
182      * @return Max sample rate in Hz. Must be defined for VehiclePropertyChangeMode::CONTINUOUS
183      * return 0 if change mode is not continuous.
184      */
getMaxSampleRate()185     public float getMaxSampleRate() {
186         return mMaxSampleRate;
187     }
188 
189     /**
190      *
191      * @return Min sample rate in Hz.Must be defined for VehiclePropertyChangeMode::CONTINUOUS
192      * return 0 if change mode is not continuous.
193      */
getMinSampleRate()194     public float getMinSampleRate() {
195         return mMinSampleRate;
196     }
197 
198     /**
199      * @return Property identifier
200      */
getPropertyId()201     public int getPropertyId() {
202         return mPropertyId;
203     }
204 
205     /**
206      * @return Value type of VehicleProperty.
207      * @hide
208      */
209     @SystemApi
210     @NonNull
getPropertyType()211     public Class<T> getPropertyType() {
212         return mType;
213     }
214 
215     /**
216      *
217      * @return true if this property doesn't hold car area-specific configuration.
218      */
isGlobalProperty()219     public boolean isGlobalProperty() {
220         return mAreaType == VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
221     }
222 
223     /**
224      *
225      * @return the number of areaIds for properties.
226      * @hide
227      */
getAreaCount()228     public int getAreaCount() {
229         return mSupportedAreas.size();
230     }
231 
232     /**
233      *
234      * @return Array of areaIds. An AreaID is a combination of one or more areas,
235      * and is represented using a bitmask of Area enums. Different AreaTypes may
236      * not be mixed in a single AreaID. For instance, a window area cannot be
237      * combined with a seat area in an AreaID.
238      * Rules for mapping a zoned property to AreaIDs:
239      *  - A property must be mapped to an array of AreaIDs that are impacted when
240      *    the property value changes.
241      *  - Each element in the array must represent an AreaID, in which, the
242      *    property value can only be changed together in all the areas within
243      *    an AreaID and never independently. That is, when the property value
244      *    changes in one of the areas in an AreaID in the array, then it must
245      *    automatically change in all other areas in the AreaID.
246      *  - The property value must be independently controllable in any two
247      *    different AreaIDs in the array.
248      *  - An area must only appear once in the array of AreaIDs. That is, an
249      *    area must only be part of a single AreaID in the array.
250      */
251     @NonNull
getAreaIds()252     public int[] getAreaIds() {
253         int[] areaIds = new int[mSupportedAreas.size()];
254         for (int i = 0; i < areaIds.length; i++) {
255             areaIds[i] = mSupportedAreas.keyAt(i);
256         }
257         return areaIds;
258     }
259 
260     /**
261      * @return  the first areaId.
262      * Throws {@link IllegalStateException} if supported area count not equals to one.
263      * @hide
264      */
getFirstAndOnlyAreaId()265     public int getFirstAndOnlyAreaId() {
266         if (mSupportedAreas.size() != 1) {
267             throw new IllegalStateException("Expected one and only area in this property. Prop: 0x"
268                     + Integer.toHexString(mPropertyId));
269         }
270         return mSupportedAreas.keyAt(0);
271     }
272 
273     /**
274      *
275      * @param areaId
276      * @return true if areaId is existing.
277      * @hide
278      */
hasArea(int areaId)279     public boolean hasArea(int areaId) {
280         return mSupportedAreas.indexOfKey(areaId) >= 0;
281     }
282 
283     /**
284      *
285      * @param areaId
286      * @return Min value in given areaId. Null if not have min value in given area.
287      */
288     @Nullable
getMinValue(int areaId)289     public T getMinValue(int areaId) {
290         AreaConfig<T> area = mSupportedAreas.get(areaId);
291         return area == null ? null : area.getMinValue();
292     }
293 
294     /**
295      *
296      * @param areaId
297      * @return Max value in given areaId. Null if not have max value in given area.
298      */
299     @Nullable
getMaxValue(int areaId)300     public T getMaxValue(int areaId) {
301         AreaConfig<T> area = mSupportedAreas.get(areaId);
302         return area == null ? null : area.getMaxValue();
303     }
304 
305     /**
306      *
307      * @return Min value in areaId 0. Null if not have min value.
308      */
309     @Nullable
getMinValue()310     public T getMinValue() {
311         AreaConfig<T> area = mSupportedAreas.get(0);
312         return area == null ? null : area.getMinValue();
313     }
314 
315     /**
316      *
317      * @return Max value in areaId 0. Null if not have max value.
318      */
319     @Nullable
getMaxValue()320     public T getMaxValue() {
321         AreaConfig<T> area = mSupportedAreas.get(0);
322         return area == null ? null : area.getMaxValue();
323     }
324 
325     @Override
describeContents()326     public int describeContents() {
327         return 0;
328     }
329 
330 
331     @Override
writeToParcel(Parcel dest, int flags)332     public void writeToParcel(Parcel dest, int flags) {
333         dest.writeInt(mAccess);
334         dest.writeInt(mAreaType);
335         dest.writeInt(mChangeMode);
336         dest.writeInt(mConfigArray.size());
337         for (int i = 0; i < mConfigArray.size(); i++) {
338             dest.writeInt(mConfigArray.get(i));
339         }
340         dest.writeString(mConfigString);
341         dest.writeFloat(mMaxSampleRate);
342         dest.writeFloat(mMinSampleRate);
343         dest.writeInt(mPropertyId);
344         dest.writeInt(mSupportedAreas.size());
345         for (int i = 0; i < mSupportedAreas.size(); i++) {
346             dest.writeInt(mSupportedAreas.keyAt(i));
347             dest.writeParcelable(mSupportedAreas.valueAt(i), flags);
348         }
349         dest.writeString(mType.getName());
350     }
351 
352     @SuppressWarnings("unchecked")
CarPropertyConfig(Parcel in)353     private CarPropertyConfig(Parcel in) {
354         mAccess = in.readInt();
355         mAreaType = in.readInt();
356         mChangeMode = in.readInt();
357         int configArraySize = in.readInt();
358         mConfigArray = new ArrayList<Integer>(configArraySize);
359         for (int i = 0; i < configArraySize; i++) {
360             mConfigArray.add(in.readInt());
361         }
362         mConfigString = in.readString();
363         mMaxSampleRate = in.readFloat();
364         mMinSampleRate = in.readFloat();
365         mPropertyId = in.readInt();
366         int areaSize = in.readInt();
367         mSupportedAreas = new SparseArray<>(areaSize);
368         for (int i = 0; i < areaSize; i++) {
369             int areaId = in.readInt();
370             AreaConfig<T> area = in.readParcelable(getClass().getClassLoader());
371             mSupportedAreas.put(areaId, area);
372         }
373         String className = in.readString();
374         try {
375             mType = (Class<T>) Class.forName(className);
376         } catch (ClassNotFoundException e) {
377             throw new IllegalArgumentException("Class not found: " + className);
378         }
379     }
380 
381     public static final Creator<CarPropertyConfig> CREATOR = new Creator<CarPropertyConfig>() {
382         @Override
383         public CarPropertyConfig createFromParcel(Parcel in) {
384             return new CarPropertyConfig(in);
385         }
386 
387         @Override
388         public CarPropertyConfig[] newArray(int size) {
389             return new CarPropertyConfig[size];
390         }
391     };
392 
393     /** @hide */
394     @Override
toString()395     public String toString() {
396         return "CarPropertyConfig{"
397                 + "mPropertyId=" + mPropertyId
398                 + ", mAccess=" + mAccess
399                 + ", mAreaType=" + mAreaType
400                 + ", mChangeMode=" + mChangeMode
401                 + ", mConfigArray=" + mConfigArray
402                 + ", mConfigString=" + mConfigString
403                 + ", mMaxSampleRate=" + mMaxSampleRate
404                 + ", mMinSampleRate=" + mMinSampleRate
405                 + ", mSupportedAreas=" + mSupportedAreas
406                 + ", mType=" + mType
407                 + '}';
408     }
409 
410     /**
411      * Represents min/max value of car property.
412      * @param <T>
413      * @hide
414      */
415     public static class AreaConfig<T> implements Parcelable {
416         @Nullable private final T mMinValue;
417         @Nullable private final T mMaxValue;
418 
AreaConfig(T minValue, T maxValue)419         private AreaConfig(T minValue, T maxValue) {
420             mMinValue = minValue;
421             mMaxValue = maxValue;
422         }
423 
424         public static final Parcelable.Creator<AreaConfig<Object>> CREATOR
425                 = getCreator(Object.class);
426 
getCreator(final Class<E> clazz)427         private static <E> Parcelable.Creator<AreaConfig<E>> getCreator(final Class<E> clazz) {
428             return new Creator<AreaConfig<E>>() {
429                 @Override
430                 public AreaConfig<E> createFromParcel(Parcel source) {
431                     return new AreaConfig<>(source);
432                 }
433 
434                 @Override @SuppressWarnings("unchecked")
435                 public AreaConfig<E>[] newArray(int size) {
436                     return (AreaConfig<E>[]) Array.newInstance(clazz, size);
437                 }
438             };
439         }
440 
441         @SuppressWarnings("unchecked")
AreaConfig(Parcel in)442         private AreaConfig(Parcel in) {
443             mMinValue = (T) in.readValue(getClass().getClassLoader());
444             mMaxValue = (T) in.readValue(getClass().getClassLoader());
445         }
446 
getMinValue()447         @Nullable public T getMinValue() { return mMinValue; }
getMaxValue()448         @Nullable public T getMaxValue() { return mMaxValue; }
449 
450         @Override
describeContents()451         public int describeContents() {
452             return 0;
453         }
454 
455         @Override
writeToParcel(Parcel dest, int flags)456         public void writeToParcel(Parcel dest, int flags) {
457             dest.writeValue(mMinValue);
458             dest.writeValue(mMaxValue);
459         }
460 
461         @Override
toString()462         public String toString() {
463             return "CarAreaConfig{" +
464                     "mMinValue=" + mMinValue +
465                     ", mMaxValue=" + mMaxValue +
466                     '}';
467         }
468     }
469 
470     /**
471      * Prepare an instance of CarPropertyConfig
472      *
473      * @return Builder<T>
474      * @hide
475      */
476     @SystemApi
477     public static <T> Builder<T> newBuilder(Class<T> type, int propertyId, int areaType,
478                                             int areaCapacity) {
479         return new Builder<>(areaCapacity, areaType, propertyId, type);
480     }
481 
482 
483     /**
484      * Prepare an instance of CarPropertyConfig
485      *
486      * @return Builder<T>
487      * @hide
488      */
489     public static <T> Builder<T> newBuilder(Class<T> type, int propertyId, int areaType) {
490         return new Builder<>(0, areaType, propertyId, type);
491     }
492 
493 
494     /**
495      * @param <T>
496      * @hide
497      * */
498     @SystemApi
499     public static class Builder<T> {
500         private int mAccess;
501         private final int mAreaType;
502         private int mChangeMode;
503         private final ArrayList<Integer> mConfigArray;
504         private String mConfigString;
505         private float mMaxSampleRate;
506         private float mMinSampleRate;
507         private final int mPropertyId;
508         private final SparseArray<AreaConfig<T>> mSupportedAreas;
509         private final Class<T> mType;
510 
511         private Builder(int areaCapacity, int areaType, int propertyId, Class<T> type) {
512             mAreaType = areaType;
513             mConfigArray = new ArrayList<>();
514             mPropertyId = propertyId;
515             if (areaCapacity != 0) {
516                 mSupportedAreas = new SparseArray<>(areaCapacity);
517             } else {
518                 mSupportedAreas = new SparseArray<>();
519             }
520             mType = type;
521         }
522 
523         /**
524          * Add supported areas parameter to CarPropertyConfig
525          *
526          * @return Builder<T>
527          */
528         public Builder<T> addAreas(int[] areaIds) {
529             for (int id : areaIds) {
530                 mSupportedAreas.put(id, null);
531             }
532             return this;
533         }
534 
535         /**
536          * Add area to CarPropertyConfig
537          *
538          * @return Builder<T>
539          */
540         public Builder<T> addArea(int areaId) {
541             return addAreaConfig(areaId, null, null);
542         }
543 
544         /**
545          * Add areaConfig to CarPropertyConfig
546          *
547          * @return Builder<T>
548          */
549         public Builder<T> addAreaConfig(int areaId, T min, T max) {
550             if (!isRangeAvailable(min, max)) {
551                 mSupportedAreas.put(areaId, null);
552             } else {
553                 mSupportedAreas.put(areaId, new AreaConfig<>(min, max));
554             }
555             return this;
556         }
557 
558         /**
559          * Set access parameter to CarPropertyConfig
560          *
561          * @return Builder<T>
562          */
563         public Builder<T> setAccess(int access) {
564             mAccess = access;
565             return this;
566         }
567 
568         /**
569          * Set changeMode parameter to CarPropertyConfig
570          *
571          * @return Builder<T>
572          */
573         public Builder<T> setChangeMode(int changeMode) {
574             mChangeMode = changeMode;
575             return this;
576         }
577 
578         /**
579          * Set configArray parameter to CarPropertyConfig
580          *
581          * @return Builder<T>
582          */
583         public Builder<T> setConfigArray(ArrayList<Integer> configArray) {
584             mConfigArray.clear();
585             mConfigArray.addAll(configArray);
586             return this;
587         }
588 
589         /**
590          * Set configString parameter to CarPropertyConfig
591          *
592          * @return Builder<T>
593          */
594         public Builder<T> setConfigString(String configString) {
595             mConfigString = configString;
596             return this;
597         }
598 
599         /**
600          * Set maxSampleRate parameter to CarPropertyConfig
601          *
602          * @return Builder<T>
603          */
604         public Builder<T> setMaxSampleRate(float maxSampleRate) {
605             mMaxSampleRate = maxSampleRate;
606             return this;
607         }
608 
609         /**
610          * Set minSampleRate parameter to CarPropertyConfig
611          *
612          * @return Builder<T>
613          */
614         public Builder<T> setMinSampleRate(float minSampleRate) {
615             mMinSampleRate = minSampleRate;
616             return this;
617         }
618 
619         public CarPropertyConfig<T> build() {
620             return new CarPropertyConfig<>(mAccess, mAreaType, mChangeMode, mConfigArray,
621                                            mConfigString, mMaxSampleRate, mMinSampleRate,
622                                            mPropertyId, mSupportedAreas, mType);
623         }
624 
625         private boolean isRangeAvailable(T min, T max) {
626             if (min == null || max == null) {
627                 return false;
628             }
629             int propertyType = mPropertyId & VehiclePropertyType.MASK;
630             switch (propertyType) {
631                 case VehiclePropertyType.INT32:
632                     return (Integer) min  != 0 || (Integer) max != 0;
633                 case VehiclePropertyType.INT64:
634                     return (Long) min != 0L || (Long) max != 0L;
635                 case VehiclePropertyType.FLOAT:
636                     return (Float) min != 0f || (Float) max != 0f;
637                 default:
638                     return false;
639             }
640         }
641     }
642 }
643