caprotocolmessage.h 11 KB
Newer Older
jihwan.seo's avatar
jihwan.seo committed
1
/******************************************************************
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright 2014 Samsung Electronics All Rights Reserved.
 *
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************/
20
/**
jihwanseo's avatar
jihwanseo committed
21 22
 * @file
 * This file contains common function for handling protocol messages.
23 24
 */

25 26
#ifndef CA_PROTOCOL_MESSAGE_H_
#define CA_PROTOCOL_MESSAGE_H_
27 28

#include "cacommon.h"
29 30 31 32
#ifndef WITH_UPSTREAM_LIBCOAP
#include "coap/config.h"
#endif
#include <coap/coap.h>
33 34 35 36 37 38 39

#ifdef __cplusplus
extern "C"
{
#endif

typedef uint32_t code_t;
jihwan.seo's avatar
jihwan.seo committed
40

41 42
#define CA_RESPONSE_CLASS(C) (((C) >> 5)*100)
#define CA_RESPONSE_CODE(C) (CA_RESPONSE_CLASS(C) + (C - COAP_RESPONSE_CODE(CA_RESPONSE_CLASS(C))))
43

44 45
#define HAVE_TIME_H 1

46
#ifdef WITH_TCP
47
static const uint8_t PAYLOAD_MARKER = 1;
48
#endif
49

50
/**
jihwan.seo's avatar
jihwan.seo committed
51 52 53
 * generates pdu structure from the given information.
 * @param[in]   code                 code of the pdu packet.
 * @param[in]   info                 pdu information.
54
 * @param[in]   endpoint             endpoint information.
jihwan.seo's avatar
jihwan.seo committed
55
 * @return  generated pdu.
56
 */
57
coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
58
                          coap_list_t **optlist, coap_transport_t *transport);
59

60
/**
jihwan.seo's avatar
jihwan.seo committed
61 62
 * extracts request information from received pdu.
 * @param[in]   pdu                   received pdu.
63
 * @param[in]   endpoint              endpoint information.
jihwan.seo's avatar
jihwan.seo committed
64 65
 * @param[out]  outReqInfo            request info structure made from received pdu.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
66
 */
67 68
CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                                   CARequestInfo_t *outReqInfo);
69 70

/**
jihwan.seo's avatar
jihwan.seo committed
71 72 73
 * extracts response information from received pdu.
 * @param[in]   pdu                   received pdu.
 * @param[out]  outResInfo            response info structure made from received pdu.
74
 * @param[in]   endpoint              endpoint information.
jihwan.seo's avatar
jihwan.seo committed
75
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
76
 */
77 78
CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
                                    const CAEndpoint_t *endpoint);
79

jnashok's avatar
jnashok committed
80
/**
jihwan.seo's avatar
jihwan.seo committed
81 82
 * extracts error information from received pdu.
 * @param[in]   pdu                   received pdu.
83
 * @param[in]   endpoint              endpoint information.
jihwan.seo's avatar
jihwan.seo committed
84 85
 * @param[out]  errorInfo             error info structure made from received pdu.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
jnashok's avatar
jnashok committed
86
 */
87 88
CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                                 CAErrorInfo_t *errorInfo);
89

90 91 92 93 94 95 96 97 98 99
/**
 * extracts signaling information from received pdu.
 * @param[in]   pdu                   received pdu.
 * @param[in]   endpoint              endpoint information.
 * @param[out]  outSigInfo            signaling info structure made from received pdu.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
 */
CAResult_t CAGetSignalingInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                                     CASignalingInfo_t *outSigInfo);

100
/**
jihwan.seo's avatar
jihwan.seo committed
101 102 103
 * creates pdu from the request information.
 * @param[in]   code                 request or response code.
 * @param[in]   info                 information to create pdu.
104
 * @param[in]   endpoint             endpoint information.
105
 * @param[out]  options              options for the request and response.
jihwan.seo's avatar
jihwan.seo committed
106
 * @return  generated pdu.
107
 */
