• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 HiHope Open Source Organization .
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  *
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 #include "ohos_init.h"
21 #include "cmsis_os2.h"
22 
23 
24 #define NUM 1
25 #define OS_DELAY 5
26 #define OS_DELAY_F 3
27 #define OS_DELAY_S 20
28 #define OS_DELAY_T 80
29 #define ATTR.STACK_SIZE 1024
30 #define QUEUE_SIZE 3
31 typedef struct {
32     osThreadId_t tid;
33     int count;
34 } message_entry;
35 osMessageQueueId_t qid;
36 // 151144
sender_thread(int * arg)37 void sender_thread(int *arg)
38 {
39     static int count = 0;
40     message_entry sentry;
41     (int)arg;
42     while (NUM) {
43         sentry.tid = osThreadGetId();
44         sentry.count = count;
45         printf("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), count);
46         osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
47         count++;
48         osDelay(OS_DELAY);
49     }
50 }
51 
receiver_thread(int * arg)52 void receiver_thread(int *arg)
53 {
54     (int)arg;
55     message_entry rentry;
56     while (NUM) {
57         osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);  // 从指定的消息队列中取得1条消息,如果消息队列为空,那么返回超时
58         printf("[Message Test] %s get %d from %s by message queue.\r\n",
59             osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
60         osDelay(OS_DELAY_F);
61     }
62 }
63 
newThread(char * name,osThreadFunc_t func,int * arg)64 osThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
65 {
66     osThreadAttr_t attr = {
67         name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
68     };
69     osThreadId_t tid = osThreadNew(func, arg, &attr);
70     if (tid == NULL) {
71         printf("[Message Test] osThreadNew(%s) failed.\r\n", name);
72     } else {
73         printf("[Message Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
74     }
75     return tid;
76 }
77 
rtosv2_msgq_main(int * arg)78 void rtosv2_msgq_main(int *arg)
79 {
80     (int)arg;
81 
82     qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL);
83     // 注释系统循环
84     osThreadId_t ctid1 = newThread("receiver1", receiver_thread, NULL);
85     osThreadId_t ctid2 = newThread("receiver2", receiver_thread, NULL);
86     osThreadId_t ptid1 = newThread("sender1", sender_thread, NULL);
87     osThreadId_t ptid2 = newThread("sender2", sender_thread, NULL);
88     osThreadId_t ptid3 = newThread("sender3", sender_thread, NULL);
89 
90     osDelay(OS_DELAY_S);
91     uint32_t cap = osMessageQueueGetCapacity(qid);
92     printf("[Message Test] osMessageQueueGetCapacity, capacity: %u.\r\n", cap);
93     uint32_t msg_size =  osMessageQueueGetMsgSize(qid);
94     printf("[Message Test] osMessageQueueGetMsgSize, size: %u.\r\n", msg_size);
95     uint32_t count = osMessageQueueGetCount(qid);
96     printf("[Message Test] osMessageQueueGetCount, count: %u.\r\n", count);
97     uint32_t space = osMessageQueueGetSpace(qid);
98     printf("[Message Test] osMessageQueueGetSpace, space: %u.\r\n", space);
99 
100     osDelay(OS_DELAY_T);
101     osThreadTerminate(ctid1);
102     osThreadTerminate(ctid2);
103     osThreadTerminate(ptid1);
104     osThreadTerminate(ptid2);
105     osThreadTerminate(ptid3);
106     osMessageQueueDelete(qid);
107 }
108 
MessageTestTask(void)109 static void MessageTestTask(void)
110 {
111     osThreadAttr_t attr;
112 
113     attr.name = "rtosv2_msgq_main";
114     attr.attr_bits = 0U;
115     attr.cb_mem = NULL;
116     attr.cb_size = 0U;
117     attr.stack_mem = NULL;
118     attr.stack_size = ATTR.STACK_SIZE;
119     attr.priority = osPriorityNormal;
120 
121     if (osThreadNew((osThreadFunc_t)rtosv2_msgq_main, NULL, &attr) == NULL) {
122         printf("[MessageTestTask] Failed to create rtosv2_msgq_main!\n");
123     }
124 }
125 
126 APP_FEATURE_INIT(MessageTestTask);
127