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"
34 #include "ocpresence.h"
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
40 * OIC Virtual resources supported by every OIC device.
42 #define OC_RSRVD_WELL_KNOWN_URI "/oic/res"
43 #define OC_RSRVD_DEVICE_URI "/oic/d"
44 #define OC_RSRVD_PLATFORM_URI "/oic/p"
45 #define OC_RSRVD_RESOURCE_TYPES_URI "/oic/res/types/d"
47 #define OC_RSRVD_PRESENCE_URI "/oic/ad"
48 #define OC_DEFAULT_PRESENCE_TTL_SECONDS (60)
49 /// OC_MAX_PRESENCE_TTL_SECONDS sets the maximum time to live (TTL) for presence.
50 /// NOTE: Changing the setting to a longer duration may lead to unsupported and untested
52 #define OC_MAX_PRESENCE_TTL_SECONDS (60 * 60 * 24) // 60 sec/min * 60 min/hr * 24 hr/day
55 ///Separtor for multiple query string
56 #define OC_QUERY_SEPARATOR "&;"
59 * Attributes used to form a proper OIC conforming JSON message.
61 //#define OC_RSRVD_OC "oic"
62 #define OC_RSRVD_PAYLOAD "payload"
63 #define OC_RSRVD_HREF "href"
64 #define OC_RSRVD_PROPERTY "prop"
65 #define OC_RSRVD_REPRESENTATION "rep"
66 #define OC_RSRVD_CONTENT_TYPE "ct"
67 #define OC_RSRVD_RESOURCE_TYPE "rt"
68 #define OC_RSRVD_RESOURCE_TYPE_PRESENCE "oic.wk.ad"
69 #define OC_RSRVD_INTERFACE "if"
70 #define OC_RSRVD_TTL "ttl"
71 #define OC_RSRVD_NONCE "non"
72 #define OC_RSRVD_TRIGGER "trg"
73 #define OC_RSRVD_LINKS "links"
75 #define OC_RSRVD_INTERFACE_DEFAULT "oic.if.baseline"
76 #define OC_RSRVD_INTERFACE_LL "oic.if.ll"
77 #define OC_RSRVD_INTERFACE_BATCH "oic.if.b"
78 #define OC_RSRVD_INTERFACE_GROUP "oic.mi.grp"
79 #define OC_RSRVD_MFG_DATE "mndt"
80 #define OC_RSRVD_FW_VERSION "mnfv"
81 #define OC_RSRVD_HOST_NAME "hn"
82 #define OC_RSRVD_VERSION "icv"
83 #define OC_RSRVD_POLICY "p"
84 #define OC_RSRVD_BITMAP "bm"
85 #define OC_RSRVD_SECURE "sec"
86 #define OC_RSRVD_HOSTING_PORT "port"
87 #define OC_RSRVD_SERVER_INSTANCE_ID "sid"
89 //**** Presence "Announcement Triggers" ****
90 #define OC_RSRVD_TRIGGER_CREATE "create"
91 #define OC_RSRVD_TRIGGER_CHANGE "change"
92 #define OC_RSRVD_TRIGGER_DELETE "delete"
96 #define OC_RSRVD_PLATFORM_ID "pi"
97 #define OC_RSRVD_MFG_NAME "mnmn"
98 #define OC_RSRVD_MFG_URL "mnml"
99 #define OC_RSRVD_MODEL_NUM "mnmo"
100 #define OC_RSRVD_MFG_DATE "mndt"
101 #define OC_RSRVD_PLATFORM_VERSION "mnpv"
102 #define OC_RSRVD_OS_VERSION "mnos"
103 #define OC_RSRVD_HARDWARE_VERSION "mnhw"
104 #define OC_RSRVD_FIRMWARE_VERSION "mnfv"
105 #define OC_RSRVD_SUPPORT_URL "mnsl"
106 #define OC_RSRVD_SYSTEM_TIME "st"
107 //*******************
110 #define OC_RSRVD_DEVICE_ID "di"
111 #define OC_RSRVD_DEVICE_NAME "n"
112 #define OC_RSRVD_SPEC_VERSION "lcv"
113 #define OC_RSRVD_DATA_MODEL_VERSION "dmv"
115 #define OC_SPEC_VERSION "0.9.0"
116 #define OC_DATA_MODEL_VERSION "sec.0.95"
117 //*******************
119 // These provide backward compatibility - their use is deprecated
121 #define OC_MULTICAST_PREFIX "224.0.1.187:5683"
122 #define OC_MULTICAST_IP "224.0.1.187"
123 #define OC_MULTICAST_PORT 5683
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 #define MAX_ADDR_STR_SIZE (40)
131 #define MAX_IDENTITY_SIZE (32)
134 * These enums (OCTransportAdapter and OCTransportFlags) must
135 * be kept synchronized with OCConnectivityType (below) as well as
136 * CATransportAdapter and CATransportFlags (in CACommon.h).
141 OC_DEFAULT_ADAPTER = 0,
143 // value zero indicates discovery
144 OC_ADAPTER_IP = (1 << 0), // IPv4 and IPv6, including 6LoWPAN
145 OC_ADAPTER_GATT_BTLE = (1 << 1), // GATT over Bluetooth LE
146 OC_ADAPTER_RFCOMM_BTEDR = (1 << 2), // RFCOMM over Bluetooth EDR
147 } OCTransportAdapter;
149 // enum layout assumes some targets have 16-bit integer (e.g., Arduino)
152 OC_DEFAULT_FLAGS = 0,
154 // Insecure transport is the default (subject to change)
155 OC_FLAG_SECURE = (1 << 4), // secure the transport path
157 // IPv4 & IPv6 autoselection is the default
158 OC_IP_USE_V6 = (1 << 5), // IP adapter only
159 OC_IP_USE_V4 = (1 << 6), // IP adapter only
161 // Link-Local multicast is the default multicast scope for IPv6.
162 // These are placed here to correspond to the IPv6 multicast address bits.
163 OC_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope (loopback)
164 OC_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
165 OC_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
166 OC_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
167 OC_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
168 OC_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
169 OC_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
172 #define OC_MASK_SCOPE (0x000F)
173 #define OC_MASK_MODS (0x0FF0)
181 unsigned char id[MAX_IDENTITY_SIZE];
185 * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
187 * OCDevAddr must be the same as CAEndpoint (in CACommon.h).
191 OCTransportAdapter adapter; // adapter type
192 OCTransportFlags flags; // transport modifiers
193 char addr[MAX_ADDR_STR_SIZE]; // address for all adapters
194 uint32_t interface; // usually zero for default interface
195 uint16_t port; // for IP
196 OCIdentity identity; // secure node identity
200 * OCConnectivityType includes elements of both OCTransportAdapter
201 * and OCTransportFlags. It is defined conditionally because the
202 * smaller definition limits expandability on 32/64 bit integer machines,
203 * and the larger definition won't fit into an enum on 16-bit integer
204 * machines like Arduino.
206 * This structure must directly correspond to OCTransportAdapter
207 * and OCTransportFlags.
211 CT_DEFAULT = 0, // use when defaults are ok
213 #if defined (__UINT32_MAX__) && (__UINT32_MAX__ == 65535) // 16-bit int
214 CT_ADAPTER_IP = (1 << 10), // IPv4 and IPv6, including 6LoWPAN
215 CT_ADAPTER_GATT_BTLE = (1 << 11), // GATT over Bluetooth LE
216 CT_ADAPTER_RFCOMM_BTEDR = (1 << 12), // RFCOMM over Bluetooth EDR
217 #define CT_ADAPTER_SHIFT 10
218 #define CT_MASK_FLAGS 0x03FF
219 #define CT_MASK_ADAPTER 0xFC00
220 #else // assume 32-bit int
221 CT_ADAPTER_IP = (1 << 16), // IPv4 and IPv6, including 6LoWPAN
222 CT_ADAPTER_GATT_BTLE = (1 << 17), // GATT over Bluetooth LE
223 CT_ADAPTER_RFCOMM_BTEDR = (1 << 18), // RFCOMM over Bluetooth EDR
224 #define CT_ADAPTER_SHIFT 16
225 #define CT_MASK_FLAGS 0xFFFF
226 #define CT_MASK_ADAPTER 0xFFFF0000
229 // Insecure transport is the default (subject to change)
230 CT_FLAG_SECURE = (1 << 4), // secure the transport path
232 // IPv4 & IPv6 autoselection is the default
233 CT_IP_USE_V6 = (1 << 5), // IP adapter only
234 CT_IP_USE_V4 = (1 << 6), // IP adapter only
236 // Link-Local multicast is the default multicast scope for IPv6.
237 // These are placed here to correspond to the IPv6 address bits.
238 CT_SCOPE_INTERFACE = 0x1, // IPv6 Interface-Local scope (loopback)
239 CT_SCOPE_LINK = 0x2, // IPv6 Link-Local scope (default)
240 CT_SCOPE_REALM = 0x3, // IPv6 Realm-Local scope
241 CT_SCOPE_ADMIN = 0x4, // IPv6 Admin-Local scope
242 CT_SCOPE_SITE = 0x5, // IPv6 Site-Local scope
243 CT_SCOPE_ORG = 0x8, // IPv6 Organization-Local scope
244 CT_SCOPE_GLOBAL = 0xE, // IPv6 Global scope
245 } OCConnectivityType;
248 * OCDoResource methods
252 OC_REST_NOMETHOD = 0,
253 OC_REST_GET = (1 << 0), ///< Read
254 OC_REST_PUT = (1 << 1), ///< Write
255 OC_REST_POST = (1 << 2), ///< Update
256 OC_REST_DELETE = (1 << 3), ///< Delete
257 /// Register observe request for most up date notifications ONLY.
258 OC_REST_OBSERVE = (1 << 4),
259 /// Register observe request for all notifications, including stale notifications.
260 OC_REST_OBSERVE_ALL = (1 << 5),
261 /// Deregister observation, intended for internal use
262 OC_REST_CANCEL_OBSERVE = (1 << 6),
264 /// Subscribe for all presence notifications of a particular resource.
265 OC_REST_PRESENCE = (1 << 7),
267 /// Allows OCDoResource caller to do discovery.
268 OC_REST_DISCOVER = (1 << 8)
272 * Host Mode of Operation.
282 * Quality of Service.
284 * OCQualityOfService attempts to abstract the guarantees provided by the underlying transport
285 * protocol. The precise definitions of each quality of service level depend on the
286 * implementation. In descriptions below are for the current implementation and may changed
291 /// Packet delivery is best effort
293 /// Packet delivery is best effort
295 /// Acknowledgements are used to confirm delivery
297 /// No Quality is defined, let the stack decide
299 } OCQualityOfService;
302 * Resource Properties.
303 * The value of a policy property is defined as bitmap.
304 * The LSB represents OC_DISCOVERABLE and Second LSB bit represents OC_OBSERVABLE and so on.
305 * Not including the policy property is equivalent to zero.
307 * ::OC_RES_PROP_NONE When none of the bits are set, the resource is non-discoverable &
308 * non-observable by the client.
309 * ::OC_DISCOVERABLE When this bit is set, the resource is allowed to be discovered by clients.
310 * ::OC_OBSERVABLE When this bit is set, the resource is allowed to be observed by clients.
311 * ::OC_ACTIVE When this bit is set, the resource is initialized, otherwise the resource
312 * is 'inactive'. 'inactive' signifies that the resource has been marked for
313 * deletion or is already deleted.
314 * ::OC_SLOW When this bit is set, the resource has been marked as 'slow'. 'slow'
315 * signifies that responses from this resource can expect delays in
316 * processing its requests from clients.
317 * ::OC_SECURE When this bit is set, the resource is a secure resource.
321 OC_RES_PROP_NONE = (0),
322 OC_DISCOVERABLE = (1 << 0),
323 OC_OBSERVABLE = (1 << 1),
324 OC_ACTIVE = (1 << 2),
327 } OCResourceProperty;
330 * Transport Protocol IDs.
334 OC_INVALID_ID = (1 << 0),
335 OC_COAP_ID = (1 << 1)
336 } OCTransportProtocolID;
339 * Declares Stack Results & Errors.
343 /* Success status code - START HERE */
345 OC_STACK_RESOURCE_CREATED,
346 OC_STACK_RESOURCE_DELETED,
348 /* Success status code - END HERE */
349 /* Error status code - START HERE */
350 OC_STACK_INVALID_URI = 20,
351 OC_STACK_INVALID_QUERY,
353 OC_STACK_INVALID_PORT,
354 OC_STACK_INVALID_CALLBACK,
355 OC_STACK_INVALID_METHOD,
356 OC_STACK_INVALID_PARAM,
357 OC_STACK_INVALID_OBSERVE_PARAM,
361 OC_STACK_ADAPTER_NOT_ENABLED,
363 OC_STACK_NO_RESOURCE, /* resource not found */
364 OC_STACK_RESOURCE_ERROR, /* ex: not supported method or interface */
365 OC_STACK_SLOW_RESOURCE,
366 OC_STACK_DUPLICATE_REQUEST,
367 OC_STACK_NO_OBSERVERS, /* resource has no registered observers */
368 OC_STACK_OBSERVER_NOT_FOUND,
369 OC_STACK_VIRTUAL_DO_NOT_HANDLE,
370 OC_STACK_INVALID_OPTION,
371 OC_STACK_MALFORMED_RESPONSE, /* the remote reply contained malformed data */
372 OC_STACK_PERSISTENT_BUFFER_REQUIRED,
373 OC_STACK_INVALID_REQUEST_HANDLE,
374 OC_STACK_INVALID_DEVICE_INFO,
375 OC_STACK_INVALID_JSON,
376 /* NOTE: Insert all new error codes here!*/
378 OC_STACK_PRESENCE_STOPPED = 128,
379 OC_STACK_PRESENCE_TIMEOUT,
380 OC_STACK_PRESENCE_DO_NOT_HANDLE,
383 /* Error status code - END HERE */
387 * Handle to an @ref OCDoResource invocation.
389 typedef void * OCDoHandle;
392 * Handle to an OCResource object owned by the OCStack.
394 typedef void * OCResourceHandle;
396 typedef void * OCRequestHandle;
399 * Unique identifier for each observation request. Used when observations are
400 * registered or deregistering. Used by entity handler to signal specific
401 * observers to be notified of resource changes.
402 * There can be maximum of 256 observations per server.
404 typedef uint8_t OCObservationId;
407 * Action associated with observation.
411 OC_OBSERVE_REGISTER = 0,
412 OC_OBSERVE_DEREGISTER = 1,
413 OC_OBSERVE_NO_OPTION = 2
418 * Persistent storage handlers. An app must provide OCPersistentStorage handler pointers when it
419 * calls OCRegisterPersistentStorageHandler.
423 * Persistent storage open handler points to default file path.
424 * Application can point to appropriate SVR database path for its Iotivity Server.
426 FILE* (* open)(const char *path, const char *mode);
427 // Persistent storage read handler
428 size_t (* read)(void *ptr, size_t size, size_t nmemb, FILE *stream);
429 // Persistent storage write handler
430 size_t (* write)(const void *ptr, size_t size, size_t nmemb, FILE *stream);
431 // Persistent storage close handler
432 int (* close)(FILE *fp);
433 // Persistent storage unlink handler
434 int (* unlink)(const char *path);
435 } OCPersistentStorage;
439 // Action associated with observation request
440 OCObserveAction action;
441 // Identifier for observation being registered/deregistered
442 OCObservationId obsId;
446 * Possible returned values from entity handler.
452 OC_EH_RESOURCE_CREATED,
453 OC_EH_RESOURCE_DELETED,
456 OC_EH_RESOURCE_NOT_FOUND
457 } OCEntityHandlerResult;
460 * This structure will be used to define the vendor specific header options to be included
461 * in communication packets.
463 typedef struct OCHeaderOption
465 // The protocol ID this option applies to
466 OCTransportProtocolID protocolID;
467 // The header option ID which will be added to communication packets
470 uint16_t optionLength;
471 // pointer to its data
472 uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
475 OCHeaderOption() = default;
476 OCHeaderOption(OCTransportProtocolID pid,
479 const uint8_t* optData)
485 // parameter includes the null terminator.
486 optionLength = optionLength < MAX_HEADER_OPTION_DATA_LENGTH ?
487 optionLength : MAX_HEADER_OPTION_DATA_LENGTH;
488 memcpy(optionData, optData, optionLength);
489 optionData[optionLength - 1] = '\0';
495 * This structure describes the platform properties. All non-Null properties will be included
496 * in a platform discovery request.
501 char *manufacturerName;
502 char *manufacturerUrl;
504 char *dateOfManufacture;
505 char *platformVersion;
506 char *operatingSystemVersion;
507 char *hardwareVersion;
508 char *firmwareVersion;
515 * This structure is expected as input for device properties.
516 * device name is mandatory and expected from the application.
517 * device id of type UUID will be generated by the stack.
525 // Enum to describe the type of object held by the OCPayload object
528 PAYLOAD_TYPE_INVALID,
529 PAYLOAD_TYPE_DISCOVERY,
531 PAYLOAD_TYPE_PLATFORM,
532 PAYLOAD_TYPE_REPRESENTATION,
533 PAYLOAD_TYPE_SECURITY,
534 PAYLOAD_TYPE_PRESENCE
539 // The type of message that was received
552 }OCRepPayloadPropType;
554 #define MAX_REP_ARRAY_DEPTH 3
557 OCRepPayloadPropType type;
558 size_t dimensions[MAX_REP_ARRAY_DEPTH];
566 struct OCRepPayload** objArray;
568 } OCRepPayloadValueArray;
570 typedef struct OCRepPayloadValue
573 OCRepPayloadPropType type;
580 struct OCRepPayload* obj;
581 OCRepPayloadValueArray arr;
583 struct OCRepPayloadValue* next;
587 typedef struct OCStringLL
589 struct OCStringLL *next;
593 // used for get/set/put/observe/etc representations
594 typedef struct OCRepPayload
599 OCStringLL* interfaces;
600 OCRepPayloadValue* values;
601 struct OCRepPayload* next;
604 // used inside a discovery payload
605 typedef struct OCResourcePayload
610 OCStringLL* interfaces;
614 struct OCResourcePayload* next;
620 OCResourcePayload* resources;
621 } OCDiscoveryPayload;
630 char* dataModelVersion;
649 uint32_t sequenceNumber;
651 OCPresenceTrigger trigger;
657 * Incoming requests handled by the server. Requests are passed in as a parameter to the
658 * @ref OCEntityHandler callback API.
659 * @brief The @ref OCEntityHandler callback API must be implemented in the application in order
660 * to receive these requests.
664 // Associated resource
665 OCResourceHandle resource;
666 OCRequestHandle requestHandle;
667 // the REST method retrieved from received request PDU
669 // resource query send by client
671 // Information associated with observation - valid only when OCEntityHandler
672 // flag includes OC_OBSERVE_FLAG
673 OCObservationInfo obsInfo;
674 // An array of the received vendor specific header options
675 uint8_t numRcvdVendorSpecificHeaderOptions;
676 OCHeaderOption * rcvdVendorSpecificHeaderOptions;
677 // the payload from the request PDU
679 } OCEntityHandlerRequest;
682 * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
686 /// Address of remote server
688 OCDevAddr *addr; // backward compatibility (points to devAddr)
689 OCConnectivityType connType; // backward compatibility
690 /// the is the result of our stack, OCStackResult should contain coap/other error codes;
691 OCStackResult result;
692 /// If associated with observe, this will represent the sequence of notifications from server.
693 uint32_t sequenceNumber;
695 const char * resourceUri;
696 // the payload for the response PDU
698 /// An array of the received vendor specific header options
699 uint8_t numRcvdVendorSpecificHeaderOptions;
700 OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
705 // Request handle is passed to server via the entity handler for each incoming request.
706 // Stack assigns when request is received, server sets to indicate what request response is for
707 OCRequestHandle requestHandle;
709 OCResourceHandle resourceHandle;
710 // Allow the entity handler to pass a result with the response
711 OCEntityHandlerResult ehResult;
712 // this is the pointer to server payload data to be transferred
714 // An array of the vendor specific header options the entity handler wishes to use in response
715 uint8_t numSendVendorSpecificHeaderOptions;
716 OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
717 // URI of new resource that entity handler might create
718 char resourceUri[MAX_URI_LENGTH];
719 // Server sets to true for persistent response buffer, false for non-persistent response buffer
720 uint8_t persistentBufferFlag;
721 } OCEntityHandlerResponse;
725 OC_REQUEST_FLAG = (1 << 1),
726 OC_OBSERVE_FLAG = (1 << 2)
727 } OCEntityHandlerFlag; //entity_handler_flag_t ;
730 * Possible returned values from client application.
734 OC_STACK_DELETE_TRANSACTION = 0,//!< OC_STACK_DELETE_TRANSACTION
735 OC_STACK_KEEP_TRANSACTION //!< OC_STACK_KEEP_TRANSACTION
736 } OCStackApplicationResult;
738 //-----------------------------------------------------------------------------
739 // Callback function definitions
740 //-----------------------------------------------------------------------------
743 * Client applications implement this callback to consume responses received from Servers.
745 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
746 OCClientResponse * clientResponse);
749 * Client applications using a context pointer implement this callback to delete the
750 * context upon removal of the callback/context pointer from the internal callback-list.
752 typedef void (* OCClientContextDeleter)(void *context);
755 * This info is passed from application to OC Stack when initiating a request to Server.
757 typedef struct OCCallbackData
760 /// The pointer to a function the stack will call to handle the requests
761 OCClientResponseHandler cb;
762 /// A pointer to a function to delete the context when this callback is removed
763 OCClientContextDeleter cd;
765 OCCallbackData() = default;
766 OCCallbackData(void* ctx, OCClientResponseHandler callback, OCClientContextDeleter deleter)
767 :context(ctx), cb(callback), cd(deleter){}
772 * Application server implementations must implement this callback to consume requests OTA.
773 * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
775 typedef OCEntityHandlerResult (*OCEntityHandler)
776 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void* callbackParam);
779 * Device Entity handler need to use this call back instead of OCEntityHandler.
781 typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
782 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri, void* callbackParam);
786 #endif // __cplusplus
788 #endif /* OCTYPES_H_ */