108
coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
109
                              const CAEndpoint_t *endpoint, coap_list_t *options,
110
                              coap_transport_t *transport);
111

112
/**
jihwan.seo's avatar
jihwan.seo committed
113 114 115 116
 * parse the URI and creates the options.
 * @param[in]    uriInfo             uri information.
 * @param[out]   options             options information.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
jihwan.seo's avatar
jihwan.seo committed
117 118 119 120
 */
CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);

/**
jihwan.seo's avatar
jihwan.seo committed
121 122
 * Helper that uses libcoap to parse either the path or the parameters of a URI
 * and populate the supplied options list.
jihwan.seo's avatar
jihwan.seo committed
123
 *
jihwan.seo's avatar
jihwan.seo committed
124 125 126 127 128 129
 * @param[in]   str                  the input partial URI string (either path or query).
 * @param[in]   length               the length of the supplied partial URI.
 * @param[in]   target               the part of the URI to parse (either COAP_OPTION_URI_PATH.
 *                                   or COAP_OPTION_URI_QUERY).
 * @param[out]  optlist              options information.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
130
 */
131
CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, uint16_t target,
jihwan.seo's avatar
jihwan.seo committed
132
                             coap_list_t **optlist);
133

134
/**
jihwan.seo's avatar
jihwan.seo committed
135 136 137 138 139
 * create option list from header information in the info.
 * @param[in]   code                 uri information.
 * @param[in]   info                 information of the request/response.
 * @param[out]  optlist              options information.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
140
 */
141
CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
142

Ziran Sun's avatar
Ziran Sun committed
143 144 145 146 147
/**
 * Helper to parse content format and accept format header options
 * and populate the supplied options list.
 *
 * @param[in]   formatOption         CoAP format header option.
148
 * @param[in]   format               content or accept format.
Ziran Sun's avatar
Ziran Sun committed
149 150 151 152 153 154
 * @param[in]   versionOption        CoAP version header option.
 * @param[in]   version              value of version.
 * @param[out]  optlist              options information.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
 */

155
CAResult_t CAParsePayloadFormatHeadOption(uint16_t formatOption, CAPayloadFormat_t format,
Ziran Sun's avatar
Ziran Sun committed
156 157
        uint16_t versionOption, uint16_t version, coap_list_t **optlist);

158
/**
jihwan.seo's avatar
jihwan.seo committed
159 160 161 162 163
 * creates option node from key length and data.
 * @param[in]   key                  key for the that needs to be sent.
 * @param[in]   length               length of the data that needs to be sent.
 * @param[in]   data                 data that needs to be sent.
 * @return  created list.
164
 */
165
coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
166

167
/**
jihwan.seo's avatar
jihwan.seo committed
168 169 170 171 172
 * order the inserted options.
 * need to replace queue head if new node has to be added before the existing queue head.
 * @param[in]   a                    option 1 for insertion.
 * @param[in]   b                    option 2 for insertion.
 * @return  0 or 1.
173
 */
174
int CAOrderOpts(void *a, void *b);
175

176
/**
jihwan.seo's avatar
jihwan.seo committed
177 178
 * number of options count.
 * @param[in]   opt_iter            option iteration for count.
179 180
 * @param[out]  optionCount         number of options.
 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
181
 */
182
CAResult_t CAGetOptionCount(coap_opt_iterator_t opt_iter, uint8_t *optionCount);
183

184
/**
jihwan.seo's avatar
jihwan.seo committed
185
 * gets option data.
186
 * @param[in]   key                  ID of the option
jihwan.seo's avatar
jihwan.seo committed
187 188 189 190 191
 * @param[in]   data                 data that is received.
 * @param[in]   length               length of the data.
 * @param[out]  option               result of the operation.
 * @param[in]   buflen               buffer length of the result.
 * @return  option count.
192
 */
193
uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
194
                         uint8_t *option, uint32_t buflen);
195

