1 /*
2  * Copyright (C) 2024 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.car.internal.property;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SuppressLint;
22 import android.car.hardware.property.CarPropertyManager;
23 import android.car.hardware.property.VehicleHalStatusCode;
24 import android.car.hardware.property.VehicleHalStatusCode.VehicleHalStatusCodeInt;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import com.android.car.internal.util.AnnotationValidations;
29 import com.android.car.internal.util.DataClass;
30 
31 import java.util.StringJoiner;
32 
33 /**
34  * Stores the various error codes for vehicle properties as they get passed up
35  * the stack.
36  *
37  * @hide
38  */
39 @DataClass(genConstructor = false)
40 public final class CarPropertyErrorCodes implements Parcelable {
41 
42     /**
43      * Status indicating no error.
44      *
45      * <p>This is not exposed to the client as this will be used only for deciding
46      * {@link GetPropertyCallback#onSuccess} or {@link GetPropertyCallback#onFailure} is called.
47      */
48     public static final int STATUS_OK = 0;
49     public static final int STATUS_TRY_AGAIN = -1;
50 
51     private static final int SYSTEM_ERROR_CODE_MASK = 0xffff;
52     private static final int VENDOR_ERROR_CODE_SHIFT = 16;
53 
54     /**
55      * CarPropertyErrorCodes with no errors.
56      */
57     public static CarPropertyErrorCodes STATUS_OK_NO_ERROR =
58             new CarPropertyErrorCodes(STATUS_OK, /* vendorErrorCode= */ 0, /* systemErrorCode */ 0);
59 
60     /**
61      * This is same as {@link CarPropertyAsyncErrorCode} except that it contains
62      * {@code STATUS_TRY_AGAIN}.
63      */
64     @IntDef(prefix = {"STATUS_"}, value = {
65             STATUS_OK,
66             CarPropertyManager.STATUS_ERROR_INTERNAL_ERROR,
67             CarPropertyManager.STATUS_ERROR_NOT_AVAILABLE,
68             CarPropertyManager.STATUS_ERROR_TIMEOUT,
69             STATUS_TRY_AGAIN
70     })
71     public @interface CarPropMgrErrorCode {}
72 
73     private @CarPropMgrErrorCode int mCarPropertyManagerErrorCode;
74     private int mVendorErrorCode;
75     private int mSystemErrorCode;
76 
77     /**
78      * Create an instance of CarPropertyErrorCodes given a car property manager error code, a vendor
79      * error code, and a system error code.
80      */
CarPropertyErrorCodes(@arPropMgrErrorCode int carPropertyManagerErrorCode, int vendorErrorCode, int systemErrorCode)81     public CarPropertyErrorCodes(@CarPropMgrErrorCode int carPropertyManagerErrorCode,
82             int vendorErrorCode, int systemErrorCode) {
83         mCarPropertyManagerErrorCode = carPropertyManagerErrorCode;
84         mVendorErrorCode = vendorErrorCode;
85         mSystemErrorCode = systemErrorCode;
86     }
87 
88     /**
89      * Get the errors returned by CarPropertyManager as defined by @link CarPropMgrErrorCode}.
90      */
getCarPropertyManagerErrorCode()91     public @CarPropMgrErrorCode int getCarPropertyManagerErrorCode() {
92         return mCarPropertyManagerErrorCode;
93     }
94 
95     /**
96      * Get the vendor specified error code to allow for more detailed error codes.
97      *
98      * A vendor error code will have a range from 0x0000 to 0xffff.
99      *
100      * @return the vendor error code if it is set, otherwise 0.
101      */
getVendorErrorCode()102     public int getVendorErrorCode() {
103         return mVendorErrorCode;
104     }
105 
106     /**
107      * Get the system error code.
108      *
109      * A system error code will have a range from 0x0000 to 0xffff.
110      */
getSystemErrorCode()111     public int getSystemErrorCode() {
112         return mSystemErrorCode;
113     }
114 
115     /**
116      * Converts a StatusCode from VHAL to error code used by CarPropertyManager.
117      */
convertVhalStatusCodeToCarPropertyManagerErrorCodes( int errorCode)118     public static CarPropertyErrorCodes convertVhalStatusCodeToCarPropertyManagerErrorCodes(
119             int errorCode) {
120         @VehicleHalStatusCodeInt int systemErrorCode = getVhalSystemErrorCode(errorCode);
121         @CarPropMgrErrorCode int carPropertyManagerErrorCode = STATUS_OK;
122 
123         switch (systemErrorCode) {
124             case VehicleHalStatusCode.STATUS_OK:
125                 break;
126             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE: // Fallthrough
127             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_DISABLED: // Fallthrough
128             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_SPEED_LOW: // Fallthrough
129             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_SPEED_HIGH: // Fallthrough
130             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_POOR_VISIBILITY: // Fallthrough
131             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_SAFETY:
132                 carPropertyManagerErrorCode = CarPropertyManager.STATUS_ERROR_NOT_AVAILABLE;
133                 break;
134             case VehicleHalStatusCode.STATUS_TRY_AGAIN:
135                 carPropertyManagerErrorCode = STATUS_TRY_AGAIN;
136                 break;
137             default:
138                 carPropertyManagerErrorCode = CarPropertyManager.STATUS_ERROR_INTERNAL_ERROR;
139                 break;
140         }
141 
142         CarPropertyErrorCodes errorCodes = new CarPropertyErrorCodes(
143                 carPropertyManagerErrorCode, getVhalVendorErrorCode(errorCode), systemErrorCode);
144 
145         return errorCodes;
146     }
147 
148     /**
149      * Returns the system error code contained in the error code returned from VHAL.
150      */
151     @SuppressLint("WrongConstant")
getVhalSystemErrorCode(int vhalErrorCode)152     public static @VehicleHalStatusCodeInt int getVhalSystemErrorCode(int vhalErrorCode) {
153         return vhalErrorCode & SYSTEM_ERROR_CODE_MASK;
154     }
155 
156     /**
157      * Returns the vendor error code contained in the error code returned from VHAL.
158      */
getVhalVendorErrorCode(int vhalErrorCode)159     public static int getVhalVendorErrorCode(int vhalErrorCode) {
160         return vhalErrorCode >>> VENDOR_ERROR_CODE_SHIFT;
161     }
162 
163     /**
164      * Returns {@code true} if {@code vehicleHalStatusCode} is one of the not available
165      * {@link VehicleHalStatusCode} values}. Otherwise returns {@code false}.
166      */
isNotAvailableVehicleHalStatusCode( @ehicleHalStatusCodeInt int vehicleHalStatusCode)167     public static boolean isNotAvailableVehicleHalStatusCode(
168             @VehicleHalStatusCodeInt int vehicleHalStatusCode) {
169         switch (vehicleHalStatusCode) {
170             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE:
171             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_DISABLED:
172             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_SPEED_LOW:
173             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_SPEED_HIGH:
174             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_POOR_VISIBILITY:
175             case VehicleHalStatusCode.STATUS_NOT_AVAILABLE_SAFETY:
176                 return true;
177             default:
178                 return false;
179         }
180     }
181 
182     /**
183      * Returns a string representation of a {@code CarPropertyErrorCodes}.
184      */
185     @NonNull
toString(CarPropertyErrorCodes carPropertyErrorCodes)186     public static String toString(CarPropertyErrorCodes carPropertyErrorCodes) {
187         var sj = new StringJoiner(", ", "CarPropertyErrorCodes{", "}");
188         sj.add("cpmErrorCode: " + carPropertyErrorCodes.getCarPropertyManagerErrorCode());
189         sj.add("vendorErrorCode: " + carPropertyErrorCodes.getVendorErrorCode());
190         sj.add("systemErrorCode: " + carPropertyErrorCodes.getSystemErrorCode());
191         return sj.toString();
192     }
193 
194 
195 
196     // Code below generated by codegen v1.0.23.
197     //
198     // DO NOT MODIFY!
199     // CHECKSTYLE:OFF Generated code
200     //
201     // To regenerate run:
202     // $ codegen $ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/com/android/car/internal/property/CarPropertyErrorCodes.java
203     //
204     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
205     //   Settings > Editor > Code Style > Formatter Control
206     //@formatter:off
207 
208 
209     @IntDef(prefix = "STATUS_", value = {
210         STATUS_OK,
211         STATUS_TRY_AGAIN
212     })
213     @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE)
214     @DataClass.Generated.Member
215     public @interface Status {}
216 
217     @DataClass.Generated.Member
statusToString(@tatus int value)218     public static String statusToString(@Status int value) {
219         switch (value) {
220             case STATUS_OK:
221                     return "STATUS_OK";
222             case STATUS_TRY_AGAIN:
223                     return "STATUS_TRY_AGAIN";
224             default: return Integer.toHexString(value);
225         }
226     }
227 
228     @DataClass.Generated.Member
setCarPropertyManagerErrorCode(@arPropMgrErrorCode int value)229     public @NonNull CarPropertyErrorCodes setCarPropertyManagerErrorCode(@CarPropMgrErrorCode int value) {
230         mCarPropertyManagerErrorCode = value;
231         AnnotationValidations.validate(
232                 CarPropMgrErrorCode.class, null, mCarPropertyManagerErrorCode);
233         return this;
234     }
235 
236     @DataClass.Generated.Member
setVendorErrorCode( int value)237     public @NonNull CarPropertyErrorCodes setVendorErrorCode( int value) {
238         mVendorErrorCode = value;
239         return this;
240     }
241 
242     @DataClass.Generated.Member
setSystemErrorCode( int value)243     public @NonNull CarPropertyErrorCodes setSystemErrorCode( int value) {
244         mSystemErrorCode = value;
245         return this;
246     }
247 
248     @Override
249     @DataClass.Generated.Member
writeToParcel(@onNull Parcel dest, int flags)250     public void writeToParcel(@NonNull Parcel dest, int flags) {
251         // You can override field parcelling by defining methods like:
252         // void parcelFieldName(Parcel dest, int flags) { ... }
253 
254         dest.writeInt(mCarPropertyManagerErrorCode);
255         dest.writeInt(mVendorErrorCode);
256         dest.writeInt(mSystemErrorCode);
257     }
258 
259     @Override
260     @DataClass.Generated.Member
describeContents()261     public int describeContents() { return 0; }
262 
263     /** @hide */
264     @SuppressWarnings({"unchecked", "RedundantCast"})
265     @DataClass.Generated.Member
CarPropertyErrorCodes(@onNull Parcel in)266     /* package-private */ CarPropertyErrorCodes(@NonNull Parcel in) {
267         // You can override field unparcelling by defining methods like:
268         // static FieldType unparcelFieldName(Parcel in) { ... }
269 
270         int carPropertyManagerErrorCode = in.readInt();
271         int vendorErrorCode = in.readInt();
272         int systemErrorCode = in.readInt();
273 
274         this.mCarPropertyManagerErrorCode = carPropertyManagerErrorCode;
275         AnnotationValidations.validate(
276                 CarPropMgrErrorCode.class, null, mCarPropertyManagerErrorCode);
277         this.mVendorErrorCode = vendorErrorCode;
278         this.mSystemErrorCode = systemErrorCode;
279 
280         // onConstructed(); // You can define this method to get a callback
281     }
282 
283     @DataClass.Generated.Member
284     public static final @NonNull Parcelable.Creator<CarPropertyErrorCodes> CREATOR
285             = new Parcelable.Creator<CarPropertyErrorCodes>() {
286         @Override
287         public CarPropertyErrorCodes[] newArray(int size) {
288             return new CarPropertyErrorCodes[size];
289         }
290 
291         @Override
292         public CarPropertyErrorCodes createFromParcel(@NonNull Parcel in) {
293             return new CarPropertyErrorCodes(in);
294         }
295     };
296 
297     @DataClass.Generated(
298             time = 1708039674741L,
299             codegenVersion = "1.0.23",
300             sourceFile = "packages/services/Car/car-lib/src/com/android/car/internal/property/CarPropertyErrorCodes.java",
301             inputSignatures = "public static final  int STATUS_OK\npublic static final  int STATUS_TRY_AGAIN\nprivate static final  int SYSTEM_ERROR_CODE_MASK\nprivate static final  int VENDOR_ERROR_CODE_SHIFT\npublic static  com.android.car.internal.property.CarPropertyErrorCodes STATUS_OK_NO_ERROR\nprivate @com.android.car.internal.property.CarPropertyErrorCodes.CarPropMgrErrorCode int mCarPropertyManagerErrorCode\nprivate  int mVendorErrorCode\nprivate  int mSystemErrorCode\npublic @com.android.car.internal.property.CarPropertyErrorCodes.CarPropMgrErrorCode int getCarPropertyManagerErrorCode()\npublic  int getVendorErrorCode()\npublic  int getSystemErrorCode()\npublic static  com.android.car.internal.property.CarPropertyErrorCodes convertVhalStatusCodeToCarPropertyManagerErrorCodes(int)\npublic static @android.annotation.SuppressLint @android.car.hardware.property.VehicleHalStatusCode.VehicleHalStatusCodeInt int getVhalSystemErrorCode(int)\npublic static  int getVhalVendorErrorCode(int)\npublic static  boolean isNotAvailableVehicleHalStatusCode(int)\npublic static @android.annotation.NonNull java.lang.String toString(com.android.car.internal.property.CarPropertyErrorCodes)\nclass CarPropertyErrorCodes extends java.lang.Object implements [android.os.Parcelable]\n@com.android.car.internal.util.DataClass(genConstructor=false)")
302     @Deprecated
__metadata()303     private void __metadata() {}
304 
305 
306     //@formatter:on
307     // End of generated code
308 
309 }
310