/*
 * voipTask.h
 *
 * Copyright (C) 2005, Ranch Networks, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * Author : Srivatsa Chivukula <srivatsa@ranchnetworks.com>
 *
 */

#ifndef _VOIP_TASK_H
#define _VOIP_TASK_H

#ifndef WIN32
#else
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#endif

#ifdef POSIX
#include "pthread.h"
#endif

#define TIMER_TASK_NAME_SIZE		8
#define TIMER_TASK_NAME_EXT_SIZE	4
#define ABS_TIMER_TASK_NAME_SIZE	(TIMER_TASK_NAME_SIZE + TIMER_TASK_NAME_EXT_SIZE + 1)

/*
#define CLONE_TASK_NAME(a, t) do			       \
    {                                                          \
       if( (a) != 0 ) {                                        \
          strncpy( (char*)(a), (char*)(t), TASK_NAME_SIZE);    \
          (a)[TASK_NAME_SIZE+1] = 0;                        \
       }                                                      \
    } while (0)
*/

#define ABS_TIMER_TASK_NAME(a,m,t)  do                               \
    {                                                          \
       if( (a) != 0 ) {                                        \
          memset((char *)a, 0, sizeof(a));                     \
          strncpy( (char*)(a), (char*)(m), TIMER_TASK_NAME_SIZE);    \
          if( strlen( (char*)(m) ) >= TIMER_TASK_NAME_SIZE ) {       \
             (a)[TIMER_TASK_NAME_SIZE] = '.';                        \
             (a)[TIMER_TASK_NAME_SIZE+1] = 0;                        \
	     strncat((char *)a, t, ABS_TIMER_TASK_NAME_SIZE);        \
          }                                                    \
          else {                                               \
             strcat( (char*)(a), ".");                         \
             strncat( (char*)(a), (char*)(t), TIMER_TASK_NAME_SIZE );\
          }                                                    \
        }                                                      \
     } while(0);

/* Following structure is used to create task. */
/*
typedef struct
{
   char 		task_name[ TASK_NAME_SIZE + 1 ];
   int 			task_stack_size;
   long 		task_priority;
   ENTRY_FUNC 		task_entry_func;
   EXIT_FUNC 		task_exit_func;
   void      		*arg;
   sysLogFac_t          facility;
   int			options;
} taskCreateParams_t;
*/

typedef struct _timer_task_context {
   /* char     task_name[TIMER_TASK_NAME_SIZE+1]; */  //create a timer task for each agent
   char     *task_name;
   int      task_stack_size;
   int      task_priority;
   void    *timer_manager;
#if defined VXWORKS
   int      timer_task_handle;
#elif defined WIN32
   HANDLE   timer_task_handle;
#elif defined POSIX
   pthread_t *timer_task_handle;
#endif
} timer_task_context_t;

#define TASK_CONTEXT_MAGIC 	0xdeadbeef
#define TASK_CONTEXT_PTR 	((task_context_t *)task_context_p)

#define INSTALL_TASK_CONTEXT(tid) do {		\
	taskVarAdd(tid, (int *)&task_context_p);\
    } while (0)

#define SET_TASK_CONTEXT(ptr) do {		\
	task_context_p = (int*)ptr;		\
    } while (0)

#define CLEAR_TASK_CONTEXT SET_TASK_CONTEXT(0)
#define GET_TASK_NAME(ptr) ((ptr)->task_name)
#define GET_SRVC_PORT(ptr) ((ptr)->service_port)
#define GET_DISPATCHER(ptr) ((ptr)->dispatcher)
	
#ifndef WIN32
static __inline__ int valid_timer_task_context(const timer_task_context_t *ptr)
#else
static int valid_timer_task_context(const void *ptr)
#endif
{
    return ( (ptr != 0) ? 1 : 0 );
};

/*
extern int sysTaskMain(int arg);
extern int sysTaskExit(int arg);
*/
/*
extern int sysCreateTask(task_context_t *task_context_p);
extern int sysGetTaskName(int tid, char *name, int name_len);
extern int sysSelfTaskName (char *name, int name_len);
extern void *sysSelfTaskAppContext(void);
extern int sysSetSelfTaskAppContext(void *context_p, size_t size);
extern int sysGetSelfTaskAppContext(void **context_p, size_t *size_p);
*/    
#endif /* _VOIP_TASK_H */
