Skip to main content

How to use HDC

This section will describe how to use hdc.

Download SDK

You can download the corresponding version of the Public SDK package by clicking the button below, please select the appropriate version starting with toolchains for your system.

Download Public SDK
tar zxf ohos-sdk-windows_linux-public.tar.gz -C ~ &&
cd ~/ohos-sdk/linux &&
unzip toolchains-linux-x64-3.2.13.5-Release.zip &&
sudo ln -sf ~/ohos-sdk/linux/toolchains/hdc /usr/local/bin/hdc

Configure the environment

For computer in the physical environment

Use the command to create a virtual bridge.

sudo modprobe tun tap &&
sudo ip link add br0 type bridge &&
sudo ip address add 192.168.137.1/24 dev br0 &&
sudo ip link set dev br0 up

Create a bridge configuration file, which is included in the official installer.

mkdir -p /path/to/qemu/etc/qemu/ &&
echo "allow all" >> /path/to/qemu/etc/qemu/bridge.conf

Use the command Allow to connect to an external network (Optional).

  1. Configure iptables
ip -o route get 8/8 2>/dev/null | grep -o 'dev [^ ]*' | cut -d' ' -f2 # Get networkable interface
eth0

sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE # Replace eth0 with a networkable interface on your computer
  1. Open ip_forward
sudo sysctl net.ipv4.ip_forward=1
  1. Disable the firewall
sudo ufw disable

For computer in the virtual environment

note

Please set the netdev option -netdev bridge,id=net0 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 and use sudo to run qemu-system-riscv64.

Enter the system and set ip and route.

ifconfig eth0 192.168.137.2/24 && route add default gw 192.168.137.1

By default, the system will start hdcd automatically. You can check the port number currently used by hdcd with the command.

note

The default port number is 5555, if there is a port conflict, a new port will be randomly assigned.

# netstat -nap | grep hdcd | grep tcp
tcp 0 0 0.0.0.0:5555 0.0.0.0:* LISTEN 104/hdcd

If the process did not start, you can start the hdcd process manually.

# hdcd -t
[D][2023-07-17 09:08:01.236][e8c5ef08][main.cpp:141] Foreground cli-mode
Option TCP enabled
[D][2023-07-17 09:08:01.252][e8c5ef08][main.cpp:304] HdcDaemon main run
[I][2023-07-17 09:08:01.257][e8c5ef08][session.cpp:25] Program running. Ver: 1.1.2a Pid:2394
[I][2023-07-17 09:08:01.259][e8c5ef08][session.cpp:33] set UV_THREADPOOL_SIZE:16
[D][2023-07-17 09:08:01.267][e8c5ef08][session.cpp:44] loopMain init
[D][2023-07-17 09:08:01.271][e8c5ef08][daemon.cpp:99] HdcDaemon InitMod
[D][2023-07-17 09:08:01.275][e8c5ef08][daemon.cpp:101] bEnableTCP:1,bEnableUSB:0
[D][2023-07-17 09:08:01.279][e8c5ef08][daemon_tcp.cpp:136] HdcDaemonTCP init
[I][2023-07-17 09:08:01.288][e8c5ef08][daemon_tcp.cpp:143] TCP listen on port:[45111]
[D][2023-07-17 09:08:01.291][e8c5ef08][daemon.cpp:122] bEnableUART:0

Connect the virtual machine

Connect the device in the physical machine based on the tcp port returned by hdcd in the virtual machine.

$ hdc tconn 192.168.137.2:5555
Connect OK
$ hdc list targets -v
192.168.137.2:5555 TCP Connected localhost hdc