replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / cablockwisetransfer.h
1 /* *****************************************************************
2  *
3  * Copyright 2015 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 /**
22  * @file
23  * This file contains common function for block messages.
24  */
25
26 #ifndef CA_BLOCKWISETRANSFER_H_
27 #define CA_BLOCKWISETRANSFER_H_
28
29 #include <stdint.h>
30
31 #include <coap/coap.h>
32 #include "cathreadpool.h"
33 #include "octhread.h"
34 #include "uarraylist.h"
35 #include "cacommon.h"
36 #include "caprotocolmessage.h"
37 #include "camessagehandler.h"
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43
44 /**
45  * Callback to send block data.
46  * @param[in]   data    send data.
47  */
48 typedef void (*CASendThreadFunc)(CAData_t *data);
49
50 /**
51  * Callback to notify received data from the remote endpoint.
52  * @param[in]   data    received data.
53  */
54 typedef void (*CAReceiveThreadFunc)(CAData_t *data);
55
56 /**
57  * context of blockwise transfer.
58  */
59 typedef struct
60 {
61     /** send method for block data. **/
62     CASendThreadFunc sendThreadFunc;
63
64     /** callback function for received message. **/
65     CAReceiveThreadFunc receivedThreadFunc;
66
67     /** array list on which the thread is operating. **/
68     u_arraylist_t *dataList;
69
70     /** data list mutex for synchronization. **/
71     oc_mutex blockDataListMutex;
72
73     /** sender mutex for synchronization. **/
74     oc_mutex blockDataSenderMutex;
75 } CABlockWiseContext_t;
76
77 /**
78  * ID set of Blockwise transfer data set(::CABlockData_t).
79  */
80 typedef struct
81 {
82     uint8_t* id;                       /**< blockData ID for CA. */
83     size_t idLength;                   /**< length of blockData ID. */
84 } CABlockDataID_t;
85
86 /**
87  * Block Data Set.
88  */
89 typedef struct
90 {
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. */
100 } CABlockData_t;
101
102 /**
103  * state of received block message from remote endpoint.
104  */
105 typedef enum
106 {
107     CA_BLOCK_UNKNOWN = 0,
108     CA_OPTION1_RESPONSE,
109     CA_OPTION1_REQUEST_LAST_BLOCK,
110     CA_OPTION1_REQUEST_BLOCK,
111     CA_OPTION2_FIRST_BLOCK,
112     CA_OPTION2_LAST_BLOCK,
113     CA_OPTION2_RESPONSE,
114     CA_OPTION2_REQUEST,
115     CA_BLOCK_INCOMPLETE,
116     CA_BLOCK_TOO_LARGE,
117     CA_BLOCK_RECEIVED_ALREADY
118 } CABlockState_t;
119
120 /**
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).
125  */
126 CAResult_t CAInitializeBlockWiseTransfer(CASendThreadFunc blockSendMethod,
127                                          CAReceiveThreadFunc receivedDataCallback);
128
129 /**
130  * Terminate the block-wise transfer context.
131  * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
132  */
133 CAResult_t CATerminateBlockWiseTransfer();
134
135 /**
136  * initialize mutex.
137  * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
138  */
139 CAResult_t CAInitBlockWiseMutexVariables();
140
141 /**
142  * terminate mutex.
143  */
144 void CATerminateBlockWiseMutexVariables();
145
146 /**
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).
151  */
152 CAResult_t CASendBlockWiseData(const CAData_t *data);
153
154 /**
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).
159  */
160 CAResult_t CAAddSendThreadQueue(const CAData_t *sendData, const CABlockDataID_t *blockID);
161
162 /**
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).
167  */
168 CAResult_t CACheckBlockOptionType(CABlockData_t *currData);
169
170 /**
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).
177  */
178 CAResult_t CAReceiveBlockWiseData(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
179                                   const CAData_t *receivedData, size_t dataLen);
180
181 /**
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).
188  */
189 CAResult_t CAProcessNextStep(const coap_pdu_t *pdu, const CAData_t *receivedData,
190                              uint8_t blockWiseStatus, const CABlockDataID_t *blockID);
191
192 /**
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).
198  */
199 CAResult_t CASendBlockMessage(const coap_pdu_t *pdu, CAMessageType_t msgType,
200                               const CABlockDataID_t *blockID);
201
202 /**
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).
209  */
210 CAResult_t CASendErrorMessage(const coap_pdu_t *pdu, uint8_t status,
211                               CAResponseResult_t responseResult,
212                               const CABlockDataID_t *blockID);
213
214 /**
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).
219  */
220 CAResult_t CAReceiveLastBlock(const CABlockDataID_t *blockID,
221                               const CAData_t *receivedData);
222
223 /**
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).
231  */
232 CAResult_t CASetNextBlockOption1(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
233                                  const CAData_t *receivedData, coap_block_t block,
234                                  size_t dataLen);
235
236 /**
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).
244  */
245 CAResult_t CASetNextBlockOption2(coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
246                                  const CAData_t *receivedData, coap_block_t block,
247                                  size_t dataLen);
248
249 /**
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).
256  */
257 CAResult_t CANegotiateBlockSize(CABlockData_t *currData, coap_block_t *block,
258                                 const coap_pdu_t *pdu, uint16_t blockType);
259
260 /**
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).
266  */
267 CAResult_t CAUpdateBlockData(CABlockData_t *currData, coap_block_t block,
268                              uint16_t blockType);
269
270 /**
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).
275  */
276 CAResult_t CAUpdateMessageId(coap_pdu_t *pdu, const CABlockDataID_t *blockID);
277
278 /**
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).
286  */
287 CAResult_t CAUpdateBlockOptionItems(CABlockData_t *currData, const coap_pdu_t *pdu,
288                                     coap_block_t *block, uint16_t blockType,
289                                     uint32_t status);
290 /**
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).
295  */
296 CAResult_t CASetMoreBitFromBlock(size_t payloadLen, coap_block_t *block);
297
298 /**
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).
304  */
305 CAResult_t CAAddBlockOption(coap_pdu_t **pdu, const CAInfo_t *info,
306                             const CAEndpoint_t *endpoint, coap_list_t **options);
307
308 /**
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).
315  */
316 CAResult_t CAAddBlockOption2(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
317                              const CABlockDataID_t *blockID, coap_list_t **options);
318
319 /**
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).
326  */
327 CAResult_t CAAddBlockOption1(coap_pdu_t **pdu, const CAInfo_t *info, size_t dataLength,
328                              const CABlockDataID_t *blockID, coap_list_t **options);
329
330 /**
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).
336  */
337 CAResult_t CAAddBlockOptionImpl(coap_block_t *block, uint8_t blockType,
338                                 coap_list_t **options);
339
340 /**
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).
345  */
346 CAResult_t CAAddOptionToPDU(coap_pdu_t *pdu, coap_list_t **options);
347
348 /**
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).
354  */
355 CAResult_t CAAddBlockSizeOption(coap_pdu_t *pdu, uint16_t sizeType, size_t dataLength,
356                                 coap_list_t **options);
357
358 /**
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.
364  */
365 bool CAIsPayloadLengthInPduWithBlockSizeOption(coap_pdu_t *pdu,
366                                                uint16_t sizeType,
367                                                size_t *totalPayloadLen);
368
369 /**
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).
377  */
378 CAResult_t CAUpdatePayloadData(CABlockData_t *currData, const CAData_t *receivedData,
379                                uint8_t status, bool isSizeOption, uint16_t blockType);
380
381 /**
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.
386  */
387 CAData_t* CACreateNewDataSet(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint);
388
389 /**
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).
395  */
396 CAResult_t CAHandleBlockErrorResponse(coap_block_t *block, uint16_t blockType,
397                                       uint32_t responseResult);
398
399 /**
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.
407  */
408 uint8_t CACheckBlockErrorType(CABlockData_t *currData, coap_block_t *receivedBlock,
409                               const CAData_t *receivedData, uint16_t blockType,
410                               size_t dataLen);
411
412 /**
413  * Destroys the given CAData.
414  * @param[in]   data    CAData to destroy.
415  * @return generated CAData.
416  */
417 void CADestroyDataSet(CAData_t* data);
418
419 /**
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.
425  */
426 CABlockDataID_t* CACreateBlockDatablockId(const CAToken_t token, uint8_t tokenLength,
427                                           uint16_t portNumber);
428
429 /**
430  * Destroy the blockId set.
431  * @param[in]   blockID     ID set of CABlockData.
432  */
433 void CADestroyBlockID(CABlockDataID_t *blockID);
434
435 /**
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.
440  */
441 bool CABlockidMatches(const CABlockData_t *currData, const CABlockDataID_t *blockID);
442 /**
443  * Print the given block option information.
444  * @param[in]   block   block option information.
445  */
446 void CALogBlockInfo(coap_block_t *block);
447
448 /**
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.
452  */
453 CAData_t *CACloneCAData(const CAData_t *data);
454
455 /**
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).
461  */
462 CAResult_t CAUpdatePayloadToCAData(CAData_t *data, const CAPayload_t payload,
463                                    size_t payloadLen);
464
465 /**
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.
469  * @return payload.
470  */
471 CAPayload_t CAGetPayloadInfo(const CAData_t *data, size_t *payloadLen);
472
473 /**
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).
478  */
479 CAResult_t CAUpdateBlockOptionType(const CABlockDataID_t *blockID,
480                                    uint8_t blockType);
481
482 /**
483  * Get the block option type from block-wise transfer list.
484  * @param[in]   blockID     ID set of CABlockData.
485  * @return block option type.
486  */
487 uint8_t CAGetBlockOptionType(const CABlockDataID_t *blockID);
488
489 /**
490  * Get the block data from block-wise transfer list.
491  * @param[in]   blockID     ID set of CABlockData.
492  * @return CAData structure.
493  */
494 CAData_t *CAGetDataSetFromBlockDataList(const CABlockDataID_t *blockID);
495
496 /**
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.
501  */
502 CABlockData_t *CAUpdateDataSetFromBlockDataList(const CABlockDataID_t *blockID,
503                                                 const CAData_t *sendData);
504
505 /**
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).
511  */
512 CAResult_t CAGetTokenFromBlockDataList(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
513                                        CAResponseInfo_t *responseInfo);
514
515 /**
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).
520  */
521 CAResult_t CACheckBlockDataValidation(const CAData_t *sendData, CABlockData_t **blockData);
522
523 /**
524  * Get the block data from block-wise transfer list.
525  * @param[in]   blockID     ID set of CABlockData.
526  * @return CABlockData_t structure.
527  */
528 CABlockData_t *CAGetBlockDataFromBlockDataList(const CABlockDataID_t *blockID);
529
530 /**
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.
535  */
536 coap_block_t *CAGetBlockOption(const CABlockDataID_t *blockID,
537                                uint16_t blockType);
538
539 /**
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.
543  * @return payload.
544  */
545 CAPayload_t CAGetPayloadFromBlockDataList(const CABlockDataID_t *blockID,
546                                           size_t *fullPayloadLen);
547
548 /**
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..
553  */
554 CABlockData_t *CACreateNewBlockData(const CAData_t *sendData);
555
556 /**
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).
560  */
561 CAResult_t CARemoveBlockDataFromList(const CABlockDataID_t *blockID);
562
563 /**
564  * Remove all block data in block-wise transfer list.
565  * @return ::CASTATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h).
566  */
567 CAResult_t CARemoveAllBlockDataFromList();
568
569 /**
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).
575  */
576 CAResult_t CARemoveBlockDataFromListWithSeed(const CAToken_t token, uint8_t tokenLength,
577                                              uint16_t portNumber);
578
579 /**
580  * Reset TTL for a blockData.
581  *
582  * @param[in]   blockID     ID set of CABlockData.
583  */
584 void CAResetBlockDataTTL(const CABlockDataID_t *blockID);
585
586 /**
587  * Checks if the blockData is past its time to live and deletes it if timed-out.
588  */
589 void CACheckAndDeleteTimedOutBlockData();
590 #ifdef __cplusplus
591 } /* extern "C" */
592 #endif
593
594 #endif  // CA_BLOCKWISETRANSFER_H_