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 //-----------------------------------------------------------------------------
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.
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
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
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"
59 ///Separtor for multiple query string
60 #define OC_QUERY_SEPARATOR "&;"
63 * Attributes used to form a proper OIC conforming JSON message.
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"
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_POLICY "p"
87 #define OC_RSRVD_BITMAP "bm"
88 #define OC_RSRVD_SECURE "sec"
89 #define OC_RSRVD_HOSTING_PORT "port"
90 #define OC_RSRVD_SERVER_INSTANCE_ID "sid"
92 //**** Presence "Announcement Triggers" ****
93 #define OC_RSRVD_TRIGGER_CREATE "create"
94 #define OC_RSRVD_TRIGGER_CHANGE "change"
95 #define OC_RSRVD_TRIGGER_DELETE "delete"
99 #define OC_RSRVD_PLATFORM_ID "pi"
100 #define OC_RSRVD_MFG_NAME "mnmn"
101 #define OC_RSRVD_MFG_URL "mnml"
102 #define OC_RSRVD_MODEL_NUM "mnmo"
103 #define OC_RSRVD_MFG_DATE "mndt"
104 #define OC_RSRVD_PLATFORM_VERSION "mnpv"
105 #define OC_RSRVD_OS_VERSION "mnos"
106 #define OC_RSRVD_HARDWARE_VERSION "mnhw"
107 #define OC_RSRVD_FIRMWARE_VERSION "mnfv"
108 #define OC_RSRVD_SUPPORT_URL "mnsl"
109 #define OC_RSRVD_SYSTEM_TIME "st"
110 //*******************
113 #define OC_RSRVD_DEVICE_ID "di"
114 #define OC_RSRVD_DEVICE_NAME "n"
115 #define OC_RSRVD_SPEC_VERSION "lcv"
116 #define OC_RSRVD_DATA_MODEL_VERSION "dmv"
118 #define OC_SPEC_VERSION "0.9.0"
119 #define OC_DATA_MODEL_VERSION "sec.0.95"
120 //*******************
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
128 * Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
130 typedef struct OCDevAddr
132 uint32_t size; ///< length of the address stored in addr field.
133 uint8_t addr[DEV_ADDR_SIZE_MAX]; ///< device address.
137 * OC Virtual resources supported by every OC device.
141 OC_WELL_KNOWN_URI= 0, ///< "/oic/res"
142 OC_DEVICE_URI, ///< "/oic/d"
143 OC_PLATFORM_URI, ///< "/oic/p"
144 OC_RESOURCE_TYPES_URI, ///< "/oic/res/d/type"
146 OC_PRESENCE, ///< "/oic/ad"
148 OC_MAX_VIRTUAL_RESOURCES ///<s Max items in the list
149 } OCVirtualResources;
152 * Standard RESTful HTTP Methods.
156 OC_REST_NOMETHOD = 0,
157 OC_REST_GET = (1 << 0), ///< Read
158 OC_REST_PUT = (1 << 1), ///< Write
159 OC_REST_POST = (1 << 2), ///< Update
160 OC_REST_DELETE = (1 << 3), ///< Delete
161 /// Register observe request for most up date notifications ONLY.
162 OC_REST_OBSERVE = (1 << 4),
163 /// Register observe request for all notifications, including stale notifications.
164 OC_REST_OBSERVE_ALL = (1 << 5),
165 /// Deregister observation, intended for internal use
166 OC_REST_CANCEL_OBSERVE = (1 << 6),
168 /// Subscribe for all presence notifications of a particular resource.
169 OC_REST_PRESENCE = (1 << 7)
174 * Host Mode of Operation.
184 * Quality of Service.
186 * OCQualityOfService attempts to abstract the guarantees provided by the underlying transport
187 * protocol. The precise definitions of each quality of service level depend on the
188 * implementation. In descriptions below are for the current implementation and may changed
193 /// Packet delivery is best effort
195 /// Packet delivery is best effort
197 /// Acknowledgements are used to confirm delivery
199 /// No Quality is defined, let the stack decide
201 } OCQualityOfService;
204 * Resource Properties.
205 * The value of a policy property is defined as bitmap.
206 * The LSB represents OC_DISCOVERABLE and Second LSB bit represents OC_OBSERVABLE and so on.
207 * Not including the policy property is equivalent to zero.
209 * ::OC_RES_PROP_NONE When none of the bits are set, the resource is non-discoverable &
210 * non-observable by the client.
211 * ::OC_DISCOVERABLE When this bit is set, the resource is allowed to be discovered by clients.
212 * ::OC_OBSERVABLE When this bit is set, the resource is allowed to be observed by clients.
213 * ::OC_ACTIVE When this bit is set, the resource is initialized, otherwise the resource
214 * is 'inactive'. 'inactive' signifies that the resource has been marked for
215 * deletion or is already deleted.
216 * ::OC_SLOW When this bit is set, the resource has been marked as 'slow'. 'slow'
217 * signifies that responses from this resource can expect delays in
218 * processing its requests from clients.
219 * ::OC_SECURE When this bit is set, the resource is a secure resource.
223 OC_RES_PROP_NONE = (0),
224 OC_DISCOVERABLE = (1 << 0),
225 OC_OBSERVABLE = (1 << 1),
226 OC_ACTIVE = (1 << 2),
229 } OCResourceProperty;
232 * Transport Protocol IDs.
236 OC_INVALID_ID = (1 << 0),
237 OC_COAP_ID = (1 << 1)
238 } OCTransportProtocolID;
249 OC_ALL // Multicast message: send over all the interfaces.
250 } OCConnectivityType;
253 * Declares Stack Results & Errors.
257 /* Success status code - START HERE */
259 OC_STACK_RESOURCE_CREATED,
260 OC_STACK_RESOURCE_DELETED,
262 /* Success status code - END HERE */
263 /* Error status code - START HERE */
264 OC_STACK_INVALID_URI = 20,
265 OC_STACK_INVALID_QUERY,
267 OC_STACK_INVALID_PORT,
268 OC_STACK_INVALID_CALLBACK,
269 OC_STACK_INVALID_METHOD,
270 OC_STACK_INVALID_PARAM,
271 OC_STACK_INVALID_OBSERVE_PARAM,
275 OC_STACK_ADAPTER_NOT_ENABLED,
277 OC_STACK_NO_RESOURCE, /* resource not found */
278 OC_STACK_RESOURCE_ERROR, /* ex: not supported method or interface */
279 OC_STACK_SLOW_RESOURCE,
280 OC_STACK_DUPLICATE_REQUEST,
281 OC_STACK_NO_OBSERVERS, /* resource has no registered observers */
282 OC_STACK_OBSERVER_NOT_FOUND,
283 OC_STACK_VIRTUAL_DO_NOT_HANDLE,
284 OC_STACK_INVALID_OPTION,
285 OC_STACK_MALFORMED_RESPONSE, /* the remote reply contained malformed data */
286 OC_STACK_PERSISTENT_BUFFER_REQUIRED,
287 OC_STACK_INVALID_REQUEST_HANDLE,
288 OC_STACK_INVALID_DEVICE_INFO,
289 OC_STACK_INVALID_JSON,
290 /* NOTE: Insert all new error codes here!*/
292 OC_STACK_PRESENCE_STOPPED = 128,
293 OC_STACK_PRESENCE_TIMEOUT,
294 OC_STACK_PRESENCE_DO_NOT_HANDLE,
297 /* Error status code - END HERE */
301 * Handle to an @ref OCDoResource invocation.
303 typedef void * OCDoHandle;
306 * Handle to an OCResource object owned by the OCStack.
308 typedef void * OCResourceHandle;
310 typedef void * OCRequestHandle;
313 * Unique identifier for each observation request. Used when observations are
314 * registered or deregistering. Used by entity handler to signal specific
315 * observers to be notified of resource changes.
316 * There can be maximum of 256 observations per server.
318 typedef uint8_t OCObservationId;
321 * Action associated with observation.
325 OC_OBSERVE_REGISTER = 0,
326 OC_OBSERVE_DEREGISTER = 1,
327 OC_OBSERVE_NO_OPTION = 2
332 * Persistent storage handlers. An app must provide OCPersistentStorage handler pointers when it
333 * calls OCRegisterPersistentStorageHandler.
337 * Persistent storage open handler points to default file path.
338 * Application can point to appropriate SVR database path for its Iotivity Server.
340 FILE* (* open)(const char *path, const char *mode);
341 // Persistent storage read handler
342 size_t (* read)(void *ptr, size_t size, size_t nmemb, FILE *stream);
343 // Persistent storage write handler
344 size_t (* write)(const void *ptr, size_t size, size_t nmemb, FILE *stream);
345 // Persistent storage close handler
346 int (* close)(FILE *fp);
347 // Persistent storage unlink handler
348 int (* unlink)(const char *path);
349 } OCPersistentStorage;
353 // Action associated with observation request
354 OCObserveAction action;
355 // Identifier for observation being registered/deregistered
356 OCObservationId obsId;
360 * Possible returned values from entity handler.
366 OC_EH_RESOURCE_CREATED,
367 OC_EH_RESOURCE_DELETED,
370 OC_EH_RESOURCE_NOT_FOUND
371 } OCEntityHandlerResult;
374 * This structure will be used to define the vendor specific header options to be included
375 * in communication packets.
377 typedef struct OCHeaderOption
379 // The protocol ID this option applies to
380 OCTransportProtocolID protocolID;
381 // The header option ID which will be added to communication packets
384 uint16_t optionLength;
385 // pointer to its data
386 uint8_t optionData[MAX_HEADER_OPTION_DATA_LENGTH];
389 OCHeaderOption() = default;
390 OCHeaderOption(OCTransportProtocolID pid,
393 const uint8_t* optData)
399 // parameter includes the null terminator.
400 optionLength = optionLength < MAX_HEADER_OPTION_DATA_LENGTH ?
401 optionLength : MAX_HEADER_OPTION_DATA_LENGTH;
402 memcpy(optionData, optData, optionLength);
403 optionData[optionLength - 1] = '\0';
409 * Incoming requests handled by the server. Requests are passed in as a parameter to the
410 * @ref OCEntityHandler callback API.
411 * @brief The @ref OCEntityHandler callback API must be implemented in the application in order
412 * to receive these requests.
416 // Associated resource
417 OCResourceHandle resource;
418 OCRequestHandle requestHandle;
419 // the REST method retrieved from received request PDU
421 // resource query send by client
423 // Information associated with observation - valid only when OCEntityHandler
424 // flag includes OC_OBSERVE_FLAG
425 OCObservationInfo obsInfo;
426 // An array of the received vendor specific header options
427 uint8_t numRcvdVendorSpecificHeaderOptions;
428 OCHeaderOption * rcvdVendorSpecificHeaderOptions;
429 // reqJSON is retrieved from the payload of the received request PDU
430 char * reqJSONPayload;
431 } OCEntityHandlerRequest;
434 * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API.
438 /// Address of remote server
440 /// Indicates adaptor type on which the response was received
441 OCConnectivityType connType;
442 /// the is the result of our stack, OCStackResult should contain coap/other error codes;
443 OCStackResult result;
444 /// If associated with observe, this will represent the sequence of notifications from server.
445 uint32_t sequenceNumber;
446 /// resJSONPayload is retrieved from the payload of the received request PDU
447 const char * resJSONPayload;
448 /// An array of the received vendor specific header options
449 uint8_t numRcvdVendorSpecificHeaderOptions;
450 OCHeaderOption rcvdVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
454 * This structure describes the platform properties. All non-Null properties will be included
455 * in a platform discovery request.
460 char *manufacturerName;
461 char *manufacturerUrl;
463 char *dateOfManufacture;
464 char *platformVersion;
465 char *operatingSystemVersion;
466 char *hardwareVersion;
467 char *firmwareVersion;
474 * This structure is expected as input for device properties.
475 * device name is mandatory and expected from the application.
476 * device id of type UUID will be generated by the stack.
486 // Request handle is passed to server via the entity handler for each incoming request.
487 // Stack assigns when request is received, server sets to indicate what request response is for
488 OCRequestHandle requestHandle;
490 OCResourceHandle resourceHandle;
491 // Allow the entity handler to pass a result with the response
492 OCEntityHandlerResult ehResult;
493 // this is the pointer to server payload data to be transferred
495 // size of server payload data. I don't think we should rely on null terminated data for size
496 uint16_t payloadSize;
497 // An array of the vendor specific header options the entity handler wishes to use in response
498 uint8_t numSendVendorSpecificHeaderOptions;
499 OCHeaderOption sendVendorSpecificHeaderOptions[MAX_HEADER_OPTIONS];
500 // URI of new resource that entity handler might create
501 char resourceUri[MAX_URI_LENGTH];
502 // Server sets to true for persistent response buffer, false for non-persistent response buffer
503 uint8_t persistentBufferFlag;
504 } OCEntityHandlerResponse;
508 OC_REQUEST_FLAG = (1 << 1),
509 OC_OBSERVE_FLAG = (1 << 2)
510 } OCEntityHandlerFlag; //entity_handler_flag_t ;
513 * Possible returned values from client application.
517 OC_STACK_DELETE_TRANSACTION = 0,//!< OC_STACK_DELETE_TRANSACTION
518 OC_STACK_KEEP_TRANSACTION //!< OC_STACK_KEEP_TRANSACTION
519 } OCStackApplicationResult;
521 //-----------------------------------------------------------------------------
522 // Callback function definitions
523 //-----------------------------------------------------------------------------
526 * Client applications implement this callback to consume responses received from Servers.
528 typedef OCStackApplicationResult (* OCClientResponseHandler)(void *context, OCDoHandle handle,
529 OCClientResponse * clientResponse);
532 * Client applications using a context pointer implement this callback to delete the
533 * context upon removal of the callback/context pointer from the internal callback-list.
535 typedef void (* OCClientContextDeleter)(void *context);
538 * This info is passed from application to OC Stack when initiating a request to Server.
540 typedef struct OCCallbackData
543 /// The pointer to a function the stack will call to handle the requests
544 OCClientResponseHandler cb;
545 /// A pointer to a function to delete the context when this callback is removed
546 OCClientContextDeleter cd;
548 OCCallbackData() = default;
549 OCCallbackData(void* ctx, OCClientResponseHandler callback, OCClientContextDeleter deleter)
550 :context(ctx), cb(callback), cd(deleter){}
555 * Application server implementations must implement this callback to consume requests OTA.
556 * Entity handler callback needs to fill the resPayload of the entityHandlerRequest.
558 typedef OCEntityHandlerResult (*OCEntityHandler)
559 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, void* callbackParam);
562 * Device Entity handler need to use this call back instead of OCEntityHandler.
564 typedef OCEntityHandlerResult (*OCDeviceEntityHandler)
565 (OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest, char* uri, void* callbackParam);
569 #endif // __cplusplus
571 #endif /* OCTYPES_H_ */