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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
30 #define USE_RANDOM_PORT (0)
32 //-----------------------------------------------------------------------------
33 // Function prototypes
34 //-----------------------------------------------------------------------------
37 * Initialize the OC Stack. Must be called prior to starting the stack.
40 * IP Address of host device. Deprecated parameter.
42 * Port of host device. Deprecated parameter.
44 * Host device is client, server, or client-server.
46 * @return ::OC_STACK_OK on success, some other value upon failure.
48 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode);
51 * Stop the OC stack. Use for a controlled shutdown.
53 * Note: OCStop() performs operations similar to OCStopPresence(), as well as OCDeleteResource() on
54 * all resources this server is hosting. OCDeleteResource() performs operations similar to
55 * OCNotifyAllObservers() to notify all client observers that the respective resource is being
58 * @return ::OC_STACK_OK on success, some other value upon failure.
60 OCStackResult OCStop();
63 * Called in main loop of OC client or server. Allows low-level processing of
66 * @return ::OC_STACK_OK on success, some other value upon failure.
68 OCStackResult OCProcess();
71 * Discover or Perform requests on a specified resource (specified by that Resource's respective
74 * @param handle @ref OCDoHandle to refer to the request sent out on behalf of
75 * calling this API. This handle can be used to cancel this operation
76 * via the OCCancel API.
77 * Note: This reference is handled internally, and
78 * should not be free'd by the consumer. A NULL handle is permitted
79 * in the event where the caller has no use for the return value.
80 * @param method @ref OCMethod to perform on the resource.
81 * @param requiredUri URI of the resource to interact with.
82 * @param referenceUri URI of the reference resource.
83 * @param request JSON encoded request.
84 * @param conType @ref OCConnectivityType type of connectivity indicating the
85 * interface. Example: ::OC_WIFI, ::OC_ETHERNET, ::OC_ALL.
86 * @param qos Quality of service. Note that if this API is called on a uri with
87 * the well-known multicast IP address, the qos will be forced to
89 * since it is impractical to send other QOS levels on such addresses.
90 * @param cbData Asynchronous callback function that is invoked
91 * by the stack when discovery or resource interaction is complete.
92 * @param options The address of an array containing the vendor specific
93 * header options to be sent with the request.
94 * @param numOptions Number of header options to be included.
96 * Note: Presence subscription amendments (ie. adding additional resource type filters by calling
97 * this API again) require the use of the same base URI as the original request to successfully
98 * amend the presence filters.
100 * @return ::OC_STACK_OK on success, some other value upon failure.
102 OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
103 const char *referenceUri, const char *request, OCConnectivityType conType,
104 OCQualityOfService qos, OCCallbackData *cbData,
105 OCHeaderOption * options, uint8_t numOptions);
108 * Cancel a request associated with a specific @ref OCDoResource invocation.
110 * @param handle - Used to identify a specific OCDoResource invocation.
111 * @param qos - used to specify Quality of Service (read below for more info)
112 * @param options- used to specify vendor specific header options when sending
113 * explicit observe cancellation
114 * @param numOptions- Number of header options to be included
116 * @return ::OC_STACK_OK on success, some other value upon failure.
118 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
123 * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
124 * presence notifications to clients via multicast. Once this API has been called with a success,
125 * clients may query for this server's presence and this server's stack will respond via multicast.
127 * Server can call this function when it comes online for the first time, or when it comes back
128 * online from offline mode, or when it re enters network.
130 * @param ttl Time To Live in seconds.
131 * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
133 * If ttl is greater than OC_MAX_PRESENCE_TTL_SECONDS, then the ttl will be set to
134 * OC_MAX_PRESENCE_TTL_SECONDS.
136 * @return ::OC_STACK_OK on success, some other value upon failure.
138 OCStackResult OCStartPresence(const uint32_t ttl);
141 * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
142 * presence notifications to clients via multicast. Once this API has been called with a success,
143 * this server's stack will not respond to clients querying for this server's presence.
145 * Server can call this function when it is terminating, going offline, or when going
148 * @return ::OC_STACK_OK on success, some other value upon failure.
151 OCStackResult OCStopPresence();
156 * Set default device entity handler.
158 * @param entityHandler Entity handler function that is called by ocstack to handle requests for
159 * any undefined resources or default actions.
160 * If NULL is passed it removes the device default entity handler.
162 * @return ::OC_STACK_OK on success, some other value upon failure.
164 OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler);
167 * Set device information.
169 * @param deviceInfo - Structure passed by the server application containing
170 * the device information.
174 * OC_STACK_OK - no errors
175 * OC_STACK_INVALID_PARAM - invalid paramerter
176 * OC_STACK_ERROR - stack process error
178 OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo);
181 * Set platform information.
183 * @param platformInfo - Structure passed by the server application containing
184 * the platform information.
188 * OC_STACK_OK - no errors
189 * OC_STACK_INVALID_PARAM - invalid paramerter
190 * OC_STACK_ERROR - stack process error
192 OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo);
197 * @param handle Pointer to handle to newly created resource. Set by ocstack and
198 * used to refer to resource.
199 * @param resourceTypeName Name of resource type. Example: "core.led".
200 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
201 * @param uri URI of the resource. Example: "/a/led".
202 * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
203 * NULL for default entity handler.
204 * @param resourceProperties Properties supported by resource.
205 * Example: ::OC_DISCOVERABLE|::OC_OBSERVABLE.
207 * @return ::OC_STACK_OK on success, some other value upon failure.
209 OCStackResult OCCreateResource(OCResourceHandle *handle,
210 const char *resourceTypeName,
211 const char *resourceInterfaceName,
213 OCEntityHandler entityHandler,
214 uint8_t resourceProperties);
218 * Add a resource to a collection resource.
220 * @param collectionHandle Handle to the collection resource.
221 * @param resourceHandle Handle to resource to be added to the collection resource.
223 * @return ::OC_STACK_OK on success, some other value upon failure.
225 OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
228 * Remove a resource from a collection resource.
230 * @param collectionHandle Handle to the collection resource.
231 * @param resourceHandle Handle to resource to be removed from the collection resource.
233 * @return ::OC_STACK_OK on success, some other value upon failure.
235 OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle);
238 * Bind a resourcetype to a resource.
240 * @param handle Handle to the resource.
241 * @param resourceTypeName Name of resource type. Example: "core.led".
243 * @return ::OC_STACK_OK on success, some other value upon failure.
245 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
246 const char *resourceTypeName);
248 * Bind a resource interface to a resource.
250 * @param handle Handle to the resource.
251 * @param resourceInterfaceName Name of resource interface. Example: "core.rw".
253 * @return ::OC_STACK_OK on success, some other value upon failure.
255 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
256 const char *resourceInterfaceName);
259 * Bind an entity handler to the resource.
261 * @param handle Handle to the resource that the contained resource is to be bound.
262 * @param entityHandler Entity handler function that is called by ocstack to handle requests, etc.
263 * @return ::OC_STACK_OK on success, some other value upon failure.
265 OCStackResult OCBindResourceHandler(OCResourceHandle handle, OCEntityHandler entityHandler);
268 * Get the number of resources that have been created in the stack.
270 * @param numResources Pointer to count variable.
272 * @return ::OC_STACK_OK on success, some other value upon failure.
274 OCStackResult OCGetNumberOfResources(uint8_t *numResources);
277 * Get a resource handle by index.
279 * @param index Index of resource, 0 to Count - 1.
281 * @return Found resource handle or NULL if not found.
283 OCResourceHandle OCGetResourceHandle(uint8_t index);
286 * Delete resource specified by handle. Deletes resource and all resourcetype and resourceinterface
289 * Note: OCDeleteResource() performs operations similar to OCNotifyAllObservers() to notify all
290 * client observers that "this" resource is being deleted.
292 * @param handle Handle of resource to be deleted.
294 * @return ::OC_STACK_OK on success, some other value upon failure.
296 OCStackResult OCDeleteResource(OCResourceHandle handle);
299 * Get the URI of the resource specified by handle.
301 * @param handle Handle of resource.
302 * @return URI string if resource found or NULL if not found.
304 const char *OCGetResourceUri(OCResourceHandle handle);
307 * Get the properties of the resource specified by handle.
308 * NOTE: that after a resource is created, the OC_ACTIVE property is set
309 * for the resource by the stack.
311 * @param handle Handle of resource.
312 * @return OCResourceProperty Bitmask or -1 if resource is not found.
314 OCResourceProperty OCGetResourceProperties(OCResourceHandle handle);
317 * Get the number of resource types of the resource.
319 * @param handle Handle of resource.
320 * @param numResourceTypes Pointer to count variable.
322 * @return ::OC_STACK_OK on success, some other value upon failure.
324 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle, uint8_t *numResourceTypes);
327 * Get name of resource type of the resource.
329 * @param handle Handle of resource.
330 * @param index Index of resource, 0 to Count - 1.
332 * @return Resource type name if resource found or NULL if resource not found.
334 const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index);
337 * Get the number of resource interfaces of the resource.
339 * @param handle Handle of resource.
340 * @param numResourceInterfaces Pointer to count variable.
342 * @return ::OC_STACK_OK on success, some other value upon failure.
344 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
345 uint8_t *numResourceInterfaces);
348 * Get name of resource interface of the resource.
350 * @param handle Handle of resource.
351 * @param index Index of resource, 0 to Count - 1.
353 * @return Resource interface name if resource found or NULL if resource not found.
355 const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index);
358 * Get methods of resource interface of the resource.
360 * @param handle Handle of resource.
361 * @param index Index of resource, 0 to Count - 1.
363 * @return Allowed methods if resource found or NULL if resource not found.
365 uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index);
368 * Get resource handle from the collection resource by index.
370 * @param collectionHandle Handle of collection resource.
371 * @param index Index of contained resource, 0 to Count - 1.
373 * @return Handle to contained resource if resource found or NULL if resource not found.
375 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
379 * Get the entity handler for a resource.
381 * @param handle Handle of resource.
383 * @return Entity handler if resource found or NULL resource not found.
385 OCEntityHandler OCGetResourceHandler(OCResourceHandle handle);
388 * Notify all registered observers that the resource representation has
389 * changed. If observation includes a query the client is notified only
390 * if the query is valid after the resource representation has changed.
392 * @param handle Handle of resource.
393 * @param qos Desired quality of service for the observation notifications.
395 * @return ::OC_STACK_OK on success, some other value upon failure.
397 OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos);
400 * Notify specific observers with updated value of representation.
401 * Before this API is invoked by entity handler it has finished processing
402 * queries for the associated observers.
404 * @param handle Handle of resource.
405 * @param obsIdList List of observation ids that need to be notified.
406 * @param numberOfIds Number of observation ids included in obsIdList.
407 * @param notificationJSONPayload JSON encoded payload to send in notification.
408 * @param qos Desired quality of service of the observation notifications.
409 * NOTE: The memory for obsIdList and notificationJSONPayload is managed by the
410 * entity invoking the API. The maximum size of the notification is 1015 bytes
411 * for non-Arduino platforms. For Arduino the maximum size is 247 bytes.
413 * @return ::OC_STACK_OK on success, some other value upon failure.
416 OCNotifyListOfObservers (OCResourceHandle handle,
417 OCObservationId *obsIdList,
419 const char *notificationJSONPayload,
420 OCQualityOfService qos);
424 * Send a response to a request.
425 * The response can be a normal, slow, or block (i.e. a response that
426 * is too large to be sent in a single PDU and must span multiple transmissions).
428 * @param response Pointer to structure that contains response parameters.
430 * @return ::OC_STACK_OK on success, some other value upon failure.
432 OCStackResult OCDoResponse(OCEntityHandlerResponse *response);
438 * This method is used to retrieved the IPv4 address from OCDev address
441 * @param ipAddr OCDevAddr address.
442 * @param a first byte of IPv4 address.
443 * @param b second byte of IPv4 address.
444 * @param c third byte of IPv4 address.
445 * @param d fourth byte of IPv4 address.
446 * @return ::OC_STACK_OK on success, some other value upon failure.
448 int32_t OCDevAddrToIPv4Addr(OCDevAddr *ipAddr, uint8_t *a, uint8_t *b,
449 uint8_t *c, uint8_t *d );
452 * This method is used to retrieve the port number from OCDev address
455 * @param ipAddr OCDevAddr address.
456 * @param port Port number.
457 * @return ::OC_STACK_OK on success, some other value upon failure.
459 int32_t OCDevAddrToPort(OCDevAddr *ipAddr, uint16_t *port);
463 #endif // __cplusplus
465 #endif /* OCSTACK_H_ */