1 /*
2  * Copyright (C) 2018 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.example.android.systemupdatersample.util;
18 
19 import android.os.UpdateEngine;
20 import android.util.SparseArray;
21 
22 import java.util.Arrays;
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 /**
27  * Helper class to work with update_engine's error codes.
28  * Many error codes are defined in  {@link UpdateEngine.ErrorCodeConstants},
29  * but you can find more in system/update_engine/common/error_code.h.
30  */
31 public final class UpdateEngineErrorCodes {
32 
33     /**
34     * Error code from the update engine. Values must agree with the ones in
35     * system/update_engine/common/error_code.h.
36     */
37     public static final int UNKNOWN = -1;
38     public static final int UPDATED_BUT_NOT_ACTIVE = 52;
39     public static final int USER_CANCELLED = 48;
40 
41     private static final SparseArray<String> CODE_TO_NAME_MAP = new SparseArray<>();
42 
43     static {
44         CODE_TO_NAME_MAP.put(0, "SUCCESS");
45         CODE_TO_NAME_MAP.put(1, "ERROR");
46         CODE_TO_NAME_MAP.put(4, "FILESYSTEM_COPIER_ERROR");
47         CODE_TO_NAME_MAP.put(5, "POST_INSTALL_RUNNER_ERROR");
48         CODE_TO_NAME_MAP.put(6, "PAYLOAD_MISMATCHED_TYPE_ERROR");
49         CODE_TO_NAME_MAP.put(7, "INSTALL_DEVICE_OPEN_ERROR");
50         CODE_TO_NAME_MAP.put(8, "KERNEL_DEVICE_OPEN_ERROR");
51         CODE_TO_NAME_MAP.put(9, "DOWNLOAD_TRANSFER_ERROR");
52         CODE_TO_NAME_MAP.put(10, "PAYLOAD_HASH_MISMATCH_ERROR");
53         CODE_TO_NAME_MAP.put(11, "PAYLOAD_SIZE_MISMATCH_ERROR");
54         CODE_TO_NAME_MAP.put(12, "DOWNLOAD_PAYLOAD_VERIFICATION_ERROR");
55         CODE_TO_NAME_MAP.put(15, "NEW_ROOTFS_VERIFICATION_ERROR");
56         CODE_TO_NAME_MAP.put(20, "DOWNLOAD_STATE_INITIALIZATION_ERROR");
57         CODE_TO_NAME_MAP.put(26, "DOWNLOAD_METADATA_SIGNATURE_MISMATCH");
58         CODE_TO_NAME_MAP.put(48, "USER_CANCELLED");
59         CODE_TO_NAME_MAP.put(52, "UPDATED_BUT_NOT_ACTIVE");
60     }
61 
62     /**
63      * Completion codes returned by update engine indicating that the update
64      * was successfully applied.
65      */
66     private static final Set<Integer> SUCCEEDED_COMPLETION_CODES = new HashSet<>(
67             Arrays.asList(UpdateEngine.ErrorCodeConstants.SUCCESS,
68                     // UPDATED_BUT_NOT_ACTIVE is returned when the payload is
69                     // successfully applied but the
70                     // device won't switch to the new slot after the next boot.
71                     UPDATED_BUT_NOT_ACTIVE));
72 
73     /**
74      * checks if update succeeded using errorCode
75      */
isUpdateSucceeded(int errorCode)76     public static boolean isUpdateSucceeded(int errorCode) {
77         return SUCCEEDED_COMPLETION_CODES.contains(errorCode);
78     }
79 
80     /**
81      * converts error code to error name
82      */
getCodeName(int errorCode)83     public static String getCodeName(int errorCode) {
84         return CODE_TO_NAME_MAP.get(errorCode);
85     }
86 
UpdateEngineErrorCodes()87     private UpdateEngineErrorCodes() {}
88 }
89