Imported Upstream version 0.9.2
[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 caprotocolmessage.h
22  * @brief 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  * @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
57  */
58 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info);
59
60 /**
61  * function for generating
62  */
63
64 /**
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)
69  */
70 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo);
71
72 /**
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)
77  */
78 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo);
79
80 /**
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)
85  */
86 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, CAErrorInfo_t *errorInfo);
87
88 /**
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
95  */
96 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
97                               const uint8_t *payload, size_t payloadSize);
98
99 /**
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)
104  */
105 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **options);
106
107 /**
108  * @brief   Helper that uses libcoap to parse either the path or the parameters of a URI
109  *          and populate the supplied options list.
110  *
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)
117  */
118 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
119                              coap_list_t **optlist);
120
121 /**
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)
127  */
128 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist);
129
130 /**
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
136  */
137 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data);
138
139 /**
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
144  * @return  0 or 1
145  */
146 int CAOrderOpts(void *a, void *b);
147
148 /**
149  * @brief   number of options count
150  * @param   opt_iter         [IN]   option iteration for count
151  * @return number of options
152  */
153 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
154
155 /**
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
162  */
163 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen);
164
165 /**
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)
171  */
172 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo);
173
174 /**
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
180  */
181 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode);
182
183 /**
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)
188  */
189 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo);
190
191 /**
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)
196  */
197 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength);
198
199 /**
200  * @brief   destroys the token
201  * @param   token            [IN]   generated token
202  * @return  None
203  */
204 void CADestroyTokenInternal(CAToken_t token);
205
206 /**
207  * @brief   destroy the ca info structure
208  * @param   info             [IN]   info structure  created from received  packet
209  * @return  None
210  */
211 void CADestroyInfo(CAInfo_t *info);
212
213 /**
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
218  */
219 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
220
221 /**
222  * @brief   gets message ID PDU binary data
223  * @param   pdu              [IN]   pdu data
224  * @param   size             [IN]   size of pdu data
225  * @return  message ID
226  */
227 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
228
229 /**
230  * @brief   gets code PDU binary data
231  * @param   pdu              [IN]   pdu data
232  * @param   size             [IN]   size of pdu data
233  * @return  code
234  */
235 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size);
236
237 #ifdef __cplusplus
238 } /* extern "C" */
239 #endif
240
241 #endif /* CA_PROTOCOL_MESSAGE_H_ */