1 /*
2  * Copyright (c) 2020 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 #ifndef OHOS_ACELITE_MESSAGE_QUEUE_UTILS_H
16 #define OHOS_ACELITE_MESSAGE_QUEUE_UTILS_H
17 
18 #include <cstdint>
19 
20 #include "memory_heap.h"
21 
22 namespace OHOS {
23 namespace ACELite {
24 /**
25  * Message queue handler which identify one queue entity
26  */
27 typedef void* QueueHandler;
28 
29 /**
30  * Successful message queue operation
31  */
32 #ifndef MSGQ_OK
33 #define MSGQ_OK 0
34 #endif
35 
36 /**
37  * Failed message queue operation
38  */
39 #ifndef MSGQ_FAIL
40 #define MSGQ_FAIL (-1)
41 #endif
42 
43 /**
44  * Wait forever timeout value
45  */
46 #ifndef WAIT_FOREVER
47 #ifdef osWaitForever
48 #define WAIT_FOREVER osWaitForever
49 #else
50 #define WAIT_FOREVER 0xFFFFFFFFU
51 #endif // osWaitForever
52 #endif // WAIT_FOREVER
53 
54 /**
55  * No wait timeout value
56  */
57 #ifndef NO_WAIT
58 #ifdef osNoWait
59 #define NO_WAIT osNoWait
60 #else
61 #define NO_WAIT 0x0U
62 #endif // osNoWait
63 #endif // NO_WAIT
64 
65 /**
66  * @enum AbilityMsgId
67  *
68  * @brief Values that represent ability message states
69  */
70 enum AbilityMsgId {
71     UNKNOWN,
72     START_ABILITY,
73     START,
74     ACTIVE,
75     NEW_DATA,
76     BACKGROUND,
77     DESTROY,
78     BACKPRESSED,
79     ASYNCWORK,
80     TE_EVENT,
81     MSG_ID_MAX,
82 };
83 
84 struct AbilityInnerMsg {
85     AbilityMsgId msgId;
86     uint16_t token;
87     const char *bundleName;
88     const char *path;
89     void *data;
90     uint16_t dataLength;
AbilityInnerMsgAbilityInnerMsg91     AbilityInnerMsg() : msgId(UNKNOWN), token(0), bundleName(nullptr), path(nullptr), data(nullptr), dataLength(0) {}
92 };
93 
94 /**
95  * @class MessageQueueUtils
96  *
97  * @brief A queue utility to handle messages
98  *
99  * @see MemoryHeap
100  */
101 class MessageQueueUtils : public MemoryHeap {
102 public:
103     MessageQueueUtils(const MessageQueueUtils &) = delete;
104     MessageQueueUtils &operator=(const MessageQueueUtils &) = delete;
105     MessageQueueUtils(MessageQueueUtils &&) = delete;
106     MessageQueueUtils &operator=(MessageQueueUtils &&) = delete;
~MessageQueueUtils()107     ~MessageQueueUtils() {}
108 
109     /**
110      * @brief Create a Message Queue entity
111      *
112      * @param [in] capacity: maximum number of messages in queue
113      * @param [in] msgSize: message size in bytes
114      * @return handler of the queue created
115      */
116     static QueueHandler CreateMessageQueue(uint32_t capacity, uint32_t msgSize);
117 
118     /**
119      * @brief Delete a Message Queue entity
120      *
121      * @param [in] handler: the handler of the target queue
122      * @return MSGQ_OK: success
123      *         MSGQ_FAIL: fail
124      */
125     static int8_t DeleteMessageQueue(QueueHandler handler);
126 
127     /**
128      * @brief Put a message into the given queue or timeout if the queue is full
129      *
130      * @param [in] handler: the handler of the target queue
131      * @param [in] msgPtr: pointer to buffer for message to put into the queue
132      * @param [in] timeOut: the time to wait when the queue is full
133      * @return MSGQ_OK: success
134      *         MSGQ_FAIL: fail
135      */
136     static int8_t PutMessage(QueueHandler handler, const void *msgPtr, uint32_t timeOut);
137 
138     /**
139      * @brief Get a message from the given queue or timeout if the queue is empty
140      *
141      * @param [in] handler: the handler of the target queue
142      * @param [in] msgPtr: pointer to buffer for message to get from the queue
143      * @param [in] timeOut: the time to wait when the queue is empty
144      * @return MSGQ_OK: success
145      *         MSGQ_FAIL: fail
146      */
147     static int8_t GetMessage(QueueHandler handler, void *msgPtr, uint32_t timeOut);
148 };
149 } // namespace ACELite
150 } // namespace OHOS
151 #endif // OHOS_ACELITE_MESSAGE_QUEUE_UTILS_H
152