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;
76 /** array list tracking multicast requests. **/
77 u_arraylist_t *multicastDataList;
79 /** mulitcast data list mutex for synchronization. **/
80 oc_mutex multicastDataListMutex;
81 } CABlockWiseContext_t;
84 * ID set of Blockwise transfer data set(::CABlockData_t).
88 uint8_t* id; /**< blockData ID for CA. */
89 size_t idLength; /**< length of blockData ID. */
97 coap_block_t block1; /**< block1 option. */
98 coap_block_t block2; /**< block2 option. */
99 uint16_t type; /**< block option type. */
100 CABlockDataID_t* blockDataId; /**< ID set of CABlockData. */
101 CAData_t *sentData; /**< sent request or response data information. */
102 CAPayload_t payload; /**< payload buffer. */
103 size_t payloadLength; /**< the total payload length to be received. */
104 size_t receivedPayloadLen; /**< currently received payload length. */
108 * state of received block message from remote endpoint.
112 CA_BLOCK_UNKNOWN = 0,
114 CA_OPTION1_REQUEST_LAST_BLOCK,
115 CA_OPTION1_REQUEST_BLOCK,
116 CA_OPTION2_FIRST_BLOCK,
117 CA_OPTION2_LAST_BLOCK,
122 CA_BLOCK_RECEIVED_ALREADY
130 CAToken_t token; /**< Token for CA */
131 uint8_t tokenLength; /**< token length */
132 CAURI_t resourceUri; /**< Resource URI information **/
133 } CABlockMulticastData_t;
136 * Initializes the block-wise transfer context.
137 * @param[in] CASendThreadFunc function point to add data in send queue thread.
138 * @param[in] CAReceiveThreadFunc function point to add data in receive queue thread.
139 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
141 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc blockSendMethod,
142 CAReceiveThreadFunc receivedDataCallback);
145 * Terminate the block-wise transfer context.
146 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
148 CAResult_t CATerminateBlockWiseTransfer(void);
152 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
154 CAResult_t CAInitBlockWiseMutexVariables(void);
159 void CATerminateBlockWiseMutexVariables(void);
162 * Pass the bulk data. if block-wise transfer process need,
163 * bulk data will be sent to block messages.
164 * @param[in] data data for sending.
165 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
167 CAResult_t CASendBlockWiseData(const CAData_t *data);
170 * Add the data to send thread queue.
171 * @param[in] sendData data for sending.
172 * @param[in] blockID ID set of CABlockData.
173 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
175 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID);
178 * Check the block option type. If it has to be sent to a block,
179 * block option will be set.
180 * @param[in] currData block data.
181 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
183 CAResult_t CACheckBlockOptionType(CABlockData_t *currData);
186 * Pass the received pdu data. and check if block option is set.
187 * @param[in] pdu received pdu binary data.
188 * @param[in] endpoint information of remote device.
189 * @param[in] receivedData received CAData.
190 * @param[in] dataLen received data length.
191 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
193 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
194 const CAData_t *receivedData, size_t dataLen);
197 * process next step by block-wise state.
198 * @param[in] pdu received pdu binary data.
199 * @param[in] receivedData received CAData.
200 * @param[in] blockWiseStatus block-wise state to move next step.
201 * @param[in] blockID ID set of CABlockData.
202 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
204 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
205 uint8_t blockWiseStatus, const CABlockDataID_t *blockID);
208 * send block message to remote device.
209 * @param[in] pdu received pdu binary data.
210 * @param[in] msgType the message type of the block.
211 * @param[in] blockID ID set of CABlockData.
212 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
214 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
215 const CABlockDataID_t *blockID);
218 * send error message to remote device.
219 * @param[in] pdu received pdu binary data.
220 * @param[in] status block-wise state to move next step.
221 * @param[in] responseResult response result code.
222 * @param[in] blockID ID set of CABlockData.
223 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
225 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
226 CAResponseResult_t responseResult,
227 const CABlockDataID_t *blockID);
230 * receive last block data.
231 * @param[in] blockID ID set of CABlockData.
232 * @param[in] receivedData received CAData.
233 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
235 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
236 const CAData_t *receivedData);
239 * set next block option 1.
240 * @param[in] pdu received pdu binary data.
241 * @param[in] endpoint information of remote device.
242 * @param[in] receivedData received CAData.
243 * @param[in] block block option data.
244 * @param[in] dataLen received data length.
245 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
247 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
248 const CAData_t *receivedData, coap_block_t block,
252 * set next block option 2.
253 * @param[in] pdu received pdu binary data.
254 * @param[in] endpoint information of remote device.
255 * @param[in] receivedData received CAData.
256 * @param[in] block block option data.
257 * @param[in] dataLen received data length.
258 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
260 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
261 const CAData_t *receivedData, coap_block_t block,
265 * Update the block option in block-wise transfer list.
266 * @param[in] currData stored block data information.
267 * @param[in] block block option to update.
268 * @param[in] msgType message type of pdu.
269 * @param[in] blockType block option type.
270 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
272 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
273 const coap_pdu_t *pdu, uint16_t blockType);
276 * Update the block option in block-wise transfer list.
277 * @param[in] currData stored block data information.
278 * @param[in] block block option of current message.
279 * @param[in] blockType block option type.
280 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
282 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
286 * Update the messageId in block-wise transfer list.
287 * @param[in] pdu received pdu binary data.
288 * @param[in] blockID ID set of CABlockData.
289 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
291 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID);
294 * Update the block option items.
295 * @param[in] currData stored block data information.
296 * @param[in] pdu received pdu binary data.
297 * @param[in,out] block block option of current message.
298 * @param[in] blockType block option type.
299 * @param[in] status current flow status for block-wise transfer.
300 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
302 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
303 coap_block_t *block, uint16_t blockType,
306 * Set the M-bit of block option.
307 * @param[in] payloadLen payload length of current bulk data.
308 * @param[out] block block option.
309 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
311 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block);
314 * check the block option what kind of option have to set.
315 * @param[out] pdu pdu object.
316 * @param[in] info information of the request/response.
317 * @param[in] endpoint port of transport.
318 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
320 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
321 const CAEndpoint_t *endpoint, coap_list_t **options);
324 * Write the block option2 in pdu binary data.
325 * @param[out] pdu pdu object.
326 * @param[in] info information of the request/response.
327 * @param[in] dataLength length of payload.
328 * @param[in] blockID ID set of CABlockData.
329 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
331 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
332 const CABlockDataID_t *blockID, coap_list_t **options);
335 * Write the block option1 in pdu binary data.
336 * @param[out] pdu pdu object.
337 * @param[in] info information of the request/response.
338 * @param[in] dataLength length of payload.
339 * @param[in] blockID ID set of CABlockData.
340 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
342 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
343 const CABlockDataID_t *blockID, coap_list_t **options);
346 * Add the block option in option list.
347 * @param[out] block block data.
348 * @param[in] blockType block option type.
349 * @param[out] options option list.
350 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
352 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
353 coap_list_t **options);
356 * Add the option list in pdu data.
357 * @param[out] pdu pdu object.
358 * @param[out] options option list.
359 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
361 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options);
364 * Add the size option in pdu data.
365 * @param[in,out] pdu pdu object.
366 * @param[in] sizeType size option type.
367 * @param[in] dataLength the total payload length to be sent.
368 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
370 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
371 coap_list_t **options);
374 * Get the size option from pdu data.
375 * @param[in] pdu pdu object.
376 * @param[in] sizeType size option type.
377 * @param[out] totalPayloadLen the total payload length to be received.
378 * @return true or false.
380 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
382 size_t *totalPayloadLen);
385 * update the total payload with the received payload.
386 * @param[in] currData stored block data information.
387 * @param[in] receivedData received CAData.
388 * @param[in] status block-wise state.
389 * @param[in] isSizeOption size option.
390 * @param[in] blockType block option type.
391 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
393 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
394 uint8_t status, bool isSizeOption, uint16_t blockType);
397 * Generate CAData structure from the given information.
398 * @param[in] pdu pdu object.
399 * @param[in] endpoint information of remote device.
400 * @return generated CAData.
402 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint);
405 * Update the block option items.
406 * @param[in,out] blockblock option of current message.
407 * @param[in] blockType block option type.
408 * @param[in] responseResult result code of pdu.
409 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
411 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
412 uint32_t responseResult);
415 * Check the received payload and if an error happens, return error type.
416 * @param[in,out] currData stored block data information.
417 * @param[in] receivedBlock received block option.
418 * @param[in] receivedData message type of pdu.
419 * @param[in] blockType block option type.
420 * @param[in] dataLen received data length.
421 * @return block state.
423 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
424 const CAData_t *receivedData, uint16_t blockType,
428 * Destroys the given CAData.
429 * @param[in] data CAData to destroy.
430 * @return generated CAData.
432 void CADestroyDataSet(CAData_t* data);
435 * Create the blockId for CABlockData.
436 * @param[in] token token of current message.
437 * @param[in] tokenLength token length of current message.
438 * @param[in] addr address, must be MAX_ADDR_STR_SIZE_CA in length.
439 * @param[in] portNumber port.
440 * @return ID set of CABlockData.
442 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
443 const char* addr, uint16_t portNumber);
446 * Destroy the blockId set.
447 * @param[in] blockID ID set of CABlockData.
449 void CADestroyBlockID(CABlockDataID_t *blockID);
452 * check whether Block ID is matched or not.
453 * @param[in] currData block data.
454 * @param[in] blockID ID set of CABlockData.
455 * @return true or false.
457 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID);
459 * Print the given block option information.
460 * @param[in] block block option information.
462 void CALogBlockInfo(coap_block_t *block);
465 * Create new CAData structure from the input information.
466 * @param[in] data CAData information that needs to be duplicated.
467 * @return created CAData structure.
469 CAData_t *CACloneCAData(const CAData_t *data);
472 * Update payload from the input information.
473 * @param[in] data CAData information that needs to be updated.
474 * @param[in] payload received new payload from the remote endpoint.
475 * @param[in] payloadLen received full payload length.
476 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
478 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
482 * Get payload and payload length from the input information.
483 * @param[in] data CAData information.
484 * @param[out] payloadLen The payload length is stored.
487 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen);
490 * Set the block option type.
491 * @param[in] blockID ID set of CABlockData.
492 * @param[in] blockType block option type.
493 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
495 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
499 * Get the block option type from block-wise transfer list.
500 * @param[in] blockID ID set of CABlockData.
501 * @return block option type.
503 uint16_t CAGetBlockOptionType(const CABlockDataID_t *blockID);
506 * Get the block data from block-wise transfer list.
507 * @param[in] blockID ID set of CABlockData.
508 * @return CAData structure.
510 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID);
513 * Update the block data from block-wise transfer list.
514 * @param[in] blockID ID set of CABlockData.
515 * @param[in] sendData New block date should be sent.
516 * @return CABlockData_t structure.
518 CABlockData_t *CAUpdateDataSetFromBlockDataList(const CABlockDataID_t *blockID,
519 const CAData_t *sendData);
522 * Get token from block-wise transfer list.
523 * @param[in] pdu received pdu binary data.
524 * @param[in] endpoint remote endpoint information.
525 * @param[in] responseInfo received response information.
526 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
528 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
529 CAResponseInfo_t *responseInfo);
532 * check whether the block data is valid or not.
533 * @param[in] sendData CAData information.
534 * @param[out] blockData block data when it is valid.
535 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
537 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData);
540 * Get the block data from block-wise transfer list.
541 * @param[in] blockID ID set of CABlockData.
542 * @return CABlockData_t structure.
544 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID);
547 * Get the block option from block-wise transfer list.
548 * @param[in] blockID ID set of CABlockData.
549 * @param[in] blockType block option type.
550 * @return coap_block_t structure.
552 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
556 * Get the full payload from block-wise list.
557 * @param[in] blockID ID set of CABlockData.
558 * @param[out] fullPayloadLen received full payload length.
561 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
562 size_t *fullPayloadLen);
565 * Create the block data from given data and add the data in block-wise transfer list.
566 * @param[in] sendData data to be added to a list.
567 * @return created CABlockData_t structure.
568 * and NULL point will be returned if there is error case..
570 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData);
573 * Remove the block data in block-wise transfer list.
574 * @param[in] blockID ID set of CABlockData.
575 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
577 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID);
580 * Remove all block data in block-wise transfer list.
581 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
583 CAResult_t CARemoveAllBlockDataFromList(void);
586 * Find the block data with seed info and remove it from block-wise transfer list.
587 * @param[in] token token of the message.
588 * @param[in] tokenLength token length of the message.
589 * @param[in] addr address, must be MAX_ADDR_STR_SIZE_CA in length.
590 * @param[in] portNumber port.
591 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
593 CAResult_t CARemoveBlockDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength,
594 const char* addr, uint16_t portNumber);
597 * Create the mulitcast data from given data and add the data in multicast list.
598 * @param[in] sendData data to be added to a list.
599 * @return created CABlockMulticastData_t structure,
600 * or NULL pointer will be returned if there is error case.
602 CABlockMulticastData_t *CACreateNewBlockMulticastData(const CAData_t *sendData);
605 * Get the block multicast data from list.
606 * @param[in] token token of the message.
607 * @param[in] tokenLength token length of the message.
608 * @return CABlockMulticastData_t structure.
610 CABlockMulticastData_t *CAGetBlockMulticastDataFromListWithSeed(const CAToken_t token,
611 uint8_t tokenLength);
614 * Remove all block multicast data in list.
615 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
617 CAResult_t CARemoveAllBlockMulticastDataFromList(void);
620 * Find the block data with seed info and remove it.
621 * @param[in] token token of the message.
622 * @param[in] tokenLength token length of the message.
623 * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
625 CAResult_t CARemoveBlockMulticastDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength);
631 #endif // CA_BLOCKWISETRANSFER_H_