<?xml version="1.0"?>
<!-- Copyright (C) 2022 Intel Corporation. -->
<!-- SPDX-License-Identifier: BSD-3-Clause -->

<xs:schema xml:id="root"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:acrn="https://projectacrn.org">

  <xs:assert test="every $vuart_connection in /acrn-config//vuart_connection satisfies
                   every $vm_name in $vuart_connection/endpoint/vm_name/text() satisfies
                   count($vuart_connection/endpoint[./vm_name/text()=$vm_name]) = 1">
    <xs:annotation acrn:severity="error" acrn:report-on="$vuart_connection/endpoint">
      <xs:documentation>The vUART configuration of VM "{$vm_name}" may not connect to itself.</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $io_port in /acrn-config//endpoint[vm_name != '']/io_port satisfies
                   count(//endpoint[./vm_name=$io_port/ancestor::endpoint/vm_name and io_port=$io_port]) = 1">
    <xs:annotation acrn:severity="error" acrn:report-on="$io_port">
      <xs:documentation>VM "{$io_port/ancestor::endpoint/vm_name}" may not duplicate use of port "{$io_port}."</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $console_port in /acrn-config//console_vuart satisfies
                   if ($console_port = 'COM Port 1')
                   then count(//endpoint[./vm_name=$console_port/ancestor::vm/name and io_port='0x3F8']) &lt; 1
                   else if ($console_port = 'COM Port 2')
                   then count(//endpoint[./vm_name=$console_port/ancestor::vm/name and io_port='0x2F8']) &lt; 1
                   else if ($console_port = 'COM Port 3')
                   then count(//endpoint[./vm_name=$console_port/ancestor::vm/name and io_port='0x3E8']) &lt; 1
                   else if ($console_port = 'COM Port 4')
                   then count(//endpoint[./vm_name=$console_port/ancestor::vm/name and io_port='0x2E8']) &lt; 1
                   else true()">
    <xs:annotation acrn:severity="error" acrn:report-on="$console_port">
      <xs:documentation>VM "{$console_port/ancestor::vm/name}" may not duplicate use of console "{$console_port}."</xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="every $NAME in /acrn-config//vuart_connection//endpoint//vm_name satisfies
                  not(/acrn-config//vuart_connection//endpoint//vm_name = '')">
    <xs:annotation acrn:severity="error" acrn:report-on="$NAME">
      <xs:documentation>VM name in "{$NAME/ancestor::vuart_connection/name}" must be set </xs:documentation>
    </xs:annotation>
  </xs:assert>

  <xs:assert test="not (//BUILD_TYPE = 'debug' and //SERIAL_CONSOLE = 'None')">
    <xs:annotation acrn:severity="error" acrn:report-on="//BUILD_TYPE">
       <xs:documentation>Select a hypervisor serial console port when Build type is Debug, or use Build type Release if no serial console is available.</xs:documentation>
    </xs:annotation>
  </xs:assert>

</xs:schema>
