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_
37 typedef uint32_t code_t;
39 #define CA_RESPONSE_CLASS(C) (((C) >> 5)*100)
40 #define CA_RESPONSE_CODE(C) (CA_RESPONSE_CLASS(C) + (C - COAP_RESPONSE_CODE(CA_RESPONSE_CLASS(C))))
43 // Include files from the arduino platform do not provide these conversions:
45 #define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
46 #define ntohs(x) htons(x)
51 static const uint8_t PAYLOAD_MARKER = 1;
54 * generates pdu structure from the given information.
55 * @param[in] code code of the pdu packet.
56 * @param[in] info pdu information.
57 * @param[in] endpoint endpoint information.
58 * @return generated pdu.
60 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
61 coap_list_t **optlist, coap_transport_type *transport);
64 * extracts request information from received pdu.
65 * @param[in] pdu received pdu.
66 * @param[in] endpoint endpoint information.
67 * @param[out] outReqInfo request info structure made from received pdu.
68 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
70 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
71 CARequestInfo_t *outReqInfo);
74 * extracts response information from received pdu.
75 * @param[in] pdu received pdu.
76 * @param[out] outResInfo response info structure made from received pdu.
77 * @param[in] endpoint endpoint information.
78 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
80 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
81 const CAEndpoint_t *endpoint);
84 * extracts error information from received pdu.
85 * @param[in] pdu received pdu.
86 * @param[in] endpoint endpoint information.
87 * @param[out] errorInfo error info structure made from received pdu.
88 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
90 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
91 CAErrorInfo_t *errorInfo);
94 * creates pdu from the request information.
95 * @param[in] code request or response code.
96 * @param[in] info information to create pdu.
97 * @param[in] endpoint endpoint information.
98 * @param[out] options options for the request and response.
99 * @return generated pdu.
101 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
102 const CAEndpoint_t *endpoint, coap_list_t *options,
103 coap_transport_type *transport);
106 * parse the URI and creates the options.
107 * @param[in] uriInfo uri information.
108 * @param[out] options options information.
109 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
111 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);
114 * Helper that uses libcoap to parse either the path or the parameters of a URI
115 * and populate the supplied options list.
117 * @param[in] str the input partial URI string (either path or query).
118 * @param[in] length the length of the supplied partial URI.
119 * @param[in] target the part of the URI to parse (either COAP_OPTION_URI_PATH.
120 * or COAP_OPTION_URI_QUERY).
121 * @param[out] optlist options information.
122 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
124 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
125 coap_list_t **optlist);
128 * create option list from header information in the info.
129 * @param[in] code uri information.
130 * @param[in] info information of the request/response.
131 * @param[out] optlist options information.
132 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
134 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
137 * creates option node from key length and data.
138 * @param[in] key key for the that needs to be sent.
139 * @param[in] length length of the data that needs to be sent.
140 * @param[in] data data that needs to be sent.
141 * @return created list.
143 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
146 * order the inserted options.
147 * need to replace queue head if new node has to be added before the existing queue head.
148 * @param[in] a option 1 for insertion.
149 * @param[in] b option 2 for insertion.
152 int CAOrderOpts(void *a, void *b);
155 * number of options count.
156 * @param[in] opt_iter option iteration for count.
157 * @return number of options.
159 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
163 * @param[in] key ID of the option
164 * @param[in] data data that is received.
165 * @param[in] length length of the data.
166 * @param[out] option result of the operation.
167 * @param[in] buflen buffer length of the result.
168 * @return option count.
170 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
171 uint8_t *option, uint32_t buflen);
174 * extracts request information from received pdu.
175 * @param[in] pdu received pdu.
176 * @param[in] endpoint endpoint information.
177 * @param[out] outCode code of the received pdu.
178 * @param[out] outInfo request info structure made from received pdu.
179 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
181 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
182 uint32_t *outCode, CAInfo_t *outInfo);
185 * create pdu from received data.
186 * @param[in] data received data.
187 * @param[in] length length of the data received.
188 * @param[out] outCode code received.
189 * @param[in] endpoint endpoint information.
190 * @return coap_pdu_t value.
192 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
193 const CAEndpoint_t *endpoint);
196 * get Token from received data(pdu).
197 * @param[in] pdu_hdr header of received pdu.
198 * @param[out] outInfo information with token received.
199 * @param[in] endpoint endpoint information.
200 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
202 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
203 const CAEndpoint_t *endpoint);
206 * generates the token.
207 * @param[out] token generated token.
208 * @param[in] tokenLength length of the token.
209 * @return CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
211 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
214 * destroys the token.
215 * @param[in] token generated token.
217 void CADestroyTokenInternal(CAToken_t token);
220 * gets message type from PDU binary data.
221 * @param[in] pdu pdu data.
222 * @param[in] size size of pdu data.
223 * @return message type.
225 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
228 * gets message ID PDU binary data.
229 * @param[in] pdu pdu data.
230 * @param[in] size size of pdu data.
231 * @return message ID.
233 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
236 * gets code PDU binary data.
237 * @param[in] pdu pdu data.
238 * @param[in] size size of pdu data.
241 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);
244 * convert format from CoAP media type encoding to CAPayloadFormat_t.
245 * @param[in] format coap format code.
248 CAPayloadFormat_t CAConvertFormat(uint8_t format);
252 * check whether CoAP over TCP is supported or not.
253 * @param[in] adapter transport adapter type.
254 * @return true or false.
256 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter);
261 * check whether blockwise transfer is supported or not.
262 * @param[in] adapter transport adapter type.
263 * @return true or false.
265 bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter);
272 #endif /* CA_PROTOCOL_MESSAGE_H_ */