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 ******************************************************************/
21 * @file caprotocolmessage.h
22 * @brief This file contains common function for handling protocol messages.
25 #ifndef CA_PROTOCOL_MESSAGE_H_
26 #define CA_PROTOCOL_MESSAGE_H_
38 typedef uint32_t code_t;
40 #define CA_RESPONSE_CLASS(C) (((C) >> 5)*100)
41 #define CA_RESPONSE_CODE(C) (CA_RESPONSE_CLASS(C) + (C - COAP_RESPONSE_CODE(CA_RESPONSE_CLASS(C))))
44 // Include files from the arduino platform do not provide these conversions:
46 #define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
47 #define ntohs(x) htons(x)
53 * @brief generates pdu structure from the given information.
54 * @param code [IN] code of the pdu packet
55 * @param info [IN] pdu information
56 * @return generated pdu
58 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info);
61 * function for generating
65 * @brief extracts request information from received pdu.
66 * @param pdu [IN] received pdu
67 * @param outReqInfo [OUT] 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, CARequestInfo_t *outReqInfo);
73 * @brief extracts response information from received pdu.
74 * @param pdu [IN] received pdu
75 * @param outResInfo [OUT] response info structure made from received pdu
76 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
78 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo);
81 * @brief extracts error information from received pdu.
82 * @param pdu [IN] received pdu
83 * @param errorInfo [OUT] error info structure made from received pdu
84 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
86 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, CAErrorInfo_t *errorInfo);
89 * @brief creates pdu from the request information
90 * @param code [IN] request or response code
91 * @param options [OUT] options for the request and response
92 * @param info [IN] information to create pdu
93 * @param payload [IN] payload for the request or response consumed
94 * @return generated pdu
96 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
97 const char *payload, size_t payloadSize);
100 * @brief parse the URI and creates the options
101 * @param uriInfo [IN] uri information
102 * @param options [OUT] options information
103 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
105 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);
108 * @brief Helper that uses libcoap to parse either the path or the parameters of a URI
109 * and populate the supplied options list.
111 * @param str [IN] the input partial URI string (either path or query)
112 * @param length [IN] the length of the supplied partial URI
113 * @param target [IN] the part of the URI to parse (either COAP_OPTION_URI_PATH
114 * or COAP_OPTION_URI_QUERY)
115 * @param optlist [OUT] options information
116 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
118 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
119 coap_list_t **optlist);
122 * @brief create option list from header information in the info
123 * @param code [IN] uri information
124 * @param info [IN] information of the request/response
125 * @param optlist [OUT] options information
126 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
128 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
131 * @brief creates option node from key length and data
132 * @param key [IN] key for the that needs to be sent
133 * @param length [IN] length of the data that needs to be sent
134 * @param data [IN] data that needs to be sent
135 * @return created list
137 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
140 * @brief order the inserted options
141 * need to replace queue head if new node has to be added before the existing queue head
142 * @param a [IN] option 1 for insertion
143 * @param b [IN] option 2 for insertion
146 int CAOrderOpts(void *a, void *b);
149 * @brief number of options count
150 * @param opt_iter [IN] option iteration for count
151 * @return number of options
153 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
156 * @brief gets option data
157 * @param data [IN] data that is received
158 * @param length [IN] length of the data
159 * @param option [OUT] result of the operation
160 * @param buflen [IN] buffer length of the result
161 * @return option count
163 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen);
166 * @brief extracts request information from received pdu.
167 * @param pdu [IN] received pdu
168 * @param outCode [OUT] code of the received pdu
169 * @param outInfo [OUT] request info structure made from received pdu
170 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
172 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo);
175 * @brief create pdu from received data
176 * @param data [IN] received data
177 * @param length [IN] length of the data received
178 * @param outCode [OUT] code received
179 * @return coap_pdu_t value
181 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode);
184 * @brief get Token fromn received data(pdu)
185 * @param pdu_hdr [IN] header of received pdu
186 * @param outInfo [OUT] information with token received
187 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
189 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo);
192 * @brief generates the token
193 * @param token [OUT] generated token
194 * @param tokenLength [IN] length of the token
195 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
197 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
200 * @brief destroys the token
201 * @param token [IN] generated token
204 void CADestroyTokenInternal(CAToken_t token);
207 * @brief destroy the ca info structure
208 * @param info [IN] info structure created from received packet
211 void CADestroyInfo(CAInfo_t *info);
214 * @brief gets message type from PDU binary data
215 * @param pdu [IN] pdu data
216 * @param size [IN] size of pdu data
217 * @return message type
219 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
222 * @brief gets message ID PDU binary data
223 * @param pdu [IN] pdu data
224 * @param size [IN] size of pdu data
227 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
230 * @brief gets code PDU binary data
231 * @param pdu [IN] pdu data
232 * @param size [IN] size of pdu data
235 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);
241 #endif /* CA_PROTOCOL_MESSAGE_H_ */