To use tinyDTLS as Contiki application, place the source code in the directory apps/tinydtls
in the Contiki source tree and invoke configure with the option –with-contiki
. This will define WITH_CONTIKI in tinydtls.h and include Makefile.contiki
in the main Makefile. To cross-compile for another platform you will need to set your host and build system accordingly. For example, when configuring for ARM, you would invoke
./configure --with-contiki --build=x86_64-linux-gnu --host=arm-none-eabi
on an x86_64 linux host.
Then, create a Contiki project with APPS
+= tinydtls in its Makefile. A sample server could look like this (with read_from_peer() and get_psk_key() as shown above).
#include "contiki.h"
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
#define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
static struct uip_udp_conn *server_conn;
.read = read_from_peer,
.event = NULL,
.get_psk_key = get_psk_key
};
PROCESS(server_process, "DTLS server process");
AUTOSTART_PROCESSES(&server_process);
PROCESS_THREAD(server_process, ev, data)
{
PROCESS_BEGIN();
server_conn = udp_new(NULL, 0, NULL);
udp_bind(server_conn, UIP_HTONS(5684));
if (!dtls_context) {
PROCESS_EXIT();
}
while(1) {
PROCESS_WAIT_EVENT();
if(ev == tcpip_event && uip_newdata()) {
uip_ipaddr_copy(&session.
addr, &UIP_IP_BUF->srcipaddr);
session.port = UIP_UDP_BUF->srcport;
session.
size =
sizeof(session.
addr) +
sizeof(session.port);
}
}
PROCESS_END();
}
uip_ipaddr_copy(&conn->ripaddr, &session->
addr);
conn->rport = session->port;
uip_udp_packet_send(conn, data, len);
memset(&conn->ripaddr, 0, sizeof(server_conn->ripaddr));
memset(&conn->rport, 0, sizeof(conn->rport));
return len;
}