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