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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
31 #define USE_RANDOM_PORT (0)
33 //-----------------------------------------------------------------------------
34 // Function prototypes
35 //-----------------------------------------------------------------------------
38 * Initialize the OC Stack. Must be called prior to starting the stack.
41 * Host device is client, server, or client-server.
43 * Default server transport flags.
45 * Default client transport flags.
47 * @return ::OC_STACK_OK on success, some other value upon failure.
49 OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags);
52 * Initialize the OC Stack. Must be called prior to starting the stack.
55 * IP Address of host device. Deprecated parameter.
57 * Port of host device. Deprecated parameter.
59 * Host device is client, server, or client-server.
61 * @return ::OC_STACK_OK on success, some other value upon failure.
63 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
66 * Stop the OC stack. Use for a controlled shutdown.
68 * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
69 * all resources this server is hosting. OCDeleteResource() performs operations similar to
70 * OCNotifyAllObservers() to notify all client observers that the respective resource is being
73 * @return ::OC_STACK_OK on success, some other value upon failure.
75 OCStackResult OCStop();
78 * Called in main loop of OC client or server. Allows low-level processing of
81 * @return ::OC_STACK_OK on success, some other value upon failure.
83 OCStackResult OCProcess();
86 * Discover or Perform requests on a specified resource (specified by that Resource's respective
89 * @param handle @ref OCDoHandle to refer to the request sent out on behalf of
90 * calling this API. This handle can be used to cancel this operation
91 * via the OCCancel API.
92 * Note: This reference is handled internally, and
93 * should not be free'd by the consumer. A NULL handle is permitted
94 * in the event where the caller has no use for the return value.
95 * @param method @ref OCMethod to perform on the resource.
96 * @param requiredUri URI of the resource to interact with. (Address prefix
97 * is deprecated in favor of destination.)
98 * @param destination Complete description of destination.
99 * @param request JSON encoded request.
100 * @param connectivityType Modifier flags when destination is not given.
101 * @param qos Quality of service. Note that if this API is called on a uri with
102 * the well-known multicast IP address, the qos will be forced to
104 * since it is impractical to send other QOS levels on such addresses.
105 * @param cbData Asynchronous callback function that is invoked
106 * by the stack when discovery or resource interaction is complete.
107 * @param options The address of an array containing the vendor specific
108 * header options to be sent with the request.
109 * @param numOptions Number of header options to be included.
111 * Note: Presence subscription amendments (ie. adding additional resource type filters by calling
112 * this API again) require the use of the same base URI as the original request to successfully
113 * amend the presence filters.
115 * @return ::OC_STACK_OK on success, some other value upon failure.
117 OCStackResult OCDoResource(OCDoHandle *handle,
119 const char *requestUri,
120 const OCDevAddr *destination,
122 OCConnectivityType connectivityType,
123 OCQualityOfService qos,
124 OCCallbackData *cbData,
125 OCHeaderOption *options,
128 * Cancel a request associated with a specific @ref OCDoResource invocation.
130 * @param handle - Used to identify a specific OCDoResource invocation.
131 * @param qos - used to specify Quality of Service (read below for more info)
132 * @param options- used to specify vendor specific header options when sending
133 * explicit observe cancellation
134 * @param numOptions- Number of header options to be included
136 * @return ::OC_STACK_OK on success, some other value upon failure.
138 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
142 * @brief Register Persistent storage callback.
143 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
145 * OC_STACK_OK - No errors; Success
146 * OC_STACK_INVALID_PARAM - Invalid parameter
148 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler);
152 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
153 * presence notifications to clients via multicast. Once this API has been called with a success,
154 * clients may query for this server's presence and this server's stack will respond via multicast.
156 * Server can call this function when it comes online for the first time, or when it comes back
157 * online from offline mode, or when it re enters network.
159 * @param ttl Time To Live in seconds.
160 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
162 * If ttl is greater than OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be set to
163 * OC_MAX_PRESENCE_TTL_SECONDS.
165 * @return ::OC_STACK_OK on success, some other value upon failure.
167 OCStackResult OCStartPresence(const uint32_t ttl);
170 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
171 * presence notifications to clients via multicast. Once this API has been called with a success,
172 * this server's stack will not respond to clients querying for this server's presence.
174 * Server can call this function when it is terminating, going offline, or when going
177 * @return ::OC_STACK_OK on success, some other value upon failure.
180 OCStackResult OCStopPresence();
185 * Set default device entity handler.
187 * @param entityHandler Entity handler function that is called by ocstack to handle requests for
188 * any undefined resources or default actions.
189 * If NULL is passed it removes the device default entity handler.
190 * @param callbackParameter paramter passed back when entityHandler is called.
192 * @return ::OC_STACK_OK on success, some other value upon failure.
194 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler, void* callbackParameter);
197 * Set device information.
199 * @param deviceInfo - Structure passed by the server application containing
200 * the device information.
204 * OC_STACK_OK - no errors
205 * OC_STACK_INVALID_PARAM - invalid paramerter
206 * OC_STACK_ERROR - stack process error
208 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
211 * Set platform information.
213 * @param platformInfo - Structure passed by the server application containing
214 * the platform information.
218 * OC_STACK_OK - no errors
219 * OC_STACK_INVALID_PARAM - invalid paramerter
220 * OC_STACK_ERROR - stack process error
222 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo);
227 * @param handle Pointer to handle to newly created resource. Set by ocstack and
228 * used to refer to resource.
229 * @param resourceTypeName Name of resource type. Example: "core.led".
230 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
231 * @param uri URI of the resource. Example: "/a/led".
232 * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
233 * NULL for default entity handler.
234 * @param callbackParameter paramter passed back when entityHandler is called.
235 * @param resourceProperties Properties supported by resource.
236 * Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
238 * @return ::OC_STACK_OK on success, some other value upon failure.
240 OCStackResult OCCreateResource(OCResourceHandle *handle,
241 const char *resourceTypeName,
242 const char *resourceInterfaceName,
244 OCEntityHandler entityHandler,
246 uint8_t resourceProperties);
250 * Add a resource to a collection resource.
252 * @param collectionHandle Handle to the collection resource.
253 * @param resourceHandle Handle to resource to be added to the collection resource.
255 * @return ::OC_STACK_OK on success, some other value upon failure.
257 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
260 * Remove a resource from a collection resource.
262 * @param collectionHandle Handle to the collection resource.
263 * @param resourceHandle Handle to resource to be removed from the collection resource.
265 * @return ::OC_STACK_OK on success, some other value upon failure.
267 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
270 * Bind a resourcetype to a resource.
272 * @param handle Handle to the resource.
273 * @param resourceTypeName Name of resource type. Example: "core.led".
275 * @return ::OC_STACK_OK on success, some other value upon failure.
277 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
278 const char *resourceTypeName);
280 * Bind a resource interface to a resource.
282 * @param handle Handle to the resource.
283 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
285 * @return ::OC_STACK_OK on success, some other value upon failure.
287 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
288 const char *resourceInterfaceName);
291 * Bind an entity handler to the resource.
293 * @param handle Handle to the resource that the contained resource is to be bound.
294 * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
295 * @param callbackParameter context paremeter that will be passed to entityHandler
296 * @return ::OC_STACK_OK on success, some other value upon failure.
298 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler,
299 void *callbackParameter);
302 * Get the number of resources that have been created in the stack.
304 * @param numResources Pointer to count variable.
306 * @return ::OC_STACK_OK on success, some other value upon failure.
308 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
311 * Get a resource handle by index.
313 * @param index Index of resource, 0 to Count - 1.
315 * @return Found resource handle or NULL if not found.
317 OCResourceHandle OCGetResourceHandle(uint8_t index);
320 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
323 * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
324 * client observers that "this" resource is being deleted.
326 * @param handle Handle of resource to be deleted.
328 * @return ::OC_STACK_OK on success, some other value upon failure.
330 OCStackResult OCDeleteResource(OCResourceHandle handle);
333 * Get the URI of the resource specified by handle.
335 * @param handle Handle of resource.
336 * @return URI string if resource found or NULL if not found.
338 const char *OCGetResourceUri(OCResourceHandle handle);
341 * Get the properties of the resource specified by handle.
342 * NOTE: that after a resource is created, the OC_ACTIVE property is set
343 * for the resource by the stack.
345 * @param handle Handle of resource.
346 * @return OCResourceProperty Bitmask or -1 if resource is not found.
348 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
351 * Get the number of resource types of the resource.
353 * @param handle Handle of resource.
354 * @param numResourceTypes Pointer to count variable.
356 * @return ::OC_STACK_OK on success, some other value upon failure.
358 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
361 * Get name of resource type of the resource.
363 * @param handle Handle of resource.
364 * @param index Index of resource, 0 to Count - 1.
366 * @return Resource type name if resource found or NULL if resource not found.
368 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
371 * Get the number of resource interfaces of the resource.
373 * @param handle Handle of resource.
374 * @param numResourceInterfaces Pointer to count variable.
376 * @return ::OC_STACK_OK on success, some other value upon failure.
378 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
379 uint8_t *numResourceInterfaces);
382 * Get name of resource interface of the resource.
384 * @param handle Handle of resource.
385 * @param index Index of resource, 0 to Count - 1.
387 * @return Resource interface name if resource found or NULL if resource not found.
389 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
392 * Get methods of resource interface of the resource.
394 * @param handle Handle of resource.
395 * @param index Index of resource, 0 to Count - 1.
397 * @return Allowed methods if resource found or NULL if resource not found.
399 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
402 * Get resource handle from the collection resource by index.
404 * @param collectionHandle Handle of collection resource.
405 * @param index Index of contained resource, 0 to Count - 1.
407 * @return Handle to contained resource if resource found or NULL if resource not found.
409 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
413 * Get the entity handler for a resource.
415 * @param handle Handle of resource.
417 * @return Entity handler if resource found or NULL resource not found.
419 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
422 * Notify all registered observers that the resource representation has
423 * changed. If observation includes a query the client is notified only
424 * if the query is valid after the resource representation has changed.
426 * @param handle Handle of resource.
427 * @param qos Desired quality of service for the observation notifications.
429 * @return ::OC_STACK_OK on success, some other value upon failure.
431 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
434 * Notify specific observers with updated value of representation.
435 * Before this API is invoked by entity handler it has finished processing
436 * queries for the associated observers.
438 * @param handle Handle of resource.
439 * @param obsIdList List of observation ids that need to be notified.
440 * @param numberOfIds Number of observation ids included in obsIdList.
441 * @param payload OCRepresentationPayload object representing the notification
442 * @param qos Desired quality of service of the observation notifications.
443 * NOTE: The memory for obsIdList and notificationJSONPayload is managed by the
444 * entity invoking the API. The maximum size of the notification is 1015 bytes
445 * for non-Arduino platforms. For Arduino the maximum size is 247 bytes.
447 * @return ::OC_STACK_OK on success, some other value upon failure.
450 OCNotifyListOfObservers (OCResourceHandle handle,
451 OCObservationId *obsIdList,
453 const OCRepPayload *payload,
454 OCQualityOfService qos);
458 * Send a response to a request.
459 * The response can be a normal, slow, or block (i.e. a response that
460 * is too large to be sent in a single PDU and must span multiple transmissions).
462 * @param response Pointer to structure that contains response parameters.
464 * @return ::OC_STACK_OK on success, some other value upon failure.
466 OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
470 #endif // __cplusplus
472 #endif /* OCSTACK_H_ */