Merge "Merge branch 'master' into resource-encapsulation" into resource-encapsulation
[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 <logger.h>
42 #include <ocrandom.h>
43
44 #include "cacommon.h"
45 #include "cainterface.h"
46 #include "securevirtualresourcetypes.h"
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif // __cplusplus
51
52
53 //-----------------------------------------------------------------------------
54 // Global variables
55 //-----------------------------------------------------------------------------
56
57 /** Default device entity Handler.*/
58 extern OCDeviceEntityHandler defaultDeviceHandler;
59
60 /** Default Callback parameter.*/
61 extern void* defaultDeviceHandlerCallbackParameter;
62
63 //-----------------------------------------------------------------------------
64 // Defines
65 //-----------------------------------------------------------------------------
66
67 /** The coap scheme */
68 #define OC_COAP_SCHEME "coap://"
69
70 /** the first outgoing sequence number will be 5*/
71 #define OC_OFFSET_SEQUENCE_NUMBER (4)
72
73 /**
74  * This structure will be created in occoap and passed up the stack on the server side.
75  */
76 typedef struct
77 {
78     /** Observe option field.*/
79     uint32_t observationOption;
80
81     /** The REST method retrieved from received request PDU.*/
82     OCMethod method;
83
84     /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
85     char resourceUrl[MAX_URI_LENGTH];
86
87     /** resource query send by client.*/
88     char query[MAX_QUERY_LENGTH];
89
90     /** reqJSON is retrieved from the payload of the received request PDU.*/
91     uint8_t *payload;
92
93     /** qos is indicating if the request is CON or NON.*/
94     OCQualityOfService qos;
95
96     /** Number of the received vendor specific header options.*/
97     uint8_t numRcvdVendorSpecificHeaderOptions;
98
99     /** Array of received vendor specific header option .*/
100     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
101
102     /** Remote end-point address **/
103     OCDevAddr devAddr;
104
105     /** Token for the observe request.*/
106     CAToken_t requestToken;
107
108     /** token length.*/
109     uint8_t tokenLength;
110
111     /** The ID of CoAP PDU.*/
112     uint16_t coapID;
113
114     /** For delayed Response.*/
115     uint8_t delayedResNeeded;
116
117     /** For More packet.*/
118     uint8_t reqMorePacket;
119
120     /** The number of requested packet.*/
121     uint32_t reqPacketNum;
122
123     /** The size of requested packet.*/
124     uint16_t reqPacketSize;
125
126     /** The number of responded packet.*/
127     uint32_t resPacketNum;
128
129     /** Responded packet size.*/
130     uint16_t resPacketSize;
131
132     /** The total size of requested packet.*/
133     size_t reqTotalSize;
134 } OCServerProtocolRequest;
135
136 /**
137  * This structure will be created in occoap and passed up the stack on the client side.
138  */
139 typedef struct
140 {
141     /** handle is retrieved by comparing the token-handle pair in the PDU.*/
142     ClientCB * cbNode;
143
144     /** This is how long this response is valid for (in seconds).*/
145     uint32_t maxAge;
146
147     /** This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led").*/
148     char * fullUri;
149
150     /** This is the relative Uri of the resource. (ex. "/a/led").*/
151     char * rcvdUri;
152
153     /** This is the received payload.*/
154     char * bufRes;
155
156     /** This is the token received OTA.*/
157     CAToken_t rcvdToken;
158
159     /** this structure will be passed to client.*/
160     OCClientResponse * clientResponse;
161 } OCResponse;
162
163 /**
164  * This typedef is to represent our Server Instance identification.
165  */
166 typedef uint8_t ServerID[16];
167
168 //-----------------------------------------------------------------------------
169 // Internal function prototypes
170 //-----------------------------------------------------------------------------
171
172
173 /**
174  * Handler function for sending a response from multiple resources, such as a collection.
175  * Aggregates responses from multiple resource until all responses are received then sends the
176  * concatenated response
177  *
178  * TODO: Need to add a timeout in case a (remote?) resource does not respond
179  *
180  * @param token         Token to search for.
181  * @param tokenLength   Length of token.
182  * @param status        Feedback status.
183  * @return
184  *     ::OCStackResult
185  */
186
187 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
188
189
190 /**
191  * Handler function to execute stack requests
192  *
193  * @param protocolRequest      Pointer to the protocol requests from server.
194  *
195  * @return
196  *     ::OCStackResult
197  */
198 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
199
200 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
201         const CAResponseResult_t responseResult, const CAMessageType_t type,
202         const uint8_t numOptions, const CAHeaderOption_t *options,
203         CAToken_t token, uint8_t tokenLength, const char *resourceUri);
204
205 #ifdef WITH_PRESENCE
206
207 /**
208  * Notify Presence subscribers that a resource has been modified.
209  *
210  * @param resourceType    Handle to the resourceType linked list of resource that was modified.
211  * @param trigger         The simplified reason this API was invoked.
212  *
213  * @return ::OC_STACK_OK on success, some other value upon failure.
214  */
215 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
216         OCPresenceTrigger trigger);
217
218 /**
219  * Send Stop Notification to Presence subscribers.
220  *
221  * @return ::OC_STACK_OK on success, some other value upon failure.
222  */
223 OCStackResult SendStopNotification();
224 #endif // WITH_PRESENCE
225
226 /**
227  * Function to parse the IPv4 address.
228  *
229  * @param ipAddrStr       Pointer to a string of IPv4 address.
230  * @param ipAddr          pointer to IPv4 adress.
231  * @param port            Port number.
232  *
233  * @return true on success, false upon failure.
234  */
235 bool ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
236
237 /**
238  * Bind a resource interface to a resource.
239  *
240  * @param resource Target resource.
241  * @param resourceInterfaceName Resource interface.
242  *
243  * @return ::OC_STACK_OK on success, some other value upon failure.
244  */
245 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
246                                             const char *resourceInterfaceName);
247 /**
248  * Bind a resource type to a resource.
249  *
250  * @param resource Target resource.
251  * @param resourceTypeName Name of resource type.
252  * @return ::OC_STACK_OK on success, some other value upon failure.
253  */
254 OCStackResult BindResourceTypeToResource(OCResource* resource,
255                                             const char *resourceTypeName);
256
257
258 /**
259  * Converts a CAResult_t type to a OCStackResult type.
260  *
261  * @param caResult CAResult_t value to convert.
262  * @return OCStackResult that was converted from the input CAResult_t value.
263  */
264 OCStackResult CAResultToOCResult(CAResult_t caResult);
265
266 /**
267  * Get a byte representation of the server instance ID.
268  * The memory is managed internal to this function, so freeing it externally will
269  * result in a runtime error.
270  *
271  * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
272  * This is done automatically during the OCInit process,
273  * so ensure that this call is done after that.
274  *
275  * @return A uint8_t representation the server instance ID.
276  */
277 const OicUuid_t* OCGetServerInstanceID(void);
278
279 /**
280  * Get a string representation the server instance ID.
281  * The memory is managed internal to this function, so freeing externally will result
282  * in a runtime error.
283  * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
284  * This is done automatically during the OCInit process,
285  * so ensure that this call is done after that.
286  *
287  * @return A string representation  the server instance ID.
288  */
289 const char* OCGetServerInstanceIDString(void);
290
291 /**
292  * Map OCQualityOfService to CAMessageType.
293  *
294  * @param qos Input qos.
295  *
296  * @return CA message type for a given qos.
297  */
298 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
299
300 #ifdef WITH_PRESENCE
301 /**
302  * Enable/disable a resource property.
303  *
304  * @param inputProperty             Pointer to resource property.
305  * @param resourceProperties        Property to be enabled/disabled.
306  * @param enable                    0:disable, 1:enable.
307  *
308  * @return OCStackResult that was converted from the input CAResult_t value.
309  */
310 //TODO: should the following function be public?
311 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
312         OCResourceProperty resourceProperties, uint8_t enable);
313 #endif
314
315 const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
316
317 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
318
319 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
320
321 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
322
323 #ifdef __cplusplus
324 }
325 #endif // __cplusplus
326
327 #endif /* OCSTACKINTERNAL_H_ */
328