Contiki 3.x
er-coap-separate.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 module for separate responses.
35  * \author
36  * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37  */
38 
39 #include "sys/cc.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include "er-coap-separate.h"
43 #include "er-coap-transactions.h"
44 
45 #define DEBUG 0
46 #if DEBUG
47 #include <stdio.h>
48 #define PRINTF(...) printf(__VA_ARGS__)
49 #define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
50 #define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
51 #else
52 #define PRINTF(...)
53 #define PRINT6ADDR(addr)
54 #define PRINTLLADDR(addr)
55 #endif
56 
57 /*---------------------------------------------------------------------------*/
58 /*- Separate Response API ---------------------------------------------------*/
59 /*---------------------------------------------------------------------------*/
60 /**
61  * \brief Reject a request that would require a separate response with an error message
62  *
63  * When the server does not have enough resources left to store the information
64  * for a separate response or otherwise cannot execute the resource handler,
65  * this function will respond with 5.03 Service Unavailable. The client can
66  * then retry later.
67  */
68 void
70 {
71  /* TODO: Accept string pointer for custom error message */
72  erbium_status_code = SERVICE_UNAVAILABLE_5_03;
73  coap_error_message = "AlreadyInUse";
74 }
75 /*----------------------------------------------------------------------------*/
76 /**
77  * \brief Initiate a separate response with an empty ACK
78  * \param request The request to accept
79  * \param separate_store A pointer to the data structure that will store the
80  * relevant information for the response
81  *
82  * When the server does not have enough resources left to store the information
83  * for a separate response or otherwise cannot execute the resource handler,
84  * this function will respond with 5.03 Service Unavailable. The client can
85  * then retry later.
86  */
87 void
88 coap_separate_accept(void *request, coap_separate_t *separate_store)
89 {
90  coap_packet_t *const coap_req = (coap_packet_t *)request;
91  coap_transaction_t *const t = coap_get_transaction_by_mid(coap_req->mid);
92 
93  PRINTF("Separate ACCEPT: /%.*s MID %u\n", coap_req->uri_path_len,
94  coap_req->uri_path, coap_req->mid);
95  if(t) {
96  /* send separate ACK for CON */
97  if(coap_req->type == COAP_TYPE_CON) {
98  coap_packet_t ack[1];
99 
100  /* ACK with empty code (0) */
101  coap_init_message(ack, COAP_TYPE_ACK, 0, coap_req->mid);
102  /* serializing into IPBUF: Only overwrites header parts that are already parsed into the request struct */
103  coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport,
104  (uip_appdata), coap_serialize_message(ack,
105  uip_appdata));
106  }
107 
108  /* store remote address */
109  uip_ipaddr_copy(&separate_store->addr, &t->addr);
110  separate_store->port = t->port;
111 
112  /* store correct response type */
113  separate_store->type =
114  coap_req->type == COAP_TYPE_CON ? COAP_TYPE_CON : COAP_TYPE_NON;
115  separate_store->mid = coap_get_mid(); /* if it was a NON, we burned one MID in the engine... */
116 
117  memcpy(separate_store->token, coap_req->token, coap_req->token_len);
118  separate_store->token_len = coap_req->token_len;
119 
120  separate_store->block1_num = coap_req->block1_num;
121  separate_store->block1_size = coap_req->block1_size;
122 
123  separate_store->block2_num = coap_req->block2_num;
124  separate_store->block2_size = coap_req->block2_size > 0 ? MIN(COAP_MAX_BLOCK_SIZE, coap_req->block2_size) : COAP_MAX_BLOCK_SIZE;
125 
126  /* signal the engine to skip automatic response and clear transaction by engine */
127  erbium_status_code = MANUAL_RESPONSE;
128  } else {
129  PRINTF("ERROR: Response transaction for separate request not found!\n");
130  erbium_status_code = INTERNAL_SERVER_ERROR_5_00;
131  }
132 }
133 /*----------------------------------------------------------------------------*/
134 void
135 coap_separate_resume(void *response, coap_separate_t *separate_store,
136  uint8_t code)
137 {
138  coap_init_message(response, separate_store->type, code,
139  separate_store->mid);
140  if(separate_store->token_len) {
141  coap_set_token(response, separate_store->token,
142  separate_store->token_len);
143  }
144  if(separate_store->block1_size) {
145  coap_set_header_block1(response, separate_store->block1_num,
146  0, separate_store->block1_size);
147  }
148 }
149 /*---------------------------------------------------------------------------*/
Default definitions of C compiler quirk work-arounds.
uip_appdata
Pointer to the application data in the packet buffer.
Definition: tcp_loader.c:74
CoAP module for reliable transport
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
void coap_separate_reject()
Reject a request that would require a separate response with an error message.
CoAP module for separate responses.
void coap_separate_accept(void *request, coap_separate_t *separate_store)
Initiate a separate response with an empty ACK.