#!/bin/bash
#
# Copyright 2018 RnD Center "ELVEES", JSC
#
# This is a test for testing System Sleep functionality with RTC acting
# as a wakeup-source.

# The test can be run on the following boards:
#     salute-el24pm1-rev1.1-om1-rev1.1-1.2;
#     salute-el24pm1-rev1.2-om1-rev1.1-1.2;
#     salute-el24pm2-rev1.0-om1-rev1.1-1.2.

set -o errexit

if [ $# -eq 0 ]; then
    SUSPEND_CYCLES=3
else
    SUSPEND_CYCLES=$1
fi

for c in $(seq 1 $SUSPEND_CYCLES); do
    echo "Starting cycle $c of $SUSPEND_CYCLES"
    time_start=$(date +%s)
    rtcwake -s 2 -m standby
    time_end=$(date +%s)
    runtime=$((time_end-time_start))
    echo "Resume time: $runtime"
    if [ $runtime -gt 5 ]; then
        echo "rtcwake test failed"
        exit 1
    fi
done

echo -e "\nTEST PASSED"
