Contiki 3.x
er-coap-block1.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Lars Schmertmann <SmallLars@t-online.de>.
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 block 1 handling
35  * \author
36  * Lars Schmertmann <SmallLars@t-online.de>
37  */
38 
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "er-coap.h"
43 #include "er-coap-block1.h"
44 
45 #define DEBUG 0
46 #if DEBUG
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 
58 /**
59  * \brief Block 1 support within a coap-ressource
60  *
61  * This function will help you to use block 1. If target is null
62  * error handling and response configuration is active. On return
63  * value 0, the last block was recived, while on return value 1
64  * more blocks will follow. With target, len and maxlen this
65  * function will assemble the blocks.
66  *
67  * You can find an example in:
68  * examples/er-rest-example/resources/res-b1-sep-b2.c
69  *
70  * \param request Request pointer from the handler
71  * \param response Response pointer from the handler
72  * \param target Pointer to the buffer where the request payload can be assembled
73  * \param len Pointer to the variable, where the function stores the actual length
74  * \param max_len Length of the "target"-Buffer
75  *
76  * \return 0 if initialisation was successful
77  * -1 if initialisation failed
78  */
79 int
80 coap_block1_handler(void *request, void *response, uint8_t *target, size_t *len, size_t max_len)
81 {
82  const uint8_t *payload = 0;
83  int pay_len = REST.get_request_payload(request, &payload);
84 
85  if(!pay_len || !payload) {
86  erbium_status_code = REST.status.BAD_REQUEST;
87  coap_error_message = "NoPayload";
88  return -1;
89  }
90 
91  coap_packet_t *packet = (coap_packet_t *)request;
92 
93  if(packet->block1_offset + pay_len > max_len) {
94  erbium_status_code = REST.status.REQUEST_ENTITY_TOO_LARGE;
95  coap_error_message = "Message to big";
96  return -1;
97  }
98 
99  if(target && len) {
100  memcpy(target + packet->block1_offset, payload, pay_len);
101  *len = packet->block1_offset + pay_len;
102  }
103 
104  if(IS_OPTION(packet, COAP_OPTION_BLOCK1)) {
105  PRINTF("Blockwise: block 1 request: Num: %u, More: %u, Size: %u, Offset: %u\n",
106  packet->block1_num,
107  packet->block1_more,
108  packet->block1_size,
109  packet->block1_offset);
110 
111  coap_set_header_block1(response, packet->block1_num, packet->block1_more, packet->block1_size);
112  if(packet->block1_more) {
113  coap_set_status_code(response, CONTINUE_2_31);
114  return 1;
115  }
116  }
117 
118  return 0;
119 }
CoAP module for block 1 handling
uint16_t len
Length of the data that was previously sent.
Definition: uip.h:1347
An implementation of the Constrained Application Protocol (RFC).
int coap_block1_handler(void *request, void *response, uint8_t *target, size_t *len, size_t max_len)
Block 1 support within a coap-ressource.