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