f7921b7f8e3ad359cb8cbae302eb2cc282a70003
[platform/upstream/iotivity.git] / resource / csdk / stack / include / octypes.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 #ifndef OCTYPES_H_
22 #define OCTYPES_H_
23
24 #include "ocstackconfig.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif // __cplusplus
29 #define WITH_PRESENCE
30 //-----------------------------------------------------------------------------
31 // Defines
32 //-----------------------------------------------------------------------------
33
34 //TODO: May want to refactor this in upcoming sprints.
35 //Don't want to expose to application layer that lower level stack is using CoAP.
36
37 /// Authority + URI string to prefix well known queries
38 #define OC_WELL_KNOWN_QUERY                  "224.0.1.187:5683/oc/core"
39 #define OC_EXPLICIT_DEVICE_DISCOVERY_URI     "224.0.1.187:5683/oc/core/d?rt=core.led"
40 /// Multicast address and port string to prefix multicast queries
41 #define OC_MULTICAST_PREFIX                  "224.0.1.187:5683"
42 /// IP Multicast address to use for multicast requests
43 #define OC_MULTICAST_IP                      "224.0.1.187"
44 /// IP Multicast port to use for multicast requests
45 #define OC_MULTICAST_PORT                    5683
46
47 #ifdef WITH_PRESENCE
48 #define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
49 /// OC_MAX_PRESENCE_TTL_SECONDS sets the maximum time to live (TTL) for presence.
50 /// NOTE: Changing the setting to a longer duration may lead to unsupported and untested
51 /// operation.
52 #define OC_MAX_PRESENCE_TTL_SECONDS     (60 * 60 * 24) // 60 sec/min * 60 min/hr * 24 hr/day
53 #define OC_PRESENCE_URI                      "/oc/presence"
54 #endif
55
56 //-----------------------------------------------------------------------------
57 // Typedefs
58 //-----------------------------------------------------------------------------
59
60 /**
61  * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
62  */
63 typedef struct OCDevAddr
64 {
65     uint32_t     size;                    ///< length of the address stored in addr field.
66     uint8_t      addr[DEV_ADDR_SIZE_MAX]; ///< device address.
67 } OCDevAddr;
68
69 /**
70  * OC Virtual resources supported by every OC device.
71  */
72 typedef enum
73 {
74     OC_WELL_KNOWN_URI= 0,       ///< "/oc/core"
75     OC_DEVICE_URI,              ///< "/oc/core/d"
76     OC_RESOURCE_TYPES_URI,      ///< "/oc/core/d/type"
77     #ifdef WITH_PRESENCE
78     OC_PRESENCE,                ///< "/oc/presence"
79     #endif
80     OC_MAX_VIRTUAL_RESOURCES    ///<s Max items in the list
81 } OCVirtualResources;
82
83 /**
84  * Standard RESTful HTTP Methods.
85  */
86 typedef enum
87 {
88     OC_REST_NOMETHOD    = 0,
89     OC_REST_GET         = (1 << 0),     ///< Read
90     OC_REST_PUT         = (1 << 1),     ///< Write
91     OC_REST_POST        = (1 << 2),     ///< Update
92     OC_REST_DELETE      = (1 << 3),     ///< Delete
93     /// Register observe request for most up date notifications ONLY.
94     OC_REST_OBSERVE     = (1 << 4),
95     /// Register observe request for all notifications, including stale notifications.
96     OC_REST_OBSERVE_ALL = (1 << 5),
97     /// Deregister observation, intended for internal use
98     OC_REST_CANCEL_OBSERVE = (1 << 6),
99     #ifdef WITH_PRESENCE
100     /// Subscribe for all presence notifications of a particular resource.
101     OC_REST_PRESENCE    = (1 << 7)
102     #endif
103 } OCMethod;
104
105 /**
106  * Host Mode of Operation.
107  */
108 typedef enum
109 {
110     OC_CLIENT = 0,
111     OC_SERVER,
112     OC_CLIENT_SERVER
113 } OCMode;
114
115 /**
116  * Quality of Service.
117  *
118  * OCQualityOfService attempts to abstract the guarantees provided by the underlying transport
119  * protocol.  The precise definitions of each quality of service level depend on the
120  * implementation.  In descriptions below are for the current implementation and may changed
121  * over time.
122  */
123 typedef enum
124 {
125     /// Packet delivery is best effort
126     OC_LOW_QOS = 0,
127     /// Packet delivery is best effort
128     OC_MEDIUM_QOS,
129     /// Acknowledgements are used to confirm delivery
130     OC_HIGH_QOS,
131     /// No Quality is defined, let the stack decide
132     OC_NA_QOS
133 } OCQualityOfService;
134
135 /**
136  * Resource Properties.
137  *
138  * ::OC_ACTIVE       When this bit is set, the resource is initialized, otherwise the resource
139  *                   is 'inactive'. 'inactive' signifies that the resource has been marked for
140  *                   deletion or is already deleted.
141  * ::OC_DISCOVERABLE When this bit is set, the resource is allowed to be discovered by clients.
142  * ::OC_OBSERVABLE   When this bit is set, the resource is allowed to be observed by clients.
143  * ::OC_SLOW         When this bit is set, the resource has been marked as 'slow'. 'slow' signifies
144  *                   that responses from this resource can expect delays in processing its
145  *                   requests from clients.
146  * ::OC_SECURE       When this bit is set, the resource is a secure resource.
147  */
148 typedef enum
149 {
150     OC_ACTIVE       = (1 << 0),
151     OC_DISCOVERABLE = (1 << 1),
152     OC_OBSERVABLE   = (1 << 2),
153     OC_SLOW         = (1 << 3),
154     OC_SECURE       = (1 << 4)
155 } OCResourceProperty;
156
157 /**
158  * Transport Protocol IDs.
159  */
160 typedef enum
161 {
162     OC_INVALID_ID   = (1 << 0),
163     OC_COAP_ID      = (1 << 1)
164 } OCTransportProtocolID;
165
166 /**
167  * Adaptor types.
168  */
169 typedef enum
170 {
171     OC_ETHERNET = 0,
172     OC_WIFI,
173     OC_EDR,
174     OC_LE,
175     OC_ALL // Multicast message: send over all the interfaces.
176 } OCConnectivityType;
177
178 /**
179  * Declares Stack Results & Errors.
180  */
181 typedef enum
182 {
183     /* Success status code - START HERE */
184     OC_STACK_OK = 0,
185     OC_STACK_RESOURCE_CREATED,
186     OC_STACK_RESOURCE_DELETED,
187     OC_STACK_CONTINUE,
188     /* Success status code - END HERE */
189     /* Error status code - START HERE */
190     OC_STACK_INVALID_URI = 20,
191     OC_STACK_INVALID_QUERY,
192     OC_STACK_INVALID_IP,
193     OC_STACK_INVALID_PORT,
194     OC_STACK_INVALID_CALLBACK,
195     OC_STACK_INVALID_METHOD,
196     OC_STACK_INVALID_PARAM,
197     OC_STACK_INVALID_OBSERVE_PARAM,
198     OC_STACK_NO_MEMORY,
199     OC_STACK_COMM_ERROR,
200     OC_STACK_TIMEOUT,
201     OC_STACK_ADAPTER_NOT_ENABLED,
202     OC_STACK_NOTIMPL,
203     OC_STACK_NO_RESOURCE,               /* resource not found */
204     OC_STACK_RESOURCE_ERROR,            /* ex: not supported method or interface */
205     OC_STACK_SLOW_RESOURCE,
206     OC_STACK_DUPLICATE_REQUEST,
207     OC_STACK_NO_OBSERVERS,              /* resource has no registered observers */
208     OC_STACK_OBSERVER_NOT_FOUND,
209     OC_STACK_VIRTUAL_DO_NOT_HANDLE,
210     OC_STACK_INVALID_OPTION,
211     OC_STACK_MALFORMED_RESPONSE,        /* the remote reply contained malformed data */
212     OC_STACK_PERSISTENT_BUFFER_REQUIRED,
213     OC_STACK_INVALID_REQUEST_HANDLE,
214     OC_STACK_INVALID_DEVICE_INFO,
215     OC_STACK_INVALID_JSON,
216     /* NOTE: Insert all new error codes here!*/
217     #ifdef WITH_PRESENCE
218     OC_STACK_PRESENCE_STOPPED = 128,
219     OC_STACK_PRESENCE_TIMEOUT,
220     OC_STACK_PRESENCE_DO_NOT_HANDLE,
221     #endif
222     OC_STACK_ERROR = 255
223     /* Error status code - END HERE */
224 } OCStackResult;
225
226 /**
227  * Handle to an @ref OCDoResource invocation.
228  */
229 typedef void * OCDoHandle;
230
231 /**
232  * Handle to an OCResource object owned by the OCStack.
233  */
234 typedef void * OCResourceHandle;
235
236 typedef void * OCRequestHandle;
237 typedef void * OCResponseHandle;
238
239 /**
240  * Unique identifier for each observation request. Used when observations are
241  * registered or deregistering. Used by entity handler to signal specific
242  * observers to be notified of resource changes.
243  * There can be maximum of 256 observations per server.
244  */
245 typedef uint8_t OCObservationId;
246
247 /**
248  * Action associated with observation.
249  */
250 typedef enum
251 {
252     OC_OBSERVE_REGISTER = 0,
253     OC_OBSERVE_DEREGISTER = 1,
254     OC_OBSERVE_NO_OPTION = 2
255 } OCObserveAction;
256
257 typedef struct
258 {
259     // Action associated with observation request
260     OCObserveAction action;
261     // Identifier for observation being registered/deregistered
262     OCObservationId obsId;
263 } OCObservationInfo;
264
265 /**
266  * Possible returned values from entity handler.
267  */
268 typedef enum
269 {
270     OC_EH_OK = 0,
271     OC_EH_ERROR,
272     OC_EH_RESOURCE_CREATED,
273     OC_EH_RESOURCE_DELETED,
274     OC_EH_SLOW,
275     OC_EH_FORBIDDEN,
276     OC_EH_RESOURCE_NOT_FOUND
277 } OCEntityHandlerResult;
278
279 /**
280  * This structure will be used to define the vendor specific header options to be included
281  * in communication packets.
282  */
283 typedef struct OCHeaderOption
284 {
285     // The protocol ID this option applies to
286     OCTransportProtocolID protocolID;
287     // The header option ID which will be added to communication packets
288     uint16_t optionID;
289     // its length   191
290     uint16_t optionLength;
291     // pointer to its data
292     uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
293 } OCHeaderOption;
294
295 /**
296  * Incoming requests handled by the server. Requests are passed in as a parameter to the
297  * @ref OCEntityHandler callback API.
298  * @brief The @ref OCEntityHandler callback API must be implemented in the application in order
299  * to receive these requests.
300  */
301 typedef struct
302 {
303     // Associated resource
304     OCResourceHandle resource;
305     OCRequestHandle requestHandle;
306     // the REST method retrieved from received request PDU
307     OCMethod method;
308     // resource query send by client
309     char * query;
310     // Information associated with observation - valid only when OCEntityHandler
311     // flag includes OC_OBSERVE_FLAG
312     OCObservationInfo obsInfo;
313     // An array of the received vendor specific header options
314     uint8_t numRcvdVendorSpecificHeaderOptions;
315     OCHeaderOption * rcvdVendorSpecificHeaderOptions;
316     // reqJSON is retrieved from the payload of the received request PDU
317     char * reqJSONPayload;
318 } OCEntityHandlerRequest;
319
320 /**
321  * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
322  */
323 typedef struct
324 {
325     /// Address of remote server
326     OCDevAddr * addr;
327     /// Indicates adaptor type on which the response was received
328     OCConnectivityType connType;
329     /// the is the result of our stack, OCStackResult should contain coap/other error codes;
330     OCStackResult result;
331     /// If associated with observe, this will represent the sequence of notifications from server.
332     uint32_t sequenceNumber;
333     /// resJSONPayload is retrieved from the payload of the received request PDU
334     const char * resJSONPayload;
335     /// An array of the received vendor specific header options
336     uint8_t numRcvdVendorSpecificHeaderOptions;
337     OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
338 } OCClientResponse;
339
340 /**
341  * This structure describes the device properties. All non-Null properties will be included
342  * in a device discovery request.
343  */
344 typedef struct
345 {
346     char *deviceName;
347     char *hostName;
348     char *deviceUUID;
349     char *contentType;
350     char *version;
351     char *manufacturerName;
352     char *manufacturerUrl;
353     char *modelNumber;
354     char *dateOfManufacture;
355     char *platformVersion;
356     char *firmwareVersion;
357     char *supportUrl;
358 } OCDeviceInfo;
359
360 typedef struct
361 {
362     // Request handle is passed to server via the entity handler for each incoming request.
363     // Stack assigns when request is received, server sets to indicate what request response is for
364     OCRequestHandle requestHandle;
365     // New handle for tracking block (or slow) response.  Stack assigns, server uses for subsequent calls
366     OCResponseHandle  *responseHandle;
367     // Resource handle
368     OCResourceHandle resourceHandle;
369     // Allow the entity handler to pass a result with the response
370     OCEntityHandlerResult  ehResult;
371     // this is the pointer to server payload data to be transferred
372     char *payload;
373     // size of server payload data.  I don't think we should rely on null terminated data for size
374     uint16_t payloadSize;
375     // An array of the vendor specific header options the entity handler wishes to use in response
376     uint8_t numSendVendorSpecificHeaderOptions;
377     OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
378     // URI of new resource that entity handler might create
379     char resourceUri[MAX_URI_LENGTH];
380     // Server sets to true for persistent response buffer, false for non-persistent response buffer
381     uint8_t persistentBufferFlag;
382 } OCEntityHandlerResponse;
383
384 typedef enum
385 {
386     OC_INIT_FLAG    = (1 << 0),
387     OC_REQUEST_FLAG = (1 << 1),
388     OC_OBSERVE_FLAG = (1 << 2)
389 } OCEntityHandlerFlag; //entity_handler_flag_t ;
390
391 /**
392  * Possible returned values from client application.
393  */
394 typedef enum
395 {
396     OC_STACK_DELETE_TRANSACTION = 0,//!< OC_STACK_DELETE_TRANSACTION
397     OC_STACK_KEEP_TRANSACTION       //!< OC_STACK_KEEP_TRANSACTION
398 } OCStackApplicationResult;
399
400 //-----------------------------------------------------------------------------
401 // Callback function definitions
402 //-----------------------------------------------------------------------------
403
404 /**
405  * Client applications implement this callback to consume responses received from Servers.
406  */
407 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
408     OCClientResponse * clientResponse);
409
410 /**
411  * Client applications using a context pointer implement this callback to delete the
412  * context upon removal of the callback/context pointer from the internal callback-list.
413  */
414 typedef void (* OCClientContextDeleter)(void *context);
415
416 /**
417  * This info is passed from application to OC Stack when initiating a request to Server.
418  */
419 typedef struct
420 {
421     void *context;
422     /// The pointer to a function the stack will call to handle the requests
423     OCClientResponseHandler cb;
424     /// A pointer to a function to delete the context when this callback is removed
425     OCClientContextDeleter cd;
426 } OCCallbackData;
427
428 /**
429  * Application server implementations must implement this callback to consume requests OTA.
430  * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
431  */
432 typedef OCEntityHandlerResult (*OCEntityHandler)
433 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest);
434
435 /**
436  * Device Entity handler need to use this call back instead of OCEntityHandler.
437  */
438 typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
439 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri);
440
441 #ifdef __cplusplus
442 }
443 #endif // __cplusplus
444
445 #endif /* OCTYPES_H_ */