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"
40 // checks if optionID is within vendor specific range
41 OCStackResult isVendorSpecific(uint16_t optionID);
43 // Convert OCStack code to CoAP code
44 uint8_t OCToCoAPResponseCode(OCStackResult result);
46 //Convert OCQualityOfService to coap NON vs CON
47 uint8_t OCToCoAPQoS(OCQualityOfService qos);
49 // Convert CoAP code to OCStack code
50 OCStackResult CoAPToOCResponseCode(uint8_t coapCode);
52 // Internal function to generate a coap pdu based on passed parameters
54 GenerateCoAPPdu(uint8_t msgType, uint8_t code, unsigned short id,
55 OCCoAPToken * token, unsigned char * payloadJSON,
56 coap_list_t *options);
58 // Internal function to send a coap pdu, it also handles NON and CON
60 SendCoAPPdu(coap_context_t * gCoAPCtx, coap_address_t* dst, coap_pdu_t * pdu,
61 coap_send_flags_t flag);
63 // Call back function used by libcoap to order option in coap pdu
64 int OrderOptions(void *a, void *b);
66 // Internal function to create an option node for coap pdu
68 CreateNewOptionNode(unsigned short key, unsigned int length,
71 // Internal function to create OCRequest struct at the server from a received coap pdu
72 OCStackResult FormOCRequest(OCRequest * * requestLoc, OCQualityOfService qos,
73 unsigned char * uriBuf, OCObserveReq * observeReq,
74 OCEntityHandlerRequest * entityHandlerRequest);
76 // Internal function to create OCEntityHandlerRequest at the server from a received coap pdu
77 OCStackResult FormOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequestLoc,
78 OCMethod method, unsigned char * resBuf, unsigned char * bufReqPayload,
79 unsigned char * queryBuf, unsigned char *newResUriBuf);
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, uint8_t * * observeOptionLoc,
84 uint8_t * * maxAgeOptionLoc, unsigned char * * payloadLoc,
85 OCHeaderOption * rcvVendorSpecificHeaderOptions,
86 uint8_t * numRcvVendorSpecificHeaderOptions);
88 // Internal function to retrieve a Token from received coap pdu
89 void RetrieveOCCoAPToken(const coap_pdu_t * pdu, OCCoAPToken * rcvdToken);
91 // Internal function to create OCObserveReq at the server
92 OCStackResult FormOCObserveReq(OCObserveReq ** observeReqLoc, uint32_t obsOption,
93 OCDevAddr * remote, OCCoAPToken * rcvdToken);
95 // Internal function to create OCResponse struct at the client from a received coap pdu
96 OCStackResult FormOCResponse(OCResponse * * responseLoc, ClientCB * cbNode,
97 uint8_t TTL, OCClientResponse * clientResponse);
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, uint8_t observeOptionLength, 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_ */