[go: up one dir, main page]

Menu

[d0916c]: / async.c  Maximize  Restore  History

Download this file

108 lines (82 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
/* 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.
*/
/**
* @file async.c
* @brief state management for asynchronous messages
*/
#ifndef WITHOUT_ASYNC
#include "config.h"
#include "utlist.h"
#include "mem.h"
#include "debug.h"
#include "async.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_opt_iterator_t opt_iter;
coap_opt_t *token;
coap_tid_t id;
size_t toklen = 0;
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;
}
token = coap_check_option(request, COAP_OPTION_TOKEN, &opt_iter);
if (token)
toklen = COAP_OPT_LENGTH(token);
/* store information for handling the asynchronous task */
s = (coap_async_state_t *)coap_malloc(sizeof(coap_async_state_t) + toklen);
if (!s) {
coap_log(LOG_CRIT, "coap_register_async: insufficient memory\n");
return NULL;
}
memset(s, 0, sizeof(coap_async_state_t) + toklen);
/* 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 (toklen) {
s->tokenlen = toklen;
memcpy(s->token, COAP_OPT_VALUE(token), toklen);
}
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 */