1/*
2 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 *    conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 *    of conditions and the following disclaimer in the documentation and/or other materials
12 *    provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 *    to endorse or promote products derived from this software without specific prior written
16 *    permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef _LOS_SIGNAL_H
32#define _LOS_SIGNAL_H
33
34#include <signal.h>
35#include "los_list.h"
36
37#ifdef __cplusplus
38#if __cplusplus
39extern "C" {
40#endif /* __cplusplus */
41#endif /* __cplusplus */
42
43/**
44 * @ingroup los_signal
45 * Signal error code: The parameters of interface is error.
46 *
47 * Value: 0x02003200
48 *
49 */
50#define LOS_ERRNO_SIGNAL_INVALID                    LOS_ERRNO_OS_FATAL(LOS_MOD_SIGNAL, 0x00)
51
52/**
53 * @ingroup los_signal
54 * Signal error code: The memory requests failed.
55 *
56 * Value: 0x02003201
57 *
58 */
59#define LOS_ERRNO_SIGNAL_NO_MEMORY                  LOS_ERRNO_OS_ERROR(LOS_MOD_SIGNAL, 0x01)
60
61/**
62 * @ingroup los_signal
63 * Signal error code: The signal is not set.
64 *
65 * Value: 0x02003202
66 *
67 */
68#define LOS_ERRNO_SIGNAL_NO_SET                     LOS_ERRNO_OS_ERROR(LOS_MOD_SIGNAL, 0x02)
69
70/**
71 * @ingroup los_signal
72 * Signal error code: Waiting for signal timeout.
73 *
74 * Value: 0x02003203
75 *
76 */
77#define LOS_ERRNO_SIGNAL_TIMEOUT                    LOS_ERRNO_OS_ERROR(LOS_MOD_SIGNAL, 0x03)
78
79/**
80 * @ingroup los_signal
81 * Signal error code: The interface is used before system start.
82 *
83 * Value: 0x02003204
84 *
85 */
86#define LOS_ERRNO_SIGNAL_CAN_NOT_CALL               LOS_ERRNO_OS_ERROR(LOS_MOD_SIGNAL, 0x04)
87
88/**
89* @ingroup los_signal
90* Mutex error code: Waiting for signal in interrupt callback.
91*
92* Value: 0x02003205
93*
94*/
95#define LOS_ERRNO_SIGNAL_PEND_INTERR                LOS_ERRNO_OS_ERROR(LOS_MOD_SIGNAL, 0x05)
96
97/**
98 * @ingroup los_signal
99 * Add the signal num to the signal set.
100 */
101#define LOS_SIGNAL_MASK(sigNo)          (1U << ((sigNo) - 1))
102
103/**
104 * @ingroup los_signal
105 * Maximum signal supported num.
106 */
107#define LOS_SIGNAL_SUPPORT_MAX           31
108
109/**
110 * @ingroup los_signal
111 * Signal handler type.
112 */
113typedef VOID (*SIG_HANDLER)(INT32 sigNo);
114
115typedef struct {
116    sigset_t     sigSetFlag;                              /**< installing signals */
117    sigset_t     sigPendFlag;                             /**< pending signals */
118    sigset_t     sigWaitFlag;                             /**< waiting signals */
119    siginfo_t    sigInfo;                                 /**< signal info */
120    SIG_HANDLER  sigHandlers[LOS_SIGNAL_SUPPORT_MAX + 1]; /**< signal handler */
121    LOS_DL_LIST  sigInfoList;                             /**< signal info list */
122    VOID         *sigSaveSP;                              /**< save stack pointer */
123    VOID         *sigRestoreSP;                           /**< restore stack pointer */
124    UINT32       sigStatus;                               /**< status of signal */
125} OsSigCB;
126
127typedef struct {
128    LOS_DL_LIST node;
129    siginfo_t info;
130} OsSigInfoNode;
131
132#define OS_SIGNAL_STATUS_WAIT            0x0001
133#define OS_SIGNAL_VALID(sigNo)           (((sigNo) > 0) && ((sigNo) <= LOS_SIGNAL_SUPPORT_MAX))
134
135UINT32 OsSignalInit(VOID);
136UINTPTR OsSignalTaskContextRestore(VOID);
137
138/**
139 * @ingroup los_signal
140 * @brief Register the handler for the specified signal.
141 *
142 * @par Description:
143 * This API is used to register the handler for the specified signal, otherwise it is the default handler.
144 *
145 * @attention None.
146 *
147 * @param sigNo   [IN] The specified signal num.
148 * @param handler [IN] The handler for this signal, which is either SIG_IGN, SIG_DFL,
149 * or the address of a programmer-defined function.
150 *
151 * @retval: SIG_ERR Type#SIG_HANDLER: error code.
152 * @retval: old     Type#SIG_HANDLER: success, the previous handler is returned.
153 * <ul><li>los_signal.h: the header file that contains the API declaration.</li></ul>
154 * @see None
155 */
156SIG_HANDLER LOS_SignalSet(INT32 sigNo, SIG_HANDLER handler);
157
158/**
159 * @ingroup los_signal
160 * @brief Shield the specified signal set.
161 *
162 * @par Description:
163 * This API is used to shield the specified signal set and get the current signal set.
164 *
165 * @attention None.
166 *
167 * @param how    [IN] The behavior of the call is dependent on the value of how, which is either SIG_BLOCK,
168 * SIG_UNBLOCK, SIG_SETMASK.
169 * @param set    [IN] The new signal set.
170 * @param oldSet [OUT] The old signal set.
171 *
172 * @retval: LOS_ERRNO_SIGNAL_CAN_NOT_CALL Type#UINT32: The interface is used before system start.
173 * @retval: LOS_ERRNO_SIGNAL_NO_MEMORY Type#UINT32: The memory requests failed.
174 * @retval: LOS_ERRNO_SIGNAL_INVALID Type#UINT32: The parameters of interface is error.
175 * @retval: LOS_OK Type#UINT32: success.
176 * <ul><li>los_signal.h: the header file that contains the API declaration.</li></ul>
177 * @see None
178 */
179UINT32 LOS_SignalMask(INT32 how, const sigset_t *set, sigset_t *oldSet);
180
181/**
182 * @ingroup los_signal
183 * @brief Suspend execution of the calling thread until one of the signals specified in
184 * the signal set becomes pending.
185 *
186 * @par Description:
187 * This API is used to suspend execution of the calling thread until one of the signals
188 * specified in the signal set becomes pending and return the signal number in sig.
189 *
190 * @attention None.
191 *
192 * @param set     [IN] The specified signal set which waiting for.
193 * @param info    [OUT] The info of signal becomes pending.
194 * @param timeout [IN] The waiting time.
195 *
196 * @retval: LOS_ERRNO_SIGNAL_INVALID Type#UINT32: The parameters of interface is error.
197 * @retval: LOS_ERRNO_SIGNAL_CAN_NOT_CALL Type#UINT32: The interface is used before system start.
198 * @retval: LOS_ERRNO_SIGNAL_NO_MEMORY Type#UINT32: The memory requests failed.
199 * @retval: LOS_ERRNO_SIGNAL_PEND_INTERR Type#UINT32: Waiting for signal in interrupt callback.
200 * @retval: LOS_ERRNO_SIGNAL_TIMEOUT Type#UINT32: Waiting for signal timeout.
201 * @retval: signo Type#UINT32: success, returning the signal num which becomes pending.
202 * <ul><li>los_signal.h: the header file that contains the API declaration.</li></ul>
203 * @see LOS_SignalSend
204 */
205UINT32 LOS_SignalWait(const sigset_t *set, siginfo_t *info, UINT32 timeout);
206
207/**
208 * @ingroup los_signal
209 * @brief Send the specified signal to the specified task.
210 *
211 * @par Description:
212 * This API is used to send the specified signal to the specified task.
213 *
214 * @attention None.
215 *
216 * @param taskID   [IN] Send a signal to this task.
217 * @param sigNo    [IN] The signal num.
218 *
219 * @retval: LOS_ERRNO_SIGNAL_NO_MEMORY Type#UINT32: The memory requests failed.
220 * @retval: LOS_ERRNO_SIGNAL_INVALID Type#UINT32: The parameters of interface is error.
221 * @retval: LOS_ERRNO_SIGNAL_NO_SET Type#UINT32: The signal is not set.
222 * @retval: LOS_OK Type#UINT32: success.
223 * <ul><li>los_signal.h: the header file that contains the API declaration.</li></ul>
224 * @see None
225 */
226UINT32 LOS_SignalSend(UINT32 taskID, INT32 sigNo);
227
228#ifdef __cplusplus
229#if __cplusplus
230}
231#endif /* __cplusplus */
232#endif /* __cplusplus */
233
234#endif /* _LOS_SIGNAL_H */