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