Contiki-NG
Toggle main menu visibility
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
*/
83
int
84
coap_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
#if COAP_MESSAGE_ON_ERROR
93
coap_error_message =
"NoPayload"
;
94
#endif
95
return
-1;
96
}
97
98
if
(request->block1_offset + pay_len > max_len) {
99
coap_status_code = REQUEST_ENTITY_TOO_LARGE_4_13;
100
#if COAP_MESSAGE_ON_ERROR
101
coap_error_message =
"Message to big"
;
102
#endif
103
return
-1;
104
}
105
106
if
(target && len) {
107
memcpy(target + request->block1_offset, payload, pay_len);
108
*len = request->block1_offset + pay_len;
109
}
110
111
if
(coap_is_option(request, COAP_OPTION_BLOCK1)) {
112
LOG_DBG(
"Blockwise: block 1 request: Num: %"
PRIu32
113
", More: %u, Size: %u, Offset: %"
PRIu32
"\n"
,
114
request->block1_num,
115
request->block1_more,
116
request->block1_size,
117
request->block1_offset);
118
119
coap_set_header_block1(response, request->block1_num,
120
request->block1_more, request->block1_size);
121
if
(request->block1_more) {
122
coap_set_status_code(response, CONTINUE_2_31);
123
return
1;
124
}
125
}
126
127
return
0;
128
}
129
/*---------------------------------------------------------------------------*/
130
/** @} */
coap-block1.h
CoAP module for block 1 handling.
coap-log.h
Log support for CoAP.
coap.h
An implementation of the Constrained Application Protocol (RFC 7252).
coap_block1_handler
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
os
net
app-layer
coap
coap-block1.c
Generated on
for Contiki-NG by
1.17.0