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