1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 * This file contains the Internal include file used by lower layers of the OC stack
31 #ifndef OCSTACKINTERNAL_H_
32 #define OCSTACKINTERNAL_H_
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
39 #include "ocstackconfig.h"
40 #include "occlientcb.h"
44 #include "cainterface.h"
45 #include "securevirtualresourcetypes.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 /** Default device entity Handler.*/
57 extern OCDeviceEntityHandler defaultDeviceHandler;
59 /** Default Callback parameter.*/
60 extern void* defaultDeviceHandlerCallbackParameter;
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 /** The coap scheme */
67 #define OC_COAP_SCHEME "coap://"
69 /** the first outgoing sequence number will be 1*/
70 #define OC_OFFSET_SEQUENCE_NUMBER (0)
73 * This structure will be created in occoap and passed up the stack on the server side.
77 /** Observe option field.*/
78 uint32_t observationOption;
80 /** The REST method retrieved from received request PDU.*/
83 /** the requested payload format. */
84 OCPayloadFormat acceptFormat;
86 /** the requested payload format. */
87 uint16_t acceptVersion;
89 /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
90 char resourceUrl[MAX_URI_LENGTH];
92 /** resource query send by client.*/
93 char query[MAX_QUERY_LENGTH];
95 /** reqJSON is retrieved from the payload of the received request PDU.*/
98 /** qos is indicating if the request is CON or NON.*/
99 OCQualityOfService qos;
101 /** Number of the received vendor specific header options.*/
102 uint8_t numRcvdVendorSpecificHeaderOptions;
104 /** Array of received vendor specific header option .*/
105 OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
107 /** Remote end-point address **/
110 /** Token for the observe request.*/
111 CAToken_t requestToken;
116 /** The ID of CoAP PDU.*/
119 /** For delayed Response.*/
120 uint8_t delayedResNeeded;
122 /** For More packet.*/
123 uint8_t reqMorePacket;
125 /** The number of requested packet.*/
126 uint32_t reqPacketNum;
128 /** The size of requested packet.*/
129 uint16_t reqPacketSize;
131 /** The number of responded packet.*/
132 uint32_t resPacketNum;
134 /** Responded packet size.*/
135 uint16_t resPacketSize;
137 /** The total size of requested packet.*/
139 } OCServerProtocolRequest;
142 * This typedef is to represent our Server Instance identification.
144 typedef uint8_t ServerID[16];
146 //-----------------------------------------------------------------------------
147 // Internal function prototypes
148 //-----------------------------------------------------------------------------
152 * Handler function for sending a response from multiple resources, such as a collection.
153 * Aggregates responses from multiple resource until all responses are received then sends the
154 * concatenated response
156 * TODO: Need to add a timeout in case a (remote?) resource does not respond
158 * @param token Token to search for.
159 * @param tokenLength Length of token.
160 * @param status Feedback status.
165 OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
169 * Handler function to execute stack requests
171 * @param protocolRequest Pointer to the protocol requests from server.
176 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
178 OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
179 const CAResponseResult_t responseResult, const CAMessageType_t type,
180 const uint8_t numOptions, const CAHeaderOption_t *options,
181 CAToken_t token, uint8_t tokenLength, const char *resourceUri,
182 CADataType_t dataType);
187 * Notify Presence subscribers that a resource has been modified.
189 * @param resourceType Handle to the resourceType linked list of resource that was modified.
190 * @param trigger The simplified reason this API was invoked.
192 * @return ::OC_STACK_OK on success, some other value upon failure.
194 OCStackResult SendPresenceNotification(OCResourceType *resourceType,
195 OCPresenceTrigger trigger);
198 * Send Stop Notification to Presence subscribers.
200 * @return ::OC_STACK_OK on success, some other value upon failure.
202 OCStackResult SendStopNotification();
203 #endif // WITH_PRESENCE
206 * Function to parse the IPv4 address.
208 * @param ipAddrStr Pointer to a string of IPv4 address.
209 * @param ipAddr pointer to IPv4 adress.
210 * @param port Port number.
212 * @return true on success, false upon failure.
214 bool ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
217 * Bind a resource interface to a resource.
219 * @param resource Target resource.
220 * @param resourceInterfaceName Resource interface.
222 * @return ::OC_STACK_OK on success, some other value upon failure.
224 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
225 const char *resourceInterfaceName);
227 * Bind a resource type to a resource.
229 * @param resource Target resource.
230 * @param resourceTypeName Name of resource type.
231 * @return ::OC_STACK_OK on success, some other value upon failure.
233 OCStackResult BindResourceTypeToResource(OCResource *resource,
234 const char *resourceTypeName);
236 * Bind a Transport Protocol Suites type to a resource.
238 * @param resource Target resource.
239 * @param resourceTpsTypes Name of transport protocol suites type.
240 * @return ::OC_STACK_OK on success, some other value upon failure.
242 OCStackResult BindTpsTypeToResource(OCResource *resource,
243 OCTpsSchemeFlags resourceTpsTypes);
246 * Convert OCStackResult to CAResponseResult_t.
248 * @param ocCode OCStackResult code.
249 * @param method OCMethod method the return code replies to.
250 * @return ::CA_CONTENT on OK, some other value upon failure.
252 CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
255 * Converts a CAResult_t type to a OCStackResult type.
257 * @param caResult CAResult_t value to convert.
258 * @return OCStackResult that was converted from the input CAResult_t value.
260 OCStackResult CAResultToOCResult(CAResult_t caResult);
263 * Converts a OCStackResult type to a bool type.
265 * @param ocResult OCStackResult value to convert.
266 * @return true on success, false upon failure.
268 bool OCResultToSuccess(OCStackResult ocResult);
271 * Map OCQualityOfService to CAMessageType.
273 * @param qos Input qos.
275 * @return CA message type for a given qos.
277 CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
281 * Enable/disable a resource property.
283 * @param inputProperty Pointer to resource property.
284 * @param resourceProperties Property to be enabled/disabled.
285 * @param enable 0:disable, 1:enable.
287 * @return OCStackResult that was converted from the input CAResult_t value.
289 //TODO: should the following function be public?
290 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
291 OCResourceProperty resourceProperties, uint8_t enable);
294 const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
296 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
298 void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
300 void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
303 * Get the CoAP ticks after the specified number of milli-seconds.
305 * @param milliSeconds Milli-seconds.
308 uint32_t GetTicks(uint32_t milliSeconds);
311 * Extract interface and resource type from the query.
313 * @param query is the request received from the client
314 * @param filterOne will include result if the interface is included in the query.
315 * @param filterTwo will include result if the resource type is included in the query.
317 * @return ::OC_STACK_OK on success, some other value upon failure
319 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo);
321 #if defined(RD_CLIENT) || defined(RD_SERVER)
323 * This function binds an resource unique ins value to the resource. This can be only called
324 * when stack is received response from resource-directory.
326 * @param requestUri URI of the resource.
327 * @param response Response from queries to remote servers.
329 * @return ::OC_STACK_OK on success, some other value upon failure.
331 OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
332 const OCClientResponse *response);
336 * Delete all of the dynamically allocated elements that were created for the resource attributes.
338 * @param resourceAttr Specified resource attribute.
340 void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes);
344 * Add resource payload with endpoint payload to discovery payload.
346 * @param payload Pointer to discovery payload.
347 * @param res Pointer to OCresource structure.
348 * @param securePort Secure port number.
349 * @param isVirtual true: virtual resource (e.g., oic/res), false: resource.
350 * @param networkInfo List of CAEndpoint_t.
351 * @param infoSize Size of CAEndpoint_t list.
352 * @param devAddr Pointer to OCDevAddr structure.
354 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
355 uint16_t securePort, bool isVirtual,
356 void *networkInfo, uint32_t infoSize,
357 const OCDevAddr *devAddr);
360 * Add resource payload with endpoint payload to discovery payload.
362 * @param payload Pointer to discovery payload.
363 * @param res Pointer to OCresource structure.
364 * @param securePort Secure port number.
365 * @param isVirtual true: virtual resource (e.g., oic/res, oic/d), false: resource.
366 * @param networkInfo List of CAEndpoint_t.
367 * @param infoSize Size of CAEndpoint_t list.
368 * @param devAddr Pointer to OCDevAddr structure.
369 * @param tcpPort TCP port number.
371 void OCDiscoveryPayloadAddResourceWithEps(OCDiscoveryPayload *payload, const OCResource *res,
372 uint16_t securePort, bool isVirtual,
373 void *networkInfo, uint32_t infoSize,
374 const OCDevAddr *devAddr, uint16_t tcpPort);
378 #endif // __cplusplus
380 #endif /* OCSTACKINTERNAL_H_ */