/************************************************************************/
/* 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>

typedef SemaphoreHandle_t	SemID;			/* Semaphore ID				*/
typedef SemaphoreHandle_t	Semaphore;		/* Semaphore ID				*/

#define NULLSEM				((SemBuf *)0)
#define NODEtoSEM(np)		(((SemBuf *)np)->semHandle)
#define SEMtoNODE(sem)		((Node *)sem)

#define SEM_NAMELEN	16
    
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					*/
	char		sem_name[SEM_NAMELEN];	/* Name (for display only)	    */
} SemBuf;							/************************************/


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

#define sem_take(sem)				xSemaphoreTake(sem, portMAX_DELAY)
#define sem_give(sem)				xSemaphoreGive(sem)

#define sem_name(semBufp, name)		strncpy((semBufp)->sem_name, name, SEM_NAMELEN)


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

void				semLibInit(void);
SemaphoreHandle_t	semCreateMutex(SemBuf *semBufp);
SemaphoreHandle_t	semCreateRecursiveMutex(SemBuf *semBufp);
void				semTaskExit(TaskHandle_t tid);

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

#endif	/* INCsemLibh */
