{
  "firmware": "Demo FW 2.4.1 (showcase)",
  "on_error": "continue",
  "loop": 1,
  "loop_infinite": false,
  "same_line": false,
  "channel_a": {
    "enabled": true,
    "transport": "tcp",
    "ip": "127.0.0.100",
    "port": 502,
    "role": "master",
    "slave_id": 1,
    "timeout_ms": 1000,
    "retry": 2
  },
  "channel_b": {
    "enabled": true,
    "transport": "tcp",
    "ip": "127.0.0.101",
    "port": 502,
    "role": "master",
    "slave_id": 1,
    "timeout_ms": 1000,
    "retry": 2
  },
  "cases": [
    {
      "test": "01 Main · gateway A to B",
      "steps": [
        {
          "log": "Gateway forward check: write on A, read back on B"
        },
        {
          "wait_until": {
            "channel": "A",
            "function": 3,
            "address": "0x0000",
            "compare": false,
            "timeout_ms": 3000,
            "interval_ms": 200
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 16,
            "address": "0x0000",
            "end_address": "0x0003",
            "value": "0x1001"
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 6,
            "address": "0x0010",
            "value": "0x55AA"
          }
        },
        {
          "wait": {
            "ms": 200
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9000",
            "end_address": "0x9003",
            "compare_op": "==",
            "expected": "0x1001"
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9010",
            "compare_op": "==",
            "expected": "0x55AA"
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 6,
            "address": "0x0300",
            "value": 1
          }
        },
        {
          "wait_until": {
            "channel": "B",
            "function": 3,
            "address": "0x9300",
            "compare_op": "==",
            "expected": 1,
            "timeout_ms": 2000,
            "interval_ms": 100
          }
        },
        {
          "if": {
            "channel": "B",
            "function": 3,
            "address": "0x9020",
            "compare_op": "range",
            "range": "0x0001 - 0x00FF"
          }
        },
        {
          "log": "Gateway status is in range"
        },
        {
          "verdict": "pass"
        },
        {
          "else": true
        },
        {
          "call": {
            "sub": "fail_path"
          }
        },
        {
          "verdict": "fail"
        },
        {
          "endif": true
        }
      ]
    },
    {
      "test": "02 Compare · == != range format",
      "steps": [
        {
          "read": {
            "channel": "A",
            "function": 3,
            "address": "0x0100",
            "compare_op": "==",
            "expected": "0x1234",
            "tolerance": 2
          }
        },
        {
          "read": {
            "channel": "A",
            "function": 3,
            "address": "0x0101",
            "compare_op": "!=",
            "expected": "0x0000"
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9100",
            "end_address": "0x9102",
            "compare_op": "range",
            "range": "0x0010 - 0x00F0"
          }
        },
        {
          "read": {
            "channel": "A",
            "function": 3,
            "address": "0x0120",
            "format": "float_abcd",
            "compare_op": "==",
            "expected": 230.5
          }
        },
        {
          "read": {
            "channel": "A",
            "function": 3,
            "address": "0x0130",
            "format": "signed",
            "compare_op": "range",
            "range": [
              -10,
              25.5
            ]
          }
        },
        {
          "read": {
            "channel": "A",
            "function": 3,
            "address": "0x0140",
            "format": "long_abcd",
            "compare_op": "==",
            "expected": 123456
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9110",
            "compare": false,
            "store": "fw"
          }
        },
        {
          "if": {
            "cond": {
              "expr": "${fw} >= 0x40"
            }
          }
        },
        {
          "log": "Firmware word ${fw} is new enough"
        },
        {
          "else": true
        },
        {
          "verdict": "fail"
        },
        {
          "endif": true
        }
      ]
    },
    {
      "test": "03 Write · FC6 / FC16 / FC15",
      "steps": [
        {
          "write": {
            "channel": "A",
            "function": 16,
            "address": "0x0200",
            "end_address": "0x0202",
            "value": "0x00AA"
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 16,
            "address": "0x0210",
            "end_address": "0x0212",
            "value": "0x00BB"
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 6,
            "address": "0x0220",
            "value": "0x00CC"
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 16,
            "address": "0x0230",
            "format": "float_cdab",
            "values": [
              1.5,
              -2
            ]
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 15,
            "address": "0x0010",
            "end_address": "0x0017",
            "value": 1
          }
        },
        {
          "wait": {
            "ms": 100
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9200",
            "end_address": "0x9202",
            "compare_op": "==",
            "expected": "0x00AA"
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9210",
            "end_address": "0x9212",
            "compare_op": "==",
            "expected": "0x00BB"
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9220",
            "compare_op": "==",
            "expected": "0x00CC"
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9230",
            "format": "float_cdab",
            "compare_op": "==",
            "expected": 1.5
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9232",
            "format": "float_cdab",
            "compare_op": "==",
            "expected": -2
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 2,
            "address": "0x0010",
            "end_address": "0x0017",
            "compare_op": "==",
            "expected": 1
          }
        }
      ]
    },
    {
      "test": "04 Loop · Set / While / Repeat",
      "steps": [
        {
          "set": {
            "var": "base",
            "value": "0x0400"
          }
        },
        {
          "set": {
            "var": "i",
            "value": 0
          }
        },
        {
          "while": {
            "cond": {
              "lhs": "${i}",
              "op": "<",
              "rhs": 8
            },
            "max_loops": 100
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 6,
            "address": "${base}+${i}",
            "value": "${i}*0x11"
          }
        },
        {
          "set": {
            "var": "i",
            "op": "+=",
            "value": 1
          }
        },
        {
          "endwhile": true
        },
        {
          "set": {
            "var": "i",
            "value": 0
          }
        },
        {
          "repeat": {
            "count": 8
          }
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9000+${base}+${i}",
            "compare_op": "==",
            "expected": "${i}*0x11",
            "store": "got"
          }
        },
        {
          "set": {
            "var": "i",
            "op": "+=",
            "value": 1
          }
        },
        {
          "endrepeat": true
        },
        {
          "if": {
            "cond": {
              "expr": "${got} == 0x77"
            }
          }
        },
        {
          "log": "Sweep done: last word ${got}"
        },
        {
          "verdict": "pass"
        },
        {
          "else": true
        },
        {
          "verdict": "fail"
        },
        {
          "endif": true
        }
      ]
    },
    {
      "test": "05 Call · subroutine with parameters",
      "steps": [
        {
          "set": {
            "var": "ok",
            "value": 0
          }
        },
        {
          "call": {
            "sub": "check_window",
            "args": {
              "addr": "0x9100",
              "lo": "0x10",
              "hi": "0xF0"
            },
            "store": "r"
          }
        },
        {
          "set": {
            "var": "ok",
            "op": "+=",
            "value": "${r}"
          }
        },
        {
          "call": {
            "sub": "check_window",
            "args": {
              "addr": "0x9101",
              "lo": "0x10",
              "hi": "0xF0"
            },
            "store": "r"
          }
        },
        {
          "set": {
            "var": "ok",
            "op": "+=",
            "value": "${r}"
          }
        },
        {
          "call": {
            "sub": "check_window",
            "args": {
              "addr": "0x9102",
              "lo": "0x10",
              "hi": "0xF0"
            },
            "store": "r"
          }
        },
        {
          "set": {
            "var": "ok",
            "op": "+=",
            "value": "${r}"
          }
        },
        {
          "log": "${ok} of 3 windows OK"
        },
        {
          "if": {
            "cond": {
              "expr": "${ok} == 3"
            }
          }
        },
        {
          "verdict": "pass"
        },
        {
          "else": true
        },
        {
          "verdict": "fail"
        },
        {
          "endif": true
        }
      ]
    },
    {
      "test": "06 Operator · Ask",
      "steps": [
        {
          "log": "Operator check: front panel"
        },
        {
          "set": {
            "var": "v24",
            "value": 0
          }
        },
        {
          "ask": {
            "message": "Scan the unit serial number",
            "answer": "text",
            "var": "sn"
          }
        },
        {
          "write": {
            "channel": "A",
            "function": 5,
            "address": "0x0001",
            "value": 1
          }
        },
        {
          "ask": {
            "message": "Unit ${sn}: are all front-panel LEDs lit?"
          }
        },
        {
          "ask": {
            "message": "Measured 24 V rail (V)?",
            "answer": "number",
            "var": "v24"
          }
        },
        {
          "if": {
            "cond": {
              "expr": "${v24} >= 23.5 && ${v24} <= 24.5"
            }
          }
        },
        {
          "log": "24 V rail OK: ${v24} V"
        },
        {
          "else": true
        },
        {
          "verdict": "fail"
        },
        {
          "endif": true
        },
        {
          "log": "Front panel check done for S/N ${sn}"
        },
        {
          "write": {
            "channel": "A",
            "function": 5,
            "address": "0x0001",
            "value": 0
          }
        }
      ]
    },
    {
      "test": "07 Flow · End Round / End Case",
      "steps": [
        {
          "write": {
            "channel": "A",
            "function": 6,
            "address": "0x0310",
            "value": 1
          }
        },
        {
          "wait": {
            "ms": 50
          }
        },
        {
          "if": {
            "channel": "A",
            "function": 3,
            "address": "0x0311",
            "compare_op": "==",
            "expected": 1
          }
        },
        {
          "log": "Warm-up round: results discarded"
        },
        {
          "end": {
            "discard": true
          }
        },
        {
          "endif": true
        },
        {
          "if": {
            "channel": "B",
            "function": 3,
            "address": "0x9020",
            "compare_op": "!=",
            "expected": "0x0010"
          }
        },
        {
          "log": "Gateway status changed; the rest of this case is skipped"
        },
        {
          "end_case": true
        },
        {
          "endif": true
        },
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9310",
            "compare_op": "==",
            "expected": 1
          }
        },
        {
          "log": "Round finished at ${now} ms"
        }
      ]
    }
  ],
  "subroutines": [
    {
      "name": "check_window",
      "params": [
        {
          "name": "addr",
          "desc": "register on channel B"
        },
        {
          "name": "lo",
          "default": 0
        },
        {
          "name": "hi",
          "default": "0xFFFF"
        }
      ],
      "steps": [
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "${addr}",
            "compare": false,
            "store": "v"
          }
        },
        {
          "if": {
            "cond": {
              "expr": "${v} >= ${lo} && ${v} <= ${hi}"
            }
          }
        },
        {
          "return": {
            "value": 1
          }
        },
        {
          "endif": true
        },
        {
          "log": "${addr} = ${v}, outside ${lo}..${hi}"
        },
        {
          "return": {
            "value": 0
          }
        }
      ]
    },
    {
      "name": "fail_path",
      "steps": [
        {
          "read": {
            "channel": "B",
            "function": 3,
            "address": "0x9020",
            "compare_op": "!=",
            "expected": "0x0000"
          }
        },
        {
          "log": "Gateway status 0x9020 is out of range"
        }
      ]
    }
  ]
}
