1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development 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 #include "los_queue.h"
17 #include "los_task.h"
18 #include "ohos_run.h"
19
20 #define BUFFER_LEN 50
21 #define USLEEP_1S (1000000)
22 #define TASK_PRIO_H 9
23 #define TASK_PRIO_L 10
24 #define QUEUE_LEN 5
25 #define QUEUE_MAX_SIZE 50
26 static UINT32 g_queue;
27
SendEntry(VOID)28 VOID SendEntry(VOID)
29 {
30 UINT32 ret = 0;
31 CHAR abuf[] = "test message";
32 UINT32 len = sizeof(abuf);
33
34 ret = LOS_QueueWriteCopy(g_queue, abuf, len, 0);
35 if (ret != LOS_OK) {
36 printf("send message failure, error: %x\n", ret);
37 }
38 }
39
RecvEntry(VOID)40 VOID RecvEntry(VOID)
41 {
42 UINT32 ret = 0;
43 CHAR readBuf[BUFFER_LEN] = {0};
44 UINT32 readLen = BUFFER_LEN;
45
46 // 休眠1s
47 usleep(USLEEP_1S);
48 ret = LOS_QueueReadCopy(g_queue, readBuf, &readLen, 0);
49 if (ret != LOS_OK) {
50 printf("recv message failure, error: %x\n", ret);
51 }
52
53 printf("recv message: %s\n", readBuf);
54
55 ret = LOS_QueueDelete(g_queue);
56 if (ret != LOS_OK) {
57 printf("delete the queue failure, error: %x\n", ret);
58 }
59
60 printf("delete the queue success.\n");
61 }
62
ExampleQueue(VOID)63 UINT32 ExampleQueue(VOID)
64 {
65 printf("start queue example.\n");
66 UINT32 ret = 0;
67 UINT32 task1, task2;
68 TSK_INIT_PARAM_S initParam = {0};
69
70 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)SendEntry;
71 initParam.usTaskPrio = TASK_PRIO_L;
72 initParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
73 initParam.pcName = "SendQueue";
74
75 LOS_TaskLock();
76 ret = LOS_TaskCreate(&task1, &initParam);
77 if (ret != LOS_OK) {
78 printf("create task1 failed, error: %x\n", ret);
79 return ret;
80 }
81
82 initParam.pcName = "RecvQueue";
83 initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)RecvEntry;
84 initParam.usTaskPrio = TASK_PRIO_H;
85 ret = LOS_TaskCreate(&task2, &initParam);
86 if (ret != LOS_OK) {
87 printf("create task2 failed, error: %x\n", ret);
88 return ret;
89 }
90
91 ret = LOS_QueueCreate("queue", QUEUE_LEN, &g_queue, 0, QUEUE_MAX_SIZE);
92 if (ret != LOS_OK) {
93 printf("create queue failure, error: %x\n", ret);
94 }
95
96 printf("create the queue success.\n");
97 LOS_TaskUnlock();
98 return ret;
99 }
100
101 OHOS_APP_RUN(ExampleQueue);
102