1 /* ---------------------------------------------------------------------------- 2 * Copyright (c) Huawei Technologies Co., Ltd. 2013-2018. All rights reserved. 3 * Description: queue 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 1. Redistributions of source code must retain the above copyright notice, this list of 7 * conditions and the following disclaimer. 8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 9 * of conditions and the following disclaimer in the documentation and/or other materials 10 * provided with the distribution. 11 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 12 * to endorse or promote products derived from this software without specific prior written 13 * permission. 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * --------------------------------------------------------------------------- */ 26 27 #ifndef _LOS_QUEUE_PRI_H 28 #define _LOS_QUEUE_PRI_H 29 30 #include "los_queue.h" 31 32 #ifdef __cplusplus 33 #if __cplusplus 34 extern "C" { 35 #endif /* __cplusplus */ 36 #endif /* __cplusplus */ 37 38 typedef enum { 39 OS_QUEUE_READ, 40 OS_QUEUE_WRITE 41 } QueueReadWrite; 42 43 typedef enum { 44 OS_QUEUE_HEAD, 45 OS_QUEUE_TAIL 46 } QueueHeadTail; 47 48 #define OS_QUEUE_OPERATE_TYPE(ReadOrWrite, HeadOrTail) (((UINT32)(HeadOrTail) << 1) | (ReadOrWrite)) 49 #define OS_QUEUE_READ_WRITE_GET(type) ((type) & (0x01)) 50 #define OS_QUEUE_READ_HEAD ((UINT32)OS_QUEUE_READ | ((UINT32)OS_QUEUE_HEAD << 1)) 51 #define OS_QUEUE_READ_TAIL ((UINT32)OS_QUEUE_READ | ((UINT32)OS_QUEUE_TAIL << 1)) 52 #define OS_QUEUE_WRITE_HEAD ((UINT32)OS_QUEUE_WRITE | ((UINT32)OS_QUEUE_HEAD << 1)) 53 #define OS_QUEUE_WRITE_TAIL ((UINT32)OS_QUEUE_WRITE | ((UINT32)OS_QUEUE_TAIL << 1)) 54 #define OS_QUEUE_OPERATE_GET(type) ((type) & (0x03)) 55 #define OS_QUEUE_IS_READ(type) (OS_QUEUE_READ_WRITE_GET(type) == (UINT32)OS_QUEUE_READ) 56 #define OS_QUEUE_IS_WRITE(type) (OS_QUEUE_READ_WRITE_GET(type) == (UINT32)OS_QUEUE_WRITE) 57 #define OS_READWRITE_LEN 2 58 59 /** 60 * @ingroup los_queue 61 * Queue information block structure 62 */ 63 typedef struct { 64 UINT8 *queue; /**< Pointer to a queue handle */ 65 UINT16 queueState; /**< Queue state */ 66 UINT16 queueLen; /**< Queue length */ 67 UINT16 queueSize; /**< Node size */ 68 UINT16 queueID; /**< queueID */ 69 UINT16 queueHead; /**< Node head */ 70 UINT16 queueTail; /**< Node tail */ 71 UINT16 readWriteableCnt[OS_READWRITE_LEN]; /**< Count of readable or writable resources, 0:readable, 1:writable */ 72 LOS_DL_LIST readWriteList[OS_READWRITE_LEN]; /**< Pointer to the linked list to be read or written, 73 0:readlist, 1:writelist */ 74 LOS_DL_LIST memList; /**< Pointer to the memory linked list */ 75 } LosQueueCB; 76 77 /* queue state */ 78 /** 79 * @ingroup los_queue 80 * Message queue state: not in use. 81 */ 82 #define OS_QUEUE_UNUSED 0 83 84 /** 85 * @ingroup los_queue 86 * Message queue state: used. 87 */ 88 #define OS_QUEUE_INUSED 1 89 90 /** 91 * @ingroup los_queue 92 * Not in use. 93 */ 94 #define OS_QUEUE_WAIT_FOR_POOL 1 95 96 /** 97 * @ingroup los_queue 98 * Normal message queue. 99 */ 100 #define OS_QUEUE_NORMAL 0 101 102 /** 103 * @ingroup los_queue 104 * Queue information control block 105 */ 106 extern LosQueueCB *g_allQueue; 107 108 /** 109 * @ingroup los_queue 110 * Obtain a handle of the queue that has a specified ID. 111 */ 112 #define GET_QUEUE_HANDLE(QueueID) (((LosQueueCB *)g_allQueue) + (QueueID)) 113 114 /** 115 * @ingroup los_queue 116 * Obtain the head node in a queue doubly linked list. 117 */ 118 #define GET_QUEUE_LIST(ptr) LOS_DL_LIST_ENTRY(ptr, LosQueueCB, readWriteList[OS_QUEUE_WRITE]) 119 #if (LOSCFG_MEMBOX == YES) 120 121 /** 122 * @ingroup los_queue 123 * @brief Alloc a stationary memory for a mail. 124 * 125 * @par Description: 126 * This API is used to alloc a stationary memory for a mail according to queueID. 127 * @attention 128 * <ul> 129 * <li>Do not alloc memory in unblocking modes such as interrupt.</li> 130 * <li>This API cannot be called before the Huawei LiteOS is initialized.</li> 131 * <li>The argument timeOut is a relative time.</li> 132 * </ul> 133 * 134 * @param queueID [IN] Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT]. 135 * @param mailPool [IN] The memory poll that stores the mail. 136 * @param timeOut [IN] Expiry time. The value range is [0,LOS_WAIT_FOREVER]. 137 * 138 * @retval #NULL The memory allocation is failed. 139 * @retval #mem The address of alloc memory. 140 * @par Dependency: 141 * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul> 142 * @see OsQueueMailFree 143 */ 144 extern VOID *OsQueueMailAlloc(UINT32 queueID, VOID *mailPool, UINT32 timeOut); 145 146 /** 147 * @ingroup los_queue 148 * @brief Free a stationary memory of a mail. 149 * 150 * @par Description: 151 * This API is used to free a stationary memory for a mail according to queueID. 152 * @attention 153 * <ul> 154 * <li>This API cannot be called before the Huawei LiteOS is initialized.</li> 155 * </ul> 156 * 157 * @param queueID [IN] Queue ID. The value range is [1,LOSCFG_BASE_IPC_QUEUE_LIMIT]. 158 * @param mailPool [IN] The mail memory poll address. 159 * @param mailMem [IN] The mail memory block address. 160 * 161 * @retval #LOS_OK 0x00000000: The memory free successfully. 162 * @retval #OS_ERRNO_QUEUE_MAIL_HANDLE_INVALID 0x02000619: The handle of the queue passed-in when the memory for 163 the queue is being freed is invalid. 164 * @retval #OS_ERRNO_QUEUE_MAIL_PTR_INVALID 0x0200061a: The pointer to the memory to be freed is null. 165 * @retval #OS_ERRNO_QUEUE_MAIL_FREE_ERROR 0x0200061b: The memory for the queue fails to be freed. 166 * @par Dependency: 167 * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul> 168 * @see OsQueueMailAlloc 169 */ 170 extern UINT32 OsQueueMailFree(UINT32 queueID, VOID *mailPool, VOID *mailMem); 171 #endif 172 173 /** 174 * @ingroup los_queue 175 * @brief Initialization queue. 176 * 177 * @par Description: 178 * This API is used to initialization queue. 179 * @attention 180 * <ul> 181 * <li>None.</li> 182 * </ul> 183 * 184 * @param None. 185 * 186 * @retval UINT32 Initialization result. 187 * @par Dependency: 188 * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul> 189 * @see None. 190 */ 191 extern UINT32 OsQueueInit(VOID *queueArray); 192 193 /** 194 * @ingroup los_queue 195 * @brief Handle when read or write queue. 196 * 197 * @par Description: 198 * This API is used to handle when read or write queue. 199 * @attention 200 * <ul> 201 * <li>None.</li> 202 * </ul> 203 * 204 * @param queueID [IN] Queue id. 205 * @param operateType [IN] Operate type 206 * @param bufferAddr [IN] Buffer address. 207 * @param bufferSize [IN] Buffer size. 208 * @param timeOut [IN] Timeout. 209 * 210 * @retval UINT32 Handle result. 211 * @par Dependency: 212 * <ul><li>los_queue_pri.h: the header file that contains the API declaration.</li></ul> 213 * @see None. 214 */ 215 extern UINT32 OsQueueOperate(UINT32 queueID, UINT32 operateType, VOID *bufferAddr, UINT32 *bufferSize, 216 UINT32 timeOut); 217 218 #ifdef __cplusplus 219 #if __cplusplus 220 } 221 #endif /* __cplusplus */ 222 #endif /* __cplusplus */ 223 224 #endif /* _LOS_QUEUE_PRI_H */ 225