1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #ifndef OCCOAPHELPER_H_
23 #define OCCOAPHELPER_H_
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
35 #include "occoaptoken.h"
36 #include "ocstackinternal.h"
39 * The Max Size of the buffer that is used to parse uri and query
42 #define MAX_URI_QUERY_BUF_SIZE ((MAX_URI_LENGTH) >= (MAX_QUERY_LENGTH) ?\
43 (MAX_URI_LENGTH) : (MAX_QUERY_LENGTH))
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)
50 // checks if optionID is within vendor specific range
51 OCStackResult isVendorSpecific(uint16_t optionID);
53 // Convert OCStack code to CoAP code
54 uint8_t OCToCoAPResponseCode(OCStackResult result);
56 //Convert OCQualityOfService to coap NON vs CON
57 uint8_t OCToCoAPQoS(OCQualityOfService qos, uint8_t * ipAddr);
59 // Convert CoAP code to OCStack code
60 OCStackResult CoAPToOCResponseCode(uint8_t coapCode);
62 // Internal function to generate a coap pdu based on passed parameters
64 GenerateCoAPPdu(uint8_t msgType, uint8_t code, unsigned short id,
65 OCCoAPToken * token, unsigned char * payloadJSON,
66 coap_list_t *options);
68 // Internal function to send a coap pdu, it also handles NON and CON
70 SendCoAPPdu(coap_context_t * gCoAPCtx, coap_address_t* dst, coap_pdu_t * pdu,
71 coap_send_flags_t flag);
73 // Call back function used by libcoap to order option in coap pdu
74 int OrderOptions(void *a, void *b);
76 // Internal function to create an option node for coap pdu
78 CreateNewOptionNode(unsigned short key, unsigned int length,
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);
91 // Internal function to retrieve a Token from received coap pdu
92 void RetrieveOCCoAPToken(const coap_pdu_t * pdu, OCCoAPToken * rcvdToken);
94 // Internal function to create OCResponse struct at the client from a received coap pdu
95 OCStackResult FormOCResponse(OCResponse * * responseLoc, ClientCB * cbNode, uint32_t maxAge,
96 unsigned char * fullUri, unsigned char * rcvdUri, OCCoAPToken * rcvdToken,
97 OCClientResponse * clientResponse, unsigned char * bufRes);
99 // Internal function to create OCClientResponse struct at the client from a received coap pdu
100 OCStackResult FormOCClientResponse(OCClientResponse * clientResponse,
101 OCStackResult result, OCDevAddr * remote, uint32_t seqNum,
102 const unsigned char * resJSONPayload);
104 // Internal function to handle the queued pdus in the send queue
105 void HandleSendQueue(coap_context_t * gCoAPCtx);
107 // Internal function to form the standard response option list
108 OCStackResult FormOptionList(coap_list_t * * optListLoc, uint8_t * addMediaType,
109 uint32_t * addMaxAge, uint32_t * observeOptionPtr,
110 uint16_t * addPortNumber, uint8_t uriLength, unsigned char * uri,
111 uint8_t queryLength, unsigned char * query, OCHeaderOption * vendorSpecificHeaderOptions,
112 uint8_t numVendorSpecificHeaderOptions);
114 // Internal function to retransmit a queue
115 OCStackResult ReTXCoAPQueue(coap_context_t * ctx, coap_queue_t * queue);
117 // Internal function called when sending/retransmission fails
118 OCStackResult HandleFailedCommunication(coap_context_t * ctx, coap_queue_t * queue);
119 #endif /* OCCOAPHELPER_H_ */