proc platform_supported { } {
	if {[have_board pc]} {
		return 1
	} elseif {[have_board rpi3] && [have_include power_on/qemu]} {
		return 1
	}
	return 0
}

if {![platform_supported]} {
	puts "Run script is not supported on this platform"
	exit 0
}

create_boot_directory

import_from_depot [depot_user]/src/[base_src] \
                  [depot_user]/src/init \
                  [depot_user]/src/terminal_crosslink \
                  [depot_user]/src/vfs \
                  [depot_user]/src/fs_rom \
                  [depot_user]/src/libc \
                  [depot_user]/src/posix \
                  [depot_user]/src/socat \
                  [depot_user]/src/test-terminal_echo

if {[have_include power_on/qemu]} {
	build { driver/uart }
} else {
	import_from_depot [depot_user]/pkg/[drivers_nic_pkg] \
	                  [depot_user]/src/nic_router \
	                  [depot_user]/src/vfs_lwip
}

append config {
<config>
	<parent-provides>
		<service name="LOG"/>
		<service name="PD"/>
		<service name="CPU"/>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="RM"/>
	</parent-provides>
	<default-route>
		<any-service> <parent/> <any-child/> </any-service>
	</default-route>
	<default caps="100"/>

	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Timer"/> </provides>
	</start>
}

if { [have_include power_on/qemu] } {

	append_if [have_board pc] config {
		<start name="uart">
			<binary name="pc_uart"/>
			<resource name="RAM" quantum="2M"/>
			<provides>
				<service name="Terminal"/>
				<service name="Uart"/>
			</provides>
			<config>
				<policy label_prefix="socat" uart="1"/>
			</config>
		</start>
	}

	append_if [have_board rpi3] config {
		<start name="uart">
			<binary name="rpi3_uart"/>
			<resource name="RAM" quantum="2M"/>
			<provides>
				<service name="Terminal"/>
				<service name="Uart"/>
			</provides>
			<config>
				<policy label_prefix="socat" uart="0"/>
			</config>
		</start>
	}

} else {

	append config {
		<start name="drivers" caps="1000" managing_system="yes">
			<resource name="RAM" quantum="32M"/>
			<binary name="init"/>
			<route>
				<service name="ROM" label="config"> <parent label="drivers.config"/> </service>
				<service name="Timer"> <child name="timer"/> </service>
				<service name="Uplink"> <child name="nic_router"/> </service>
				<any-service> <parent/> </any-service>
			</route>
		</start>
		<start name="nic_router" caps="200">
			<resource name="RAM" quantum="10M"/>
			<provides>
				<service name="Nic"/>
				<service name="Uplink"/>
			</provides>
			<config verbose_domain_state="yes">

				<policy label_prefix="socat"   domain="downlink"/>
				<policy label_prefix="drivers" domain="uplink"/>

				<domain name="uplink">

					<nat domain="downlink"
						tcp-ports="1"
						udp-ports="1"
						icmp-ids="1"/>

					<tcp-forward port="5555" domain="downlink" to="10.0.3.2"/>

				</domain>

				<domain name="downlink" interface="10.0.3.1/24">

					<dhcp-server ip_first="10.0.3.2" ip_last="10.0.3.2"/>

					<tcp dst="0.0.0.0/0"><permit-any domain="uplink" /></tcp>

				</domain>

			</config>
		</start>
	}
}

append config {

	<start name="terminal_crosslink">
		<resource name="RAM" quantum="2M"/>
		<provides>
			<service name="Terminal"/>
		</provides>
	</start>

	<start name="vfs" caps="120">
		<resource name="RAM" quantum="30M"/>
		<provides><service name="File_system"/></provides>
		<config>
			<vfs>
				<tar name="socat.tar" />
			</vfs>
			<policy label_prefix="vfs_rom" root="/"/>
			<default-policy root="/" writeable="no"/>
		</config>
	</start>

	<start name="vfs_rom">
		<resource name="RAM" quantum="30M"/>
		<binary name="fs_rom"/>
		<provides> <service name="ROM"/> </provides>
		<config/>
		<route>
			<service name="File_system"> <child name="vfs"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>

	<start name="socat" caps="200">
		<binary name="/bin/socat"/>
		<resource name="RAM" quantum="30M" />
		<config>
			<vfs>
				<dir name="dev">
					<log/>
					<inline name="rtc">2018-01-01 00:01</inline>
					<terminal name="crosslink" label="crosslink"/>}
append_if [have_include power_on/qemu] config {
					<terminal name="uart" label="uart"/>
}
append config {
				</dir>}
if {![have_include power_on/qemu]} {
	append config {
				<dir name="socket"> <lwip dhcp="yes"/> </dir>
	}
}
append config {
				<fs/>
			</vfs>
			<libc stdout="/dev/log"
			      stderr="/dev/log"
			      rtc="/dev/rtc"
			      socket="/socket"/>
			<arg value="socat"/>
			<arg value="/dev/crosslink"/>}
if {[have_include power_on/qemu]} {
	append config {
			<arg value="/dev/uart"/>
	}
} else {
	append config {
			<arg value="TCP4-LISTEN:5555"/>
	}
}
append config {
		</config>
		<route>
			<service name="File_system"> <child name="vfs"/> </service>
			<service name="ROM" label_suffix=".lib.so"> <parent/> </service>
			<service name="ROM" label_last="/bin/socat"> <child name="vfs_rom"/> </service>
			<service name="Terminal" label="uart">
				<child name="uart"/>
			</service>
			<service name="Terminal" label="crosslink">
				<child name="terminal_crosslink"/>
			</service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

	<start name="test-terminal_echo">
		<resource name="RAM" quantum="1M"/>
		<route>
			<service name="Terminal"> <child name="terminal_crosslink"/> </service>
			<any-service> <parent/> <any-child/> </any-service>
		</route>
	</start>

</config>
}

install_config $config

build_boot_image [build_artifacts]

set port 5555

if {[have_include power_on/qemu]} {

	set host "localhost"

	# qemu config
	append qemu_args " -display none "

	if {[have_board rpi3]} {
		# connect comport 0 with TCP port $port
		append qemu_args " -serial chardev:uart "
		# connect comport 1 to stdio
		append qemu_args " -serial stdio "
	} else {
		# connect comport 0 to stdio
		append qemu_args " -serial stdio "
		# connect comport 1 with TCP port $port
		append qemu_args " -serial chardev:uart "
	}

	append qemu_args " -chardev socket,id=uart,port=$port,host=$host,server,nowait,ipv4 "

	run_genode_until {\[init -> uart\].*\n} 30

} else {

	set match_string "nic_router. .uplink. dynamic IP config: interface .*\n"

	run_genode_until $match_string 30

	regexp $match_string $output host
	regexp {[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+} $host host
}

# wait a moment for all components to become ready
after 2000

set genode_id [output_spawn_id]

eval spawn socat - TCP:$host:$port
set host_socat_id [list $spawn_id $genode_id]

send "test message\n"

# echo of input by host socat
run_genode_until {test message} 10 $host_socat_id

# log message from test-terminal_echo
run_genode_until {got 13 byte} 10 $genode_id

# intro message from test-terminal_echo
run_genode_until {--- Terminal echo test started - now you can type characters to be echoed. ---} 10 $host_socat_id

# echo from test-terminal_echo
run_genode_until {test message} 10 $host_socat_id
