async.c
Go to the documentation of this file.
/* async.c -- state management for asynchronous messages
*
* Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#ifndef WITHOUT_ASYNC
#include "coap_config.h"
#include "coap.h"
#include "async.h"
#include "debug.h"
#include "mem.h"
#include "utlist.h"
coap_async_state_t *
coap_register_async(coap_context_t *context, coap_address_t *peer,
coap_pdu_t *request, unsigned char flags, void *data) {
coap_async_state_t *s;
coap_tid_t id;
coap_transaction_id(peer, request, &id);
LL_SEARCH_SCALAR(context->async_state,s,id,id);
if (s != NULL) {
/* We must return NULL here as the caller must know that he is
* responsible for releasing @p data. */
debug("asynchronous state for transaction %d already registered\n", id);
return NULL;
}
/* store information for handling the asynchronous task */
s = (coap_async_state_t *)coap_malloc(sizeof(coap_async_state_t) +
request->hdr->token_length);
if (!s) {
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}
memset(s, 0, sizeof(coap_async_state_t) + request->hdr->token_length);
/* set COAP_ASYNC_CONFIRM according to request's type */
s->flags = flags & ~COAP_ASYNC_CONFIRM;
if (request->hdr->type == COAP_MESSAGE_CON)
s->flags |= COAP_ASYNC_CONFIRM;
s->appdata = data;
memcpy(&s->peer, peer, sizeof(coap_address_t));
if (request->hdr->token_length) {
s->tokenlen = request->hdr->token_length;
memcpy(s->token, request->hdr->token, request->hdr->token_length);
}
memcpy(&s->id, &id, sizeof(coap_tid_t));
coap_touch_async(s);
LL_PREPEND(context->async_state, s);
return s;
}
coap_async_state_t *
coap_find_async(coap_context_t *context, coap_tid_t id) {
coap_async_state_t *tmp;
LL_SEARCH_SCALAR(context->async_state,tmp,id,id);
return tmp;
}
int
coap_remove_async(coap_context_t *context, coap_tid_t id,
coap_async_state_t **s) {
coap_async_state_t *tmp = coap_find_async(context, id);
if (tmp)
LL_DELETE(context->async_state,tmp);
*s = tmp;
return tmp != NULL;
}
void
coap_free_async(coap_async_state_t *s) {
if (s && (s->flags & COAP_ASYNC_RELEASE_DATA) != 0)
coap_free(s->appdata);
coap_free(s);
}
#else
void does_not_exist(); /* make some compilers happy */
#endif /* WITHOUT_ASYNC */
coap_async_state_t::appdata
void * appdata
This field can be used to register opaque application data with the asynchronous state object...
Definition: async.h:45
coap_hdr_t::token
unsigned char token[]
Definition: pdu.h:192
mem.h
coap_malloc
static void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:70
debug.h
coap_address_t
multi-purpose address abstraction
Definition: address.h:59
async.h
State management for asynchronous messages.
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
LL_PREPEND
#define LL_PREPEND(head, add)
Definition: utlist.h:309
coap_async_state_t::flags
unsigned char flags
holds the flags to control behaviour
Definition: async.h:32
coap_async_state_t::peer
coap_address_t peer
the peer to notify
Definition: async.h:49
coap_context_t::async_state
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:83
coap_pdu_t::hdr
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
Definition: pdu.h:229
coap_async_state_t
Definition: async.h:31
coap_transaction_id
void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
Calculates a unique transaction id from given arguments peer and pdu.
Definition: net.c:495
debug
#define debug(...)
Definition: debug.h:66
coap_async_state_t::id
coap_tid_t id
transaction id
Definition: async.h:47
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_register_async
coap_async_state_t * coap_register_async(coap_context_t *context, coap_address_t *peer, coap_pdu_t *request, unsigned char flags, void *data)
Allocates a new coap_async_state_t object and fills its fields according to the given request...
Definition: async.c:24
utlist.h
COAP_ASYNC_CONFIRM
#define COAP_ASYNC_CONFIRM
send confirmable response
Definition: async.h:57
COAP_MESSAGE_CON
#define COAP_MESSAGE_CON
Definition: pdu.h:43
COAP_ASYNC_RELEASE_DATA
#define COAP_ASYNC_RELEASE_DATA
release application data on destruction
Definition: async.h:62
LOG_CRIT
Definition: debug.h:29
coap_touch_async
static void coap_touch_async(coap_async_state_t *s)
Updates the time stamp of s.
Definition: async.h:140
coap_remove_async
int coap_remove_async(coap_context_t *context, coap_tid_t id, coap_async_state_t **s)
Removes the state object identified by id from context.
Definition: async.c:80
coap_find_async
coap_async_state_t * coap_find_async(coap_context_t *context, coap_tid_t id)
Retrieves the object identified by id from the list of asynchronous transactions that are registered ...
Definition: async.c:73
coap_free_async
void coap_free_async(coap_async_state_t *s)
Releases the memory that was allocated by coap_async_state_init() for the object s.
Definition: async.c:92
coap_async_state_t::tokenlen
size_t tokenlen
length of the token
Definition: async.h:50
coap_hdr_t::token_length
unsigned int token_length
Definition: pdu.h:186
coap_free
static void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:77
LL_DELETE
#define LL_DELETE(head, del)
Definition: utlist.h:349
coap_async_state_t::token
unsigned char token[]
the token to use in a response
Definition: async.h:51
coap_hdr_t::type
unsigned int type
Definition: pdu.h:187
LL_SEARCH_SCALAR
#define LL_SEARCH_SCALAR(head, out, field, val)
Definition: utlist.h:440
coap_log
#define coap_log(...)
Definition: debug.h:58
coap.h
coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76