[JIRA IOT-738] fixed to calculate message length for option
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / caprotocolmessage.h
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20 /**
21  * @file
22  * This file contains common function for handling protocol messages.
23  */
24
25 #ifndef CA_PROTOCOL_MESSAGE_H_
26 #define CA_PROTOCOL_MESSAGE_H_
27
28 #include "cacommon.h"
29 #include "config.h"
30 #include "coap.h"
31
32 #ifdef __cplusplus
33 extern "C"
34 {
35 #endif
36
37 typedef uint32_t code_t;
38
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))))
41
42
43 // Include files from the arduino platform do not provide these conversions:
44 #ifdef ARDUINO
45 #define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
46 #define ntohs(x) htons(x)
47 #else
48 #define HAVE_TIME_H 1
49 #endif
50
51 static const uint8_t PAYLOAD_MARKER = 1;
52
53 /**
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.
59  */
60 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint);
61
62 /**
63  * extracts request information from received pdu.
64  * @param[in]   pdu                   received pdu.
65  * @param[in]   endpoint              endpoint information.
66  * @param[out]  outReqInfo            request info structure made from received pdu.
67  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
68  */
69 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
70                                    CARequestInfo_t *outReqInfo);
71
72 /**
73  * extracts response information from received pdu.
74  * @param[in]   pdu                   received pdu.
75  * @param[out]  outResInfo            response info structure made from received pdu.
76  * @param[in]   endpoint              endpoint information.
77  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
78  */
79 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
80                                     const CAEndpoint_t *endpoint);
81
82 /**
83  * extracts error information from received pdu.
84  * @param[in]   pdu                   received pdu.
85  * @param[in]   endpoint              endpoint information.
86  * @param[out]  errorInfo             error info structure made from received pdu.
87  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
88  */
89 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
90                                  CAErrorInfo_t *errorInfo);
91
92 /**
93  * creates pdu from the request information.
94  * @param[in]   code                 request or response code.
95  * @param[in]   info                 information to create pdu.
96  * @param[in]   endpoint             endpoint information.
97  * @param[out]  options              options for the request and response.
98  * @return  generated pdu.
99  */
100 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
101                               const CAEndpoint_t *endpoint, coap_list_t *options);
102
103 /**
104  * parse the URI and creates the options.
105  * @param[in]    uriInfo             uri information.
106  * @param[out]   options             options information.
107  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
108  */
109 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);
110
111 /**
112  * Helper that uses libcoap to parse either the path or the parameters of a URI
113  * and populate the supplied options list.
114  *
115  * @param[in]   str                  the input partial URI string (either path or query).
116  * @param[in]   length               the length of the supplied partial URI.
117  * @param[in]   target               the part of the URI to parse (either COAP_OPTION_URI_PATH.
118  *                                   or COAP_OPTION_URI_QUERY).
119  * @param[out]  optlist              options information.
120  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
121  */
122 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
123                              coap_list_t **optlist);
124
125 /**
126  * create option list from header information in the info.
127  * @param[in]   code                 uri information.
128  * @param[in]   info                 information of the request/response.
129  * @param[out]  optlist              options information.
130  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
131  */
132 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
133
134 /**
135  * creates option node from key length and data.
136  * @param[in]   key                  key for the that needs to be sent.
137  * @param[in]   length               length of the data that needs to be sent.
138  * @param[in]   data                 data that needs to be sent.
139  * @return  created list.
140  */
141 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
142
143 /**
144  * order the inserted options.
145  * need to replace queue head if new node has to be added before the existing queue head.
146  * @param[in]   a                    option 1 for insertion.
147  * @param[in]   b                    option 2 for insertion.
148  * @return  0 or 1.
149  */
150 int CAOrderOpts(void *a, void *b);
151
152 /**
153  * number of options count.
154  * @param[in]   opt_iter            option iteration for count.
155  * @return number of options.
156  */
157 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
158
159 /**
160  * gets option data.
161  * @param[in]   key                  ID of the option
162  * @param[in]   data                 data that is received.
163  * @param[in]   length               length of the data.
164  * @param[out]  option               result of the operation.
165  * @param[in]   buflen               buffer length of the result.
166  * @return  option count.
167  */
168 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
169                          uint8_t *option, uint32_t buflen);
170
171 /**
172  * extracts request information from received pdu.
173  * @param[in]    pdu                  received pdu.
174  * @param[in]    endpoint             endpoint information.
175  * @param[out]   outCode              code of the received pdu.
176  * @param[out]   outInfo              request info structure made from received pdu.
177  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
178  */
179 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
180                             uint32_t *outCode, CAInfo_t *outInfo);
181
182 /**
183  * create pdu from received data.
184  * @param[in]   data                received data.
185  * @param[in]   length              length of the data received.
186  * @param[out]  outCode             code received.
187  * @param[in]   endpoint            endpoint information.
188  * @return  coap_pdu_t value.
189  */
190 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
191                        const CAEndpoint_t *endpoint);
192
193 /**
194  * get Token from received data(pdu).
195  * @param[in]    pdu_hdr             header of received pdu.
196  * @param[out]   outInfo             information with token received.
197  * @param[in]    endpoint            endpoint information.
198  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
199  */
200 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
201                              const CAEndpoint_t *endpoint);
202
203 /**
204  * generates the token.
205  * @param[out]   token           generated token.
206  * @param[in]    tokenLength     length of the token.
207  * @return  CA_STATUS_OK or ERROR CODES (CAResult_t error codes in cacommon.h).
208  */
209 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
210
211 /**
212  * destroys the token.
213  * @param[in]   token           generated token.
214  */
215 void CADestroyTokenInternal(CAToken_t token);
216
217 /**
218  * destroy the ca info structure.
219  * @param[in]   info            info structure  created from received  packet.
220  */
221 void CADestroyInfo(CAInfo_t *info);
222
223 /**
224  * gets message type from PDU binary data.
225  * @param[in]   pdu                 pdu data.
226  * @param[in]   size                size of pdu data.
227  * @return  message type.
228  */
229 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
230
231 /**
232  * gets message ID PDU binary data.
233  * @param[in]   pdu                 pdu data.
234  * @param[in]   size                size of pdu data.
235  * @return  message ID.
236  */
237 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
238
239 /**
240  * gets code PDU binary data.
241  * @param[in]   pdu                 pdu data.
242  * @param[in]   size                size of pdu data.
243  * @return  code.
244  */
245 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);
246
247 /**
248  * convert format from CoAP media type encoding to CAPayloadFormat_t.
249  * @param[in]   format              coap format code.
250  * @return format.
251  */
252 CAPayloadFormat_t CAConvertFormat(uint8_t format);
253
254 #ifdef __cplusplus
255 } /* extern "C" */
256 #endif
257
258 #endif /* CA_PROTOCOL_MESSAGE_H_ */