1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
22 * This file contains common function for handling protocol messages.
25 #ifndef CA_PROTOCOL_MESSAGE_H_
26 #define CA_PROTOCOL_MESSAGE_H_
29 #ifndef WITH_UPSTREAM_LIBCOAP
30 #include "coap/config.h"
32 #include <coap/coap.h>
39 typedef uint32_t code_t;
41 #define CA_RESPONSE_CLASS(C) (((C) >> 5)*100)
42 #define CA_RESPONSE_CODE(C) (CA_RESPONSE_CLASS(C) + (C - COAP_RESPONSE_CODE(CA_RESPONSE_CLASS(C))))
45 // Include files from the arduino platform do not provide these conversions:
47 #define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
48 #define ntohs(x) htons(x)
53 static const uint8_t PAYLOAD_MARKER = 1;
56 * generates pdu structure from the given information.
57 * @param[in] code code of the pdu packet.
58 * @param[in] info pdu information.
59 * @param[in] endpoint endpoint information.
60 * @return generated pdu.
62 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
63 coap_list_t **optlist, coap_transport_t *transport);
66 * extracts request information from received pdu.
67 * @param[in] pdu received pdu.
68 * @param[in] endpoint endpoint information.
69 * @param[out] outReqInfo request info structure made from received pdu.
70 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
72 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
73 CARequestInfo_t *outReqInfo);
76 * extracts response information from received pdu.
77 * @param[in] pdu received pdu.
78 * @param[out] outResInfo response info structure made from received pdu.
79 * @param[in] endpoint endpoint information.
80 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
82 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
83 const CAEndpoint_t *endpoint);
86 * extracts error information from received pdu.
87 * @param[in] pdu received pdu.
88 * @param[in] endpoint endpoint information.
89 * @param[out] errorInfo error info structure made from received pdu.
90 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
92 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
93 CAErrorInfo_t *errorInfo);
96 * creates pdu from the request information.
97 * @param[in] code request or response code.
98 * @param[in] info information to create pdu.
99 * @param[in] endpoint endpoint information.
100 * @param[out] options options for the request and response.
101 * @return generated pdu.
103 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
104 const CAEndpoint_t *endpoint, coap_list_t *options,
105 coap_transport_t *transport);
108 * parse the URI and creates the options.
109 * @param[in] uriInfo uri information.
110 * @param[out] options options information.
111 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
113 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);
116 * Helper that uses libcoap to parse either the path or the parameters of a URI
117 * and populate the supplied options list.
119 * @param[in] str the input partial URI string (either path or query).
120 * @param[in] length the length of the supplied partial URI.
121 * @param[in] target the part of the URI to parse (either COAP_OPTION_URI_PATH.
122 * or COAP_OPTION_URI_QUERY).
123 * @param[out] optlist options information.
124 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
126 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
127 coap_list_t **optlist);
130 * create option list from header information in the info.
131 * @param[in] code uri information.
132 * @param[in] info information of the request/response.
133 * @param[out] optlist options information.
134 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
136 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
139 * creates option node from key length and data.
140 * @param[in] key key for the that needs to be sent.
141 * @param[in] length length of the data that needs to be sent.
142 * @param[in] data data that needs to be sent.
143 * @return created list.
145 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
148 * order the inserted options.
149 * need to replace queue head if new node has to be added before the existing queue head.
150 * @param[in] a option 1 for insertion.
151 * @param[in] b option 2 for insertion.
154 int CAOrderOpts(void *a, void *b);
157 * number of options count.
158 * @param[in] opt_iter option iteration for count.
159 * @return number of options.
161 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
165 * @param[in] key ID of the option
166 * @param[in] data data that is received.
167 * @param[in] length length of the data.
168 * @param[out] option result of the operation.
169 * @param[in] buflen buffer length of the result.
170 * @return option count.
172 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
173 uint8_t *option, uint32_t buflen);
176 * extracts request information from received pdu.
177 * @param[in] pdu received pdu.
178 * @param[in] endpoint endpoint information.
179 * @param[out] outCode code of the received pdu.
180 * @param[out] outInfo request info structure made from received pdu.
181 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
183 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
184 uint32_t *outCode, CAInfo_t *outInfo);
187 * create pdu from received data.
188 * @param[in] data received data.
189 * @param[in] length length of the data received.
190 * @param[out] outCode code received.
191 * @param[in] endpoint endpoint information.
192 * @return coap_pdu_t value.
194 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
195 const CAEndpoint_t *endpoint);
198 * get Token from received data(pdu).
199 * @param[in] pdu_hdr header of received pdu.
200 * @param[out] outInfo information with token received.
201 * @param[in] endpoint endpoint information.
202 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
204 CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
206 const CAEndpoint_t *endpoint);
209 * generates the token.
210 * @param[out] token generated token.
211 * @param[in] tokenLength length of the token.
212 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
214 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
217 * destroys the token.
218 * @param[in] token generated token.
220 void CADestroyTokenInternal(CAToken_t token);
223 * gets message type from PDU binary data.
224 * @param[in] pdu pdu data.
225 * @param[in] size size of pdu data.
226 * @return message type.
228 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
231 * gets message ID PDU binary data.
232 * @param[in] pdu pdu data.
233 * @param[in] size size of pdu data.
234 * @return message ID.
236 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
239 * gets code PDU binary data.
240 * @param[in] pdu pdu data.
241 * @param[in] size size of pdu data.
244 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);
247 * convert format from CoAP media type encoding to CAPayloadFormat_t.
248 * @param[in] format coap format code.
251 CAPayloadFormat_t CAConvertFormat(uint8_t format);
255 * check whether CoAP over TCP is supported or not.
256 * @param[in] adapter transport adapter type.
257 * @return true or false.
259 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter);
264 * check whether blockwise transfer is supported or not.
265 * @param[in] adapter transport adapter type.
266 * @return true or false.
268 bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter);
275 #endif /* CA_PROTOCOL_MESSAGE_H_ */