1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 #include "ocstackconfig.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
35 #define OC_WELL_KNOWN_QUERY "/oic/res"
36 #define OC_MULTICAST_DISCOVERY_URI "/oic/res"
39 #define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
40 /// OC_MAX_PRESENCE_TTL_SECONDS sets the maximum time to live (TTL) for presence.
41 /// NOTE: Changing the setting to a longer duration may lead to unsupported and untested
43 #define OC_MAX_PRESENCE_TTL_SECONDS (60 * 60 * 24) // 60 sec/min * 60 min/hr * 24 hr/day
44 #define OC_PRESENCE_URI "/oic/ad"
47 ///Separtor for multiple query string
48 #define OC_QUERY_SEPARATOR "&;"
51 * Attributes used to form a proper OIC conforming JSON message.
53 #define OC_RSRVD_OC "oic"
54 #define OC_RSRVD_PAYLOAD "payload"
55 #define OC_RSRVD_HREF "href"
56 #define OC_RSRVD_PROPERTY "prop"
57 #define OC_RSRVD_REPRESENTATION "rep"
58 #define OC_RSRVD_CONTENT_TYPE "ct"
59 #define OC_RSRVD_RESOURCE_TYPE "rt"
60 #define OC_RSRVD_RESOURCE_TYPE_PRESENCE "oic.wk.ad"
61 #define OC_RSRVD_INTERFACE "if"
62 #define OC_RSRVD_TTL "ttl"
63 #define OC_RSRVD_NONCE "non"
64 #define OC_RSRVD_TRIGGER "trg"
66 #define OC_RSRVD_INTERFACE_DEFAULT "oic.if.baseline"
67 #define OC_RSRVD_INTERFACE_LL "oic.if.ll"
68 #define OC_RSRVD_INTERFACE_BATCH "oic.if.b"
69 #define OC_RSRVD_INTERFACE_GROUP "oic.mi.grp"
70 #define OC_RSRVD_MFG_DATE "mndt"
71 #define OC_RSRVD_FW_VERSION "mnfv"
72 #define OC_RSRVD_HOST_NAME "hn"
73 #define OC_RSRVD_VERSION "icv"
74 #define OC_RSRVD_POLICY "p"
75 #define OC_RSRVD_BITMAP "bm"
76 #define OC_RSRVD_SECURE "sec"
77 #define OC_RSRVD_HOSTING_PORT "port"
78 #define OC_RSRVD_SERVER_INSTANCE_ID "sid"
80 //**** Presence "Announcement Triggers" ****
81 #define OC_RSRVD_TRIGGER_CREATE "create"
82 #define OC_RSRVD_TRIGGER_CHANGE "change"
83 #define OC_RSRVD_TRIGGER_DELETE "delete"
87 #define OC_RSRVD_PLATFORM_ID "pi"
88 #define OC_RSRVD_MFG_NAME "mnmn"
89 #define OC_RSRVD_MFG_URL "mnml"
90 #define OC_RSRVD_MODEL_NUM "mnmo"
91 #define OC_RSRVD_MFG_DATE "mndt"
92 #define OC_RSRVD_PLATFORM_VERSION "mnpv"
93 #define OC_RSRVD_OS_VERSION "mnos"
94 #define OC_RSRVD_HARDWARE_VERSION "mnhw"
95 #define OC_RSRVD_FIRMWARE_VERSION "mnfv"
96 #define OC_RSRVD_SUPPORT_URL "mnsl"
97 #define OC_RSRVD_SYSTEM_TIME "st"
101 #define OC_RSRVD_DEVICE_ID "di"
102 #define OC_RSRVD_DEVICE_NAME "n"
103 #define OC_RSRVD_SPEC_VERSION "lcv"
104 #define OC_RSRVD_DATA_MODEL_VERSION "dmv"
106 #define OC_SPEC_VERSION "0.9.0"
107 #define OC_DATA_MODEL_VERSION "sec.0.95"
108 //*******************
110 // These provide backward compatibility - their use is deprecated
112 #define OC_MULTICAST_PREFIX "224.0.1.187:5683"
113 #define OC_MULTICAST_IP "224.0.1.187:5683"
114 #define OC_MULTICAST_PORT 5683
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 #define MAX_ADDR_STR_SIZE (40)
122 #define MAX_IDENTITY_SIZE (32)
125 * These enums (OCTransportAdapter and OCTransportFlags) must
126 * be kept synchronized with OCConnectivityType (below) as well as
127 * CATransportAdapter and CATransportFlags (in CACommon.h).
132 OC_DEFAULT_ADAPTER = 0,
134 // value zero indicates discovery
135 OC_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
136 OC_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
137 OC_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
138 } OCTransportAdapter;
140 // enum layout assumes some targets have 16-bit integer (e.g., Arduino)
143 OC_DEFAULT_FLAGS = 0,
145 // Insecure transport is the default (subject to change)
146 OC_FLAG_SECURE = (1 << 4), // secure the transport path
148 // IPv4 & IPv6 autoselection is the default
149 OC_IP_USE_V6 = (1 << 5), // IP adapter only
150 OC_IP_USE_V4 = (1 << 6), // IP adapter only
152 // Link-Local multicast is the default multicast scope for IPv6.
153 // These are placed here to correspond to the IPv6 multicast address bits.
154 OC_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope (loopback)
155 OC_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
156 OC_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
157 OC_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
158 OC_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
159 OC_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
160 OC_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
163 #define OC_MASK_SCOPE (0x000F)
164 #define OC_MASK_MODS (0x0FF0)
172 unsigned char id[MAX_IDENTITY_SIZE];
176 * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
178 * OCDevAddr must be the same as CAEndpoint (in CACommon.h).
182 OCTransportAdapter adapter; // adapter type
183 OCTransportFlags flags; // transport modifiers
184 char addr[MAX_ADDR_STR_SIZE]; // address for all adapters
185 uint32_t interface; // usually zero for default interface
186 uint16_t port; // for IP
187 OCIdentity identity; // secure node identity
191 * OCConnectivityType includes elements of both OCTransportAdapter
192 * and OCTransportFlags. It is defined conditionally because the
193 * smaller definition limits expandability on 32/64 bit integer machines,
194 * and the larger definition won't fit into an enum on 16-bit integer
195 * machines like Arduino.
197 * This structure must directly correspond to OCTransportAdapter
198 * and OCTransportFlags.
202 CT_DEFAULT = 0, // use when defaults are ok
204 #if defined (__UINT32_MAX__) && (__UINT32_MAX__ == 65535) // 16-bit int
205 CT_ADAPTER_IP = (1 << 10), // IPv4 and IPv6, including 6LoWPAN
206 CT_ADAPTER_GATT_BTLE = (1 << 11), // GATT over Bluetooth LE
207 CT_ADAPTER_RFCOMM_BTEDR = (1 << 12), // RFCOMM over Bluetooth EDR
208 #define CT_ADAPTER_SHIFT 10
209 #define CT_MASK_FLAGS 0x03FF
210 #define CT_MASK_ADAPTER 0xFC00
211 #else // assume 32-bit int
212 CT_ADAPTER_IP = (1 << 16), // IPv4 and IPv6, including 6LoWPAN
213 CT_ADAPTER_GATT_BTLE = (1 << 17), // GATT over Bluetooth LE
214 CT_ADAPTER_RFCOMM_BTEDR = (1 << 18), // RFCOMM over Bluetooth EDR
215 #define CT_ADAPTER_SHIFT 16
216 #define CT_MASK_FLAGS 0xFFFF
217 #define CT_MASK_ADAPTER 0xFFFF0000
220 // Insecure transport is the default (subject to change)
221 CT_FLAG_SECURE = (1 << 4), // secure the transport path
223 // IPv4 & IPv6 autoselection is the default
224 CT_IP_USE_V6 = (1 << 5), // IP adapter only
225 CT_IP_USE_V4 = (1 << 6), // IP adapter only
227 // Link-Local multicast is the default multicast scope for IPv6.
228 // These are placed here to correspond to the IPv6 address bits.
229 CT_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope (loopback)
230 CT_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
231 CT_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
232 CT_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
233 CT_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
234 CT_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
235 CT_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
236 } OCConnectivityType;
239 * OC Virtual resources supported by every OC device.
243 OC_WELL_KNOWN_URI= 0, ///< "/oic/res"
244 OC_DEVICE_URI, ///< "/oic/d"
245 OC_PLATFORM_URI, ///< "/oic/p"
246 OC_RESOURCE_TYPES_URI, ///< "/oic/res/d/type"
248 OC_PRESENCE, ///< "/oic/ad"
250 OC_MAX_VIRTUAL_RESOURCES ///<s Max items in the list
251 } OCVirtualResources;
254 * OCDoResource methods
258 OC_REST_NOMETHOD = 0,
259 OC_REST_GET = (1 << 0), ///< Read
260 OC_REST_PUT = (1 << 1), ///< Write
261 OC_REST_POST = (1 << 2), ///< Update
262 OC_REST_DELETE = (1 << 3), ///< Delete
263 /// Register observe request for most up date notifications ONLY.
264 OC_REST_OBSERVE = (1 << 4),
265 /// Register observe request for all notifications, including stale notifications.
266 OC_REST_OBSERVE_ALL = (1 << 5),
267 /// Deregister observation, intended for internal use
268 OC_REST_CANCEL_OBSERVE = (1 << 6),
270 /// Subscribe for all presence notifications of a particular resource.
271 OC_REST_PRESENCE = (1 << 7),
273 /// Allows OCDoResource caller to do discovery.
274 OC_REST_DISCOVER = (1 << 8)
278 * Host Mode of Operation.
288 * Quality of Service.
290 * OCQualityOfService attempts to abstract the guarantees provided by the underlying transport
291 * protocol. The precise definitions of each quality of service level depend on the
292 * implementation. In descriptions below are for the current implementation and may changed
297 /// Packet delivery is best effort
299 /// Packet delivery is best effort
301 /// Acknowledgements are used to confirm delivery
303 /// No Quality is defined, let the stack decide
305 } OCQualityOfService;
308 * Resource Properties.
309 * The value of a policy property is defined as bitmap.
310 * The LSB represents OC_DISCOVERABLE and Second LSB bit represents OC_OBSERVABLE and so on.
311 * Not including the policy property is equivalent to zero.
313 * ::OC_RES_PROP_NONE When none of the bits are set, the resource is non-discoverable &
314 * non-observable by the client.
315 * ::OC_DISCOVERABLE When this bit is set, the resource is allowed to be discovered by clients.
316 * ::OC_OBSERVABLE When this bit is set, the resource is allowed to be observed by clients.
317 * ::OC_ACTIVE When this bit is set, the resource is initialized, otherwise the resource
318 * is 'inactive'. 'inactive' signifies that the resource has been marked for
319 * deletion or is already deleted.
320 * ::OC_SLOW When this bit is set, the resource has been marked as 'slow'. 'slow'
321 * signifies that responses from this resource can expect delays in
322 * processing its requests from clients.
323 * ::OC_SECURE When this bit is set, the resource is a secure resource.
327 OC_RES_PROP_NONE = (0),
328 OC_DISCOVERABLE = (1 << 0),
329 OC_OBSERVABLE = (1 << 1),
330 OC_ACTIVE = (1 << 2),
333 } OCResourceProperty;
336 * Transport Protocol IDs.
340 OC_INVALID_ID = (1 << 0),
341 OC_COAP_ID = (1 << 1)
342 } OCTransportProtocolID;
345 * Declares Stack Results & Errors.
349 /* Success status code - START HERE */
351 OC_STACK_RESOURCE_CREATED,
352 OC_STACK_RESOURCE_DELETED,
354 /* Success status code - END HERE */
355 /* Error status code - START HERE */
356 OC_STACK_INVALID_URI = 20,
357 OC_STACK_INVALID_QUERY,
359 OC_STACK_INVALID_PORT,
360 OC_STACK_INVALID_CALLBACK,
361 OC_STACK_INVALID_METHOD,
362 OC_STACK_INVALID_PARAM,
363 OC_STACK_INVALID_OBSERVE_PARAM,
367 OC_STACK_ADAPTER_NOT_ENABLED,
369 OC_STACK_NO_RESOURCE, /* resource not found */
370 OC_STACK_RESOURCE_ERROR, /* ex: not supported method or interface */
371 OC_STACK_SLOW_RESOURCE,
372 OC_STACK_DUPLICATE_REQUEST,
373 OC_STACK_NO_OBSERVERS, /* resource has no registered observers */
374 OC_STACK_OBSERVER_NOT_FOUND,
375 OC_STACK_VIRTUAL_DO_NOT_HANDLE,
376 OC_STACK_INVALID_OPTION,
377 OC_STACK_MALFORMED_RESPONSE, /* the remote reply contained malformed data */
378 OC_STACK_PERSISTENT_BUFFER_REQUIRED,
379 OC_STACK_INVALID_REQUEST_HANDLE,
380 OC_STACK_INVALID_DEVICE_INFO,
381 OC_STACK_INVALID_JSON,
382 /* NOTE: Insert all new error codes here!*/
384 OC_STACK_PRESENCE_STOPPED = 128,
385 OC_STACK_PRESENCE_TIMEOUT,
386 OC_STACK_PRESENCE_DO_NOT_HANDLE,
389 /* Error status code - END HERE */
393 * Handle to an @ref OCDoResource invocation.
395 typedef void * OCDoHandle;
398 * Handle to an OCResource object owned by the OCStack.
400 typedef void * OCResourceHandle;
402 typedef void * OCRequestHandle;
405 * Unique identifier for each observation request. Used when observations are
406 * registered or deregistering. Used by entity handler to signal specific
407 * observers to be notified of resource changes.
408 * There can be maximum of 256 observations per server.
410 typedef uint8_t OCObservationId;
413 * Action associated with observation.
417 OC_OBSERVE_REGISTER = 0,
418 OC_OBSERVE_DEREGISTER = 1,
419 OC_OBSERVE_NO_OPTION = 2
424 * Persistent storage handlers. An app must provide OCPersistentStorage handler pointers when it
425 * calls OCRegisterPersistentStorageHandler.
429 * Persistent storage open handler points to default file path.
430 * Application can point to appropriate SVR database path for its Iotivity Server.
432 FILE* (* open)(const char *path, const char *mode);
433 // Persistent storage read handler
434 size_t (* read)(void *ptr, size_t size, size_t nmemb, FILE *stream);
435 // Persistent storage write handler
436 size_t (* write)(const void *ptr, size_t size, size_t nmemb, FILE *stream);
437 // Persistent storage close handler
438 int (* close)(FILE *fp);
439 // Persistent storage unlink handler
440 int (* unlink)(const char *path);
441 } OCPersistentStorage;
445 // Action associated with observation request
446 OCObserveAction action;
447 // Identifier for observation being registered/deregistered
448 OCObservationId obsId;
452 * Possible returned values from entity handler.
458 OC_EH_RESOURCE_CREATED,
459 OC_EH_RESOURCE_DELETED,
462 OC_EH_RESOURCE_NOT_FOUND
463 } OCEntityHandlerResult;
466 * This structure will be used to define the vendor specific header options to be included
467 * in communication packets.
469 typedef struct OCHeaderOption
471 // The protocol ID this option applies to
472 OCTransportProtocolID protocolID;
473 // The header option ID which will be added to communication packets
476 uint16_t optionLength;
477 // pointer to its data
478 uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
481 OCHeaderOption() = default;
482 OCHeaderOption(OCTransportProtocolID pid,
485 const uint8_t* optData)
491 // parameter includes the null terminator.
492 optionLength = optionLength < MAX_HEADER_OPTION_DATA_LENGTH ?
493 optionLength : MAX_HEADER_OPTION_DATA_LENGTH;
494 memcpy(optionData, optData, optionLength);
495 optionData[optionLength - 1] = '\0';
501 * Incoming requests handled by the server. Requests are passed in as a parameter to the
502 * @ref OCEntityHandler callback API.
503 * @brief The @ref OCEntityHandler callback API must be implemented in the application in order
504 * to receive these requests.
508 // Associated resource
509 OCResourceHandle resource;
510 OCRequestHandle requestHandle;
511 // the REST method retrieved from received request PDU
513 // resource query send by client
515 // Information associated with observation - valid only when OCEntityHandler
516 // flag includes OC_OBSERVE_FLAG
517 OCObservationInfo obsInfo;
518 // An array of the received vendor specific header options
519 uint8_t numRcvdVendorSpecificHeaderOptions;
520 OCHeaderOption * rcvdVendorSpecificHeaderOptions;
521 // reqJSON is retrieved from the payload of the received request PDU
522 char * reqJSONPayload;
523 } OCEntityHandlerRequest;
526 * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
530 /// Address of remote server
532 OCDevAddr *addr; // backward compatibility (points to devAddr)
533 OCConnectivityType connType; // backward compatibility
534 /// the is the result of our stack, OCStackResult should contain coap/other error codes;
535 OCStackResult result;
536 /// If associated with observe, this will represent the sequence of notifications from server.
537 uint32_t sequenceNumber;
539 const char * resourceUri;
540 /// resJSONPayload is retrieved from the payload of the received request PDU
541 const char * resJSONPayload;
542 /// An array of the received vendor specific header options
543 uint8_t numRcvdVendorSpecificHeaderOptions;
544 OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
548 * This structure describes the platform properties. All non-Null properties will be included
549 * in a platform discovery request.
554 char *manufacturerName;
555 char *manufacturerUrl;
557 char *dateOfManufacture;
558 char *platformVersion;
559 char *operatingSystemVersion;
560 char *hardwareVersion;
561 char *firmwareVersion;
568 * This structure is expected as input for device properties.
569 * device name is mandatory and expected from the application.
570 * device id of type UUID will be generated by the stack.
580 // Request handle is passed to server via the entity handler for each incoming request.
581 // Stack assigns when request is received, server sets to indicate what request response is for
582 OCRequestHandle requestHandle;
584 OCResourceHandle resourceHandle;
585 // Allow the entity handler to pass a result with the response
586 OCEntityHandlerResult ehResult;
587 // this is the pointer to server payload data to be transferred
589 // size of server payload data. I don't think we should rely on null terminated data for size
590 uint16_t payloadSize;
591 // An array of the vendor specific header options the entity handler wishes to use in response
592 uint8_t numSendVendorSpecificHeaderOptions;
593 OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
594 // URI of new resource that entity handler might create
595 char resourceUri[MAX_URI_LENGTH];
596 // Server sets to true for persistent response buffer, false for non-persistent response buffer
597 uint8_t persistentBufferFlag;
598 } OCEntityHandlerResponse;
602 OC_REQUEST_FLAG = (1 << 1),
603 OC_OBSERVE_FLAG = (1 << 2)
604 } OCEntityHandlerFlag; //entity_handler_flag_t ;
607 * Possible returned values from client application.
611 OC_STACK_DELETE_TRANSACTION = 0,//!< OC_STACK_DELETE_TRANSACTION
612 OC_STACK_KEEP_TRANSACTION //!< OC_STACK_KEEP_TRANSACTION
613 } OCStackApplicationResult;
615 //-----------------------------------------------------------------------------
616 // Callback function definitions
617 //-----------------------------------------------------------------------------
620 * Client applications implement this callback to consume responses received from Servers.
622 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
623 OCClientResponse * clientResponse);
626 * Client applications using a context pointer implement this callback to delete the
627 * context upon removal of the callback/context pointer from the internal callback-list.
629 typedef void (* OCClientContextDeleter)(void *context);
632 * This info is passed from application to OC Stack when initiating a request to Server.
634 typedef struct OCCallbackData
637 /// The pointer to a function the stack will call to handle the requests
638 OCClientResponseHandler cb;
639 /// A pointer to a function to delete the context when this callback is removed
640 OCClientContextDeleter cd;
642 OCCallbackData() = default;
643 OCCallbackData(void* ctx, OCClientResponseHandler callback, OCClientContextDeleter deleter)
644 :context(ctx), cb(callback), cd(deleter){}
649 * Application server implementations must implement this callback to consume requests OTA.
650 * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
652 typedef OCEntityHandlerResult (*OCEntityHandler)
653 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void* callbackParam);
656 * Device Entity handler need to use this call back instead of OCEntityHandler.
658 typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
659 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri, void* callbackParam);
663 #endif // __cplusplus
665 #endif /* OCTYPES_H_ */