10a7ce71fSopenharmony_ci/*
20a7ce71fSopenharmony_ci * Copyright (C) 2022 HiHope Open Source Organization .
30a7ce71fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40a7ce71fSopenharmony_ci * you may not use this file except in compliance with the License.
50a7ce71fSopenharmony_ci * You may obtain a copy of the License at
60a7ce71fSopenharmony_ci *
70a7ce71fSopenharmony_ci *     http:// www.apache.org/licenses/LICENSE-2.0
80a7ce71fSopenharmony_ci *
90a7ce71fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100a7ce71fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110a7ce71fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120a7ce71fSopenharmony_ci * See the License for the specific language governing permissions and
130a7ce71fSopenharmony_ci *
140a7ce71fSopenharmony_ci * limitations under the License.
150a7ce71fSopenharmony_ci */
160a7ce71fSopenharmony_ci
170a7ce71fSopenharmony_ci#include <stdio.h>
180a7ce71fSopenharmony_ci#include <unistd.h>
190a7ce71fSopenharmony_ci
200a7ce71fSopenharmony_ci#include "ohos_init.h"
210a7ce71fSopenharmony_ci#include "cmsis_os2.h"
220a7ce71fSopenharmony_ci
230a7ce71fSopenharmony_ci
240a7ce71fSopenharmony_ci#define NUM 1
250a7ce71fSopenharmony_ci#define OS_DELAY 5
260a7ce71fSopenharmony_ci#define OS_DELAY_F 3
270a7ce71fSopenharmony_ci#define OS_DELAY_S 20
280a7ce71fSopenharmony_ci#define OS_DELAY_T 80
290a7ce71fSopenharmony_ci#define ATTR.STACK_SIZE 1024
300a7ce71fSopenharmony_ci#define QUEUE_SIZE 3
310a7ce71fSopenharmony_citypedef struct {
320a7ce71fSopenharmony_ci    osThreadId_t tid;
330a7ce71fSopenharmony_ci    int count;
340a7ce71fSopenharmony_ci} message_entry;
350a7ce71fSopenharmony_ciosMessageQueueId_t qid;
360a7ce71fSopenharmony_ci
370a7ce71fSopenharmony_civoid sender_thread(int *arg)
380a7ce71fSopenharmony_ci{
390a7ce71fSopenharmony_ci    static int count = 0;
400a7ce71fSopenharmony_ci    message_entry sentry;
410a7ce71fSopenharmony_ci    (int)arg;
420a7ce71fSopenharmony_ci    while (NUM) {
430a7ce71fSopenharmony_ci        sentry.tid = osThreadGetId();
440a7ce71fSopenharmony_ci        sentry.count = count;
450a7ce71fSopenharmony_ci        printf("[Message Test] %s send %d to message queue.\r\n", osThreadGetName(osThreadGetId()), count);
460a7ce71fSopenharmony_ci        osMessageQueuePut(qid, (const int *)&sentry, 0, osWaitForever);
470a7ce71fSopenharmony_ci        count++;
480a7ce71fSopenharmony_ci        osDelay(OS_DELAY);
490a7ce71fSopenharmony_ci    }
500a7ce71fSopenharmony_ci}
510a7ce71fSopenharmony_ci
520a7ce71fSopenharmony_civoid receiver_thread(int *arg)
530a7ce71fSopenharmony_ci{
540a7ce71fSopenharmony_ci    (int)arg;
550a7ce71fSopenharmony_ci    message_entry rentry;
560a7ce71fSopenharmony_ci    while (NUM) {
570a7ce71fSopenharmony_ci        osMessageQueueGet(qid, (int *)&rentry, NULL, osWaitForever);
580a7ce71fSopenharmony_ci        printf("[Message Test] %s get %d from %s by message queue.\r\n",
590a7ce71fSopenharmony_ci            osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));
600a7ce71fSopenharmony_ci        osDelay(OS_DELAY_F);
610a7ce71fSopenharmony_ci    }
620a7ce71fSopenharmony_ci}
630a7ce71fSopenharmony_ci
640a7ce71fSopenharmony_ciosThreadId_t newThread(char *name, osThreadFunc_t func, int *arg)
650a7ce71fSopenharmony_ci{
660a7ce71fSopenharmony_ci    osThreadAttr_t attr = {
670a7ce71fSopenharmony_ci        name, 0, NULL, 0, NULL, 1024*2, osPriorityNormal, 0, 0
680a7ce71fSopenharmony_ci    };
690a7ce71fSopenharmony_ci    osThreadId_t tid = osThreadNew(func, arg, &attr);
700a7ce71fSopenharmony_ci    if (tid == NULL) {
710a7ce71fSopenharmony_ci        printf("[Message Test] osThreadNew(%s) failed.\r\n", name);
720a7ce71fSopenharmony_ci    } else {
730a7ce71fSopenharmony_ci        printf("[Message Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);
740a7ce71fSopenharmony_ci    }
750a7ce71fSopenharmony_ci    return tid;
760a7ce71fSopenharmony_ci}
770a7ce71fSopenharmony_ci
780a7ce71fSopenharmony_civoid rtosv2_msgq_main(int *arg)
790a7ce71fSopenharmony_ci{
800a7ce71fSopenharmony_ci    (int)arg;
810a7ce71fSopenharmony_ci
820a7ce71fSopenharmony_ci    qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL);
830a7ce71fSopenharmony_ci
840a7ce71fSopenharmony_ci    osThreadId_t ctid1 = newThread("receiver1", receiver_thread, NULL);
850a7ce71fSopenharmony_ci    osThreadId_t ctid2 = newThread("receiver2", receiver_thread, NULL);
860a7ce71fSopenharmony_ci    osThreadId_t ptid1 = newThread("sender1", sender_thread, NULL);
870a7ce71fSopenharmony_ci    osThreadId_t ptid2 = newThread("sender2", sender_thread, NULL);
880a7ce71fSopenharmony_ci    osThreadId_t ptid3 = newThread("sender3", sender_thread, NULL);
890a7ce71fSopenharmony_ci
900a7ce71fSopenharmony_ci    osDelay(OS_DELAY_S);
910a7ce71fSopenharmony_ci    uint32_t cap = osMessageQueueGetCapacity(qid);
920a7ce71fSopenharmony_ci    printf("[Message Test] osMessageQueueGetCapacity, capacity: %u.\r\n", cap);
930a7ce71fSopenharmony_ci    uint32_t msg_size =  osMessageQueueGetMsgSize(qid);
940a7ce71fSopenharmony_ci    printf("[Message Test] osMessageQueueGetMsgSize, size: %u.\r\n", msg_size);
950a7ce71fSopenharmony_ci    uint32_t count = osMessageQueueGetCount(qid);
960a7ce71fSopenharmony_ci    printf("[Message Test] osMessageQueueGetCount, count: %u.\r\n", count);
970a7ce71fSopenharmony_ci    uint32_t space = osMessageQueueGetSpace(qid);
980a7ce71fSopenharmony_ci    printf("[Message Test] osMessageQueueGetSpace, space: %u.\r\n", space);
990a7ce71fSopenharmony_ci
1000a7ce71fSopenharmony_ci    osDelay(OS_DELAY_T);
1010a7ce71fSopenharmony_ci    osThreadTerminate(ctid1);
1020a7ce71fSopenharmony_ci    osThreadTerminate(ctid2);
1030a7ce71fSopenharmony_ci    osThreadTerminate(ptid1);
1040a7ce71fSopenharmony_ci    osThreadTerminate(ptid2);
1050a7ce71fSopenharmony_ci    osThreadTerminate(ptid3);
1060a7ce71fSopenharmony_ci    osMessageQueueDelete(qid);
1070a7ce71fSopenharmony_ci}
1080a7ce71fSopenharmony_ci
1090a7ce71fSopenharmony_cistatic void MessageTestTask(void)
1100a7ce71fSopenharmony_ci{
1110a7ce71fSopenharmony_ci    osThreadAttr_t attr;
1120a7ce71fSopenharmony_ci
1130a7ce71fSopenharmony_ci    attr.name = "rtosv2_msgq_main";
1140a7ce71fSopenharmony_ci    attr.attr_bits = 0U;
1150a7ce71fSopenharmony_ci    attr.cb_mem = NULL;
1160a7ce71fSopenharmony_ci    attr.cb_size = 0U;
1170a7ce71fSopenharmony_ci    attr.stack_mem = NULL;
1180a7ce71fSopenharmony_ci    attr.stack_size = ATTR.STACK_SIZE;
1190a7ce71fSopenharmony_ci    attr.priority = osPriorityNormal;
1200a7ce71fSopenharmony_ci
1210a7ce71fSopenharmony_ci    if (osThreadNew((osThreadFunc_t)rtosv2_msgq_main, NULL, &attr) == NULL) {
1220a7ce71fSopenharmony_ci        printf("[MessageTestTask] Failed to create rtosv2_msgq_main!\n");
1230a7ce71fSopenharmony_ci    }
1240a7ce71fSopenharmony_ci}
1250a7ce71fSopenharmony_ci
1260a7ce71fSopenharmony_ciAPP_FEATURE_INIT(MessageTestTask);
127