/************************************************************************/
/* Copyright 2002 - 2016 MBARI											*/
/************************************************************************/
/* Summary	: Definitions for Semaphore library for OASIS5				*/
/* Filename : sem.h														*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: OASIS5 Mooring Controller									*/
/* Revision: 1.0														*/
/* Created	: 06/15/2016, from OASIS3 sem.h								*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/************************************************************************/
/* Modification History:												*/
/* 15jun2016 rah - OASIS5 version created from OASIS3 sem.h				*/
/* 10oct2002 rah - OASIS3 version created from OASIS sem.h				*/
/************************************************************************/

#ifndef INCsemLibh
#define INCsemLibh		1

#include <semphr.h>
#include <portmacro.h>
#include <olist.h>
#include <string.h>

#define SEM_NAMELEN	16

typedef enum {SEM_NORM = 0, SEM_RECURSIVE} SemRecursiveType;

typedef struct						/************************************/
{									/* Structure to track Mutex semaphores*/
	Node			sem_link;		/* For linked list of semaphores	*/
	SemaphoreHandle_t semHandle;	/* Actual semaphore handle			*/
	StaticSemaphore_t semBuf;		/* Semaphore buffer					*/
	SemRecursiveType  semRecursive;	/* Is it recursive type?			*/
	char		sem_name[SEM_NAMELEN];	/* Name (for display only)	    */
} Semaphore;						/************************************/

typedef Semaphore 		*SemID;		/* Semaphore ID			   			 */
#define NULLSEM			((SemID)0)
#define NODEtoSEM(np)	((SemID)np)
#define SEMtoNODE(sem)	((Node *)sem)



/********************************************************************/
/* Macros to map O3/O4 semaphore functions to FreeRTOS functions	*/
/* or other OASIS5 semaphore functions.	 Note that these map		*/
/* O3/O4/O5 semaphores to FreeRTOS mutex semaphores.  If you need	*/
/* other types of semaphores, you need to make native FreeRTOS calls*/
/********************************************************************/

#define semTake(sem)			semTakeTmout(sem, portMAX_DELAY)
#define semName(sem, name)		strncpy((sem)->sem_name, name, SEM_NAMELEN)


/********************************/
/*	Function Declarations		*/
/********************************/

void		semLibInit(void);
SemID		semCreateMutex(Semaphore *sem);
SemID		semCreateRecursiveMutex(Semaphore *semBufp);
Errno		semTakeTmout(SemID sem, TickType_t ticksToWait);
Errno		semGive(SemID sem);
void		semTaskExit(TaskHandle_t tid);

#ifdef DEBUG_SYSTEM_CMD
LstHead				*semGetSemList(void);
#endif

#endif	/* INCsemLibh */
