Contiki-NG
coap-blocking-api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
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  * This file is part of the Contiki operating system.
30  */
31 
32 /**
33  * \file
34  * CoAP implementation for the REST Engine.
35  * \author
36  * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37  */
38 
39 /**
40  * \addtogroup coap
41  * @{
42  */
43 
44 #include "coap-engine.h"
45 #include "coap-blocking-api.h"
46 #include "sys/cc.h"
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <inttypes.h>
51 
52 /* Log configuration */
53 #include "coap-log.h"
54 #define LOG_MODULE "coap"
55 #define LOG_LEVEL LOG_LEVEL_COAP
56 
57 /*---------------------------------------------------------------------------*/
58 /*- Client Part -------------------------------------------------------------*/
59 /*---------------------------------------------------------------------------*/
60 void
61 coap_blocking_request_callback(void *callback_data, coap_message_t *response)
62 {
63  coap_request_state_t *state = (coap_request_state_t *)callback_data;
64 
65  state->response = response;
66  process_poll(state->process);
67 }
68 /*---------------------------------------------------------------------------*/
69 PT_THREAD(coap_blocking_request
70  (coap_request_state_t *state, process_event_t ev,
71  coap_endpoint_t *remote_ep,
72  coap_message_t *request,
73  coap_blocking_response_handler_t request_callback))
74 {
75  PT_BEGIN(&state->pt);
76 
77  static uint32_t res_block;
78  static uint8_t more;
79  static uint8_t block_error;
80 
81  state->block_num = 0;
82  state->response = NULL;
83  state->process = PROCESS_CURRENT();
84 
85  more = 0;
86  res_block = 0;
87  block_error = 0;
88 
89  do {
90  request->mid = coap_get_mid();
91  if((state->transaction = coap_new_transaction(request->mid, remote_ep))) {
92  state->transaction->callback = coap_blocking_request_callback;
93  state->transaction->callback_data = state;
94 
95  if(state->block_num > 0) {
96  coap_set_header_block2(request, state->block_num, 0,
97  COAP_MAX_CHUNK_SIZE);
98  }
99  state->transaction->message_len = coap_serialize_message(request,
100  state->
101  transaction->
102  message);
103 
104  coap_send_transaction(state->transaction);
105  LOG_DBG("Requested #%"PRIu32" (MID %u)\n", state->block_num, request->mid);
106 
107  PT_YIELD_UNTIL(&state->pt, ev == PROCESS_EVENT_POLL);
108 
109  if(!state->response) {
110  LOG_WARN("Server not responding\n");
111  PT_EXIT(&state->pt);
112  }
113 
114  coap_get_header_block2(state->response, &res_block, &more, NULL, NULL);
115 
116  LOG_DBG("Received #%"PRIu32"%s (%u bytes)\n", res_block, more ? "+" : "",
117  state->response->payload_len);
118 
119  if(res_block == state->block_num) {
120  request_callback(state->response);
121  ++(state->block_num);
122  } else {
123  LOG_WARN("WRONG BLOCK %"PRIu32"/%"PRIu32"\n",
124  res_block, state->block_num);
125  ++block_error;
126  }
127  } else {
128  LOG_WARN("Could not allocate transaction buffer");
129  PT_EXIT(&state->pt);
130  }
131  } while(more && block_error < COAP_MAX_ATTEMPTS);
132 
133  PT_END(&state->pt);
134 }
135 /*---------------------------------------------------------------------------*/
136 /** @} */
Log support for CoAP
CoAP engine implementation.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition: pt.h:114
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
Definition: pt.h:309
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
#define PT_END(pt)
Declare the end of a protothread.
Definition: pt.h:126
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define PT_EXIT(pt)
Exit the protothread.
Definition: pt.h:245
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
Default definitions of C compiler quirk work-arounds.