Merge branch 'master' into resource-manipulation
[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 // Internal include file used by lower layers of the OC stack
24 //-----------------------------------------------------------------------------
25 #ifndef OCSTACKINTERNAL_H_
26 #define OCSTACKINTERNAL_H_
27
28 //-----------------------------------------------------------------------------
29 // Includes
30 //-----------------------------------------------------------------------------
31 #include <stdbool.h>
32 #include "ocstack.h"
33 #include "ocstackconfig.h"
34 #include "occlientcb.h"
35 #include <logger.h>
36 #include <ocrandom.h>
37
38 #include "cacommon.h"
39 #include "cainterface.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif // __cplusplus
44
45
46 //-----------------------------------------------------------------------------
47 // Global variables
48 //-----------------------------------------------------------------------------
49 extern OCDeviceEntityHandler defaultDeviceHandler;
50 extern void* defaultDeviceHandlerCallbackParameter;
51
52 //-----------------------------------------------------------------------------
53 // Defines
54 //-----------------------------------------------------------------------------
55 #define OC_COAP_SCHEME "coap://"
56 #define OC_OFFSET_SEQUENCE_NUMBER (4) // the first outgoing sequence number will be 5
57
58 /**
59  * This structure will be created in occoap and passed up the stack on the server side.
60  */
61 typedef struct
62 {
63     // Observe option field
64     uint32_t observationOption;
65     // the REST method retrieved from received request PDU
66     OCMethod method;
67     // resourceUrl will be filled in occoap using the path options in received request PDU
68     char resourceUrl[MAX_URI_LENGTH];
69     // resource query send by client
70     char query[MAX_QUERY_LENGTH];
71     // reqJSON is retrieved from the payload of the received request PDU
72     char reqJSONPayload[MAX_REQUEST_LENGTH];
73     // qos is indicating if the request is CON or NON
74     OCQualityOfService qos;
75     // An array of the received vendor specific header options
76     uint8_t numRcvdVendorSpecificHeaderOptions;
77     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
78
79     /** Remote endpoint address **/
80     OCDevAddr devAddr;
81
82     //token for the observe request
83     CAToken_t requestToken;
84     uint8_t tokenLength; //token length
85     // The ID of CoAP pdu
86     uint16_t coapID;
87     uint8_t delayedResNeeded;
88     uint8_t reqMorePacket;
89     uint32_t reqPacketNum;
90     uint16_t reqPacketSize;
91     uint32_t resPacketNum;
92     uint16_t resPacketSize;
93     size_t reqTotalSize;
94 } OCServerProtocolRequest;
95
96 /**
97  * This structure will be created in occoap and passed up the stack on the client side.
98  */
99 typedef struct
100 {
101     // handle is retrieved by comparing the token-handle pair in the PDU.
102     ClientCB * cbNode;
103     // This is how long this response is valid for (in seconds).
104     uint32_t maxAge;
105     // This is the Uri of the resource. (ex. "coap://192.168.1.1/a/led")
106     char * fullUri;
107     // This is the relative Uri of the resource. (ex. "/a/led")
108     char * rcvdUri;
109     // This is the received payload.
110     char * bufRes;
111
112     // This is the token received OTA.
113     CAToken_t rcvdToken;
114
115     // this structure will be passed to client
116     OCClientResponse * clientResponse;
117 } OCResponse;
118
119 /**
120  * This typedef is to represent our Server Instance identification.
121  */
122 typedef uint8_t ServerID[16];
123
124 //-----------------------------------------------------------------------------
125 // Internal function prototypes
126 //-----------------------------------------------------------------------------
127
128
129 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
130
131 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
132
133 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
134         const CAResponseResult_t responseResult, const CAMessageType_t type,
135         const uint8_t numOptions, const CAHeaderOption_t *options,
136         CAToken_t token, uint8_t tokenLength);
137
138 #ifdef WITH_PRESENCE
139 /**
140  * The OCPresenceTrigger enum delineates the three spec-compliant modes for
141  * "Trigger." These enum values are then mapped to JSON strings
142  * "create", "change", "delete", respectively, before getting encoded into
143  * the JSON payload.
144  *
145  * @enum OC_PRESENCE_TRIGGER_CREATE The creation of a resource is associated with
146  *                            this invocation of @ref SendPresenceNotification.
147  * @enum OC_PRESENCE_TRIGGER_CHANGE The change/update of a resource is associated
148  *                            this invocation of @ref SendPresenceNotification.
149  * @enum OC_PRESENCE_TRIGGER_DELETE The deletion of a resource is associated with
150  *                            this invocation of @ref SendPresenceNotification.
151  *
152  */
153 typedef enum
154 {
155     OC_PRESENCE_TRIGGER_CREATE = 0,
156     OC_PRESENCE_TRIGGER_CHANGE = 1,
157     OC_PRESENCE_TRIGGER_DELETE = 2
158 } OCPresenceTrigger;
159
160 /**
161  * Notify Presence subscribers that a resource has been modified.
162  *
163  * @param resourceType Handle to the resourceType linked list of resource
164  *                     that was modified.
165  * @param trigger The simplified reason this API was invoked. Valid values are
166  *                  @ref OC_PRESENCE_TRIGGER_CREATE, @ref OC_PRESENCE_TRIGGER_CHANGE,
167  *                  @ref OC_PRESENCE_TRIGGER_DELETE.
168  * @return ::OC_STACK_OK on success, some other value upon failure.
169  */
170 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
171         OCPresenceTrigger trigger);
172
173 /**
174  * Send Stop Notification to Presence subscribers.
175  *
176  * @return ::OC_STACK_OK on success, some other value upon failure.
177  */
178 OCStackResult SendStopNotification();
179 #endif // WITH_PRESENCE
180 bool ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
181
182 /**
183  * Bind a resource interface to a resource.
184  *
185  * @param resource Target resource.
186  * @param resourceInterfaceName Resource interface.
187  * @return ::OC_STACK_OK on success, some other value upon failure.
188  */
189 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
190                                             const char *resourceInterfaceName);
191 /**
192  * Bind a resourcetype to a resource.
193  *
194  * @param resource Target resource.
195  * @param resourceTypeName Name of resource type.
196  * @return ::OC_STACK_OK on success, some other value upon failure.
197  */
198 OCStackResult BindResourceTypeToResource(OCResource* resource,
199                                             const char *resourceTypeName);
200
201 // Converts a CAResult_t type to a OCStackResult type.
202 /**
203  * Converts a CAResult_t type to a OCStackResult type.
204  *
205  * @param caResult CAResult_t value to convert
206  * @return OCStackResult that was converted from the input CAResult_t value.
207  */
208 OCStackResult CAResultToOCResult(CAResult_t caResult);
209
210 /**
211  * Get a byte representation of the server instance ID.
212  * The memory is managed internal to this function, so freeing it externally will
213  * result in a runtime error
214  *
215  * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
216  * This is done automatically during the OCInit process,
217  * so ensure that this call is done after that.
218  *
219  * @return A uint8_t representation the server instance ID.
220  */
221 const uint8_t* OCGetServerInstanceID(void);
222
223 /**
224  * Get a string representation the server instance ID.
225  * The memory is managed internal to this function, so freeing externally will result
226  * in a runtime error
227  * Note: This will NOT seed the RNG, so it must be called after the RNG is seeded.
228  * This is done automatically during the OCInit process,
229  * so ensure that this call is done after that.
230  *
231  * @return A string representation  the server instance ID.
232  */
233 const char* OCGetServerInstanceIDString(void);
234
235 /**
236  * Map OCQualityOfService to CAMessageType.
237  *
238  * @param qos Input qos.
239  * @return CA message type for a given qos.
240  */
241 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
242
243 #ifdef WITH_PRESENCE
244 /**
245  * Enable/disable a resource property.
246  *
247  * @param inputProperty Pointer to resource property.
248  * @param resourceProperties Property to be enabled/disabled.
249  * @param enable 0:disable, 1:enable.
250  *
251  * @return OCStackResult that was converted from the input CAResult_t value.
252  */
253 //TODO: should the following function be public?
254 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
255         OCResourceProperty resourceProperties, uint8_t enable);
256 #endif
257
258 const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
259
260 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
261
262 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
263
264 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
265
266 #ifdef __cplusplus
267 }
268 #endif // __cplusplus
269
270 #endif /* OCSTACKINTERNAL_H_ */
271