1c87c5fbaSopenharmony_ciExample of libcoap running on lwIP 2c87c5fbaSopenharmony_ci================================== 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_ciTo run the server example, do 5c87c5fbaSopenharmony_ci 6c87c5fbaSopenharmony_ci $ make 7c87c5fbaSopenharmony_ci $ sudo ./server # No TinyDTLS support 8c87c5fbaSopenharmony_ci or 9c87c5fbaSopenharmony_ci $ sudo ./server-dtls # With TinyDTLS support 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ci(and in a second terminal) 12c87c5fbaSopenharmony_ci 13c87c5fbaSopenharmony_ci $ sudo ip a a dev tap0 192.168.113.1/24 14c87c5fbaSopenharmony_ci 15c87c5fbaSopenharmony_ciand query `coap://192.168.113.2/time?ticks` with any coap tool, 16c87c5fbaSopenharmony_cior query `coap://192.168.113.2/.well-known/core`. 17c87c5fbaSopenharmony_ci(If server-dtls is running, you can use coaps:// as appropriate.) 18c87c5fbaSopenharmony_ci 19c87c5fbaSopenharmony_ciThis will 20c87c5fbaSopenharmony_ci 21c87c5fbaSopenharmony_ci* download lwip and lwip-contrib from the upstream git sources 22c87c5fbaSopenharmony_ci* build the server application 23c87c5fbaSopenharmony_ci* run the server application, creating a virtual network device tap0 (unless 24c87c5fbaSopenharmony_ci that exists) 25c87c5fbaSopenharmony_ci* configure your network interface to make the server accessible. 26c87c5fbaSopenharmony_ci 27c87c5fbaSopenharmony_ci* return the appropriate response from the server to the client. 28c87c5fbaSopenharmony_ci 29c87c5fbaSopenharmony_ciThe server supports the following options 30c87c5fbaSopenharmony_ci"-k PSK" option where PSK defines the DTLS PSK to use (default is "secretPSK"). 31c87c5fbaSopenharmony_ci (Only works for server-dtls.) 32c87c5fbaSopenharmony_ci"-v level" option where logging "level" can be 0 to 7 (default is 4). 33c87c5fbaSopenharmony_ci"-V level" option where DTLS logging "level" can be 0 to 7 (default is 3). 34c87c5fbaSopenharmony_ci (Only works for server-dtls.) 35c87c5fbaSopenharmony_ci 36c87c5fbaSopenharmony_ciThe server creates a resource for 'time' with a query 'ticks'. This is 37c87c5fbaSopenharmony_cireported for `.well-known/core`. The work flow for adding more resources does 38c87c5fbaSopenharmony_cinot differ from regular libcoap usage. If you seem to run out of memory 39c87c5fbaSopenharmony_cicreating the resources, tweak the number of pre-allocated resources 40c87c5fbaSopenharmony_ciin `config/lwippools.h`. 41c87c5fbaSopenharmony_ci 42c87c5fbaSopenharmony_ciTo run the client example 43c87c5fbaSopenharmony_ci 44c87c5fbaSopenharmony_ci $ make 45c87c5fbaSopenharmony_ci $ sudo ./client # No TinyDTLS support 46c87c5fbaSopenharmony_ci or 47c87c5fbaSopenharmony_ci $ sudo ./client-dtls # With TinyDTLS support 48c87c5fbaSopenharmony_ci 49c87c5fbaSopenharmony_ciAs client (or client-dtls) tries to connect to coap://libcoap.net/, the tap0 50c87c5fbaSopenharmony_ciinterface will need IP forwarding enabled 51c87c5fbaSopenharmony_ci 52c87c5fbaSopenharmony_ci $ sudo sysctl -w net.ipv4.conf.default.forwarding=1 53c87c5fbaSopenharmony_ci 54c87c5fbaSopenharmony_ciThen you will need IP forwarding enabled on your public interface 55c87c5fbaSopenharmony_ci(where eth0 is replaced by your public facing interface name) 56c87c5fbaSopenharmony_cifor response packets 57c87c5fbaSopenharmony_ci 58c87c5fbaSopenharmony_ci $ sudo sysctl -w net.eth0.conf.default.forwarding=1 59c87c5fbaSopenharmony_ci 60c87c5fbaSopenharmony_ciAs well as the interface connecting to the internet will need a NAT rule to 61c87c5fbaSopenharmony_cimasquerade the internal IP address (192.168.114.2) to the IP of the outgoing 62c87c5fbaSopenharmony_ciinterface (where eth0 is replaced by your public facing interface name) 63c87c5fbaSopenharmony_ci 64c87c5fbaSopenharmony_ci $ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 65c87c5fbaSopenharmony_ci 66c87c5fbaSopenharmony_ciThe client supports the following options 67c87c5fbaSopenharmony_ci"-k PSK" option where PSK defines the DTLS PSK to use (default is "secretPSK"). 68c87c5fbaSopenharmony_ci (Only works for client-dtls.) 69c87c5fbaSopenharmony_ci"-u id" option where id defines the DTLS id to use (default is "abc"). 70c87c5fbaSopenharmony_ci (Only works for client-dtls.) 71c87c5fbaSopenharmony_ci"-v level" option where logging "level" can be 0 to 7 (default is 4). 72c87c5fbaSopenharmony_ci"-V level" option where DTLS logging "level" can be 0 to 7 (default is 3). 73c87c5fbaSopenharmony_ci (Only works for client-dtls.) 74c87c5fbaSopenharmony_ci 75c87c5fbaSopenharmony_ciThe client supports an optional parameter which is the CoAP URI to connect to 76c87c5fbaSopenharmony_ci.e.g "coap://libcoap.net/.well-known/core". The default 77c87c5fbaSopenharmony_ciis "coap://libcoap.net/" for client and client-dtls. 78c87c5fbaSopenharmony_ciUsing "coaps://libcoap.net" will establish a DTLS session if there is 79c87c5fbaSopenharmony_ciDTLS support compiled in (client-dtls). 80