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 #include "queue_adapter.h"
16 #include <cmsis_os.h>
17 #include <ohos_types.h>
18 #include <ohos_errno.h>
19
QUEUE_Create(const char * name,int size,int count)20 MQueueId QUEUE_Create(const char *name, int size, int count)
21 {
22 osMessageQueueAttr_t queueAttr = {name, 0, NULL, 0, NULL, 0};
23 return (MQueueId)osMessageQueueNew(count, size, &queueAttr);
24 }
25
QUEUE_Put(MQueueId queueId,const void * element,uint8 pri,int timeout)26 int QUEUE_Put(MQueueId queueId, const void *element, uint8 pri, int timeout)
27 {
28 uint32_t waitTime = (timeout <= 0) ? 0 : (uint32_t)timeout;
29 osStatus_t ret = osMessageQueuePut(queueId, element, pri, waitTime);
30 if (ret != osOK) {
31 return EC_BUSBUSY;
32 }
33 return EC_SUCCESS;
34 }
35
QUEUE_Pop(MQueueId queueId,void * element,uint8 * pri,int timeout)36 int QUEUE_Pop(MQueueId queueId, void *element, uint8 *pri, int timeout)
37 {
38 uint32_t waitTime = (timeout <= 0) ? osWaitForever : (uint32_t)timeout;
39 osStatus_t evt = osMessageQueueGet(queueId, element, pri, waitTime);
40 if (evt != osOK) {
41 return EC_BUSBUSY;
42 }
43 return EC_SUCCESS;
44 }
45
QUEUE_Destroy(MQueueId queueId)46 int QUEUE_Destroy(MQueueId queueId)
47 {
48 osStatus_t evt = osMessageQueueDelete(queueId);
49 if (evt != osOK) {
50 return EC_FAILURE;
51 }
52 return EC_SUCCESS;
53 }