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