From 5640293e8fd0e30b1b7c8932a6f58249947c3171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Wed, 7 Dec 2016 17:59:30 +0100 Subject: [PATCH] er-coap: separate the communication layer This code was copied and adapted from: https://github.com/cetic/6lbr Licensed under the same license as Contiki. --- apps/er-coap/Makefile.er-coap | 2 ++ apps/er-coap/er-coap-communication.h | 15 ++++++++++ apps/er-coap/er-coap-engine.c | 7 +++-- apps/er-coap/er-coap-engine.h | 1 + apps/er-coap/er-coap-udp.c | 42 ++++++++++++++++++++++++++++ apps/er-coap/er-coap.c | 23 ++------------- 6 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 apps/er-coap/er-coap-communication.h create mode 100644 apps/er-coap/er-coap-udp.c diff --git a/apps/er-coap/Makefile.er-coap b/apps/er-coap/Makefile.er-coap index 23b70613e..6a2ef7108 100755 --- a/apps/er-coap/Makefile.er-coap +++ b/apps/er-coap/Makefile.er-coap @@ -4,3 +4,5 @@ er-coap_src = er-coap.c er-coap-engine.c er-coap-transactions.c \ # Erbium will implement the REST Engine CFLAGS += -DREST=coap_rest_implementation + +er-coap_src += er-coap-udp.c diff --git a/apps/er-coap/er-coap-communication.h b/apps/er-coap/er-coap-communication.h new file mode 100644 index 000000000..bae0639da --- /dev/null +++ b/apps/er-coap/er-coap-communication.h @@ -0,0 +1,15 @@ +#ifndef _ER_COAP_COMMUNICATION_H_ +#define _ER_COAP_COMMUNICATION_H_ + +#include "contiki.h" + +void +coap_init_communication_layer(uint16_t port); + +void +coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length); + +void +coap_handle_receive(void); + +#endif diff --git a/apps/er-coap/er-coap-engine.c b/apps/er-coap/er-coap-engine.c index 872de013a..786eb092e 100644 --- a/apps/er-coap/er-coap-engine.c +++ b/apps/er-coap/er-coap-engine.c @@ -41,6 +41,7 @@ #include #include #include "er-coap-engine.h" +#include "er-coap-communication.h" #define DEBUG 0 #if DEBUG @@ -64,8 +65,8 @@ static service_callback_t service_cbk = NULL; /*---------------------------------------------------------------------------*/ /*- Internal API ------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -static int -coap_receive(void) +int +coap_receive() { erbium_status_code = NO_ERROR; @@ -346,7 +347,7 @@ PROCESS_THREAD(coap_engine, ev, data) PROCESS_YIELD(); if(ev == tcpip_event) { - coap_receive(); + coap_handle_receive(); } else if(ev == PROCESS_EVENT_TIMER) { /* retransmissions are handled here */ coap_check_transactions(); diff --git a/apps/er-coap/er-coap-engine.h b/apps/er-coap/er-coap-engine.h index c6c6ae676..5d40d1511 100644 --- a/apps/er-coap/er-coap-engine.h +++ b/apps/er-coap/er-coap-engine.h @@ -52,6 +52,7 @@ typedef coap_packet_t rest_request_t; typedef coap_packet_t rest_response_t; void coap_init_engine(void); +int coap_receive(); /*---------------------------------------------------------------------------*/ /*- Client Part -------------------------------------------------------------*/ diff --git a/apps/er-coap/er-coap-udp.c b/apps/er-coap/er-coap-udp.c new file mode 100644 index 000000000..6476d10c3 --- /dev/null +++ b/apps/er-coap/er-coap-udp.c @@ -0,0 +1,42 @@ +#include "contiki.h" +#include "contiki-net.h" +#include "er-coap-engine.h" +#include "er-coap-communication.h" + +#include + +#define DEBUG DEBUG_NONE +#include "uip-debug.h" + +static struct uip_udp_conn *udp_conn = NULL; + +/*-----------------------------------------------------------------------------------*/ +void +coap_init_communication_layer(uint16_t port) +{ + /* new connection with remote host */ + udp_conn = udp_new(NULL, 0, NULL); + udp_bind(udp_conn, port); + PRINTF("Listening on port %u\n", uip_ntohs(udp_conn->lport)); +} +/*-----------------------------------------------------------------------------------*/ +void +coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, uint16_t length) +{ + /* Configure connection to reply to client */ + uip_ipaddr_copy(&udp_conn->ripaddr, addr); + udp_conn->rport = port; + + uip_udp_packet_send(udp_conn, data, length); + PRINTF("-sent UDP datagram (%u)-\n", length); + + /* Restore server connection to allow data from any node */ + memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); + udp_conn->rport = 0; +} +/*-----------------------------------------------------------------------------------*/ +void +coap_handle_receive() +{ + coap_receive(); +} diff --git a/apps/er-coap/er-coap.c b/apps/er-coap/er-coap.c index 3c71ff6f9..c544f333c 100644 --- a/apps/er-coap/er-coap.c +++ b/apps/er-coap/er-coap.c @@ -44,6 +44,7 @@ #include "er-coap.h" #include "er-coap-transactions.h" +#include "er-coap-communication.h" #define DEBUG 0 #if DEBUG @@ -60,7 +61,6 @@ /*---------------------------------------------------------------------------*/ /*- Variables ---------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/ -static struct uip_udp_conn *udp_conn = NULL; static uint16_t current_mid = 0; coap_status_t erbium_status_code = NO_ERROR; @@ -279,9 +279,7 @@ void coap_init_connection(uint16_t port) { /* new connection with remote host */ - udp_conn = udp_new(NULL, 0, NULL); - udp_bind(udp_conn, port); - PRINTF("Listening on port %u\n", uip_ntohs(udp_conn->lport)); + coap_init_communication_layer(port); /* initialize transaction ID */ current_mid = random_rand(); @@ -422,23 +420,6 @@ coap_serialize_message(void *packet, uint8_t *buffer) return (option - buffer) + coap_pkt->payload_len; /* packet length */ } /*---------------------------------------------------------------------------*/ -void -coap_send_message(uip_ipaddr_t *addr, uint16_t port, uint8_t *data, - uint16_t length) -{ - /* configure connection to reply to client */ - uip_ipaddr_copy(&udp_conn->ripaddr, addr); - udp_conn->rport = port; - - uip_udp_packet_send(udp_conn, data, length); - - PRINTF("-sent UDP datagram (%u)-\n", length); - - /* restore server socket to allow data from any node */ - memset(&udp_conn->ripaddr, 0, sizeof(udp_conn->ripaddr)); - udp_conn->rport = 0; -} -/*---------------------------------------------------------------------------*/ coap_status_t coap_parse_message(void *packet, uint8_t *data, uint16_t data_len) {