1 /* *****************************************************************
3 * Copyright 2015 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 ******************************************************************/
23 * This file contains common function for block messages.
26 #ifndef CA_BLOCKWISETRANSFER_H_
27 #define CA_BLOCKWISETRANSFER_H_
31 #include <coap/coap.h>
32 #include "cathreadpool.h"
34 #include "uarraylist.h"
36 #include "caprotocolmessage.h"
37 #include "camessagehandler.h"
45 * Callback to send block data.
46 * @param[in] data send data.
48 typedef void (*CASendThreadFunc)(CAData_t *data);
51 * Callback to notify received data from the remote endpoint.
52 * @param[in] data received data.
54 typedef void (*CAReceiveThreadFunc)(CAData_t *data);
57 * context of blockwise transfer.
61 /** send method for block data. **/
62 CASendThreadFunc sendThreadFunc;
64 /** callback function for received message. **/
65 CAReceiveThreadFunc receivedThreadFunc;
67 /** array list on which the thread is operating. **/
68 u_arraylist_t *dataList;
70 /** data list mutex for synchronization. **/
71 oc_mutex blockDataListMutex;
73 /** sender mutex for synchronization. **/
74 oc_mutex blockDataSenderMutex;
75 } CABlockWiseContext_t;
78 * ID set of Blockwise transfer data set(::CABlockData_t).
82 uint8_t* id; /**< blockData ID for CA. */
83 size_t idLength; /**< length of blockData ID. */
91 coap_block_t block1; /**< block1 option. */
92 coap_block_t block2; /**< block2 option. */
93 uint16_t type; /**< block option type. */
94 CABlockDataID_t* blockDataId; /**< ID set of CABlockData. */
95 CAData_t *sentData; /**< sent request or response data information. */
96 CAPayload_t payload; /**< payload buffer. */
97 size_t payloadLength; /**< the total payload length to be received. */
98 size_t receivedPayloadLen; /**< currently received payload length. */
99 uint64_t ttl; /** The TTL for this blockData. */
103 * state of received block message from remote endpoint.
107 CA_BLOCK_UNKNOWN = 0,
109 CA_OPTION1_REQUEST_LAST_BLOCK,
110 CA_OPTION1_REQUEST_BLOCK,
111 CA_OPTION2_FIRST_BLOCK,
112 CA_OPTION2_LAST_BLOCK,
117 CA_BLOCK_RECEIVED_ALREADY
121 * Initializes the block-wise transfer context.
122 * @param[in] CASendThreadFunc function point to add data in send queue thread.
123 * @param[in] CAReceiveThreadFunc function point to add data in receive queue thread.
124 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
126 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc blockSendMethod,
127 CAReceiveThreadFunc receivedDataCallback);
130 * Terminate the block-wise transfer context.
131 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
133 CAResult_t CATerminateBlockWiseTransfer();
137 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
139 CAResult_t CAInitBlockWiseMutexVariables();
144 void CATerminateBlockWiseMutexVariables();
147 * Pass the bulk data. if block-wise transfer process need,
148 * bulk data will be sent to block messages.
149 * @param[in] data data for sending.
150 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
152 CAResult_t CASendBlockWiseData(const CAData_t *data);
155 * Add the data to send thread queue.
156 * @param[in] sendData data for sending.
157 * @param[in] blockID ID set of CABlockData.
158 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
160 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID);
163 * Check the block option type. If it has to be sent to a block,
164 * block option will be set.
165 * @param[in] currData block data.
166 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
168 CAResult_t CACheckBlockOptionType(CABlockData_t *currData);
171 * Pass the received pdu data. and check if block option is set.
172 * @param[in] pdu received pdu binary data.
173 * @param[in] endpoint information of remote device.
174 * @param[in] receivedData received CAData.
175 * @param[in] dataLen received data length.
176 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
178 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
179 const CAData_t *receivedData, size_t dataLen);
182 * process next step by block-wise state.
183 * @param[in] pdu received pdu binary data.
184 * @param[in] receivedData received CAData.
185 * @param[in] blockWiseStatus block-wise state to move next step.
186 * @param[in] blockID ID set of CABlockData.
187 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
189 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
190 uint8_t blockWiseStatus, const CABlockDataID_t *blockID);
193 * send block message to remote device.
194 * @param[in] pdu received pdu binary data.
195 * @param[in] msgType the message type of the block.
196 * @param[in] blockID ID set of CABlockData.
197 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
199 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
200 const CABlockDataID_t *blockID);
203 * send error message to remote device.
204 * @param[in] pdu received pdu binary data.
205 * @param[in] status block-wise state to move next step.
206 * @param[in] responseResult response result code.
207 * @param[in] blockID ID set of CABlockData.
208 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
210 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
211 CAResponseResult_t responseResult,
212 const CABlockDataID_t *blockID);
215 * receive last block data.
216 * @param[in] blockID ID set of CABlockData.
217 * @param[in] receivedData received CAData.
218 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
220 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
221 const CAData_t *receivedData);
224 * set next block option 1.
225 * @param[in] pdu received pdu binary data.
226 * @param[in] endpoint information of remote device.
227 * @param[in] receivedData received CAData.
228 * @param[in] block block option data.
229 * @param[in] dataLen received data length.
230 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
232 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
233 const CAData_t *receivedData, coap_block_t block,
237 * set next block option 2.
238 * @param[in] pdu received pdu binary data.
239 * @param[in] endpoint information of remote device.
240 * @param[in] receivedData received CAData.
241 * @param[in] block block option data.
242 * @param[in] dataLen received data length.
243 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
245 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
246 const CAData_t *receivedData, coap_block_t block,
250 * Update the block option in block-wise transfer list.
251 * @param[in] currData stored block data information.
252 * @param[in] block block option to update.
253 * @param[in] msgType message type of pdu.
254 * @param[in] blockType block option type.
255 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
257 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
258 const coap_pdu_t *pdu, uint16_t blockType);
261 * Update the block option in block-wise transfer list.
262 * @param[in] currData stored block data information.
263 * @param[in] block block option of current message.
264 * @param[in] blockType block option type.
265 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
267 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
271 * Update the messageId in block-wise transfer list.
272 * @param[in] pdu received pdu binary data.
273 * @param[in] blockID ID set of CABlockData.
274 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
276 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID);
279 * Update the block option items.
280 * @param[in] currData stored block data information.
281 * @param[in] pdu received pdu binary data.
282 * @param[in/out] block block option of current message.
283 * @param[in] blockType block option type.
284 * @param[in] status current flow status for block-wise transfer.
285 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
287 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
288 coap_block_t *block, uint16_t blockType,
291 * Set the M-bit of block option.
292 * @param[in] payloadLen payload length of current bulk data.
293 * @param[out] block block option.
294 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
296 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block);
299 * check the block option what kind of option have to set.
300 * @param[out] pdu pdu object.
301 * @param[in] info information of the request/response.
302 * @param[in] endpoint port of transport.
303 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
305 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
306 const CAEndpoint_t *endpoint, coap_list_t **options);
309 * Write the block option2 in pdu binary data.
310 * @param[out] pdu pdu object.
311 * @param[in] info information of the request/response.
312 * @param[in] dataLength length of payload.
313 * @param[in] blockID ID set of CABlockData.
314 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
316 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
317 const CABlockDataID_t *blockID, coap_list_t **options);
320 * Write the block option1 in pdu binary data.
321 * @param[out] pdu pdu object.
322 * @param[in] info information of the request/response.
323 * @param[in] dataLength length of payload.
324 * @param[in] blockID ID set of CABlockData.
325 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
327 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
328 const CABlockDataID_t *blockID, coap_list_t **options);
331 * Add the block option in option list.
332 * @param[out] block block data.
333 * @param[in] blockType block option type.
334 * @param[out] options option list.
335 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
337 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
338 coap_list_t **options);
341 * Add the option list in pdu data.
342 * @param[out] pdu pdu object.
343 * @param[out] options option list.
344 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
346 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options);
349 * Add the size option in pdu data.
350 * @param[in/out] pdu pdu object.
351 * @param[in] sizeType size option type.
352 * @param[in] dataLength the total payload length to be sent.
353 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
355 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
356 coap_list_t **options);
359 * Get the size option from pdu data.
360 * @param[in] pdu pdu object.
361 * @param[in] sizeType size option type.
362 * @param[out] totalPayloadLen the total payload length to be received.
363 * @return true or false.
365 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
367 size_t *totalPayloadLen);
370 * update the total payload with the received payload.
371 * @param[in] currData stored block data information.
372 * @param[in] receivedData received CAData.
373 * @param[in] status block-wise state.
374 * @param[in] isSizeOption size option.
375 * @param[in] blockType block option type.
376 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
378 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
379 uint8_t status, bool isSizeOption, uint16_t blockType);
382 * Generate CAData structure from the given information.
383 * @param[in] pdu pdu object.
384 * @param[in] endpoint information of remote device.
385 * @return generated CAData.
387 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint);
390 * Update the block option items.
391 * @param[in/out] blockblock option of current message.
392 * @param[in] blockType block option type.
393 * @param[in] responseResult result code of pdu.
394 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
396 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
397 uint32_t responseResult);
400 * Check the received payload and if an error happens, return error type.
401 * @param[in/out] currData stored block data information.
402 * @param[in] receivedBlock received block option.
403 * @param[in] receivedData message type of pdu.
404 * @param[in] blockType block option type.
405 * @param[in] dataLen received data length.
406 * @return block state.
408 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
409 const CAData_t *receivedData, uint16_t blockType,
413 * Destroys the given CAData.
414 * @param[in] data CAData to destroy.
415 * @return generated CAData.
417 void CADestroyDataSet(CAData_t* data);
420 * Create the blockId for CABlockData.
421 * @param[in] token token of current message.
422 * @param[in] tokenLength token length of current message.
423 * @param[in] portNumber port.
424 * @return ID set of CABlockData.
426 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
427 uint16_t portNumber);
430 * Destroy the blockId set.
431 * @param[in] blockID ID set of CABlockData.
433 void CADestroyBlockID(CABlockDataID_t *blockID);
436 * check whether Block ID is matched or not.
437 * @param[in] currData block data.
438 * @param[in] blockID ID set of CABlockData.
439 * @return true or false.
441 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID);
443 * Print the given block option information.
444 * @param[in] block block option information.
446 void CALogBlockInfo(coap_block_t *block);
449 * Create new CAData structure from the input information.
450 * @param[in] data CAData information that needs to be duplicated.
451 * @return created CAData structure.
453 CAData_t *CACloneCAData(const CAData_t *data);
456 * Update payload from the input information.
457 * @param[in] data CAData information that needs to be updated.
458 * @param[in] payload received new payload from the remote endpoint.
459 * @param[in] payloadLen received full payload length.
460 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
462 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
466 * Get payload and payload length from the input information.
467 * @param[in] data CAData information.
468 * @param[out] payloadLen The payload length is stored.
471 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen);
474 * Set the block option type.
475 * @param[in] blockID ID set of CABlockData.
476 * @param[in] blockType block option type.
477 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
479 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
483 * Get the block option type from block-wise transfer list.
484 * @param[in] blockID ID set of CABlockData.
485 * @return block option type.
487 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID);
490 * Get the block data from block-wise transfer list.
491 * @param[in] blockID ID set of CABlockData.
492 * @return CAData structure.
494 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID);
497 * Update the block data from block-wise transfer list.
498 * @param[in] blockID ID set of CABlockData.
499 * @param[in] sendData New block date should be sent.
500 * @return CAData structure.
502 CABlockData_t *CAUpdateDataSetFromBlockDataList(const CABlockDataID_t *blockID,
503 const CAData_t *sendData);
506 * Get token from block-wise transfer list.
507 * @param[in] pdu received pdu binary data.
508 * @param[in] endpoint remote endpoint information.
509 * @param[in] responseInfo received response information.
510 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
512 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
513 CAResponseInfo_t *responseInfo);
516 * check whether the block data is valid or not.
517 * @param[in] sendData CAData information.
518 * @param[out] blockData block data when it is valid.
519 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
521 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData);
524 * Get the block data from block-wise transfer list.
525 * @param[in] blockID ID set of CABlockData.
526 * @return CABlockData_t structure.
528 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID);
531 * Get the block option from block-wise transfer list.
532 * @param[in] blockID ID set of CABlockData.
533 * @param[in] blockType block option type.
534 * @return coap_block_t structure.
536 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
540 * Get the full payload from block-wise list.
541 * @param[in] blockID ID set of CABlockData.
542 * @param[out] fullPayloadLen received full payload length.
545 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
546 size_t *fullPayloadLen);
549 * Create the block data from given data and add the data in block-wise transfer list.
550 * @param[in] sendData data to be added to a list.
551 * @return created CABlockData_t structure.
552 * and NULL point will be returned if there is error case..
554 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData);
557 * Remove the block data in block-wise transfer list.
558 * @param[in] blockID ID set of CABlockData.
559 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
561 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID);
564 * Remove all block data in block-wise transfer list.
565 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
567 CAResult_t CARemoveAllBlockDataFromList();
570 * Find the block data with seed info and remove it from block-wise transfer list.
571 * @param[in] token token of the message.
572 * @param[in] tokenLength token length of the message.
573 * @param[in] portNumber port.
574 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
576 CAResult_t CARemoveBlockDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength,
577 uint16_t portNumber);
580 * Reset TTL for a blockData.
582 * @param[in] blockID ID set of CABlockData.
584 void CAResetBlockDataTTL(const CABlockDataID_t *blockID);
587 * Checks if the blockData is past its time to live and deletes it if timed-out.
589 void CACheckAndDeleteTimedOutBlockData();
594 #endif // CA_BLOCKWISETRANSFER_H_