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 * IP Address of host device. Deprecated parameter.
43 * Port of host device. Deprecated parameter.
45 * Host device is client, server, or client-server.
47 * @return ::OC_STACK_OK on success, some other value upon failure.
49 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
52 * Stop the OC stack. Use for a controlled shutdown.
54 * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
55 * all resources this server is hosting. OCDeleteResource() performs operations similar to
56 * OCNotifyAllObservers() to notify all client observers that the respective resource is being
59 * @return ::OC_STACK_OK on success, some other value upon failure.
61 OCStackResult OCStop();
64 * Called in main loop of OC client or server. Allows low-level processing of
67 * @return ::OC_STACK_OK on success, some other value upon failure.
69 OCStackResult OCProcess();
72 * Discover or Perform requests on a specified resource (specified by that Resource's respective
75 * @param handle @ref OCDoHandle to refer to the request sent out on behalf of
76 * calling this API. This handle can be used to cancel this operation
77 * via the OCCancel API.
78 * Note: This reference is handled internally, and
79 * should not be free'd by the consumer. A NULL handle is permitted
80 * in the event where the caller has no use for the return value.
81 * @param method @ref OCMethod to perform on the resource.
82 * @param requiredUri URI of the resource to interact with.
83 * @param referenceUri URI of the reference resource.
84 * @param request JSON encoded request.
85 * @param conType @ref OCConnectivityType type of connectivity indicating the
86 * interface. Example: ::OC_WIFI, ::OC_ETHERNET, ::OC_ALL.
87 * @param qos Quality of service. Note that if this API is called on a uri with
88 * the well-known multicast IP address, the qos will be forced to
90 * since it is impractical to send other QOS levels on such addresses.
91 * @param cbData Asynchronous callback function that is invoked
92 * by the stack when discovery or resource interaction is complete.
93 * @param options The address of an array containing the vendor specific
94 * header options to be sent with the request.
95 * @param numOptions Number of header options to be included.
97 * Note: Presence subscription amendments (ie. adding additional resource type filters by calling
98 * this API again) require the use of the same base URI as the original request to successfully
99 * amend the presence filters.
101 * @return ::OC_STACK_OK on success, some other value upon failure.
103 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
104 const char *referenceUri, const char *request, OCConnectivityType conType,
105 OCQualityOfService qos, OCCallbackData *cbData,
106 OCHeaderOption * options, uint8_t numOptions);
109 * Cancel a request associated with a specific @ref OCDoResource invocation.
111 * @param handle - Used to identify a specific OCDoResource invocation.
112 * @param qos - used to specify Quality of Service (read below for more info)
113 * @param options- used to specify vendor specific header options when sending
114 * explicit observe cancellation
115 * @param numOptions- Number of header options to be included
117 * @return ::OC_STACK_OK on success, some other value upon failure.
119 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
123 * @brief Register Persistent storage callback.
124 * @param persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
126 * OC_STACK_OK - No errors; Success
127 * OC_STACK_INVALID_PARAM - Invalid parameter
129 OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler);
133 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
134 * presence notifications to clients via multicast. Once this API has been called with a success,
135 * clients may query for this server's presence and this server's stack will respond via multicast.
137 * Server can call this function when it comes online for the first time, or when it comes back
138 * online from offline mode, or when it re enters network.
140 * @param ttl Time To Live in seconds.
141 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
143 * If ttl is greater than OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be set to
144 * OC_MAX_PRESENCE_TTL_SECONDS.
146 * @return ::OC_STACK_OK on success, some other value upon failure.
148 OCStackResult OCStartPresence(const uint32_t ttl);
151 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
152 * presence notifications to clients via multicast. Once this API has been called with a success,
153 * this server's stack will not respond to clients querying for this server's presence.
155 * Server can call this function when it is terminating, going offline, or when going
158 * @return ::OC_STACK_OK on success, some other value upon failure.
161 OCStackResult OCStopPresence();
166 * Set default device entity handler.
168 * @param entityHandler Entity handler function that is called by ocstack to handle requests for
169 * any undefined resources or default actions.
170 * If NULL is passed it removes the device default entity handler.
171 * @param callbackParameter paramter passed back when entityHandler is called.
173 * @return ::OC_STACK_OK on success, some other value upon failure.
175 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler, void* callbackParameter);
178 * Set device information.
180 * @param deviceInfo - Structure passed by the server application containing
181 * the device information.
185 * OC_STACK_OK - no errors
186 * OC_STACK_INVALID_PARAM - invalid paramerter
187 * OC_STACK_ERROR - stack process error
189 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
192 * Set platform information.
194 * @param platformInfo - Structure passed by the server application containing
195 * the platform information.
199 * OC_STACK_OK - no errors
200 * OC_STACK_INVALID_PARAM - invalid paramerter
201 * OC_STACK_ERROR - stack process error
203 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo);
208 * @param handle Pointer to handle to newly created resource. Set by ocstack and
209 * used to refer to resource.
210 * @param resourceTypeName Name of resource type. Example: "core.led".
211 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
212 * @param uri URI of the resource. Example: "/a/led".
213 * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
214 * NULL for default entity handler.
215 * @param callbackParameter paramter passed back when entityHandler is called.
216 * @param resourceProperties Properties supported by resource.
217 * Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
219 * @return ::OC_STACK_OK on success, some other value upon failure.
221 OCStackResult OCCreateResource(OCResourceHandle *handle,
222 const char *resourceTypeName,
223 const char *resourceInterfaceName,
225 OCEntityHandler entityHandler,
227 uint8_t resourceProperties);
231 * Add a resource to a collection resource.
233 * @param collectionHandle Handle to the collection resource.
234 * @param resourceHandle Handle to resource to be added to the collection resource.
236 * @return ::OC_STACK_OK on success, some other value upon failure.
238 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
241 * Remove a resource from a collection resource.
243 * @param collectionHandle Handle to the collection resource.
244 * @param resourceHandle Handle to resource to be removed from the collection resource.
246 * @return ::OC_STACK_OK on success, some other value upon failure.
248 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
251 * Bind a resourcetype to a resource.
253 * @param handle Handle to the resource.
254 * @param resourceTypeName Name of resource type. Example: "core.led".
256 * @return ::OC_STACK_OK on success, some other value upon failure.
258 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
259 const char *resourceTypeName);
261 * Bind a resource interface to a resource.
263 * @param handle Handle to the resource.
264 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
266 * @return ::OC_STACK_OK on success, some other value upon failure.
268 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
269 const char *resourceInterfaceName);
272 * Bind an entity handler to the resource.
274 * @param handle Handle to the resource that the contained resource is to be bound.
275 * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
276 * @param callbackParameter context paremeter that will be passed to entityHandler
277 * @return ::OC_STACK_OK on success, some other value upon failure.
279 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler,
280 void *callbackParameter);
283 * Get the number of resources that have been created in the stack.
285 * @param numResources Pointer to count variable.
287 * @return ::OC_STACK_OK on success, some other value upon failure.
289 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
292 * Get a resource handle by index.
294 * @param index Index of resource, 0 to Count - 1.
296 * @return Found resource handle or NULL if not found.
298 OCResourceHandle OCGetResourceHandle(uint8_t index);
301 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
304 * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
305 * client observers that "this" resource is being deleted.
307 * @param handle Handle of resource to be deleted.
309 * @return ::OC_STACK_OK on success, some other value upon failure.
311 OCStackResult OCDeleteResource(OCResourceHandle handle);
314 * Get the URI of the resource specified by handle.
316 * @param handle Handle of resource.
317 * @return URI string if resource found or NULL if not found.
319 const char *OCGetResourceUri(OCResourceHandle handle);
322 * Get the properties of the resource specified by handle.
323 * NOTE: that after a resource is created, the OC_ACTIVE property is set
324 * for the resource by the stack.
326 * @param handle Handle of resource.
327 * @return OCResourceProperty Bitmask or -1 if resource is not found.
329 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
332 * Get the number of resource types of the resource.
334 * @param handle Handle of resource.
335 * @param numResourceTypes Pointer to count variable.
337 * @return ::OC_STACK_OK on success, some other value upon failure.
339 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
342 * Get name of resource type of the resource.
344 * @param handle Handle of resource.
345 * @param index Index of resource, 0 to Count - 1.
347 * @return Resource type name if resource found or NULL if resource not found.
349 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
352 * Get the number of resource interfaces of the resource.
354 * @param handle Handle of resource.
355 * @param numResourceInterfaces Pointer to count variable.
357 * @return ::OC_STACK_OK on success, some other value upon failure.
359 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
360 uint8_t *numResourceInterfaces);
363 * Get name of resource interface of the resource.
365 * @param handle Handle of resource.
366 * @param index Index of resource, 0 to Count - 1.
368 * @return Resource interface name if resource found or NULL if resource not found.
370 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
373 * Get methods of resource interface of the resource.
375 * @param handle Handle of resource.
376 * @param index Index of resource, 0 to Count - 1.
378 * @return Allowed methods if resource found or NULL if resource not found.
380 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
383 * Get resource handle from the collection resource by index.
385 * @param collectionHandle Handle of collection resource.
386 * @param index Index of contained resource, 0 to Count - 1.
388 * @return Handle to contained resource if resource found or NULL if resource not found.
390 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
394 * Get the entity handler for a resource.
396 * @param handle Handle of resource.
398 * @return Entity handler if resource found or NULL resource not found.
400 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
403 * Notify all registered observers that the resource representation has
404 * changed. If observation includes a query the client is notified only
405 * if the query is valid after the resource representation has changed.
407 * @param handle Handle of resource.
408 * @param qos Desired quality of service for the observation notifications.
410 * @return ::OC_STACK_OK on success, some other value upon failure.
412 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
415 * Notify specific observers with updated value of representation.
416 * Before this API is invoked by entity handler it has finished processing
417 * queries for the associated observers.
419 * @param handle Handle of resource.
420 * @param obsIdList List of observation ids that need to be notified.
421 * @param numberOfIds Number of observation ids included in obsIdList.
422 * @param notificationJSONPayload JSON encoded payload to send in notification.
423 * @param qos Desired quality of service of the observation notifications.
424 * NOTE: The memory for obsIdList and notificationJSONPayload is managed by the
425 * entity invoking the API. The maximum size of the notification is 1015 bytes
426 * for non-Arduino platforms. For Arduino the maximum size is 247 bytes.
428 * @return ::OC_STACK_OK on success, some other value upon failure.
431 OCNotifyListOfObservers (OCResourceHandle handle,
432 OCObservationId *obsIdList,
434 const char *notificationJSONPayload,
435 OCQualityOfService qos);
439 * Send a response to a request.
440 * The response can be a normal, slow, or block (i.e. a response that
441 * is too large to be sent in a single PDU and must span multiple transmissions).
443 * @param response Pointer to structure that contains response parameters.
445 * @return ::OC_STACK_OK on success, some other value upon failure.
447 OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
453 * This method is used to retrieved the IPv4 address from OCDev address
456 * @param ipAddr OCDevAddr address.
457 * @param a first byte of IPv4 address.
458 * @param b second byte of IPv4 address.
459 * @param c third byte of IPv4 address.
460 * @param d fourth byte of IPv4 address.
461 * @return ::OC_STACK_OK on success, some other value upon failure.
463 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
464 uint8_t *c, uint8_t *d );
467 * This method is used to retrieve the port number from OCDev address
470 * @param ipAddr OCDevAddr address.
471 * @param port Port number.
472 * @return ::OC_STACK_OK on success, some other value upon failure.
474 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
478 #endif // __cplusplus
480 #endif /* OCSTACK_H_ */