Updated libcoap to send messages using secure port
[contrib/iotivity.git] / 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 #define BUF_SIZE (64)
39
40 // checks if optionID is within vendor specific range
41 OCStackResult isVendorSpecific(uint16_t optionID);
42
43 // Convert OCStack code to CoAP code
44 uint8_t OCToCoAPResponseCode(OCStackResult result);
45
46 //Convert OCQualityOfService to coap NON vs CON
47 uint8_t OCToCoAPQoS(OCQualityOfService qos);
48
49 // Convert CoAP code to OCStack code
50 OCStackResult CoAPToOCResponseCode(uint8_t coapCode);
51
52 // Internal function to generate a coap pdu based on passed parameters
53 coap_pdu_t *
54 GenerateCoAPPdu(uint8_t msgType, uint8_t code, unsigned short id,
55         OCCoAPToken * token, unsigned char * payloadJSON,
56         coap_list_t *options);
57
58 // Internal function to send a coap pdu, it also handles NON and CON
59 OCStackResult
60 SendCoAPPdu(coap_context_t * gCoAPCtx, coap_address_t* dst, coap_pdu_t * pdu,
61         coap_send_flags_t flag);
62
63 // Call back function used by libcoap to order option in coap pdu
64 int OrderOptions(void *a, void *b);
65
66 // Internal function to create an option node for coap pdu
67 coap_list_t *
68 CreateNewOptionNode(unsigned short key, unsigned int length,
69         unsigned char *data);
70
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);
75
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);
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, uint8_t * * observeOptionLoc,
84         uint8_t * * maxAgeOptionLoc, unsigned char * * payloadLoc,
85         OCHeaderOption * rcvVendorSpecificHeaderOptions,
86         uint8_t * numRcvVendorSpecificHeaderOptions);
87
88 // Internal function to retrieve a Token from received coap pdu
89 void RetrieveOCCoAPToken(const coap_pdu_t * pdu, OCCoAPToken * rcvdToken);
90
91 // Internal function to create OCObserveReq at the server
92 OCStackResult FormOCObserveReq(OCObserveReq ** observeReqLoc, uint32_t obsOption,
93             OCDevAddr * remote, OCCoAPToken * rcvdToken);
94
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);
98
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);
103
104 // Internal function to handle the queued pdus in the send queue
105 void HandleSendQueue(coap_context_t * gCoAPCtx);
106
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);
113
114 // Internal function to retransmit a queue
115 OCStackResult ReTXCoAPQueue(coap_context_t * ctx, coap_queue_t * queue);
116
117 // Internal function called when sending/retransmission fails
118 OCStackResult HandleFailedCommunication(coap_context_t * ctx, coap_queue_t * queue);
119 #endif /* OCCOAPHELPER_H_ */