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