31769b1fca97cbf8fa085cb0f2d62950c8932e60
[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 "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 requested payload format. */
84     OCPayloadFormat acceptFormat;
85
86     /** the requested payload format. */
87     uint16_t acceptVersion;
88
89     /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
90     char resourceUrl[MAX_URI_LENGTH];
91
92     /** resource query send by client.*/
93     char query[MAX_QUERY_LENGTH];
94
95     /** reqJSON is retrieved from the payload of the received request PDU.*/
96     uint8_t *payload;
97
98     /** qos is indicating if the request is CON or NON.*/
99     OCQualityOfService qos;
100
101     /** Number of the received vendor specific header options.*/
102     uint8_t numRcvdVendorSpecificHeaderOptions;
103
104     /** Array of received vendor specific header option .*/
105     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
106
107     /** Remote end-point address **/
108     OCDevAddr devAddr;
109
110     /** Token for the observe request.*/
111     CAToken_t requestToken;
112
113     /** token length.*/
114     uint8_t tokenLength;
115
116     /** The ID of CoAP PDU.*/
117     uint16_t coapID;
118
119     /** For delayed Response.*/
120     uint8_t delayedResNeeded;
121
122     /** For More packet.*/
123     uint8_t reqMorePacket;
124
125     /** The number of requested packet.*/
126     uint32_t reqPacketNum;
127
128     /** The size of requested packet.*/
129     uint16_t reqPacketSize;
130
131     /** The number of responded packet.*/
132     uint32_t resPacketNum;
133
134     /** Responded packet size.*/
135     uint16_t resPacketSize;
136
137     /** The total size of requested packet.*/
138     size_t reqTotalSize;
139 } OCServerProtocolRequest;
140
141 /**
142  * This typedef is to represent our Server Instance identification.
143  */
144 typedef uint8_t ServerID[16];
145
146 //-----------------------------------------------------------------------------
147 // Internal function prototypes
148 //-----------------------------------------------------------------------------
149
150
151 /**
152  * Handler function for sending a response from multiple resources, such as a collection.
153  * Aggregates responses from multiple resource until all responses are received then sends the
154  * concatenated response
155  *
156  * TODO: Need to add a timeout in case a (remote?) resource does not respond
157  *
158  * @param token         Token to search for.
159  * @param tokenLength   Length of token.
160  * @param status        Feedback status.
161  * @return
162  *     ::OCStackResult
163  */
164
165 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
166
167
168 /**
169  * Handler function to execute stack requests
170  *
171  * @param protocolRequest      Pointer to the protocol requests from server.
172  *
173  * @return
174  *     ::OCStackResult
175  */
176 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
177
178 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
179         const CAResponseResult_t responseResult, const CAMessageType_t type,
180         const uint8_t numOptions, const CAHeaderOption_t *options,
181         CAToken_t token, uint8_t tokenLength, const char *resourceUri,
182         CADataType_t dataType);
183
184 #ifdef WITH_PRESENCE
185
186 /**
187  * Notify Presence subscribers that a resource has been modified.
188  *
189  * @param resourceType    Handle to the resourceType linked list of resource that was modified.
190  * @param trigger         The simplified reason this API was invoked.
191  *
192  * @return ::OC_STACK_OK on success, some other value upon failure.
193  */
194 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
195         OCPresenceTrigger trigger);
196
197 /**
198  * Send Stop Notification to Presence subscribers.
199  *
200  * @return ::OC_STACK_OK on success, some other value upon failure.
201  */
202 OCStackResult SendStopNotification();
203 #endif // WITH_PRESENCE
204
205 /**
206  * Function to parse the IPv4 address.
207  *
208  * @param ipAddrStr       Pointer to a string of IPv4 address.
209  * @param ipAddr          pointer to IPv4 adress.
210  * @param port            Port number.
211  *
212  * @return true on success, false upon failure.
213  */
214 bool ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
215
216 /**
217  * Bind a resource interface to a resource.
218  *
219  * @param resource Target resource.
220  * @param resourceInterfaceName Resource interface.
221  *
222  * @return ::OC_STACK_OK on success, some other value upon failure.
223  */
224 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
225                                             const char *resourceInterfaceName);
226 /**
227  * Bind a resource type to a resource.
228  *
229  * @param resource Target resource.
230  * @param resourceTypeName Name of resource type.
231  * @return ::OC_STACK_OK on success, some other value upon failure.
232  */
233 OCStackResult BindResourceTypeToResource(OCResource *resource,
234                                             const char *resourceTypeName);
235 /**
236  * Bind a Transport Protocol Suites type to a resource.
237  *
238  * @param resource Target resource.
239  * @param resourceTpsTypes Name of transport protocol suites type.
240  * @return ::OC_STACK_OK on success, some other value upon failure.
241  */
242 OCStackResult BindTpsTypeToResource(OCResource *resource,
243                                     OCTpsSchemeFlags resourceTpsTypes);
244
245 /**
246  * Convert OCStackResult to CAResponseResult_t.
247  *
248  * @param ocCode OCStackResult code.
249  * @param method OCMethod method the return code replies to.
250  * @return ::CA_CONTENT on OK, some other value upon failure.
251  */
252 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
253
254 /**
255  * Converts a CAResult_t type to a OCStackResult type.
256  *
257  * @param caResult CAResult_t value to convert.
258  * @return OCStackResult that was converted from the input CAResult_t value.
259  */
260 OCStackResult CAResultToOCResult(CAResult_t caResult);
261
262 /**
263  * Converts a OCStackResult type to a bool type.
264  *
265  * @param ocResult OCStackResult value to convert.
266  * @return true on success, false upon failure.
267  */
268 bool OCResultToSuccess(OCStackResult ocResult);
269
270 /**
271  * Map OCQualityOfService to CAMessageType.
272  *
273  * @param qos Input qos.
274  *
275  * @return CA message type for a given qos.
276  */
277 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
278
279 #ifdef WITH_PRESENCE
280 /**
281  * Enable/disable a resource property.
282  *
283  * @param inputProperty             Pointer to resource property.
284  * @param resourceProperties        Property to be enabled/disabled.
285  * @param enable                    0:disable, 1:enable.
286  *
287  * @return OCStackResult that was converted from the input CAResult_t value.
288  */
289 //TODO: should the following function be public?
290 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
291         OCResourceProperty resourceProperties, uint8_t enable);
292 #endif
293
294 const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
295
296 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
297
298 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
299
300 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
301
302 /**
303  * Get the CoAP ticks after the specified number of milli-seconds.
304  *
305  * @param milliSeconds Milli-seconds.
306  * @return CoAP ticks
307  */
308 uint32_t GetTicks(uint32_t milliSeconds);
309
310 /**
311  * Extract interface and resource type from the query.
312  *
313  * @param query is the request received from the client
314  * @param filterOne will include result if the interface is included in the query.
315  * @param filterTwo will include result if the resource type is included in the query.
316  *
317  * @return ::OC_STACK_OK on success, some other value upon failure
318  */
319 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo);
320
321 #if defined(RD_CLIENT) || defined(RD_SERVER)
322 /**
323  * This function binds an resource unique ins value to the resource. This can be only called
324  * when stack is received response from resource-directory.
325  *
326  * @param requestUri URI of the resource.
327  * @param response Response from queries to remote servers.
328  *
329  * @return ::OC_STACK_OK on success, some other value upon failure.
330  */
331 OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
332                                               const OCClientResponse *response);
333 #endif
334
335 /**
336  * Delete all of the dynamically allocated elements that were created for the resource attributes.
337  *
338  * @param resourceAttr Specified resource attribute.
339  */
340 void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes);
341
342 #ifndef TCP_ADAPTER
343 /**
344  * Add resource payload with endpoint payload to discovery payload.
345  *
346  * @param payload       Pointer to discovery payload.
347  * @param res           Pointer to OCresource structure.
348  * @param securePort    Secure port number.
349  * @param isVirtual     true: virtual resource (e.g., oic/res), false: resource.
350  * @param networkInfo   List of CAEndpoint_t.
351  * @param infoSize      Size of CAEndpoint_t list.
352  * @param devAddr       Pointer to OCDevAddr structure.
353  */
354 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
355                                           uint16_t securePort, bool isVirtual,
356                                           void *networkInfo, uint32_t infoSize,
357                                           const OCDevAddr *devAddr);
358 #else
359 /**
360  * Add resource payload with endpoint payload to discovery payload.
361  *
362  * @param payload       Pointer to discovery payload.
363  * @param res           Pointer to OCresource structure.
364  * @param securePort    Secure port number.
365  * @param isVirtual     true: virtual resource (e.g., oic/res, oic/d), false: resource.
366  * @param networkInfo   List of CAEndpoint_t.
367  * @param infoSize      Size of CAEndpoint_t list.
368  * @param devAddr       Pointer to OCDevAddr structure.
369  * @param tcpPort       TCP port number.
370  */
371 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
372                                           uint16_t securePort, bool isVirtual,
373                                           void *networkInfo, uint32_t infoSize,
374                                           const OCDevAddr *devAddr, uint16_t tcpPort);
375 #endif
376 #ifdef __cplusplus
377 }
378 #endif // __cplusplus
379
380 #endif /* OCSTACKINTERNAL_H_ */