Skip to content

Commit

Permalink
WIP - split out coap_context
Browse files Browse the repository at this point in the history
  • Loading branch information
franc0is committed Sep 6, 2015
1 parent 57650d1 commit f0eb4e3
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 258 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \
src/async.c \
src/block.c \
src/coap_io.c \
src/coap_context.c \
src/debug.c \
src/encode.c \
src/hashkey.c \
Expand All @@ -77,6 +78,7 @@ libcoap_include_HEADERS = \
include/coap/bits.h \
include/coap/block.h \
include/coap/coap.h \
include/coap/coap_context.h \
include/coap/coap_io.h \
include/coap/coap_time.h \
include/coap/debug.h \
Expand Down
110 changes: 110 additions & 0 deletions include/coap/coap_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#ifndef _COAP_CONTEXT_H_
#define _COAP_CONTEXT_H_

#include <stdlib.h>
#include <string.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#include "pdu.h"
#include "coap_time.h"

typedef struct coap_queue_t { // TODO create coap_queue.h
struct coap_queue_t *next;
coap_tick_t t; /**< when to send PDU for the next time */
unsigned char retransmit_cnt; /**< retransmission counter, will be removed
* when zero */
unsigned int timeout; /**< the randomized timeout value */
coap_endpoint_t local_if; /**< the local address interface */
coap_address_t remote; /**< remote address */
coap_tid_t id; /**< unique transaction id */
coap_pdu_t *pdu; /**< the CoAP PDU to send */
} coap_queue_t;

/** Message handler that is used as call-back in coap_context_t */
typedef void (*coap_response_handler_t)(struct coap_context_t *,
const coap_endpoint_t *local_interface,
const coap_address_t *remote,
coap_pdu_t *sent,
coap_pdu_t *received,
const coap_tid_t id);

/** The CoAP stack's global state is stored in a coap_context_t object */
typedef struct coap_context_t {
coap_opt_filter_t known_options;
struct coap_resource_t *resources; /**< hash table or list of known resources */

#ifndef WITHOUT_ASYNC
/**
* list of asynchronous transactions */
struct coap_async_state_t *async_state;
#endif /* WITHOUT_ASYNC */

/**
* The time stamp in the first element of the sendqeue is relative
* to sendqueue_basetime. */
coap_tick_t sendqueue_basetime;
coap_queue_t *sendqueue;
coap_endpoint_t *endpoint; /**< the endpoint used for listening */

#ifdef WITH_POSIX
int sockfd; /**< send/receive socket */
#endif /* WITH_POSIX */

#ifdef WITH_CONTIKI
struct uip_udp_conn *conn; /**< uIP connection object */
struct etimer retransmit_timer; /**< fires when the next packet must be sent */
struct etimer notify_timer; /**< used to check resources periodically */
#endif /* WITH_CONTIKI */

#ifdef WITH_LWIP
uint8_t timer_configured; /**< Set to 1 when a retransmission is
* scheduled using lwIP timers for this
* context, otherwise 0. */
#endif /* WITH_LWIP */

/**
* The last message id that was used is stored in this field. The initial
* value is set by coap_new_context() and is usually a random value. A new
* message id can be created with coap_new_message_id().
*/
unsigned short message_id;

/**
* The next value to be used for Observe. This field is global for all
* resources and will be updated when notifications are created.
*/
unsigned int observe;

coap_response_handler_t response_handler;

ssize_t (*network_send)(struct coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
unsigned char *data, size_t datalen);

ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet);

} coap_context_t;

/**
* Creates a new coap_context_t object that will hold the CoAP stack status.
*/
coap_context_t *coap_new_context(const coap_address_t *listen_addr);

/**
* CoAP stack context must be released with coap_free_context(). This function
* clears all entries from the receive queue and send queue and deletes the
* resources that have been registered with @p context, and frees the attached
* endpoints.
*/
void coap_free_context(coap_context_t *context);

#endif /* _COAP_CONTEXT_H_ */
95 changes: 1 addition & 94 deletions include/coap/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,11 @@ extern "C" {
#include "coap_io.h"
#include "prng.h"
#include "pdu.h"
#include "coap_context.h"
#include "coap_time.h"

#include "platform_utils.h"

struct coap_queue_t;

typedef struct coap_queue_t {
struct coap_queue_t *next;
coap_tick_t t; /**< when to send PDU for the next time */
unsigned char retransmit_cnt; /**< retransmission counter, will be removed
* when zero */
unsigned int timeout; /**< the randomized timeout value */
coap_endpoint_t local_if; /**< the local address interface */
coap_address_t remote; /**< remote address */
coap_tid_t id; /**< unique transaction id */
coap_pdu_t *pdu; /**< the CoAP PDU to send */
} coap_queue_t;

/** Adds node to given queue, ordered by node->t. */
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);

