Merge "Implement functionality to set PSK credentials for DTLS in OC stack."
[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 "ocstack.h"
32 #include "ocstackconfig.h"
33 #include "occoaptoken.h"
34 #include "occlientcb.h"
35 #include <logger.h>
36 #include <ocrandom.h>
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif // __cplusplus
41
42
43 //-----------------------------------------------------------------------------
44 // Global variables
45 //-----------------------------------------------------------------------------
46 extern OCDeviceEntityHandler defaultDeviceHandler;
47
48 //-----------------------------------------------------------------------------
49 // Defines
50 //-----------------------------------------------------------------------------
51 #define OC_COAP_SCHEME "coap://"
52 #define OC_OFFSET_SEQUENCE_NUMBER (4) // the first outgoing sequence number will be 5
53
54 #define OC_OBSERVER_NOT_INTERESTED       (0)
55 #define OC_OBSERVER_STILL_INTERESTED     (1)
56 #define OC_OBSERVER_FAILED_COMM          (2)
57
58 //-----------------------------------------------------------------------------
59 // Virtual Resource Presence Attributes
60 //-----------------------------------------------------------------------------
61 #ifdef WITH_PRESENCE
62 typedef struct PRESENCERESOURCE{
63     OCResourceHandle handle;
64     uint32_t presenceTTL;
65 } PresenceResource;
66 #endif
67
68 //-----------------------------------------------------------------------------
69 // Forward declarations
70 //-----------------------------------------------------------------------------
71 struct rsrc_t;
72
73 //-----------------------------------------------------------------------------
74 // Typedefs
75 //-----------------------------------------------------------------------------
76
77 // IF here stands for Interface
78 typedef enum {
79     STACK_IF_DEFAULT = 0,
80     STACK_IF_LL,
81     STACK_IF_BATCH,
82     STACK_IF_INVALID
83 } OCStackIfTypes;
84
85 typedef struct resourcetype_t {
86     struct resourcetype_t *next; // linked list; for multiple types on resource
87
88     // Name of the type; this string is ‘.’ (dot) separate list of segments where each segment is a
89     // namespace and the final segment is the type; type and sub-types can be separate with ‘-‘ (dash)
90     // usually only two segments would be defined. Either way this string is meant to be human friendly
91     // and is used opaquely and not parsed by code
92     // This name is used in the “rt=” parameter of a resource description when resources are introspected
93     // and is also use in the <base URI>/types list of available types
94     char *resourcetypename;
95 } OCResourceType;
96
97 typedef struct attr_t {
98     struct attr_t *next; // Points to next resource in list
99
100     // The name of the attribute; used to look up the attribute in list;
101     // for a given attribute SHOULD not be changed once assigned
102     const char *attrName;
103     char *attrValue; // value of the attribute as string
104 } OCAttribute;
105
106 typedef struct resourceinterface_t {
107     struct resourceinterface_t *next; // linked list; for multiple interfaces on resource
108
109     // Name of the interface; this is ‘.’ (dot) separate list of segments where each segment is
110     // a namespace and the final segment is the interface; usually only two segments would be defined.
111     // Either way this string is opaque and not parsed by segment
112     char *name ;
113
114     // Supported content types to serialize request and response on this interface
115     // (REMOVE for V1 – only jSON for all but core.ll that uses Link Format)
116 #if 0
117     char *inputContentType ;
118     char *outputContentType ;
119 #endif
120     /*** Future placeholder for access control and policy ***/
121 } OCResourceInterface;
122
123 typedef struct rsrc_t {
124     struct rsrc_t *next; // Points to next resource in list
125     // Relative path on the device; will be combined with base url to create fully qualified path
126     char *host;
127     char *uri;
128     OCResourceType *rsrcType; // Resource type(s); linked list
129     OCResourceInterface *rsrcInterface; // Resource interface(s); linked list
130     OCAttribute *rsrcAttributes; // Resource interface(s); linked list
131     // Array of pointers to resources; can be used to represent a container of resources
132     // (i.e. hierarchies of resources) or for reference resources (i.e. for a resource collection)
133     struct rsrc_t *rsrcResources[MAX_CONTAINED_RESOURCES];
134     //struct rsrc_t *rsrcResources;
135     // Pointer to function that handles the entity bound to the resource.
136     // This handler has to be explicitly defined by the programmer
137     OCEntityHandler entityHandler;
138     // Properties on the resource – defines meta information on the resource
139     OCResourceProperty resourceProperties ; /* ACTIVE, DISCOVERABLE etc */
140     // Pointer to an opaque object where app/user specific data can be placed with the resource;
141     // this could be information for the entity handler between invocations
142     void *context;
143     // NOTE: Methods supported by this resource should be based on the interface targeted
144     // i.e. look into the interface structure based on the query request Can be removed here; place holder for the note above
145     /* method_t methods; */
146     // Sequence number for observable resources. Per the CoAP standard it is a 24 bit value.
147     uint32_t sequenceNum;
148 } OCResource;
149
150 typedef struct {
151     // Observe option field
152     uint32_t option;
153     // IP address & port of client registered for observe
154     OCDevAddr *subAddr;
155     // CoAP token for the observe request
156     OCCoAPToken *token;
157     // The result of the observe request
158     OCStackResult result;
159 } OCObserveReq;
160
161 // following structure will be created in occoap and passed up the stack on the server side
162 typedef struct {
163     // the REST method retrieved from received request PDU
164     OCMethod method;
165     // resourceUrl will be filled in occoap using the path options in received request PDU
166     unsigned char resourceUrl[MAX_URI_LENGTH];
167     // resource query send by client
168     unsigned char query[MAX_QUERY_LENGTH];
169     // reqJSON is retrieved from the payload of the received request PDU
170     unsigned char reqJSONPayload[MAX_REQUEST_LENGTH];
171     // qos is indicating if the request is CON or NON
172     OCQualityOfService qos;
173     // Observe option field
174     uint32_t observationOption;
175     // An array of the received vendor specific header options
176     uint8_t numRcvdVendorSpecificHeaderOptions;
177     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
178     //////////////////////////////////////////////////////////
179     // TODO: Consider moving these member to CoAP
180     // IP address & port of client registered for observe
181     OCDevAddr requesterAddr;
182     // CoAP token for the observe request
183     OCCoAPToken requestToken;
184     // The ID of CoAP pdu
185     uint16_t coapID;
186     uint8_t delayedResNeeded;
187     uint8_t secured;
188     //////////////////////////////////////////////////////////
189     uint8_t reqMorePacket;
190     uint32_t reqPacketNum;
191     uint16_t reqPacketSize;
192     uint32_t resPacketNum;
193     uint16_t resPacketSize;
194     uint32_t reqTotalSize;
195 } OCServerProtocolRequest;
196
197 typedef struct
198 {
199     // qos is indicating if the request is CON or NON
200     OCQualityOfService qos;
201     // Observe option field
202     uint32_t observationOption;
203     // Allow the entity handler to pass a result with the response
204     OCStackResult result;
205     // IP address & port of client registered for observe
206     OCDevAddr *requesterAddr;
207     // CoAP token for the observe request
208     OCCoAPToken *requestToken;
209     // The ID of CoAP pdu
210     uint16_t coapID;
211     // Flag indicating that response is to be delayed before sending
212     uint8_t delayedResNeeded;
213     uint8_t secured;
214     uint8_t slowFlag;
215     uint8_t notificationFlag;
216     // this is the pointer to server payload data to be transferred
217     unsigned char *payload;
218     // size of server payload data.  Don't rely on null terminated data for size
219     uint16_t payloadSize;
220     // An array of the vendor specific header options the entity handler wishes to use in response
221     uint8_t numSendVendorSpecificHeaderOptions;
222     OCHeaderOption *sendVendorSpecificHeaderOptions;
223     // URI of new resource that entity handler might create
224     unsigned char * resourceUri;
225 } OCServerProtocolResponse;
226
227 // following structure will be created in occoap and passed up the stack on the client side
228 typedef struct {
229     // handle is retrieved by comparing the token-handle pair in the PDU.
230     ClientCB * cbNode;
231     // This is how long this response is valid for (in seconds).
232     uint32_t TTL;
233     // this structure will be passed to client
234     OCClientResponse * clientResponse;
235 } OCResponse;
236
237 //-----------------------------------------------------------------------------
238 // Internal function prototypes
239 //-----------------------------------------------------------------------------
240 OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status);
241 OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest);
242 void HandleStackResponses(OCResponse * response);
243 int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port);
244 #ifdef WITH_PRESENCE
245 OCStackResult SendPresenceNotification(OCResourceType *resourceType);
246 #endif
247
248 OCStackResult BindResourceInterfaceToResource(OCResource* resource,
249                                             const char *resourceInterfaceName);
250
251 OCStackResult BindResourceTypeToResource(OCResource* resource,
252                                             const char *resourceTypeName);
253
254 #ifdef WITH_PRESENCE
255 //TODO: should the following function be public?
256 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
257         OCResourceProperty resourceProperties, uint8_t enable);
258 #endif
259
260 #ifdef __cplusplus
261 }
262 #endif // __cplusplus
263
264 #endif /* OCSTACKINTERNAL_H_ */