Address RI Code review comments
[platform/upstream/iotivity.git] / resource / csdk / stack / include / internal / ocstackinternal.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 //-----------------------------------------------------------------------------
23 // Internal include file used by lower layers of the OC stack
24 //-----------------------------------------------------------------------------
25 #ifndef OCSTACKINTERNAL_H_
26 #define OCSTACKINTERNAL_H_
27
28 //-----------------------------------------------------------------------------
29 // Includes
30 //-----------------------------------------------------------------------------
31 #include <stdbool.h>
32 #include "ocstack.h"
33 #include "ocstackconfig.h"
34 #include "occlientcb.h"
35 #include <logger.h>
36 #include <ocrandom.h>
37
38 #include "cacommon.h"
39 #include "cainterface.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif // __cplusplus
44
45
46 //-----------------------------------------------------------------------------
47 // Global variables
48 //-----------------------------------------------------------------------------
49 extern OCDeviceEntityHandler defaultDeviceHandler;
50
51 //-----------------------------------------------------------------------------
52 // Defines
53 //-----------------------------------------------------------------------------
54 #define OC_COAP_SCHEME "coap://"
55 #define OC_OFFSET_SEQUENCE_NUMBER (4) // the first outgoing sequence number will be 5
56
57 typedef struct
58 {
59     // Observe option field
60     uint32_t option;
61     // IP address & port of client registered for observe
62     OCDevAddr *subAddr;
63
64     CAToken_t *token;
65
66     // The result of the observe request
67     OCStackResult result;
68 } OCObserveReq;
69
70 // following structure will be created in occoap and passed up the stack on the server side
71 typedef struct
72 {
73     // Observe option field
74     uint32_t observationOption;
75     // the REST method retrieved from received request PDU
76     OCMethod method;
77     // resourceUrl will be filled in occoap using the path options in received request PDU
78     char resourceUrl[MAX_URI_LENGTH];
79     // resource query send by client
80     char query[MAX_QUERY_LENGTH];
81     // reqJSON is retrieved from the payload of the received request PDU
82     char reqJSONPayload[MAX_REQUEST_LENGTH];
83     // qos is indicating if the request is CON or NON
84     OCQualityOfService qos;
85     // An array of the received vendor specific header options
86     uint8_t numRcvdVendorSpecificHeaderOptions;
87     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
88
89     /** Remote Endpoint address **/
90     //////////////////////////////////////////////////////////
91     // TODO: bundle this up as endpoint
92     CAAddress_t addressInfo;
93     /** Connectivity of the endpoint**/
94     CAConnectivityType_t connectivityType;
95
96     //token for the observe request
97     CAToken_t requestToken;
98
99     // The ID of CoAP pdu
100     uint16_t coapID;
101     uint8_t delayedResNeeded;
102     uint8_t secured;
103     //////////////////////////////////////////////////////////
104     uint8_t reqMorePacket;
105     uint32_t reqPacketNum;
106     uint16_t reqPacketSize;
107     uint32_t resPacketNum;
108     uint16_t resPacketSize;
109     size_t reqTotalSize;
110 } OCServerProtocolRequest;
111
112 typedef struct
113 {
114     // Observe option field
115     uint32_t observationOption;
116     // qos is indicating if the request is CON or NON
117     OCQualityOfService qos;
118     // Allow the entity handler to pass a result with the response
119     OCStackResult result;
120     // IP address & port of client registered for observe
121     OCDevAddr *requesterAddr;
122
123     CAToken_t *requestToken;
124
125     // The ID of CoAP pdu
126     uint16_t coapID;
127     // Flag indicating that response is to be delayed before sending
128     uint8_t delayedResNeeded;
129     uint8_t secured;
130     uint8_t slowFlag;
131     uint8_t notificationFlag;
132     // this is the pointer to server payload data to be transferred
133     char *payload;
134     // size of server payload data.  Don't rely on null terminated data for size
135     uint16_t payloadSize;
136     // An array of the vendor specific header options the entity handler wishes to use in response
137     uint8_t numSendVendorSpecificHeaderOptions;
138     OCHeaderOption *sendVendorSpecificHeaderOptions;
139     // URI of new resource that entity handler might create
140     char * resourceUri;
141 } OCServerProtocolResponse;
142
143 // following structure will be created in occoap and passed up the stack on the client side
144 typedef struct
145 {
146     // handle is retrieved by comparing the token-handle pair in the PDU.
147     ClientCB * cbNode;
148     // This is how long this response is valid for (in seconds).
149     uint32_t maxAge;
150     // This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led")
151     char * fullUri;
152     // This is the relative Uri of the resource. (ex. "/a/led")
153     char * rcvdUri;
154     // This is the received payload.
155     char * bufRes;
156
157     // This is the token received OTA.
158     CAToken_t * rcvdToken;
159
160     // this structure will be passed to client
161     OCClientResponse * clientResponse;
162 } OCResponse;
163
164 // following typedef is to represent our Server Instance identification.
165 typedef uint32_t ServerID;
166
167 //-----------------------------------------------------------------------------
168 // Internal function prototypes
169 //-----------------------------------------------------------------------------
170
171 OCStackResult OCStackFeedBack(CAToken_t * token, uint8_t status);
172
173 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
174 OCStackResult HandleStackResponses(OCResponse * response);
175 #ifdef WITH_PRESENCE
176 OCStackResult SendPresenceNotification(OCResourceType *resourceType);
177 OCStackResult SendStopNotification();
178 #endif // WITH_PRESENCE
179 int ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
180
181 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
182                                             const char *resourceInterfaceName);
183
184 OCStackResult BindResourceTypeToResource(OCResource* resource,
185                                             const char *resourceTypeName);
186 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName);
187
188 // returns the internal representation of the server instance ID.
189 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
190 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
191 // so ensure that this call is done after that.
192 const ServerID OCGetServerInstanceID(void);
193
194 // Converts a CAResult_t type to a OCStackResult type.
195 OCStackResult CAResultToOCResult(CAResult_t caResult);
196
197 // returns a string representation  the server instance ID.
198 // The memory is managed internal to this function, so freeing externally will result
199 // in a compiler error
200 // Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
201 // This is done automatically during the OCInit process (via the call to OCInitCoAP),
202 // so ensure that this call is done after that.
203 const char* OCGetServerInstanceIDString(void);
204
205 /**
206  * Map OCQualityOfService to CAMessageType
207  *
208  * @param OCQualityOfService - Input qos.
209  *
210  * Returns CA message type for a given qos.
211  */
212 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
213
214 #ifdef WITH_PRESENCE
215 //TODO: should the following function be public?
216 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
217         OCResourceProperty resourceProperties, uint8_t enable);
218 #endif
219
220 #ifdef __cplusplus
221 }
222 #endif // __cplusplus
223
224 #endif /* OCSTACKINTERNAL_H_ */
225