Expand All @@ -78,78 +65,12 @@ struct coap_context_t;
struct coap_async_state_t;
#endif

/** Message handler that is used as call-back in coap_context_t */
typedef void (*coap_response_handler_t)(struct coap_context_t *,
const coap_endpoint_t *local_interface,
const coap_address_t *remote,
coap_pdu_t *sent,
coap_pdu_t *received,
const coap_tid_t id);

#define COAP_MID_CACHE_SIZE 3
typedef struct {
unsigned char flags[COAP_MID_CACHE_SIZE];
coap_key_t item[COAP_MID_CACHE_SIZE];
} coap_mid_cache_t;

/** The CoAP stack's global state is stored in a coap_context_t object */
typedef struct coap_context_t {
coap_opt_filter_t known_options;
struct coap_resource_t *resources; /**< hash table or list of known resources */

#ifndef WITHOUT_ASYNC
/**
* list of asynchronous transactions */
struct coap_async_state_t *async_state;
#endif /* WITHOUT_ASYNC */

/**
* The time stamp in the first element of the sendqeue is relative
* to sendqueue_basetime. */
coap_tick_t sendqueue_basetime;
coap_queue_t *sendqueue;
coap_endpoint_t *endpoint; /**< the endpoint used for listening */

#ifdef WITH_POSIX
int sockfd; /**< send/receive socket */
#endif /* WITH_POSIX */

#ifdef WITH_CONTIKI
struct uip_udp_conn *conn; /**< uIP connection object */
struct etimer retransmit_timer; /**< fires when the next packet must be sent */
struct etimer notify_timer; /**< used to check resources periodically */
#endif /* WITH_CONTIKI */

#ifdef WITH_LWIP
uint8_t timer_configured; /**< Set to 1 when a retransmission is
* scheduled using lwIP timers for this
* context, otherwise 0. */
#endif /* WITH_LWIP */

/**
* The last message id that was used is stored in this field. The initial
* value is set by coap_new_context() and is usually a random value. A new
* message id can be created with coap_new_message_id().
*/
unsigned short message_id;

/**
* The next value to be used for Observe. This field is global for all
* resources and will be updated when notifications are created.
*/
unsigned int observe;

coap_response_handler_t response_handler;

ssize_t (*network_send)(struct coap_context_t *context,
const coap_endpoint_t *local_interface,
const coap_address_t *dst,
unsigned char *data, size_t datalen);

ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet);

} coap_context_t;

/**
* Registers a new message handler that is called whenever a response was
* received that matches an ongoing transaction.
Expand Down Expand Up @@ -192,11 +113,6 @@ coap_queue_t *coap_peek_next( coap_context_t *context );
*/
coap_queue_t *coap_pop_next( coap_context_t *context );

/**
* Creates a new coap_context_t object that will hold the CoAP stack status.
*/
coap_context_t *coap_new_context(const coap_address_t *listen_addr);

/**
* Returns a new message id and updates @p context->message_id accordingly. The
* message id is returned in network byte order to make it easier to read in
Expand All @@ -212,15 +128,6 @@ coap_new_message_id(coap_context_t *context) {
return HTONS(context->message_id);
}

/**
* CoAP stack context must be released with coap_free_context(). This function
* clears all entries from the receive queue and send queue and deletes the
* resources that have been registered with @p context, and frees the attached
* endpoints.
*/
void coap_free_context(coap_context_t *context);


/**
* Sends a confirmed CoAP message to given destination. The memory that is
* allocated by pdu will not be released by coap_send_confirmed(). The caller
Expand Down
2 changes: 0 additions & 2 deletions platform/posix/platform_utils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#ifndef _PLATFORM_UTILS_H_
#define _PLATFORM_UTILS_H_

#define HTONS(BYTES) htons(BYTES);

#endif /* _PLATFORM_UTILS_H_ */
Loading

0 comments on commit f0eb4e3

Please sign in to comment.