/**********************************************************
* Celestial Systems                                      **
* This program illustrate the service of Message queue   **
* by which real-time tasks can exchange or pass data     **  
* through a Xenomai-managed queue of messages.            ** 
**********************************************************/

/* Include Files */

#include <linux/kernel.h>
#include <native/task.h>
#include <native/queue.h>
#include <native/pipe.h>

/* Global Defines */
#define TASK_PRIO  99       /* Highest RT priority */
#define TASK_MODE  0        /* No flags */
#define TASK_STKSZ 4096     /* Stack size (in bytes) */

#define BUFSIZ 10

/* Global variables */

RT_PIPE rt_pipe;  /* Pipe Descriptor */


RT_TASK  task_desc;
extern RT_QUEUE queue_desc;

/* Global variables */



void task_body (void *cookie)
{
     int err;
     ssize_t len;
     void *msg;
     printk ("task started\n");
     //len = rt_queue_receive(&queue_desc,&msg,TM_NONBLOCK);
     len = rt_queue_receive(&queue_desc,&msg,TM_INFINITE);
     //len = rt_queue_receive(&queue_desc,&msg,1000);
     if(len==0)
     {
          printk("error in receiving %d\n",len);
     }
     else
     {
        printk("the message size is :%d, the message is :%s\n",len,(const char *)msg);
        
	err = rt_pipe_write(&rt_pipe, msg, BUFSIZ, P_NORMAL);
        if (err > 0)
        {
              printk("Data is writen on pipe\n");
        }
        else
        {
             printk("Error in writing = %d\n",err);
        }
	err= rt_queue_free(&queue_desc,msg);
        if(err!=0)
        {
       		printk("error while freeing %d\n",err);
        }
        else
      	{
        	printk("freed\n");
        }
     }

}


/*Register a Module*/

static int __init my_module1(void)
{
     int err;
     ssize_t len;
     void *msg;
	/*Create an FIRST RT task by the name COND_TASK_ONE */
   	 err = rt_pipe_create(&rt_pipe, "pipe",4,0);  /* Creating a pipe with name as pipe, minor number as 4*/
        if (err !=0)
                printk("Error in creating pipe\n");
        else
        {
		printk("creating pipe\n");
	}
	
        err = rt_task_create(&task_desc, "QUEUE_TASK", TASK_STKSZ, TASK_PRIO, TASK_MODE);
        if(!err)
        {
           /* Starting the execution of first task */
	
           err = rt_task_start(&task_desc, &task_body, NULL);
           if (err != 0)
           printk("Error in starting the First task ERROR = %d\n",err);
           else
           printk("Successful in starting the First task\n");
         }
    
   /*
    
    len = rt_queue_receive(&queue_desc,&msg,TM_INFINITE);
    
    if (len == -EPERM)
    {
	printk ("EPERM error\n");
	return;
    }
    if(len==0)
    {
       printk("error in receiving %d\n",len);
    }
    else
    {
                printk("the message size is :%d, the message is :%s\n",len,(const char *)msg);
            	err= rt_queue_free(&queue_desc,msg);
        	if(err!=0)
    		{
      			 printk("error while freeing %d\n",err);
         	}
        	else
      		{
        		printk("freed\n");
        	}
    }

    printk("INIT DONE\n");
    */
     return 0;

}

static void __exit exit_module1(void)
{
	printk("deleting task and queue and unbinding them\n");
}
module_init(my_module1);
module_exit(exit_module1);
MODULE_LICENSE("GPL");
