Contiki-NG
coap-callback-api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, SICS Swedish ICT
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *
30  */
31 
32 /**
33  * \file
34  * Callback API for doing CoAP requests
35  * Adapted from the blocking API
36  * \author
37  * Joakim Eriksson, joakime@sics.se
38  */
39 
40 /**
41  * \addtogroup coap
42  * @{
43  */
44 
45 #include "coap-engine.h"
46 #include "coap-callback-api.h"
47 #include "coap-transactions.h"
48 #include "sys/cc.h"
49 #include <stdlib.h>
50 #include <string.h>
51 
52 /* Log configuration */
53 #include "coap-log.h"
54 #define LOG_MODULE "coap"
55 #define LOG_LEVEL LOG_LEVEL_COAP
56 
57 /* These should go into the state struct so that we can have multiple
58  requests */
59 
60 static uint32_t res_block;
61 static uint8_t more;
62 static uint8_t block_error;
63 
64 static void coap_request_callback(void *callback_data, coap_message_t *response);
65 
66 /*---------------------------------------------------------------------------*/
67 
68 static int
69 progress_request(coap_request_state_t *state) {
70  coap_message_t *request = state->request;
71  request->mid = coap_get_mid();
72  if((state->transaction =
73  coap_new_transaction(request->mid, state->remote_endpoint))) {
74  state->transaction->callback = coap_request_callback;
75  state->transaction->callback_data = state;
76 
77  if(state->block_num > 0) {
78  coap_set_header_block2(request, state->block_num, 0,
79  COAP_MAX_CHUNK_SIZE);
80  }
81  state->transaction->message_len =
82  coap_serialize_message(request, state->transaction->message);
83 
84  coap_send_transaction(state->transaction);
85  LOG_DBG("Requested #%"PRIu32" (MID %u)\n", state->block_num, request->mid);
86  return 1;
87  }
88  return 0;
89 }
90 
91 /*---------------------------------------------------------------------------*/
92 
93 static void
94 coap_request_callback(void *callback_data, coap_message_t *response)
95 {
96  coap_request_state_t *state = (coap_request_state_t *)callback_data;
97  uint32_t res_block1;
98 
99  state->response = response;
100 
101  LOG_DBG("request callback\n");
102 
103  if(!state->response) {
104  LOG_WARN("Server not responding giving up...\n");
105  state->callback(state);
106  return;
107  }
108 
109  /* Got a response */
110  coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
111  coap_get_header_block1(state->response, &res_block1, NULL, NULL, NULL);
112 
113  LOG_DBG("Received #%lu%s B1:%lu (%u bytes)\n",
114  (unsigned long)res_block, (unsigned)more ? "+" : "",
115  (unsigned long)res_block1,
116  state->response->payload_len);
117 
118  if(res_block == state->block_num) {
119  /* Call the callback function as we have more data */
120  state->callback(state);
121  /* this is only for counting BLOCK2 blocks.*/
122  ++(state->block_num);
123  } else {
124  LOG_WARN("WRONG BLOCK %"PRIu32"/%"PRIu32"\n", res_block, state->block_num);
125  ++block_error;
126  }
127 
128  if(more && block_error < COAP_MAX_ATTEMPTS) {
129  progress_request(state);
130  } else {
131  /* failure - now we give up and notify the callback */
132  state->response = NULL;
133  state->callback(state);
134  }
135 }
136 
137 /*---------------------------------------------------------------------------*/
138 
139 int
140 coap_send_request(coap_request_state_t *state, coap_endpoint_t *endpoint,
141  coap_message_t *request,
142  void (*callback)(coap_request_state_t *state))
143 {
144  /* can we have these variables shared between multiple requests? */
145  /* ripped from blocking request */
146  more = 0;
147  res_block = 0;
148  block_error = 0;
149 
150  state->block_num = 0;
151  state->response = NULL;
152  state->request = request;
153  state->remote_endpoint = endpoint;
154  state->callback = callback;
155 
156  return progress_request(state);
157 }
158 /*---------------------------------------------------------------------------*/
159 /** @} */
Log support for CoAP
CoAP engine implementation.
int coap_send_request(coap_request_state_t *state, coap_endpoint_t *endpoint, coap_message_t *request, void(*callback)(coap_request_state_t *state))
Send a CoAP request to a remote endpoint.
Callback API for doing CoAP requests Adapted from the blocking API
CoAP module for reliable transport
Default definitions of C compiler quirk work-arounds.