#!/bin/bash
#########################################################################################
# 1.2.0
# Version that use functions from the common library : update_common
#########################################################################################

source /usr/local/bin/update_common

GPIO_COMMON_SCRIPT_VERSION="1.2.2"
GPIO_COMMON_SCRIPT_INITIALS="[G-C]"

#########################################################################################
# Local variables defined
#########################################################################################

TMP_DIR=/tmp
TMP_SYS_DIR=$TMP_DIR/sys
TMP_SYS_CLASS_DIR=$TMP_SYS_DIR/class
TMP_SYS_CLASS_GPIO_DIR=$TMP_SYS_CLASS_DIR/gpio

VIRT_GPIO_DIR=/sys/class/gpio

COM_DIR=gpio234
SEL_DIR=gpio235
POE_DIR=gpio16
CAP_DIR=gpio29

REAL_COM_FILE=$TMP_SYS_CLASS_GPIO_DIR/$COM_DIR/value
REAL_SEL_FILE=$TMP_SYS_CLASS_GPIO_DIR/$SEL_DIR/value
REAL_POE_FILE=$TMP_SYS_CLASS_GPIO_DIR/$POE_DIR/value
REAL_CAP_FILE=$TMP_SYS_CLASS_GPIO_DIR/$CAP_DIR/value

LOG_FILE=/dev/console

#########################################################################################
# Fonctions
#########################################################################################

###########################################################################################
# Function that add the initials of the script to each message that will be logged.
# That makes the reading of the logs unambiguous

GPIO_Common_WriteLog() {
	LogUpdate "$GPIO_COMMON_SCRIPT_INITIALS : $1" $2 $3
}

#########################################################################################
# Function that create, if necessary,  all directories of a pathname passed as argument 
#
# Order of parameters
# $1 Specific final part of the pathname

MakesGpioRealPathAvailable() {

	if [ -z "$1" ]; then
		return 1
	fi

	if [ ! -e "$TMP_DIR" ]; then
		CreateDirectory $TMP_DIR
	fi

	if [ ! -e "$TMP_SYS_DIR" ]; then
		CreateDirectory $TMP_SYS_DIR
	fi

	if [ ! -e "$TMP_SYS_CLASS_DIR" ]; then
		CreateDirectory $TMP_SYS_CLASS_DIR
	fi

	if [ ! -e "$TMP_SYS_CLASS_GPIO_DIR" ]; then
		CreateDirectory $TMP_SYS_CLASS_GPIO_DIR
	fi

	if [ ! -e "$TMP_SYS_CLASS_GPIO_DIR/$1" ]; then
		CreateDirectory $TMP_SYS_CLASS_GPIO_DIR/$1
	fi

	return 0
}

#########################################################################################
# Function that return the value of a gpio from a virtual file 
# All gpios virtual files are located under $VIRT_GPIO_DIR directory
#
# Input  : $1 Specific final part of the pathname
# Output : GPIO value

GetVirtualGpioValue()  {

	if [ ! -f "$VIRT_GPIO_DIR/$1/value" ]; then																		# Argument should address a real existing file
		LogUpdate "GetVirtualGpioValue : $VIRT_GPIO_DIR/$1/value doesn't exists" $LOG_WARNING
		return 1
	fi

	GPIO_VALUE=`cat $VIRT_GPIO_DIR/$1/value`																							# Get the value and put it in the variable
	LogUpdate "GetVirtualGpioValue : $VIRT_GPIO_DIR/$1 - $GPIO_VALUE" $LOG_WARNING

	return 0
}

#########################################################################################
# Function that get the content of a "real" gpio file and put it in a variable
# All real gpio files are located under $TMP_SYS_CLASS_GPIO_DIR directory
#
# Input : $1 Specific final part of the pathname 

GetRealGpioValue() {

	if [ ! -f "$TMP_SYS_CLASS_GPIO_DIR/$1/value" ]; then																		# Argument should address a real existing file
		LogUpdate "GetRealGpioValue : $TMP_SYS_CLASS_GPIO_DIR/$1/value doesn't exists" $LOG_WARNING
		return 1
	fi

	GPIO_VALUE=`cat $TMP_SYS_CLASS_GPIO_DIR/$1/value`																			# Ok fine, get the value and put it in the variable
	LogUpdate "GetRealGpioValue : $TMP_SYS_CLASS_GPIO_DIR/$1 - $GPIO_VALUE" $LOG_WARNING

	return 0
}

#########################################################################################
# Function that set a real gpio file named "value" with the content of a global variable
# All real gpio files are located under $TMP_SYS_CLASS_GPIO_DIR directory
#
# Order of arguments
# $1 Specific final part of the pathname
# $2 Value to set

SetRealGpioValue() {
	echo "$2" > $TMP_SYS_CLASS_GPIO_DIR/$1/value
	LogUpdate "SetRealGpioValue : $TMP_SYS_CLASS_GPIO_DIR/$1 with value $2" $LOG_WARNING
}

#########################################################################################
# Function that remove physically a "real" gpio file
# All real gpio files are located under $TMP_SYS_CLASS_GPIO_DIR directory
#
# Order of arguments
# $1 Specific part of the pathname

RemoveRealGpioFile() {

	if [ -z "$1" ]; then																										# Argument should not be null
		return 1
	fi

	if [ -e "$TMP_SYS_CLASS_GPIO_DIR/$1/value" ]; then																			# Check presence of files
		rm $TMP_SYS_CLASS_GPIO_DIR/$1/value																						# Remove file will produce a DELETE event to software
	fi

	return 0
}

#########################################################################################
# Function that create a real gpio file which will contain the gpio value
# All real gpio files are located under $TMP_SYS_CLASS_GPIO_DIR directory
#
# Order of arguments
# $1 Mandatory : Specific final part of the pathname
# $2 Optional : Debug mode

InitOneGpioMonitoring() {

	if [ ! -z "$1" ]; then																											# Final part of the pathname should not be null

		if [ ! -r "$VIRT_GPIO_DIR/$1/value" ]; then																					# Argument should address a virtual existing file
			LogUpdate "Init $1 Gpio monitoring can't be done because directory doesn't exists"
			return 1
		fi

		LogUpdate "InitOneGpioMonitoring : $TMP_SYS_CLASS_GPIO_DIR/$1" $LOG_WARNING

		MakesGpioRealPathAvailable $1																								# Build path for real files if necessary

		if [ ! -f "$TMP_SYS_CLASS_GPIO_DIR/$1/value" ]; then																		# If real files doesn't exist

			if GetVirtualGpioValue $1; then
				echo "$GPIO_VALUE" > $TMP_SYS_CLASS_GPIO_DIR/$1/value
				SetRealGpioValue $1 $GPIO_VALUE																						# And copy it in a real file
			fi

		fi

	fi

	LogUpdate "Init $1 Gpio monitoring done"
	return 0
}

#########################################################################################
InitGpiosMonitoring() {

	LogUpdate "----------------------------- Init GPIOs virtual files monitoring v$GPIO_COMMON_SCRIPT_VERSION - started" 

	boardtype=$(Get_BoardType)

	InitOneGpioMonitoring $COM_DIR

	if [ $boardtype == $KNX_BOARD ]; then
		InitOneGpioMonitoring $SEL_DIR
	fi

	InitOneGpioMonitoring $POE_DIR
	InitOneGpioMonitoring $CAP_DIR

	LogUpdate "----------------------------- Init GPIOs virtual files monitoring v$GPIO_COMMON_SCRIPT_VERSION - ended" 
}

