List child resource eps instead of parent.
[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
24 /**
25  * @file
26  *
27  * This file contains the Internal include file used by lower layers of the OC stack
28  *
29  */
30
31 #ifndef OCSTACKINTERNAL_H_
32 #define OCSTACKINTERNAL_H_
33
34 //-----------------------------------------------------------------------------
35 // Includes
36 //-----------------------------------------------------------------------------
37 #include <stdbool.h>
38 #include "ocstack.h"
39 #include "ocstackconfig.h"
40 #include "occlientcb.h"
41 #include <ocrandom.h>
42
43 #include "cacommon.h"
44 #include "cainterface.h"
45 #include "experimental/securevirtualresourcetypes.h"
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif // __cplusplus
50
51
52 //-----------------------------------------------------------------------------
53 // Global variables
54 //-----------------------------------------------------------------------------
55
56 /** Default device entity Handler.*/
57 extern OCDeviceEntityHandler defaultDeviceHandler;
58
59 /** Default Callback parameter.*/
60 extern void* defaultDeviceHandlerCallbackParameter;
61
62 //-----------------------------------------------------------------------------
63 // Defines
64 //-----------------------------------------------------------------------------
65
66 /** The coap scheme */
67 #define OC_COAP_SCHEME "coap://"
68
69 /** the first outgoing sequence number will be 1*/
70 #define OC_OFFSET_SEQUENCE_NUMBER (0)
71
72 /**
73  * This structure will be created in occoap and passed up the stack on the server side.
74  */
75 typedef struct
76 {
77     /** Observe option field.*/
78     uint32_t observationOption;
79
80     /** The REST method retrieved from received request PDU.*/
81     OCMethod method;
82
83     /** the provided payload format. */
84     OCPayloadFormat payloadFormat;
85
86     /** the requested payload format. */
87     OCPayloadFormat acceptFormat;
88
89     /** the requested payload version. */
90     uint16_t acceptVersion;
91
92     /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
93     char resourceUrl[MAX_URI_LENGTH];
94
95     /** resource query send by client.*/
96     char query[MAX_QUERY_LENGTH];
97
98     /** reqJSON is retrieved from the payload of the received request PDU.*/
99     uint8_t *payload;
100
101     /** qos is indicating if the request is CON or NON.*/
102     OCQualityOfService qos;
103
104     /** Number of the received vendor specific header options.*/
105     uint8_t numRcvdVendorSpecificHeaderOptions;
106
107     /** Array of received vendor specific header option .*/
108     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
109
110     /** Remote end-point address **/
111     OCDevAddr devAddr;
112
113     /** Token for the observe request.*/
114     CAToken_t requestToken;
115
116     /** token length.*/
117     uint8_t tokenLength;
118
119     /** The ID of CoAP PDU.*/
120     uint16_t coapID;
121
122     /** For delayed Response.*/
123     uint8_t delayedResNeeded;
124
125     /** For More packet.*/
126     uint8_t reqMorePacket;
127
128     /** The number of requested packet.*/
129     uint32_t reqPacketNum;
130
131     /** The size of requested packet.*/
132     uint16_t reqPacketSize;
133
134     /** The number of responded packet.*/
135     uint32_t resPacketNum;
136
137     /** Responded packet size.*/
138     uint16_t resPacketSize;
139
140     /** The total size of requested packet.*/
141     size_t reqTotalSize;
142 } OCServerProtocolRequest;
143
144 /**
145  * This typedef is to represent our Server Instance identification.
146  */
147 typedef uint8_t ServerID[16];
148
149 //-----------------------------------------------------------------------------
150 // Internal function prototypes
151 //-----------------------------------------------------------------------------
152
153
154 /**
155  * Handler function for sending a response from multiple resources, such as a collection.
156  * Aggregates responses from multiple resource until all responses are received then sends the
157  * concatenated response
158  *
159  * TODO: Need to add a timeout in case a (remote?) resource does not respond
160  *
161  * @param token         Token to search for.
162  * @param tokenLength   Length of token.
163  * @param status        Feedback status.
164  * @return
165  *     ::OCStackResult
166  */
167
168 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
169
170
171 /**
172  * Handler function to execute stack requests
173  *
174  * @param protocolRequest      Pointer to the protocol requests from server.
175  *
176  * @return
177  *     ::OCStackResult
178  */
179 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
180
181 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
182         const CAResponseResult_t responseResult, const CAMessageType_t type,
183         const uint8_t numOptions, const CAHeaderOption_t *options,
184         CAToken_t token, uint8_t tokenLength, const char *resourceUri,
185         CADataType_t dataType);
186
187 #ifdef WITH_PRESENCE
188
189 /**
190  * Notify Presence subscribers that a resource has been modified.
191  *
192  * @param resourceType    Handle to the resourceType linked list of resource that was modified.
193  * @param trigger         The simplified reason this API was invoked.
194  *
195  * @return ::OC_STACK_OK on success, some other value upon failure.
196  */
197 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
198         OCPresenceTrigger trigger);
199
200 /**
201  * Send Stop Notification to Presence subscribers.
202  *
203  * @return ::OC_STACK_OK on success, some other value upon failure.
204  */
205 OCStackResult SendStopNotification();
206 #endif // WITH_PRESENCE
207
208 /**
209  * Bind a resource interface to a resource.
210  *
211  * @param resource Target resource.
212  * @param resourceInterfaceName Resource interface.
213  *
214  * @return ::OC_STACK_OK on success, some other value upon failure.
215  */
216 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
217                                             const char *resourceInterfaceName);
218 /**
219  * Bind a resource type to a resource.
220  *
221  * @param resource Target resource.
222  * @param resourceTypeName Name of resource type.
223  * @return ::OC_STACK_OK on success, some other value upon failure.
224  */
225 OCStackResult BindResourceTypeToResource(OCResource *resource,
226                                             const char *resourceTypeName);
227 /**
228  * Bind a Transport Protocol Suites type to a resource.
229  *
230  * @param resource Target resource.
231  * @param resourceTpsTypes Name of transport protocol suites type.
232  * @return ::OC_STACK_OK on success, some other value upon failure.
233  */
234 OCStackResult BindTpsTypeToResource(OCResource *resource,
235                                     OCTpsSchemeFlags resourceTpsTypes);
236
237 /**
238  * Convert OCStackResult to CAResponseResult_t.
239  *
240  * @param ocCode OCStackResult code.
241  * @param method OCMethod method the return code replies to.
242  * @return ::CA_CONTENT on OK, some other value upon failure.
243  */
244 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
245
246 /**
247  * Converts a CAResult_t type to a OCStackResult type.
248  *
249  * @param caResult CAResult_t value to convert.
250  * @return OCStackResult that was converted from the input CAResult_t value.
251  */
252 OCStackResult CAResultToOCResult(CAResult_t caResult);
253
254 /**
255  * Converts a OCStackResult type to a bool type.
256  *
257  * @param ocResult OCStackResult value to convert.
258  * @return true on success, false upon failure.
259  */
260 bool OCResultToSuccess(OCStackResult ocResult);
261
262 /**
263  * Map OCQualityOfService to CAMessageType.
264  *
265  * @param qos Input qos.
266  *
267  * @return CA message type for a given qos.
268  */
269 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
270
271 #ifdef WITH_PRESENCE
272 /**
273  * Enable/disable a resource property.
274  *
275  * @param inputProperty             Pointer to resource property.
276  * @param resourceProperties        Property to be enabled/disabled.
277  * @param enable                    0:disable, 1:enable.
278  *
279  * @return OCStackResult that was converted from the input CAResult_t value.
280  */
281 //TODO: should the following function be public?
282 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
283         OCResourceProperty resourceProperties, uint8_t enable);
284 #endif
285
286 const char *OC_CALL convertTriggerEnumToString(OCPresenceTrigger trigger);
287
288 OCPresenceTrigger OC_CALL convertTriggerStringToEnum(const char * triggerStr);
289
290 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
291
292 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
293
294 /**
295  * Get the CoAP ticks after the specified number of milli-seconds.
296  *
297  * @param milliSeconds Milli-seconds.
298  * @return CoAP ticks
299  */
300 uint32_t GetTicks(uint32_t milliSeconds);
301
302 /**
303  * Extract interface and resource type from the query.
304  *
305  * @param query is the request received from the client
306  * @param filterOne will include result if the interface is included in the query.
307  * @param filterTwo will include result if the resource type is included in the query.
308  *
309  * @return ::OC_STACK_OK on success, some other value upon failure
310  */
311 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo);
312
313 #if defined(RD_CLIENT) || defined(RD_SERVER)
314 /**
315  * This function binds an resource unique ins value to the resource. This can be only called
316  * when the stack has received a response from resource-directory.
317  *
318  * @param requestUri URI of the resource.
319  * @param response Response from queries to remote servers.
320  *
321  * @return ::OC_STACK_OK on success, some other value upon failure.
322  */
323 OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
324                                               const OCClientResponse *response);
325 #endif
326
327 /**
328  * Delete all of the dynamically allocated elements that were created for the resource attributes.
329  *
330  * @param rsrcAttributes Specified resource attribute.
331  */
332 void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes);
333
334 #ifndef TCP_ADAPTER
335 /**
336  * Add resource payload with endpoint payload to discovery payload.
337  *
338  * @param payload       Pointer to discovery payload.
339  * @param res           Pointer to OCresource structure.
340  * @param securePort    Secure port number.
341  * @param networkInfo   List of CAEndpoint_t.
342  * @param infoSize      Size of CAEndpoint_t list.
343  * @param devAddr       Pointer to OCDevAddr structure.
344  */
345 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
346                                           uint16_t securePort, void *networkInfo, size_t infoSize,
347                                           const OCDevAddr *devAddr);
348 #else
349 /**
350  * Add resource payload with endpoint payload to discovery payload.
351  *
352  * @param payload       Pointer to discovery payload.
353  * @param res           Pointer to OCresource structure.
354  * @param securePort    Secure port number.
355  * @param networkInfo   List of CAEndpoint_t.
356  * @param infoSize      Size of CAEndpoint_t list.
357  * @param devAddr       Pointer to OCDevAddr structure.
358  * @param tcpPort       TCP port number.
359  */
360 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
361                                           uint16_t securePort, void *networkInfo, size_t infoSize,
362                                           const OCDevAddr *devAddr, uint16_t tcpPort);
363
364 /* This method will retrieve the tcp port */
365 OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port, bool secured);
366 #endif
367
368 /**
369  * This function creates list of OCEndpointPayload structure,
370  * which matches with the resource's endpointType from list of
371  * CAEndpoint_t.
372  *
373  * @param[in] resource the resource
374  * @param[in] devAddr devAddr Structure pointing to the address.
375  * @param[in] networkInfo array of CAEndpoint_t
376  * @param[in] infoSize size of array
377  * @param[out] listHead pointer to HeadNode pointer
378  * @param[out] epSize size of array(set NULL not to use it)
379  * @param[out] selfEp endpoint that matches devAddr for use in anchor(set NULL not to use it)
380  *
381  * @return if success return pointer else NULL
382  */
383 OCEndpointPayload* CreateEndpointPayloadList(const OCResource *resource,
384     const OCDevAddr *devAddr, CAEndpoint_t *networkInfo,
385     size_t infoSize, OCEndpointPayload **listHead, size_t* epSize, OCEndpointPayload** selfEp);
386
387 /*
388 * This function returns to destroy endpoint payload
389 *
390 */
391 void OC_CALL OCEndpointPayloadDestroy(OCEndpointPayload* payload);
392
393 // Check on Accept Version option.
394 bool OCRequestIsOCFContentFormat(OCEntityHandlerRequest *ehRequest, bool* isOCFContentFormat);
395
396 #ifdef __cplusplus
397 }
398 #endif // __cplusplus
399
400 #endif /* OCSTACKINTERNAL_H_ */