#include <vxWorks.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <semLib.h>
#include <taskLib.h>
#include <msgQLib.h>
#include <logLib.h>
#include <ioLib.h>
#define MAX_MSGS 10
#define MAX_MSG_LEN 100
#define DELAY_TICKS 50
#define STACK_SIZE 20000
int taskManage;
int taskSingal;
int taskSend;
int taskRev;
int i=1;
/*定义信号量,消息队列*/
SEM_ID synSemId;
MSG_Q_ID msgQId1;
MSG_Q_ID msgQId2;
MSG_Q_ID msgQId3;
MSG_Q_ID msgQId4;
/*函数声明*/
STATUS progStart(void);
STATUS tManage(void);
STATUS tSingal(void);
STATUS tSend(void);
STATUS tRev(void);
/*创建各种信号量、任务、消息队列*/
STATUS progStart(void)
{
synSemId=semBCreate(SEM_Q_FIFO,SEM_EMPTY); /*创建信号量*/
/*创建四个任务*/
taskManage = taskSpawn("taskManage", 200, 0, STACK_SIZE, (FUNCPTR)tManage,0,0,0,0,0,0,0,0,0,0);
taskSingal = taskSpawn("taskSingal", 130, 0, STACK_SIZE, (FUNCPTR)tSingal,0,0,0,0,0,0,0,0,0,0);
taskSend = taskSpawn("taskSend", 130, 0, STACK_SIZE, (FUNCPTR)tSend,0,0,0,0,0,0,0,0,0,0);
taskRev = taskSpawn("taskRev", 130, 0, STACK_SIZE, (FUNCPTR)tRev,0,0,0,0,0,0,0,0,0,0);
/*创建消息队列*/
msgQId1=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY);
msgQId2=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY);
msgQId3=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY);
msgQId4=msgQCreate(MAX_MSGS,MAX_MSG_LEN,MSG_Q_PRIORITY);
return(OK);
}
/*taskManage管理函数*/
STATUS tManage(void)
{
char msgbuf[50] = {'\0'};
char msgexit[50]= "exit";
taskDelay(DELAY_TICKS);
printf("The initial has done!\n");
semFlush(synSemId);
FOREVER
{
msgQReceive(msgQId1, msgbuf, MAX_MSG_LEN, WAIT_FOREVER);
if(strcmp(msgbuf,msgexit)==0)
{
logMsg("\nThe system will be ended\n",0,0,0,0,0,0);
semDelete(synSemId);
taskDelete(taskSingal);
taskDelete(taskSend);
taskDelete(taskRev);
logMsg("\nGoodbye!\n",0,0,0,0,0,0);
taskDelete(taskManage);
}
else
{
logMsg(msgbuf,0,0,0,0,0,0);
}
}
}
/*开始信号激励函数*/
STATUS tSingal(void)
{
printf("The system is initialling...\n");
printf("Task2 initialization is OK!\n");
semTake(synSemId,WAIT_FOREVER);
for(i;i<=20;i++)
{
if(msgQSend(msgQId3, "begin", sizeof("begin"), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return(ERROR);
if(msgQSend(msgQId4, "begin", sizeof("begin"), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return(ERROR);
taskDelay(DELAY_TICKS);
}
if(msgQSend(msgQId1, "exit", sizeof("exit"), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return ERROR;
}
STATUS tSend(void)
{
char msgBuf[20];
char msgErro[50] = "The message is erro!\n";
printf("Task3 initialization is OK!\n");
semTake(synSemId,WAIT_FOREVER);
FOREVER
{
if(msgQReceive(msgQId3, msgBuf, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)
return(ERROR);
if((strlen(msgBuf)!=0)&&(strcmp(msgBuf,"begin"))==0)
{
printf("The task3 received message from the task2 is:");
printf("%s",msgBuf);
printf("\n");
}
if(msgQSend(msgQId4, "Sending", sizeof("Sending"), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return(ERROR);
if((strlen(msgBuf)!=0)&&(strcmp(msgBuf,"Receiving"))==0)
{
printf("The task3 received message from the task4 is:");
printf("%s",msgBuf);
printf("\n");
}
else
if(msgQSend(msgQId1, msgErro, sizeof(msgErro), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return(ERROR);
memset(msgBuf,0,20);
}
return (OK);
}
/*接收回复函数*/
STATUS tRev(void)
{
char msgBuf[20];
char msgErro[50] = "The message is erro!\n";
printf("Task4 initialization is OK!\n");
semTake(synSemId,WAIT_FOREVER);
FOREVER
{
if(msgQReceive(msgQId4, msgBuf, MAX_MSG_LEN, WAIT_FOREVER) == ERROR)
{
return(ERROR);
}
if((strlen(msgBuf)!=0)&&(strcmp(msgBuf,"begin"))==0)
{
printf("The task4 received message from the task2 is:");
printf("%s",msgBuf);
printf("\n");
}
if((strlen(msgBuf)!=0)&&(strcmp(msgBuf,"Sending"))==0)
{
printf("The task4 received message from the task3 is:");
printf("%s",msgBuf);
printf("\n");
if(msgQSend(msgQId3, "Receiving", sizeof("Receiving"), WAIT_FOREVER, MSG_PRI_NORMAL) == ERROR)
return(ERROR);
}
}
return (OK);
}