1 /*
2  * Copyright (c) 2022 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 #ifndef UTILS_BASE_ERRORS_H
17 #define UTILS_BASE_ERRORS_H
18 
19 #include <errno.h>
20 namespace OHOS {
21 /**
22  * ErrCode layout
23  *
24  * +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
25  * | 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|
26  * +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
27  * |Field|Reserved|        Subsystem      |  Module      |                  Code                         |
28  * +-----+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
29  */
30 
31 using ErrCode = int;
32 
33 enum {
34     SUBSYS_COMMON                = 0,
35     SUBSYS_AAFWK                 = 1,
36     SUBSYS_ACCOUNT               = 2,
37     SUBSYS_AI                    = 3,
38     SUBSYS_APPEXECFWK            = 4,
39     SUBSYS_APPLICATIONS          = 5,
40     SUBSYS_ARVR                  = 6,
41     SUBSYS_ARVRHARDWARE          = 7,
42     SUBSYS_BARRIERFREE           = 8,
43     SUBSYS_BIOMETRICS            = 9,
44     SUBSYS_CCRUNTIME             = 10,
45     SUBSYS_COMMUNICATION         = 11,
46     SUBSYS_DFX                   = 12,
47     SUBSYS_DISTRIBUTEDDATAMNG    = 13,
48     SUBSYS_DISTRIBUTEDSCHEDULE   = 14,
49     SUBSYS_DRIVERS               = 15,
50     SUBSYS_GLOBAL                = 16,
51     SUBSYS_GRAPHIC               = 17,
52     SUBSYS_HBS                   = 18,
53     SUBSYS_IAWARE                = 19,
54     SUBSYS_IDE                   = 20,
55     SUBSYS_INTELLIACCESSORIES    = 21,
56     SUBSYS_INTELLISPEAKER        = 22,
57     SUBSYS_INTELLITV             = 23,
58     SUBSYS_IOT                   = 24,
59     SUBSYS_IOTHARDWARE           = 25,
60     SUBSYS_IVIHARDWARE           = 26,
61     SUBSYS_KERNEL                = 27,
62     SUBSYS_LOCATION              = 28,
63     SUBSYS_MSDP                  = 29,
64     SUBSYS_MULTIMEDIA            = 30,
65     SUBSYS_MULTIMODAINPUT        = 31,
66     SUBSYS_NOTIFICATION          = 32,
67     SUBSYS_POWERMNG              = 33,
68     SUBSYS_ROUTER                = 34,
69     SUBSYS_SECURITY              = 35,
70     SUBSYS_SENSORS               = 36,
71     SUBSYS_SMALLSERVICES         = 37,
72     SUBSYS_SOURCECODETRANSFORMER = 38,
73     SUBSYS_STARTUP               = 39,
74     SUBSYS_TELEPONY              = 40,
75     SUBSYS_UPDATE                = 41,
76     SUBSYS_USB                   = 42,
77     SUBSYS_WEARABLE              = 43,
78     SUBSYS_WEARABLEHARDWARE      = 44,
79     SUBSYS_IVI                   = 45
80     // new type
81 };
82 
83 // be used to init the subsystem errorno.
84 constexpr ErrCode ErrCodeOffset(unsigned int subsystem, unsigned int module = 0)
85 {
86     constexpr int SUBSYSTEM_BIT_NUM = 21;
87     constexpr int MODULE_BIT_NUM = 16;
88     return (subsystem << SUBSYSTEM_BIT_NUM) | (module << MODULE_BIT_NUM);
89 }
90 
91 // offset of common error, only be used in this file.
92 constexpr ErrCode BASE_ERR_OFFSET = ErrCodeOffset(SUBSYS_COMMON);
93 
94 enum {
95     ERR_OK                = 0,
96     ERR_NO_MEMORY         = BASE_ERR_OFFSET + ENOMEM,
97     ERR_INVALID_OPERATION = BASE_ERR_OFFSET + ENOSYS,
98     ERR_INVALID_VALUE     = BASE_ERR_OFFSET + EINVAL,
99     ERR_NAME_NOT_FOUND    = BASE_ERR_OFFSET + ENOENT,
100     ERR_PERMISSION_DENIED = BASE_ERR_OFFSET + EPERM,
101     ERR_NO_INIT           = BASE_ERR_OFFSET + ENODEV,
102     ERR_ALREADY_EXISTS    = BASE_ERR_OFFSET + EEXIST,
103     ERR_DEAD_OBJECT       = BASE_ERR_OFFSET + EPIPE,
104     ERR_OVERFLOW          = BASE_ERR_OFFSET + EOVERFLOW,
105     ERR_ENOUGH_DATA       = BASE_ERR_OFFSET + ENODATA,
106     ERR_WOULD_BLOCK       = BASE_ERR_OFFSET + EWOULDBLOCK,
107     ERR_TIMED_OUT         = BASE_ERR_OFFSET + ETIMEDOUT
108 };
109 #define SUCCEEDED(errCode) ((errCode) == ERR_OK)
110 #define FAILED(errCode) ((errCode) != ERR_OK)
111 }
112 #endif
113