xref: /commonlibrary/c_utils/base/include/errors.h (revision 3f4cbf05)
1/*
2 * Copyright (c) 2021 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 Provides 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
27namespace 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
39using ErrCode = int;
40
41/**
42 * @brief Enumerates the values of the <b>Subsystem</b> segment
43 * of ErrCode for every subsystem.
44 */
45enum {
46    SUBSYS_COMMON                = 0,
47    SUBSYS_AAFWK                 = 1,
48    SUBSYS_ACCOUNT               = 2,
49    SUBSYS_AI                    = 3,
50    SUBSYS_APPEXECFWK            = 4,
51    SUBSYS_APPLICATIONS          = 5,
52    SUBSYS_ARVR                  = 6,
53    SUBSYS_ARVRHARDWARE          = 7,
54    SUBSYS_BARRIERFREE           = 8,
55    SUBSYS_BIOMETRICS            = 9,
56    SUBSYS_CCRUNTIME             = 10,
57    SUBSYS_COMMUNICATION         = 11,
58    SUBSYS_DFX                   = 12,
59    SUBSYS_DISTRIBUTEDDATAMNG    = 13,
60    SUBSYS_DISTRIBUTEDSCHEDULE   = 14,
61    SUBSYS_DRIVERS               = 15,
62    SUBSYS_GLOBAL                = 16,
63    SUBSYS_GRAPHIC               = 17,
64    SUBSYS_HBS                   = 18,
65    SUBSYS_IAWARE                = 19,
66    SUBSYS_IDE                   = 20,
67    SUBSYS_INTELLIACCESSORIES    = 21,
68    SUBSYS_INTELLISPEAKER        = 22,
69    SUBSYS_INTELLITV             = 23,
70    SUBSYS_IOT                   = 24,
71    SUBSYS_IOTHARDWARE           = 25,
72    SUBSYS_IVIHARDWARE           = 26,
73    SUBSYS_KERNEL                = 27,
74    SUBSYS_LOCATION              = 28,
75    SUBSYS_MSDP                  = 29,
76    SUBSYS_MULTIMEDIA            = 30,
77    SUBSYS_MULTIMODAINPUT        = 31,
78    SUBSYS_NOTIFICATION          = 32,
79    SUBSYS_POWERMNG              = 33,
80    SUBSYS_ROUTER                = 34,
81    SUBSYS_SECURITY              = 35,
82    SUBSYS_SENSORS               = 36,
83    SUBSYS_SMALLSERVICES         = 37,
84    SUBSYS_SOURCECODETRANSFORMER = 38,
85    SUBSYS_STARTUP               = 39,
86    SUBSYS_TELEPONY              = 40,
87    SUBSYS_UPDATE                = 41,
88    SUBSYS_USB                   = 42,
89    SUBSYS_WEARABLE              = 43,
90    SUBSYS_WEARABLEHARDWARE      = 44,
91    SUBSYS_IVI                   = 45,
92    SUBSYS_DISTRIBUTEDHARDWARE   = 46,
93    SUBSYS_DEVICEPROFILE         = 47,
94    SUBSYS_CUSTOMIZATION         = 48,
95    SUBSYS_FILEMANAGEMENT        = 49,
96    // new type
97};
98
99// be used to init the subsystem errorno.
100/**
101 * @brief Generates the base error codes for a specified module
102 * in specified subsystem.
103 *
104 * @param subsystem Indicates the subsystem.
105 * @param module Indicates the module.
106 * The default value is 0.
107 * @return Returns the base error codes of the specified module.
108 */
109constexpr ErrCode ErrCodeOffset(unsigned int subsystem, unsigned int module = 0)
110{
111    constexpr int SUBSYSTEM_BIT_NUM = 21;
112    constexpr int MODULE_BIT_NUM = 16;
113    return (subsystem << SUBSYSTEM_BIT_NUM) | (module << MODULE_BIT_NUM);
114}
115
116// offset of common error, only be used in this file.
117/**
118 * @brief Provides the common base error codes, which apply to all modules,
119 * in the commonlibrary subsystem.
120 */
121constexpr ErrCode BASE_ERR_OFFSET = ErrCodeOffset(SUBSYS_COMMON);
122
123/**
124 * @brief Enumerates the common base error codes
125 * in the commonlibrary subsystem.
126 *
127 * @see Related error codes defined in errno.h
128 */
129enum {
130    ERR_OK                = 0,
131    ERR_NO_MEMORY         = BASE_ERR_OFFSET + ENOMEM,
132    ERR_INVALID_OPERATION = BASE_ERR_OFFSET + ENOSYS,
133    ERR_INVALID_VALUE     = BASE_ERR_OFFSET + EINVAL,
134    ERR_NAME_NOT_FOUND    = BASE_ERR_OFFSET + ENOENT,
135    ERR_PERMISSION_DENIED = BASE_ERR_OFFSET + EPERM,
136    ERR_NO_INIT           = BASE_ERR_OFFSET + ENODEV,
137    ERR_ALREADY_EXISTS    = BASE_ERR_OFFSET + EEXIST,
138    ERR_DEAD_OBJECT       = BASE_ERR_OFFSET + EPIPE,
139    ERR_OVERFLOW          = BASE_ERR_OFFSET + EOVERFLOW,
140    ERR_ENOUGH_DATA       = BASE_ERR_OFFSET + ENODATA,
141    ERR_WOULD_BLOCK       = BASE_ERR_OFFSET + EWOULDBLOCK,
142    ERR_TIMED_OUT         = BASE_ERR_OFFSET + ETIMEDOUT
143};
144
145#define SUCCEEDED(errCode) ((errCode) == ERR_OK)
146#define FAILED(errCode) ((errCode) != ERR_OK)
147
148}
149
150#endif
151