wrap CAcloseSslConnectionFreeEndpoint method with __TIZEN__ flag
[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 #ifndef WITH_UPSTREAM_LIBCOAP
30 #include "coap/config.h"
31 #endif
32 #include <coap/coap.h>
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #endif
38
39 typedef uint32_t code_t;
40
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))))
43
44
45 // Include files from the arduino platform do not provide these conversions:
46 #ifdef ARDUINO
47 #define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
48 #define ntohs(x) htons(x)
49 #else
50 #define HAVE_TIME_H 1
51 #endif
52
53 static const uint8_t PAYLOAD_MARKER = 1;
54
55 /**
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.
61  */
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);
64
65 /**
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).
71  */
72 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
73                                    CARequestInfo_t *outReqInfo);
74
75 /**
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).
81  */
82 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
83                                     const CAEndpoint_t *endpoint);
84
85 /**
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).
91  */
92 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
93                                  CAErrorInfo_t *errorInfo);
94
95 /**
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.
102  */
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);
106
107 /**
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).
112  */
113 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);
114
115 /**
116  * Helper that uses libcoap to parse either the path or the parameters of a URI
117  * and populate the supplied options list.
118  *
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).
125  */
126 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
127                              coap_list_t **optlist);
128
129 /**
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).
135  */
136 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
137
138 /**
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.
144  */
145 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
146
147 /**
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.
152  * @return  0 or 1.
153  */
154 int CAOrderOpts(void *a, void *b);
155
156 /**
157  * number of options count.
158  * @param[in]   opt_iter            option iteration for count.
159  * @return number of options.
160  */
161 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
162
163 /**
164  * gets option data.
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.
171  */
172 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
173                          uint8_t *option, uint32_t buflen);
174
175 /**
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).
182  */
183 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
184                             uint32_t *outCode, CAInfo_t *outInfo);
185
186 /**
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.
193  */
194 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
195                        const CAEndpoint_t *endpoint);
196
197 /**
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).
203  */
204 CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
205                              CAInfo_t *outInfo,
206                              const CAEndpoint_t *endpoint);
207
208 /**
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).
213  */
214 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
215
216 /**
217  * destroys the token.
218  * @param[in]   token           generated token.
219  */
220 void CADestroyTokenInternal(CAToken_t token);
221
222 /**
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.
227  */
228 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
229
230 /**
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.
235  */
236 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
237
238 /**
239  * gets code PDU binary data.
240  * @param[in]   pdu                 pdu data.
241  * @param[in]   size                size of pdu data.
242  * @return  code.
243  */
244 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);
245
246 /**
247  * convert format from CoAP media type encoding to CAPayloadFormat_t.
248  * @param[in]   format              coap format code.
249  * @return format.
250  */
251 CAPayloadFormat_t CAConvertFormat(uint8_t format);
252
253 #ifdef WITH_TCP
254 /**
255  * check whether CoAP over TCP is supported or not.
256  * @param[in]   adapter             transport adapter type.
257  * @return true or false.
258  */
259 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter);
260 #endif
261
262 #ifdef WITH_BWT
263 /**
264  * check whether blockwise transfer is supported or not.
265  * @param[in]   adapter             transport adapter type.
266  * @return true or false.
267  */
268 bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter);
269 #endif
270
271 #ifdef __cplusplus
272 } /* extern "C" */
273 #endif
274
275 #endif /* CA_PROTOCOL_MESSAGE_H_ */