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.
This commit is contained in:
parent
f70adde9a9
commit
5640293e8f
|
@ -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
|
||||
|
|
15
apps/er-coap/er-coap-communication.h
Normal file
15
apps/er-coap/er-coap-communication.h
Normal file
|
@ -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
|
|
@ -41,6 +41,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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();
|
||||
|
|
|
@ -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 -------------------------------------------------------------*/
|
||||
|
|
42
apps/er-coap/er-coap-udp.c
Normal file
42
apps/er-coap/er-coap-udp.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "er-coap-engine.h"
|
||||
#include "er-coap-communication.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#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();
|
||||
}
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue