Contiki-NG
Loading...
Searching...
No Matches
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/**
40 * \addtogroup coap
41 * @{
42 */
43
44#include <string.h>
45#include <inttypes.h>
46
47#include "coap.h"
48#include "coap-block1.h"
49
50/* Log configuration */
51#include "coap-log.h"
52#define LOG_MODULE "coap"
53#define LOG_LEVEL LOG_LEVEL_COAP
54
55/*---------------------------------------------------------------------------*/
56
57/**
58 * \brief Block 1 support within a CoAP resource.
59 *
60 * This function will help you to use block 1. If the target
61 * parameter is \p NULL, then error handling and response
62 * configuration is active.
63 *
64 * You can find an example in
65 * examples/coap/coap-example-server/resources/res-b1-sep-b2.c
66 *
67 * \param request Request pointer from the handler.
68 *
69 * \param response Response pointer from the handler.
70 *
71 * \param target Pointer to the buffer where the request payload can
72 * be assembled.
73 *
74 * \param len Pointer to the variable where the function stores the
75 * actual length.
76 *
77 * \param max_len Length of the \p target buffer.
78 *
79 * \return 0 if the initialisation was successful,
80 * 1 if more blocks will follow, or
81 * -1 if the initialisation failed.
82 */
83int
84coap_block1_handler(coap_message_t *request, coap_message_t *response,
85 uint8_t *target, size_t *len, size_t max_len)
86{
87 const uint8_t *payload = 0;
88 int pay_len = coap_get_payload(request, &payload);
89
90 if(!pay_len || !payload) {
91 coap_status_code = BAD_REQUEST_4_00;
92 coap_error_message = "NoPayload";
93 return -1;
94 }
95
96 if(request->block1_offset + pay_len > max_len) {
97 coap_status_code = REQUEST_ENTITY_TOO_LARGE_4_13;
98 coap_error_message = "Message to big";
99 return -1;
100 }
101
102 if(target && len) {
103 memcpy(target + request->block1_offset, payload, pay_len);
104 *len = request->block1_offset + pay_len;
105 }
106
107 if(coap_is_option(request, COAP_OPTION_BLOCK1)) {
108 LOG_DBG("Blockwise: block 1 request: Num: %"PRIu32
109 ", More: %u, Size: %u, Offset: %"PRIu32"\n",
110 request->block1_num,
111 request->block1_more,
112 request->block1_size,
113 request->block1_offset);
114
115 coap_set_header_block1(response, request->block1_num,
116 request->block1_more, request->block1_size);
117 if(request->block1_more) {
118 coap_set_status_code(response, CONTINUE_2_31);
119 return 1;
120 }
121 }
122
123 return 0;
124}
125/*---------------------------------------------------------------------------*/
126/** @} */
CoAP module for block 1 handling.
Log support for CoAP.
An implementation of the Constrained Application Protocol (RFC 7252).
int coap_block1_handler(coap_message_t *request, coap_message_t *response, uint8_t *target, size_t *len, size_t max_len)
Block 1 support within a CoAP resource.
Definition coap-block1.c:84