1*cfb92d14SAndroid Build Coastguard Worker# OpenThread CLI - UDP Example 2*cfb92d14SAndroid Build Coastguard Worker 3*cfb92d14SAndroid Build Coastguard WorkerThe OpenThread UDP APIs may be invoked via the OpenThread CLI. 4*cfb92d14SAndroid Build Coastguard Worker 5*cfb92d14SAndroid Build Coastguard Worker## Quick Start 6*cfb92d14SAndroid Build Coastguard Worker 7*cfb92d14SAndroid Build Coastguard Worker### Form Network 8*cfb92d14SAndroid Build Coastguard Worker 9*cfb92d14SAndroid Build Coastguard WorkerForm a network with at least two devices. 10*cfb92d14SAndroid Build Coastguard Worker 11*cfb92d14SAndroid Build Coastguard Worker### Node 1 12*cfb92d14SAndroid Build Coastguard Worker 13*cfb92d14SAndroid Build Coastguard WorkerOn node 1, open and bind the example UDP socket. 14*cfb92d14SAndroid Build Coastguard Worker 15*cfb92d14SAndroid Build Coastguard Worker```bash 16*cfb92d14SAndroid Build Coastguard Worker> udp open 17*cfb92d14SAndroid Build Coastguard Worker> udp bind :: 1234 18*cfb92d14SAndroid Build Coastguard Worker``` 19*cfb92d14SAndroid Build Coastguard Worker 20*cfb92d14SAndroid Build Coastguard WorkerThe `::` specifies the IPv6 Unspecified Address. 21*cfb92d14SAndroid Build Coastguard Worker 22*cfb92d14SAndroid Build Coastguard Worker### Node 2 23*cfb92d14SAndroid Build Coastguard Worker 24*cfb92d14SAndroid Build Coastguard WorkerOn node 2, open the example UDP socket and send a simple message. 25*cfb92d14SAndroid Build Coastguard Worker 26*cfb92d14SAndroid Build Coastguard Worker```bash 27*cfb92d14SAndroid Build Coastguard Worker> udp open 28*cfb92d14SAndroid Build Coastguard Worker> udp send fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234 hello 29*cfb92d14SAndroid Build Coastguard Worker``` 30*cfb92d14SAndroid Build Coastguard Worker 31*cfb92d14SAndroid Build Coastguard Worker### Result 32*cfb92d14SAndroid Build Coastguard Worker 33*cfb92d14SAndroid Build Coastguard WorkerOn node 1, you should see a print out similar to below: 34*cfb92d14SAndroid Build Coastguard Worker 35*cfb92d14SAndroid Build Coastguard Worker```bash 36*cfb92d14SAndroid Build Coastguard Worker5 bytes from fdde:ad00:beef:0:dac3:6792:e2e:90d8 49153 hello 37*cfb92d14SAndroid Build Coastguard Worker``` 38*cfb92d14SAndroid Build Coastguard Worker 39*cfb92d14SAndroid Build Coastguard Worker## Command List 40*cfb92d14SAndroid Build Coastguard Worker 41*cfb92d14SAndroid Build Coastguard Worker- [help](#help) 42*cfb92d14SAndroid Build Coastguard Worker- [bind](#bind-netif-ip-port) 43*cfb92d14SAndroid Build Coastguard Worker- [close](#close) 44*cfb92d14SAndroid Build Coastguard Worker- [connect](#connect-ip-port) 45*cfb92d14SAndroid Build Coastguard Worker- [linksecurity](#linksecurity) 46*cfb92d14SAndroid Build Coastguard Worker- [open](#open) 47*cfb92d14SAndroid Build Coastguard Worker- [send](#send-ip-port-message) 48*cfb92d14SAndroid Build Coastguard Worker 49*cfb92d14SAndroid Build Coastguard Worker## Command Details 50*cfb92d14SAndroid Build Coastguard Worker 51*cfb92d14SAndroid Build Coastguard Worker### help 52*cfb92d14SAndroid Build Coastguard Worker 53*cfb92d14SAndroid Build Coastguard WorkerList the UDP CLI commands. 54*cfb92d14SAndroid Build Coastguard Worker 55*cfb92d14SAndroid Build Coastguard Worker```bash 56*cfb92d14SAndroid Build Coastguard Worker> udp help 57*cfb92d14SAndroid Build Coastguard Workerhelp 58*cfb92d14SAndroid Build Coastguard Workerbind 59*cfb92d14SAndroid Build Coastguard Workerclose 60*cfb92d14SAndroid Build Coastguard Workerconnect 61*cfb92d14SAndroid Build Coastguard Workeropen 62*cfb92d14SAndroid Build Coastguard Workersend 63*cfb92d14SAndroid Build Coastguard WorkerDone 64*cfb92d14SAndroid Build Coastguard Worker``` 65*cfb92d14SAndroid Build Coastguard Worker 66*cfb92d14SAndroid Build Coastguard Worker### bind [netif] \<ip\> \<port\> 67*cfb92d14SAndroid Build Coastguard Worker 68*cfb92d14SAndroid Build Coastguard WorkerAssigns a name (i.e. IPv6 address and port) to the example socket. 69*cfb92d14SAndroid Build Coastguard Worker 70*cfb92d14SAndroid Build Coastguard Worker- netif: the network interface to bind to. 71*cfb92d14SAndroid Build Coastguard Worker - not specified: Thread network interface. 72*cfb92d14SAndroid Build Coastguard Worker - `-u`: unspecified network interface. 73*cfb92d14SAndroid Build Coastguard Worker - `-b`: Backbone network interface. 74*cfb92d14SAndroid Build Coastguard Worker- ip: the unicast IPv6 address or the unspecified IPv6 address (`::`). 75*cfb92d14SAndroid Build Coastguard Worker- port: the UDP port 76*cfb92d14SAndroid Build Coastguard Worker 77*cfb92d14SAndroid Build Coastguard Worker```bash 78*cfb92d14SAndroid Build Coastguard Worker> udp bind :: 1234 79*cfb92d14SAndroid Build Coastguard WorkerDone 80*cfb92d14SAndroid Build Coastguard Worker> udp bind -u :: 1234 81*cfb92d14SAndroid Build Coastguard WorkerDone 82*cfb92d14SAndroid Build Coastguard Worker> udp bind -b :: 1234 83*cfb92d14SAndroid Build Coastguard WorkerDone 84*cfb92d14SAndroid Build Coastguard Worker``` 85*cfb92d14SAndroid Build Coastguard Worker 86*cfb92d14SAndroid Build Coastguard Worker> Note: to receive datagrams sent to a multicast IPv6 address, the unspecified IPv6 address must be used. Using a multicast address for the \<ip\> argument is not supported. Also, the node must subscribe to the multicast group using `ipmaddr add` before it can receive UDP multicast. 87*cfb92d14SAndroid Build Coastguard Worker 88*cfb92d14SAndroid Build Coastguard Worker### close 89*cfb92d14SAndroid Build Coastguard Worker 90*cfb92d14SAndroid Build Coastguard WorkerCloses the example socket. 91*cfb92d14SAndroid Build Coastguard Worker 92*cfb92d14SAndroid Build Coastguard Worker```bash 93*cfb92d14SAndroid Build Coastguard Worker> udp close 94*cfb92d14SAndroid Build Coastguard WorkerDone 95*cfb92d14SAndroid Build Coastguard Worker``` 96*cfb92d14SAndroid Build Coastguard Worker 97*cfb92d14SAndroid Build Coastguard Worker### connect \<ip\> \<port\> 98*cfb92d14SAndroid Build Coastguard Worker 99*cfb92d14SAndroid Build Coastguard WorkerSpecifies the peer with which the socket is to be associated. 100*cfb92d14SAndroid Build Coastguard Worker 101*cfb92d14SAndroid Build Coastguard Worker- ip: the peer's IP address. 102*cfb92d14SAndroid Build Coastguard Worker- port: the peer's UDP port. 103*cfb92d14SAndroid Build Coastguard Worker 104*cfb92d14SAndroid Build Coastguard Worker```bash 105*cfb92d14SAndroid Build Coastguard Worker> udp connect fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234 106*cfb92d14SAndroid Build Coastguard WorkerDone 107*cfb92d14SAndroid Build Coastguard Worker``` 108*cfb92d14SAndroid Build Coastguard Worker 109*cfb92d14SAndroid Build Coastguard WorkerThe address can be an IPv4 address, which will be synthesized to an IPv6 address using the preferred NAT64 prefix from the network data. 110*cfb92d14SAndroid Build Coastguard Worker 111*cfb92d14SAndroid Build Coastguard Worker> Note: The command will return `InvalidState` when the preferred NAT64 prefix is unavailable. 112*cfb92d14SAndroid Build Coastguard Worker 113*cfb92d14SAndroid Build Coastguard Worker```bash 114*cfb92d14SAndroid Build Coastguard Worker> udp connect 172.17.0.1 1234 115*cfb92d14SAndroid Build Coastguard WorkerConnecting to synthesized IPv6 address: fdde:ad00:beef:2:0:0:ac11:1 116*cfb92d14SAndroid Build Coastguard WorkerDone 117*cfb92d14SAndroid Build Coastguard Worker``` 118*cfb92d14SAndroid Build Coastguard Worker 119*cfb92d14SAndroid Build Coastguard Worker### linksecurity 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard WorkerIndicates whether the link security is enabled or disabled. 122*cfb92d14SAndroid Build Coastguard Worker 123*cfb92d14SAndroid Build Coastguard Worker```bash 124*cfb92d14SAndroid Build Coastguard Worker> udp linksecurity 125*cfb92d14SAndroid Build Coastguard WorkerEnabled 126*cfb92d14SAndroid Build Coastguard WorkerDone 127*cfb92d14SAndroid Build Coastguard Worker``` 128*cfb92d14SAndroid Build Coastguard Worker 129*cfb92d14SAndroid Build Coastguard Worker### linksecurity enable 130*cfb92d14SAndroid Build Coastguard Worker 131*cfb92d14SAndroid Build Coastguard WorkerEnable link security. 132*cfb92d14SAndroid Build Coastguard Worker 133*cfb92d14SAndroid Build Coastguard Worker```bash 134*cfb92d14SAndroid Build Coastguard Worker> udp linksecurity enable 135*cfb92d14SAndroid Build Coastguard WorkerDone 136*cfb92d14SAndroid Build Coastguard Worker``` 137*cfb92d14SAndroid Build Coastguard Worker 138*cfb92d14SAndroid Build Coastguard Worker### linksecurity disable 139*cfb92d14SAndroid Build Coastguard Worker 140*cfb92d14SAndroid Build Coastguard WorkerDisable link security. 141*cfb92d14SAndroid Build Coastguard Worker 142*cfb92d14SAndroid Build Coastguard Worker```bash 143*cfb92d14SAndroid Build Coastguard Worker> udp linksecurity disable 144*cfb92d14SAndroid Build Coastguard WorkerDone 145*cfb92d14SAndroid Build Coastguard Worker``` 146*cfb92d14SAndroid Build Coastguard Worker 147*cfb92d14SAndroid Build Coastguard Worker### open 148*cfb92d14SAndroid Build Coastguard Worker 149*cfb92d14SAndroid Build Coastguard WorkerOpens the example socket. 150*cfb92d14SAndroid Build Coastguard Worker 151*cfb92d14SAndroid Build Coastguard Worker```bash 152*cfb92d14SAndroid Build Coastguard Worker> udp open 153*cfb92d14SAndroid Build Coastguard WorkerDone 154*cfb92d14SAndroid Build Coastguard Worker``` 155*cfb92d14SAndroid Build Coastguard Worker 156*cfb92d14SAndroid Build Coastguard Worker### send \<ip\> \<port\> \<message\> 157*cfb92d14SAndroid Build Coastguard Worker 158*cfb92d14SAndroid Build Coastguard WorkerSend a UDP message. 159*cfb92d14SAndroid Build Coastguard Worker 160*cfb92d14SAndroid Build Coastguard Worker- ip: the destination address. 161*cfb92d14SAndroid Build Coastguard Worker- port: the UDP destination port. 162*cfb92d14SAndroid Build Coastguard Worker- message: the message to send. 163*cfb92d14SAndroid Build Coastguard Worker 164*cfb92d14SAndroid Build Coastguard Worker```bash 165*cfb92d14SAndroid Build Coastguard Worker> udp send fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234 hello 166*cfb92d14SAndroid Build Coastguard WorkerDone 167*cfb92d14SAndroid Build Coastguard Worker``` 168*cfb92d14SAndroid Build Coastguard Worker 169*cfb92d14SAndroid Build Coastguard WorkerThe address can be an IPv4 address, which will be synthesized to an IPv6 address using the preferred NAT64 prefix from the network data. 170*cfb92d14SAndroid Build Coastguard Worker 171*cfb92d14SAndroid Build Coastguard Worker> Note: The command will return `InvalidState` when the preferred NAT64 prefix is unavailable. 172*cfb92d14SAndroid Build Coastguard Worker 173*cfb92d14SAndroid Build Coastguard Worker```bash 174*cfb92d14SAndroid Build Coastguard Worker> udp send 172.17.0.1 1234 hello 175*cfb92d14SAndroid Build Coastguard WorkerSending to synthesized IPv6 address: fdde:ad00:beef:2:0:0:ac11:1 176*cfb92d14SAndroid Build Coastguard WorkerDone 177*cfb92d14SAndroid Build Coastguard Worker``` 178*cfb92d14SAndroid Build Coastguard Worker 179*cfb92d14SAndroid Build Coastguard Worker### send \<ip\> \<port\> \<type\> \<value\> 180*cfb92d14SAndroid Build Coastguard Worker 181*cfb92d14SAndroid Build Coastguard WorkerSend a few bytes over UDP. 182*cfb92d14SAndroid Build Coastguard Worker 183*cfb92d14SAndroid Build Coastguard Worker- ip: the IPv6 destination address. 184*cfb92d14SAndroid Build Coastguard Worker- port: the UDP destination port. 185*cfb92d14SAndroid Build Coastguard Worker- type: the type of the message: 186*cfb92d14SAndroid Build Coastguard Worker - `-t`: text payload in the `value`, same as without specifying the type. 187*cfb92d14SAndroid Build Coastguard Worker - `-s`: autogenerated payload with specified length indicated in the `value`. 188*cfb92d14SAndroid Build Coastguard Worker - `-x`: binary data in hexadecimal representation in the `value`. 189*cfb92d14SAndroid Build Coastguard Worker 190*cfb92d14SAndroid Build Coastguard Worker```bash 191*cfb92d14SAndroid Build Coastguard Worker> udp send fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234 -t hello 192*cfb92d14SAndroid Build Coastguard WorkerDone 193*cfb92d14SAndroid Build Coastguard Worker 194*cfb92d14SAndroid Build Coastguard Worker> udp send fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234 -x 68656c6c6f 195*cfb92d14SAndroid Build Coastguard WorkerDone 196*cfb92d14SAndroid Build Coastguard Worker 197*cfb92d14SAndroid Build Coastguard Worker> udp send fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234 -s 800 198*cfb92d14SAndroid Build Coastguard WorkerDone 199*cfb92d14SAndroid Build Coastguard Worker 200*cfb92d14SAndroid Build Coastguard Worker``` 201*cfb92d14SAndroid Build Coastguard Worker 202*cfb92d14SAndroid Build Coastguard Worker### send \<message\> 203*cfb92d14SAndroid Build Coastguard Worker 204*cfb92d14SAndroid Build Coastguard WorkerSend a UDP message on a connected socket. 205*cfb92d14SAndroid Build Coastguard Worker 206*cfb92d14SAndroid Build Coastguard Worker- message: the message to send. 207*cfb92d14SAndroid Build Coastguard Worker 208*cfb92d14SAndroid Build Coastguard Worker```bash 209*cfb92d14SAndroid Build Coastguard Worker> udp send hello 210*cfb92d14SAndroid Build Coastguard WorkerDone 211*cfb92d14SAndroid Build Coastguard Worker``` 212*cfb92d14SAndroid Build Coastguard Worker 213*cfb92d14SAndroid Build Coastguard Worker### send \<type\> \<value\> 214*cfb92d14SAndroid Build Coastguard Worker 215*cfb92d14SAndroid Build Coastguard WorkerSend a few bytes over UDP. 216*cfb92d14SAndroid Build Coastguard Worker 217*cfb92d14SAndroid Build Coastguard Worker- type: the type of the message: 218*cfb92d14SAndroid Build Coastguard Worker - `-t`: text payload in the `value`, same as without specifying the type. 219*cfb92d14SAndroid Build Coastguard Worker - `-s`: autogenerated payload with specified length indicated in the `value`. 220*cfb92d14SAndroid Build Coastguard Worker - `-x`: binary data in hexadecimal representation in the `value`. 221*cfb92d14SAndroid Build Coastguard Worker 222*cfb92d14SAndroid Build Coastguard Worker```bash 223*cfb92d14SAndroid Build Coastguard Worker> udp send -t hello 224*cfb92d14SAndroid Build Coastguard WorkerDone 225*cfb92d14SAndroid Build Coastguard Worker 226*cfb92d14SAndroid Build Coastguard Worker> udp send -x 68656c6c6f 227*cfb92d14SAndroid Build Coastguard WorkerDone 228*cfb92d14SAndroid Build Coastguard Worker 229*cfb92d14SAndroid Build Coastguard Worker> udp send -s 800 230*cfb92d14SAndroid Build Coastguard WorkerDone 231*cfb92d14SAndroid Build Coastguard Worker``` 232