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