#!/bin/sh
# Copyright (C) 2015 PIVA Software <www.pivasoftware.com>
# 	Author: MOHAMED Kallel <mohamed.kallel@pivasoftware.com>

#############################
#   Entry point functuons   #
#############################

prefix_list="$prefix_list $DMROOT.IPPingDiagnostics."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_IPPingDiagnostics"

UCI_GET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state get"
UCI_SET_VARSTATE="/sbin/uci -q ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} -P /var/state set"

entry_execute_method_root_IPPingDiagnostics() {
	case "$1" in ""|"$DMROOT."|"$DMROOT.IPPingDiagnostics."*)
		common_execute_method_obj "$DMROOT.IPPingDiagnostics." "0"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.DiagnosticsState" "1" "ipping_get easycwmp.@local[0].DiagnosticsState None" "ipping_set_diagnostic_state"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.Host" "1" "ipping_get easycwmp.@local[0].Host" "ipping_set easycwmp.@local[0].Host"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.NumberOfRepetitions" "1" "ipping_get easycwmp.@local[0].NumberOfRepetitions 3" "ipping_set_number easycwmp.@local[0].NumberOfRepetitions" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.Timeout" "1" "ipping_get easycwmp.@local[0].Timeout 1000" "ipping_set_number easycwmp.@local[0].Timeout" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.DataBlockSize" "1" "ipping_get easycwmp.@local[0].DataBlockSize 64" "ipping_set_number easycwmp.@local[0].DataBlockSize" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.SuccessCount" "0" "ipping_get easycwmp.@local[0].SuccessCount 0" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.FailureCount" "0" "ipping_get easycwmp.@local[0].FailureCount 0" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.AverageResponseTime" "0" "ipping_get easycwmp.@local[0].AverageResponseTime 0" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.MinimumResponseTime" "0" "ipping_get easycwmp.@local[0].MinimumResponseTime 0" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IPPingDiagnostics.MaximumResponseTime" "0" "ipping_get easycwmp.@local[0].MaximumResponseTime 0" "" "xsd:unsignedInt"
		return 0;
		;;
	esac
	return $E_INVALID_PARAMETER_NAME;
}

#######################################
#   Data model parameters functions   #
#######################################

ipping_get() {
	local val=`$UCI_GET_VARSTATE $1`
	echo ${val:-$2}
}

ipping_set() {
	ipping_stop_diagnostic
	if [ "`$UCI_GET_VARSTATE easycwmp.@local[0].DiagnosticsState`" != "Requested" ]; then
		$UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=None
	fi
	$UCI_SET_VARSTATE $1=$2
	return 0
}

ipping_set_number() {
	case $2 in
		(*[^0-9]*|'') return $E_INVALID_PARAMETER_VALUE;;
	esac
	[ $2 -lt 1 ] && return $E_INVALID_PARAMETER_VALUE
	ipping_stop_diagnostic
	if [ "`$UCI_GET_VARSTATE easycwmp.@local[0].DiagnosticsState`" != "Requested" ]; then
		$UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=None
	fi
	$UCI_SET_VARSTATE $1=$2
	return 0
}

ipping_set_diagnostic_state() {
	local val="$1"
	case $val in 
		Requested)
			ipping_stop_diagnostic
			$UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=Requested
			common_execute_command_in_apply_service "/bin/sh $FUNCTION_PATH/ipping_launch run &"
			return 0
		;;
	esac
	return $E_INVALID_PARAMETER_VALUE;
}

ipping_stop_diagnostic() {
	local pids=`ps aux | grep ipping_launch | grep -v grep | awk '{print $2}'`
	if [ -n "$pids" ]; then
		kill -9 $pids
		$UCI_SET_VARSTATE easycwmp.@local[0].DiagnosticsState=None
	fi
}


