iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / occoap / include / occoaphelper.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 #ifndef OCCOAPHELPER_H_
23 #define OCCOAPHELPER_H_
24
25 //-----------------------------------------------------------------------------
26 // Includes
27 //-----------------------------------------------------------------------------
28 #ifndef WITH_ARDUINO
29 #include <unistd.h>
30 #endif
31 #include <limits.h>
32 #include <ctype.h>
33 #include "coap.h"
34 #include "ocstack.h"
35 #include "occoaptoken.h"
36 #include "ocstackinternal.h"
37
38 /**
39  * The Max Size of the buffer that is used to parse uri and query
40  * individually.
41  */
42 #define MAX_URI_QUERY_BUF_SIZE ((MAX_URI_LENGTH) >= (MAX_QUERY_LENGTH) ?\
43                             (MAX_URI_LENGTH) : (MAX_QUERY_LENGTH))
44
45 #define COAP_WK_IPAddr_0 (224)
46 #define COAP_WK_IPAddr_1 (0)
47 #define COAP_WK_IPAddr_2 (1)
48 #define COAP_WK_IPAddr_3 (187)
49
50 // checks if optionID is within vendor specific range
51 OCStackResult isVendorSpecific(uint16_t optionID);
52
53 // Convert OCStack code to CoAP code
54 uint8_t OCToCoAPResponseCode(OCStackResult result);
55
56 //Convert OCQualityOfService to coap NON vs CON
57 uint8_t OCToCoAPQoS(OCQualityOfService qos, uint8_t * ipAddr);
58
59 // Convert CoAP code to OCStack code
60 OCStackResult CoAPToOCResponseCode(uint8_t coapCode);
61
62 // Internal function to generate a coap pdu based on passed parameters
63 coap_pdu_t *
64 GenerateCoAPPdu(uint8_t msgType, uint8_t code, unsigned short id,
65         OCCoAPToken * token, unsigned char * payloadJSON,
66         coap_list_t *options);
67
68 // Internal function to send a coap pdu, it also handles NON and CON
69 OCStackResult
70 SendCoAPPdu(coap_context_t * gCoAPCtx, coap_address_t* dst, coap_pdu_t * pdu,
71         coap_send_flags_t flag);
72
73 // Call back function used by libcoap to order option in coap pdu
74 int OrderOptions(void *a, void *b);
75
76 // Internal function to create an option node for coap pdu
77 coap_list_t *
78 CreateNewOptionNode(unsigned short key, unsigned int length,
79         unsigned char *data);
80
81 // Internal function to retrieve Uri and Query from received coap pdu
82 OCStackResult ParseCoAPPdu(coap_pdu_t * pdu, unsigned char * uriBuf,
83         unsigned char * queryBuf, uint32_t * observeOption,
84         uint32_t * maxAgeOption,
85         uint8_t * numVendorSpecificHeaderOptions,
86         OCHeaderOption * vendorSpecificHeaderOptions,
87         coap_block_t * block1, coap_block_t * block2,
88         uint16_t * size1, uint16_t * size2,
89         unsigned char * payload);
90
91 // Internal function to retrieve a Token from received coap pdu
92 void RetrieveOCCoAPToken(const coap_pdu_t * pdu, OCCoAPToken * rcvdToken);
93
94 // Internal function to create OCResponse struct at the client from a received coap pdu
95 OCStackResult FormOCResponse(OCResponse * * responseLoc, ClientCB * cbNode,
96         uint8_t TTL, OCClientResponse * clientResponse);
97
98 // Internal function to create OCClientResponse struct at the client from a received coap pdu
99 OCStackResult FormOCClientResponse(OCClientResponse * clientResponse,
100         OCStackResult result, OCDevAddr * remote, uint32_t seqNum,
101         const unsigned char * resJSONPayload);
102
103 // Internal function to handle the queued pdus in the send queue
104 void HandleSendQueue(coap_context_t * gCoAPCtx);
105
106 // Internal function to form the standard response option list
107 OCStackResult FormOptionList(coap_list_t * * optListLoc, uint8_t * addMediaType,
108         uint32_t * addMaxAge, uint32_t * observeOptionPtr,
109         uint16_t * addPortNumber, uint8_t uriLength, unsigned char * uri,
110         uint8_t queryLength, unsigned char * query, OCHeaderOption * vendorSpecificHeaderOptions,
111         uint8_t numVendorSpecificHeaderOptions);
112
113 // Internal function to retransmit a queue
114 OCStackResult ReTXCoAPQueue(coap_context_t * ctx, coap_queue_t * queue);
115
116 // Internal function called when sending/retransmission fails
117 OCStackResult HandleFailedCommunication(coap_context_t * ctx, coap_queue_t * queue);
118 #endif /* OCCOAPHELPER_H_ */