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