//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
-//-----------------------------------------------------------------------------
-// Internal include file used by lower layers of the OC stack
-//-----------------------------------------------------------------------------
+
+
+/**
+ * @file
+ *
+ * This file contains the Internal include file used by lower layers of the OC stack
+ *
+ */
+
#ifndef OCSTACKINTERNAL_H_
#define OCSTACKINTERNAL_H_
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
+#include <stdbool.h>
#include "ocstack.h"
#include "ocstackconfig.h"
-#include "occoaptoken.h"
#include "occlientcb.h"
-#include <logger.h>
#include <ocrandom.h>
+#include "cacommon.h"
+#include "cainterface.h"
+#include "securevirtualresourcetypes.h"
+
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
+
+/** Default device entity Handler.*/
extern OCDeviceEntityHandler defaultDeviceHandler;
+/** Default Callback parameter.*/
+extern void* defaultDeviceHandlerCallbackParameter;
+
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
+
+/** The coap scheme */
#define OC_COAP_SCHEME "coap://"
-#define OC_OFFSET_SEQUENCE_NUMBER (4) // the first outgoing sequence number will be 5
-//-----------------------------------------------------------------------------
-// Virtual Resource Presence Attributes
-//-----------------------------------------------------------------------------
-#ifdef WITH_PRESENCE
-typedef struct PRESENCERESOURCE{
- OCResourceHandle handle;
- uint32_t presenceTTL;
-} PresenceResource;
-#endif
+/** the first outgoing sequence number will be 1*/
+#define OC_OFFSET_SEQUENCE_NUMBER (0)
-//-----------------------------------------------------------------------------
-// Forward declarations
-//-----------------------------------------------------------------------------
-struct rsrc_t;
+/**
+ * This structure will be created in occoap and passed up the stack on the server side.
+ */
+typedef struct
+{
+ /** Observe option field.*/
+ uint32_t observationOption;
-//-----------------------------------------------------------------------------
-// Typedefs
-//-----------------------------------------------------------------------------
+ /** The REST method retrieved from received request PDU.*/
+ OCMethod method;
-// IF here stands for Interface
-typedef enum {
- STACK_IF_DEFAULT = 0,
- STACK_IF_LL,
- STACK_IF_BATCH
-} OCStackIfTypes;
-
-typedef struct resourcetype_t {
- struct resourcetype_t *next; // linked list; for multiple types on resource
-
- // Name of the type; this string is ‘.’ (dot) separate list of segments where each segment is a
- // namespace and the final segment is the type; type and sub-types can be separate with ‘-‘ (dash)
- // usually only two segments would be defined. Either way this string is meant to be human friendly
- // and is used opaquely and not parsed by code
- // This name is used in the “rt=” parameter of a resource description when resources are introspected
- // and is also use in the <base URI>/types list of available types
- char *resourcetypename;
-} OCResourceType;
-
-typedef struct attr_t {
- struct attr_t *next; // Points to next resource in list
-
- // The name of the attribute; used to look up the attribute in list;
- // for a given attribute SHOULD not be changed once assigned
- const char *attrName;
- char *attrValue; // value of the attribute as string
-} OCAttribute;
-
-typedef struct resourceinterface_t {
- struct resourceinterface_t *next; // linked list; for multiple interfaces on resource
-
- // Name of the interface; this is ‘.’ (dot) separate list of segments where each segment is
- // a namespace and the final segment is the interface; usually only two segments would be defined.
- // Either way this string is opaque and not parsed by segment
- char *name ;
-
- // Supported content types to serialize request and response on this interface
- // (REMOVE for V1 – only jSON for all but core.ll that uses Link Format)
-#if 0
- char *inputContentType ;
- char *outputContentType ;
-#endif
- /*** Future placeholder for access control and policy ***/
-} OCResourceInterface;
-
-typedef struct rsrc_t {
- struct rsrc_t *next; // Points to next resource in list
- // Relative path on the device; will be combined with base url to create fully qualified path
- char *host;
- char *uri;
- OCResourceType *rsrcType; // Resource type(s); linked list
- OCResourceInterface *rsrcInterface; // Resource interface(s); linked list
- OCAttribute *rsrcAttributes; // Resource interface(s); linked list
- // Array of pointers to resources; can be used to represent a container of resources
- // (i.e. hierarchies of resources) or for reference resources (i.e. for a resource collection)
- struct rsrc_t *rsrcResources[MAX_CONTAINED_RESOURCES];
- //struct rsrc_t *rsrcResources;
- // Pointer to function that handles the entity bound to the resource.
- // This handler has to be explicitly defined by the programmer
- OCEntityHandler entityHandler;
- // Properties on the resource – defines meta information on the resource
- OCResourceProperty resourceProperties ; /* ACTIVE, DISCOVERABLE etc */
- // Pointer to an opaque object where app/user specific data can be placed with the resource;
- // this could be information for the entity handler between invocations
- void *context;
- // NOTE: Methods supported by this resource should be based on the interface targeted
- // i.e. look into the interface structure based on the query request Can be removed here; place holder for the note above
- /* method_t methods; */
- // Sequence number for observable resources. Per the CoAP standard it is a 24 bit value.
- uint32_t sequenceNum;
-} OCResource;
-
-typedef struct {
- // Observe option field
- uint32_t option;
- // IP address & port of client registered for observe
- OCDevAddr *subAddr;
- // CoAP token for the observe request
- OCCoAPToken *token;
- // The result of the observe request
- OCStackResult result;
-} OCObserveReq;
-
-// following structure will be created in occoap and passed up the stack on the server side
-typedef struct {
- // resourceUrl will be filled in occoap using the path options in received request PDU
- unsigned char * resourceUrl;
- // qos is indicating if the request is CON or NON
+ /** the requested payload format. */
+ OCPayloadFormat acceptFormat;
+
+ /** resourceUrl will be filled in occoap using the path options in received request PDU.*/
+ char resourceUrl[MAX_URI_LENGTH];
+
+ /** resource query send by client.*/
+ char query[MAX_QUERY_LENGTH];
+
+ /** reqJSON is retrieved from the payload of the received request PDU.*/
+ uint8_t *payload;
+
+ /** qos is indicating if the request is CON or NON.*/
OCQualityOfService qos;
- // this structure points to the information for processing observe option
- OCObserveReq *observe;
- // If a subscription update, this is count of observe notifications from server perspective.
- uint32_t sequenceNum;
- // this structure will be passed to entity handler
- OCEntityHandlerRequest * entityHandlerRequest;
- // Indicate whether the request arrives on a secure port
- uint8_t secure;
-} OCRequest;
-
-// following structure will be created in occoap and passed up the stack on the client side
-typedef struct {
- // handle is retrieved by comparing the token-handle pair in the PDU.
- ClientCB * cbNode;
- // This is how long this response is valid for (in seconds).
- uint32_t TTL;
- // this structure will be passed to client
- OCClientResponse * clientResponse;
-} OCResponse;
+
+ /** Number of the received vendor specific header options.*/
+ uint8_t numRcvdVendorSpecificHeaderOptions;
+
+ /** Array of received vendor specific header option .*/
+ OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
+
+ /** Remote end-point address **/
+ OCDevAddr devAddr;
+
+ /** Token for the observe request.*/
+ CAToken_t requestToken;
+
+ /** token length.*/
+ uint8_t tokenLength;
+
+ /** The ID of CoAP PDU.*/
+ uint16_t coapID;
+
+ /** For delayed Response.*/
+ uint8_t delayedResNeeded;
+
+ /** For More packet.*/
+ uint8_t reqMorePacket;
+
+ /** The number of requested packet.*/
+ uint32_t reqPacketNum;
+
+ /** The size of requested packet.*/
+ uint16_t reqPacketSize;
+
+ /** The number of responded packet.*/
+ uint32_t resPacketNum;
+
+ /** Responded packet size.*/
+ uint16_t resPacketSize;
+
+ /** The total size of requested packet.*/
+ size_t reqTotalSize;
+} OCServerProtocolRequest;
+
+/**
+ * This typedef is to represent our Server Instance identification.
+ */
+typedef uint8_t ServerID[16];
//-----------------------------------------------------------------------------
// Internal function prototypes
//-----------------------------------------------------------------------------
-OCStackResult HandleStackRequests(OCRequest * request);
-void HandleStackResponses(OCResponse * response);
-int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
+
+/**
+ * Handler function for sending a response from multiple resources, such as a collection.
+ * Aggregates responses from multiple resource until all responses are received then sends the
+ * concatenated response
+ *
+ * TODO: Need to add a timeout in case a (remote?) resource does not respond
+ *
+ * @param token Token to search for.
+ * @param tokenLength Length of token.
+ * @param status Feedback status.
+ * @return
+ * ::OCStackResult
+ */
+
+OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status);
+
+
+/**
+ * Handler function to execute stack requests
+ *
+ * @param protocolRequest Pointer to the protocol requests from server.
+ *
+ * @return
+ * ::OCStackResult
+ */
+OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
+
+OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
+ const CAResponseResult_t responseResult, const CAMessageType_t type,
+ const uint8_t numOptions, const CAHeaderOption_t *options,
+ CAToken_t token, uint8_t tokenLength, const char *resourceUri,
+ CADataType_t dataType);
#ifdef WITH_PRESENCE
+
+/**
+ * Notify Presence subscribers that a resource has been modified.
+ *
+ * @param resourceType Handle to the resourceType linked list of resource that was modified.
+ * @param trigger The simplified reason this API was invoked.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult SendPresenceNotification(OCResourceType *resourceType,
+ OCPresenceTrigger trigger);
+
+/**
+ * Send Stop Notification to Presence subscribers.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult SendStopNotification();
+#endif // WITH_PRESENCE
+
+/**
+ * Function to parse the IPv4 address.
+ *
+ * @param ipAddrStr Pointer to a string of IPv4 address.
+ * @param ipAddr pointer to IPv4 adress.
+ * @param port Port number.
+ *
+ * @return true on success, false upon failure.
+ */
+bool ParseIPv4Address(char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
+
+/**
+ * Bind a resource interface to a resource.
+ *
+ * @param resource Target resource.
+ * @param resourceInterfaceName Resource interface.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult BindResourceInterfaceToResource(OCResource* resource,
+ const char *resourceInterfaceName);
+/**
+ * Bind a resource type to a resource.
+ *
+ * @param resource Target resource.
+ * @param resourceTypeName Name of resource type.
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult BindResourceTypeToResource(OCResource* resource,
+ const char *resourceTypeName);
+
+/**
+ * Convert OCStackResult to CAResponseResult_t.
+ *
+ * @param ocCode OCStackResult code.
+ * @param method OCMethod method the return code replies to.
+ * @return ::CA_CONTENT on OK, some other value upon failure.
+ */
+CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
+
+/**
+ * Converts a CAResult_t type to a OCStackResult type.
+ *
+ * @param caResult CAResult_t value to convert.
+ * @return OCStackResult that was converted from the input CAResult_t value.
+ */
+OCStackResult CAResultToOCResult(CAResult_t caResult);
+
+/**
+ * Converts a OCStackResult type to a bool type.
+ *
+ * @param ocResult OCStackResult value to convert.
+ * @return true on success, false upon failure.
+ */
+bool OCResultToSuccess(OCStackResult ocResult);
+
+/**
+ * Map OCQualityOfService to CAMessageType.
+ *
+ * @param qos Input qos.
+ *
+ * @return CA message type for a given qos.
+ */
+CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos);
+
+#ifdef WITH_PRESENCE
+/**
+ * Enable/disable a resource property.
+ *
+ * @param inputProperty Pointer to resource property.
+ * @param resourceProperties Property to be enabled/disabled.
+ * @param enable 0:disable, 1:enable.
+ *
+ * @return OCStackResult that was converted from the input CAResult_t value.
+ */
//TODO: should the following function be public?
OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
OCResourceProperty resourceProperties, uint8_t enable);
+
+const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
+
+OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
+#endif // WITH_PRESENCE
+
+void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out);
+
+void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out);
+
+/**
+ * Get the CoAP ticks after the specified number of milli-seconds.
+ *
+ * @param milliSeconds Milli-seconds.
+ * @return CoAP ticks
+ */
+uint32_t GetTicks(uint32_t milliSeconds);
+
+/**
+ * Extract interface and resource type from the query.
+ *
+ * @param query is the request received from the client
+ * @param filterOne will include result if the interface is included in the query.
+ * @param filterTwo will include result if the resource type is included in the query.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure
+ */
+OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo);
+
+#if defined(RD_CLIENT) || defined(RD_SERVER)
+/**
+ * This function binds an resource unique ins value to the resource. This can be only called
+ * when stack is received response from resource-directory.
+ *
+ * @param requestUri URI of the resource.
+ * @param response Response from queries to remote servers.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult OCUpdateResourceInsWithResponse(const char *requestUri,
+ const OCClientResponse *response);
#endif
+/**
+ * Get the CoAP ticks after the specified number of milli-seconds.
+ *
+ * @param milliSeconds Milli-seconds.
+ * @return CoAP ticks
+ */
+uint32_t GetTicks(uint32_t milliSeconds);
+
+/**
+ * Delete all of the dynamically allocated elements that were created for the resource attributes.
+ *
+ * @param resourceAttr Specified resource attribute.
+ */
+void OCDeleteResourceAttributes(OCAttribute *rsrcAttributes);
+
+/**
+ * A request uri consists of the following components in order:
+ * example
+ * optionally one of
+ * CoAP over UDP prefix "coap://"
+ * CoAP over TCP prefix "coap+tcp://"
+ * CoAP over DTLS prefix "coaps://"
+ * CoAP over TLS prefix "coaps+tcp://"
+ * optionally one of
+ * IPv6 address "[1234::5678]"
+ * IPv4 address "192.168.1.1"
+ * optional port ":5683"
+ * resource uri "/oc/core..."
+ *
+ * for PRESENCE requests, extract resource type.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+OCStackResult ParseRequestUri(const char *fullUri,
+ OCTransportAdapter adapter,
+ OCTransportFlags flags,
+ OCDevAddr **devAddr,
+ char **resourceUri,
+ char **resourceType);
+
+/**
+ * Fix up client response data.
+ *
+ * @param cr Response from queries to remote servers.
+ */
+void FixUpClientResponse(OCClientResponse *cr);
+
#ifdef __cplusplus
}
#endif // __cplusplus