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