196
/**
jihwan.seo's avatar
jihwan.seo committed
197 198
 * extracts request information from received pdu.
 * @param[in]    pdu                  received pdu.
199
 * @param[in]    endpoint             endpoint information.
jihwan.seo's avatar
jihwan.seo committed
200 201 202
 * @param[out]   outCode              code of the received pdu.
 * @param[out]   outInfo              request info structure made from received pdu.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
203
 */
204 205
CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
                            uint32_t *outCode, CAInfo_t *outInfo);
206 207

/**
jihwan.seo's avatar
jihwan.seo committed
208 209 210 211
 * create pdu from received data.
 * @param[in]   data                received data.
 * @param[in]   length              length of the data received.
 * @param[out]  outCode             code received.
212
 * @param[in]   endpoint            endpoint information.
jihwan.seo's avatar
jihwan.seo committed
213
 * @return  coap_pdu_t value.
214
 */
215
coap_pdu_t *CAParsePDU(const char *data, size_t length, uint32_t *outCode,
216
                       const CAEndpoint_t *endpoint);
217

jihwan.seo's avatar
jihwan.seo committed
218
/**
jihwanseo's avatar
jihwanseo committed
219
 * get Token from received data(pdu).
jihwan.seo's avatar
jihwan.seo committed
220 221
 * @param[in]    pdu_hdr             header of received pdu.
 * @param[out]   outInfo             information with token received.
222
 * @param[in]    endpoint            endpoint information.
jihwan.seo's avatar
jihwan.seo committed
223
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
jihwan.seo's avatar
jihwan.seo committed
224
 */
225 226
CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
                             CAInfo_t *outInfo,
227
                             const CAEndpoint_t *endpoint);
jihwan.seo's avatar
jihwan.seo committed
228

229
/**
jihwan.seo's avatar
jihwan.seo committed
230 231 232 233
 * generates the token.
 * @param[out]   token           generated token.
 * @param[in]    tokenLength     length of the token.
 * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
234
 */
235
CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
236

237
/**
jihwan.seo's avatar
jihwan.seo committed
238 239
 * destroys the token.
 * @param[in]   token           generated token.
240
 */
241 242
void CADestroyTokenInternal(CAToken_t token);

243
/**
jihwan.seo's avatar
jihwan.seo committed
244 245 246 247
 * gets message type from PDU binary data.
 * @param[in]   pdu                 pdu data.
 * @param[in]   size                size of pdu data.
 * @return  message type.
248
 */
249
CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
250 251

/**
jihwan.seo's avatar
jihwan.seo committed
252 253 254 255
 * gets message ID PDU binary data.
 * @param[in]   pdu                 pdu data.
 * @param[in]   size                size of pdu data.
 * @return  message ID.
256
 */
257
uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
258

jihwan.seo's avatar
jihwan.seo committed
259
/**
jihwan.seo's avatar
jihwan.seo committed
260 261 262 263
 * gets code PDU binary data.
 * @param[in]   pdu                 pdu data.
 * @param[in]   size                size of pdu data.
 * @return  code.
jihwan.seo's avatar
jihwan.seo committed
264 265 266
 */
CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);

267
/**
268
 * convert format from CoAP media type encoding to CAPayloadFormat_t.
269 270 271
 * @param[in]   format              coap format code.
 * @return format.
 */
Ziran Sun's avatar
Ziran Sun committed
272
CAPayloadFormat_t CAConvertFormat(uint16_t format);
273

274 275 276 277 278 279 280 281 282
#ifdef WITH_TCP
/**
 * check whether CoAP over TCP is supported or not.
 * @param[in]   adapter             transport adapter type.
 * @return true or false.
 */
bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter);
#endif

283 284 285 286 287 288 289 290 291
#ifdef WITH_BWT
/**
 * check whether blockwise transfer is supported or not.
 * @param[in]   adapter             transport adapter type.
 * @return true or false.
 */
bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter);
#endif

292 293 294 295
#ifdef __cplusplus
} /* extern "C" */
#endif

296
#endif /* CA_PROTOCOL_MESSAGE_H_ */