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 ******************************************************************/
24 * This file contains common function for handling protocol messages.
27 #ifndef __CA_PROTOCOL_MESSAGE_SINGLETHREAD_H_
28 #define __CA_PROTOCOL_MESSAGE_SINGLETHREAD_H_
35 #define htons(x) ( ((x)<< 8 & 0xFF00) | ((x)>> 8 & 0x00FF) )
36 #define ntohs(x) htons(x)
43 typedef uint32_t code_t;
46 * @brief generates pdu structure from the given information.
47 * @param uri [IN] uri information of the pdu
48 * @param code [IN] code of the pdu packet
49 * @param info [IN] pdu information such as request code ,response code and payload
50 * @return coap_pdu_t created pdu
52 coap_pdu_t *CAGeneratePdu(const char *uri, uint32_t code, const CAInfo_t info);
55 * function for generating
59 * @brief extracts request information from received pdu.
60 * @param pdu [IN] received pdu
61 * @param outReqInfo [OUT] request info structure made from received pdu
62 * @param outUri [OUT] uri received in the received pdu
63 * @param buflen [IN] Buffer Length for outUri parameter
66 void CAGetRequestInfoFromPdu(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo,
67 char *outUri, uint32_t bufLen);
70 * @brief extracts response information from received pdu.
71 * @param pdu [IN] received pdu
72 * @param outResInfo [OUT] response info structure made from received pdu
73 * @param outUri [OUT] uri received in the received pdu
74 * @param buflen [IN] Buffer Length for outUri parameter
77 void CAGetResponseInfoFromPdu(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
78 char *outUri, uint32_t bufLen);
81 * @brief creates pdu from the request information
82 * @param code [IN] request or response code
83 * @param options [OUT] options for the request and response
84 * @param info [IN] information to create pdu
85 * @param payload [IN] payload for the request or response consumed
88 coap_pdu_t *CAGeneratePduImpl(const code_t code, coap_list_t *options,
89 const CAInfo_t info, const char *payload);
92 * @brief parse the URI and creates the options
93 * @param uriInfo [IN] uri information
94 * @param options [OUT] options information
97 void CAParseURI(const char *uriInfo, coap_list_t **options);
100 * @brief create option list from header information in the info
101 * @param code [IN] uri information
102 * @param info [IN] options information
103 * @param optlist [OUT] options information
106 void CAParseHeadOption(uint32_t code, const CAInfo_t info, coap_list_t **optlist);
109 * @brief creates option node from key length and data
110 * need to replace queue head if new node has to be added before the existing queue head
111 * @param key [IN] key for the that needs to be sent
112 * @param length [IN] length of the data that needs to be sent
113 * @param data [IN] data that needs to be sent
114 * @return created list
116 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length,
117 const uint8_t *data);
120 * @brief order the inserted options
121 * @param a [IN] option 1 for insertion
122 * @param b [IN] option 2 for insertion
125 int CAOrderOpts(void *a, void *b);
128 * @brief number of options count
129 * @param opt_iter [IN] option iteration for count
130 * @return number of options
132 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter);
135 * @brief gets option data
136 * @param data [IN] data that is received
137 * @param length [IN] length of the data
138 * @param option [OUT] result of the operation
139 * @param buflen [IN] buffer length of the result
140 * @return option count
142 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen);
145 * @brief extracts request information from received pdu.
146 * @param pdu [IN] received pdu
147 * @param outCode [OUT] code of the received pdu
148 * @param outInfo [OUT] request info structure made from received pdu
149 * @param outUri [OUT] uri received in the received pdu
150 * @param buflen [IN] Buffer Length for outUri parameter
153 void CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo,
154 char *outUri, uint32_t bufLen);
157 * @brief create pdu fromn received data
158 * @param data [IN] received data
159 * @param length [IN] length of the data received
160 * @param outCode [OUT] code received
163 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode);
166 * @brief generates the token
167 * @param token [OUT] generated token
168 * @return CA_STATUS_OK or ERROR CODES ( CAResult_t error codes in cacommon.h)
171 CAResult_t CAGenerateTokenInternal(CAToken_t *token);
174 * @brief destroys the token
175 * @param token [IN] generated token
178 void CADestroyTokenInternal(CAToken_t token);
181 * @brief destroy the ca info structure
182 * @param info [IN] info structure created from received packet
185 void CADestroyInfo(CAInfo_t *info);
188 * @brief gets message type from PDU binary data
189 * @param pdu [IN] pdu data
190 * @param size [IN] size of pdu data
191 * @return message type
193 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size);
196 * @brief gets message ID PDU binary data
197 * @param pdu [IN] pdu data
198 * @param size [IN] size of pdu data
201 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size);
207 #endif //#ifndef __CA_PROTOCOL_MESSAGE_SINGLETHREAD_H_