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