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.
*/
#include "coap_internal.h"
#ifndef WITHOUT_ASYNC
/* utlist-style macros for searching pairs in linked lists */
#define SEARCH_PAIR(head,out,field1,val1,field2,val2) \
SEARCH_PAIR2(head,out,field1,val1,field2,val2,next)
#define SEARCH_PAIR2(head,out,field1,val1,field2,val2,next) \
do { \
LL_FOREACH2(head,out,next) { \
if ((out)->field1 == (val1) && (out)->field2 == (val2)) break; \
} \
} while(0)
coap_async_state_t *
coap_register_async(coap_context_t *context, coap_session_t *session,
coap_pdu_t *request, unsigned char flags, void *data) {
coap_async_state_t *s;
coap_tid_t id = request->tid;
SEARCH_PAIR(context->async_state,s,session,session,id,id);
if (s != NULL) {
/* We must return NULL here as the caller must know that he is
* responsible for releasing @p data. */
coap_log(LOG_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));
if (!s) {
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}
memset(s, 0, sizeof(coap_async_state_t));
/* set COAP_ASYNC_CONFIRM according to request's type */
s->flags = flags & ~COAP_ASYNC_CONFIRM;
if (request->type == COAP_MESSAGE_CON)
s->flags |= COAP_ASYNC_CONFIRM;
s->appdata = data;
s->session = coap_session_reference( session );
s->id = id;
if (request->token_length) {
/* A token can be up to 8 bytes */
s->tokenlen = (request->token_length > 8) ? 8 : request->token_length;
memcpy(s->token, request->token, s->tokenlen);
}
coap_touch_async(s);
LL_PREPEND(context->async_state, s);
return s;
}
coap_async_state_t *
coap_find_async(coap_context_t *context, coap_session_t *session, coap_tid_t id) {
coap_async_state_t *tmp;
SEARCH_PAIR(context->async_state,tmp,session,session,id,id);
return tmp;
}
int
coap_remove_async(coap_context_t *context, coap_session_t *session,
coap_tid_t id, coap_async_state_t **s) {
coap_async_state_t *tmp = coap_find_async(context, session, 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) {
if (s->session) {
coap_session_release(s->session);
}
if ((s->flags & COAP_ASYNC_RELEASE_DATA) != 0) {
coap_free(s->appdata);
}
coap_free(s);
}
}
#else
void does_not_exist(void); /* make some compilers happy */
#endif /* WITHOUT_ASYNC */
SEARCH_PAIR
#define SEARCH_PAIR(head, out, field1, val1, field2, val2)
Definition: async.c:19
coap_internal.h
Pulls together all the internal only header files.
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
coap_register_async
coap_async_state_t * coap_register_async(coap_context_t *context, coap_session_t *session, 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:30
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:96
coap_touch_async
COAP_STATIC_INLINE void coap_touch_async(coap_async_state_t *s)
Updates the time stamp of s.
Definition: async.h:142
coap_find_async
coap_async_state_t * coap_find_async(coap_context_t *context, coap_session_t *session, coap_tid_t id)
Retrieves the object identified by id from the list of asynchronous transactions that are registered ...
Definition: async.c:77
COAP_ASYNC_RELEASE_DATA
#define COAP_ASYNC_RELEASE_DATA
release application data on destruction
Definition: async.h:61
coap_remove_async
int coap_remove_async(coap_context_t *context, coap_session_t *session, coap_tid_t id, coap_async_state_t **s)
Removes the state object identified by id from context.
Definition: async.c:84
COAP_ASYNC_CONFIRM
#define COAP_ASYNC_CONFIRM
send confirmable response
Definition: async.h:56
coap_log
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
LOG_CRIT
@ LOG_CRIT
Critical.
Definition: coap_debug.h:50
LOG_DEBUG
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:55
coap_malloc
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:75
coap_free
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:82
COAP_MESSAGE_CON
#define COAP_MESSAGE_CON
Definition: pdu.h:72
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
coap_async_state_t
Definition: async.h:31
coap_async_state_t::flags
unsigned char flags
holds the flags to control behaviour
Definition: async.h:32
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_async_state_t::tokenlen
size_t tokenlen
length of the token
Definition: async.h:49
coap_async_state_t::session
coap_session_t * session
transaction session
Definition: async.h:46
coap_async_state_t::token
uint8_t token[8]
the token to use in a response
Definition: async.h:50
coap_async_state_t::id
coap_tid_t id
transaction id
Definition: async.h:47
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:147
coap_context_t::async_state
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:157
coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:285
coap_pdu_t::type
uint8_t type
message type
Definition: pdu.h:286
coap_pdu_t::token
uint8_t * token
first byte of token, if any, or options
Definition: pdu.h:296
coap_pdu_t::tid
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:291
coap_pdu_t::token_length
uint8_t token_length
length of Token
Definition: pdu.h:290
coap_session_t
Definition: coap_session.h:59
LL_DELETE
#define LL_DELETE(head, del)
Definition: utlist.h:385
LL_PREPEND
#define LL_PREPEND(head, add)
Definition: utlist.h:314