Mods per RI code review.
[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 /**
71  * This structure will be created in occoap and passed up the stack on the server side.
72  */
73 typedef struct
74 {
75     // Observe option field
76     uint32_t observationOption;
77     // the REST method retrieved from received request PDU
78     OCMethod method;
79     // resourceUrl will be filled in occoap using the path options in received request PDU
80     char resourceUrl[MAX_URI_LENGTH];
81     // resource query send by client
82     char query[MAX_QUERY_LENGTH];
83     // reqJSON is retrieved from the payload of the received request PDU
84     char reqJSONPayload[MAX_REQUEST_LENGTH];
85     // qos is indicating if the request is CON or NON
86     OCQualityOfService qos;
87     // An array of the received vendor specific header options
88     uint8_t numRcvdVendorSpecificHeaderOptions;
89     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
90
91     /** Remote Endpoint address **/
92     //////////////////////////////////////////////////////////
93     // TODO: bundle this up as endpoint
94     CAAddress_t addressInfo;
95     /** Connectivity of the endpoint**/
96     CAConnectivityType_t connectivityType;
97
98     //token for the observe request
99     CAToken_t requestToken;
100     uint8_t tokenLength; //token length
101     // The ID of CoAP pdu
102     uint16_t coapID;
103     uint8_t delayedResNeeded;
104     uint8_t secured;
105     //////////////////////////////////////////////////////////
106     uint8_t reqMorePacket;
107     uint32_t reqPacketNum;
108     uint16_t reqPacketSize;
109     uint32_t resPacketNum;
110     uint16_t resPacketSize;
111     size_t reqTotalSize;
112 } OCServerProtocolRequest;
113
114 typedef struct
115 {
116     // Observe option field
117     uint32_t observationOption;
118     // qos is indicating if the request is CON or NON
119     OCQualityOfService qos;
120     // Allow the entity handler to pass a result with the response
121     OCStackResult result;
122     // IP address & port of client registered for observe
123     OCDevAddr *requesterAddr;
124
125     CAToken_t *requestToken;
126
127     // The ID of CoAP pdu
128     uint16_t coapID;
129     // Flag indicating that response is to be delayed before sending
130     uint8_t delayedResNeeded;
131     uint8_t secured;
132     uint8_t slowFlag;
133     uint8_t notificationFlag;
134     // this is the pointer to server payload data to be transferred
135     char *payload;
136     // size of server payload data.  Don't rely on null terminated data for size
137     uint16_t payloadSize;
138     // An array of the vendor specific header options the entity handler wishes to use in response
139     uint8_t numSendVendorSpecificHeaderOptions;
140     OCHeaderOption *sendVendorSpecificHeaderOptions;
141     // URI of new resource that entity handler might create
142     char * resourceUri;
143 } OCServerProtocolResponse;
144
145 /**
146  * This structure will be created in occoap and passed up the stack on the client side.
147  */
148 typedef struct
149 {
150     // handle is retrieved by comparing the token-handle pair in the PDU.
151     ClientCB * cbNode;
152     // This is how long this response is valid for (in seconds).
153     uint32_t maxAge;
154     // This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led")
155     char * fullUri;
156     // This is the relative Uri of the resource. (ex. "/a/led")
157     char * rcvdUri;
158     // This is the received payload.
159     char * bufRes;
160
161     // This is the token received OTA.
162     CAToken_t * rcvdToken;
163
164     // this structure will be passed to client
165     OCClientResponse * clientResponse;
166 } OCResponse;
167
168 /**
169  * This typedef is to represent our Server Instance identification.
170  */
171 typedef uint32_t ServerID;
172
173 //-----------------------------------------------------------------------------
174 // Internal function prototypes
175 //-----------------------------------------------------------------------------
176
177 #ifdef WITH_PRESENCE
178 /**
179  * Notify Presence subscribers that a resource has been modified.
180  *
181  * @param resourceType Handle to the resourceType linked list of resource
182  *                     that was modified.
183  * @return ::OC_STACK_OK on success, some other value upon failure.
184  */
185 OCStackResult SendPresenceNotification(OCResourceType *resourceType);
186
187 /**
188  * Send Stop Notification to Presence subscribers.
189  *
190  * @return ::OC_STACK_OK on success, some other value upon failure.
191  */
192 OCStackResult SendStopNotification();
193 #endif // WITH_PRESENCE
194
195 /**
196  * Bind a resource interface to a resource.
197  *
198  * @param resource Target resource.
199  * @param resourceInterfaceName Resource interface.
200  * @return ::OC_STACK_OK on success, some other value upon failure.
201  */
202 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
203                                             const char *resourceInterfaceName);
204 /**
205  * Bind a resourcetype to a resource.
206  *
207  * @param resource Target resource.
208  * @param resourceTypeName Name of resource type.
209  * @return ::OC_STACK_OK on success, some other value upon failure.
210  */
211 OCStackResult BindResourceTypeToResource(OCResource* resource,
212                                             const char *resourceTypeName);
213
214 // Converts a CAResult_t type to a OCStackResult type.
215 /**
216  * Converts a CAResult_t type to a OCStackResult type.
217  *
218  * @param caResult CAResult_t value to convert
219  * @return OCStackResult that was converted from the input CAResult_t value.
220  */
221 OCStackResult CAResultToOCResult(CAResult_t caResult);
222
223 /**
224  * Get a string representation the server instance ID.
225  * The memory is managed internal to this function, so freeing externally will result
226  * in a compiler error
227  * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
228  * This is done automatically during the OCInit process (via the call to OCInitCoAP),
229  * so ensure that this call is done after that.
230  *
231  * @return A string representation  the server instance ID.
232  */
233 const char* OCGetServerInstanceIDString(void);
234
235 /**
236  * Map OCQualityOfService to CAMessageType.
237  *
238  * @param qos Input qos.
239  * @return CA message type for a given qos.
240  */
241 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
242
243 #ifdef WITH_PRESENCE
244 /**
245  * Enable/disable a resource property.
246  *
247  * @param inputProperty Pointer to resource property.
248  * @param resourceProperties Property to be enabled/disabled.
249  * @param enable 0:disable, 1:enable.
250  *
251  * @return OCStackResult that was converted from the input CAResult_t value.
252  */
253 //TODO: should the following function be public?
254 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
255         OCResourceProperty resourceProperties, uint8_t enable);
256 #endif
257
258 #ifdef __cplusplus
259 }
260 #endif // __cplusplus
261
262 #endif /* OCSTACKINTERNAL_H_ */
263