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