1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16  /**
17   * @file errors.h
18   *
19   * @brief Provide format of error code in OpenHarmony.
20   */
21 
22 #ifndef UTILS_BASE_ERRORS_H
23 #define UTILS_BASE_ERRORS_H
24 
25 #include <cerrno>
26 
27 namespace OHOS {
28 
29 /**
30  * ErrCode layout
31  *
32  * +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
33  * | Bit |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|09|08|07|06|05|04|03|02|01|00|
34  * +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
35  * |Field|Reserved|        Subsystem      |  Module      |                  Code                         |
36  * +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
37  */
38 
39 using ErrCode = int;
40 
41 /**
42  * @brief Value of 'Subsystem' segment of ErrCode for every subsystem.
43  */
44 enum {
45     SUBSYS_COMMON                = 0,
46     SUBSYS_AAFWK                 = 1,
47     SUBSYS_ACCOUNT               = 2,
48     SUBSYS_AI                    = 3,
49     SUBSYS_APPEXECFWK            = 4,
50     SUBSYS_APPLICATIONS          = 5,
51     SUBSYS_ARVR                  = 6,
52     SUBSYS_ARVRHARDWARE          = 7,
53     SUBSYS_BARRIERFREE           = 8,
54     SUBSYS_BIOMETRICS            = 9,
55     SUBSYS_CCRUNTIME             = 10,
56     SUBSYS_COMMUNICATION         = 11,
57     SUBSYS_DFX                   = 12,
58     SUBSYS_DISTRIBUTEDDATAMNG    = 13,
59     SUBSYS_DISTRIBUTEDSCHEDULE   = 14,
60     SUBSYS_DRIVERS               = 15,
61     SUBSYS_GLOBAL                = 16,
62     SUBSYS_GRAPHIC               = 17,
63     SUBSYS_HBS                   = 18,
64     SUBSYS_IAWARE                = 19,
65     SUBSYS_IDE                   = 20,
66     SUBSYS_INTELLIACCESSORIES    = 21,
67     SUBSYS_INTELLISPEAKER        = 22,
68     SUBSYS_INTELLITV             = 23,
69     SUBSYS_IOT                   = 24,
70     SUBSYS_IOTHARDWARE           = 25,
71     SUBSYS_IVIHARDWARE           = 26,
72     SUBSYS_KERNEL                = 27,
73     SUBSYS_LOCATION              = 28,
74     SUBSYS_MSDP                  = 29,
75     SUBSYS_MULTIMEDIA            = 30,
76     SUBSYS_MULTIMODAINPUT        = 31,
77     SUBSYS_NOTIFICATION          = 32,
78     SUBSYS_POWERMNG              = 33,
79     SUBSYS_ROUTER                = 34,
80     SUBSYS_SECURITY              = 35,
81     SUBSYS_SENSORS               = 36,
82     SUBSYS_SMALLSERVICES         = 37,
83     SUBSYS_SOURCECODETRANSFORMER = 38,
84     SUBSYS_STARTUP               = 39,
85     SUBSYS_TELEPONY              = 40,
86     SUBSYS_UPDATE                = 41,
87     SUBSYS_USB                   = 42,
88     SUBSYS_WEARABLE              = 43,
89     SUBSYS_WEARABLEHARDWARE      = 44,
90     SUBSYS_IVI                   = 45,
91     SUBSYS_DISTRIBUTEDHARDWARE   = 46,
92     SUBSYS_DEVICEPROFILE         = 47,
93     SUBSYS_CUSTOMIZATION         = 48,
94     SUBSYS_FILEMANAGEMENT        = 49,
95     // new type
96 };
97 
98 // be used to init the subsystem errorno.
99 /**
100  * @brief Generate base error code for a specified module in specified
101  * subsystem.
102  *
103  * @param subsystem Value of 'Subsystem' segment.
104  * @param module Value of 'Module' segment,
105  * which is 0 by default.
106  * @return Return base ErrCode for specified module.
107  */
108 constexpr ErrCode ErrCodeOffset(unsigned int subsystem, unsigned int module = 0)
109 {
110     constexpr int SUBSYSTEM_BIT_NUM = 21;
111     constexpr int MODULE_BIT_NUM = 16;
112     return (subsystem << SUBSYSTEM_BIT_NUM) | (module << MODULE_BIT_NUM);
113 }
114 
115 // offset of common error, only be used in this file.
116 /**
117  * @brief Base ErrCode of common(valid for all modules)
118  * in commonlibrary subsystem.
119  */
120 constexpr ErrCode BASE_ERR_OFFSET = ErrCodeOffset(SUBSYS_COMMON);
121 
122 /**
123  * @brief Value of common 'Code' segment of ErrCode
124  * in commonlibrary subsystem.
125  *
126  * @see Related error codes defined in errno.h
127  */
128 enum {
129     ERR_OK                = 0,
130     ERR_NO_MEMORY         = BASE_ERR_OFFSET + ENOMEM,
131     ERR_INVALID_OPERATION = BASE_ERR_OFFSET + ENOSYS,
132     ERR_INVALID_VALUE     = BASE_ERR_OFFSET + EINVAL,
133     ERR_NAME_NOT_FOUND    = BASE_ERR_OFFSET + ENOENT,
134     ERR_PERMISSION_DENIED = BASE_ERR_OFFSET + EPERM,
135     ERR_NO_INIT           = BASE_ERR_OFFSET + ENODEV,
136     ERR_ALREADY_EXISTS    = BASE_ERR_OFFSET + EEXIST,
137     ERR_DEAD_OBJECT       = BASE_ERR_OFFSET + EPIPE,
138     ERR_OVERFLOW          = BASE_ERR_OFFSET + EOVERFLOW,
139     ERR_ENOUGH_DATA       = BASE_ERR_OFFSET + ENODATA,
140     ERR_WOULD_BLOCK       = BASE_ERR_OFFSET + EWOULDBLOCK,
141     ERR_TIMED_OUT         = BASE_ERR_OFFSET + ETIMEDOUT
142 };
143 
144 }
145 
146 #endif
147