#!/usr/bin/expect -f
#
#  Copyright (c) 2022, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

source "tests/scripts/expect/_common.exp"
source "tests/scripts/expect/_multinode.exp"

spawn_node 1
spawn_node 2

switch_node 1
send "diag start\n"
expect_line "Done"

send "diag channel 11\n"
expect_line "Done"

send "diag stats clear\n"
expect_line "Done"

switch_node 2

send "diag start\n"
expect_line "Done"

send "diag channel 11\n"
expect_line "Done"

send "diag stats clear\n"
expect_line "Done"

send "diag send 10 100\n"
expect_line "Done"

send "diag stats\n"
expect "received packets: 0"
expect "sent success packets: 10"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
expect "sent error invalid state packets: 0"
expect "sent error others packets: 0"
expect "first received packet: rssi=0, lqi=0"
expect "last received packet: rssi=0, lqi=0"
expect_line "Done"

send "diag send async 10 100\n"
expect_line "Done"

sleep 2

send "diag stats\n"
expect "received packets: 0"
expect "sent success packets: 20"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
expect "sent error others packets: 0"
expect "first received packet: rssi=0, lqi=0"
expect "last received packet: rssi=0, lqi=0"
expect_line "Done"

switch_node 1

send "diag stats\n"
expect "received packets: 20"
expect "sent success packets: 0"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
expect "sent error invalid state packets: 0"
expect "sent error others packets: 0"
expect "first received packet: rssi=-20, lqi=0"
expect "last received packet: rssi=-20, lqi=0"
expect_line "Done"

send "diag stats clear\n"
expect_line "Done"

switch_node 2

send "diag repeat 20 100\n"
expect_line "Done"
sleep 1
send "diag repeat stop\n"
expect_line "Done"

switch_node 1

send "diag stats\n"
expect -r {received packets: \d+}
expect "sent success packets: 0"
expect "sent error cca packets: 0"
expect "sent error abort packets: 0"
expect "sent error invalid state packets: 0"
expect "sent error others packets: 0"
expect "first received packet: rssi=-20, lqi=0"
expect "last received packet: rssi=-20, lqi=0"
expect_line "Done"

send "diag stats clear\n"
expect_line "Done"

dispose_all


spawn_node 1

send "diag start\n"
expect_line "Done"

send_user "input too short test\n"
send "diag frame 11\n"
expect "Error"

send_user "input too long test\n"
send "diag frame 11223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900\n"
expect "Error"

send_user "input odd length test\n"
send "diag frame 123\n"
expect "Error"

send "diag radio sleep\n"
expect_line "Done"

send "diag radio disable\n"
expect_line "Done"

send "diag send 10 10\n"
expect "Error 13: InvalidState"

send "diag radio enable\n"
expect_line "Done"

send "diag radio receive\n"
expect_line "Done"

send "diag repeat 100 100\n"
expect "Done"

send "diag send 1 10\n"
expect "Error 13: InvalidState"

send "diag repeat stop\n"
expect "Done"

send "diag send 1 10\n"
expect "Done"

send_user "shortest frame test\n"
send "diag frame 112233\n"
expect "Done"
send "diag send 1\n"
expect "Done"

send_user "longest frame test\n"
send "diag frame 11223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677\n"
expect "Done"
send "diag repeat 1\n"
expect "Done"
sleep 3
send "diag repeat stop\n"
expect "Done"

send "diag repeat stop\n"
expect "Done"

send_user "send frame with security processed\n"
send "diag frame -s 112233\n"
expect "Done"
send "diag send 1\n"
expect "Done"

send_user "send frame with CSMA/CA enabled\n"
send "diag frame -c 112233\n"
expect "Done"
send "diag send 1\n"
expect "Done"

send_user "send frame with tx power\n"
send "diag frame -p 11 112233\n"
expect "Done"
send "diag send 1\n"
expect "Done"

send "diag channel 11\n"
expect_line "Done"

send "diag power 10\n"
expect_line "Done"

send "diag radio sleep\n"
expect_line "Done"

send "diag radio state\n"
expect "sleep"
expect_line "Done"

send "diag radio receive\n"
expect_line "Done"

send "diag radio state\n"
expect "receive"
expect_line "Done"

send "diag gpio set 0 1\n"
expect_line "Done"

send "diag gpio get 0\n"
expect "1"
expect_line "Done"

send "diag gpio mode 0 in\n"
expect_line "Done"

send "diag gpio mode 0\n"
expect "in"
expect_line "Done"

send "diag gpio mode 0 out\n"
expect_line "Done"

send "diag gpio mode 0\n"
expect "out"
expect_line "Done"

send "diag cw start\n"
expect_line "Done"

send "diag cw stop\n"
expect_line "Done"

send "diag stream start\n"
expect_line "Done"

send "diag stream stop\n"
expect_line "Done"

send "diag rawpowersetting 112233\n"
expect_line "Done"

send "diag rawpowersetting\n"
expect "112233"
expect_line "Done"

send "diag rawpowersetting enable\n"
expect_line "Done"

send "diag rawpowersetting disable\n"
expect_line "Done"

send "diag invalid_commad\n"
expect "Error 35: InvalidCommand"

send "diag stop\n"
expect_line "Done"

send "diag channel\n"
expect "Error 13: InvalidState"

dispose_all
